gdb/
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
0b302171 3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
c906108c
SS
33#include "symtab.h"
34#include "gdbtypes.h"
c906108c 35#include "objfiles.h"
fa8f86ff 36#include "dwarf2.h"
c906108c
SS
37#include "buildsym.h"
38#include "demangle.h"
50f182aa 39#include "gdb-demangle.h"
c906108c 40#include "expression.h"
d5166ae1 41#include "filenames.h" /* for DOSish file names */
2e276125 42#include "macrotab.h"
c906108c
SS
43#include "language.h"
44#include "complaints.h"
357e46e7 45#include "bcache.h"
4c2df51b
DJ
46#include "dwarf2expr.h"
47#include "dwarf2loc.h"
9219021c 48#include "cp-support.h"
72bf9492 49#include "hashtab.h"
ae038cb0
DJ
50#include "command.h"
51#include "gdbcmd.h"
edb3359d 52#include "block.h"
ff013f42 53#include "addrmap.h"
94af9270
KS
54#include "typeprint.h"
55#include "jv-lang.h"
ccefe4c4 56#include "psympriv.h"
9291a0cd
TT
57#include "exceptions.h"
58#include "gdb_stat.h"
96d19272 59#include "completer.h"
34eaf542 60#include "vec.h"
98bfdba5 61#include "c-lang.h"
a766d390 62#include "go-lang.h"
98bfdba5 63#include "valprint.h"
3019eac3 64#include "gdbcore.h" /* for gnutarget */
156942c7 65#include "gdb/gdb-index.h"
60d5a603 66#include <ctype.h>
4c2df51b 67
c906108c
SS
68#include <fcntl.h>
69#include "gdb_string.h"
4bdf3d34 70#include "gdb_assert.h"
c906108c 71#include <sys/types.h>
233a11ab
CS
72#ifdef HAVE_ZLIB_H
73#include <zlib.h>
74#endif
dce234bc
PP
75#ifdef HAVE_MMAP
76#include <sys/mman.h>
85d9bd0e
TT
77#ifndef MAP_FAILED
78#define MAP_FAILED ((void *) -1)
79#endif
dce234bc 80#endif
d8151005 81
34eaf542
TT
82typedef struct symbol *symbolp;
83DEF_VEC_P (symbolp);
84
45cfd468
DE
85/* When non-zero, print basic high level tracing messages.
86 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
87static int dwarf2_read_debug = 0;
88
d97bc12b
DE
89/* When non-zero, dump DIEs after they are read in. */
90static int dwarf2_die_debug = 0;
91
900e11f9
JK
92/* When non-zero, cross-check physname against demangler. */
93static int check_physname = 0;
94
481860b3
GB
95/* When non-zero, do not reject deprecated .gdb_index sections. */
96int use_deprecated_index_sections = 0;
97
dce234bc
PP
98static int pagesize;
99
df8a16a1
DJ
100/* When set, the file that we're processing is known to have debugging
101 info for C++ namespaces. GCC 3.3.x did not produce this information,
102 but later versions do. */
103
104static int processing_has_namespace_info;
105
6502dd73
DJ
106static const struct objfile_data *dwarf2_objfile_data_key;
107
dce234bc
PP
108struct dwarf2_section_info
109{
110 asection *asection;
111 gdb_byte *buffer;
112 bfd_size_type size;
b315ab21
TG
113 /* Not NULL if the section was actually mmapped. */
114 void *map_addr;
115 /* Page aligned size of mmapped area. */
116 bfd_size_type map_len;
be391dca
TT
117 /* True if we have tried to read this section. */
118 int readin;
dce234bc
PP
119};
120
8b70b953
TT
121typedef struct dwarf2_section_info dwarf2_section_info_def;
122DEF_VEC_O (dwarf2_section_info_def);
123
9291a0cd
TT
124/* All offsets in the index are of this type. It must be
125 architecture-independent. */
126typedef uint32_t offset_type;
127
128DEF_VEC_I (offset_type);
129
156942c7
DE
130/* Ensure only legit values are used. */
131#define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
132 do { \
133 gdb_assert ((unsigned int) (value) <= 1); \
134 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
135 } while (0)
136
137/* Ensure only legit values are used. */
138#define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
139 do { \
140 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
141 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
142 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
143 } while (0)
144
145/* Ensure we don't use more than the alloted nuber of bits for the CU. */
146#define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
147 do { \
148 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
149 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
150 } while (0)
151
9291a0cd
TT
152/* A description of the mapped index. The file format is described in
153 a comment by the code that writes the index. */
154struct mapped_index
155{
559a7a62
JK
156 /* Index data format version. */
157 int version;
158
9291a0cd
TT
159 /* The total length of the buffer. */
160 off_t total_size;
b11b1f88 161
9291a0cd
TT
162 /* A pointer to the address table data. */
163 const gdb_byte *address_table;
b11b1f88 164
9291a0cd
TT
165 /* Size of the address table data in bytes. */
166 offset_type address_table_size;
b11b1f88 167
3876f04e
DE
168 /* The symbol table, implemented as a hash table. */
169 const offset_type *symbol_table;
b11b1f88 170
9291a0cd 171 /* Size in slots, each slot is 2 offset_types. */
3876f04e 172 offset_type symbol_table_slots;
b11b1f88 173
9291a0cd
TT
174 /* A pointer to the constant pool. */
175 const char *constant_pool;
176};
177
95554aad
TT
178typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
179DEF_VEC_P (dwarf2_per_cu_ptr);
180
9cdd5dbd
DE
181/* Collection of data recorded per objfile.
182 This hangs off of dwarf2_objfile_data_key. */
183
6502dd73
DJ
184struct dwarf2_per_objfile
185{
dce234bc
PP
186 struct dwarf2_section_info info;
187 struct dwarf2_section_info abbrev;
188 struct dwarf2_section_info line;
dce234bc
PP
189 struct dwarf2_section_info loc;
190 struct dwarf2_section_info macinfo;
cf2c3c16 191 struct dwarf2_section_info macro;
dce234bc
PP
192 struct dwarf2_section_info str;
193 struct dwarf2_section_info ranges;
3019eac3 194 struct dwarf2_section_info addr;
dce234bc
PP
195 struct dwarf2_section_info frame;
196 struct dwarf2_section_info eh_frame;
9291a0cd 197 struct dwarf2_section_info gdb_index;
ae038cb0 198
8b70b953
TT
199 VEC (dwarf2_section_info_def) *types;
200
be391dca
TT
201 /* Back link. */
202 struct objfile *objfile;
203
d467dd73 204 /* Table of all the compilation units. This is used to locate
10b3939b 205 the target compilation unit of a particular reference. */
ae038cb0
DJ
206 struct dwarf2_per_cu_data **all_comp_units;
207
208 /* The number of compilation units in ALL_COMP_UNITS. */
209 int n_comp_units;
210
1fd400ff 211 /* The number of .debug_types-related CUs. */
d467dd73 212 int n_type_units;
1fd400ff 213
d467dd73
DE
214 /* The .debug_types-related CUs (TUs). */
215 struct dwarf2_per_cu_data **all_type_units;
1fd400ff 216
ae038cb0
DJ
217 /* A chain of compilation units that are currently read in, so that
218 they can be freed later. */
219 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 220
348e048f
DE
221 /* A table mapping .debug_types signatures to its signatured_type entry.
222 This is NULL if the .debug_types section hasn't been read in yet. */
223 htab_t signatured_types;
224
3019eac3
DE
225 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
226 This is NULL if the table hasn't been allocated yet. */
227 htab_t dwo_files;
228
72dca2f5
FR
229 /* A flag indicating wether this objfile has a section loaded at a
230 VMA of 0. */
231 int has_section_at_zero;
9291a0cd 232
ae2de4f8
DE
233 /* True if we are using the mapped index,
234 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
235 unsigned char using_index;
236
ae2de4f8 237 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 238 struct mapped_index *index_table;
98bfdba5 239
7b9f3c50
DE
240 /* When using index_table, this keeps track of all quick_file_names entries.
241 TUs can share line table entries with CUs or other TUs, and there can be
242 a lot more TUs than unique line tables, so we maintain a separate table
243 of all line table entries to support the sharing. */
244 htab_t quick_file_names_table;
245
98bfdba5
PA
246 /* Set during partial symbol reading, to prevent queueing of full
247 symbols. */
248 int reading_partial_symbols;
673bfd45 249
dee91e82 250 /* Table mapping type DIEs to their struct type *.
673bfd45 251 This is NULL if not allocated yet.
dee91e82
DE
252 The mapping is done via (CU/TU signature + DIE offset) -> type. */
253 htab_t die_type_hash;
95554aad
TT
254
255 /* The CUs we recently read. */
256 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
257};
258
259static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 260
251d32d9 261/* Default names of the debugging sections. */
c906108c 262
233a11ab
CS
263/* Note that if the debugging section has been compressed, it might
264 have a name like .zdebug_info. */
265
9cdd5dbd
DE
266static const struct dwarf2_debug_sections dwarf2_elf_names =
267{
251d32d9
TG
268 { ".debug_info", ".zdebug_info" },
269 { ".debug_abbrev", ".zdebug_abbrev" },
270 { ".debug_line", ".zdebug_line" },
271 { ".debug_loc", ".zdebug_loc" },
272 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 273 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
274 { ".debug_str", ".zdebug_str" },
275 { ".debug_ranges", ".zdebug_ranges" },
276 { ".debug_types", ".zdebug_types" },
3019eac3 277 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
278 { ".debug_frame", ".zdebug_frame" },
279 { ".eh_frame", NULL },
24d3216f
TT
280 { ".gdb_index", ".zgdb_index" },
281 23
251d32d9 282};
c906108c 283
3019eac3
DE
284/* List of DWO sections. */
285
286static const struct dwo_section_names
287{
288 struct dwarf2_section_names abbrev_dwo;
289 struct dwarf2_section_names info_dwo;
290 struct dwarf2_section_names line_dwo;
291 struct dwarf2_section_names loc_dwo;
09262596
DE
292 struct dwarf2_section_names macinfo_dwo;
293 struct dwarf2_section_names macro_dwo;
3019eac3
DE
294 struct dwarf2_section_names str_dwo;
295 struct dwarf2_section_names str_offsets_dwo;
296 struct dwarf2_section_names types_dwo;
297}
298dwo_section_names =
299{
300 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
301 { ".debug_info.dwo", ".zdebug_info.dwo" },
302 { ".debug_line.dwo", ".zdebug_line.dwo" },
303 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
09262596
DE
304 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
305 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
306 { ".debug_str.dwo", ".zdebug_str.dwo" },
307 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
308 { ".debug_types.dwo", ".zdebug_types.dwo" },
309};
310
c906108c
SS
311/* local data types */
312
107d2387
AC
313/* The data in a compilation unit header, after target2host
314 translation, looks like this. */
c906108c 315struct comp_unit_head
a738430d 316{
c764a876 317 unsigned int length;
a738430d 318 short version;
a738430d
MK
319 unsigned char addr_size;
320 unsigned char signed_addr_p;
b64f50a1 321 sect_offset abbrev_offset;
57349743 322
a738430d
MK
323 /* Size of file offsets; either 4 or 8. */
324 unsigned int offset_size;
57349743 325
a738430d
MK
326 /* Size of the length field; either 4 or 12. */
327 unsigned int initial_length_size;
57349743 328
a738430d
MK
329 /* Offset to the first byte of this compilation unit header in the
330 .debug_info section, for resolving relative reference dies. */
b64f50a1 331 sect_offset offset;
57349743 332
d00adf39
DE
333 /* Offset to first die in this cu from the start of the cu.
334 This will be the first byte following the compilation unit header. */
b64f50a1 335 cu_offset first_die_offset;
a738430d 336};
c906108c 337
3da10d80
KS
338/* Type used for delaying computation of method physnames.
339 See comments for compute_delayed_physnames. */
340struct delayed_method_info
341{
342 /* The type to which the method is attached, i.e., its parent class. */
343 struct type *type;
344
345 /* The index of the method in the type's function fieldlists. */
346 int fnfield_index;
347
348 /* The index of the method in the fieldlist. */
349 int index;
350
351 /* The name of the DIE. */
352 const char *name;
353
354 /* The DIE associated with this method. */
355 struct die_info *die;
356};
357
358typedef struct delayed_method_info delayed_method_info;
359DEF_VEC_O (delayed_method_info);
360
e7c27a73
DJ
361/* Internal state when decoding a particular compilation unit. */
362struct dwarf2_cu
363{
364 /* The objfile containing this compilation unit. */
365 struct objfile *objfile;
366
d00adf39 367 /* The header of the compilation unit. */
e7c27a73 368 struct comp_unit_head header;
e142c38c 369
d00adf39
DE
370 /* Base address of this compilation unit. */
371 CORE_ADDR base_address;
372
373 /* Non-zero if base_address has been set. */
374 int base_known;
375
e142c38c
DJ
376 /* The language we are debugging. */
377 enum language language;
378 const struct language_defn *language_defn;
379
b0f35d58
DL
380 const char *producer;
381
e142c38c
DJ
382 /* The generic symbol table building routines have separate lists for
383 file scope symbols and all all other scopes (local scopes). So
384 we need to select the right one to pass to add_symbol_to_list().
385 We do it by keeping a pointer to the correct list in list_in_scope.
386
387 FIXME: The original dwarf code just treated the file scope as the
388 first local scope, and all other local scopes as nested local
389 scopes, and worked fine. Check to see if we really need to
390 distinguish these in buildsym.c. */
391 struct pending **list_in_scope;
392
433df2d4
DE
393 /* The abbrev table for this CU.
394 Normally this points to the abbrev table in the objfile.
395 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
396 struct abbrev_table *abbrev_table;
72bf9492 397
b64f50a1
JK
398 /* Hash table holding all the loaded partial DIEs
399 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
400 htab_t partial_dies;
401
402 /* Storage for things with the same lifetime as this read-in compilation
403 unit, including partial DIEs. */
404 struct obstack comp_unit_obstack;
405
ae038cb0
DJ
406 /* When multiple dwarf2_cu structures are living in memory, this field
407 chains them all together, so that they can be released efficiently.
408 We will probably also want a generation counter so that most-recently-used
409 compilation units are cached... */
410 struct dwarf2_per_cu_data *read_in_chain;
411
412 /* Backchain to our per_cu entry if the tree has been built. */
413 struct dwarf2_per_cu_data *per_cu;
414
415 /* How many compilation units ago was this CU last referenced? */
416 int last_used;
417
b64f50a1
JK
418 /* A hash table of DIE cu_offset for following references with
419 die_info->offset.sect_off as hash. */
51545339 420 htab_t die_hash;
10b3939b
DJ
421
422 /* Full DIEs if read in. */
423 struct die_info *dies;
424
425 /* A set of pointers to dwarf2_per_cu_data objects for compilation
426 units referenced by this one. Only set during full symbol processing;
427 partial symbol tables do not have dependencies. */
428 htab_t dependencies;
429
cb1df416
DJ
430 /* Header data from the line table, during full symbol processing. */
431 struct line_header *line_header;
432
3da10d80
KS
433 /* A list of methods which need to have physnames computed
434 after all type information has been read. */
435 VEC (delayed_method_info) *method_list;
436
96408a79
SA
437 /* To be copied to symtab->call_site_htab. */
438 htab_t call_site_htab;
439
034e5797
DE
440 /* Non-NULL if this CU came from a DWO file.
441 There is an invariant here that is important to remember:
442 Except for attributes copied from the top level DIE in the "main"
443 (or "stub") file in preparation for reading the DWO file
444 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
445 Either there isn't a DWO file (in which case this is NULL and the point
446 is moot), or there is and either we're not going to read it (in which
447 case this is NULL) or there is and we are reading it (in which case this
448 is non-NULL). */
3019eac3
DE
449 struct dwo_unit *dwo_unit;
450
451 /* The DW_AT_addr_base attribute if present, zero otherwise
452 (zero is a valid value though).
453 Note this value comes from the stub CU/TU's DIE. */
454 ULONGEST addr_base;
455
2e3cf129
DE
456 /* The DW_AT_ranges_base attribute if present, zero otherwise
457 (zero is a valid value though).
458 Note this value comes from the stub CU/TU's DIE.
459 Also note that the value is zero in the non-DWO case so this value can
460 be used without needing to know whether DWO files are in use or not. */
461 ULONGEST ranges_base;
462
ae038cb0
DJ
463 /* Mark used when releasing cached dies. */
464 unsigned int mark : 1;
465
8be455d7
JK
466 /* This CU references .debug_loc. See the symtab->locations_valid field.
467 This test is imperfect as there may exist optimized debug code not using
468 any location list and still facing inlining issues if handled as
469 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 470 unsigned int has_loclist : 1;
ba919b58 471
685b1105
JK
472 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
473 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
474 are valid. This information is cached because profiling CU expansion
475 showed excessive time spent in producer_is_gxx_lt_4_6. */
ba919b58
TT
476 unsigned int checked_producer : 1;
477 unsigned int producer_is_gxx_lt_4_6 : 1;
685b1105 478 unsigned int producer_is_icc : 1;
e7c27a73
DJ
479};
480
10b3939b
DJ
481/* Persistent data held for a compilation unit, even when not
482 processing it. We put a pointer to this structure in the
28dee7f5 483 read_symtab_private field of the psymtab. */
10b3939b 484
ae038cb0
DJ
485struct dwarf2_per_cu_data
486{
348e048f 487 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 488 bytes should suffice to store the length of any compilation unit
45452591
DE
489 - if it doesn't, GDB will fall over anyway.
490 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
491 initial_length_size.
492 If the DIE refers to a DWO file, this is always of the original die,
493 not the DWO file. */
b64f50a1 494 sect_offset offset;
348e048f 495 unsigned int length : 29;
ae038cb0
DJ
496
497 /* Flag indicating this compilation unit will be read in before
498 any of the current compilation units are processed. */
c764a876 499 unsigned int queued : 1;
ae038cb0 500
0d99eb77
DE
501 /* This flag will be set when reading partial DIEs if we need to load
502 absolutely all DIEs for this compilation unit, instead of just the ones
503 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
504 hash table and don't find it. */
505 unsigned int load_all_dies : 1;
506
3019eac3
DE
507 /* Non-zero if this CU is from .debug_types. */
508 unsigned int is_debug_types : 1;
509
510 /* The section this CU/TU lives in.
511 If the DIE refers to a DWO file, this is always the original die,
512 not the DWO file. */
513 struct dwarf2_section_info *info_or_types_section;
348e048f 514
17ea53c3
JK
515 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
516 of the CU cache it gets reset to NULL again. */
ae038cb0 517 struct dwarf2_cu *cu;
1c379e20 518
9cdd5dbd
DE
519 /* The corresponding objfile.
520 Normally we can get the objfile from dwarf2_per_objfile.
521 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
522 struct objfile *objfile;
523
524 /* When using partial symbol tables, the 'psymtab' field is active.
525 Otherwise the 'quick' field is active. */
526 union
527 {
528 /* The partial symbol table associated with this compilation unit,
95554aad 529 or NULL for unread partial units. */
9291a0cd
TT
530 struct partial_symtab *psymtab;
531
532 /* Data needed by the "quick" functions. */
533 struct dwarf2_per_cu_quick_data *quick;
534 } v;
95554aad
TT
535
536 /* The CUs we import using DW_TAG_imported_unit. This is filled in
537 while reading psymtabs, used to compute the psymtab dependencies,
538 and then cleared. Then it is filled in again while reading full
539 symbols, and only deleted when the objfile is destroyed. */
540 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
541};
542
348e048f
DE
543/* Entry in the signatured_types hash table. */
544
545struct signatured_type
546{
42e7ad6c
DE
547 /* The "per_cu" object of this type.
548 N.B.: This is the first member so that it's easy to convert pointers
549 between them. */
550 struct dwarf2_per_cu_data per_cu;
551
3019eac3 552 /* The type's signature. */
348e048f
DE
553 ULONGEST signature;
554
3019eac3
DE
555 /* Offset in the TU of the type's DIE, as read from the TU header.
556 If the definition lives in a DWO file, this value is unusable. */
557 cu_offset type_offset_in_tu;
558
559 /* Offset in the section of the type's DIE.
560 If the definition lives in a DWO file, this is the offset in the
561 .debug_types.dwo section.
562 The value is zero until the actual value is known.
563 Zero is otherwise not a valid section offset. */
564 sect_offset type_offset_in_section;
348e048f
DE
565};
566
3019eac3
DE
567/* These sections are what may appear in a "dwo" file. */
568
569struct dwo_sections
570{
571 struct dwarf2_section_info abbrev;
572 struct dwarf2_section_info info;
573 struct dwarf2_section_info line;
574 struct dwarf2_section_info loc;
09262596
DE
575 struct dwarf2_section_info macinfo;
576 struct dwarf2_section_info macro;
3019eac3
DE
577 struct dwarf2_section_info str;
578 struct dwarf2_section_info str_offsets;
579 VEC (dwarf2_section_info_def) *types;
580};
581
582/* Common bits of DWO CUs/TUs. */
583
584struct dwo_unit
585{
586 /* Backlink to the containing struct dwo_file. */
587 struct dwo_file *dwo_file;
588
589 /* The "id" that distinguishes this CU/TU.
590 .debug_info calls this "dwo_id", .debug_types calls this "signature".
591 Since signatures came first, we stick with it for consistency. */
592 ULONGEST signature;
593
594 /* The section this CU/TU lives in, in the DWO file. */
595 struct dwarf2_section_info *info_or_types_section;
596
597 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
598 sect_offset offset;
599 unsigned int length;
600
601 /* For types, offset in the type's DIE of the type defined by this TU. */
602 cu_offset type_offset_in_tu;
603};
604
605/* Data for one DWO file. */
606
607struct dwo_file
608{
609 /* The DW_AT_GNU_dwo_name attribute.
610 We don't manage space for this, it's an attribute. */
611 const char *dwo_name;
612
613 /* The bfd, when the file is open. Otherwise this is NULL. */
614 bfd *dwo_bfd;
615
616 /* Section info for this file. */
617 struct dwo_sections sections;
618
619 /* Table of CUs in the file.
620 Each element is a struct dwo_unit. */
621 htab_t cus;
622
623 /* Table of TUs in the file.
624 Each element is a struct dwo_unit. */
625 htab_t tus;
626};
627
0963b4bd
MS
628/* Struct used to pass misc. parameters to read_die_and_children, et
629 al. which are used for both .debug_info and .debug_types dies.
630 All parameters here are unchanging for the life of the call. This
dee91e82 631 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
632
633struct die_reader_specs
634{
dee91e82 635 /* die_section->asection->owner. */
93311388
DE
636 bfd* abfd;
637
638 /* The CU of the DIE we are parsing. */
639 struct dwarf2_cu *cu;
640
3019eac3
DE
641 /* Non-NULL if reading a DWO file. */
642 struct dwo_file *dwo_file;
643
dee91e82 644 /* The section the die comes from.
3019eac3 645 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
646 struct dwarf2_section_info *die_section;
647
648 /* die_section->buffer. */
649 gdb_byte *buffer;
f664829e
DE
650
651 /* The end of the buffer. */
652 const gdb_byte *buffer_end;
93311388
DE
653};
654
fd820528 655/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
656typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
657 gdb_byte *info_ptr,
658 struct die_info *comp_unit_die,
659 int has_children,
660 void *data);
661
debd256d
JB
662/* The line number information for a compilation unit (found in the
663 .debug_line section) begins with a "statement program header",
664 which contains the following information. */
665struct line_header
666{
667 unsigned int total_length;
668 unsigned short version;
669 unsigned int header_length;
670 unsigned char minimum_instruction_length;
2dc7f7b3 671 unsigned char maximum_ops_per_instruction;
debd256d
JB
672 unsigned char default_is_stmt;
673 int line_base;
674 unsigned char line_range;
675 unsigned char opcode_base;
676
677 /* standard_opcode_lengths[i] is the number of operands for the
678 standard opcode whose value is i. This means that
679 standard_opcode_lengths[0] is unused, and the last meaningful
680 element is standard_opcode_lengths[opcode_base - 1]. */
681 unsigned char *standard_opcode_lengths;
682
683 /* The include_directories table. NOTE! These strings are not
684 allocated with xmalloc; instead, they are pointers into
685 debug_line_buffer. If you try to free them, `free' will get
686 indigestion. */
687 unsigned int num_include_dirs, include_dirs_size;
688 char **include_dirs;
689
690 /* The file_names table. NOTE! These strings are not allocated
691 with xmalloc; instead, they are pointers into debug_line_buffer.
692 Don't try to free them directly. */
693 unsigned int num_file_names, file_names_size;
694 struct file_entry
c906108c 695 {
debd256d
JB
696 char *name;
697 unsigned int dir_index;
698 unsigned int mod_time;
699 unsigned int length;
aaa75496 700 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 701 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
702 } *file_names;
703
704 /* The start and end of the statement program following this
6502dd73 705 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 706 gdb_byte *statement_program_start, *statement_program_end;
debd256d 707};
c906108c
SS
708
709/* When we construct a partial symbol table entry we only
0963b4bd 710 need this much information. */
c906108c
SS
711struct partial_die_info
712 {
72bf9492 713 /* Offset of this DIE. */
b64f50a1 714 sect_offset offset;
72bf9492
DJ
715
716 /* DWARF-2 tag for this DIE. */
717 ENUM_BITFIELD(dwarf_tag) tag : 16;
718
72bf9492
DJ
719 /* Assorted flags describing the data found in this DIE. */
720 unsigned int has_children : 1;
721 unsigned int is_external : 1;
722 unsigned int is_declaration : 1;
723 unsigned int has_type : 1;
724 unsigned int has_specification : 1;
725 unsigned int has_pc_info : 1;
481860b3 726 unsigned int may_be_inlined : 1;
72bf9492
DJ
727
728 /* Flag set if the SCOPE field of this structure has been
729 computed. */
730 unsigned int scope_set : 1;
731
fa4028e9
JB
732 /* Flag set if the DIE has a byte_size attribute. */
733 unsigned int has_byte_size : 1;
734
98bfdba5
PA
735 /* Flag set if any of the DIE's children are template arguments. */
736 unsigned int has_template_arguments : 1;
737
abc72ce4
DE
738 /* Flag set if fixup_partial_die has been called on this die. */
739 unsigned int fixup_called : 1;
740
72bf9492 741 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 742 sometimes a default name for unnamed DIEs. */
c906108c 743 char *name;
72bf9492 744
abc72ce4
DE
745 /* The linkage name, if present. */
746 const char *linkage_name;
747
72bf9492
DJ
748 /* The scope to prepend to our children. This is generally
749 allocated on the comp_unit_obstack, so will disappear
750 when this compilation unit leaves the cache. */
751 char *scope;
752
95554aad
TT
753 /* Some data associated with the partial DIE. The tag determines
754 which field is live. */
755 union
756 {
757 /* The location description associated with this DIE, if any. */
758 struct dwarf_block *locdesc;
759 /* The offset of an import, for DW_TAG_imported_unit. */
760 sect_offset offset;
761 } d;
72bf9492
DJ
762
763 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
764 CORE_ADDR lowpc;
765 CORE_ADDR highpc;
72bf9492 766
93311388 767 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 768 DW_AT_sibling, if any. */
abc72ce4
DE
769 /* NOTE: This member isn't strictly necessary, read_partial_die could
770 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 771 gdb_byte *sibling;
72bf9492
DJ
772
773 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
774 DW_AT_specification (or DW_AT_abstract_origin or
775 DW_AT_extension). */
b64f50a1 776 sect_offset spec_offset;
72bf9492
DJ
777
778 /* Pointers to this DIE's parent, first child, and next sibling,
779 if any. */
780 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
781 };
782
0963b4bd 783/* This data structure holds the information of an abbrev. */
c906108c
SS
784struct abbrev_info
785 {
786 unsigned int number; /* number identifying abbrev */
787 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
788 unsigned short has_children; /* boolean */
789 unsigned short num_attrs; /* number of attributes */
c906108c
SS
790 struct attr_abbrev *attrs; /* an array of attribute descriptions */
791 struct abbrev_info *next; /* next in chain */
792 };
793
794struct attr_abbrev
795 {
9d25dd43
DE
796 ENUM_BITFIELD(dwarf_attribute) name : 16;
797 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
798 };
799
433df2d4
DE
800/* Size of abbrev_table.abbrev_hash_table. */
801#define ABBREV_HASH_SIZE 121
802
803/* Top level data structure to contain an abbreviation table. */
804
805struct abbrev_table
806{
807 /* Where the abbrev table came from. */
808 struct dwarf2_section_info *section;
809 sect_offset offset;
810
811 /* Storage for the abbrev table. */
812 struct obstack abbrev_obstack;
813
814 /* Hash table of abbrevs.
815 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
816 It could be statically allocated, but the previous code didn't so we
817 don't either. */
818 struct abbrev_info **abbrevs;
819};
820
0963b4bd 821/* Attributes have a name and a value. */
b60c80d6
DJ
822struct attribute
823 {
9d25dd43 824 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
825 ENUM_BITFIELD(dwarf_form) form : 15;
826
827 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
828 field should be in u.str (existing only for DW_STRING) but it is kept
829 here for better struct attribute alignment. */
830 unsigned int string_is_canonical : 1;
831
b60c80d6
DJ
832 union
833 {
834 char *str;
835 struct dwarf_block *blk;
43bbcdc2
PH
836 ULONGEST unsnd;
837 LONGEST snd;
b60c80d6 838 CORE_ADDR addr;
348e048f 839 struct signatured_type *signatured_type;
b60c80d6
DJ
840 }
841 u;
842 };
843
0963b4bd 844/* This data structure holds a complete die structure. */
c906108c
SS
845struct die_info
846 {
76815b17
DE
847 /* DWARF-2 tag for this DIE. */
848 ENUM_BITFIELD(dwarf_tag) tag : 16;
849
850 /* Number of attributes */
98bfdba5
PA
851 unsigned char num_attrs;
852
853 /* True if we're presently building the full type name for the
854 type derived from this DIE. */
855 unsigned char building_fullname : 1;
76815b17
DE
856
857 /* Abbrev number */
858 unsigned int abbrev;
859
93311388 860 /* Offset in .debug_info or .debug_types section. */
b64f50a1 861 sect_offset offset;
78ba4af6
JB
862
863 /* The dies in a compilation unit form an n-ary tree. PARENT
864 points to this die's parent; CHILD points to the first child of
865 this node; and all the children of a given node are chained
4950bc1c 866 together via their SIBLING fields. */
639d11d3
DC
867 struct die_info *child; /* Its first child, if any. */
868 struct die_info *sibling; /* Its next sibling, if any. */
869 struct die_info *parent; /* Its parent, if any. */
c906108c 870
b60c80d6
DJ
871 /* An array of attributes, with NUM_ATTRS elements. There may be
872 zero, but it's not common and zero-sized arrays are not
873 sufficiently portable C. */
874 struct attribute attrs[1];
c906108c
SS
875 };
876
0963b4bd 877/* Get at parts of an attribute structure. */
c906108c
SS
878
879#define DW_STRING(attr) ((attr)->u.str)
8285870a 880#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
881#define DW_UNSND(attr) ((attr)->u.unsnd)
882#define DW_BLOCK(attr) ((attr)->u.blk)
883#define DW_SND(attr) ((attr)->u.snd)
884#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 885#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 886
0963b4bd 887/* Blocks are a bunch of untyped bytes. */
c906108c
SS
888struct dwarf_block
889 {
890 unsigned int size;
1d6edc3c
JK
891
892 /* Valid only if SIZE is not zero. */
fe1b8b76 893 gdb_byte *data;
c906108c
SS
894 };
895
c906108c
SS
896#ifndef ATTR_ALLOC_CHUNK
897#define ATTR_ALLOC_CHUNK 4
898#endif
899
c906108c
SS
900/* Allocate fields for structs, unions and enums in this size. */
901#ifndef DW_FIELD_ALLOC_CHUNK
902#define DW_FIELD_ALLOC_CHUNK 4
903#endif
904
c906108c
SS
905/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
906 but this would require a corresponding change in unpack_field_as_long
907 and friends. */
908static int bits_per_byte = 8;
909
910/* The routines that read and process dies for a C struct or C++ class
911 pass lists of data member fields and lists of member function fields
912 in an instance of a field_info structure, as defined below. */
913struct field_info
c5aa993b 914 {
0963b4bd 915 /* List of data member and baseclasses fields. */
c5aa993b
JM
916 struct nextfield
917 {
918 struct nextfield *next;
919 int accessibility;
920 int virtuality;
921 struct field field;
922 }
7d0ccb61 923 *fields, *baseclasses;
c906108c 924
7d0ccb61 925 /* Number of fields (including baseclasses). */
c5aa993b 926 int nfields;
c906108c 927
c5aa993b
JM
928 /* Number of baseclasses. */
929 int nbaseclasses;
c906108c 930
c5aa993b
JM
931 /* Set if the accesibility of one of the fields is not public. */
932 int non_public_fields;
c906108c 933
c5aa993b
JM
934 /* Member function fields array, entries are allocated in the order they
935 are encountered in the object file. */
936 struct nextfnfield
937 {
938 struct nextfnfield *next;
939 struct fn_field fnfield;
940 }
941 *fnfields;
c906108c 942
c5aa993b
JM
943 /* Member function fieldlist array, contains name of possibly overloaded
944 member function, number of overloaded member functions and a pointer
945 to the head of the member function field chain. */
946 struct fnfieldlist
947 {
948 char *name;
949 int length;
950 struct nextfnfield *head;
951 }
952 *fnfieldlists;
c906108c 953
c5aa993b
JM
954 /* Number of entries in the fnfieldlists array. */
955 int nfnfields;
98751a41
JK
956
957 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
958 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
959 struct typedef_field_list
960 {
961 struct typedef_field field;
962 struct typedef_field_list *next;
963 }
964 *typedef_field_list;
965 unsigned typedef_field_list_count;
c5aa993b 966 };
c906108c 967
10b3939b
DJ
968/* One item on the queue of compilation units to read in full symbols
969 for. */
970struct dwarf2_queue_item
971{
972 struct dwarf2_per_cu_data *per_cu;
95554aad 973 enum language pretend_language;
10b3939b
DJ
974 struct dwarf2_queue_item *next;
975};
976
977/* The current queue. */
978static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
979
ae038cb0
DJ
980/* Loaded secondary compilation units are kept in memory until they
981 have not been referenced for the processing of this many
982 compilation units. Set this to zero to disable caching. Cache
983 sizes of up to at least twenty will improve startup time for
984 typical inter-CU-reference binaries, at an obvious memory cost. */
985static int dwarf2_max_cache_age = 5;
920d2a44
AC
986static void
987show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
988 struct cmd_list_element *c, const char *value)
989{
3e43a32a
MS
990 fprintf_filtered (file, _("The upper bound on the age of cached "
991 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
992 value);
993}
994
ae038cb0 995
0963b4bd 996/* Various complaints about symbol reading that don't abort the process. */
c906108c 997
4d3c2250
KB
998static void
999dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 1000{
4d3c2250 1001 complaint (&symfile_complaints,
e2e0b3e5 1002 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
1003}
1004
25e43795
DJ
1005static void
1006dwarf2_debug_line_missing_file_complaint (void)
1007{
1008 complaint (&symfile_complaints,
1009 _(".debug_line section has line data without a file"));
1010}
1011
59205f5a
JB
1012static void
1013dwarf2_debug_line_missing_end_sequence_complaint (void)
1014{
1015 complaint (&symfile_complaints,
3e43a32a
MS
1016 _(".debug_line section has line "
1017 "program sequence without an end"));
59205f5a
JB
1018}
1019
4d3c2250
KB
1020static void
1021dwarf2_complex_location_expr_complaint (void)
2e276125 1022{
e2e0b3e5 1023 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
1024}
1025
4d3c2250
KB
1026static void
1027dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1028 int arg3)
2e276125 1029{
4d3c2250 1030 complaint (&symfile_complaints,
3e43a32a
MS
1031 _("const value length mismatch for '%s', got %d, expected %d"),
1032 arg1, arg2, arg3);
4d3c2250
KB
1033}
1034
1035static void
f664829e 1036dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 1037{
4d3c2250 1038 complaint (&symfile_complaints,
f664829e
DE
1039 _("debug info runs off end of %s section"
1040 " [in module %s]"),
1041 section->asection->name,
1042 bfd_get_filename (section->asection->owner));
4d3c2250
KB
1043}
1044
1045static void
1046dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 1047{
4d3c2250 1048 complaint (&symfile_complaints,
3e43a32a
MS
1049 _("macro debug info contains a "
1050 "malformed macro definition:\n`%s'"),
4d3c2250
KB
1051 arg1);
1052}
1053
1054static void
1055dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1056{
4d3c2250 1057 complaint (&symfile_complaints,
3e43a32a
MS
1058 _("invalid attribute class or form for '%s' in '%s'"),
1059 arg1, arg2);
4d3c2250 1060}
c906108c 1061
c906108c
SS
1062/* local function prototypes */
1063
4efb68b1 1064static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1065
aaa75496
JB
1066static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1067 struct objfile *);
1068
918dd910
JK
1069static void dwarf2_find_base_address (struct die_info *die,
1070 struct dwarf2_cu *cu);
1071
c67a9c90 1072static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1073
72bf9492
DJ
1074static void scan_partial_symbols (struct partial_die_info *,
1075 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1076 int, struct dwarf2_cu *);
c906108c 1077
72bf9492
DJ
1078static void add_partial_symbol (struct partial_die_info *,
1079 struct dwarf2_cu *);
63d06c5c 1080
72bf9492
DJ
1081static void add_partial_namespace (struct partial_die_info *pdi,
1082 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1083 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1084
5d7cb8df
JK
1085static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1086 CORE_ADDR *highpc, int need_pc,
1087 struct dwarf2_cu *cu);
1088
72bf9492
DJ
1089static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1090 struct dwarf2_cu *cu);
91c24f0a 1091
bc30ff58
JB
1092static void add_partial_subprogram (struct partial_die_info *pdi,
1093 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1094 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1095
a14ed312 1096static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 1097
a14ed312 1098static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1099
433df2d4
DE
1100static struct abbrev_info *abbrev_table_lookup_abbrev
1101 (const struct abbrev_table *, unsigned int);
1102
1103static struct abbrev_table *abbrev_table_read_table
1104 (struct dwarf2_section_info *, sect_offset);
1105
1106static void abbrev_table_free (struct abbrev_table *);
1107
dee91e82
DE
1108static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1109 struct dwarf2_section_info *);
c906108c 1110
f3dd6933 1111static void dwarf2_free_abbrev_table (void *);
c906108c 1112
6caca83c
CC
1113static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1114
dee91e82
DE
1115static struct partial_die_info *load_partial_dies
1116 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1117
dee91e82
DE
1118static gdb_byte *read_partial_die (const struct die_reader_specs *,
1119 struct partial_die_info *,
1120 struct abbrev_info *,
1121 unsigned int,
1122 gdb_byte *);
c906108c 1123
b64f50a1 1124static struct partial_die_info *find_partial_die (sect_offset,
10b3939b 1125 struct dwarf2_cu *);
72bf9492
DJ
1126
1127static void fixup_partial_die (struct partial_die_info *,
1128 struct dwarf2_cu *);
1129
dee91e82
DE
1130static gdb_byte *read_attribute (const struct die_reader_specs *,
1131 struct attribute *, struct attr_abbrev *,
1132 gdb_byte *);
a8329558 1133
fe1b8b76 1134static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 1135
fe1b8b76 1136static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 1137
fe1b8b76 1138static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 1139
fe1b8b76 1140static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 1141
93311388 1142static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 1143
fe1b8b76 1144static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1145 unsigned int *);
c906108c 1146
c764a876
DE
1147static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1148
1149static LONGEST read_checked_initial_length_and_offset
1150 (bfd *, gdb_byte *, const struct comp_unit_head *,
1151 unsigned int *, unsigned int *);
613e1657 1152
fe1b8b76 1153static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1154 unsigned int *);
1155
1156static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1157
fe1b8b76 1158static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1159
9b1c24c8 1160static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1161
fe1b8b76
JB
1162static char *read_indirect_string (bfd *, gdb_byte *,
1163 const struct comp_unit_head *,
1164 unsigned int *);
4bdf3d34 1165
12df843f 1166static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1167
12df843f 1168static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1169
3019eac3
DE
1170static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1171 unsigned int *);
1172
1173static char *read_str_index (const struct die_reader_specs *reader,
1174 struct dwarf2_cu *cu, ULONGEST str_index);
1175
e142c38c 1176static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1177
e142c38c
DJ
1178static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1179 struct dwarf2_cu *);
c906108c 1180
348e048f
DE
1181static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1182 unsigned int,
1183 struct dwarf2_cu *);
1184
05cf31d1
JB
1185static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1186 struct dwarf2_cu *cu);
1187
e142c38c 1188static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1189
e142c38c 1190static struct die_info *die_specification (struct die_info *die,
f2f0e013 1191 struct dwarf2_cu **);
63d06c5c 1192
debd256d
JB
1193static void free_line_header (struct line_header *lh);
1194
aaa75496
JB
1195static void add_file_name (struct line_header *, char *, unsigned int,
1196 unsigned int, unsigned int);
1197
3019eac3
DE
1198static struct line_header *dwarf_decode_line_header (unsigned int offset,
1199 struct dwarf2_cu *cu);
debd256d 1200
f3f5162e
DE
1201static void dwarf_decode_lines (struct line_header *, const char *,
1202 struct dwarf2_cu *, struct partial_symtab *,
1203 int);
c906108c 1204
72b9f47f 1205static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1206
a14ed312 1207static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1208 struct dwarf2_cu *);
c906108c 1209
34eaf542
TT
1210static struct symbol *new_symbol_full (struct die_info *, struct type *,
1211 struct dwarf2_cu *, struct symbol *);
1212
a14ed312 1213static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1214 struct dwarf2_cu *);
c906108c 1215
98bfdba5
PA
1216static void dwarf2_const_value_attr (struct attribute *attr,
1217 struct type *type,
1218 const char *name,
1219 struct obstack *obstack,
12df843f 1220 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1221 gdb_byte **bytes,
1222 struct dwarf2_locexpr_baton **baton);
2df3850c 1223
e7c27a73 1224static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1225
b4ba55a1
JB
1226static int need_gnat_info (struct dwarf2_cu *);
1227
3e43a32a
MS
1228static struct type *die_descriptive_type (struct die_info *,
1229 struct dwarf2_cu *);
b4ba55a1
JB
1230
1231static void set_descriptive_type (struct type *, struct die_info *,
1232 struct dwarf2_cu *);
1233
e7c27a73
DJ
1234static struct type *die_containing_type (struct die_info *,
1235 struct dwarf2_cu *);
c906108c 1236
673bfd45
DE
1237static struct type *lookup_die_type (struct die_info *, struct attribute *,
1238 struct dwarf2_cu *);
c906108c 1239
f792889a 1240static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1241
673bfd45
DE
1242static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1243
0d5cff50 1244static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1245
6e70227d 1246static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1247 const char *suffix, int physname,
1248 struct dwarf2_cu *cu);
63d06c5c 1249
e7c27a73 1250static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1251
348e048f
DE
1252static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1253
e7c27a73 1254static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1255
e7c27a73 1256static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1257
96408a79
SA
1258static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1259
ff013f42
JK
1260static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1261 struct dwarf2_cu *, struct partial_symtab *);
1262
a14ed312 1263static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1264 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1265 struct partial_symtab *);
c906108c 1266
fae299cd
DC
1267static void get_scope_pc_bounds (struct die_info *,
1268 CORE_ADDR *, CORE_ADDR *,
1269 struct dwarf2_cu *);
1270
801e3a5b
JB
1271static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1272 CORE_ADDR, struct dwarf2_cu *);
1273
a14ed312 1274static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1275 struct dwarf2_cu *);
c906108c 1276
a14ed312 1277static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1278 struct type *, struct dwarf2_cu *);
c906108c 1279
a14ed312 1280static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1281 struct die_info *, struct type *,
e7c27a73 1282 struct dwarf2_cu *);
c906108c 1283
a14ed312 1284static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1285 struct type *,
1286 struct dwarf2_cu *);
c906108c 1287
134d01f1 1288static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1289
e7c27a73 1290static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1291
e7c27a73 1292static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1293
5d7cb8df
JK
1294static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1295
27aa8d6a
SW
1296static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1297
f55ee35c
JK
1298static struct type *read_module_type (struct die_info *die,
1299 struct dwarf2_cu *cu);
1300
38d518c9 1301static const char *namespace_name (struct die_info *die,
e142c38c 1302 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1303
134d01f1 1304static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1305
e7c27a73 1306static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1307
6e70227d 1308static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1309 struct dwarf2_cu *);
1310
dee91e82 1311static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1312 gdb_byte *info_ptr,
fe1b8b76 1313 gdb_byte **new_info_ptr,
639d11d3
DC
1314 struct die_info *parent);
1315
dee91e82 1316static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1317 gdb_byte *info_ptr,
fe1b8b76 1318 gdb_byte **new_info_ptr,
639d11d3
DC
1319 struct die_info *parent);
1320
3019eac3
DE
1321static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1322 struct die_info **, gdb_byte *, int *, int);
1323
dee91e82
DE
1324static gdb_byte *read_full_die (const struct die_reader_specs *,
1325 struct die_info **, gdb_byte *, int *);
93311388 1326
e7c27a73 1327static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1328
71c25dea
TT
1329static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1330 struct obstack *);
1331
e142c38c 1332static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1333
98bfdba5
PA
1334static const char *dwarf2_full_name (char *name,
1335 struct die_info *die,
1336 struct dwarf2_cu *cu);
1337
e142c38c 1338static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1339 struct dwarf2_cu **);
9219021c 1340
f39c6ffd 1341static const char *dwarf_tag_name (unsigned int);
c906108c 1342
f39c6ffd 1343static const char *dwarf_attr_name (unsigned int);
c906108c 1344
f39c6ffd 1345static const char *dwarf_form_name (unsigned int);
c906108c 1346
a14ed312 1347static char *dwarf_bool_name (unsigned int);
c906108c 1348
f39c6ffd 1349static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1350
f9aca02d 1351static struct die_info *sibling_die (struct die_info *);
c906108c 1352
d97bc12b
DE
1353static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1354
1355static void dump_die_for_error (struct die_info *);
1356
1357static void dump_die_1 (struct ui_file *, int level, int max_level,
1358 struct die_info *);
c906108c 1359
d97bc12b 1360/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1361
51545339 1362static void store_in_ref_table (struct die_info *,
10b3939b 1363 struct dwarf2_cu *);
c906108c 1364
93311388
DE
1365static int is_ref_attr (struct attribute *);
1366
b64f50a1 1367static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1368
43bbcdc2 1369static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1370
348e048f
DE
1371static struct die_info *follow_die_ref_or_sig (struct die_info *,
1372 struct attribute *,
1373 struct dwarf2_cu **);
1374
10b3939b
DJ
1375static struct die_info *follow_die_ref (struct die_info *,
1376 struct attribute *,
f2f0e013 1377 struct dwarf2_cu **);
c906108c 1378
348e048f
DE
1379static struct die_info *follow_die_sig (struct die_info *,
1380 struct attribute *,
1381 struct dwarf2_cu **);
1382
6c83ed52
TT
1383static struct signatured_type *lookup_signatured_type_at_offset
1384 (struct objfile *objfile,
b64f50a1 1385 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1386
e5fe5e75 1387static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1388
52dc124a 1389static void read_signatured_type (struct signatured_type *);
348e048f 1390
c906108c
SS
1391/* memory allocation interface */
1392
7b5a2f43 1393static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1394
b60c80d6 1395static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1396
09262596
DE
1397static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1398 char *, int);
2e276125 1399
8e19ed76
PS
1400static int attr_form_is_block (struct attribute *);
1401
3690dd37
JB
1402static int attr_form_is_section_offset (struct attribute *);
1403
1404static int attr_form_is_constant (struct attribute *);
1405
8cf6f0b1
TT
1406static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1407 struct dwarf2_loclist_baton *baton,
1408 struct attribute *attr);
1409
93e7bd98
DJ
1410static void dwarf2_symbol_mark_computed (struct attribute *attr,
1411 struct symbol *sym,
1412 struct dwarf2_cu *cu);
4c2df51b 1413
dee91e82
DE
1414static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1415 gdb_byte *info_ptr,
1416 struct abbrev_info *abbrev);
4bb7a0a7 1417
72bf9492
DJ
1418static void free_stack_comp_unit (void *);
1419
72bf9492
DJ
1420static hashval_t partial_die_hash (const void *item);
1421
1422static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1423
ae038cb0 1424static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
b64f50a1 1425 (sect_offset offset, struct objfile *objfile);
ae038cb0 1426
9816fde3 1427static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1428 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1429
1430static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1431 struct die_info *comp_unit_die,
1432 enum language pretend_language);
93311388 1433
68dc6402 1434static void free_heap_comp_unit (void *);
ae038cb0
DJ
1435
1436static void free_cached_comp_units (void *);
1437
1438static void age_cached_comp_units (void);
1439
dee91e82 1440static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1441
f792889a
DJ
1442static struct type *set_die_type (struct die_info *, struct type *,
1443 struct dwarf2_cu *);
1c379e20 1444
ae038cb0
DJ
1445static void create_all_comp_units (struct objfile *);
1446
0e50663e 1447static int create_all_type_units (struct objfile *);
1fd400ff 1448
95554aad
TT
1449static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1450 enum language);
10b3939b 1451
95554aad
TT
1452static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1453 enum language);
10b3939b
DJ
1454
1455static void dwarf2_add_dependence (struct dwarf2_cu *,
1456 struct dwarf2_per_cu_data *);
1457
ae038cb0
DJ
1458static void dwarf2_mark (struct dwarf2_cu *);
1459
1460static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1461
b64f50a1 1462static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1463 struct dwarf2_per_cu_data *per_cu);
1464
f792889a 1465static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1466
9291a0cd
TT
1467static void dwarf2_release_queue (void *dummy);
1468
95554aad
TT
1469static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1470 enum language pretend_language);
1471
1472static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1473 struct dwarf2_per_cu_data *per_cu,
1474 enum language pretend_language);
9291a0cd 1475
a0f42c21 1476static void process_queue (void);
9291a0cd
TT
1477
1478static void find_file_and_directory (struct die_info *die,
1479 struct dwarf2_cu *cu,
1480 char **name, char **comp_dir);
1481
1482static char *file_full_name (int file, struct line_header *lh,
1483 const char *comp_dir);
1484
fd820528
DE
1485static void init_cutu_and_read_dies
1486 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
3019eac3
DE
1487 die_reader_func_ftype *die_reader_func, void *data);
1488
dee91e82
DE
1489static void init_cutu_and_read_dies_simple
1490 (struct dwarf2_per_cu_data *this_cu,
1491 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1492
673bfd45 1493static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1494
95554aad 1495static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *, int);
dee91e82 1496
3019eac3
DE
1497static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1498
1499static struct dwo_unit *lookup_dwo_comp_unit
1500 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1501
1502static struct dwo_unit *lookup_dwo_type_unit
1503 (struct signatured_type *, char *, const char *);
1504
1505static void free_dwo_file_cleanup (void *);
1506
1507static void munmap_section_buffer (struct dwarf2_section_info *);
1508
95554aad
TT
1509static void process_cu_includes (void);
1510
9291a0cd
TT
1511#if WORDS_BIGENDIAN
1512
1513/* Convert VALUE between big- and little-endian. */
1514static offset_type
1515byte_swap (offset_type value)
1516{
1517 offset_type result;
1518
1519 result = (value & 0xff) << 24;
1520 result |= (value & 0xff00) << 8;
1521 result |= (value & 0xff0000) >> 8;
1522 result |= (value & 0xff000000) >> 24;
1523 return result;
1524}
1525
1526#define MAYBE_SWAP(V) byte_swap (V)
1527
1528#else
1529#define MAYBE_SWAP(V) (V)
1530#endif /* WORDS_BIGENDIAN */
1531
1532/* The suffix for an index file. */
1533#define INDEX_SUFFIX ".gdb-index"
1534
3da10d80
KS
1535static const char *dwarf2_physname (char *name, struct die_info *die,
1536 struct dwarf2_cu *cu);
1537
c906108c 1538/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1539 information and return true if we have enough to do something.
1540 NAMES points to the dwarf2 section names, or is NULL if the standard
1541 ELF names are used. */
c906108c
SS
1542
1543int
251d32d9
TG
1544dwarf2_has_info (struct objfile *objfile,
1545 const struct dwarf2_debug_sections *names)
c906108c 1546{
be391dca
TT
1547 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1548 if (!dwarf2_per_objfile)
1549 {
1550 /* Initialize per-objfile state. */
1551 struct dwarf2_per_objfile *data
1552 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1553
be391dca
TT
1554 memset (data, 0, sizeof (*data));
1555 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1556 dwarf2_per_objfile = data;
6502dd73 1557
251d32d9
TG
1558 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1559 (void *) names);
be391dca
TT
1560 dwarf2_per_objfile->objfile = objfile;
1561 }
1562 return (dwarf2_per_objfile->info.asection != NULL
1563 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1564}
1565
251d32d9
TG
1566/* When loading sections, we look either for uncompressed section or for
1567 compressed section names. */
233a11ab
CS
1568
1569static int
251d32d9
TG
1570section_is_p (const char *section_name,
1571 const struct dwarf2_section_names *names)
233a11ab 1572{
251d32d9
TG
1573 if (names->normal != NULL
1574 && strcmp (section_name, names->normal) == 0)
1575 return 1;
1576 if (names->compressed != NULL
1577 && strcmp (section_name, names->compressed) == 0)
1578 return 1;
1579 return 0;
233a11ab
CS
1580}
1581
c906108c
SS
1582/* This function is mapped across the sections and remembers the
1583 offset and size of each of the debugging sections we are interested
1584 in. */
1585
1586static void
251d32d9 1587dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1588{
251d32d9
TG
1589 const struct dwarf2_debug_sections *names;
1590
1591 if (vnames == NULL)
1592 names = &dwarf2_elf_names;
1593 else
1594 names = (const struct dwarf2_debug_sections *) vnames;
1595
1596 if (section_is_p (sectp->name, &names->info))
c906108c 1597 {
dce234bc
PP
1598 dwarf2_per_objfile->info.asection = sectp;
1599 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1600 }
251d32d9 1601 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1602 {
dce234bc
PP
1603 dwarf2_per_objfile->abbrev.asection = sectp;
1604 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1605 }
251d32d9 1606 else if (section_is_p (sectp->name, &names->line))
c906108c 1607 {
dce234bc
PP
1608 dwarf2_per_objfile->line.asection = sectp;
1609 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1610 }
251d32d9 1611 else if (section_is_p (sectp->name, &names->loc))
c906108c 1612 {
dce234bc
PP
1613 dwarf2_per_objfile->loc.asection = sectp;
1614 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1615 }
251d32d9 1616 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1617 {
dce234bc
PP
1618 dwarf2_per_objfile->macinfo.asection = sectp;
1619 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1620 }
cf2c3c16
TT
1621 else if (section_is_p (sectp->name, &names->macro))
1622 {
1623 dwarf2_per_objfile->macro.asection = sectp;
1624 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1625 }
251d32d9 1626 else if (section_is_p (sectp->name, &names->str))
c906108c 1627 {
dce234bc
PP
1628 dwarf2_per_objfile->str.asection = sectp;
1629 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1630 }
3019eac3
DE
1631 else if (section_is_p (sectp->name, &names->addr))
1632 {
1633 dwarf2_per_objfile->addr.asection = sectp;
1634 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1635 }
251d32d9 1636 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1637 {
dce234bc
PP
1638 dwarf2_per_objfile->frame.asection = sectp;
1639 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1640 }
251d32d9 1641 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1642 {
81b9b86e 1643 flagword aflag = bfd_get_section_flags (abfd, sectp);
9a619af0 1644
3799ccc6
EZ
1645 if (aflag & SEC_HAS_CONTENTS)
1646 {
dce234bc
PP
1647 dwarf2_per_objfile->eh_frame.asection = sectp;
1648 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1649 }
b6af0555 1650 }
251d32d9 1651 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1652 {
dce234bc
PP
1653 dwarf2_per_objfile->ranges.asection = sectp;
1654 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1655 }
251d32d9 1656 else if (section_is_p (sectp->name, &names->types))
348e048f 1657 {
8b70b953
TT
1658 struct dwarf2_section_info type_section;
1659
1660 memset (&type_section, 0, sizeof (type_section));
1661 type_section.asection = sectp;
1662 type_section.size = bfd_get_section_size (sectp);
1663
1664 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1665 &type_section);
348e048f 1666 }
251d32d9 1667 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1668 {
1669 dwarf2_per_objfile->gdb_index.asection = sectp;
1670 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1671 }
dce234bc 1672
72dca2f5
FR
1673 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1674 && bfd_section_vma (abfd, sectp) == 0)
1675 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1676}
1677
dce234bc
PP
1678/* Decompress a section that was compressed using zlib. Store the
1679 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1680
1681static void
dce234bc
PP
1682zlib_decompress_section (struct objfile *objfile, asection *sectp,
1683 gdb_byte **outbuf, bfd_size_type *outsize)
1684{
3019eac3 1685 bfd *abfd = sectp->owner;
dce234bc
PP
1686#ifndef HAVE_ZLIB_H
1687 error (_("Support for zlib-compressed DWARF data (from '%s') "
1688 "is disabled in this copy of GDB"),
1689 bfd_get_filename (abfd));
1690#else
1691 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1692 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1693 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1694 bfd_size_type uncompressed_size;
1695 gdb_byte *uncompressed_buffer;
1696 z_stream strm;
1697 int rc;
1698 int header_size = 12;
1699
1700 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1701 || bfd_bread (compressed_buffer,
1702 compressed_size, abfd) != compressed_size)
dce234bc
PP
1703 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1704 bfd_get_filename (abfd));
1705
1706 /* Read the zlib header. In this case, it should be "ZLIB" followed
1707 by the uncompressed section size, 8 bytes in big-endian order. */
1708 if (compressed_size < header_size
1709 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1710 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1711 bfd_get_filename (abfd));
1712 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1713 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1714 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1715 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1716 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1717 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1718 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1719 uncompressed_size += compressed_buffer[11];
1720
1721 /* It is possible the section consists of several compressed
1722 buffers concatenated together, so we uncompress in a loop. */
1723 strm.zalloc = NULL;
1724 strm.zfree = NULL;
1725 strm.opaque = NULL;
1726 strm.avail_in = compressed_size - header_size;
1727 strm.next_in = (Bytef*) compressed_buffer + header_size;
1728 strm.avail_out = uncompressed_size;
1729 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1730 uncompressed_size);
1731 rc = inflateInit (&strm);
1732 while (strm.avail_in > 0)
1733 {
1734 if (rc != Z_OK)
1735 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1736 bfd_get_filename (abfd), rc);
1737 strm.next_out = ((Bytef*) uncompressed_buffer
1738 + (uncompressed_size - strm.avail_out));
1739 rc = inflate (&strm, Z_FINISH);
1740 if (rc != Z_STREAM_END)
1741 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1742 bfd_get_filename (abfd), rc);
1743 rc = inflateReset (&strm);
1744 }
1745 rc = inflateEnd (&strm);
1746 if (rc != Z_OK
1747 || strm.avail_out != 0)
1748 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1749 bfd_get_filename (abfd), rc);
1750
affddf13 1751 do_cleanups (cleanup);
dce234bc
PP
1752 *outbuf = uncompressed_buffer;
1753 *outsize = uncompressed_size;
1754#endif
233a11ab
CS
1755}
1756
fceca515
DE
1757/* A helper function that decides whether a section is empty,
1758 or not present. */
9e0ac564
TT
1759
1760static int
1761dwarf2_section_empty_p (struct dwarf2_section_info *info)
1762{
1763 return info->asection == NULL || info->size == 0;
1764}
1765
3019eac3
DE
1766/* Read the contents of the section INFO.
1767 OBJFILE is the main object file, but not necessarily the file where
1768 the section comes from. E.g., for DWO files INFO->asection->owner
1769 is the bfd of the DWO file.
dce234bc 1770 If the section is compressed, uncompress it before returning. */
c906108c 1771
dce234bc
PP
1772static void
1773dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1774{
dce234bc 1775 asection *sectp = info->asection;
3019eac3 1776 bfd *abfd;
dce234bc
PP
1777 gdb_byte *buf, *retbuf;
1778 unsigned char header[4];
c906108c 1779
be391dca
TT
1780 if (info->readin)
1781 return;
dce234bc 1782 info->buffer = NULL;
b315ab21 1783 info->map_addr = NULL;
be391dca 1784 info->readin = 1;
188dd5d6 1785
9e0ac564 1786 if (dwarf2_section_empty_p (info))
dce234bc 1787 return;
c906108c 1788
3019eac3
DE
1789 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1790 abfd = sectp->owner;
1791
dce234bc
PP
1792 /* Check if the file has a 4-byte header indicating compression. */
1793 if (info->size > sizeof (header)
1794 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1795 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1796 {
1797 /* Upon decompression, update the buffer and its size. */
1798 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1799 {
1800 zlib_decompress_section (objfile, sectp, &info->buffer,
1801 &info->size);
1802 return;
1803 }
1804 }
4bdf3d34 1805
dce234bc
PP
1806#ifdef HAVE_MMAP
1807 if (pagesize == 0)
1808 pagesize = getpagesize ();
2e276125 1809
dce234bc
PP
1810 /* Only try to mmap sections which are large enough: we don't want to
1811 waste space due to fragmentation. Also, only try mmap for sections
1812 without relocations. */
1813
1814 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1815 {
b315ab21
TG
1816 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1817 MAP_PRIVATE, sectp->filepos,
1818 &info->map_addr, &info->map_len);
dce234bc 1819
b315ab21 1820 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1821 {
be391dca 1822#if HAVE_POSIX_MADVISE
b315ab21 1823 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1824#endif
dce234bc
PP
1825 return;
1826 }
1827 }
1828#endif
1829
1830 /* If we get here, we are a normal, not-compressed section. */
1831 info->buffer = buf
1832 = obstack_alloc (&objfile->objfile_obstack, info->size);
1833
1834 /* When debugging .o files, we may need to apply relocations; see
1835 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1836 We never compress sections in .o files, so we only need to
1837 try this when the section is not compressed. */
ac8035ab 1838 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1839 if (retbuf != NULL)
1840 {
1841 info->buffer = retbuf;
1842 return;
1843 }
1844
1845 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1846 || bfd_bread (buf, info->size, abfd) != info->size)
1847 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1848 bfd_get_filename (abfd));
1849}
1850
9e0ac564
TT
1851/* A helper function that returns the size of a section in a safe way.
1852 If you are positive that the section has been read before using the
1853 size, then it is safe to refer to the dwarf2_section_info object's
1854 "size" field directly. In other cases, you must call this
1855 function, because for compressed sections the size field is not set
1856 correctly until the section has been read. */
1857
1858static bfd_size_type
1859dwarf2_section_size (struct objfile *objfile,
1860 struct dwarf2_section_info *info)
1861{
1862 if (!info->readin)
1863 dwarf2_read_section (objfile, info);
1864 return info->size;
1865}
1866
dce234bc 1867/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1868 SECTION_NAME. */
af34e669 1869
dce234bc 1870void
3017a003
TG
1871dwarf2_get_section_info (struct objfile *objfile,
1872 enum dwarf2_section_enum sect,
dce234bc
PP
1873 asection **sectp, gdb_byte **bufp,
1874 bfd_size_type *sizep)
1875{
1876 struct dwarf2_per_objfile *data
1877 = objfile_data (objfile, dwarf2_objfile_data_key);
1878 struct dwarf2_section_info *info;
a3b2a86b
TT
1879
1880 /* We may see an objfile without any DWARF, in which case we just
1881 return nothing. */
1882 if (data == NULL)
1883 {
1884 *sectp = NULL;
1885 *bufp = NULL;
1886 *sizep = 0;
1887 return;
1888 }
3017a003
TG
1889 switch (sect)
1890 {
1891 case DWARF2_DEBUG_FRAME:
1892 info = &data->frame;
1893 break;
1894 case DWARF2_EH_FRAME:
1895 info = &data->eh_frame;
1896 break;
1897 default:
1898 gdb_assert_not_reached ("unexpected section");
1899 }
dce234bc 1900
9e0ac564 1901 dwarf2_read_section (objfile, info);
dce234bc
PP
1902
1903 *sectp = info->asection;
1904 *bufp = info->buffer;
1905 *sizep = info->size;
1906}
1907
9291a0cd 1908\f
7b9f3c50
DE
1909/* DWARF quick_symbols_functions support. */
1910
1911/* TUs can share .debug_line entries, and there can be a lot more TUs than
1912 unique line tables, so we maintain a separate table of all .debug_line
1913 derived entries to support the sharing.
1914 All the quick functions need is the list of file names. We discard the
1915 line_header when we're done and don't need to record it here. */
1916struct quick_file_names
1917{
1918 /* The offset in .debug_line of the line table. We hash on this. */
1919 unsigned int offset;
1920
1921 /* The number of entries in file_names, real_names. */
1922 unsigned int num_file_names;
1923
1924 /* The file names from the line table, after being run through
1925 file_full_name. */
1926 const char **file_names;
1927
1928 /* The file names from the line table after being run through
1929 gdb_realpath. These are computed lazily. */
1930 const char **real_names;
1931};
1932
1933/* When using the index (and thus not using psymtabs), each CU has an
1934 object of this type. This is used to hold information needed by
1935 the various "quick" methods. */
1936struct dwarf2_per_cu_quick_data
1937{
1938 /* The file table. This can be NULL if there was no file table
1939 or it's currently not read in.
1940 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1941 struct quick_file_names *file_names;
1942
1943 /* The corresponding symbol table. This is NULL if symbols for this
1944 CU have not yet been read. */
1945 struct symtab *symtab;
1946
1947 /* A temporary mark bit used when iterating over all CUs in
1948 expand_symtabs_matching. */
1949 unsigned int mark : 1;
1950
1951 /* True if we've tried to read the file table and found there isn't one.
1952 There will be no point in trying to read it again next time. */
1953 unsigned int no_file_data : 1;
1954};
1955
1956/* Hash function for a quick_file_names. */
1957
1958static hashval_t
1959hash_file_name_entry (const void *e)
1960{
1961 const struct quick_file_names *file_data = e;
1962
1963 return file_data->offset;
1964}
1965
1966/* Equality function for a quick_file_names. */
1967
1968static int
1969eq_file_name_entry (const void *a, const void *b)
1970{
1971 const struct quick_file_names *ea = a;
1972 const struct quick_file_names *eb = b;
1973
1974 return ea->offset == eb->offset;
1975}
1976
1977/* Delete function for a quick_file_names. */
1978
1979static void
1980delete_file_name_entry (void *e)
1981{
1982 struct quick_file_names *file_data = e;
1983 int i;
1984
1985 for (i = 0; i < file_data->num_file_names; ++i)
1986 {
1987 xfree ((void*) file_data->file_names[i]);
1988 if (file_data->real_names)
1989 xfree ((void*) file_data->real_names[i]);
1990 }
1991
1992 /* The space for the struct itself lives on objfile_obstack,
1993 so we don't free it here. */
1994}
1995
1996/* Create a quick_file_names hash table. */
1997
1998static htab_t
1999create_quick_file_names_table (unsigned int nr_initial_entries)
2000{
2001 return htab_create_alloc (nr_initial_entries,
2002 hash_file_name_entry, eq_file_name_entry,
2003 delete_file_name_entry, xcalloc, xfree);
2004}
9291a0cd 2005
918dd910
JK
2006/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2007 have to be created afterwards. You should call age_cached_comp_units after
2008 processing PER_CU->CU. dw2_setup must have been already called. */
2009
2010static void
2011load_cu (struct dwarf2_per_cu_data *per_cu)
2012{
3019eac3 2013 if (per_cu->is_debug_types)
e5fe5e75 2014 load_full_type_unit (per_cu);
918dd910 2015 else
95554aad 2016 load_full_comp_unit (per_cu, language_minimal);
918dd910 2017
918dd910 2018 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
2019
2020 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2021}
2022
a0f42c21 2023/* Read in the symbols for PER_CU. */
2fdf6df6 2024
9291a0cd 2025static void
a0f42c21 2026dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
2027{
2028 struct cleanup *back_to;
2029
2030 back_to = make_cleanup (dwarf2_release_queue, NULL);
2031
95554aad
TT
2032 if (dwarf2_per_objfile->using_index
2033 ? per_cu->v.quick->symtab == NULL
2034 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2035 {
2036 queue_comp_unit (per_cu, language_minimal);
2037 load_cu (per_cu);
2038 }
9291a0cd 2039
a0f42c21 2040 process_queue ();
9291a0cd
TT
2041
2042 /* Age the cache, releasing compilation units that have not
2043 been used recently. */
2044 age_cached_comp_units ();
2045
2046 do_cleanups (back_to);
2047}
2048
2049/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2050 the objfile from which this CU came. Returns the resulting symbol
2051 table. */
2fdf6df6 2052
9291a0cd 2053static struct symtab *
a0f42c21 2054dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2055{
95554aad 2056 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2057 if (!per_cu->v.quick->symtab)
2058 {
2059 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2060 increment_reading_symtab ();
a0f42c21 2061 dw2_do_instantiate_symtab (per_cu);
95554aad 2062 process_cu_includes ();
9291a0cd
TT
2063 do_cleanups (back_to);
2064 }
2065 return per_cu->v.quick->symtab;
2066}
2067
1fd400ff 2068/* Return the CU given its index. */
2fdf6df6 2069
1fd400ff
TT
2070static struct dwarf2_per_cu_data *
2071dw2_get_cu (int index)
2072{
2073 if (index >= dwarf2_per_objfile->n_comp_units)
2074 {
2075 index -= dwarf2_per_objfile->n_comp_units;
d467dd73 2076 return dwarf2_per_objfile->all_type_units[index];
1fd400ff
TT
2077 }
2078 return dwarf2_per_objfile->all_comp_units[index];
2079}
2080
9291a0cd
TT
2081/* A helper function that knows how to read a 64-bit value in a way
2082 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2083 otherwise. */
2fdf6df6 2084
9291a0cd
TT
2085static int
2086extract_cu_value (const char *bytes, ULONGEST *result)
2087{
2088 if (sizeof (ULONGEST) < 8)
2089 {
2090 int i;
2091
2092 /* Ignore the upper 4 bytes if they are all zero. */
2093 for (i = 0; i < 4; ++i)
2094 if (bytes[i + 4] != 0)
2095 return 0;
2096
2097 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2098 }
2099 else
2100 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2101 return 1;
2102}
2103
2104/* Read the CU list from the mapped index, and use it to create all
2105 the CU objects for this objfile. Return 0 if something went wrong,
2106 1 if everything went ok. */
2fdf6df6 2107
9291a0cd 2108static int
1fd400ff
TT
2109create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2110 offset_type cu_list_elements)
9291a0cd
TT
2111{
2112 offset_type i;
9291a0cd
TT
2113
2114 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2115 dwarf2_per_objfile->all_comp_units
2116 = obstack_alloc (&objfile->objfile_obstack,
2117 dwarf2_per_objfile->n_comp_units
2118 * sizeof (struct dwarf2_per_cu_data *));
2119
2120 for (i = 0; i < cu_list_elements; i += 2)
2121 {
2122 struct dwarf2_per_cu_data *the_cu;
2123 ULONGEST offset, length;
2124
2125 if (!extract_cu_value (cu_list, &offset)
2126 || !extract_cu_value (cu_list + 8, &length))
2127 return 0;
2128 cu_list += 2 * 8;
2129
2130 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2131 struct dwarf2_per_cu_data);
b64f50a1 2132 the_cu->offset.sect_off = offset;
9291a0cd
TT
2133 the_cu->length = length;
2134 the_cu->objfile = objfile;
3019eac3 2135 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
9291a0cd
TT
2136 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2137 struct dwarf2_per_cu_quick_data);
2138 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2139 }
2140
2141 return 1;
2142}
2143
1fd400ff 2144/* Create the signatured type hash table from the index. */
673bfd45 2145
1fd400ff 2146static int
673bfd45 2147create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2148 struct dwarf2_section_info *section,
673bfd45
DE
2149 const gdb_byte *bytes,
2150 offset_type elements)
1fd400ff
TT
2151{
2152 offset_type i;
673bfd45 2153 htab_t sig_types_hash;
1fd400ff 2154
d467dd73
DE
2155 dwarf2_per_objfile->n_type_units = elements / 3;
2156 dwarf2_per_objfile->all_type_units
1fd400ff 2157 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2158 dwarf2_per_objfile->n_type_units
1fd400ff
TT
2159 * sizeof (struct dwarf2_per_cu_data *));
2160
673bfd45 2161 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2162
2163 for (i = 0; i < elements; i += 3)
2164 {
52dc124a
DE
2165 struct signatured_type *sig_type;
2166 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2167 void **slot;
2168
2169 if (!extract_cu_value (bytes, &offset)
52dc124a 2170 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
2171 return 0;
2172 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2173 bytes += 3 * 8;
2174
52dc124a 2175 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2176 struct signatured_type);
52dc124a 2177 sig_type->signature = signature;
3019eac3
DE
2178 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2179 sig_type->per_cu.is_debug_types = 1;
2180 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2181 sig_type->per_cu.offset.sect_off = offset;
2182 sig_type->per_cu.objfile = objfile;
2183 sig_type->per_cu.v.quick
1fd400ff
TT
2184 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2185 struct dwarf2_per_cu_quick_data);
2186
52dc124a
DE
2187 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2188 *slot = sig_type;
1fd400ff 2189
52dc124a 2190 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
1fd400ff
TT
2191 }
2192
673bfd45 2193 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2194
2195 return 1;
2196}
2197
9291a0cd
TT
2198/* Read the address map data from the mapped index, and use it to
2199 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2200
9291a0cd
TT
2201static void
2202create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2203{
2204 const gdb_byte *iter, *end;
2205 struct obstack temp_obstack;
2206 struct addrmap *mutable_map;
2207 struct cleanup *cleanup;
2208 CORE_ADDR baseaddr;
2209
2210 obstack_init (&temp_obstack);
2211 cleanup = make_cleanup_obstack_free (&temp_obstack);
2212 mutable_map = addrmap_create_mutable (&temp_obstack);
2213
2214 iter = index->address_table;
2215 end = iter + index->address_table_size;
2216
2217 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2218
2219 while (iter < end)
2220 {
2221 ULONGEST hi, lo, cu_index;
2222 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2223 iter += 8;
2224 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2225 iter += 8;
2226 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2227 iter += 4;
2228
2229 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2230 dw2_get_cu (cu_index));
9291a0cd
TT
2231 }
2232
2233 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2234 &objfile->objfile_obstack);
2235 do_cleanups (cleanup);
2236}
2237
59d7bcaf
JK
2238/* The hash function for strings in the mapped index. This is the same as
2239 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2240 implementation. This is necessary because the hash function is tied to the
2241 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2242 SYMBOL_HASH_NEXT.
2243
2244 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2245
9291a0cd 2246static hashval_t
559a7a62 2247mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2248{
2249 const unsigned char *str = (const unsigned char *) p;
2250 hashval_t r = 0;
2251 unsigned char c;
2252
2253 while ((c = *str++) != 0)
559a7a62
JK
2254 {
2255 if (index_version >= 5)
2256 c = tolower (c);
2257 r = r * 67 + c - 113;
2258 }
9291a0cd
TT
2259
2260 return r;
2261}
2262
2263/* Find a slot in the mapped index INDEX for the object named NAME.
2264 If NAME is found, set *VEC_OUT to point to the CU vector in the
2265 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2266
9291a0cd
TT
2267static int
2268find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2269 offset_type **vec_out)
2270{
0cf03b49
JK
2271 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2272 offset_type hash;
9291a0cd 2273 offset_type slot, step;
559a7a62 2274 int (*cmp) (const char *, const char *);
9291a0cd 2275
0cf03b49
JK
2276 if (current_language->la_language == language_cplus
2277 || current_language->la_language == language_java
2278 || current_language->la_language == language_fortran)
2279 {
2280 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2281 not contain any. */
2282 const char *paren = strchr (name, '(');
2283
2284 if (paren)
2285 {
2286 char *dup;
2287
2288 dup = xmalloc (paren - name + 1);
2289 memcpy (dup, name, paren - name);
2290 dup[paren - name] = 0;
2291
2292 make_cleanup (xfree, dup);
2293 name = dup;
2294 }
2295 }
2296
559a7a62 2297 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2298 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2299 simulate our NAME being searched is also lowercased. */
2300 hash = mapped_index_string_hash ((index->version == 4
2301 && case_sensitivity == case_sensitive_off
2302 ? 5 : index->version),
2303 name);
2304
3876f04e
DE
2305 slot = hash & (index->symbol_table_slots - 1);
2306 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2307 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2308
2309 for (;;)
2310 {
2311 /* Convert a slot number to an offset into the table. */
2312 offset_type i = 2 * slot;
2313 const char *str;
3876f04e 2314 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2315 {
2316 do_cleanups (back_to);
2317 return 0;
2318 }
9291a0cd 2319
3876f04e 2320 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2321 if (!cmp (name, str))
9291a0cd
TT
2322 {
2323 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2324 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2325 do_cleanups (back_to);
9291a0cd
TT
2326 return 1;
2327 }
2328
3876f04e 2329 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2330 }
2331}
2332
2333/* Read the index file. If everything went ok, initialize the "quick"
2334 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2335
9291a0cd
TT
2336static int
2337dwarf2_read_index (struct objfile *objfile)
2338{
9291a0cd
TT
2339 char *addr;
2340 struct mapped_index *map;
b3b272e1 2341 offset_type *metadata;
ac0b195c
KW
2342 const gdb_byte *cu_list;
2343 const gdb_byte *types_list = NULL;
2344 offset_type version, cu_list_elements;
2345 offset_type types_list_elements = 0;
1fd400ff 2346 int i;
9291a0cd 2347
9e0ac564 2348 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2349 return 0;
82430852
JK
2350
2351 /* Older elfutils strip versions could keep the section in the main
2352 executable while splitting it for the separate debug info file. */
2353 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2354 & SEC_HAS_CONTENTS) == 0)
2355 return 0;
2356
9291a0cd
TT
2357 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2358
2359 addr = dwarf2_per_objfile->gdb_index.buffer;
2360 /* Version check. */
1fd400ff 2361 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2362 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2363 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2364 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2365 indices. */
831adc1f 2366 if (version < 4)
481860b3
GB
2367 {
2368 static int warning_printed = 0;
2369 if (!warning_printed)
2370 {
2371 warning (_("Skipping obsolete .gdb_index section in %s."),
2372 objfile->name);
2373 warning_printed = 1;
2374 }
2375 return 0;
2376 }
2377 /* Index version 4 uses a different hash function than index version
2378 5 and later.
2379
2380 Versions earlier than 6 did not emit psymbols for inlined
2381 functions. Using these files will cause GDB not to be able to
2382 set breakpoints on inlined functions by name, so we ignore these
2383 indices unless the --use-deprecated-index-sections command line
2384 option was supplied. */
2385 if (version < 6 && !use_deprecated_index_sections)
2386 {
2387 static int warning_printed = 0;
2388 if (!warning_printed)
2389 {
2390 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2391 "--use-deprecated-index-sections to use them anyway"),
2392 objfile->name);
2393 warning_printed = 1;
2394 }
2395 return 0;
2396 }
2397 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2398 longer backward compatible. */
156942c7 2399 if (version > 7)
594e8718 2400 return 0;
9291a0cd
TT
2401
2402 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2403 map->version = version;
b3b272e1 2404 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2405
2406 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2407
2408 i = 0;
2409 cu_list = addr + MAYBE_SWAP (metadata[i]);
2410 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2411 / 8);
1fd400ff
TT
2412 ++i;
2413
987d643c
TT
2414 types_list = addr + MAYBE_SWAP (metadata[i]);
2415 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2416 - MAYBE_SWAP (metadata[i]))
2417 / 8);
2418 ++i;
1fd400ff
TT
2419
2420 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2421 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2422 - MAYBE_SWAP (metadata[i]));
2423 ++i;
2424
3876f04e
DE
2425 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2426 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2427 - MAYBE_SWAP (metadata[i]))
2428 / (2 * sizeof (offset_type)));
1fd400ff 2429 ++i;
9291a0cd 2430
1fd400ff
TT
2431 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2432
0fefef59
DE
2433 /* Don't use the index if it's empty. */
2434 if (map->symbol_table_slots == 0)
2435 return 0;
2436
1fd400ff
TT
2437 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2438 return 0;
2439
8b70b953
TT
2440 if (types_list_elements)
2441 {
2442 struct dwarf2_section_info *section;
2443
2444 /* We can only handle a single .debug_types when we have an
2445 index. */
2446 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2447 return 0;
2448
2449 section = VEC_index (dwarf2_section_info_def,
2450 dwarf2_per_objfile->types, 0);
2451
2452 if (!create_signatured_type_table_from_index (objfile, section,
2453 types_list,
2454 types_list_elements))
2455 return 0;
2456 }
9291a0cd
TT
2457
2458 create_addrmap_from_index (objfile, map);
2459
2460 dwarf2_per_objfile->index_table = map;
2461 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2462 dwarf2_per_objfile->quick_file_names_table =
2463 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2464
2465 return 1;
2466}
2467
2468/* A helper for the "quick" functions which sets the global
2469 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2470
9291a0cd
TT
2471static void
2472dw2_setup (struct objfile *objfile)
2473{
2474 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2475 gdb_assert (dwarf2_per_objfile);
2476}
2477
dee91e82 2478/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2479
dee91e82
DE
2480static void
2481dw2_get_file_names_reader (const struct die_reader_specs *reader,
2482 gdb_byte *info_ptr,
2483 struct die_info *comp_unit_die,
2484 int has_children,
2485 void *data)
9291a0cd 2486{
dee91e82
DE
2487 struct dwarf2_cu *cu = reader->cu;
2488 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2489 struct objfile *objfile = dwarf2_per_objfile->objfile;
7b9f3c50 2490 struct line_header *lh;
9291a0cd 2491 struct attribute *attr;
dee91e82 2492 int i;
9291a0cd 2493 char *name, *comp_dir;
7b9f3c50
DE
2494 void **slot;
2495 struct quick_file_names *qfn;
2496 unsigned int line_offset;
9291a0cd 2497
07261596
TT
2498 /* Our callers never want to match partial units -- instead they
2499 will match the enclosing full CU. */
2500 if (comp_unit_die->tag == DW_TAG_partial_unit)
2501 {
2502 this_cu->v.quick->no_file_data = 1;
2503 return;
2504 }
2505
7b9f3c50
DE
2506 lh = NULL;
2507 slot = NULL;
2508 line_offset = 0;
dee91e82
DE
2509
2510 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2511 if (attr)
2512 {
7b9f3c50
DE
2513 struct quick_file_names find_entry;
2514
2515 line_offset = DW_UNSND (attr);
2516
2517 /* We may have already read in this line header (TU line header sharing).
2518 If we have we're done. */
2519 find_entry.offset = line_offset;
2520 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2521 &find_entry, INSERT);
2522 if (*slot != NULL)
2523 {
7b9f3c50 2524 this_cu->v.quick->file_names = *slot;
dee91e82 2525 return;
7b9f3c50
DE
2526 }
2527
3019eac3 2528 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2529 }
2530 if (lh == NULL)
2531 {
7b9f3c50 2532 this_cu->v.quick->no_file_data = 1;
dee91e82 2533 return;
9291a0cd
TT
2534 }
2535
7b9f3c50
DE
2536 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2537 qfn->offset = line_offset;
2538 gdb_assert (slot != NULL);
2539 *slot = qfn;
9291a0cd 2540
dee91e82 2541 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2542
7b9f3c50
DE
2543 qfn->num_file_names = lh->num_file_names;
2544 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2545 lh->num_file_names * sizeof (char *));
9291a0cd 2546 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2547 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2548 qfn->real_names = NULL;
9291a0cd 2549
7b9f3c50 2550 free_line_header (lh);
7b9f3c50
DE
2551
2552 this_cu->v.quick->file_names = qfn;
dee91e82
DE
2553}
2554
2555/* A helper for the "quick" functions which attempts to read the line
2556 table for THIS_CU. */
2557
2558static struct quick_file_names *
2559dw2_get_file_names (struct objfile *objfile,
2560 struct dwarf2_per_cu_data *this_cu)
2561{
2562 if (this_cu->v.quick->file_names != NULL)
2563 return this_cu->v.quick->file_names;
2564 /* If we know there is no line data, no point in looking again. */
2565 if (this_cu->v.quick->no_file_data)
2566 return NULL;
2567
3019eac3
DE
2568 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2569 in the stub for CUs, there's is no need to lookup the DWO file.
2570 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2571 DWO file. */
2572 if (this_cu->is_debug_types)
fd820528 2573 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
3019eac3
DE
2574 else
2575 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2576
2577 if (this_cu->v.quick->no_file_data)
2578 return NULL;
2579 return this_cu->v.quick->file_names;
9291a0cd
TT
2580}
2581
2582/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2583 real path for a given file name from the line table. */
2fdf6df6 2584
9291a0cd 2585static const char *
7b9f3c50
DE
2586dw2_get_real_path (struct objfile *objfile,
2587 struct quick_file_names *qfn, int index)
9291a0cd 2588{
7b9f3c50
DE
2589 if (qfn->real_names == NULL)
2590 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2591 qfn->num_file_names, sizeof (char *));
9291a0cd 2592
7b9f3c50
DE
2593 if (qfn->real_names[index] == NULL)
2594 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2595
7b9f3c50 2596 return qfn->real_names[index];
9291a0cd
TT
2597}
2598
2599static struct symtab *
2600dw2_find_last_source_symtab (struct objfile *objfile)
2601{
2602 int index;
ae2de4f8 2603
9291a0cd
TT
2604 dw2_setup (objfile);
2605 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2606 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2607}
2608
7b9f3c50
DE
2609/* Traversal function for dw2_forget_cached_source_info. */
2610
2611static int
2612dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2613{
7b9f3c50 2614 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2615
7b9f3c50 2616 if (file_data->real_names)
9291a0cd 2617 {
7b9f3c50 2618 int i;
9291a0cd 2619
7b9f3c50 2620 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2621 {
7b9f3c50
DE
2622 xfree ((void*) file_data->real_names[i]);
2623 file_data->real_names[i] = NULL;
9291a0cd
TT
2624 }
2625 }
7b9f3c50
DE
2626
2627 return 1;
2628}
2629
2630static void
2631dw2_forget_cached_source_info (struct objfile *objfile)
2632{
2633 dw2_setup (objfile);
2634
2635 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2636 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2637}
2638
f8eba3c6
TT
2639/* Helper function for dw2_map_symtabs_matching_filename that expands
2640 the symtabs and calls the iterator. */
2641
2642static int
2643dw2_map_expand_apply (struct objfile *objfile,
2644 struct dwarf2_per_cu_data *per_cu,
2645 const char *name,
2646 const char *full_path, const char *real_path,
2647 int (*callback) (struct symtab *, void *),
2648 void *data)
2649{
2650 struct symtab *last_made = objfile->symtabs;
2651
2652 /* Don't visit already-expanded CUs. */
2653 if (per_cu->v.quick->symtab)
2654 return 0;
2655
2656 /* This may expand more than one symtab, and we want to iterate over
2657 all of them. */
a0f42c21 2658 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2659
2660 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2661 objfile->symtabs, last_made);
2662}
2663
2664/* Implementation of the map_symtabs_matching_filename method. */
2665
9291a0cd 2666static int
f8eba3c6
TT
2667dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2668 const char *full_path, const char *real_path,
2669 int (*callback) (struct symtab *, void *),
2670 void *data)
9291a0cd
TT
2671{
2672 int i;
c011a4f4 2673 const char *name_basename = lbasename (name);
4aac40c8
TT
2674 int name_len = strlen (name);
2675 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
2676
2677 dw2_setup (objfile);
ae2de4f8 2678
1fd400ff 2679 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2680 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2681 {
2682 int j;
e254ef6a 2683 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2684 struct quick_file_names *file_data;
9291a0cd 2685
3d7bb9d9 2686 /* We only need to look at symtabs not already expanded. */
e254ef6a 2687 if (per_cu->v.quick->symtab)
9291a0cd
TT
2688 continue;
2689
7b9f3c50
DE
2690 file_data = dw2_get_file_names (objfile, per_cu);
2691 if (file_data == NULL)
9291a0cd
TT
2692 continue;
2693
7b9f3c50 2694 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2695 {
7b9f3c50 2696 const char *this_name = file_data->file_names[j];
9291a0cd 2697
4aac40c8
TT
2698 if (FILENAME_CMP (name, this_name) == 0
2699 || (!is_abs && compare_filenames_for_search (this_name,
2700 name, name_len)))
9291a0cd 2701 {
f8eba3c6
TT
2702 if (dw2_map_expand_apply (objfile, per_cu,
2703 name, full_path, real_path,
2704 callback, data))
2705 return 1;
4aac40c8 2706 }
9291a0cd 2707
c011a4f4
DE
2708 /* Before we invoke realpath, which can get expensive when many
2709 files are involved, do a quick comparison of the basenames. */
2710 if (! basenames_may_differ
2711 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2712 continue;
2713
9291a0cd
TT
2714 if (full_path != NULL)
2715 {
7b9f3c50
DE
2716 const char *this_real_name = dw2_get_real_path (objfile,
2717 file_data, j);
9291a0cd 2718
7b9f3c50 2719 if (this_real_name != NULL
4aac40c8
TT
2720 && (FILENAME_CMP (full_path, this_real_name) == 0
2721 || (!is_abs
2722 && compare_filenames_for_search (this_real_name,
2723 name, name_len))))
9291a0cd 2724 {
f8eba3c6
TT
2725 if (dw2_map_expand_apply (objfile, per_cu,
2726 name, full_path, real_path,
2727 callback, data))
2728 return 1;
9291a0cd
TT
2729 }
2730 }
2731
2732 if (real_path != NULL)
2733 {
7b9f3c50
DE
2734 const char *this_real_name = dw2_get_real_path (objfile,
2735 file_data, j);
9291a0cd 2736
7b9f3c50 2737 if (this_real_name != NULL
4aac40c8
TT
2738 && (FILENAME_CMP (real_path, this_real_name) == 0
2739 || (!is_abs
2740 && compare_filenames_for_search (this_real_name,
2741 name, name_len))))
9291a0cd 2742 {
f8eba3c6
TT
2743 if (dw2_map_expand_apply (objfile, per_cu,
2744 name, full_path, real_path,
2745 callback, data))
2746 return 1;
9291a0cd
TT
2747 }
2748 }
2749 }
2750 }
2751
9291a0cd
TT
2752 return 0;
2753}
2754
2755static struct symtab *
2756dw2_lookup_symbol (struct objfile *objfile, int block_index,
2757 const char *name, domain_enum domain)
2758{
774b6a14 2759 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2760 instead. */
2761 return NULL;
2762}
2763
2764/* A helper function that expands all symtabs that hold an object
156942c7
DE
2765 named NAME. If WANT_SPECIFIC_BLOCK is non-zero, only look for
2766 symbols in block BLOCK_KIND. */
2fdf6df6 2767
9291a0cd 2768static void
156942c7
DE
2769dw2_do_expand_symtabs_matching (struct objfile *objfile,
2770 int want_specific_block,
2771 enum block_enum block_kind,
2772 const char *name, domain_enum domain)
9291a0cd 2773{
156942c7
DE
2774 struct mapped_index *index;
2775
9291a0cd
TT
2776 dw2_setup (objfile);
2777
156942c7
DE
2778 index = dwarf2_per_objfile->index_table;
2779
ae2de4f8 2780 /* index_table is NULL if OBJF_READNOW. */
156942c7 2781 if (index)
9291a0cd
TT
2782 {
2783 offset_type *vec;
2784
156942c7 2785 if (find_slot_in_mapped_hash (index, name, &vec))
9291a0cd
TT
2786 {
2787 offset_type i, len = MAYBE_SWAP (*vec);
2788 for (i = 0; i < len; ++i)
2789 {
156942c7
DE
2790 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[i + 1]);
2791 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
e254ef6a 2792 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
156942c7
DE
2793 int want_static = block_kind != GLOBAL_BLOCK;
2794 /* This value is only valid for index versions >= 7. */
2795 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
2796 gdb_index_symbol_kind symbol_kind =
2797 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
2798
2799 if (want_specific_block
2800 && index->version >= 7
2801 && want_static != is_static)
2802 continue;
2803
2804 /* Only check the symbol's kind if it has one.
2805 Indices prior to version 7 don't record it. */
2806 if (index->version >= 7)
2807 {
2808 switch (domain)
2809 {
2810 case VAR_DOMAIN:
2811 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
2812 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
2813 /* Some types are also in VAR_DOMAIN. */
2814 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
2815 continue;
2816 break;
2817 case STRUCT_DOMAIN:
2818 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
2819 continue;
2820 break;
2821 case LABEL_DOMAIN:
2822 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
2823 continue;
2824 break;
2825 default:
2826 break;
2827 }
2828 }
1fd400ff 2829
a0f42c21 2830 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2831 }
2832 }
2833 }
2834}
2835
774b6a14
TT
2836static void
2837dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2838 enum block_enum block_kind, const char *name,
774b6a14 2839 domain_enum domain)
9291a0cd 2840{
156942c7 2841 dw2_do_expand_symtabs_matching (objfile, 1, block_kind, name, domain);
9291a0cd
TT
2842}
2843
2844static void
2845dw2_print_stats (struct objfile *objfile)
2846{
2847 int i, count;
2848
2849 dw2_setup (objfile);
2850 count = 0;
1fd400ff 2851 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2852 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2853 {
e254ef6a 2854 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2855
e254ef6a 2856 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2857 ++count;
2858 }
2859 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2860}
2861
2862static void
2863dw2_dump (struct objfile *objfile)
2864{
2865 /* Nothing worth printing. */
2866}
2867
2868static void
2869dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2870 struct section_offsets *delta)
2871{
2872 /* There's nothing to relocate here. */
2873}
2874
2875static void
2876dw2_expand_symtabs_for_function (struct objfile *objfile,
2877 const char *func_name)
2878{
156942c7
DE
2879 /* Note: It doesn't matter what we pass for block_kind here. */
2880 dw2_do_expand_symtabs_matching (objfile, 0, GLOBAL_BLOCK, func_name,
2881 VAR_DOMAIN);
9291a0cd
TT
2882}
2883
2884static void
2885dw2_expand_all_symtabs (struct objfile *objfile)
2886{
2887 int i;
2888
2889 dw2_setup (objfile);
1fd400ff
TT
2890
2891 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2892 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2893 {
e254ef6a 2894 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2895
a0f42c21 2896 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2897 }
2898}
2899
2900static void
2901dw2_expand_symtabs_with_filename (struct objfile *objfile,
2902 const char *filename)
2903{
2904 int i;
2905
2906 dw2_setup (objfile);
d4637a04
DE
2907
2908 /* We don't need to consider type units here.
2909 This is only called for examining code, e.g. expand_line_sal.
2910 There can be an order of magnitude (or more) more type units
2911 than comp units, and we avoid them if we can. */
2912
2913 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2914 {
2915 int j;
e254ef6a 2916 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2917 struct quick_file_names *file_data;
9291a0cd 2918
3d7bb9d9 2919 /* We only need to look at symtabs not already expanded. */
e254ef6a 2920 if (per_cu->v.quick->symtab)
9291a0cd
TT
2921 continue;
2922
7b9f3c50
DE
2923 file_data = dw2_get_file_names (objfile, per_cu);
2924 if (file_data == NULL)
9291a0cd
TT
2925 continue;
2926
7b9f3c50 2927 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2928 {
7b9f3c50 2929 const char *this_name = file_data->file_names[j];
1ef75ecc 2930 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2931 {
a0f42c21 2932 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2933 break;
2934 }
2935 }
2936 }
2937}
2938
356d9f9d
TT
2939/* A helper function for dw2_find_symbol_file that finds the primary
2940 file name for a given CU. This is a die_reader_func. */
2941
2942static void
2943dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
2944 gdb_byte *info_ptr,
2945 struct die_info *comp_unit_die,
2946 int has_children,
2947 void *data)
2948{
2949 const char **result_ptr = data;
2950 struct dwarf2_cu *cu = reader->cu;
2951 struct attribute *attr;
2952
2953 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
2954 if (attr == NULL)
2955 *result_ptr = NULL;
2956 else
2957 *result_ptr = DW_STRING (attr);
2958}
2959
dd786858 2960static const char *
9291a0cd
TT
2961dw2_find_symbol_file (struct objfile *objfile, const char *name)
2962{
e254ef6a 2963 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2964 offset_type *vec;
7b9f3c50 2965 struct quick_file_names *file_data;
356d9f9d 2966 const char *filename;
9291a0cd
TT
2967
2968 dw2_setup (objfile);
2969
ae2de4f8 2970 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2971 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2972 {
2973 struct symtab *s;
2974
d790cf0a
DE
2975 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
2976 {
2977 struct blockvector *bv = BLOCKVECTOR (s);
2978 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2979 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2980
2981 if (sym)
2982 return sym->symtab->filename;
2983 }
96408a79
SA
2984 return NULL;
2985 }
9291a0cd
TT
2986
2987 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2988 name, &vec))
2989 return NULL;
2990
2991 /* Note that this just looks at the very first one named NAME -- but
2992 actually we are looking for a function. find_main_filename
2993 should be rewritten so that it doesn't require a custom hook. It
2994 could just use the ordinary symbol tables. */
2995 /* vec[0] is the length, which must always be >0. */
156942c7 2996 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
9291a0cd 2997
356d9f9d
TT
2998 if (per_cu->v.quick->symtab != NULL)
2999 return per_cu->v.quick->symtab->filename;
3000
663b969e
TT
3001 init_cutu_and_read_dies (per_cu, 0, 0, dw2_get_primary_filename_reader,
3002 &filename);
9291a0cd 3003
356d9f9d 3004 return filename;
9291a0cd
TT
3005}
3006
3007static void
40658b94
PH
3008dw2_map_matching_symbols (const char * name, domain_enum namespace,
3009 struct objfile *objfile, int global,
3010 int (*callback) (struct block *,
3011 struct symbol *, void *),
2edb89d3
JK
3012 void *data, symbol_compare_ftype *match,
3013 symbol_compare_ftype *ordered_compare)
9291a0cd 3014{
40658b94 3015 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
3016 current language is Ada for a non-Ada objfile using GNU index. As Ada
3017 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
3018}
3019
3020static void
f8eba3c6
TT
3021dw2_expand_symtabs_matching
3022 (struct objfile *objfile,
3023 int (*file_matcher) (const char *, void *),
e078317b 3024 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
3025 enum search_domain kind,
3026 void *data)
9291a0cd
TT
3027{
3028 int i;
3029 offset_type iter;
4b5246aa 3030 struct mapped_index *index;
9291a0cd
TT
3031
3032 dw2_setup (objfile);
ae2de4f8
DE
3033
3034 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
3035 if (!dwarf2_per_objfile->index_table)
3036 return;
4b5246aa 3037 index = dwarf2_per_objfile->index_table;
9291a0cd 3038
7b08b9eb 3039 if (file_matcher != NULL)
24c79950
TT
3040 {
3041 struct cleanup *cleanup;
3042 htab_t visited_found, visited_not_found;
3043
3044 visited_found = htab_create_alloc (10,
3045 htab_hash_pointer, htab_eq_pointer,
3046 NULL, xcalloc, xfree);
3047 cleanup = make_cleanup_htab_delete (visited_found);
3048 visited_not_found = htab_create_alloc (10,
3049 htab_hash_pointer, htab_eq_pointer,
3050 NULL, xcalloc, xfree);
3051 make_cleanup_htab_delete (visited_not_found);
3052
3053 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3054 + dwarf2_per_objfile->n_type_units); ++i)
3055 {
3056 int j;
3057 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3058 struct quick_file_names *file_data;
3059 void **slot;
7b08b9eb 3060
24c79950 3061 per_cu->v.quick->mark = 0;
3d7bb9d9 3062
24c79950
TT
3063 /* We only need to look at symtabs not already expanded. */
3064 if (per_cu->v.quick->symtab)
3065 continue;
7b08b9eb 3066
24c79950
TT
3067 file_data = dw2_get_file_names (objfile, per_cu);
3068 if (file_data == NULL)
3069 continue;
7b08b9eb 3070
24c79950
TT
3071 if (htab_find (visited_not_found, file_data) != NULL)
3072 continue;
3073 else if (htab_find (visited_found, file_data) != NULL)
3074 {
3075 per_cu->v.quick->mark = 1;
3076 continue;
3077 }
3078
3079 for (j = 0; j < file_data->num_file_names; ++j)
3080 {
3081 if (file_matcher (file_data->file_names[j], data))
3082 {
3083 per_cu->v.quick->mark = 1;
3084 break;
3085 }
3086 }
3087
3088 slot = htab_find_slot (per_cu->v.quick->mark
3089 ? visited_found
3090 : visited_not_found,
3091 file_data, INSERT);
3092 *slot = file_data;
3093 }
3094
3095 do_cleanups (cleanup);
3096 }
9291a0cd 3097
3876f04e 3098 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3099 {
3100 offset_type idx = 2 * iter;
3101 const char *name;
3102 offset_type *vec, vec_len, vec_idx;
3103
3876f04e 3104 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3105 continue;
3106
3876f04e 3107 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3108
e078317b 3109 if (! (*name_matcher) (name, data))
9291a0cd
TT
3110 continue;
3111
3112 /* The name was matched, now expand corresponding CUs that were
3113 marked. */
4b5246aa 3114 vec = (offset_type *) (index->constant_pool
3876f04e 3115 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3116 vec_len = MAYBE_SWAP (vec[0]);
3117 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3118 {
e254ef6a 3119 struct dwarf2_per_cu_data *per_cu;
156942c7
DE
3120 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3121 gdb_index_symbol_kind symbol_kind =
3122 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3123 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3124
3125 /* Don't crash on bad data. */
3126 if (cu_index >= (dwarf2_per_objfile->n_comp_units
667e0a4b 3127 + dwarf2_per_objfile->n_type_units))
156942c7 3128 continue;
1fd400ff 3129
156942c7
DE
3130 /* Only check the symbol's kind if it has one.
3131 Indices prior to version 7 don't record it. */
3132 if (index->version >= 7)
3133 {
3134 switch (kind)
3135 {
3136 case VARIABLES_DOMAIN:
3137 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3138 continue;
3139 break;
3140 case FUNCTIONS_DOMAIN:
3141 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3142 continue;
3143 break;
3144 case TYPES_DOMAIN:
3145 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3146 continue;
3147 break;
3148 default:
3149 break;
3150 }
3151 }
3152
3153 per_cu = dw2_get_cu (cu_index);
7b08b9eb 3154 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3155 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3156 }
3157 }
3158}
3159
9703b513
TT
3160/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3161 symtab. */
3162
3163static struct symtab *
3164recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3165{
3166 int i;
3167
3168 if (BLOCKVECTOR (symtab) != NULL
3169 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3170 return symtab;
3171
a3ec0bb1
DE
3172 if (symtab->includes == NULL)
3173 return NULL;
3174
9703b513
TT
3175 for (i = 0; symtab->includes[i]; ++i)
3176 {
a3ec0bb1 3177 struct symtab *s = symtab->includes[i];
9703b513
TT
3178
3179 s = recursively_find_pc_sect_symtab (s, pc);
3180 if (s != NULL)
3181 return s;
3182 }
3183
3184 return NULL;
3185}
3186
9291a0cd
TT
3187static struct symtab *
3188dw2_find_pc_sect_symtab (struct objfile *objfile,
3189 struct minimal_symbol *msymbol,
3190 CORE_ADDR pc,
3191 struct obj_section *section,
3192 int warn_if_readin)
3193{
3194 struct dwarf2_per_cu_data *data;
9703b513 3195 struct symtab *result;
9291a0cd
TT
3196
3197 dw2_setup (objfile);
3198
3199 if (!objfile->psymtabs_addrmap)
3200 return NULL;
3201
3202 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3203 if (!data)
3204 return NULL;
3205
3206 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3207 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3208 paddress (get_objfile_arch (objfile), pc));
3209
9703b513
TT
3210 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3211 gdb_assert (result != NULL);
3212 return result;
9291a0cd
TT
3213}
3214
9291a0cd 3215static void
44b13c5a 3216dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3217 void *data, int need_fullname)
9291a0cd
TT
3218{
3219 int i;
24c79950
TT
3220 struct cleanup *cleanup;
3221 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3222 NULL, xcalloc, xfree);
9291a0cd 3223
24c79950 3224 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3225 dw2_setup (objfile);
ae2de4f8 3226
24c79950
TT
3227 /* We can ignore file names coming from already-expanded CUs. */
3228 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3229 + dwarf2_per_objfile->n_type_units); ++i)
3230 {
3231 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3232
3233 if (per_cu->v.quick->symtab)
3234 {
3235 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3236 INSERT);
3237
3238 *slot = per_cu->v.quick->file_names;
3239 }
3240 }
3241
1fd400ff 3242 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3243 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
3244 {
3245 int j;
e254ef6a 3246 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3247 struct quick_file_names *file_data;
24c79950 3248 void **slot;
9291a0cd 3249
3d7bb9d9 3250 /* We only need to look at symtabs not already expanded. */
e254ef6a 3251 if (per_cu->v.quick->symtab)
9291a0cd
TT
3252 continue;
3253
7b9f3c50
DE
3254 file_data = dw2_get_file_names (objfile, per_cu);
3255 if (file_data == NULL)
9291a0cd
TT
3256 continue;
3257
24c79950
TT
3258 slot = htab_find_slot (visited, file_data, INSERT);
3259 if (*slot)
3260 {
3261 /* Already visited. */
3262 continue;
3263 }
3264 *slot = file_data;
3265
7b9f3c50 3266 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3267 {
74e2f255
DE
3268 const char *this_real_name;
3269
3270 if (need_fullname)
3271 this_real_name = dw2_get_real_path (objfile, file_data, j);
3272 else
3273 this_real_name = NULL;
7b9f3c50 3274 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3275 }
3276 }
24c79950
TT
3277
3278 do_cleanups (cleanup);
9291a0cd
TT
3279}
3280
3281static int
3282dw2_has_symbols (struct objfile *objfile)
3283{
3284 return 1;
3285}
3286
3287const struct quick_symbol_functions dwarf2_gdb_index_functions =
3288{
3289 dw2_has_symbols,
3290 dw2_find_last_source_symtab,
3291 dw2_forget_cached_source_info,
f8eba3c6 3292 dw2_map_symtabs_matching_filename,
9291a0cd 3293 dw2_lookup_symbol,
774b6a14 3294 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3295 dw2_print_stats,
3296 dw2_dump,
3297 dw2_relocate,
3298 dw2_expand_symtabs_for_function,
3299 dw2_expand_all_symtabs,
3300 dw2_expand_symtabs_with_filename,
3301 dw2_find_symbol_file,
40658b94 3302 dw2_map_matching_symbols,
9291a0cd
TT
3303 dw2_expand_symtabs_matching,
3304 dw2_find_pc_sect_symtab,
9291a0cd
TT
3305 dw2_map_symbol_filenames
3306};
3307
3308/* Initialize for reading DWARF for this objfile. Return 0 if this
3309 file will use psymtabs, or 1 if using the GNU index. */
3310
3311int
3312dwarf2_initialize_objfile (struct objfile *objfile)
3313{
3314 /* If we're about to read full symbols, don't bother with the
3315 indices. In this case we also don't care if some other debug
3316 format is making psymtabs, because they are all about to be
3317 expanded anyway. */
3318 if ((objfile->flags & OBJF_READNOW))
3319 {
3320 int i;
3321
3322 dwarf2_per_objfile->using_index = 1;
3323 create_all_comp_units (objfile);
0e50663e 3324 create_all_type_units (objfile);
7b9f3c50
DE
3325 dwarf2_per_objfile->quick_file_names_table =
3326 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3327
1fd400ff 3328 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3329 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3330 {
e254ef6a 3331 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3332
e254ef6a
DE
3333 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3334 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3335 }
3336
3337 /* Return 1 so that gdb sees the "quick" functions. However,
3338 these functions will be no-ops because we will have expanded
3339 all symtabs. */
3340 return 1;
3341 }
3342
3343 if (dwarf2_read_index (objfile))
3344 return 1;
3345
9291a0cd
TT
3346 return 0;
3347}
3348
3349\f
3350
dce234bc
PP
3351/* Build a partial symbol table. */
3352
3353void
f29dff0a 3354dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3355{
f29dff0a 3356 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3357 {
3358 init_psymbol_list (objfile, 1024);
3359 }
3360
d146bf1e 3361 dwarf2_build_psymtabs_hard (objfile);
c906108c 3362}
c906108c 3363
1ce1cefd
DE
3364/* Return the total length of the CU described by HEADER. */
3365
3366static unsigned int
3367get_cu_length (const struct comp_unit_head *header)
3368{
3369 return header->initial_length_size + header->length;
3370}
3371
45452591
DE
3372/* Return TRUE if OFFSET is within CU_HEADER. */
3373
3374static inline int
b64f50a1 3375offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3376{
b64f50a1 3377 sect_offset bottom = { cu_header->offset.sect_off };
1ce1cefd 3378 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
9a619af0 3379
b64f50a1 3380 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3381}
3382
3b80fe9b
DE
3383/* Find the base address of the compilation unit for range lists and
3384 location lists. It will normally be specified by DW_AT_low_pc.
3385 In DWARF-3 draft 4, the base address could be overridden by
3386 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3387 compilation units with discontinuous ranges. */
3388
3389static void
3390dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3391{
3392 struct attribute *attr;
3393
3394 cu->base_known = 0;
3395 cu->base_address = 0;
3396
3397 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3398 if (attr)
3399 {
3400 cu->base_address = DW_ADDR (attr);
3401 cu->base_known = 1;
3402 }
3403 else
3404 {
3405 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3406 if (attr)
3407 {
3408 cu->base_address = DW_ADDR (attr);
3409 cu->base_known = 1;
3410 }
3411 }
3412}
3413
93311388
DE
3414/* Read in the comp unit header information from the debug_info at info_ptr.
3415 NOTE: This leaves members offset, first_die_offset to be filled in
3416 by the caller. */
107d2387 3417
fe1b8b76 3418static gdb_byte *
107d2387 3419read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3420 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3421{
3422 int signed_addr;
891d2f0b 3423 unsigned int bytes_read;
c764a876
DE
3424
3425 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3426 cu_header->initial_length_size = bytes_read;
3427 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3428 info_ptr += bytes_read;
107d2387
AC
3429 cu_header->version = read_2_bytes (abfd, info_ptr);
3430 info_ptr += 2;
b64f50a1
JK
3431 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3432 &bytes_read);
613e1657 3433 info_ptr += bytes_read;
107d2387
AC
3434 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3435 info_ptr += 1;
3436 signed_addr = bfd_get_sign_extend_vma (abfd);
3437 if (signed_addr < 0)
8e65ff28 3438 internal_error (__FILE__, __LINE__,
e2e0b3e5 3439 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3440 cu_header->signed_addr_p = signed_addr;
c764a876 3441
107d2387
AC
3442 return info_ptr;
3443}
3444
9ff913ba
DE
3445/* Subroutine of read_and_check_comp_unit_head and
3446 read_and_check_type_unit_head to simplify them.
3447 Perform various error checking on the header. */
3448
3449static void
3450error_check_comp_unit_head (struct comp_unit_head *header,
4bdcc0c1
DE
3451 struct dwarf2_section_info *section,
3452 struct dwarf2_section_info *abbrev_section)
9ff913ba
DE
3453{
3454 bfd *abfd = section->asection->owner;
3455 const char *filename = bfd_get_filename (abfd);
3456
3457 if (header->version != 2 && header->version != 3 && header->version != 4)
3458 error (_("Dwarf Error: wrong version in compilation unit header "
3459 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3460 filename);
3461
b64f50a1 3462 if (header->abbrev_offset.sect_off
9ff913ba
DE
3463 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3464 &dwarf2_per_objfile->abbrev))
3465 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3466 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3467 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3468 filename);
3469
3470 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3471 avoid potential 32-bit overflow. */
1ce1cefd 3472 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
9ff913ba
DE
3473 > section->size)
3474 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3475 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3476 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3477 filename);
3478}
3479
3480/* Read in a CU/TU header and perform some basic error checking.
3481 The contents of the header are stored in HEADER.
3482 The result is a pointer to the start of the first DIE. */
adabb602 3483
fe1b8b76 3484static gdb_byte *
9ff913ba
DE
3485read_and_check_comp_unit_head (struct comp_unit_head *header,
3486 struct dwarf2_section_info *section,
4bdcc0c1 3487 struct dwarf2_section_info *abbrev_section,
9ff913ba
DE
3488 gdb_byte *info_ptr,
3489 int is_debug_types_section)
72bf9492 3490{
fe1b8b76 3491 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3492 bfd *abfd = section->asection->owner;
72bf9492 3493
b64f50a1 3494 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3495
72bf9492
DJ
3496 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3497
460c1c54
CC
3498 /* If we're reading a type unit, skip over the signature and
3499 type_offset fields. */
b0df02fd 3500 if (is_debug_types_section)
460c1c54
CC
3501 info_ptr += 8 /*signature*/ + header->offset_size;
3502
b64f50a1 3503 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3504
4bdcc0c1 3505 error_check_comp_unit_head (header, section, abbrev_section);
72bf9492
DJ
3506
3507 return info_ptr;
3508}
3509
348e048f
DE
3510/* Read in the types comp unit header information from .debug_types entry at
3511 types_ptr. The result is a pointer to one past the end of the header. */
3512
3513static gdb_byte *
9ff913ba
DE
3514read_and_check_type_unit_head (struct comp_unit_head *header,
3515 struct dwarf2_section_info *section,
4bdcc0c1 3516 struct dwarf2_section_info *abbrev_section,
9ff913ba 3517 gdb_byte *info_ptr,
dee91e82
DE
3518 ULONGEST *signature,
3519 cu_offset *type_offset_in_tu)
348e048f 3520{
9ff913ba
DE
3521 gdb_byte *beg_of_comp_unit = info_ptr;
3522 bfd *abfd = section->asection->owner;
348e048f 3523
b64f50a1 3524 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3525
9ff913ba 3526 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3527
9ff913ba
DE
3528 /* If we're reading a type unit, skip over the signature and
3529 type_offset fields. */
3530 if (signature != NULL)
3531 *signature = read_8_bytes (abfd, info_ptr);
3532 info_ptr += 8;
dee91e82
DE
3533 if (type_offset_in_tu != NULL)
3534 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3535 header->offset_size);
9ff913ba
DE
3536 info_ptr += header->offset_size;
3537
b64f50a1 3538 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3539
4bdcc0c1 3540 error_check_comp_unit_head (header, section, abbrev_section);
9ff913ba
DE
3541
3542 return info_ptr;
348e048f
DE
3543}
3544
aaa75496
JB
3545/* Allocate a new partial symtab for file named NAME and mark this new
3546 partial symtab as being an include of PST. */
3547
3548static void
3549dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3550 struct objfile *objfile)
3551{
3552 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3553
3554 subpst->section_offsets = pst->section_offsets;
3555 subpst->textlow = 0;
3556 subpst->texthigh = 0;
3557
3558 subpst->dependencies = (struct partial_symtab **)
3559 obstack_alloc (&objfile->objfile_obstack,
3560 sizeof (struct partial_symtab *));
3561 subpst->dependencies[0] = pst;
3562 subpst->number_of_dependencies = 1;
3563
3564 subpst->globals_offset = 0;
3565 subpst->n_global_syms = 0;
3566 subpst->statics_offset = 0;
3567 subpst->n_static_syms = 0;
3568 subpst->symtab = NULL;
3569 subpst->read_symtab = pst->read_symtab;
3570 subpst->readin = 0;
3571
3572 /* No private part is necessary for include psymtabs. This property
3573 can be used to differentiate between such include psymtabs and
10b3939b 3574 the regular ones. */
58a9656e 3575 subpst->read_symtab_private = NULL;
aaa75496
JB
3576}
3577
3578/* Read the Line Number Program data and extract the list of files
3579 included by the source file represented by PST. Build an include
d85a05f0 3580 partial symtab for each of these included files. */
aaa75496
JB
3581
3582static void
3583dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
3584 struct die_info *die,
3585 struct partial_symtab *pst)
aaa75496 3586{
d85a05f0
DJ
3587 struct line_header *lh = NULL;
3588 struct attribute *attr;
aaa75496 3589
d85a05f0
DJ
3590 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3591 if (attr)
3019eac3 3592 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
3593 if (lh == NULL)
3594 return; /* No linetable, so no includes. */
3595
c6da4cef 3596 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3597 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3598
3599 free_line_header (lh);
3600}
3601
348e048f 3602static hashval_t
52dc124a 3603hash_signatured_type (const void *item)
348e048f 3604{
52dc124a 3605 const struct signatured_type *sig_type = item;
9a619af0 3606
348e048f 3607 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 3608 return sig_type->signature;
348e048f
DE
3609}
3610
3611static int
52dc124a 3612eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
3613{
3614 const struct signatured_type *lhs = item_lhs;
3615 const struct signatured_type *rhs = item_rhs;
9a619af0 3616
348e048f
DE
3617 return lhs->signature == rhs->signature;
3618}
3619
1fd400ff
TT
3620/* Allocate a hash table for signatured types. */
3621
3622static htab_t
673bfd45 3623allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3624{
3625 return htab_create_alloc_ex (41,
52dc124a
DE
3626 hash_signatured_type,
3627 eq_signatured_type,
1fd400ff
TT
3628 NULL,
3629 &objfile->objfile_obstack,
3630 hashtab_obstack_allocate,
3631 dummy_obstack_deallocate);
3632}
3633
d467dd73 3634/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3635
3636static int
d467dd73 3637add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3638{
3639 struct signatured_type *sigt = *slot;
3640 struct dwarf2_per_cu_data ***datap = datum;
3641
3642 **datap = &sigt->per_cu;
3643 ++*datap;
3644
3645 return 1;
3646}
3647
3019eac3
DE
3648/* Create the hash table of all entries in the .debug_types section.
3649 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3650 The result is a pointer to the hash table or NULL if there are
3651 no types. */
348e048f 3652
3019eac3
DE
3653static htab_t
3654create_debug_types_hash_table (struct dwo_file *dwo_file,
3655 VEC (dwarf2_section_info_def) *types)
348e048f 3656{
3019eac3 3657 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 3658 htab_t types_htab = NULL;
8b70b953
TT
3659 int ix;
3660 struct dwarf2_section_info *section;
4bdcc0c1 3661 struct dwarf2_section_info *abbrev_section;
348e048f 3662
3019eac3
DE
3663 if (VEC_empty (dwarf2_section_info_def, types))
3664 return NULL;
348e048f 3665
4bdcc0c1
DE
3666 abbrev_section = (dwo_file != NULL
3667 ? &dwo_file->sections.abbrev
3668 : &dwarf2_per_objfile->abbrev);
3669
09406207
DE
3670 if (dwarf2_read_debug)
3671 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
3672 dwo_file ? ".dwo" : "",
3673 bfd_get_filename (abbrev_section->asection->owner));
3674
8b70b953 3675 for (ix = 0;
3019eac3 3676 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
3677 ++ix)
3678 {
3019eac3 3679 bfd *abfd;
8b70b953 3680 gdb_byte *info_ptr, *end_ptr;
348e048f 3681
8b70b953
TT
3682 dwarf2_read_section (objfile, section);
3683 info_ptr = section->buffer;
348e048f 3684
8b70b953
TT
3685 if (info_ptr == NULL)
3686 continue;
348e048f 3687
3019eac3
DE
3688 /* We can't set abfd until now because the section may be empty or
3689 not present, in which case section->asection will be NULL. */
3690 abfd = section->asection->owner;
3691
8b70b953 3692 if (types_htab == NULL)
3019eac3
DE
3693 {
3694 if (dwo_file)
3695 types_htab = allocate_dwo_unit_table (objfile);
3696 else
3697 types_htab = allocate_signatured_type_table (objfile);
3698 }
348e048f 3699
dee91e82
DE
3700 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3701 because we don't need to read any dies: the signature is in the
3702 header. */
8b70b953
TT
3703
3704 end_ptr = info_ptr + section->size;
3705 while (info_ptr < end_ptr)
3706 {
b64f50a1 3707 sect_offset offset;
3019eac3 3708 cu_offset type_offset_in_tu;
8b70b953 3709 ULONGEST signature;
52dc124a 3710 struct signatured_type *sig_type;
3019eac3 3711 struct dwo_unit *dwo_tu;
8b70b953
TT
3712 void **slot;
3713 gdb_byte *ptr = info_ptr;
9ff913ba 3714 struct comp_unit_head header;
dee91e82 3715 unsigned int length;
348e048f 3716
b64f50a1 3717 offset.sect_off = ptr - section->buffer;
348e048f 3718
8b70b953 3719 /* We need to read the type's signature in order to build the hash
9ff913ba 3720 table, but we don't need anything else just yet. */
348e048f 3721
4bdcc0c1
DE
3722 ptr = read_and_check_type_unit_head (&header, section,
3723 abbrev_section, ptr,
3019eac3 3724 &signature, &type_offset_in_tu);
6caca83c 3725
1ce1cefd 3726 length = get_cu_length (&header);
dee91e82 3727
6caca83c 3728 /* Skip dummy type units. */
dee91e82
DE
3729 if (ptr >= info_ptr + length
3730 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 3731 {
1ce1cefd 3732 info_ptr += length;
6caca83c
CC
3733 continue;
3734 }
8b70b953 3735
3019eac3
DE
3736 if (dwo_file)
3737 {
3738 sig_type = NULL;
3739 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3740 struct dwo_unit);
3741 dwo_tu->dwo_file = dwo_file;
3742 dwo_tu->signature = signature;
3743 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3744 dwo_tu->info_or_types_section = section;
3745 dwo_tu->offset = offset;
3746 dwo_tu->length = length;
3747 }
3748 else
3749 {
3750 /* N.B.: type_offset is not usable if this type uses a DWO file.
3751 The real type_offset is in the DWO file. */
3752 dwo_tu = NULL;
3753 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3754 struct signatured_type);
3755 sig_type->signature = signature;
3756 sig_type->type_offset_in_tu = type_offset_in_tu;
3757 sig_type->per_cu.objfile = objfile;
3758 sig_type->per_cu.is_debug_types = 1;
3759 sig_type->per_cu.info_or_types_section = section;
3760 sig_type->per_cu.offset = offset;
3761 sig_type->per_cu.length = length;
3762 }
8b70b953 3763
3019eac3
DE
3764 slot = htab_find_slot (types_htab,
3765 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3766 INSERT);
8b70b953
TT
3767 gdb_assert (slot != NULL);
3768 if (*slot != NULL)
3769 {
3019eac3
DE
3770 sect_offset dup_offset;
3771
3772 if (dwo_file)
3773 {
3774 const struct dwo_unit *dup_tu = *slot;
3775
3776 dup_offset = dup_tu->offset;
3777 }
3778 else
3779 {
3780 const struct signatured_type *dup_tu = *slot;
3781
3782 dup_offset = dup_tu->per_cu.offset;
3783 }
b3c8eb43 3784
8b70b953
TT
3785 complaint (&symfile_complaints,
3786 _("debug type entry at offset 0x%x is duplicate to the "
3787 "entry at offset 0x%x, signature 0x%s"),
3019eac3 3788 offset.sect_off, dup_offset.sect_off,
8b70b953 3789 phex (signature, sizeof (signature)));
8b70b953 3790 }
3019eac3 3791 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 3792
09406207 3793 if (dwarf2_read_debug)
8b70b953 3794 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
3795 offset.sect_off,
3796 phex (signature, sizeof (signature)));
348e048f 3797
dee91e82 3798 info_ptr += length;
8b70b953 3799 }
348e048f
DE
3800 }
3801
3019eac3
DE
3802 return types_htab;
3803}
3804
3805/* Create the hash table of all entries in the .debug_types section,
3806 and initialize all_type_units.
3807 The result is zero if there is an error (e.g. missing .debug_types section),
3808 otherwise non-zero. */
3809
3810static int
3811create_all_type_units (struct objfile *objfile)
3812{
3813 htab_t types_htab;
3814 struct dwarf2_per_cu_data **iter;
3815
3816 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3817 if (types_htab == NULL)
3818 {
3819 dwarf2_per_objfile->signatured_types = NULL;
3820 return 0;
3821 }
3822
348e048f
DE
3823 dwarf2_per_objfile->signatured_types = types_htab;
3824
d467dd73
DE
3825 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3826 dwarf2_per_objfile->all_type_units
1fd400ff 3827 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3828 dwarf2_per_objfile->n_type_units
1fd400ff 3829 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3830 iter = &dwarf2_per_objfile->all_type_units[0];
3831 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3832 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3833 == dwarf2_per_objfile->n_type_units);
1fd400ff 3834
348e048f
DE
3835 return 1;
3836}
3837
380bca97 3838/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 3839 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
3840
3841static struct signatured_type *
e319fa28 3842lookup_signatured_type (ULONGEST sig)
348e048f
DE
3843{
3844 struct signatured_type find_entry, *entry;
3845
3846 if (dwarf2_per_objfile->signatured_types == NULL)
3847 {
3848 complaint (&symfile_complaints,
55f1336d 3849 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 3850 return NULL;
348e048f
DE
3851 }
3852
3853 find_entry.signature = sig;
3854 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3855 return entry;
3856}
42e7ad6c
DE
3857\f
3858/* Low level DIE reading support. */
348e048f 3859
d85a05f0
DJ
3860/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3861
3862static void
3863init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 3864 struct dwarf2_cu *cu,
3019eac3
DE
3865 struct dwarf2_section_info *section,
3866 struct dwo_file *dwo_file)
d85a05f0 3867{
fceca515 3868 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 3869 reader->abfd = section->asection->owner;
d85a05f0 3870 reader->cu = cu;
3019eac3 3871 reader->dwo_file = dwo_file;
dee91e82
DE
3872 reader->die_section = section;
3873 reader->buffer = section->buffer;
f664829e 3874 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
3875}
3876
fd820528 3877/* Initialize a CU (or TU) and read its DIEs.
3019eac3 3878 If the CU defers to a DWO file, read the DWO file as well.
dee91e82
DE
3879
3880 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3881 Otherwise, a new CU is allocated with xmalloc.
3882
3883 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3884 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3885
3886 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 3887 linker) then DIE_READER_FUNC will not get called. */
aaa75496 3888
70221824 3889static void
fd820528
DE
3890init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3891 int use_existing_cu, int keep,
3892 die_reader_func_ftype *die_reader_func,
3893 void *data)
c906108c 3894{
dee91e82 3895 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
3896 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3897 bfd *abfd = section->asection->owner;
dee91e82
DE
3898 struct dwarf2_cu *cu;
3899 gdb_byte *begin_info_ptr, *info_ptr;
3900 struct die_reader_specs reader;
d85a05f0 3901 struct die_info *comp_unit_die;
dee91e82 3902 int has_children;
d85a05f0 3903 struct attribute *attr;
dee91e82
DE
3904 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3905 struct signatured_type *sig_type = NULL;
4bdcc0c1 3906 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
3907 /* Non-zero if CU currently points to a DWO file and we need to
3908 reread it. When this happens we need to reread the skeleton die
3909 before we can reread the DWO file. */
3910 int rereading_dwo_cu = 0;
c906108c 3911
09406207
DE
3912 if (dwarf2_die_debug)
3913 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
3914 this_cu->is_debug_types ? "type" : "comp",
3915 this_cu->offset.sect_off);
3916
dee91e82
DE
3917 if (use_existing_cu)
3918 gdb_assert (keep);
23745b47 3919
dee91e82
DE
3920 cleanups = make_cleanup (null_cleanup, NULL);
3921
3922 /* This is cheap if the section is already read in. */
3923 dwarf2_read_section (objfile, section);
3924
3925 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1 3926 abbrev_section = &dwarf2_per_objfile->abbrev;
dee91e82
DE
3927
3928 if (use_existing_cu && this_cu->cu != NULL)
3929 {
3930 cu = this_cu->cu;
42e7ad6c
DE
3931
3932 /* If this CU is from a DWO file we need to start over, we need to
3933 refetch the attributes from the skeleton CU.
3934 This could be optimized by retrieving those attributes from when we
3935 were here the first time: the previous comp_unit_die was stored in
3936 comp_unit_obstack. But there's no data yet that we need this
3937 optimization. */
3938 if (cu->dwo_unit != NULL)
3939 rereading_dwo_cu = 1;
dee91e82
DE
3940 }
3941 else
3942 {
3943 /* If !use_existing_cu, this_cu->cu must be NULL. */
3944 gdb_assert (this_cu->cu == NULL);
3945
3946 cu = xmalloc (sizeof (*cu));
3947 init_one_comp_unit (cu, this_cu);
3948
3949 /* If an error occurs while loading, release our storage. */
3950 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
42e7ad6c 3951 }
dee91e82 3952
42e7ad6c
DE
3953 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
3954 {
3955 /* We already have the header, there's no need to read it in again. */
3956 info_ptr += cu->header.first_die_offset.cu_off;
3957 }
3958 else
3959 {
3019eac3 3960 if (this_cu->is_debug_types)
dee91e82
DE
3961 {
3962 ULONGEST signature;
42e7ad6c 3963 cu_offset type_offset_in_tu;
dee91e82 3964
4bdcc0c1
DE
3965 info_ptr = read_and_check_type_unit_head (&cu->header, section,
3966 abbrev_section, info_ptr,
42e7ad6c
DE
3967 &signature,
3968 &type_offset_in_tu);
dee91e82 3969
42e7ad6c
DE
3970 /* Since per_cu is the first member of struct signatured_type,
3971 we can go from a pointer to one to a pointer to the other. */
3972 sig_type = (struct signatured_type *) this_cu;
3973 gdb_assert (sig_type->signature == signature);
3974 gdb_assert (sig_type->type_offset_in_tu.cu_off
3975 == type_offset_in_tu.cu_off);
dee91e82
DE
3976 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3977
42e7ad6c
DE
3978 /* LENGTH has not been set yet for type units if we're
3979 using .gdb_index. */
1ce1cefd 3980 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
3981
3982 /* Establish the type offset that can be used to lookup the type. */
3983 sig_type->type_offset_in_section.sect_off =
3984 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
3985 }
3986 else
3987 {
4bdcc0c1
DE
3988 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
3989 abbrev_section,
3990 info_ptr, 0);
dee91e82
DE
3991
3992 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 3993 gdb_assert (this_cu->length == get_cu_length (&cu->header));
dee91e82
DE
3994 }
3995 }
10b3939b 3996
6caca83c 3997 /* Skip dummy compilation units. */
dee91e82 3998 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
3999 || peek_abbrev_code (abfd, info_ptr) == 0)
4000 {
dee91e82 4001 do_cleanups (cleanups);
21b2bd31 4002 return;
6caca83c
CC
4003 }
4004
433df2d4
DE
4005 /* If we don't have them yet, read the abbrevs for this compilation unit.
4006 And if we need to read them now, make sure they're freed when we're
42e7ad6c
DE
4007 done. Note that it's important that if the CU had an abbrev table
4008 on entry we don't free it when we're done: Somewhere up the call stack
4009 it may be in use. */
433df2d4 4010 if (cu->abbrev_table == NULL)
dee91e82 4011 {
4bdcc0c1 4012 dwarf2_read_abbrevs (cu, abbrev_section);
dee91e82
DE
4013 make_cleanup (dwarf2_free_abbrev_table, cu);
4014 }
42e7ad6c
DE
4015 else if (rereading_dwo_cu)
4016 {
4017 dwarf2_free_abbrev_table (cu);
4018 dwarf2_read_abbrevs (cu, abbrev_section);
4019 }
af703f96 4020
dee91e82 4021 /* Read the top level CU/TU die. */
3019eac3 4022 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 4023 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 4024
3019eac3
DE
4025 /* If we have a DWO stub, process it and then read in the DWO file.
4026 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
4027 a DWO CU, that this test will fail. */
4028 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4029 if (attr)
4030 {
4031 char *dwo_name = DW_STRING (attr);
42e7ad6c 4032 const char *comp_dir_string;
3019eac3
DE
4033 struct dwo_unit *dwo_unit;
4034 ULONGEST signature; /* Or dwo_id. */
42e7ad6c 4035 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
3019eac3 4036 int i,num_extra_attrs;
4bdcc0c1 4037 struct dwarf2_section_info *dwo_abbrev_section;
3019eac3
DE
4038
4039 if (has_children)
4040 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
4041 " has children (offset 0x%x) [in module %s]"),
4042 this_cu->offset.sect_off, bfd_get_filename (abfd));
4043
4044 /* These attributes aren't processed until later:
4045 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4046 However, the attribute is found in the stub which we won't have later.
4047 In order to not impose this complication on the rest of the code,
4048 we read them here and copy them to the DWO CU/TU die. */
3019eac3
DE
4049
4050 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4051 DWO file. */
42e7ad6c 4052 stmt_list = NULL;
3019eac3
DE
4053 if (! this_cu->is_debug_types)
4054 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
4055 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
4056 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
4057 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
42e7ad6c 4058 comp_dir = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3019eac3
DE
4059
4060 /* There should be a DW_AT_addr_base attribute here (if needed).
4061 We need the value before we can process DW_FORM_GNU_addr_index. */
4062 cu->addr_base = 0;
3019eac3
DE
4063 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
4064 if (attr)
2e3cf129
DE
4065 cu->addr_base = DW_UNSND (attr);
4066
4067 /* There should be a DW_AT_ranges_base attribute here (if needed).
4068 We need the value before we can process DW_AT_ranges. */
4069 cu->ranges_base = 0;
4070 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_ranges_base, cu);
4071 if (attr)
4072 cu->ranges_base = DW_UNSND (attr);
3019eac3
DE
4073
4074 if (this_cu->is_debug_types)
4075 {
4076 gdb_assert (sig_type != NULL);
4077 signature = sig_type->signature;
4078 }
4079 else
4080 {
4081 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4082 if (! attr)
4083 error (_("Dwarf Error: missing dwo_id [in module %s]"),
4084 dwo_name);
4085 signature = DW_UNSND (attr);
4086 }
4087
4088 /* We may need the comp_dir in order to find the DWO file. */
42e7ad6c
DE
4089 comp_dir_string = NULL;
4090 if (comp_dir)
4091 comp_dir_string = DW_STRING (comp_dir);
3019eac3
DE
4092
4093 if (this_cu->is_debug_types)
42e7ad6c 4094 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir_string);
3019eac3 4095 else
42e7ad6c 4096 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir_string,
3019eac3
DE
4097 signature);
4098
4099 if (dwo_unit == NULL)
4100 {
4101 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
4102 " with ID %s [in module %s]"),
4103 this_cu->offset.sect_off,
4104 phex (signature, sizeof (signature)),
4105 objfile->name);
4106 }
4107
4108 /* Set up for reading the DWO CU/TU. */
4109 cu->dwo_unit = dwo_unit;
4110 section = dwo_unit->info_or_types_section;
4111 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4bdcc0c1 4112 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
3019eac3
DE
4113 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
4114
4115 if (this_cu->is_debug_types)
4116 {
4117 ULONGEST signature;
4118
4bdcc0c1
DE
4119 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4120 dwo_abbrev_section,
4121 info_ptr,
3019eac3
DE
4122 &signature, NULL);
4123 gdb_assert (sig_type->signature == signature);
4124 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4125 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
3019eac3
DE
4126
4127 /* Establish the type offset that can be used to lookup the type.
4128 For DWO files, we don't know it until now. */
4129 sig_type->type_offset_in_section.sect_off =
4130 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4131 }
4132 else
4133 {
4bdcc0c1
DE
4134 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4135 dwo_abbrev_section,
4136 info_ptr, 0);
3019eac3 4137 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
1ce1cefd 4138 gdb_assert (dwo_unit->length == get_cu_length (&cu->header));
3019eac3
DE
4139 }
4140
4141 /* Discard the original CU's abbrev table, and read the DWO's. */
4142 dwarf2_free_abbrev_table (cu);
4bdcc0c1 4143 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
3019eac3
DE
4144
4145 /* Read in the die, but leave space to copy over the attributes
4146 from the stub. This has the benefit of simplifying the rest of
4147 the code - all the real work is done here. */
4148 num_extra_attrs = ((stmt_list != NULL)
4149 + (low_pc != NULL)
4150 + (high_pc != NULL)
42e7ad6c
DE
4151 + (ranges != NULL)
4152 + (comp_dir != NULL));
3019eac3
DE
4153 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
4154 &has_children, num_extra_attrs);
4155
4156 /* Copy over the attributes from the stub to the DWO die. */
4157 i = comp_unit_die->num_attrs;
4158 if (stmt_list != NULL)
4159 comp_unit_die->attrs[i++] = *stmt_list;
4160 if (low_pc != NULL)
4161 comp_unit_die->attrs[i++] = *low_pc;
4162 if (high_pc != NULL)
4163 comp_unit_die->attrs[i++] = *high_pc;
4164 if (ranges != NULL)
4165 comp_unit_die->attrs[i++] = *ranges;
42e7ad6c
DE
4166 if (comp_dir != NULL)
4167 comp_unit_die->attrs[i++] = *comp_dir;
3019eac3
DE
4168 comp_unit_die->num_attrs += num_extra_attrs;
4169
4170 /* Skip dummy compilation units. */
4171 if (info_ptr >= begin_info_ptr + dwo_unit->length
4172 || peek_abbrev_code (abfd, info_ptr) == 0)
4173 {
4174 do_cleanups (cleanups);
4175 return;
4176 }
4177 }
4178
dee91e82
DE
4179 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4180
4181 if (free_cu_cleanup != NULL)
348e048f 4182 {
dee91e82
DE
4183 if (keep)
4184 {
4185 /* We've successfully allocated this compilation unit. Let our
4186 caller clean it up when finished with it. */
4187 discard_cleanups (free_cu_cleanup);
4188
4189 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4190 So we have to manually free the abbrev table. */
4191 dwarf2_free_abbrev_table (cu);
4192
4193 /* Link this CU into read_in_chain. */
4194 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4195 dwarf2_per_objfile->read_in_chain = this_cu;
4196 }
4197 else
4198 do_cleanups (free_cu_cleanup);
348e048f 4199 }
dee91e82
DE
4200
4201 do_cleanups (cleanups);
4202}
4203
3019eac3
DE
4204/* Read CU/TU THIS_CU in section SECTION,
4205 but do not follow DW_AT_GNU_dwo_name if present.
4206 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4207 have already done the lookup to find the DWO file).
dee91e82
DE
4208
4209 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4210 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4211
4212 We fill in THIS_CU->length.
4213
4214 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4215 linker) then DIE_READER_FUNC will not get called.
4216
4217 THIS_CU->cu is always freed when done.
3019eac3
DE
4218 This is done in order to not leave THIS_CU->cu in a state where we have
4219 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4220
4221static void
4222init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4223 struct dwarf2_section_info *abbrev_section,
3019eac3 4224 struct dwo_file *dwo_file,
dee91e82
DE
4225 die_reader_func_ftype *die_reader_func,
4226 void *data)
4227{
4228 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4229 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4230 bfd *abfd = section->asection->owner;
dee91e82
DE
4231 struct dwarf2_cu cu;
4232 gdb_byte *begin_info_ptr, *info_ptr;
4233 struct die_reader_specs reader;
4234 struct cleanup *cleanups;
4235 struct die_info *comp_unit_die;
4236 int has_children;
4237
09406207
DE
4238 if (dwarf2_die_debug)
4239 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
4240 this_cu->is_debug_types ? "type" : "comp",
4241 this_cu->offset.sect_off);
4242
dee91e82
DE
4243 gdb_assert (this_cu->cu == NULL);
4244
dee91e82
DE
4245 /* This is cheap if the section is already read in. */
4246 dwarf2_read_section (objfile, section);
4247
4248 init_one_comp_unit (&cu, this_cu);
4249
4250 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4251
4252 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4bdcc0c1
DE
4253 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
4254 abbrev_section, info_ptr,
3019eac3 4255 this_cu->is_debug_types);
dee91e82 4256
1ce1cefd 4257 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
4258
4259 /* Skip dummy compilation units. */
4260 if (info_ptr >= begin_info_ptr + this_cu->length
4261 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4262 {
dee91e82 4263 do_cleanups (cleanups);
21b2bd31 4264 return;
93311388 4265 }
72bf9492 4266
dee91e82
DE
4267 dwarf2_read_abbrevs (&cu, abbrev_section);
4268 make_cleanup (dwarf2_free_abbrev_table, &cu);
4269
3019eac3 4270 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4271 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4272
4273 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4274
4275 do_cleanups (cleanups);
4276}
4277
3019eac3
DE
4278/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4279 does not lookup the specified DWO file.
4280 This cannot be used to read DWO files.
dee91e82
DE
4281
4282 THIS_CU->cu is always freed when done.
3019eac3
DE
4283 This is done in order to not leave THIS_CU->cu in a state where we have
4284 to care whether it refers to the "main" CU or the DWO CU.
4285 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4286
4287static void
4288init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4289 die_reader_func_ftype *die_reader_func,
4290 void *data)
4291{
4292 init_cutu_and_read_dies_no_follow (this_cu,
4293 &dwarf2_per_objfile->abbrev,
3019eac3 4294 NULL,
dee91e82
DE
4295 die_reader_func, data);
4296}
4297
4298/* die_reader_func for process_psymtab_comp_unit. */
4299
4300static void
4301process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4302 gdb_byte *info_ptr,
4303 struct die_info *comp_unit_die,
4304 int has_children,
4305 void *data)
4306{
4307 struct dwarf2_cu *cu = reader->cu;
4308 struct objfile *objfile = cu->objfile;
4309 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4310 struct attribute *attr;
4311 CORE_ADDR baseaddr;
4312 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4313 struct partial_symtab *pst;
4314 int has_pc_info;
4315 const char *filename;
95554aad 4316 int *want_partial_unit_ptr = data;
dee91e82 4317
95554aad
TT
4318 if (comp_unit_die->tag == DW_TAG_partial_unit
4319 && (want_partial_unit_ptr == NULL
4320 || !*want_partial_unit_ptr))
dee91e82
DE
4321 return;
4322
95554aad 4323 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4324
4325 cu->list_in_scope = &file_symbols;
c906108c 4326
93311388 4327 /* Allocate a new partial symbol table structure. */
dee91e82 4328 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4329 if (attr == NULL || !DW_STRING (attr))
4330 filename = "";
4331 else
4332 filename = DW_STRING (attr);
93311388 4333 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 4334 filename,
93311388
DE
4335 /* TEXTLOW and TEXTHIGH are set below. */
4336 0,
4337 objfile->global_psymbols.next,
4338 objfile->static_psymbols.next);
9750bca9 4339 pst->psymtabs_addrmap_supported = 1;
72bf9492 4340
dee91e82 4341 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4342 if (attr != NULL)
4343 pst->dirname = DW_STRING (attr);
72bf9492 4344
dee91e82 4345 pst->read_symtab_private = per_cu;
72bf9492 4346
93311388 4347 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4348
0963b4bd 4349 /* Store the function that reads in the rest of the symbol table. */
93311388 4350 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 4351
dee91e82 4352 per_cu->v.psymtab = pst;
c906108c 4353
dee91e82 4354 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4355
93311388
DE
4356 /* Possibly set the default values of LOWPC and HIGHPC from
4357 `DW_AT_ranges'. */
d85a05f0 4358 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4359 &best_highpc, cu, pst);
d85a05f0 4360 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4361 /* Store the contiguous range if it is not empty; it can be empty for
4362 CUs with no code. */
4363 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4364 best_lowpc + baseaddr,
4365 best_highpc + baseaddr - 1, pst);
93311388
DE
4366
4367 /* Check if comp unit has_children.
4368 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4369 If not, there's no more debug_info for this comp unit. */
d85a05f0 4370 if (has_children)
93311388
DE
4371 {
4372 struct partial_die_info *first_die;
4373 CORE_ADDR lowpc, highpc;
31ffec48 4374
93311388
DE
4375 lowpc = ((CORE_ADDR) -1);
4376 highpc = ((CORE_ADDR) 0);
c906108c 4377
dee91e82 4378 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4379
93311388 4380 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4381 ! has_pc_info, cu);
57c22c6c 4382
93311388
DE
4383 /* If we didn't find a lowpc, set it to highpc to avoid
4384 complaints from `maint check'. */
4385 if (lowpc == ((CORE_ADDR) -1))
4386 lowpc = highpc;
10b3939b 4387
93311388
DE
4388 /* If the compilation unit didn't have an explicit address range,
4389 then use the information extracted from its child dies. */
d85a05f0 4390 if (! has_pc_info)
93311388 4391 {
d85a05f0
DJ
4392 best_lowpc = lowpc;
4393 best_highpc = highpc;
93311388
DE
4394 }
4395 }
d85a05f0
DJ
4396 pst->textlow = best_lowpc + baseaddr;
4397 pst->texthigh = best_highpc + baseaddr;
c906108c 4398
93311388
DE
4399 pst->n_global_syms = objfile->global_psymbols.next -
4400 (objfile->global_psymbols.list + pst->globals_offset);
4401 pst->n_static_syms = objfile->static_psymbols.next -
4402 (objfile->static_psymbols.list + pst->statics_offset);
4403 sort_pst_symbols (pst);
c906108c 4404
95554aad
TT
4405 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4406 {
4407 int i;
4408 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4409 struct dwarf2_per_cu_data *iter;
4410
4411 /* Fill in 'dependencies' here; we fill in 'users' in a
4412 post-pass. */
4413 pst->number_of_dependencies = len;
4414 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4415 len * sizeof (struct symtab *));
4416 for (i = 0;
4417 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4418 i, iter);
4419 ++i)
4420 pst->dependencies[i] = iter->v.psymtab;
4421
4422 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4423 }
4424
3019eac3 4425 if (per_cu->is_debug_types)
348e048f
DE
4426 {
4427 /* It's not clear we want to do anything with stmt lists here.
4428 Waiting to see what gcc ultimately does. */
4429 }
d85a05f0 4430 else
93311388
DE
4431 {
4432 /* Get the list of files included in the current compilation unit,
4433 and build a psymtab for each of them. */
dee91e82 4434 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
93311388 4435 }
09406207
DE
4436
4437 if (dwarf2_read_debug)
4438 {
4439 struct gdbarch *gdbarch = get_objfile_arch (objfile);
4440
4441 fprintf_unfiltered (gdb_stdlog,
4442 "Psymtab for %s unit @0x%x: 0x%s - 0x%s"
4443 ", %d global, %d static syms\n",
4444 per_cu->is_debug_types ? "type" : "comp",
4445 per_cu->offset.sect_off,
4446 paddress (gdbarch, pst->textlow),
4447 paddress (gdbarch, pst->texthigh),
4448 pst->n_global_syms, pst->n_static_syms);
4449 }
dee91e82 4450}
ae038cb0 4451
dee91e82
DE
4452/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4453 Process compilation unit THIS_CU for a psymtab. */
4454
4455static void
95554aad
TT
4456process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4457 int want_partial_unit)
dee91e82
DE
4458{
4459 /* If this compilation unit was already read in, free the
4460 cached copy in order to read it in again. This is
4461 necessary because we skipped some symbols when we first
4462 read in the compilation unit (see load_partial_dies).
4463 This problem could be avoided, but the benefit is unclear. */
4464 if (this_cu->cu != NULL)
4465 free_one_cached_comp_unit (this_cu);
4466
3019eac3 4467 gdb_assert (! this_cu->is_debug_types);
fd820528 4468 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
95554aad 4469 &want_partial_unit);
dee91e82
DE
4470
4471 /* Age out any secondary CUs. */
4472 age_cached_comp_units ();
93311388 4473}
ff013f42 4474
348e048f
DE
4475/* Traversal function for htab_traverse_noresize.
4476 Process one .debug_types comp-unit. */
4477
4478static int
dee91e82 4479process_psymtab_type_unit (void **slot, void *info)
348e048f 4480{
dee91e82
DE
4481 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4482 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 4483
fd820528 4484 gdb_assert (per_cu->is_debug_types);
a0f42c21 4485 gdb_assert (info == NULL);
348e048f 4486
dee91e82
DE
4487 /* If this compilation unit was already read in, free the
4488 cached copy in order to read it in again. This is
4489 necessary because we skipped some symbols when we first
4490 read in the compilation unit (see load_partial_dies).
4491 This problem could be avoided, but the benefit is unclear. */
4492 if (per_cu->cu != NULL)
4493 free_one_cached_comp_unit (per_cu);
4494
fd820528
DE
4495 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4496 NULL);
dee91e82
DE
4497
4498 /* Age out any secondary CUs. */
4499 age_cached_comp_units ();
348e048f
DE
4500
4501 return 1;
4502}
4503
4504/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4505 Build partial symbol tables for the .debug_types comp-units. */
4506
4507static void
4508build_type_psymtabs (struct objfile *objfile)
4509{
0e50663e 4510 if (! create_all_type_units (objfile))
348e048f
DE
4511 return;
4512
4513 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
dee91e82 4514 process_psymtab_type_unit, NULL);
348e048f
DE
4515}
4516
60606b2c
TT
4517/* A cleanup function that clears objfile's psymtabs_addrmap field. */
4518
4519static void
4520psymtabs_addrmap_cleanup (void *o)
4521{
4522 struct objfile *objfile = o;
ec61707d 4523
60606b2c
TT
4524 objfile->psymtabs_addrmap = NULL;
4525}
4526
95554aad
TT
4527/* Compute the 'user' field for each psymtab in OBJFILE. */
4528
4529static void
4530set_partial_user (struct objfile *objfile)
4531{
4532 int i;
4533
4534 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4535 {
4536 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4537 struct partial_symtab *pst = per_cu->v.psymtab;
4538 int j;
4539
4540 for (j = 0; j < pst->number_of_dependencies; ++j)
4541 {
4542 /* Set the 'user' field only if it is not already set. */
4543 if (pst->dependencies[j]->user == NULL)
4544 pst->dependencies[j]->user = pst;
4545 }
4546 }
4547}
4548
93311388
DE
4549/* Build the partial symbol table by doing a quick pass through the
4550 .debug_info and .debug_abbrev sections. */
72bf9492 4551
93311388 4552static void
c67a9c90 4553dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 4554{
60606b2c
TT
4555 struct cleanup *back_to, *addrmap_cleanup;
4556 struct obstack temp_obstack;
21b2bd31 4557 int i;
93311388 4558
45cfd468
DE
4559 if (dwarf2_read_debug)
4560 {
4561 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4562 objfile->name);
4563 }
4564
98bfdba5
PA
4565 dwarf2_per_objfile->reading_partial_symbols = 1;
4566
be391dca 4567 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 4568
93311388
DE
4569 /* Any cached compilation units will be linked by the per-objfile
4570 read_in_chain. Make sure to free them when we're done. */
4571 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 4572
348e048f
DE
4573 build_type_psymtabs (objfile);
4574
93311388 4575 create_all_comp_units (objfile);
c906108c 4576
60606b2c
TT
4577 /* Create a temporary address map on a temporary obstack. We later
4578 copy this to the final obstack. */
4579 obstack_init (&temp_obstack);
4580 make_cleanup_obstack_free (&temp_obstack);
4581 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4582 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 4583
21b2bd31 4584 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 4585 {
21b2bd31 4586 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 4587
95554aad 4588 process_psymtab_comp_unit (per_cu, 0);
c906108c 4589 }
ff013f42 4590
95554aad
TT
4591 set_partial_user (objfile);
4592
ff013f42
JK
4593 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4594 &objfile->objfile_obstack);
60606b2c 4595 discard_cleanups (addrmap_cleanup);
ff013f42 4596
ae038cb0 4597 do_cleanups (back_to);
45cfd468
DE
4598
4599 if (dwarf2_read_debug)
4600 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4601 objfile->name);
ae038cb0
DJ
4602}
4603
3019eac3 4604/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
4605
4606static void
dee91e82
DE
4607load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4608 gdb_byte *info_ptr,
4609 struct die_info *comp_unit_die,
4610 int has_children,
4611 void *data)
ae038cb0 4612{
dee91e82 4613 struct dwarf2_cu *cu = reader->cu;
ae038cb0 4614
95554aad 4615 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 4616
ae038cb0
DJ
4617 /* Check if comp unit has_children.
4618 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4619 If not, there's no more debug_info for this comp unit. */
d85a05f0 4620 if (has_children)
dee91e82
DE
4621 load_partial_dies (reader, info_ptr, 0);
4622}
98bfdba5 4623
dee91e82
DE
4624/* Load the partial DIEs for a secondary CU into memory.
4625 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 4626
dee91e82
DE
4627static void
4628load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4629{
fd820528 4630 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
4631}
4632
9cdd5dbd
DE
4633/* Create a list of all compilation units in OBJFILE.
4634 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
4635
4636static void
4637create_all_comp_units (struct objfile *objfile)
4638{
4639 int n_allocated;
4640 int n_comp_units;
4641 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
4642 gdb_byte *info_ptr;
4643
4644 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4645 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4646
4647 n_comp_units = 0;
4648 n_allocated = 10;
4649 all_comp_units = xmalloc (n_allocated
4650 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 4651
3e43a32a
MS
4652 while (info_ptr < dwarf2_per_objfile->info.buffer
4653 + dwarf2_per_objfile->info.size)
ae038cb0 4654 {
c764a876 4655 unsigned int length, initial_length_size;
ae038cb0 4656 struct dwarf2_per_cu_data *this_cu;
b64f50a1 4657 sect_offset offset;
ae038cb0 4658
b64f50a1 4659 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4660
4661 /* Read just enough information to find out where the next
4662 compilation unit is. */
c764a876
DE
4663 length = read_initial_length (objfile->obfd, info_ptr,
4664 &initial_length_size);
ae038cb0
DJ
4665
4666 /* Save the compilation unit for later lookup. */
4667 this_cu = obstack_alloc (&objfile->objfile_obstack,
4668 sizeof (struct dwarf2_per_cu_data));
4669 memset (this_cu, 0, sizeof (*this_cu));
4670 this_cu->offset = offset;
c764a876 4671 this_cu->length = length + initial_length_size;
9291a0cd 4672 this_cu->objfile = objfile;
3019eac3 4673 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
ae038cb0
DJ
4674
4675 if (n_comp_units == n_allocated)
4676 {
4677 n_allocated *= 2;
4678 all_comp_units = xrealloc (all_comp_units,
4679 n_allocated
4680 * sizeof (struct dwarf2_per_cu_data *));
4681 }
4682 all_comp_units[n_comp_units++] = this_cu;
4683
4684 info_ptr = info_ptr + this_cu->length;
4685 }
4686
4687 dwarf2_per_objfile->all_comp_units
4688 = obstack_alloc (&objfile->objfile_obstack,
4689 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4690 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4691 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4692 xfree (all_comp_units);
4693 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
4694}
4695
5734ee8b
DJ
4696/* Process all loaded DIEs for compilation unit CU, starting at
4697 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4698 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4699 DW_AT_ranges). If NEED_PC is set, then this function will set
4700 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4701 and record the covered ranges in the addrmap. */
c906108c 4702
72bf9492
DJ
4703static void
4704scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 4705 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 4706{
72bf9492 4707 struct partial_die_info *pdi;
c906108c 4708
91c24f0a
DC
4709 /* Now, march along the PDI's, descending into ones which have
4710 interesting children but skipping the children of the other ones,
4711 until we reach the end of the compilation unit. */
c906108c 4712
72bf9492 4713 pdi = first_die;
91c24f0a 4714
72bf9492
DJ
4715 while (pdi != NULL)
4716 {
4717 fixup_partial_die (pdi, cu);
c906108c 4718
f55ee35c 4719 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
4720 children, so we need to look at them. Ditto for anonymous
4721 enums. */
933c6fe4 4722
72bf9492 4723 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
4724 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4725 || pdi->tag == DW_TAG_imported_unit)
c906108c 4726 {
72bf9492 4727 switch (pdi->tag)
c906108c
SS
4728 {
4729 case DW_TAG_subprogram:
5734ee8b 4730 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 4731 break;
72929c62 4732 case DW_TAG_constant:
c906108c
SS
4733 case DW_TAG_variable:
4734 case DW_TAG_typedef:
91c24f0a 4735 case DW_TAG_union_type:
72bf9492 4736 if (!pdi->is_declaration)
63d06c5c 4737 {
72bf9492 4738 add_partial_symbol (pdi, cu);
63d06c5c
DC
4739 }
4740 break;
c906108c 4741 case DW_TAG_class_type:
680b30c7 4742 case DW_TAG_interface_type:
c906108c 4743 case DW_TAG_structure_type:
72bf9492 4744 if (!pdi->is_declaration)
c906108c 4745 {
72bf9492 4746 add_partial_symbol (pdi, cu);
c906108c
SS
4747 }
4748 break;
91c24f0a 4749 case DW_TAG_enumeration_type:
72bf9492
DJ
4750 if (!pdi->is_declaration)
4751 add_partial_enumeration (pdi, cu);
c906108c
SS
4752 break;
4753 case DW_TAG_base_type:
a02abb62 4754 case DW_TAG_subrange_type:
c906108c 4755 /* File scope base type definitions are added to the partial
c5aa993b 4756 symbol table. */
72bf9492 4757 add_partial_symbol (pdi, cu);
c906108c 4758 break;
d9fa45fe 4759 case DW_TAG_namespace:
5734ee8b 4760 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 4761 break;
5d7cb8df
JK
4762 case DW_TAG_module:
4763 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4764 break;
95554aad
TT
4765 case DW_TAG_imported_unit:
4766 {
4767 struct dwarf2_per_cu_data *per_cu;
4768
4769 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4770 cu->objfile);
4771
4772 /* Go read the partial unit, if needed. */
4773 if (per_cu->v.psymtab == NULL)
4774 process_psymtab_comp_unit (per_cu, 1);
4775
4776 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4777 per_cu);
4778 }
4779 break;
c906108c
SS
4780 default:
4781 break;
4782 }
4783 }
4784
72bf9492
DJ
4785 /* If the die has a sibling, skip to the sibling. */
4786
4787 pdi = pdi->die_sibling;
4788 }
4789}
4790
4791/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 4792
72bf9492 4793 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
4794 name is concatenated with "::" and the partial DIE's name. For
4795 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
4796 Enumerators are an exception; they use the scope of their parent
4797 enumeration type, i.e. the name of the enumeration type is not
4798 prepended to the enumerator.
91c24f0a 4799
72bf9492
DJ
4800 There are two complexities. One is DW_AT_specification; in this
4801 case "parent" means the parent of the target of the specification,
4802 instead of the direct parent of the DIE. The other is compilers
4803 which do not emit DW_TAG_namespace; in this case we try to guess
4804 the fully qualified name of structure types from their members'
4805 linkage names. This must be done using the DIE's children rather
4806 than the children of any DW_AT_specification target. We only need
4807 to do this for structures at the top level, i.e. if the target of
4808 any DW_AT_specification (if any; otherwise the DIE itself) does not
4809 have a parent. */
4810
4811/* Compute the scope prefix associated with PDI's parent, in
4812 compilation unit CU. The result will be allocated on CU's
4813 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4814 field. NULL is returned if no prefix is necessary. */
4815static char *
4816partial_die_parent_scope (struct partial_die_info *pdi,
4817 struct dwarf2_cu *cu)
4818{
4819 char *grandparent_scope;
4820 struct partial_die_info *parent, *real_pdi;
91c24f0a 4821
72bf9492
DJ
4822 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4823 then this means the parent of the specification DIE. */
4824
4825 real_pdi = pdi;
72bf9492 4826 while (real_pdi->has_specification)
10b3939b 4827 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
4828
4829 parent = real_pdi->die_parent;
4830 if (parent == NULL)
4831 return NULL;
4832
4833 if (parent->scope_set)
4834 return parent->scope;
4835
4836 fixup_partial_die (parent, cu);
4837
10b3939b 4838 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 4839
acebe513
UW
4840 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4841 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4842 Work around this problem here. */
4843 if (cu->language == language_cplus
6e70227d 4844 && parent->tag == DW_TAG_namespace
acebe513
UW
4845 && strcmp (parent->name, "::") == 0
4846 && grandparent_scope == NULL)
4847 {
4848 parent->scope = NULL;
4849 parent->scope_set = 1;
4850 return NULL;
4851 }
4852
9c6c53f7
SA
4853 if (pdi->tag == DW_TAG_enumerator)
4854 /* Enumerators should not get the name of the enumeration as a prefix. */
4855 parent->scope = grandparent_scope;
4856 else if (parent->tag == DW_TAG_namespace
f55ee35c 4857 || parent->tag == DW_TAG_module
72bf9492
DJ
4858 || parent->tag == DW_TAG_structure_type
4859 || parent->tag == DW_TAG_class_type
680b30c7 4860 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
4861 || parent->tag == DW_TAG_union_type
4862 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
4863 {
4864 if (grandparent_scope == NULL)
4865 parent->scope = parent->name;
4866 else
3e43a32a
MS
4867 parent->scope = typename_concat (&cu->comp_unit_obstack,
4868 grandparent_scope,
f55ee35c 4869 parent->name, 0, cu);
72bf9492 4870 }
72bf9492
DJ
4871 else
4872 {
4873 /* FIXME drow/2004-04-01: What should we be doing with
4874 function-local names? For partial symbols, we should probably be
4875 ignoring them. */
4876 complaint (&symfile_complaints,
e2e0b3e5 4877 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 4878 parent->tag, pdi->offset.sect_off);
72bf9492 4879 parent->scope = grandparent_scope;
c906108c
SS
4880 }
4881
72bf9492
DJ
4882 parent->scope_set = 1;
4883 return parent->scope;
4884}
4885
4886/* Return the fully scoped name associated with PDI, from compilation unit
4887 CU. The result will be allocated with malloc. */
4568ecf9 4888
72bf9492
DJ
4889static char *
4890partial_die_full_name (struct partial_die_info *pdi,
4891 struct dwarf2_cu *cu)
4892{
4893 char *parent_scope;
4894
98bfdba5
PA
4895 /* If this is a template instantiation, we can not work out the
4896 template arguments from partial DIEs. So, unfortunately, we have
4897 to go through the full DIEs. At least any work we do building
4898 types here will be reused if full symbols are loaded later. */
4899 if (pdi->has_template_arguments)
4900 {
4901 fixup_partial_die (pdi, cu);
4902
4903 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4904 {
4905 struct die_info *die;
4906 struct attribute attr;
4907 struct dwarf2_cu *ref_cu = cu;
4908
b64f50a1 4909 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
4910 attr.name = 0;
4911 attr.form = DW_FORM_ref_addr;
4568ecf9 4912 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
4913 die = follow_die_ref (NULL, &attr, &ref_cu);
4914
4915 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4916 }
4917 }
4918
72bf9492
DJ
4919 parent_scope = partial_die_parent_scope (pdi, cu);
4920 if (parent_scope == NULL)
4921 return NULL;
4922 else
f55ee35c 4923 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4924}
4925
4926static void
72bf9492 4927add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4928{
e7c27a73 4929 struct objfile *objfile = cu->objfile;
c906108c 4930 CORE_ADDR addr = 0;
decbce07 4931 char *actual_name = NULL;
e142c38c 4932 CORE_ADDR baseaddr;
72bf9492 4933 int built_actual_name = 0;
e142c38c
DJ
4934
4935 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4936
94af9270
KS
4937 actual_name = partial_die_full_name (pdi, cu);
4938 if (actual_name)
4939 built_actual_name = 1;
63d06c5c 4940
72bf9492
DJ
4941 if (actual_name == NULL)
4942 actual_name = pdi->name;
4943
c906108c
SS
4944 switch (pdi->tag)
4945 {
4946 case DW_TAG_subprogram:
2cfa0c8d 4947 if (pdi->is_external || cu->language == language_ada)
c906108c 4948 {
2cfa0c8d
JB
4949 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4950 of the global scope. But in Ada, we want to be able to access
4951 nested procedures globally. So all Ada subprograms are stored
4952 in the global scope. */
f47fb265 4953 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4954 mst_text, objfile); */
f47fb265
MS
4955 add_psymbol_to_list (actual_name, strlen (actual_name),
4956 built_actual_name,
4957 VAR_DOMAIN, LOC_BLOCK,
4958 &objfile->global_psymbols,
4959 0, pdi->lowpc + baseaddr,
4960 cu->language, objfile);
c906108c
SS
4961 }
4962 else
4963 {
f47fb265 4964 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4965 mst_file_text, objfile); */
f47fb265
MS
4966 add_psymbol_to_list (actual_name, strlen (actual_name),
4967 built_actual_name,
4968 VAR_DOMAIN, LOC_BLOCK,
4969 &objfile->static_psymbols,
4970 0, pdi->lowpc + baseaddr,
4971 cu->language, objfile);
c906108c
SS
4972 }
4973 break;
72929c62
JB
4974 case DW_TAG_constant:
4975 {
4976 struct psymbol_allocation_list *list;
4977
4978 if (pdi->is_external)
4979 list = &objfile->global_psymbols;
4980 else
4981 list = &objfile->static_psymbols;
f47fb265
MS
4982 add_psymbol_to_list (actual_name, strlen (actual_name),
4983 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4984 list, 0, 0, cu->language, objfile);
72929c62
JB
4985 }
4986 break;
c906108c 4987 case DW_TAG_variable:
95554aad
TT
4988 if (pdi->d.locdesc)
4989 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 4990
95554aad 4991 if (pdi->d.locdesc
caac4577
JG
4992 && addr == 0
4993 && !dwarf2_per_objfile->has_section_at_zero)
4994 {
4995 /* A global or static variable may also have been stripped
4996 out by the linker if unused, in which case its address
4997 will be nullified; do not add such variables into partial
4998 symbol table then. */
4999 }
5000 else if (pdi->is_external)
c906108c
SS
5001 {
5002 /* Global Variable.
5003 Don't enter into the minimal symbol tables as there is
5004 a minimal symbol table entry from the ELF symbols already.
5005 Enter into partial symbol table if it has a location
5006 descriptor or a type.
5007 If the location descriptor is missing, new_symbol will create
5008 a LOC_UNRESOLVED symbol, the address of the variable will then
5009 be determined from the minimal symbol table whenever the variable
5010 is referenced.
5011 The address for the partial symbol table entry is not
5012 used by GDB, but it comes in handy for debugging partial symbol
5013 table building. */
5014
95554aad 5015 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
5016 add_psymbol_to_list (actual_name, strlen (actual_name),
5017 built_actual_name,
5018 VAR_DOMAIN, LOC_STATIC,
5019 &objfile->global_psymbols,
5020 0, addr + baseaddr,
5021 cu->language, objfile);
c906108c
SS
5022 }
5023 else
5024 {
0963b4bd 5025 /* Static Variable. Skip symbols without location descriptors. */
95554aad 5026 if (pdi->d.locdesc == NULL)
decbce07
MS
5027 {
5028 if (built_actual_name)
5029 xfree (actual_name);
5030 return;
5031 }
f47fb265 5032 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 5033 mst_file_data, objfile); */
f47fb265
MS
5034 add_psymbol_to_list (actual_name, strlen (actual_name),
5035 built_actual_name,
5036 VAR_DOMAIN, LOC_STATIC,
5037 &objfile->static_psymbols,
5038 0, addr + baseaddr,
5039 cu->language, objfile);
c906108c
SS
5040 }
5041 break;
5042 case DW_TAG_typedef:
5043 case DW_TAG_base_type:
a02abb62 5044 case DW_TAG_subrange_type:
38d518c9 5045 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5046 built_actual_name,
176620f1 5047 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 5048 &objfile->static_psymbols,
e142c38c 5049 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5050 break;
72bf9492
DJ
5051 case DW_TAG_namespace:
5052 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5053 built_actual_name,
72bf9492
DJ
5054 VAR_DOMAIN, LOC_TYPEDEF,
5055 &objfile->global_psymbols,
5056 0, (CORE_ADDR) 0, cu->language, objfile);
5057 break;
c906108c 5058 case DW_TAG_class_type:
680b30c7 5059 case DW_TAG_interface_type:
c906108c
SS
5060 case DW_TAG_structure_type:
5061 case DW_TAG_union_type:
5062 case DW_TAG_enumeration_type:
fa4028e9
JB
5063 /* Skip external references. The DWARF standard says in the section
5064 about "Structure, Union, and Class Type Entries": "An incomplete
5065 structure, union or class type is represented by a structure,
5066 union or class entry that does not have a byte size attribute
5067 and that has a DW_AT_declaration attribute." */
5068 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
5069 {
5070 if (built_actual_name)
5071 xfree (actual_name);
5072 return;
5073 }
fa4028e9 5074
63d06c5c
DC
5075 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
5076 static vs. global. */
38d518c9 5077 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5078 built_actual_name,
176620f1 5079 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
5080 (cu->language == language_cplus
5081 || cu->language == language_java)
63d06c5c
DC
5082 ? &objfile->global_psymbols
5083 : &objfile->static_psymbols,
e142c38c 5084 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 5085
c906108c
SS
5086 break;
5087 case DW_TAG_enumerator:
38d518c9 5088 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 5089 built_actual_name,
176620f1 5090 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
5091 (cu->language == language_cplus
5092 || cu->language == language_java)
f6fe98ef
DJ
5093 ? &objfile->global_psymbols
5094 : &objfile->static_psymbols,
e142c38c 5095 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
5096 break;
5097 default:
5098 break;
5099 }
5c4e30ca 5100
72bf9492
DJ
5101 if (built_actual_name)
5102 xfree (actual_name);
c906108c
SS
5103}
5104
5c4e30ca
DC
5105/* Read a partial die corresponding to a namespace; also, add a symbol
5106 corresponding to that namespace to the symbol table. NAMESPACE is
5107 the name of the enclosing namespace. */
91c24f0a 5108
72bf9492
DJ
5109static void
5110add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 5111 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 5112 int need_pc, struct dwarf2_cu *cu)
91c24f0a 5113{
72bf9492 5114 /* Add a symbol for the namespace. */
e7c27a73 5115
72bf9492 5116 add_partial_symbol (pdi, cu);
5c4e30ca
DC
5117
5118 /* Now scan partial symbols in that namespace. */
5119
91c24f0a 5120 if (pdi->has_children)
5734ee8b 5121 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
5122}
5123
5d7cb8df
JK
5124/* Read a partial die corresponding to a Fortran module. */
5125
5126static void
5127add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
5128 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
5129{
f55ee35c 5130 /* Now scan partial symbols in that module. */
5d7cb8df
JK
5131
5132 if (pdi->has_children)
5133 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
5134}
5135
bc30ff58
JB
5136/* Read a partial die corresponding to a subprogram and create a partial
5137 symbol for that subprogram. When the CU language allows it, this
5138 routine also defines a partial symbol for each nested subprogram
5139 that this subprogram contains.
6e70227d 5140
bc30ff58
JB
5141 DIE my also be a lexical block, in which case we simply search
5142 recursively for suprograms defined inside that lexical block.
5143 Again, this is only performed when the CU language allows this
5144 type of definitions. */
5145
5146static void
5147add_partial_subprogram (struct partial_die_info *pdi,
5148 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 5149 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
5150{
5151 if (pdi->tag == DW_TAG_subprogram)
5152 {
5153 if (pdi->has_pc_info)
5154 {
5155 if (pdi->lowpc < *lowpc)
5156 *lowpc = pdi->lowpc;
5157 if (pdi->highpc > *highpc)
5158 *highpc = pdi->highpc;
5734ee8b
DJ
5159 if (need_pc)
5160 {
5161 CORE_ADDR baseaddr;
5162 struct objfile *objfile = cu->objfile;
5163
5164 baseaddr = ANOFFSET (objfile->section_offsets,
5165 SECT_OFF_TEXT (objfile));
5166 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
5167 pdi->lowpc + baseaddr,
5168 pdi->highpc - 1 + baseaddr,
9291a0cd 5169 cu->per_cu->v.psymtab);
5734ee8b 5170 }
481860b3
GB
5171 }
5172
5173 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
5174 {
bc30ff58 5175 if (!pdi->is_declaration)
e8d05480
JB
5176 /* Ignore subprogram DIEs that do not have a name, they are
5177 illegal. Do not emit a complaint at this point, we will
5178 do so when we convert this psymtab into a symtab. */
5179 if (pdi->name)
5180 add_partial_symbol (pdi, cu);
bc30ff58
JB
5181 }
5182 }
6e70227d 5183
bc30ff58
JB
5184 if (! pdi->has_children)
5185 return;
5186
5187 if (cu->language == language_ada)
5188 {
5189 pdi = pdi->die_child;
5190 while (pdi != NULL)
5191 {
5192 fixup_partial_die (pdi, cu);
5193 if (pdi->tag == DW_TAG_subprogram
5194 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 5195 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
5196 pdi = pdi->die_sibling;
5197 }
5198 }
5199}
5200
91c24f0a
DC
5201/* Read a partial die corresponding to an enumeration type. */
5202
72bf9492
DJ
5203static void
5204add_partial_enumeration (struct partial_die_info *enum_pdi,
5205 struct dwarf2_cu *cu)
91c24f0a 5206{
72bf9492 5207 struct partial_die_info *pdi;
91c24f0a
DC
5208
5209 if (enum_pdi->name != NULL)
72bf9492
DJ
5210 add_partial_symbol (enum_pdi, cu);
5211
5212 pdi = enum_pdi->die_child;
5213 while (pdi)
91c24f0a 5214 {
72bf9492 5215 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 5216 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 5217 else
72bf9492
DJ
5218 add_partial_symbol (pdi, cu);
5219 pdi = pdi->die_sibling;
91c24f0a 5220 }
91c24f0a
DC
5221}
5222
6caca83c
CC
5223/* Return the initial uleb128 in the die at INFO_PTR. */
5224
5225static unsigned int
5226peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
5227{
5228 unsigned int bytes_read;
5229
5230 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5231}
5232
4bb7a0a7
DJ
5233/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
5234 Return the corresponding abbrev, or NULL if the number is zero (indicating
5235 an empty DIE). In either case *BYTES_READ will be set to the length of
5236 the initial number. */
5237
5238static struct abbrev_info *
fe1b8b76 5239peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 5240 struct dwarf2_cu *cu)
4bb7a0a7
DJ
5241{
5242 bfd *abfd = cu->objfile->obfd;
5243 unsigned int abbrev_number;
5244 struct abbrev_info *abbrev;
5245
5246 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
5247
5248 if (abbrev_number == 0)
5249 return NULL;
5250
433df2d4 5251 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
4bb7a0a7
DJ
5252 if (!abbrev)
5253 {
3e43a32a
MS
5254 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
5255 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
5256 }
5257
5258 return abbrev;
5259}
5260
93311388
DE
5261/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5262 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
5263 DIE. Any children of the skipped DIEs will also be skipped. */
5264
fe1b8b76 5265static gdb_byte *
dee91e82 5266skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 5267{
dee91e82 5268 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
5269 struct abbrev_info *abbrev;
5270 unsigned int bytes_read;
5271
5272 while (1)
5273 {
5274 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5275 if (abbrev == NULL)
5276 return info_ptr + bytes_read;
5277 else
dee91e82 5278 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
5279 }
5280}
5281
93311388
DE
5282/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5283 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
5284 abbrev corresponding to that skipped uleb128 should be passed in
5285 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5286 children. */
5287
fe1b8b76 5288static gdb_byte *
dee91e82
DE
5289skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5290 struct abbrev_info *abbrev)
4bb7a0a7
DJ
5291{
5292 unsigned int bytes_read;
5293 struct attribute attr;
dee91e82
DE
5294 bfd *abfd = reader->abfd;
5295 struct dwarf2_cu *cu = reader->cu;
5296 gdb_byte *buffer = reader->buffer;
f664829e
DE
5297 const gdb_byte *buffer_end = reader->buffer_end;
5298 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
5299 unsigned int form, i;
5300
5301 for (i = 0; i < abbrev->num_attrs; i++)
5302 {
5303 /* The only abbrev we care about is DW_AT_sibling. */
5304 if (abbrev->attrs[i].name == DW_AT_sibling)
5305 {
dee91e82 5306 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 5307 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
5308 complaint (&symfile_complaints,
5309 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 5310 else
b64f50a1 5311 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
5312 }
5313
5314 /* If it isn't DW_AT_sibling, skip this attribute. */
5315 form = abbrev->attrs[i].form;
5316 skip_attribute:
5317 switch (form)
5318 {
4bb7a0a7 5319 case DW_FORM_ref_addr:
ae411497
TT
5320 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5321 and later it is offset sized. */
5322 if (cu->header.version == 2)
5323 info_ptr += cu->header.addr_size;
5324 else
5325 info_ptr += cu->header.offset_size;
5326 break;
5327 case DW_FORM_addr:
4bb7a0a7
DJ
5328 info_ptr += cu->header.addr_size;
5329 break;
5330 case DW_FORM_data1:
5331 case DW_FORM_ref1:
5332 case DW_FORM_flag:
5333 info_ptr += 1;
5334 break;
2dc7f7b3
TT
5335 case DW_FORM_flag_present:
5336 break;
4bb7a0a7
DJ
5337 case DW_FORM_data2:
5338 case DW_FORM_ref2:
5339 info_ptr += 2;
5340 break;
5341 case DW_FORM_data4:
5342 case DW_FORM_ref4:
5343 info_ptr += 4;
5344 break;
5345 case DW_FORM_data8:
5346 case DW_FORM_ref8:
55f1336d 5347 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
5348 info_ptr += 8;
5349 break;
5350 case DW_FORM_string:
9b1c24c8 5351 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
5352 info_ptr += bytes_read;
5353 break;
2dc7f7b3 5354 case DW_FORM_sec_offset:
4bb7a0a7
DJ
5355 case DW_FORM_strp:
5356 info_ptr += cu->header.offset_size;
5357 break;
2dc7f7b3 5358 case DW_FORM_exprloc:
4bb7a0a7
DJ
5359 case DW_FORM_block:
5360 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5361 info_ptr += bytes_read;
5362 break;
5363 case DW_FORM_block1:
5364 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5365 break;
5366 case DW_FORM_block2:
5367 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5368 break;
5369 case DW_FORM_block4:
5370 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5371 break;
5372 case DW_FORM_sdata:
5373 case DW_FORM_udata:
5374 case DW_FORM_ref_udata:
3019eac3
DE
5375 case DW_FORM_GNU_addr_index:
5376 case DW_FORM_GNU_str_index:
f664829e 5377 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
5378 break;
5379 case DW_FORM_indirect:
5380 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5381 info_ptr += bytes_read;
5382 /* We need to continue parsing from here, so just go back to
5383 the top. */
5384 goto skip_attribute;
5385
5386 default:
3e43a32a
MS
5387 error (_("Dwarf Error: Cannot handle %s "
5388 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
5389 dwarf_form_name (form),
5390 bfd_get_filename (abfd));
5391 }
5392 }
5393
5394 if (abbrev->has_children)
dee91e82 5395 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
5396 else
5397 return info_ptr;
5398}
5399
93311388 5400/* Locate ORIG_PDI's sibling.
dee91e82 5401 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 5402
fe1b8b76 5403static gdb_byte *
dee91e82
DE
5404locate_pdi_sibling (const struct die_reader_specs *reader,
5405 struct partial_die_info *orig_pdi,
5406 gdb_byte *info_ptr)
91c24f0a
DC
5407{
5408 /* Do we know the sibling already? */
72bf9492 5409
91c24f0a
DC
5410 if (orig_pdi->sibling)
5411 return orig_pdi->sibling;
5412
5413 /* Are there any children to deal with? */
5414
5415 if (!orig_pdi->has_children)
5416 return info_ptr;
5417
4bb7a0a7 5418 /* Skip the children the long way. */
91c24f0a 5419
dee91e82 5420 return skip_children (reader, info_ptr);
91c24f0a
DC
5421}
5422
c906108c
SS
5423/* Expand this partial symbol table into a full symbol table. */
5424
5425static void
fba45db2 5426dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 5427{
c906108c
SS
5428 if (pst != NULL)
5429 {
5430 if (pst->readin)
5431 {
3e43a32a
MS
5432 warning (_("bug: psymtab for %s is already read in."),
5433 pst->filename);
c906108c
SS
5434 }
5435 else
5436 {
5437 if (info_verbose)
5438 {
3e43a32a
MS
5439 printf_filtered (_("Reading in symbols for %s..."),
5440 pst->filename);
c906108c
SS
5441 gdb_flush (gdb_stdout);
5442 }
5443
10b3939b
DJ
5444 /* Restore our global data. */
5445 dwarf2_per_objfile = objfile_data (pst->objfile,
5446 dwarf2_objfile_data_key);
5447
b2ab525c
KB
5448 /* If this psymtab is constructed from a debug-only objfile, the
5449 has_section_at_zero flag will not necessarily be correct. We
5450 can get the correct value for this flag by looking at the data
5451 associated with the (presumably stripped) associated objfile. */
5452 if (pst->objfile->separate_debug_objfile_backlink)
5453 {
5454 struct dwarf2_per_objfile *dpo_backlink
5455 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5456 dwarf2_objfile_data_key);
9a619af0 5457
b2ab525c
KB
5458 dwarf2_per_objfile->has_section_at_zero
5459 = dpo_backlink->has_section_at_zero;
5460 }
5461
98bfdba5
PA
5462 dwarf2_per_objfile->reading_partial_symbols = 0;
5463
c906108c
SS
5464 psymtab_to_symtab_1 (pst);
5465
5466 /* Finish up the debug error message. */
5467 if (info_verbose)
a3f17187 5468 printf_filtered (_("done.\n"));
c906108c
SS
5469 }
5470 }
95554aad
TT
5471
5472 process_cu_includes ();
c906108c 5473}
9cdd5dbd
DE
5474\f
5475/* Reading in full CUs. */
c906108c 5476
10b3939b
DJ
5477/* Add PER_CU to the queue. */
5478
5479static void
95554aad
TT
5480queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5481 enum language pretend_language)
10b3939b
DJ
5482{
5483 struct dwarf2_queue_item *item;
5484
5485 per_cu->queued = 1;
5486 item = xmalloc (sizeof (*item));
5487 item->per_cu = per_cu;
95554aad 5488 item->pretend_language = pretend_language;
10b3939b
DJ
5489 item->next = NULL;
5490
5491 if (dwarf2_queue == NULL)
5492 dwarf2_queue = item;
5493 else
5494 dwarf2_queue_tail->next = item;
5495
5496 dwarf2_queue_tail = item;
5497}
5498
0907af0c
DE
5499/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
5500 unit and add it to our queue.
5501 The result is non-zero if PER_CU was queued, otherwise the result is zero
5502 meaning either PER_CU is already queued or it is already loaded. */
5503
5504static int
5505maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
5506 struct dwarf2_per_cu_data *per_cu,
5507 enum language pretend_language)
5508{
5509 /* We may arrive here during partial symbol reading, if we need full
5510 DIEs to process an unusual case (e.g. template arguments). Do
5511 not queue PER_CU, just tell our caller to load its DIEs. */
5512 if (dwarf2_per_objfile->reading_partial_symbols)
5513 {
5514 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
5515 return 1;
5516 return 0;
5517 }
5518
5519 /* Mark the dependence relation so that we don't flush PER_CU
5520 too early. */
5521 dwarf2_add_dependence (this_cu, per_cu);
5522
5523 /* If it's already on the queue, we have nothing to do. */
5524 if (per_cu->queued)
5525 return 0;
5526
5527 /* If the compilation unit is already loaded, just mark it as
5528 used. */
5529 if (per_cu->cu != NULL)
5530 {
5531 per_cu->cu->last_used = 0;
5532 return 0;
5533 }
5534
5535 /* Add it to the queue. */
5536 queue_comp_unit (per_cu, pretend_language);
5537
5538 return 1;
5539}
5540
10b3939b
DJ
5541/* Process the queue. */
5542
5543static void
a0f42c21 5544process_queue (void)
10b3939b
DJ
5545{
5546 struct dwarf2_queue_item *item, *next_item;
5547
45cfd468
DE
5548 if (dwarf2_read_debug)
5549 {
5550 fprintf_unfiltered (gdb_stdlog,
5551 "Expanding one or more symtabs of objfile %s ...\n",
5552 dwarf2_per_objfile->objfile->name);
5553 }
5554
03dd20cc
DJ
5555 /* The queue starts out with one item, but following a DIE reference
5556 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
5557 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5558 {
9291a0cd
TT
5559 if (dwarf2_per_objfile->using_index
5560 ? !item->per_cu->v.quick->symtab
5561 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
95554aad 5562 process_full_comp_unit (item->per_cu, item->pretend_language);
10b3939b
DJ
5563
5564 item->per_cu->queued = 0;
5565 next_item = item->next;
5566 xfree (item);
5567 }
5568
5569 dwarf2_queue_tail = NULL;
45cfd468
DE
5570
5571 if (dwarf2_read_debug)
5572 {
5573 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
5574 dwarf2_per_objfile->objfile->name);
5575 }
10b3939b
DJ
5576}
5577
5578/* Free all allocated queue entries. This function only releases anything if
5579 an error was thrown; if the queue was processed then it would have been
5580 freed as we went along. */
5581
5582static void
5583dwarf2_release_queue (void *dummy)
5584{
5585 struct dwarf2_queue_item *item, *last;
5586
5587 item = dwarf2_queue;
5588 while (item)
5589 {
5590 /* Anything still marked queued is likely to be in an
5591 inconsistent state, so discard it. */
5592 if (item->per_cu->queued)
5593 {
5594 if (item->per_cu->cu != NULL)
dee91e82 5595 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
5596 item->per_cu->queued = 0;
5597 }
5598
5599 last = item;
5600 item = item->next;
5601 xfree (last);
5602 }
5603
5604 dwarf2_queue = dwarf2_queue_tail = NULL;
5605}
5606
5607/* Read in full symbols for PST, and anything it depends on. */
5608
c906108c 5609static void
fba45db2 5610psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 5611{
10b3939b 5612 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
5613 int i;
5614
95554aad
TT
5615 if (pst->readin)
5616 return;
5617
aaa75496 5618 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
5619 if (!pst->dependencies[i]->readin
5620 && pst->dependencies[i]->user == NULL)
aaa75496
JB
5621 {
5622 /* Inform about additional files that need to be read in. */
5623 if (info_verbose)
5624 {
a3f17187 5625 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
5626 fputs_filtered (" ", gdb_stdout);
5627 wrap_here ("");
5628 fputs_filtered ("and ", gdb_stdout);
5629 wrap_here ("");
5630 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 5631 wrap_here (""); /* Flush output. */
aaa75496
JB
5632 gdb_flush (gdb_stdout);
5633 }
5634 psymtab_to_symtab_1 (pst->dependencies[i]);
5635 }
5636
e38df1d0 5637 per_cu = pst->read_symtab_private;
10b3939b
DJ
5638
5639 if (per_cu == NULL)
aaa75496
JB
5640 {
5641 /* It's an include file, no symbols to read for it.
5642 Everything is in the parent symtab. */
5643 pst->readin = 1;
5644 return;
5645 }
c906108c 5646
a0f42c21 5647 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
5648}
5649
dee91e82
DE
5650/* Trivial hash function for die_info: the hash value of a DIE
5651 is its offset in .debug_info for this objfile. */
10b3939b 5652
dee91e82
DE
5653static hashval_t
5654die_hash (const void *item)
10b3939b 5655{
dee91e82 5656 const struct die_info *die = item;
6502dd73 5657
dee91e82
DE
5658 return die->offset.sect_off;
5659}
63d06c5c 5660
dee91e82
DE
5661/* Trivial comparison function for die_info structures: two DIEs
5662 are equal if they have the same offset. */
98bfdba5 5663
dee91e82
DE
5664static int
5665die_eq (const void *item_lhs, const void *item_rhs)
5666{
5667 const struct die_info *die_lhs = item_lhs;
5668 const struct die_info *die_rhs = item_rhs;
c906108c 5669
dee91e82
DE
5670 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5671}
c906108c 5672
dee91e82
DE
5673/* die_reader_func for load_full_comp_unit.
5674 This is identical to read_signatured_type_reader,
5675 but is kept separate for now. */
c906108c 5676
dee91e82
DE
5677static void
5678load_full_comp_unit_reader (const struct die_reader_specs *reader,
5679 gdb_byte *info_ptr,
5680 struct die_info *comp_unit_die,
5681 int has_children,
5682 void *data)
5683{
5684 struct dwarf2_cu *cu = reader->cu;
95554aad 5685 enum language *language_ptr = data;
6caca83c 5686
dee91e82
DE
5687 gdb_assert (cu->die_hash == NULL);
5688 cu->die_hash =
5689 htab_create_alloc_ex (cu->header.length / 12,
5690 die_hash,
5691 die_eq,
5692 NULL,
5693 &cu->comp_unit_obstack,
5694 hashtab_obstack_allocate,
5695 dummy_obstack_deallocate);
e142c38c 5696
dee91e82
DE
5697 if (has_children)
5698 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5699 &info_ptr, comp_unit_die);
5700 cu->dies = comp_unit_die;
5701 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
5702
5703 /* We try not to read any attributes in this function, because not
9cdd5dbd 5704 all CUs needed for references have been loaded yet, and symbol
10b3939b 5705 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
5706 or we won't be able to build types correctly.
5707 Similarly, if we do not read the producer, we can not apply
5708 producer-specific interpretation. */
95554aad 5709 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 5710}
10b3939b 5711
dee91e82 5712/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 5713
dee91e82 5714static void
95554aad
TT
5715load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5716 enum language pretend_language)
dee91e82 5717{
3019eac3 5718 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 5719
95554aad
TT
5720 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5721 &pretend_language);
10b3939b
DJ
5722}
5723
3da10d80
KS
5724/* Add a DIE to the delayed physname list. */
5725
5726static void
5727add_to_method_list (struct type *type, int fnfield_index, int index,
5728 const char *name, struct die_info *die,
5729 struct dwarf2_cu *cu)
5730{
5731 struct delayed_method_info mi;
5732 mi.type = type;
5733 mi.fnfield_index = fnfield_index;
5734 mi.index = index;
5735 mi.name = name;
5736 mi.die = die;
5737 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5738}
5739
5740/* A cleanup for freeing the delayed method list. */
5741
5742static void
5743free_delayed_list (void *ptr)
5744{
5745 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5746 if (cu->method_list != NULL)
5747 {
5748 VEC_free (delayed_method_info, cu->method_list);
5749 cu->method_list = NULL;
5750 }
5751}
5752
5753/* Compute the physnames of any methods on the CU's method list.
5754
5755 The computation of method physnames is delayed in order to avoid the
5756 (bad) condition that one of the method's formal parameters is of an as yet
5757 incomplete type. */
5758
5759static void
5760compute_delayed_physnames (struct dwarf2_cu *cu)
5761{
5762 int i;
5763 struct delayed_method_info *mi;
5764 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5765 {
1d06ead6 5766 const char *physname;
3da10d80
KS
5767 struct fn_fieldlist *fn_flp
5768 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 5769 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
5770 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5771 }
5772}
5773
a766d390
DE
5774/* Go objects should be embedded in a DW_TAG_module DIE,
5775 and it's not clear if/how imported objects will appear.
5776 To keep Go support simple until that's worked out,
5777 go back through what we've read and create something usable.
5778 We could do this while processing each DIE, and feels kinda cleaner,
5779 but that way is more invasive.
5780 This is to, for example, allow the user to type "p var" or "b main"
5781 without having to specify the package name, and allow lookups
5782 of module.object to work in contexts that use the expression
5783 parser. */
5784
5785static void
5786fixup_go_packaging (struct dwarf2_cu *cu)
5787{
5788 char *package_name = NULL;
5789 struct pending *list;
5790 int i;
5791
5792 for (list = global_symbols; list != NULL; list = list->next)
5793 {
5794 for (i = 0; i < list->nsyms; ++i)
5795 {
5796 struct symbol *sym = list->symbol[i];
5797
5798 if (SYMBOL_LANGUAGE (sym) == language_go
5799 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5800 {
5801 char *this_package_name = go_symbol_package_name (sym);
5802
5803 if (this_package_name == NULL)
5804 continue;
5805 if (package_name == NULL)
5806 package_name = this_package_name;
5807 else
5808 {
5809 if (strcmp (package_name, this_package_name) != 0)
5810 complaint (&symfile_complaints,
5811 _("Symtab %s has objects from two different Go packages: %s and %s"),
5812 (sym->symtab && sym->symtab->filename
5813 ? sym->symtab->filename
5814 : cu->objfile->name),
5815 this_package_name, package_name);
5816 xfree (this_package_name);
5817 }
5818 }
5819 }
5820 }
5821
5822 if (package_name != NULL)
5823 {
5824 struct objfile *objfile = cu->objfile;
5825 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5826 package_name, objfile);
5827 struct symbol *sym;
5828
5829 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5830
5831 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5832 SYMBOL_SET_LANGUAGE (sym, language_go);
5833 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5834 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5835 e.g., "main" finds the "main" module and not C's main(). */
5836 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5837 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5838 SYMBOL_TYPE (sym) = type;
5839
5840 add_symbol_to_list (sym, &global_symbols);
5841
5842 xfree (package_name);
5843 }
5844}
5845
95554aad
TT
5846static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5847
5848/* Return the symtab for PER_CU. This works properly regardless of
5849 whether we're using the index or psymtabs. */
5850
5851static struct symtab *
5852get_symtab (struct dwarf2_per_cu_data *per_cu)
5853{
5854 return (dwarf2_per_objfile->using_index
5855 ? per_cu->v.quick->symtab
5856 : per_cu->v.psymtab->symtab);
5857}
5858
5859/* A helper function for computing the list of all symbol tables
5860 included by PER_CU. */
5861
5862static void
5863recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5864 htab_t all_children,
5865 struct dwarf2_per_cu_data *per_cu)
5866{
5867 void **slot;
5868 int ix;
5869 struct dwarf2_per_cu_data *iter;
5870
5871 slot = htab_find_slot (all_children, per_cu, INSERT);
5872 if (*slot != NULL)
5873 {
5874 /* This inclusion and its children have been processed. */
5875 return;
5876 }
5877
5878 *slot = per_cu;
5879 /* Only add a CU if it has a symbol table. */
5880 if (get_symtab (per_cu) != NULL)
5881 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5882
5883 for (ix = 0;
5884 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5885 ++ix)
5886 recursively_compute_inclusions (result, all_children, iter);
5887}
5888
5889/* Compute the symtab 'includes' fields for the symtab related to
5890 PER_CU. */
5891
5892static void
5893compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5894{
5895 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5896 {
5897 int ix, len;
5898 struct dwarf2_per_cu_data *iter;
5899 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5900 htab_t all_children;
5901 struct symtab *symtab = get_symtab (per_cu);
5902
5903 /* If we don't have a symtab, we can just skip this case. */
5904 if (symtab == NULL)
5905 return;
5906
5907 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5908 NULL, xcalloc, xfree);
5909
5910 for (ix = 0;
5911 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5912 ix, iter);
5913 ++ix)
5914 recursively_compute_inclusions (&result_children, all_children, iter);
5915
5916 /* Now we have a transitive closure of all the included CUs, so
5917 we can convert it to a list of symtabs. */
5918 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5919 symtab->includes
5920 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5921 (len + 1) * sizeof (struct symtab *));
5922 for (ix = 0;
5923 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5924 ++ix)
5925 symtab->includes[ix] = get_symtab (iter);
5926 symtab->includes[len] = NULL;
5927
5928 VEC_free (dwarf2_per_cu_ptr, result_children);
5929 htab_delete (all_children);
5930 }
5931}
5932
5933/* Compute the 'includes' field for the symtabs of all the CUs we just
5934 read. */
5935
5936static void
5937process_cu_includes (void)
5938{
5939 int ix;
5940 struct dwarf2_per_cu_data *iter;
5941
5942 for (ix = 0;
5943 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5944 ix, iter);
5945 ++ix)
5946 compute_symtab_includes (iter);
5947
5948 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5949}
5950
9cdd5dbd 5951/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
5952 already been loaded into memory. */
5953
5954static void
95554aad
TT
5955process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5956 enum language pretend_language)
10b3939b 5957{
10b3939b 5958 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 5959 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
5960 CORE_ADDR lowpc, highpc;
5961 struct symtab *symtab;
3da10d80 5962 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
5963 CORE_ADDR baseaddr;
5964
45cfd468
DE
5965 if (dwarf2_read_debug)
5966 {
5967 fprintf_unfiltered (gdb_stdlog,
5968 "Expanding symtab of %s at offset 0x%x\n",
5969 per_cu->is_debug_types ? "TU" : "CU",
5970 per_cu->offset.sect_off);
5971 }
5972
10b3939b
DJ
5973 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5974
10b3939b
DJ
5975 buildsym_init ();
5976 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 5977 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
5978
5979 cu->list_in_scope = &file_symbols;
c906108c 5980
95554aad
TT
5981 cu->language = pretend_language;
5982 cu->language_defn = language_def (cu->language);
5983
c906108c 5984 /* Do line number decoding in read_file_scope () */
10b3939b 5985 process_die (cu->dies, cu);
c906108c 5986
a766d390
DE
5987 /* For now fudge the Go package. */
5988 if (cu->language == language_go)
5989 fixup_go_packaging (cu);
5990
3da10d80
KS
5991 /* Now that we have processed all the DIEs in the CU, all the types
5992 should be complete, and it should now be safe to compute all of the
5993 physnames. */
5994 compute_delayed_physnames (cu);
5995 do_cleanups (delayed_list_cleanup);
5996
fae299cd
DC
5997 /* Some compilers don't define a DW_AT_high_pc attribute for the
5998 compilation unit. If the DW_AT_high_pc is missing, synthesize
5999 it, by scanning the DIE's below the compilation unit. */
10b3939b 6000 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 6001
613e1657 6002 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 6003
8be455d7 6004 if (symtab != NULL)
c906108c 6005 {
df15bd07 6006 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 6007
8be455d7
JK
6008 /* Set symtab language to language from DW_AT_language. If the
6009 compilation is from a C file generated by language preprocessors, do
6010 not set the language if it was already deduced by start_subfile. */
6011 if (!(cu->language == language_c && symtab->language != language_c))
6012 symtab->language = cu->language;
6013
6014 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
6015 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
6016 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
6017 there were bugs in prologue debug info, fixed later in GCC-4.5
6018 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
6019
6020 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
6021 needed, it would be wrong due to missing DW_AT_producer there.
6022
6023 Still one can confuse GDB by using non-standard GCC compilation
6024 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
6025 */
ab260dad 6026 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 6027 symtab->locations_valid = 1;
e0d00bc7
JK
6028
6029 if (gcc_4_minor >= 5)
6030 symtab->epilogue_unwind_valid = 1;
96408a79
SA
6031
6032 symtab->call_site_htab = cu->call_site_htab;
c906108c 6033 }
9291a0cd
TT
6034
6035 if (dwarf2_per_objfile->using_index)
6036 per_cu->v.quick->symtab = symtab;
6037 else
6038 {
6039 struct partial_symtab *pst = per_cu->v.psymtab;
6040 pst->symtab = symtab;
6041 pst->readin = 1;
6042 }
c906108c 6043
95554aad
TT
6044 /* Push it for inclusion processing later. */
6045 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
6046
c906108c 6047 do_cleanups (back_to);
45cfd468
DE
6048
6049 if (dwarf2_read_debug)
6050 {
6051 fprintf_unfiltered (gdb_stdlog,
6052 "Done expanding symtab of %s at offset 0x%x\n",
6053 per_cu->is_debug_types ? "TU" : "CU",
6054 per_cu->offset.sect_off);
6055 }
c906108c
SS
6056}
6057
95554aad
TT
6058/* Process an imported unit DIE. */
6059
6060static void
6061process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
6062{
6063 struct attribute *attr;
6064
6065 attr = dwarf2_attr (die, DW_AT_import, cu);
6066 if (attr != NULL)
6067 {
6068 struct dwarf2_per_cu_data *per_cu;
6069 struct symtab *imported_symtab;
6070 sect_offset offset;
6071
6072 offset = dwarf2_get_ref_die_offset (attr);
6073 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
6074
6075 /* Queue the unit, if needed. */
6076 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
6077 load_full_comp_unit (per_cu, cu->language);
6078
6079 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
6080 per_cu);
6081 }
6082}
6083
c906108c
SS
6084/* Process a die and its children. */
6085
6086static void
e7c27a73 6087process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
6088{
6089 switch (die->tag)
6090 {
6091 case DW_TAG_padding:
6092 break;
6093 case DW_TAG_compile_unit:
95554aad 6094 case DW_TAG_partial_unit:
e7c27a73 6095 read_file_scope (die, cu);
c906108c 6096 break;
348e048f
DE
6097 case DW_TAG_type_unit:
6098 read_type_unit_scope (die, cu);
6099 break;
c906108c 6100 case DW_TAG_subprogram:
c906108c 6101 case DW_TAG_inlined_subroutine:
edb3359d 6102 read_func_scope (die, cu);
c906108c
SS
6103 break;
6104 case DW_TAG_lexical_block:
14898363
L
6105 case DW_TAG_try_block:
6106 case DW_TAG_catch_block:
e7c27a73 6107 read_lexical_block_scope (die, cu);
c906108c 6108 break;
96408a79
SA
6109 case DW_TAG_GNU_call_site:
6110 read_call_site_scope (die, cu);
6111 break;
c906108c 6112 case DW_TAG_class_type:
680b30c7 6113 case DW_TAG_interface_type:
c906108c
SS
6114 case DW_TAG_structure_type:
6115 case DW_TAG_union_type:
134d01f1 6116 process_structure_scope (die, cu);
c906108c
SS
6117 break;
6118 case DW_TAG_enumeration_type:
134d01f1 6119 process_enumeration_scope (die, cu);
c906108c 6120 break;
134d01f1 6121
f792889a
DJ
6122 /* These dies have a type, but processing them does not create
6123 a symbol or recurse to process the children. Therefore we can
6124 read them on-demand through read_type_die. */
c906108c 6125 case DW_TAG_subroutine_type:
72019c9c 6126 case DW_TAG_set_type:
c906108c 6127 case DW_TAG_array_type:
c906108c 6128 case DW_TAG_pointer_type:
c906108c 6129 case DW_TAG_ptr_to_member_type:
c906108c 6130 case DW_TAG_reference_type:
c906108c 6131 case DW_TAG_string_type:
c906108c 6132 break;
134d01f1 6133
c906108c 6134 case DW_TAG_base_type:
a02abb62 6135 case DW_TAG_subrange_type:
cb249c71 6136 case DW_TAG_typedef:
134d01f1
DJ
6137 /* Add a typedef symbol for the type definition, if it has a
6138 DW_AT_name. */
f792889a 6139 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 6140 break;
c906108c 6141 case DW_TAG_common_block:
e7c27a73 6142 read_common_block (die, cu);
c906108c
SS
6143 break;
6144 case DW_TAG_common_inclusion:
6145 break;
d9fa45fe 6146 case DW_TAG_namespace:
63d06c5c 6147 processing_has_namespace_info = 1;
e7c27a73 6148 read_namespace (die, cu);
d9fa45fe 6149 break;
5d7cb8df 6150 case DW_TAG_module:
f55ee35c 6151 processing_has_namespace_info = 1;
5d7cb8df
JK
6152 read_module (die, cu);
6153 break;
d9fa45fe
DC
6154 case DW_TAG_imported_declaration:
6155 case DW_TAG_imported_module:
63d06c5c 6156 processing_has_namespace_info = 1;
27aa8d6a
SW
6157 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
6158 || cu->language != language_fortran))
6159 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
6160 dwarf_tag_name (die->tag));
6161 read_import_statement (die, cu);
d9fa45fe 6162 break;
95554aad
TT
6163
6164 case DW_TAG_imported_unit:
6165 process_imported_unit_die (die, cu);
6166 break;
6167
c906108c 6168 default:
e7c27a73 6169 new_symbol (die, NULL, cu);
c906108c
SS
6170 break;
6171 }
6172}
6173
94af9270
KS
6174/* A helper function for dwarf2_compute_name which determines whether DIE
6175 needs to have the name of the scope prepended to the name listed in the
6176 die. */
6177
6178static int
6179die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
6180{
1c809c68
TT
6181 struct attribute *attr;
6182
94af9270
KS
6183 switch (die->tag)
6184 {
6185 case DW_TAG_namespace:
6186 case DW_TAG_typedef:
6187 case DW_TAG_class_type:
6188 case DW_TAG_interface_type:
6189 case DW_TAG_structure_type:
6190 case DW_TAG_union_type:
6191 case DW_TAG_enumeration_type:
6192 case DW_TAG_enumerator:
6193 case DW_TAG_subprogram:
6194 case DW_TAG_member:
6195 return 1;
6196
6197 case DW_TAG_variable:
c2b0a229 6198 case DW_TAG_constant:
94af9270
KS
6199 /* We only need to prefix "globally" visible variables. These include
6200 any variable marked with DW_AT_external or any variable that
6201 lives in a namespace. [Variables in anonymous namespaces
6202 require prefixing, but they are not DW_AT_external.] */
6203
6204 if (dwarf2_attr (die, DW_AT_specification, cu))
6205 {
6206 struct dwarf2_cu *spec_cu = cu;
9a619af0 6207
94af9270
KS
6208 return die_needs_namespace (die_specification (die, &spec_cu),
6209 spec_cu);
6210 }
6211
1c809c68 6212 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
6213 if (attr == NULL && die->parent->tag != DW_TAG_namespace
6214 && die->parent->tag != DW_TAG_module)
1c809c68
TT
6215 return 0;
6216 /* A variable in a lexical block of some kind does not need a
6217 namespace, even though in C++ such variables may be external
6218 and have a mangled name. */
6219 if (die->parent->tag == DW_TAG_lexical_block
6220 || die->parent->tag == DW_TAG_try_block
1054b214
TT
6221 || die->parent->tag == DW_TAG_catch_block
6222 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
6223 return 0;
6224 return 1;
94af9270
KS
6225
6226 default:
6227 return 0;
6228 }
6229}
6230
98bfdba5
PA
6231/* Retrieve the last character from a mem_file. */
6232
6233static void
6234do_ui_file_peek_last (void *object, const char *buffer, long length)
6235{
6236 char *last_char_p = (char *) object;
6237
6238 if (length > 0)
6239 *last_char_p = buffer[length - 1];
6240}
6241
94af9270 6242/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
6243 compute the physname for the object, which include a method's:
6244 - formal parameters (C++/Java),
6245 - receiver type (Go),
6246 - return type (Java).
6247
6248 The term "physname" is a bit confusing.
6249 For C++, for example, it is the demangled name.
6250 For Go, for example, it's the mangled name.
94af9270 6251
af6b7be1
JB
6252 For Ada, return the DIE's linkage name rather than the fully qualified
6253 name. PHYSNAME is ignored..
6254
94af9270
KS
6255 The result is allocated on the objfile_obstack and canonicalized. */
6256
6257static const char *
6258dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
6259 int physname)
6260{
bb5ed363
DE
6261 struct objfile *objfile = cu->objfile;
6262
94af9270
KS
6263 if (name == NULL)
6264 name = dwarf2_name (die, cu);
6265
f55ee35c
JK
6266 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
6267 compute it by typename_concat inside GDB. */
6268 if (cu->language == language_ada
6269 || (cu->language == language_fortran && physname))
6270 {
6271 /* For Ada unit, we prefer the linkage name over the name, as
6272 the former contains the exported name, which the user expects
6273 to be able to reference. Ideally, we want the user to be able
6274 to reference this entity using either natural or linkage name,
6275 but we haven't started looking at this enhancement yet. */
6276 struct attribute *attr;
6277
6278 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6279 if (attr == NULL)
6280 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6281 if (attr && DW_STRING (attr))
6282 return DW_STRING (attr);
6283 }
6284
94af9270
KS
6285 /* These are the only languages we know how to qualify names in. */
6286 if (name != NULL
f55ee35c
JK
6287 && (cu->language == language_cplus || cu->language == language_java
6288 || cu->language == language_fortran))
94af9270
KS
6289 {
6290 if (die_needs_namespace (die, cu))
6291 {
6292 long length;
0d5cff50 6293 const char *prefix;
94af9270
KS
6294 struct ui_file *buf;
6295
6296 prefix = determine_prefix (die, cu);
6297 buf = mem_fileopen ();
6298 if (*prefix != '\0')
6299 {
f55ee35c
JK
6300 char *prefixed_name = typename_concat (NULL, prefix, name,
6301 physname, cu);
9a619af0 6302
94af9270
KS
6303 fputs_unfiltered (prefixed_name, buf);
6304 xfree (prefixed_name);
6305 }
6306 else
62d5b8da 6307 fputs_unfiltered (name, buf);
94af9270 6308
98bfdba5
PA
6309 /* Template parameters may be specified in the DIE's DW_AT_name, or
6310 as children with DW_TAG_template_type_param or
6311 DW_TAG_value_type_param. If the latter, add them to the name
6312 here. If the name already has template parameters, then
6313 skip this step; some versions of GCC emit both, and
6314 it is more efficient to use the pre-computed name.
6315
6316 Something to keep in mind about this process: it is very
6317 unlikely, or in some cases downright impossible, to produce
6318 something that will match the mangled name of a function.
6319 If the definition of the function has the same debug info,
6320 we should be able to match up with it anyway. But fallbacks
6321 using the minimal symbol, for instance to find a method
6322 implemented in a stripped copy of libstdc++, will not work.
6323 If we do not have debug info for the definition, we will have to
6324 match them up some other way.
6325
6326 When we do name matching there is a related problem with function
6327 templates; two instantiated function templates are allowed to
6328 differ only by their return types, which we do not add here. */
6329
6330 if (cu->language == language_cplus && strchr (name, '<') == NULL)
6331 {
6332 struct attribute *attr;
6333 struct die_info *child;
6334 int first = 1;
6335
6336 die->building_fullname = 1;
6337
6338 for (child = die->child; child != NULL; child = child->sibling)
6339 {
6340 struct type *type;
12df843f 6341 LONGEST value;
98bfdba5
PA
6342 gdb_byte *bytes;
6343 struct dwarf2_locexpr_baton *baton;
6344 struct value *v;
6345
6346 if (child->tag != DW_TAG_template_type_param
6347 && child->tag != DW_TAG_template_value_param)
6348 continue;
6349
6350 if (first)
6351 {
6352 fputs_unfiltered ("<", buf);
6353 first = 0;
6354 }
6355 else
6356 fputs_unfiltered (", ", buf);
6357
6358 attr = dwarf2_attr (child, DW_AT_type, cu);
6359 if (attr == NULL)
6360 {
6361 complaint (&symfile_complaints,
6362 _("template parameter missing DW_AT_type"));
6363 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6364 continue;
6365 }
6366 type = die_type (child, cu);
6367
6368 if (child->tag == DW_TAG_template_type_param)
6369 {
6370 c_print_type (type, "", buf, -1, 0);
6371 continue;
6372 }
6373
6374 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6375 if (attr == NULL)
6376 {
6377 complaint (&symfile_complaints,
3e43a32a
MS
6378 _("template parameter missing "
6379 "DW_AT_const_value"));
98bfdba5
PA
6380 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6381 continue;
6382 }
6383
6384 dwarf2_const_value_attr (attr, type, name,
6385 &cu->comp_unit_obstack, cu,
6386 &value, &bytes, &baton);
6387
6388 if (TYPE_NOSIGN (type))
6389 /* GDB prints characters as NUMBER 'CHAR'. If that's
6390 changed, this can use value_print instead. */
6391 c_printchar (value, type, buf);
6392 else
6393 {
6394 struct value_print_options opts;
6395
6396 if (baton != NULL)
6397 v = dwarf2_evaluate_loc_desc (type, NULL,
6398 baton->data,
6399 baton->size,
6400 baton->per_cu);
6401 else if (bytes != NULL)
6402 {
6403 v = allocate_value (type);
6404 memcpy (value_contents_writeable (v), bytes,
6405 TYPE_LENGTH (type));
6406 }
6407 else
6408 v = value_from_longest (type, value);
6409
3e43a32a
MS
6410 /* Specify decimal so that we do not depend on
6411 the radix. */
98bfdba5
PA
6412 get_formatted_print_options (&opts, 'd');
6413 opts.raw = 1;
6414 value_print (v, buf, &opts);
6415 release_value (v);
6416 value_free (v);
6417 }
6418 }
6419
6420 die->building_fullname = 0;
6421
6422 if (!first)
6423 {
6424 /* Close the argument list, with a space if necessary
6425 (nested templates). */
6426 char last_char = '\0';
6427 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6428 if (last_char == '>')
6429 fputs_unfiltered (" >", buf);
6430 else
6431 fputs_unfiltered (">", buf);
6432 }
6433 }
6434
94af9270
KS
6435 /* For Java and C++ methods, append formal parameter type
6436 information, if PHYSNAME. */
6e70227d 6437
94af9270
KS
6438 if (physname && die->tag == DW_TAG_subprogram
6439 && (cu->language == language_cplus
6440 || cu->language == language_java))
6441 {
6442 struct type *type = read_type_die (die, cu);
6443
3167638f 6444 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
6445
6446 if (cu->language == language_java)
6447 {
6448 /* For java, we must append the return type to method
0963b4bd 6449 names. */
94af9270
KS
6450 if (die->tag == DW_TAG_subprogram)
6451 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6452 0, 0);
6453 }
6454 else if (cu->language == language_cplus)
6455 {
60430eff
DJ
6456 /* Assume that an artificial first parameter is
6457 "this", but do not crash if it is not. RealView
6458 marks unnamed (and thus unused) parameters as
6459 artificial; there is no way to differentiate
6460 the two cases. */
94af9270
KS
6461 if (TYPE_NFIELDS (type) > 0
6462 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 6463 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
6464 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6465 0))))
94af9270
KS
6466 fputs_unfiltered (" const", buf);
6467 }
6468 }
6469
bb5ed363 6470 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
6471 &length);
6472 ui_file_delete (buf);
6473
6474 if (cu->language == language_cplus)
6475 {
6476 char *cname
6477 = dwarf2_canonicalize_name (name, cu,
bb5ed363 6478 &objfile->objfile_obstack);
9a619af0 6479
94af9270
KS
6480 if (cname != NULL)
6481 name = cname;
6482 }
6483 }
6484 }
6485
6486 return name;
6487}
6488
0114d602
DJ
6489/* Return the fully qualified name of DIE, based on its DW_AT_name.
6490 If scope qualifiers are appropriate they will be added. The result
6491 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
6492 not have a name. NAME may either be from a previous call to
6493 dwarf2_name or NULL.
6494
0963b4bd 6495 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
6496
6497static const char *
94af9270 6498dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 6499{
94af9270
KS
6500 return dwarf2_compute_name (name, die, cu, 0);
6501}
0114d602 6502
94af9270
KS
6503/* Construct a physname for the given DIE in CU. NAME may either be
6504 from a previous call to dwarf2_name or NULL. The result will be
6505 allocated on the objfile_objstack or NULL if the DIE does not have a
6506 name.
0114d602 6507
94af9270 6508 The output string will be canonicalized (if C++/Java). */
0114d602 6509
94af9270
KS
6510static const char *
6511dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6512{
bb5ed363 6513 struct objfile *objfile = cu->objfile;
900e11f9
JK
6514 struct attribute *attr;
6515 const char *retval, *mangled = NULL, *canon = NULL;
6516 struct cleanup *back_to;
6517 int need_copy = 1;
6518
6519 /* In this case dwarf2_compute_name is just a shortcut not building anything
6520 on its own. */
6521 if (!die_needs_namespace (die, cu))
6522 return dwarf2_compute_name (name, die, cu, 1);
6523
6524 back_to = make_cleanup (null_cleanup, NULL);
6525
6526 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6527 if (!attr)
6528 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6529
6530 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6531 has computed. */
6532 if (attr && DW_STRING (attr))
6533 {
6534 char *demangled;
6535
6536 mangled = DW_STRING (attr);
6537
6538 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6539 type. It is easier for GDB users to search for such functions as
6540 `name(params)' than `long name(params)'. In such case the minimal
6541 symbol names do not match the full symbol names but for template
6542 functions there is never a need to look up their definition from their
6543 declaration so the only disadvantage remains the minimal symbol
6544 variant `long name(params)' does not have the proper inferior type.
6545 */
6546
a766d390
DE
6547 if (cu->language == language_go)
6548 {
6549 /* This is a lie, but we already lie to the caller new_symbol_full.
6550 new_symbol_full assumes we return the mangled name.
6551 This just undoes that lie until things are cleaned up. */
6552 demangled = NULL;
6553 }
6554 else
6555 {
6556 demangled = cplus_demangle (mangled,
6557 (DMGL_PARAMS | DMGL_ANSI
6558 | (cu->language == language_java
6559 ? DMGL_JAVA | DMGL_RET_POSTFIX
6560 : DMGL_RET_DROP)));
6561 }
900e11f9
JK
6562 if (demangled)
6563 {
6564 make_cleanup (xfree, demangled);
6565 canon = demangled;
6566 }
6567 else
6568 {
6569 canon = mangled;
6570 need_copy = 0;
6571 }
6572 }
6573
6574 if (canon == NULL || check_physname)
6575 {
6576 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6577
6578 if (canon != NULL && strcmp (physname, canon) != 0)
6579 {
6580 /* It may not mean a bug in GDB. The compiler could also
6581 compute DW_AT_linkage_name incorrectly. But in such case
6582 GDB would need to be bug-to-bug compatible. */
6583
6584 complaint (&symfile_complaints,
6585 _("Computed physname <%s> does not match demangled <%s> "
6586 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 6587 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
6588
6589 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6590 is available here - over computed PHYSNAME. It is safer
6591 against both buggy GDB and buggy compilers. */
6592
6593 retval = canon;
6594 }
6595 else
6596 {
6597 retval = physname;
6598 need_copy = 0;
6599 }
6600 }
6601 else
6602 retval = canon;
6603
6604 if (need_copy)
6605 retval = obsavestring (retval, strlen (retval),
bb5ed363 6606 &objfile->objfile_obstack);
900e11f9
JK
6607
6608 do_cleanups (back_to);
6609 return retval;
0114d602
DJ
6610}
6611
27aa8d6a
SW
6612/* Read the import statement specified by the given die and record it. */
6613
6614static void
6615read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6616{
bb5ed363 6617 struct objfile *objfile = cu->objfile;
27aa8d6a 6618 struct attribute *import_attr;
32019081 6619 struct die_info *imported_die, *child_die;
de4affc9 6620 struct dwarf2_cu *imported_cu;
27aa8d6a 6621 const char *imported_name;
794684b6 6622 const char *imported_name_prefix;
13387711
SW
6623 const char *canonical_name;
6624 const char *import_alias;
6625 const char *imported_declaration = NULL;
794684b6 6626 const char *import_prefix;
32019081
JK
6627 VEC (const_char_ptr) *excludes = NULL;
6628 struct cleanup *cleanups;
13387711
SW
6629
6630 char *temp;
27aa8d6a
SW
6631
6632 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6633 if (import_attr == NULL)
6634 {
6635 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6636 dwarf_tag_name (die->tag));
6637 return;
6638 }
6639
de4affc9
CC
6640 imported_cu = cu;
6641 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6642 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
6643 if (imported_name == NULL)
6644 {
6645 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6646
6647 The import in the following code:
6648 namespace A
6649 {
6650 typedef int B;
6651 }
6652
6653 int main ()
6654 {
6655 using A::B;
6656 B b;
6657 return b;
6658 }
6659
6660 ...
6661 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6662 <52> DW_AT_decl_file : 1
6663 <53> DW_AT_decl_line : 6
6664 <54> DW_AT_import : <0x75>
6665 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6666 <59> DW_AT_name : B
6667 <5b> DW_AT_decl_file : 1
6668 <5c> DW_AT_decl_line : 2
6669 <5d> DW_AT_type : <0x6e>
6670 ...
6671 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6672 <76> DW_AT_byte_size : 4
6673 <77> DW_AT_encoding : 5 (signed)
6674
6675 imports the wrong die ( 0x75 instead of 0x58 ).
6676 This case will be ignored until the gcc bug is fixed. */
6677 return;
6678 }
6679
82856980
SW
6680 /* Figure out the local name after import. */
6681 import_alias = dwarf2_name (die, cu);
27aa8d6a 6682
794684b6
SW
6683 /* Figure out where the statement is being imported to. */
6684 import_prefix = determine_prefix (die, cu);
6685
6686 /* Figure out what the scope of the imported die is and prepend it
6687 to the name of the imported die. */
de4affc9 6688 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 6689
f55ee35c
JK
6690 if (imported_die->tag != DW_TAG_namespace
6691 && imported_die->tag != DW_TAG_module)
794684b6 6692 {
13387711
SW
6693 imported_declaration = imported_name;
6694 canonical_name = imported_name_prefix;
794684b6 6695 }
13387711 6696 else if (strlen (imported_name_prefix) > 0)
794684b6 6697 {
13387711
SW
6698 temp = alloca (strlen (imported_name_prefix)
6699 + 2 + strlen (imported_name) + 1);
6700 strcpy (temp, imported_name_prefix);
6701 strcat (temp, "::");
6702 strcat (temp, imported_name);
6703 canonical_name = temp;
794684b6 6704 }
13387711
SW
6705 else
6706 canonical_name = imported_name;
794684b6 6707
32019081
JK
6708 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6709
6710 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6711 for (child_die = die->child; child_die && child_die->tag;
6712 child_die = sibling_die (child_die))
6713 {
6714 /* DWARF-4: A Fortran use statement with a “rename list” may be
6715 represented by an imported module entry with an import attribute
6716 referring to the module and owned entries corresponding to those
6717 entities that are renamed as part of being imported. */
6718
6719 if (child_die->tag != DW_TAG_imported_declaration)
6720 {
6721 complaint (&symfile_complaints,
6722 _("child DW_TAG_imported_declaration expected "
6723 "- DIE at 0x%x [in module %s]"),
b64f50a1 6724 child_die->offset.sect_off, objfile->name);
32019081
JK
6725 continue;
6726 }
6727
6728 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6729 if (import_attr == NULL)
6730 {
6731 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6732 dwarf_tag_name (child_die->tag));
6733 continue;
6734 }
6735
6736 imported_cu = cu;
6737 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6738 &imported_cu);
6739 imported_name = dwarf2_name (imported_die, imported_cu);
6740 if (imported_name == NULL)
6741 {
6742 complaint (&symfile_complaints,
6743 _("child DW_TAG_imported_declaration has unknown "
6744 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 6745 child_die->offset.sect_off, objfile->name);
32019081
JK
6746 continue;
6747 }
6748
6749 VEC_safe_push (const_char_ptr, excludes, imported_name);
6750
6751 process_die (child_die, cu);
6752 }
6753
c0cc3a76
SW
6754 cp_add_using_directive (import_prefix,
6755 canonical_name,
6756 import_alias,
13387711 6757 imported_declaration,
32019081 6758 excludes,
bb5ed363 6759 &objfile->objfile_obstack);
32019081
JK
6760
6761 do_cleanups (cleanups);
27aa8d6a
SW
6762}
6763
ae2de4f8
DE
6764/* Cleanup function for read_file_scope. */
6765
cb1df416
DJ
6766static void
6767free_cu_line_header (void *arg)
6768{
6769 struct dwarf2_cu *cu = arg;
6770
6771 free_line_header (cu->line_header);
6772 cu->line_header = NULL;
6773}
6774
9291a0cd
TT
6775static void
6776find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6777 char **name, char **comp_dir)
6778{
6779 struct attribute *attr;
6780
6781 *name = NULL;
6782 *comp_dir = NULL;
6783
6784 /* Find the filename. Do not use dwarf2_name here, since the filename
6785 is not a source language identifier. */
6786 attr = dwarf2_attr (die, DW_AT_name, cu);
6787 if (attr)
6788 {
6789 *name = DW_STRING (attr);
6790 }
6791
6792 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6793 if (attr)
6794 *comp_dir = DW_STRING (attr);
6795 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6796 {
6797 *comp_dir = ldirname (*name);
6798 if (*comp_dir != NULL)
6799 make_cleanup (xfree, *comp_dir);
6800 }
6801 if (*comp_dir != NULL)
6802 {
6803 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6804 directory, get rid of it. */
6805 char *cp = strchr (*comp_dir, ':');
6806
6807 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6808 *comp_dir = cp + 1;
6809 }
6810
6811 if (*name == NULL)
6812 *name = "<unknown>";
6813}
6814
f3f5162e
DE
6815/* Handle DW_AT_stmt_list for a compilation unit or type unit.
6816 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6817 COMP_DIR is the compilation directory.
6818 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
6819
6820static void
6821handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 6822 const char *comp_dir, int want_line_info)
2ab95328
TT
6823{
6824 struct attribute *attr;
2ab95328 6825
2ab95328
TT
6826 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6827 if (attr)
6828 {
6829 unsigned int line_offset = DW_UNSND (attr);
6830 struct line_header *line_header
3019eac3 6831 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
6832
6833 if (line_header)
dee91e82
DE
6834 {
6835 cu->line_header = line_header;
6836 make_cleanup (free_cu_line_header, cu);
f3f5162e 6837 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
dee91e82 6838 }
2ab95328
TT
6839 }
6840}
6841
95554aad 6842/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 6843
c906108c 6844static void
e7c27a73 6845read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6846{
dee91e82 6847 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 6848 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 6849 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
6850 CORE_ADDR highpc = ((CORE_ADDR) 0);
6851 struct attribute *attr;
e1024ff1 6852 char *name = NULL;
c906108c
SS
6853 char *comp_dir = NULL;
6854 struct die_info *child_die;
6855 bfd *abfd = objfile->obfd;
e142c38c 6856 CORE_ADDR baseaddr;
6e70227d 6857
e142c38c 6858 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6859
fae299cd 6860 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
6861
6862 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6863 from finish_block. */
2acceee2 6864 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
6865 lowpc = highpc;
6866 lowpc += baseaddr;
6867 highpc += baseaddr;
6868
9291a0cd 6869 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 6870
95554aad 6871 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 6872
f4b8a18d
KW
6873 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6874 standardised yet. As a workaround for the language detection we fall
6875 back to the DW_AT_producer string. */
6876 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6877 cu->language = language_opencl;
6878
3019eac3
DE
6879 /* Similar hack for Go. */
6880 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6881 set_cu_language (DW_LANG_Go, cu);
6882
6883 /* We assume that we're processing GCC output. */
6884 processing_gcc_compilation = 2;
6885
6886 processing_has_namespace_info = 0;
6887
6888 start_symtab (name, comp_dir, lowpc);
6889 record_debugformat ("DWARF 2");
6890 record_producer (cu->producer);
6891
6892 /* Decode line number information if present. We do this before
6893 processing child DIEs, so that the line header table is available
6894 for DW_AT_decl_file. */
6895 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6896
6897 /* Process all dies in compilation unit. */
6898 if (die->child != NULL)
6899 {
6900 child_die = die->child;
6901 while (child_die && child_die->tag)
6902 {
6903 process_die (child_die, cu);
6904 child_die = sibling_die (child_die);
6905 }
6906 }
6907
6908 /* Decode macro information, if present. Dwarf 2 macro information
6909 refers to information in the line number info statement program
6910 header, so we can only read it if we've read the header
6911 successfully. */
6912 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6913 if (attr && cu->line_header)
6914 {
6915 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6916 complaint (&symfile_complaints,
6917 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6918
09262596 6919 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
3019eac3
DE
6920 }
6921 else
6922 {
6923 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6924 if (attr && cu->line_header)
6925 {
6926 unsigned int macro_offset = DW_UNSND (attr);
6927
09262596 6928 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
3019eac3
DE
6929 }
6930 }
6931
6932 do_cleanups (back_to);
6933}
6934
6935/* Process DW_TAG_type_unit.
6936 For TUs we want to skip the first top level sibling if it's not the
6937 actual type being defined by this TU. In this case the first top
6938 level sibling is there to provide context only. */
6939
6940static void
6941read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6942{
6943 struct objfile *objfile = cu->objfile;
6944 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6945 CORE_ADDR lowpc;
6946 struct attribute *attr;
6947 char *name = NULL;
6948 char *comp_dir = NULL;
6949 struct die_info *child_die;
6950 bfd *abfd = objfile->obfd;
6951
6952 /* start_symtab needs a low pc, but we don't really have one.
6953 Do what read_file_scope would do in the absence of such info. */
6954 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6955
6956 /* Find the filename. Do not use dwarf2_name here, since the filename
6957 is not a source language identifier. */
6958 attr = dwarf2_attr (die, DW_AT_name, cu);
6959 if (attr)
6960 name = DW_STRING (attr);
6961
6962 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6963 if (attr)
6964 comp_dir = DW_STRING (attr);
6965 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6966 {
6967 comp_dir = ldirname (name);
6968 if (comp_dir != NULL)
6969 make_cleanup (xfree, comp_dir);
6970 }
6971
6972 if (name == NULL)
6973 name = "<unknown>";
6974
95554aad 6975 prepare_one_comp_unit (cu, die, language_minimal);
3019eac3
DE
6976
6977 /* We assume that we're processing GCC output. */
6978 processing_gcc_compilation = 2;
6979
6980 processing_has_namespace_info = 0;
6981
6982 start_symtab (name, comp_dir, lowpc);
6983 record_debugformat ("DWARF 2");
6984 record_producer (cu->producer);
6985
6986 /* Decode line number information if present. We do this before
6987 processing child DIEs, so that the line header table is available
6988 for DW_AT_decl_file.
6989 We don't need the pc/line-number mapping for type units. */
6990 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6991
6992 /* Process the dies in the type unit. */
6993 if (die->child == NULL)
6994 {
6995 dump_die_for_error (die);
6996 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6997 bfd_get_filename (abfd));
6998 }
6999
7000 child_die = die->child;
7001
7002 while (child_die && child_die->tag)
7003 {
7004 process_die (child_die, cu);
7005
7006 child_die = sibling_die (child_die);
7007 }
7008
7009 do_cleanups (back_to);
7010}
7011\f
7012/* DWO files. */
7013
7014static hashval_t
7015hash_dwo_file (const void *item)
7016{
7017 const struct dwo_file *dwo_file = item;
7018
7019 return htab_hash_string (dwo_file->dwo_name);
7020}
7021
7022static int
7023eq_dwo_file (const void *item_lhs, const void *item_rhs)
7024{
7025 const struct dwo_file *lhs = item_lhs;
7026 const struct dwo_file *rhs = item_rhs;
7027
7028 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
7029}
7030
7031/* Allocate a hash table for DWO files. */
7032
7033static htab_t
7034allocate_dwo_file_hash_table (void)
7035{
7036 struct objfile *objfile = dwarf2_per_objfile->objfile;
7037
7038 return htab_create_alloc_ex (41,
7039 hash_dwo_file,
7040 eq_dwo_file,
7041 NULL,
7042 &objfile->objfile_obstack,
7043 hashtab_obstack_allocate,
7044 dummy_obstack_deallocate);
7045}
7046
7047static hashval_t
7048hash_dwo_unit (const void *item)
7049{
7050 const struct dwo_unit *dwo_unit = item;
7051
7052 /* This drops the top 32 bits of the id, but is ok for a hash. */
7053 return dwo_unit->signature;
7054}
7055
7056static int
7057eq_dwo_unit (const void *item_lhs, const void *item_rhs)
7058{
7059 const struct dwo_unit *lhs = item_lhs;
7060 const struct dwo_unit *rhs = item_rhs;
7061
7062 /* The signature is assumed to be unique within the DWO file.
7063 So while object file CU dwo_id's always have the value zero,
7064 that's OK, assuming each object file DWO file has only one CU,
7065 and that's the rule for now. */
7066 return lhs->signature == rhs->signature;
7067}
7068
7069/* Allocate a hash table for DWO CUs,TUs.
7070 There is one of these tables for each of CUs,TUs for each DWO file. */
7071
7072static htab_t
7073allocate_dwo_unit_table (struct objfile *objfile)
7074{
7075 /* Start out with a pretty small number.
7076 Generally DWO files contain only one CU and maybe some TUs. */
7077 return htab_create_alloc_ex (3,
7078 hash_dwo_unit,
7079 eq_dwo_unit,
7080 NULL,
7081 &objfile->objfile_obstack,
7082 hashtab_obstack_allocate,
7083 dummy_obstack_deallocate);
7084}
7085
7086/* This function is mapped across the sections and remembers the offset and
7087 size of each of the DWO debugging sections we are interested in. */
7088
7089static void
7090dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
7091{
7092 struct dwo_file *dwo_file = dwo_file_ptr;
7093 const struct dwo_section_names *names = &dwo_section_names;
7094
7095 if (section_is_p (sectp->name, &names->abbrev_dwo))
7096 {
7097 dwo_file->sections.abbrev.asection = sectp;
7098 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
7099 }
7100 else if (section_is_p (sectp->name, &names->info_dwo))
7101 {
7102 dwo_file->sections.info.asection = sectp;
7103 dwo_file->sections.info.size = bfd_get_section_size (sectp);
7104 }
7105 else if (section_is_p (sectp->name, &names->line_dwo))
7106 {
7107 dwo_file->sections.line.asection = sectp;
7108 dwo_file->sections.line.size = bfd_get_section_size (sectp);
7109 }
7110 else if (section_is_p (sectp->name, &names->loc_dwo))
7111 {
7112 dwo_file->sections.loc.asection = sectp;
7113 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
7114 }
09262596
DE
7115 else if (section_is_p (sectp->name, &names->macinfo_dwo))
7116 {
7117 dwo_file->sections.macinfo.asection = sectp;
7118 dwo_file->sections.macinfo.size = bfd_get_section_size (sectp);
7119 }
7120 else if (section_is_p (sectp->name, &names->macro_dwo))
7121 {
7122 dwo_file->sections.macro.asection = sectp;
7123 dwo_file->sections.macro.size = bfd_get_section_size (sectp);
7124 }
3019eac3
DE
7125 else if (section_is_p (sectp->name, &names->str_dwo))
7126 {
7127 dwo_file->sections.str.asection = sectp;
7128 dwo_file->sections.str.size = bfd_get_section_size (sectp);
7129 }
7130 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
7131 {
7132 dwo_file->sections.str_offsets.asection = sectp;
7133 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
7134 }
7135 else if (section_is_p (sectp->name, &names->types_dwo))
7136 {
7137 struct dwarf2_section_info type_section;
7138
7139 memset (&type_section, 0, sizeof (type_section));
7140 type_section.asection = sectp;
7141 type_section.size = bfd_get_section_size (sectp);
7142 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
7143 &type_section);
7144 }
7145}
7146
7147/* Structure used to pass data to create_debug_info_hash_table_reader. */
7148
7149struct create_dwo_info_table_data
7150{
7151 struct dwo_file *dwo_file;
7152 htab_t cu_htab;
7153};
7154
7155/* die_reader_func for create_debug_info_hash_table. */
7156
7157static void
7158create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
7159 gdb_byte *info_ptr,
7160 struct die_info *comp_unit_die,
7161 int has_children,
7162 void *datap)
7163{
7164 struct dwarf2_cu *cu = reader->cu;
7165 struct objfile *objfile = dwarf2_per_objfile->objfile;
7166 sect_offset offset = cu->per_cu->offset;
7167 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
7168 struct create_dwo_info_table_data *data = datap;
7169 struct dwo_file *dwo_file = data->dwo_file;
7170 htab_t cu_htab = data->cu_htab;
7171 void **slot;
7172 struct attribute *attr;
7173 struct dwo_unit *dwo_unit;
7174
7175 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7176 if (attr == NULL)
7177 {
7178 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
7179 " its dwo_id [in module %s]"),
7180 offset.sect_off, dwo_file->dwo_name);
7181 return;
7182 }
7183
7184 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
7185 dwo_unit->dwo_file = dwo_file;
7186 dwo_unit->signature = DW_UNSND (attr);
7187 dwo_unit->info_or_types_section = section;
7188 dwo_unit->offset = offset;
7189 dwo_unit->length = cu->per_cu->length;
7190
7191 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
7192 gdb_assert (slot != NULL);
7193 if (*slot != NULL)
7194 {
7195 const struct dwo_unit *dup_dwo_unit = *slot;
7196
7197 complaint (&symfile_complaints,
7198 _("debug entry at offset 0x%x is duplicate to the entry at"
7199 " offset 0x%x, dwo_id 0x%s [in module %s]"),
7200 offset.sect_off, dup_dwo_unit->offset.sect_off,
7201 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
7202 dwo_file->dwo_name);
7203 }
7204 else
7205 *slot = dwo_unit;
7206
09406207 7207 if (dwarf2_read_debug)
3019eac3
DE
7208 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
7209 offset.sect_off,
7210 phex (dwo_unit->signature,
7211 sizeof (dwo_unit->signature)));
7212}
7213
7214/* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
7215
7216static htab_t
7217create_debug_info_hash_table (struct dwo_file *dwo_file)
7218{
7219 struct objfile *objfile = dwarf2_per_objfile->objfile;
7220 struct dwarf2_section_info *section = &dwo_file->sections.info;
7221 bfd *abfd;
7222 htab_t cu_htab;
7223 gdb_byte *info_ptr, *end_ptr;
7224 struct create_dwo_info_table_data create_dwo_info_table_data;
7225
7226 dwarf2_read_section (objfile, section);
7227 info_ptr = section->buffer;
7228
7229 if (info_ptr == NULL)
7230 return NULL;
7231
7232 /* We can't set abfd until now because the section may be empty or
7233 not present, in which case section->asection will be NULL. */
7234 abfd = section->asection->owner;
7235
09406207 7236 if (dwarf2_read_debug)
3019eac3
DE
7237 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
7238 bfd_get_filename (abfd));
7239
7240 cu_htab = allocate_dwo_unit_table (objfile);
7241
7242 create_dwo_info_table_data.dwo_file = dwo_file;
7243 create_dwo_info_table_data.cu_htab = cu_htab;
7244
7245 end_ptr = info_ptr + section->size;
7246 while (info_ptr < end_ptr)
7247 {
7248 struct dwarf2_per_cu_data per_cu;
7249
7250 memset (&per_cu, 0, sizeof (per_cu));
7251 per_cu.objfile = objfile;
7252 per_cu.is_debug_types = 0;
7253 per_cu.offset.sect_off = info_ptr - section->buffer;
7254 per_cu.info_or_types_section = section;
7255
7256 init_cutu_and_read_dies_no_follow (&per_cu,
7257 &dwo_file->sections.abbrev,
7258 dwo_file,
7259 create_debug_info_hash_table_reader,
7260 &create_dwo_info_table_data);
7261
7262 info_ptr += per_cu.length;
7263 }
7264
7265 return cu_htab;
7266}
7267
7268/* Subroutine of open_dwo_file to simplify it.
7269 Open the file specified by FILE_NAME and hand it off to BFD for
7270 preliminary analysis. Return a newly initialized bfd *, which
7271 includes a canonicalized copy of FILE_NAME.
7272 In case of trouble, return NULL.
7273 NOTE: This function is derived from symfile_bfd_open. */
7274
7275static bfd *
7276try_open_dwo_file (const char *file_name)
7277{
7278 bfd *sym_bfd;
7279 int desc;
7280 char *absolute_name;
3019eac3
DE
7281
7282 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
7283 O_RDONLY | O_BINARY, &absolute_name);
7284 if (desc < 0)
7285 return NULL;
7286
7287 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
7288 if (!sym_bfd)
7289 {
3019eac3
DE
7290 xfree (absolute_name);
7291 return NULL;
7292 }
7293 bfd_set_cacheable (sym_bfd, 1);
7294
7295 if (!bfd_check_format (sym_bfd, bfd_object))
7296 {
7297 bfd_close (sym_bfd); /* This also closes desc. */
7298 xfree (absolute_name);
7299 return NULL;
7300 }
7301
7302 /* bfd_usrdata exists for applications and libbfd must not touch it. */
7303 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
7304
7305 return sym_bfd;
7306}
7307
7308/* Try to open DWO file DWO_NAME.
7309 COMP_DIR is the DW_AT_comp_dir attribute.
7310 The result is the bfd handle of the file.
7311 If there is a problem finding or opening the file, return NULL.
7312 Upon success, the canonicalized path of the file is stored in the bfd,
7313 same as symfile_bfd_open. */
7314
7315static bfd *
7316open_dwo_file (const char *dwo_name, const char *comp_dir)
7317{
7318 bfd *abfd;
3019eac3
DE
7319
7320 if (IS_ABSOLUTE_PATH (dwo_name))
7321 return try_open_dwo_file (dwo_name);
7322
7323 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
7324
7325 if (comp_dir != NULL)
7326 {
7327 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
7328
7329 /* NOTE: If comp_dir is a relative path, this will also try the
7330 search path, which seems useful. */
7331 abfd = try_open_dwo_file (path_to_try);
7332 xfree (path_to_try);
7333 if (abfd != NULL)
7334 return abfd;
7335 }
7336
7337 /* That didn't work, try debug-file-directory, which, despite its name,
7338 is a list of paths. */
7339
7340 if (*debug_file_directory == '\0')
7341 return NULL;
7342
7343 return try_open_dwo_file (dwo_name);
7344}
7345
7346/* Initialize the use of the DWO file specified by DWO_NAME. */
7347
7348static struct dwo_file *
7349init_dwo_file (const char *dwo_name, const char *comp_dir)
7350{
7351 struct objfile *objfile = dwarf2_per_objfile->objfile;
7352 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7353 struct dwo_file);
7354 bfd *abfd;
7355 struct cleanup *cleanups;
7356
09406207 7357 if (dwarf2_read_debug)
3019eac3
DE
7358 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7359
7360 abfd = open_dwo_file (dwo_name, comp_dir);
7361 if (abfd == NULL)
7362 return NULL;
7363 dwo_file->dwo_name = dwo_name;
7364 dwo_file->dwo_bfd = abfd;
7365
7366 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7367
7368 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7369
7370 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7371
7372 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7373 dwo_file->sections.types);
7374
7375 discard_cleanups (cleanups);
7376
7377 return dwo_file;
7378}
7379
7380/* Lookup DWO file DWO_NAME. */
7381
7382static struct dwo_file *
7383lookup_dwo_file (char *dwo_name, const char *comp_dir)
7384{
7385 struct dwo_file *dwo_file;
7386 struct dwo_file find_entry;
7387 void **slot;
7388
7389 if (dwarf2_per_objfile->dwo_files == NULL)
7390 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7391
7392 /* Have we already seen this DWO file? */
7393 find_entry.dwo_name = dwo_name;
7394 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7395
7396 /* If not, read it in and build a table of the DWOs it contains. */
7397 if (*slot == NULL)
7398 *slot = init_dwo_file (dwo_name, comp_dir);
7399
7400 /* NOTE: This will be NULL if unable to open the file. */
7401 dwo_file = *slot;
7402
7403 return dwo_file;
7404}
7405
7406/* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7407 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7408 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7409 nomenclature as TUs).
1c658ad5 7410 The result is a pointer to the dwo_unit object or NULL if we didn't find it
3019eac3
DE
7411 (dwo_id mismatch or couldn't find the DWO file). */
7412
7413static struct dwo_unit *
7414lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7415 char *dwo_name, const char *comp_dir,
7416 ULONGEST signature)
7417{
7418 struct objfile *objfile = dwarf2_per_objfile->objfile;
7419 struct dwo_file *dwo_file;
7420
7421 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7422 if (dwo_file == NULL)
7423 return NULL;
7424
7425 /* Look up the DWO using its signature(dwo_id). */
7426
7427 if (dwo_file->cus != NULL)
7428 {
7429 struct dwo_unit find_dwo_cu, *dwo_cu;
7430
7431 find_dwo_cu.signature = signature;
7432 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
a766d390 7433
3019eac3
DE
7434 if (dwo_cu != NULL)
7435 return dwo_cu;
7436 }
c906108c 7437
3019eac3 7438 /* We didn't find it. This must mean a dwo_id mismatch. */
df8a16a1 7439
3019eac3
DE
7440 complaint (&symfile_complaints,
7441 _("Could not find DWO CU referenced by CU at offset 0x%x"
7442 " [in module %s]"),
7443 this_cu->offset.sect_off, objfile->name);
7444 return NULL;
7445}
c906108c 7446
3019eac3
DE
7447/* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7448 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
1c658ad5 7449 The result is a pointer to the dwo_unit object or NULL if we didn't find it
3019eac3 7450 (dwo_id mismatch or couldn't find the DWO file). */
debd256d 7451
3019eac3
DE
7452static struct dwo_unit *
7453lookup_dwo_type_unit (struct signatured_type *this_tu,
7454 char *dwo_name, const char *comp_dir)
7455{
7456 struct objfile *objfile = dwarf2_per_objfile->objfile;
7457 struct dwo_file *dwo_file;
cb1df416 7458
3019eac3
DE
7459 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7460 if (dwo_file == NULL)
7461 return NULL;
cf2c3c16 7462
3019eac3
DE
7463 /* Look up the DWO using its signature(dwo_id). */
7464
7465 if (dwo_file->tus != NULL)
cf2c3c16 7466 {
3019eac3 7467 struct dwo_unit find_dwo_tu, *dwo_tu;
9a619af0 7468
3019eac3
DE
7469 find_dwo_tu.signature = this_tu->signature;
7470 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7471
7472 if (dwo_tu != NULL)
7473 return dwo_tu;
2e276125 7474 }
9cdd5dbd 7475
3019eac3
DE
7476 /* We didn't find it. This must mean a dwo_id mismatch. */
7477
7478 complaint (&symfile_complaints,
7479 _("Could not find DWO TU referenced by TU at offset 0x%x"
7480 " [in module %s]"),
7481 this_tu->per_cu.offset.sect_off, objfile->name);
7482 return NULL;
5fb290d7
DJ
7483}
7484
3019eac3
DE
7485/* Free all resources associated with DWO_FILE.
7486 Close the DWO file and munmap the sections.
7487 All memory should be on the objfile obstack. */
348e048f
DE
7488
7489static void
3019eac3 7490free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 7491{
3019eac3
DE
7492 int ix;
7493 struct dwarf2_section_info *section;
348e048f 7494
3019eac3
DE
7495 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7496 bfd_close (dwo_file->dwo_bfd);
348e048f 7497
3019eac3
DE
7498 munmap_section_buffer (&dwo_file->sections.abbrev);
7499 munmap_section_buffer (&dwo_file->sections.info);
7500 munmap_section_buffer (&dwo_file->sections.line);
7501 munmap_section_buffer (&dwo_file->sections.loc);
7502 munmap_section_buffer (&dwo_file->sections.str);
7503 munmap_section_buffer (&dwo_file->sections.str_offsets);
348e048f 7504
3019eac3
DE
7505 for (ix = 0;
7506 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7507 ix, section);
7508 ++ix)
7509 munmap_section_buffer (section);
348e048f 7510
3019eac3
DE
7511 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7512}
348e048f 7513
3019eac3 7514/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 7515
3019eac3
DE
7516static void
7517free_dwo_file_cleanup (void *arg)
7518{
7519 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7520 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 7521
3019eac3
DE
7522 free_dwo_file (dwo_file, objfile);
7523}
348e048f 7524
3019eac3 7525/* Traversal function for free_dwo_files. */
2ab95328 7526
3019eac3
DE
7527static int
7528free_dwo_file_from_slot (void **slot, void *info)
7529{
7530 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7531 struct objfile *objfile = (struct objfile *) info;
348e048f 7532
3019eac3 7533 free_dwo_file (dwo_file, objfile);
348e048f 7534
3019eac3
DE
7535 return 1;
7536}
348e048f 7537
3019eac3 7538/* Free all resources associated with DWO_FILES. */
348e048f 7539
3019eac3
DE
7540static void
7541free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7542{
7543 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 7544}
3019eac3
DE
7545\f
7546/* Read in various DIEs. */
348e048f 7547
d389af10
JK
7548/* qsort helper for inherit_abstract_dies. */
7549
7550static int
7551unsigned_int_compar (const void *ap, const void *bp)
7552{
7553 unsigned int a = *(unsigned int *) ap;
7554 unsigned int b = *(unsigned int *) bp;
7555
7556 return (a > b) - (b > a);
7557}
7558
7559/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
7560 Inherit only the children of the DW_AT_abstract_origin DIE not being
7561 already referenced by DW_AT_abstract_origin from the children of the
7562 current DIE. */
d389af10
JK
7563
7564static void
7565inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7566{
7567 struct die_info *child_die;
7568 unsigned die_children_count;
7569 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
7570 sect_offset *offsets;
7571 sect_offset *offsets_end, *offsetp;
d389af10
JK
7572 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7573 struct die_info *origin_die;
7574 /* Iterator of the ORIGIN_DIE children. */
7575 struct die_info *origin_child_die;
7576 struct cleanup *cleanups;
7577 struct attribute *attr;
cd02d79d
PA
7578 struct dwarf2_cu *origin_cu;
7579 struct pending **origin_previous_list_in_scope;
d389af10
JK
7580
7581 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7582 if (!attr)
7583 return;
7584
cd02d79d
PA
7585 /* Note that following die references may follow to a die in a
7586 different cu. */
7587
7588 origin_cu = cu;
7589 origin_die = follow_die_ref (die, attr, &origin_cu);
7590
7591 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7592 symbols in. */
7593 origin_previous_list_in_scope = origin_cu->list_in_scope;
7594 origin_cu->list_in_scope = cu->list_in_scope;
7595
edb3359d
DJ
7596 if (die->tag != origin_die->tag
7597 && !(die->tag == DW_TAG_inlined_subroutine
7598 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7599 complaint (&symfile_complaints,
7600 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 7601 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
7602
7603 child_die = die->child;
7604 die_children_count = 0;
7605 while (child_die && child_die->tag)
7606 {
7607 child_die = sibling_die (child_die);
7608 die_children_count++;
7609 }
7610 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7611 cleanups = make_cleanup (xfree, offsets);
7612
7613 offsets_end = offsets;
7614 child_die = die->child;
7615 while (child_die && child_die->tag)
7616 {
c38f313d
DJ
7617 /* For each CHILD_DIE, find the corresponding child of
7618 ORIGIN_DIE. If there is more than one layer of
7619 DW_AT_abstract_origin, follow them all; there shouldn't be,
7620 but GCC versions at least through 4.4 generate this (GCC PR
7621 40573). */
7622 struct die_info *child_origin_die = child_die;
cd02d79d 7623 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 7624
c38f313d
DJ
7625 while (1)
7626 {
cd02d79d
PA
7627 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7628 child_origin_cu);
c38f313d
DJ
7629 if (attr == NULL)
7630 break;
cd02d79d
PA
7631 child_origin_die = follow_die_ref (child_origin_die, attr,
7632 &child_origin_cu);
c38f313d
DJ
7633 }
7634
d389af10
JK
7635 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7636 counterpart may exist. */
c38f313d 7637 if (child_origin_die != child_die)
d389af10 7638 {
edb3359d
DJ
7639 if (child_die->tag != child_origin_die->tag
7640 && !(child_die->tag == DW_TAG_inlined_subroutine
7641 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7642 complaint (&symfile_complaints,
7643 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7644 "different tags"), child_die->offset.sect_off,
7645 child_origin_die->offset.sect_off);
c38f313d
DJ
7646 if (child_origin_die->parent != origin_die)
7647 complaint (&symfile_complaints,
7648 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7649 "different parents"), child_die->offset.sect_off,
7650 child_origin_die->offset.sect_off);
c38f313d
DJ
7651 else
7652 *offsets_end++ = child_origin_die->offset;
d389af10
JK
7653 }
7654 child_die = sibling_die (child_die);
7655 }
7656 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7657 unsigned_int_compar);
7658 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 7659 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
7660 complaint (&symfile_complaints,
7661 _("Multiple children of DIE 0x%x refer "
7662 "to DIE 0x%x as their abstract origin"),
b64f50a1 7663 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
7664
7665 offsetp = offsets;
7666 origin_child_die = origin_die->child;
7667 while (origin_child_die && origin_child_die->tag)
7668 {
7669 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
7670 while (offsetp < offsets_end
7671 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 7672 offsetp++;
b64f50a1
JK
7673 if (offsetp >= offsets_end
7674 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
7675 {
7676 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 7677 process_die (origin_child_die, origin_cu);
d389af10
JK
7678 }
7679 origin_child_die = sibling_die (origin_child_die);
7680 }
cd02d79d 7681 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
7682
7683 do_cleanups (cleanups);
7684}
7685
c906108c 7686static void
e7c27a73 7687read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7688{
e7c27a73 7689 struct objfile *objfile = cu->objfile;
52f0bd74 7690 struct context_stack *new;
c906108c
SS
7691 CORE_ADDR lowpc;
7692 CORE_ADDR highpc;
7693 struct die_info *child_die;
edb3359d 7694 struct attribute *attr, *call_line, *call_file;
c906108c 7695 char *name;
e142c38c 7696 CORE_ADDR baseaddr;
801e3a5b 7697 struct block *block;
edb3359d 7698 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
7699 VEC (symbolp) *template_args = NULL;
7700 struct template_symbol *templ_func = NULL;
edb3359d
DJ
7701
7702 if (inlined_func)
7703 {
7704 /* If we do not have call site information, we can't show the
7705 caller of this inlined function. That's too confusing, so
7706 only use the scope for local variables. */
7707 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7708 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7709 if (call_line == NULL || call_file == NULL)
7710 {
7711 read_lexical_block_scope (die, cu);
7712 return;
7713 }
7714 }
c906108c 7715
e142c38c
DJ
7716 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7717
94af9270 7718 name = dwarf2_name (die, cu);
c906108c 7719
e8d05480
JB
7720 /* Ignore functions with missing or empty names. These are actually
7721 illegal according to the DWARF standard. */
7722 if (name == NULL)
7723 {
7724 complaint (&symfile_complaints,
b64f50a1
JK
7725 _("missing name for subprogram DIE at %d"),
7726 die->offset.sect_off);
e8d05480
JB
7727 return;
7728 }
7729
7730 /* Ignore functions with missing or invalid low and high pc attributes. */
7731 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7732 {
ae4d0c03
PM
7733 attr = dwarf2_attr (die, DW_AT_external, cu);
7734 if (!attr || !DW_UNSND (attr))
7735 complaint (&symfile_complaints,
3e43a32a
MS
7736 _("cannot get low and high bounds "
7737 "for subprogram DIE at %d"),
b64f50a1 7738 die->offset.sect_off);
e8d05480
JB
7739 return;
7740 }
c906108c
SS
7741
7742 lowpc += baseaddr;
7743 highpc += baseaddr;
7744
34eaf542
TT
7745 /* If we have any template arguments, then we must allocate a
7746 different sort of symbol. */
7747 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7748 {
7749 if (child_die->tag == DW_TAG_template_type_param
7750 || child_die->tag == DW_TAG_template_value_param)
7751 {
7752 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7753 struct template_symbol);
7754 templ_func->base.is_cplus_template_function = 1;
7755 break;
7756 }
7757 }
7758
c906108c 7759 new = push_context (0, lowpc);
34eaf542
TT
7760 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7761 (struct symbol *) templ_func);
4c2df51b 7762
4cecd739
DJ
7763 /* If there is a location expression for DW_AT_frame_base, record
7764 it. */
e142c38c 7765 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 7766 if (attr)
c034e007
AC
7767 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7768 expression is being recorded directly in the function's symbol
7769 and not in a separate frame-base object. I guess this hack is
7770 to avoid adding some sort of frame-base adjunct/annex to the
7771 function's symbol :-(. The problem with doing this is that it
7772 results in a function symbol with a location expression that
7773 has nothing to do with the location of the function, ouch! The
7774 relationship should be: a function's symbol has-a frame base; a
7775 frame-base has-a location expression. */
e7c27a73 7776 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 7777
e142c38c 7778 cu->list_in_scope = &local_symbols;
c906108c 7779
639d11d3 7780 if (die->child != NULL)
c906108c 7781 {
639d11d3 7782 child_die = die->child;
c906108c
SS
7783 while (child_die && child_die->tag)
7784 {
34eaf542
TT
7785 if (child_die->tag == DW_TAG_template_type_param
7786 || child_die->tag == DW_TAG_template_value_param)
7787 {
7788 struct symbol *arg = new_symbol (child_die, NULL, cu);
7789
f1078f66
DJ
7790 if (arg != NULL)
7791 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7792 }
7793 else
7794 process_die (child_die, cu);
c906108c
SS
7795 child_die = sibling_die (child_die);
7796 }
7797 }
7798
d389af10
JK
7799 inherit_abstract_dies (die, cu);
7800
4a811a97
UW
7801 /* If we have a DW_AT_specification, we might need to import using
7802 directives from the context of the specification DIE. See the
7803 comment in determine_prefix. */
7804 if (cu->language == language_cplus
7805 && dwarf2_attr (die, DW_AT_specification, cu))
7806 {
7807 struct dwarf2_cu *spec_cu = cu;
7808 struct die_info *spec_die = die_specification (die, &spec_cu);
7809
7810 while (spec_die)
7811 {
7812 child_die = spec_die->child;
7813 while (child_die && child_die->tag)
7814 {
7815 if (child_die->tag == DW_TAG_imported_module)
7816 process_die (child_die, spec_cu);
7817 child_die = sibling_die (child_die);
7818 }
7819
7820 /* In some cases, GCC generates specification DIEs that
7821 themselves contain DW_AT_specification attributes. */
7822 spec_die = die_specification (spec_die, &spec_cu);
7823 }
7824 }
7825
c906108c
SS
7826 new = pop_context ();
7827 /* Make a block for the local symbols within. */
801e3a5b
JB
7828 block = finish_block (new->name, &local_symbols, new->old_blocks,
7829 lowpc, highpc, objfile);
7830
df8a16a1 7831 /* For C++, set the block's scope. */
f55ee35c 7832 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 7833 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 7834 determine_prefix (die, cu),
df8a16a1
DJ
7835 processing_has_namespace_info);
7836
801e3a5b
JB
7837 /* If we have address ranges, record them. */
7838 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 7839
34eaf542
TT
7840 /* Attach template arguments to function. */
7841 if (! VEC_empty (symbolp, template_args))
7842 {
7843 gdb_assert (templ_func != NULL);
7844
7845 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7846 templ_func->template_arguments
7847 = obstack_alloc (&objfile->objfile_obstack,
7848 (templ_func->n_template_arguments
7849 * sizeof (struct symbol *)));
7850 memcpy (templ_func->template_arguments,
7851 VEC_address (symbolp, template_args),
7852 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7853 VEC_free (symbolp, template_args);
7854 }
7855
208d8187
JB
7856 /* In C++, we can have functions nested inside functions (e.g., when
7857 a function declares a class that has methods). This means that
7858 when we finish processing a function scope, we may need to go
7859 back to building a containing block's symbol lists. */
7860 local_symbols = new->locals;
7861 param_symbols = new->params;
27aa8d6a 7862 using_directives = new->using_directives;
208d8187 7863
921e78cf
JB
7864 /* If we've finished processing a top-level function, subsequent
7865 symbols go in the file symbol list. */
7866 if (outermost_context_p ())
e142c38c 7867 cu->list_in_scope = &file_symbols;
c906108c
SS
7868}
7869
7870/* Process all the DIES contained within a lexical block scope. Start
7871 a new scope, process the dies, and then close the scope. */
7872
7873static void
e7c27a73 7874read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7875{
e7c27a73 7876 struct objfile *objfile = cu->objfile;
52f0bd74 7877 struct context_stack *new;
c906108c
SS
7878 CORE_ADDR lowpc, highpc;
7879 struct die_info *child_die;
e142c38c
DJ
7880 CORE_ADDR baseaddr;
7881
7882 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
7883
7884 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
7885 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7886 as multiple lexical blocks? Handling children in a sane way would
6e70227d 7887 be nasty. Might be easier to properly extend generic blocks to
af34e669 7888 describe ranges. */
d85a05f0 7889 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
7890 return;
7891 lowpc += baseaddr;
7892 highpc += baseaddr;
7893
7894 push_context (0, lowpc);
639d11d3 7895 if (die->child != NULL)
c906108c 7896 {
639d11d3 7897 child_die = die->child;
c906108c
SS
7898 while (child_die && child_die->tag)
7899 {
e7c27a73 7900 process_die (child_die, cu);
c906108c
SS
7901 child_die = sibling_die (child_die);
7902 }
7903 }
7904 new = pop_context ();
7905
8540c487 7906 if (local_symbols != NULL || using_directives != NULL)
c906108c 7907 {
801e3a5b
JB
7908 struct block *block
7909 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7910 highpc, objfile);
7911
7912 /* Note that recording ranges after traversing children, as we
7913 do here, means that recording a parent's ranges entails
7914 walking across all its children's ranges as they appear in
7915 the address map, which is quadratic behavior.
7916
7917 It would be nicer to record the parent's ranges before
7918 traversing its children, simply overriding whatever you find
7919 there. But since we don't even decide whether to create a
7920 block until after we've traversed its children, that's hard
7921 to do. */
7922 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
7923 }
7924 local_symbols = new->locals;
27aa8d6a 7925 using_directives = new->using_directives;
c906108c
SS
7926}
7927
96408a79
SA
7928/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7929
7930static void
7931read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7932{
7933 struct objfile *objfile = cu->objfile;
7934 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7935 CORE_ADDR pc, baseaddr;
7936 struct attribute *attr;
7937 struct call_site *call_site, call_site_local;
7938 void **slot;
7939 int nparams;
7940 struct die_info *child_die;
7941
7942 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7943
7944 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7945 if (!attr)
7946 {
7947 complaint (&symfile_complaints,
7948 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7949 "DIE 0x%x [in module %s]"),
b64f50a1 7950 die->offset.sect_off, objfile->name);
96408a79
SA
7951 return;
7952 }
7953 pc = DW_ADDR (attr) + baseaddr;
7954
7955 if (cu->call_site_htab == NULL)
7956 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7957 NULL, &objfile->objfile_obstack,
7958 hashtab_obstack_allocate, NULL);
7959 call_site_local.pc = pc;
7960 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7961 if (*slot != NULL)
7962 {
7963 complaint (&symfile_complaints,
7964 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7965 "DIE 0x%x [in module %s]"),
b64f50a1 7966 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
7967 return;
7968 }
7969
7970 /* Count parameters at the caller. */
7971
7972 nparams = 0;
7973 for (child_die = die->child; child_die && child_die->tag;
7974 child_die = sibling_die (child_die))
7975 {
7976 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7977 {
7978 complaint (&symfile_complaints,
7979 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7980 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7981 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
7982 continue;
7983 }
7984
7985 nparams++;
7986 }
7987
7988 call_site = obstack_alloc (&objfile->objfile_obstack,
7989 (sizeof (*call_site)
7990 + (sizeof (*call_site->parameter)
7991 * (nparams - 1))));
7992 *slot = call_site;
7993 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7994 call_site->pc = pc;
7995
7996 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7997 {
7998 struct die_info *func_die;
7999
8000 /* Skip also over DW_TAG_inlined_subroutine. */
8001 for (func_die = die->parent;
8002 func_die && func_die->tag != DW_TAG_subprogram
8003 && func_die->tag != DW_TAG_subroutine_type;
8004 func_die = func_die->parent);
8005
8006 /* DW_AT_GNU_all_call_sites is a superset
8007 of DW_AT_GNU_all_tail_call_sites. */
8008 if (func_die
8009 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
8010 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
8011 {
8012 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
8013 not complete. But keep CALL_SITE for look ups via call_site_htab,
8014 both the initial caller containing the real return address PC and
8015 the final callee containing the current PC of a chain of tail
8016 calls do not need to have the tail call list complete. But any
8017 function candidate for a virtual tail call frame searched via
8018 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
8019 determined unambiguously. */
8020 }
8021 else
8022 {
8023 struct type *func_type = NULL;
8024
8025 if (func_die)
8026 func_type = get_die_type (func_die, cu);
8027 if (func_type != NULL)
8028 {
8029 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
8030
8031 /* Enlist this call site to the function. */
8032 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
8033 TYPE_TAIL_CALL_LIST (func_type) = call_site;
8034 }
8035 else
8036 complaint (&symfile_complaints,
8037 _("Cannot find function owning DW_TAG_GNU_call_site "
8038 "DIE 0x%x [in module %s]"),
b64f50a1 8039 die->offset.sect_off, objfile->name);
96408a79
SA
8040 }
8041 }
8042
8043 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
8044 if (attr == NULL)
8045 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
8046 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
8047 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
8048 /* Keep NULL DWARF_BLOCK. */;
8049 else if (attr_form_is_block (attr))
8050 {
8051 struct dwarf2_locexpr_baton *dlbaton;
8052
8053 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
8054 dlbaton->data = DW_BLOCK (attr)->data;
8055 dlbaton->size = DW_BLOCK (attr)->size;
8056 dlbaton->per_cu = cu->per_cu;
8057
8058 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
8059 }
8060 else if (is_ref_attr (attr))
8061 {
96408a79
SA
8062 struct dwarf2_cu *target_cu = cu;
8063 struct die_info *target_die;
8064
8065 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
8066 gdb_assert (target_cu->objfile == objfile);
8067 if (die_is_declaration (target_die, target_cu))
8068 {
8069 const char *target_physname;
8070
8071 target_physname = dwarf2_physname (NULL, target_die, target_cu);
8072 if (target_physname == NULL)
8073 complaint (&symfile_complaints,
8074 _("DW_AT_GNU_call_site_target target DIE has invalid "
8075 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 8076 die->offset.sect_off, objfile->name);
96408a79
SA
8077 else
8078 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
8079 }
8080 else
8081 {
8082 CORE_ADDR lowpc;
8083
8084 /* DW_AT_entry_pc should be preferred. */
8085 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
8086 complaint (&symfile_complaints,
8087 _("DW_AT_GNU_call_site_target target DIE has invalid "
8088 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 8089 die->offset.sect_off, objfile->name);
96408a79
SA
8090 else
8091 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
8092 }
8093 }
8094 else
8095 complaint (&symfile_complaints,
8096 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
8097 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 8098 die->offset.sect_off, objfile->name);
96408a79
SA
8099
8100 call_site->per_cu = cu->per_cu;
8101
8102 for (child_die = die->child;
8103 child_die && child_die->tag;
8104 child_die = sibling_die (child_die))
8105 {
96408a79 8106 struct call_site_parameter *parameter;
1788b2d3 8107 struct attribute *loc, *origin;
96408a79
SA
8108
8109 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
8110 {
8111 /* Already printed the complaint above. */
8112 continue;
8113 }
8114
8115 gdb_assert (call_site->parameter_count < nparams);
8116 parameter = &call_site->parameter[call_site->parameter_count];
8117
1788b2d3
JK
8118 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
8119 specifies DW_TAG_formal_parameter. Value of the data assumed for the
8120 register is contained in DW_AT_GNU_call_site_value. */
96408a79 8121
24c5c679 8122 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
8123 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
8124 if (loc == NULL && origin != NULL && is_ref_attr (origin))
8125 {
8126 sect_offset offset;
8127
8128 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
8129 offset = dwarf2_get_ref_die_offset (origin);
8130 gdb_assert (offset.sect_off >= cu->header.offset.sect_off);
8131 parameter->u.param_offset.cu_off = (offset.sect_off
8132 - cu->header.offset.sect_off);
8133 }
8134 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
8135 {
8136 complaint (&symfile_complaints,
8137 _("No DW_FORM_block* DW_AT_location for "
8138 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 8139 child_die->offset.sect_off, objfile->name);
96408a79
SA
8140 continue;
8141 }
24c5c679 8142 else
96408a79 8143 {
24c5c679
JK
8144 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
8145 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
8146 if (parameter->u.dwarf_reg != -1)
8147 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
8148 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
8149 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
8150 &parameter->u.fb_offset))
8151 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
8152 else
8153 {
8154 complaint (&symfile_complaints,
8155 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
8156 "for DW_FORM_block* DW_AT_location is supported for "
8157 "DW_TAG_GNU_call_site child DIE 0x%x "
8158 "[in module %s]"),
8159 child_die->offset.sect_off, objfile->name);
8160 continue;
8161 }
96408a79
SA
8162 }
8163
8164 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
8165 if (!attr_form_is_block (attr))
8166 {
8167 complaint (&symfile_complaints,
8168 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
8169 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 8170 child_die->offset.sect_off, objfile->name);
96408a79
SA
8171 continue;
8172 }
8173 parameter->value = DW_BLOCK (attr)->data;
8174 parameter->value_size = DW_BLOCK (attr)->size;
8175
8176 /* Parameters are not pre-cleared by memset above. */
8177 parameter->data_value = NULL;
8178 parameter->data_value_size = 0;
8179 call_site->parameter_count++;
8180
8181 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
8182 if (attr)
8183 {
8184 if (!attr_form_is_block (attr))
8185 complaint (&symfile_complaints,
8186 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
8187 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 8188 child_die->offset.sect_off, objfile->name);
96408a79
SA
8189 else
8190 {
8191 parameter->data_value = DW_BLOCK (attr)->data;
8192 parameter->data_value_size = DW_BLOCK (attr)->size;
8193 }
8194 }
8195 }
8196}
8197
43039443 8198/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
8199 Return 1 if the attributes are present and valid, otherwise, return 0.
8200 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
8201
8202static int
8203dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
8204 CORE_ADDR *high_return, struct dwarf2_cu *cu,
8205 struct partial_symtab *ranges_pst)
43039443
JK
8206{
8207 struct objfile *objfile = cu->objfile;
8208 struct comp_unit_head *cu_header = &cu->header;
8209 bfd *obfd = objfile->obfd;
8210 unsigned int addr_size = cu_header->addr_size;
8211 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8212 /* Base address selection entry. */
8213 CORE_ADDR base;
8214 int found_base;
8215 unsigned int dummy;
8216 gdb_byte *buffer;
8217 CORE_ADDR marker;
8218 int low_set;
8219 CORE_ADDR low = 0;
8220 CORE_ADDR high = 0;
ff013f42 8221 CORE_ADDR baseaddr;
43039443 8222
d00adf39
DE
8223 found_base = cu->base_known;
8224 base = cu->base_address;
43039443 8225
be391dca 8226 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 8227 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
8228 {
8229 complaint (&symfile_complaints,
8230 _("Offset %d out of bounds for DW_AT_ranges attribute"),
8231 offset);
8232 return 0;
8233 }
dce234bc 8234 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
8235
8236 /* Read in the largest possible address. */
8237 marker = read_address (obfd, buffer, cu, &dummy);
8238 if ((marker & mask) == mask)
8239 {
8240 /* If we found the largest possible address, then
8241 read the base address. */
8242 base = read_address (obfd, buffer + addr_size, cu, &dummy);
8243 buffer += 2 * addr_size;
8244 offset += 2 * addr_size;
8245 found_base = 1;
8246 }
8247
8248 low_set = 0;
8249
e7030f15 8250 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 8251
43039443
JK
8252 while (1)
8253 {
8254 CORE_ADDR range_beginning, range_end;
8255
8256 range_beginning = read_address (obfd, buffer, cu, &dummy);
8257 buffer += addr_size;
8258 range_end = read_address (obfd, buffer, cu, &dummy);
8259 buffer += addr_size;
8260 offset += 2 * addr_size;
8261
8262 /* An end of list marker is a pair of zero addresses. */
8263 if (range_beginning == 0 && range_end == 0)
8264 /* Found the end of list entry. */
8265 break;
8266
8267 /* Each base address selection entry is a pair of 2 values.
8268 The first is the largest possible address, the second is
8269 the base address. Check for a base address here. */
8270 if ((range_beginning & mask) == mask)
8271 {
8272 /* If we found the largest possible address, then
8273 read the base address. */
8274 base = read_address (obfd, buffer + addr_size, cu, &dummy);
8275 found_base = 1;
8276 continue;
8277 }
8278
8279 if (!found_base)
8280 {
8281 /* We have no valid base address for the ranges
8282 data. */
8283 complaint (&symfile_complaints,
8284 _("Invalid .debug_ranges data (no base address)"));
8285 return 0;
8286 }
8287
9277c30c
UW
8288 if (range_beginning > range_end)
8289 {
8290 /* Inverted range entries are invalid. */
8291 complaint (&symfile_complaints,
8292 _("Invalid .debug_ranges data (inverted range)"));
8293 return 0;
8294 }
8295
8296 /* Empty range entries have no effect. */
8297 if (range_beginning == range_end)
8298 continue;
8299
43039443
JK
8300 range_beginning += base;
8301 range_end += base;
8302
9277c30c 8303 if (ranges_pst != NULL)
ff013f42 8304 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
8305 range_beginning + baseaddr,
8306 range_end - 1 + baseaddr,
ff013f42
JK
8307 ranges_pst);
8308
43039443
JK
8309 /* FIXME: This is recording everything as a low-high
8310 segment of consecutive addresses. We should have a
8311 data structure for discontiguous block ranges
8312 instead. */
8313 if (! low_set)
8314 {
8315 low = range_beginning;
8316 high = range_end;
8317 low_set = 1;
8318 }
8319 else
8320 {
8321 if (range_beginning < low)
8322 low = range_beginning;
8323 if (range_end > high)
8324 high = range_end;
8325 }
8326 }
8327
8328 if (! low_set)
8329 /* If the first entry is an end-of-list marker, the range
8330 describes an empty scope, i.e. no instructions. */
8331 return 0;
8332
8333 if (low_return)
8334 *low_return = low;
8335 if (high_return)
8336 *high_return = high;
8337 return 1;
8338}
8339
af34e669
DJ
8340/* Get low and high pc attributes from a die. Return 1 if the attributes
8341 are present and valid, otherwise, return 0. Return -1 if the range is
8342 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 8343
c906108c 8344static int
af34e669 8345dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
8346 CORE_ADDR *highpc, struct dwarf2_cu *cu,
8347 struct partial_symtab *pst)
c906108c
SS
8348{
8349 struct attribute *attr;
91da1414 8350 struct attribute *attr_high;
af34e669
DJ
8351 CORE_ADDR low = 0;
8352 CORE_ADDR high = 0;
8353 int ret = 0;
c906108c 8354
91da1414
MW
8355 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8356 if (attr_high)
af34e669 8357 {
e142c38c 8358 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 8359 if (attr)
91da1414
MW
8360 {
8361 low = DW_ADDR (attr);
3019eac3
DE
8362 if (attr_high->form == DW_FORM_addr
8363 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8364 high = DW_ADDR (attr_high);
8365 else
8366 high = low + DW_UNSND (attr_high);
8367 }
af34e669
DJ
8368 else
8369 /* Found high w/o low attribute. */
8370 return 0;
8371
8372 /* Found consecutive range of addresses. */
8373 ret = 1;
8374 }
c906108c 8375 else
af34e669 8376 {
e142c38c 8377 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
8378 if (attr != NULL)
8379 {
2e3cf129
DE
8380 unsigned int ranges_offset = DW_UNSND (attr) + cu->ranges_base;
8381
af34e669 8382 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 8383 .debug_ranges section. */
2e3cf129 8384 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
af34e669 8385 return 0;
43039443 8386 /* Found discontinuous range of addresses. */
af34e669
DJ
8387 ret = -1;
8388 }
8389 }
c906108c 8390
9373cf26
JK
8391 /* read_partial_die has also the strict LOW < HIGH requirement. */
8392 if (high <= low)
c906108c
SS
8393 return 0;
8394
8395 /* When using the GNU linker, .gnu.linkonce. sections are used to
8396 eliminate duplicate copies of functions and vtables and such.
8397 The linker will arbitrarily choose one and discard the others.
8398 The AT_*_pc values for such functions refer to local labels in
8399 these sections. If the section from that file was discarded, the
8400 labels are not in the output, so the relocs get a value of 0.
8401 If this is a discarded function, mark the pc bounds as invalid,
8402 so that GDB will ignore it. */
72dca2f5 8403 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
8404 return 0;
8405
8406 *lowpc = low;
96408a79
SA
8407 if (highpc)
8408 *highpc = high;
af34e669 8409 return ret;
c906108c
SS
8410}
8411
b084d499
JB
8412/* Assuming that DIE represents a subprogram DIE or a lexical block, get
8413 its low and high PC addresses. Do nothing if these addresses could not
8414 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8415 and HIGHPC to the high address if greater than HIGHPC. */
8416
8417static void
8418dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8419 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8420 struct dwarf2_cu *cu)
8421{
8422 CORE_ADDR low, high;
8423 struct die_info *child = die->child;
8424
d85a05f0 8425 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
8426 {
8427 *lowpc = min (*lowpc, low);
8428 *highpc = max (*highpc, high);
8429 }
8430
8431 /* If the language does not allow nested subprograms (either inside
8432 subprograms or lexical blocks), we're done. */
8433 if (cu->language != language_ada)
8434 return;
6e70227d 8435
b084d499
JB
8436 /* Check all the children of the given DIE. If it contains nested
8437 subprograms, then check their pc bounds. Likewise, we need to
8438 check lexical blocks as well, as they may also contain subprogram
8439 definitions. */
8440 while (child && child->tag)
8441 {
8442 if (child->tag == DW_TAG_subprogram
8443 || child->tag == DW_TAG_lexical_block)
8444 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8445 child = sibling_die (child);
8446 }
8447}
8448
fae299cd
DC
8449/* Get the low and high pc's represented by the scope DIE, and store
8450 them in *LOWPC and *HIGHPC. If the correct values can't be
8451 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8452
8453static void
8454get_scope_pc_bounds (struct die_info *die,
8455 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8456 struct dwarf2_cu *cu)
8457{
8458 CORE_ADDR best_low = (CORE_ADDR) -1;
8459 CORE_ADDR best_high = (CORE_ADDR) 0;
8460 CORE_ADDR current_low, current_high;
8461
d85a05f0 8462 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
8463 {
8464 best_low = current_low;
8465 best_high = current_high;
8466 }
8467 else
8468 {
8469 struct die_info *child = die->child;
8470
8471 while (child && child->tag)
8472 {
8473 switch (child->tag) {
8474 case DW_TAG_subprogram:
b084d499 8475 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
8476 break;
8477 case DW_TAG_namespace:
f55ee35c 8478 case DW_TAG_module:
fae299cd
DC
8479 /* FIXME: carlton/2004-01-16: Should we do this for
8480 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8481 that current GCC's always emit the DIEs corresponding
8482 to definitions of methods of classes as children of a
8483 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8484 the DIEs giving the declarations, which could be
8485 anywhere). But I don't see any reason why the
8486 standards says that they have to be there. */
8487 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8488
8489 if (current_low != ((CORE_ADDR) -1))
8490 {
8491 best_low = min (best_low, current_low);
8492 best_high = max (best_high, current_high);
8493 }
8494 break;
8495 default:
0963b4bd 8496 /* Ignore. */
fae299cd
DC
8497 break;
8498 }
8499
8500 child = sibling_die (child);
8501 }
8502 }
8503
8504 *lowpc = best_low;
8505 *highpc = best_high;
8506}
8507
801e3a5b
JB
8508/* Record the address ranges for BLOCK, offset by BASEADDR, as given
8509 in DIE. */
380bca97 8510
801e3a5b
JB
8511static void
8512dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8513 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8514{
bb5ed363 8515 struct objfile *objfile = cu->objfile;
801e3a5b 8516 struct attribute *attr;
91da1414 8517 struct attribute *attr_high;
801e3a5b 8518
91da1414
MW
8519 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8520 if (attr_high)
801e3a5b 8521 {
801e3a5b
JB
8522 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8523 if (attr)
8524 {
8525 CORE_ADDR low = DW_ADDR (attr);
91da1414 8526 CORE_ADDR high;
3019eac3
DE
8527 if (attr_high->form == DW_FORM_addr
8528 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8529 high = DW_ADDR (attr_high);
8530 else
8531 high = low + DW_UNSND (attr_high);
9a619af0 8532
801e3a5b
JB
8533 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8534 }
8535 }
8536
8537 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8538 if (attr)
8539 {
bb5ed363 8540 bfd *obfd = objfile->obfd;
801e3a5b
JB
8541
8542 /* The value of the DW_AT_ranges attribute is the offset of the
8543 address range list in the .debug_ranges section. */
2e3cf129 8544 unsigned long offset = DW_UNSND (attr) + cu->ranges_base;
dce234bc 8545 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
8546
8547 /* For some target architectures, but not others, the
8548 read_address function sign-extends the addresses it returns.
8549 To recognize base address selection entries, we need a
8550 mask. */
8551 unsigned int addr_size = cu->header.addr_size;
8552 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8553
8554 /* The base address, to which the next pair is relative. Note
8555 that this 'base' is a DWARF concept: most entries in a range
8556 list are relative, to reduce the number of relocs against the
8557 debugging information. This is separate from this function's
8558 'baseaddr' argument, which GDB uses to relocate debugging
8559 information from a shared library based on the address at
8560 which the library was loaded. */
d00adf39
DE
8561 CORE_ADDR base = cu->base_address;
8562 int base_known = cu->base_known;
801e3a5b 8563
be391dca 8564 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 8565 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
8566 {
8567 complaint (&symfile_complaints,
8568 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8569 offset);
8570 return;
8571 }
8572
8573 for (;;)
8574 {
8575 unsigned int bytes_read;
8576 CORE_ADDR start, end;
8577
8578 start = read_address (obfd, buffer, cu, &bytes_read);
8579 buffer += bytes_read;
8580 end = read_address (obfd, buffer, cu, &bytes_read);
8581 buffer += bytes_read;
8582
8583 /* Did we find the end of the range list? */
8584 if (start == 0 && end == 0)
8585 break;
8586
8587 /* Did we find a base address selection entry? */
8588 else if ((start & base_select_mask) == base_select_mask)
8589 {
8590 base = end;
8591 base_known = 1;
8592 }
8593
8594 /* We found an ordinary address range. */
8595 else
8596 {
8597 if (!base_known)
8598 {
8599 complaint (&symfile_complaints,
3e43a32a
MS
8600 _("Invalid .debug_ranges data "
8601 "(no base address)"));
801e3a5b
JB
8602 return;
8603 }
8604
9277c30c
UW
8605 if (start > end)
8606 {
8607 /* Inverted range entries are invalid. */
8608 complaint (&symfile_complaints,
8609 _("Invalid .debug_ranges data "
8610 "(inverted range)"));
8611 return;
8612 }
8613
8614 /* Empty range entries have no effect. */
8615 if (start == end)
8616 continue;
8617
6e70227d
DE
8618 record_block_range (block,
8619 baseaddr + base + start,
801e3a5b
JB
8620 baseaddr + base + end - 1);
8621 }
8622 }
8623 }
8624}
8625
685b1105
JK
8626/* Check whether the producer field indicates either of GCC < 4.6, or the
8627 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 8628
685b1105
JK
8629static void
8630check_producer (struct dwarf2_cu *cu)
60d5a603
JK
8631{
8632 const char *cs;
8633 int major, minor, release;
8634
8635 if (cu->producer == NULL)
8636 {
8637 /* For unknown compilers expect their behavior is DWARF version
8638 compliant.
8639
8640 GCC started to support .debug_types sections by -gdwarf-4 since
8641 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8642 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8643 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8644 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 8645 }
685b1105 8646 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 8647 {
685b1105
JK
8648 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8649
ba919b58
TT
8650 cs = &cu->producer[strlen ("GNU ")];
8651 while (*cs && !isdigit (*cs))
8652 cs++;
8653 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8654 {
8655 /* Not recognized as GCC. */
8656 }
8657 else
685b1105
JK
8658 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
8659 }
8660 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
8661 cu->producer_is_icc = 1;
8662 else
8663 {
8664 /* For other non-GCC compilers, expect their behavior is DWARF version
8665 compliant. */
60d5a603
JK
8666 }
8667
ba919b58 8668 cu->checked_producer = 1;
685b1105 8669}
ba919b58 8670
685b1105
JK
8671/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8672 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8673 during 4.6.0 experimental. */
8674
8675static int
8676producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8677{
8678 if (!cu->checked_producer)
8679 check_producer (cu);
8680
8681 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
8682}
8683
8684/* Return the default accessibility type if it is not overriden by
8685 DW_AT_accessibility. */
8686
8687static enum dwarf_access_attribute
8688dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8689{
8690 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8691 {
8692 /* The default DWARF 2 accessibility for members is public, the default
8693 accessibility for inheritance is private. */
8694
8695 if (die->tag != DW_TAG_inheritance)
8696 return DW_ACCESS_public;
8697 else
8698 return DW_ACCESS_private;
8699 }
8700 else
8701 {
8702 /* DWARF 3+ defines the default accessibility a different way. The same
8703 rules apply now for DW_TAG_inheritance as for the members and it only
8704 depends on the container kind. */
8705
8706 if (die->parent->tag == DW_TAG_class_type)
8707 return DW_ACCESS_private;
8708 else
8709 return DW_ACCESS_public;
8710 }
8711}
8712
74ac6d43
TT
8713/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8714 offset. If the attribute was not found return 0, otherwise return
8715 1. If it was found but could not properly be handled, set *OFFSET
8716 to 0. */
8717
8718static int
8719handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8720 LONGEST *offset)
8721{
8722 struct attribute *attr;
8723
8724 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8725 if (attr != NULL)
8726 {
8727 *offset = 0;
8728
8729 /* Note that we do not check for a section offset first here.
8730 This is because DW_AT_data_member_location is new in DWARF 4,
8731 so if we see it, we can assume that a constant form is really
8732 a constant and not a section offset. */
8733 if (attr_form_is_constant (attr))
8734 *offset = dwarf2_get_attr_constant_value (attr, 0);
8735 else if (attr_form_is_section_offset (attr))
8736 dwarf2_complex_location_expr_complaint ();
8737 else if (attr_form_is_block (attr))
8738 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8739 else
8740 dwarf2_complex_location_expr_complaint ();
8741
8742 return 1;
8743 }
8744
8745 return 0;
8746}
8747
c906108c
SS
8748/* Add an aggregate field to the field list. */
8749
8750static void
107d2387 8751dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 8752 struct dwarf2_cu *cu)
6e70227d 8753{
e7c27a73 8754 struct objfile *objfile = cu->objfile;
5e2b427d 8755 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8756 struct nextfield *new_field;
8757 struct attribute *attr;
8758 struct field *fp;
8759 char *fieldname = "";
8760
8761 /* Allocate a new field list entry and link it in. */
8762 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 8763 make_cleanup (xfree, new_field);
c906108c 8764 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
8765
8766 if (die->tag == DW_TAG_inheritance)
8767 {
8768 new_field->next = fip->baseclasses;
8769 fip->baseclasses = new_field;
8770 }
8771 else
8772 {
8773 new_field->next = fip->fields;
8774 fip->fields = new_field;
8775 }
c906108c
SS
8776 fip->nfields++;
8777
e142c38c 8778 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
8779 if (attr)
8780 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
8781 else
8782 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
8783 if (new_field->accessibility != DW_ACCESS_public)
8784 fip->non_public_fields = 1;
60d5a603 8785
e142c38c 8786 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
8787 if (attr)
8788 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
8789 else
8790 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
8791
8792 fp = &new_field->field;
a9a9bd0f 8793
e142c38c 8794 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 8795 {
74ac6d43
TT
8796 LONGEST offset;
8797
a9a9bd0f 8798 /* Data member other than a C++ static data member. */
6e70227d 8799
c906108c 8800 /* Get type of field. */
e7c27a73 8801 fp->type = die_type (die, cu);
c906108c 8802
d6a843b5 8803 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 8804
c906108c 8805 /* Get bit size of field (zero if none). */
e142c38c 8806 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
8807 if (attr)
8808 {
8809 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8810 }
8811 else
8812 {
8813 FIELD_BITSIZE (*fp) = 0;
8814 }
8815
8816 /* Get bit offset of field. */
74ac6d43
TT
8817 if (handle_data_member_location (die, cu, &offset))
8818 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 8819 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
8820 if (attr)
8821 {
5e2b427d 8822 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
8823 {
8824 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
8825 additional bit offset from the MSB of the containing
8826 anonymous object to the MSB of the field. We don't
8827 have to do anything special since we don't need to
8828 know the size of the anonymous object. */
f41f5e61 8829 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
8830 }
8831 else
8832 {
8833 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
8834 MSB of the anonymous object, subtract off the number of
8835 bits from the MSB of the field to the MSB of the
8836 object, and then subtract off the number of bits of
8837 the field itself. The result is the bit offset of
8838 the LSB of the field. */
c906108c
SS
8839 int anonymous_size;
8840 int bit_offset = DW_UNSND (attr);
8841
e142c38c 8842 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8843 if (attr)
8844 {
8845 /* The size of the anonymous object containing
8846 the bit field is explicit, so use the
8847 indicated size (in bytes). */
8848 anonymous_size = DW_UNSND (attr);
8849 }
8850 else
8851 {
8852 /* The size of the anonymous object containing
8853 the bit field must be inferred from the type
8854 attribute of the data member containing the
8855 bit field. */
8856 anonymous_size = TYPE_LENGTH (fp->type);
8857 }
f41f5e61
PA
8858 SET_FIELD_BITPOS (*fp,
8859 (FIELD_BITPOS (*fp)
8860 + anonymous_size * bits_per_byte
8861 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
8862 }
8863 }
8864
8865 /* Get name of field. */
39cbfefa
DJ
8866 fieldname = dwarf2_name (die, cu);
8867 if (fieldname == NULL)
8868 fieldname = "";
d8151005
DJ
8869
8870 /* The name is already allocated along with this objfile, so we don't
8871 need to duplicate it for the type. */
8872 fp->name = fieldname;
c906108c
SS
8873
8874 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 8875 pointer or virtual base class pointer) to private. */
e142c38c 8876 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 8877 {
d48cc9dd 8878 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
8879 new_field->accessibility = DW_ACCESS_private;
8880 fip->non_public_fields = 1;
8881 }
8882 }
a9a9bd0f 8883 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 8884 {
a9a9bd0f
DC
8885 /* C++ static member. */
8886
8887 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8888 is a declaration, but all versions of G++ as of this writing
8889 (so through at least 3.2.1) incorrectly generate
8890 DW_TAG_variable tags. */
6e70227d 8891
ff355380 8892 const char *physname;
c906108c 8893
a9a9bd0f 8894 /* Get name of field. */
39cbfefa
DJ
8895 fieldname = dwarf2_name (die, cu);
8896 if (fieldname == NULL)
c906108c
SS
8897 return;
8898
254e6b9e 8899 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
8900 if (attr
8901 /* Only create a symbol if this is an external value.
8902 new_symbol checks this and puts the value in the global symbol
8903 table, which we want. If it is not external, new_symbol
8904 will try to put the value in cu->list_in_scope which is wrong. */
8905 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
8906 {
8907 /* A static const member, not much different than an enum as far as
8908 we're concerned, except that we can support more types. */
8909 new_symbol (die, NULL, cu);
8910 }
8911
2df3850c 8912 /* Get physical name. */
ff355380 8913 physname = dwarf2_physname (fieldname, die, cu);
c906108c 8914
d8151005
DJ
8915 /* The name is already allocated along with this objfile, so we don't
8916 need to duplicate it for the type. */
8917 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 8918 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 8919 FIELD_NAME (*fp) = fieldname;
c906108c
SS
8920 }
8921 else if (die->tag == DW_TAG_inheritance)
8922 {
74ac6d43 8923 LONGEST offset;
d4b96c9a 8924
74ac6d43
TT
8925 /* C++ base class field. */
8926 if (handle_data_member_location (die, cu, &offset))
8927 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 8928 FIELD_BITSIZE (*fp) = 0;
e7c27a73 8929 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
8930 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8931 fip->nbaseclasses++;
8932 }
8933}
8934
98751a41
JK
8935/* Add a typedef defined in the scope of the FIP's class. */
8936
8937static void
8938dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8939 struct dwarf2_cu *cu)
6e70227d 8940{
98751a41 8941 struct objfile *objfile = cu->objfile;
98751a41
JK
8942 struct typedef_field_list *new_field;
8943 struct attribute *attr;
8944 struct typedef_field *fp;
8945 char *fieldname = "";
8946
8947 /* Allocate a new field list entry and link it in. */
8948 new_field = xzalloc (sizeof (*new_field));
8949 make_cleanup (xfree, new_field);
8950
8951 gdb_assert (die->tag == DW_TAG_typedef);
8952
8953 fp = &new_field->field;
8954
8955 /* Get name of field. */
8956 fp->name = dwarf2_name (die, cu);
8957 if (fp->name == NULL)
8958 return;
8959
8960 fp->type = read_type_die (die, cu);
8961
8962 new_field->next = fip->typedef_field_list;
8963 fip->typedef_field_list = new_field;
8964 fip->typedef_field_list_count++;
8965}
8966
c906108c
SS
8967/* Create the vector of fields, and attach it to the type. */
8968
8969static void
fba45db2 8970dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8971 struct dwarf2_cu *cu)
c906108c
SS
8972{
8973 int nfields = fip->nfields;
8974
8975 /* Record the field count, allocate space for the array of fields,
8976 and create blank accessibility bitfields if necessary. */
8977 TYPE_NFIELDS (type) = nfields;
8978 TYPE_FIELDS (type) = (struct field *)
8979 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8980 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8981
b4ba55a1 8982 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
8983 {
8984 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8985
8986 TYPE_FIELD_PRIVATE_BITS (type) =
8987 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8988 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8989
8990 TYPE_FIELD_PROTECTED_BITS (type) =
8991 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8992 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8993
774b6a14
TT
8994 TYPE_FIELD_IGNORE_BITS (type) =
8995 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8996 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
8997 }
8998
8999 /* If the type has baseclasses, allocate and clear a bit vector for
9000 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 9001 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
9002 {
9003 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 9004 unsigned char *pointer;
c906108c
SS
9005
9006 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
9007 pointer = TYPE_ALLOC (type, num_bytes);
9008 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
9009 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
9010 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
9011 }
9012
3e43a32a
MS
9013 /* Copy the saved-up fields into the field vector. Start from the head of
9014 the list, adding to the tail of the field array, so that they end up in
9015 the same order in the array in which they were added to the list. */
c906108c
SS
9016 while (nfields-- > 0)
9017 {
7d0ccb61
DJ
9018 struct nextfield *fieldp;
9019
9020 if (fip->fields)
9021 {
9022 fieldp = fip->fields;
9023 fip->fields = fieldp->next;
9024 }
9025 else
9026 {
9027 fieldp = fip->baseclasses;
9028 fip->baseclasses = fieldp->next;
9029 }
9030
9031 TYPE_FIELD (type, nfields) = fieldp->field;
9032 switch (fieldp->accessibility)
c906108c 9033 {
c5aa993b 9034 case DW_ACCESS_private:
b4ba55a1
JB
9035 if (cu->language != language_ada)
9036 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 9037 break;
c906108c 9038
c5aa993b 9039 case DW_ACCESS_protected:
b4ba55a1
JB
9040 if (cu->language != language_ada)
9041 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 9042 break;
c906108c 9043
c5aa993b
JM
9044 case DW_ACCESS_public:
9045 break;
c906108c 9046
c5aa993b
JM
9047 default:
9048 /* Unknown accessibility. Complain and treat it as public. */
9049 {
e2e0b3e5 9050 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 9051 fieldp->accessibility);
c5aa993b
JM
9052 }
9053 break;
c906108c
SS
9054 }
9055 if (nfields < fip->nbaseclasses)
9056 {
7d0ccb61 9057 switch (fieldp->virtuality)
c906108c 9058 {
c5aa993b
JM
9059 case DW_VIRTUALITY_virtual:
9060 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 9061 if (cu->language == language_ada)
a73c6dcd 9062 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
9063 SET_TYPE_FIELD_VIRTUAL (type, nfields);
9064 break;
c906108c
SS
9065 }
9066 }
c906108c
SS
9067 }
9068}
9069
c906108c
SS
9070/* Add a member function to the proper fieldlist. */
9071
9072static void
107d2387 9073dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 9074 struct type *type, struct dwarf2_cu *cu)
c906108c 9075{
e7c27a73 9076 struct objfile *objfile = cu->objfile;
c906108c
SS
9077 struct attribute *attr;
9078 struct fnfieldlist *flp;
9079 int i;
9080 struct fn_field *fnp;
9081 char *fieldname;
c906108c 9082 struct nextfnfield *new_fnfield;
f792889a 9083 struct type *this_type;
60d5a603 9084 enum dwarf_access_attribute accessibility;
c906108c 9085
b4ba55a1 9086 if (cu->language == language_ada)
a73c6dcd 9087 error (_("unexpected member function in Ada type"));
b4ba55a1 9088
2df3850c 9089 /* Get name of member function. */
39cbfefa
DJ
9090 fieldname = dwarf2_name (die, cu);
9091 if (fieldname == NULL)
2df3850c 9092 return;
c906108c 9093
c906108c
SS
9094 /* Look up member function name in fieldlist. */
9095 for (i = 0; i < fip->nfnfields; i++)
9096 {
27bfe10e 9097 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
9098 break;
9099 }
9100
9101 /* Create new list element if necessary. */
9102 if (i < fip->nfnfields)
9103 flp = &fip->fnfieldlists[i];
9104 else
9105 {
9106 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
9107 {
9108 fip->fnfieldlists = (struct fnfieldlist *)
9109 xrealloc (fip->fnfieldlists,
9110 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 9111 * sizeof (struct fnfieldlist));
c906108c 9112 if (fip->nfnfields == 0)
c13c43fd 9113 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
9114 }
9115 flp = &fip->fnfieldlists[fip->nfnfields];
9116 flp->name = fieldname;
9117 flp->length = 0;
9118 flp->head = NULL;
3da10d80 9119 i = fip->nfnfields++;
c906108c
SS
9120 }
9121
9122 /* Create a new member function field and chain it to the field list
0963b4bd 9123 entry. */
c906108c 9124 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 9125 make_cleanup (xfree, new_fnfield);
c906108c
SS
9126 memset (new_fnfield, 0, sizeof (struct nextfnfield));
9127 new_fnfield->next = flp->head;
9128 flp->head = new_fnfield;
9129 flp->length++;
9130
9131 /* Fill in the member function field info. */
9132 fnp = &new_fnfield->fnfield;
3da10d80
KS
9133
9134 /* Delay processing of the physname until later. */
9135 if (cu->language == language_cplus || cu->language == language_java)
9136 {
9137 add_to_method_list (type, i, flp->length - 1, fieldname,
9138 die, cu);
9139 }
9140 else
9141 {
1d06ead6 9142 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
9143 fnp->physname = physname ? physname : "";
9144 }
9145
c906108c 9146 fnp->type = alloc_type (objfile);
f792889a
DJ
9147 this_type = read_type_die (die, cu);
9148 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 9149 {
f792889a 9150 int nparams = TYPE_NFIELDS (this_type);
c906108c 9151
f792889a 9152 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
9153 of the method itself (TYPE_CODE_METHOD). */
9154 smash_to_method_type (fnp->type, type,
f792889a
DJ
9155 TYPE_TARGET_TYPE (this_type),
9156 TYPE_FIELDS (this_type),
9157 TYPE_NFIELDS (this_type),
9158 TYPE_VARARGS (this_type));
c906108c
SS
9159
9160 /* Handle static member functions.
c5aa993b 9161 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
9162 member functions. G++ helps GDB by marking the first
9163 parameter for non-static member functions (which is the this
9164 pointer) as artificial. We obtain this information from
9165 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 9166 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
9167 fnp->voffset = VOFFSET_STATIC;
9168 }
9169 else
e2e0b3e5 9170 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 9171 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
9172
9173 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 9174 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 9175 fnp->fcontext = die_containing_type (die, cu);
c906108c 9176
3e43a32a
MS
9177 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
9178 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
9179
9180 /* Get accessibility. */
e142c38c 9181 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 9182 if (attr)
60d5a603
JK
9183 accessibility = DW_UNSND (attr);
9184 else
9185 accessibility = dwarf2_default_access_attribute (die, cu);
9186 switch (accessibility)
c906108c 9187 {
60d5a603
JK
9188 case DW_ACCESS_private:
9189 fnp->is_private = 1;
9190 break;
9191 case DW_ACCESS_protected:
9192 fnp->is_protected = 1;
9193 break;
c906108c
SS
9194 }
9195
b02dede2 9196 /* Check for artificial methods. */
e142c38c 9197 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
9198 if (attr && DW_UNSND (attr) != 0)
9199 fnp->is_artificial = 1;
9200
0d564a31 9201 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
9202 function. For older versions of GCC, this is an offset in the
9203 appropriate virtual table, as specified by DW_AT_containing_type.
9204 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
9205 to the object address. */
9206
e142c38c 9207 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 9208 if (attr)
8e19ed76 9209 {
aec5aa8b 9210 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 9211 {
aec5aa8b
TT
9212 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
9213 {
9214 /* Old-style GCC. */
9215 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
9216 }
9217 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
9218 || (DW_BLOCK (attr)->size > 1
9219 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
9220 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
9221 {
9222 struct dwarf_block blk;
9223 int offset;
9224
9225 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
9226 ? 1 : 2);
9227 blk.size = DW_BLOCK (attr)->size - offset;
9228 blk.data = DW_BLOCK (attr)->data + offset;
9229 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
9230 if ((fnp->voffset % cu->header.addr_size) != 0)
9231 dwarf2_complex_location_expr_complaint ();
9232 else
9233 fnp->voffset /= cu->header.addr_size;
9234 fnp->voffset += 2;
9235 }
9236 else
9237 dwarf2_complex_location_expr_complaint ();
9238
9239 if (!fnp->fcontext)
9240 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
9241 }
3690dd37 9242 else if (attr_form_is_section_offset (attr))
8e19ed76 9243 {
4d3c2250 9244 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9245 }
9246 else
9247 {
4d3c2250
KB
9248 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
9249 fieldname);
8e19ed76 9250 }
0d564a31 9251 }
d48cc9dd
DJ
9252 else
9253 {
9254 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
9255 if (attr && DW_UNSND (attr))
9256 {
9257 /* GCC does this, as of 2008-08-25; PR debug/37237. */
9258 complaint (&symfile_complaints,
3e43a32a
MS
9259 _("Member function \"%s\" (offset %d) is virtual "
9260 "but the vtable offset is not specified"),
b64f50a1 9261 fieldname, die->offset.sect_off);
9655fd1a 9262 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
9263 TYPE_CPLUS_DYNAMIC (type) = 1;
9264 }
9265 }
c906108c
SS
9266}
9267
9268/* Create the vector of member function fields, and attach it to the type. */
9269
9270static void
fba45db2 9271dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 9272 struct dwarf2_cu *cu)
c906108c
SS
9273{
9274 struct fnfieldlist *flp;
c906108c
SS
9275 int i;
9276
b4ba55a1 9277 if (cu->language == language_ada)
a73c6dcd 9278 error (_("unexpected member functions in Ada type"));
b4ba55a1 9279
c906108c
SS
9280 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9281 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
9282 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
9283
9284 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
9285 {
9286 struct nextfnfield *nfp = flp->head;
9287 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
9288 int k;
9289
9290 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
9291 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
9292 fn_flp->fn_fields = (struct fn_field *)
9293 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
9294 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 9295 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
9296 }
9297
9298 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
9299}
9300
1168df01
JB
9301/* Returns non-zero if NAME is the name of a vtable member in CU's
9302 language, zero otherwise. */
9303static int
9304is_vtable_name (const char *name, struct dwarf2_cu *cu)
9305{
9306 static const char vptr[] = "_vptr";
987504bb 9307 static const char vtable[] = "vtable";
1168df01 9308
987504bb
JJ
9309 /* Look for the C++ and Java forms of the vtable. */
9310 if ((cu->language == language_java
9311 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
9312 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
9313 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
9314 return 1;
9315
9316 return 0;
9317}
9318
c0dd20ea 9319/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
9320 functions, with the ABI-specified layout. If TYPE describes
9321 such a structure, smash it into a member function type.
61049d3b
DJ
9322
9323 GCC shouldn't do this; it should just output pointer to member DIEs.
9324 This is GCC PR debug/28767. */
c0dd20ea 9325
0b92b5bb
TT
9326static void
9327quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 9328{
0b92b5bb 9329 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
9330
9331 /* Check for a structure with no name and two children. */
0b92b5bb
TT
9332 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
9333 return;
c0dd20ea
DJ
9334
9335 /* Check for __pfn and __delta members. */
0b92b5bb
TT
9336 if (TYPE_FIELD_NAME (type, 0) == NULL
9337 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
9338 || TYPE_FIELD_NAME (type, 1) == NULL
9339 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
9340 return;
c0dd20ea
DJ
9341
9342 /* Find the type of the method. */
0b92b5bb 9343 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
9344 if (pfn_type == NULL
9345 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
9346 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 9347 return;
c0dd20ea
DJ
9348
9349 /* Look for the "this" argument. */
9350 pfn_type = TYPE_TARGET_TYPE (pfn_type);
9351 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 9352 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 9353 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 9354 return;
c0dd20ea
DJ
9355
9356 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
9357 new_type = alloc_type (objfile);
9358 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
9359 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
9360 TYPE_VARARGS (pfn_type));
0b92b5bb 9361 smash_to_methodptr_type (type, new_type);
c0dd20ea 9362}
1168df01 9363
685b1105
JK
9364/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
9365 (icc). */
9366
9367static int
9368producer_is_icc (struct dwarf2_cu *cu)
9369{
9370 if (!cu->checked_producer)
9371 check_producer (cu);
9372
9373 return cu->producer_is_icc;
9374}
9375
c906108c 9376/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
9377 (definition) to create a type for the structure or union. Fill in
9378 the type's name and general properties; the members will not be
9379 processed until process_structure_type.
c906108c 9380
c767944b
DJ
9381 NOTE: we need to call these functions regardless of whether or not the
9382 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
9383 structure or union. This gets the type entered into our set of
9384 user defined types.
9385
9386 However, if the structure is incomplete (an opaque struct/union)
9387 then suppress creating a symbol table entry for it since gdb only
9388 wants to find the one with the complete definition. Note that if
9389 it is complete, we just call new_symbol, which does it's own
9390 checking about whether the struct/union is anonymous or not (and
9391 suppresses creating a symbol table entry itself). */
9392
f792889a 9393static struct type *
134d01f1 9394read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9395{
e7c27a73 9396 struct objfile *objfile = cu->objfile;
c906108c
SS
9397 struct type *type;
9398 struct attribute *attr;
39cbfefa 9399 char *name;
c906108c 9400
348e048f
DE
9401 /* If the definition of this type lives in .debug_types, read that type.
9402 Don't follow DW_AT_specification though, that will take us back up
9403 the chain and we want to go down. */
9404 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9405 if (attr)
9406 {
9407 struct dwarf2_cu *type_cu = cu;
9408 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9409
348e048f
DE
9410 /* We could just recurse on read_structure_type, but we need to call
9411 get_die_type to ensure only one type for this DIE is created.
9412 This is important, for example, because for c++ classes we need
9413 TYPE_NAME set which is only done by new_symbol. Blech. */
9414 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9415
9416 /* TYPE_CU may not be the same as CU.
9417 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9418 return set_die_type (die, type, cu);
9419 }
9420
c0dd20ea 9421 type = alloc_type (objfile);
c906108c 9422 INIT_CPLUS_SPECIFIC (type);
93311388 9423
39cbfefa
DJ
9424 name = dwarf2_name (die, cu);
9425 if (name != NULL)
c906108c 9426 {
987504bb
JJ
9427 if (cu->language == language_cplus
9428 || cu->language == language_java)
63d06c5c 9429 {
3da10d80
KS
9430 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9431
9432 /* dwarf2_full_name might have already finished building the DIE's
9433 type. If so, there is no need to continue. */
9434 if (get_die_type (die, cu) != NULL)
9435 return get_die_type (die, cu);
9436
9437 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
9438 if (die->tag == DW_TAG_structure_type
9439 || die->tag == DW_TAG_class_type)
9440 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
9441 }
9442 else
9443 {
d8151005
DJ
9444 /* The name is already allocated along with this objfile, so
9445 we don't need to duplicate it for the type. */
94af9270
KS
9446 TYPE_TAG_NAME (type) = (char *) name;
9447 if (die->tag == DW_TAG_class_type)
9448 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 9449 }
c906108c
SS
9450 }
9451
9452 if (die->tag == DW_TAG_structure_type)
9453 {
9454 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9455 }
9456 else if (die->tag == DW_TAG_union_type)
9457 {
9458 TYPE_CODE (type) = TYPE_CODE_UNION;
9459 }
9460 else
9461 {
c906108c
SS
9462 TYPE_CODE (type) = TYPE_CODE_CLASS;
9463 }
9464
0cc2414c
TT
9465 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9466 TYPE_DECLARED_CLASS (type) = 1;
9467
e142c38c 9468 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9469 if (attr)
9470 {
9471 TYPE_LENGTH (type) = DW_UNSND (attr);
9472 }
9473 else
9474 {
9475 TYPE_LENGTH (type) = 0;
9476 }
9477
685b1105
JK
9478 if (producer_is_icc (cu))
9479 {
9480 /* ICC does not output the required DW_AT_declaration
9481 on incomplete types, but gives them a size of zero. */
9482 }
9483 else
9484 TYPE_STUB_SUPPORTED (type) = 1;
9485
dc718098 9486 if (die_is_declaration (die, cu))
876cecd0 9487 TYPE_STUB (type) = 1;
a6c727b2
DJ
9488 else if (attr == NULL && die->child == NULL
9489 && producer_is_realview (cu->producer))
9490 /* RealView does not output the required DW_AT_declaration
9491 on incomplete types. */
9492 TYPE_STUB (type) = 1;
dc718098 9493
c906108c
SS
9494 /* We need to add the type field to the die immediately so we don't
9495 infinitely recurse when dealing with pointers to the structure
0963b4bd 9496 type within the structure itself. */
1c379e20 9497 set_die_type (die, type, cu);
c906108c 9498
7e314c57
JK
9499 /* set_die_type should be already done. */
9500 set_descriptive_type (type, die, cu);
9501
c767944b
DJ
9502 return type;
9503}
9504
9505/* Finish creating a structure or union type, including filling in
9506 its members and creating a symbol for it. */
9507
9508static void
9509process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9510{
9511 struct objfile *objfile = cu->objfile;
9512 struct die_info *child_die = die->child;
9513 struct type *type;
9514
9515 type = get_die_type (die, cu);
9516 if (type == NULL)
9517 type = read_structure_type (die, cu);
9518
e142c38c 9519 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
9520 {
9521 struct field_info fi;
9522 struct die_info *child_die;
34eaf542 9523 VEC (symbolp) *template_args = NULL;
c767944b 9524 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
9525
9526 memset (&fi, 0, sizeof (struct field_info));
9527
639d11d3 9528 child_die = die->child;
c906108c
SS
9529
9530 while (child_die && child_die->tag)
9531 {
a9a9bd0f
DC
9532 if (child_die->tag == DW_TAG_member
9533 || child_die->tag == DW_TAG_variable)
c906108c 9534 {
a9a9bd0f
DC
9535 /* NOTE: carlton/2002-11-05: A C++ static data member
9536 should be a DW_TAG_member that is a declaration, but
9537 all versions of G++ as of this writing (so through at
9538 least 3.2.1) incorrectly generate DW_TAG_variable
9539 tags for them instead. */
e7c27a73 9540 dwarf2_add_field (&fi, child_die, cu);
c906108c 9541 }
8713b1b1 9542 else if (child_die->tag == DW_TAG_subprogram)
c906108c 9543 {
0963b4bd 9544 /* C++ member function. */
e7c27a73 9545 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
9546 }
9547 else if (child_die->tag == DW_TAG_inheritance)
9548 {
9549 /* C++ base class field. */
e7c27a73 9550 dwarf2_add_field (&fi, child_die, cu);
c906108c 9551 }
98751a41
JK
9552 else if (child_die->tag == DW_TAG_typedef)
9553 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
9554 else if (child_die->tag == DW_TAG_template_type_param
9555 || child_die->tag == DW_TAG_template_value_param)
9556 {
9557 struct symbol *arg = new_symbol (child_die, NULL, cu);
9558
f1078f66
DJ
9559 if (arg != NULL)
9560 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9561 }
9562
c906108c
SS
9563 child_die = sibling_die (child_die);
9564 }
9565
34eaf542
TT
9566 /* Attach template arguments to type. */
9567 if (! VEC_empty (symbolp, template_args))
9568 {
9569 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9570 TYPE_N_TEMPLATE_ARGUMENTS (type)
9571 = VEC_length (symbolp, template_args);
9572 TYPE_TEMPLATE_ARGUMENTS (type)
9573 = obstack_alloc (&objfile->objfile_obstack,
9574 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9575 * sizeof (struct symbol *)));
9576 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9577 VEC_address (symbolp, template_args),
9578 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9579 * sizeof (struct symbol *)));
9580 VEC_free (symbolp, template_args);
9581 }
9582
c906108c
SS
9583 /* Attach fields and member functions to the type. */
9584 if (fi.nfields)
e7c27a73 9585 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
9586 if (fi.nfnfields)
9587 {
e7c27a73 9588 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 9589
c5aa993b 9590 /* Get the type which refers to the base class (possibly this
c906108c 9591 class itself) which contains the vtable pointer for the current
0d564a31
DJ
9592 class from the DW_AT_containing_type attribute. This use of
9593 DW_AT_containing_type is a GNU extension. */
c906108c 9594
e142c38c 9595 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 9596 {
e7c27a73 9597 struct type *t = die_containing_type (die, cu);
c906108c
SS
9598
9599 TYPE_VPTR_BASETYPE (type) = t;
9600 if (type == t)
9601 {
c906108c
SS
9602 int i;
9603
9604 /* Our own class provides vtbl ptr. */
9605 for (i = TYPE_NFIELDS (t) - 1;
9606 i >= TYPE_N_BASECLASSES (t);
9607 --i)
9608 {
0d5cff50 9609 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 9610
1168df01 9611 if (is_vtable_name (fieldname, cu))
c906108c
SS
9612 {
9613 TYPE_VPTR_FIELDNO (type) = i;
9614 break;
9615 }
9616 }
9617
9618 /* Complain if virtual function table field not found. */
9619 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 9620 complaint (&symfile_complaints,
3e43a32a
MS
9621 _("virtual function table pointer "
9622 "not found when defining class '%s'"),
4d3c2250
KB
9623 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9624 "");
c906108c
SS
9625 }
9626 else
9627 {
9628 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9629 }
9630 }
f6235d4c
EZ
9631 else if (cu->producer
9632 && strncmp (cu->producer,
9633 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9634 {
9635 /* The IBM XLC compiler does not provide direct indication
9636 of the containing type, but the vtable pointer is
9637 always named __vfp. */
9638
9639 int i;
9640
9641 for (i = TYPE_NFIELDS (type) - 1;
9642 i >= TYPE_N_BASECLASSES (type);
9643 --i)
9644 {
9645 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9646 {
9647 TYPE_VPTR_FIELDNO (type) = i;
9648 TYPE_VPTR_BASETYPE (type) = type;
9649 break;
9650 }
9651 }
9652 }
c906108c 9653 }
98751a41
JK
9654
9655 /* Copy fi.typedef_field_list linked list elements content into the
9656 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9657 if (fi.typedef_field_list)
9658 {
9659 int i = fi.typedef_field_list_count;
9660
a0d7a4ff 9661 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
9662 TYPE_TYPEDEF_FIELD_ARRAY (type)
9663 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9664 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9665
9666 /* Reverse the list order to keep the debug info elements order. */
9667 while (--i >= 0)
9668 {
9669 struct typedef_field *dest, *src;
6e70227d 9670
98751a41
JK
9671 dest = &TYPE_TYPEDEF_FIELD (type, i);
9672 src = &fi.typedef_field_list->field;
9673 fi.typedef_field_list = fi.typedef_field_list->next;
9674 *dest = *src;
9675 }
9676 }
c767944b
DJ
9677
9678 do_cleanups (back_to);
eb2a6f42
TT
9679
9680 if (HAVE_CPLUS_STRUCT (type))
9681 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 9682 }
63d06c5c 9683
bb5ed363 9684 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 9685
90aeadfc
DC
9686 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9687 snapshots) has been known to create a die giving a declaration
9688 for a class that has, as a child, a die giving a definition for a
9689 nested class. So we have to process our children even if the
9690 current die is a declaration. Normally, of course, a declaration
9691 won't have any children at all. */
134d01f1 9692
90aeadfc
DC
9693 while (child_die != NULL && child_die->tag)
9694 {
9695 if (child_die->tag == DW_TAG_member
9696 || child_die->tag == DW_TAG_variable
34eaf542
TT
9697 || child_die->tag == DW_TAG_inheritance
9698 || child_die->tag == DW_TAG_template_value_param
9699 || child_die->tag == DW_TAG_template_type_param)
134d01f1 9700 {
90aeadfc 9701 /* Do nothing. */
134d01f1 9702 }
90aeadfc
DC
9703 else
9704 process_die (child_die, cu);
134d01f1 9705
90aeadfc 9706 child_die = sibling_die (child_die);
134d01f1
DJ
9707 }
9708
fa4028e9
JB
9709 /* Do not consider external references. According to the DWARF standard,
9710 these DIEs are identified by the fact that they have no byte_size
9711 attribute, and a declaration attribute. */
9712 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9713 || !die_is_declaration (die, cu))
c767944b 9714 new_symbol (die, type, cu);
134d01f1
DJ
9715}
9716
9717/* Given a DW_AT_enumeration_type die, set its type. We do not
9718 complete the type's fields yet, or create any symbols. */
c906108c 9719
f792889a 9720static struct type *
134d01f1 9721read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9722{
e7c27a73 9723 struct objfile *objfile = cu->objfile;
c906108c 9724 struct type *type;
c906108c 9725 struct attribute *attr;
0114d602 9726 const char *name;
134d01f1 9727
348e048f
DE
9728 /* If the definition of this type lives in .debug_types, read that type.
9729 Don't follow DW_AT_specification though, that will take us back up
9730 the chain and we want to go down. */
9731 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9732 if (attr)
9733 {
9734 struct dwarf2_cu *type_cu = cu;
9735 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9736
348e048f 9737 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9738
9739 /* TYPE_CU may not be the same as CU.
9740 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9741 return set_die_type (die, type, cu);
9742 }
9743
c906108c
SS
9744 type = alloc_type (objfile);
9745
9746 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 9747 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 9748 if (name != NULL)
0114d602 9749 TYPE_TAG_NAME (type) = (char *) name;
c906108c 9750
e142c38c 9751 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9752 if (attr)
9753 {
9754 TYPE_LENGTH (type) = DW_UNSND (attr);
9755 }
9756 else
9757 {
9758 TYPE_LENGTH (type) = 0;
9759 }
9760
137033e9
JB
9761 /* The enumeration DIE can be incomplete. In Ada, any type can be
9762 declared as private in the package spec, and then defined only
9763 inside the package body. Such types are known as Taft Amendment
9764 Types. When another package uses such a type, an incomplete DIE
9765 may be generated by the compiler. */
02eb380e 9766 if (die_is_declaration (die, cu))
876cecd0 9767 TYPE_STUB (type) = 1;
02eb380e 9768
f792889a 9769 return set_die_type (die, type, cu);
134d01f1
DJ
9770}
9771
9772/* Given a pointer to a die which begins an enumeration, process all
9773 the dies that define the members of the enumeration, and create the
9774 symbol for the enumeration type.
9775
9776 NOTE: We reverse the order of the element list. */
9777
9778static void
9779process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9780{
f792889a 9781 struct type *this_type;
134d01f1 9782
f792889a
DJ
9783 this_type = get_die_type (die, cu);
9784 if (this_type == NULL)
9785 this_type = read_enumeration_type (die, cu);
9dc481d3 9786
639d11d3 9787 if (die->child != NULL)
c906108c 9788 {
9dc481d3
DE
9789 struct die_info *child_die;
9790 struct symbol *sym;
9791 struct field *fields = NULL;
9792 int num_fields = 0;
9793 int unsigned_enum = 1;
9794 char *name;
cafec441
TT
9795 int flag_enum = 1;
9796 ULONGEST mask = 0;
9dc481d3 9797
639d11d3 9798 child_die = die->child;
c906108c
SS
9799 while (child_die && child_die->tag)
9800 {
9801 if (child_die->tag != DW_TAG_enumerator)
9802 {
e7c27a73 9803 process_die (child_die, cu);
c906108c
SS
9804 }
9805 else
9806 {
39cbfefa
DJ
9807 name = dwarf2_name (child_die, cu);
9808 if (name)
c906108c 9809 {
f792889a 9810 sym = new_symbol (child_die, this_type, cu);
c906108c 9811 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
9812 {
9813 unsigned_enum = 0;
9814 flag_enum = 0;
9815 }
9816 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9817 flag_enum = 0;
9818 else
9819 mask |= SYMBOL_VALUE (sym);
c906108c
SS
9820
9821 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9822 {
9823 fields = (struct field *)
9824 xrealloc (fields,
9825 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 9826 * sizeof (struct field));
c906108c
SS
9827 }
9828
3567439c 9829 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 9830 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 9831 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
9832 FIELD_BITSIZE (fields[num_fields]) = 0;
9833
9834 num_fields++;
9835 }
9836 }
9837
9838 child_die = sibling_die (child_die);
9839 }
9840
9841 if (num_fields)
9842 {
f792889a
DJ
9843 TYPE_NFIELDS (this_type) = num_fields;
9844 TYPE_FIELDS (this_type) = (struct field *)
9845 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9846 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 9847 sizeof (struct field) * num_fields);
b8c9b27d 9848 xfree (fields);
c906108c
SS
9849 }
9850 if (unsigned_enum)
876cecd0 9851 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
9852 if (flag_enum)
9853 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 9854 }
134d01f1 9855
6c83ed52
TT
9856 /* If we are reading an enum from a .debug_types unit, and the enum
9857 is a declaration, and the enum is not the signatured type in the
9858 unit, then we do not want to add a symbol for it. Adding a
9859 symbol would in some cases obscure the true definition of the
9860 enum, giving users an incomplete type when the definition is
9861 actually available. Note that we do not want to do this for all
9862 enums which are just declarations, because C++0x allows forward
9863 enum declarations. */
3019eac3 9864 if (cu->per_cu->is_debug_types
6c83ed52
TT
9865 && die_is_declaration (die, cu))
9866 {
52dc124a 9867 struct signatured_type *sig_type;
6c83ed52 9868
52dc124a 9869 sig_type
6c83ed52 9870 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 9871 cu->per_cu->info_or_types_section,
6c83ed52 9872 cu->per_cu->offset);
3019eac3
DE
9873 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9874 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
9875 return;
9876 }
9877
f792889a 9878 new_symbol (die, this_type, cu);
c906108c
SS
9879}
9880
9881/* Extract all information from a DW_TAG_array_type DIE and put it in
9882 the DIE's type field. For now, this only handles one dimensional
9883 arrays. */
9884
f792889a 9885static struct type *
e7c27a73 9886read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9887{
e7c27a73 9888 struct objfile *objfile = cu->objfile;
c906108c 9889 struct die_info *child_die;
7e314c57 9890 struct type *type;
c906108c
SS
9891 struct type *element_type, *range_type, *index_type;
9892 struct type **range_types = NULL;
9893 struct attribute *attr;
9894 int ndim = 0;
9895 struct cleanup *back_to;
39cbfefa 9896 char *name;
c906108c 9897
e7c27a73 9898 element_type = die_type (die, cu);
c906108c 9899
7e314c57
JK
9900 /* The die_type call above may have already set the type for this DIE. */
9901 type = get_die_type (die, cu);
9902 if (type)
9903 return type;
9904
c906108c
SS
9905 /* Irix 6.2 native cc creates array types without children for
9906 arrays with unspecified length. */
639d11d3 9907 if (die->child == NULL)
c906108c 9908 {
46bf5051 9909 index_type = objfile_type (objfile)->builtin_int;
c906108c 9910 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
9911 type = create_array_type (NULL, element_type, range_type);
9912 return set_die_type (die, type, cu);
c906108c
SS
9913 }
9914
9915 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 9916 child_die = die->child;
c906108c
SS
9917 while (child_die && child_die->tag)
9918 {
9919 if (child_die->tag == DW_TAG_subrange_type)
9920 {
f792889a 9921 struct type *child_type = read_type_die (child_die, cu);
9a619af0 9922
f792889a 9923 if (child_type != NULL)
a02abb62 9924 {
0963b4bd
MS
9925 /* The range type was succesfully read. Save it for the
9926 array type creation. */
a02abb62
JB
9927 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9928 {
9929 range_types = (struct type **)
9930 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9931 * sizeof (struct type *));
9932 if (ndim == 0)
9933 make_cleanup (free_current_contents, &range_types);
9934 }
f792889a 9935 range_types[ndim++] = child_type;
a02abb62 9936 }
c906108c
SS
9937 }
9938 child_die = sibling_die (child_die);
9939 }
9940
9941 /* Dwarf2 dimensions are output from left to right, create the
9942 necessary array types in backwards order. */
7ca2d3a3 9943
c906108c 9944 type = element_type;
7ca2d3a3
DL
9945
9946 if (read_array_order (die, cu) == DW_ORD_col_major)
9947 {
9948 int i = 0;
9a619af0 9949
7ca2d3a3
DL
9950 while (i < ndim)
9951 type = create_array_type (NULL, type, range_types[i++]);
9952 }
9953 else
9954 {
9955 while (ndim-- > 0)
9956 type = create_array_type (NULL, type, range_types[ndim]);
9957 }
c906108c 9958
f5f8a009
EZ
9959 /* Understand Dwarf2 support for vector types (like they occur on
9960 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9961 array type. This is not part of the Dwarf2/3 standard yet, but a
9962 custom vendor extension. The main difference between a regular
9963 array and the vector variant is that vectors are passed by value
9964 to functions. */
e142c38c 9965 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 9966 if (attr)
ea37ba09 9967 make_vector_type (type);
f5f8a009 9968
dbc98a8b
KW
9969 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9970 implementation may choose to implement triple vectors using this
9971 attribute. */
9972 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9973 if (attr)
9974 {
9975 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9976 TYPE_LENGTH (type) = DW_UNSND (attr);
9977 else
3e43a32a
MS
9978 complaint (&symfile_complaints,
9979 _("DW_AT_byte_size for array type smaller "
9980 "than the total size of elements"));
dbc98a8b
KW
9981 }
9982
39cbfefa
DJ
9983 name = dwarf2_name (die, cu);
9984 if (name)
9985 TYPE_NAME (type) = name;
6e70227d 9986
0963b4bd 9987 /* Install the type in the die. */
7e314c57
JK
9988 set_die_type (die, type, cu);
9989
9990 /* set_die_type should be already done. */
b4ba55a1
JB
9991 set_descriptive_type (type, die, cu);
9992
c906108c
SS
9993 do_cleanups (back_to);
9994
7e314c57 9995 return type;
c906108c
SS
9996}
9997
7ca2d3a3 9998static enum dwarf_array_dim_ordering
6e70227d 9999read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
10000{
10001 struct attribute *attr;
10002
10003 attr = dwarf2_attr (die, DW_AT_ordering, cu);
10004
10005 if (attr) return DW_SND (attr);
10006
0963b4bd
MS
10007 /* GNU F77 is a special case, as at 08/2004 array type info is the
10008 opposite order to the dwarf2 specification, but data is still
10009 laid out as per normal fortran.
7ca2d3a3 10010
0963b4bd
MS
10011 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
10012 version checking. */
7ca2d3a3 10013
905e0470
PM
10014 if (cu->language == language_fortran
10015 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
10016 {
10017 return DW_ORD_row_major;
10018 }
10019
6e70227d 10020 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
10021 {
10022 case array_column_major:
10023 return DW_ORD_col_major;
10024 case array_row_major:
10025 default:
10026 return DW_ORD_row_major;
10027 };
10028}
10029
72019c9c 10030/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 10031 the DIE's type field. */
72019c9c 10032
f792889a 10033static struct type *
72019c9c
GM
10034read_set_type (struct die_info *die, struct dwarf2_cu *cu)
10035{
7e314c57
JK
10036 struct type *domain_type, *set_type;
10037 struct attribute *attr;
f792889a 10038
7e314c57
JK
10039 domain_type = die_type (die, cu);
10040
10041 /* The die_type call above may have already set the type for this DIE. */
10042 set_type = get_die_type (die, cu);
10043 if (set_type)
10044 return set_type;
10045
10046 set_type = create_set_type (NULL, domain_type);
10047
10048 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
10049 if (attr)
10050 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 10051
f792889a 10052 return set_die_type (die, set_type, cu);
72019c9c 10053}
7ca2d3a3 10054
c906108c
SS
10055/* First cut: install each common block member as a global variable. */
10056
10057static void
e7c27a73 10058read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
10059{
10060 struct die_info *child_die;
10061 struct attribute *attr;
10062 struct symbol *sym;
10063 CORE_ADDR base = (CORE_ADDR) 0;
10064
e142c38c 10065 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
10066 if (attr)
10067 {
0963b4bd 10068 /* Support the .debug_loc offsets. */
8e19ed76
PS
10069 if (attr_form_is_block (attr))
10070 {
e7c27a73 10071 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 10072 }
3690dd37 10073 else if (attr_form_is_section_offset (attr))
8e19ed76 10074 {
4d3c2250 10075 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
10076 }
10077 else
10078 {
4d3c2250
KB
10079 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
10080 "common block member");
8e19ed76 10081 }
c906108c 10082 }
639d11d3 10083 if (die->child != NULL)
c906108c 10084 {
639d11d3 10085 child_die = die->child;
c906108c
SS
10086 while (child_die && child_die->tag)
10087 {
74ac6d43
TT
10088 LONGEST offset;
10089
e7c27a73 10090 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
10091 if (sym != NULL
10092 && handle_data_member_location (child_die, cu, &offset))
c906108c 10093 {
74ac6d43 10094 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
10095 add_symbol_to_list (sym, &global_symbols);
10096 }
10097 child_die = sibling_die (child_die);
10098 }
10099 }
10100}
10101
0114d602 10102/* Create a type for a C++ namespace. */
d9fa45fe 10103
0114d602
DJ
10104static struct type *
10105read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 10106{
e7c27a73 10107 struct objfile *objfile = cu->objfile;
0114d602 10108 const char *previous_prefix, *name;
9219021c 10109 int is_anonymous;
0114d602
DJ
10110 struct type *type;
10111
10112 /* For extensions, reuse the type of the original namespace. */
10113 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
10114 {
10115 struct die_info *ext_die;
10116 struct dwarf2_cu *ext_cu = cu;
9a619af0 10117
0114d602
DJ
10118 ext_die = dwarf2_extension (die, &ext_cu);
10119 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
10120
10121 /* EXT_CU may not be the same as CU.
10122 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
10123 return set_die_type (die, type, cu);
10124 }
9219021c 10125
e142c38c 10126 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
10127
10128 /* Now build the name of the current namespace. */
10129
0114d602
DJ
10130 previous_prefix = determine_prefix (die, cu);
10131 if (previous_prefix[0] != '\0')
10132 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 10133 previous_prefix, name, 0, cu);
0114d602
DJ
10134
10135 /* Create the type. */
10136 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
10137 objfile);
10138 TYPE_NAME (type) = (char *) name;
10139 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10140
60531b24 10141 return set_die_type (die, type, cu);
0114d602
DJ
10142}
10143
10144/* Read a C++ namespace. */
10145
10146static void
10147read_namespace (struct die_info *die, struct dwarf2_cu *cu)
10148{
10149 struct objfile *objfile = cu->objfile;
0114d602 10150 int is_anonymous;
9219021c 10151
5c4e30ca
DC
10152 /* Add a symbol associated to this if we haven't seen the namespace
10153 before. Also, add a using directive if it's an anonymous
10154 namespace. */
9219021c 10155
f2f0e013 10156 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
10157 {
10158 struct type *type;
10159
0114d602 10160 type = read_type_die (die, cu);
e7c27a73 10161 new_symbol (die, type, cu);
5c4e30ca 10162
e8e80198 10163 namespace_name (die, &is_anonymous, cu);
5c4e30ca 10164 if (is_anonymous)
0114d602
DJ
10165 {
10166 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 10167
c0cc3a76 10168 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 10169 NULL, NULL, &objfile->objfile_obstack);
0114d602 10170 }
5c4e30ca 10171 }
9219021c 10172
639d11d3 10173 if (die->child != NULL)
d9fa45fe 10174 {
639d11d3 10175 struct die_info *child_die = die->child;
6e70227d 10176
d9fa45fe
DC
10177 while (child_die && child_die->tag)
10178 {
e7c27a73 10179 process_die (child_die, cu);
d9fa45fe
DC
10180 child_die = sibling_die (child_die);
10181 }
10182 }
38d518c9
EZ
10183}
10184
f55ee35c
JK
10185/* Read a Fortran module as type. This DIE can be only a declaration used for
10186 imported module. Still we need that type as local Fortran "use ... only"
10187 declaration imports depend on the created type in determine_prefix. */
10188
10189static struct type *
10190read_module_type (struct die_info *die, struct dwarf2_cu *cu)
10191{
10192 struct objfile *objfile = cu->objfile;
10193 char *module_name;
10194 struct type *type;
10195
10196 module_name = dwarf2_name (die, cu);
10197 if (!module_name)
3e43a32a
MS
10198 complaint (&symfile_complaints,
10199 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 10200 die->offset.sect_off);
f55ee35c
JK
10201 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
10202
10203 /* determine_prefix uses TYPE_TAG_NAME. */
10204 TYPE_TAG_NAME (type) = TYPE_NAME (type);
10205
10206 return set_die_type (die, type, cu);
10207}
10208
5d7cb8df
JK
10209/* Read a Fortran module. */
10210
10211static void
10212read_module (struct die_info *die, struct dwarf2_cu *cu)
10213{
10214 struct die_info *child_die = die->child;
10215
5d7cb8df
JK
10216 while (child_die && child_die->tag)
10217 {
10218 process_die (child_die, cu);
10219 child_die = sibling_die (child_die);
10220 }
10221}
10222
38d518c9
EZ
10223/* Return the name of the namespace represented by DIE. Set
10224 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
10225 namespace. */
10226
10227static const char *
e142c38c 10228namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
10229{
10230 struct die_info *current_die;
10231 const char *name = NULL;
10232
10233 /* Loop through the extensions until we find a name. */
10234
10235 for (current_die = die;
10236 current_die != NULL;
f2f0e013 10237 current_die = dwarf2_extension (die, &cu))
38d518c9 10238 {
e142c38c 10239 name = dwarf2_name (current_die, cu);
38d518c9
EZ
10240 if (name != NULL)
10241 break;
10242 }
10243
10244 /* Is it an anonymous namespace? */
10245
10246 *is_anonymous = (name == NULL);
10247 if (*is_anonymous)
2b1dbab0 10248 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
10249
10250 return name;
d9fa45fe
DC
10251}
10252
c906108c
SS
10253/* Extract all information from a DW_TAG_pointer_type DIE and add to
10254 the user defined type vector. */
10255
f792889a 10256static struct type *
e7c27a73 10257read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10258{
5e2b427d 10259 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 10260 struct comp_unit_head *cu_header = &cu->header;
c906108c 10261 struct type *type;
8b2dbe47
KB
10262 struct attribute *attr_byte_size;
10263 struct attribute *attr_address_class;
10264 int byte_size, addr_class;
7e314c57
JK
10265 struct type *target_type;
10266
10267 target_type = die_type (die, cu);
c906108c 10268
7e314c57
JK
10269 /* The die_type call above may have already set the type for this DIE. */
10270 type = get_die_type (die, cu);
10271 if (type)
10272 return type;
10273
10274 type = lookup_pointer_type (target_type);
8b2dbe47 10275
e142c38c 10276 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
10277 if (attr_byte_size)
10278 byte_size = DW_UNSND (attr_byte_size);
c906108c 10279 else
8b2dbe47
KB
10280 byte_size = cu_header->addr_size;
10281
e142c38c 10282 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
10283 if (attr_address_class)
10284 addr_class = DW_UNSND (attr_address_class);
10285 else
10286 addr_class = DW_ADDR_none;
10287
10288 /* If the pointer size or address class is different than the
10289 default, create a type variant marked as such and set the
10290 length accordingly. */
10291 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 10292 {
5e2b427d 10293 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
10294 {
10295 int type_flags;
10296
849957d9 10297 type_flags = gdbarch_address_class_type_flags
5e2b427d 10298 (gdbarch, byte_size, addr_class);
876cecd0
TT
10299 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
10300 == 0);
8b2dbe47
KB
10301 type = make_type_with_address_space (type, type_flags);
10302 }
10303 else if (TYPE_LENGTH (type) != byte_size)
10304 {
3e43a32a
MS
10305 complaint (&symfile_complaints,
10306 _("invalid pointer size %d"), byte_size);
8b2dbe47 10307 }
6e70227d 10308 else
9a619af0
MS
10309 {
10310 /* Should we also complain about unhandled address classes? */
10311 }
c906108c 10312 }
8b2dbe47
KB
10313
10314 TYPE_LENGTH (type) = byte_size;
f792889a 10315 return set_die_type (die, type, cu);
c906108c
SS
10316}
10317
10318/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
10319 the user defined type vector. */
10320
f792889a 10321static struct type *
e7c27a73 10322read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
10323{
10324 struct type *type;
10325 struct type *to_type;
10326 struct type *domain;
10327
e7c27a73
DJ
10328 to_type = die_type (die, cu);
10329 domain = die_containing_type (die, cu);
0d5de010 10330
7e314c57
JK
10331 /* The calls above may have already set the type for this DIE. */
10332 type = get_die_type (die, cu);
10333 if (type)
10334 return type;
10335
0d5de010
DJ
10336 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
10337 type = lookup_methodptr_type (to_type);
10338 else
10339 type = lookup_memberptr_type (to_type, domain);
c906108c 10340
f792889a 10341 return set_die_type (die, type, cu);
c906108c
SS
10342}
10343
10344/* Extract all information from a DW_TAG_reference_type DIE and add to
10345 the user defined type vector. */
10346
f792889a 10347static struct type *
e7c27a73 10348read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10349{
e7c27a73 10350 struct comp_unit_head *cu_header = &cu->header;
7e314c57 10351 struct type *type, *target_type;
c906108c
SS
10352 struct attribute *attr;
10353
7e314c57
JK
10354 target_type = die_type (die, cu);
10355
10356 /* The die_type call above may have already set the type for this DIE. */
10357 type = get_die_type (die, cu);
10358 if (type)
10359 return type;
10360
10361 type = lookup_reference_type (target_type);
e142c38c 10362 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10363 if (attr)
10364 {
10365 TYPE_LENGTH (type) = DW_UNSND (attr);
10366 }
10367 else
10368 {
107d2387 10369 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 10370 }
f792889a 10371 return set_die_type (die, type, cu);
c906108c
SS
10372}
10373
f792889a 10374static struct type *
e7c27a73 10375read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10376{
f792889a 10377 struct type *base_type, *cv_type;
c906108c 10378
e7c27a73 10379 base_type = die_type (die, cu);
7e314c57
JK
10380
10381 /* The die_type call above may have already set the type for this DIE. */
10382 cv_type = get_die_type (die, cu);
10383 if (cv_type)
10384 return cv_type;
10385
2f608a3a
KW
10386 /* In case the const qualifier is applied to an array type, the element type
10387 is so qualified, not the array type (section 6.7.3 of C99). */
10388 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
10389 {
10390 struct type *el_type, *inner_array;
10391
10392 base_type = copy_type (base_type);
10393 inner_array = base_type;
10394
10395 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10396 {
10397 TYPE_TARGET_TYPE (inner_array) =
10398 copy_type (TYPE_TARGET_TYPE (inner_array));
10399 inner_array = TYPE_TARGET_TYPE (inner_array);
10400 }
10401
10402 el_type = TYPE_TARGET_TYPE (inner_array);
10403 TYPE_TARGET_TYPE (inner_array) =
10404 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10405
10406 return set_die_type (die, base_type, cu);
10407 }
10408
f792889a
DJ
10409 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10410 return set_die_type (die, cv_type, cu);
c906108c
SS
10411}
10412
f792889a 10413static struct type *
e7c27a73 10414read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10415{
f792889a 10416 struct type *base_type, *cv_type;
c906108c 10417
e7c27a73 10418 base_type = die_type (die, cu);
7e314c57
JK
10419
10420 /* The die_type call above may have already set the type for this DIE. */
10421 cv_type = get_die_type (die, cu);
10422 if (cv_type)
10423 return cv_type;
10424
f792889a
DJ
10425 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10426 return set_die_type (die, cv_type, cu);
c906108c
SS
10427}
10428
10429/* Extract all information from a DW_TAG_string_type DIE and add to
10430 the user defined type vector. It isn't really a user defined type,
10431 but it behaves like one, with other DIE's using an AT_user_def_type
10432 attribute to reference it. */
10433
f792889a 10434static struct type *
e7c27a73 10435read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10436{
e7c27a73 10437 struct objfile *objfile = cu->objfile;
3b7538c0 10438 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10439 struct type *type, *range_type, *index_type, *char_type;
10440 struct attribute *attr;
10441 unsigned int length;
10442
e142c38c 10443 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
10444 if (attr)
10445 {
10446 length = DW_UNSND (attr);
10447 }
10448 else
10449 {
0963b4bd 10450 /* Check for the DW_AT_byte_size attribute. */
e142c38c 10451 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
10452 if (attr)
10453 {
10454 length = DW_UNSND (attr);
10455 }
10456 else
10457 {
10458 length = 1;
10459 }
c906108c 10460 }
6ccb9162 10461
46bf5051 10462 index_type = objfile_type (objfile)->builtin_int;
c906108c 10463 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
10464 char_type = language_string_char_type (cu->language_defn, gdbarch);
10465 type = create_string_type (NULL, char_type, range_type);
6ccb9162 10466
f792889a 10467 return set_die_type (die, type, cu);
c906108c
SS
10468}
10469
10470/* Handle DIES due to C code like:
10471
10472 struct foo
c5aa993b
JM
10473 {
10474 int (*funcp)(int a, long l);
10475 int b;
10476 };
c906108c 10477
0963b4bd 10478 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 10479
f792889a 10480static struct type *
e7c27a73 10481read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10482{
bb5ed363 10483 struct objfile *objfile = cu->objfile;
0963b4bd
MS
10484 struct type *type; /* Type that this function returns. */
10485 struct type *ftype; /* Function that returns above type. */
c906108c
SS
10486 struct attribute *attr;
10487
e7c27a73 10488 type = die_type (die, cu);
7e314c57
JK
10489
10490 /* The die_type call above may have already set the type for this DIE. */
10491 ftype = get_die_type (die, cu);
10492 if (ftype)
10493 return ftype;
10494
0c8b41f1 10495 ftype = lookup_function_type (type);
c906108c 10496
5b8101ae 10497 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 10498 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 10499 if ((attr && (DW_UNSND (attr) != 0))
987504bb 10500 || cu->language == language_cplus
5b8101ae
PM
10501 || cu->language == language_java
10502 || cu->language == language_pascal)
876cecd0 10503 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
10504 else if (producer_is_realview (cu->producer))
10505 /* RealView does not emit DW_AT_prototyped. We can not
10506 distinguish prototyped and unprototyped functions; default to
10507 prototyped, since that is more common in modern code (and
10508 RealView warns about unprototyped functions). */
10509 TYPE_PROTOTYPED (ftype) = 1;
c906108c 10510
c055b101
CV
10511 /* Store the calling convention in the type if it's available in
10512 the subroutine die. Otherwise set the calling convention to
10513 the default value DW_CC_normal. */
10514 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
10515 if (attr)
10516 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10517 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10518 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10519 else
10520 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
10521
10522 /* We need to add the subroutine type to the die immediately so
10523 we don't infinitely recurse when dealing with parameters
0963b4bd 10524 declared as the same subroutine type. */
76c10ea2 10525 set_die_type (die, ftype, cu);
6e70227d 10526
639d11d3 10527 if (die->child != NULL)
c906108c 10528 {
bb5ed363 10529 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 10530 struct die_info *child_die;
8072405b 10531 int nparams, iparams;
c906108c
SS
10532
10533 /* Count the number of parameters.
10534 FIXME: GDB currently ignores vararg functions, but knows about
10535 vararg member functions. */
8072405b 10536 nparams = 0;
639d11d3 10537 child_die = die->child;
c906108c
SS
10538 while (child_die && child_die->tag)
10539 {
10540 if (child_die->tag == DW_TAG_formal_parameter)
10541 nparams++;
10542 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 10543 TYPE_VARARGS (ftype) = 1;
c906108c
SS
10544 child_die = sibling_die (child_die);
10545 }
10546
10547 /* Allocate storage for parameters and fill them in. */
10548 TYPE_NFIELDS (ftype) = nparams;
10549 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 10550 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 10551
8072405b
JK
10552 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10553 even if we error out during the parameters reading below. */
10554 for (iparams = 0; iparams < nparams; iparams++)
10555 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10556
10557 iparams = 0;
639d11d3 10558 child_die = die->child;
c906108c
SS
10559 while (child_die && child_die->tag)
10560 {
10561 if (child_die->tag == DW_TAG_formal_parameter)
10562 {
3ce3b1ba
PA
10563 struct type *arg_type;
10564
10565 /* DWARF version 2 has no clean way to discern C++
10566 static and non-static member functions. G++ helps
10567 GDB by marking the first parameter for non-static
10568 member functions (which is the this pointer) as
10569 artificial. We pass this information to
10570 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10571
10572 DWARF version 3 added DW_AT_object_pointer, which GCC
10573 4.5 does not yet generate. */
e142c38c 10574 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
10575 if (attr)
10576 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10577 else
418835cc
KS
10578 {
10579 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10580
10581 /* GCC/43521: In java, the formal parameter
10582 "this" is sometimes not marked with DW_AT_artificial. */
10583 if (cu->language == language_java)
10584 {
10585 const char *name = dwarf2_name (child_die, cu);
9a619af0 10586
418835cc
KS
10587 if (name && !strcmp (name, "this"))
10588 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10589 }
10590 }
3ce3b1ba
PA
10591 arg_type = die_type (child_die, cu);
10592
10593 /* RealView does not mark THIS as const, which the testsuite
10594 expects. GCC marks THIS as const in method definitions,
10595 but not in the class specifications (GCC PR 43053). */
10596 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10597 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10598 {
10599 int is_this = 0;
10600 struct dwarf2_cu *arg_cu = cu;
10601 const char *name = dwarf2_name (child_die, cu);
10602
10603 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10604 if (attr)
10605 {
10606 /* If the compiler emits this, use it. */
10607 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10608 is_this = 1;
10609 }
10610 else if (name && strcmp (name, "this") == 0)
10611 /* Function definitions will have the argument names. */
10612 is_this = 1;
10613 else if (name == NULL && iparams == 0)
10614 /* Declarations may not have the names, so like
10615 elsewhere in GDB, assume an artificial first
10616 argument is "this". */
10617 is_this = 1;
10618
10619 if (is_this)
10620 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10621 arg_type, 0);
10622 }
10623
10624 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
10625 iparams++;
10626 }
10627 child_die = sibling_die (child_die);
10628 }
10629 }
10630
76c10ea2 10631 return ftype;
c906108c
SS
10632}
10633
f792889a 10634static struct type *
e7c27a73 10635read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10636{
e7c27a73 10637 struct objfile *objfile = cu->objfile;
0114d602 10638 const char *name = NULL;
3c8e0968 10639 struct type *this_type, *target_type;
c906108c 10640
94af9270 10641 name = dwarf2_full_name (NULL, die, cu);
f792889a 10642 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
10643 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10644 TYPE_NAME (this_type) = (char *) name;
f792889a 10645 set_die_type (die, this_type, cu);
3c8e0968
DE
10646 target_type = die_type (die, cu);
10647 if (target_type != this_type)
10648 TYPE_TARGET_TYPE (this_type) = target_type;
10649 else
10650 {
10651 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10652 spec and cause infinite loops in GDB. */
10653 complaint (&symfile_complaints,
10654 _("Self-referential DW_TAG_typedef "
10655 "- DIE at 0x%x [in module %s]"),
b64f50a1 10656 die->offset.sect_off, objfile->name);
3c8e0968
DE
10657 TYPE_TARGET_TYPE (this_type) = NULL;
10658 }
f792889a 10659 return this_type;
c906108c
SS
10660}
10661
10662/* Find a representation of a given base type and install
10663 it in the TYPE field of the die. */
10664
f792889a 10665static struct type *
e7c27a73 10666read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10667{
e7c27a73 10668 struct objfile *objfile = cu->objfile;
c906108c
SS
10669 struct type *type;
10670 struct attribute *attr;
10671 int encoding = 0, size = 0;
39cbfefa 10672 char *name;
6ccb9162
UW
10673 enum type_code code = TYPE_CODE_INT;
10674 int type_flags = 0;
10675 struct type *target_type = NULL;
c906108c 10676
e142c38c 10677 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
10678 if (attr)
10679 {
10680 encoding = DW_UNSND (attr);
10681 }
e142c38c 10682 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10683 if (attr)
10684 {
10685 size = DW_UNSND (attr);
10686 }
39cbfefa 10687 name = dwarf2_name (die, cu);
6ccb9162 10688 if (!name)
c906108c 10689 {
6ccb9162
UW
10690 complaint (&symfile_complaints,
10691 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 10692 }
6ccb9162
UW
10693
10694 switch (encoding)
c906108c 10695 {
6ccb9162
UW
10696 case DW_ATE_address:
10697 /* Turn DW_ATE_address into a void * pointer. */
10698 code = TYPE_CODE_PTR;
10699 type_flags |= TYPE_FLAG_UNSIGNED;
10700 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10701 break;
10702 case DW_ATE_boolean:
10703 code = TYPE_CODE_BOOL;
10704 type_flags |= TYPE_FLAG_UNSIGNED;
10705 break;
10706 case DW_ATE_complex_float:
10707 code = TYPE_CODE_COMPLEX;
10708 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10709 break;
10710 case DW_ATE_decimal_float:
10711 code = TYPE_CODE_DECFLOAT;
10712 break;
10713 case DW_ATE_float:
10714 code = TYPE_CODE_FLT;
10715 break;
10716 case DW_ATE_signed:
10717 break;
10718 case DW_ATE_unsigned:
10719 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
10720 if (cu->language == language_fortran
10721 && name
10722 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10723 code = TYPE_CODE_CHAR;
6ccb9162
UW
10724 break;
10725 case DW_ATE_signed_char:
6e70227d 10726 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10727 || cu->language == language_pascal
10728 || cu->language == language_fortran)
6ccb9162
UW
10729 code = TYPE_CODE_CHAR;
10730 break;
10731 case DW_ATE_unsigned_char:
868a0084 10732 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10733 || cu->language == language_pascal
10734 || cu->language == language_fortran)
6ccb9162
UW
10735 code = TYPE_CODE_CHAR;
10736 type_flags |= TYPE_FLAG_UNSIGNED;
10737 break;
75079b2b
TT
10738 case DW_ATE_UTF:
10739 /* We just treat this as an integer and then recognize the
10740 type by name elsewhere. */
10741 break;
10742
6ccb9162
UW
10743 default:
10744 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10745 dwarf_type_encoding_name (encoding));
10746 break;
c906108c 10747 }
6ccb9162 10748
0114d602
DJ
10749 type = init_type (code, size, type_flags, NULL, objfile);
10750 TYPE_NAME (type) = name;
6ccb9162
UW
10751 TYPE_TARGET_TYPE (type) = target_type;
10752
0114d602 10753 if (name && strcmp (name, "char") == 0)
876cecd0 10754 TYPE_NOSIGN (type) = 1;
0114d602 10755
f792889a 10756 return set_die_type (die, type, cu);
c906108c
SS
10757}
10758
a02abb62
JB
10759/* Read the given DW_AT_subrange DIE. */
10760
f792889a 10761static struct type *
a02abb62
JB
10762read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10763{
10764 struct type *base_type;
10765 struct type *range_type;
10766 struct attribute *attr;
4fae6e18
JK
10767 LONGEST low, high;
10768 int low_default_is_valid;
39cbfefa 10769 char *name;
43bbcdc2 10770 LONGEST negative_mask;
e77813c8 10771
a02abb62 10772 base_type = die_type (die, cu);
953ac07e
JK
10773 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10774 check_typedef (base_type);
a02abb62 10775
7e314c57
JK
10776 /* The die_type call above may have already set the type for this DIE. */
10777 range_type = get_die_type (die, cu);
10778 if (range_type)
10779 return range_type;
10780
4fae6e18
JK
10781 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10782 omitting DW_AT_lower_bound. */
10783 switch (cu->language)
6e70227d 10784 {
4fae6e18
JK
10785 case language_c:
10786 case language_cplus:
10787 low = 0;
10788 low_default_is_valid = 1;
10789 break;
10790 case language_fortran:
10791 low = 1;
10792 low_default_is_valid = 1;
10793 break;
10794 case language_d:
10795 case language_java:
10796 case language_objc:
10797 low = 0;
10798 low_default_is_valid = (cu->header.version >= 4);
10799 break;
10800 case language_ada:
10801 case language_m2:
10802 case language_pascal:
a02abb62 10803 low = 1;
4fae6e18
JK
10804 low_default_is_valid = (cu->header.version >= 4);
10805 break;
10806 default:
10807 low = 0;
10808 low_default_is_valid = 0;
10809 break;
a02abb62
JB
10810 }
10811
dd5e6932
DJ
10812 /* FIXME: For variable sized arrays either of these could be
10813 a variable rather than a constant value. We'll allow it,
10814 but we don't know how to handle it. */
e142c38c 10815 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 10816 if (attr)
4fae6e18
JK
10817 low = dwarf2_get_attr_constant_value (attr, low);
10818 else if (!low_default_is_valid)
10819 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10820 "- DIE at 0x%x [in module %s]"),
10821 die->offset.sect_off, cu->objfile->name);
a02abb62 10822
e142c38c 10823 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 10824 if (attr)
6e70227d 10825 {
d48323d8 10826 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
10827 {
10828 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 10829 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
10830 FIXME: GDB does not yet know how to handle dynamic
10831 arrays properly, treat them as arrays with unspecified
10832 length for now.
10833
10834 FIXME: jimb/2003-09-22: GDB does not really know
10835 how to handle arrays of unspecified length
10836 either; we just represent them as zero-length
10837 arrays. Choose an appropriate upper bound given
10838 the lower bound we've computed above. */
10839 high = low - 1;
10840 }
10841 else
10842 high = dwarf2_get_attr_constant_value (attr, 1);
10843 }
e77813c8
PM
10844 else
10845 {
10846 attr = dwarf2_attr (die, DW_AT_count, cu);
10847 if (attr)
10848 {
10849 int count = dwarf2_get_attr_constant_value (attr, 1);
10850 high = low + count - 1;
10851 }
c2ff108b
JK
10852 else
10853 {
10854 /* Unspecified array length. */
10855 high = low - 1;
10856 }
e77813c8
PM
10857 }
10858
10859 /* Dwarf-2 specifications explicitly allows to create subrange types
10860 without specifying a base type.
10861 In that case, the base type must be set to the type of
10862 the lower bound, upper bound or count, in that order, if any of these
10863 three attributes references an object that has a type.
10864 If no base type is found, the Dwarf-2 specifications say that
10865 a signed integer type of size equal to the size of an address should
10866 be used.
10867 For the following C code: `extern char gdb_int [];'
10868 GCC produces an empty range DIE.
10869 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 10870 high bound or count are not yet handled by this code. */
e77813c8
PM
10871 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10872 {
10873 struct objfile *objfile = cu->objfile;
10874 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10875 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10876 struct type *int_type = objfile_type (objfile)->builtin_int;
10877
10878 /* Test "int", "long int", and "long long int" objfile types,
10879 and select the first one having a size above or equal to the
10880 architecture address size. */
10881 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10882 base_type = int_type;
10883 else
10884 {
10885 int_type = objfile_type (objfile)->builtin_long;
10886 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10887 base_type = int_type;
10888 else
10889 {
10890 int_type = objfile_type (objfile)->builtin_long_long;
10891 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10892 base_type = int_type;
10893 }
10894 }
10895 }
a02abb62 10896
6e70227d 10897 negative_mask =
43bbcdc2
PH
10898 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10899 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10900 low |= negative_mask;
10901 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10902 high |= negative_mask;
10903
a02abb62
JB
10904 range_type = create_range_type (NULL, base_type, low, high);
10905
bbb0eef6
JK
10906 /* Mark arrays with dynamic length at least as an array of unspecified
10907 length. GDB could check the boundary but before it gets implemented at
10908 least allow accessing the array elements. */
d48323d8 10909 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
10910 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10911
c2ff108b
JK
10912 /* Ada expects an empty array on no boundary attributes. */
10913 if (attr == NULL && cu->language != language_ada)
10914 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10915
39cbfefa
DJ
10916 name = dwarf2_name (die, cu);
10917 if (name)
10918 TYPE_NAME (range_type) = name;
6e70227d 10919
e142c38c 10920 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
10921 if (attr)
10922 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10923
7e314c57
JK
10924 set_die_type (die, range_type, cu);
10925
10926 /* set_die_type should be already done. */
b4ba55a1
JB
10927 set_descriptive_type (range_type, die, cu);
10928
7e314c57 10929 return range_type;
a02abb62 10930}
6e70227d 10931
f792889a 10932static struct type *
81a17f79
JB
10933read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10934{
10935 struct type *type;
81a17f79 10936
81a17f79
JB
10937 /* For now, we only support the C meaning of an unspecified type: void. */
10938
0114d602
DJ
10939 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10940 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 10941
f792889a 10942 return set_die_type (die, type, cu);
81a17f79 10943}
a02abb62 10944
639d11d3
DC
10945/* Read a single die and all its descendents. Set the die's sibling
10946 field to NULL; set other fields in the die correctly, and set all
10947 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10948 location of the info_ptr after reading all of those dies. PARENT
10949 is the parent of the die in question. */
10950
10951static struct die_info *
dee91e82
DE
10952read_die_and_children (const struct die_reader_specs *reader,
10953 gdb_byte *info_ptr,
10954 gdb_byte **new_info_ptr,
10955 struct die_info *parent)
639d11d3
DC
10956{
10957 struct die_info *die;
fe1b8b76 10958 gdb_byte *cur_ptr;
639d11d3
DC
10959 int has_children;
10960
93311388 10961 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
10962 if (die == NULL)
10963 {
10964 *new_info_ptr = cur_ptr;
10965 return NULL;
10966 }
93311388 10967 store_in_ref_table (die, reader->cu);
639d11d3
DC
10968
10969 if (has_children)
348e048f 10970 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
10971 else
10972 {
10973 die->child = NULL;
10974 *new_info_ptr = cur_ptr;
10975 }
10976
10977 die->sibling = NULL;
10978 die->parent = parent;
10979 return die;
10980}
10981
10982/* Read a die, all of its descendents, and all of its siblings; set
10983 all of the fields of all of the dies correctly. Arguments are as
10984 in read_die_and_children. */
10985
10986static struct die_info *
93311388
DE
10987read_die_and_siblings (const struct die_reader_specs *reader,
10988 gdb_byte *info_ptr,
fe1b8b76 10989 gdb_byte **new_info_ptr,
639d11d3
DC
10990 struct die_info *parent)
10991{
10992 struct die_info *first_die, *last_sibling;
fe1b8b76 10993 gdb_byte *cur_ptr;
639d11d3 10994
c906108c 10995 cur_ptr = info_ptr;
639d11d3
DC
10996 first_die = last_sibling = NULL;
10997
10998 while (1)
c906108c 10999 {
639d11d3 11000 struct die_info *die
dee91e82 11001 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 11002
1d325ec1 11003 if (die == NULL)
c906108c 11004 {
639d11d3
DC
11005 *new_info_ptr = cur_ptr;
11006 return first_die;
c906108c 11007 }
1d325ec1
DJ
11008
11009 if (!first_die)
11010 first_die = die;
c906108c 11011 else
1d325ec1
DJ
11012 last_sibling->sibling = die;
11013
11014 last_sibling = die;
c906108c 11015 }
c906108c
SS
11016}
11017
3019eac3
DE
11018/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
11019 attributes.
11020 The caller is responsible for filling in the extra attributes
11021 and updating (*DIEP)->num_attrs.
11022 Set DIEP to point to a newly allocated die with its information,
11023 except for its child, sibling, and parent fields.
11024 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
11025
11026static gdb_byte *
3019eac3
DE
11027read_full_die_1 (const struct die_reader_specs *reader,
11028 struct die_info **diep, gdb_byte *info_ptr,
11029 int *has_children, int num_extra_attrs)
93311388 11030{
b64f50a1
JK
11031 unsigned int abbrev_number, bytes_read, i;
11032 sect_offset offset;
93311388
DE
11033 struct abbrev_info *abbrev;
11034 struct die_info *die;
11035 struct dwarf2_cu *cu = reader->cu;
11036 bfd *abfd = reader->abfd;
11037
b64f50a1 11038 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
11039 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11040 info_ptr += bytes_read;
11041 if (!abbrev_number)
11042 {
11043 *diep = NULL;
11044 *has_children = 0;
11045 return info_ptr;
11046 }
11047
433df2d4 11048 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
93311388 11049 if (!abbrev)
348e048f
DE
11050 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
11051 abbrev_number,
11052 bfd_get_filename (abfd));
11053
3019eac3 11054 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
11055 die->offset = offset;
11056 die->tag = abbrev->tag;
11057 die->abbrev = abbrev_number;
11058
3019eac3
DE
11059 /* Make the result usable.
11060 The caller needs to update num_attrs after adding the extra
11061 attributes. */
93311388
DE
11062 die->num_attrs = abbrev->num_attrs;
11063
11064 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
11065 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
11066 info_ptr);
93311388
DE
11067
11068 *diep = die;
11069 *has_children = abbrev->has_children;
11070 return info_ptr;
11071}
11072
3019eac3
DE
11073/* Read a die and all its attributes.
11074 Set DIEP to point to a newly allocated die with its information,
11075 except for its child, sibling, and parent fields.
11076 Set HAS_CHILDREN to tell whether the die has children or not. */
11077
11078static gdb_byte *
11079read_full_die (const struct die_reader_specs *reader,
11080 struct die_info **diep, gdb_byte *info_ptr,
11081 int *has_children)
11082{
11083 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
11084}
433df2d4
DE
11085\f
11086/* Abbreviation tables.
3019eac3 11087
433df2d4 11088 In DWARF version 2, the description of the debugging information is
c906108c
SS
11089 stored in a separate .debug_abbrev section. Before we read any
11090 dies from a section we read in all abbreviations and install them
433df2d4
DE
11091 in a hash table. */
11092
11093/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
11094
11095static struct abbrev_info *
11096abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
11097{
11098 struct abbrev_info *abbrev;
11099
11100 abbrev = (struct abbrev_info *)
11101 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
11102 memset (abbrev, 0, sizeof (struct abbrev_info));
11103 return abbrev;
11104}
11105
11106/* Add an abbreviation to the table. */
c906108c
SS
11107
11108static void
433df2d4
DE
11109abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
11110 unsigned int abbrev_number,
11111 struct abbrev_info *abbrev)
11112{
11113 unsigned int hash_number;
11114
11115 hash_number = abbrev_number % ABBREV_HASH_SIZE;
11116 abbrev->next = abbrev_table->abbrevs[hash_number];
11117 abbrev_table->abbrevs[hash_number] = abbrev;
11118}
dee91e82 11119
433df2d4
DE
11120/* Look up an abbrev in the table.
11121 Returns NULL if the abbrev is not found. */
11122
11123static struct abbrev_info *
11124abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
11125 unsigned int abbrev_number)
c906108c 11126{
433df2d4
DE
11127 unsigned int hash_number;
11128 struct abbrev_info *abbrev;
11129
11130 hash_number = abbrev_number % ABBREV_HASH_SIZE;
11131 abbrev = abbrev_table->abbrevs[hash_number];
11132
11133 while (abbrev)
11134 {
11135 if (abbrev->number == abbrev_number)
11136 return abbrev;
11137 abbrev = abbrev->next;
11138 }
11139 return NULL;
11140}
11141
11142/* Read in an abbrev table. */
11143
11144static struct abbrev_table *
11145abbrev_table_read_table (struct dwarf2_section_info *section,
11146 sect_offset offset)
11147{
11148 struct objfile *objfile = dwarf2_per_objfile->objfile;
11149 bfd *abfd = section->asection->owner;
11150 struct abbrev_table *abbrev_table;
fe1b8b76 11151 gdb_byte *abbrev_ptr;
c906108c
SS
11152 struct abbrev_info *cur_abbrev;
11153 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 11154 unsigned int abbrev_form;
f3dd6933
DJ
11155 struct attr_abbrev *cur_attrs;
11156 unsigned int allocated_attrs;
c906108c 11157
433df2d4
DE
11158 abbrev_table = XMALLOC (struct abbrev_table);
11159 obstack_init (&abbrev_table->abbrev_obstack);
11160 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
11161 (ABBREV_HASH_SIZE
11162 * sizeof (struct abbrev_info *)));
11163 memset (abbrev_table->abbrevs, 0,
11164 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 11165
433df2d4
DE
11166 dwarf2_read_section (objfile, section);
11167 abbrev_ptr = section->buffer + offset.sect_off;
c906108c
SS
11168 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11169 abbrev_ptr += bytes_read;
11170
f3dd6933
DJ
11171 allocated_attrs = ATTR_ALLOC_CHUNK;
11172 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 11173
0963b4bd 11174 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
11175 while (abbrev_number)
11176 {
433df2d4 11177 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
c906108c
SS
11178
11179 /* read in abbrev header */
11180 cur_abbrev->number = abbrev_number;
11181 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11182 abbrev_ptr += bytes_read;
11183 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
11184 abbrev_ptr += 1;
11185
11186 /* now read in declarations */
11187 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11188 abbrev_ptr += bytes_read;
11189 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11190 abbrev_ptr += bytes_read;
11191 while (abbrev_name)
11192 {
f3dd6933 11193 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 11194 {
f3dd6933
DJ
11195 allocated_attrs += ATTR_ALLOC_CHUNK;
11196 cur_attrs
11197 = xrealloc (cur_attrs, (allocated_attrs
11198 * sizeof (struct attr_abbrev)));
c906108c 11199 }
ae038cb0 11200
f3dd6933
DJ
11201 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
11202 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
11203 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11204 abbrev_ptr += bytes_read;
11205 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11206 abbrev_ptr += bytes_read;
11207 }
11208
433df2d4 11209 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
f3dd6933
DJ
11210 (cur_abbrev->num_attrs
11211 * sizeof (struct attr_abbrev)));
11212 memcpy (cur_abbrev->attrs, cur_attrs,
11213 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
11214
433df2d4 11215 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
c906108c
SS
11216
11217 /* Get next abbreviation.
11218 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
11219 always properly terminated with an abbrev number of 0.
11220 Exit loop if we encounter an abbreviation which we have
11221 already read (which means we are about to read the abbreviations
11222 for the next compile unit) or if the end of the abbreviation
11223 table is reached. */
433df2d4 11224 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
11225 break;
11226 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
11227 abbrev_ptr += bytes_read;
433df2d4 11228 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
c906108c
SS
11229 break;
11230 }
f3dd6933
DJ
11231
11232 xfree (cur_attrs);
433df2d4 11233 return abbrev_table;
c906108c
SS
11234}
11235
433df2d4 11236/* Free the resources held by ABBREV_TABLE. */
c906108c 11237
c906108c 11238static void
433df2d4 11239abbrev_table_free (struct abbrev_table *abbrev_table)
c906108c 11240{
433df2d4
DE
11241 obstack_free (&abbrev_table->abbrev_obstack, NULL);
11242 xfree (abbrev_table);
c906108c
SS
11243}
11244
433df2d4
DE
11245/* Read the abbrev table for CU from ABBREV_SECTION. */
11246
11247static void
11248dwarf2_read_abbrevs (struct dwarf2_cu *cu,
11249 struct dwarf2_section_info *abbrev_section)
c906108c 11250{
433df2d4
DE
11251 cu->abbrev_table =
11252 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
11253}
c906108c 11254
433df2d4 11255/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 11256
433df2d4
DE
11257static void
11258dwarf2_free_abbrev_table (void *ptr_to_cu)
11259{
11260 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 11261
433df2d4
DE
11262 abbrev_table_free (cu->abbrev_table);
11263 /* Set this to NULL so that we SEGV if we try to read it later,
11264 and also because free_comp_unit verifies this is NULL. */
11265 cu->abbrev_table = NULL;
11266}
11267\f
72bf9492
DJ
11268/* Returns nonzero if TAG represents a type that we might generate a partial
11269 symbol for. */
11270
11271static int
11272is_type_tag_for_partial (int tag)
11273{
11274 switch (tag)
11275 {
11276#if 0
11277 /* Some types that would be reasonable to generate partial symbols for,
11278 that we don't at present. */
11279 case DW_TAG_array_type:
11280 case DW_TAG_file_type:
11281 case DW_TAG_ptr_to_member_type:
11282 case DW_TAG_set_type:
11283 case DW_TAG_string_type:
11284 case DW_TAG_subroutine_type:
11285#endif
11286 case DW_TAG_base_type:
11287 case DW_TAG_class_type:
680b30c7 11288 case DW_TAG_interface_type:
72bf9492
DJ
11289 case DW_TAG_enumeration_type:
11290 case DW_TAG_structure_type:
11291 case DW_TAG_subrange_type:
11292 case DW_TAG_typedef:
11293 case DW_TAG_union_type:
11294 return 1;
11295 default:
11296 return 0;
11297 }
11298}
11299
11300/* Load all DIEs that are interesting for partial symbols into memory. */
11301
11302static struct partial_die_info *
dee91e82
DE
11303load_partial_dies (const struct die_reader_specs *reader,
11304 gdb_byte *info_ptr, int building_psymtab)
72bf9492 11305{
dee91e82 11306 struct dwarf2_cu *cu = reader->cu;
bb5ed363 11307 struct objfile *objfile = cu->objfile;
72bf9492
DJ
11308 struct partial_die_info *part_die;
11309 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
11310 struct abbrev_info *abbrev;
11311 unsigned int bytes_read;
5afb4e99 11312 unsigned int load_all = 0;
72bf9492
DJ
11313 int nesting_level = 1;
11314
11315 parent_die = NULL;
11316 last_die = NULL;
11317
7adf1e79
DE
11318 gdb_assert (cu->per_cu != NULL);
11319 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
11320 load_all = 1;
11321
72bf9492
DJ
11322 cu->partial_dies
11323 = htab_create_alloc_ex (cu->header.length / 12,
11324 partial_die_hash,
11325 partial_die_eq,
11326 NULL,
11327 &cu->comp_unit_obstack,
11328 hashtab_obstack_allocate,
11329 dummy_obstack_deallocate);
11330
11331 part_die = obstack_alloc (&cu->comp_unit_obstack,
11332 sizeof (struct partial_die_info));
11333
11334 while (1)
11335 {
11336 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
11337
11338 /* A NULL abbrev means the end of a series of children. */
11339 if (abbrev == NULL)
11340 {
11341 if (--nesting_level == 0)
11342 {
11343 /* PART_DIE was probably the last thing allocated on the
11344 comp_unit_obstack, so we could call obstack_free
11345 here. We don't do that because the waste is small,
11346 and will be cleaned up when we're done with this
11347 compilation unit. This way, we're also more robust
11348 against other users of the comp_unit_obstack. */
11349 return first_die;
11350 }
11351 info_ptr += bytes_read;
11352 last_die = parent_die;
11353 parent_die = parent_die->die_parent;
11354 continue;
11355 }
11356
98bfdba5
PA
11357 /* Check for template arguments. We never save these; if
11358 they're seen, we just mark the parent, and go on our way. */
11359 if (parent_die != NULL
11360 && cu->language == language_cplus
11361 && (abbrev->tag == DW_TAG_template_type_param
11362 || abbrev->tag == DW_TAG_template_value_param))
11363 {
11364 parent_die->has_template_arguments = 1;
11365
11366 if (!load_all)
11367 {
11368 /* We don't need a partial DIE for the template argument. */
dee91e82 11369 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
11370 continue;
11371 }
11372 }
11373
0d99eb77 11374 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
11375 Skip their other children. */
11376 if (!load_all
11377 && cu->language == language_cplus
11378 && parent_die != NULL
11379 && parent_die->tag == DW_TAG_subprogram)
11380 {
dee91e82 11381 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
11382 continue;
11383 }
11384
5afb4e99
DJ
11385 /* Check whether this DIE is interesting enough to save. Normally
11386 we would not be interested in members here, but there may be
11387 later variables referencing them via DW_AT_specification (for
11388 static members). */
11389 if (!load_all
11390 && !is_type_tag_for_partial (abbrev->tag)
72929c62 11391 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
11392 && abbrev->tag != DW_TAG_enumerator
11393 && abbrev->tag != DW_TAG_subprogram
bc30ff58 11394 && abbrev->tag != DW_TAG_lexical_block
72bf9492 11395 && abbrev->tag != DW_TAG_variable
5afb4e99 11396 && abbrev->tag != DW_TAG_namespace
f55ee35c 11397 && abbrev->tag != DW_TAG_module
95554aad
TT
11398 && abbrev->tag != DW_TAG_member
11399 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
11400 {
11401 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11402 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
11403 continue;
11404 }
11405
dee91e82
DE
11406 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
11407 info_ptr);
72bf9492
DJ
11408
11409 /* This two-pass algorithm for processing partial symbols has a
11410 high cost in cache pressure. Thus, handle some simple cases
11411 here which cover the majority of C partial symbols. DIEs
11412 which neither have specification tags in them, nor could have
11413 specification tags elsewhere pointing at them, can simply be
11414 processed and discarded.
11415
11416 This segment is also optional; scan_partial_symbols and
11417 add_partial_symbol will handle these DIEs if we chain
11418 them in normally. When compilers which do not emit large
11419 quantities of duplicate debug information are more common,
11420 this code can probably be removed. */
11421
11422 /* Any complete simple types at the top level (pretty much all
11423 of them, for a language without namespaces), can be processed
11424 directly. */
11425 if (parent_die == NULL
11426 && part_die->has_specification == 0
11427 && part_die->is_declaration == 0
d8228535 11428 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
11429 || part_die->tag == DW_TAG_base_type
11430 || part_die->tag == DW_TAG_subrange_type))
11431 {
11432 if (building_psymtab && part_die->name != NULL)
04a679b8 11433 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11434 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
11435 &objfile->static_psymbols,
11436 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 11437 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11438 continue;
11439 }
11440
d8228535
JK
11441 /* The exception for DW_TAG_typedef with has_children above is
11442 a workaround of GCC PR debug/47510. In the case of this complaint
11443 type_name_no_tag_or_error will error on such types later.
11444
11445 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11446 it could not find the child DIEs referenced later, this is checked
11447 above. In correct DWARF DW_TAG_typedef should have no children. */
11448
11449 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11450 complaint (&symfile_complaints,
11451 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11452 "- DIE at 0x%x [in module %s]"),
b64f50a1 11453 part_die->offset.sect_off, objfile->name);
d8228535 11454
72bf9492
DJ
11455 /* If we're at the second level, and we're an enumerator, and
11456 our parent has no specification (meaning possibly lives in a
11457 namespace elsewhere), then we can add the partial symbol now
11458 instead of queueing it. */
11459 if (part_die->tag == DW_TAG_enumerator
11460 && parent_die != NULL
11461 && parent_die->die_parent == NULL
11462 && parent_die->tag == DW_TAG_enumeration_type
11463 && parent_die->has_specification == 0)
11464 {
11465 if (part_die->name == NULL)
3e43a32a
MS
11466 complaint (&symfile_complaints,
11467 _("malformed enumerator DIE ignored"));
72bf9492 11468 else if (building_psymtab)
04a679b8 11469 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11470 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
11471 (cu->language == language_cplus
11472 || cu->language == language_java)
bb5ed363
DE
11473 ? &objfile->global_psymbols
11474 : &objfile->static_psymbols,
11475 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 11476
dee91e82 11477 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11478 continue;
11479 }
11480
11481 /* We'll save this DIE so link it in. */
11482 part_die->die_parent = parent_die;
11483 part_die->die_sibling = NULL;
11484 part_die->die_child = NULL;
11485
11486 if (last_die && last_die == parent_die)
11487 last_die->die_child = part_die;
11488 else if (last_die)
11489 last_die->die_sibling = part_die;
11490
11491 last_die = part_die;
11492
11493 if (first_die == NULL)
11494 first_die = part_die;
11495
11496 /* Maybe add the DIE to the hash table. Not all DIEs that we
11497 find interesting need to be in the hash table, because we
11498 also have the parent/sibling/child chains; only those that we
11499 might refer to by offset later during partial symbol reading.
11500
11501 For now this means things that might have be the target of a
11502 DW_AT_specification, DW_AT_abstract_origin, or
11503 DW_AT_extension. DW_AT_extension will refer only to
11504 namespaces; DW_AT_abstract_origin refers to functions (and
11505 many things under the function DIE, but we do not recurse
11506 into function DIEs during partial symbol reading) and
11507 possibly variables as well; DW_AT_specification refers to
11508 declarations. Declarations ought to have the DW_AT_declaration
11509 flag. It happens that GCC forgets to put it in sometimes, but
11510 only for functions, not for types.
11511
11512 Adding more things than necessary to the hash table is harmless
11513 except for the performance cost. Adding too few will result in
5afb4e99
DJ
11514 wasted time in find_partial_die, when we reread the compilation
11515 unit with load_all_dies set. */
72bf9492 11516
5afb4e99 11517 if (load_all
72929c62 11518 || abbrev->tag == DW_TAG_constant
5afb4e99 11519 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
11520 || abbrev->tag == DW_TAG_variable
11521 || abbrev->tag == DW_TAG_namespace
11522 || part_die->is_declaration)
11523 {
11524 void **slot;
11525
11526 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 11527 part_die->offset.sect_off, INSERT);
72bf9492
DJ
11528 *slot = part_die;
11529 }
11530
11531 part_die = obstack_alloc (&cu->comp_unit_obstack,
11532 sizeof (struct partial_die_info));
11533
11534 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 11535 we have no reason to follow the children of structures; for other
98bfdba5
PA
11536 languages we have to, so that we can get at method physnames
11537 to infer fully qualified class names, for DW_AT_specification,
11538 and for C++ template arguments. For C++, we also look one level
11539 inside functions to find template arguments (if the name of the
11540 function does not already contain the template arguments).
bc30ff58
JB
11541
11542 For Ada, we need to scan the children of subprograms and lexical
11543 blocks as well because Ada allows the definition of nested
11544 entities that could be interesting for the debugger, such as
11545 nested subprograms for instance. */
72bf9492 11546 if (last_die->has_children
5afb4e99
DJ
11547 && (load_all
11548 || last_die->tag == DW_TAG_namespace
f55ee35c 11549 || last_die->tag == DW_TAG_module
72bf9492 11550 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
11551 || (cu->language == language_cplus
11552 && last_die->tag == DW_TAG_subprogram
11553 && (last_die->name == NULL
11554 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
11555 || (cu->language != language_c
11556 && (last_die->tag == DW_TAG_class_type
680b30c7 11557 || last_die->tag == DW_TAG_interface_type
72bf9492 11558 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
11559 || last_die->tag == DW_TAG_union_type))
11560 || (cu->language == language_ada
11561 && (last_die->tag == DW_TAG_subprogram
11562 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
11563 {
11564 nesting_level++;
11565 parent_die = last_die;
11566 continue;
11567 }
11568
11569 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11570 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
11571
11572 /* Back to the top, do it again. */
11573 }
11574}
11575
c906108c
SS
11576/* Read a minimal amount of information into the minimal die structure. */
11577
fe1b8b76 11578static gdb_byte *
dee91e82
DE
11579read_partial_die (const struct die_reader_specs *reader,
11580 struct partial_die_info *part_die,
11581 struct abbrev_info *abbrev, unsigned int abbrev_len,
11582 gdb_byte *info_ptr)
c906108c 11583{
dee91e82 11584 struct dwarf2_cu *cu = reader->cu;
bb5ed363 11585 struct objfile *objfile = cu->objfile;
dee91e82 11586 gdb_byte *buffer = reader->buffer;
fa238c03 11587 unsigned int i;
c906108c 11588 struct attribute attr;
c5aa993b 11589 int has_low_pc_attr = 0;
c906108c 11590 int has_high_pc_attr = 0;
91da1414 11591 int high_pc_relative = 0;
c906108c 11592
72bf9492 11593 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 11594
b64f50a1 11595 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
11596
11597 info_ptr += abbrev_len;
11598
11599 if (abbrev == NULL)
11600 return info_ptr;
11601
c906108c
SS
11602 part_die->tag = abbrev->tag;
11603 part_die->has_children = abbrev->has_children;
c906108c
SS
11604
11605 for (i = 0; i < abbrev->num_attrs; ++i)
11606 {
dee91e82 11607 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
11608
11609 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 11610 partial symbol table. */
c906108c
SS
11611 switch (attr.name)
11612 {
11613 case DW_AT_name:
71c25dea
TT
11614 switch (part_die->tag)
11615 {
11616 case DW_TAG_compile_unit:
95554aad 11617 case DW_TAG_partial_unit:
348e048f 11618 case DW_TAG_type_unit:
71c25dea
TT
11619 /* Compilation units have a DW_AT_name that is a filename, not
11620 a source language identifier. */
11621 case DW_TAG_enumeration_type:
11622 case DW_TAG_enumerator:
11623 /* These tags always have simple identifiers already; no need
11624 to canonicalize them. */
11625 part_die->name = DW_STRING (&attr);
11626 break;
11627 default:
11628 part_die->name
11629 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 11630 &objfile->objfile_obstack);
71c25dea
TT
11631 break;
11632 }
c906108c 11633 break;
31ef98ae 11634 case DW_AT_linkage_name:
c906108c 11635 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
11636 /* Note that both forms of linkage name might appear. We
11637 assume they will be the same, and we only store the last
11638 one we see. */
94af9270
KS
11639 if (cu->language == language_ada)
11640 part_die->name = DW_STRING (&attr);
abc72ce4 11641 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
11642 break;
11643 case DW_AT_low_pc:
11644 has_low_pc_attr = 1;
11645 part_die->lowpc = DW_ADDR (&attr);
11646 break;
11647 case DW_AT_high_pc:
11648 has_high_pc_attr = 1;
3019eac3
DE
11649 if (attr.form == DW_FORM_addr
11650 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
11651 part_die->highpc = DW_ADDR (&attr);
11652 else
11653 {
11654 high_pc_relative = 1;
11655 part_die->highpc = DW_UNSND (&attr);
11656 }
c906108c
SS
11657 break;
11658 case DW_AT_location:
0963b4bd 11659 /* Support the .debug_loc offsets. */
8e19ed76
PS
11660 if (attr_form_is_block (&attr))
11661 {
95554aad 11662 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 11663 }
3690dd37 11664 else if (attr_form_is_section_offset (&attr))
8e19ed76 11665 {
4d3c2250 11666 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11667 }
11668 else
11669 {
4d3c2250
KB
11670 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11671 "partial symbol information");
8e19ed76 11672 }
c906108c 11673 break;
c906108c
SS
11674 case DW_AT_external:
11675 part_die->is_external = DW_UNSND (&attr);
11676 break;
11677 case DW_AT_declaration:
11678 part_die->is_declaration = DW_UNSND (&attr);
11679 break;
11680 case DW_AT_type:
11681 part_die->has_type = 1;
11682 break;
11683 case DW_AT_abstract_origin:
11684 case DW_AT_specification:
72bf9492
DJ
11685 case DW_AT_extension:
11686 part_die->has_specification = 1;
c764a876 11687 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
11688 break;
11689 case DW_AT_sibling:
11690 /* Ignore absolute siblings, they might point outside of
11691 the current compile unit. */
11692 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
11693 complaint (&symfile_complaints,
11694 _("ignoring absolute DW_AT_sibling"));
c906108c 11695 else
b64f50a1 11696 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 11697 break;
fa4028e9
JB
11698 case DW_AT_byte_size:
11699 part_die->has_byte_size = 1;
11700 break;
68511cec
CES
11701 case DW_AT_calling_convention:
11702 /* DWARF doesn't provide a way to identify a program's source-level
11703 entry point. DW_AT_calling_convention attributes are only meant
11704 to describe functions' calling conventions.
11705
11706 However, because it's a necessary piece of information in
11707 Fortran, and because DW_CC_program is the only piece of debugging
11708 information whose definition refers to a 'main program' at all,
11709 several compilers have begun marking Fortran main programs with
11710 DW_CC_program --- even when those functions use the standard
11711 calling conventions.
11712
11713 So until DWARF specifies a way to provide this information and
11714 compilers pick up the new representation, we'll support this
11715 practice. */
11716 if (DW_UNSND (&attr) == DW_CC_program
11717 && cu->language == language_fortran)
01f8c46d
JK
11718 {
11719 set_main_name (part_die->name);
11720
11721 /* As this DIE has a static linkage the name would be difficult
11722 to look up later. */
11723 language_of_main = language_fortran;
11724 }
68511cec 11725 break;
481860b3
GB
11726 case DW_AT_inline:
11727 if (DW_UNSND (&attr) == DW_INL_inlined
11728 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11729 part_die->may_be_inlined = 1;
11730 break;
95554aad
TT
11731
11732 case DW_AT_import:
11733 if (part_die->tag == DW_TAG_imported_unit)
11734 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11735 break;
11736
c906108c
SS
11737 default:
11738 break;
11739 }
11740 }
11741
91da1414
MW
11742 if (high_pc_relative)
11743 part_die->highpc += part_die->lowpc;
11744
9373cf26
JK
11745 if (has_low_pc_attr && has_high_pc_attr)
11746 {
11747 /* When using the GNU linker, .gnu.linkonce. sections are used to
11748 eliminate duplicate copies of functions and vtables and such.
11749 The linker will arbitrarily choose one and discard the others.
11750 The AT_*_pc values for such functions refer to local labels in
11751 these sections. If the section from that file was discarded, the
11752 labels are not in the output, so the relocs get a value of 0.
11753 If this is a discarded function, mark the pc bounds as invalid,
11754 so that GDB will ignore it. */
11755 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11756 {
bb5ed363 11757 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11758
11759 complaint (&symfile_complaints,
11760 _("DW_AT_low_pc %s is zero "
11761 "for DIE at 0x%x [in module %s]"),
11762 paddress (gdbarch, part_die->lowpc),
b64f50a1 11763 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11764 }
11765 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11766 else if (part_die->lowpc >= part_die->highpc)
11767 {
bb5ed363 11768 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11769
11770 complaint (&symfile_complaints,
11771 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11772 "for DIE at 0x%x [in module %s]"),
11773 paddress (gdbarch, part_die->lowpc),
11774 paddress (gdbarch, part_die->highpc),
b64f50a1 11775 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11776 }
11777 else
11778 part_die->has_pc_info = 1;
11779 }
85cbf3d3 11780
c906108c
SS
11781 return info_ptr;
11782}
11783
72bf9492
DJ
11784/* Find a cached partial DIE at OFFSET in CU. */
11785
11786static struct partial_die_info *
b64f50a1 11787find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
11788{
11789 struct partial_die_info *lookup_die = NULL;
11790 struct partial_die_info part_die;
11791
11792 part_die.offset = offset;
b64f50a1
JK
11793 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11794 offset.sect_off);
72bf9492 11795
72bf9492
DJ
11796 return lookup_die;
11797}
11798
348e048f
DE
11799/* Find a partial DIE at OFFSET, which may or may not be in CU,
11800 except in the case of .debug_types DIEs which do not reference
11801 outside their CU (they do however referencing other types via
55f1336d 11802 DW_FORM_ref_sig8). */
72bf9492
DJ
11803
11804static struct partial_die_info *
b64f50a1 11805find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
72bf9492 11806{
bb5ed363 11807 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
11808 struct dwarf2_per_cu_data *per_cu = NULL;
11809 struct partial_die_info *pd = NULL;
72bf9492 11810
45452591 11811 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
11812 {
11813 pd = find_partial_die_in_comp_unit (offset, cu);
11814 if (pd != NULL)
11815 return pd;
0d99eb77
DE
11816 /* We missed recording what we needed.
11817 Load all dies and try again. */
11818 per_cu = cu->per_cu;
5afb4e99 11819 }
0d99eb77
DE
11820 else
11821 {
11822 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 11823 if (cu->per_cu->is_debug_types)
0d99eb77
DE
11824 {
11825 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11826 " external reference to offset 0x%lx [in module %s].\n"),
11827 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11828 bfd_get_filename (objfile->obfd));
11829 }
11830 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
72bf9492 11831
0d99eb77
DE
11832 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11833 load_partial_comp_unit (per_cu);
ae038cb0 11834
0d99eb77
DE
11835 per_cu->cu->last_used = 0;
11836 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11837 }
5afb4e99 11838
dee91e82
DE
11839 /* If we didn't find it, and not all dies have been loaded,
11840 load them all and try again. */
11841
5afb4e99
DJ
11842 if (pd == NULL && per_cu->load_all_dies == 0)
11843 {
5afb4e99 11844 per_cu->load_all_dies = 1;
fd820528
DE
11845
11846 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11847 THIS_CU->cu may already be in use. So we can't just free it and
11848 replace its DIEs with the ones we read in. Instead, we leave those
11849 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11850 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11851 set. */
dee91e82 11852 load_partial_comp_unit (per_cu);
5afb4e99
DJ
11853
11854 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11855 }
11856
11857 if (pd == NULL)
11858 internal_error (__FILE__, __LINE__,
3e43a32a
MS
11859 _("could not find partial DIE 0x%x "
11860 "in cache [from module %s]\n"),
b64f50a1 11861 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 11862 return pd;
72bf9492
DJ
11863}
11864
abc72ce4
DE
11865/* See if we can figure out if the class lives in a namespace. We do
11866 this by looking for a member function; its demangled name will
11867 contain namespace info, if there is any. */
11868
11869static void
11870guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11871 struct dwarf2_cu *cu)
11872{
11873 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11874 what template types look like, because the demangler
11875 frequently doesn't give the same name as the debug info. We
11876 could fix this by only using the demangled name to get the
11877 prefix (but see comment in read_structure_type). */
11878
11879 struct partial_die_info *real_pdi;
11880 struct partial_die_info *child_pdi;
11881
11882 /* If this DIE (this DIE's specification, if any) has a parent, then
11883 we should not do this. We'll prepend the parent's fully qualified
11884 name when we create the partial symbol. */
11885
11886 real_pdi = struct_pdi;
11887 while (real_pdi->has_specification)
11888 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11889
11890 if (real_pdi->die_parent != NULL)
11891 return;
11892
11893 for (child_pdi = struct_pdi->die_child;
11894 child_pdi != NULL;
11895 child_pdi = child_pdi->die_sibling)
11896 {
11897 if (child_pdi->tag == DW_TAG_subprogram
11898 && child_pdi->linkage_name != NULL)
11899 {
11900 char *actual_class_name
11901 = language_class_name_from_physname (cu->language_defn,
11902 child_pdi->linkage_name);
11903 if (actual_class_name != NULL)
11904 {
11905 struct_pdi->name
11906 = obsavestring (actual_class_name,
11907 strlen (actual_class_name),
11908 &cu->objfile->objfile_obstack);
11909 xfree (actual_class_name);
11910 }
11911 break;
11912 }
11913 }
11914}
11915
72bf9492
DJ
11916/* Adjust PART_DIE before generating a symbol for it. This function
11917 may set the is_external flag or change the DIE's name. */
11918
11919static void
11920fixup_partial_die (struct partial_die_info *part_die,
11921 struct dwarf2_cu *cu)
11922{
abc72ce4
DE
11923 /* Once we've fixed up a die, there's no point in doing so again.
11924 This also avoids a memory leak if we were to call
11925 guess_partial_die_structure_name multiple times. */
11926 if (part_die->fixup_called)
11927 return;
11928
72bf9492
DJ
11929 /* If we found a reference attribute and the DIE has no name, try
11930 to find a name in the referred to DIE. */
11931
11932 if (part_die->name == NULL && part_die->has_specification)
11933 {
11934 struct partial_die_info *spec_die;
72bf9492 11935
10b3939b 11936 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 11937
10b3939b 11938 fixup_partial_die (spec_die, cu);
72bf9492
DJ
11939
11940 if (spec_die->name)
11941 {
11942 part_die->name = spec_die->name;
11943
11944 /* Copy DW_AT_external attribute if it is set. */
11945 if (spec_die->is_external)
11946 part_die->is_external = spec_die->is_external;
11947 }
11948 }
11949
11950 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
11951
11952 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 11953 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 11954
abc72ce4
DE
11955 /* If there is no parent die to provide a namespace, and there are
11956 children, see if we can determine the namespace from their linkage
122d1940 11957 name. */
abc72ce4 11958 if (cu->language == language_cplus
8b70b953 11959 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
11960 && part_die->die_parent == NULL
11961 && part_die->has_children
11962 && (part_die->tag == DW_TAG_class_type
11963 || part_die->tag == DW_TAG_structure_type
11964 || part_die->tag == DW_TAG_union_type))
11965 guess_partial_die_structure_name (part_die, cu);
11966
53832f31
TT
11967 /* GCC might emit a nameless struct or union that has a linkage
11968 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11969 if (part_die->name == NULL
96408a79
SA
11970 && (part_die->tag == DW_TAG_class_type
11971 || part_die->tag == DW_TAG_interface_type
11972 || part_die->tag == DW_TAG_structure_type
11973 || part_die->tag == DW_TAG_union_type)
53832f31
TT
11974 && part_die->linkage_name != NULL)
11975 {
11976 char *demangled;
11977
11978 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11979 if (demangled)
11980 {
96408a79
SA
11981 const char *base;
11982
11983 /* Strip any leading namespaces/classes, keep only the base name.
11984 DW_AT_name for named DIEs does not contain the prefixes. */
11985 base = strrchr (demangled, ':');
11986 if (base && base > demangled && base[-1] == ':')
11987 base++;
11988 else
11989 base = demangled;
11990
11991 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
11992 &cu->objfile->objfile_obstack);
11993 xfree (demangled);
11994 }
11995 }
11996
abc72ce4 11997 part_die->fixup_called = 1;
72bf9492
DJ
11998}
11999
a8329558 12000/* Read an attribute value described by an attribute form. */
c906108c 12001
fe1b8b76 12002static gdb_byte *
dee91e82
DE
12003read_attribute_value (const struct die_reader_specs *reader,
12004 struct attribute *attr, unsigned form,
12005 gdb_byte *info_ptr)
c906108c 12006{
dee91e82
DE
12007 struct dwarf2_cu *cu = reader->cu;
12008 bfd *abfd = reader->abfd;
e7c27a73 12009 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
12010 unsigned int bytes_read;
12011 struct dwarf_block *blk;
12012
a8329558
KW
12013 attr->form = form;
12014 switch (form)
c906108c 12015 {
c906108c 12016 case DW_FORM_ref_addr:
ae411497 12017 if (cu->header.version == 2)
4568ecf9 12018 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 12019 else
4568ecf9
DE
12020 DW_UNSND (attr) = read_offset (abfd, info_ptr,
12021 &cu->header, &bytes_read);
ae411497
TT
12022 info_ptr += bytes_read;
12023 break;
12024 case DW_FORM_addr:
e7c27a73 12025 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 12026 info_ptr += bytes_read;
c906108c
SS
12027 break;
12028 case DW_FORM_block2:
7b5a2f43 12029 blk = dwarf_alloc_block (cu);
c906108c
SS
12030 blk->size = read_2_bytes (abfd, info_ptr);
12031 info_ptr += 2;
12032 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12033 info_ptr += blk->size;
12034 DW_BLOCK (attr) = blk;
12035 break;
12036 case DW_FORM_block4:
7b5a2f43 12037 blk = dwarf_alloc_block (cu);
c906108c
SS
12038 blk->size = read_4_bytes (abfd, info_ptr);
12039 info_ptr += 4;
12040 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12041 info_ptr += blk->size;
12042 DW_BLOCK (attr) = blk;
12043 break;
12044 case DW_FORM_data2:
12045 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
12046 info_ptr += 2;
12047 break;
12048 case DW_FORM_data4:
12049 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
12050 info_ptr += 4;
12051 break;
12052 case DW_FORM_data8:
12053 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
12054 info_ptr += 8;
12055 break;
2dc7f7b3
TT
12056 case DW_FORM_sec_offset:
12057 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
12058 info_ptr += bytes_read;
12059 break;
c906108c 12060 case DW_FORM_string:
9b1c24c8 12061 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 12062 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
12063 info_ptr += bytes_read;
12064 break;
4bdf3d34
JJ
12065 case DW_FORM_strp:
12066 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
12067 &bytes_read);
8285870a 12068 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
12069 info_ptr += bytes_read;
12070 break;
2dc7f7b3 12071 case DW_FORM_exprloc:
c906108c 12072 case DW_FORM_block:
7b5a2f43 12073 blk = dwarf_alloc_block (cu);
c906108c
SS
12074 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12075 info_ptr += bytes_read;
12076 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12077 info_ptr += blk->size;
12078 DW_BLOCK (attr) = blk;
12079 break;
12080 case DW_FORM_block1:
7b5a2f43 12081 blk = dwarf_alloc_block (cu);
c906108c
SS
12082 blk->size = read_1_byte (abfd, info_ptr);
12083 info_ptr += 1;
12084 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
12085 info_ptr += blk->size;
12086 DW_BLOCK (attr) = blk;
12087 break;
12088 case DW_FORM_data1:
12089 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
12090 info_ptr += 1;
12091 break;
12092 case DW_FORM_flag:
12093 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
12094 info_ptr += 1;
12095 break;
2dc7f7b3
TT
12096 case DW_FORM_flag_present:
12097 DW_UNSND (attr) = 1;
12098 break;
c906108c
SS
12099 case DW_FORM_sdata:
12100 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
12101 info_ptr += bytes_read;
12102 break;
12103 case DW_FORM_udata:
12104 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12105 info_ptr += bytes_read;
12106 break;
12107 case DW_FORM_ref1:
4568ecf9
DE
12108 DW_UNSND (attr) = (cu->header.offset.sect_off
12109 + read_1_byte (abfd, info_ptr));
c906108c
SS
12110 info_ptr += 1;
12111 break;
12112 case DW_FORM_ref2:
4568ecf9
DE
12113 DW_UNSND (attr) = (cu->header.offset.sect_off
12114 + read_2_bytes (abfd, info_ptr));
c906108c
SS
12115 info_ptr += 2;
12116 break;
12117 case DW_FORM_ref4:
4568ecf9
DE
12118 DW_UNSND (attr) = (cu->header.offset.sect_off
12119 + read_4_bytes (abfd, info_ptr));
c906108c
SS
12120 info_ptr += 4;
12121 break;
613e1657 12122 case DW_FORM_ref8:
4568ecf9
DE
12123 DW_UNSND (attr) = (cu->header.offset.sect_off
12124 + read_8_bytes (abfd, info_ptr));
613e1657
KB
12125 info_ptr += 8;
12126 break;
55f1336d 12127 case DW_FORM_ref_sig8:
348e048f
DE
12128 /* Convert the signature to something we can record in DW_UNSND
12129 for later lookup.
12130 NOTE: This is NULL if the type wasn't found. */
12131 DW_SIGNATURED_TYPE (attr) =
e319fa28 12132 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
12133 info_ptr += 8;
12134 break;
c906108c 12135 case DW_FORM_ref_udata:
4568ecf9
DE
12136 DW_UNSND (attr) = (cu->header.offset.sect_off
12137 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
12138 info_ptr += bytes_read;
12139 break;
c906108c 12140 case DW_FORM_indirect:
a8329558
KW
12141 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12142 info_ptr += bytes_read;
dee91e82 12143 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 12144 break;
3019eac3
DE
12145 case DW_FORM_GNU_addr_index:
12146 if (reader->dwo_file == NULL)
12147 {
12148 /* For now flag a hard error.
12149 Later we can turn this into a complaint. */
12150 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
12151 dwarf_form_name (form),
12152 bfd_get_filename (abfd));
12153 }
12154 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
12155 info_ptr += bytes_read;
12156 break;
12157 case DW_FORM_GNU_str_index:
12158 if (reader->dwo_file == NULL)
12159 {
12160 /* For now flag a hard error.
12161 Later we can turn this into a complaint if warranted. */
12162 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
12163 dwarf_form_name (form),
12164 bfd_get_filename (abfd));
12165 }
12166 {
12167 ULONGEST str_index =
12168 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
12169
12170 DW_STRING (attr) = read_str_index (reader, cu, str_index);
12171 DW_STRING_IS_CANONICAL (attr) = 0;
12172 info_ptr += bytes_read;
12173 }
12174 break;
c906108c 12175 default:
8a3fe4f8 12176 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
12177 dwarf_form_name (form),
12178 bfd_get_filename (abfd));
c906108c 12179 }
28e94949
JB
12180
12181 /* We have seen instances where the compiler tried to emit a byte
12182 size attribute of -1 which ended up being encoded as an unsigned
12183 0xffffffff. Although 0xffffffff is technically a valid size value,
12184 an object of this size seems pretty unlikely so we can relatively
12185 safely treat these cases as if the size attribute was invalid and
12186 treat them as zero by default. */
12187 if (attr->name == DW_AT_byte_size
12188 && form == DW_FORM_data4
12189 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
12190 {
12191 complaint
12192 (&symfile_complaints,
43bbcdc2
PH
12193 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
12194 hex_string (DW_UNSND (attr)));
01c66ae6
JB
12195 DW_UNSND (attr) = 0;
12196 }
28e94949 12197
c906108c
SS
12198 return info_ptr;
12199}
12200
a8329558
KW
12201/* Read an attribute described by an abbreviated attribute. */
12202
fe1b8b76 12203static gdb_byte *
dee91e82
DE
12204read_attribute (const struct die_reader_specs *reader,
12205 struct attribute *attr, struct attr_abbrev *abbrev,
12206 gdb_byte *info_ptr)
a8329558
KW
12207{
12208 attr->name = abbrev->name;
dee91e82 12209 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
12210}
12211
0963b4bd 12212/* Read dwarf information from a buffer. */
c906108c
SS
12213
12214static unsigned int
fe1b8b76 12215read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 12216{
fe1b8b76 12217 return bfd_get_8 (abfd, buf);
c906108c
SS
12218}
12219
12220static int
fe1b8b76 12221read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 12222{
fe1b8b76 12223 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
12224}
12225
12226static unsigned int
fe1b8b76 12227read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 12228{
fe1b8b76 12229 return bfd_get_16 (abfd, buf);
c906108c
SS
12230}
12231
21ae7a4d
JK
12232static int
12233read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
12234{
12235 return bfd_get_signed_16 (abfd, buf);
12236}
12237
c906108c 12238static unsigned int
fe1b8b76 12239read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 12240{
fe1b8b76 12241 return bfd_get_32 (abfd, buf);
c906108c
SS
12242}
12243
21ae7a4d
JK
12244static int
12245read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
12246{
12247 return bfd_get_signed_32 (abfd, buf);
12248}
12249
93311388 12250static ULONGEST
fe1b8b76 12251read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 12252{
fe1b8b76 12253 return bfd_get_64 (abfd, buf);
c906108c
SS
12254}
12255
12256static CORE_ADDR
fe1b8b76 12257read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 12258 unsigned int *bytes_read)
c906108c 12259{
e7c27a73 12260 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
12261 CORE_ADDR retval = 0;
12262
107d2387 12263 if (cu_header->signed_addr_p)
c906108c 12264 {
107d2387
AC
12265 switch (cu_header->addr_size)
12266 {
12267 case 2:
fe1b8b76 12268 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
12269 break;
12270 case 4:
fe1b8b76 12271 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
12272 break;
12273 case 8:
fe1b8b76 12274 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
12275 break;
12276 default:
8e65ff28 12277 internal_error (__FILE__, __LINE__,
e2e0b3e5 12278 _("read_address: bad switch, signed [in module %s]"),
659b0389 12279 bfd_get_filename (abfd));
107d2387
AC
12280 }
12281 }
12282 else
12283 {
12284 switch (cu_header->addr_size)
12285 {
12286 case 2:
fe1b8b76 12287 retval = bfd_get_16 (abfd, buf);
107d2387
AC
12288 break;
12289 case 4:
fe1b8b76 12290 retval = bfd_get_32 (abfd, buf);
107d2387
AC
12291 break;
12292 case 8:
fe1b8b76 12293 retval = bfd_get_64 (abfd, buf);
107d2387
AC
12294 break;
12295 default:
8e65ff28 12296 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
12297 _("read_address: bad switch, "
12298 "unsigned [in module %s]"),
659b0389 12299 bfd_get_filename (abfd));
107d2387 12300 }
c906108c 12301 }
64367e0a 12302
107d2387
AC
12303 *bytes_read = cu_header->addr_size;
12304 return retval;
c906108c
SS
12305}
12306
f7ef9339 12307/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
12308 specification allows the initial length to take up either 4 bytes
12309 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
12310 bytes describe the length and all offsets will be 8 bytes in length
12311 instead of 4.
12312
f7ef9339
KB
12313 An older, non-standard 64-bit format is also handled by this
12314 function. The older format in question stores the initial length
12315 as an 8-byte quantity without an escape value. Lengths greater
12316 than 2^32 aren't very common which means that the initial 4 bytes
12317 is almost always zero. Since a length value of zero doesn't make
12318 sense for the 32-bit format, this initial zero can be considered to
12319 be an escape value which indicates the presence of the older 64-bit
12320 format. As written, the code can't detect (old format) lengths
917c78fc
MK
12321 greater than 4GB. If it becomes necessary to handle lengths
12322 somewhat larger than 4GB, we could allow other small values (such
12323 as the non-sensical values of 1, 2, and 3) to also be used as
12324 escape values indicating the presence of the old format.
f7ef9339 12325
917c78fc
MK
12326 The value returned via bytes_read should be used to increment the
12327 relevant pointer after calling read_initial_length().
c764a876 12328
613e1657
KB
12329 [ Note: read_initial_length() and read_offset() are based on the
12330 document entitled "DWARF Debugging Information Format", revision
f7ef9339 12331 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
12332 from:
12333
f7ef9339 12334 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 12335
613e1657
KB
12336 This document is only a draft and is subject to change. (So beware.)
12337
f7ef9339 12338 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
12339 determined empirically by examining 64-bit ELF files produced by
12340 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
12341
12342 - Kevin, July 16, 2002
613e1657
KB
12343 ] */
12344
12345static LONGEST
c764a876 12346read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 12347{
fe1b8b76 12348 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 12349
dd373385 12350 if (length == 0xffffffff)
613e1657 12351 {
fe1b8b76 12352 length = bfd_get_64 (abfd, buf + 4);
613e1657 12353 *bytes_read = 12;
613e1657 12354 }
dd373385 12355 else if (length == 0)
f7ef9339 12356 {
dd373385 12357 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 12358 length = bfd_get_64 (abfd, buf);
f7ef9339 12359 *bytes_read = 8;
f7ef9339 12360 }
613e1657
KB
12361 else
12362 {
12363 *bytes_read = 4;
613e1657
KB
12364 }
12365
c764a876
DE
12366 return length;
12367}
dd373385 12368
c764a876
DE
12369/* Cover function for read_initial_length.
12370 Returns the length of the object at BUF, and stores the size of the
12371 initial length in *BYTES_READ and stores the size that offsets will be in
12372 *OFFSET_SIZE.
12373 If the initial length size is not equivalent to that specified in
12374 CU_HEADER then issue a complaint.
12375 This is useful when reading non-comp-unit headers. */
dd373385 12376
c764a876
DE
12377static LONGEST
12378read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
12379 const struct comp_unit_head *cu_header,
12380 unsigned int *bytes_read,
12381 unsigned int *offset_size)
12382{
12383 LONGEST length = read_initial_length (abfd, buf, bytes_read);
12384
12385 gdb_assert (cu_header->initial_length_size == 4
12386 || cu_header->initial_length_size == 8
12387 || cu_header->initial_length_size == 12);
12388
12389 if (cu_header->initial_length_size != *bytes_read)
12390 complaint (&symfile_complaints,
12391 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 12392
c764a876 12393 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 12394 return length;
613e1657
KB
12395}
12396
12397/* Read an offset from the data stream. The size of the offset is
917c78fc 12398 given by cu_header->offset_size. */
613e1657
KB
12399
12400static LONGEST
fe1b8b76 12401read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 12402 unsigned int *bytes_read)
c764a876
DE
12403{
12404 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 12405
c764a876
DE
12406 *bytes_read = cu_header->offset_size;
12407 return offset;
12408}
12409
12410/* Read an offset from the data stream. */
12411
12412static LONGEST
12413read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
12414{
12415 LONGEST retval = 0;
12416
c764a876 12417 switch (offset_size)
613e1657
KB
12418 {
12419 case 4:
fe1b8b76 12420 retval = bfd_get_32 (abfd, buf);
613e1657
KB
12421 break;
12422 case 8:
fe1b8b76 12423 retval = bfd_get_64 (abfd, buf);
613e1657
KB
12424 break;
12425 default:
8e65ff28 12426 internal_error (__FILE__, __LINE__,
c764a876 12427 _("read_offset_1: bad switch [in module %s]"),
659b0389 12428 bfd_get_filename (abfd));
613e1657
KB
12429 }
12430
917c78fc 12431 return retval;
613e1657
KB
12432}
12433
fe1b8b76
JB
12434static gdb_byte *
12435read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
12436{
12437 /* If the size of a host char is 8 bits, we can return a pointer
12438 to the buffer, otherwise we have to copy the data to a buffer
12439 allocated on the temporary obstack. */
4bdf3d34 12440 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 12441 return buf;
c906108c
SS
12442}
12443
12444static char *
9b1c24c8 12445read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
12446{
12447 /* If the size of a host char is 8 bits, we can return a pointer
12448 to the string, otherwise we have to copy the string to a buffer
12449 allocated on the temporary obstack. */
4bdf3d34 12450 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
12451 if (*buf == '\0')
12452 {
12453 *bytes_read_ptr = 1;
12454 return NULL;
12455 }
fe1b8b76
JB
12456 *bytes_read_ptr = strlen ((char *) buf) + 1;
12457 return (char *) buf;
4bdf3d34
JJ
12458}
12459
12460static char *
cf2c3c16 12461read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 12462{
be391dca 12463 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 12464 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
12465 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12466 bfd_get_filename (abfd));
dce234bc 12467 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
12468 error (_("DW_FORM_strp pointing outside of "
12469 ".debug_str section [in module %s]"),
12470 bfd_get_filename (abfd));
4bdf3d34 12471 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 12472 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 12473 return NULL;
dce234bc 12474 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
12475}
12476
cf2c3c16
TT
12477static char *
12478read_indirect_string (bfd *abfd, gdb_byte *buf,
12479 const struct comp_unit_head *cu_header,
12480 unsigned int *bytes_read_ptr)
12481{
12482 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12483
12484 return read_indirect_string_at_offset (abfd, str_offset);
12485}
12486
12df843f 12487static ULONGEST
fe1b8b76 12488read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12489{
12df843f 12490 ULONGEST result;
ce5d95e1 12491 unsigned int num_read;
c906108c
SS
12492 int i, shift;
12493 unsigned char byte;
12494
12495 result = 0;
12496 shift = 0;
12497 num_read = 0;
12498 i = 0;
12499 while (1)
12500 {
fe1b8b76 12501 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12502 buf++;
12503 num_read++;
12df843f 12504 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
12505 if ((byte & 128) == 0)
12506 {
12507 break;
12508 }
12509 shift += 7;
12510 }
12511 *bytes_read_ptr = num_read;
12512 return result;
12513}
12514
12df843f 12515static LONGEST
fe1b8b76 12516read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12517{
12df843f 12518 LONGEST result;
77e0b926 12519 int i, shift, num_read;
c906108c
SS
12520 unsigned char byte;
12521
12522 result = 0;
12523 shift = 0;
c906108c
SS
12524 num_read = 0;
12525 i = 0;
12526 while (1)
12527 {
fe1b8b76 12528 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12529 buf++;
12530 num_read++;
12df843f 12531 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
12532 shift += 7;
12533 if ((byte & 128) == 0)
12534 {
12535 break;
12536 }
12537 }
77e0b926 12538 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 12539 result |= -(((LONGEST) 1) << shift);
c906108c
SS
12540 *bytes_read_ptr = num_read;
12541 return result;
12542}
12543
3019eac3
DE
12544/* Given index ADDR_INDEX in .debug_addr, fetch the value.
12545 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12546 ADDR_SIZE is the size of addresses from the CU header. */
12547
12548static CORE_ADDR
12549read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12550{
12551 struct objfile *objfile = dwarf2_per_objfile->objfile;
12552 bfd *abfd = objfile->obfd;
12553 const gdb_byte *info_ptr;
12554
12555 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12556 if (dwarf2_per_objfile->addr.buffer == NULL)
12557 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12558 objfile->name);
12559 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12560 error (_("DW_FORM_addr_index pointing outside of "
12561 ".debug_addr section [in module %s]"),
12562 objfile->name);
12563 info_ptr = (dwarf2_per_objfile->addr.buffer
12564 + addr_base + addr_index * addr_size);
12565 if (addr_size == 4)
12566 return bfd_get_32 (abfd, info_ptr);
12567 else
12568 return bfd_get_64 (abfd, info_ptr);
12569}
12570
12571/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12572
12573static CORE_ADDR
12574read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12575{
12576 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12577}
12578
12579/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12580
12581static CORE_ADDR
12582read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12583 unsigned int *bytes_read)
12584{
12585 bfd *abfd = cu->objfile->obfd;
12586 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12587
12588 return read_addr_index (cu, addr_index);
12589}
12590
12591/* Data structure to pass results from dwarf2_read_addr_index_reader
12592 back to dwarf2_read_addr_index. */
12593
12594struct dwarf2_read_addr_index_data
12595{
12596 ULONGEST addr_base;
12597 int addr_size;
12598};
12599
12600/* die_reader_func for dwarf2_read_addr_index. */
12601
12602static void
12603dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12604 gdb_byte *info_ptr,
12605 struct die_info *comp_unit_die,
12606 int has_children,
12607 void *data)
12608{
12609 struct dwarf2_cu *cu = reader->cu;
12610 struct dwarf2_read_addr_index_data *aidata =
12611 (struct dwarf2_read_addr_index_data *) data;
12612
12613 aidata->addr_base = cu->addr_base;
12614 aidata->addr_size = cu->header.addr_size;
12615}
12616
12617/* Given an index in .debug_addr, fetch the value.
12618 NOTE: This can be called during dwarf expression evaluation,
12619 long after the debug information has been read, and thus per_cu->cu
12620 may no longer exist. */
12621
12622CORE_ADDR
12623dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12624 unsigned int addr_index)
12625{
12626 struct objfile *objfile = per_cu->objfile;
12627 struct dwarf2_cu *cu = per_cu->cu;
12628 ULONGEST addr_base;
12629 int addr_size;
12630
12631 /* This is intended to be called from outside this file. */
12632 dw2_setup (objfile);
12633
12634 /* We need addr_base and addr_size.
12635 If we don't have PER_CU->cu, we have to get it.
12636 Nasty, but the alternative is storing the needed info in PER_CU,
12637 which at this point doesn't seem justified: it's not clear how frequently
12638 it would get used and it would increase the size of every PER_CU.
12639 Entry points like dwarf2_per_cu_addr_size do a similar thing
12640 so we're not in uncharted territory here.
12641 Alas we need to be a bit more complicated as addr_base is contained
12642 in the DIE.
12643
12644 We don't need to read the entire CU(/TU).
12645 We just need the header and top level die.
12646 IWBN to use the aging mechanism to let us lazily later discard the CU.
12647 See however init_cutu_and_read_dies_simple. */
12648
12649 if (cu != NULL)
12650 {
12651 addr_base = cu->addr_base;
12652 addr_size = cu->header.addr_size;
12653 }
12654 else
12655 {
12656 struct dwarf2_read_addr_index_data aidata;
12657
12658 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12659 &aidata);
12660 addr_base = aidata.addr_base;
12661 addr_size = aidata.addr_size;
12662 }
12663
12664 return read_addr_index_1 (addr_index, addr_base, addr_size);
12665}
12666
12667/* Given a DW_AT_str_index, fetch the string. */
12668
12669static char *
12670read_str_index (const struct die_reader_specs *reader,
12671 struct dwarf2_cu *cu, ULONGEST str_index)
12672{
12673 struct objfile *objfile = dwarf2_per_objfile->objfile;
12674 const char *dwo_name = objfile->name;
12675 bfd *abfd = objfile->obfd;
12676 struct dwo_sections *sections = &reader->dwo_file->sections;
12677 gdb_byte *info_ptr;
12678 ULONGEST str_offset;
12679
12680 dwarf2_read_section (objfile, &sections->str);
12681 dwarf2_read_section (objfile, &sections->str_offsets);
12682 if (sections->str.buffer == NULL)
12683 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12684 " in CU at offset 0x%lx [in module %s]"),
12685 (long) cu->header.offset.sect_off, dwo_name);
12686 if (sections->str_offsets.buffer == NULL)
12687 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12688 " in CU at offset 0x%lx [in module %s]"),
12689 (long) cu->header.offset.sect_off, dwo_name);
12690 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12691 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12692 " section in CU at offset 0x%lx [in module %s]"),
12693 (long) cu->header.offset.sect_off, dwo_name);
12694 info_ptr = (sections->str_offsets.buffer
12695 + str_index * cu->header.offset_size);
12696 if (cu->header.offset_size == 4)
12697 str_offset = bfd_get_32 (abfd, info_ptr);
12698 else
12699 str_offset = bfd_get_64 (abfd, info_ptr);
12700 if (str_offset >= sections->str.size)
12701 error (_("Offset from DW_FORM_str_index pointing outside of"
12702 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12703 (long) cu->header.offset.sect_off, dwo_name);
12704 return (char *) (sections->str.buffer + str_offset);
12705}
12706
3019eac3
DE
12707/* Return the length of an LEB128 number in BUF. */
12708
12709static int
12710leb128_size (const gdb_byte *buf)
12711{
12712 const gdb_byte *begin = buf;
12713 gdb_byte byte;
12714
12715 while (1)
12716 {
12717 byte = *buf++;
12718 if ((byte & 128) == 0)
12719 return buf - begin;
12720 }
12721}
12722
c906108c 12723static void
e142c38c 12724set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
12725{
12726 switch (lang)
12727 {
12728 case DW_LANG_C89:
76bee0cc 12729 case DW_LANG_C99:
c906108c 12730 case DW_LANG_C:
e142c38c 12731 cu->language = language_c;
c906108c
SS
12732 break;
12733 case DW_LANG_C_plus_plus:
e142c38c 12734 cu->language = language_cplus;
c906108c 12735 break;
6aecb9c2
JB
12736 case DW_LANG_D:
12737 cu->language = language_d;
12738 break;
c906108c
SS
12739 case DW_LANG_Fortran77:
12740 case DW_LANG_Fortran90:
b21b22e0 12741 case DW_LANG_Fortran95:
e142c38c 12742 cu->language = language_fortran;
c906108c 12743 break;
a766d390
DE
12744 case DW_LANG_Go:
12745 cu->language = language_go;
12746 break;
c906108c 12747 case DW_LANG_Mips_Assembler:
e142c38c 12748 cu->language = language_asm;
c906108c 12749 break;
bebd888e 12750 case DW_LANG_Java:
e142c38c 12751 cu->language = language_java;
bebd888e 12752 break;
c906108c 12753 case DW_LANG_Ada83:
8aaf0b47 12754 case DW_LANG_Ada95:
bc5f45f8
JB
12755 cu->language = language_ada;
12756 break;
72019c9c
GM
12757 case DW_LANG_Modula2:
12758 cu->language = language_m2;
12759 break;
fe8e67fd
PM
12760 case DW_LANG_Pascal83:
12761 cu->language = language_pascal;
12762 break;
22566fbd
DJ
12763 case DW_LANG_ObjC:
12764 cu->language = language_objc;
12765 break;
c906108c
SS
12766 case DW_LANG_Cobol74:
12767 case DW_LANG_Cobol85:
c906108c 12768 default:
e142c38c 12769 cu->language = language_minimal;
c906108c
SS
12770 break;
12771 }
e142c38c 12772 cu->language_defn = language_def (cu->language);
c906108c
SS
12773}
12774
12775/* Return the named attribute or NULL if not there. */
12776
12777static struct attribute *
e142c38c 12778dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 12779{
a48e046c 12780 for (;;)
c906108c 12781 {
a48e046c
TT
12782 unsigned int i;
12783 struct attribute *spec = NULL;
12784
12785 for (i = 0; i < die->num_attrs; ++i)
12786 {
12787 if (die->attrs[i].name == name)
12788 return &die->attrs[i];
12789 if (die->attrs[i].name == DW_AT_specification
12790 || die->attrs[i].name == DW_AT_abstract_origin)
12791 spec = &die->attrs[i];
12792 }
12793
12794 if (!spec)
12795 break;
c906108c 12796
f2f0e013 12797 die = follow_die_ref (die, spec, &cu);
f2f0e013 12798 }
c5aa993b 12799
c906108c
SS
12800 return NULL;
12801}
12802
348e048f
DE
12803/* Return the named attribute or NULL if not there,
12804 but do not follow DW_AT_specification, etc.
12805 This is for use in contexts where we're reading .debug_types dies.
12806 Following DW_AT_specification, DW_AT_abstract_origin will take us
12807 back up the chain, and we want to go down. */
12808
12809static struct attribute *
12810dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12811 struct dwarf2_cu *cu)
12812{
12813 unsigned int i;
12814
12815 for (i = 0; i < die->num_attrs; ++i)
12816 if (die->attrs[i].name == name)
12817 return &die->attrs[i];
12818
12819 return NULL;
12820}
12821
05cf31d1
JB
12822/* Return non-zero iff the attribute NAME is defined for the given DIE,
12823 and holds a non-zero value. This function should only be used for
2dc7f7b3 12824 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
12825
12826static int
12827dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12828{
12829 struct attribute *attr = dwarf2_attr (die, name, cu);
12830
12831 return (attr && DW_UNSND (attr));
12832}
12833
3ca72b44 12834static int
e142c38c 12835die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 12836{
05cf31d1
JB
12837 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12838 which value is non-zero. However, we have to be careful with
12839 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12840 (via dwarf2_flag_true_p) follows this attribute. So we may
12841 end up accidently finding a declaration attribute that belongs
12842 to a different DIE referenced by the specification attribute,
12843 even though the given DIE does not have a declaration attribute. */
12844 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12845 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
12846}
12847
63d06c5c 12848/* Return the die giving the specification for DIE, if there is
f2f0e013 12849 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
12850 containing the return value on output. If there is no
12851 specification, but there is an abstract origin, that is
12852 returned. */
63d06c5c
DC
12853
12854static struct die_info *
f2f0e013 12855die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 12856{
f2f0e013
DJ
12857 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12858 *spec_cu);
63d06c5c 12859
edb3359d
DJ
12860 if (spec_attr == NULL)
12861 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12862
63d06c5c
DC
12863 if (spec_attr == NULL)
12864 return NULL;
12865 else
f2f0e013 12866 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 12867}
c906108c 12868
debd256d 12869/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
12870 refers to.
12871 NOTE: This is also used as a "cleanup" function. */
12872
debd256d
JB
12873static void
12874free_line_header (struct line_header *lh)
12875{
12876 if (lh->standard_opcode_lengths)
a8bc7b56 12877 xfree (lh->standard_opcode_lengths);
debd256d
JB
12878
12879 /* Remember that all the lh->file_names[i].name pointers are
12880 pointers into debug_line_buffer, and don't need to be freed. */
12881 if (lh->file_names)
a8bc7b56 12882 xfree (lh->file_names);
debd256d
JB
12883
12884 /* Similarly for the include directory names. */
12885 if (lh->include_dirs)
a8bc7b56 12886 xfree (lh->include_dirs);
debd256d 12887
a8bc7b56 12888 xfree (lh);
debd256d
JB
12889}
12890
debd256d 12891/* Add an entry to LH's include directory table. */
ae2de4f8 12892
debd256d
JB
12893static void
12894add_include_dir (struct line_header *lh, char *include_dir)
c906108c 12895{
debd256d
JB
12896 /* Grow the array if necessary. */
12897 if (lh->include_dirs_size == 0)
c5aa993b 12898 {
debd256d
JB
12899 lh->include_dirs_size = 1; /* for testing */
12900 lh->include_dirs = xmalloc (lh->include_dirs_size
12901 * sizeof (*lh->include_dirs));
12902 }
12903 else if (lh->num_include_dirs >= lh->include_dirs_size)
12904 {
12905 lh->include_dirs_size *= 2;
12906 lh->include_dirs = xrealloc (lh->include_dirs,
12907 (lh->include_dirs_size
12908 * sizeof (*lh->include_dirs)));
c5aa993b 12909 }
c906108c 12910
debd256d
JB
12911 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12912}
6e70227d 12913
debd256d 12914/* Add an entry to LH's file name table. */
ae2de4f8 12915
debd256d
JB
12916static void
12917add_file_name (struct line_header *lh,
12918 char *name,
12919 unsigned int dir_index,
12920 unsigned int mod_time,
12921 unsigned int length)
12922{
12923 struct file_entry *fe;
12924
12925 /* Grow the array if necessary. */
12926 if (lh->file_names_size == 0)
12927 {
12928 lh->file_names_size = 1; /* for testing */
12929 lh->file_names = xmalloc (lh->file_names_size
12930 * sizeof (*lh->file_names));
12931 }
12932 else if (lh->num_file_names >= lh->file_names_size)
12933 {
12934 lh->file_names_size *= 2;
12935 lh->file_names = xrealloc (lh->file_names,
12936 (lh->file_names_size
12937 * sizeof (*lh->file_names)));
12938 }
12939
12940 fe = &lh->file_names[lh->num_file_names++];
12941 fe->name = name;
12942 fe->dir_index = dir_index;
12943 fe->mod_time = mod_time;
12944 fe->length = length;
aaa75496 12945 fe->included_p = 0;
cb1df416 12946 fe->symtab = NULL;
debd256d 12947}
6e70227d 12948
debd256d 12949/* Read the statement program header starting at OFFSET in
3019eac3 12950 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 12951 to a struct line_header, allocated using xmalloc.
debd256d
JB
12952
12953 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
12954 the returned object point into the dwarf line section buffer,
12955 and must not be freed. */
ae2de4f8 12956
debd256d 12957static struct line_header *
3019eac3 12958dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
12959{
12960 struct cleanup *back_to;
12961 struct line_header *lh;
fe1b8b76 12962 gdb_byte *line_ptr;
c764a876 12963 unsigned int bytes_read, offset_size;
debd256d
JB
12964 int i;
12965 char *cur_dir, *cur_file;
3019eac3
DE
12966 struct dwarf2_section_info *section;
12967 bfd *abfd;
12968
12969 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12970 DWO file. */
12971 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12972 section = &cu->dwo_unit->dwo_file->sections.line;
12973 else
12974 section = &dwarf2_per_objfile->line;
debd256d 12975
3019eac3
DE
12976 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12977 if (section->buffer == NULL)
debd256d 12978 {
3019eac3
DE
12979 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12980 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12981 else
12982 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
12983 return 0;
12984 }
12985
fceca515
DE
12986 /* We can't do this until we know the section is non-empty.
12987 Only then do we know we have such a section. */
12988 abfd = section->asection->owner;
12989
a738430d
MK
12990 /* Make sure that at least there's room for the total_length field.
12991 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 12992 if (offset + 4 >= section->size)
debd256d 12993 {
4d3c2250 12994 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12995 return 0;
12996 }
12997
12998 lh = xmalloc (sizeof (*lh));
12999 memset (lh, 0, sizeof (*lh));
13000 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
13001 (void *) lh);
13002
3019eac3 13003 line_ptr = section->buffer + offset;
debd256d 13004
a738430d 13005 /* Read in the header. */
6e70227d 13006 lh->total_length =
c764a876
DE
13007 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
13008 &bytes_read, &offset_size);
debd256d 13009 line_ptr += bytes_read;
3019eac3 13010 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 13011 {
4d3c2250 13012 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
13013 return 0;
13014 }
13015 lh->statement_program_end = line_ptr + lh->total_length;
13016 lh->version = read_2_bytes (abfd, line_ptr);
13017 line_ptr += 2;
c764a876
DE
13018 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
13019 line_ptr += offset_size;
debd256d
JB
13020 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
13021 line_ptr += 1;
2dc7f7b3
TT
13022 if (lh->version >= 4)
13023 {
13024 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
13025 line_ptr += 1;
13026 }
13027 else
13028 lh->maximum_ops_per_instruction = 1;
13029
13030 if (lh->maximum_ops_per_instruction == 0)
13031 {
13032 lh->maximum_ops_per_instruction = 1;
13033 complaint (&symfile_complaints,
3e43a32a
MS
13034 _("invalid maximum_ops_per_instruction "
13035 "in `.debug_line' section"));
2dc7f7b3
TT
13036 }
13037
debd256d
JB
13038 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
13039 line_ptr += 1;
13040 lh->line_base = read_1_signed_byte (abfd, line_ptr);
13041 line_ptr += 1;
13042 lh->line_range = read_1_byte (abfd, line_ptr);
13043 line_ptr += 1;
13044 lh->opcode_base = read_1_byte (abfd, line_ptr);
13045 line_ptr += 1;
13046 lh->standard_opcode_lengths
fe1b8b76 13047 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
13048
13049 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
13050 for (i = 1; i < lh->opcode_base; ++i)
13051 {
13052 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
13053 line_ptr += 1;
13054 }
13055
a738430d 13056 /* Read directory table. */
9b1c24c8 13057 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
13058 {
13059 line_ptr += bytes_read;
13060 add_include_dir (lh, cur_dir);
13061 }
13062 line_ptr += bytes_read;
13063
a738430d 13064 /* Read file name table. */
9b1c24c8 13065 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
13066 {
13067 unsigned int dir_index, mod_time, length;
13068
13069 line_ptr += bytes_read;
13070 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13071 line_ptr += bytes_read;
13072 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13073 line_ptr += bytes_read;
13074 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13075 line_ptr += bytes_read;
13076
13077 add_file_name (lh, cur_file, dir_index, mod_time, length);
13078 }
13079 line_ptr += bytes_read;
6e70227d 13080 lh->statement_program_start = line_ptr;
debd256d 13081
3019eac3 13082 if (line_ptr > (section->buffer + section->size))
4d3c2250 13083 complaint (&symfile_complaints,
3e43a32a
MS
13084 _("line number info header doesn't "
13085 "fit in `.debug_line' section"));
debd256d
JB
13086
13087 discard_cleanups (back_to);
13088 return lh;
13089}
c906108c 13090
c6da4cef
DE
13091/* Subroutine of dwarf_decode_lines to simplify it.
13092 Return the file name of the psymtab for included file FILE_INDEX
13093 in line header LH of PST.
13094 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13095 If space for the result is malloc'd, it will be freed by a cleanup.
13096 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
13097
13098static char *
13099psymtab_include_file_name (const struct line_header *lh, int file_index,
13100 const struct partial_symtab *pst,
13101 const char *comp_dir)
13102{
13103 const struct file_entry fe = lh->file_names [file_index];
13104 char *include_name = fe.name;
13105 char *include_name_to_compare = include_name;
13106 char *dir_name = NULL;
72b9f47f
TT
13107 const char *pst_filename;
13108 char *copied_name = NULL;
c6da4cef
DE
13109 int file_is_pst;
13110
13111 if (fe.dir_index)
13112 dir_name = lh->include_dirs[fe.dir_index - 1];
13113
13114 if (!IS_ABSOLUTE_PATH (include_name)
13115 && (dir_name != NULL || comp_dir != NULL))
13116 {
13117 /* Avoid creating a duplicate psymtab for PST.
13118 We do this by comparing INCLUDE_NAME and PST_FILENAME.
13119 Before we do the comparison, however, we need to account
13120 for DIR_NAME and COMP_DIR.
13121 First prepend dir_name (if non-NULL). If we still don't
13122 have an absolute path prepend comp_dir (if non-NULL).
13123 However, the directory we record in the include-file's
13124 psymtab does not contain COMP_DIR (to match the
13125 corresponding symtab(s)).
13126
13127 Example:
13128
13129 bash$ cd /tmp
13130 bash$ gcc -g ./hello.c
13131 include_name = "hello.c"
13132 dir_name = "."
13133 DW_AT_comp_dir = comp_dir = "/tmp"
13134 DW_AT_name = "./hello.c" */
13135
13136 if (dir_name != NULL)
13137 {
13138 include_name = concat (dir_name, SLASH_STRING,
13139 include_name, (char *)NULL);
13140 include_name_to_compare = include_name;
13141 make_cleanup (xfree, include_name);
13142 }
13143 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
13144 {
13145 include_name_to_compare = concat (comp_dir, SLASH_STRING,
13146 include_name, (char *)NULL);
13147 }
13148 }
13149
13150 pst_filename = pst->filename;
13151 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
13152 {
72b9f47f
TT
13153 copied_name = concat (pst->dirname, SLASH_STRING,
13154 pst_filename, (char *)NULL);
13155 pst_filename = copied_name;
c6da4cef
DE
13156 }
13157
1e3fad37 13158 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
13159
13160 if (include_name_to_compare != include_name)
13161 xfree (include_name_to_compare);
72b9f47f
TT
13162 if (copied_name != NULL)
13163 xfree (copied_name);
c6da4cef
DE
13164
13165 if (file_is_pst)
13166 return NULL;
13167 return include_name;
13168}
13169
c91513d8
PP
13170/* Ignore this record_line request. */
13171
13172static void
13173noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
13174{
13175 return;
13176}
13177
f3f5162e
DE
13178/* Subroutine of dwarf_decode_lines to simplify it.
13179 Process the line number information in LH. */
debd256d 13180
c906108c 13181static void
f3f5162e
DE
13182dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
13183 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 13184{
a8c50c1f 13185 gdb_byte *line_ptr, *extended_end;
fe1b8b76 13186 gdb_byte *line_end;
a8c50c1f 13187 unsigned int bytes_read, extended_len;
c906108c 13188 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
13189 CORE_ADDR baseaddr;
13190 struct objfile *objfile = cu->objfile;
f3f5162e 13191 bfd *abfd = objfile->obfd;
fbf65064 13192 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 13193 const int decode_for_pst_p = (pst != NULL);
f3f5162e 13194 struct subfile *last_subfile = NULL;
c91513d8
PP
13195 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
13196 = record_line;
e142c38c
DJ
13197
13198 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 13199
debd256d
JB
13200 line_ptr = lh->statement_program_start;
13201 line_end = lh->statement_program_end;
c906108c
SS
13202
13203 /* Read the statement sequences until there's nothing left. */
13204 while (line_ptr < line_end)
13205 {
13206 /* state machine registers */
13207 CORE_ADDR address = 0;
13208 unsigned int file = 1;
13209 unsigned int line = 1;
13210 unsigned int column = 0;
debd256d 13211 int is_stmt = lh->default_is_stmt;
c906108c
SS
13212 int basic_block = 0;
13213 int end_sequence = 0;
fbf65064 13214 CORE_ADDR addr;
2dc7f7b3 13215 unsigned char op_index = 0;
c906108c 13216
aaa75496 13217 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 13218 {
aaa75496 13219 /* Start a subfile for the current file of the state machine. */
debd256d
JB
13220 /* lh->include_dirs and lh->file_names are 0-based, but the
13221 directory and file name numbers in the statement program
13222 are 1-based. */
13223 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 13224 char *dir = NULL;
a738430d 13225
debd256d
JB
13226 if (fe->dir_index)
13227 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
13228
13229 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
13230 }
13231
a738430d 13232 /* Decode the table. */
c5aa993b 13233 while (!end_sequence)
c906108c
SS
13234 {
13235 op_code = read_1_byte (abfd, line_ptr);
13236 line_ptr += 1;
59205f5a
JB
13237 if (line_ptr > line_end)
13238 {
13239 dwarf2_debug_line_missing_end_sequence_complaint ();
13240 break;
13241 }
9aa1fe7e 13242
debd256d 13243 if (op_code >= lh->opcode_base)
6e70227d 13244 {
a738430d 13245 /* Special operand. */
debd256d 13246 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
13247 address += (((op_index + (adj_opcode / lh->line_range))
13248 / lh->maximum_ops_per_instruction)
13249 * lh->minimum_instruction_length);
13250 op_index = ((op_index + (adj_opcode / lh->line_range))
13251 % lh->maximum_ops_per_instruction);
debd256d 13252 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 13253 if (lh->num_file_names < file || file == 0)
25e43795 13254 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
13255 /* For now we ignore lines not starting on an
13256 instruction boundary. */
13257 else if (op_index == 0)
25e43795
DJ
13258 {
13259 lh->file_names[file - 1].included_p = 1;
ca5f395d 13260 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
13261 {
13262 if (last_subfile != current_subfile)
13263 {
13264 addr = gdbarch_addr_bits_remove (gdbarch, address);
13265 if (last_subfile)
c91513d8 13266 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
13267 last_subfile = current_subfile;
13268 }
25e43795 13269 /* Append row to matrix using current values. */
7019d805 13270 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13271 (*p_record_line) (current_subfile, line, addr);
366da635 13272 }
25e43795 13273 }
ca5f395d 13274 basic_block = 0;
9aa1fe7e
GK
13275 }
13276 else switch (op_code)
c906108c
SS
13277 {
13278 case DW_LNS_extended_op:
3e43a32a
MS
13279 extended_len = read_unsigned_leb128 (abfd, line_ptr,
13280 &bytes_read);
473b7be6 13281 line_ptr += bytes_read;
a8c50c1f 13282 extended_end = line_ptr + extended_len;
c906108c
SS
13283 extended_op = read_1_byte (abfd, line_ptr);
13284 line_ptr += 1;
13285 switch (extended_op)
13286 {
13287 case DW_LNE_end_sequence:
c91513d8 13288 p_record_line = record_line;
c906108c 13289 end_sequence = 1;
c906108c
SS
13290 break;
13291 case DW_LNE_set_address:
e7c27a73 13292 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
13293
13294 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
13295 {
13296 /* This line table is for a function which has been
13297 GCd by the linker. Ignore it. PR gdb/12528 */
13298
13299 long line_offset
13300 = line_ptr - dwarf2_per_objfile->line.buffer;
13301
13302 complaint (&symfile_complaints,
13303 _(".debug_line address at offset 0x%lx is 0 "
13304 "[in module %s]"),
bb5ed363 13305 line_offset, objfile->name);
c91513d8
PP
13306 p_record_line = noop_record_line;
13307 }
13308
2dc7f7b3 13309 op_index = 0;
107d2387
AC
13310 line_ptr += bytes_read;
13311 address += baseaddr;
c906108c
SS
13312 break;
13313 case DW_LNE_define_file:
debd256d
JB
13314 {
13315 char *cur_file;
13316 unsigned int dir_index, mod_time, length;
6e70227d 13317
3e43a32a
MS
13318 cur_file = read_direct_string (abfd, line_ptr,
13319 &bytes_read);
debd256d
JB
13320 line_ptr += bytes_read;
13321 dir_index =
13322 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13323 line_ptr += bytes_read;
13324 mod_time =
13325 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13326 line_ptr += bytes_read;
13327 length =
13328 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13329 line_ptr += bytes_read;
13330 add_file_name (lh, cur_file, dir_index, mod_time, length);
13331 }
c906108c 13332 break;
d0c6ba3d
CC
13333 case DW_LNE_set_discriminator:
13334 /* The discriminator is not interesting to the debugger;
13335 just ignore it. */
13336 line_ptr = extended_end;
13337 break;
c906108c 13338 default:
4d3c2250 13339 complaint (&symfile_complaints,
e2e0b3e5 13340 _("mangled .debug_line section"));
debd256d 13341 return;
c906108c 13342 }
a8c50c1f
DJ
13343 /* Make sure that we parsed the extended op correctly. If e.g.
13344 we expected a different address size than the producer used,
13345 we may have read the wrong number of bytes. */
13346 if (line_ptr != extended_end)
13347 {
13348 complaint (&symfile_complaints,
13349 _("mangled .debug_line section"));
13350 return;
13351 }
c906108c
SS
13352 break;
13353 case DW_LNS_copy:
59205f5a 13354 if (lh->num_file_names < file || file == 0)
25e43795
DJ
13355 dwarf2_debug_line_missing_file_complaint ();
13356 else
366da635 13357 {
25e43795 13358 lh->file_names[file - 1].included_p = 1;
ca5f395d 13359 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
13360 {
13361 if (last_subfile != current_subfile)
13362 {
13363 addr = gdbarch_addr_bits_remove (gdbarch, address);
13364 if (last_subfile)
c91513d8 13365 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
13366 last_subfile = current_subfile;
13367 }
7019d805 13368 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13369 (*p_record_line) (current_subfile, line, addr);
fbf65064 13370 }
366da635 13371 }
c906108c
SS
13372 basic_block = 0;
13373 break;
13374 case DW_LNS_advance_pc:
2dc7f7b3
TT
13375 {
13376 CORE_ADDR adjust
13377 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13378
13379 address += (((op_index + adjust)
13380 / lh->maximum_ops_per_instruction)
13381 * lh->minimum_instruction_length);
13382 op_index = ((op_index + adjust)
13383 % lh->maximum_ops_per_instruction);
13384 line_ptr += bytes_read;
13385 }
c906108c
SS
13386 break;
13387 case DW_LNS_advance_line:
13388 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
13389 line_ptr += bytes_read;
13390 break;
13391 case DW_LNS_set_file:
debd256d 13392 {
a738430d
MK
13393 /* The arrays lh->include_dirs and lh->file_names are
13394 0-based, but the directory and file name numbers in
13395 the statement program are 1-based. */
debd256d 13396 struct file_entry *fe;
4f1520fb 13397 char *dir = NULL;
a738430d 13398
debd256d
JB
13399 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13400 line_ptr += bytes_read;
59205f5a 13401 if (lh->num_file_names < file || file == 0)
25e43795
DJ
13402 dwarf2_debug_line_missing_file_complaint ();
13403 else
13404 {
13405 fe = &lh->file_names[file - 1];
13406 if (fe->dir_index)
13407 dir = lh->include_dirs[fe->dir_index - 1];
13408 if (!decode_for_pst_p)
13409 {
13410 last_subfile = current_subfile;
13411 dwarf2_start_subfile (fe->name, dir, comp_dir);
13412 }
13413 }
debd256d 13414 }
c906108c
SS
13415 break;
13416 case DW_LNS_set_column:
13417 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13418 line_ptr += bytes_read;
13419 break;
13420 case DW_LNS_negate_stmt:
13421 is_stmt = (!is_stmt);
13422 break;
13423 case DW_LNS_set_basic_block:
13424 basic_block = 1;
13425 break;
c2c6d25f
JM
13426 /* Add to the address register of the state machine the
13427 address increment value corresponding to special opcode
a738430d
MK
13428 255. I.e., this value is scaled by the minimum
13429 instruction length since special opcode 255 would have
b021a221 13430 scaled the increment. */
c906108c 13431 case DW_LNS_const_add_pc:
2dc7f7b3
TT
13432 {
13433 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13434
13435 address += (((op_index + adjust)
13436 / lh->maximum_ops_per_instruction)
13437 * lh->minimum_instruction_length);
13438 op_index = ((op_index + adjust)
13439 % lh->maximum_ops_per_instruction);
13440 }
c906108c
SS
13441 break;
13442 case DW_LNS_fixed_advance_pc:
13443 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 13444 op_index = 0;
c906108c
SS
13445 line_ptr += 2;
13446 break;
9aa1fe7e 13447 default:
a738430d
MK
13448 {
13449 /* Unknown standard opcode, ignore it. */
9aa1fe7e 13450 int i;
a738430d 13451
debd256d 13452 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
13453 {
13454 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13455 line_ptr += bytes_read;
13456 }
13457 }
c906108c
SS
13458 }
13459 }
59205f5a
JB
13460 if (lh->num_file_names < file || file == 0)
13461 dwarf2_debug_line_missing_file_complaint ();
13462 else
13463 {
13464 lh->file_names[file - 1].included_p = 1;
13465 if (!decode_for_pst_p)
fbf65064
UW
13466 {
13467 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13468 (*p_record_line) (current_subfile, 0, addr);
fbf65064 13469 }
59205f5a 13470 }
c906108c 13471 }
f3f5162e
DE
13472}
13473
13474/* Decode the Line Number Program (LNP) for the given line_header
13475 structure and CU. The actual information extracted and the type
13476 of structures created from the LNP depends on the value of PST.
13477
13478 1. If PST is NULL, then this procedure uses the data from the program
13479 to create all necessary symbol tables, and their linetables.
13480
13481 2. If PST is not NULL, this procedure reads the program to determine
13482 the list of files included by the unit represented by PST, and
13483 builds all the associated partial symbol tables.
13484
13485 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13486 It is used for relative paths in the line table.
13487 NOTE: When processing partial symtabs (pst != NULL),
13488 comp_dir == pst->dirname.
13489
13490 NOTE: It is important that psymtabs have the same file name (via strcmp)
13491 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13492 symtab we don't use it in the name of the psymtabs we create.
13493 E.g. expand_line_sal requires this when finding psymtabs to expand.
13494 A good testcase for this is mb-inline.exp. */
13495
13496static void
13497dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13498 struct dwarf2_cu *cu, struct partial_symtab *pst,
13499 int want_line_info)
13500{
13501 struct objfile *objfile = cu->objfile;
13502 const int decode_for_pst_p = (pst != NULL);
13503 struct subfile *first_subfile = current_subfile;
13504
13505 if (want_line_info)
13506 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
13507
13508 if (decode_for_pst_p)
13509 {
13510 int file_index;
13511
13512 /* Now that we're done scanning the Line Header Program, we can
13513 create the psymtab of each included file. */
13514 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13515 if (lh->file_names[file_index].included_p == 1)
13516 {
c6da4cef
DE
13517 char *include_name =
13518 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13519 if (include_name != NULL)
aaa75496
JB
13520 dwarf2_create_include_psymtab (include_name, pst, objfile);
13521 }
13522 }
cb1df416
DJ
13523 else
13524 {
13525 /* Make sure a symtab is created for every file, even files
13526 which contain only variables (i.e. no code with associated
13527 line numbers). */
cb1df416 13528 int i;
cb1df416
DJ
13529
13530 for (i = 0; i < lh->num_file_names; i++)
13531 {
13532 char *dir = NULL;
f3f5162e 13533 struct file_entry *fe;
9a619af0 13534
cb1df416
DJ
13535 fe = &lh->file_names[i];
13536 if (fe->dir_index)
13537 dir = lh->include_dirs[fe->dir_index - 1];
13538 dwarf2_start_subfile (fe->name, dir, comp_dir);
13539
13540 /* Skip the main file; we don't need it, and it must be
13541 allocated last, so that it will show up before the
13542 non-primary symtabs in the objfile's symtab list. */
13543 if (current_subfile == first_subfile)
13544 continue;
13545
13546 if (current_subfile->symtab == NULL)
13547 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 13548 objfile);
cb1df416
DJ
13549 fe->symtab = current_subfile->symtab;
13550 }
13551 }
c906108c
SS
13552}
13553
13554/* Start a subfile for DWARF. FILENAME is the name of the file and
13555 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
13556 or NULL if not known. COMP_DIR is the compilation directory for the
13557 linetable's compilation unit or NULL if not known.
c906108c
SS
13558 This routine tries to keep line numbers from identical absolute and
13559 relative file names in a common subfile.
13560
13561 Using the `list' example from the GDB testsuite, which resides in
13562 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13563 of /srcdir/list0.c yields the following debugging information for list0.c:
13564
c5aa993b
JM
13565 DW_AT_name: /srcdir/list0.c
13566 DW_AT_comp_dir: /compdir
357e46e7 13567 files.files[0].name: list0.h
c5aa993b 13568 files.files[0].dir: /srcdir
357e46e7 13569 files.files[1].name: list0.c
c5aa993b 13570 files.files[1].dir: /srcdir
c906108c
SS
13571
13572 The line number information for list0.c has to end up in a single
4f1520fb
FR
13573 subfile, so that `break /srcdir/list0.c:1' works as expected.
13574 start_subfile will ensure that this happens provided that we pass the
13575 concatenation of files.files[1].dir and files.files[1].name as the
13576 subfile's name. */
c906108c
SS
13577
13578static void
3e43a32a
MS
13579dwarf2_start_subfile (char *filename, const char *dirname,
13580 const char *comp_dir)
c906108c 13581{
4f1520fb
FR
13582 char *fullname;
13583
13584 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13585 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13586 second argument to start_subfile. To be consistent, we do the
13587 same here. In order not to lose the line information directory,
13588 we concatenate it to the filename when it makes sense.
13589 Note that the Dwarf3 standard says (speaking of filenames in line
13590 information): ``The directory index is ignored for file names
13591 that represent full path names''. Thus ignoring dirname in the
13592 `else' branch below isn't an issue. */
c906108c 13593
d5166ae1 13594 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
13595 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13596 else
13597 fullname = filename;
c906108c 13598
4f1520fb
FR
13599 start_subfile (fullname, comp_dir);
13600
13601 if (fullname != filename)
13602 xfree (fullname);
c906108c
SS
13603}
13604
4c2df51b
DJ
13605static void
13606var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 13607 struct dwarf2_cu *cu)
4c2df51b 13608{
e7c27a73
DJ
13609 struct objfile *objfile = cu->objfile;
13610 struct comp_unit_head *cu_header = &cu->header;
13611
4c2df51b
DJ
13612 /* NOTE drow/2003-01-30: There used to be a comment and some special
13613 code here to turn a symbol with DW_AT_external and a
13614 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13615 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13616 with some versions of binutils) where shared libraries could have
13617 relocations against symbols in their debug information - the
13618 minimal symbol would have the right address, but the debug info
13619 would not. It's no longer necessary, because we will explicitly
13620 apply relocations when we read in the debug information now. */
13621
13622 /* A DW_AT_location attribute with no contents indicates that a
13623 variable has been optimized away. */
13624 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13625 {
13626 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13627 return;
13628 }
13629
13630 /* Handle one degenerate form of location expression specially, to
13631 preserve GDB's previous behavior when section offsets are
3019eac3
DE
13632 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13633 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
13634
13635 if (attr_form_is_block (attr)
3019eac3
DE
13636 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13637 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13638 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13639 && (DW_BLOCK (attr)->size
13640 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 13641 {
891d2f0b 13642 unsigned int dummy;
4c2df51b 13643
3019eac3
DE
13644 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13645 SYMBOL_VALUE_ADDRESS (sym) =
13646 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13647 else
13648 SYMBOL_VALUE_ADDRESS (sym) =
13649 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 13650 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
13651 fixup_symbol_section (sym, objfile);
13652 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13653 SYMBOL_SECTION (sym));
4c2df51b
DJ
13654 return;
13655 }
13656
13657 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13658 expression evaluator, and use LOC_COMPUTED only when necessary
13659 (i.e. when the value of a register or memory location is
13660 referenced, or a thread-local block, etc.). Then again, it might
13661 not be worthwhile. I'm assuming that it isn't unless performance
13662 or memory numbers show me otherwise. */
13663
e7c27a73 13664 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 13665 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
13666
13667 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13668 cu->has_loclist = 1;
4c2df51b
DJ
13669}
13670
c906108c
SS
13671/* Given a pointer to a DWARF information entry, figure out if we need
13672 to make a symbol table entry for it, and if so, create a new entry
13673 and return a pointer to it.
13674 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
13675 used the passed type.
13676 If SPACE is not NULL, use it to hold the new symbol. If it is
13677 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
13678
13679static struct symbol *
34eaf542
TT
13680new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13681 struct symbol *space)
c906108c 13682{
e7c27a73 13683 struct objfile *objfile = cu->objfile;
c906108c
SS
13684 struct symbol *sym = NULL;
13685 char *name;
13686 struct attribute *attr = NULL;
13687 struct attribute *attr2 = NULL;
e142c38c 13688 CORE_ADDR baseaddr;
e37fd15a
SW
13689 struct pending **list_to_add = NULL;
13690
edb3359d 13691 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
13692
13693 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 13694
94af9270 13695 name = dwarf2_name (die, cu);
c906108c
SS
13696 if (name)
13697 {
94af9270 13698 const char *linkagename;
34eaf542 13699 int suppress_add = 0;
94af9270 13700
34eaf542
TT
13701 if (space)
13702 sym = space;
13703 else
13704 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 13705 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
13706
13707 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 13708 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
13709 linkagename = dwarf2_physname (name, die, cu);
13710 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 13711
f55ee35c
JK
13712 /* Fortran does not have mangling standard and the mangling does differ
13713 between gfortran, iFort etc. */
13714 if (cu->language == language_fortran
b250c185 13715 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
13716 symbol_set_demangled_name (&(sym->ginfo),
13717 (char *) dwarf2_full_name (name, die, cu),
13718 NULL);
f55ee35c 13719
c906108c 13720 /* Default assumptions.
c5aa993b 13721 Use the passed type or decode it from the die. */
176620f1 13722 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 13723 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
13724 if (type != NULL)
13725 SYMBOL_TYPE (sym) = type;
13726 else
e7c27a73 13727 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
13728 attr = dwarf2_attr (die,
13729 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13730 cu);
c906108c
SS
13731 if (attr)
13732 {
13733 SYMBOL_LINE (sym) = DW_UNSND (attr);
13734 }
cb1df416 13735
edb3359d
DJ
13736 attr = dwarf2_attr (die,
13737 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13738 cu);
cb1df416
DJ
13739 if (attr)
13740 {
13741 int file_index = DW_UNSND (attr);
9a619af0 13742
cb1df416
DJ
13743 if (cu->line_header == NULL
13744 || file_index > cu->line_header->num_file_names)
13745 complaint (&symfile_complaints,
13746 _("file index out of range"));
1c3d648d 13747 else if (file_index > 0)
cb1df416
DJ
13748 {
13749 struct file_entry *fe;
9a619af0 13750
cb1df416
DJ
13751 fe = &cu->line_header->file_names[file_index - 1];
13752 SYMBOL_SYMTAB (sym) = fe->symtab;
13753 }
13754 }
13755
c906108c
SS
13756 switch (die->tag)
13757 {
13758 case DW_TAG_label:
e142c38c 13759 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
13760 if (attr)
13761 {
13762 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13763 }
0f5238ed
TT
13764 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13765 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 13766 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 13767 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
13768 break;
13769 case DW_TAG_subprogram:
13770 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13771 finish_block. */
13772 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 13773 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
13774 if ((attr2 && (DW_UNSND (attr2) != 0))
13775 || cu->language == language_ada)
c906108c 13776 {
2cfa0c8d
JB
13777 /* Subprograms marked external are stored as a global symbol.
13778 Ada subprograms, whether marked external or not, are always
13779 stored as a global symbol, because we want to be able to
13780 access them globally. For instance, we want to be able
13781 to break on a nested subprogram without having to
13782 specify the context. */
e37fd15a 13783 list_to_add = &global_symbols;
c906108c
SS
13784 }
13785 else
13786 {
e37fd15a 13787 list_to_add = cu->list_in_scope;
c906108c
SS
13788 }
13789 break;
edb3359d
DJ
13790 case DW_TAG_inlined_subroutine:
13791 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13792 finish_block. */
13793 SYMBOL_CLASS (sym) = LOC_BLOCK;
13794 SYMBOL_INLINED (sym) = 1;
481860b3 13795 list_to_add = cu->list_in_scope;
edb3359d 13796 break;
34eaf542
TT
13797 case DW_TAG_template_value_param:
13798 suppress_add = 1;
13799 /* Fall through. */
72929c62 13800 case DW_TAG_constant:
c906108c 13801 case DW_TAG_variable:
254e6b9e 13802 case DW_TAG_member:
0963b4bd
MS
13803 /* Compilation with minimal debug info may result in
13804 variables with missing type entries. Change the
13805 misleading `void' type to something sensible. */
c906108c 13806 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 13807 SYMBOL_TYPE (sym)
46bf5051 13808 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 13809
e142c38c 13810 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
13811 /* In the case of DW_TAG_member, we should only be called for
13812 static const members. */
13813 if (die->tag == DW_TAG_member)
13814 {
3863f96c
DE
13815 /* dwarf2_add_field uses die_is_declaration,
13816 so we do the same. */
254e6b9e
DE
13817 gdb_assert (die_is_declaration (die, cu));
13818 gdb_assert (attr);
13819 }
c906108c
SS
13820 if (attr)
13821 {
e7c27a73 13822 dwarf2_const_value (attr, sym, cu);
e142c38c 13823 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 13824 if (!suppress_add)
34eaf542
TT
13825 {
13826 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 13827 list_to_add = &global_symbols;
34eaf542 13828 else
e37fd15a 13829 list_to_add = cu->list_in_scope;
34eaf542 13830 }
c906108c
SS
13831 break;
13832 }
e142c38c 13833 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13834 if (attr)
13835 {
e7c27a73 13836 var_decode_location (attr, sym, cu);
e142c38c 13837 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
13838 if (SYMBOL_CLASS (sym) == LOC_STATIC
13839 && SYMBOL_VALUE_ADDRESS (sym) == 0
13840 && !dwarf2_per_objfile->has_section_at_zero)
13841 {
13842 /* When a static variable is eliminated by the linker,
13843 the corresponding debug information is not stripped
13844 out, but the variable address is set to null;
13845 do not add such variables into symbol table. */
13846 }
13847 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 13848 {
f55ee35c
JK
13849 /* Workaround gfortran PR debug/40040 - it uses
13850 DW_AT_location for variables in -fPIC libraries which may
13851 get overriden by other libraries/executable and get
13852 a different address. Resolve it by the minimal symbol
13853 which may come from inferior's executable using copy
13854 relocation. Make this workaround only for gfortran as for
13855 other compilers GDB cannot guess the minimal symbol
13856 Fortran mangling kind. */
13857 if (cu->language == language_fortran && die->parent
13858 && die->parent->tag == DW_TAG_module
13859 && cu->producer
13860 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13861 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13862
1c809c68
TT
13863 /* A variable with DW_AT_external is never static,
13864 but it may be block-scoped. */
13865 list_to_add = (cu->list_in_scope == &file_symbols
13866 ? &global_symbols : cu->list_in_scope);
1c809c68 13867 }
c906108c 13868 else
e37fd15a 13869 list_to_add = cu->list_in_scope;
c906108c
SS
13870 }
13871 else
13872 {
13873 /* We do not know the address of this symbol.
c5aa993b
JM
13874 If it is an external symbol and we have type information
13875 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13876 The address of the variable will then be determined from
13877 the minimal symbol table whenever the variable is
13878 referenced. */
e142c38c 13879 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 13880 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 13881 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 13882 {
0fe7935b
DJ
13883 /* A variable with DW_AT_external is never static, but it
13884 may be block-scoped. */
13885 list_to_add = (cu->list_in_scope == &file_symbols
13886 ? &global_symbols : cu->list_in_scope);
13887
c906108c 13888 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 13889 }
442ddf59
JK
13890 else if (!die_is_declaration (die, cu))
13891 {
13892 /* Use the default LOC_OPTIMIZED_OUT class. */
13893 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
13894 if (!suppress_add)
13895 list_to_add = cu->list_in_scope;
442ddf59 13896 }
c906108c
SS
13897 }
13898 break;
13899 case DW_TAG_formal_parameter:
edb3359d
DJ
13900 /* If we are inside a function, mark this as an argument. If
13901 not, we might be looking at an argument to an inlined function
13902 when we do not have enough information to show inlined frames;
13903 pretend it's a local variable in that case so that the user can
13904 still see it. */
13905 if (context_stack_depth > 0
13906 && context_stack[context_stack_depth - 1].name != NULL)
13907 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 13908 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13909 if (attr)
13910 {
e7c27a73 13911 var_decode_location (attr, sym, cu);
c906108c 13912 }
e142c38c 13913 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13914 if (attr)
13915 {
e7c27a73 13916 dwarf2_const_value (attr, sym, cu);
c906108c 13917 }
f346a30d 13918
e37fd15a 13919 list_to_add = cu->list_in_scope;
c906108c
SS
13920 break;
13921 case DW_TAG_unspecified_parameters:
13922 /* From varargs functions; gdb doesn't seem to have any
13923 interest in this information, so just ignore it for now.
13924 (FIXME?) */
13925 break;
34eaf542
TT
13926 case DW_TAG_template_type_param:
13927 suppress_add = 1;
13928 /* Fall through. */
c906108c 13929 case DW_TAG_class_type:
680b30c7 13930 case DW_TAG_interface_type:
c906108c
SS
13931 case DW_TAG_structure_type:
13932 case DW_TAG_union_type:
72019c9c 13933 case DW_TAG_set_type:
c906108c
SS
13934 case DW_TAG_enumeration_type:
13935 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13936 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 13937
63d06c5c 13938 {
987504bb 13939 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
13940 really ever be static objects: otherwise, if you try
13941 to, say, break of a class's method and you're in a file
13942 which doesn't mention that class, it won't work unless
13943 the check for all static symbols in lookup_symbol_aux
13944 saves you. See the OtherFileClass tests in
13945 gdb.c++/namespace.exp. */
13946
e37fd15a 13947 if (!suppress_add)
34eaf542 13948 {
34eaf542
TT
13949 list_to_add = (cu->list_in_scope == &file_symbols
13950 && (cu->language == language_cplus
13951 || cu->language == language_java)
13952 ? &global_symbols : cu->list_in_scope);
63d06c5c 13953
64382290
TT
13954 /* The semantics of C++ state that "struct foo {
13955 ... }" also defines a typedef for "foo". A Java
13956 class declaration also defines a typedef for the
13957 class. */
13958 if (cu->language == language_cplus
13959 || cu->language == language_java
13960 || cu->language == language_ada)
13961 {
13962 /* The symbol's name is already allocated along
13963 with this objfile, so we don't need to
13964 duplicate it for the type. */
13965 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13966 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13967 }
63d06c5c
DC
13968 }
13969 }
c906108c
SS
13970 break;
13971 case DW_TAG_typedef:
63d06c5c
DC
13972 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13973 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13974 list_to_add = cu->list_in_scope;
63d06c5c 13975 break;
c906108c 13976 case DW_TAG_base_type:
a02abb62 13977 case DW_TAG_subrange_type:
c906108c 13978 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13979 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13980 list_to_add = cu->list_in_scope;
c906108c
SS
13981 break;
13982 case DW_TAG_enumerator:
e142c38c 13983 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13984 if (attr)
13985 {
e7c27a73 13986 dwarf2_const_value (attr, sym, cu);
c906108c 13987 }
63d06c5c
DC
13988 {
13989 /* NOTE: carlton/2003-11-10: See comment above in the
13990 DW_TAG_class_type, etc. block. */
13991
e142c38c 13992 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
13993 && (cu->language == language_cplus
13994 || cu->language == language_java)
e142c38c 13995 ? &global_symbols : cu->list_in_scope);
63d06c5c 13996 }
c906108c 13997 break;
5c4e30ca
DC
13998 case DW_TAG_namespace:
13999 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 14000 list_to_add = &global_symbols;
5c4e30ca 14001 break;
c906108c
SS
14002 default:
14003 /* Not a tag we recognize. Hopefully we aren't processing
14004 trash data, but since we must specifically ignore things
14005 we don't recognize, there is nothing else we should do at
0963b4bd 14006 this point. */
e2e0b3e5 14007 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 14008 dwarf_tag_name (die->tag));
c906108c
SS
14009 break;
14010 }
df8a16a1 14011
e37fd15a
SW
14012 if (suppress_add)
14013 {
14014 sym->hash_next = objfile->template_symbols;
14015 objfile->template_symbols = sym;
14016 list_to_add = NULL;
14017 }
14018
14019 if (list_to_add != NULL)
14020 add_symbol_to_list (sym, list_to_add);
14021
df8a16a1
DJ
14022 /* For the benefit of old versions of GCC, check for anonymous
14023 namespaces based on the demangled name. */
14024 if (!processing_has_namespace_info
94af9270 14025 && cu->language == language_cplus)
a10964d1 14026 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
14027 }
14028 return (sym);
14029}
14030
34eaf542
TT
14031/* A wrapper for new_symbol_full that always allocates a new symbol. */
14032
14033static struct symbol *
14034new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14035{
14036 return new_symbol_full (die, type, cu, NULL);
14037}
14038
98bfdba5
PA
14039/* Given an attr with a DW_FORM_dataN value in host byte order,
14040 zero-extend it as appropriate for the symbol's type. The DWARF
14041 standard (v4) is not entirely clear about the meaning of using
14042 DW_FORM_dataN for a constant with a signed type, where the type is
14043 wider than the data. The conclusion of a discussion on the DWARF
14044 list was that this is unspecified. We choose to always zero-extend
14045 because that is the interpretation long in use by GCC. */
c906108c 14046
98bfdba5
PA
14047static gdb_byte *
14048dwarf2_const_value_data (struct attribute *attr, struct type *type,
14049 const char *name, struct obstack *obstack,
12df843f 14050 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 14051{
e7c27a73 14052 struct objfile *objfile = cu->objfile;
e17a4113
UW
14053 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
14054 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
14055 LONGEST l = DW_UNSND (attr);
14056
14057 if (bits < sizeof (*value) * 8)
14058 {
14059 l &= ((LONGEST) 1 << bits) - 1;
14060 *value = l;
14061 }
14062 else if (bits == sizeof (*value) * 8)
14063 *value = l;
14064 else
14065 {
14066 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
14067 store_unsigned_integer (bytes, bits / 8, byte_order, l);
14068 return bytes;
14069 }
14070
14071 return NULL;
14072}
14073
14074/* Read a constant value from an attribute. Either set *VALUE, or if
14075 the value does not fit in *VALUE, set *BYTES - either already
14076 allocated on the objfile obstack, or newly allocated on OBSTACK,
14077 or, set *BATON, if we translated the constant to a location
14078 expression. */
14079
14080static void
14081dwarf2_const_value_attr (struct attribute *attr, struct type *type,
14082 const char *name, struct obstack *obstack,
14083 struct dwarf2_cu *cu,
12df843f 14084 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
14085 struct dwarf2_locexpr_baton **baton)
14086{
14087 struct objfile *objfile = cu->objfile;
14088 struct comp_unit_head *cu_header = &cu->header;
c906108c 14089 struct dwarf_block *blk;
98bfdba5
PA
14090 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
14091 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
14092
14093 *value = 0;
14094 *bytes = NULL;
14095 *baton = NULL;
c906108c
SS
14096
14097 switch (attr->form)
14098 {
14099 case DW_FORM_addr:
3019eac3 14100 case DW_FORM_GNU_addr_index:
ac56253d 14101 {
ac56253d
TT
14102 gdb_byte *data;
14103
98bfdba5
PA
14104 if (TYPE_LENGTH (type) != cu_header->addr_size)
14105 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 14106 cu_header->addr_size,
98bfdba5 14107 TYPE_LENGTH (type));
ac56253d
TT
14108 /* Symbols of this form are reasonably rare, so we just
14109 piggyback on the existing location code rather than writing
14110 a new implementation of symbol_computed_ops. */
98bfdba5
PA
14111 *baton = obstack_alloc (&objfile->objfile_obstack,
14112 sizeof (struct dwarf2_locexpr_baton));
14113 (*baton)->per_cu = cu->per_cu;
14114 gdb_assert ((*baton)->per_cu);
ac56253d 14115
98bfdba5
PA
14116 (*baton)->size = 2 + cu_header->addr_size;
14117 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
14118 (*baton)->data = data;
ac56253d
TT
14119
14120 data[0] = DW_OP_addr;
14121 store_unsigned_integer (&data[1], cu_header->addr_size,
14122 byte_order, DW_ADDR (attr));
14123 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 14124 }
c906108c 14125 break;
4ac36638 14126 case DW_FORM_string:
93b5768b 14127 case DW_FORM_strp:
3019eac3 14128 case DW_FORM_GNU_str_index:
98bfdba5
PA
14129 /* DW_STRING is already allocated on the objfile obstack, point
14130 directly to it. */
14131 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 14132 break;
c906108c
SS
14133 case DW_FORM_block1:
14134 case DW_FORM_block2:
14135 case DW_FORM_block4:
14136 case DW_FORM_block:
2dc7f7b3 14137 case DW_FORM_exprloc:
c906108c 14138 blk = DW_BLOCK (attr);
98bfdba5
PA
14139 if (TYPE_LENGTH (type) != blk->size)
14140 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
14141 TYPE_LENGTH (type));
14142 *bytes = blk->data;
c906108c 14143 break;
2df3850c
JM
14144
14145 /* The DW_AT_const_value attributes are supposed to carry the
14146 symbol's value "represented as it would be on the target
14147 architecture." By the time we get here, it's already been
14148 converted to host endianness, so we just need to sign- or
14149 zero-extend it as appropriate. */
14150 case DW_FORM_data1:
3e43a32a
MS
14151 *bytes = dwarf2_const_value_data (attr, type, name,
14152 obstack, cu, value, 8);
2df3850c 14153 break;
c906108c 14154 case DW_FORM_data2:
3e43a32a
MS
14155 *bytes = dwarf2_const_value_data (attr, type, name,
14156 obstack, cu, value, 16);
2df3850c 14157 break;
c906108c 14158 case DW_FORM_data4:
3e43a32a
MS
14159 *bytes = dwarf2_const_value_data (attr, type, name,
14160 obstack, cu, value, 32);
2df3850c 14161 break;
c906108c 14162 case DW_FORM_data8:
3e43a32a
MS
14163 *bytes = dwarf2_const_value_data (attr, type, name,
14164 obstack, cu, value, 64);
2df3850c
JM
14165 break;
14166
c906108c 14167 case DW_FORM_sdata:
98bfdba5 14168 *value = DW_SND (attr);
2df3850c
JM
14169 break;
14170
c906108c 14171 case DW_FORM_udata:
98bfdba5 14172 *value = DW_UNSND (attr);
c906108c 14173 break;
2df3850c 14174
c906108c 14175 default:
4d3c2250 14176 complaint (&symfile_complaints,
e2e0b3e5 14177 _("unsupported const value attribute form: '%s'"),
4d3c2250 14178 dwarf_form_name (attr->form));
98bfdba5 14179 *value = 0;
c906108c
SS
14180 break;
14181 }
14182}
14183
2df3850c 14184
98bfdba5
PA
14185/* Copy constant value from an attribute to a symbol. */
14186
2df3850c 14187static void
98bfdba5
PA
14188dwarf2_const_value (struct attribute *attr, struct symbol *sym,
14189 struct dwarf2_cu *cu)
2df3850c 14190{
98bfdba5
PA
14191 struct objfile *objfile = cu->objfile;
14192 struct comp_unit_head *cu_header = &cu->header;
12df843f 14193 LONGEST value;
98bfdba5
PA
14194 gdb_byte *bytes;
14195 struct dwarf2_locexpr_baton *baton;
2df3850c 14196
98bfdba5
PA
14197 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
14198 SYMBOL_PRINT_NAME (sym),
14199 &objfile->objfile_obstack, cu,
14200 &value, &bytes, &baton);
2df3850c 14201
98bfdba5
PA
14202 if (baton != NULL)
14203 {
14204 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14205 SYMBOL_LOCATION_BATON (sym) = baton;
14206 SYMBOL_CLASS (sym) = LOC_COMPUTED;
14207 }
14208 else if (bytes != NULL)
14209 {
14210 SYMBOL_VALUE_BYTES (sym) = bytes;
14211 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
14212 }
14213 else
14214 {
14215 SYMBOL_VALUE (sym) = value;
14216 SYMBOL_CLASS (sym) = LOC_CONST;
14217 }
2df3850c
JM
14218}
14219
c906108c
SS
14220/* Return the type of the die in question using its DW_AT_type attribute. */
14221
14222static struct type *
e7c27a73 14223die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14224{
c906108c 14225 struct attribute *type_attr;
c906108c 14226
e142c38c 14227 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
14228 if (!type_attr)
14229 {
14230 /* A missing DW_AT_type represents a void type. */
46bf5051 14231 return objfile_type (cu->objfile)->builtin_void;
c906108c 14232 }
348e048f 14233
673bfd45 14234 return lookup_die_type (die, type_attr, cu);
c906108c
SS
14235}
14236
b4ba55a1
JB
14237/* True iff CU's producer generates GNAT Ada auxiliary information
14238 that allows to find parallel types through that information instead
14239 of having to do expensive parallel lookups by type name. */
14240
14241static int
14242need_gnat_info (struct dwarf2_cu *cu)
14243{
14244 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
14245 of GNAT produces this auxiliary information, without any indication
14246 that it is produced. Part of enhancing the FSF version of GNAT
14247 to produce that information will be to put in place an indicator
14248 that we can use in order to determine whether the descriptive type
14249 info is available or not. One suggestion that has been made is
14250 to use a new attribute, attached to the CU die. For now, assume
14251 that the descriptive type info is not available. */
14252 return 0;
14253}
14254
b4ba55a1
JB
14255/* Return the auxiliary type of the die in question using its
14256 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
14257 attribute is not present. */
14258
14259static struct type *
14260die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
14261{
b4ba55a1 14262 struct attribute *type_attr;
b4ba55a1
JB
14263
14264 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
14265 if (!type_attr)
14266 return NULL;
14267
673bfd45 14268 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
14269}
14270
14271/* If DIE has a descriptive_type attribute, then set the TYPE's
14272 descriptive type accordingly. */
14273
14274static void
14275set_descriptive_type (struct type *type, struct die_info *die,
14276 struct dwarf2_cu *cu)
14277{
14278 struct type *descriptive_type = die_descriptive_type (die, cu);
14279
14280 if (descriptive_type)
14281 {
14282 ALLOCATE_GNAT_AUX_TYPE (type);
14283 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
14284 }
14285}
14286
c906108c
SS
14287/* Return the containing type of the die in question using its
14288 DW_AT_containing_type attribute. */
14289
14290static struct type *
e7c27a73 14291die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14292{
c906108c 14293 struct attribute *type_attr;
c906108c 14294
e142c38c 14295 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
14296 if (!type_attr)
14297 error (_("Dwarf Error: Problem turning containing type into gdb type "
14298 "[in module %s]"), cu->objfile->name);
14299
673bfd45 14300 return lookup_die_type (die, type_attr, cu);
c906108c
SS
14301}
14302
673bfd45
DE
14303/* Look up the type of DIE in CU using its type attribute ATTR.
14304 If there is no type substitute an error marker. */
14305
c906108c 14306static struct type *
673bfd45
DE
14307lookup_die_type (struct die_info *die, struct attribute *attr,
14308 struct dwarf2_cu *cu)
c906108c 14309{
bb5ed363 14310 struct objfile *objfile = cu->objfile;
f792889a
DJ
14311 struct type *this_type;
14312
673bfd45
DE
14313 /* First see if we have it cached. */
14314
14315 if (is_ref_attr (attr))
14316 {
b64f50a1 14317 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
14318
14319 this_type = get_die_type_at_offset (offset, cu->per_cu);
14320 }
55f1336d 14321 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
14322 {
14323 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
14324
14325 /* sig_type will be NULL if the signatured type is missing from
14326 the debug info. */
14327 if (sig_type == NULL)
14328 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14329 "at 0x%x [in module %s]"),
b64f50a1 14330 die->offset.sect_off, objfile->name);
673bfd45 14331
3019eac3
DE
14332 gdb_assert (sig_type->per_cu.is_debug_types);
14333 /* If we haven't filled in type_offset_in_section yet, then we
14334 haven't read the type in yet. */
14335 this_type = NULL;
14336 if (sig_type->type_offset_in_section.sect_off != 0)
14337 {
14338 this_type =
14339 get_die_type_at_offset (sig_type->type_offset_in_section,
14340 &sig_type->per_cu);
14341 }
673bfd45
DE
14342 }
14343 else
14344 {
14345 dump_die_for_error (die);
14346 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 14347 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
14348 }
14349
14350 /* If not cached we need to read it in. */
14351
14352 if (this_type == NULL)
14353 {
14354 struct die_info *type_die;
14355 struct dwarf2_cu *type_cu = cu;
14356
14357 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
14358 /* If we found the type now, it's probably because the type came
14359 from an inter-CU reference and the type's CU got expanded before
14360 ours. */
14361 this_type = get_die_type (type_die, type_cu);
14362 if (this_type == NULL)
14363 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
14364 }
14365
14366 /* If we still don't have a type use an error marker. */
14367
14368 if (this_type == NULL)
c906108c 14369 {
b00fdb78
TT
14370 char *message, *saved;
14371
14372 /* read_type_die already issued a complaint. */
14373 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 14374 objfile->name,
b64f50a1
JK
14375 cu->header.offset.sect_off,
14376 die->offset.sect_off);
bb5ed363 14377 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
14378 message, strlen (message));
14379 xfree (message);
14380
bb5ed363 14381 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 14382 }
673bfd45 14383
f792889a 14384 return this_type;
c906108c
SS
14385}
14386
673bfd45
DE
14387/* Return the type in DIE, CU.
14388 Returns NULL for invalid types.
14389
14390 This first does a lookup in the appropriate type_hash table,
14391 and only reads the die in if necessary.
14392
14393 NOTE: This can be called when reading in partial or full symbols. */
14394
f792889a 14395static struct type *
e7c27a73 14396read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14397{
f792889a
DJ
14398 struct type *this_type;
14399
14400 this_type = get_die_type (die, cu);
14401 if (this_type)
14402 return this_type;
14403
673bfd45
DE
14404 return read_type_die_1 (die, cu);
14405}
14406
14407/* Read the type in DIE, CU.
14408 Returns NULL for invalid types. */
14409
14410static struct type *
14411read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
14412{
14413 struct type *this_type = NULL;
14414
c906108c
SS
14415 switch (die->tag)
14416 {
14417 case DW_TAG_class_type:
680b30c7 14418 case DW_TAG_interface_type:
c906108c
SS
14419 case DW_TAG_structure_type:
14420 case DW_TAG_union_type:
f792889a 14421 this_type = read_structure_type (die, cu);
c906108c
SS
14422 break;
14423 case DW_TAG_enumeration_type:
f792889a 14424 this_type = read_enumeration_type (die, cu);
c906108c
SS
14425 break;
14426 case DW_TAG_subprogram:
14427 case DW_TAG_subroutine_type:
edb3359d 14428 case DW_TAG_inlined_subroutine:
f792889a 14429 this_type = read_subroutine_type (die, cu);
c906108c
SS
14430 break;
14431 case DW_TAG_array_type:
f792889a 14432 this_type = read_array_type (die, cu);
c906108c 14433 break;
72019c9c 14434 case DW_TAG_set_type:
f792889a 14435 this_type = read_set_type (die, cu);
72019c9c 14436 break;
c906108c 14437 case DW_TAG_pointer_type:
f792889a 14438 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
14439 break;
14440 case DW_TAG_ptr_to_member_type:
f792889a 14441 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
14442 break;
14443 case DW_TAG_reference_type:
f792889a 14444 this_type = read_tag_reference_type (die, cu);
c906108c
SS
14445 break;
14446 case DW_TAG_const_type:
f792889a 14447 this_type = read_tag_const_type (die, cu);
c906108c
SS
14448 break;
14449 case DW_TAG_volatile_type:
f792889a 14450 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
14451 break;
14452 case DW_TAG_string_type:
f792889a 14453 this_type = read_tag_string_type (die, cu);
c906108c
SS
14454 break;
14455 case DW_TAG_typedef:
f792889a 14456 this_type = read_typedef (die, cu);
c906108c 14457 break;
a02abb62 14458 case DW_TAG_subrange_type:
f792889a 14459 this_type = read_subrange_type (die, cu);
a02abb62 14460 break;
c906108c 14461 case DW_TAG_base_type:
f792889a 14462 this_type = read_base_type (die, cu);
c906108c 14463 break;
81a17f79 14464 case DW_TAG_unspecified_type:
f792889a 14465 this_type = read_unspecified_type (die, cu);
81a17f79 14466 break;
0114d602
DJ
14467 case DW_TAG_namespace:
14468 this_type = read_namespace_type (die, cu);
14469 break;
f55ee35c
JK
14470 case DW_TAG_module:
14471 this_type = read_module_type (die, cu);
14472 break;
c906108c 14473 default:
3e43a32a
MS
14474 complaint (&symfile_complaints,
14475 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 14476 dwarf_tag_name (die->tag));
c906108c
SS
14477 break;
14478 }
63d06c5c 14479
f792889a 14480 return this_type;
63d06c5c
DC
14481}
14482
abc72ce4
DE
14483/* See if we can figure out if the class lives in a namespace. We do
14484 this by looking for a member function; its demangled name will
14485 contain namespace info, if there is any.
14486 Return the computed name or NULL.
14487 Space for the result is allocated on the objfile's obstack.
14488 This is the full-die version of guess_partial_die_structure_name.
14489 In this case we know DIE has no useful parent. */
14490
14491static char *
14492guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14493{
14494 struct die_info *spec_die;
14495 struct dwarf2_cu *spec_cu;
14496 struct die_info *child;
14497
14498 spec_cu = cu;
14499 spec_die = die_specification (die, &spec_cu);
14500 if (spec_die != NULL)
14501 {
14502 die = spec_die;
14503 cu = spec_cu;
14504 }
14505
14506 for (child = die->child;
14507 child != NULL;
14508 child = child->sibling)
14509 {
14510 if (child->tag == DW_TAG_subprogram)
14511 {
14512 struct attribute *attr;
14513
14514 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14515 if (attr == NULL)
14516 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14517 if (attr != NULL)
14518 {
14519 char *actual_name
14520 = language_class_name_from_physname (cu->language_defn,
14521 DW_STRING (attr));
14522 char *name = NULL;
14523
14524 if (actual_name != NULL)
14525 {
14526 char *die_name = dwarf2_name (die, cu);
14527
14528 if (die_name != NULL
14529 && strcmp (die_name, actual_name) != 0)
14530 {
14531 /* Strip off the class name from the full name.
14532 We want the prefix. */
14533 int die_name_len = strlen (die_name);
14534 int actual_name_len = strlen (actual_name);
14535
14536 /* Test for '::' as a sanity check. */
14537 if (actual_name_len > die_name_len + 2
3e43a32a
MS
14538 && actual_name[actual_name_len
14539 - die_name_len - 1] == ':')
abc72ce4
DE
14540 name =
14541 obsavestring (actual_name,
14542 actual_name_len - die_name_len - 2,
14543 &cu->objfile->objfile_obstack);
14544 }
14545 }
14546 xfree (actual_name);
14547 return name;
14548 }
14549 }
14550 }
14551
14552 return NULL;
14553}
14554
96408a79
SA
14555/* GCC might emit a nameless typedef that has a linkage name. Determine the
14556 prefix part in such case. See
14557 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14558
14559static char *
14560anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14561{
14562 struct attribute *attr;
14563 char *base;
14564
14565 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14566 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14567 return NULL;
14568
14569 attr = dwarf2_attr (die, DW_AT_name, cu);
14570 if (attr != NULL && DW_STRING (attr) != NULL)
14571 return NULL;
14572
14573 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14574 if (attr == NULL)
14575 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14576 if (attr == NULL || DW_STRING (attr) == NULL)
14577 return NULL;
14578
14579 /* dwarf2_name had to be already called. */
14580 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14581
14582 /* Strip the base name, keep any leading namespaces/classes. */
14583 base = strrchr (DW_STRING (attr), ':');
14584 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14585 return "";
14586
14587 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14588 &cu->objfile->objfile_obstack);
14589}
14590
fdde2d81 14591/* Return the name of the namespace/class that DIE is defined within,
0114d602 14592 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 14593
0114d602
DJ
14594 For example, if we're within the method foo() in the following
14595 code:
14596
14597 namespace N {
14598 class C {
14599 void foo () {
14600 }
14601 };
14602 }
14603
14604 then determine_prefix on foo's die will return "N::C". */
fdde2d81 14605
0d5cff50 14606static const char *
e142c38c 14607determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 14608{
0114d602
DJ
14609 struct die_info *parent, *spec_die;
14610 struct dwarf2_cu *spec_cu;
14611 struct type *parent_type;
96408a79 14612 char *retval;
63d06c5c 14613
f55ee35c
JK
14614 if (cu->language != language_cplus && cu->language != language_java
14615 && cu->language != language_fortran)
0114d602
DJ
14616 return "";
14617
96408a79
SA
14618 retval = anonymous_struct_prefix (die, cu);
14619 if (retval)
14620 return retval;
14621
0114d602
DJ
14622 /* We have to be careful in the presence of DW_AT_specification.
14623 For example, with GCC 3.4, given the code
14624
14625 namespace N {
14626 void foo() {
14627 // Definition of N::foo.
14628 }
14629 }
14630
14631 then we'll have a tree of DIEs like this:
14632
14633 1: DW_TAG_compile_unit
14634 2: DW_TAG_namespace // N
14635 3: DW_TAG_subprogram // declaration of N::foo
14636 4: DW_TAG_subprogram // definition of N::foo
14637 DW_AT_specification // refers to die #3
14638
14639 Thus, when processing die #4, we have to pretend that we're in
14640 the context of its DW_AT_specification, namely the contex of die
14641 #3. */
14642 spec_cu = cu;
14643 spec_die = die_specification (die, &spec_cu);
14644 if (spec_die == NULL)
14645 parent = die->parent;
14646 else
63d06c5c 14647 {
0114d602
DJ
14648 parent = spec_die->parent;
14649 cu = spec_cu;
63d06c5c 14650 }
0114d602
DJ
14651
14652 if (parent == NULL)
14653 return "";
98bfdba5
PA
14654 else if (parent->building_fullname)
14655 {
14656 const char *name;
14657 const char *parent_name;
14658
14659 /* It has been seen on RealView 2.2 built binaries,
14660 DW_TAG_template_type_param types actually _defined_ as
14661 children of the parent class:
14662
14663 enum E {};
14664 template class <class Enum> Class{};
14665 Class<enum E> class_e;
14666
14667 1: DW_TAG_class_type (Class)
14668 2: DW_TAG_enumeration_type (E)
14669 3: DW_TAG_enumerator (enum1:0)
14670 3: DW_TAG_enumerator (enum2:1)
14671 ...
14672 2: DW_TAG_template_type_param
14673 DW_AT_type DW_FORM_ref_udata (E)
14674
14675 Besides being broken debug info, it can put GDB into an
14676 infinite loop. Consider:
14677
14678 When we're building the full name for Class<E>, we'll start
14679 at Class, and go look over its template type parameters,
14680 finding E. We'll then try to build the full name of E, and
14681 reach here. We're now trying to build the full name of E,
14682 and look over the parent DIE for containing scope. In the
14683 broken case, if we followed the parent DIE of E, we'd again
14684 find Class, and once again go look at its template type
14685 arguments, etc., etc. Simply don't consider such parent die
14686 as source-level parent of this die (it can't be, the language
14687 doesn't allow it), and break the loop here. */
14688 name = dwarf2_name (die, cu);
14689 parent_name = dwarf2_name (parent, cu);
14690 complaint (&symfile_complaints,
14691 _("template param type '%s' defined within parent '%s'"),
14692 name ? name : "<unknown>",
14693 parent_name ? parent_name : "<unknown>");
14694 return "";
14695 }
63d06c5c 14696 else
0114d602
DJ
14697 switch (parent->tag)
14698 {
63d06c5c 14699 case DW_TAG_namespace:
0114d602 14700 parent_type = read_type_die (parent, cu);
acebe513
UW
14701 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14702 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14703 Work around this problem here. */
14704 if (cu->language == language_cplus
14705 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14706 return "";
0114d602
DJ
14707 /* We give a name to even anonymous namespaces. */
14708 return TYPE_TAG_NAME (parent_type);
63d06c5c 14709 case DW_TAG_class_type:
680b30c7 14710 case DW_TAG_interface_type:
63d06c5c 14711 case DW_TAG_structure_type:
0114d602 14712 case DW_TAG_union_type:
f55ee35c 14713 case DW_TAG_module:
0114d602
DJ
14714 parent_type = read_type_die (parent, cu);
14715 if (TYPE_TAG_NAME (parent_type) != NULL)
14716 return TYPE_TAG_NAME (parent_type);
14717 else
14718 /* An anonymous structure is only allowed non-static data
14719 members; no typedefs, no member functions, et cetera.
14720 So it does not need a prefix. */
14721 return "";
abc72ce4 14722 case DW_TAG_compile_unit:
95554aad 14723 case DW_TAG_partial_unit:
abc72ce4
DE
14724 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14725 if (cu->language == language_cplus
8b70b953 14726 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14727 && die->child != NULL
14728 && (die->tag == DW_TAG_class_type
14729 || die->tag == DW_TAG_structure_type
14730 || die->tag == DW_TAG_union_type))
14731 {
14732 char *name = guess_full_die_structure_name (die, cu);
14733 if (name != NULL)
14734 return name;
14735 }
14736 return "";
63d06c5c 14737 default:
8176b9b8 14738 return determine_prefix (parent, cu);
63d06c5c 14739 }
63d06c5c
DC
14740}
14741
3e43a32a
MS
14742/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14743 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14744 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14745 an obconcat, otherwise allocate storage for the result. The CU argument is
14746 used to determine the language and hence, the appropriate separator. */
987504bb 14747
f55ee35c 14748#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
14749
14750static char *
f55ee35c
JK
14751typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14752 int physname, struct dwarf2_cu *cu)
63d06c5c 14753{
f55ee35c 14754 const char *lead = "";
5c315b68 14755 const char *sep;
63d06c5c 14756
3e43a32a
MS
14757 if (suffix == NULL || suffix[0] == '\0'
14758 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
14759 sep = "";
14760 else if (cu->language == language_java)
14761 sep = ".";
f55ee35c
JK
14762 else if (cu->language == language_fortran && physname)
14763 {
14764 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14765 DW_AT_MIPS_linkage_name is preferred and used instead. */
14766
14767 lead = "__";
14768 sep = "_MOD_";
14769 }
987504bb
JJ
14770 else
14771 sep = "::";
63d06c5c 14772
6dd47d34
DE
14773 if (prefix == NULL)
14774 prefix = "";
14775 if (suffix == NULL)
14776 suffix = "";
14777
987504bb
JJ
14778 if (obs == NULL)
14779 {
3e43a32a
MS
14780 char *retval
14781 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 14782
f55ee35c
JK
14783 strcpy (retval, lead);
14784 strcat (retval, prefix);
6dd47d34
DE
14785 strcat (retval, sep);
14786 strcat (retval, suffix);
63d06c5c
DC
14787 return retval;
14788 }
987504bb
JJ
14789 else
14790 {
14791 /* We have an obstack. */
f55ee35c 14792 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 14793 }
63d06c5c
DC
14794}
14795
c906108c
SS
14796/* Return sibling of die, NULL if no sibling. */
14797
f9aca02d 14798static struct die_info *
fba45db2 14799sibling_die (struct die_info *die)
c906108c 14800{
639d11d3 14801 return die->sibling;
c906108c
SS
14802}
14803
71c25dea
TT
14804/* Get name of a die, return NULL if not found. */
14805
14806static char *
14807dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14808 struct obstack *obstack)
14809{
14810 if (name && cu->language == language_cplus)
14811 {
14812 char *canon_name = cp_canonicalize_string (name);
14813
14814 if (canon_name != NULL)
14815 {
14816 if (strcmp (canon_name, name) != 0)
14817 name = obsavestring (canon_name, strlen (canon_name),
14818 obstack);
14819 xfree (canon_name);
14820 }
14821 }
14822
14823 return name;
c906108c
SS
14824}
14825
9219021c
DC
14826/* Get name of a die, return NULL if not found. */
14827
14828static char *
e142c38c 14829dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
14830{
14831 struct attribute *attr;
14832
e142c38c 14833 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
14834 if ((!attr || !DW_STRING (attr))
14835 && die->tag != DW_TAG_class_type
14836 && die->tag != DW_TAG_interface_type
14837 && die->tag != DW_TAG_structure_type
14838 && die->tag != DW_TAG_union_type)
71c25dea
TT
14839 return NULL;
14840
14841 switch (die->tag)
14842 {
14843 case DW_TAG_compile_unit:
95554aad 14844 case DW_TAG_partial_unit:
71c25dea
TT
14845 /* Compilation units have a DW_AT_name that is a filename, not
14846 a source language identifier. */
14847 case DW_TAG_enumeration_type:
14848 case DW_TAG_enumerator:
14849 /* These tags always have simple identifiers already; no need
14850 to canonicalize them. */
14851 return DW_STRING (attr);
907af001 14852
418835cc
KS
14853 case DW_TAG_subprogram:
14854 /* Java constructors will all be named "<init>", so return
14855 the class name when we see this special case. */
14856 if (cu->language == language_java
14857 && DW_STRING (attr) != NULL
14858 && strcmp (DW_STRING (attr), "<init>") == 0)
14859 {
14860 struct dwarf2_cu *spec_cu = cu;
14861 struct die_info *spec_die;
14862
14863 /* GCJ will output '<init>' for Java constructor names.
14864 For this special case, return the name of the parent class. */
14865
14866 /* GCJ may output suprogram DIEs with AT_specification set.
14867 If so, use the name of the specified DIE. */
14868 spec_die = die_specification (die, &spec_cu);
14869 if (spec_die != NULL)
14870 return dwarf2_name (spec_die, spec_cu);
14871
14872 do
14873 {
14874 die = die->parent;
14875 if (die->tag == DW_TAG_class_type)
14876 return dwarf2_name (die, cu);
14877 }
95554aad
TT
14878 while (die->tag != DW_TAG_compile_unit
14879 && die->tag != DW_TAG_partial_unit);
418835cc 14880 }
907af001
UW
14881 break;
14882
14883 case DW_TAG_class_type:
14884 case DW_TAG_interface_type:
14885 case DW_TAG_structure_type:
14886 case DW_TAG_union_type:
14887 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14888 structures or unions. These were of the form "._%d" in GCC 4.1,
14889 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14890 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
14891 if (attr && DW_STRING (attr)
14892 && (strncmp (DW_STRING (attr), "._", 2) == 0
14893 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 14894 return NULL;
53832f31
TT
14895
14896 /* GCC might emit a nameless typedef that has a linkage name. See
14897 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14898 if (!attr || DW_STRING (attr) == NULL)
14899 {
df5c6c50 14900 char *demangled = NULL;
53832f31
TT
14901
14902 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14903 if (attr == NULL)
14904 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14905
14906 if (attr == NULL || DW_STRING (attr) == NULL)
14907 return NULL;
14908
df5c6c50
JK
14909 /* Avoid demangling DW_STRING (attr) the second time on a second
14910 call for the same DIE. */
14911 if (!DW_STRING_IS_CANONICAL (attr))
14912 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
14913
14914 if (demangled)
14915 {
96408a79
SA
14916 char *base;
14917
53832f31 14918 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
14919 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14920 &cu->objfile->objfile_obstack);
53832f31
TT
14921 DW_STRING_IS_CANONICAL (attr) = 1;
14922 xfree (demangled);
96408a79
SA
14923
14924 /* Strip any leading namespaces/classes, keep only the base name.
14925 DW_AT_name for named DIEs does not contain the prefixes. */
14926 base = strrchr (DW_STRING (attr), ':');
14927 if (base && base > DW_STRING (attr) && base[-1] == ':')
14928 return &base[1];
14929 else
14930 return DW_STRING (attr);
53832f31
TT
14931 }
14932 }
907af001
UW
14933 break;
14934
71c25dea 14935 default:
907af001
UW
14936 break;
14937 }
14938
14939 if (!DW_STRING_IS_CANONICAL (attr))
14940 {
14941 DW_STRING (attr)
14942 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14943 &cu->objfile->objfile_obstack);
14944 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 14945 }
907af001 14946 return DW_STRING (attr);
9219021c
DC
14947}
14948
14949/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
14950 is none. *EXT_CU is the CU containing DIE on input, and the CU
14951 containing the return value on output. */
9219021c
DC
14952
14953static struct die_info *
f2f0e013 14954dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
14955{
14956 struct attribute *attr;
9219021c 14957
f2f0e013 14958 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
14959 if (attr == NULL)
14960 return NULL;
14961
f2f0e013 14962 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
14963}
14964
c906108c
SS
14965/* Convert a DIE tag into its string name. */
14966
f39c6ffd 14967static const char *
aa1ee363 14968dwarf_tag_name (unsigned tag)
c906108c 14969{
f39c6ffd
TT
14970 const char *name = get_DW_TAG_name (tag);
14971
14972 if (name == NULL)
14973 return "DW_TAG_<unknown>";
14974
14975 return name;
c906108c
SS
14976}
14977
14978/* Convert a DWARF attribute code into its string name. */
14979
f39c6ffd 14980static const char *
aa1ee363 14981dwarf_attr_name (unsigned attr)
c906108c 14982{
f39c6ffd
TT
14983 const char *name;
14984
c764a876 14985#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
14986 if (attr == DW_AT_MIPS_fde)
14987 return "DW_AT_MIPS_fde";
14988#else
14989 if (attr == DW_AT_HP_block_index)
14990 return "DW_AT_HP_block_index";
c764a876 14991#endif
f39c6ffd
TT
14992
14993 name = get_DW_AT_name (attr);
14994
14995 if (name == NULL)
14996 return "DW_AT_<unknown>";
14997
14998 return name;
c906108c
SS
14999}
15000
15001/* Convert a DWARF value form code into its string name. */
15002
f39c6ffd 15003static const char *
aa1ee363 15004dwarf_form_name (unsigned form)
c906108c 15005{
f39c6ffd
TT
15006 const char *name = get_DW_FORM_name (form);
15007
15008 if (name == NULL)
15009 return "DW_FORM_<unknown>";
15010
15011 return name;
c906108c
SS
15012}
15013
15014static char *
fba45db2 15015dwarf_bool_name (unsigned mybool)
c906108c
SS
15016{
15017 if (mybool)
15018 return "TRUE";
15019 else
15020 return "FALSE";
15021}
15022
15023/* Convert a DWARF type code into its string name. */
15024
f39c6ffd 15025static const char *
aa1ee363 15026dwarf_type_encoding_name (unsigned enc)
c906108c 15027{
f39c6ffd 15028 const char *name = get_DW_ATE_name (enc);
c906108c 15029
f39c6ffd
TT
15030 if (name == NULL)
15031 return "DW_ATE_<unknown>";
c906108c 15032
f39c6ffd 15033 return name;
c906108c 15034}
c906108c 15035
f9aca02d 15036static void
d97bc12b 15037dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
15038{
15039 unsigned int i;
15040
d97bc12b
DE
15041 print_spaces (indent, f);
15042 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 15043 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
15044
15045 if (die->parent != NULL)
15046 {
15047 print_spaces (indent, f);
15048 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 15049 die->parent->offset.sect_off);
d97bc12b
DE
15050 }
15051
15052 print_spaces (indent, f);
15053 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 15054 dwarf_bool_name (die->child != NULL));
c906108c 15055
d97bc12b
DE
15056 print_spaces (indent, f);
15057 fprintf_unfiltered (f, " attributes:\n");
15058
c906108c
SS
15059 for (i = 0; i < die->num_attrs; ++i)
15060 {
d97bc12b
DE
15061 print_spaces (indent, f);
15062 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
15063 dwarf_attr_name (die->attrs[i].name),
15064 dwarf_form_name (die->attrs[i].form));
d97bc12b 15065
c906108c
SS
15066 switch (die->attrs[i].form)
15067 {
c906108c 15068 case DW_FORM_addr:
3019eac3 15069 case DW_FORM_GNU_addr_index:
d97bc12b 15070 fprintf_unfiltered (f, "address: ");
5af949e3 15071 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
15072 break;
15073 case DW_FORM_block2:
15074 case DW_FORM_block4:
15075 case DW_FORM_block:
15076 case DW_FORM_block1:
3e43a32a
MS
15077 fprintf_unfiltered (f, "block: size %d",
15078 DW_BLOCK (&die->attrs[i])->size);
c906108c 15079 break;
2dc7f7b3
TT
15080 case DW_FORM_exprloc:
15081 fprintf_unfiltered (f, "expression: size %u",
15082 DW_BLOCK (&die->attrs[i])->size);
15083 break;
4568ecf9
DE
15084 case DW_FORM_ref_addr:
15085 fprintf_unfiltered (f, "ref address: ");
15086 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
15087 break;
10b3939b
DJ
15088 case DW_FORM_ref1:
15089 case DW_FORM_ref2:
15090 case DW_FORM_ref4:
4568ecf9
DE
15091 case DW_FORM_ref8:
15092 case DW_FORM_ref_udata:
d97bc12b 15093 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 15094 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 15095 break;
c906108c
SS
15096 case DW_FORM_data1:
15097 case DW_FORM_data2:
15098 case DW_FORM_data4:
ce5d95e1 15099 case DW_FORM_data8:
c906108c
SS
15100 case DW_FORM_udata:
15101 case DW_FORM_sdata:
43bbcdc2
PH
15102 fprintf_unfiltered (f, "constant: %s",
15103 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 15104 break;
2dc7f7b3
TT
15105 case DW_FORM_sec_offset:
15106 fprintf_unfiltered (f, "section offset: %s",
15107 pulongest (DW_UNSND (&die->attrs[i])));
15108 break;
55f1336d 15109 case DW_FORM_ref_sig8:
348e048f
DE
15110 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
15111 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 15112 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
15113 else
15114 fprintf_unfiltered (f, "signatured type, offset: unknown");
15115 break;
c906108c 15116 case DW_FORM_string:
4bdf3d34 15117 case DW_FORM_strp:
3019eac3 15118 case DW_FORM_GNU_str_index:
8285870a 15119 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 15120 DW_STRING (&die->attrs[i])
8285870a
JK
15121 ? DW_STRING (&die->attrs[i]) : "",
15122 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
15123 break;
15124 case DW_FORM_flag:
15125 if (DW_UNSND (&die->attrs[i]))
d97bc12b 15126 fprintf_unfiltered (f, "flag: TRUE");
c906108c 15127 else
d97bc12b 15128 fprintf_unfiltered (f, "flag: FALSE");
c906108c 15129 break;
2dc7f7b3
TT
15130 case DW_FORM_flag_present:
15131 fprintf_unfiltered (f, "flag: TRUE");
15132 break;
a8329558 15133 case DW_FORM_indirect:
0963b4bd
MS
15134 /* The reader will have reduced the indirect form to
15135 the "base form" so this form should not occur. */
3e43a32a
MS
15136 fprintf_unfiltered (f,
15137 "unexpected attribute form: DW_FORM_indirect");
a8329558 15138 break;
c906108c 15139 default:
d97bc12b 15140 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 15141 die->attrs[i].form);
d97bc12b 15142 break;
c906108c 15143 }
d97bc12b 15144 fprintf_unfiltered (f, "\n");
c906108c
SS
15145 }
15146}
15147
f9aca02d 15148static void
d97bc12b 15149dump_die_for_error (struct die_info *die)
c906108c 15150{
d97bc12b
DE
15151 dump_die_shallow (gdb_stderr, 0, die);
15152}
15153
15154static void
15155dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
15156{
15157 int indent = level * 4;
15158
15159 gdb_assert (die != NULL);
15160
15161 if (level >= max_level)
15162 return;
15163
15164 dump_die_shallow (f, indent, die);
15165
15166 if (die->child != NULL)
c906108c 15167 {
d97bc12b
DE
15168 print_spaces (indent, f);
15169 fprintf_unfiltered (f, " Children:");
15170 if (level + 1 < max_level)
15171 {
15172 fprintf_unfiltered (f, "\n");
15173 dump_die_1 (f, level + 1, max_level, die->child);
15174 }
15175 else
15176 {
3e43a32a
MS
15177 fprintf_unfiltered (f,
15178 " [not printed, max nesting level reached]\n");
d97bc12b
DE
15179 }
15180 }
15181
15182 if (die->sibling != NULL && level > 0)
15183 {
15184 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
15185 }
15186}
15187
d97bc12b
DE
15188/* This is called from the pdie macro in gdbinit.in.
15189 It's not static so gcc will keep a copy callable from gdb. */
15190
15191void
15192dump_die (struct die_info *die, int max_level)
15193{
15194 dump_die_1 (gdb_stdlog, 0, max_level, die);
15195}
15196
f9aca02d 15197static void
51545339 15198store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15199{
51545339 15200 void **slot;
c906108c 15201
b64f50a1
JK
15202 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
15203 INSERT);
51545339
DJ
15204
15205 *slot = die;
c906108c
SS
15206}
15207
b64f50a1
JK
15208/* DW_ADDR is always stored already as sect_offset; despite for the forms
15209 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
15210
93311388
DE
15211static int
15212is_ref_attr (struct attribute *attr)
c906108c 15213{
c906108c
SS
15214 switch (attr->form)
15215 {
15216 case DW_FORM_ref_addr:
c906108c
SS
15217 case DW_FORM_ref1:
15218 case DW_FORM_ref2:
15219 case DW_FORM_ref4:
613e1657 15220 case DW_FORM_ref8:
c906108c 15221 case DW_FORM_ref_udata:
93311388 15222 return 1;
c906108c 15223 default:
93311388 15224 return 0;
c906108c 15225 }
93311388
DE
15226}
15227
b64f50a1
JK
15228/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
15229 required kind. */
15230
15231static sect_offset
93311388
DE
15232dwarf2_get_ref_die_offset (struct attribute *attr)
15233{
4568ecf9 15234 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 15235
93311388 15236 if (is_ref_attr (attr))
b64f50a1 15237 return retval;
93311388 15238
b64f50a1 15239 retval.sect_off = 0;
93311388
DE
15240 complaint (&symfile_complaints,
15241 _("unsupported die ref attribute form: '%s'"),
15242 dwarf_form_name (attr->form));
b64f50a1 15243 return retval;
c906108c
SS
15244}
15245
43bbcdc2
PH
15246/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
15247 * the value held by the attribute is not constant. */
a02abb62 15248
43bbcdc2 15249static LONGEST
a02abb62
JB
15250dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
15251{
15252 if (attr->form == DW_FORM_sdata)
15253 return DW_SND (attr);
15254 else if (attr->form == DW_FORM_udata
15255 || attr->form == DW_FORM_data1
15256 || attr->form == DW_FORM_data2
15257 || attr->form == DW_FORM_data4
15258 || attr->form == DW_FORM_data8)
15259 return DW_UNSND (attr);
15260 else
15261 {
3e43a32a
MS
15262 complaint (&symfile_complaints,
15263 _("Attribute value is not a constant (%s)"),
a02abb62
JB
15264 dwarf_form_name (attr->form));
15265 return default_value;
15266 }
15267}
15268
348e048f
DE
15269/* Follow reference or signature attribute ATTR of SRC_DIE.
15270 On entry *REF_CU is the CU of SRC_DIE.
15271 On exit *REF_CU is the CU of the result. */
15272
15273static struct die_info *
15274follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
15275 struct dwarf2_cu **ref_cu)
15276{
15277 struct die_info *die;
15278
15279 if (is_ref_attr (attr))
15280 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 15281 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
15282 die = follow_die_sig (src_die, attr, ref_cu);
15283 else
15284 {
15285 dump_die_for_error (src_die);
15286 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
15287 (*ref_cu)->objfile->name);
15288 }
15289
15290 return die;
03dd20cc
DJ
15291}
15292
5c631832 15293/* Follow reference OFFSET.
673bfd45
DE
15294 On entry *REF_CU is the CU of the source die referencing OFFSET.
15295 On exit *REF_CU is the CU of the result.
15296 Returns NULL if OFFSET is invalid. */
f504f079 15297
f9aca02d 15298static struct die_info *
b64f50a1 15299follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
c906108c 15300{
10b3939b 15301 struct die_info temp_die;
f2f0e013 15302 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 15303
348e048f
DE
15304 gdb_assert (cu->per_cu != NULL);
15305
98bfdba5
PA
15306 target_cu = cu;
15307
3019eac3 15308 if (cu->per_cu->is_debug_types)
348e048f
DE
15309 {
15310 /* .debug_types CUs cannot reference anything outside their CU.
15311 If they need to, they have to reference a signatured type via
55f1336d 15312 DW_FORM_ref_sig8. */
348e048f 15313 if (! offset_in_cu_p (&cu->header, offset))
5c631832 15314 return NULL;
348e048f
DE
15315 }
15316 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
15317 {
15318 struct dwarf2_per_cu_data *per_cu;
9a619af0 15319
45452591 15320 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
15321
15322 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
15323 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
15324 load_full_comp_unit (per_cu, cu->language);
03dd20cc 15325
10b3939b
DJ
15326 target_cu = per_cu->cu;
15327 }
98bfdba5
PA
15328 else if (cu->dies == NULL)
15329 {
15330 /* We're loading full DIEs during partial symbol reading. */
15331 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 15332 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 15333 }
c906108c 15334
f2f0e013 15335 *ref_cu = target_cu;
51545339 15336 temp_die.offset = offset;
b64f50a1 15337 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 15338}
10b3939b 15339
5c631832
JK
15340/* Follow reference attribute ATTR of SRC_DIE.
15341 On entry *REF_CU is the CU of SRC_DIE.
15342 On exit *REF_CU is the CU of the result. */
15343
15344static struct die_info *
15345follow_die_ref (struct die_info *src_die, struct attribute *attr,
15346 struct dwarf2_cu **ref_cu)
15347{
b64f50a1 15348 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
15349 struct dwarf2_cu *cu = *ref_cu;
15350 struct die_info *die;
15351
15352 die = follow_die_offset (offset, ref_cu);
15353 if (!die)
15354 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
15355 "at 0x%x [in module %s]"),
b64f50a1 15356 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 15357
5c631832
JK
15358 return die;
15359}
15360
d83e736b
JK
15361/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
15362 Returned value is intended for DW_OP_call*. Returned
15363 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
15364
15365struct dwarf2_locexpr_baton
b64f50a1 15366dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
15367 struct dwarf2_per_cu_data *per_cu,
15368 CORE_ADDR (*get_frame_pc) (void *baton),
15369 void *baton)
5c631832 15370{
b64f50a1 15371 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 15372 struct dwarf2_cu *cu;
5c631832
JK
15373 struct die_info *die;
15374 struct attribute *attr;
15375 struct dwarf2_locexpr_baton retval;
15376
8cf6f0b1
TT
15377 dw2_setup (per_cu->objfile);
15378
918dd910
JK
15379 if (per_cu->cu == NULL)
15380 load_cu (per_cu);
15381 cu = per_cu->cu;
15382
5c631832
JK
15383 die = follow_die_offset (offset, &cu);
15384 if (!die)
15385 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 15386 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15387
15388 attr = dwarf2_attr (die, DW_AT_location, cu);
15389 if (!attr)
15390 {
e103e986
JK
15391 /* DWARF: "If there is no such attribute, then there is no effect.".
15392 DATA is ignored if SIZE is 0. */
5c631832 15393
e103e986 15394 retval.data = NULL;
5c631832
JK
15395 retval.size = 0;
15396 }
8cf6f0b1
TT
15397 else if (attr_form_is_section_offset (attr))
15398 {
15399 struct dwarf2_loclist_baton loclist_baton;
15400 CORE_ADDR pc = (*get_frame_pc) (baton);
15401 size_t size;
15402
15403 fill_in_loclist_baton (cu, &loclist_baton, attr);
15404
15405 retval.data = dwarf2_find_location_expression (&loclist_baton,
15406 &size, pc);
15407 retval.size = size;
15408 }
5c631832
JK
15409 else
15410 {
15411 if (!attr_form_is_block (attr))
15412 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15413 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 15414 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15415
15416 retval.data = DW_BLOCK (attr)->data;
15417 retval.size = DW_BLOCK (attr)->size;
15418 }
15419 retval.per_cu = cu->per_cu;
918dd910 15420
918dd910
JK
15421 age_cached_comp_units ();
15422
5c631832 15423 return retval;
348e048f
DE
15424}
15425
8a9b8146
TT
15426/* Return the type of the DIE at DIE_OFFSET in the CU named by
15427 PER_CU. */
15428
15429struct type *
b64f50a1 15430dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
15431 struct dwarf2_per_cu_data *per_cu)
15432{
b64f50a1
JK
15433 sect_offset die_offset_sect;
15434
8a9b8146 15435 dw2_setup (per_cu->objfile);
b64f50a1
JK
15436
15437 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15438 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
15439}
15440
348e048f
DE
15441/* Follow the signature attribute ATTR in SRC_DIE.
15442 On entry *REF_CU is the CU of SRC_DIE.
15443 On exit *REF_CU is the CU of the result. */
15444
15445static struct die_info *
15446follow_die_sig (struct die_info *src_die, struct attribute *attr,
15447 struct dwarf2_cu **ref_cu)
15448{
15449 struct objfile *objfile = (*ref_cu)->objfile;
15450 struct die_info temp_die;
15451 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15452 struct dwarf2_cu *sig_cu;
15453 struct die_info *die;
15454
15455 /* sig_type will be NULL if the signatured type is missing from
15456 the debug info. */
15457 if (sig_type == NULL)
15458 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15459 "at 0x%x [in module %s]"),
b64f50a1 15460 src_die->offset.sect_off, objfile->name);
348e048f
DE
15461
15462 /* If necessary, add it to the queue and load its DIEs. */
15463
95554aad 15464 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 15465 read_signatured_type (sig_type);
348e048f
DE
15466
15467 gdb_assert (sig_type->per_cu.cu != NULL);
15468
15469 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
15470 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15471 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
15472 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15473 temp_die.offset.sect_off);
348e048f
DE
15474 if (die)
15475 {
15476 *ref_cu = sig_cu;
15477 return die;
15478 }
15479
3e43a32a
MS
15480 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15481 "from DIE at 0x%x [in module %s]"),
b64f50a1 15482 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
15483}
15484
15485/* Given an offset of a signatured type, return its signatured_type. */
15486
15487static struct signatured_type *
8b70b953
TT
15488lookup_signatured_type_at_offset (struct objfile *objfile,
15489 struct dwarf2_section_info *section,
b64f50a1 15490 sect_offset offset)
348e048f 15491{
b64f50a1 15492 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
15493 unsigned int length, initial_length_size;
15494 unsigned int sig_offset;
52dc124a 15495 struct signatured_type find_entry, *sig_type;
348e048f
DE
15496
15497 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15498 sig_offset = (initial_length_size
15499 + 2 /*version*/
15500 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15501 + 1 /*address_size*/);
15502 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 15503 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
15504
15505 /* This is only used to lookup previously recorded types.
15506 If we didn't find it, it's our bug. */
52dc124a
DE
15507 gdb_assert (sig_type != NULL);
15508 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 15509
52dc124a 15510 return sig_type;
348e048f
DE
15511}
15512
e5fe5e75 15513/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
15514
15515static void
e5fe5e75 15516load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 15517{
e5fe5e75 15518 struct objfile *objfile = per_cu->objfile;
3019eac3 15519 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
b64f50a1 15520 sect_offset offset = per_cu->offset;
52dc124a 15521 struct signatured_type *sig_type;
348e048f 15522
8b70b953 15523 dwarf2_read_section (objfile, sect);
be391dca 15524
348e048f 15525 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
15526 hash table lookup. */
15527 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15528 the signature to assert we found the right one.
15529 Ok, but it's a lot of work. We should simplify things so any needed
15530 assert doesn't require all this clumsiness. */
52dc124a 15531 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f 15532
dee91e82 15533 gdb_assert (&sig_type->per_cu == per_cu);
52dc124a 15534 gdb_assert (sig_type->per_cu.cu == NULL);
348e048f 15535
52dc124a 15536 read_signatured_type (sig_type);
348e048f 15537
52dc124a 15538 gdb_assert (sig_type->per_cu.cu != NULL);
348e048f
DE
15539}
15540
dee91e82
DE
15541/* die_reader_func for read_signatured_type.
15542 This is identical to load_full_comp_unit_reader,
15543 but is kept separate for now. */
348e048f
DE
15544
15545static void
dee91e82
DE
15546read_signatured_type_reader (const struct die_reader_specs *reader,
15547 gdb_byte *info_ptr,
15548 struct die_info *comp_unit_die,
15549 int has_children,
15550 void *data)
348e048f 15551{
dee91e82 15552 struct dwarf2_cu *cu = reader->cu;
348e048f 15553
dee91e82
DE
15554 gdb_assert (cu->die_hash == NULL);
15555 cu->die_hash =
15556 htab_create_alloc_ex (cu->header.length / 12,
15557 die_hash,
15558 die_eq,
15559 NULL,
15560 &cu->comp_unit_obstack,
15561 hashtab_obstack_allocate,
15562 dummy_obstack_deallocate);
348e048f 15563
dee91e82
DE
15564 if (has_children)
15565 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15566 &info_ptr, comp_unit_die);
15567 cu->dies = comp_unit_die;
15568 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
15569
15570 /* We try not to read any attributes in this function, because not
9cdd5dbd 15571 all CUs needed for references have been loaded yet, and symbol
348e048f 15572 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
15573 or we won't be able to build types correctly.
15574 Similarly, if we do not read the producer, we can not apply
15575 producer-specific interpretation. */
95554aad 15576 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 15577}
348e048f 15578
3019eac3
DE
15579/* Read in a signatured type and build its CU and DIEs.
15580 If the type is a stub for the real type in a DWO file,
15581 read in the real type from the DWO file as well. */
dee91e82
DE
15582
15583static void
15584read_signatured_type (struct signatured_type *sig_type)
15585{
15586 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 15587
3019eac3 15588 gdb_assert (per_cu->is_debug_types);
dee91e82 15589 gdb_assert (per_cu->cu == NULL);
348e048f 15590
fd820528 15591 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
c906108c
SS
15592}
15593
c906108c
SS
15594/* Decode simple location descriptions.
15595 Given a pointer to a dwarf block that defines a location, compute
15596 the location and return the value.
15597
4cecd739
DJ
15598 NOTE drow/2003-11-18: This function is called in two situations
15599 now: for the address of static or global variables (partial symbols
15600 only) and for offsets into structures which are expected to be
15601 (more or less) constant. The partial symbol case should go away,
15602 and only the constant case should remain. That will let this
15603 function complain more accurately. A few special modes are allowed
15604 without complaint for global variables (for instance, global
15605 register values and thread-local values).
c906108c
SS
15606
15607 A location description containing no operations indicates that the
4cecd739 15608 object is optimized out. The return value is 0 for that case.
6b992462
DJ
15609 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15610 callers will only want a very basic result and this can become a
21ae7a4d
JK
15611 complaint.
15612
15613 Note that stack[0] is unused except as a default error return. */
c906108c
SS
15614
15615static CORE_ADDR
e7c27a73 15616decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 15617{
e7c27a73 15618 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
15619 int i;
15620 int size = blk->size;
15621 gdb_byte *data = blk->data;
15622 CORE_ADDR stack[64];
15623 int stacki;
15624 unsigned int bytes_read, unsnd;
15625 gdb_byte op;
c906108c 15626
21ae7a4d
JK
15627 i = 0;
15628 stacki = 0;
15629 stack[stacki] = 0;
15630 stack[++stacki] = 0;
15631
15632 while (i < size)
15633 {
15634 op = data[i++];
15635 switch (op)
15636 {
15637 case DW_OP_lit0:
15638 case DW_OP_lit1:
15639 case DW_OP_lit2:
15640 case DW_OP_lit3:
15641 case DW_OP_lit4:
15642 case DW_OP_lit5:
15643 case DW_OP_lit6:
15644 case DW_OP_lit7:
15645 case DW_OP_lit8:
15646 case DW_OP_lit9:
15647 case DW_OP_lit10:
15648 case DW_OP_lit11:
15649 case DW_OP_lit12:
15650 case DW_OP_lit13:
15651 case DW_OP_lit14:
15652 case DW_OP_lit15:
15653 case DW_OP_lit16:
15654 case DW_OP_lit17:
15655 case DW_OP_lit18:
15656 case DW_OP_lit19:
15657 case DW_OP_lit20:
15658 case DW_OP_lit21:
15659 case DW_OP_lit22:
15660 case DW_OP_lit23:
15661 case DW_OP_lit24:
15662 case DW_OP_lit25:
15663 case DW_OP_lit26:
15664 case DW_OP_lit27:
15665 case DW_OP_lit28:
15666 case DW_OP_lit29:
15667 case DW_OP_lit30:
15668 case DW_OP_lit31:
15669 stack[++stacki] = op - DW_OP_lit0;
15670 break;
f1bea926 15671
21ae7a4d
JK
15672 case DW_OP_reg0:
15673 case DW_OP_reg1:
15674 case DW_OP_reg2:
15675 case DW_OP_reg3:
15676 case DW_OP_reg4:
15677 case DW_OP_reg5:
15678 case DW_OP_reg6:
15679 case DW_OP_reg7:
15680 case DW_OP_reg8:
15681 case DW_OP_reg9:
15682 case DW_OP_reg10:
15683 case DW_OP_reg11:
15684 case DW_OP_reg12:
15685 case DW_OP_reg13:
15686 case DW_OP_reg14:
15687 case DW_OP_reg15:
15688 case DW_OP_reg16:
15689 case DW_OP_reg17:
15690 case DW_OP_reg18:
15691 case DW_OP_reg19:
15692 case DW_OP_reg20:
15693 case DW_OP_reg21:
15694 case DW_OP_reg22:
15695 case DW_OP_reg23:
15696 case DW_OP_reg24:
15697 case DW_OP_reg25:
15698 case DW_OP_reg26:
15699 case DW_OP_reg27:
15700 case DW_OP_reg28:
15701 case DW_OP_reg29:
15702 case DW_OP_reg30:
15703 case DW_OP_reg31:
15704 stack[++stacki] = op - DW_OP_reg0;
15705 if (i < size)
15706 dwarf2_complex_location_expr_complaint ();
15707 break;
c906108c 15708
21ae7a4d
JK
15709 case DW_OP_regx:
15710 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15711 i += bytes_read;
15712 stack[++stacki] = unsnd;
15713 if (i < size)
15714 dwarf2_complex_location_expr_complaint ();
15715 break;
c906108c 15716
21ae7a4d
JK
15717 case DW_OP_addr:
15718 stack[++stacki] = read_address (objfile->obfd, &data[i],
15719 cu, &bytes_read);
15720 i += bytes_read;
15721 break;
d53d4ac5 15722
21ae7a4d
JK
15723 case DW_OP_const1u:
15724 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15725 i += 1;
15726 break;
15727
15728 case DW_OP_const1s:
15729 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15730 i += 1;
15731 break;
15732
15733 case DW_OP_const2u:
15734 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15735 i += 2;
15736 break;
15737
15738 case DW_OP_const2s:
15739 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15740 i += 2;
15741 break;
d53d4ac5 15742
21ae7a4d
JK
15743 case DW_OP_const4u:
15744 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15745 i += 4;
15746 break;
15747
15748 case DW_OP_const4s:
15749 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15750 i += 4;
15751 break;
15752
585861ea
JK
15753 case DW_OP_const8u:
15754 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15755 i += 8;
15756 break;
15757
21ae7a4d
JK
15758 case DW_OP_constu:
15759 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15760 &bytes_read);
15761 i += bytes_read;
15762 break;
15763
15764 case DW_OP_consts:
15765 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15766 i += bytes_read;
15767 break;
15768
15769 case DW_OP_dup:
15770 stack[stacki + 1] = stack[stacki];
15771 stacki++;
15772 break;
15773
15774 case DW_OP_plus:
15775 stack[stacki - 1] += stack[stacki];
15776 stacki--;
15777 break;
15778
15779 case DW_OP_plus_uconst:
15780 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15781 &bytes_read);
15782 i += bytes_read;
15783 break;
15784
15785 case DW_OP_minus:
15786 stack[stacki - 1] -= stack[stacki];
15787 stacki--;
15788 break;
15789
15790 case DW_OP_deref:
15791 /* If we're not the last op, then we definitely can't encode
15792 this using GDB's address_class enum. This is valid for partial
15793 global symbols, although the variable's address will be bogus
15794 in the psymtab. */
15795 if (i < size)
15796 dwarf2_complex_location_expr_complaint ();
15797 break;
15798
15799 case DW_OP_GNU_push_tls_address:
15800 /* The top of the stack has the offset from the beginning
15801 of the thread control block at which the variable is located. */
15802 /* Nothing should follow this operator, so the top of stack would
15803 be returned. */
15804 /* This is valid for partial global symbols, but the variable's
585861ea
JK
15805 address will be bogus in the psymtab. Make it always at least
15806 non-zero to not look as a variable garbage collected by linker
15807 which have DW_OP_addr 0. */
21ae7a4d
JK
15808 if (i < size)
15809 dwarf2_complex_location_expr_complaint ();
585861ea 15810 stack[stacki]++;
21ae7a4d
JK
15811 break;
15812
15813 case DW_OP_GNU_uninit:
15814 break;
15815
3019eac3 15816 case DW_OP_GNU_addr_index:
49f6c839 15817 case DW_OP_GNU_const_index:
3019eac3
DE
15818 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15819 &bytes_read);
15820 i += bytes_read;
15821 break;
15822
21ae7a4d
JK
15823 default:
15824 {
f39c6ffd 15825 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
15826
15827 if (name)
15828 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15829 name);
15830 else
15831 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15832 op);
15833 }
15834
15835 return (stack[stacki]);
d53d4ac5 15836 }
3c6e0cb3 15837
21ae7a4d
JK
15838 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15839 outside of the allocated space. Also enforce minimum>0. */
15840 if (stacki >= ARRAY_SIZE (stack) - 1)
15841 {
15842 complaint (&symfile_complaints,
15843 _("location description stack overflow"));
15844 return 0;
15845 }
15846
15847 if (stacki <= 0)
15848 {
15849 complaint (&symfile_complaints,
15850 _("location description stack underflow"));
15851 return 0;
15852 }
15853 }
15854 return (stack[stacki]);
c906108c
SS
15855}
15856
15857/* memory allocation interface */
15858
c906108c 15859static struct dwarf_block *
7b5a2f43 15860dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
15861{
15862 struct dwarf_block *blk;
15863
15864 blk = (struct dwarf_block *)
7b5a2f43 15865 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
15866 return (blk);
15867}
15868
c906108c 15869static struct die_info *
b60c80d6 15870dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
15871{
15872 struct die_info *die;
b60c80d6
DJ
15873 size_t size = sizeof (struct die_info);
15874
15875 if (num_attrs > 1)
15876 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 15877
b60c80d6 15878 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
15879 memset (die, 0, sizeof (struct die_info));
15880 return (die);
15881}
2e276125
JB
15882
15883\f
15884/* Macro support. */
15885
2e276125
JB
15886/* Return the full name of file number I in *LH's file name table.
15887 Use COMP_DIR as the name of the current directory of the
15888 compilation. The result is allocated using xmalloc; the caller is
15889 responsible for freeing it. */
15890static char *
15891file_full_name (int file, struct line_header *lh, const char *comp_dir)
15892{
6a83a1e6
EZ
15893 /* Is the file number a valid index into the line header's file name
15894 table? Remember that file numbers start with one, not zero. */
15895 if (1 <= file && file <= lh->num_file_names)
15896 {
15897 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 15898
6a83a1e6
EZ
15899 if (IS_ABSOLUTE_PATH (fe->name))
15900 return xstrdup (fe->name);
15901 else
15902 {
15903 const char *dir;
15904 int dir_len;
15905 char *full_name;
15906
15907 if (fe->dir_index)
15908 dir = lh->include_dirs[fe->dir_index - 1];
15909 else
15910 dir = comp_dir;
15911
15912 if (dir)
15913 {
15914 dir_len = strlen (dir);
15915 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15916 strcpy (full_name, dir);
15917 full_name[dir_len] = '/';
15918 strcpy (full_name + dir_len + 1, fe->name);
15919 return full_name;
15920 }
15921 else
15922 return xstrdup (fe->name);
15923 }
15924 }
2e276125
JB
15925 else
15926 {
6a83a1e6
EZ
15927 /* The compiler produced a bogus file number. We can at least
15928 record the macro definitions made in the file, even if we
15929 won't be able to find the file by name. */
15930 char fake_name[80];
9a619af0 15931
6a83a1e6 15932 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 15933
6e70227d 15934 complaint (&symfile_complaints,
6a83a1e6
EZ
15935 _("bad file number in macro information (%d)"),
15936 file);
2e276125 15937
6a83a1e6 15938 return xstrdup (fake_name);
2e276125
JB
15939 }
15940}
15941
15942
15943static struct macro_source_file *
15944macro_start_file (int file, int line,
15945 struct macro_source_file *current_file,
15946 const char *comp_dir,
15947 struct line_header *lh, struct objfile *objfile)
15948{
15949 /* The full name of this source file. */
15950 char *full_name = file_full_name (file, lh, comp_dir);
15951
15952 /* We don't create a macro table for this compilation unit
15953 at all until we actually get a filename. */
15954 if (! pending_macros)
4a146b47 15955 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 15956 objfile->macro_cache);
2e276125
JB
15957
15958 if (! current_file)
abc9d0dc
TT
15959 {
15960 /* If we have no current file, then this must be the start_file
15961 directive for the compilation unit's main source file. */
15962 current_file = macro_set_main (pending_macros, full_name);
15963 macro_define_special (pending_macros);
15964 }
2e276125
JB
15965 else
15966 current_file = macro_include (current_file, line, full_name);
15967
15968 xfree (full_name);
6e70227d 15969
2e276125
JB
15970 return current_file;
15971}
15972
15973
15974/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15975 followed by a null byte. */
15976static char *
15977copy_string (const char *buf, int len)
15978{
15979 char *s = xmalloc (len + 1);
9a619af0 15980
2e276125
JB
15981 memcpy (s, buf, len);
15982 s[len] = '\0';
2e276125
JB
15983 return s;
15984}
15985
15986
15987static const char *
15988consume_improper_spaces (const char *p, const char *body)
15989{
15990 if (*p == ' ')
15991 {
4d3c2250 15992 complaint (&symfile_complaints,
3e43a32a
MS
15993 _("macro definition contains spaces "
15994 "in formal argument list:\n`%s'"),
4d3c2250 15995 body);
2e276125
JB
15996
15997 while (*p == ' ')
15998 p++;
15999 }
16000
16001 return p;
16002}
16003
16004
16005static void
16006parse_macro_definition (struct macro_source_file *file, int line,
16007 const char *body)
16008{
16009 const char *p;
16010
16011 /* The body string takes one of two forms. For object-like macro
16012 definitions, it should be:
16013
16014 <macro name> " " <definition>
16015
16016 For function-like macro definitions, it should be:
16017
16018 <macro name> "() " <definition>
16019 or
16020 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
16021
16022 Spaces may appear only where explicitly indicated, and in the
16023 <definition>.
16024
16025 The Dwarf 2 spec says that an object-like macro's name is always
16026 followed by a space, but versions of GCC around March 2002 omit
6e70227d 16027 the space when the macro's definition is the empty string.
2e276125
JB
16028
16029 The Dwarf 2 spec says that there should be no spaces between the
16030 formal arguments in a function-like macro's formal argument list,
16031 but versions of GCC around March 2002 include spaces after the
16032 commas. */
16033
16034
16035 /* Find the extent of the macro name. The macro name is terminated
16036 by either a space or null character (for an object-like macro) or
16037 an opening paren (for a function-like macro). */
16038 for (p = body; *p; p++)
16039 if (*p == ' ' || *p == '(')
16040 break;
16041
16042 if (*p == ' ' || *p == '\0')
16043 {
16044 /* It's an object-like macro. */
16045 int name_len = p - body;
16046 char *name = copy_string (body, name_len);
16047 const char *replacement;
16048
16049 if (*p == ' ')
16050 replacement = body + name_len + 1;
16051 else
16052 {
4d3c2250 16053 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16054 replacement = body + name_len;
16055 }
6e70227d 16056
2e276125
JB
16057 macro_define_object (file, line, name, replacement);
16058
16059 xfree (name);
16060 }
16061 else if (*p == '(')
16062 {
16063 /* It's a function-like macro. */
16064 char *name = copy_string (body, p - body);
16065 int argc = 0;
16066 int argv_size = 1;
16067 char **argv = xmalloc (argv_size * sizeof (*argv));
16068
16069 p++;
16070
16071 p = consume_improper_spaces (p, body);
16072
16073 /* Parse the formal argument list. */
16074 while (*p && *p != ')')
16075 {
16076 /* Find the extent of the current argument name. */
16077 const char *arg_start = p;
16078
16079 while (*p && *p != ',' && *p != ')' && *p != ' ')
16080 p++;
16081
16082 if (! *p || p == arg_start)
4d3c2250 16083 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16084 else
16085 {
16086 /* Make sure argv has room for the new argument. */
16087 if (argc >= argv_size)
16088 {
16089 argv_size *= 2;
16090 argv = xrealloc (argv, argv_size * sizeof (*argv));
16091 }
16092
16093 argv[argc++] = copy_string (arg_start, p - arg_start);
16094 }
16095
16096 p = consume_improper_spaces (p, body);
16097
16098 /* Consume the comma, if present. */
16099 if (*p == ',')
16100 {
16101 p++;
16102
16103 p = consume_improper_spaces (p, body);
16104 }
16105 }
16106
16107 if (*p == ')')
16108 {
16109 p++;
16110
16111 if (*p == ' ')
16112 /* Perfectly formed definition, no complaints. */
16113 macro_define_function (file, line, name,
6e70227d 16114 argc, (const char **) argv,
2e276125
JB
16115 p + 1);
16116 else if (*p == '\0')
16117 {
16118 /* Complain, but do define it. */
4d3c2250 16119 dwarf2_macro_malformed_definition_complaint (body);
2e276125 16120 macro_define_function (file, line, name,
6e70227d 16121 argc, (const char **) argv,
2e276125
JB
16122 p);
16123 }
16124 else
16125 /* Just complain. */
4d3c2250 16126 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16127 }
16128 else
16129 /* Just complain. */
4d3c2250 16130 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16131
16132 xfree (name);
16133 {
16134 int i;
16135
16136 for (i = 0; i < argc; i++)
16137 xfree (argv[i]);
16138 }
16139 xfree (argv);
16140 }
16141 else
4d3c2250 16142 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
16143}
16144
cf2c3c16
TT
16145/* Skip some bytes from BYTES according to the form given in FORM.
16146 Returns the new pointer. */
2e276125 16147
cf2c3c16 16148static gdb_byte *
f664829e 16149skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
16150 enum dwarf_form form,
16151 unsigned int offset_size,
16152 struct dwarf2_section_info *section)
2e276125 16153{
cf2c3c16 16154 unsigned int bytes_read;
2e276125 16155
cf2c3c16 16156 switch (form)
2e276125 16157 {
cf2c3c16
TT
16158 case DW_FORM_data1:
16159 case DW_FORM_flag:
16160 ++bytes;
16161 break;
16162
16163 case DW_FORM_data2:
16164 bytes += 2;
16165 break;
16166
16167 case DW_FORM_data4:
16168 bytes += 4;
16169 break;
16170
16171 case DW_FORM_data8:
16172 bytes += 8;
16173 break;
16174
16175 case DW_FORM_string:
16176 read_direct_string (abfd, bytes, &bytes_read);
16177 bytes += bytes_read;
16178 break;
16179
16180 case DW_FORM_sec_offset:
16181 case DW_FORM_strp:
16182 bytes += offset_size;
16183 break;
16184
16185 case DW_FORM_block:
16186 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
16187 bytes += bytes_read;
16188 break;
16189
16190 case DW_FORM_block1:
16191 bytes += 1 + read_1_byte (abfd, bytes);
16192 break;
16193 case DW_FORM_block2:
16194 bytes += 2 + read_2_bytes (abfd, bytes);
16195 break;
16196 case DW_FORM_block4:
16197 bytes += 4 + read_4_bytes (abfd, bytes);
16198 break;
16199
16200 case DW_FORM_sdata:
16201 case DW_FORM_udata:
3019eac3
DE
16202 case DW_FORM_GNU_addr_index:
16203 case DW_FORM_GNU_str_index:
f664829e
DE
16204 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
16205 if (bytes == NULL)
16206 {
16207 dwarf2_section_buffer_overflow_complaint (section);
16208 return NULL;
16209 }
cf2c3c16
TT
16210 break;
16211
16212 default:
16213 {
16214 complain:
16215 complaint (&symfile_complaints,
16216 _("invalid form 0x%x in `%s'"),
16217 form,
16218 section->asection->name);
16219 return NULL;
16220 }
2e276125
JB
16221 }
16222
cf2c3c16
TT
16223 return bytes;
16224}
757a13d0 16225
cf2c3c16
TT
16226/* A helper for dwarf_decode_macros that handles skipping an unknown
16227 opcode. Returns an updated pointer to the macro data buffer; or,
16228 on error, issues a complaint and returns NULL. */
757a13d0 16229
cf2c3c16
TT
16230static gdb_byte *
16231skip_unknown_opcode (unsigned int opcode,
16232 gdb_byte **opcode_definitions,
f664829e 16233 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
16234 bfd *abfd,
16235 unsigned int offset_size,
16236 struct dwarf2_section_info *section)
16237{
16238 unsigned int bytes_read, i;
16239 unsigned long arg;
16240 gdb_byte *defn;
2e276125 16241
cf2c3c16 16242 if (opcode_definitions[opcode] == NULL)
2e276125 16243 {
cf2c3c16
TT
16244 complaint (&symfile_complaints,
16245 _("unrecognized DW_MACFINO opcode 0x%x"),
16246 opcode);
16247 return NULL;
16248 }
2e276125 16249
cf2c3c16
TT
16250 defn = opcode_definitions[opcode];
16251 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
16252 defn += bytes_read;
2e276125 16253
cf2c3c16
TT
16254 for (i = 0; i < arg; ++i)
16255 {
f664829e
DE
16256 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
16257 section);
cf2c3c16
TT
16258 if (mac_ptr == NULL)
16259 {
16260 /* skip_form_bytes already issued the complaint. */
16261 return NULL;
16262 }
16263 }
757a13d0 16264
cf2c3c16
TT
16265 return mac_ptr;
16266}
757a13d0 16267
cf2c3c16
TT
16268/* A helper function which parses the header of a macro section.
16269 If the macro section is the extended (for now called "GNU") type,
16270 then this updates *OFFSET_SIZE. Returns a pointer to just after
16271 the header, or issues a complaint and returns NULL on error. */
757a13d0 16272
cf2c3c16
TT
16273static gdb_byte *
16274dwarf_parse_macro_header (gdb_byte **opcode_definitions,
16275 bfd *abfd,
16276 gdb_byte *mac_ptr,
16277 unsigned int *offset_size,
16278 int section_is_gnu)
16279{
16280 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 16281
cf2c3c16
TT
16282 if (section_is_gnu)
16283 {
16284 unsigned int version, flags;
757a13d0 16285
cf2c3c16
TT
16286 version = read_2_bytes (abfd, mac_ptr);
16287 if (version != 4)
16288 {
16289 complaint (&symfile_complaints,
16290 _("unrecognized version `%d' in .debug_macro section"),
16291 version);
16292 return NULL;
16293 }
16294 mac_ptr += 2;
757a13d0 16295
cf2c3c16
TT
16296 flags = read_1_byte (abfd, mac_ptr);
16297 ++mac_ptr;
16298 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 16299
cf2c3c16
TT
16300 if ((flags & 2) != 0)
16301 /* We don't need the line table offset. */
16302 mac_ptr += *offset_size;
757a13d0 16303
cf2c3c16
TT
16304 /* Vendor opcode descriptions. */
16305 if ((flags & 4) != 0)
16306 {
16307 unsigned int i, count;
757a13d0 16308
cf2c3c16
TT
16309 count = read_1_byte (abfd, mac_ptr);
16310 ++mac_ptr;
16311 for (i = 0; i < count; ++i)
16312 {
16313 unsigned int opcode, bytes_read;
16314 unsigned long arg;
16315
16316 opcode = read_1_byte (abfd, mac_ptr);
16317 ++mac_ptr;
16318 opcode_definitions[opcode] = mac_ptr;
16319 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16320 mac_ptr += bytes_read;
16321 mac_ptr += arg;
16322 }
757a13d0 16323 }
cf2c3c16 16324 }
757a13d0 16325
cf2c3c16
TT
16326 return mac_ptr;
16327}
757a13d0 16328
cf2c3c16 16329/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 16330 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
16331
16332static void
16333dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
16334 struct macro_source_file *current_file,
16335 struct line_header *lh, char *comp_dir,
16336 struct dwarf2_section_info *section,
16337 int section_is_gnu,
16338 unsigned int offset_size,
8fc3fc34
TT
16339 struct objfile *objfile,
16340 htab_t include_hash)
cf2c3c16
TT
16341{
16342 enum dwarf_macro_record_type macinfo_type;
16343 int at_commandline;
16344 gdb_byte *opcode_definitions[256];
757a13d0 16345
cf2c3c16
TT
16346 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16347 &offset_size, section_is_gnu);
16348 if (mac_ptr == NULL)
16349 {
16350 /* We already issued a complaint. */
16351 return;
16352 }
757a13d0
JK
16353
16354 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
16355 GDB is still reading the definitions from command line. First
16356 DW_MACINFO_start_file will need to be ignored as it was already executed
16357 to create CURRENT_FILE for the main source holding also the command line
16358 definitions. On first met DW_MACINFO_start_file this flag is reset to
16359 normally execute all the remaining DW_MACINFO_start_file macinfos. */
16360
16361 at_commandline = 1;
16362
16363 do
16364 {
16365 /* Do we at least have room for a macinfo type byte? */
16366 if (mac_ptr >= mac_end)
16367 {
f664829e 16368 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
16369 break;
16370 }
16371
16372 macinfo_type = read_1_byte (abfd, mac_ptr);
16373 mac_ptr++;
16374
cf2c3c16
TT
16375 /* Note that we rely on the fact that the corresponding GNU and
16376 DWARF constants are the same. */
757a13d0
JK
16377 switch (macinfo_type)
16378 {
16379 /* A zero macinfo type indicates the end of the macro
16380 information. */
16381 case 0:
16382 break;
2e276125 16383
cf2c3c16
TT
16384 case DW_MACRO_GNU_define:
16385 case DW_MACRO_GNU_undef:
16386 case DW_MACRO_GNU_define_indirect:
16387 case DW_MACRO_GNU_undef_indirect:
2e276125 16388 {
891d2f0b 16389 unsigned int bytes_read;
2e276125
JB
16390 int line;
16391 char *body;
cf2c3c16 16392 int is_define;
2e276125 16393
cf2c3c16
TT
16394 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16395 mac_ptr += bytes_read;
16396
16397 if (macinfo_type == DW_MACRO_GNU_define
16398 || macinfo_type == DW_MACRO_GNU_undef)
16399 {
16400 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16401 mac_ptr += bytes_read;
16402 }
16403 else
16404 {
16405 LONGEST str_offset;
16406
16407 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16408 mac_ptr += offset_size;
2e276125 16409
cf2c3c16
TT
16410 body = read_indirect_string_at_offset (abfd, str_offset);
16411 }
16412
16413 is_define = (macinfo_type == DW_MACRO_GNU_define
16414 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 16415 if (! current_file)
757a13d0
JK
16416 {
16417 /* DWARF violation as no main source is present. */
16418 complaint (&symfile_complaints,
16419 _("debug info with no main source gives macro %s "
16420 "on line %d: %s"),
cf2c3c16
TT
16421 is_define ? _("definition") : _("undefinition"),
16422 line, body);
757a13d0
JK
16423 break;
16424 }
3e43a32a
MS
16425 if ((line == 0 && !at_commandline)
16426 || (line != 0 && at_commandline))
4d3c2250 16427 complaint (&symfile_complaints,
757a13d0
JK
16428 _("debug info gives %s macro %s with %s line %d: %s"),
16429 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 16430 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
16431 line == 0 ? _("zero") : _("non-zero"), line, body);
16432
cf2c3c16 16433 if (is_define)
757a13d0 16434 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
16435 else
16436 {
16437 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16438 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16439 macro_undef (current_file, line, body);
16440 }
2e276125
JB
16441 }
16442 break;
16443
cf2c3c16 16444 case DW_MACRO_GNU_start_file:
2e276125 16445 {
891d2f0b 16446 unsigned int bytes_read;
2e276125
JB
16447 int line, file;
16448
16449 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16450 mac_ptr += bytes_read;
16451 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16452 mac_ptr += bytes_read;
16453
3e43a32a
MS
16454 if ((line == 0 && !at_commandline)
16455 || (line != 0 && at_commandline))
757a13d0
JK
16456 complaint (&symfile_complaints,
16457 _("debug info gives source %d included "
16458 "from %s at %s line %d"),
16459 file, at_commandline ? _("command-line") : _("file"),
16460 line == 0 ? _("zero") : _("non-zero"), line);
16461
16462 if (at_commandline)
16463 {
cf2c3c16
TT
16464 /* This DW_MACRO_GNU_start_file was executed in the
16465 pass one. */
757a13d0
JK
16466 at_commandline = 0;
16467 }
16468 else
16469 current_file = macro_start_file (file, line,
16470 current_file, comp_dir,
cf2c3c16 16471 lh, objfile);
2e276125
JB
16472 }
16473 break;
16474
cf2c3c16 16475 case DW_MACRO_GNU_end_file:
2e276125 16476 if (! current_file)
4d3c2250 16477 complaint (&symfile_complaints,
3e43a32a
MS
16478 _("macro debug info has an unmatched "
16479 "`close_file' directive"));
2e276125
JB
16480 else
16481 {
16482 current_file = current_file->included_by;
16483 if (! current_file)
16484 {
cf2c3c16 16485 enum dwarf_macro_record_type next_type;
2e276125
JB
16486
16487 /* GCC circa March 2002 doesn't produce the zero
16488 type byte marking the end of the compilation
16489 unit. Complain if it's not there, but exit no
16490 matter what. */
16491
16492 /* Do we at least have room for a macinfo type byte? */
16493 if (mac_ptr >= mac_end)
16494 {
f664829e 16495 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
16496 return;
16497 }
16498
16499 /* We don't increment mac_ptr here, so this is just
16500 a look-ahead. */
16501 next_type = read_1_byte (abfd, mac_ptr);
16502 if (next_type != 0)
4d3c2250 16503 complaint (&symfile_complaints,
3e43a32a
MS
16504 _("no terminating 0-type entry for "
16505 "macros in `.debug_macinfo' section"));
2e276125
JB
16506
16507 return;
16508 }
16509 }
16510 break;
16511
cf2c3c16
TT
16512 case DW_MACRO_GNU_transparent_include:
16513 {
16514 LONGEST offset;
8fc3fc34 16515 void **slot;
cf2c3c16
TT
16516
16517 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16518 mac_ptr += offset_size;
16519
8fc3fc34
TT
16520 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16521 if (*slot != NULL)
16522 {
16523 /* This has actually happened; see
16524 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16525 complaint (&symfile_complaints,
16526 _("recursive DW_MACRO_GNU_transparent_include in "
16527 ".debug_macro section"));
16528 }
16529 else
16530 {
16531 *slot = mac_ptr;
16532
16533 dwarf_decode_macro_bytes (abfd,
16534 section->buffer + offset,
16535 mac_end, current_file,
16536 lh, comp_dir,
16537 section, section_is_gnu,
16538 offset_size, objfile, include_hash);
16539
16540 htab_remove_elt (include_hash, mac_ptr);
16541 }
cf2c3c16
TT
16542 }
16543 break;
16544
2e276125 16545 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
16546 if (!section_is_gnu)
16547 {
16548 unsigned int bytes_read;
16549 int constant;
2e276125 16550
cf2c3c16
TT
16551 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16552 mac_ptr += bytes_read;
16553 read_direct_string (abfd, mac_ptr, &bytes_read);
16554 mac_ptr += bytes_read;
2e276125 16555
cf2c3c16
TT
16556 /* We don't recognize any vendor extensions. */
16557 break;
16558 }
16559 /* FALLTHROUGH */
16560
16561 default:
16562 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 16563 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
16564 section);
16565 if (mac_ptr == NULL)
16566 return;
16567 break;
2e276125 16568 }
757a13d0 16569 } while (macinfo_type != 0);
2e276125 16570}
8e19ed76 16571
cf2c3c16 16572static void
09262596
DE
16573dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
16574 char *comp_dir, int section_is_gnu)
cf2c3c16 16575{
bb5ed363 16576 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
16577 struct line_header *lh = cu->line_header;
16578 bfd *abfd;
cf2c3c16
TT
16579 gdb_byte *mac_ptr, *mac_end;
16580 struct macro_source_file *current_file = 0;
16581 enum dwarf_macro_record_type macinfo_type;
16582 unsigned int offset_size = cu->header.offset_size;
16583 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
16584 struct cleanup *cleanup;
16585 htab_t include_hash;
16586 void **slot;
09262596
DE
16587 struct dwarf2_section_info *section;
16588 const char *section_name;
16589
16590 if (cu->dwo_unit != NULL)
16591 {
16592 if (section_is_gnu)
16593 {
16594 section = &cu->dwo_unit->dwo_file->sections.macro;
16595 section_name = ".debug_macro.dwo";
16596 }
16597 else
16598 {
16599 section = &cu->dwo_unit->dwo_file->sections.macinfo;
16600 section_name = ".debug_macinfo.dwo";
16601 }
16602 }
16603 else
16604 {
16605 if (section_is_gnu)
16606 {
16607 section = &dwarf2_per_objfile->macro;
16608 section_name = ".debug_macro";
16609 }
16610 else
16611 {
16612 section = &dwarf2_per_objfile->macinfo;
16613 section_name = ".debug_macinfo";
16614 }
16615 }
cf2c3c16 16616
bb5ed363 16617 dwarf2_read_section (objfile, section);
cf2c3c16
TT
16618 if (section->buffer == NULL)
16619 {
fceca515 16620 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
16621 return;
16622 }
09262596 16623 abfd = section->asection->owner;
cf2c3c16
TT
16624
16625 /* First pass: Find the name of the base filename.
16626 This filename is needed in order to process all macros whose definition
16627 (or undefinition) comes from the command line. These macros are defined
16628 before the first DW_MACINFO_start_file entry, and yet still need to be
16629 associated to the base file.
16630
16631 To determine the base file name, we scan the macro definitions until we
16632 reach the first DW_MACINFO_start_file entry. We then initialize
16633 CURRENT_FILE accordingly so that any macro definition found before the
16634 first DW_MACINFO_start_file can still be associated to the base file. */
16635
16636 mac_ptr = section->buffer + offset;
16637 mac_end = section->buffer + section->size;
16638
16639 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16640 &offset_size, section_is_gnu);
16641 if (mac_ptr == NULL)
16642 {
16643 /* We already issued a complaint. */
16644 return;
16645 }
16646
16647 do
16648 {
16649 /* Do we at least have room for a macinfo type byte? */
16650 if (mac_ptr >= mac_end)
16651 {
16652 /* Complaint is printed during the second pass as GDB will probably
16653 stop the first pass earlier upon finding
16654 DW_MACINFO_start_file. */
16655 break;
16656 }
16657
16658 macinfo_type = read_1_byte (abfd, mac_ptr);
16659 mac_ptr++;
16660
16661 /* Note that we rely on the fact that the corresponding GNU and
16662 DWARF constants are the same. */
16663 switch (macinfo_type)
16664 {
16665 /* A zero macinfo type indicates the end of the macro
16666 information. */
16667 case 0:
16668 break;
16669
16670 case DW_MACRO_GNU_define:
16671 case DW_MACRO_GNU_undef:
16672 /* Only skip the data by MAC_PTR. */
16673 {
16674 unsigned int bytes_read;
16675
16676 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16677 mac_ptr += bytes_read;
16678 read_direct_string (abfd, mac_ptr, &bytes_read);
16679 mac_ptr += bytes_read;
16680 }
16681 break;
16682
16683 case DW_MACRO_GNU_start_file:
16684 {
16685 unsigned int bytes_read;
16686 int line, file;
16687
16688 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16689 mac_ptr += bytes_read;
16690 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16691 mac_ptr += bytes_read;
16692
16693 current_file = macro_start_file (file, line, current_file,
bb5ed363 16694 comp_dir, lh, objfile);
cf2c3c16
TT
16695 }
16696 break;
16697
16698 case DW_MACRO_GNU_end_file:
16699 /* No data to skip by MAC_PTR. */
16700 break;
16701
16702 case DW_MACRO_GNU_define_indirect:
16703 case DW_MACRO_GNU_undef_indirect:
16704 {
16705 unsigned int bytes_read;
16706
16707 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16708 mac_ptr += bytes_read;
16709 mac_ptr += offset_size;
16710 }
16711 break;
16712
16713 case DW_MACRO_GNU_transparent_include:
16714 /* Note that, according to the spec, a transparent include
16715 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16716 skip this opcode. */
16717 mac_ptr += offset_size;
16718 break;
16719
16720 case DW_MACINFO_vendor_ext:
16721 /* Only skip the data by MAC_PTR. */
16722 if (!section_is_gnu)
16723 {
16724 unsigned int bytes_read;
16725
16726 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16727 mac_ptr += bytes_read;
16728 read_direct_string (abfd, mac_ptr, &bytes_read);
16729 mac_ptr += bytes_read;
16730 }
16731 /* FALLTHROUGH */
16732
16733 default:
16734 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 16735 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
16736 section);
16737 if (mac_ptr == NULL)
16738 return;
16739 break;
16740 }
16741 } while (macinfo_type != 0 && current_file == NULL);
16742
16743 /* Second pass: Process all entries.
16744
16745 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16746 command-line macro definitions/undefinitions. This flag is unset when we
16747 reach the first DW_MACINFO_start_file entry. */
16748
8fc3fc34
TT
16749 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16750 NULL, xcalloc, xfree);
16751 cleanup = make_cleanup_htab_delete (include_hash);
16752 mac_ptr = section->buffer + offset;
16753 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16754 *slot = mac_ptr;
16755 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
cf2c3c16 16756 current_file, lh, comp_dir, section, section_is_gnu,
8fc3fc34
TT
16757 offset_size, objfile, include_hash);
16758 do_cleanups (cleanup);
cf2c3c16
TT
16759}
16760
8e19ed76 16761/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 16762 if so return true else false. */
380bca97 16763
8e19ed76
PS
16764static int
16765attr_form_is_block (struct attribute *attr)
16766{
16767 return (attr == NULL ? 0 :
16768 attr->form == DW_FORM_block1
16769 || attr->form == DW_FORM_block2
16770 || attr->form == DW_FORM_block4
2dc7f7b3
TT
16771 || attr->form == DW_FORM_block
16772 || attr->form == DW_FORM_exprloc);
8e19ed76 16773}
4c2df51b 16774
c6a0999f
JB
16775/* Return non-zero if ATTR's value is a section offset --- classes
16776 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16777 You may use DW_UNSND (attr) to retrieve such offsets.
16778
16779 Section 7.5.4, "Attribute Encodings", explains that no attribute
16780 may have a value that belongs to more than one of these classes; it
16781 would be ambiguous if we did, because we use the same forms for all
16782 of them. */
380bca97 16783
3690dd37
JB
16784static int
16785attr_form_is_section_offset (struct attribute *attr)
16786{
16787 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
16788 || attr->form == DW_FORM_data8
16789 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
16790}
16791
3690dd37
JB
16792/* Return non-zero if ATTR's value falls in the 'constant' class, or
16793 zero otherwise. When this function returns true, you can apply
16794 dwarf2_get_attr_constant_value to it.
16795
16796 However, note that for some attributes you must check
16797 attr_form_is_section_offset before using this test. DW_FORM_data4
16798 and DW_FORM_data8 are members of both the constant class, and of
16799 the classes that contain offsets into other debug sections
16800 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16801 that, if an attribute's can be either a constant or one of the
16802 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16803 taken as section offsets, not constants. */
380bca97 16804
3690dd37
JB
16805static int
16806attr_form_is_constant (struct attribute *attr)
16807{
16808 switch (attr->form)
16809 {
16810 case DW_FORM_sdata:
16811 case DW_FORM_udata:
16812 case DW_FORM_data1:
16813 case DW_FORM_data2:
16814 case DW_FORM_data4:
16815 case DW_FORM_data8:
16816 return 1;
16817 default:
16818 return 0;
16819 }
16820}
16821
3019eac3
DE
16822/* Return the .debug_loc section to use for CU.
16823 For DWO files use .debug_loc.dwo. */
16824
16825static struct dwarf2_section_info *
16826cu_debug_loc_section (struct dwarf2_cu *cu)
16827{
16828 if (cu->dwo_unit)
16829 return &cu->dwo_unit->dwo_file->sections.loc;
16830 return &dwarf2_per_objfile->loc;
16831}
16832
8cf6f0b1
TT
16833/* A helper function that fills in a dwarf2_loclist_baton. */
16834
16835static void
16836fill_in_loclist_baton (struct dwarf2_cu *cu,
16837 struct dwarf2_loclist_baton *baton,
16838 struct attribute *attr)
16839{
3019eac3
DE
16840 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16841
16842 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
16843
16844 baton->per_cu = cu->per_cu;
16845 gdb_assert (baton->per_cu);
16846 /* We don't know how long the location list is, but make sure we
16847 don't run off the edge of the section. */
3019eac3
DE
16848 baton->size = section->size - DW_UNSND (attr);
16849 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 16850 baton->base_address = cu->base_address;
f664829e 16851 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
16852}
16853
4c2df51b
DJ
16854static void
16855dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 16856 struct dwarf2_cu *cu)
4c2df51b 16857{
bb5ed363 16858 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 16859 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 16860
3690dd37 16861 if (attr_form_is_section_offset (attr)
3019eac3 16862 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
16863 the section. If so, fall through to the complaint in the
16864 other branch. */
3019eac3 16865 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 16866 {
0d53c4c4 16867 struct dwarf2_loclist_baton *baton;
4c2df51b 16868
bb5ed363 16869 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16870 sizeof (struct dwarf2_loclist_baton));
4c2df51b 16871
8cf6f0b1 16872 fill_in_loclist_baton (cu, baton, attr);
be391dca 16873
d00adf39 16874 if (cu->base_known == 0)
0d53c4c4 16875 complaint (&symfile_complaints,
3e43a32a
MS
16876 _("Location list used without "
16877 "specifying the CU base address."));
4c2df51b 16878
768a979c 16879 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
16880 SYMBOL_LOCATION_BATON (sym) = baton;
16881 }
16882 else
16883 {
16884 struct dwarf2_locexpr_baton *baton;
16885
bb5ed363 16886 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16887 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
16888 baton->per_cu = cu->per_cu;
16889 gdb_assert (baton->per_cu);
0d53c4c4
DJ
16890
16891 if (attr_form_is_block (attr))
16892 {
16893 /* Note that we're just copying the block's data pointer
16894 here, not the actual data. We're still pointing into the
6502dd73
DJ
16895 info_buffer for SYM's objfile; right now we never release
16896 that buffer, but when we do clean up properly this may
16897 need to change. */
0d53c4c4
DJ
16898 baton->size = DW_BLOCK (attr)->size;
16899 baton->data = DW_BLOCK (attr)->data;
16900 }
16901 else
16902 {
16903 dwarf2_invalid_attrib_class_complaint ("location description",
16904 SYMBOL_NATURAL_NAME (sym));
16905 baton->size = 0;
0d53c4c4 16906 }
6e70227d 16907
768a979c 16908 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
16909 SYMBOL_LOCATION_BATON (sym) = baton;
16910 }
4c2df51b 16911}
6502dd73 16912
9aa1f1e3
TT
16913/* Return the OBJFILE associated with the compilation unit CU. If CU
16914 came from a separate debuginfo file, then the master objfile is
16915 returned. */
ae0d2f24
UW
16916
16917struct objfile *
16918dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16919{
9291a0cd 16920 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
16921
16922 /* Return the master objfile, so that we can report and look up the
16923 correct file containing this variable. */
16924 if (objfile->separate_debug_objfile_backlink)
16925 objfile = objfile->separate_debug_objfile_backlink;
16926
16927 return objfile;
16928}
16929
96408a79
SA
16930/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16931 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16932 CU_HEADERP first. */
16933
16934static const struct comp_unit_head *
16935per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16936 struct dwarf2_per_cu_data *per_cu)
16937{
96408a79
SA
16938 gdb_byte *info_ptr;
16939
16940 if (per_cu->cu)
16941 return &per_cu->cu->header;
16942
0bc3a05c 16943 info_ptr = per_cu->info_or_types_section->buffer + per_cu->offset.sect_off;
96408a79
SA
16944
16945 memset (cu_headerp, 0, sizeof (*cu_headerp));
0bc3a05c 16946 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
96408a79
SA
16947
16948 return cu_headerp;
16949}
16950
ae0d2f24
UW
16951/* Return the address size given in the compilation unit header for CU. */
16952
98714339 16953int
ae0d2f24
UW
16954dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16955{
96408a79
SA
16956 struct comp_unit_head cu_header_local;
16957 const struct comp_unit_head *cu_headerp;
c471e790 16958
96408a79
SA
16959 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16960
16961 return cu_headerp->addr_size;
ae0d2f24
UW
16962}
16963
9eae7c52
TT
16964/* Return the offset size given in the compilation unit header for CU. */
16965
16966int
16967dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16968{
96408a79
SA
16969 struct comp_unit_head cu_header_local;
16970 const struct comp_unit_head *cu_headerp;
9c6c53f7 16971
96408a79
SA
16972 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16973
16974 return cu_headerp->offset_size;
16975}
16976
16977/* See its dwarf2loc.h declaration. */
16978
16979int
16980dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16981{
16982 struct comp_unit_head cu_header_local;
16983 const struct comp_unit_head *cu_headerp;
16984
16985 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16986
16987 if (cu_headerp->version == 2)
16988 return cu_headerp->addr_size;
16989 else
16990 return cu_headerp->offset_size;
181cebd4
JK
16991}
16992
9aa1f1e3
TT
16993/* Return the text offset of the CU. The returned offset comes from
16994 this CU's objfile. If this objfile came from a separate debuginfo
16995 file, then the offset may be different from the corresponding
16996 offset in the parent objfile. */
16997
16998CORE_ADDR
16999dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
17000{
bb3fa9d0 17001 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
17002
17003 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17004}
17005
348e048f
DE
17006/* Locate the .debug_info compilation unit from CU's objfile which contains
17007 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
17008
17009static struct dwarf2_per_cu_data *
b64f50a1 17010dwarf2_find_containing_comp_unit (sect_offset offset,
ae038cb0
DJ
17011 struct objfile *objfile)
17012{
17013 struct dwarf2_per_cu_data *this_cu;
17014 int low, high;
17015
ae038cb0
DJ
17016 low = 0;
17017 high = dwarf2_per_objfile->n_comp_units - 1;
17018 while (high > low)
17019 {
17020 int mid = low + (high - low) / 2;
9a619af0 17021
b64f50a1
JK
17022 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
17023 >= offset.sect_off)
ae038cb0
DJ
17024 high = mid;
17025 else
17026 low = mid + 1;
17027 }
17028 gdb_assert (low == high);
b64f50a1
JK
17029 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
17030 > offset.sect_off)
ae038cb0 17031 {
10b3939b 17032 if (low == 0)
8a3fe4f8
AC
17033 error (_("Dwarf Error: could not find partial DIE containing "
17034 "offset 0x%lx [in module %s]"),
b64f50a1 17035 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 17036
b64f50a1
JK
17037 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
17038 <= offset.sect_off);
ae038cb0
DJ
17039 return dwarf2_per_objfile->all_comp_units[low-1];
17040 }
17041 else
17042 {
17043 this_cu = dwarf2_per_objfile->all_comp_units[low];
17044 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
17045 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
17046 error (_("invalid dwarf2 offset %u"), offset.sect_off);
17047 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
17048 return this_cu;
17049 }
17050}
17051
23745b47 17052/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 17053
9816fde3 17054static void
23745b47 17055init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 17056{
9816fde3 17057 memset (cu, 0, sizeof (*cu));
23745b47
DE
17058 per_cu->cu = cu;
17059 cu->per_cu = per_cu;
17060 cu->objfile = per_cu->objfile;
93311388 17061 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
17062}
17063
17064/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
17065
17066static void
95554aad
TT
17067prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
17068 enum language pretend_language)
9816fde3
JK
17069{
17070 struct attribute *attr;
17071
17072 /* Set the language we're debugging. */
17073 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
17074 if (attr)
17075 set_cu_language (DW_UNSND (attr), cu);
17076 else
9cded63f 17077 {
95554aad 17078 cu->language = pretend_language;
9cded63f
TT
17079 cu->language_defn = language_def (cu->language);
17080 }
dee91e82
DE
17081
17082 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
17083 if (attr)
17084 cu->producer = DW_STRING (attr);
93311388
DE
17085}
17086
ae038cb0
DJ
17087/* Release one cached compilation unit, CU. We unlink it from the tree
17088 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
17089 the caller is responsible for that.
17090 NOTE: DATA is a void * because this function is also used as a
17091 cleanup routine. */
ae038cb0
DJ
17092
17093static void
68dc6402 17094free_heap_comp_unit (void *data)
ae038cb0
DJ
17095{
17096 struct dwarf2_cu *cu = data;
17097
23745b47
DE
17098 gdb_assert (cu->per_cu != NULL);
17099 cu->per_cu->cu = NULL;
ae038cb0
DJ
17100 cu->per_cu = NULL;
17101
17102 obstack_free (&cu->comp_unit_obstack, NULL);
17103
17104 xfree (cu);
17105}
17106
72bf9492 17107/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 17108 when we're finished with it. We can't free the pointer itself, but be
dee91e82 17109 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
17110
17111static void
17112free_stack_comp_unit (void *data)
17113{
17114 struct dwarf2_cu *cu = data;
17115
23745b47
DE
17116 gdb_assert (cu->per_cu != NULL);
17117 cu->per_cu->cu = NULL;
17118 cu->per_cu = NULL;
17119
72bf9492
DJ
17120 obstack_free (&cu->comp_unit_obstack, NULL);
17121 cu->partial_dies = NULL;
ae038cb0
DJ
17122}
17123
17124/* Free all cached compilation units. */
17125
17126static void
17127free_cached_comp_units (void *data)
17128{
17129 struct dwarf2_per_cu_data *per_cu, **last_chain;
17130
17131 per_cu = dwarf2_per_objfile->read_in_chain;
17132 last_chain = &dwarf2_per_objfile->read_in_chain;
17133 while (per_cu != NULL)
17134 {
17135 struct dwarf2_per_cu_data *next_cu;
17136
17137 next_cu = per_cu->cu->read_in_chain;
17138
68dc6402 17139 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
17140 *last_chain = next_cu;
17141
17142 per_cu = next_cu;
17143 }
17144}
17145
17146/* Increase the age counter on each cached compilation unit, and free
17147 any that are too old. */
17148
17149static void
17150age_cached_comp_units (void)
17151{
17152 struct dwarf2_per_cu_data *per_cu, **last_chain;
17153
17154 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
17155 per_cu = dwarf2_per_objfile->read_in_chain;
17156 while (per_cu != NULL)
17157 {
17158 per_cu->cu->last_used ++;
17159 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
17160 dwarf2_mark (per_cu->cu);
17161 per_cu = per_cu->cu->read_in_chain;
17162 }
17163
17164 per_cu = dwarf2_per_objfile->read_in_chain;
17165 last_chain = &dwarf2_per_objfile->read_in_chain;
17166 while (per_cu != NULL)
17167 {
17168 struct dwarf2_per_cu_data *next_cu;
17169
17170 next_cu = per_cu->cu->read_in_chain;
17171
17172 if (!per_cu->cu->mark)
17173 {
68dc6402 17174 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
17175 *last_chain = next_cu;
17176 }
17177 else
17178 last_chain = &per_cu->cu->read_in_chain;
17179
17180 per_cu = next_cu;
17181 }
17182}
17183
17184/* Remove a single compilation unit from the cache. */
17185
17186static void
dee91e82 17187free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
17188{
17189 struct dwarf2_per_cu_data *per_cu, **last_chain;
17190
17191 per_cu = dwarf2_per_objfile->read_in_chain;
17192 last_chain = &dwarf2_per_objfile->read_in_chain;
17193 while (per_cu != NULL)
17194 {
17195 struct dwarf2_per_cu_data *next_cu;
17196
17197 next_cu = per_cu->cu->read_in_chain;
17198
dee91e82 17199 if (per_cu == target_per_cu)
ae038cb0 17200 {
68dc6402 17201 free_heap_comp_unit (per_cu->cu);
dee91e82 17202 per_cu->cu = NULL;
ae038cb0
DJ
17203 *last_chain = next_cu;
17204 break;
17205 }
17206 else
17207 last_chain = &per_cu->cu->read_in_chain;
17208
17209 per_cu = next_cu;
17210 }
17211}
17212
fe3e1990
DJ
17213/* Release all extra memory associated with OBJFILE. */
17214
17215void
17216dwarf2_free_objfile (struct objfile *objfile)
17217{
17218 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17219
17220 if (dwarf2_per_objfile == NULL)
17221 return;
17222
17223 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
17224 free_cached_comp_units (NULL);
17225
7b9f3c50
DE
17226 if (dwarf2_per_objfile->quick_file_names_table)
17227 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 17228
fe3e1990
DJ
17229 /* Everything else should be on the objfile obstack. */
17230}
17231
dee91e82
DE
17232/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
17233 We store these in a hash table separate from the DIEs, and preserve them
17234 when the DIEs are flushed out of cache.
17235
17236 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
17237 uniquely identify the type. A file may have multiple .debug_types sections,
17238 or the type may come from a DWO file. We have to use something in
17239 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
17240 routine, get_die_type_at_offset, from outside this file, and thus won't
17241 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
17242 of the objfile. */
1c379e20 17243
dee91e82 17244struct dwarf2_per_cu_offset_and_type
1c379e20 17245{
dee91e82 17246 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 17247 sect_offset offset;
1c379e20
DJ
17248 struct type *type;
17249};
17250
dee91e82 17251/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
17252
17253static hashval_t
dee91e82 17254per_cu_offset_and_type_hash (const void *item)
1c379e20 17255{
dee91e82 17256 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 17257
dee91e82 17258 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
17259}
17260
dee91e82 17261/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
17262
17263static int
dee91e82 17264per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 17265{
dee91e82
DE
17266 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
17267 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 17268
dee91e82
DE
17269 return (ofs_lhs->per_cu == ofs_rhs->per_cu
17270 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
17271}
17272
17273/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
17274 table if necessary. For convenience, return TYPE.
17275
17276 The DIEs reading must have careful ordering to:
17277 * Not cause infite loops trying to read in DIEs as a prerequisite for
17278 reading current DIE.
17279 * Not trying to dereference contents of still incompletely read in types
17280 while reading in other DIEs.
17281 * Enable referencing still incompletely read in types just by a pointer to
17282 the type without accessing its fields.
17283
17284 Therefore caller should follow these rules:
17285 * Try to fetch any prerequisite types we may need to build this DIE type
17286 before building the type and calling set_die_type.
e71ec853 17287 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
17288 possible before fetching more types to complete the current type.
17289 * Make the type as complete as possible before fetching more types. */
1c379e20 17290
f792889a 17291static struct type *
1c379e20
DJ
17292set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
17293{
dee91e82 17294 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 17295 struct objfile *objfile = cu->objfile;
1c379e20 17296
b4ba55a1
JB
17297 /* For Ada types, make sure that the gnat-specific data is always
17298 initialized (if not already set). There are a few types where
17299 we should not be doing so, because the type-specific area is
17300 already used to hold some other piece of info (eg: TYPE_CODE_FLT
17301 where the type-specific area is used to store the floatformat).
17302 But this is not a problem, because the gnat-specific information
17303 is actually not needed for these types. */
17304 if (need_gnat_info (cu)
17305 && TYPE_CODE (type) != TYPE_CODE_FUNC
17306 && TYPE_CODE (type) != TYPE_CODE_FLT
17307 && !HAVE_GNAT_AUX_INFO (type))
17308 INIT_GNAT_SPECIFIC (type);
17309
dee91e82 17310 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 17311 {
dee91e82
DE
17312 dwarf2_per_objfile->die_type_hash =
17313 htab_create_alloc_ex (127,
17314 per_cu_offset_and_type_hash,
17315 per_cu_offset_and_type_eq,
17316 NULL,
17317 &objfile->objfile_obstack,
17318 hashtab_obstack_allocate,
17319 dummy_obstack_deallocate);
f792889a 17320 }
1c379e20 17321
dee91e82 17322 ofs.per_cu = cu->per_cu;
1c379e20
DJ
17323 ofs.offset = die->offset;
17324 ofs.type = type;
dee91e82
DE
17325 slot = (struct dwarf2_per_cu_offset_and_type **)
17326 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
17327 if (*slot)
17328 complaint (&symfile_complaints,
17329 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 17330 die->offset.sect_off);
673bfd45 17331 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 17332 **slot = ofs;
f792889a 17333 return type;
1c379e20
DJ
17334}
17335
380bca97 17336/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 17337 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
17338
17339static struct type *
b64f50a1 17340get_die_type_at_offset (sect_offset offset,
673bfd45 17341 struct dwarf2_per_cu_data *per_cu)
1c379e20 17342{
dee91e82 17343 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 17344
dee91e82 17345 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 17346 return NULL;
1c379e20 17347
dee91e82 17348 ofs.per_cu = per_cu;
673bfd45 17349 ofs.offset = offset;
dee91e82 17350 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
17351 if (slot)
17352 return slot->type;
17353 else
17354 return NULL;
17355}
17356
673bfd45
DE
17357/* Look up the type for DIE in the appropriate type_hash table,
17358 or return NULL if DIE does not have a saved type. */
17359
17360static struct type *
17361get_die_type (struct die_info *die, struct dwarf2_cu *cu)
17362{
17363 return get_die_type_at_offset (die->offset, cu->per_cu);
17364}
17365
10b3939b
DJ
17366/* Add a dependence relationship from CU to REF_PER_CU. */
17367
17368static void
17369dwarf2_add_dependence (struct dwarf2_cu *cu,
17370 struct dwarf2_per_cu_data *ref_per_cu)
17371{
17372 void **slot;
17373
17374 if (cu->dependencies == NULL)
17375 cu->dependencies
17376 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
17377 NULL, &cu->comp_unit_obstack,
17378 hashtab_obstack_allocate,
17379 dummy_obstack_deallocate);
17380
17381 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
17382 if (*slot == NULL)
17383 *slot = ref_per_cu;
17384}
1c379e20 17385
f504f079
DE
17386/* Subroutine of dwarf2_mark to pass to htab_traverse.
17387 Set the mark field in every compilation unit in the
ae038cb0
DJ
17388 cache that we must keep because we are keeping CU. */
17389
10b3939b
DJ
17390static int
17391dwarf2_mark_helper (void **slot, void *data)
17392{
17393 struct dwarf2_per_cu_data *per_cu;
17394
17395 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
17396
17397 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17398 reading of the chain. As such dependencies remain valid it is not much
17399 useful to track and undo them during QUIT cleanups. */
17400 if (per_cu->cu == NULL)
17401 return 1;
17402
10b3939b
DJ
17403 if (per_cu->cu->mark)
17404 return 1;
17405 per_cu->cu->mark = 1;
17406
17407 if (per_cu->cu->dependencies != NULL)
17408 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17409
17410 return 1;
17411}
17412
f504f079
DE
17413/* Set the mark field in CU and in every other compilation unit in the
17414 cache that we must keep because we are keeping CU. */
17415
ae038cb0
DJ
17416static void
17417dwarf2_mark (struct dwarf2_cu *cu)
17418{
17419 if (cu->mark)
17420 return;
17421 cu->mark = 1;
10b3939b
DJ
17422 if (cu->dependencies != NULL)
17423 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
17424}
17425
17426static void
17427dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17428{
17429 while (per_cu)
17430 {
17431 per_cu->cu->mark = 0;
17432 per_cu = per_cu->cu->read_in_chain;
17433 }
72bf9492
DJ
17434}
17435
72bf9492
DJ
17436/* Trivial hash function for partial_die_info: the hash value of a DIE
17437 is its offset in .debug_info for this objfile. */
17438
17439static hashval_t
17440partial_die_hash (const void *item)
17441{
17442 const struct partial_die_info *part_die = item;
9a619af0 17443
b64f50a1 17444 return part_die->offset.sect_off;
72bf9492
DJ
17445}
17446
17447/* Trivial comparison function for partial_die_info structures: two DIEs
17448 are equal if they have the same offset. */
17449
17450static int
17451partial_die_eq (const void *item_lhs, const void *item_rhs)
17452{
17453 const struct partial_die_info *part_die_lhs = item_lhs;
17454 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 17455
b64f50a1 17456 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
17457}
17458
ae038cb0
DJ
17459static struct cmd_list_element *set_dwarf2_cmdlist;
17460static struct cmd_list_element *show_dwarf2_cmdlist;
17461
17462static void
17463set_dwarf2_cmd (char *args, int from_tty)
17464{
17465 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17466}
17467
17468static void
17469show_dwarf2_cmd (char *args, int from_tty)
6e70227d 17470{
ae038cb0
DJ
17471 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17472}
17473
dce234bc
PP
17474/* If section described by INFO was mmapped, munmap it now. */
17475
17476static void
17477munmap_section_buffer (struct dwarf2_section_info *info)
17478{
b315ab21 17479 if (info->map_addr != NULL)
dce234bc
PP
17480 {
17481#ifdef HAVE_MMAP
b315ab21 17482 int res;
9a619af0 17483
b315ab21
TG
17484 res = munmap (info->map_addr, info->map_len);
17485 gdb_assert (res == 0);
dce234bc
PP
17486#else
17487 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 17488 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
17489#endif
17490 }
17491}
17492
17493/* munmap debug sections for OBJFILE, if necessary. */
17494
17495static void
c1bd65d0 17496dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
17497{
17498 struct dwarf2_per_objfile *data = d;
8b70b953
TT
17499 int ix;
17500 struct dwarf2_section_info *section;
9a619af0 17501
16be1145
DE
17502 /* This is sorted according to the order they're defined in to make it easier
17503 to keep in sync. */
dce234bc
PP
17504 munmap_section_buffer (&data->info);
17505 munmap_section_buffer (&data->abbrev);
17506 munmap_section_buffer (&data->line);
16be1145 17507 munmap_section_buffer (&data->loc);
dce234bc 17508 munmap_section_buffer (&data->macinfo);
cf2c3c16 17509 munmap_section_buffer (&data->macro);
16be1145 17510 munmap_section_buffer (&data->str);
dce234bc 17511 munmap_section_buffer (&data->ranges);
3019eac3 17512 munmap_section_buffer (&data->addr);
dce234bc
PP
17513 munmap_section_buffer (&data->frame);
17514 munmap_section_buffer (&data->eh_frame);
9291a0cd 17515 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
17516
17517 for (ix = 0;
17518 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17519 ++ix)
17520 munmap_section_buffer (section);
17521
95554aad
TT
17522 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17523 VEC_free (dwarf2_per_cu_ptr,
17524 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17525
8b70b953 17526 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
17527
17528 if (data->dwo_files)
17529 free_dwo_files (data->dwo_files, objfile);
9291a0cd
TT
17530}
17531
17532\f
ae2de4f8 17533/* The "save gdb-index" command. */
9291a0cd
TT
17534
17535/* The contents of the hash table we create when building the string
17536 table. */
17537struct strtab_entry
17538{
17539 offset_type offset;
17540 const char *str;
17541};
17542
559a7a62
JK
17543/* Hash function for a strtab_entry.
17544
17545 Function is used only during write_hash_table so no index format backward
17546 compatibility is needed. */
b89be57b 17547
9291a0cd
TT
17548static hashval_t
17549hash_strtab_entry (const void *e)
17550{
17551 const struct strtab_entry *entry = e;
559a7a62 17552 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
17553}
17554
17555/* Equality function for a strtab_entry. */
b89be57b 17556
9291a0cd
TT
17557static int
17558eq_strtab_entry (const void *a, const void *b)
17559{
17560 const struct strtab_entry *ea = a;
17561 const struct strtab_entry *eb = b;
17562 return !strcmp (ea->str, eb->str);
17563}
17564
17565/* Create a strtab_entry hash table. */
b89be57b 17566
9291a0cd
TT
17567static htab_t
17568create_strtab (void)
17569{
17570 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17571 xfree, xcalloc, xfree);
17572}
17573
17574/* Add a string to the constant pool. Return the string's offset in
17575 host order. */
b89be57b 17576
9291a0cd
TT
17577static offset_type
17578add_string (htab_t table, struct obstack *cpool, const char *str)
17579{
17580 void **slot;
17581 struct strtab_entry entry;
17582 struct strtab_entry *result;
17583
17584 entry.str = str;
17585 slot = htab_find_slot (table, &entry, INSERT);
17586 if (*slot)
17587 result = *slot;
17588 else
17589 {
17590 result = XNEW (struct strtab_entry);
17591 result->offset = obstack_object_size (cpool);
17592 result->str = str;
17593 obstack_grow_str0 (cpool, str);
17594 *slot = result;
17595 }
17596 return result->offset;
17597}
17598
17599/* An entry in the symbol table. */
17600struct symtab_index_entry
17601{
17602 /* The name of the symbol. */
17603 const char *name;
17604 /* The offset of the name in the constant pool. */
17605 offset_type index_offset;
17606 /* A sorted vector of the indices of all the CUs that hold an object
17607 of this name. */
17608 VEC (offset_type) *cu_indices;
17609};
17610
17611/* The symbol table. This is a power-of-2-sized hash table. */
17612struct mapped_symtab
17613{
17614 offset_type n_elements;
17615 offset_type size;
17616 struct symtab_index_entry **data;
17617};
17618
17619/* Hash function for a symtab_index_entry. */
b89be57b 17620
9291a0cd
TT
17621static hashval_t
17622hash_symtab_entry (const void *e)
17623{
17624 const struct symtab_index_entry *entry = e;
17625 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17626 sizeof (offset_type) * VEC_length (offset_type,
17627 entry->cu_indices),
17628 0);
17629}
17630
17631/* Equality function for a symtab_index_entry. */
b89be57b 17632
9291a0cd
TT
17633static int
17634eq_symtab_entry (const void *a, const void *b)
17635{
17636 const struct symtab_index_entry *ea = a;
17637 const struct symtab_index_entry *eb = b;
17638 int len = VEC_length (offset_type, ea->cu_indices);
17639 if (len != VEC_length (offset_type, eb->cu_indices))
17640 return 0;
17641 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17642 VEC_address (offset_type, eb->cu_indices),
17643 sizeof (offset_type) * len);
17644}
17645
17646/* Destroy a symtab_index_entry. */
b89be57b 17647
9291a0cd
TT
17648static void
17649delete_symtab_entry (void *p)
17650{
17651 struct symtab_index_entry *entry = p;
17652 VEC_free (offset_type, entry->cu_indices);
17653 xfree (entry);
17654}
17655
17656/* Create a hash table holding symtab_index_entry objects. */
b89be57b 17657
9291a0cd 17658static htab_t
3876f04e 17659create_symbol_hash_table (void)
9291a0cd
TT
17660{
17661 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17662 delete_symtab_entry, xcalloc, xfree);
17663}
17664
17665/* Create a new mapped symtab object. */
b89be57b 17666
9291a0cd
TT
17667static struct mapped_symtab *
17668create_mapped_symtab (void)
17669{
17670 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17671 symtab->n_elements = 0;
17672 symtab->size = 1024;
17673 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17674 return symtab;
17675}
17676
17677/* Destroy a mapped_symtab. */
b89be57b 17678
9291a0cd
TT
17679static void
17680cleanup_mapped_symtab (void *p)
17681{
17682 struct mapped_symtab *symtab = p;
17683 /* The contents of the array are freed when the other hash table is
17684 destroyed. */
17685 xfree (symtab->data);
17686 xfree (symtab);
17687}
17688
17689/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
17690 the slot.
17691
17692 Function is used only during write_hash_table so no index format backward
17693 compatibility is needed. */
b89be57b 17694
9291a0cd
TT
17695static struct symtab_index_entry **
17696find_slot (struct mapped_symtab *symtab, const char *name)
17697{
559a7a62 17698 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
17699
17700 index = hash & (symtab->size - 1);
17701 step = ((hash * 17) & (symtab->size - 1)) | 1;
17702
17703 for (;;)
17704 {
17705 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17706 return &symtab->data[index];
17707 index = (index + step) & (symtab->size - 1);
17708 }
17709}
17710
17711/* Expand SYMTAB's hash table. */
b89be57b 17712
9291a0cd
TT
17713static void
17714hash_expand (struct mapped_symtab *symtab)
17715{
17716 offset_type old_size = symtab->size;
17717 offset_type i;
17718 struct symtab_index_entry **old_entries = symtab->data;
17719
17720 symtab->size *= 2;
17721 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17722
17723 for (i = 0; i < old_size; ++i)
17724 {
17725 if (old_entries[i])
17726 {
17727 struct symtab_index_entry **slot = find_slot (symtab,
17728 old_entries[i]->name);
17729 *slot = old_entries[i];
17730 }
17731 }
17732
17733 xfree (old_entries);
17734}
17735
156942c7
DE
17736/* Add an entry to SYMTAB. NAME is the name of the symbol.
17737 CU_INDEX is the index of the CU in which the symbol appears.
17738 IS_STATIC is one if the symbol is static, otherwise zero (global). */
b89be57b 17739
9291a0cd
TT
17740static void
17741add_index_entry (struct mapped_symtab *symtab, const char *name,
156942c7 17742 int is_static, gdb_index_symbol_kind kind,
9291a0cd
TT
17743 offset_type cu_index)
17744{
17745 struct symtab_index_entry **slot;
156942c7 17746 offset_type cu_index_and_attrs;
9291a0cd
TT
17747
17748 ++symtab->n_elements;
17749 if (4 * symtab->n_elements / 3 >= symtab->size)
17750 hash_expand (symtab);
17751
17752 slot = find_slot (symtab, name);
17753 if (!*slot)
17754 {
17755 *slot = XNEW (struct symtab_index_entry);
17756 (*slot)->name = name;
156942c7 17757 /* index_offset is set later. */
9291a0cd
TT
17758 (*slot)->cu_indices = NULL;
17759 }
156942c7
DE
17760
17761 cu_index_and_attrs = 0;
17762 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
17763 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
17764 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
17765
17766 /* We don't want to record an index value twice as we want to avoid the
17767 duplication.
17768 We process all global symbols and then all static symbols
17769 (which would allow us to avoid the duplication by only having to check
17770 the last entry pushed), but a symbol could have multiple kinds in one CU.
17771 To keep things simple we don't worry about the duplication here and
17772 sort and uniqufy the list after we've processed all symbols. */
17773 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
17774}
17775
17776/* qsort helper routine for uniquify_cu_indices. */
17777
17778static int
17779offset_type_compare (const void *ap, const void *bp)
17780{
17781 offset_type a = *(offset_type *) ap;
17782 offset_type b = *(offset_type *) bp;
17783
17784 return (a > b) - (b > a);
17785}
17786
17787/* Sort and remove duplicates of all symbols' cu_indices lists. */
17788
17789static void
17790uniquify_cu_indices (struct mapped_symtab *symtab)
17791{
17792 int i;
17793
17794 for (i = 0; i < symtab->size; ++i)
17795 {
17796 struct symtab_index_entry *entry = symtab->data[i];
17797
17798 if (entry
17799 && entry->cu_indices != NULL)
17800 {
17801 unsigned int next_to_insert, next_to_check;
17802 offset_type last_value;
17803
17804 qsort (VEC_address (offset_type, entry->cu_indices),
17805 VEC_length (offset_type, entry->cu_indices),
17806 sizeof (offset_type), offset_type_compare);
17807
17808 last_value = VEC_index (offset_type, entry->cu_indices, 0);
17809 next_to_insert = 1;
17810 for (next_to_check = 1;
17811 next_to_check < VEC_length (offset_type, entry->cu_indices);
17812 ++next_to_check)
17813 {
17814 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
17815 != last_value)
17816 {
17817 last_value = VEC_index (offset_type, entry->cu_indices,
17818 next_to_check);
17819 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
17820 last_value);
17821 ++next_to_insert;
17822 }
17823 }
17824 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
17825 }
17826 }
9291a0cd
TT
17827}
17828
17829/* Add a vector of indices to the constant pool. */
b89be57b 17830
9291a0cd 17831static offset_type
3876f04e 17832add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
17833 struct symtab_index_entry *entry)
17834{
17835 void **slot;
17836
3876f04e 17837 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
17838 if (!*slot)
17839 {
17840 offset_type len = VEC_length (offset_type, entry->cu_indices);
17841 offset_type val = MAYBE_SWAP (len);
17842 offset_type iter;
17843 int i;
17844
17845 *slot = entry;
17846 entry->index_offset = obstack_object_size (cpool);
17847
17848 obstack_grow (cpool, &val, sizeof (val));
17849 for (i = 0;
17850 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17851 ++i)
17852 {
17853 val = MAYBE_SWAP (iter);
17854 obstack_grow (cpool, &val, sizeof (val));
17855 }
17856 }
17857 else
17858 {
17859 struct symtab_index_entry *old_entry = *slot;
17860 entry->index_offset = old_entry->index_offset;
17861 entry = old_entry;
17862 }
17863 return entry->index_offset;
17864}
17865
17866/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17867 constant pool entries going into the obstack CPOOL. */
b89be57b 17868
9291a0cd
TT
17869static void
17870write_hash_table (struct mapped_symtab *symtab,
17871 struct obstack *output, struct obstack *cpool)
17872{
17873 offset_type i;
3876f04e 17874 htab_t symbol_hash_table;
9291a0cd
TT
17875 htab_t str_table;
17876
3876f04e 17877 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 17878 str_table = create_strtab ();
3876f04e 17879
9291a0cd
TT
17880 /* We add all the index vectors to the constant pool first, to
17881 ensure alignment is ok. */
17882 for (i = 0; i < symtab->size; ++i)
17883 {
17884 if (symtab->data[i])
3876f04e 17885 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
17886 }
17887
17888 /* Now write out the hash table. */
17889 for (i = 0; i < symtab->size; ++i)
17890 {
17891 offset_type str_off, vec_off;
17892
17893 if (symtab->data[i])
17894 {
17895 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17896 vec_off = symtab->data[i]->index_offset;
17897 }
17898 else
17899 {
17900 /* While 0 is a valid constant pool index, it is not valid
17901 to have 0 for both offsets. */
17902 str_off = 0;
17903 vec_off = 0;
17904 }
17905
17906 str_off = MAYBE_SWAP (str_off);
17907 vec_off = MAYBE_SWAP (vec_off);
17908
17909 obstack_grow (output, &str_off, sizeof (str_off));
17910 obstack_grow (output, &vec_off, sizeof (vec_off));
17911 }
17912
17913 htab_delete (str_table);
3876f04e 17914 htab_delete (symbol_hash_table);
9291a0cd
TT
17915}
17916
0a5429f6
DE
17917/* Struct to map psymtab to CU index in the index file. */
17918struct psymtab_cu_index_map
17919{
17920 struct partial_symtab *psymtab;
17921 unsigned int cu_index;
17922};
17923
17924static hashval_t
17925hash_psymtab_cu_index (const void *item)
17926{
17927 const struct psymtab_cu_index_map *map = item;
17928
17929 return htab_hash_pointer (map->psymtab);
17930}
17931
17932static int
17933eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17934{
17935 const struct psymtab_cu_index_map *lhs = item_lhs;
17936 const struct psymtab_cu_index_map *rhs = item_rhs;
17937
17938 return lhs->psymtab == rhs->psymtab;
17939}
17940
17941/* Helper struct for building the address table. */
17942struct addrmap_index_data
17943{
17944 struct objfile *objfile;
17945 struct obstack *addr_obstack;
17946 htab_t cu_index_htab;
17947
17948 /* Non-zero if the previous_* fields are valid.
17949 We can't write an entry until we see the next entry (since it is only then
17950 that we know the end of the entry). */
17951 int previous_valid;
17952 /* Index of the CU in the table of all CUs in the index file. */
17953 unsigned int previous_cu_index;
0963b4bd 17954 /* Start address of the CU. */
0a5429f6
DE
17955 CORE_ADDR previous_cu_start;
17956};
17957
17958/* Write an address entry to OBSTACK. */
b89be57b 17959
9291a0cd 17960static void
0a5429f6
DE
17961add_address_entry (struct objfile *objfile, struct obstack *obstack,
17962 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 17963{
0a5429f6 17964 offset_type cu_index_to_write;
9291a0cd
TT
17965 char addr[8];
17966 CORE_ADDR baseaddr;
17967
17968 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17969
0a5429f6
DE
17970 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17971 obstack_grow (obstack, addr, 8);
17972 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17973 obstack_grow (obstack, addr, 8);
17974 cu_index_to_write = MAYBE_SWAP (cu_index);
17975 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17976}
17977
17978/* Worker function for traversing an addrmap to build the address table. */
17979
17980static int
17981add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17982{
17983 struct addrmap_index_data *data = datap;
17984 struct partial_symtab *pst = obj;
0a5429f6
DE
17985
17986 if (data->previous_valid)
17987 add_address_entry (data->objfile, data->addr_obstack,
17988 data->previous_cu_start, start_addr,
17989 data->previous_cu_index);
17990
17991 data->previous_cu_start = start_addr;
17992 if (pst != NULL)
17993 {
17994 struct psymtab_cu_index_map find_map, *map;
17995 find_map.psymtab = pst;
17996 map = htab_find (data->cu_index_htab, &find_map);
17997 gdb_assert (map != NULL);
17998 data->previous_cu_index = map->cu_index;
17999 data->previous_valid = 1;
18000 }
18001 else
18002 data->previous_valid = 0;
18003
18004 return 0;
18005}
18006
18007/* Write OBJFILE's address map to OBSTACK.
18008 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
18009 in the index file. */
18010
18011static void
18012write_address_map (struct objfile *objfile, struct obstack *obstack,
18013 htab_t cu_index_htab)
18014{
18015 struct addrmap_index_data addrmap_index_data;
18016
18017 /* When writing the address table, we have to cope with the fact that
18018 the addrmap iterator only provides the start of a region; we have to
18019 wait until the next invocation to get the start of the next region. */
18020
18021 addrmap_index_data.objfile = objfile;
18022 addrmap_index_data.addr_obstack = obstack;
18023 addrmap_index_data.cu_index_htab = cu_index_htab;
18024 addrmap_index_data.previous_valid = 0;
18025
18026 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
18027 &addrmap_index_data);
18028
18029 /* It's highly unlikely the last entry (end address = 0xff...ff)
18030 is valid, but we should still handle it.
18031 The end address is recorded as the start of the next region, but that
18032 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
18033 anyway. */
18034 if (addrmap_index_data.previous_valid)
18035 add_address_entry (objfile, obstack,
18036 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
18037 addrmap_index_data.previous_cu_index);
9291a0cd
TT
18038}
18039
156942c7
DE
18040/* Return the symbol kind of PSYM. */
18041
18042static gdb_index_symbol_kind
18043symbol_kind (struct partial_symbol *psym)
18044{
18045 domain_enum domain = PSYMBOL_DOMAIN (psym);
18046 enum address_class aclass = PSYMBOL_CLASS (psym);
18047
18048 switch (domain)
18049 {
18050 case VAR_DOMAIN:
18051 switch (aclass)
18052 {
18053 case LOC_BLOCK:
18054 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
18055 case LOC_TYPEDEF:
18056 return GDB_INDEX_SYMBOL_KIND_TYPE;
18057 case LOC_COMPUTED:
18058 case LOC_CONST_BYTES:
18059 case LOC_OPTIMIZED_OUT:
18060 case LOC_STATIC:
18061 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
18062 case LOC_CONST:
18063 /* Note: It's currently impossible to recognize psyms as enum values
18064 short of reading the type info. For now punt. */
18065 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
18066 default:
18067 /* There are other LOC_FOO values that one might want to classify
18068 as variables, but dwarf2read.c doesn't currently use them. */
18069 return GDB_INDEX_SYMBOL_KIND_OTHER;
18070 }
18071 case STRUCT_DOMAIN:
18072 return GDB_INDEX_SYMBOL_KIND_TYPE;
18073 default:
18074 return GDB_INDEX_SYMBOL_KIND_OTHER;
18075 }
18076}
18077
9291a0cd 18078/* Add a list of partial symbols to SYMTAB. */
b89be57b 18079
9291a0cd
TT
18080static void
18081write_psymbols (struct mapped_symtab *symtab,
987d643c 18082 htab_t psyms_seen,
9291a0cd
TT
18083 struct partial_symbol **psymp,
18084 int count,
987d643c
TT
18085 offset_type cu_index,
18086 int is_static)
9291a0cd
TT
18087{
18088 for (; count-- > 0; ++psymp)
18089 {
156942c7
DE
18090 struct partial_symbol *psym = *psymp;
18091 void **slot;
987d643c 18092
156942c7 18093 if (SYMBOL_LANGUAGE (psym) == language_ada)
9291a0cd 18094 error (_("Ada is not currently supported by the index"));
987d643c 18095
987d643c 18096 /* Only add a given psymbol once. */
156942c7 18097 slot = htab_find_slot (psyms_seen, psym, INSERT);
987d643c
TT
18098 if (!*slot)
18099 {
156942c7
DE
18100 gdb_index_symbol_kind kind = symbol_kind (psym);
18101
18102 *slot = psym;
18103 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
18104 is_static, kind, cu_index);
987d643c 18105 }
9291a0cd
TT
18106 }
18107}
18108
18109/* Write the contents of an ("unfinished") obstack to FILE. Throw an
18110 exception if there is an error. */
b89be57b 18111
9291a0cd
TT
18112static void
18113write_obstack (FILE *file, struct obstack *obstack)
18114{
18115 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
18116 file)
18117 != obstack_object_size (obstack))
18118 error (_("couldn't data write to file"));
18119}
18120
18121/* Unlink a file if the argument is not NULL. */
b89be57b 18122
9291a0cd
TT
18123static void
18124unlink_if_set (void *p)
18125{
18126 char **filename = p;
18127 if (*filename)
18128 unlink (*filename);
18129}
18130
1fd400ff
TT
18131/* A helper struct used when iterating over debug_types. */
18132struct signatured_type_index_data
18133{
18134 struct objfile *objfile;
18135 struct mapped_symtab *symtab;
18136 struct obstack *types_list;
987d643c 18137 htab_t psyms_seen;
1fd400ff
TT
18138 int cu_index;
18139};
18140
18141/* A helper function that writes a single signatured_type to an
18142 obstack. */
b89be57b 18143
1fd400ff
TT
18144static int
18145write_one_signatured_type (void **slot, void *d)
18146{
18147 struct signatured_type_index_data *info = d;
18148 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
18149 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
18150 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
18151 gdb_byte val[8];
18152
18153 write_psymbols (info->symtab,
987d643c 18154 info->psyms_seen,
3e43a32a
MS
18155 info->objfile->global_psymbols.list
18156 + psymtab->globals_offset,
987d643c
TT
18157 psymtab->n_global_syms, info->cu_index,
18158 0);
1fd400ff 18159 write_psymbols (info->symtab,
987d643c 18160 info->psyms_seen,
3e43a32a
MS
18161 info->objfile->static_psymbols.list
18162 + psymtab->statics_offset,
987d643c
TT
18163 psymtab->n_static_syms, info->cu_index,
18164 1);
1fd400ff 18165
b64f50a1
JK
18166 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18167 entry->per_cu.offset.sect_off);
1fd400ff 18168 obstack_grow (info->types_list, val, 8);
3019eac3
DE
18169 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18170 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
18171 obstack_grow (info->types_list, val, 8);
18172 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
18173 obstack_grow (info->types_list, val, 8);
18174
18175 ++info->cu_index;
18176
18177 return 1;
18178}
18179
95554aad
TT
18180/* Recurse into all "included" dependencies and write their symbols as
18181 if they appeared in this psymtab. */
18182
18183static void
18184recursively_write_psymbols (struct objfile *objfile,
18185 struct partial_symtab *psymtab,
18186 struct mapped_symtab *symtab,
18187 htab_t psyms_seen,
18188 offset_type cu_index)
18189{
18190 int i;
18191
18192 for (i = 0; i < psymtab->number_of_dependencies; ++i)
18193 if (psymtab->dependencies[i]->user != NULL)
18194 recursively_write_psymbols (objfile, psymtab->dependencies[i],
18195 symtab, psyms_seen, cu_index);
18196
18197 write_psymbols (symtab,
18198 psyms_seen,
18199 objfile->global_psymbols.list + psymtab->globals_offset,
18200 psymtab->n_global_syms, cu_index,
18201 0);
18202 write_psymbols (symtab,
18203 psyms_seen,
18204 objfile->static_psymbols.list + psymtab->statics_offset,
18205 psymtab->n_static_syms, cu_index,
18206 1);
18207}
18208
9291a0cd 18209/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 18210
9291a0cd
TT
18211static void
18212write_psymtabs_to_index (struct objfile *objfile, const char *dir)
18213{
18214 struct cleanup *cleanup;
18215 char *filename, *cleanup_filename;
1fd400ff
TT
18216 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
18217 struct obstack cu_list, types_cu_list;
9291a0cd
TT
18218 int i;
18219 FILE *out_file;
18220 struct mapped_symtab *symtab;
18221 offset_type val, size_of_contents, total_len;
18222 struct stat st;
987d643c 18223 htab_t psyms_seen;
0a5429f6
DE
18224 htab_t cu_index_htab;
18225 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 18226
b4f2f049 18227 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 18228 return;
b4f2f049 18229
9291a0cd
TT
18230 if (dwarf2_per_objfile->using_index)
18231 error (_("Cannot use an index to create the index"));
18232
8b70b953
TT
18233 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
18234 error (_("Cannot make an index when the file has multiple .debug_types sections"));
18235
9291a0cd 18236 if (stat (objfile->name, &st) < 0)
7e17e088 18237 perror_with_name (objfile->name);
9291a0cd
TT
18238
18239 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
18240 INDEX_SUFFIX, (char *) NULL);
18241 cleanup = make_cleanup (xfree, filename);
18242
18243 out_file = fopen (filename, "wb");
18244 if (!out_file)
18245 error (_("Can't open `%s' for writing"), filename);
18246
18247 cleanup_filename = filename;
18248 make_cleanup (unlink_if_set, &cleanup_filename);
18249
18250 symtab = create_mapped_symtab ();
18251 make_cleanup (cleanup_mapped_symtab, symtab);
18252
18253 obstack_init (&addr_obstack);
18254 make_cleanup_obstack_free (&addr_obstack);
18255
18256 obstack_init (&cu_list);
18257 make_cleanup_obstack_free (&cu_list);
18258
1fd400ff
TT
18259 obstack_init (&types_cu_list);
18260 make_cleanup_obstack_free (&types_cu_list);
18261
987d643c
TT
18262 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
18263 NULL, xcalloc, xfree);
96408a79 18264 make_cleanup_htab_delete (psyms_seen);
987d643c 18265
0a5429f6
DE
18266 /* While we're scanning CU's create a table that maps a psymtab pointer
18267 (which is what addrmap records) to its index (which is what is recorded
18268 in the index file). This will later be needed to write the address
18269 table. */
18270 cu_index_htab = htab_create_alloc (100,
18271 hash_psymtab_cu_index,
18272 eq_psymtab_cu_index,
18273 NULL, xcalloc, xfree);
96408a79 18274 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
18275 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
18276 xmalloc (sizeof (struct psymtab_cu_index_map)
18277 * dwarf2_per_objfile->n_comp_units);
18278 make_cleanup (xfree, psymtab_cu_index_map);
18279
18280 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
18281 work here. Also, the debug_types entries do not appear in
18282 all_comp_units, but only in their own hash table. */
9291a0cd
TT
18283 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
18284 {
3e43a32a
MS
18285 struct dwarf2_per_cu_data *per_cu
18286 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 18287 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 18288 gdb_byte val[8];
0a5429f6
DE
18289 struct psymtab_cu_index_map *map;
18290 void **slot;
9291a0cd 18291
95554aad
TT
18292 if (psymtab->user == NULL)
18293 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 18294
0a5429f6
DE
18295 map = &psymtab_cu_index_map[i];
18296 map->psymtab = psymtab;
18297 map->cu_index = i;
18298 slot = htab_find_slot (cu_index_htab, map, INSERT);
18299 gdb_assert (slot != NULL);
18300 gdb_assert (*slot == NULL);
18301 *slot = map;
9291a0cd 18302
b64f50a1
JK
18303 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
18304 per_cu->offset.sect_off);
9291a0cd 18305 obstack_grow (&cu_list, val, 8);
e254ef6a 18306 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
18307 obstack_grow (&cu_list, val, 8);
18308 }
18309
0a5429f6
DE
18310 /* Dump the address map. */
18311 write_address_map (objfile, &addr_obstack, cu_index_htab);
18312
1fd400ff
TT
18313 /* Write out the .debug_type entries, if any. */
18314 if (dwarf2_per_objfile->signatured_types)
18315 {
18316 struct signatured_type_index_data sig_data;
18317
18318 sig_data.objfile = objfile;
18319 sig_data.symtab = symtab;
18320 sig_data.types_list = &types_cu_list;
987d643c 18321 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
18322 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
18323 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
18324 write_one_signatured_type, &sig_data);
18325 }
18326
156942c7
DE
18327 /* Now that we've processed all symbols we can shrink their cu_indices
18328 lists. */
18329 uniquify_cu_indices (symtab);
18330
9291a0cd
TT
18331 obstack_init (&constant_pool);
18332 make_cleanup_obstack_free (&constant_pool);
18333 obstack_init (&symtab_obstack);
18334 make_cleanup_obstack_free (&symtab_obstack);
18335 write_hash_table (symtab, &symtab_obstack, &constant_pool);
18336
18337 obstack_init (&contents);
18338 make_cleanup_obstack_free (&contents);
1fd400ff 18339 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
18340 total_len = size_of_contents;
18341
18342 /* The version number. */
156942c7 18343 val = MAYBE_SWAP (7);
9291a0cd
TT
18344 obstack_grow (&contents, &val, sizeof (val));
18345
18346 /* The offset of the CU list from the start of the file. */
18347 val = MAYBE_SWAP (total_len);
18348 obstack_grow (&contents, &val, sizeof (val));
18349 total_len += obstack_object_size (&cu_list);
18350
1fd400ff
TT
18351 /* The offset of the types CU list from the start of the file. */
18352 val = MAYBE_SWAP (total_len);
18353 obstack_grow (&contents, &val, sizeof (val));
18354 total_len += obstack_object_size (&types_cu_list);
18355
9291a0cd
TT
18356 /* The offset of the address table from the start of the file. */
18357 val = MAYBE_SWAP (total_len);
18358 obstack_grow (&contents, &val, sizeof (val));
18359 total_len += obstack_object_size (&addr_obstack);
18360
18361 /* The offset of the symbol table from the start of the file. */
18362 val = MAYBE_SWAP (total_len);
18363 obstack_grow (&contents, &val, sizeof (val));
18364 total_len += obstack_object_size (&symtab_obstack);
18365
18366 /* The offset of the constant pool from the start of the file. */
18367 val = MAYBE_SWAP (total_len);
18368 obstack_grow (&contents, &val, sizeof (val));
18369 total_len += obstack_object_size (&constant_pool);
18370
18371 gdb_assert (obstack_object_size (&contents) == size_of_contents);
18372
18373 write_obstack (out_file, &contents);
18374 write_obstack (out_file, &cu_list);
1fd400ff 18375 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
18376 write_obstack (out_file, &addr_obstack);
18377 write_obstack (out_file, &symtab_obstack);
18378 write_obstack (out_file, &constant_pool);
18379
18380 fclose (out_file);
18381
18382 /* We want to keep the file, so we set cleanup_filename to NULL
18383 here. See unlink_if_set. */
18384 cleanup_filename = NULL;
18385
18386 do_cleanups (cleanup);
18387}
18388
90476074
TT
18389/* Implementation of the `save gdb-index' command.
18390
18391 Note that the file format used by this command is documented in the
18392 GDB manual. Any changes here must be documented there. */
11570e71 18393
9291a0cd
TT
18394static void
18395save_gdb_index_command (char *arg, int from_tty)
18396{
18397 struct objfile *objfile;
18398
18399 if (!arg || !*arg)
96d19272 18400 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
18401
18402 ALL_OBJFILES (objfile)
18403 {
18404 struct stat st;
18405
18406 /* If the objfile does not correspond to an actual file, skip it. */
18407 if (stat (objfile->name, &st) < 0)
18408 continue;
18409
18410 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
18411 if (dwarf2_per_objfile)
18412 {
18413 volatile struct gdb_exception except;
18414
18415 TRY_CATCH (except, RETURN_MASK_ERROR)
18416 {
18417 write_psymtabs_to_index (objfile, arg);
18418 }
18419 if (except.reason < 0)
18420 exception_fprintf (gdb_stderr, except,
18421 _("Error while writing index for `%s': "),
18422 objfile->name);
18423 }
18424 }
dce234bc
PP
18425}
18426
9291a0cd
TT
18427\f
18428
9eae7c52
TT
18429int dwarf2_always_disassemble;
18430
18431static void
18432show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
18433 struct cmd_list_element *c, const char *value)
18434{
3e43a32a
MS
18435 fprintf_filtered (file,
18436 _("Whether to always disassemble "
18437 "DWARF expressions is %s.\n"),
9eae7c52
TT
18438 value);
18439}
18440
900e11f9
JK
18441static void
18442show_check_physname (struct ui_file *file, int from_tty,
18443 struct cmd_list_element *c, const char *value)
18444{
18445 fprintf_filtered (file,
18446 _("Whether to check \"physname\" is %s.\n"),
18447 value);
18448}
18449
6502dd73
DJ
18450void _initialize_dwarf2_read (void);
18451
18452void
18453_initialize_dwarf2_read (void)
18454{
96d19272
JK
18455 struct cmd_list_element *c;
18456
dce234bc 18457 dwarf2_objfile_data_key
c1bd65d0 18458 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 18459
1bedd215
AC
18460 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
18461Set DWARF 2 specific variables.\n\
18462Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
18463 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
18464 0/*allow-unknown*/, &maintenance_set_cmdlist);
18465
1bedd215
AC
18466 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
18467Show DWARF 2 specific variables\n\
18468Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
18469 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
18470 0/*allow-unknown*/, &maintenance_show_cmdlist);
18471
18472 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
18473 &dwarf2_max_cache_age, _("\
18474Set the upper bound on the age of cached dwarf2 compilation units."), _("\
18475Show the upper bound on the age of cached dwarf2 compilation units."), _("\
18476A higher limit means that cached compilation units will be stored\n\
18477in memory longer, and more total memory will be used. Zero disables\n\
18478caching, which can slow down startup."),
2c5b56ce 18479 NULL,
920d2a44 18480 show_dwarf2_max_cache_age,
2c5b56ce 18481 &set_dwarf2_cmdlist,
ae038cb0 18482 &show_dwarf2_cmdlist);
d97bc12b 18483
9eae7c52
TT
18484 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
18485 &dwarf2_always_disassemble, _("\
18486Set whether `info address' always disassembles DWARF expressions."), _("\
18487Show whether `info address' always disassembles DWARF expressions."), _("\
18488When enabled, DWARF expressions are always printed in an assembly-like\n\
18489syntax. When disabled, expressions will be printed in a more\n\
18490conversational style, when possible."),
18491 NULL,
18492 show_dwarf2_always_disassemble,
18493 &set_dwarf2_cmdlist,
18494 &show_dwarf2_cmdlist);
18495
45cfd468
DE
18496 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
18497Set debugging of the dwarf2 reader."), _("\
18498Show debugging of the dwarf2 reader."), _("\
18499When enabled, debugging messages are printed during dwarf2 reading\n\
18500and symtab expansion."),
18501 NULL,
18502 NULL,
18503 &setdebuglist, &showdebuglist);
18504
d97bc12b
DE
18505 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18506Set debugging of the dwarf2 DIE reader."), _("\
18507Show debugging of the dwarf2 DIE reader."), _("\
18508When enabled (non-zero), DIEs are dumped after they are read in.\n\
18509The value is the maximum depth to print."),
18510 NULL,
18511 NULL,
18512 &setdebuglist, &showdebuglist);
9291a0cd 18513
900e11f9
JK
18514 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18515Set cross-checking of \"physname\" code against demangler."), _("\
18516Show cross-checking of \"physname\" code against demangler."), _("\
18517When enabled, GDB's internal \"physname\" code is checked against\n\
18518the demangler."),
18519 NULL, show_check_physname,
18520 &setdebuglist, &showdebuglist);
18521
96d19272 18522 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 18523 _("\
fc1a9d6e 18524Save a gdb-index file.\n\
11570e71 18525Usage: save gdb-index DIRECTORY"),
96d19272
JK
18526 &save_cmdlist);
18527 set_cmd_completer (c, filename_completer);
6502dd73 18528}
This page took 2.546792 seconds and 4 git commands to generate.