PR macros/13205:
[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 */
60d5a603 65#include <ctype.h>
4c2df51b 66
c906108c
SS
67#include <fcntl.h>
68#include "gdb_string.h"
4bdf3d34 69#include "gdb_assert.h"
c906108c 70#include <sys/types.h>
233a11ab
CS
71#ifdef HAVE_ZLIB_H
72#include <zlib.h>
73#endif
dce234bc
PP
74#ifdef HAVE_MMAP
75#include <sys/mman.h>
85d9bd0e
TT
76#ifndef MAP_FAILED
77#define MAP_FAILED ((void *) -1)
78#endif
dce234bc 79#endif
d8151005 80
34eaf542
TT
81typedef struct symbol *symbolp;
82DEF_VEC_P (symbolp);
83
d97bc12b
DE
84/* When non-zero, dump DIEs after they are read in. */
85static int dwarf2_die_debug = 0;
86
900e11f9
JK
87/* When non-zero, cross-check physname against demangler. */
88static int check_physname = 0;
89
481860b3
GB
90/* When non-zero, do not reject deprecated .gdb_index sections. */
91int use_deprecated_index_sections = 0;
92
dce234bc
PP
93static int pagesize;
94
df8a16a1
DJ
95/* When set, the file that we're processing is known to have debugging
96 info for C++ namespaces. GCC 3.3.x did not produce this information,
97 but later versions do. */
98
99static int processing_has_namespace_info;
100
6502dd73
DJ
101static const struct objfile_data *dwarf2_objfile_data_key;
102
dce234bc
PP
103struct dwarf2_section_info
104{
105 asection *asection;
106 gdb_byte *buffer;
107 bfd_size_type size;
b315ab21
TG
108 /* Not NULL if the section was actually mmapped. */
109 void *map_addr;
110 /* Page aligned size of mmapped area. */
111 bfd_size_type map_len;
be391dca
TT
112 /* True if we have tried to read this section. */
113 int readin;
dce234bc
PP
114};
115
8b70b953
TT
116typedef struct dwarf2_section_info dwarf2_section_info_def;
117DEF_VEC_O (dwarf2_section_info_def);
118
9291a0cd
TT
119/* All offsets in the index are of this type. It must be
120 architecture-independent. */
121typedef uint32_t offset_type;
122
123DEF_VEC_I (offset_type);
124
125/* A description of the mapped index. The file format is described in
126 a comment by the code that writes the index. */
127struct mapped_index
128{
559a7a62
JK
129 /* Index data format version. */
130 int version;
131
9291a0cd
TT
132 /* The total length of the buffer. */
133 off_t total_size;
b11b1f88 134
9291a0cd
TT
135 /* A pointer to the address table data. */
136 const gdb_byte *address_table;
b11b1f88 137
9291a0cd
TT
138 /* Size of the address table data in bytes. */
139 offset_type address_table_size;
b11b1f88 140
3876f04e
DE
141 /* The symbol table, implemented as a hash table. */
142 const offset_type *symbol_table;
b11b1f88 143
9291a0cd 144 /* Size in slots, each slot is 2 offset_types. */
3876f04e 145 offset_type symbol_table_slots;
b11b1f88 146
9291a0cd
TT
147 /* A pointer to the constant pool. */
148 const char *constant_pool;
149};
150
95554aad
TT
151typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
152DEF_VEC_P (dwarf2_per_cu_ptr);
153
9cdd5dbd
DE
154/* Collection of data recorded per objfile.
155 This hangs off of dwarf2_objfile_data_key. */
156
6502dd73
DJ
157struct dwarf2_per_objfile
158{
dce234bc
PP
159 struct dwarf2_section_info info;
160 struct dwarf2_section_info abbrev;
161 struct dwarf2_section_info line;
dce234bc
PP
162 struct dwarf2_section_info loc;
163 struct dwarf2_section_info macinfo;
cf2c3c16 164 struct dwarf2_section_info macro;
dce234bc
PP
165 struct dwarf2_section_info str;
166 struct dwarf2_section_info ranges;
3019eac3 167 struct dwarf2_section_info addr;
dce234bc
PP
168 struct dwarf2_section_info frame;
169 struct dwarf2_section_info eh_frame;
9291a0cd 170 struct dwarf2_section_info gdb_index;
ae038cb0 171
8b70b953
TT
172 VEC (dwarf2_section_info_def) *types;
173
be391dca
TT
174 /* Back link. */
175 struct objfile *objfile;
176
d467dd73 177 /* Table of all the compilation units. This is used to locate
10b3939b 178 the target compilation unit of a particular reference. */
ae038cb0
DJ
179 struct dwarf2_per_cu_data **all_comp_units;
180
181 /* The number of compilation units in ALL_COMP_UNITS. */
182 int n_comp_units;
183
1fd400ff 184 /* The number of .debug_types-related CUs. */
d467dd73 185 int n_type_units;
1fd400ff 186
d467dd73
DE
187 /* The .debug_types-related CUs (TUs). */
188 struct dwarf2_per_cu_data **all_type_units;
1fd400ff 189
ae038cb0
DJ
190 /* A chain of compilation units that are currently read in, so that
191 they can be freed later. */
192 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 193
348e048f
DE
194 /* A table mapping .debug_types signatures to its signatured_type entry.
195 This is NULL if the .debug_types section hasn't been read in yet. */
196 htab_t signatured_types;
197
3019eac3
DE
198 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
199 This is NULL if the table hasn't been allocated yet. */
200 htab_t dwo_files;
201
72dca2f5
FR
202 /* A flag indicating wether this objfile has a section loaded at a
203 VMA of 0. */
204 int has_section_at_zero;
9291a0cd 205
ae2de4f8
DE
206 /* True if we are using the mapped index,
207 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
208 unsigned char using_index;
209
ae2de4f8 210 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 211 struct mapped_index *index_table;
98bfdba5 212
7b9f3c50
DE
213 /* When using index_table, this keeps track of all quick_file_names entries.
214 TUs can share line table entries with CUs or other TUs, and there can be
215 a lot more TUs than unique line tables, so we maintain a separate table
216 of all line table entries to support the sharing. */
217 htab_t quick_file_names_table;
218
98bfdba5
PA
219 /* Set during partial symbol reading, to prevent queueing of full
220 symbols. */
221 int reading_partial_symbols;
673bfd45 222
dee91e82 223 /* Table mapping type DIEs to their struct type *.
673bfd45 224 This is NULL if not allocated yet.
dee91e82
DE
225 The mapping is done via (CU/TU signature + DIE offset) -> type. */
226 htab_t die_type_hash;
95554aad
TT
227
228 /* The CUs we recently read. */
229 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
230};
231
232static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 233
251d32d9 234/* Default names of the debugging sections. */
c906108c 235
233a11ab
CS
236/* Note that if the debugging section has been compressed, it might
237 have a name like .zdebug_info. */
238
9cdd5dbd
DE
239static const struct dwarf2_debug_sections dwarf2_elf_names =
240{
251d32d9
TG
241 { ".debug_info", ".zdebug_info" },
242 { ".debug_abbrev", ".zdebug_abbrev" },
243 { ".debug_line", ".zdebug_line" },
244 { ".debug_loc", ".zdebug_loc" },
245 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 246 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
247 { ".debug_str", ".zdebug_str" },
248 { ".debug_ranges", ".zdebug_ranges" },
249 { ".debug_types", ".zdebug_types" },
3019eac3 250 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
251 { ".debug_frame", ".zdebug_frame" },
252 { ".eh_frame", NULL },
24d3216f
TT
253 { ".gdb_index", ".zgdb_index" },
254 23
251d32d9 255};
c906108c 256
3019eac3
DE
257/* List of DWO sections. */
258
259static const struct dwo_section_names
260{
261 struct dwarf2_section_names abbrev_dwo;
262 struct dwarf2_section_names info_dwo;
263 struct dwarf2_section_names line_dwo;
264 struct dwarf2_section_names loc_dwo;
265 struct dwarf2_section_names str_dwo;
266 struct dwarf2_section_names str_offsets_dwo;
267 struct dwarf2_section_names types_dwo;
268}
269dwo_section_names =
270{
271 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
272 { ".debug_info.dwo", ".zdebug_info.dwo" },
273 { ".debug_line.dwo", ".zdebug_line.dwo" },
274 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
275 { ".debug_str.dwo", ".zdebug_str.dwo" },
276 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
277 { ".debug_types.dwo", ".zdebug_types.dwo" },
278};
279
c906108c
SS
280/* local data types */
281
0963b4bd 282/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
283#ifndef ABBREV_HASH_SIZE
284#define ABBREV_HASH_SIZE 121
285#endif
286
107d2387
AC
287/* The data in a compilation unit header, after target2host
288 translation, looks like this. */
c906108c 289struct comp_unit_head
a738430d 290{
c764a876 291 unsigned int length;
a738430d 292 short version;
a738430d
MK
293 unsigned char addr_size;
294 unsigned char signed_addr_p;
b64f50a1 295 sect_offset abbrev_offset;
57349743 296
a738430d
MK
297 /* Size of file offsets; either 4 or 8. */
298 unsigned int offset_size;
57349743 299
a738430d
MK
300 /* Size of the length field; either 4 or 12. */
301 unsigned int initial_length_size;
57349743 302
a738430d
MK
303 /* Offset to the first byte of this compilation unit header in the
304 .debug_info section, for resolving relative reference dies. */
b64f50a1 305 sect_offset offset;
57349743 306
d00adf39
DE
307 /* Offset to first die in this cu from the start of the cu.
308 This will be the first byte following the compilation unit header. */
b64f50a1 309 cu_offset first_die_offset;
a738430d 310};
c906108c 311
3da10d80
KS
312/* Type used for delaying computation of method physnames.
313 See comments for compute_delayed_physnames. */
314struct delayed_method_info
315{
316 /* The type to which the method is attached, i.e., its parent class. */
317 struct type *type;
318
319 /* The index of the method in the type's function fieldlists. */
320 int fnfield_index;
321
322 /* The index of the method in the fieldlist. */
323 int index;
324
325 /* The name of the DIE. */
326 const char *name;
327
328 /* The DIE associated with this method. */
329 struct die_info *die;
330};
331
332typedef struct delayed_method_info delayed_method_info;
333DEF_VEC_O (delayed_method_info);
334
e7c27a73
DJ
335/* Internal state when decoding a particular compilation unit. */
336struct dwarf2_cu
337{
338 /* The objfile containing this compilation unit. */
339 struct objfile *objfile;
340
d00adf39 341 /* The header of the compilation unit. */
e7c27a73 342 struct comp_unit_head header;
e142c38c 343
d00adf39
DE
344 /* Base address of this compilation unit. */
345 CORE_ADDR base_address;
346
347 /* Non-zero if base_address has been set. */
348 int base_known;
349
e142c38c
DJ
350 /* The language we are debugging. */
351 enum language language;
352 const struct language_defn *language_defn;
353
b0f35d58
DL
354 const char *producer;
355
e142c38c
DJ
356 /* The generic symbol table building routines have separate lists for
357 file scope symbols and all all other scopes (local scopes). So
358 we need to select the right one to pass to add_symbol_to_list().
359 We do it by keeping a pointer to the correct list in list_in_scope.
360
361 FIXME: The original dwarf code just treated the file scope as the
362 first local scope, and all other local scopes as nested local
363 scopes, and worked fine. Check to see if we really need to
364 distinguish these in buildsym.c. */
365 struct pending **list_in_scope;
366
f3dd6933
DJ
367 /* DWARF abbreviation table associated with this compilation unit. */
368 struct abbrev_info **dwarf2_abbrevs;
369
370 /* Storage for the abbrev table. */
371 struct obstack abbrev_obstack;
72bf9492 372
b64f50a1
JK
373 /* Hash table holding all the loaded partial DIEs
374 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
375 htab_t partial_dies;
376
377 /* Storage for things with the same lifetime as this read-in compilation
378 unit, including partial DIEs. */
379 struct obstack comp_unit_obstack;
380
ae038cb0
DJ
381 /* When multiple dwarf2_cu structures are living in memory, this field
382 chains them all together, so that they can be released efficiently.
383 We will probably also want a generation counter so that most-recently-used
384 compilation units are cached... */
385 struct dwarf2_per_cu_data *read_in_chain;
386
387 /* Backchain to our per_cu entry if the tree has been built. */
388 struct dwarf2_per_cu_data *per_cu;
389
390 /* How many compilation units ago was this CU last referenced? */
391 int last_used;
392
b64f50a1
JK
393 /* A hash table of DIE cu_offset for following references with
394 die_info->offset.sect_off as hash. */
51545339 395 htab_t die_hash;
10b3939b
DJ
396
397 /* Full DIEs if read in. */
398 struct die_info *dies;
399
400 /* A set of pointers to dwarf2_per_cu_data objects for compilation
401 units referenced by this one. Only set during full symbol processing;
402 partial symbol tables do not have dependencies. */
403 htab_t dependencies;
404
cb1df416
DJ
405 /* Header data from the line table, during full symbol processing. */
406 struct line_header *line_header;
407
3da10d80
KS
408 /* A list of methods which need to have physnames computed
409 after all type information has been read. */
410 VEC (delayed_method_info) *method_list;
411
96408a79
SA
412 /* To be copied to symtab->call_site_htab. */
413 htab_t call_site_htab;
414
3019eac3
DE
415 /* Non-NULL if this CU came from a DWO file. */
416 struct dwo_unit *dwo_unit;
417
418 /* The DW_AT_addr_base attribute if present, zero otherwise
419 (zero is a valid value though).
420 Note this value comes from the stub CU/TU's DIE. */
421 ULONGEST addr_base;
422
ae038cb0
DJ
423 /* Mark used when releasing cached dies. */
424 unsigned int mark : 1;
425
8be455d7
JK
426 /* This CU references .debug_loc. See the symtab->locations_valid field.
427 This test is imperfect as there may exist optimized debug code not using
428 any location list and still facing inlining issues if handled as
429 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 430 unsigned int has_loclist : 1;
ba919b58
TT
431
432 /* These cache the results of producer_is_gxx_lt_4_6.
433 CHECKED_PRODUCER is set if PRODUCER_IS_GXX_LT_4_6 is valid. This
434 information is cached because profiling CU expansion showed
435 excessive time spent in producer_is_gxx_lt_4_6. */
436 unsigned int checked_producer : 1;
437 unsigned int producer_is_gxx_lt_4_6 : 1;
3019eac3
DE
438
439 /* Non-zero if DW_AT_addr_base was found.
440 Used when processing DWO files. */
441 unsigned int have_addr_base : 1;
e7c27a73
DJ
442};
443
10b3939b
DJ
444/* Persistent data held for a compilation unit, even when not
445 processing it. We put a pointer to this structure in the
28dee7f5 446 read_symtab_private field of the psymtab. */
10b3939b 447
ae038cb0
DJ
448struct dwarf2_per_cu_data
449{
348e048f 450 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 451 bytes should suffice to store the length of any compilation unit
45452591
DE
452 - if it doesn't, GDB will fall over anyway.
453 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
454 initial_length_size.
455 If the DIE refers to a DWO file, this is always of the original die,
456 not the DWO file. */
b64f50a1 457 sect_offset offset;
348e048f 458 unsigned int length : 29;
ae038cb0
DJ
459
460 /* Flag indicating this compilation unit will be read in before
461 any of the current compilation units are processed. */
c764a876 462 unsigned int queued : 1;
ae038cb0 463
0d99eb77
DE
464 /* This flag will be set when reading partial DIEs if we need to load
465 absolutely all DIEs for this compilation unit, instead of just the ones
466 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
467 hash table and don't find it. */
468 unsigned int load_all_dies : 1;
469
3019eac3
DE
470 /* Non-zero if this CU is from .debug_types. */
471 unsigned int is_debug_types : 1;
472
473 /* The section this CU/TU lives in.
474 If the DIE refers to a DWO file, this is always the original die,
475 not the DWO file. */
476 struct dwarf2_section_info *info_or_types_section;
348e048f 477
17ea53c3
JK
478 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
479 of the CU cache it gets reset to NULL again. */
ae038cb0 480 struct dwarf2_cu *cu;
1c379e20 481
9cdd5dbd
DE
482 /* The corresponding objfile.
483 Normally we can get the objfile from dwarf2_per_objfile.
484 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
485 struct objfile *objfile;
486
487 /* When using partial symbol tables, the 'psymtab' field is active.
488 Otherwise the 'quick' field is active. */
489 union
490 {
491 /* The partial symbol table associated with this compilation unit,
95554aad 492 or NULL for unread partial units. */
9291a0cd
TT
493 struct partial_symtab *psymtab;
494
495 /* Data needed by the "quick" functions. */
496 struct dwarf2_per_cu_quick_data *quick;
497 } v;
95554aad
TT
498
499 /* The CUs we import using DW_TAG_imported_unit. This is filled in
500 while reading psymtabs, used to compute the psymtab dependencies,
501 and then cleared. Then it is filled in again while reading full
502 symbols, and only deleted when the objfile is destroyed. */
503 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
504};
505
348e048f
DE
506/* Entry in the signatured_types hash table. */
507
508struct signatured_type
509{
3019eac3 510 /* The type's signature. */
348e048f
DE
511 ULONGEST signature;
512
3019eac3
DE
513 /* Offset in the TU of the type's DIE, as read from the TU header.
514 If the definition lives in a DWO file, this value is unusable. */
515 cu_offset type_offset_in_tu;
516
517 /* Offset in the section of the type's DIE.
518 If the definition lives in a DWO file, this is the offset in the
519 .debug_types.dwo section.
520 The value is zero until the actual value is known.
521 Zero is otherwise not a valid section offset. */
522 sect_offset type_offset_in_section;
348e048f
DE
523
524 /* The CU(/TU) of this type. */
525 struct dwarf2_per_cu_data per_cu;
526};
527
3019eac3
DE
528/* These sections are what may appear in a "dwo" file. */
529
530struct dwo_sections
531{
532 struct dwarf2_section_info abbrev;
533 struct dwarf2_section_info info;
534 struct dwarf2_section_info line;
535 struct dwarf2_section_info loc;
536 struct dwarf2_section_info str;
537 struct dwarf2_section_info str_offsets;
538 VEC (dwarf2_section_info_def) *types;
539};
540
541/* Common bits of DWO CUs/TUs. */
542
543struct dwo_unit
544{
545 /* Backlink to the containing struct dwo_file. */
546 struct dwo_file *dwo_file;
547
548 /* The "id" that distinguishes this CU/TU.
549 .debug_info calls this "dwo_id", .debug_types calls this "signature".
550 Since signatures came first, we stick with it for consistency. */
551 ULONGEST signature;
552
553 /* The section this CU/TU lives in, in the DWO file. */
554 struct dwarf2_section_info *info_or_types_section;
555
556 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
557 sect_offset offset;
558 unsigned int length;
559
560 /* For types, offset in the type's DIE of the type defined by this TU. */
561 cu_offset type_offset_in_tu;
562};
563
564/* Data for one DWO file. */
565
566struct dwo_file
567{
568 /* The DW_AT_GNU_dwo_name attribute.
569 We don't manage space for this, it's an attribute. */
570 const char *dwo_name;
571
572 /* The bfd, when the file is open. Otherwise this is NULL. */
573 bfd *dwo_bfd;
574
575 /* Section info for this file. */
576 struct dwo_sections sections;
577
578 /* Table of CUs in the file.
579 Each element is a struct dwo_unit. */
580 htab_t cus;
581
582 /* Table of TUs in the file.
583 Each element is a struct dwo_unit. */
584 htab_t tus;
585};
586
0963b4bd
MS
587/* Struct used to pass misc. parameters to read_die_and_children, et
588 al. which are used for both .debug_info and .debug_types dies.
589 All parameters here are unchanging for the life of the call. This
dee91e82 590 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
591
592struct die_reader_specs
593{
dee91e82 594 /* die_section->asection->owner. */
93311388
DE
595 bfd* abfd;
596
597 /* The CU of the DIE we are parsing. */
598 struct dwarf2_cu *cu;
599
3019eac3
DE
600 /* Non-NULL if reading a DWO file. */
601 struct dwo_file *dwo_file;
602
dee91e82 603 /* The section the die comes from.
3019eac3 604 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
605 struct dwarf2_section_info *die_section;
606
607 /* die_section->buffer. */
608 gdb_byte *buffer;
93311388
DE
609};
610
fd820528 611/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
612typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
613 gdb_byte *info_ptr,
614 struct die_info *comp_unit_die,
615 int has_children,
616 void *data);
617
debd256d
JB
618/* The line number information for a compilation unit (found in the
619 .debug_line section) begins with a "statement program header",
620 which contains the following information. */
621struct line_header
622{
623 unsigned int total_length;
624 unsigned short version;
625 unsigned int header_length;
626 unsigned char minimum_instruction_length;
2dc7f7b3 627 unsigned char maximum_ops_per_instruction;
debd256d
JB
628 unsigned char default_is_stmt;
629 int line_base;
630 unsigned char line_range;
631 unsigned char opcode_base;
632
633 /* standard_opcode_lengths[i] is the number of operands for the
634 standard opcode whose value is i. This means that
635 standard_opcode_lengths[0] is unused, and the last meaningful
636 element is standard_opcode_lengths[opcode_base - 1]. */
637 unsigned char *standard_opcode_lengths;
638
639 /* The include_directories table. NOTE! These strings are not
640 allocated with xmalloc; instead, they are pointers into
641 debug_line_buffer. If you try to free them, `free' will get
642 indigestion. */
643 unsigned int num_include_dirs, include_dirs_size;
644 char **include_dirs;
645
646 /* The file_names table. NOTE! These strings are not allocated
647 with xmalloc; instead, they are pointers into debug_line_buffer.
648 Don't try to free them directly. */
649 unsigned int num_file_names, file_names_size;
650 struct file_entry
c906108c 651 {
debd256d
JB
652 char *name;
653 unsigned int dir_index;
654 unsigned int mod_time;
655 unsigned int length;
aaa75496 656 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 657 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
658 } *file_names;
659
660 /* The start and end of the statement program following this
6502dd73 661 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 662 gdb_byte *statement_program_start, *statement_program_end;
debd256d 663};
c906108c
SS
664
665/* When we construct a partial symbol table entry we only
0963b4bd 666 need this much information. */
c906108c
SS
667struct partial_die_info
668 {
72bf9492 669 /* Offset of this DIE. */
b64f50a1 670 sect_offset offset;
72bf9492
DJ
671
672 /* DWARF-2 tag for this DIE. */
673 ENUM_BITFIELD(dwarf_tag) tag : 16;
674
72bf9492
DJ
675 /* Assorted flags describing the data found in this DIE. */
676 unsigned int has_children : 1;
677 unsigned int is_external : 1;
678 unsigned int is_declaration : 1;
679 unsigned int has_type : 1;
680 unsigned int has_specification : 1;
681 unsigned int has_pc_info : 1;
481860b3 682 unsigned int may_be_inlined : 1;
72bf9492
DJ
683
684 /* Flag set if the SCOPE field of this structure has been
685 computed. */
686 unsigned int scope_set : 1;
687
fa4028e9
JB
688 /* Flag set if the DIE has a byte_size attribute. */
689 unsigned int has_byte_size : 1;
690
98bfdba5
PA
691 /* Flag set if any of the DIE's children are template arguments. */
692 unsigned int has_template_arguments : 1;
693
abc72ce4
DE
694 /* Flag set if fixup_partial_die has been called on this die. */
695 unsigned int fixup_called : 1;
696
72bf9492 697 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 698 sometimes a default name for unnamed DIEs. */
c906108c 699 char *name;
72bf9492 700
abc72ce4
DE
701 /* The linkage name, if present. */
702 const char *linkage_name;
703
72bf9492
DJ
704 /* The scope to prepend to our children. This is generally
705 allocated on the comp_unit_obstack, so will disappear
706 when this compilation unit leaves the cache. */
707 char *scope;
708
95554aad
TT
709 /* Some data associated with the partial DIE. The tag determines
710 which field is live. */
711 union
712 {
713 /* The location description associated with this DIE, if any. */
714 struct dwarf_block *locdesc;
715 /* The offset of an import, for DW_TAG_imported_unit. */
716 sect_offset offset;
717 } d;
72bf9492
DJ
718
719 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
720 CORE_ADDR lowpc;
721 CORE_ADDR highpc;
72bf9492 722
93311388 723 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 724 DW_AT_sibling, if any. */
abc72ce4
DE
725 /* NOTE: This member isn't strictly necessary, read_partial_die could
726 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 727 gdb_byte *sibling;
72bf9492
DJ
728
729 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
730 DW_AT_specification (or DW_AT_abstract_origin or
731 DW_AT_extension). */
b64f50a1 732 sect_offset spec_offset;
72bf9492
DJ
733
734 /* Pointers to this DIE's parent, first child, and next sibling,
735 if any. */
736 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
737 };
738
0963b4bd 739/* This data structure holds the information of an abbrev. */
c906108c
SS
740struct abbrev_info
741 {
742 unsigned int number; /* number identifying abbrev */
743 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
744 unsigned short has_children; /* boolean */
745 unsigned short num_attrs; /* number of attributes */
c906108c
SS
746 struct attr_abbrev *attrs; /* an array of attribute descriptions */
747 struct abbrev_info *next; /* next in chain */
748 };
749
750struct attr_abbrev
751 {
9d25dd43
DE
752 ENUM_BITFIELD(dwarf_attribute) name : 16;
753 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
754 };
755
0963b4bd 756/* Attributes have a name and a value. */
b60c80d6
DJ
757struct attribute
758 {
9d25dd43 759 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
760 ENUM_BITFIELD(dwarf_form) form : 15;
761
762 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
763 field should be in u.str (existing only for DW_STRING) but it is kept
764 here for better struct attribute alignment. */
765 unsigned int string_is_canonical : 1;
766
b60c80d6
DJ
767 union
768 {
769 char *str;
770 struct dwarf_block *blk;
43bbcdc2
PH
771 ULONGEST unsnd;
772 LONGEST snd;
b60c80d6 773 CORE_ADDR addr;
348e048f 774 struct signatured_type *signatured_type;
b60c80d6
DJ
775 }
776 u;
777 };
778
0963b4bd 779/* This data structure holds a complete die structure. */
c906108c
SS
780struct die_info
781 {
76815b17
DE
782 /* DWARF-2 tag for this DIE. */
783 ENUM_BITFIELD(dwarf_tag) tag : 16;
784
785 /* Number of attributes */
98bfdba5
PA
786 unsigned char num_attrs;
787
788 /* True if we're presently building the full type name for the
789 type derived from this DIE. */
790 unsigned char building_fullname : 1;
76815b17
DE
791
792 /* Abbrev number */
793 unsigned int abbrev;
794
93311388 795 /* Offset in .debug_info or .debug_types section. */
b64f50a1 796 sect_offset offset;
78ba4af6
JB
797
798 /* The dies in a compilation unit form an n-ary tree. PARENT
799 points to this die's parent; CHILD points to the first child of
800 this node; and all the children of a given node are chained
4950bc1c 801 together via their SIBLING fields. */
639d11d3
DC
802 struct die_info *child; /* Its first child, if any. */
803 struct die_info *sibling; /* Its next sibling, if any. */
804 struct die_info *parent; /* Its parent, if any. */
c906108c 805
b60c80d6
DJ
806 /* An array of attributes, with NUM_ATTRS elements. There may be
807 zero, but it's not common and zero-sized arrays are not
808 sufficiently portable C. */
809 struct attribute attrs[1];
c906108c
SS
810 };
811
0963b4bd 812/* Get at parts of an attribute structure. */
c906108c
SS
813
814#define DW_STRING(attr) ((attr)->u.str)
8285870a 815#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
816#define DW_UNSND(attr) ((attr)->u.unsnd)
817#define DW_BLOCK(attr) ((attr)->u.blk)
818#define DW_SND(attr) ((attr)->u.snd)
819#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 820#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 821
0963b4bd 822/* Blocks are a bunch of untyped bytes. */
c906108c
SS
823struct dwarf_block
824 {
825 unsigned int size;
1d6edc3c
JK
826
827 /* Valid only if SIZE is not zero. */
fe1b8b76 828 gdb_byte *data;
c906108c
SS
829 };
830
c906108c
SS
831#ifndef ATTR_ALLOC_CHUNK
832#define ATTR_ALLOC_CHUNK 4
833#endif
834
c906108c
SS
835/* Allocate fields for structs, unions and enums in this size. */
836#ifndef DW_FIELD_ALLOC_CHUNK
837#define DW_FIELD_ALLOC_CHUNK 4
838#endif
839
c906108c
SS
840/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
841 but this would require a corresponding change in unpack_field_as_long
842 and friends. */
843static int bits_per_byte = 8;
844
845/* The routines that read and process dies for a C struct or C++ class
846 pass lists of data member fields and lists of member function fields
847 in an instance of a field_info structure, as defined below. */
848struct field_info
c5aa993b 849 {
0963b4bd 850 /* List of data member and baseclasses fields. */
c5aa993b
JM
851 struct nextfield
852 {
853 struct nextfield *next;
854 int accessibility;
855 int virtuality;
856 struct field field;
857 }
7d0ccb61 858 *fields, *baseclasses;
c906108c 859
7d0ccb61 860 /* Number of fields (including baseclasses). */
c5aa993b 861 int nfields;
c906108c 862
c5aa993b
JM
863 /* Number of baseclasses. */
864 int nbaseclasses;
c906108c 865
c5aa993b
JM
866 /* Set if the accesibility of one of the fields is not public. */
867 int non_public_fields;
c906108c 868
c5aa993b
JM
869 /* Member function fields array, entries are allocated in the order they
870 are encountered in the object file. */
871 struct nextfnfield
872 {
873 struct nextfnfield *next;
874 struct fn_field fnfield;
875 }
876 *fnfields;
c906108c 877
c5aa993b
JM
878 /* Member function fieldlist array, contains name of possibly overloaded
879 member function, number of overloaded member functions and a pointer
880 to the head of the member function field chain. */
881 struct fnfieldlist
882 {
883 char *name;
884 int length;
885 struct nextfnfield *head;
886 }
887 *fnfieldlists;
c906108c 888
c5aa993b
JM
889 /* Number of entries in the fnfieldlists array. */
890 int nfnfields;
98751a41
JK
891
892 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
893 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
894 struct typedef_field_list
895 {
896 struct typedef_field field;
897 struct typedef_field_list *next;
898 }
899 *typedef_field_list;
900 unsigned typedef_field_list_count;
c5aa993b 901 };
c906108c 902
10b3939b
DJ
903/* One item on the queue of compilation units to read in full symbols
904 for. */
905struct dwarf2_queue_item
906{
907 struct dwarf2_per_cu_data *per_cu;
95554aad 908 enum language pretend_language;
10b3939b
DJ
909 struct dwarf2_queue_item *next;
910};
911
912/* The current queue. */
913static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
914
ae038cb0
DJ
915/* Loaded secondary compilation units are kept in memory until they
916 have not been referenced for the processing of this many
917 compilation units. Set this to zero to disable caching. Cache
918 sizes of up to at least twenty will improve startup time for
919 typical inter-CU-reference binaries, at an obvious memory cost. */
920static int dwarf2_max_cache_age = 5;
920d2a44
AC
921static void
922show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
923 struct cmd_list_element *c, const char *value)
924{
3e43a32a
MS
925 fprintf_filtered (file, _("The upper bound on the age of cached "
926 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
927 value);
928}
929
ae038cb0 930
0963b4bd 931/* Various complaints about symbol reading that don't abort the process. */
c906108c 932
4d3c2250
KB
933static void
934dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 935{
4d3c2250 936 complaint (&symfile_complaints,
e2e0b3e5 937 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
938}
939
25e43795
DJ
940static void
941dwarf2_debug_line_missing_file_complaint (void)
942{
943 complaint (&symfile_complaints,
944 _(".debug_line section has line data without a file"));
945}
946
59205f5a
JB
947static void
948dwarf2_debug_line_missing_end_sequence_complaint (void)
949{
950 complaint (&symfile_complaints,
3e43a32a
MS
951 _(".debug_line section has line "
952 "program sequence without an end"));
59205f5a
JB
953}
954
4d3c2250
KB
955static void
956dwarf2_complex_location_expr_complaint (void)
2e276125 957{
e2e0b3e5 958 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
959}
960
4d3c2250
KB
961static void
962dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
963 int arg3)
2e276125 964{
4d3c2250 965 complaint (&symfile_complaints,
3e43a32a
MS
966 _("const value length mismatch for '%s', got %d, expected %d"),
967 arg1, arg2, arg3);
4d3c2250
KB
968}
969
970static void
cf2c3c16 971dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
2e276125 972{
4d3c2250 973 complaint (&symfile_complaints,
cf2c3c16
TT
974 _("macro info runs off end of `%s' section"),
975 section->asection->name);
4d3c2250
KB
976}
977
978static void
979dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 980{
4d3c2250 981 complaint (&symfile_complaints,
3e43a32a
MS
982 _("macro debug info contains a "
983 "malformed macro definition:\n`%s'"),
4d3c2250
KB
984 arg1);
985}
986
987static void
988dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 989{
4d3c2250 990 complaint (&symfile_complaints,
3e43a32a
MS
991 _("invalid attribute class or form for '%s' in '%s'"),
992 arg1, arg2);
4d3c2250 993}
c906108c 994
c906108c
SS
995/* local function prototypes */
996
4efb68b1 997static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 998
aaa75496
JB
999static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1000 struct objfile *);
1001
918dd910
JK
1002static void dwarf2_find_base_address (struct die_info *die,
1003 struct dwarf2_cu *cu);
1004
c67a9c90 1005static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1006
72bf9492
DJ
1007static void scan_partial_symbols (struct partial_die_info *,
1008 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1009 int, struct dwarf2_cu *);
c906108c 1010
72bf9492
DJ
1011static void add_partial_symbol (struct partial_die_info *,
1012 struct dwarf2_cu *);
63d06c5c 1013
72bf9492
DJ
1014static void add_partial_namespace (struct partial_die_info *pdi,
1015 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1016 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1017
5d7cb8df
JK
1018static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1019 CORE_ADDR *highpc, int need_pc,
1020 struct dwarf2_cu *cu);
1021
72bf9492
DJ
1022static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1023 struct dwarf2_cu *cu);
91c24f0a 1024
bc30ff58
JB
1025static void add_partial_subprogram (struct partial_die_info *pdi,
1026 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1027 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1028
a14ed312 1029static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 1030
a14ed312 1031static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1032
dee91e82
DE
1033static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1034 struct dwarf2_section_info *);
c906108c 1035
f3dd6933 1036static void dwarf2_free_abbrev_table (void *);
c906108c 1037
6caca83c
CC
1038static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1039
fe1b8b76 1040static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 1041 struct dwarf2_cu *);
72bf9492 1042
57349743 1043static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 1044 struct dwarf2_cu *);
c906108c 1045
dee91e82
DE
1046static struct partial_die_info *load_partial_dies
1047 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1048
dee91e82
DE
1049static gdb_byte *read_partial_die (const struct die_reader_specs *,
1050 struct partial_die_info *,
1051 struct abbrev_info *,
1052 unsigned int,
1053 gdb_byte *);
c906108c 1054
b64f50a1 1055static struct partial_die_info *find_partial_die (sect_offset,
10b3939b 1056 struct dwarf2_cu *);
72bf9492
DJ
1057
1058static void fixup_partial_die (struct partial_die_info *,
1059 struct dwarf2_cu *);
1060
dee91e82
DE
1061static gdb_byte *read_attribute (const struct die_reader_specs *,
1062 struct attribute *, struct attr_abbrev *,
1063 gdb_byte *);
a8329558 1064
fe1b8b76 1065static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 1066
fe1b8b76 1067static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 1068
fe1b8b76 1069static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 1070
fe1b8b76 1071static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 1072
93311388 1073static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 1074
fe1b8b76 1075static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1076 unsigned int *);
c906108c 1077
c764a876
DE
1078static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1079
1080static LONGEST read_checked_initial_length_and_offset
1081 (bfd *, gdb_byte *, const struct comp_unit_head *,
1082 unsigned int *, unsigned int *);
613e1657 1083
fe1b8b76 1084static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1085 unsigned int *);
1086
1087static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1088
fe1b8b76 1089static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1090
9b1c24c8 1091static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1092
fe1b8b76
JB
1093static char *read_indirect_string (bfd *, gdb_byte *,
1094 const struct comp_unit_head *,
1095 unsigned int *);
4bdf3d34 1096
12df843f 1097static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1098
12df843f 1099static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1100
3019eac3
DE
1101static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1102 unsigned int *);
1103
1104static char *read_str_index (const struct die_reader_specs *reader,
1105 struct dwarf2_cu *cu, ULONGEST str_index);
1106
fe1b8b76 1107static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 1108
e142c38c 1109static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1110
e142c38c
DJ
1111static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1112 struct dwarf2_cu *);
c906108c 1113
348e048f
DE
1114static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1115 unsigned int,
1116 struct dwarf2_cu *);
1117
05cf31d1
JB
1118static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1119 struct dwarf2_cu *cu);
1120
e142c38c 1121static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1122
e142c38c 1123static struct die_info *die_specification (struct die_info *die,
f2f0e013 1124 struct dwarf2_cu **);
63d06c5c 1125
debd256d
JB
1126static void free_line_header (struct line_header *lh);
1127
aaa75496
JB
1128static void add_file_name (struct line_header *, char *, unsigned int,
1129 unsigned int, unsigned int);
1130
3019eac3
DE
1131static struct line_header *dwarf_decode_line_header (unsigned int offset,
1132 struct dwarf2_cu *cu);
debd256d 1133
f3f5162e
DE
1134static void dwarf_decode_lines (struct line_header *, const char *,
1135 struct dwarf2_cu *, struct partial_symtab *,
1136 int);
c906108c 1137
72b9f47f 1138static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1139
a14ed312 1140static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1141 struct dwarf2_cu *);
c906108c 1142
34eaf542
TT
1143static struct symbol *new_symbol_full (struct die_info *, struct type *,
1144 struct dwarf2_cu *, struct symbol *);
1145
a14ed312 1146static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1147 struct dwarf2_cu *);
c906108c 1148
98bfdba5
PA
1149static void dwarf2_const_value_attr (struct attribute *attr,
1150 struct type *type,
1151 const char *name,
1152 struct obstack *obstack,
12df843f 1153 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1154 gdb_byte **bytes,
1155 struct dwarf2_locexpr_baton **baton);
2df3850c 1156
e7c27a73 1157static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1158
b4ba55a1
JB
1159static int need_gnat_info (struct dwarf2_cu *);
1160
3e43a32a
MS
1161static struct type *die_descriptive_type (struct die_info *,
1162 struct dwarf2_cu *);
b4ba55a1
JB
1163
1164static void set_descriptive_type (struct type *, struct die_info *,
1165 struct dwarf2_cu *);
1166
e7c27a73
DJ
1167static struct type *die_containing_type (struct die_info *,
1168 struct dwarf2_cu *);
c906108c 1169
673bfd45
DE
1170static struct type *lookup_die_type (struct die_info *, struct attribute *,
1171 struct dwarf2_cu *);
c906108c 1172
f792889a 1173static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1174
673bfd45
DE
1175static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1176
0d5cff50 1177static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1178
6e70227d 1179static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1180 const char *suffix, int physname,
1181 struct dwarf2_cu *cu);
63d06c5c 1182
e7c27a73 1183static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1184
348e048f
DE
1185static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1186
e7c27a73 1187static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1188
e7c27a73 1189static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1190
96408a79
SA
1191static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1192
ff013f42
JK
1193static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1194 struct dwarf2_cu *, struct partial_symtab *);
1195
a14ed312 1196static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1197 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1198 struct partial_symtab *);
c906108c 1199
fae299cd
DC
1200static void get_scope_pc_bounds (struct die_info *,
1201 CORE_ADDR *, CORE_ADDR *,
1202 struct dwarf2_cu *);
1203
801e3a5b
JB
1204static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1205 CORE_ADDR, struct dwarf2_cu *);
1206
a14ed312 1207static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1208 struct dwarf2_cu *);
c906108c 1209
a14ed312 1210static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1211 struct type *, struct dwarf2_cu *);
c906108c 1212
a14ed312 1213static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1214 struct die_info *, struct type *,
e7c27a73 1215 struct dwarf2_cu *);
c906108c 1216
a14ed312 1217static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1218 struct type *,
1219 struct dwarf2_cu *);
c906108c 1220
134d01f1 1221static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1222
e7c27a73 1223static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1224
e7c27a73 1225static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1226
5d7cb8df
JK
1227static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1228
27aa8d6a
SW
1229static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1230
f55ee35c
JK
1231static struct type *read_module_type (struct die_info *die,
1232 struct dwarf2_cu *cu);
1233
38d518c9 1234static const char *namespace_name (struct die_info *die,
e142c38c 1235 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1236
134d01f1 1237static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1238
e7c27a73 1239static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1240
6e70227d 1241static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1242 struct dwarf2_cu *);
1243
dee91e82 1244static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1245 gdb_byte *info_ptr,
fe1b8b76 1246 gdb_byte **new_info_ptr,
639d11d3
DC
1247 struct die_info *parent);
1248
dee91e82 1249static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1250 gdb_byte *info_ptr,
fe1b8b76 1251 gdb_byte **new_info_ptr,
639d11d3
DC
1252 struct die_info *parent);
1253
3019eac3
DE
1254static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1255 struct die_info **, gdb_byte *, int *, int);
1256
dee91e82
DE
1257static gdb_byte *read_full_die (const struct die_reader_specs *,
1258 struct die_info **, gdb_byte *, int *);
93311388 1259
e7c27a73 1260static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1261
71c25dea
TT
1262static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1263 struct obstack *);
1264
e142c38c 1265static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1266
98bfdba5
PA
1267static const char *dwarf2_full_name (char *name,
1268 struct die_info *die,
1269 struct dwarf2_cu *cu);
1270
e142c38c 1271static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1272 struct dwarf2_cu **);
9219021c 1273
f39c6ffd 1274static const char *dwarf_tag_name (unsigned int);
c906108c 1275
f39c6ffd 1276static const char *dwarf_attr_name (unsigned int);
c906108c 1277
f39c6ffd 1278static const char *dwarf_form_name (unsigned int);
c906108c 1279
a14ed312 1280static char *dwarf_bool_name (unsigned int);
c906108c 1281
f39c6ffd 1282static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1283
f9aca02d 1284static struct die_info *sibling_die (struct die_info *);
c906108c 1285
d97bc12b
DE
1286static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1287
1288static void dump_die_for_error (struct die_info *);
1289
1290static void dump_die_1 (struct ui_file *, int level, int max_level,
1291 struct die_info *);
c906108c 1292
d97bc12b 1293/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1294
51545339 1295static void store_in_ref_table (struct die_info *,
10b3939b 1296 struct dwarf2_cu *);
c906108c 1297
93311388
DE
1298static int is_ref_attr (struct attribute *);
1299
b64f50a1 1300static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1301
43bbcdc2 1302static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1303
348e048f
DE
1304static struct die_info *follow_die_ref_or_sig (struct die_info *,
1305 struct attribute *,
1306 struct dwarf2_cu **);
1307
10b3939b
DJ
1308static struct die_info *follow_die_ref (struct die_info *,
1309 struct attribute *,
f2f0e013 1310 struct dwarf2_cu **);
c906108c 1311
348e048f
DE
1312static struct die_info *follow_die_sig (struct die_info *,
1313 struct attribute *,
1314 struct dwarf2_cu **);
1315
6c83ed52
TT
1316static struct signatured_type *lookup_signatured_type_at_offset
1317 (struct objfile *objfile,
b64f50a1 1318 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1319
e5fe5e75 1320static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1321
52dc124a 1322static void read_signatured_type (struct signatured_type *);
348e048f 1323
c906108c
SS
1324/* memory allocation interface */
1325
7b5a2f43 1326static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1327
f3dd6933 1328static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1329
b60c80d6 1330static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1331
2e276125 1332static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1333 char *, bfd *, struct dwarf2_cu *,
1334 struct dwarf2_section_info *,
fceca515 1335 int, const char *);
2e276125 1336
8e19ed76
PS
1337static int attr_form_is_block (struct attribute *);
1338
3690dd37
JB
1339static int attr_form_is_section_offset (struct attribute *);
1340
1341static int attr_form_is_constant (struct attribute *);
1342
8cf6f0b1
TT
1343static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1344 struct dwarf2_loclist_baton *baton,
1345 struct attribute *attr);
1346
93e7bd98
DJ
1347static void dwarf2_symbol_mark_computed (struct attribute *attr,
1348 struct symbol *sym,
1349 struct dwarf2_cu *cu);
4c2df51b 1350
dee91e82
DE
1351static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1352 gdb_byte *info_ptr,
1353 struct abbrev_info *abbrev);
4bb7a0a7 1354
72bf9492
DJ
1355static void free_stack_comp_unit (void *);
1356
72bf9492
DJ
1357static hashval_t partial_die_hash (const void *item);
1358
1359static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1360
ae038cb0 1361static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
b64f50a1 1362 (sect_offset offset, struct objfile *objfile);
ae038cb0 1363
9816fde3 1364static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1365 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1366
1367static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1368 struct die_info *comp_unit_die,
1369 enum language pretend_language);
93311388 1370
68dc6402 1371static void free_heap_comp_unit (void *);
ae038cb0
DJ
1372
1373static void free_cached_comp_units (void *);
1374
1375static void age_cached_comp_units (void);
1376
dee91e82 1377static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1378
f792889a
DJ
1379static struct type *set_die_type (struct die_info *, struct type *,
1380 struct dwarf2_cu *);
1c379e20 1381
ae038cb0
DJ
1382static void create_all_comp_units (struct objfile *);
1383
0e50663e 1384static int create_all_type_units (struct objfile *);
1fd400ff 1385
95554aad
TT
1386static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1387 enum language);
10b3939b 1388
95554aad
TT
1389static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1390 enum language);
10b3939b
DJ
1391
1392static void dwarf2_add_dependence (struct dwarf2_cu *,
1393 struct dwarf2_per_cu_data *);
1394
ae038cb0
DJ
1395static void dwarf2_mark (struct dwarf2_cu *);
1396
1397static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1398
b64f50a1 1399static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1400 struct dwarf2_per_cu_data *per_cu);
1401
f792889a 1402static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1403
9291a0cd
TT
1404static void dwarf2_release_queue (void *dummy);
1405
95554aad
TT
1406static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1407 enum language pretend_language);
1408
1409static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1410 struct dwarf2_per_cu_data *per_cu,
1411 enum language pretend_language);
9291a0cd 1412
a0f42c21 1413static void process_queue (void);
9291a0cd
TT
1414
1415static void find_file_and_directory (struct die_info *die,
1416 struct dwarf2_cu *cu,
1417 char **name, char **comp_dir);
1418
1419static char *file_full_name (int file, struct line_header *lh,
1420 const char *comp_dir);
1421
9ff913ba
DE
1422static gdb_byte *read_and_check_comp_unit_head
1423 (struct comp_unit_head *header,
1424 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1425 int is_debug_types_section);
9291a0cd 1426
fd820528
DE
1427static void init_cutu_and_read_dies
1428 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
3019eac3
DE
1429 die_reader_func_ftype *die_reader_func, void *data);
1430
dee91e82
DE
1431static void init_cutu_and_read_dies_simple
1432 (struct dwarf2_per_cu_data *this_cu,
1433 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1434
673bfd45 1435static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1436
95554aad 1437static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *, int);
dee91e82 1438
3019eac3
DE
1439static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1440
1441static struct dwo_unit *lookup_dwo_comp_unit
1442 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1443
1444static struct dwo_unit *lookup_dwo_type_unit
1445 (struct signatured_type *, char *, const char *);
1446
1447static void free_dwo_file_cleanup (void *);
1448
1449static void munmap_section_buffer (struct dwarf2_section_info *);
1450
95554aad
TT
1451static void process_cu_includes (void);
1452
9291a0cd
TT
1453#if WORDS_BIGENDIAN
1454
1455/* Convert VALUE between big- and little-endian. */
1456static offset_type
1457byte_swap (offset_type value)
1458{
1459 offset_type result;
1460
1461 result = (value & 0xff) << 24;
1462 result |= (value & 0xff00) << 8;
1463 result |= (value & 0xff0000) >> 8;
1464 result |= (value & 0xff000000) >> 24;
1465 return result;
1466}
1467
1468#define MAYBE_SWAP(V) byte_swap (V)
1469
1470#else
1471#define MAYBE_SWAP(V) (V)
1472#endif /* WORDS_BIGENDIAN */
1473
1474/* The suffix for an index file. */
1475#define INDEX_SUFFIX ".gdb-index"
1476
3da10d80
KS
1477static const char *dwarf2_physname (char *name, struct die_info *die,
1478 struct dwarf2_cu *cu);
1479
c906108c 1480/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1481 information and return true if we have enough to do something.
1482 NAMES points to the dwarf2 section names, or is NULL if the standard
1483 ELF names are used. */
c906108c
SS
1484
1485int
251d32d9
TG
1486dwarf2_has_info (struct objfile *objfile,
1487 const struct dwarf2_debug_sections *names)
c906108c 1488{
be391dca
TT
1489 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1490 if (!dwarf2_per_objfile)
1491 {
1492 /* Initialize per-objfile state. */
1493 struct dwarf2_per_objfile *data
1494 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1495
be391dca
TT
1496 memset (data, 0, sizeof (*data));
1497 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1498 dwarf2_per_objfile = data;
6502dd73 1499
251d32d9
TG
1500 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1501 (void *) names);
be391dca
TT
1502 dwarf2_per_objfile->objfile = objfile;
1503 }
1504 return (dwarf2_per_objfile->info.asection != NULL
1505 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1506}
1507
251d32d9
TG
1508/* When loading sections, we look either for uncompressed section or for
1509 compressed section names. */
233a11ab
CS
1510
1511static int
251d32d9
TG
1512section_is_p (const char *section_name,
1513 const struct dwarf2_section_names *names)
233a11ab 1514{
251d32d9
TG
1515 if (names->normal != NULL
1516 && strcmp (section_name, names->normal) == 0)
1517 return 1;
1518 if (names->compressed != NULL
1519 && strcmp (section_name, names->compressed) == 0)
1520 return 1;
1521 return 0;
233a11ab
CS
1522}
1523
c906108c
SS
1524/* This function is mapped across the sections and remembers the
1525 offset and size of each of the debugging sections we are interested
1526 in. */
1527
1528static void
251d32d9 1529dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1530{
251d32d9
TG
1531 const struct dwarf2_debug_sections *names;
1532
1533 if (vnames == NULL)
1534 names = &dwarf2_elf_names;
1535 else
1536 names = (const struct dwarf2_debug_sections *) vnames;
1537
1538 if (section_is_p (sectp->name, &names->info))
c906108c 1539 {
dce234bc
PP
1540 dwarf2_per_objfile->info.asection = sectp;
1541 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1542 }
251d32d9 1543 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1544 {
dce234bc
PP
1545 dwarf2_per_objfile->abbrev.asection = sectp;
1546 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1547 }
251d32d9 1548 else if (section_is_p (sectp->name, &names->line))
c906108c 1549 {
dce234bc
PP
1550 dwarf2_per_objfile->line.asection = sectp;
1551 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1552 }
251d32d9 1553 else if (section_is_p (sectp->name, &names->loc))
c906108c 1554 {
dce234bc
PP
1555 dwarf2_per_objfile->loc.asection = sectp;
1556 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1557 }
251d32d9 1558 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1559 {
dce234bc
PP
1560 dwarf2_per_objfile->macinfo.asection = sectp;
1561 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1562 }
cf2c3c16
TT
1563 else if (section_is_p (sectp->name, &names->macro))
1564 {
1565 dwarf2_per_objfile->macro.asection = sectp;
1566 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1567 }
251d32d9 1568 else if (section_is_p (sectp->name, &names->str))
c906108c 1569 {
dce234bc
PP
1570 dwarf2_per_objfile->str.asection = sectp;
1571 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1572 }
3019eac3
DE
1573 else if (section_is_p (sectp->name, &names->addr))
1574 {
1575 dwarf2_per_objfile->addr.asection = sectp;
1576 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1577 }
251d32d9 1578 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1579 {
dce234bc
PP
1580 dwarf2_per_objfile->frame.asection = sectp;
1581 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1582 }
251d32d9 1583 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1584 {
3799ccc6 1585 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1586
3799ccc6
EZ
1587 if (aflag & SEC_HAS_CONTENTS)
1588 {
dce234bc
PP
1589 dwarf2_per_objfile->eh_frame.asection = sectp;
1590 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1591 }
b6af0555 1592 }
251d32d9 1593 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1594 {
dce234bc
PP
1595 dwarf2_per_objfile->ranges.asection = sectp;
1596 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1597 }
251d32d9 1598 else if (section_is_p (sectp->name, &names->types))
348e048f 1599 {
8b70b953
TT
1600 struct dwarf2_section_info type_section;
1601
1602 memset (&type_section, 0, sizeof (type_section));
1603 type_section.asection = sectp;
1604 type_section.size = bfd_get_section_size (sectp);
1605
1606 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1607 &type_section);
348e048f 1608 }
251d32d9 1609 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1610 {
1611 dwarf2_per_objfile->gdb_index.asection = sectp;
1612 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1613 }
dce234bc 1614
72dca2f5
FR
1615 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1616 && bfd_section_vma (abfd, sectp) == 0)
1617 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1618}
1619
dce234bc
PP
1620/* Decompress a section that was compressed using zlib. Store the
1621 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1622
1623static void
dce234bc
PP
1624zlib_decompress_section (struct objfile *objfile, asection *sectp,
1625 gdb_byte **outbuf, bfd_size_type *outsize)
1626{
3019eac3 1627 bfd *abfd = sectp->owner;
dce234bc
PP
1628#ifndef HAVE_ZLIB_H
1629 error (_("Support for zlib-compressed DWARF data (from '%s') "
1630 "is disabled in this copy of GDB"),
1631 bfd_get_filename (abfd));
1632#else
1633 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1634 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1635 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1636 bfd_size_type uncompressed_size;
1637 gdb_byte *uncompressed_buffer;
1638 z_stream strm;
1639 int rc;
1640 int header_size = 12;
1641
1642 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1643 || bfd_bread (compressed_buffer,
1644 compressed_size, abfd) != compressed_size)
dce234bc
PP
1645 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1646 bfd_get_filename (abfd));
1647
1648 /* Read the zlib header. In this case, it should be "ZLIB" followed
1649 by the uncompressed section size, 8 bytes in big-endian order. */
1650 if (compressed_size < header_size
1651 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1652 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1653 bfd_get_filename (abfd));
1654 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1655 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1656 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1657 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1658 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1659 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1660 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1661 uncompressed_size += compressed_buffer[11];
1662
1663 /* It is possible the section consists of several compressed
1664 buffers concatenated together, so we uncompress in a loop. */
1665 strm.zalloc = NULL;
1666 strm.zfree = NULL;
1667 strm.opaque = NULL;
1668 strm.avail_in = compressed_size - header_size;
1669 strm.next_in = (Bytef*) compressed_buffer + header_size;
1670 strm.avail_out = uncompressed_size;
1671 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1672 uncompressed_size);
1673 rc = inflateInit (&strm);
1674 while (strm.avail_in > 0)
1675 {
1676 if (rc != Z_OK)
1677 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1678 bfd_get_filename (abfd), rc);
1679 strm.next_out = ((Bytef*) uncompressed_buffer
1680 + (uncompressed_size - strm.avail_out));
1681 rc = inflate (&strm, Z_FINISH);
1682 if (rc != Z_STREAM_END)
1683 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1684 bfd_get_filename (abfd), rc);
1685 rc = inflateReset (&strm);
1686 }
1687 rc = inflateEnd (&strm);
1688 if (rc != Z_OK
1689 || strm.avail_out != 0)
1690 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1691 bfd_get_filename (abfd), rc);
1692
affddf13 1693 do_cleanups (cleanup);
dce234bc
PP
1694 *outbuf = uncompressed_buffer;
1695 *outsize = uncompressed_size;
1696#endif
233a11ab
CS
1697}
1698
fceca515
DE
1699/* A helper function that decides whether a section is empty,
1700 or not present. */
9e0ac564
TT
1701
1702static int
1703dwarf2_section_empty_p (struct dwarf2_section_info *info)
1704{
1705 return info->asection == NULL || info->size == 0;
1706}
1707
3019eac3
DE
1708/* Read the contents of the section INFO.
1709 OBJFILE is the main object file, but not necessarily the file where
1710 the section comes from. E.g., for DWO files INFO->asection->owner
1711 is the bfd of the DWO file.
dce234bc 1712 If the section is compressed, uncompress it before returning. */
c906108c 1713
dce234bc
PP
1714static void
1715dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1716{
dce234bc 1717 asection *sectp = info->asection;
3019eac3 1718 bfd *abfd;
dce234bc
PP
1719 gdb_byte *buf, *retbuf;
1720 unsigned char header[4];
c906108c 1721
be391dca
TT
1722 if (info->readin)
1723 return;
dce234bc 1724 info->buffer = NULL;
b315ab21 1725 info->map_addr = NULL;
be391dca 1726 info->readin = 1;
188dd5d6 1727
9e0ac564 1728 if (dwarf2_section_empty_p (info))
dce234bc 1729 return;
c906108c 1730
3019eac3
DE
1731 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1732 abfd = sectp->owner;
1733
dce234bc
PP
1734 /* Check if the file has a 4-byte header indicating compression. */
1735 if (info->size > sizeof (header)
1736 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1737 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1738 {
1739 /* Upon decompression, update the buffer and its size. */
1740 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1741 {
1742 zlib_decompress_section (objfile, sectp, &info->buffer,
1743 &info->size);
1744 return;
1745 }
1746 }
4bdf3d34 1747
dce234bc
PP
1748#ifdef HAVE_MMAP
1749 if (pagesize == 0)
1750 pagesize = getpagesize ();
2e276125 1751
dce234bc
PP
1752 /* Only try to mmap sections which are large enough: we don't want to
1753 waste space due to fragmentation. Also, only try mmap for sections
1754 without relocations. */
1755
1756 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1757 {
b315ab21
TG
1758 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1759 MAP_PRIVATE, sectp->filepos,
1760 &info->map_addr, &info->map_len);
dce234bc 1761
b315ab21 1762 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1763 {
be391dca 1764#if HAVE_POSIX_MADVISE
b315ab21 1765 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1766#endif
dce234bc
PP
1767 return;
1768 }
1769 }
1770#endif
1771
1772 /* If we get here, we are a normal, not-compressed section. */
1773 info->buffer = buf
1774 = obstack_alloc (&objfile->objfile_obstack, info->size);
1775
1776 /* When debugging .o files, we may need to apply relocations; see
1777 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1778 We never compress sections in .o files, so we only need to
1779 try this when the section is not compressed. */
ac8035ab 1780 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1781 if (retbuf != NULL)
1782 {
1783 info->buffer = retbuf;
1784 return;
1785 }
1786
1787 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1788 || bfd_bread (buf, info->size, abfd) != info->size)
1789 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1790 bfd_get_filename (abfd));
1791}
1792
9e0ac564
TT
1793/* A helper function that returns the size of a section in a safe way.
1794 If you are positive that the section has been read before using the
1795 size, then it is safe to refer to the dwarf2_section_info object's
1796 "size" field directly. In other cases, you must call this
1797 function, because for compressed sections the size field is not set
1798 correctly until the section has been read. */
1799
1800static bfd_size_type
1801dwarf2_section_size (struct objfile *objfile,
1802 struct dwarf2_section_info *info)
1803{
1804 if (!info->readin)
1805 dwarf2_read_section (objfile, info);
1806 return info->size;
1807}
1808
dce234bc 1809/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1810 SECTION_NAME. */
af34e669 1811
dce234bc 1812void
3017a003
TG
1813dwarf2_get_section_info (struct objfile *objfile,
1814 enum dwarf2_section_enum sect,
dce234bc
PP
1815 asection **sectp, gdb_byte **bufp,
1816 bfd_size_type *sizep)
1817{
1818 struct dwarf2_per_objfile *data
1819 = objfile_data (objfile, dwarf2_objfile_data_key);
1820 struct dwarf2_section_info *info;
a3b2a86b
TT
1821
1822 /* We may see an objfile without any DWARF, in which case we just
1823 return nothing. */
1824 if (data == NULL)
1825 {
1826 *sectp = NULL;
1827 *bufp = NULL;
1828 *sizep = 0;
1829 return;
1830 }
3017a003
TG
1831 switch (sect)
1832 {
1833 case DWARF2_DEBUG_FRAME:
1834 info = &data->frame;
1835 break;
1836 case DWARF2_EH_FRAME:
1837 info = &data->eh_frame;
1838 break;
1839 default:
1840 gdb_assert_not_reached ("unexpected section");
1841 }
dce234bc 1842
9e0ac564 1843 dwarf2_read_section (objfile, info);
dce234bc
PP
1844
1845 *sectp = info->asection;
1846 *bufp = info->buffer;
1847 *sizep = info->size;
1848}
1849
9291a0cd 1850\f
7b9f3c50
DE
1851/* DWARF quick_symbols_functions support. */
1852
1853/* TUs can share .debug_line entries, and there can be a lot more TUs than
1854 unique line tables, so we maintain a separate table of all .debug_line
1855 derived entries to support the sharing.
1856 All the quick functions need is the list of file names. We discard the
1857 line_header when we're done and don't need to record it here. */
1858struct quick_file_names
1859{
1860 /* The offset in .debug_line of the line table. We hash on this. */
1861 unsigned int offset;
1862
1863 /* The number of entries in file_names, real_names. */
1864 unsigned int num_file_names;
1865
1866 /* The file names from the line table, after being run through
1867 file_full_name. */
1868 const char **file_names;
1869
1870 /* The file names from the line table after being run through
1871 gdb_realpath. These are computed lazily. */
1872 const char **real_names;
1873};
1874
1875/* When using the index (and thus not using psymtabs), each CU has an
1876 object of this type. This is used to hold information needed by
1877 the various "quick" methods. */
1878struct dwarf2_per_cu_quick_data
1879{
1880 /* The file table. This can be NULL if there was no file table
1881 or it's currently not read in.
1882 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1883 struct quick_file_names *file_names;
1884
1885 /* The corresponding symbol table. This is NULL if symbols for this
1886 CU have not yet been read. */
1887 struct symtab *symtab;
1888
1889 /* A temporary mark bit used when iterating over all CUs in
1890 expand_symtabs_matching. */
1891 unsigned int mark : 1;
1892
1893 /* True if we've tried to read the file table and found there isn't one.
1894 There will be no point in trying to read it again next time. */
1895 unsigned int no_file_data : 1;
1896};
1897
1898/* Hash function for a quick_file_names. */
1899
1900static hashval_t
1901hash_file_name_entry (const void *e)
1902{
1903 const struct quick_file_names *file_data = e;
1904
1905 return file_data->offset;
1906}
1907
1908/* Equality function for a quick_file_names. */
1909
1910static int
1911eq_file_name_entry (const void *a, const void *b)
1912{
1913 const struct quick_file_names *ea = a;
1914 const struct quick_file_names *eb = b;
1915
1916 return ea->offset == eb->offset;
1917}
1918
1919/* Delete function for a quick_file_names. */
1920
1921static void
1922delete_file_name_entry (void *e)
1923{
1924 struct quick_file_names *file_data = e;
1925 int i;
1926
1927 for (i = 0; i < file_data->num_file_names; ++i)
1928 {
1929 xfree ((void*) file_data->file_names[i]);
1930 if (file_data->real_names)
1931 xfree ((void*) file_data->real_names[i]);
1932 }
1933
1934 /* The space for the struct itself lives on objfile_obstack,
1935 so we don't free it here. */
1936}
1937
1938/* Create a quick_file_names hash table. */
1939
1940static htab_t
1941create_quick_file_names_table (unsigned int nr_initial_entries)
1942{
1943 return htab_create_alloc (nr_initial_entries,
1944 hash_file_name_entry, eq_file_name_entry,
1945 delete_file_name_entry, xcalloc, xfree);
1946}
9291a0cd 1947
918dd910
JK
1948/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1949 have to be created afterwards. You should call age_cached_comp_units after
1950 processing PER_CU->CU. dw2_setup must have been already called. */
1951
1952static void
1953load_cu (struct dwarf2_per_cu_data *per_cu)
1954{
3019eac3 1955 if (per_cu->is_debug_types)
e5fe5e75 1956 load_full_type_unit (per_cu);
918dd910 1957 else
95554aad 1958 load_full_comp_unit (per_cu, language_minimal);
918dd910 1959
918dd910 1960 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
1961
1962 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
1963}
1964
a0f42c21 1965/* Read in the symbols for PER_CU. */
2fdf6df6 1966
9291a0cd 1967static void
a0f42c21 1968dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1969{
1970 struct cleanup *back_to;
1971
1972 back_to = make_cleanup (dwarf2_release_queue, NULL);
1973
95554aad
TT
1974 if (dwarf2_per_objfile->using_index
1975 ? per_cu->v.quick->symtab == NULL
1976 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
1977 {
1978 queue_comp_unit (per_cu, language_minimal);
1979 load_cu (per_cu);
1980 }
9291a0cd 1981
a0f42c21 1982 process_queue ();
9291a0cd
TT
1983
1984 /* Age the cache, releasing compilation units that have not
1985 been used recently. */
1986 age_cached_comp_units ();
1987
1988 do_cleanups (back_to);
1989}
1990
1991/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1992 the objfile from which this CU came. Returns the resulting symbol
1993 table. */
2fdf6df6 1994
9291a0cd 1995static struct symtab *
a0f42c21 1996dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 1997{
95554aad 1998 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
1999 if (!per_cu->v.quick->symtab)
2000 {
2001 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2002 increment_reading_symtab ();
a0f42c21 2003 dw2_do_instantiate_symtab (per_cu);
95554aad 2004 process_cu_includes ();
9291a0cd
TT
2005 do_cleanups (back_to);
2006 }
2007 return per_cu->v.quick->symtab;
2008}
2009
1fd400ff 2010/* Return the CU given its index. */
2fdf6df6 2011
1fd400ff
TT
2012static struct dwarf2_per_cu_data *
2013dw2_get_cu (int index)
2014{
2015 if (index >= dwarf2_per_objfile->n_comp_units)
2016 {
2017 index -= dwarf2_per_objfile->n_comp_units;
d467dd73 2018 return dwarf2_per_objfile->all_type_units[index];
1fd400ff
TT
2019 }
2020 return dwarf2_per_objfile->all_comp_units[index];
2021}
2022
9291a0cd
TT
2023/* A helper function that knows how to read a 64-bit value in a way
2024 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2025 otherwise. */
2fdf6df6 2026
9291a0cd
TT
2027static int
2028extract_cu_value (const char *bytes, ULONGEST *result)
2029{
2030 if (sizeof (ULONGEST) < 8)
2031 {
2032 int i;
2033
2034 /* Ignore the upper 4 bytes if they are all zero. */
2035 for (i = 0; i < 4; ++i)
2036 if (bytes[i + 4] != 0)
2037 return 0;
2038
2039 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2040 }
2041 else
2042 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2043 return 1;
2044}
2045
2046/* Read the CU list from the mapped index, and use it to create all
2047 the CU objects for this objfile. Return 0 if something went wrong,
2048 1 if everything went ok. */
2fdf6df6 2049
9291a0cd 2050static int
1fd400ff
TT
2051create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2052 offset_type cu_list_elements)
9291a0cd
TT
2053{
2054 offset_type i;
9291a0cd
TT
2055
2056 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2057 dwarf2_per_objfile->all_comp_units
2058 = obstack_alloc (&objfile->objfile_obstack,
2059 dwarf2_per_objfile->n_comp_units
2060 * sizeof (struct dwarf2_per_cu_data *));
2061
2062 for (i = 0; i < cu_list_elements; i += 2)
2063 {
2064 struct dwarf2_per_cu_data *the_cu;
2065 ULONGEST offset, length;
2066
2067 if (!extract_cu_value (cu_list, &offset)
2068 || !extract_cu_value (cu_list + 8, &length))
2069 return 0;
2070 cu_list += 2 * 8;
2071
2072 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2073 struct dwarf2_per_cu_data);
b64f50a1 2074 the_cu->offset.sect_off = offset;
9291a0cd
TT
2075 the_cu->length = length;
2076 the_cu->objfile = objfile;
3019eac3 2077 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
9291a0cd
TT
2078 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2079 struct dwarf2_per_cu_quick_data);
2080 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2081 }
2082
2083 return 1;
2084}
2085
1fd400ff 2086/* Create the signatured type hash table from the index. */
673bfd45 2087
1fd400ff 2088static int
673bfd45 2089create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2090 struct dwarf2_section_info *section,
673bfd45
DE
2091 const gdb_byte *bytes,
2092 offset_type elements)
1fd400ff
TT
2093{
2094 offset_type i;
673bfd45 2095 htab_t sig_types_hash;
1fd400ff 2096
d467dd73
DE
2097 dwarf2_per_objfile->n_type_units = elements / 3;
2098 dwarf2_per_objfile->all_type_units
1fd400ff 2099 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2100 dwarf2_per_objfile->n_type_units
1fd400ff
TT
2101 * sizeof (struct dwarf2_per_cu_data *));
2102
673bfd45 2103 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2104
2105 for (i = 0; i < elements; i += 3)
2106 {
52dc124a
DE
2107 struct signatured_type *sig_type;
2108 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2109 void **slot;
2110
2111 if (!extract_cu_value (bytes, &offset)
52dc124a 2112 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
2113 return 0;
2114 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2115 bytes += 3 * 8;
2116
52dc124a 2117 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2118 struct signatured_type);
52dc124a 2119 sig_type->signature = signature;
3019eac3
DE
2120 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2121 sig_type->per_cu.is_debug_types = 1;
2122 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2123 sig_type->per_cu.offset.sect_off = offset;
2124 sig_type->per_cu.objfile = objfile;
2125 sig_type->per_cu.v.quick
1fd400ff
TT
2126 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2127 struct dwarf2_per_cu_quick_data);
2128
52dc124a
DE
2129 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2130 *slot = sig_type;
1fd400ff 2131
52dc124a 2132 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
1fd400ff
TT
2133 }
2134
673bfd45 2135 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2136
2137 return 1;
2138}
2139
9291a0cd
TT
2140/* Read the address map data from the mapped index, and use it to
2141 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2142
9291a0cd
TT
2143static void
2144create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2145{
2146 const gdb_byte *iter, *end;
2147 struct obstack temp_obstack;
2148 struct addrmap *mutable_map;
2149 struct cleanup *cleanup;
2150 CORE_ADDR baseaddr;
2151
2152 obstack_init (&temp_obstack);
2153 cleanup = make_cleanup_obstack_free (&temp_obstack);
2154 mutable_map = addrmap_create_mutable (&temp_obstack);
2155
2156 iter = index->address_table;
2157 end = iter + index->address_table_size;
2158
2159 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2160
2161 while (iter < end)
2162 {
2163 ULONGEST hi, lo, cu_index;
2164 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2165 iter += 8;
2166 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2167 iter += 8;
2168 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2169 iter += 4;
2170
2171 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2172 dw2_get_cu (cu_index));
9291a0cd
TT
2173 }
2174
2175 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2176 &objfile->objfile_obstack);
2177 do_cleanups (cleanup);
2178}
2179
59d7bcaf
JK
2180/* The hash function for strings in the mapped index. This is the same as
2181 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2182 implementation. This is necessary because the hash function is tied to the
2183 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2184 SYMBOL_HASH_NEXT.
2185
2186 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2187
9291a0cd 2188static hashval_t
559a7a62 2189mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2190{
2191 const unsigned char *str = (const unsigned char *) p;
2192 hashval_t r = 0;
2193 unsigned char c;
2194
2195 while ((c = *str++) != 0)
559a7a62
JK
2196 {
2197 if (index_version >= 5)
2198 c = tolower (c);
2199 r = r * 67 + c - 113;
2200 }
9291a0cd
TT
2201
2202 return r;
2203}
2204
2205/* Find a slot in the mapped index INDEX for the object named NAME.
2206 If NAME is found, set *VEC_OUT to point to the CU vector in the
2207 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2208
9291a0cd
TT
2209static int
2210find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2211 offset_type **vec_out)
2212{
0cf03b49
JK
2213 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2214 offset_type hash;
9291a0cd 2215 offset_type slot, step;
559a7a62 2216 int (*cmp) (const char *, const char *);
9291a0cd 2217
0cf03b49
JK
2218 if (current_language->la_language == language_cplus
2219 || current_language->la_language == language_java
2220 || current_language->la_language == language_fortran)
2221 {
2222 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2223 not contain any. */
2224 const char *paren = strchr (name, '(');
2225
2226 if (paren)
2227 {
2228 char *dup;
2229
2230 dup = xmalloc (paren - name + 1);
2231 memcpy (dup, name, paren - name);
2232 dup[paren - name] = 0;
2233
2234 make_cleanup (xfree, dup);
2235 name = dup;
2236 }
2237 }
2238
559a7a62 2239 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2240 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2241 simulate our NAME being searched is also lowercased. */
2242 hash = mapped_index_string_hash ((index->version == 4
2243 && case_sensitivity == case_sensitive_off
2244 ? 5 : index->version),
2245 name);
2246
3876f04e
DE
2247 slot = hash & (index->symbol_table_slots - 1);
2248 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2249 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2250
2251 for (;;)
2252 {
2253 /* Convert a slot number to an offset into the table. */
2254 offset_type i = 2 * slot;
2255 const char *str;
3876f04e 2256 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2257 {
2258 do_cleanups (back_to);
2259 return 0;
2260 }
9291a0cd 2261
3876f04e 2262 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2263 if (!cmp (name, str))
9291a0cd
TT
2264 {
2265 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2266 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2267 do_cleanups (back_to);
9291a0cd
TT
2268 return 1;
2269 }
2270
3876f04e 2271 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2272 }
2273}
2274
2275/* Read the index file. If everything went ok, initialize the "quick"
2276 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2277
9291a0cd
TT
2278static int
2279dwarf2_read_index (struct objfile *objfile)
2280{
9291a0cd
TT
2281 char *addr;
2282 struct mapped_index *map;
b3b272e1 2283 offset_type *metadata;
ac0b195c
KW
2284 const gdb_byte *cu_list;
2285 const gdb_byte *types_list = NULL;
2286 offset_type version, cu_list_elements;
2287 offset_type types_list_elements = 0;
1fd400ff 2288 int i;
9291a0cd 2289
9e0ac564 2290 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2291 return 0;
82430852
JK
2292
2293 /* Older elfutils strip versions could keep the section in the main
2294 executable while splitting it for the separate debug info file. */
2295 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2296 & SEC_HAS_CONTENTS) == 0)
2297 return 0;
2298
9291a0cd
TT
2299 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2300
2301 addr = dwarf2_per_objfile->gdb_index.buffer;
2302 /* Version check. */
1fd400ff 2303 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2304 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2305 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2306 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2307 indices. */
831adc1f 2308 if (version < 4)
481860b3
GB
2309 {
2310 static int warning_printed = 0;
2311 if (!warning_printed)
2312 {
2313 warning (_("Skipping obsolete .gdb_index section in %s."),
2314 objfile->name);
2315 warning_printed = 1;
2316 }
2317 return 0;
2318 }
2319 /* Index version 4 uses a different hash function than index version
2320 5 and later.
2321
2322 Versions earlier than 6 did not emit psymbols for inlined
2323 functions. Using these files will cause GDB not to be able to
2324 set breakpoints on inlined functions by name, so we ignore these
2325 indices unless the --use-deprecated-index-sections command line
2326 option was supplied. */
2327 if (version < 6 && !use_deprecated_index_sections)
2328 {
2329 static int warning_printed = 0;
2330 if (!warning_printed)
2331 {
2332 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2333 "--use-deprecated-index-sections to use them anyway"),
2334 objfile->name);
2335 warning_printed = 1;
2336 }
2337 return 0;
2338 }
2339 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2340 longer backward compatible. */
481860b3 2341 if (version > 6)
594e8718 2342 return 0;
9291a0cd
TT
2343
2344 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2345 map->version = version;
b3b272e1 2346 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2347
2348 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2349
2350 i = 0;
2351 cu_list = addr + MAYBE_SWAP (metadata[i]);
2352 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2353 / 8);
1fd400ff
TT
2354 ++i;
2355
987d643c
TT
2356 types_list = addr + MAYBE_SWAP (metadata[i]);
2357 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2358 - MAYBE_SWAP (metadata[i]))
2359 / 8);
2360 ++i;
1fd400ff
TT
2361
2362 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2363 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2364 - MAYBE_SWAP (metadata[i]));
2365 ++i;
2366
3876f04e
DE
2367 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2368 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2369 - MAYBE_SWAP (metadata[i]))
2370 / (2 * sizeof (offset_type)));
1fd400ff 2371 ++i;
9291a0cd 2372
1fd400ff
TT
2373 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2374
0fefef59
DE
2375 /* Don't use the index if it's empty. */
2376 if (map->symbol_table_slots == 0)
2377 return 0;
2378
1fd400ff
TT
2379 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2380 return 0;
2381
8b70b953
TT
2382 if (types_list_elements)
2383 {
2384 struct dwarf2_section_info *section;
2385
2386 /* We can only handle a single .debug_types when we have an
2387 index. */
2388 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2389 return 0;
2390
2391 section = VEC_index (dwarf2_section_info_def,
2392 dwarf2_per_objfile->types, 0);
2393
2394 if (!create_signatured_type_table_from_index (objfile, section,
2395 types_list,
2396 types_list_elements))
2397 return 0;
2398 }
9291a0cd
TT
2399
2400 create_addrmap_from_index (objfile, map);
2401
2402 dwarf2_per_objfile->index_table = map;
2403 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2404 dwarf2_per_objfile->quick_file_names_table =
2405 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2406
2407 return 1;
2408}
2409
2410/* A helper for the "quick" functions which sets the global
2411 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2412
9291a0cd
TT
2413static void
2414dw2_setup (struct objfile *objfile)
2415{
2416 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2417 gdb_assert (dwarf2_per_objfile);
2418}
2419
dee91e82 2420/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2421
dee91e82
DE
2422static void
2423dw2_get_file_names_reader (const struct die_reader_specs *reader,
2424 gdb_byte *info_ptr,
2425 struct die_info *comp_unit_die,
2426 int has_children,
2427 void *data)
9291a0cd 2428{
dee91e82
DE
2429 struct dwarf2_cu *cu = reader->cu;
2430 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2431 struct objfile *objfile = dwarf2_per_objfile->objfile;
7b9f3c50 2432 struct line_header *lh;
9291a0cd 2433 struct attribute *attr;
dee91e82 2434 int i;
9ff913ba 2435 unsigned int bytes_read;
9291a0cd 2436 char *name, *comp_dir;
7b9f3c50
DE
2437 void **slot;
2438 struct quick_file_names *qfn;
2439 unsigned int line_offset;
9291a0cd 2440
7b9f3c50
DE
2441 lh = NULL;
2442 slot = NULL;
2443 line_offset = 0;
dee91e82
DE
2444
2445 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2446 if (attr)
2447 {
7b9f3c50
DE
2448 struct quick_file_names find_entry;
2449
2450 line_offset = DW_UNSND (attr);
2451
2452 /* We may have already read in this line header (TU line header sharing).
2453 If we have we're done. */
2454 find_entry.offset = line_offset;
2455 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2456 &find_entry, INSERT);
2457 if (*slot != NULL)
2458 {
7b9f3c50 2459 this_cu->v.quick->file_names = *slot;
dee91e82 2460 return;
7b9f3c50
DE
2461 }
2462
3019eac3 2463 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2464 }
2465 if (lh == NULL)
2466 {
7b9f3c50 2467 this_cu->v.quick->no_file_data = 1;
dee91e82 2468 return;
9291a0cd
TT
2469 }
2470
7b9f3c50
DE
2471 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2472 qfn->offset = line_offset;
2473 gdb_assert (slot != NULL);
2474 *slot = qfn;
9291a0cd 2475
dee91e82 2476 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2477
7b9f3c50
DE
2478 qfn->num_file_names = lh->num_file_names;
2479 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2480 lh->num_file_names * sizeof (char *));
9291a0cd 2481 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2482 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2483 qfn->real_names = NULL;
9291a0cd 2484
7b9f3c50 2485 free_line_header (lh);
7b9f3c50
DE
2486
2487 this_cu->v.quick->file_names = qfn;
dee91e82
DE
2488}
2489
2490/* A helper for the "quick" functions which attempts to read the line
2491 table for THIS_CU. */
2492
2493static struct quick_file_names *
2494dw2_get_file_names (struct objfile *objfile,
2495 struct dwarf2_per_cu_data *this_cu)
2496{
2497 if (this_cu->v.quick->file_names != NULL)
2498 return this_cu->v.quick->file_names;
2499 /* If we know there is no line data, no point in looking again. */
2500 if (this_cu->v.quick->no_file_data)
2501 return NULL;
2502
3019eac3
DE
2503 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2504 in the stub for CUs, there's is no need to lookup the DWO file.
2505 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2506 DWO file. */
2507 if (this_cu->is_debug_types)
fd820528 2508 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
3019eac3
DE
2509 else
2510 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2511
2512 if (this_cu->v.quick->no_file_data)
2513 return NULL;
2514 return this_cu->v.quick->file_names;
9291a0cd
TT
2515}
2516
2517/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2518 real path for a given file name from the line table. */
2fdf6df6 2519
9291a0cd 2520static const char *
7b9f3c50
DE
2521dw2_get_real_path (struct objfile *objfile,
2522 struct quick_file_names *qfn, int index)
9291a0cd 2523{
7b9f3c50
DE
2524 if (qfn->real_names == NULL)
2525 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2526 qfn->num_file_names, sizeof (char *));
9291a0cd 2527
7b9f3c50
DE
2528 if (qfn->real_names[index] == NULL)
2529 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2530
7b9f3c50 2531 return qfn->real_names[index];
9291a0cd
TT
2532}
2533
2534static struct symtab *
2535dw2_find_last_source_symtab (struct objfile *objfile)
2536{
2537 int index;
ae2de4f8 2538
9291a0cd
TT
2539 dw2_setup (objfile);
2540 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2541 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2542}
2543
7b9f3c50
DE
2544/* Traversal function for dw2_forget_cached_source_info. */
2545
2546static int
2547dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2548{
7b9f3c50 2549 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2550
7b9f3c50 2551 if (file_data->real_names)
9291a0cd 2552 {
7b9f3c50 2553 int i;
9291a0cd 2554
7b9f3c50 2555 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2556 {
7b9f3c50
DE
2557 xfree ((void*) file_data->real_names[i]);
2558 file_data->real_names[i] = NULL;
9291a0cd
TT
2559 }
2560 }
7b9f3c50
DE
2561
2562 return 1;
2563}
2564
2565static void
2566dw2_forget_cached_source_info (struct objfile *objfile)
2567{
2568 dw2_setup (objfile);
2569
2570 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2571 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2572}
2573
f8eba3c6
TT
2574/* Helper function for dw2_map_symtabs_matching_filename that expands
2575 the symtabs and calls the iterator. */
2576
2577static int
2578dw2_map_expand_apply (struct objfile *objfile,
2579 struct dwarf2_per_cu_data *per_cu,
2580 const char *name,
2581 const char *full_path, const char *real_path,
2582 int (*callback) (struct symtab *, void *),
2583 void *data)
2584{
2585 struct symtab *last_made = objfile->symtabs;
2586
2587 /* Don't visit already-expanded CUs. */
2588 if (per_cu->v.quick->symtab)
2589 return 0;
2590
2591 /* This may expand more than one symtab, and we want to iterate over
2592 all of them. */
a0f42c21 2593 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2594
2595 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2596 objfile->symtabs, last_made);
2597}
2598
2599/* Implementation of the map_symtabs_matching_filename method. */
2600
9291a0cd 2601static int
f8eba3c6
TT
2602dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2603 const char *full_path, const char *real_path,
2604 int (*callback) (struct symtab *, void *),
2605 void *data)
9291a0cd
TT
2606{
2607 int i;
c011a4f4 2608 const char *name_basename = lbasename (name);
4aac40c8
TT
2609 int name_len = strlen (name);
2610 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
2611
2612 dw2_setup (objfile);
ae2de4f8 2613
1fd400ff 2614 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2615 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2616 {
2617 int j;
e254ef6a 2618 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2619 struct quick_file_names *file_data;
9291a0cd 2620
3d7bb9d9 2621 /* We only need to look at symtabs not already expanded. */
e254ef6a 2622 if (per_cu->v.quick->symtab)
9291a0cd
TT
2623 continue;
2624
7b9f3c50
DE
2625 file_data = dw2_get_file_names (objfile, per_cu);
2626 if (file_data == NULL)
9291a0cd
TT
2627 continue;
2628
7b9f3c50 2629 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2630 {
7b9f3c50 2631 const char *this_name = file_data->file_names[j];
9291a0cd 2632
4aac40c8
TT
2633 if (FILENAME_CMP (name, this_name) == 0
2634 || (!is_abs && compare_filenames_for_search (this_name,
2635 name, name_len)))
9291a0cd 2636 {
f8eba3c6
TT
2637 if (dw2_map_expand_apply (objfile, per_cu,
2638 name, full_path, real_path,
2639 callback, data))
2640 return 1;
4aac40c8 2641 }
9291a0cd 2642
c011a4f4
DE
2643 /* Before we invoke realpath, which can get expensive when many
2644 files are involved, do a quick comparison of the basenames. */
2645 if (! basenames_may_differ
2646 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2647 continue;
2648
9291a0cd
TT
2649 if (full_path != NULL)
2650 {
7b9f3c50
DE
2651 const char *this_real_name = dw2_get_real_path (objfile,
2652 file_data, j);
9291a0cd 2653
7b9f3c50 2654 if (this_real_name != NULL
4aac40c8
TT
2655 && (FILENAME_CMP (full_path, this_real_name) == 0
2656 || (!is_abs
2657 && compare_filenames_for_search (this_real_name,
2658 name, name_len))))
9291a0cd 2659 {
f8eba3c6
TT
2660 if (dw2_map_expand_apply (objfile, per_cu,
2661 name, full_path, real_path,
2662 callback, data))
2663 return 1;
9291a0cd
TT
2664 }
2665 }
2666
2667 if (real_path != NULL)
2668 {
7b9f3c50
DE
2669 const char *this_real_name = dw2_get_real_path (objfile,
2670 file_data, j);
9291a0cd 2671
7b9f3c50 2672 if (this_real_name != NULL
4aac40c8
TT
2673 && (FILENAME_CMP (real_path, this_real_name) == 0
2674 || (!is_abs
2675 && compare_filenames_for_search (this_real_name,
2676 name, name_len))))
9291a0cd 2677 {
f8eba3c6
TT
2678 if (dw2_map_expand_apply (objfile, per_cu,
2679 name, full_path, real_path,
2680 callback, data))
2681 return 1;
9291a0cd
TT
2682 }
2683 }
2684 }
2685 }
2686
9291a0cd
TT
2687 return 0;
2688}
2689
2690static struct symtab *
2691dw2_lookup_symbol (struct objfile *objfile, int block_index,
2692 const char *name, domain_enum domain)
2693{
774b6a14 2694 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2695 instead. */
2696 return NULL;
2697}
2698
2699/* A helper function that expands all symtabs that hold an object
2700 named NAME. */
2fdf6df6 2701
9291a0cd
TT
2702static void
2703dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2704{
2705 dw2_setup (objfile);
2706
ae2de4f8 2707 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2708 if (dwarf2_per_objfile->index_table)
2709 {
2710 offset_type *vec;
2711
2712 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2713 name, &vec))
2714 {
2715 offset_type i, len = MAYBE_SWAP (*vec);
2716 for (i = 0; i < len; ++i)
2717 {
2718 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2719 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2720
a0f42c21 2721 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2722 }
2723 }
2724 }
2725}
2726
774b6a14
TT
2727static void
2728dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2729 enum block_enum block_kind, const char *name,
774b6a14 2730 domain_enum domain)
9291a0cd 2731{
774b6a14 2732 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2733}
2734
2735static void
2736dw2_print_stats (struct objfile *objfile)
2737{
2738 int i, count;
2739
2740 dw2_setup (objfile);
2741 count = 0;
1fd400ff 2742 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2743 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2744 {
e254ef6a 2745 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2746
e254ef6a 2747 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2748 ++count;
2749 }
2750 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2751}
2752
2753static void
2754dw2_dump (struct objfile *objfile)
2755{
2756 /* Nothing worth printing. */
2757}
2758
2759static void
2760dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2761 struct section_offsets *delta)
2762{
2763 /* There's nothing to relocate here. */
2764}
2765
2766static void
2767dw2_expand_symtabs_for_function (struct objfile *objfile,
2768 const char *func_name)
2769{
2770 dw2_do_expand_symtabs_matching (objfile, func_name);
2771}
2772
2773static void
2774dw2_expand_all_symtabs (struct objfile *objfile)
2775{
2776 int i;
2777
2778 dw2_setup (objfile);
1fd400ff
TT
2779
2780 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2781 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2782 {
e254ef6a 2783 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2784
a0f42c21 2785 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2786 }
2787}
2788
2789static void
2790dw2_expand_symtabs_with_filename (struct objfile *objfile,
2791 const char *filename)
2792{
2793 int i;
2794
2795 dw2_setup (objfile);
d4637a04
DE
2796
2797 /* We don't need to consider type units here.
2798 This is only called for examining code, e.g. expand_line_sal.
2799 There can be an order of magnitude (or more) more type units
2800 than comp units, and we avoid them if we can. */
2801
2802 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2803 {
2804 int j;
e254ef6a 2805 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2806 struct quick_file_names *file_data;
9291a0cd 2807
3d7bb9d9 2808 /* We only need to look at symtabs not already expanded. */
e254ef6a 2809 if (per_cu->v.quick->symtab)
9291a0cd
TT
2810 continue;
2811
7b9f3c50
DE
2812 file_data = dw2_get_file_names (objfile, per_cu);
2813 if (file_data == NULL)
9291a0cd
TT
2814 continue;
2815
7b9f3c50 2816 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2817 {
7b9f3c50 2818 const char *this_name = file_data->file_names[j];
1ef75ecc 2819 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2820 {
a0f42c21 2821 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2822 break;
2823 }
2824 }
2825 }
2826}
2827
dd786858 2828static const char *
9291a0cd
TT
2829dw2_find_symbol_file (struct objfile *objfile, const char *name)
2830{
e254ef6a 2831 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2832 offset_type *vec;
7b9f3c50 2833 struct quick_file_names *file_data;
9291a0cd
TT
2834
2835 dw2_setup (objfile);
2836
ae2de4f8 2837 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2838 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2839 {
2840 struct symtab *s;
2841
2842 ALL_OBJFILE_SYMTABS (objfile, s)
2843 if (s->primary)
2844 {
2845 struct blockvector *bv = BLOCKVECTOR (s);
2846 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2847 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2848
2849 if (sym)
2850 return sym->symtab->filename;
2851 }
2852 return NULL;
2853 }
9291a0cd
TT
2854
2855 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2856 name, &vec))
2857 return NULL;
2858
2859 /* Note that this just looks at the very first one named NAME -- but
2860 actually we are looking for a function. find_main_filename
2861 should be rewritten so that it doesn't require a custom hook. It
2862 could just use the ordinary symbol tables. */
2863 /* vec[0] is the length, which must always be >0. */
e254ef6a 2864 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2865
7b9f3c50 2866 file_data = dw2_get_file_names (objfile, per_cu);
943cb756
DE
2867 if (file_data == NULL
2868 || file_data->num_file_names == 0)
9291a0cd
TT
2869 return NULL;
2870
7b9f3c50 2871 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2872}
2873
2874static void
40658b94
PH
2875dw2_map_matching_symbols (const char * name, domain_enum namespace,
2876 struct objfile *objfile, int global,
2877 int (*callback) (struct block *,
2878 struct symbol *, void *),
2edb89d3
JK
2879 void *data, symbol_compare_ftype *match,
2880 symbol_compare_ftype *ordered_compare)
9291a0cd 2881{
40658b94 2882 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2883 current language is Ada for a non-Ada objfile using GNU index. As Ada
2884 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2885}
2886
2887static void
f8eba3c6
TT
2888dw2_expand_symtabs_matching
2889 (struct objfile *objfile,
2890 int (*file_matcher) (const char *, void *),
e078317b 2891 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
2892 enum search_domain kind,
2893 void *data)
9291a0cd
TT
2894{
2895 int i;
2896 offset_type iter;
4b5246aa 2897 struct mapped_index *index;
9291a0cd
TT
2898
2899 dw2_setup (objfile);
ae2de4f8
DE
2900
2901 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2902 if (!dwarf2_per_objfile->index_table)
2903 return;
4b5246aa 2904 index = dwarf2_per_objfile->index_table;
9291a0cd 2905
7b08b9eb 2906 if (file_matcher != NULL)
24c79950
TT
2907 {
2908 struct cleanup *cleanup;
2909 htab_t visited_found, visited_not_found;
2910
2911 visited_found = htab_create_alloc (10,
2912 htab_hash_pointer, htab_eq_pointer,
2913 NULL, xcalloc, xfree);
2914 cleanup = make_cleanup_htab_delete (visited_found);
2915 visited_not_found = htab_create_alloc (10,
2916 htab_hash_pointer, htab_eq_pointer,
2917 NULL, xcalloc, xfree);
2918 make_cleanup_htab_delete (visited_not_found);
2919
2920 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2921 + dwarf2_per_objfile->n_type_units); ++i)
2922 {
2923 int j;
2924 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2925 struct quick_file_names *file_data;
2926 void **slot;
7b08b9eb 2927
24c79950 2928 per_cu->v.quick->mark = 0;
3d7bb9d9 2929
24c79950
TT
2930 /* We only need to look at symtabs not already expanded. */
2931 if (per_cu->v.quick->symtab)
2932 continue;
7b08b9eb 2933
24c79950
TT
2934 file_data = dw2_get_file_names (objfile, per_cu);
2935 if (file_data == NULL)
2936 continue;
7b08b9eb 2937
24c79950
TT
2938 if (htab_find (visited_not_found, file_data) != NULL)
2939 continue;
2940 else if (htab_find (visited_found, file_data) != NULL)
2941 {
2942 per_cu->v.quick->mark = 1;
2943 continue;
2944 }
2945
2946 for (j = 0; j < file_data->num_file_names; ++j)
2947 {
2948 if (file_matcher (file_data->file_names[j], data))
2949 {
2950 per_cu->v.quick->mark = 1;
2951 break;
2952 }
2953 }
2954
2955 slot = htab_find_slot (per_cu->v.quick->mark
2956 ? visited_found
2957 : visited_not_found,
2958 file_data, INSERT);
2959 *slot = file_data;
2960 }
2961
2962 do_cleanups (cleanup);
2963 }
9291a0cd 2964
3876f04e 2965 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2966 {
2967 offset_type idx = 2 * iter;
2968 const char *name;
2969 offset_type *vec, vec_len, vec_idx;
2970
3876f04e 2971 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2972 continue;
2973
3876f04e 2974 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 2975
e078317b 2976 if (! (*name_matcher) (name, data))
9291a0cd
TT
2977 continue;
2978
2979 /* The name was matched, now expand corresponding CUs that were
2980 marked. */
4b5246aa 2981 vec = (offset_type *) (index->constant_pool
3876f04e 2982 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2983 vec_len = MAYBE_SWAP (vec[0]);
2984 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2985 {
e254ef6a 2986 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2987
e254ef6a 2988 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2989 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 2990 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2991 }
2992 }
2993}
2994
2995static struct symtab *
2996dw2_find_pc_sect_symtab (struct objfile *objfile,
2997 struct minimal_symbol *msymbol,
2998 CORE_ADDR pc,
2999 struct obj_section *section,
3000 int warn_if_readin)
3001{
3002 struct dwarf2_per_cu_data *data;
3003
3004 dw2_setup (objfile);
3005
3006 if (!objfile->psymtabs_addrmap)
3007 return NULL;
3008
3009 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3010 if (!data)
3011 return NULL;
3012
3013 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3014 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3015 paddress (get_objfile_arch (objfile), pc));
3016
a0f42c21 3017 return dw2_instantiate_symtab (data);
9291a0cd
TT
3018}
3019
9291a0cd 3020static void
44b13c5a 3021dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3022 void *data, int need_fullname)
9291a0cd
TT
3023{
3024 int i;
24c79950
TT
3025 struct cleanup *cleanup;
3026 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3027 NULL, xcalloc, xfree);
9291a0cd 3028
24c79950 3029 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3030 dw2_setup (objfile);
ae2de4f8 3031
24c79950
TT
3032 /* We can ignore file names coming from already-expanded CUs. */
3033 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3034 + dwarf2_per_objfile->n_type_units); ++i)
3035 {
3036 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3037
3038 if (per_cu->v.quick->symtab)
3039 {
3040 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3041 INSERT);
3042
3043 *slot = per_cu->v.quick->file_names;
3044 }
3045 }
3046
1fd400ff 3047 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3048 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
3049 {
3050 int j;
e254ef6a 3051 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3052 struct quick_file_names *file_data;
24c79950 3053 void **slot;
9291a0cd 3054
3d7bb9d9 3055 /* We only need to look at symtabs not already expanded. */
e254ef6a 3056 if (per_cu->v.quick->symtab)
9291a0cd
TT
3057 continue;
3058
7b9f3c50
DE
3059 file_data = dw2_get_file_names (objfile, per_cu);
3060 if (file_data == NULL)
9291a0cd
TT
3061 continue;
3062
24c79950
TT
3063 slot = htab_find_slot (visited, file_data, INSERT);
3064 if (*slot)
3065 {
3066 /* Already visited. */
3067 continue;
3068 }
3069 *slot = file_data;
3070
7b9f3c50 3071 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3072 {
74e2f255
DE
3073 const char *this_real_name;
3074
3075 if (need_fullname)
3076 this_real_name = dw2_get_real_path (objfile, file_data, j);
3077 else
3078 this_real_name = NULL;
7b9f3c50 3079 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3080 }
3081 }
24c79950
TT
3082
3083 do_cleanups (cleanup);
9291a0cd
TT
3084}
3085
3086static int
3087dw2_has_symbols (struct objfile *objfile)
3088{
3089 return 1;
3090}
3091
3092const struct quick_symbol_functions dwarf2_gdb_index_functions =
3093{
3094 dw2_has_symbols,
3095 dw2_find_last_source_symtab,
3096 dw2_forget_cached_source_info,
f8eba3c6 3097 dw2_map_symtabs_matching_filename,
9291a0cd 3098 dw2_lookup_symbol,
774b6a14 3099 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3100 dw2_print_stats,
3101 dw2_dump,
3102 dw2_relocate,
3103 dw2_expand_symtabs_for_function,
3104 dw2_expand_all_symtabs,
3105 dw2_expand_symtabs_with_filename,
3106 dw2_find_symbol_file,
40658b94 3107 dw2_map_matching_symbols,
9291a0cd
TT
3108 dw2_expand_symtabs_matching,
3109 dw2_find_pc_sect_symtab,
9291a0cd
TT
3110 dw2_map_symbol_filenames
3111};
3112
3113/* Initialize for reading DWARF for this objfile. Return 0 if this
3114 file will use psymtabs, or 1 if using the GNU index. */
3115
3116int
3117dwarf2_initialize_objfile (struct objfile *objfile)
3118{
3119 /* If we're about to read full symbols, don't bother with the
3120 indices. In this case we also don't care if some other debug
3121 format is making psymtabs, because they are all about to be
3122 expanded anyway. */
3123 if ((objfile->flags & OBJF_READNOW))
3124 {
3125 int i;
3126
3127 dwarf2_per_objfile->using_index = 1;
3128 create_all_comp_units (objfile);
0e50663e 3129 create_all_type_units (objfile);
7b9f3c50
DE
3130 dwarf2_per_objfile->quick_file_names_table =
3131 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3132
1fd400ff 3133 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3134 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3135 {
e254ef6a 3136 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3137
e254ef6a
DE
3138 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3139 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3140 }
3141
3142 /* Return 1 so that gdb sees the "quick" functions. However,
3143 these functions will be no-ops because we will have expanded
3144 all symtabs. */
3145 return 1;
3146 }
3147
3148 if (dwarf2_read_index (objfile))
3149 return 1;
3150
9291a0cd
TT
3151 return 0;
3152}
3153
3154\f
3155
dce234bc
PP
3156/* Build a partial symbol table. */
3157
3158void
f29dff0a 3159dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3160{
f29dff0a 3161 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3162 {
3163 init_psymbol_list (objfile, 1024);
3164 }
3165
d146bf1e 3166 dwarf2_build_psymtabs_hard (objfile);
c906108c 3167}
c906108c 3168
45452591
DE
3169/* Return TRUE if OFFSET is within CU_HEADER. */
3170
3171static inline int
b64f50a1 3172offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3173{
b64f50a1
JK
3174 sect_offset bottom = { cu_header->offset.sect_off };
3175 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3176 + cu_header->initial_length_size) };
9a619af0 3177
b64f50a1 3178 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3179}
3180
93311388
DE
3181/* Read in the comp unit header information from the debug_info at info_ptr.
3182 NOTE: This leaves members offset, first_die_offset to be filled in
3183 by the caller. */
107d2387 3184
fe1b8b76 3185static gdb_byte *
107d2387 3186read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3187 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3188{
3189 int signed_addr;
891d2f0b 3190 unsigned int bytes_read;
c764a876
DE
3191
3192 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3193 cu_header->initial_length_size = bytes_read;
3194 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3195 info_ptr += bytes_read;
107d2387
AC
3196 cu_header->version = read_2_bytes (abfd, info_ptr);
3197 info_ptr += 2;
b64f50a1
JK
3198 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3199 &bytes_read);
613e1657 3200 info_ptr += bytes_read;
107d2387
AC
3201 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3202 info_ptr += 1;
3203 signed_addr = bfd_get_sign_extend_vma (abfd);
3204 if (signed_addr < 0)
8e65ff28 3205 internal_error (__FILE__, __LINE__,
e2e0b3e5 3206 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3207 cu_header->signed_addr_p = signed_addr;
c764a876 3208
107d2387
AC
3209 return info_ptr;
3210}
3211
9ff913ba
DE
3212/* Subroutine of read_and_check_comp_unit_head and
3213 read_and_check_type_unit_head to simplify them.
3214 Perform various error checking on the header. */
3215
3216static void
3217error_check_comp_unit_head (struct comp_unit_head *header,
3218 struct dwarf2_section_info *section)
3219{
3220 bfd *abfd = section->asection->owner;
3221 const char *filename = bfd_get_filename (abfd);
3222
3223 if (header->version != 2 && header->version != 3 && header->version != 4)
3224 error (_("Dwarf Error: wrong version in compilation unit header "
3225 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3226 filename);
3227
b64f50a1 3228 if (header->abbrev_offset.sect_off
9ff913ba
DE
3229 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3230 &dwarf2_per_objfile->abbrev))
3231 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3232 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3233 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3234 filename);
3235
3236 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3237 avoid potential 32-bit overflow. */
b64f50a1 3238 if (((unsigned long) header->offset.sect_off
9ff913ba
DE
3239 + header->length + header->initial_length_size)
3240 > section->size)
3241 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3242 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3243 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3244 filename);
3245}
3246
3247/* Read in a CU/TU header and perform some basic error checking.
3248 The contents of the header are stored in HEADER.
3249 The result is a pointer to the start of the first DIE. */
adabb602 3250
fe1b8b76 3251static gdb_byte *
9ff913ba
DE
3252read_and_check_comp_unit_head (struct comp_unit_head *header,
3253 struct dwarf2_section_info *section,
3254 gdb_byte *info_ptr,
3255 int is_debug_types_section)
72bf9492 3256{
fe1b8b76 3257 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3258 bfd *abfd = section->asection->owner;
72bf9492 3259
b64f50a1 3260 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3261
72bf9492
DJ
3262 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3263
460c1c54
CC
3264 /* If we're reading a type unit, skip over the signature and
3265 type_offset fields. */
b0df02fd 3266 if (is_debug_types_section)
460c1c54
CC
3267 info_ptr += 8 /*signature*/ + header->offset_size;
3268
b64f50a1 3269 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3270
9ff913ba 3271 error_check_comp_unit_head (header, section);
72bf9492
DJ
3272
3273 return info_ptr;
3274}
3275
348e048f
DE
3276/* Read in the types comp unit header information from .debug_types entry at
3277 types_ptr. The result is a pointer to one past the end of the header. */
3278
3279static gdb_byte *
9ff913ba
DE
3280read_and_check_type_unit_head (struct comp_unit_head *header,
3281 struct dwarf2_section_info *section,
3282 gdb_byte *info_ptr,
dee91e82
DE
3283 ULONGEST *signature,
3284 cu_offset *type_offset_in_tu)
348e048f 3285{
9ff913ba
DE
3286 gdb_byte *beg_of_comp_unit = info_ptr;
3287 bfd *abfd = section->asection->owner;
348e048f 3288
b64f50a1 3289 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3290
9ff913ba 3291 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3292
9ff913ba
DE
3293 /* If we're reading a type unit, skip over the signature and
3294 type_offset fields. */
3295 if (signature != NULL)
3296 *signature = read_8_bytes (abfd, info_ptr);
3297 info_ptr += 8;
dee91e82
DE
3298 if (type_offset_in_tu != NULL)
3299 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3300 header->offset_size);
9ff913ba
DE
3301 info_ptr += header->offset_size;
3302
b64f50a1 3303 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3304
9ff913ba
DE
3305 error_check_comp_unit_head (header, section);
3306
3307 return info_ptr;
348e048f
DE
3308}
3309
aaa75496
JB
3310/* Allocate a new partial symtab for file named NAME and mark this new
3311 partial symtab as being an include of PST. */
3312
3313static void
3314dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3315 struct objfile *objfile)
3316{
3317 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3318
3319 subpst->section_offsets = pst->section_offsets;
3320 subpst->textlow = 0;
3321 subpst->texthigh = 0;
3322
3323 subpst->dependencies = (struct partial_symtab **)
3324 obstack_alloc (&objfile->objfile_obstack,
3325 sizeof (struct partial_symtab *));
3326 subpst->dependencies[0] = pst;
3327 subpst->number_of_dependencies = 1;
3328
3329 subpst->globals_offset = 0;
3330 subpst->n_global_syms = 0;
3331 subpst->statics_offset = 0;
3332 subpst->n_static_syms = 0;
3333 subpst->symtab = NULL;
3334 subpst->read_symtab = pst->read_symtab;
3335 subpst->readin = 0;
3336
3337 /* No private part is necessary for include psymtabs. This property
3338 can be used to differentiate between such include psymtabs and
10b3939b 3339 the regular ones. */
58a9656e 3340 subpst->read_symtab_private = NULL;
aaa75496
JB
3341}
3342
3343/* Read the Line Number Program data and extract the list of files
3344 included by the source file represented by PST. Build an include
d85a05f0 3345 partial symtab for each of these included files. */
aaa75496
JB
3346
3347static void
3348dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
3349 struct die_info *die,
3350 struct partial_symtab *pst)
aaa75496 3351{
d85a05f0
DJ
3352 struct line_header *lh = NULL;
3353 struct attribute *attr;
aaa75496 3354
d85a05f0
DJ
3355 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3356 if (attr)
3019eac3 3357 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
3358 if (lh == NULL)
3359 return; /* No linetable, so no includes. */
3360
c6da4cef 3361 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3362 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3363
3364 free_line_header (lh);
3365}
3366
348e048f 3367static hashval_t
52dc124a 3368hash_signatured_type (const void *item)
348e048f 3369{
52dc124a 3370 const struct signatured_type *sig_type = item;
9a619af0 3371
348e048f 3372 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 3373 return sig_type->signature;
348e048f
DE
3374}
3375
3376static int
52dc124a 3377eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
3378{
3379 const struct signatured_type *lhs = item_lhs;
3380 const struct signatured_type *rhs = item_rhs;
9a619af0 3381
348e048f
DE
3382 return lhs->signature == rhs->signature;
3383}
3384
1fd400ff
TT
3385/* Allocate a hash table for signatured types. */
3386
3387static htab_t
673bfd45 3388allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3389{
3390 return htab_create_alloc_ex (41,
52dc124a
DE
3391 hash_signatured_type,
3392 eq_signatured_type,
1fd400ff
TT
3393 NULL,
3394 &objfile->objfile_obstack,
3395 hashtab_obstack_allocate,
3396 dummy_obstack_deallocate);
3397}
3398
d467dd73 3399/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3400
3401static int
d467dd73 3402add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3403{
3404 struct signatured_type *sigt = *slot;
3405 struct dwarf2_per_cu_data ***datap = datum;
3406
3407 **datap = &sigt->per_cu;
3408 ++*datap;
3409
3410 return 1;
3411}
3412
3019eac3
DE
3413/* Create the hash table of all entries in the .debug_types section.
3414 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3415 The result is a pointer to the hash table or NULL if there are
3416 no types. */
348e048f 3417
3019eac3
DE
3418static htab_t
3419create_debug_types_hash_table (struct dwo_file *dwo_file,
3420 VEC (dwarf2_section_info_def) *types)
348e048f 3421{
3019eac3 3422 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 3423 htab_t types_htab = NULL;
8b70b953
TT
3424 int ix;
3425 struct dwarf2_section_info *section;
348e048f 3426
3019eac3
DE
3427 if (VEC_empty (dwarf2_section_info_def, types))
3428 return NULL;
348e048f 3429
8b70b953 3430 for (ix = 0;
3019eac3 3431 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
3432 ++ix)
3433 {
3019eac3 3434 bfd *abfd;
8b70b953 3435 gdb_byte *info_ptr, *end_ptr;
348e048f 3436
8b70b953
TT
3437 dwarf2_read_section (objfile, section);
3438 info_ptr = section->buffer;
348e048f 3439
8b70b953
TT
3440 if (info_ptr == NULL)
3441 continue;
348e048f 3442
3019eac3
DE
3443 /* We can't set abfd until now because the section may be empty or
3444 not present, in which case section->asection will be NULL. */
3445 abfd = section->asection->owner;
3446
8b70b953 3447 if (types_htab == NULL)
3019eac3
DE
3448 {
3449 if (dwo_file)
3450 types_htab = allocate_dwo_unit_table (objfile);
3451 else
3452 types_htab = allocate_signatured_type_table (objfile);
3453 }
348e048f 3454
8b70b953 3455 if (dwarf2_die_debug)
dee91e82
DE
3456 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3457 bfd_get_filename (abfd));
3458
3459 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3460 because we don't need to read any dies: the signature is in the
3461 header. */
8b70b953
TT
3462
3463 end_ptr = info_ptr + section->size;
3464 while (info_ptr < end_ptr)
3465 {
b64f50a1 3466 sect_offset offset;
3019eac3 3467 cu_offset type_offset_in_tu;
8b70b953 3468 ULONGEST signature;
52dc124a 3469 struct signatured_type *sig_type;
3019eac3 3470 struct dwo_unit *dwo_tu;
8b70b953
TT
3471 void **slot;
3472 gdb_byte *ptr = info_ptr;
9ff913ba 3473 struct comp_unit_head header;
dee91e82 3474 unsigned int length;
348e048f 3475
b64f50a1 3476 offset.sect_off = ptr - section->buffer;
348e048f 3477
8b70b953 3478 /* We need to read the type's signature in order to build the hash
9ff913ba 3479 table, but we don't need anything else just yet. */
348e048f 3480
9ff913ba 3481 ptr = read_and_check_type_unit_head (&header, section, ptr,
3019eac3 3482 &signature, &type_offset_in_tu);
6caca83c 3483
dee91e82
DE
3484 length = header.initial_length_size + header.length;
3485
6caca83c 3486 /* Skip dummy type units. */
dee91e82
DE
3487 if (ptr >= info_ptr + length
3488 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 3489 {
dee91e82 3490 info_ptr += header.initial_length_size + header.length;
6caca83c
CC
3491 continue;
3492 }
8b70b953 3493
3019eac3
DE
3494 if (dwo_file)
3495 {
3496 sig_type = NULL;
3497 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3498 struct dwo_unit);
3499 dwo_tu->dwo_file = dwo_file;
3500 dwo_tu->signature = signature;
3501 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3502 dwo_tu->info_or_types_section = section;
3503 dwo_tu->offset = offset;
3504 dwo_tu->length = length;
3505 }
3506 else
3507 {
3508 /* N.B.: type_offset is not usable if this type uses a DWO file.
3509 The real type_offset is in the DWO file. */
3510 dwo_tu = NULL;
3511 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3512 struct signatured_type);
3513 sig_type->signature = signature;
3514 sig_type->type_offset_in_tu = type_offset_in_tu;
3515 sig_type->per_cu.objfile = objfile;
3516 sig_type->per_cu.is_debug_types = 1;
3517 sig_type->per_cu.info_or_types_section = section;
3518 sig_type->per_cu.offset = offset;
3519 sig_type->per_cu.length = length;
3520 }
8b70b953 3521
3019eac3
DE
3522 slot = htab_find_slot (types_htab,
3523 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3524 INSERT);
8b70b953
TT
3525 gdb_assert (slot != NULL);
3526 if (*slot != NULL)
3527 {
3019eac3
DE
3528 sect_offset dup_offset;
3529
3530 if (dwo_file)
3531 {
3532 const struct dwo_unit *dup_tu = *slot;
3533
3534 dup_offset = dup_tu->offset;
3535 }
3536 else
3537 {
3538 const struct signatured_type *dup_tu = *slot;
3539
3540 dup_offset = dup_tu->per_cu.offset;
3541 }
b3c8eb43 3542
8b70b953
TT
3543 complaint (&symfile_complaints,
3544 _("debug type entry at offset 0x%x is duplicate to the "
3545 "entry at offset 0x%x, signature 0x%s"),
3019eac3 3546 offset.sect_off, dup_offset.sect_off,
8b70b953 3547 phex (signature, sizeof (signature)));
8b70b953 3548 }
3019eac3 3549 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 3550
8b70b953
TT
3551 if (dwarf2_die_debug)
3552 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
3553 offset.sect_off,
3554 phex (signature, sizeof (signature)));
348e048f 3555
dee91e82 3556 info_ptr += length;
8b70b953 3557 }
348e048f
DE
3558 }
3559
3019eac3
DE
3560 return types_htab;
3561}
3562
3563/* Create the hash table of all entries in the .debug_types section,
3564 and initialize all_type_units.
3565 The result is zero if there is an error (e.g. missing .debug_types section),
3566 otherwise non-zero. */
3567
3568static int
3569create_all_type_units (struct objfile *objfile)
3570{
3571 htab_t types_htab;
3572 struct dwarf2_per_cu_data **iter;
3573
3574 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3575 if (types_htab == NULL)
3576 {
3577 dwarf2_per_objfile->signatured_types = NULL;
3578 return 0;
3579 }
3580
348e048f
DE
3581 dwarf2_per_objfile->signatured_types = types_htab;
3582
d467dd73
DE
3583 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3584 dwarf2_per_objfile->all_type_units
1fd400ff 3585 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3586 dwarf2_per_objfile->n_type_units
1fd400ff 3587 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3588 iter = &dwarf2_per_objfile->all_type_units[0];
3589 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3590 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3591 == dwarf2_per_objfile->n_type_units);
1fd400ff 3592
348e048f
DE
3593 return 1;
3594}
3595
380bca97 3596/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 3597 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
3598
3599static struct signatured_type *
e319fa28 3600lookup_signatured_type (ULONGEST sig)
348e048f
DE
3601{
3602 struct signatured_type find_entry, *entry;
3603
3604 if (dwarf2_per_objfile->signatured_types == NULL)
3605 {
3606 complaint (&symfile_complaints,
55f1336d 3607 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 3608 return NULL;
348e048f
DE
3609 }
3610
3611 find_entry.signature = sig;
3612 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3613 return entry;
3614}
3615
d85a05f0
DJ
3616/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3617
3618static void
3619init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 3620 struct dwarf2_cu *cu,
3019eac3
DE
3621 struct dwarf2_section_info *section,
3622 struct dwo_file *dwo_file)
d85a05f0 3623{
fceca515 3624 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 3625 reader->abfd = section->asection->owner;
d85a05f0 3626 reader->cu = cu;
3019eac3 3627 reader->dwo_file = dwo_file;
dee91e82
DE
3628 reader->die_section = section;
3629 reader->buffer = section->buffer;
d85a05f0
DJ
3630}
3631
3632/* Find the base address of the compilation unit for range lists and
3633 location lists. It will normally be specified by DW_AT_low_pc.
3634 In DWARF-3 draft 4, the base address could be overridden by
3635 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3636 compilation units with discontinuous ranges. */
3637
3638static void
3639dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3640{
3641 struct attribute *attr;
3642
3643 cu->base_known = 0;
3644 cu->base_address = 0;
3645
3646 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3647 if (attr)
3648 {
3649 cu->base_address = DW_ADDR (attr);
3650 cu->base_known = 1;
3651 }
3652 else
3653 {
3654 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3655 if (attr)
3656 {
3657 cu->base_address = DW_ADDR (attr);
3658 cu->base_known = 1;
3659 }
3660 }
3661}
3662
fd820528 3663/* Initialize a CU (or TU) and read its DIEs.
3019eac3 3664 If the CU defers to a DWO file, read the DWO file as well.
dee91e82
DE
3665
3666 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3667 Otherwise, a new CU is allocated with xmalloc.
3668
3669 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3670 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3671
3672 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 3673 linker) then DIE_READER_FUNC will not get called. */
aaa75496 3674
70221824 3675static void
fd820528
DE
3676init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3677 int use_existing_cu, int keep,
3678 die_reader_func_ftype *die_reader_func,
3679 void *data)
c906108c 3680{
dee91e82 3681 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
3682 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3683 bfd *abfd = section->asection->owner;
dee91e82
DE
3684 struct dwarf2_cu *cu;
3685 gdb_byte *begin_info_ptr, *info_ptr;
3686 struct die_reader_specs reader;
d85a05f0 3687 struct die_info *comp_unit_die;
dee91e82 3688 int has_children;
d85a05f0 3689 struct attribute *attr;
dee91e82
DE
3690 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3691 struct signatured_type *sig_type = NULL;
c906108c 3692
dee91e82
DE
3693 if (use_existing_cu)
3694 gdb_assert (keep);
23745b47 3695
dee91e82
DE
3696 cleanups = make_cleanup (null_cleanup, NULL);
3697
3698 /* This is cheap if the section is already read in. */
3699 dwarf2_read_section (objfile, section);
3700
3701 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3702
3703 if (use_existing_cu && this_cu->cu != NULL)
3704 {
3705 cu = this_cu->cu;
3706 info_ptr += cu->header.first_die_offset.cu_off;
3707 }
3708 else
3709 {
3710 /* If !use_existing_cu, this_cu->cu must be NULL. */
3711 gdb_assert (this_cu->cu == NULL);
3712
3713 cu = xmalloc (sizeof (*cu));
3714 init_one_comp_unit (cu, this_cu);
3715
3716 /* If an error occurs while loading, release our storage. */
3717 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3718
3019eac3 3719 if (this_cu->is_debug_types)
dee91e82
DE
3720 {
3721 ULONGEST signature;
3722
3723 info_ptr = read_and_check_type_unit_head (&cu->header,
3724 section, info_ptr,
3725 &signature, NULL);
3726
3727 /* There's no way to get from PER_CU to its containing
3728 struct signatured_type.
3729 But we have the signature so we can use that. */
3730 sig_type = lookup_signatured_type (signature);
3731 /* We've already scanned all the signatured types,
3732 this must succeed. */
3733 gdb_assert (sig_type != NULL);
3734 gdb_assert (&sig_type->per_cu == this_cu);
3735 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3736
3737 /* LENGTH has not been set yet for type units. */
3738 this_cu->length = cu->header.length + cu->header.initial_length_size;
3019eac3
DE
3739
3740 /* Establish the type offset that can be used to lookup the type. */
3741 sig_type->type_offset_in_section.sect_off =
3742 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
3743 }
3744 else
3745 {
3746 info_ptr = read_and_check_comp_unit_head (&cu->header,
3747 section, info_ptr, 0);
3748
3749 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3750 gdb_assert (this_cu->length
3751 == cu->header.length + cu->header.initial_length_size);
3752 }
3753 }
10b3939b 3754
6caca83c 3755 /* Skip dummy compilation units. */
dee91e82 3756 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
3757 || peek_abbrev_code (abfd, info_ptr) == 0)
3758 {
dee91e82 3759 do_cleanups (cleanups);
21b2bd31 3760 return;
6caca83c
CC
3761 }
3762
93311388 3763 /* Read the abbrevs for this compilation unit into a table. */
dee91e82
DE
3764 if (cu->dwarf2_abbrevs == NULL)
3765 {
3766 dwarf2_read_abbrevs (cu, &dwarf2_per_objfile->abbrev);
3767 make_cleanup (dwarf2_free_abbrev_table, cu);
3768 }
af703f96 3769
dee91e82 3770 /* Read the top level CU/TU die. */
3019eac3 3771 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 3772 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 3773
3019eac3
DE
3774 /* If we have a DWO stub, process it and then read in the DWO file.
3775 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3776 a DWO CU, that this test will fail. */
3777 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3778 if (attr)
3779 {
3780 char *dwo_name = DW_STRING (attr);
3781 const char *comp_dir;
3782 struct dwo_unit *dwo_unit;
3783 ULONGEST signature; /* Or dwo_id. */
3784 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3785 int i,num_extra_attrs;
3786
3787 if (has_children)
3788 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3789 " has children (offset 0x%x) [in module %s]"),
3790 this_cu->offset.sect_off, bfd_get_filename (abfd));
3791
3792 /* These attributes aren't processed until later:
3793 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3794 However, the attribute is found in the stub which we won't have later.
3795 In order to not impose this complication on the rest of the code,
3796 we read them here and copy them to the DWO CU/TU die. */
3797 stmt_list = low_pc = high_pc = ranges = NULL;
3798
3799 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3800 DWO file. */
3801 if (! this_cu->is_debug_types)
3802 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3803 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3804 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3805 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3806
3807 /* There should be a DW_AT_addr_base attribute here (if needed).
3808 We need the value before we can process DW_FORM_GNU_addr_index. */
3809 cu->addr_base = 0;
3810 cu->have_addr_base = 0;
3811 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
3812 if (attr)
3813 {
3814 cu->addr_base = DW_UNSND (attr);
3815 cu->have_addr_base = 1;
3816 }
3817
3818 if (this_cu->is_debug_types)
3819 {
3820 gdb_assert (sig_type != NULL);
3821 signature = sig_type->signature;
3822 }
3823 else
3824 {
3825 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3826 if (! attr)
3827 error (_("Dwarf Error: missing dwo_id [in module %s]"),
3828 dwo_name);
3829 signature = DW_UNSND (attr);
3830 }
3831
3832 /* We may need the comp_dir in order to find the DWO file. */
3833 comp_dir = NULL;
3834 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3835 if (attr)
3836 comp_dir = DW_STRING (attr);
3837
3838 if (this_cu->is_debug_types)
3839 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
3840 else
3841 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
3842 signature);
3843
3844 if (dwo_unit == NULL)
3845 {
3846 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
3847 " with ID %s [in module %s]"),
3848 this_cu->offset.sect_off,
3849 phex (signature, sizeof (signature)),
3850 objfile->name);
3851 }
3852
3853 /* Set up for reading the DWO CU/TU. */
3854 cu->dwo_unit = dwo_unit;
3855 section = dwo_unit->info_or_types_section;
3856 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
3857 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
3858
3859 if (this_cu->is_debug_types)
3860 {
3861 ULONGEST signature;
3862
3863 info_ptr = read_and_check_type_unit_head (&cu->header,
3864 section, info_ptr,
3865 &signature, NULL);
3866 gdb_assert (sig_type->signature == signature);
3867 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3868 gdb_assert (dwo_unit->length
3869 == cu->header.length + cu->header.initial_length_size);
3870
3871 /* Establish the type offset that can be used to lookup the type.
3872 For DWO files, we don't know it until now. */
3873 sig_type->type_offset_in_section.sect_off =
3874 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
3875 }
3876 else
3877 {
3878 info_ptr = read_and_check_comp_unit_head (&cu->header,
3879 section, info_ptr, 0);
3880 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3881 gdb_assert (dwo_unit->length
3882 == cu->header.length + cu->header.initial_length_size);
3883 }
3884
3885 /* Discard the original CU's abbrev table, and read the DWO's. */
3886 dwarf2_free_abbrev_table (cu);
3887 dwarf2_read_abbrevs (cu, &dwo_unit->dwo_file->sections.abbrev);
3888
3889 /* Read in the die, but leave space to copy over the attributes
3890 from the stub. This has the benefit of simplifying the rest of
3891 the code - all the real work is done here. */
3892 num_extra_attrs = ((stmt_list != NULL)
3893 + (low_pc != NULL)
3894 + (high_pc != NULL)
3895 + (ranges != NULL));
3896 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
3897 &has_children, num_extra_attrs);
3898
3899 /* Copy over the attributes from the stub to the DWO die. */
3900 i = comp_unit_die->num_attrs;
3901 if (stmt_list != NULL)
3902 comp_unit_die->attrs[i++] = *stmt_list;
3903 if (low_pc != NULL)
3904 comp_unit_die->attrs[i++] = *low_pc;
3905 if (high_pc != NULL)
3906 comp_unit_die->attrs[i++] = *high_pc;
3907 if (ranges != NULL)
3908 comp_unit_die->attrs[i++] = *ranges;
3909 comp_unit_die->num_attrs += num_extra_attrs;
3910
3911 /* Skip dummy compilation units. */
3912 if (info_ptr >= begin_info_ptr + dwo_unit->length
3913 || peek_abbrev_code (abfd, info_ptr) == 0)
3914 {
3915 do_cleanups (cleanups);
3916 return;
3917 }
3918 }
3919
dee91e82
DE
3920 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3921
3922 if (free_cu_cleanup != NULL)
348e048f 3923 {
dee91e82
DE
3924 if (keep)
3925 {
3926 /* We've successfully allocated this compilation unit. Let our
3927 caller clean it up when finished with it. */
3928 discard_cleanups (free_cu_cleanup);
3929
3930 /* We can only discard free_cu_cleanup and all subsequent cleanups.
3931 So we have to manually free the abbrev table. */
3932 dwarf2_free_abbrev_table (cu);
3933
3934 /* Link this CU into read_in_chain. */
3935 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3936 dwarf2_per_objfile->read_in_chain = this_cu;
3937 }
3938 else
3939 do_cleanups (free_cu_cleanup);
348e048f 3940 }
dee91e82
DE
3941
3942 do_cleanups (cleanups);
3943}
3944
3019eac3
DE
3945/* Read CU/TU THIS_CU in section SECTION,
3946 but do not follow DW_AT_GNU_dwo_name if present.
3947 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
3948 have already done the lookup to find the DWO file).
dee91e82
DE
3949
3950 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 3951 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
3952
3953 We fill in THIS_CU->length.
3954
3955 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3956 linker) then DIE_READER_FUNC will not get called.
3957
3958 THIS_CU->cu is always freed when done.
3019eac3
DE
3959 This is done in order to not leave THIS_CU->cu in a state where we have
3960 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
3961
3962static void
3963init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3964 struct dwarf2_section_info *abbrev_section,
3019eac3 3965 struct dwo_file *dwo_file,
dee91e82
DE
3966 die_reader_func_ftype *die_reader_func,
3967 void *data)
3968{
3969 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
3970 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3971 bfd *abfd = section->asection->owner;
dee91e82
DE
3972 struct dwarf2_cu cu;
3973 gdb_byte *begin_info_ptr, *info_ptr;
3974 struct die_reader_specs reader;
3975 struct cleanup *cleanups;
3976 struct die_info *comp_unit_die;
3977 int has_children;
3978
3979 gdb_assert (this_cu->cu == NULL);
3980
dee91e82
DE
3981 /* This is cheap if the section is already read in. */
3982 dwarf2_read_section (objfile, section);
3983
3984 init_one_comp_unit (&cu, this_cu);
3985
3986 cleanups = make_cleanup (free_stack_comp_unit, &cu);
3987
3988 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3989 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3019eac3 3990 this_cu->is_debug_types);
dee91e82
DE
3991
3992 this_cu->length = cu.header.length + cu.header.initial_length_size;
3993
3994 /* Skip dummy compilation units. */
3995 if (info_ptr >= begin_info_ptr + this_cu->length
3996 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 3997 {
dee91e82 3998 do_cleanups (cleanups);
21b2bd31 3999 return;
93311388 4000 }
72bf9492 4001
dee91e82
DE
4002 dwarf2_read_abbrevs (&cu, abbrev_section);
4003 make_cleanup (dwarf2_free_abbrev_table, &cu);
4004
3019eac3 4005 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4006 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4007
4008 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4009
4010 do_cleanups (cleanups);
4011}
4012
3019eac3
DE
4013/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4014 does not lookup the specified DWO file.
4015 This cannot be used to read DWO files.
dee91e82
DE
4016
4017 THIS_CU->cu is always freed when done.
3019eac3
DE
4018 This is done in order to not leave THIS_CU->cu in a state where we have
4019 to care whether it refers to the "main" CU or the DWO CU.
4020 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4021
4022static void
4023init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4024 die_reader_func_ftype *die_reader_func,
4025 void *data)
4026{
4027 init_cutu_and_read_dies_no_follow (this_cu,
4028 &dwarf2_per_objfile->abbrev,
3019eac3 4029 NULL,
dee91e82
DE
4030 die_reader_func, data);
4031}
4032
4033/* die_reader_func for process_psymtab_comp_unit. */
4034
4035static void
4036process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4037 gdb_byte *info_ptr,
4038 struct die_info *comp_unit_die,
4039 int has_children,
4040 void *data)
4041{
4042 struct dwarf2_cu *cu = reader->cu;
4043 struct objfile *objfile = cu->objfile;
4044 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4045 bfd *abfd = objfile->obfd;
4046 struct attribute *attr;
4047 CORE_ADDR baseaddr;
4048 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4049 struct partial_symtab *pst;
4050 int has_pc_info;
4051 const char *filename;
95554aad 4052 int *want_partial_unit_ptr = data;
dee91e82 4053
95554aad
TT
4054 if (comp_unit_die->tag == DW_TAG_partial_unit
4055 && (want_partial_unit_ptr == NULL
4056 || !*want_partial_unit_ptr))
dee91e82
DE
4057 return;
4058
95554aad 4059 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4060
4061 cu->list_in_scope = &file_symbols;
c906108c 4062
93311388 4063 /* Allocate a new partial symbol table structure. */
dee91e82 4064 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4065 if (attr == NULL || !DW_STRING (attr))
4066 filename = "";
4067 else
4068 filename = DW_STRING (attr);
93311388 4069 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 4070 filename,
93311388
DE
4071 /* TEXTLOW and TEXTHIGH are set below. */
4072 0,
4073 objfile->global_psymbols.next,
4074 objfile->static_psymbols.next);
9750bca9 4075 pst->psymtabs_addrmap_supported = 1;
72bf9492 4076
dee91e82 4077 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4078 if (attr != NULL)
4079 pst->dirname = DW_STRING (attr);
72bf9492 4080
dee91e82 4081 pst->read_symtab_private = per_cu;
72bf9492 4082
93311388 4083 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4084
0963b4bd 4085 /* Store the function that reads in the rest of the symbol table. */
93311388 4086 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 4087
dee91e82 4088 per_cu->v.psymtab = pst;
c906108c 4089
dee91e82 4090 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4091
93311388
DE
4092 /* Possibly set the default values of LOWPC and HIGHPC from
4093 `DW_AT_ranges'. */
d85a05f0 4094 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4095 &best_highpc, cu, pst);
d85a05f0 4096 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4097 /* Store the contiguous range if it is not empty; it can be empty for
4098 CUs with no code. */
4099 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4100 best_lowpc + baseaddr,
4101 best_highpc + baseaddr - 1, pst);
93311388
DE
4102
4103 /* Check if comp unit has_children.
4104 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4105 If not, there's no more debug_info for this comp unit. */
d85a05f0 4106 if (has_children)
93311388
DE
4107 {
4108 struct partial_die_info *first_die;
4109 CORE_ADDR lowpc, highpc;
31ffec48 4110
93311388
DE
4111 lowpc = ((CORE_ADDR) -1);
4112 highpc = ((CORE_ADDR) 0);
c906108c 4113
dee91e82 4114 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4115
93311388 4116 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4117 ! has_pc_info, cu);
57c22c6c 4118
93311388
DE
4119 /* If we didn't find a lowpc, set it to highpc to avoid
4120 complaints from `maint check'. */
4121 if (lowpc == ((CORE_ADDR) -1))
4122 lowpc = highpc;
10b3939b 4123
93311388
DE
4124 /* If the compilation unit didn't have an explicit address range,
4125 then use the information extracted from its child dies. */
d85a05f0 4126 if (! has_pc_info)
93311388 4127 {
d85a05f0
DJ
4128 best_lowpc = lowpc;
4129 best_highpc = highpc;
93311388
DE
4130 }
4131 }
d85a05f0
DJ
4132 pst->textlow = best_lowpc + baseaddr;
4133 pst->texthigh = best_highpc + baseaddr;
c906108c 4134
93311388
DE
4135 pst->n_global_syms = objfile->global_psymbols.next -
4136 (objfile->global_psymbols.list + pst->globals_offset);
4137 pst->n_static_syms = objfile->static_psymbols.next -
4138 (objfile->static_psymbols.list + pst->statics_offset);
4139 sort_pst_symbols (pst);
c906108c 4140
95554aad
TT
4141 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4142 {
4143 int i;
4144 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4145 struct dwarf2_per_cu_data *iter;
4146
4147 /* Fill in 'dependencies' here; we fill in 'users' in a
4148 post-pass. */
4149 pst->number_of_dependencies = len;
4150 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4151 len * sizeof (struct symtab *));
4152 for (i = 0;
4153 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4154 i, iter);
4155 ++i)
4156 pst->dependencies[i] = iter->v.psymtab;
4157
4158 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4159 }
4160
3019eac3 4161 if (per_cu->is_debug_types)
348e048f
DE
4162 {
4163 /* It's not clear we want to do anything with stmt lists here.
4164 Waiting to see what gcc ultimately does. */
4165 }
d85a05f0 4166 else
93311388
DE
4167 {
4168 /* Get the list of files included in the current compilation unit,
4169 and build a psymtab for each of them. */
dee91e82 4170 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
93311388 4171 }
dee91e82 4172}
ae038cb0 4173
dee91e82
DE
4174/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4175 Process compilation unit THIS_CU for a psymtab. */
4176
4177static void
95554aad
TT
4178process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4179 int want_partial_unit)
dee91e82
DE
4180{
4181 /* If this compilation unit was already read in, free the
4182 cached copy in order to read it in again. This is
4183 necessary because we skipped some symbols when we first
4184 read in the compilation unit (see load_partial_dies).
4185 This problem could be avoided, but the benefit is unclear. */
4186 if (this_cu->cu != NULL)
4187 free_one_cached_comp_unit (this_cu);
4188
3019eac3 4189 gdb_assert (! this_cu->is_debug_types);
fd820528 4190 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
95554aad 4191 &want_partial_unit);
dee91e82
DE
4192
4193 /* Age out any secondary CUs. */
4194 age_cached_comp_units ();
93311388 4195}
ff013f42 4196
348e048f
DE
4197/* Traversal function for htab_traverse_noresize.
4198 Process one .debug_types comp-unit. */
4199
4200static int
dee91e82 4201process_psymtab_type_unit (void **slot, void *info)
348e048f 4202{
dee91e82
DE
4203 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4204 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 4205
fd820528 4206 gdb_assert (per_cu->is_debug_types);
a0f42c21 4207 gdb_assert (info == NULL);
348e048f 4208
dee91e82
DE
4209 /* If this compilation unit was already read in, free the
4210 cached copy in order to read it in again. This is
4211 necessary because we skipped some symbols when we first
4212 read in the compilation unit (see load_partial_dies).
4213 This problem could be avoided, but the benefit is unclear. */
4214 if (per_cu->cu != NULL)
4215 free_one_cached_comp_unit (per_cu);
4216
fd820528
DE
4217 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4218 NULL);
dee91e82
DE
4219
4220 /* Age out any secondary CUs. */
4221 age_cached_comp_units ();
348e048f
DE
4222
4223 return 1;
4224}
4225
4226/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4227 Build partial symbol tables for the .debug_types comp-units. */
4228
4229static void
4230build_type_psymtabs (struct objfile *objfile)
4231{
0e50663e 4232 if (! create_all_type_units (objfile))
348e048f
DE
4233 return;
4234
4235 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
dee91e82 4236 process_psymtab_type_unit, NULL);
348e048f
DE
4237}
4238
60606b2c
TT
4239/* A cleanup function that clears objfile's psymtabs_addrmap field. */
4240
4241static void
4242psymtabs_addrmap_cleanup (void *o)
4243{
4244 struct objfile *objfile = o;
ec61707d 4245
60606b2c
TT
4246 objfile->psymtabs_addrmap = NULL;
4247}
4248
95554aad
TT
4249/* Compute the 'user' field for each psymtab in OBJFILE. */
4250
4251static void
4252set_partial_user (struct objfile *objfile)
4253{
4254 int i;
4255
4256 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4257 {
4258 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4259 struct partial_symtab *pst = per_cu->v.psymtab;
4260 int j;
4261
4262 for (j = 0; j < pst->number_of_dependencies; ++j)
4263 {
4264 /* Set the 'user' field only if it is not already set. */
4265 if (pst->dependencies[j]->user == NULL)
4266 pst->dependencies[j]->user = pst;
4267 }
4268 }
4269}
4270
93311388
DE
4271/* Build the partial symbol table by doing a quick pass through the
4272 .debug_info and .debug_abbrev sections. */
72bf9492 4273
93311388 4274static void
c67a9c90 4275dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 4276{
60606b2c
TT
4277 struct cleanup *back_to, *addrmap_cleanup;
4278 struct obstack temp_obstack;
21b2bd31 4279 int i;
93311388 4280
98bfdba5
PA
4281 dwarf2_per_objfile->reading_partial_symbols = 1;
4282
be391dca 4283 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 4284
93311388
DE
4285 /* Any cached compilation units will be linked by the per-objfile
4286 read_in_chain. Make sure to free them when we're done. */
4287 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 4288
348e048f
DE
4289 build_type_psymtabs (objfile);
4290
93311388 4291 create_all_comp_units (objfile);
c906108c 4292
60606b2c
TT
4293 /* Create a temporary address map on a temporary obstack. We later
4294 copy this to the final obstack. */
4295 obstack_init (&temp_obstack);
4296 make_cleanup_obstack_free (&temp_obstack);
4297 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4298 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 4299
21b2bd31 4300 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 4301 {
21b2bd31 4302 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 4303
95554aad 4304 process_psymtab_comp_unit (per_cu, 0);
c906108c 4305 }
ff013f42 4306
95554aad
TT
4307 set_partial_user (objfile);
4308
ff013f42
JK
4309 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4310 &objfile->objfile_obstack);
60606b2c 4311 discard_cleanups (addrmap_cleanup);
ff013f42 4312
ae038cb0
DJ
4313 do_cleanups (back_to);
4314}
4315
3019eac3 4316/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
4317
4318static void
dee91e82
DE
4319load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4320 gdb_byte *info_ptr,
4321 struct die_info *comp_unit_die,
4322 int has_children,
4323 void *data)
ae038cb0 4324{
dee91e82 4325 struct dwarf2_cu *cu = reader->cu;
ae038cb0 4326
95554aad 4327 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 4328
ae038cb0
DJ
4329 /* Check if comp unit has_children.
4330 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4331 If not, there's no more debug_info for this comp unit. */
d85a05f0 4332 if (has_children)
dee91e82
DE
4333 load_partial_dies (reader, info_ptr, 0);
4334}
98bfdba5 4335
dee91e82
DE
4336/* Load the partial DIEs for a secondary CU into memory.
4337 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 4338
dee91e82
DE
4339static void
4340load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4341{
fd820528 4342 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
4343}
4344
9cdd5dbd
DE
4345/* Create a list of all compilation units in OBJFILE.
4346 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
4347
4348static void
4349create_all_comp_units (struct objfile *objfile)
4350{
4351 int n_allocated;
4352 int n_comp_units;
4353 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
4354 gdb_byte *info_ptr;
4355
4356 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4357 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4358
4359 n_comp_units = 0;
4360 n_allocated = 10;
4361 all_comp_units = xmalloc (n_allocated
4362 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 4363
3e43a32a
MS
4364 while (info_ptr < dwarf2_per_objfile->info.buffer
4365 + dwarf2_per_objfile->info.size)
ae038cb0 4366 {
c764a876 4367 unsigned int length, initial_length_size;
ae038cb0 4368 struct dwarf2_per_cu_data *this_cu;
b64f50a1 4369 sect_offset offset;
ae038cb0 4370
b64f50a1 4371 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4372
4373 /* Read just enough information to find out where the next
4374 compilation unit is. */
c764a876
DE
4375 length = read_initial_length (objfile->obfd, info_ptr,
4376 &initial_length_size);
ae038cb0
DJ
4377
4378 /* Save the compilation unit for later lookup. */
4379 this_cu = obstack_alloc (&objfile->objfile_obstack,
4380 sizeof (struct dwarf2_per_cu_data));
4381 memset (this_cu, 0, sizeof (*this_cu));
4382 this_cu->offset = offset;
c764a876 4383 this_cu->length = length + initial_length_size;
9291a0cd 4384 this_cu->objfile = objfile;
3019eac3 4385 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
ae038cb0
DJ
4386
4387 if (n_comp_units == n_allocated)
4388 {
4389 n_allocated *= 2;
4390 all_comp_units = xrealloc (all_comp_units,
4391 n_allocated
4392 * sizeof (struct dwarf2_per_cu_data *));
4393 }
4394 all_comp_units[n_comp_units++] = this_cu;
4395
4396 info_ptr = info_ptr + this_cu->length;
4397 }
4398
4399 dwarf2_per_objfile->all_comp_units
4400 = obstack_alloc (&objfile->objfile_obstack,
4401 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4402 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4403 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4404 xfree (all_comp_units);
4405 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
4406}
4407
5734ee8b
DJ
4408/* Process all loaded DIEs for compilation unit CU, starting at
4409 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4410 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4411 DW_AT_ranges). If NEED_PC is set, then this function will set
4412 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4413 and record the covered ranges in the addrmap. */
c906108c 4414
72bf9492
DJ
4415static void
4416scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 4417 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 4418{
72bf9492 4419 struct partial_die_info *pdi;
c906108c 4420
91c24f0a
DC
4421 /* Now, march along the PDI's, descending into ones which have
4422 interesting children but skipping the children of the other ones,
4423 until we reach the end of the compilation unit. */
c906108c 4424
72bf9492 4425 pdi = first_die;
91c24f0a 4426
72bf9492
DJ
4427 while (pdi != NULL)
4428 {
4429 fixup_partial_die (pdi, cu);
c906108c 4430
f55ee35c 4431 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
4432 children, so we need to look at them. Ditto for anonymous
4433 enums. */
933c6fe4 4434
72bf9492 4435 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
4436 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4437 || pdi->tag == DW_TAG_imported_unit)
c906108c 4438 {
72bf9492 4439 switch (pdi->tag)
c906108c
SS
4440 {
4441 case DW_TAG_subprogram:
5734ee8b 4442 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 4443 break;
72929c62 4444 case DW_TAG_constant:
c906108c
SS
4445 case DW_TAG_variable:
4446 case DW_TAG_typedef:
91c24f0a 4447 case DW_TAG_union_type:
72bf9492 4448 if (!pdi->is_declaration)
63d06c5c 4449 {
72bf9492 4450 add_partial_symbol (pdi, cu);
63d06c5c
DC
4451 }
4452 break;
c906108c 4453 case DW_TAG_class_type:
680b30c7 4454 case DW_TAG_interface_type:
c906108c 4455 case DW_TAG_structure_type:
72bf9492 4456 if (!pdi->is_declaration)
c906108c 4457 {
72bf9492 4458 add_partial_symbol (pdi, cu);
c906108c
SS
4459 }
4460 break;
91c24f0a 4461 case DW_TAG_enumeration_type:
72bf9492
DJ
4462 if (!pdi->is_declaration)
4463 add_partial_enumeration (pdi, cu);
c906108c
SS
4464 break;
4465 case DW_TAG_base_type:
a02abb62 4466 case DW_TAG_subrange_type:
c906108c 4467 /* File scope base type definitions are added to the partial
c5aa993b 4468 symbol table. */
72bf9492 4469 add_partial_symbol (pdi, cu);
c906108c 4470 break;
d9fa45fe 4471 case DW_TAG_namespace:
5734ee8b 4472 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 4473 break;
5d7cb8df
JK
4474 case DW_TAG_module:
4475 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4476 break;
95554aad
TT
4477 case DW_TAG_imported_unit:
4478 {
4479 struct dwarf2_per_cu_data *per_cu;
4480
4481 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4482 cu->objfile);
4483
4484 /* Go read the partial unit, if needed. */
4485 if (per_cu->v.psymtab == NULL)
4486 process_psymtab_comp_unit (per_cu, 1);
4487
4488 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4489 per_cu);
4490 }
4491 break;
c906108c
SS
4492 default:
4493 break;
4494 }
4495 }
4496
72bf9492
DJ
4497 /* If the die has a sibling, skip to the sibling. */
4498
4499 pdi = pdi->die_sibling;
4500 }
4501}
4502
4503/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 4504
72bf9492 4505 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
4506 name is concatenated with "::" and the partial DIE's name. For
4507 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
4508 Enumerators are an exception; they use the scope of their parent
4509 enumeration type, i.e. the name of the enumeration type is not
4510 prepended to the enumerator.
91c24f0a 4511
72bf9492
DJ
4512 There are two complexities. One is DW_AT_specification; in this
4513 case "parent" means the parent of the target of the specification,
4514 instead of the direct parent of the DIE. The other is compilers
4515 which do not emit DW_TAG_namespace; in this case we try to guess
4516 the fully qualified name of structure types from their members'
4517 linkage names. This must be done using the DIE's children rather
4518 than the children of any DW_AT_specification target. We only need
4519 to do this for structures at the top level, i.e. if the target of
4520 any DW_AT_specification (if any; otherwise the DIE itself) does not
4521 have a parent. */
4522
4523/* Compute the scope prefix associated with PDI's parent, in
4524 compilation unit CU. The result will be allocated on CU's
4525 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4526 field. NULL is returned if no prefix is necessary. */
4527static char *
4528partial_die_parent_scope (struct partial_die_info *pdi,
4529 struct dwarf2_cu *cu)
4530{
4531 char *grandparent_scope;
4532 struct partial_die_info *parent, *real_pdi;
91c24f0a 4533
72bf9492
DJ
4534 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4535 then this means the parent of the specification DIE. */
4536
4537 real_pdi = pdi;
72bf9492 4538 while (real_pdi->has_specification)
10b3939b 4539 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
4540
4541 parent = real_pdi->die_parent;
4542 if (parent == NULL)
4543 return NULL;
4544
4545 if (parent->scope_set)
4546 return parent->scope;
4547
4548 fixup_partial_die (parent, cu);
4549
10b3939b 4550 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 4551
acebe513
UW
4552 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4553 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4554 Work around this problem here. */
4555 if (cu->language == language_cplus
6e70227d 4556 && parent->tag == DW_TAG_namespace
acebe513
UW
4557 && strcmp (parent->name, "::") == 0
4558 && grandparent_scope == NULL)
4559 {
4560 parent->scope = NULL;
4561 parent->scope_set = 1;
4562 return NULL;
4563 }
4564
9c6c53f7
SA
4565 if (pdi->tag == DW_TAG_enumerator)
4566 /* Enumerators should not get the name of the enumeration as a prefix. */
4567 parent->scope = grandparent_scope;
4568 else if (parent->tag == DW_TAG_namespace
f55ee35c 4569 || parent->tag == DW_TAG_module
72bf9492
DJ
4570 || parent->tag == DW_TAG_structure_type
4571 || parent->tag == DW_TAG_class_type
680b30c7 4572 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
4573 || parent->tag == DW_TAG_union_type
4574 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
4575 {
4576 if (grandparent_scope == NULL)
4577 parent->scope = parent->name;
4578 else
3e43a32a
MS
4579 parent->scope = typename_concat (&cu->comp_unit_obstack,
4580 grandparent_scope,
f55ee35c 4581 parent->name, 0, cu);
72bf9492 4582 }
72bf9492
DJ
4583 else
4584 {
4585 /* FIXME drow/2004-04-01: What should we be doing with
4586 function-local names? For partial symbols, we should probably be
4587 ignoring them. */
4588 complaint (&symfile_complaints,
e2e0b3e5 4589 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 4590 parent->tag, pdi->offset.sect_off);
72bf9492 4591 parent->scope = grandparent_scope;
c906108c
SS
4592 }
4593
72bf9492
DJ
4594 parent->scope_set = 1;
4595 return parent->scope;
4596}
4597
4598/* Return the fully scoped name associated with PDI, from compilation unit
4599 CU. The result will be allocated with malloc. */
4568ecf9 4600
72bf9492
DJ
4601static char *
4602partial_die_full_name (struct partial_die_info *pdi,
4603 struct dwarf2_cu *cu)
4604{
4605 char *parent_scope;
4606
98bfdba5
PA
4607 /* If this is a template instantiation, we can not work out the
4608 template arguments from partial DIEs. So, unfortunately, we have
4609 to go through the full DIEs. At least any work we do building
4610 types here will be reused if full symbols are loaded later. */
4611 if (pdi->has_template_arguments)
4612 {
4613 fixup_partial_die (pdi, cu);
4614
4615 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4616 {
4617 struct die_info *die;
4618 struct attribute attr;
4619 struct dwarf2_cu *ref_cu = cu;
4620
b64f50a1 4621 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
4622 attr.name = 0;
4623 attr.form = DW_FORM_ref_addr;
4568ecf9 4624 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
4625 die = follow_die_ref (NULL, &attr, &ref_cu);
4626
4627 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4628 }
4629 }
4630
72bf9492
DJ
4631 parent_scope = partial_die_parent_scope (pdi, cu);
4632 if (parent_scope == NULL)
4633 return NULL;
4634 else
f55ee35c 4635 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4636}
4637
4638static void
72bf9492 4639add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4640{
e7c27a73 4641 struct objfile *objfile = cu->objfile;
c906108c 4642 CORE_ADDR addr = 0;
decbce07 4643 char *actual_name = NULL;
e142c38c 4644 CORE_ADDR baseaddr;
72bf9492 4645 int built_actual_name = 0;
e142c38c
DJ
4646
4647 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4648
94af9270
KS
4649 actual_name = partial_die_full_name (pdi, cu);
4650 if (actual_name)
4651 built_actual_name = 1;
63d06c5c 4652
72bf9492
DJ
4653 if (actual_name == NULL)
4654 actual_name = pdi->name;
4655
c906108c
SS
4656 switch (pdi->tag)
4657 {
4658 case DW_TAG_subprogram:
2cfa0c8d 4659 if (pdi->is_external || cu->language == language_ada)
c906108c 4660 {
2cfa0c8d
JB
4661 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4662 of the global scope. But in Ada, we want to be able to access
4663 nested procedures globally. So all Ada subprograms are stored
4664 in the global scope. */
f47fb265 4665 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4666 mst_text, objfile); */
f47fb265
MS
4667 add_psymbol_to_list (actual_name, strlen (actual_name),
4668 built_actual_name,
4669 VAR_DOMAIN, LOC_BLOCK,
4670 &objfile->global_psymbols,
4671 0, pdi->lowpc + baseaddr,
4672 cu->language, objfile);
c906108c
SS
4673 }
4674 else
4675 {
f47fb265 4676 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4677 mst_file_text, objfile); */
f47fb265
MS
4678 add_psymbol_to_list (actual_name, strlen (actual_name),
4679 built_actual_name,
4680 VAR_DOMAIN, LOC_BLOCK,
4681 &objfile->static_psymbols,
4682 0, pdi->lowpc + baseaddr,
4683 cu->language, objfile);
c906108c
SS
4684 }
4685 break;
72929c62
JB
4686 case DW_TAG_constant:
4687 {
4688 struct psymbol_allocation_list *list;
4689
4690 if (pdi->is_external)
4691 list = &objfile->global_psymbols;
4692 else
4693 list = &objfile->static_psymbols;
f47fb265
MS
4694 add_psymbol_to_list (actual_name, strlen (actual_name),
4695 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4696 list, 0, 0, cu->language, objfile);
72929c62
JB
4697 }
4698 break;
c906108c 4699 case DW_TAG_variable:
95554aad
TT
4700 if (pdi->d.locdesc)
4701 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 4702
95554aad 4703 if (pdi->d.locdesc
caac4577
JG
4704 && addr == 0
4705 && !dwarf2_per_objfile->has_section_at_zero)
4706 {
4707 /* A global or static variable may also have been stripped
4708 out by the linker if unused, in which case its address
4709 will be nullified; do not add such variables into partial
4710 symbol table then. */
4711 }
4712 else if (pdi->is_external)
c906108c
SS
4713 {
4714 /* Global Variable.
4715 Don't enter into the minimal symbol tables as there is
4716 a minimal symbol table entry from the ELF symbols already.
4717 Enter into partial symbol table if it has a location
4718 descriptor or a type.
4719 If the location descriptor is missing, new_symbol will create
4720 a LOC_UNRESOLVED symbol, the address of the variable will then
4721 be determined from the minimal symbol table whenever the variable
4722 is referenced.
4723 The address for the partial symbol table entry is not
4724 used by GDB, but it comes in handy for debugging partial symbol
4725 table building. */
4726
95554aad 4727 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
4728 add_psymbol_to_list (actual_name, strlen (actual_name),
4729 built_actual_name,
4730 VAR_DOMAIN, LOC_STATIC,
4731 &objfile->global_psymbols,
4732 0, addr + baseaddr,
4733 cu->language, objfile);
c906108c
SS
4734 }
4735 else
4736 {
0963b4bd 4737 /* Static Variable. Skip symbols without location descriptors. */
95554aad 4738 if (pdi->d.locdesc == NULL)
decbce07
MS
4739 {
4740 if (built_actual_name)
4741 xfree (actual_name);
4742 return;
4743 }
f47fb265 4744 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4745 mst_file_data, objfile); */
f47fb265
MS
4746 add_psymbol_to_list (actual_name, strlen (actual_name),
4747 built_actual_name,
4748 VAR_DOMAIN, LOC_STATIC,
4749 &objfile->static_psymbols,
4750 0, addr + baseaddr,
4751 cu->language, objfile);
c906108c
SS
4752 }
4753 break;
4754 case DW_TAG_typedef:
4755 case DW_TAG_base_type:
a02abb62 4756 case DW_TAG_subrange_type:
38d518c9 4757 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4758 built_actual_name,
176620f1 4759 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4760 &objfile->static_psymbols,
e142c38c 4761 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4762 break;
72bf9492
DJ
4763 case DW_TAG_namespace:
4764 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4765 built_actual_name,
72bf9492
DJ
4766 VAR_DOMAIN, LOC_TYPEDEF,
4767 &objfile->global_psymbols,
4768 0, (CORE_ADDR) 0, cu->language, objfile);
4769 break;
c906108c 4770 case DW_TAG_class_type:
680b30c7 4771 case DW_TAG_interface_type:
c906108c
SS
4772 case DW_TAG_structure_type:
4773 case DW_TAG_union_type:
4774 case DW_TAG_enumeration_type:
fa4028e9
JB
4775 /* Skip external references. The DWARF standard says in the section
4776 about "Structure, Union, and Class Type Entries": "An incomplete
4777 structure, union or class type is represented by a structure,
4778 union or class entry that does not have a byte size attribute
4779 and that has a DW_AT_declaration attribute." */
4780 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4781 {
4782 if (built_actual_name)
4783 xfree (actual_name);
4784 return;
4785 }
fa4028e9 4786
63d06c5c
DC
4787 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4788 static vs. global. */
38d518c9 4789 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4790 built_actual_name,
176620f1 4791 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4792 (cu->language == language_cplus
4793 || cu->language == language_java)
63d06c5c
DC
4794 ? &objfile->global_psymbols
4795 : &objfile->static_psymbols,
e142c38c 4796 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4797
c906108c
SS
4798 break;
4799 case DW_TAG_enumerator:
38d518c9 4800 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4801 built_actual_name,
176620f1 4802 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4803 (cu->language == language_cplus
4804 || cu->language == language_java)
f6fe98ef
DJ
4805 ? &objfile->global_psymbols
4806 : &objfile->static_psymbols,
e142c38c 4807 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4808 break;
4809 default:
4810 break;
4811 }
5c4e30ca 4812
72bf9492
DJ
4813 if (built_actual_name)
4814 xfree (actual_name);
c906108c
SS
4815}
4816
5c4e30ca
DC
4817/* Read a partial die corresponding to a namespace; also, add a symbol
4818 corresponding to that namespace to the symbol table. NAMESPACE is
4819 the name of the enclosing namespace. */
91c24f0a 4820
72bf9492
DJ
4821static void
4822add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4823 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4824 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4825{
72bf9492 4826 /* Add a symbol for the namespace. */
e7c27a73 4827
72bf9492 4828 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4829
4830 /* Now scan partial symbols in that namespace. */
4831
91c24f0a 4832 if (pdi->has_children)
5734ee8b 4833 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4834}
4835
5d7cb8df
JK
4836/* Read a partial die corresponding to a Fortran module. */
4837
4838static void
4839add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4840 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4841{
f55ee35c 4842 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4843
4844 if (pdi->has_children)
4845 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4846}
4847
bc30ff58
JB
4848/* Read a partial die corresponding to a subprogram and create a partial
4849 symbol for that subprogram. When the CU language allows it, this
4850 routine also defines a partial symbol for each nested subprogram
4851 that this subprogram contains.
6e70227d 4852
bc30ff58
JB
4853 DIE my also be a lexical block, in which case we simply search
4854 recursively for suprograms defined inside that lexical block.
4855 Again, this is only performed when the CU language allows this
4856 type of definitions. */
4857
4858static void
4859add_partial_subprogram (struct partial_die_info *pdi,
4860 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4861 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4862{
4863 if (pdi->tag == DW_TAG_subprogram)
4864 {
4865 if (pdi->has_pc_info)
4866 {
4867 if (pdi->lowpc < *lowpc)
4868 *lowpc = pdi->lowpc;
4869 if (pdi->highpc > *highpc)
4870 *highpc = pdi->highpc;
5734ee8b
DJ
4871 if (need_pc)
4872 {
4873 CORE_ADDR baseaddr;
4874 struct objfile *objfile = cu->objfile;
4875
4876 baseaddr = ANOFFSET (objfile->section_offsets,
4877 SECT_OFF_TEXT (objfile));
4878 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4879 pdi->lowpc + baseaddr,
4880 pdi->highpc - 1 + baseaddr,
9291a0cd 4881 cu->per_cu->v.psymtab);
5734ee8b 4882 }
481860b3
GB
4883 }
4884
4885 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4886 {
bc30ff58 4887 if (!pdi->is_declaration)
e8d05480
JB
4888 /* Ignore subprogram DIEs that do not have a name, they are
4889 illegal. Do not emit a complaint at this point, we will
4890 do so when we convert this psymtab into a symtab. */
4891 if (pdi->name)
4892 add_partial_symbol (pdi, cu);
bc30ff58
JB
4893 }
4894 }
6e70227d 4895
bc30ff58
JB
4896 if (! pdi->has_children)
4897 return;
4898
4899 if (cu->language == language_ada)
4900 {
4901 pdi = pdi->die_child;
4902 while (pdi != NULL)
4903 {
4904 fixup_partial_die (pdi, cu);
4905 if (pdi->tag == DW_TAG_subprogram
4906 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4907 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4908 pdi = pdi->die_sibling;
4909 }
4910 }
4911}
4912
91c24f0a
DC
4913/* Read a partial die corresponding to an enumeration type. */
4914
72bf9492
DJ
4915static void
4916add_partial_enumeration (struct partial_die_info *enum_pdi,
4917 struct dwarf2_cu *cu)
91c24f0a 4918{
72bf9492 4919 struct partial_die_info *pdi;
91c24f0a
DC
4920
4921 if (enum_pdi->name != NULL)
72bf9492
DJ
4922 add_partial_symbol (enum_pdi, cu);
4923
4924 pdi = enum_pdi->die_child;
4925 while (pdi)
91c24f0a 4926 {
72bf9492 4927 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4928 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4929 else
72bf9492
DJ
4930 add_partial_symbol (pdi, cu);
4931 pdi = pdi->die_sibling;
91c24f0a 4932 }
91c24f0a
DC
4933}
4934
6caca83c
CC
4935/* Return the initial uleb128 in the die at INFO_PTR. */
4936
4937static unsigned int
4938peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4939{
4940 unsigned int bytes_read;
4941
4942 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4943}
4944
4bb7a0a7
DJ
4945/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4946 Return the corresponding abbrev, or NULL if the number is zero (indicating
4947 an empty DIE). In either case *BYTES_READ will be set to the length of
4948 the initial number. */
4949
4950static struct abbrev_info *
fe1b8b76 4951peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4952 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4953{
4954 bfd *abfd = cu->objfile->obfd;
4955 unsigned int abbrev_number;
4956 struct abbrev_info *abbrev;
4957
4958 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4959
4960 if (abbrev_number == 0)
4961 return NULL;
4962
4963 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4964 if (!abbrev)
4965 {
3e43a32a
MS
4966 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4967 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4968 }
4969
4970 return abbrev;
4971}
4972
93311388
DE
4973/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4974 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4975 DIE. Any children of the skipped DIEs will also be skipped. */
4976
fe1b8b76 4977static gdb_byte *
dee91e82 4978skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 4979{
dee91e82 4980 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
4981 struct abbrev_info *abbrev;
4982 unsigned int bytes_read;
4983
4984 while (1)
4985 {
4986 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4987 if (abbrev == NULL)
4988 return info_ptr + bytes_read;
4989 else
dee91e82 4990 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
4991 }
4992}
4993
93311388
DE
4994/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4995 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4996 abbrev corresponding to that skipped uleb128 should be passed in
4997 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4998 children. */
4999
fe1b8b76 5000static gdb_byte *
dee91e82
DE
5001skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5002 struct abbrev_info *abbrev)
4bb7a0a7
DJ
5003{
5004 unsigned int bytes_read;
5005 struct attribute attr;
dee91e82
DE
5006 bfd *abfd = reader->abfd;
5007 struct dwarf2_cu *cu = reader->cu;
5008 gdb_byte *buffer = reader->buffer;
4bb7a0a7
DJ
5009 unsigned int form, i;
5010
5011 for (i = 0; i < abbrev->num_attrs; i++)
5012 {
5013 /* The only abbrev we care about is DW_AT_sibling. */
5014 if (abbrev->attrs[i].name == DW_AT_sibling)
5015 {
dee91e82 5016 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 5017 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
5018 complaint (&symfile_complaints,
5019 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 5020 else
b64f50a1 5021 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
5022 }
5023
5024 /* If it isn't DW_AT_sibling, skip this attribute. */
5025 form = abbrev->attrs[i].form;
5026 skip_attribute:
5027 switch (form)
5028 {
4bb7a0a7 5029 case DW_FORM_ref_addr:
ae411497
TT
5030 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5031 and later it is offset sized. */
5032 if (cu->header.version == 2)
5033 info_ptr += cu->header.addr_size;
5034 else
5035 info_ptr += cu->header.offset_size;
5036 break;
5037 case DW_FORM_addr:
4bb7a0a7
DJ
5038 info_ptr += cu->header.addr_size;
5039 break;
5040 case DW_FORM_data1:
5041 case DW_FORM_ref1:
5042 case DW_FORM_flag:
5043 info_ptr += 1;
5044 break;
2dc7f7b3
TT
5045 case DW_FORM_flag_present:
5046 break;
4bb7a0a7
DJ
5047 case DW_FORM_data2:
5048 case DW_FORM_ref2:
5049 info_ptr += 2;
5050 break;
5051 case DW_FORM_data4:
5052 case DW_FORM_ref4:
5053 info_ptr += 4;
5054 break;
5055 case DW_FORM_data8:
5056 case DW_FORM_ref8:
55f1336d 5057 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
5058 info_ptr += 8;
5059 break;
5060 case DW_FORM_string:
9b1c24c8 5061 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
5062 info_ptr += bytes_read;
5063 break;
2dc7f7b3 5064 case DW_FORM_sec_offset:
4bb7a0a7
DJ
5065 case DW_FORM_strp:
5066 info_ptr += cu->header.offset_size;
5067 break;
2dc7f7b3 5068 case DW_FORM_exprloc:
4bb7a0a7
DJ
5069 case DW_FORM_block:
5070 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5071 info_ptr += bytes_read;
5072 break;
5073 case DW_FORM_block1:
5074 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5075 break;
5076 case DW_FORM_block2:
5077 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5078 break;
5079 case DW_FORM_block4:
5080 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5081 break;
5082 case DW_FORM_sdata:
5083 case DW_FORM_udata:
5084 case DW_FORM_ref_udata:
3019eac3
DE
5085 case DW_FORM_GNU_addr_index:
5086 case DW_FORM_GNU_str_index:
4bb7a0a7
DJ
5087 info_ptr = skip_leb128 (abfd, info_ptr);
5088 break;
5089 case DW_FORM_indirect:
5090 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5091 info_ptr += bytes_read;
5092 /* We need to continue parsing from here, so just go back to
5093 the top. */
5094 goto skip_attribute;
5095
5096 default:
3e43a32a
MS
5097 error (_("Dwarf Error: Cannot handle %s "
5098 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
5099 dwarf_form_name (form),
5100 bfd_get_filename (abfd));
5101 }
5102 }
5103
5104 if (abbrev->has_children)
dee91e82 5105 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
5106 else
5107 return info_ptr;
5108}
5109
93311388 5110/* Locate ORIG_PDI's sibling.
dee91e82 5111 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 5112
fe1b8b76 5113static gdb_byte *
dee91e82
DE
5114locate_pdi_sibling (const struct die_reader_specs *reader,
5115 struct partial_die_info *orig_pdi,
5116 gdb_byte *info_ptr)
91c24f0a
DC
5117{
5118 /* Do we know the sibling already? */
72bf9492 5119
91c24f0a
DC
5120 if (orig_pdi->sibling)
5121 return orig_pdi->sibling;
5122
5123 /* Are there any children to deal with? */
5124
5125 if (!orig_pdi->has_children)
5126 return info_ptr;
5127
4bb7a0a7 5128 /* Skip the children the long way. */
91c24f0a 5129
dee91e82 5130 return skip_children (reader, info_ptr);
91c24f0a
DC
5131}
5132
c906108c
SS
5133/* Expand this partial symbol table into a full symbol table. */
5134
5135static void
fba45db2 5136dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 5137{
c906108c
SS
5138 if (pst != NULL)
5139 {
5140 if (pst->readin)
5141 {
3e43a32a
MS
5142 warning (_("bug: psymtab for %s is already read in."),
5143 pst->filename);
c906108c
SS
5144 }
5145 else
5146 {
5147 if (info_verbose)
5148 {
3e43a32a
MS
5149 printf_filtered (_("Reading in symbols for %s..."),
5150 pst->filename);
c906108c
SS
5151 gdb_flush (gdb_stdout);
5152 }
5153
10b3939b
DJ
5154 /* Restore our global data. */
5155 dwarf2_per_objfile = objfile_data (pst->objfile,
5156 dwarf2_objfile_data_key);
5157
b2ab525c
KB
5158 /* If this psymtab is constructed from a debug-only objfile, the
5159 has_section_at_zero flag will not necessarily be correct. We
5160 can get the correct value for this flag by looking at the data
5161 associated with the (presumably stripped) associated objfile. */
5162 if (pst->objfile->separate_debug_objfile_backlink)
5163 {
5164 struct dwarf2_per_objfile *dpo_backlink
5165 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5166 dwarf2_objfile_data_key);
9a619af0 5167
b2ab525c
KB
5168 dwarf2_per_objfile->has_section_at_zero
5169 = dpo_backlink->has_section_at_zero;
5170 }
5171
98bfdba5
PA
5172 dwarf2_per_objfile->reading_partial_symbols = 0;
5173
c906108c
SS
5174 psymtab_to_symtab_1 (pst);
5175
5176 /* Finish up the debug error message. */
5177 if (info_verbose)
a3f17187 5178 printf_filtered (_("done.\n"));
c906108c
SS
5179 }
5180 }
95554aad
TT
5181
5182 process_cu_includes ();
c906108c 5183}
9cdd5dbd
DE
5184\f
5185/* Reading in full CUs. */
c906108c 5186
10b3939b
DJ
5187/* Add PER_CU to the queue. */
5188
5189static void
95554aad
TT
5190queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5191 enum language pretend_language)
10b3939b
DJ
5192{
5193 struct dwarf2_queue_item *item;
5194
5195 per_cu->queued = 1;
5196 item = xmalloc (sizeof (*item));
5197 item->per_cu = per_cu;
95554aad 5198 item->pretend_language = pretend_language;
10b3939b
DJ
5199 item->next = NULL;
5200
5201 if (dwarf2_queue == NULL)
5202 dwarf2_queue = item;
5203 else
5204 dwarf2_queue_tail->next = item;
5205
5206 dwarf2_queue_tail = item;
5207}
5208
5209/* Process the queue. */
5210
5211static void
a0f42c21 5212process_queue (void)
10b3939b
DJ
5213{
5214 struct dwarf2_queue_item *item, *next_item;
5215
03dd20cc
DJ
5216 /* The queue starts out with one item, but following a DIE reference
5217 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
5218 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5219 {
9291a0cd
TT
5220 if (dwarf2_per_objfile->using_index
5221 ? !item->per_cu->v.quick->symtab
5222 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
95554aad 5223 process_full_comp_unit (item->per_cu, item->pretend_language);
10b3939b
DJ
5224
5225 item->per_cu->queued = 0;
5226 next_item = item->next;
5227 xfree (item);
5228 }
5229
5230 dwarf2_queue_tail = NULL;
5231}
5232
5233/* Free all allocated queue entries. This function only releases anything if
5234 an error was thrown; if the queue was processed then it would have been
5235 freed as we went along. */
5236
5237static void
5238dwarf2_release_queue (void *dummy)
5239{
5240 struct dwarf2_queue_item *item, *last;
5241
5242 item = dwarf2_queue;
5243 while (item)
5244 {
5245 /* Anything still marked queued is likely to be in an
5246 inconsistent state, so discard it. */
5247 if (item->per_cu->queued)
5248 {
5249 if (item->per_cu->cu != NULL)
dee91e82 5250 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
5251 item->per_cu->queued = 0;
5252 }
5253
5254 last = item;
5255 item = item->next;
5256 xfree (last);
5257 }
5258
5259 dwarf2_queue = dwarf2_queue_tail = NULL;
5260}
5261
5262/* Read in full symbols for PST, and anything it depends on. */
5263
c906108c 5264static void
fba45db2 5265psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 5266{
10b3939b 5267 struct dwarf2_per_cu_data *per_cu;
c906108c 5268 struct cleanup *back_to;
aaa75496
JB
5269 int i;
5270
95554aad
TT
5271 if (pst->readin)
5272 return;
5273
aaa75496 5274 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
5275 if (!pst->dependencies[i]->readin
5276 && pst->dependencies[i]->user == NULL)
aaa75496
JB
5277 {
5278 /* Inform about additional files that need to be read in. */
5279 if (info_verbose)
5280 {
a3f17187 5281 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
5282 fputs_filtered (" ", gdb_stdout);
5283 wrap_here ("");
5284 fputs_filtered ("and ", gdb_stdout);
5285 wrap_here ("");
5286 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 5287 wrap_here (""); /* Flush output. */
aaa75496
JB
5288 gdb_flush (gdb_stdout);
5289 }
5290 psymtab_to_symtab_1 (pst->dependencies[i]);
5291 }
5292
e38df1d0 5293 per_cu = pst->read_symtab_private;
10b3939b
DJ
5294
5295 if (per_cu == NULL)
aaa75496
JB
5296 {
5297 /* It's an include file, no symbols to read for it.
5298 Everything is in the parent symtab. */
5299 pst->readin = 1;
5300 return;
5301 }
c906108c 5302
a0f42c21 5303 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
5304}
5305
dee91e82
DE
5306/* Trivial hash function for die_info: the hash value of a DIE
5307 is its offset in .debug_info for this objfile. */
10b3939b 5308
dee91e82
DE
5309static hashval_t
5310die_hash (const void *item)
10b3939b 5311{
dee91e82 5312 const struct die_info *die = item;
6502dd73 5313
dee91e82
DE
5314 return die->offset.sect_off;
5315}
63d06c5c 5316
dee91e82
DE
5317/* Trivial comparison function for die_info structures: two DIEs
5318 are equal if they have the same offset. */
98bfdba5 5319
dee91e82
DE
5320static int
5321die_eq (const void *item_lhs, const void *item_rhs)
5322{
5323 const struct die_info *die_lhs = item_lhs;
5324 const struct die_info *die_rhs = item_rhs;
c906108c 5325
dee91e82
DE
5326 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5327}
c906108c 5328
dee91e82
DE
5329/* die_reader_func for load_full_comp_unit.
5330 This is identical to read_signatured_type_reader,
5331 but is kept separate for now. */
c906108c 5332
dee91e82
DE
5333static void
5334load_full_comp_unit_reader (const struct die_reader_specs *reader,
5335 gdb_byte *info_ptr,
5336 struct die_info *comp_unit_die,
5337 int has_children,
5338 void *data)
5339{
5340 struct dwarf2_cu *cu = reader->cu;
5341 struct attribute *attr;
95554aad 5342 enum language *language_ptr = data;
6caca83c 5343
dee91e82
DE
5344 gdb_assert (cu->die_hash == NULL);
5345 cu->die_hash =
5346 htab_create_alloc_ex (cu->header.length / 12,
5347 die_hash,
5348 die_eq,
5349 NULL,
5350 &cu->comp_unit_obstack,
5351 hashtab_obstack_allocate,
5352 dummy_obstack_deallocate);
e142c38c 5353
dee91e82
DE
5354 if (has_children)
5355 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5356 &info_ptr, comp_unit_die);
5357 cu->dies = comp_unit_die;
5358 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
5359
5360 /* We try not to read any attributes in this function, because not
9cdd5dbd 5361 all CUs needed for references have been loaded yet, and symbol
10b3939b 5362 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
5363 or we won't be able to build types correctly.
5364 Similarly, if we do not read the producer, we can not apply
5365 producer-specific interpretation. */
95554aad 5366 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 5367}
10b3939b 5368
dee91e82 5369/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 5370
dee91e82 5371static void
95554aad
TT
5372load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5373 enum language pretend_language)
dee91e82 5374{
3019eac3 5375 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 5376
95554aad
TT
5377 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5378 &pretend_language);
10b3939b
DJ
5379}
5380
3da10d80
KS
5381/* Add a DIE to the delayed physname list. */
5382
5383static void
5384add_to_method_list (struct type *type, int fnfield_index, int index,
5385 const char *name, struct die_info *die,
5386 struct dwarf2_cu *cu)
5387{
5388 struct delayed_method_info mi;
5389 mi.type = type;
5390 mi.fnfield_index = fnfield_index;
5391 mi.index = index;
5392 mi.name = name;
5393 mi.die = die;
5394 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5395}
5396
5397/* A cleanup for freeing the delayed method list. */
5398
5399static void
5400free_delayed_list (void *ptr)
5401{
5402 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5403 if (cu->method_list != NULL)
5404 {
5405 VEC_free (delayed_method_info, cu->method_list);
5406 cu->method_list = NULL;
5407 }
5408}
5409
5410/* Compute the physnames of any methods on the CU's method list.
5411
5412 The computation of method physnames is delayed in order to avoid the
5413 (bad) condition that one of the method's formal parameters is of an as yet
5414 incomplete type. */
5415
5416static void
5417compute_delayed_physnames (struct dwarf2_cu *cu)
5418{
5419 int i;
5420 struct delayed_method_info *mi;
5421 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5422 {
1d06ead6 5423 const char *physname;
3da10d80
KS
5424 struct fn_fieldlist *fn_flp
5425 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 5426 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
5427 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5428 }
5429}
5430
a766d390
DE
5431/* Go objects should be embedded in a DW_TAG_module DIE,
5432 and it's not clear if/how imported objects will appear.
5433 To keep Go support simple until that's worked out,
5434 go back through what we've read and create something usable.
5435 We could do this while processing each DIE, and feels kinda cleaner,
5436 but that way is more invasive.
5437 This is to, for example, allow the user to type "p var" or "b main"
5438 without having to specify the package name, and allow lookups
5439 of module.object to work in contexts that use the expression
5440 parser. */
5441
5442static void
5443fixup_go_packaging (struct dwarf2_cu *cu)
5444{
5445 char *package_name = NULL;
5446 struct pending *list;
5447 int i;
5448
5449 for (list = global_symbols; list != NULL; list = list->next)
5450 {
5451 for (i = 0; i < list->nsyms; ++i)
5452 {
5453 struct symbol *sym = list->symbol[i];
5454
5455 if (SYMBOL_LANGUAGE (sym) == language_go
5456 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5457 {
5458 char *this_package_name = go_symbol_package_name (sym);
5459
5460 if (this_package_name == NULL)
5461 continue;
5462 if (package_name == NULL)
5463 package_name = this_package_name;
5464 else
5465 {
5466 if (strcmp (package_name, this_package_name) != 0)
5467 complaint (&symfile_complaints,
5468 _("Symtab %s has objects from two different Go packages: %s and %s"),
5469 (sym->symtab && sym->symtab->filename
5470 ? sym->symtab->filename
5471 : cu->objfile->name),
5472 this_package_name, package_name);
5473 xfree (this_package_name);
5474 }
5475 }
5476 }
5477 }
5478
5479 if (package_name != NULL)
5480 {
5481 struct objfile *objfile = cu->objfile;
5482 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5483 package_name, objfile);
5484 struct symbol *sym;
5485
5486 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5487
5488 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5489 SYMBOL_SET_LANGUAGE (sym, language_go);
5490 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5491 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5492 e.g., "main" finds the "main" module and not C's main(). */
5493 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5494 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5495 SYMBOL_TYPE (sym) = type;
5496
5497 add_symbol_to_list (sym, &global_symbols);
5498
5499 xfree (package_name);
5500 }
5501}
5502
95554aad
TT
5503static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5504
5505/* Return the symtab for PER_CU. This works properly regardless of
5506 whether we're using the index or psymtabs. */
5507
5508static struct symtab *
5509get_symtab (struct dwarf2_per_cu_data *per_cu)
5510{
5511 return (dwarf2_per_objfile->using_index
5512 ? per_cu->v.quick->symtab
5513 : per_cu->v.psymtab->symtab);
5514}
5515
5516/* A helper function for computing the list of all symbol tables
5517 included by PER_CU. */
5518
5519static void
5520recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5521 htab_t all_children,
5522 struct dwarf2_per_cu_data *per_cu)
5523{
5524 void **slot;
5525 int ix;
5526 struct dwarf2_per_cu_data *iter;
5527
5528 slot = htab_find_slot (all_children, per_cu, INSERT);
5529 if (*slot != NULL)
5530 {
5531 /* This inclusion and its children have been processed. */
5532 return;
5533 }
5534
5535 *slot = per_cu;
5536 /* Only add a CU if it has a symbol table. */
5537 if (get_symtab (per_cu) != NULL)
5538 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5539
5540 for (ix = 0;
5541 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5542 ++ix)
5543 recursively_compute_inclusions (result, all_children, iter);
5544}
5545
5546/* Compute the symtab 'includes' fields for the symtab related to
5547 PER_CU. */
5548
5549static void
5550compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5551{
5552 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5553 {
5554 int ix, len;
5555 struct dwarf2_per_cu_data *iter;
5556 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5557 htab_t all_children;
5558 struct symtab *symtab = get_symtab (per_cu);
5559
5560 /* If we don't have a symtab, we can just skip this case. */
5561 if (symtab == NULL)
5562 return;
5563
5564 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5565 NULL, xcalloc, xfree);
5566
5567 for (ix = 0;
5568 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5569 ix, iter);
5570 ++ix)
5571 recursively_compute_inclusions (&result_children, all_children, iter);
5572
5573 /* Now we have a transitive closure of all the included CUs, so
5574 we can convert it to a list of symtabs. */
5575 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5576 symtab->includes
5577 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5578 (len + 1) * sizeof (struct symtab *));
5579 for (ix = 0;
5580 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5581 ++ix)
5582 symtab->includes[ix] = get_symtab (iter);
5583 symtab->includes[len] = NULL;
5584
5585 VEC_free (dwarf2_per_cu_ptr, result_children);
5586 htab_delete (all_children);
5587 }
5588}
5589
5590/* Compute the 'includes' field for the symtabs of all the CUs we just
5591 read. */
5592
5593static void
5594process_cu_includes (void)
5595{
5596 int ix;
5597 struct dwarf2_per_cu_data *iter;
5598
5599 for (ix = 0;
5600 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5601 ix, iter);
5602 ++ix)
5603 compute_symtab_includes (iter);
5604
5605 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5606}
5607
9cdd5dbd 5608/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
5609 already been loaded into memory. */
5610
5611static void
95554aad
TT
5612process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5613 enum language pretend_language)
10b3939b 5614{
10b3939b 5615 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 5616 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
5617 CORE_ADDR lowpc, highpc;
5618 struct symtab *symtab;
3da10d80 5619 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
5620 CORE_ADDR baseaddr;
5621
5622 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5623
10b3939b
DJ
5624 buildsym_init ();
5625 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 5626 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
5627
5628 cu->list_in_scope = &file_symbols;
c906108c 5629
95554aad
TT
5630 cu->language = pretend_language;
5631 cu->language_defn = language_def (cu->language);
5632
c906108c 5633 /* Do line number decoding in read_file_scope () */
10b3939b 5634 process_die (cu->dies, cu);
c906108c 5635
a766d390
DE
5636 /* For now fudge the Go package. */
5637 if (cu->language == language_go)
5638 fixup_go_packaging (cu);
5639
3da10d80
KS
5640 /* Now that we have processed all the DIEs in the CU, all the types
5641 should be complete, and it should now be safe to compute all of the
5642 physnames. */
5643 compute_delayed_physnames (cu);
5644 do_cleanups (delayed_list_cleanup);
5645
fae299cd
DC
5646 /* Some compilers don't define a DW_AT_high_pc attribute for the
5647 compilation unit. If the DW_AT_high_pc is missing, synthesize
5648 it, by scanning the DIE's below the compilation unit. */
10b3939b 5649 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 5650
613e1657 5651 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 5652
8be455d7 5653 if (symtab != NULL)
c906108c 5654 {
df15bd07 5655 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 5656
8be455d7
JK
5657 /* Set symtab language to language from DW_AT_language. If the
5658 compilation is from a C file generated by language preprocessors, do
5659 not set the language if it was already deduced by start_subfile. */
5660 if (!(cu->language == language_c && symtab->language != language_c))
5661 symtab->language = cu->language;
5662
5663 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5664 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
5665 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5666 there were bugs in prologue debug info, fixed later in GCC-4.5
5667 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
5668
5669 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5670 needed, it would be wrong due to missing DW_AT_producer there.
5671
5672 Still one can confuse GDB by using non-standard GCC compilation
5673 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5674 */
ab260dad 5675 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 5676 symtab->locations_valid = 1;
e0d00bc7
JK
5677
5678 if (gcc_4_minor >= 5)
5679 symtab->epilogue_unwind_valid = 1;
96408a79
SA
5680
5681 symtab->call_site_htab = cu->call_site_htab;
c906108c 5682 }
9291a0cd
TT
5683
5684 if (dwarf2_per_objfile->using_index)
5685 per_cu->v.quick->symtab = symtab;
5686 else
5687 {
5688 struct partial_symtab *pst = per_cu->v.psymtab;
5689 pst->symtab = symtab;
5690 pst->readin = 1;
5691 }
c906108c 5692
95554aad
TT
5693 /* Push it for inclusion processing later. */
5694 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5695
c906108c
SS
5696 do_cleanups (back_to);
5697}
5698
95554aad
TT
5699/* Process an imported unit DIE. */
5700
5701static void
5702process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5703{
5704 struct attribute *attr;
5705
5706 attr = dwarf2_attr (die, DW_AT_import, cu);
5707 if (attr != NULL)
5708 {
5709 struct dwarf2_per_cu_data *per_cu;
5710 struct symtab *imported_symtab;
5711 sect_offset offset;
5712
5713 offset = dwarf2_get_ref_die_offset (attr);
5714 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5715
5716 /* Queue the unit, if needed. */
5717 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
5718 load_full_comp_unit (per_cu, cu->language);
5719
5720 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5721 per_cu);
5722 }
5723}
5724
c906108c
SS
5725/* Process a die and its children. */
5726
5727static void
e7c27a73 5728process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
5729{
5730 switch (die->tag)
5731 {
5732 case DW_TAG_padding:
5733 break;
5734 case DW_TAG_compile_unit:
95554aad 5735 case DW_TAG_partial_unit:
e7c27a73 5736 read_file_scope (die, cu);
c906108c 5737 break;
348e048f
DE
5738 case DW_TAG_type_unit:
5739 read_type_unit_scope (die, cu);
5740 break;
c906108c 5741 case DW_TAG_subprogram:
c906108c 5742 case DW_TAG_inlined_subroutine:
edb3359d 5743 read_func_scope (die, cu);
c906108c
SS
5744 break;
5745 case DW_TAG_lexical_block:
14898363
L
5746 case DW_TAG_try_block:
5747 case DW_TAG_catch_block:
e7c27a73 5748 read_lexical_block_scope (die, cu);
c906108c 5749 break;
96408a79
SA
5750 case DW_TAG_GNU_call_site:
5751 read_call_site_scope (die, cu);
5752 break;
c906108c 5753 case DW_TAG_class_type:
680b30c7 5754 case DW_TAG_interface_type:
c906108c
SS
5755 case DW_TAG_structure_type:
5756 case DW_TAG_union_type:
134d01f1 5757 process_structure_scope (die, cu);
c906108c
SS
5758 break;
5759 case DW_TAG_enumeration_type:
134d01f1 5760 process_enumeration_scope (die, cu);
c906108c 5761 break;
134d01f1 5762
f792889a
DJ
5763 /* These dies have a type, but processing them does not create
5764 a symbol or recurse to process the children. Therefore we can
5765 read them on-demand through read_type_die. */
c906108c 5766 case DW_TAG_subroutine_type:
72019c9c 5767 case DW_TAG_set_type:
c906108c 5768 case DW_TAG_array_type:
c906108c 5769 case DW_TAG_pointer_type:
c906108c 5770 case DW_TAG_ptr_to_member_type:
c906108c 5771 case DW_TAG_reference_type:
c906108c 5772 case DW_TAG_string_type:
c906108c 5773 break;
134d01f1 5774
c906108c 5775 case DW_TAG_base_type:
a02abb62 5776 case DW_TAG_subrange_type:
cb249c71 5777 case DW_TAG_typedef:
134d01f1
DJ
5778 /* Add a typedef symbol for the type definition, if it has a
5779 DW_AT_name. */
f792889a 5780 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 5781 break;
c906108c 5782 case DW_TAG_common_block:
e7c27a73 5783 read_common_block (die, cu);
c906108c
SS
5784 break;
5785 case DW_TAG_common_inclusion:
5786 break;
d9fa45fe 5787 case DW_TAG_namespace:
63d06c5c 5788 processing_has_namespace_info = 1;
e7c27a73 5789 read_namespace (die, cu);
d9fa45fe 5790 break;
5d7cb8df 5791 case DW_TAG_module:
f55ee35c 5792 processing_has_namespace_info = 1;
5d7cb8df
JK
5793 read_module (die, cu);
5794 break;
d9fa45fe
DC
5795 case DW_TAG_imported_declaration:
5796 case DW_TAG_imported_module:
63d06c5c 5797 processing_has_namespace_info = 1;
27aa8d6a
SW
5798 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5799 || cu->language != language_fortran))
5800 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5801 dwarf_tag_name (die->tag));
5802 read_import_statement (die, cu);
d9fa45fe 5803 break;
95554aad
TT
5804
5805 case DW_TAG_imported_unit:
5806 process_imported_unit_die (die, cu);
5807 break;
5808
c906108c 5809 default:
e7c27a73 5810 new_symbol (die, NULL, cu);
c906108c
SS
5811 break;
5812 }
5813}
5814
94af9270
KS
5815/* A helper function for dwarf2_compute_name which determines whether DIE
5816 needs to have the name of the scope prepended to the name listed in the
5817 die. */
5818
5819static int
5820die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5821{
1c809c68
TT
5822 struct attribute *attr;
5823
94af9270
KS
5824 switch (die->tag)
5825 {
5826 case DW_TAG_namespace:
5827 case DW_TAG_typedef:
5828 case DW_TAG_class_type:
5829 case DW_TAG_interface_type:
5830 case DW_TAG_structure_type:
5831 case DW_TAG_union_type:
5832 case DW_TAG_enumeration_type:
5833 case DW_TAG_enumerator:
5834 case DW_TAG_subprogram:
5835 case DW_TAG_member:
5836 return 1;
5837
5838 case DW_TAG_variable:
c2b0a229 5839 case DW_TAG_constant:
94af9270
KS
5840 /* We only need to prefix "globally" visible variables. These include
5841 any variable marked with DW_AT_external or any variable that
5842 lives in a namespace. [Variables in anonymous namespaces
5843 require prefixing, but they are not DW_AT_external.] */
5844
5845 if (dwarf2_attr (die, DW_AT_specification, cu))
5846 {
5847 struct dwarf2_cu *spec_cu = cu;
9a619af0 5848
94af9270
KS
5849 return die_needs_namespace (die_specification (die, &spec_cu),
5850 spec_cu);
5851 }
5852
1c809c68 5853 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
5854 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5855 && die->parent->tag != DW_TAG_module)
1c809c68
TT
5856 return 0;
5857 /* A variable in a lexical block of some kind does not need a
5858 namespace, even though in C++ such variables may be external
5859 and have a mangled name. */
5860 if (die->parent->tag == DW_TAG_lexical_block
5861 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5862 || die->parent->tag == DW_TAG_catch_block
5863 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5864 return 0;
5865 return 1;
94af9270
KS
5866
5867 default:
5868 return 0;
5869 }
5870}
5871
98bfdba5
PA
5872/* Retrieve the last character from a mem_file. */
5873
5874static void
5875do_ui_file_peek_last (void *object, const char *buffer, long length)
5876{
5877 char *last_char_p = (char *) object;
5878
5879 if (length > 0)
5880 *last_char_p = buffer[length - 1];
5881}
5882
94af9270 5883/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
5884 compute the physname for the object, which include a method's:
5885 - formal parameters (C++/Java),
5886 - receiver type (Go),
5887 - return type (Java).
5888
5889 The term "physname" is a bit confusing.
5890 For C++, for example, it is the demangled name.
5891 For Go, for example, it's the mangled name.
94af9270 5892
af6b7be1
JB
5893 For Ada, return the DIE's linkage name rather than the fully qualified
5894 name. PHYSNAME is ignored..
5895
94af9270
KS
5896 The result is allocated on the objfile_obstack and canonicalized. */
5897
5898static const char *
5899dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5900 int physname)
5901{
bb5ed363
DE
5902 struct objfile *objfile = cu->objfile;
5903
94af9270
KS
5904 if (name == NULL)
5905 name = dwarf2_name (die, cu);
5906
f55ee35c
JK
5907 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5908 compute it by typename_concat inside GDB. */
5909 if (cu->language == language_ada
5910 || (cu->language == language_fortran && physname))
5911 {
5912 /* For Ada unit, we prefer the linkage name over the name, as
5913 the former contains the exported name, which the user expects
5914 to be able to reference. Ideally, we want the user to be able
5915 to reference this entity using either natural or linkage name,
5916 but we haven't started looking at this enhancement yet. */
5917 struct attribute *attr;
5918
5919 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5920 if (attr == NULL)
5921 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5922 if (attr && DW_STRING (attr))
5923 return DW_STRING (attr);
5924 }
5925
94af9270
KS
5926 /* These are the only languages we know how to qualify names in. */
5927 if (name != NULL
f55ee35c
JK
5928 && (cu->language == language_cplus || cu->language == language_java
5929 || cu->language == language_fortran))
94af9270
KS
5930 {
5931 if (die_needs_namespace (die, cu))
5932 {
5933 long length;
0d5cff50 5934 const char *prefix;
94af9270
KS
5935 struct ui_file *buf;
5936
5937 prefix = determine_prefix (die, cu);
5938 buf = mem_fileopen ();
5939 if (*prefix != '\0')
5940 {
f55ee35c
JK
5941 char *prefixed_name = typename_concat (NULL, prefix, name,
5942 physname, cu);
9a619af0 5943
94af9270
KS
5944 fputs_unfiltered (prefixed_name, buf);
5945 xfree (prefixed_name);
5946 }
5947 else
62d5b8da 5948 fputs_unfiltered (name, buf);
94af9270 5949
98bfdba5
PA
5950 /* Template parameters may be specified in the DIE's DW_AT_name, or
5951 as children with DW_TAG_template_type_param or
5952 DW_TAG_value_type_param. If the latter, add them to the name
5953 here. If the name already has template parameters, then
5954 skip this step; some versions of GCC emit both, and
5955 it is more efficient to use the pre-computed name.
5956
5957 Something to keep in mind about this process: it is very
5958 unlikely, or in some cases downright impossible, to produce
5959 something that will match the mangled name of a function.
5960 If the definition of the function has the same debug info,
5961 we should be able to match up with it anyway. But fallbacks
5962 using the minimal symbol, for instance to find a method
5963 implemented in a stripped copy of libstdc++, will not work.
5964 If we do not have debug info for the definition, we will have to
5965 match them up some other way.
5966
5967 When we do name matching there is a related problem with function
5968 templates; two instantiated function templates are allowed to
5969 differ only by their return types, which we do not add here. */
5970
5971 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5972 {
5973 struct attribute *attr;
5974 struct die_info *child;
5975 int first = 1;
5976
5977 die->building_fullname = 1;
5978
5979 for (child = die->child; child != NULL; child = child->sibling)
5980 {
5981 struct type *type;
12df843f 5982 LONGEST value;
98bfdba5
PA
5983 gdb_byte *bytes;
5984 struct dwarf2_locexpr_baton *baton;
5985 struct value *v;
5986
5987 if (child->tag != DW_TAG_template_type_param
5988 && child->tag != DW_TAG_template_value_param)
5989 continue;
5990
5991 if (first)
5992 {
5993 fputs_unfiltered ("<", buf);
5994 first = 0;
5995 }
5996 else
5997 fputs_unfiltered (", ", buf);
5998
5999 attr = dwarf2_attr (child, DW_AT_type, cu);
6000 if (attr == NULL)
6001 {
6002 complaint (&symfile_complaints,
6003 _("template parameter missing DW_AT_type"));
6004 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6005 continue;
6006 }
6007 type = die_type (child, cu);
6008
6009 if (child->tag == DW_TAG_template_type_param)
6010 {
6011 c_print_type (type, "", buf, -1, 0);
6012 continue;
6013 }
6014
6015 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6016 if (attr == NULL)
6017 {
6018 complaint (&symfile_complaints,
3e43a32a
MS
6019 _("template parameter missing "
6020 "DW_AT_const_value"));
98bfdba5
PA
6021 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6022 continue;
6023 }
6024
6025 dwarf2_const_value_attr (attr, type, name,
6026 &cu->comp_unit_obstack, cu,
6027 &value, &bytes, &baton);
6028
6029 if (TYPE_NOSIGN (type))
6030 /* GDB prints characters as NUMBER 'CHAR'. If that's
6031 changed, this can use value_print instead. */
6032 c_printchar (value, type, buf);
6033 else
6034 {
6035 struct value_print_options opts;
6036
6037 if (baton != NULL)
6038 v = dwarf2_evaluate_loc_desc (type, NULL,
6039 baton->data,
6040 baton->size,
6041 baton->per_cu);
6042 else if (bytes != NULL)
6043 {
6044 v = allocate_value (type);
6045 memcpy (value_contents_writeable (v), bytes,
6046 TYPE_LENGTH (type));
6047 }
6048 else
6049 v = value_from_longest (type, value);
6050
3e43a32a
MS
6051 /* Specify decimal so that we do not depend on
6052 the radix. */
98bfdba5
PA
6053 get_formatted_print_options (&opts, 'd');
6054 opts.raw = 1;
6055 value_print (v, buf, &opts);
6056 release_value (v);
6057 value_free (v);
6058 }
6059 }
6060
6061 die->building_fullname = 0;
6062
6063 if (!first)
6064 {
6065 /* Close the argument list, with a space if necessary
6066 (nested templates). */
6067 char last_char = '\0';
6068 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6069 if (last_char == '>')
6070 fputs_unfiltered (" >", buf);
6071 else
6072 fputs_unfiltered (">", buf);
6073 }
6074 }
6075
94af9270
KS
6076 /* For Java and C++ methods, append formal parameter type
6077 information, if PHYSNAME. */
6e70227d 6078
94af9270
KS
6079 if (physname && die->tag == DW_TAG_subprogram
6080 && (cu->language == language_cplus
6081 || cu->language == language_java))
6082 {
6083 struct type *type = read_type_die (die, cu);
6084
3167638f 6085 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
6086
6087 if (cu->language == language_java)
6088 {
6089 /* For java, we must append the return type to method
0963b4bd 6090 names. */
94af9270
KS
6091 if (die->tag == DW_TAG_subprogram)
6092 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6093 0, 0);
6094 }
6095 else if (cu->language == language_cplus)
6096 {
60430eff
DJ
6097 /* Assume that an artificial first parameter is
6098 "this", but do not crash if it is not. RealView
6099 marks unnamed (and thus unused) parameters as
6100 artificial; there is no way to differentiate
6101 the two cases. */
94af9270
KS
6102 if (TYPE_NFIELDS (type) > 0
6103 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 6104 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
6105 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6106 0))))
94af9270
KS
6107 fputs_unfiltered (" const", buf);
6108 }
6109 }
6110
bb5ed363 6111 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
6112 &length);
6113 ui_file_delete (buf);
6114
6115 if (cu->language == language_cplus)
6116 {
6117 char *cname
6118 = dwarf2_canonicalize_name (name, cu,
bb5ed363 6119 &objfile->objfile_obstack);
9a619af0 6120
94af9270
KS
6121 if (cname != NULL)
6122 name = cname;
6123 }
6124 }
6125 }
6126
6127 return name;
6128}
6129
0114d602
DJ
6130/* Return the fully qualified name of DIE, based on its DW_AT_name.
6131 If scope qualifiers are appropriate they will be added. The result
6132 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
6133 not have a name. NAME may either be from a previous call to
6134 dwarf2_name or NULL.
6135
0963b4bd 6136 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
6137
6138static const char *
94af9270 6139dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 6140{
94af9270
KS
6141 return dwarf2_compute_name (name, die, cu, 0);
6142}
0114d602 6143
94af9270
KS
6144/* Construct a physname for the given DIE in CU. NAME may either be
6145 from a previous call to dwarf2_name or NULL. The result will be
6146 allocated on the objfile_objstack or NULL if the DIE does not have a
6147 name.
0114d602 6148
94af9270 6149 The output string will be canonicalized (if C++/Java). */
0114d602 6150
94af9270
KS
6151static const char *
6152dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6153{
bb5ed363 6154 struct objfile *objfile = cu->objfile;
900e11f9
JK
6155 struct attribute *attr;
6156 const char *retval, *mangled = NULL, *canon = NULL;
6157 struct cleanup *back_to;
6158 int need_copy = 1;
6159
6160 /* In this case dwarf2_compute_name is just a shortcut not building anything
6161 on its own. */
6162 if (!die_needs_namespace (die, cu))
6163 return dwarf2_compute_name (name, die, cu, 1);
6164
6165 back_to = make_cleanup (null_cleanup, NULL);
6166
6167 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6168 if (!attr)
6169 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6170
6171 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6172 has computed. */
6173 if (attr && DW_STRING (attr))
6174 {
6175 char *demangled;
6176
6177 mangled = DW_STRING (attr);
6178
6179 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6180 type. It is easier for GDB users to search for such functions as
6181 `name(params)' than `long name(params)'. In such case the minimal
6182 symbol names do not match the full symbol names but for template
6183 functions there is never a need to look up their definition from their
6184 declaration so the only disadvantage remains the minimal symbol
6185 variant `long name(params)' does not have the proper inferior type.
6186 */
6187
a766d390
DE
6188 if (cu->language == language_go)
6189 {
6190 /* This is a lie, but we already lie to the caller new_symbol_full.
6191 new_symbol_full assumes we return the mangled name.
6192 This just undoes that lie until things are cleaned up. */
6193 demangled = NULL;
6194 }
6195 else
6196 {
6197 demangled = cplus_demangle (mangled,
6198 (DMGL_PARAMS | DMGL_ANSI
6199 | (cu->language == language_java
6200 ? DMGL_JAVA | DMGL_RET_POSTFIX
6201 : DMGL_RET_DROP)));
6202 }
900e11f9
JK
6203 if (demangled)
6204 {
6205 make_cleanup (xfree, demangled);
6206 canon = demangled;
6207 }
6208 else
6209 {
6210 canon = mangled;
6211 need_copy = 0;
6212 }
6213 }
6214
6215 if (canon == NULL || check_physname)
6216 {
6217 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6218
6219 if (canon != NULL && strcmp (physname, canon) != 0)
6220 {
6221 /* It may not mean a bug in GDB. The compiler could also
6222 compute DW_AT_linkage_name incorrectly. But in such case
6223 GDB would need to be bug-to-bug compatible. */
6224
6225 complaint (&symfile_complaints,
6226 _("Computed physname <%s> does not match demangled <%s> "
6227 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 6228 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
6229
6230 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6231 is available here - over computed PHYSNAME. It is safer
6232 against both buggy GDB and buggy compilers. */
6233
6234 retval = canon;
6235 }
6236 else
6237 {
6238 retval = physname;
6239 need_copy = 0;
6240 }
6241 }
6242 else
6243 retval = canon;
6244
6245 if (need_copy)
6246 retval = obsavestring (retval, strlen (retval),
bb5ed363 6247 &objfile->objfile_obstack);
900e11f9
JK
6248
6249 do_cleanups (back_to);
6250 return retval;
0114d602
DJ
6251}
6252
27aa8d6a
SW
6253/* Read the import statement specified by the given die and record it. */
6254
6255static void
6256read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6257{
bb5ed363 6258 struct objfile *objfile = cu->objfile;
27aa8d6a 6259 struct attribute *import_attr;
32019081 6260 struct die_info *imported_die, *child_die;
de4affc9 6261 struct dwarf2_cu *imported_cu;
27aa8d6a 6262 const char *imported_name;
794684b6 6263 const char *imported_name_prefix;
13387711
SW
6264 const char *canonical_name;
6265 const char *import_alias;
6266 const char *imported_declaration = NULL;
794684b6 6267 const char *import_prefix;
32019081
JK
6268 VEC (const_char_ptr) *excludes = NULL;
6269 struct cleanup *cleanups;
13387711
SW
6270
6271 char *temp;
27aa8d6a
SW
6272
6273 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6274 if (import_attr == NULL)
6275 {
6276 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6277 dwarf_tag_name (die->tag));
6278 return;
6279 }
6280
de4affc9
CC
6281 imported_cu = cu;
6282 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6283 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
6284 if (imported_name == NULL)
6285 {
6286 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6287
6288 The import in the following code:
6289 namespace A
6290 {
6291 typedef int B;
6292 }
6293
6294 int main ()
6295 {
6296 using A::B;
6297 B b;
6298 return b;
6299 }
6300
6301 ...
6302 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6303 <52> DW_AT_decl_file : 1
6304 <53> DW_AT_decl_line : 6
6305 <54> DW_AT_import : <0x75>
6306 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6307 <59> DW_AT_name : B
6308 <5b> DW_AT_decl_file : 1
6309 <5c> DW_AT_decl_line : 2
6310 <5d> DW_AT_type : <0x6e>
6311 ...
6312 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6313 <76> DW_AT_byte_size : 4
6314 <77> DW_AT_encoding : 5 (signed)
6315
6316 imports the wrong die ( 0x75 instead of 0x58 ).
6317 This case will be ignored until the gcc bug is fixed. */
6318 return;
6319 }
6320
82856980
SW
6321 /* Figure out the local name after import. */
6322 import_alias = dwarf2_name (die, cu);
27aa8d6a 6323
794684b6
SW
6324 /* Figure out where the statement is being imported to. */
6325 import_prefix = determine_prefix (die, cu);
6326
6327 /* Figure out what the scope of the imported die is and prepend it
6328 to the name of the imported die. */
de4affc9 6329 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 6330
f55ee35c
JK
6331 if (imported_die->tag != DW_TAG_namespace
6332 && imported_die->tag != DW_TAG_module)
794684b6 6333 {
13387711
SW
6334 imported_declaration = imported_name;
6335 canonical_name = imported_name_prefix;
794684b6 6336 }
13387711 6337 else if (strlen (imported_name_prefix) > 0)
794684b6 6338 {
13387711
SW
6339 temp = alloca (strlen (imported_name_prefix)
6340 + 2 + strlen (imported_name) + 1);
6341 strcpy (temp, imported_name_prefix);
6342 strcat (temp, "::");
6343 strcat (temp, imported_name);
6344 canonical_name = temp;
794684b6 6345 }
13387711
SW
6346 else
6347 canonical_name = imported_name;
794684b6 6348
32019081
JK
6349 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6350
6351 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6352 for (child_die = die->child; child_die && child_die->tag;
6353 child_die = sibling_die (child_die))
6354 {
6355 /* DWARF-4: A Fortran use statement with a “rename list” may be
6356 represented by an imported module entry with an import attribute
6357 referring to the module and owned entries corresponding to those
6358 entities that are renamed as part of being imported. */
6359
6360 if (child_die->tag != DW_TAG_imported_declaration)
6361 {
6362 complaint (&symfile_complaints,
6363 _("child DW_TAG_imported_declaration expected "
6364 "- DIE at 0x%x [in module %s]"),
b64f50a1 6365 child_die->offset.sect_off, objfile->name);
32019081
JK
6366 continue;
6367 }
6368
6369 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6370 if (import_attr == NULL)
6371 {
6372 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6373 dwarf_tag_name (child_die->tag));
6374 continue;
6375 }
6376
6377 imported_cu = cu;
6378 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6379 &imported_cu);
6380 imported_name = dwarf2_name (imported_die, imported_cu);
6381 if (imported_name == NULL)
6382 {
6383 complaint (&symfile_complaints,
6384 _("child DW_TAG_imported_declaration has unknown "
6385 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 6386 child_die->offset.sect_off, objfile->name);
32019081
JK
6387 continue;
6388 }
6389
6390 VEC_safe_push (const_char_ptr, excludes, imported_name);
6391
6392 process_die (child_die, cu);
6393 }
6394
c0cc3a76
SW
6395 cp_add_using_directive (import_prefix,
6396 canonical_name,
6397 import_alias,
13387711 6398 imported_declaration,
32019081 6399 excludes,
bb5ed363 6400 &objfile->objfile_obstack);
32019081
JK
6401
6402 do_cleanups (cleanups);
27aa8d6a
SW
6403}
6404
ae2de4f8
DE
6405/* Cleanup function for read_file_scope. */
6406
cb1df416
DJ
6407static void
6408free_cu_line_header (void *arg)
6409{
6410 struct dwarf2_cu *cu = arg;
6411
6412 free_line_header (cu->line_header);
6413 cu->line_header = NULL;
6414}
6415
9291a0cd
TT
6416static void
6417find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6418 char **name, char **comp_dir)
6419{
6420 struct attribute *attr;
6421
6422 *name = NULL;
6423 *comp_dir = NULL;
6424
6425 /* Find the filename. Do not use dwarf2_name here, since the filename
6426 is not a source language identifier. */
6427 attr = dwarf2_attr (die, DW_AT_name, cu);
6428 if (attr)
6429 {
6430 *name = DW_STRING (attr);
6431 }
6432
6433 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6434 if (attr)
6435 *comp_dir = DW_STRING (attr);
6436 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6437 {
6438 *comp_dir = ldirname (*name);
6439 if (*comp_dir != NULL)
6440 make_cleanup (xfree, *comp_dir);
6441 }
6442 if (*comp_dir != NULL)
6443 {
6444 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6445 directory, get rid of it. */
6446 char *cp = strchr (*comp_dir, ':');
6447
6448 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6449 *comp_dir = cp + 1;
6450 }
6451
6452 if (*name == NULL)
6453 *name = "<unknown>";
6454}
6455
f3f5162e
DE
6456/* Handle DW_AT_stmt_list for a compilation unit or type unit.
6457 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6458 COMP_DIR is the compilation directory.
6459 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
6460
6461static void
6462handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 6463 const char *comp_dir, int want_line_info)
2ab95328
TT
6464{
6465 struct attribute *attr;
2ab95328 6466
2ab95328
TT
6467 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6468 if (attr)
6469 {
6470 unsigned int line_offset = DW_UNSND (attr);
6471 struct line_header *line_header
3019eac3 6472 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
6473
6474 if (line_header)
dee91e82
DE
6475 {
6476 cu->line_header = line_header;
6477 make_cleanup (free_cu_line_header, cu);
f3f5162e 6478 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
dee91e82 6479 }
2ab95328
TT
6480 }
6481}
6482
95554aad 6483/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 6484
c906108c 6485static void
e7c27a73 6486read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6487{
dee91e82 6488 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 6489 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 6490 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
6491 CORE_ADDR highpc = ((CORE_ADDR) 0);
6492 struct attribute *attr;
e1024ff1 6493 char *name = NULL;
c906108c
SS
6494 char *comp_dir = NULL;
6495 struct die_info *child_die;
6496 bfd *abfd = objfile->obfd;
e142c38c 6497 CORE_ADDR baseaddr;
6e70227d 6498
e142c38c 6499 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6500
fae299cd 6501 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
6502
6503 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6504 from finish_block. */
2acceee2 6505 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
6506 lowpc = highpc;
6507 lowpc += baseaddr;
6508 highpc += baseaddr;
6509
9291a0cd 6510 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 6511
95554aad 6512 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 6513
f4b8a18d
KW
6514 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6515 standardised yet. As a workaround for the language detection we fall
6516 back to the DW_AT_producer string. */
6517 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6518 cu->language = language_opencl;
6519
3019eac3
DE
6520 /* Similar hack for Go. */
6521 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6522 set_cu_language (DW_LANG_Go, cu);
6523
6524 /* We assume that we're processing GCC output. */
6525 processing_gcc_compilation = 2;
6526
6527 processing_has_namespace_info = 0;
6528
6529 start_symtab (name, comp_dir, lowpc);
6530 record_debugformat ("DWARF 2");
6531 record_producer (cu->producer);
6532
6533 /* Decode line number information if present. We do this before
6534 processing child DIEs, so that the line header table is available
6535 for DW_AT_decl_file. */
6536 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6537
6538 /* Process all dies in compilation unit. */
6539 if (die->child != NULL)
6540 {
6541 child_die = die->child;
6542 while (child_die && child_die->tag)
6543 {
6544 process_die (child_die, cu);
6545 child_die = sibling_die (child_die);
6546 }
6547 }
6548
6549 /* Decode macro information, if present. Dwarf 2 macro information
6550 refers to information in the line number info statement program
6551 header, so we can only read it if we've read the header
6552 successfully. */
6553 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6554 if (attr && cu->line_header)
6555 {
6556 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6557 complaint (&symfile_complaints,
6558 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6559
6560 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
6561 comp_dir, abfd, cu,
fceca515
DE
6562 &dwarf2_per_objfile->macro, 1,
6563 ".debug_macro");
3019eac3
DE
6564 }
6565 else
6566 {
6567 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6568 if (attr && cu->line_header)
6569 {
6570 unsigned int macro_offset = DW_UNSND (attr);
6571
6572 dwarf_decode_macros (cu->line_header, macro_offset,
6573 comp_dir, abfd, cu,
fceca515
DE
6574 &dwarf2_per_objfile->macinfo, 0,
6575 ".debug_macinfo");
3019eac3
DE
6576 }
6577 }
6578
6579 do_cleanups (back_to);
6580}
6581
6582/* Process DW_TAG_type_unit.
6583 For TUs we want to skip the first top level sibling if it's not the
6584 actual type being defined by this TU. In this case the first top
6585 level sibling is there to provide context only. */
6586
6587static void
6588read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6589{
6590 struct objfile *objfile = cu->objfile;
6591 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6592 CORE_ADDR lowpc;
6593 struct attribute *attr;
6594 char *name = NULL;
6595 char *comp_dir = NULL;
6596 struct die_info *child_die;
6597 bfd *abfd = objfile->obfd;
6598
6599 /* start_symtab needs a low pc, but we don't really have one.
6600 Do what read_file_scope would do in the absence of such info. */
6601 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6602
6603 /* Find the filename. Do not use dwarf2_name here, since the filename
6604 is not a source language identifier. */
6605 attr = dwarf2_attr (die, DW_AT_name, cu);
6606 if (attr)
6607 name = DW_STRING (attr);
6608
6609 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6610 if (attr)
6611 comp_dir = DW_STRING (attr);
6612 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6613 {
6614 comp_dir = ldirname (name);
6615 if (comp_dir != NULL)
6616 make_cleanup (xfree, comp_dir);
6617 }
6618
6619 if (name == NULL)
6620 name = "<unknown>";
6621
95554aad 6622 prepare_one_comp_unit (cu, die, language_minimal);
3019eac3
DE
6623
6624 /* We assume that we're processing GCC output. */
6625 processing_gcc_compilation = 2;
6626
6627 processing_has_namespace_info = 0;
6628
6629 start_symtab (name, comp_dir, lowpc);
6630 record_debugformat ("DWARF 2");
6631 record_producer (cu->producer);
6632
6633 /* Decode line number information if present. We do this before
6634 processing child DIEs, so that the line header table is available
6635 for DW_AT_decl_file.
6636 We don't need the pc/line-number mapping for type units. */
6637 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6638
6639 /* Process the dies in the type unit. */
6640 if (die->child == NULL)
6641 {
6642 dump_die_for_error (die);
6643 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6644 bfd_get_filename (abfd));
6645 }
6646
6647 child_die = die->child;
6648
6649 while (child_die && child_die->tag)
6650 {
6651 process_die (child_die, cu);
6652
6653 child_die = sibling_die (child_die);
6654 }
6655
6656 do_cleanups (back_to);
6657}
6658\f
6659/* DWO files. */
6660
6661static hashval_t
6662hash_dwo_file (const void *item)
6663{
6664 const struct dwo_file *dwo_file = item;
6665
6666 return htab_hash_string (dwo_file->dwo_name);
6667}
6668
6669static int
6670eq_dwo_file (const void *item_lhs, const void *item_rhs)
6671{
6672 const struct dwo_file *lhs = item_lhs;
6673 const struct dwo_file *rhs = item_rhs;
6674
6675 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6676}
6677
6678/* Allocate a hash table for DWO files. */
6679
6680static htab_t
6681allocate_dwo_file_hash_table (void)
6682{
6683 struct objfile *objfile = dwarf2_per_objfile->objfile;
6684
6685 return htab_create_alloc_ex (41,
6686 hash_dwo_file,
6687 eq_dwo_file,
6688 NULL,
6689 &objfile->objfile_obstack,
6690 hashtab_obstack_allocate,
6691 dummy_obstack_deallocate);
6692}
6693
6694static hashval_t
6695hash_dwo_unit (const void *item)
6696{
6697 const struct dwo_unit *dwo_unit = item;
6698
6699 /* This drops the top 32 bits of the id, but is ok for a hash. */
6700 return dwo_unit->signature;
6701}
6702
6703static int
6704eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6705{
6706 const struct dwo_unit *lhs = item_lhs;
6707 const struct dwo_unit *rhs = item_rhs;
6708
6709 /* The signature is assumed to be unique within the DWO file.
6710 So while object file CU dwo_id's always have the value zero,
6711 that's OK, assuming each object file DWO file has only one CU,
6712 and that's the rule for now. */
6713 return lhs->signature == rhs->signature;
6714}
6715
6716/* Allocate a hash table for DWO CUs,TUs.
6717 There is one of these tables for each of CUs,TUs for each DWO file. */
6718
6719static htab_t
6720allocate_dwo_unit_table (struct objfile *objfile)
6721{
6722 /* Start out with a pretty small number.
6723 Generally DWO files contain only one CU and maybe some TUs. */
6724 return htab_create_alloc_ex (3,
6725 hash_dwo_unit,
6726 eq_dwo_unit,
6727 NULL,
6728 &objfile->objfile_obstack,
6729 hashtab_obstack_allocate,
6730 dummy_obstack_deallocate);
6731}
6732
6733/* This function is mapped across the sections and remembers the offset and
6734 size of each of the DWO debugging sections we are interested in. */
6735
6736static void
6737dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6738{
6739 struct dwo_file *dwo_file = dwo_file_ptr;
6740 const struct dwo_section_names *names = &dwo_section_names;
6741
6742 if (section_is_p (sectp->name, &names->abbrev_dwo))
6743 {
6744 dwo_file->sections.abbrev.asection = sectp;
6745 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6746 }
6747 else if (section_is_p (sectp->name, &names->info_dwo))
6748 {
6749 dwo_file->sections.info.asection = sectp;
6750 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6751 }
6752 else if (section_is_p (sectp->name, &names->line_dwo))
6753 {
6754 dwo_file->sections.line.asection = sectp;
6755 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6756 }
6757 else if (section_is_p (sectp->name, &names->loc_dwo))
6758 {
6759 dwo_file->sections.loc.asection = sectp;
6760 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6761 }
6762 else if (section_is_p (sectp->name, &names->str_dwo))
6763 {
6764 dwo_file->sections.str.asection = sectp;
6765 dwo_file->sections.str.size = bfd_get_section_size (sectp);
6766 }
6767 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
6768 {
6769 dwo_file->sections.str_offsets.asection = sectp;
6770 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
6771 }
6772 else if (section_is_p (sectp->name, &names->types_dwo))
6773 {
6774 struct dwarf2_section_info type_section;
6775
6776 memset (&type_section, 0, sizeof (type_section));
6777 type_section.asection = sectp;
6778 type_section.size = bfd_get_section_size (sectp);
6779 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
6780 &type_section);
6781 }
6782}
6783
6784/* Structure used to pass data to create_debug_info_hash_table_reader. */
6785
6786struct create_dwo_info_table_data
6787{
6788 struct dwo_file *dwo_file;
6789 htab_t cu_htab;
6790};
6791
6792/* die_reader_func for create_debug_info_hash_table. */
6793
6794static void
6795create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
6796 gdb_byte *info_ptr,
6797 struct die_info *comp_unit_die,
6798 int has_children,
6799 void *datap)
6800{
6801 struct dwarf2_cu *cu = reader->cu;
6802 struct objfile *objfile = dwarf2_per_objfile->objfile;
6803 sect_offset offset = cu->per_cu->offset;
6804 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
6805 struct create_dwo_info_table_data *data = datap;
6806 struct dwo_file *dwo_file = data->dwo_file;
6807 htab_t cu_htab = data->cu_htab;
6808 void **slot;
6809 struct attribute *attr;
6810 struct dwo_unit *dwo_unit;
6811
6812 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6813 if (attr == NULL)
6814 {
6815 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
6816 " its dwo_id [in module %s]"),
6817 offset.sect_off, dwo_file->dwo_name);
6818 return;
6819 }
6820
6821 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
6822 dwo_unit->dwo_file = dwo_file;
6823 dwo_unit->signature = DW_UNSND (attr);
6824 dwo_unit->info_or_types_section = section;
6825 dwo_unit->offset = offset;
6826 dwo_unit->length = cu->per_cu->length;
6827
6828 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
6829 gdb_assert (slot != NULL);
6830 if (*slot != NULL)
6831 {
6832 const struct dwo_unit *dup_dwo_unit = *slot;
6833
6834 complaint (&symfile_complaints,
6835 _("debug entry at offset 0x%x is duplicate to the entry at"
6836 " offset 0x%x, dwo_id 0x%s [in module %s]"),
6837 offset.sect_off, dup_dwo_unit->offset.sect_off,
6838 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
6839 dwo_file->dwo_name);
6840 }
6841 else
6842 *slot = dwo_unit;
6843
6844 if (dwarf2_die_debug)
6845 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
6846 offset.sect_off,
6847 phex (dwo_unit->signature,
6848 sizeof (dwo_unit->signature)));
6849}
6850
6851/* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
6852
6853static htab_t
6854create_debug_info_hash_table (struct dwo_file *dwo_file)
6855{
6856 struct objfile *objfile = dwarf2_per_objfile->objfile;
6857 struct dwarf2_section_info *section = &dwo_file->sections.info;
6858 bfd *abfd;
6859 htab_t cu_htab;
6860 gdb_byte *info_ptr, *end_ptr;
6861 struct create_dwo_info_table_data create_dwo_info_table_data;
6862
6863 dwarf2_read_section (objfile, section);
6864 info_ptr = section->buffer;
6865
6866 if (info_ptr == NULL)
6867 return NULL;
6868
6869 /* We can't set abfd until now because the section may be empty or
6870 not present, in which case section->asection will be NULL. */
6871 abfd = section->asection->owner;
6872
6873 if (dwarf2_die_debug)
6874 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
6875 bfd_get_filename (abfd));
6876
6877 cu_htab = allocate_dwo_unit_table (objfile);
6878
6879 create_dwo_info_table_data.dwo_file = dwo_file;
6880 create_dwo_info_table_data.cu_htab = cu_htab;
6881
6882 end_ptr = info_ptr + section->size;
6883 while (info_ptr < end_ptr)
6884 {
6885 struct dwarf2_per_cu_data per_cu;
6886
6887 memset (&per_cu, 0, sizeof (per_cu));
6888 per_cu.objfile = objfile;
6889 per_cu.is_debug_types = 0;
6890 per_cu.offset.sect_off = info_ptr - section->buffer;
6891 per_cu.info_or_types_section = section;
6892
6893 init_cutu_and_read_dies_no_follow (&per_cu,
6894 &dwo_file->sections.abbrev,
6895 dwo_file,
6896 create_debug_info_hash_table_reader,
6897 &create_dwo_info_table_data);
6898
6899 info_ptr += per_cu.length;
6900 }
6901
6902 return cu_htab;
6903}
6904
6905/* Subroutine of open_dwo_file to simplify it.
6906 Open the file specified by FILE_NAME and hand it off to BFD for
6907 preliminary analysis. Return a newly initialized bfd *, which
6908 includes a canonicalized copy of FILE_NAME.
6909 In case of trouble, return NULL.
6910 NOTE: This function is derived from symfile_bfd_open. */
6911
6912static bfd *
6913try_open_dwo_file (const char *file_name)
6914{
6915 bfd *sym_bfd;
6916 int desc;
6917 char *absolute_name;
6918 char *name;
6919
6920 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
6921 O_RDONLY | O_BINARY, &absolute_name);
6922 if (desc < 0)
6923 return NULL;
6924
6925 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
6926 if (!sym_bfd)
6927 {
6928 close (desc);
6929 xfree (absolute_name);
6930 return NULL;
6931 }
6932 bfd_set_cacheable (sym_bfd, 1);
6933
6934 if (!bfd_check_format (sym_bfd, bfd_object))
6935 {
6936 bfd_close (sym_bfd); /* This also closes desc. */
6937 xfree (absolute_name);
6938 return NULL;
6939 }
6940
6941 /* bfd_usrdata exists for applications and libbfd must not touch it. */
6942 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
6943
6944 return sym_bfd;
6945}
6946
6947/* Try to open DWO file DWO_NAME.
6948 COMP_DIR is the DW_AT_comp_dir attribute.
6949 The result is the bfd handle of the file.
6950 If there is a problem finding or opening the file, return NULL.
6951 Upon success, the canonicalized path of the file is stored in the bfd,
6952 same as symfile_bfd_open. */
6953
6954static bfd *
6955open_dwo_file (const char *dwo_name, const char *comp_dir)
6956{
6957 bfd *abfd;
6958 char *path_to_try, *debug_dir;
6959
6960 if (IS_ABSOLUTE_PATH (dwo_name))
6961 return try_open_dwo_file (dwo_name);
6962
6963 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
6964
6965 if (comp_dir != NULL)
6966 {
6967 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
6968
6969 /* NOTE: If comp_dir is a relative path, this will also try the
6970 search path, which seems useful. */
6971 abfd = try_open_dwo_file (path_to_try);
6972 xfree (path_to_try);
6973 if (abfd != NULL)
6974 return abfd;
6975 }
6976
6977 /* That didn't work, try debug-file-directory, which, despite its name,
6978 is a list of paths. */
6979
6980 if (*debug_file_directory == '\0')
6981 return NULL;
6982
6983 return try_open_dwo_file (dwo_name);
6984}
6985
6986/* Initialize the use of the DWO file specified by DWO_NAME. */
6987
6988static struct dwo_file *
6989init_dwo_file (const char *dwo_name, const char *comp_dir)
6990{
6991 struct objfile *objfile = dwarf2_per_objfile->objfile;
6992 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6993 struct dwo_file);
6994 bfd *abfd;
6995 struct cleanup *cleanups;
6996
6997 if (dwarf2_die_debug)
6998 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
6999
7000 abfd = open_dwo_file (dwo_name, comp_dir);
7001 if (abfd == NULL)
7002 return NULL;
7003 dwo_file->dwo_name = dwo_name;
7004 dwo_file->dwo_bfd = abfd;
7005
7006 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7007
7008 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7009
7010 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7011
7012 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7013 dwo_file->sections.types);
7014
7015 discard_cleanups (cleanups);
7016
7017 return dwo_file;
7018}
7019
7020/* Lookup DWO file DWO_NAME. */
7021
7022static struct dwo_file *
7023lookup_dwo_file (char *dwo_name, const char *comp_dir)
7024{
7025 struct dwo_file *dwo_file;
7026 struct dwo_file find_entry;
7027 void **slot;
7028
7029 if (dwarf2_per_objfile->dwo_files == NULL)
7030 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7031
7032 /* Have we already seen this DWO file? */
7033 find_entry.dwo_name = dwo_name;
7034 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7035
7036 /* If not, read it in and build a table of the DWOs it contains. */
7037 if (*slot == NULL)
7038 *slot = init_dwo_file (dwo_name, comp_dir);
7039
7040 /* NOTE: This will be NULL if unable to open the file. */
7041 dwo_file = *slot;
7042
7043 return dwo_file;
7044}
7045
7046/* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7047 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7048 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7049 nomenclature as TUs).
7050 The result is the DWO CU or NULL if we didn't find it
7051 (dwo_id mismatch or couldn't find the DWO file). */
7052
7053static struct dwo_unit *
7054lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7055 char *dwo_name, const char *comp_dir,
7056 ULONGEST signature)
7057{
7058 struct objfile *objfile = dwarf2_per_objfile->objfile;
7059 struct dwo_file *dwo_file;
7060
7061 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7062 if (dwo_file == NULL)
7063 return NULL;
7064
7065 /* Look up the DWO using its signature(dwo_id). */
7066
7067 if (dwo_file->cus != NULL)
7068 {
7069 struct dwo_unit find_dwo_cu, *dwo_cu;
7070
7071 find_dwo_cu.signature = signature;
7072 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
a766d390 7073
3019eac3
DE
7074 if (dwo_cu != NULL)
7075 return dwo_cu;
7076 }
c906108c 7077
3019eac3 7078 /* We didn't find it. This must mean a dwo_id mismatch. */
df8a16a1 7079
3019eac3
DE
7080 complaint (&symfile_complaints,
7081 _("Could not find DWO CU referenced by CU at offset 0x%x"
7082 " [in module %s]"),
7083 this_cu->offset.sect_off, objfile->name);
7084 return NULL;
7085}
c906108c 7086
3019eac3
DE
7087/* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7088 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7089 The result is the DWO CU or NULL if we didn't find it
7090 (dwo_id mismatch or couldn't find the DWO file). */
debd256d 7091
3019eac3
DE
7092static struct dwo_unit *
7093lookup_dwo_type_unit (struct signatured_type *this_tu,
7094 char *dwo_name, const char *comp_dir)
7095{
7096 struct objfile *objfile = dwarf2_per_objfile->objfile;
7097 struct dwo_file *dwo_file;
7098 struct dwo_unit find_dwo_tu, *dwo_tu;
cb1df416 7099
3019eac3
DE
7100 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7101 if (dwo_file == NULL)
7102 return NULL;
cf2c3c16 7103
3019eac3
DE
7104 /* Look up the DWO using its signature(dwo_id). */
7105
7106 if (dwo_file->tus != NULL)
cf2c3c16 7107 {
3019eac3 7108 struct dwo_unit find_dwo_tu, *dwo_tu;
9a619af0 7109
3019eac3
DE
7110 find_dwo_tu.signature = this_tu->signature;
7111 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7112
7113 if (dwo_tu != NULL)
7114 return dwo_tu;
2e276125 7115 }
9cdd5dbd 7116
3019eac3
DE
7117 /* We didn't find it. This must mean a dwo_id mismatch. */
7118
7119 complaint (&symfile_complaints,
7120 _("Could not find DWO TU referenced by TU at offset 0x%x"
7121 " [in module %s]"),
7122 this_tu->per_cu.offset.sect_off, objfile->name);
7123 return NULL;
5fb290d7
DJ
7124}
7125
3019eac3
DE
7126/* Free all resources associated with DWO_FILE.
7127 Close the DWO file and munmap the sections.
7128 All memory should be on the objfile obstack. */
348e048f
DE
7129
7130static void
3019eac3 7131free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 7132{
3019eac3
DE
7133 int ix;
7134 struct dwarf2_section_info *section;
348e048f 7135
3019eac3
DE
7136 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7137 bfd_close (dwo_file->dwo_bfd);
348e048f 7138
3019eac3
DE
7139 munmap_section_buffer (&dwo_file->sections.abbrev);
7140 munmap_section_buffer (&dwo_file->sections.info);
7141 munmap_section_buffer (&dwo_file->sections.line);
7142 munmap_section_buffer (&dwo_file->sections.loc);
7143 munmap_section_buffer (&dwo_file->sections.str);
7144 munmap_section_buffer (&dwo_file->sections.str_offsets);
348e048f 7145
3019eac3
DE
7146 for (ix = 0;
7147 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7148 ix, section);
7149 ++ix)
7150 munmap_section_buffer (section);
348e048f 7151
3019eac3
DE
7152 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7153}
348e048f 7154
3019eac3 7155/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 7156
3019eac3
DE
7157static void
7158free_dwo_file_cleanup (void *arg)
7159{
7160 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7161 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 7162
3019eac3
DE
7163 free_dwo_file (dwo_file, objfile);
7164}
348e048f 7165
3019eac3 7166/* Traversal function for free_dwo_files. */
2ab95328 7167
3019eac3
DE
7168static int
7169free_dwo_file_from_slot (void **slot, void *info)
7170{
7171 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7172 struct objfile *objfile = (struct objfile *) info;
348e048f 7173
3019eac3 7174 free_dwo_file (dwo_file, objfile);
348e048f 7175
3019eac3
DE
7176 return 1;
7177}
348e048f 7178
3019eac3 7179/* Free all resources associated with DWO_FILES. */
348e048f 7180
3019eac3
DE
7181static void
7182free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7183{
7184 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 7185}
3019eac3
DE
7186\f
7187/* Read in various DIEs. */
348e048f 7188
d389af10
JK
7189/* qsort helper for inherit_abstract_dies. */
7190
7191static int
7192unsigned_int_compar (const void *ap, const void *bp)
7193{
7194 unsigned int a = *(unsigned int *) ap;
7195 unsigned int b = *(unsigned int *) bp;
7196
7197 return (a > b) - (b > a);
7198}
7199
7200/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
7201 Inherit only the children of the DW_AT_abstract_origin DIE not being
7202 already referenced by DW_AT_abstract_origin from the children of the
7203 current DIE. */
d389af10
JK
7204
7205static void
7206inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7207{
7208 struct die_info *child_die;
7209 unsigned die_children_count;
7210 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
7211 sect_offset *offsets;
7212 sect_offset *offsets_end, *offsetp;
d389af10
JK
7213 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7214 struct die_info *origin_die;
7215 /* Iterator of the ORIGIN_DIE children. */
7216 struct die_info *origin_child_die;
7217 struct cleanup *cleanups;
7218 struct attribute *attr;
cd02d79d
PA
7219 struct dwarf2_cu *origin_cu;
7220 struct pending **origin_previous_list_in_scope;
d389af10
JK
7221
7222 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7223 if (!attr)
7224 return;
7225
cd02d79d
PA
7226 /* Note that following die references may follow to a die in a
7227 different cu. */
7228
7229 origin_cu = cu;
7230 origin_die = follow_die_ref (die, attr, &origin_cu);
7231
7232 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7233 symbols in. */
7234 origin_previous_list_in_scope = origin_cu->list_in_scope;
7235 origin_cu->list_in_scope = cu->list_in_scope;
7236
edb3359d
DJ
7237 if (die->tag != origin_die->tag
7238 && !(die->tag == DW_TAG_inlined_subroutine
7239 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7240 complaint (&symfile_complaints,
7241 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 7242 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
7243
7244 child_die = die->child;
7245 die_children_count = 0;
7246 while (child_die && child_die->tag)
7247 {
7248 child_die = sibling_die (child_die);
7249 die_children_count++;
7250 }
7251 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7252 cleanups = make_cleanup (xfree, offsets);
7253
7254 offsets_end = offsets;
7255 child_die = die->child;
7256 while (child_die && child_die->tag)
7257 {
c38f313d
DJ
7258 /* For each CHILD_DIE, find the corresponding child of
7259 ORIGIN_DIE. If there is more than one layer of
7260 DW_AT_abstract_origin, follow them all; there shouldn't be,
7261 but GCC versions at least through 4.4 generate this (GCC PR
7262 40573). */
7263 struct die_info *child_origin_die = child_die;
cd02d79d 7264 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 7265
c38f313d
DJ
7266 while (1)
7267 {
cd02d79d
PA
7268 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7269 child_origin_cu);
c38f313d
DJ
7270 if (attr == NULL)
7271 break;
cd02d79d
PA
7272 child_origin_die = follow_die_ref (child_origin_die, attr,
7273 &child_origin_cu);
c38f313d
DJ
7274 }
7275
d389af10
JK
7276 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7277 counterpart may exist. */
c38f313d 7278 if (child_origin_die != child_die)
d389af10 7279 {
edb3359d
DJ
7280 if (child_die->tag != child_origin_die->tag
7281 && !(child_die->tag == DW_TAG_inlined_subroutine
7282 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7283 complaint (&symfile_complaints,
7284 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7285 "different tags"), child_die->offset.sect_off,
7286 child_origin_die->offset.sect_off);
c38f313d
DJ
7287 if (child_origin_die->parent != origin_die)
7288 complaint (&symfile_complaints,
7289 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7290 "different parents"), child_die->offset.sect_off,
7291 child_origin_die->offset.sect_off);
c38f313d
DJ
7292 else
7293 *offsets_end++ = child_origin_die->offset;
d389af10
JK
7294 }
7295 child_die = sibling_die (child_die);
7296 }
7297 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7298 unsigned_int_compar);
7299 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 7300 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
7301 complaint (&symfile_complaints,
7302 _("Multiple children of DIE 0x%x refer "
7303 "to DIE 0x%x as their abstract origin"),
b64f50a1 7304 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
7305
7306 offsetp = offsets;
7307 origin_child_die = origin_die->child;
7308 while (origin_child_die && origin_child_die->tag)
7309 {
7310 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
7311 while (offsetp < offsets_end
7312 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 7313 offsetp++;
b64f50a1
JK
7314 if (offsetp >= offsets_end
7315 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
7316 {
7317 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 7318 process_die (origin_child_die, origin_cu);
d389af10
JK
7319 }
7320 origin_child_die = sibling_die (origin_child_die);
7321 }
cd02d79d 7322 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
7323
7324 do_cleanups (cleanups);
7325}
7326
c906108c 7327static void
e7c27a73 7328read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7329{
e7c27a73 7330 struct objfile *objfile = cu->objfile;
52f0bd74 7331 struct context_stack *new;
c906108c
SS
7332 CORE_ADDR lowpc;
7333 CORE_ADDR highpc;
7334 struct die_info *child_die;
edb3359d 7335 struct attribute *attr, *call_line, *call_file;
c906108c 7336 char *name;
e142c38c 7337 CORE_ADDR baseaddr;
801e3a5b 7338 struct block *block;
edb3359d 7339 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
7340 VEC (symbolp) *template_args = NULL;
7341 struct template_symbol *templ_func = NULL;
edb3359d
DJ
7342
7343 if (inlined_func)
7344 {
7345 /* If we do not have call site information, we can't show the
7346 caller of this inlined function. That's too confusing, so
7347 only use the scope for local variables. */
7348 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7349 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7350 if (call_line == NULL || call_file == NULL)
7351 {
7352 read_lexical_block_scope (die, cu);
7353 return;
7354 }
7355 }
c906108c 7356
e142c38c
DJ
7357 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7358
94af9270 7359 name = dwarf2_name (die, cu);
c906108c 7360
e8d05480
JB
7361 /* Ignore functions with missing or empty names. These are actually
7362 illegal according to the DWARF standard. */
7363 if (name == NULL)
7364 {
7365 complaint (&symfile_complaints,
b64f50a1
JK
7366 _("missing name for subprogram DIE at %d"),
7367 die->offset.sect_off);
e8d05480
JB
7368 return;
7369 }
7370
7371 /* Ignore functions with missing or invalid low and high pc attributes. */
7372 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7373 {
ae4d0c03
PM
7374 attr = dwarf2_attr (die, DW_AT_external, cu);
7375 if (!attr || !DW_UNSND (attr))
7376 complaint (&symfile_complaints,
3e43a32a
MS
7377 _("cannot get low and high bounds "
7378 "for subprogram DIE at %d"),
b64f50a1 7379 die->offset.sect_off);
e8d05480
JB
7380 return;
7381 }
c906108c
SS
7382
7383 lowpc += baseaddr;
7384 highpc += baseaddr;
7385
34eaf542
TT
7386 /* If we have any template arguments, then we must allocate a
7387 different sort of symbol. */
7388 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7389 {
7390 if (child_die->tag == DW_TAG_template_type_param
7391 || child_die->tag == DW_TAG_template_value_param)
7392 {
7393 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7394 struct template_symbol);
7395 templ_func->base.is_cplus_template_function = 1;
7396 break;
7397 }
7398 }
7399
c906108c 7400 new = push_context (0, lowpc);
34eaf542
TT
7401 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7402 (struct symbol *) templ_func);
4c2df51b 7403
4cecd739
DJ
7404 /* If there is a location expression for DW_AT_frame_base, record
7405 it. */
e142c38c 7406 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 7407 if (attr)
c034e007
AC
7408 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7409 expression is being recorded directly in the function's symbol
7410 and not in a separate frame-base object. I guess this hack is
7411 to avoid adding some sort of frame-base adjunct/annex to the
7412 function's symbol :-(. The problem with doing this is that it
7413 results in a function symbol with a location expression that
7414 has nothing to do with the location of the function, ouch! The
7415 relationship should be: a function's symbol has-a frame base; a
7416 frame-base has-a location expression. */
e7c27a73 7417 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 7418
e142c38c 7419 cu->list_in_scope = &local_symbols;
c906108c 7420
639d11d3 7421 if (die->child != NULL)
c906108c 7422 {
639d11d3 7423 child_die = die->child;
c906108c
SS
7424 while (child_die && child_die->tag)
7425 {
34eaf542
TT
7426 if (child_die->tag == DW_TAG_template_type_param
7427 || child_die->tag == DW_TAG_template_value_param)
7428 {
7429 struct symbol *arg = new_symbol (child_die, NULL, cu);
7430
f1078f66
DJ
7431 if (arg != NULL)
7432 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7433 }
7434 else
7435 process_die (child_die, cu);
c906108c
SS
7436 child_die = sibling_die (child_die);
7437 }
7438 }
7439
d389af10
JK
7440 inherit_abstract_dies (die, cu);
7441
4a811a97
UW
7442 /* If we have a DW_AT_specification, we might need to import using
7443 directives from the context of the specification DIE. See the
7444 comment in determine_prefix. */
7445 if (cu->language == language_cplus
7446 && dwarf2_attr (die, DW_AT_specification, cu))
7447 {
7448 struct dwarf2_cu *spec_cu = cu;
7449 struct die_info *spec_die = die_specification (die, &spec_cu);
7450
7451 while (spec_die)
7452 {
7453 child_die = spec_die->child;
7454 while (child_die && child_die->tag)
7455 {
7456 if (child_die->tag == DW_TAG_imported_module)
7457 process_die (child_die, spec_cu);
7458 child_die = sibling_die (child_die);
7459 }
7460
7461 /* In some cases, GCC generates specification DIEs that
7462 themselves contain DW_AT_specification attributes. */
7463 spec_die = die_specification (spec_die, &spec_cu);
7464 }
7465 }
7466
c906108c
SS
7467 new = pop_context ();
7468 /* Make a block for the local symbols within. */
801e3a5b
JB
7469 block = finish_block (new->name, &local_symbols, new->old_blocks,
7470 lowpc, highpc, objfile);
7471
df8a16a1 7472 /* For C++, set the block's scope. */
f55ee35c 7473 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 7474 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 7475 determine_prefix (die, cu),
df8a16a1
DJ
7476 processing_has_namespace_info);
7477
801e3a5b
JB
7478 /* If we have address ranges, record them. */
7479 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 7480
34eaf542
TT
7481 /* Attach template arguments to function. */
7482 if (! VEC_empty (symbolp, template_args))
7483 {
7484 gdb_assert (templ_func != NULL);
7485
7486 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7487 templ_func->template_arguments
7488 = obstack_alloc (&objfile->objfile_obstack,
7489 (templ_func->n_template_arguments
7490 * sizeof (struct symbol *)));
7491 memcpy (templ_func->template_arguments,
7492 VEC_address (symbolp, template_args),
7493 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7494 VEC_free (symbolp, template_args);
7495 }
7496
208d8187
JB
7497 /* In C++, we can have functions nested inside functions (e.g., when
7498 a function declares a class that has methods). This means that
7499 when we finish processing a function scope, we may need to go
7500 back to building a containing block's symbol lists. */
7501 local_symbols = new->locals;
7502 param_symbols = new->params;
27aa8d6a 7503 using_directives = new->using_directives;
208d8187 7504
921e78cf
JB
7505 /* If we've finished processing a top-level function, subsequent
7506 symbols go in the file symbol list. */
7507 if (outermost_context_p ())
e142c38c 7508 cu->list_in_scope = &file_symbols;
c906108c
SS
7509}
7510
7511/* Process all the DIES contained within a lexical block scope. Start
7512 a new scope, process the dies, and then close the scope. */
7513
7514static void
e7c27a73 7515read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7516{
e7c27a73 7517 struct objfile *objfile = cu->objfile;
52f0bd74 7518 struct context_stack *new;
c906108c
SS
7519 CORE_ADDR lowpc, highpc;
7520 struct die_info *child_die;
e142c38c
DJ
7521 CORE_ADDR baseaddr;
7522
7523 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
7524
7525 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
7526 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7527 as multiple lexical blocks? Handling children in a sane way would
6e70227d 7528 be nasty. Might be easier to properly extend generic blocks to
af34e669 7529 describe ranges. */
d85a05f0 7530 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
7531 return;
7532 lowpc += baseaddr;
7533 highpc += baseaddr;
7534
7535 push_context (0, lowpc);
639d11d3 7536 if (die->child != NULL)
c906108c 7537 {
639d11d3 7538 child_die = die->child;
c906108c
SS
7539 while (child_die && child_die->tag)
7540 {
e7c27a73 7541 process_die (child_die, cu);
c906108c
SS
7542 child_die = sibling_die (child_die);
7543 }
7544 }
7545 new = pop_context ();
7546
8540c487 7547 if (local_symbols != NULL || using_directives != NULL)
c906108c 7548 {
801e3a5b
JB
7549 struct block *block
7550 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7551 highpc, objfile);
7552
7553 /* Note that recording ranges after traversing children, as we
7554 do here, means that recording a parent's ranges entails
7555 walking across all its children's ranges as they appear in
7556 the address map, which is quadratic behavior.
7557
7558 It would be nicer to record the parent's ranges before
7559 traversing its children, simply overriding whatever you find
7560 there. But since we don't even decide whether to create a
7561 block until after we've traversed its children, that's hard
7562 to do. */
7563 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
7564 }
7565 local_symbols = new->locals;
27aa8d6a 7566 using_directives = new->using_directives;
c906108c
SS
7567}
7568
96408a79
SA
7569/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7570
7571static void
7572read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7573{
7574 struct objfile *objfile = cu->objfile;
7575 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7576 CORE_ADDR pc, baseaddr;
7577 struct attribute *attr;
7578 struct call_site *call_site, call_site_local;
7579 void **slot;
7580 int nparams;
7581 struct die_info *child_die;
7582
7583 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7584
7585 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7586 if (!attr)
7587 {
7588 complaint (&symfile_complaints,
7589 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7590 "DIE 0x%x [in module %s]"),
b64f50a1 7591 die->offset.sect_off, objfile->name);
96408a79
SA
7592 return;
7593 }
7594 pc = DW_ADDR (attr) + baseaddr;
7595
7596 if (cu->call_site_htab == NULL)
7597 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7598 NULL, &objfile->objfile_obstack,
7599 hashtab_obstack_allocate, NULL);
7600 call_site_local.pc = pc;
7601 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7602 if (*slot != NULL)
7603 {
7604 complaint (&symfile_complaints,
7605 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7606 "DIE 0x%x [in module %s]"),
b64f50a1 7607 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
7608 return;
7609 }
7610
7611 /* Count parameters at the caller. */
7612
7613 nparams = 0;
7614 for (child_die = die->child; child_die && child_die->tag;
7615 child_die = sibling_die (child_die))
7616 {
7617 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7618 {
7619 complaint (&symfile_complaints,
7620 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7621 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7622 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
7623 continue;
7624 }
7625
7626 nparams++;
7627 }
7628
7629 call_site = obstack_alloc (&objfile->objfile_obstack,
7630 (sizeof (*call_site)
7631 + (sizeof (*call_site->parameter)
7632 * (nparams - 1))));
7633 *slot = call_site;
7634 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7635 call_site->pc = pc;
7636
7637 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7638 {
7639 struct die_info *func_die;
7640
7641 /* Skip also over DW_TAG_inlined_subroutine. */
7642 for (func_die = die->parent;
7643 func_die && func_die->tag != DW_TAG_subprogram
7644 && func_die->tag != DW_TAG_subroutine_type;
7645 func_die = func_die->parent);
7646
7647 /* DW_AT_GNU_all_call_sites is a superset
7648 of DW_AT_GNU_all_tail_call_sites. */
7649 if (func_die
7650 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7651 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7652 {
7653 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7654 not complete. But keep CALL_SITE for look ups via call_site_htab,
7655 both the initial caller containing the real return address PC and
7656 the final callee containing the current PC of a chain of tail
7657 calls do not need to have the tail call list complete. But any
7658 function candidate for a virtual tail call frame searched via
7659 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7660 determined unambiguously. */
7661 }
7662 else
7663 {
7664 struct type *func_type = NULL;
7665
7666 if (func_die)
7667 func_type = get_die_type (func_die, cu);
7668 if (func_type != NULL)
7669 {
7670 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7671
7672 /* Enlist this call site to the function. */
7673 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7674 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7675 }
7676 else
7677 complaint (&symfile_complaints,
7678 _("Cannot find function owning DW_TAG_GNU_call_site "
7679 "DIE 0x%x [in module %s]"),
b64f50a1 7680 die->offset.sect_off, objfile->name);
96408a79
SA
7681 }
7682 }
7683
7684 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7685 if (attr == NULL)
7686 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7687 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7688 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7689 /* Keep NULL DWARF_BLOCK. */;
7690 else if (attr_form_is_block (attr))
7691 {
7692 struct dwarf2_locexpr_baton *dlbaton;
7693
7694 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7695 dlbaton->data = DW_BLOCK (attr)->data;
7696 dlbaton->size = DW_BLOCK (attr)->size;
7697 dlbaton->per_cu = cu->per_cu;
7698
7699 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7700 }
7701 else if (is_ref_attr (attr))
7702 {
96408a79
SA
7703 struct dwarf2_cu *target_cu = cu;
7704 struct die_info *target_die;
7705
7706 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7707 gdb_assert (target_cu->objfile == objfile);
7708 if (die_is_declaration (target_die, target_cu))
7709 {
7710 const char *target_physname;
7711
7712 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7713 if (target_physname == NULL)
7714 complaint (&symfile_complaints,
7715 _("DW_AT_GNU_call_site_target target DIE has invalid "
7716 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 7717 die->offset.sect_off, objfile->name);
96408a79
SA
7718 else
7719 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7720 }
7721 else
7722 {
7723 CORE_ADDR lowpc;
7724
7725 /* DW_AT_entry_pc should be preferred. */
7726 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7727 complaint (&symfile_complaints,
7728 _("DW_AT_GNU_call_site_target target DIE has invalid "
7729 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 7730 die->offset.sect_off, objfile->name);
96408a79
SA
7731 else
7732 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7733 }
7734 }
7735 else
7736 complaint (&symfile_complaints,
7737 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7738 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 7739 die->offset.sect_off, objfile->name);
96408a79
SA
7740
7741 call_site->per_cu = cu->per_cu;
7742
7743 for (child_die = die->child;
7744 child_die && child_die->tag;
7745 child_die = sibling_die (child_die))
7746 {
7747 struct dwarf2_locexpr_baton *dlbaton;
7748 struct call_site_parameter *parameter;
7749
7750 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7751 {
7752 /* Already printed the complaint above. */
7753 continue;
7754 }
7755
7756 gdb_assert (call_site->parameter_count < nparams);
7757 parameter = &call_site->parameter[call_site->parameter_count];
7758
7759 /* DW_AT_location specifies the register number. Value of the data
7760 assumed for the register is contained in DW_AT_GNU_call_site_value. */
7761
7762 attr = dwarf2_attr (child_die, DW_AT_location, cu);
7763 if (!attr || !attr_form_is_block (attr))
7764 {
7765 complaint (&symfile_complaints,
7766 _("No DW_FORM_block* DW_AT_location for "
7767 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7768 child_die->offset.sect_off, objfile->name);
96408a79
SA
7769 continue;
7770 }
7771 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
7772 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
7773 if (parameter->dwarf_reg == -1
7774 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
7775 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
7776 &parameter->fb_offset))
7777 {
7778 complaint (&symfile_complaints,
7779 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
7780 "for DW_FORM_block* DW_AT_location for "
7781 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7782 child_die->offset.sect_off, objfile->name);
96408a79
SA
7783 continue;
7784 }
7785
7786 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
7787 if (!attr_form_is_block (attr))
7788 {
7789 complaint (&symfile_complaints,
7790 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
7791 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7792 child_die->offset.sect_off, objfile->name);
96408a79
SA
7793 continue;
7794 }
7795 parameter->value = DW_BLOCK (attr)->data;
7796 parameter->value_size = DW_BLOCK (attr)->size;
7797
7798 /* Parameters are not pre-cleared by memset above. */
7799 parameter->data_value = NULL;
7800 parameter->data_value_size = 0;
7801 call_site->parameter_count++;
7802
7803 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
7804 if (attr)
7805 {
7806 if (!attr_form_is_block (attr))
7807 complaint (&symfile_complaints,
7808 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
7809 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7810 child_die->offset.sect_off, objfile->name);
96408a79
SA
7811 else
7812 {
7813 parameter->data_value = DW_BLOCK (attr)->data;
7814 parameter->data_value_size = DW_BLOCK (attr)->size;
7815 }
7816 }
7817 }
7818}
7819
43039443 7820/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
7821 Return 1 if the attributes are present and valid, otherwise, return 0.
7822 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
7823
7824static int
7825dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
7826 CORE_ADDR *high_return, struct dwarf2_cu *cu,
7827 struct partial_symtab *ranges_pst)
43039443
JK
7828{
7829 struct objfile *objfile = cu->objfile;
7830 struct comp_unit_head *cu_header = &cu->header;
7831 bfd *obfd = objfile->obfd;
7832 unsigned int addr_size = cu_header->addr_size;
7833 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7834 /* Base address selection entry. */
7835 CORE_ADDR base;
7836 int found_base;
7837 unsigned int dummy;
7838 gdb_byte *buffer;
7839 CORE_ADDR marker;
7840 int low_set;
7841 CORE_ADDR low = 0;
7842 CORE_ADDR high = 0;
ff013f42 7843 CORE_ADDR baseaddr;
43039443 7844
d00adf39
DE
7845 found_base = cu->base_known;
7846 base = cu->base_address;
43039443 7847
be391dca 7848 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 7849 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
7850 {
7851 complaint (&symfile_complaints,
7852 _("Offset %d out of bounds for DW_AT_ranges attribute"),
7853 offset);
7854 return 0;
7855 }
dce234bc 7856 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
7857
7858 /* Read in the largest possible address. */
7859 marker = read_address (obfd, buffer, cu, &dummy);
7860 if ((marker & mask) == mask)
7861 {
7862 /* If we found the largest possible address, then
7863 read the base address. */
7864 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7865 buffer += 2 * addr_size;
7866 offset += 2 * addr_size;
7867 found_base = 1;
7868 }
7869
7870 low_set = 0;
7871
e7030f15 7872 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 7873
43039443
JK
7874 while (1)
7875 {
7876 CORE_ADDR range_beginning, range_end;
7877
7878 range_beginning = read_address (obfd, buffer, cu, &dummy);
7879 buffer += addr_size;
7880 range_end = read_address (obfd, buffer, cu, &dummy);
7881 buffer += addr_size;
7882 offset += 2 * addr_size;
7883
7884 /* An end of list marker is a pair of zero addresses. */
7885 if (range_beginning == 0 && range_end == 0)
7886 /* Found the end of list entry. */
7887 break;
7888
7889 /* Each base address selection entry is a pair of 2 values.
7890 The first is the largest possible address, the second is
7891 the base address. Check for a base address here. */
7892 if ((range_beginning & mask) == mask)
7893 {
7894 /* If we found the largest possible address, then
7895 read the base address. */
7896 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7897 found_base = 1;
7898 continue;
7899 }
7900
7901 if (!found_base)
7902 {
7903 /* We have no valid base address for the ranges
7904 data. */
7905 complaint (&symfile_complaints,
7906 _("Invalid .debug_ranges data (no base address)"));
7907 return 0;
7908 }
7909
9277c30c
UW
7910 if (range_beginning > range_end)
7911 {
7912 /* Inverted range entries are invalid. */
7913 complaint (&symfile_complaints,
7914 _("Invalid .debug_ranges data (inverted range)"));
7915 return 0;
7916 }
7917
7918 /* Empty range entries have no effect. */
7919 if (range_beginning == range_end)
7920 continue;
7921
43039443
JK
7922 range_beginning += base;
7923 range_end += base;
7924
9277c30c 7925 if (ranges_pst != NULL)
ff013f42 7926 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
7927 range_beginning + baseaddr,
7928 range_end - 1 + baseaddr,
ff013f42
JK
7929 ranges_pst);
7930
43039443
JK
7931 /* FIXME: This is recording everything as a low-high
7932 segment of consecutive addresses. We should have a
7933 data structure for discontiguous block ranges
7934 instead. */
7935 if (! low_set)
7936 {
7937 low = range_beginning;
7938 high = range_end;
7939 low_set = 1;
7940 }
7941 else
7942 {
7943 if (range_beginning < low)
7944 low = range_beginning;
7945 if (range_end > high)
7946 high = range_end;
7947 }
7948 }
7949
7950 if (! low_set)
7951 /* If the first entry is an end-of-list marker, the range
7952 describes an empty scope, i.e. no instructions. */
7953 return 0;
7954
7955 if (low_return)
7956 *low_return = low;
7957 if (high_return)
7958 *high_return = high;
7959 return 1;
7960}
7961
af34e669
DJ
7962/* Get low and high pc attributes from a die. Return 1 if the attributes
7963 are present and valid, otherwise, return 0. Return -1 if the range is
7964 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 7965
c906108c 7966static int
af34e669 7967dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
7968 CORE_ADDR *highpc, struct dwarf2_cu *cu,
7969 struct partial_symtab *pst)
c906108c
SS
7970{
7971 struct attribute *attr;
91da1414 7972 struct attribute *attr_high;
af34e669
DJ
7973 CORE_ADDR low = 0;
7974 CORE_ADDR high = 0;
7975 int ret = 0;
c906108c 7976
91da1414
MW
7977 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
7978 if (attr_high)
af34e669 7979 {
e142c38c 7980 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 7981 if (attr)
91da1414
MW
7982 {
7983 low = DW_ADDR (attr);
3019eac3
DE
7984 if (attr_high->form == DW_FORM_addr
7985 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
7986 high = DW_ADDR (attr_high);
7987 else
7988 high = low + DW_UNSND (attr_high);
7989 }
af34e669
DJ
7990 else
7991 /* Found high w/o low attribute. */
7992 return 0;
7993
7994 /* Found consecutive range of addresses. */
7995 ret = 1;
7996 }
c906108c 7997 else
af34e669 7998 {
e142c38c 7999 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
8000 if (attr != NULL)
8001 {
af34e669 8002 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 8003 .debug_ranges section. */
d85a05f0 8004 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 8005 return 0;
43039443 8006 /* Found discontinuous range of addresses. */
af34e669
DJ
8007 ret = -1;
8008 }
8009 }
c906108c 8010
9373cf26
JK
8011 /* read_partial_die has also the strict LOW < HIGH requirement. */
8012 if (high <= low)
c906108c
SS
8013 return 0;
8014
8015 /* When using the GNU linker, .gnu.linkonce. sections are used to
8016 eliminate duplicate copies of functions and vtables and such.
8017 The linker will arbitrarily choose one and discard the others.
8018 The AT_*_pc values for such functions refer to local labels in
8019 these sections. If the section from that file was discarded, the
8020 labels are not in the output, so the relocs get a value of 0.
8021 If this is a discarded function, mark the pc bounds as invalid,
8022 so that GDB will ignore it. */
72dca2f5 8023 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
8024 return 0;
8025
8026 *lowpc = low;
96408a79
SA
8027 if (highpc)
8028 *highpc = high;
af34e669 8029 return ret;
c906108c
SS
8030}
8031
b084d499
JB
8032/* Assuming that DIE represents a subprogram DIE or a lexical block, get
8033 its low and high PC addresses. Do nothing if these addresses could not
8034 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8035 and HIGHPC to the high address if greater than HIGHPC. */
8036
8037static void
8038dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8039 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8040 struct dwarf2_cu *cu)
8041{
8042 CORE_ADDR low, high;
8043 struct die_info *child = die->child;
8044
d85a05f0 8045 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
8046 {
8047 *lowpc = min (*lowpc, low);
8048 *highpc = max (*highpc, high);
8049 }
8050
8051 /* If the language does not allow nested subprograms (either inside
8052 subprograms or lexical blocks), we're done. */
8053 if (cu->language != language_ada)
8054 return;
6e70227d 8055
b084d499
JB
8056 /* Check all the children of the given DIE. If it contains nested
8057 subprograms, then check their pc bounds. Likewise, we need to
8058 check lexical blocks as well, as they may also contain subprogram
8059 definitions. */
8060 while (child && child->tag)
8061 {
8062 if (child->tag == DW_TAG_subprogram
8063 || child->tag == DW_TAG_lexical_block)
8064 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8065 child = sibling_die (child);
8066 }
8067}
8068
fae299cd
DC
8069/* Get the low and high pc's represented by the scope DIE, and store
8070 them in *LOWPC and *HIGHPC. If the correct values can't be
8071 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8072
8073static void
8074get_scope_pc_bounds (struct die_info *die,
8075 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8076 struct dwarf2_cu *cu)
8077{
8078 CORE_ADDR best_low = (CORE_ADDR) -1;
8079 CORE_ADDR best_high = (CORE_ADDR) 0;
8080 CORE_ADDR current_low, current_high;
8081
d85a05f0 8082 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
8083 {
8084 best_low = current_low;
8085 best_high = current_high;
8086 }
8087 else
8088 {
8089 struct die_info *child = die->child;
8090
8091 while (child && child->tag)
8092 {
8093 switch (child->tag) {
8094 case DW_TAG_subprogram:
b084d499 8095 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
8096 break;
8097 case DW_TAG_namespace:
f55ee35c 8098 case DW_TAG_module:
fae299cd
DC
8099 /* FIXME: carlton/2004-01-16: Should we do this for
8100 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8101 that current GCC's always emit the DIEs corresponding
8102 to definitions of methods of classes as children of a
8103 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8104 the DIEs giving the declarations, which could be
8105 anywhere). But I don't see any reason why the
8106 standards says that they have to be there. */
8107 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8108
8109 if (current_low != ((CORE_ADDR) -1))
8110 {
8111 best_low = min (best_low, current_low);
8112 best_high = max (best_high, current_high);
8113 }
8114 break;
8115 default:
0963b4bd 8116 /* Ignore. */
fae299cd
DC
8117 break;
8118 }
8119
8120 child = sibling_die (child);
8121 }
8122 }
8123
8124 *lowpc = best_low;
8125 *highpc = best_high;
8126}
8127
801e3a5b
JB
8128/* Record the address ranges for BLOCK, offset by BASEADDR, as given
8129 in DIE. */
380bca97 8130
801e3a5b
JB
8131static void
8132dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8133 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8134{
bb5ed363 8135 struct objfile *objfile = cu->objfile;
801e3a5b 8136 struct attribute *attr;
91da1414 8137 struct attribute *attr_high;
801e3a5b 8138
91da1414
MW
8139 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8140 if (attr_high)
801e3a5b 8141 {
801e3a5b
JB
8142 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8143 if (attr)
8144 {
8145 CORE_ADDR low = DW_ADDR (attr);
91da1414 8146 CORE_ADDR high;
3019eac3
DE
8147 if (attr_high->form == DW_FORM_addr
8148 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8149 high = DW_ADDR (attr_high);
8150 else
8151 high = low + DW_UNSND (attr_high);
9a619af0 8152
801e3a5b
JB
8153 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8154 }
8155 }
8156
8157 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8158 if (attr)
8159 {
bb5ed363 8160 bfd *obfd = objfile->obfd;
801e3a5b
JB
8161
8162 /* The value of the DW_AT_ranges attribute is the offset of the
8163 address range list in the .debug_ranges section. */
8164 unsigned long offset = DW_UNSND (attr);
dce234bc 8165 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
8166
8167 /* For some target architectures, but not others, the
8168 read_address function sign-extends the addresses it returns.
8169 To recognize base address selection entries, we need a
8170 mask. */
8171 unsigned int addr_size = cu->header.addr_size;
8172 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8173
8174 /* The base address, to which the next pair is relative. Note
8175 that this 'base' is a DWARF concept: most entries in a range
8176 list are relative, to reduce the number of relocs against the
8177 debugging information. This is separate from this function's
8178 'baseaddr' argument, which GDB uses to relocate debugging
8179 information from a shared library based on the address at
8180 which the library was loaded. */
d00adf39
DE
8181 CORE_ADDR base = cu->base_address;
8182 int base_known = cu->base_known;
801e3a5b 8183
be391dca 8184 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 8185 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
8186 {
8187 complaint (&symfile_complaints,
8188 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8189 offset);
8190 return;
8191 }
8192
8193 for (;;)
8194 {
8195 unsigned int bytes_read;
8196 CORE_ADDR start, end;
8197
8198 start = read_address (obfd, buffer, cu, &bytes_read);
8199 buffer += bytes_read;
8200 end = read_address (obfd, buffer, cu, &bytes_read);
8201 buffer += bytes_read;
8202
8203 /* Did we find the end of the range list? */
8204 if (start == 0 && end == 0)
8205 break;
8206
8207 /* Did we find a base address selection entry? */
8208 else if ((start & base_select_mask) == base_select_mask)
8209 {
8210 base = end;
8211 base_known = 1;
8212 }
8213
8214 /* We found an ordinary address range. */
8215 else
8216 {
8217 if (!base_known)
8218 {
8219 complaint (&symfile_complaints,
3e43a32a
MS
8220 _("Invalid .debug_ranges data "
8221 "(no base address)"));
801e3a5b
JB
8222 return;
8223 }
8224
9277c30c
UW
8225 if (start > end)
8226 {
8227 /* Inverted range entries are invalid. */
8228 complaint (&symfile_complaints,
8229 _("Invalid .debug_ranges data "
8230 "(inverted range)"));
8231 return;
8232 }
8233
8234 /* Empty range entries have no effect. */
8235 if (start == end)
8236 continue;
8237
6e70227d
DE
8238 record_block_range (block,
8239 baseaddr + base + start,
801e3a5b
JB
8240 baseaddr + base + end - 1);
8241 }
8242 }
8243 }
8244}
8245
60d5a603
JK
8246/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8247 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8248 during 4.6.0 experimental. */
8249
8250static int
8251producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8252{
8253 const char *cs;
8254 int major, minor, release;
ba919b58 8255 int result = 0;
60d5a603
JK
8256
8257 if (cu->producer == NULL)
8258 {
8259 /* For unknown compilers expect their behavior is DWARF version
8260 compliant.
8261
8262 GCC started to support .debug_types sections by -gdwarf-4 since
8263 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8264 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8265 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8266 interpreted incorrectly by GDB now - GCC PR debug/48229. */
8267
8268 return 0;
8269 }
8270
ba919b58
TT
8271 if (cu->checked_producer)
8272 return cu->producer_is_gxx_lt_4_6;
8273
60d5a603
JK
8274 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8275
8276 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
8277 {
8278 /* For non-GCC compilers expect their behavior is DWARF version
8279 compliant. */
60d5a603 8280 }
ba919b58 8281 else
60d5a603 8282 {
ba919b58
TT
8283 cs = &cu->producer[strlen ("GNU ")];
8284 while (*cs && !isdigit (*cs))
8285 cs++;
8286 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8287 {
8288 /* Not recognized as GCC. */
8289 }
8290 else
8291 result = major < 4 || (major == 4 && minor < 6);
60d5a603
JK
8292 }
8293
ba919b58
TT
8294 cu->checked_producer = 1;
8295 cu->producer_is_gxx_lt_4_6 = result;
8296
8297 return result;
60d5a603
JK
8298}
8299
8300/* Return the default accessibility type if it is not overriden by
8301 DW_AT_accessibility. */
8302
8303static enum dwarf_access_attribute
8304dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8305{
8306 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8307 {
8308 /* The default DWARF 2 accessibility for members is public, the default
8309 accessibility for inheritance is private. */
8310
8311 if (die->tag != DW_TAG_inheritance)
8312 return DW_ACCESS_public;
8313 else
8314 return DW_ACCESS_private;
8315 }
8316 else
8317 {
8318 /* DWARF 3+ defines the default accessibility a different way. The same
8319 rules apply now for DW_TAG_inheritance as for the members and it only
8320 depends on the container kind. */
8321
8322 if (die->parent->tag == DW_TAG_class_type)
8323 return DW_ACCESS_private;
8324 else
8325 return DW_ACCESS_public;
8326 }
8327}
8328
74ac6d43
TT
8329/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8330 offset. If the attribute was not found return 0, otherwise return
8331 1. If it was found but could not properly be handled, set *OFFSET
8332 to 0. */
8333
8334static int
8335handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8336 LONGEST *offset)
8337{
8338 struct attribute *attr;
8339
8340 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8341 if (attr != NULL)
8342 {
8343 *offset = 0;
8344
8345 /* Note that we do not check for a section offset first here.
8346 This is because DW_AT_data_member_location is new in DWARF 4,
8347 so if we see it, we can assume that a constant form is really
8348 a constant and not a section offset. */
8349 if (attr_form_is_constant (attr))
8350 *offset = dwarf2_get_attr_constant_value (attr, 0);
8351 else if (attr_form_is_section_offset (attr))
8352 dwarf2_complex_location_expr_complaint ();
8353 else if (attr_form_is_block (attr))
8354 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8355 else
8356 dwarf2_complex_location_expr_complaint ();
8357
8358 return 1;
8359 }
8360
8361 return 0;
8362}
8363
c906108c
SS
8364/* Add an aggregate field to the field list. */
8365
8366static void
107d2387 8367dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 8368 struct dwarf2_cu *cu)
6e70227d 8369{
e7c27a73 8370 struct objfile *objfile = cu->objfile;
5e2b427d 8371 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8372 struct nextfield *new_field;
8373 struct attribute *attr;
8374 struct field *fp;
8375 char *fieldname = "";
8376
8377 /* Allocate a new field list entry and link it in. */
8378 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 8379 make_cleanup (xfree, new_field);
c906108c 8380 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
8381
8382 if (die->tag == DW_TAG_inheritance)
8383 {
8384 new_field->next = fip->baseclasses;
8385 fip->baseclasses = new_field;
8386 }
8387 else
8388 {
8389 new_field->next = fip->fields;
8390 fip->fields = new_field;
8391 }
c906108c
SS
8392 fip->nfields++;
8393
e142c38c 8394 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
8395 if (attr)
8396 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
8397 else
8398 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
8399 if (new_field->accessibility != DW_ACCESS_public)
8400 fip->non_public_fields = 1;
60d5a603 8401
e142c38c 8402 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
8403 if (attr)
8404 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
8405 else
8406 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
8407
8408 fp = &new_field->field;
a9a9bd0f 8409
e142c38c 8410 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 8411 {
74ac6d43
TT
8412 LONGEST offset;
8413
a9a9bd0f 8414 /* Data member other than a C++ static data member. */
6e70227d 8415
c906108c 8416 /* Get type of field. */
e7c27a73 8417 fp->type = die_type (die, cu);
c906108c 8418
d6a843b5 8419 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 8420
c906108c 8421 /* Get bit size of field (zero if none). */
e142c38c 8422 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
8423 if (attr)
8424 {
8425 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8426 }
8427 else
8428 {
8429 FIELD_BITSIZE (*fp) = 0;
8430 }
8431
8432 /* Get bit offset of field. */
74ac6d43
TT
8433 if (handle_data_member_location (die, cu, &offset))
8434 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 8435 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
8436 if (attr)
8437 {
5e2b427d 8438 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
8439 {
8440 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
8441 additional bit offset from the MSB of the containing
8442 anonymous object to the MSB of the field. We don't
8443 have to do anything special since we don't need to
8444 know the size of the anonymous object. */
f41f5e61 8445 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
8446 }
8447 else
8448 {
8449 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
8450 MSB of the anonymous object, subtract off the number of
8451 bits from the MSB of the field to the MSB of the
8452 object, and then subtract off the number of bits of
8453 the field itself. The result is the bit offset of
8454 the LSB of the field. */
c906108c
SS
8455 int anonymous_size;
8456 int bit_offset = DW_UNSND (attr);
8457
e142c38c 8458 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8459 if (attr)
8460 {
8461 /* The size of the anonymous object containing
8462 the bit field is explicit, so use the
8463 indicated size (in bytes). */
8464 anonymous_size = DW_UNSND (attr);
8465 }
8466 else
8467 {
8468 /* The size of the anonymous object containing
8469 the bit field must be inferred from the type
8470 attribute of the data member containing the
8471 bit field. */
8472 anonymous_size = TYPE_LENGTH (fp->type);
8473 }
f41f5e61
PA
8474 SET_FIELD_BITPOS (*fp,
8475 (FIELD_BITPOS (*fp)
8476 + anonymous_size * bits_per_byte
8477 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
8478 }
8479 }
8480
8481 /* Get name of field. */
39cbfefa
DJ
8482 fieldname = dwarf2_name (die, cu);
8483 if (fieldname == NULL)
8484 fieldname = "";
d8151005
DJ
8485
8486 /* The name is already allocated along with this objfile, so we don't
8487 need to duplicate it for the type. */
8488 fp->name = fieldname;
c906108c
SS
8489
8490 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 8491 pointer or virtual base class pointer) to private. */
e142c38c 8492 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 8493 {
d48cc9dd 8494 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
8495 new_field->accessibility = DW_ACCESS_private;
8496 fip->non_public_fields = 1;
8497 }
8498 }
a9a9bd0f 8499 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 8500 {
a9a9bd0f
DC
8501 /* C++ static member. */
8502
8503 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8504 is a declaration, but all versions of G++ as of this writing
8505 (so through at least 3.2.1) incorrectly generate
8506 DW_TAG_variable tags. */
6e70227d 8507
ff355380 8508 const char *physname;
c906108c 8509
a9a9bd0f 8510 /* Get name of field. */
39cbfefa
DJ
8511 fieldname = dwarf2_name (die, cu);
8512 if (fieldname == NULL)
c906108c
SS
8513 return;
8514
254e6b9e 8515 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
8516 if (attr
8517 /* Only create a symbol if this is an external value.
8518 new_symbol checks this and puts the value in the global symbol
8519 table, which we want. If it is not external, new_symbol
8520 will try to put the value in cu->list_in_scope which is wrong. */
8521 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
8522 {
8523 /* A static const member, not much different than an enum as far as
8524 we're concerned, except that we can support more types. */
8525 new_symbol (die, NULL, cu);
8526 }
8527
2df3850c 8528 /* Get physical name. */
ff355380 8529 physname = dwarf2_physname (fieldname, die, cu);
c906108c 8530
d8151005
DJ
8531 /* The name is already allocated along with this objfile, so we don't
8532 need to duplicate it for the type. */
8533 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 8534 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 8535 FIELD_NAME (*fp) = fieldname;
c906108c
SS
8536 }
8537 else if (die->tag == DW_TAG_inheritance)
8538 {
74ac6d43 8539 LONGEST offset;
d4b96c9a 8540
74ac6d43
TT
8541 /* C++ base class field. */
8542 if (handle_data_member_location (die, cu, &offset))
8543 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 8544 FIELD_BITSIZE (*fp) = 0;
e7c27a73 8545 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
8546 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8547 fip->nbaseclasses++;
8548 }
8549}
8550
98751a41
JK
8551/* Add a typedef defined in the scope of the FIP's class. */
8552
8553static void
8554dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8555 struct dwarf2_cu *cu)
6e70227d 8556{
98751a41 8557 struct objfile *objfile = cu->objfile;
98751a41
JK
8558 struct typedef_field_list *new_field;
8559 struct attribute *attr;
8560 struct typedef_field *fp;
8561 char *fieldname = "";
8562
8563 /* Allocate a new field list entry and link it in. */
8564 new_field = xzalloc (sizeof (*new_field));
8565 make_cleanup (xfree, new_field);
8566
8567 gdb_assert (die->tag == DW_TAG_typedef);
8568
8569 fp = &new_field->field;
8570
8571 /* Get name of field. */
8572 fp->name = dwarf2_name (die, cu);
8573 if (fp->name == NULL)
8574 return;
8575
8576 fp->type = read_type_die (die, cu);
8577
8578 new_field->next = fip->typedef_field_list;
8579 fip->typedef_field_list = new_field;
8580 fip->typedef_field_list_count++;
8581}
8582
c906108c
SS
8583/* Create the vector of fields, and attach it to the type. */
8584
8585static void
fba45db2 8586dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8587 struct dwarf2_cu *cu)
c906108c
SS
8588{
8589 int nfields = fip->nfields;
8590
8591 /* Record the field count, allocate space for the array of fields,
8592 and create blank accessibility bitfields if necessary. */
8593 TYPE_NFIELDS (type) = nfields;
8594 TYPE_FIELDS (type) = (struct field *)
8595 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8596 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8597
b4ba55a1 8598 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
8599 {
8600 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8601
8602 TYPE_FIELD_PRIVATE_BITS (type) =
8603 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8604 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8605
8606 TYPE_FIELD_PROTECTED_BITS (type) =
8607 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8608 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8609
774b6a14
TT
8610 TYPE_FIELD_IGNORE_BITS (type) =
8611 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8612 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
8613 }
8614
8615 /* If the type has baseclasses, allocate and clear a bit vector for
8616 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 8617 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
8618 {
8619 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 8620 unsigned char *pointer;
c906108c
SS
8621
8622 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
8623 pointer = TYPE_ALLOC (type, num_bytes);
8624 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
8625 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8626 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8627 }
8628
3e43a32a
MS
8629 /* Copy the saved-up fields into the field vector. Start from the head of
8630 the list, adding to the tail of the field array, so that they end up in
8631 the same order in the array in which they were added to the list. */
c906108c
SS
8632 while (nfields-- > 0)
8633 {
7d0ccb61
DJ
8634 struct nextfield *fieldp;
8635
8636 if (fip->fields)
8637 {
8638 fieldp = fip->fields;
8639 fip->fields = fieldp->next;
8640 }
8641 else
8642 {
8643 fieldp = fip->baseclasses;
8644 fip->baseclasses = fieldp->next;
8645 }
8646
8647 TYPE_FIELD (type, nfields) = fieldp->field;
8648 switch (fieldp->accessibility)
c906108c 8649 {
c5aa993b 8650 case DW_ACCESS_private:
b4ba55a1
JB
8651 if (cu->language != language_ada)
8652 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 8653 break;
c906108c 8654
c5aa993b 8655 case DW_ACCESS_protected:
b4ba55a1
JB
8656 if (cu->language != language_ada)
8657 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 8658 break;
c906108c 8659
c5aa993b
JM
8660 case DW_ACCESS_public:
8661 break;
c906108c 8662
c5aa993b
JM
8663 default:
8664 /* Unknown accessibility. Complain and treat it as public. */
8665 {
e2e0b3e5 8666 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 8667 fieldp->accessibility);
c5aa993b
JM
8668 }
8669 break;
c906108c
SS
8670 }
8671 if (nfields < fip->nbaseclasses)
8672 {
7d0ccb61 8673 switch (fieldp->virtuality)
c906108c 8674 {
c5aa993b
JM
8675 case DW_VIRTUALITY_virtual:
8676 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 8677 if (cu->language == language_ada)
a73c6dcd 8678 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
8679 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8680 break;
c906108c
SS
8681 }
8682 }
c906108c
SS
8683 }
8684}
8685
c906108c
SS
8686/* Add a member function to the proper fieldlist. */
8687
8688static void
107d2387 8689dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 8690 struct type *type, struct dwarf2_cu *cu)
c906108c 8691{
e7c27a73 8692 struct objfile *objfile = cu->objfile;
c906108c
SS
8693 struct attribute *attr;
8694 struct fnfieldlist *flp;
8695 int i;
8696 struct fn_field *fnp;
8697 char *fieldname;
c906108c 8698 struct nextfnfield *new_fnfield;
f792889a 8699 struct type *this_type;
60d5a603 8700 enum dwarf_access_attribute accessibility;
c906108c 8701
b4ba55a1 8702 if (cu->language == language_ada)
a73c6dcd 8703 error (_("unexpected member function in Ada type"));
b4ba55a1 8704
2df3850c 8705 /* Get name of member function. */
39cbfefa
DJ
8706 fieldname = dwarf2_name (die, cu);
8707 if (fieldname == NULL)
2df3850c 8708 return;
c906108c 8709
c906108c
SS
8710 /* Look up member function name in fieldlist. */
8711 for (i = 0; i < fip->nfnfields; i++)
8712 {
27bfe10e 8713 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
8714 break;
8715 }
8716
8717 /* Create new list element if necessary. */
8718 if (i < fip->nfnfields)
8719 flp = &fip->fnfieldlists[i];
8720 else
8721 {
8722 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8723 {
8724 fip->fnfieldlists = (struct fnfieldlist *)
8725 xrealloc (fip->fnfieldlists,
8726 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8727 * sizeof (struct fnfieldlist));
c906108c 8728 if (fip->nfnfields == 0)
c13c43fd 8729 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
8730 }
8731 flp = &fip->fnfieldlists[fip->nfnfields];
8732 flp->name = fieldname;
8733 flp->length = 0;
8734 flp->head = NULL;
3da10d80 8735 i = fip->nfnfields++;
c906108c
SS
8736 }
8737
8738 /* Create a new member function field and chain it to the field list
0963b4bd 8739 entry. */
c906108c 8740 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 8741 make_cleanup (xfree, new_fnfield);
c906108c
SS
8742 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8743 new_fnfield->next = flp->head;
8744 flp->head = new_fnfield;
8745 flp->length++;
8746
8747 /* Fill in the member function field info. */
8748 fnp = &new_fnfield->fnfield;
3da10d80
KS
8749
8750 /* Delay processing of the physname until later. */
8751 if (cu->language == language_cplus || cu->language == language_java)
8752 {
8753 add_to_method_list (type, i, flp->length - 1, fieldname,
8754 die, cu);
8755 }
8756 else
8757 {
1d06ead6 8758 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
8759 fnp->physname = physname ? physname : "";
8760 }
8761
c906108c 8762 fnp->type = alloc_type (objfile);
f792889a
DJ
8763 this_type = read_type_die (die, cu);
8764 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 8765 {
f792889a 8766 int nparams = TYPE_NFIELDS (this_type);
c906108c 8767
f792889a 8768 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
8769 of the method itself (TYPE_CODE_METHOD). */
8770 smash_to_method_type (fnp->type, type,
f792889a
DJ
8771 TYPE_TARGET_TYPE (this_type),
8772 TYPE_FIELDS (this_type),
8773 TYPE_NFIELDS (this_type),
8774 TYPE_VARARGS (this_type));
c906108c
SS
8775
8776 /* Handle static member functions.
c5aa993b 8777 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
8778 member functions. G++ helps GDB by marking the first
8779 parameter for non-static member functions (which is the this
8780 pointer) as artificial. We obtain this information from
8781 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 8782 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
8783 fnp->voffset = VOFFSET_STATIC;
8784 }
8785 else
e2e0b3e5 8786 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 8787 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
8788
8789 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 8790 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 8791 fnp->fcontext = die_containing_type (die, cu);
c906108c 8792
3e43a32a
MS
8793 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
8794 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
8795
8796 /* Get accessibility. */
e142c38c 8797 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 8798 if (attr)
60d5a603
JK
8799 accessibility = DW_UNSND (attr);
8800 else
8801 accessibility = dwarf2_default_access_attribute (die, cu);
8802 switch (accessibility)
c906108c 8803 {
60d5a603
JK
8804 case DW_ACCESS_private:
8805 fnp->is_private = 1;
8806 break;
8807 case DW_ACCESS_protected:
8808 fnp->is_protected = 1;
8809 break;
c906108c
SS
8810 }
8811
b02dede2 8812 /* Check for artificial methods. */
e142c38c 8813 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
8814 if (attr && DW_UNSND (attr) != 0)
8815 fnp->is_artificial = 1;
8816
0d564a31 8817 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
8818 function. For older versions of GCC, this is an offset in the
8819 appropriate virtual table, as specified by DW_AT_containing_type.
8820 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
8821 to the object address. */
8822
e142c38c 8823 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 8824 if (attr)
8e19ed76 8825 {
aec5aa8b 8826 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 8827 {
aec5aa8b
TT
8828 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
8829 {
8830 /* Old-style GCC. */
8831 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8832 }
8833 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
8834 || (DW_BLOCK (attr)->size > 1
8835 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
8836 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
8837 {
8838 struct dwarf_block blk;
8839 int offset;
8840
8841 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
8842 ? 1 : 2);
8843 blk.size = DW_BLOCK (attr)->size - offset;
8844 blk.data = DW_BLOCK (attr)->data + offset;
8845 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
8846 if ((fnp->voffset % cu->header.addr_size) != 0)
8847 dwarf2_complex_location_expr_complaint ();
8848 else
8849 fnp->voffset /= cu->header.addr_size;
8850 fnp->voffset += 2;
8851 }
8852 else
8853 dwarf2_complex_location_expr_complaint ();
8854
8855 if (!fnp->fcontext)
8856 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
8857 }
3690dd37 8858 else if (attr_form_is_section_offset (attr))
8e19ed76 8859 {
4d3c2250 8860 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8861 }
8862 else
8863 {
4d3c2250
KB
8864 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
8865 fieldname);
8e19ed76 8866 }
0d564a31 8867 }
d48cc9dd
DJ
8868 else
8869 {
8870 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8871 if (attr && DW_UNSND (attr))
8872 {
8873 /* GCC does this, as of 2008-08-25; PR debug/37237. */
8874 complaint (&symfile_complaints,
3e43a32a
MS
8875 _("Member function \"%s\" (offset %d) is virtual "
8876 "but the vtable offset is not specified"),
b64f50a1 8877 fieldname, die->offset.sect_off);
9655fd1a 8878 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
8879 TYPE_CPLUS_DYNAMIC (type) = 1;
8880 }
8881 }
c906108c
SS
8882}
8883
8884/* Create the vector of member function fields, and attach it to the type. */
8885
8886static void
fba45db2 8887dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8888 struct dwarf2_cu *cu)
c906108c
SS
8889{
8890 struct fnfieldlist *flp;
c906108c
SS
8891 int i;
8892
b4ba55a1 8893 if (cu->language == language_ada)
a73c6dcd 8894 error (_("unexpected member functions in Ada type"));
b4ba55a1 8895
c906108c
SS
8896 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8897 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
8898 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
8899
8900 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
8901 {
8902 struct nextfnfield *nfp = flp->head;
8903 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
8904 int k;
8905
8906 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
8907 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
8908 fn_flp->fn_fields = (struct fn_field *)
8909 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
8910 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 8911 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
8912 }
8913
8914 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
8915}
8916
1168df01
JB
8917/* Returns non-zero if NAME is the name of a vtable member in CU's
8918 language, zero otherwise. */
8919static int
8920is_vtable_name (const char *name, struct dwarf2_cu *cu)
8921{
8922 static const char vptr[] = "_vptr";
987504bb 8923 static const char vtable[] = "vtable";
1168df01 8924
987504bb
JJ
8925 /* Look for the C++ and Java forms of the vtable. */
8926 if ((cu->language == language_java
8927 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
8928 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
8929 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
8930 return 1;
8931
8932 return 0;
8933}
8934
c0dd20ea 8935/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
8936 functions, with the ABI-specified layout. If TYPE describes
8937 such a structure, smash it into a member function type.
61049d3b
DJ
8938
8939 GCC shouldn't do this; it should just output pointer to member DIEs.
8940 This is GCC PR debug/28767. */
c0dd20ea 8941
0b92b5bb
TT
8942static void
8943quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 8944{
0b92b5bb 8945 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
8946
8947 /* Check for a structure with no name and two children. */
0b92b5bb
TT
8948 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
8949 return;
c0dd20ea
DJ
8950
8951 /* Check for __pfn and __delta members. */
0b92b5bb
TT
8952 if (TYPE_FIELD_NAME (type, 0) == NULL
8953 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
8954 || TYPE_FIELD_NAME (type, 1) == NULL
8955 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
8956 return;
c0dd20ea
DJ
8957
8958 /* Find the type of the method. */
0b92b5bb 8959 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
8960 if (pfn_type == NULL
8961 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
8962 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 8963 return;
c0dd20ea
DJ
8964
8965 /* Look for the "this" argument. */
8966 pfn_type = TYPE_TARGET_TYPE (pfn_type);
8967 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 8968 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 8969 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 8970 return;
c0dd20ea
DJ
8971
8972 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
8973 new_type = alloc_type (objfile);
8974 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
8975 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
8976 TYPE_VARARGS (pfn_type));
0b92b5bb 8977 smash_to_methodptr_type (type, new_type);
c0dd20ea 8978}
1168df01 8979
c906108c 8980/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
8981 (definition) to create a type for the structure or union. Fill in
8982 the type's name and general properties; the members will not be
8983 processed until process_structure_type.
c906108c 8984
c767944b
DJ
8985 NOTE: we need to call these functions regardless of whether or not the
8986 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
8987 structure or union. This gets the type entered into our set of
8988 user defined types.
8989
8990 However, if the structure is incomplete (an opaque struct/union)
8991 then suppress creating a symbol table entry for it since gdb only
8992 wants to find the one with the complete definition. Note that if
8993 it is complete, we just call new_symbol, which does it's own
8994 checking about whether the struct/union is anonymous or not (and
8995 suppresses creating a symbol table entry itself). */
8996
f792889a 8997static struct type *
134d01f1 8998read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8999{
e7c27a73 9000 struct objfile *objfile = cu->objfile;
c906108c
SS
9001 struct type *type;
9002 struct attribute *attr;
39cbfefa 9003 char *name;
c906108c 9004
348e048f
DE
9005 /* If the definition of this type lives in .debug_types, read that type.
9006 Don't follow DW_AT_specification though, that will take us back up
9007 the chain and we want to go down. */
9008 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9009 if (attr)
9010 {
9011 struct dwarf2_cu *type_cu = cu;
9012 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9013
348e048f
DE
9014 /* We could just recurse on read_structure_type, but we need to call
9015 get_die_type to ensure only one type for this DIE is created.
9016 This is important, for example, because for c++ classes we need
9017 TYPE_NAME set which is only done by new_symbol. Blech. */
9018 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9019
9020 /* TYPE_CU may not be the same as CU.
9021 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9022 return set_die_type (die, type, cu);
9023 }
9024
c0dd20ea 9025 type = alloc_type (objfile);
c906108c 9026 INIT_CPLUS_SPECIFIC (type);
93311388 9027
39cbfefa
DJ
9028 name = dwarf2_name (die, cu);
9029 if (name != NULL)
c906108c 9030 {
987504bb
JJ
9031 if (cu->language == language_cplus
9032 || cu->language == language_java)
63d06c5c 9033 {
3da10d80
KS
9034 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9035
9036 /* dwarf2_full_name might have already finished building the DIE's
9037 type. If so, there is no need to continue. */
9038 if (get_die_type (die, cu) != NULL)
9039 return get_die_type (die, cu);
9040
9041 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
9042 if (die->tag == DW_TAG_structure_type
9043 || die->tag == DW_TAG_class_type)
9044 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
9045 }
9046 else
9047 {
d8151005
DJ
9048 /* The name is already allocated along with this objfile, so
9049 we don't need to duplicate it for the type. */
94af9270
KS
9050 TYPE_TAG_NAME (type) = (char *) name;
9051 if (die->tag == DW_TAG_class_type)
9052 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 9053 }
c906108c
SS
9054 }
9055
9056 if (die->tag == DW_TAG_structure_type)
9057 {
9058 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9059 }
9060 else if (die->tag == DW_TAG_union_type)
9061 {
9062 TYPE_CODE (type) = TYPE_CODE_UNION;
9063 }
9064 else
9065 {
c906108c
SS
9066 TYPE_CODE (type) = TYPE_CODE_CLASS;
9067 }
9068
0cc2414c
TT
9069 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9070 TYPE_DECLARED_CLASS (type) = 1;
9071
e142c38c 9072 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9073 if (attr)
9074 {
9075 TYPE_LENGTH (type) = DW_UNSND (attr);
9076 }
9077 else
9078 {
9079 TYPE_LENGTH (type) = 0;
9080 }
9081
876cecd0 9082 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 9083 if (die_is_declaration (die, cu))
876cecd0 9084 TYPE_STUB (type) = 1;
a6c727b2
DJ
9085 else if (attr == NULL && die->child == NULL
9086 && producer_is_realview (cu->producer))
9087 /* RealView does not output the required DW_AT_declaration
9088 on incomplete types. */
9089 TYPE_STUB (type) = 1;
dc718098 9090
c906108c
SS
9091 /* We need to add the type field to the die immediately so we don't
9092 infinitely recurse when dealing with pointers to the structure
0963b4bd 9093 type within the structure itself. */
1c379e20 9094 set_die_type (die, type, cu);
c906108c 9095
7e314c57
JK
9096 /* set_die_type should be already done. */
9097 set_descriptive_type (type, die, cu);
9098
c767944b
DJ
9099 return type;
9100}
9101
9102/* Finish creating a structure or union type, including filling in
9103 its members and creating a symbol for it. */
9104
9105static void
9106process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9107{
9108 struct objfile *objfile = cu->objfile;
9109 struct die_info *child_die = die->child;
9110 struct type *type;
9111
9112 type = get_die_type (die, cu);
9113 if (type == NULL)
9114 type = read_structure_type (die, cu);
9115
e142c38c 9116 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
9117 {
9118 struct field_info fi;
9119 struct die_info *child_die;
34eaf542 9120 VEC (symbolp) *template_args = NULL;
c767944b 9121 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
9122
9123 memset (&fi, 0, sizeof (struct field_info));
9124
639d11d3 9125 child_die = die->child;
c906108c
SS
9126
9127 while (child_die && child_die->tag)
9128 {
a9a9bd0f
DC
9129 if (child_die->tag == DW_TAG_member
9130 || child_die->tag == DW_TAG_variable)
c906108c 9131 {
a9a9bd0f
DC
9132 /* NOTE: carlton/2002-11-05: A C++ static data member
9133 should be a DW_TAG_member that is a declaration, but
9134 all versions of G++ as of this writing (so through at
9135 least 3.2.1) incorrectly generate DW_TAG_variable
9136 tags for them instead. */
e7c27a73 9137 dwarf2_add_field (&fi, child_die, cu);
c906108c 9138 }
8713b1b1 9139 else if (child_die->tag == DW_TAG_subprogram)
c906108c 9140 {
0963b4bd 9141 /* C++ member function. */
e7c27a73 9142 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
9143 }
9144 else if (child_die->tag == DW_TAG_inheritance)
9145 {
9146 /* C++ base class field. */
e7c27a73 9147 dwarf2_add_field (&fi, child_die, cu);
c906108c 9148 }
98751a41
JK
9149 else if (child_die->tag == DW_TAG_typedef)
9150 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
9151 else if (child_die->tag == DW_TAG_template_type_param
9152 || child_die->tag == DW_TAG_template_value_param)
9153 {
9154 struct symbol *arg = new_symbol (child_die, NULL, cu);
9155
f1078f66
DJ
9156 if (arg != NULL)
9157 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9158 }
9159
c906108c
SS
9160 child_die = sibling_die (child_die);
9161 }
9162
34eaf542
TT
9163 /* Attach template arguments to type. */
9164 if (! VEC_empty (symbolp, template_args))
9165 {
9166 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9167 TYPE_N_TEMPLATE_ARGUMENTS (type)
9168 = VEC_length (symbolp, template_args);
9169 TYPE_TEMPLATE_ARGUMENTS (type)
9170 = obstack_alloc (&objfile->objfile_obstack,
9171 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9172 * sizeof (struct symbol *)));
9173 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9174 VEC_address (symbolp, template_args),
9175 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9176 * sizeof (struct symbol *)));
9177 VEC_free (symbolp, template_args);
9178 }
9179
c906108c
SS
9180 /* Attach fields and member functions to the type. */
9181 if (fi.nfields)
e7c27a73 9182 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
9183 if (fi.nfnfields)
9184 {
e7c27a73 9185 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 9186
c5aa993b 9187 /* Get the type which refers to the base class (possibly this
c906108c 9188 class itself) which contains the vtable pointer for the current
0d564a31
DJ
9189 class from the DW_AT_containing_type attribute. This use of
9190 DW_AT_containing_type is a GNU extension. */
c906108c 9191
e142c38c 9192 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 9193 {
e7c27a73 9194 struct type *t = die_containing_type (die, cu);
c906108c
SS
9195
9196 TYPE_VPTR_BASETYPE (type) = t;
9197 if (type == t)
9198 {
c906108c
SS
9199 int i;
9200
9201 /* Our own class provides vtbl ptr. */
9202 for (i = TYPE_NFIELDS (t) - 1;
9203 i >= TYPE_N_BASECLASSES (t);
9204 --i)
9205 {
0d5cff50 9206 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 9207
1168df01 9208 if (is_vtable_name (fieldname, cu))
c906108c
SS
9209 {
9210 TYPE_VPTR_FIELDNO (type) = i;
9211 break;
9212 }
9213 }
9214
9215 /* Complain if virtual function table field not found. */
9216 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 9217 complaint (&symfile_complaints,
3e43a32a
MS
9218 _("virtual function table pointer "
9219 "not found when defining class '%s'"),
4d3c2250
KB
9220 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9221 "");
c906108c
SS
9222 }
9223 else
9224 {
9225 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9226 }
9227 }
f6235d4c
EZ
9228 else if (cu->producer
9229 && strncmp (cu->producer,
9230 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9231 {
9232 /* The IBM XLC compiler does not provide direct indication
9233 of the containing type, but the vtable pointer is
9234 always named __vfp. */
9235
9236 int i;
9237
9238 for (i = TYPE_NFIELDS (type) - 1;
9239 i >= TYPE_N_BASECLASSES (type);
9240 --i)
9241 {
9242 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9243 {
9244 TYPE_VPTR_FIELDNO (type) = i;
9245 TYPE_VPTR_BASETYPE (type) = type;
9246 break;
9247 }
9248 }
9249 }
c906108c 9250 }
98751a41
JK
9251
9252 /* Copy fi.typedef_field_list linked list elements content into the
9253 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9254 if (fi.typedef_field_list)
9255 {
9256 int i = fi.typedef_field_list_count;
9257
a0d7a4ff 9258 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
9259 TYPE_TYPEDEF_FIELD_ARRAY (type)
9260 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9261 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9262
9263 /* Reverse the list order to keep the debug info elements order. */
9264 while (--i >= 0)
9265 {
9266 struct typedef_field *dest, *src;
6e70227d 9267
98751a41
JK
9268 dest = &TYPE_TYPEDEF_FIELD (type, i);
9269 src = &fi.typedef_field_list->field;
9270 fi.typedef_field_list = fi.typedef_field_list->next;
9271 *dest = *src;
9272 }
9273 }
c767944b
DJ
9274
9275 do_cleanups (back_to);
eb2a6f42
TT
9276
9277 if (HAVE_CPLUS_STRUCT (type))
9278 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 9279 }
63d06c5c 9280
bb5ed363 9281 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 9282
90aeadfc
DC
9283 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9284 snapshots) has been known to create a die giving a declaration
9285 for a class that has, as a child, a die giving a definition for a
9286 nested class. So we have to process our children even if the
9287 current die is a declaration. Normally, of course, a declaration
9288 won't have any children at all. */
134d01f1 9289
90aeadfc
DC
9290 while (child_die != NULL && child_die->tag)
9291 {
9292 if (child_die->tag == DW_TAG_member
9293 || child_die->tag == DW_TAG_variable
34eaf542
TT
9294 || child_die->tag == DW_TAG_inheritance
9295 || child_die->tag == DW_TAG_template_value_param
9296 || child_die->tag == DW_TAG_template_type_param)
134d01f1 9297 {
90aeadfc 9298 /* Do nothing. */
134d01f1 9299 }
90aeadfc
DC
9300 else
9301 process_die (child_die, cu);
134d01f1 9302
90aeadfc 9303 child_die = sibling_die (child_die);
134d01f1
DJ
9304 }
9305
fa4028e9
JB
9306 /* Do not consider external references. According to the DWARF standard,
9307 these DIEs are identified by the fact that they have no byte_size
9308 attribute, and a declaration attribute. */
9309 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9310 || !die_is_declaration (die, cu))
c767944b 9311 new_symbol (die, type, cu);
134d01f1
DJ
9312}
9313
9314/* Given a DW_AT_enumeration_type die, set its type. We do not
9315 complete the type's fields yet, or create any symbols. */
c906108c 9316
f792889a 9317static struct type *
134d01f1 9318read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9319{
e7c27a73 9320 struct objfile *objfile = cu->objfile;
c906108c 9321 struct type *type;
c906108c 9322 struct attribute *attr;
0114d602 9323 const char *name;
134d01f1 9324
348e048f
DE
9325 /* If the definition of this type lives in .debug_types, read that type.
9326 Don't follow DW_AT_specification though, that will take us back up
9327 the chain and we want to go down. */
9328 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9329 if (attr)
9330 {
9331 struct dwarf2_cu *type_cu = cu;
9332 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9333
348e048f 9334 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9335
9336 /* TYPE_CU may not be the same as CU.
9337 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9338 return set_die_type (die, type, cu);
9339 }
9340
c906108c
SS
9341 type = alloc_type (objfile);
9342
9343 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 9344 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 9345 if (name != NULL)
0114d602 9346 TYPE_TAG_NAME (type) = (char *) name;
c906108c 9347
e142c38c 9348 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9349 if (attr)
9350 {
9351 TYPE_LENGTH (type) = DW_UNSND (attr);
9352 }
9353 else
9354 {
9355 TYPE_LENGTH (type) = 0;
9356 }
9357
137033e9
JB
9358 /* The enumeration DIE can be incomplete. In Ada, any type can be
9359 declared as private in the package spec, and then defined only
9360 inside the package body. Such types are known as Taft Amendment
9361 Types. When another package uses such a type, an incomplete DIE
9362 may be generated by the compiler. */
02eb380e 9363 if (die_is_declaration (die, cu))
876cecd0 9364 TYPE_STUB (type) = 1;
02eb380e 9365
f792889a 9366 return set_die_type (die, type, cu);
134d01f1
DJ
9367}
9368
9369/* Given a pointer to a die which begins an enumeration, process all
9370 the dies that define the members of the enumeration, and create the
9371 symbol for the enumeration type.
9372
9373 NOTE: We reverse the order of the element list. */
9374
9375static void
9376process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9377{
f792889a 9378 struct type *this_type;
134d01f1 9379
f792889a
DJ
9380 this_type = get_die_type (die, cu);
9381 if (this_type == NULL)
9382 this_type = read_enumeration_type (die, cu);
9dc481d3 9383
639d11d3 9384 if (die->child != NULL)
c906108c 9385 {
9dc481d3
DE
9386 struct die_info *child_die;
9387 struct symbol *sym;
9388 struct field *fields = NULL;
9389 int num_fields = 0;
9390 int unsigned_enum = 1;
9391 char *name;
cafec441
TT
9392 int flag_enum = 1;
9393 ULONGEST mask = 0;
9dc481d3 9394
639d11d3 9395 child_die = die->child;
c906108c
SS
9396 while (child_die && child_die->tag)
9397 {
9398 if (child_die->tag != DW_TAG_enumerator)
9399 {
e7c27a73 9400 process_die (child_die, cu);
c906108c
SS
9401 }
9402 else
9403 {
39cbfefa
DJ
9404 name = dwarf2_name (child_die, cu);
9405 if (name)
c906108c 9406 {
f792889a 9407 sym = new_symbol (child_die, this_type, cu);
c906108c 9408 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
9409 {
9410 unsigned_enum = 0;
9411 flag_enum = 0;
9412 }
9413 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9414 flag_enum = 0;
9415 else
9416 mask |= SYMBOL_VALUE (sym);
c906108c
SS
9417
9418 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9419 {
9420 fields = (struct field *)
9421 xrealloc (fields,
9422 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 9423 * sizeof (struct field));
c906108c
SS
9424 }
9425
3567439c 9426 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 9427 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 9428 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
9429 FIELD_BITSIZE (fields[num_fields]) = 0;
9430
9431 num_fields++;
9432 }
9433 }
9434
9435 child_die = sibling_die (child_die);
9436 }
9437
9438 if (num_fields)
9439 {
f792889a
DJ
9440 TYPE_NFIELDS (this_type) = num_fields;
9441 TYPE_FIELDS (this_type) = (struct field *)
9442 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9443 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 9444 sizeof (struct field) * num_fields);
b8c9b27d 9445 xfree (fields);
c906108c
SS
9446 }
9447 if (unsigned_enum)
876cecd0 9448 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
9449 if (flag_enum)
9450 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 9451 }
134d01f1 9452
6c83ed52
TT
9453 /* If we are reading an enum from a .debug_types unit, and the enum
9454 is a declaration, and the enum is not the signatured type in the
9455 unit, then we do not want to add a symbol for it. Adding a
9456 symbol would in some cases obscure the true definition of the
9457 enum, giving users an incomplete type when the definition is
9458 actually available. Note that we do not want to do this for all
9459 enums which are just declarations, because C++0x allows forward
9460 enum declarations. */
3019eac3 9461 if (cu->per_cu->is_debug_types
6c83ed52
TT
9462 && die_is_declaration (die, cu))
9463 {
52dc124a 9464 struct signatured_type *sig_type;
6c83ed52 9465
52dc124a 9466 sig_type
6c83ed52 9467 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 9468 cu->per_cu->info_or_types_section,
6c83ed52 9469 cu->per_cu->offset);
3019eac3
DE
9470 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9471 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
9472 return;
9473 }
9474
f792889a 9475 new_symbol (die, this_type, cu);
c906108c
SS
9476}
9477
9478/* Extract all information from a DW_TAG_array_type DIE and put it in
9479 the DIE's type field. For now, this only handles one dimensional
9480 arrays. */
9481
f792889a 9482static struct type *
e7c27a73 9483read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9484{
e7c27a73 9485 struct objfile *objfile = cu->objfile;
c906108c 9486 struct die_info *child_die;
7e314c57 9487 struct type *type;
c906108c
SS
9488 struct type *element_type, *range_type, *index_type;
9489 struct type **range_types = NULL;
9490 struct attribute *attr;
9491 int ndim = 0;
9492 struct cleanup *back_to;
39cbfefa 9493 char *name;
c906108c 9494
e7c27a73 9495 element_type = die_type (die, cu);
c906108c 9496
7e314c57
JK
9497 /* The die_type call above may have already set the type for this DIE. */
9498 type = get_die_type (die, cu);
9499 if (type)
9500 return type;
9501
c906108c
SS
9502 /* Irix 6.2 native cc creates array types without children for
9503 arrays with unspecified length. */
639d11d3 9504 if (die->child == NULL)
c906108c 9505 {
46bf5051 9506 index_type = objfile_type (objfile)->builtin_int;
c906108c 9507 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
9508 type = create_array_type (NULL, element_type, range_type);
9509 return set_die_type (die, type, cu);
c906108c
SS
9510 }
9511
9512 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 9513 child_die = die->child;
c906108c
SS
9514 while (child_die && child_die->tag)
9515 {
9516 if (child_die->tag == DW_TAG_subrange_type)
9517 {
f792889a 9518 struct type *child_type = read_type_die (child_die, cu);
9a619af0 9519
f792889a 9520 if (child_type != NULL)
a02abb62 9521 {
0963b4bd
MS
9522 /* The range type was succesfully read. Save it for the
9523 array type creation. */
a02abb62
JB
9524 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9525 {
9526 range_types = (struct type **)
9527 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9528 * sizeof (struct type *));
9529 if (ndim == 0)
9530 make_cleanup (free_current_contents, &range_types);
9531 }
f792889a 9532 range_types[ndim++] = child_type;
a02abb62 9533 }
c906108c
SS
9534 }
9535 child_die = sibling_die (child_die);
9536 }
9537
9538 /* Dwarf2 dimensions are output from left to right, create the
9539 necessary array types in backwards order. */
7ca2d3a3 9540
c906108c 9541 type = element_type;
7ca2d3a3
DL
9542
9543 if (read_array_order (die, cu) == DW_ORD_col_major)
9544 {
9545 int i = 0;
9a619af0 9546
7ca2d3a3
DL
9547 while (i < ndim)
9548 type = create_array_type (NULL, type, range_types[i++]);
9549 }
9550 else
9551 {
9552 while (ndim-- > 0)
9553 type = create_array_type (NULL, type, range_types[ndim]);
9554 }
c906108c 9555
f5f8a009
EZ
9556 /* Understand Dwarf2 support for vector types (like they occur on
9557 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9558 array type. This is not part of the Dwarf2/3 standard yet, but a
9559 custom vendor extension. The main difference between a regular
9560 array and the vector variant is that vectors are passed by value
9561 to functions. */
e142c38c 9562 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 9563 if (attr)
ea37ba09 9564 make_vector_type (type);
f5f8a009 9565
dbc98a8b
KW
9566 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9567 implementation may choose to implement triple vectors using this
9568 attribute. */
9569 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9570 if (attr)
9571 {
9572 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9573 TYPE_LENGTH (type) = DW_UNSND (attr);
9574 else
3e43a32a
MS
9575 complaint (&symfile_complaints,
9576 _("DW_AT_byte_size for array type smaller "
9577 "than the total size of elements"));
dbc98a8b
KW
9578 }
9579
39cbfefa
DJ
9580 name = dwarf2_name (die, cu);
9581 if (name)
9582 TYPE_NAME (type) = name;
6e70227d 9583
0963b4bd 9584 /* Install the type in the die. */
7e314c57
JK
9585 set_die_type (die, type, cu);
9586
9587 /* set_die_type should be already done. */
b4ba55a1
JB
9588 set_descriptive_type (type, die, cu);
9589
c906108c
SS
9590 do_cleanups (back_to);
9591
7e314c57 9592 return type;
c906108c
SS
9593}
9594
7ca2d3a3 9595static enum dwarf_array_dim_ordering
6e70227d 9596read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
9597{
9598 struct attribute *attr;
9599
9600 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9601
9602 if (attr) return DW_SND (attr);
9603
0963b4bd
MS
9604 /* GNU F77 is a special case, as at 08/2004 array type info is the
9605 opposite order to the dwarf2 specification, but data is still
9606 laid out as per normal fortran.
7ca2d3a3 9607
0963b4bd
MS
9608 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9609 version checking. */
7ca2d3a3 9610
905e0470
PM
9611 if (cu->language == language_fortran
9612 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
9613 {
9614 return DW_ORD_row_major;
9615 }
9616
6e70227d 9617 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
9618 {
9619 case array_column_major:
9620 return DW_ORD_col_major;
9621 case array_row_major:
9622 default:
9623 return DW_ORD_row_major;
9624 };
9625}
9626
72019c9c 9627/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 9628 the DIE's type field. */
72019c9c 9629
f792889a 9630static struct type *
72019c9c
GM
9631read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9632{
7e314c57
JK
9633 struct type *domain_type, *set_type;
9634 struct attribute *attr;
f792889a 9635
7e314c57
JK
9636 domain_type = die_type (die, cu);
9637
9638 /* The die_type call above may have already set the type for this DIE. */
9639 set_type = get_die_type (die, cu);
9640 if (set_type)
9641 return set_type;
9642
9643 set_type = create_set_type (NULL, domain_type);
9644
9645 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
9646 if (attr)
9647 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 9648
f792889a 9649 return set_die_type (die, set_type, cu);
72019c9c 9650}
7ca2d3a3 9651
c906108c
SS
9652/* First cut: install each common block member as a global variable. */
9653
9654static void
e7c27a73 9655read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
9656{
9657 struct die_info *child_die;
9658 struct attribute *attr;
9659 struct symbol *sym;
9660 CORE_ADDR base = (CORE_ADDR) 0;
9661
e142c38c 9662 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
9663 if (attr)
9664 {
0963b4bd 9665 /* Support the .debug_loc offsets. */
8e19ed76
PS
9666 if (attr_form_is_block (attr))
9667 {
e7c27a73 9668 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 9669 }
3690dd37 9670 else if (attr_form_is_section_offset (attr))
8e19ed76 9671 {
4d3c2250 9672 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9673 }
9674 else
9675 {
4d3c2250
KB
9676 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9677 "common block member");
8e19ed76 9678 }
c906108c 9679 }
639d11d3 9680 if (die->child != NULL)
c906108c 9681 {
639d11d3 9682 child_die = die->child;
c906108c
SS
9683 while (child_die && child_die->tag)
9684 {
74ac6d43
TT
9685 LONGEST offset;
9686
e7c27a73 9687 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
9688 if (sym != NULL
9689 && handle_data_member_location (child_die, cu, &offset))
c906108c 9690 {
74ac6d43 9691 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
9692 add_symbol_to_list (sym, &global_symbols);
9693 }
9694 child_die = sibling_die (child_die);
9695 }
9696 }
9697}
9698
0114d602 9699/* Create a type for a C++ namespace. */
d9fa45fe 9700
0114d602
DJ
9701static struct type *
9702read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 9703{
e7c27a73 9704 struct objfile *objfile = cu->objfile;
0114d602 9705 const char *previous_prefix, *name;
9219021c 9706 int is_anonymous;
0114d602
DJ
9707 struct type *type;
9708
9709 /* For extensions, reuse the type of the original namespace. */
9710 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9711 {
9712 struct die_info *ext_die;
9713 struct dwarf2_cu *ext_cu = cu;
9a619af0 9714
0114d602
DJ
9715 ext_die = dwarf2_extension (die, &ext_cu);
9716 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
9717
9718 /* EXT_CU may not be the same as CU.
9719 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
9720 return set_die_type (die, type, cu);
9721 }
9219021c 9722
e142c38c 9723 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
9724
9725 /* Now build the name of the current namespace. */
9726
0114d602
DJ
9727 previous_prefix = determine_prefix (die, cu);
9728 if (previous_prefix[0] != '\0')
9729 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 9730 previous_prefix, name, 0, cu);
0114d602
DJ
9731
9732 /* Create the type. */
9733 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
9734 objfile);
9735 TYPE_NAME (type) = (char *) name;
9736 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9737
60531b24 9738 return set_die_type (die, type, cu);
0114d602
DJ
9739}
9740
9741/* Read a C++ namespace. */
9742
9743static void
9744read_namespace (struct die_info *die, struct dwarf2_cu *cu)
9745{
9746 struct objfile *objfile = cu->objfile;
0114d602 9747 int is_anonymous;
9219021c 9748
5c4e30ca
DC
9749 /* Add a symbol associated to this if we haven't seen the namespace
9750 before. Also, add a using directive if it's an anonymous
9751 namespace. */
9219021c 9752
f2f0e013 9753 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
9754 {
9755 struct type *type;
9756
0114d602 9757 type = read_type_die (die, cu);
e7c27a73 9758 new_symbol (die, type, cu);
5c4e30ca 9759
e8e80198 9760 namespace_name (die, &is_anonymous, cu);
5c4e30ca 9761 if (is_anonymous)
0114d602
DJ
9762 {
9763 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 9764
c0cc3a76 9765 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 9766 NULL, NULL, &objfile->objfile_obstack);
0114d602 9767 }
5c4e30ca 9768 }
9219021c 9769
639d11d3 9770 if (die->child != NULL)
d9fa45fe 9771 {
639d11d3 9772 struct die_info *child_die = die->child;
6e70227d 9773
d9fa45fe
DC
9774 while (child_die && child_die->tag)
9775 {
e7c27a73 9776 process_die (child_die, cu);
d9fa45fe
DC
9777 child_die = sibling_die (child_die);
9778 }
9779 }
38d518c9
EZ
9780}
9781
f55ee35c
JK
9782/* Read a Fortran module as type. This DIE can be only a declaration used for
9783 imported module. Still we need that type as local Fortran "use ... only"
9784 declaration imports depend on the created type in determine_prefix. */
9785
9786static struct type *
9787read_module_type (struct die_info *die, struct dwarf2_cu *cu)
9788{
9789 struct objfile *objfile = cu->objfile;
9790 char *module_name;
9791 struct type *type;
9792
9793 module_name = dwarf2_name (die, cu);
9794 if (!module_name)
3e43a32a
MS
9795 complaint (&symfile_complaints,
9796 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 9797 die->offset.sect_off);
f55ee35c
JK
9798 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
9799
9800 /* determine_prefix uses TYPE_TAG_NAME. */
9801 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9802
9803 return set_die_type (die, type, cu);
9804}
9805
5d7cb8df
JK
9806/* Read a Fortran module. */
9807
9808static void
9809read_module (struct die_info *die, struct dwarf2_cu *cu)
9810{
9811 struct die_info *child_die = die->child;
9812
5d7cb8df
JK
9813 while (child_die && child_die->tag)
9814 {
9815 process_die (child_die, cu);
9816 child_die = sibling_die (child_die);
9817 }
9818}
9819
38d518c9
EZ
9820/* Return the name of the namespace represented by DIE. Set
9821 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
9822 namespace. */
9823
9824static const char *
e142c38c 9825namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
9826{
9827 struct die_info *current_die;
9828 const char *name = NULL;
9829
9830 /* Loop through the extensions until we find a name. */
9831
9832 for (current_die = die;
9833 current_die != NULL;
f2f0e013 9834 current_die = dwarf2_extension (die, &cu))
38d518c9 9835 {
e142c38c 9836 name = dwarf2_name (current_die, cu);
38d518c9
EZ
9837 if (name != NULL)
9838 break;
9839 }
9840
9841 /* Is it an anonymous namespace? */
9842
9843 *is_anonymous = (name == NULL);
9844 if (*is_anonymous)
2b1dbab0 9845 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
9846
9847 return name;
d9fa45fe
DC
9848}
9849
c906108c
SS
9850/* Extract all information from a DW_TAG_pointer_type DIE and add to
9851 the user defined type vector. */
9852
f792889a 9853static struct type *
e7c27a73 9854read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9855{
5e2b427d 9856 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 9857 struct comp_unit_head *cu_header = &cu->header;
c906108c 9858 struct type *type;
8b2dbe47
KB
9859 struct attribute *attr_byte_size;
9860 struct attribute *attr_address_class;
9861 int byte_size, addr_class;
7e314c57
JK
9862 struct type *target_type;
9863
9864 target_type = die_type (die, cu);
c906108c 9865
7e314c57
JK
9866 /* The die_type call above may have already set the type for this DIE. */
9867 type = get_die_type (die, cu);
9868 if (type)
9869 return type;
9870
9871 type = lookup_pointer_type (target_type);
8b2dbe47 9872
e142c38c 9873 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
9874 if (attr_byte_size)
9875 byte_size = DW_UNSND (attr_byte_size);
c906108c 9876 else
8b2dbe47
KB
9877 byte_size = cu_header->addr_size;
9878
e142c38c 9879 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
9880 if (attr_address_class)
9881 addr_class = DW_UNSND (attr_address_class);
9882 else
9883 addr_class = DW_ADDR_none;
9884
9885 /* If the pointer size or address class is different than the
9886 default, create a type variant marked as such and set the
9887 length accordingly. */
9888 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 9889 {
5e2b427d 9890 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
9891 {
9892 int type_flags;
9893
849957d9 9894 type_flags = gdbarch_address_class_type_flags
5e2b427d 9895 (gdbarch, byte_size, addr_class);
876cecd0
TT
9896 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
9897 == 0);
8b2dbe47
KB
9898 type = make_type_with_address_space (type, type_flags);
9899 }
9900 else if (TYPE_LENGTH (type) != byte_size)
9901 {
3e43a32a
MS
9902 complaint (&symfile_complaints,
9903 _("invalid pointer size %d"), byte_size);
8b2dbe47 9904 }
6e70227d 9905 else
9a619af0
MS
9906 {
9907 /* Should we also complain about unhandled address classes? */
9908 }
c906108c 9909 }
8b2dbe47
KB
9910
9911 TYPE_LENGTH (type) = byte_size;
f792889a 9912 return set_die_type (die, type, cu);
c906108c
SS
9913}
9914
9915/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
9916 the user defined type vector. */
9917
f792889a 9918static struct type *
e7c27a73 9919read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
9920{
9921 struct type *type;
9922 struct type *to_type;
9923 struct type *domain;
9924
e7c27a73
DJ
9925 to_type = die_type (die, cu);
9926 domain = die_containing_type (die, cu);
0d5de010 9927
7e314c57
JK
9928 /* The calls above may have already set the type for this DIE. */
9929 type = get_die_type (die, cu);
9930 if (type)
9931 return type;
9932
0d5de010
DJ
9933 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
9934 type = lookup_methodptr_type (to_type);
9935 else
9936 type = lookup_memberptr_type (to_type, domain);
c906108c 9937
f792889a 9938 return set_die_type (die, type, cu);
c906108c
SS
9939}
9940
9941/* Extract all information from a DW_TAG_reference_type DIE and add to
9942 the user defined type vector. */
9943
f792889a 9944static struct type *
e7c27a73 9945read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9946{
e7c27a73 9947 struct comp_unit_head *cu_header = &cu->header;
7e314c57 9948 struct type *type, *target_type;
c906108c
SS
9949 struct attribute *attr;
9950
7e314c57
JK
9951 target_type = die_type (die, cu);
9952
9953 /* The die_type call above may have already set the type for this DIE. */
9954 type = get_die_type (die, cu);
9955 if (type)
9956 return type;
9957
9958 type = lookup_reference_type (target_type);
e142c38c 9959 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9960 if (attr)
9961 {
9962 TYPE_LENGTH (type) = DW_UNSND (attr);
9963 }
9964 else
9965 {
107d2387 9966 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 9967 }
f792889a 9968 return set_die_type (die, type, cu);
c906108c
SS
9969}
9970
f792889a 9971static struct type *
e7c27a73 9972read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9973{
f792889a 9974 struct type *base_type, *cv_type;
c906108c 9975
e7c27a73 9976 base_type = die_type (die, cu);
7e314c57
JK
9977
9978 /* The die_type call above may have already set the type for this DIE. */
9979 cv_type = get_die_type (die, cu);
9980 if (cv_type)
9981 return cv_type;
9982
2f608a3a
KW
9983 /* In case the const qualifier is applied to an array type, the element type
9984 is so qualified, not the array type (section 6.7.3 of C99). */
9985 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
9986 {
9987 struct type *el_type, *inner_array;
9988
9989 base_type = copy_type (base_type);
9990 inner_array = base_type;
9991
9992 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
9993 {
9994 TYPE_TARGET_TYPE (inner_array) =
9995 copy_type (TYPE_TARGET_TYPE (inner_array));
9996 inner_array = TYPE_TARGET_TYPE (inner_array);
9997 }
9998
9999 el_type = TYPE_TARGET_TYPE (inner_array);
10000 TYPE_TARGET_TYPE (inner_array) =
10001 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10002
10003 return set_die_type (die, base_type, cu);
10004 }
10005
f792889a
DJ
10006 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10007 return set_die_type (die, cv_type, cu);
c906108c
SS
10008}
10009
f792889a 10010static struct type *
e7c27a73 10011read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10012{
f792889a 10013 struct type *base_type, *cv_type;
c906108c 10014
e7c27a73 10015 base_type = die_type (die, cu);
7e314c57
JK
10016
10017 /* The die_type call above may have already set the type for this DIE. */
10018 cv_type = get_die_type (die, cu);
10019 if (cv_type)
10020 return cv_type;
10021
f792889a
DJ
10022 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10023 return set_die_type (die, cv_type, cu);
c906108c
SS
10024}
10025
10026/* Extract all information from a DW_TAG_string_type DIE and add to
10027 the user defined type vector. It isn't really a user defined type,
10028 but it behaves like one, with other DIE's using an AT_user_def_type
10029 attribute to reference it. */
10030
f792889a 10031static struct type *
e7c27a73 10032read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10033{
e7c27a73 10034 struct objfile *objfile = cu->objfile;
3b7538c0 10035 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10036 struct type *type, *range_type, *index_type, *char_type;
10037 struct attribute *attr;
10038 unsigned int length;
10039
e142c38c 10040 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
10041 if (attr)
10042 {
10043 length = DW_UNSND (attr);
10044 }
10045 else
10046 {
0963b4bd 10047 /* Check for the DW_AT_byte_size attribute. */
e142c38c 10048 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
10049 if (attr)
10050 {
10051 length = DW_UNSND (attr);
10052 }
10053 else
10054 {
10055 length = 1;
10056 }
c906108c 10057 }
6ccb9162 10058
46bf5051 10059 index_type = objfile_type (objfile)->builtin_int;
c906108c 10060 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
10061 char_type = language_string_char_type (cu->language_defn, gdbarch);
10062 type = create_string_type (NULL, char_type, range_type);
6ccb9162 10063
f792889a 10064 return set_die_type (die, type, cu);
c906108c
SS
10065}
10066
10067/* Handle DIES due to C code like:
10068
10069 struct foo
c5aa993b
JM
10070 {
10071 int (*funcp)(int a, long l);
10072 int b;
10073 };
c906108c 10074
0963b4bd 10075 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 10076
f792889a 10077static struct type *
e7c27a73 10078read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10079{
bb5ed363 10080 struct objfile *objfile = cu->objfile;
0963b4bd
MS
10081 struct type *type; /* Type that this function returns. */
10082 struct type *ftype; /* Function that returns above type. */
c906108c
SS
10083 struct attribute *attr;
10084
e7c27a73 10085 type = die_type (die, cu);
7e314c57
JK
10086
10087 /* The die_type call above may have already set the type for this DIE. */
10088 ftype = get_die_type (die, cu);
10089 if (ftype)
10090 return ftype;
10091
0c8b41f1 10092 ftype = lookup_function_type (type);
c906108c 10093
5b8101ae 10094 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 10095 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 10096 if ((attr && (DW_UNSND (attr) != 0))
987504bb 10097 || cu->language == language_cplus
5b8101ae
PM
10098 || cu->language == language_java
10099 || cu->language == language_pascal)
876cecd0 10100 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
10101 else if (producer_is_realview (cu->producer))
10102 /* RealView does not emit DW_AT_prototyped. We can not
10103 distinguish prototyped and unprototyped functions; default to
10104 prototyped, since that is more common in modern code (and
10105 RealView warns about unprototyped functions). */
10106 TYPE_PROTOTYPED (ftype) = 1;
c906108c 10107
c055b101
CV
10108 /* Store the calling convention in the type if it's available in
10109 the subroutine die. Otherwise set the calling convention to
10110 the default value DW_CC_normal. */
10111 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
10112 if (attr)
10113 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10114 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10115 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10116 else
10117 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
10118
10119 /* We need to add the subroutine type to the die immediately so
10120 we don't infinitely recurse when dealing with parameters
0963b4bd 10121 declared as the same subroutine type. */
76c10ea2 10122 set_die_type (die, ftype, cu);
6e70227d 10123
639d11d3 10124 if (die->child != NULL)
c906108c 10125 {
bb5ed363 10126 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 10127 struct die_info *child_die;
8072405b 10128 int nparams, iparams;
c906108c
SS
10129
10130 /* Count the number of parameters.
10131 FIXME: GDB currently ignores vararg functions, but knows about
10132 vararg member functions. */
8072405b 10133 nparams = 0;
639d11d3 10134 child_die = die->child;
c906108c
SS
10135 while (child_die && child_die->tag)
10136 {
10137 if (child_die->tag == DW_TAG_formal_parameter)
10138 nparams++;
10139 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 10140 TYPE_VARARGS (ftype) = 1;
c906108c
SS
10141 child_die = sibling_die (child_die);
10142 }
10143
10144 /* Allocate storage for parameters and fill them in. */
10145 TYPE_NFIELDS (ftype) = nparams;
10146 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 10147 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 10148
8072405b
JK
10149 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10150 even if we error out during the parameters reading below. */
10151 for (iparams = 0; iparams < nparams; iparams++)
10152 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10153
10154 iparams = 0;
639d11d3 10155 child_die = die->child;
c906108c
SS
10156 while (child_die && child_die->tag)
10157 {
10158 if (child_die->tag == DW_TAG_formal_parameter)
10159 {
3ce3b1ba
PA
10160 struct type *arg_type;
10161
10162 /* DWARF version 2 has no clean way to discern C++
10163 static and non-static member functions. G++ helps
10164 GDB by marking the first parameter for non-static
10165 member functions (which is the this pointer) as
10166 artificial. We pass this information to
10167 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10168
10169 DWARF version 3 added DW_AT_object_pointer, which GCC
10170 4.5 does not yet generate. */
e142c38c 10171 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
10172 if (attr)
10173 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10174 else
418835cc
KS
10175 {
10176 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10177
10178 /* GCC/43521: In java, the formal parameter
10179 "this" is sometimes not marked with DW_AT_artificial. */
10180 if (cu->language == language_java)
10181 {
10182 const char *name = dwarf2_name (child_die, cu);
9a619af0 10183
418835cc
KS
10184 if (name && !strcmp (name, "this"))
10185 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10186 }
10187 }
3ce3b1ba
PA
10188 arg_type = die_type (child_die, cu);
10189
10190 /* RealView does not mark THIS as const, which the testsuite
10191 expects. GCC marks THIS as const in method definitions,
10192 but not in the class specifications (GCC PR 43053). */
10193 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10194 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10195 {
10196 int is_this = 0;
10197 struct dwarf2_cu *arg_cu = cu;
10198 const char *name = dwarf2_name (child_die, cu);
10199
10200 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10201 if (attr)
10202 {
10203 /* If the compiler emits this, use it. */
10204 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10205 is_this = 1;
10206 }
10207 else if (name && strcmp (name, "this") == 0)
10208 /* Function definitions will have the argument names. */
10209 is_this = 1;
10210 else if (name == NULL && iparams == 0)
10211 /* Declarations may not have the names, so like
10212 elsewhere in GDB, assume an artificial first
10213 argument is "this". */
10214 is_this = 1;
10215
10216 if (is_this)
10217 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10218 arg_type, 0);
10219 }
10220
10221 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
10222 iparams++;
10223 }
10224 child_die = sibling_die (child_die);
10225 }
10226 }
10227
76c10ea2 10228 return ftype;
c906108c
SS
10229}
10230
f792889a 10231static struct type *
e7c27a73 10232read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10233{
e7c27a73 10234 struct objfile *objfile = cu->objfile;
0114d602 10235 const char *name = NULL;
3c8e0968 10236 struct type *this_type, *target_type;
c906108c 10237
94af9270 10238 name = dwarf2_full_name (NULL, die, cu);
f792889a 10239 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
10240 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10241 TYPE_NAME (this_type) = (char *) name;
f792889a 10242 set_die_type (die, this_type, cu);
3c8e0968
DE
10243 target_type = die_type (die, cu);
10244 if (target_type != this_type)
10245 TYPE_TARGET_TYPE (this_type) = target_type;
10246 else
10247 {
10248 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10249 spec and cause infinite loops in GDB. */
10250 complaint (&symfile_complaints,
10251 _("Self-referential DW_TAG_typedef "
10252 "- DIE at 0x%x [in module %s]"),
b64f50a1 10253 die->offset.sect_off, objfile->name);
3c8e0968
DE
10254 TYPE_TARGET_TYPE (this_type) = NULL;
10255 }
f792889a 10256 return this_type;
c906108c
SS
10257}
10258
10259/* Find a representation of a given base type and install
10260 it in the TYPE field of the die. */
10261
f792889a 10262static struct type *
e7c27a73 10263read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10264{
e7c27a73 10265 struct objfile *objfile = cu->objfile;
c906108c
SS
10266 struct type *type;
10267 struct attribute *attr;
10268 int encoding = 0, size = 0;
39cbfefa 10269 char *name;
6ccb9162
UW
10270 enum type_code code = TYPE_CODE_INT;
10271 int type_flags = 0;
10272 struct type *target_type = NULL;
c906108c 10273
e142c38c 10274 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
10275 if (attr)
10276 {
10277 encoding = DW_UNSND (attr);
10278 }
e142c38c 10279 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10280 if (attr)
10281 {
10282 size = DW_UNSND (attr);
10283 }
39cbfefa 10284 name = dwarf2_name (die, cu);
6ccb9162 10285 if (!name)
c906108c 10286 {
6ccb9162
UW
10287 complaint (&symfile_complaints,
10288 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 10289 }
6ccb9162
UW
10290
10291 switch (encoding)
c906108c 10292 {
6ccb9162
UW
10293 case DW_ATE_address:
10294 /* Turn DW_ATE_address into a void * pointer. */
10295 code = TYPE_CODE_PTR;
10296 type_flags |= TYPE_FLAG_UNSIGNED;
10297 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10298 break;
10299 case DW_ATE_boolean:
10300 code = TYPE_CODE_BOOL;
10301 type_flags |= TYPE_FLAG_UNSIGNED;
10302 break;
10303 case DW_ATE_complex_float:
10304 code = TYPE_CODE_COMPLEX;
10305 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10306 break;
10307 case DW_ATE_decimal_float:
10308 code = TYPE_CODE_DECFLOAT;
10309 break;
10310 case DW_ATE_float:
10311 code = TYPE_CODE_FLT;
10312 break;
10313 case DW_ATE_signed:
10314 break;
10315 case DW_ATE_unsigned:
10316 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
10317 if (cu->language == language_fortran
10318 && name
10319 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10320 code = TYPE_CODE_CHAR;
6ccb9162
UW
10321 break;
10322 case DW_ATE_signed_char:
6e70227d 10323 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10324 || cu->language == language_pascal
10325 || cu->language == language_fortran)
6ccb9162
UW
10326 code = TYPE_CODE_CHAR;
10327 break;
10328 case DW_ATE_unsigned_char:
868a0084 10329 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10330 || cu->language == language_pascal
10331 || cu->language == language_fortran)
6ccb9162
UW
10332 code = TYPE_CODE_CHAR;
10333 type_flags |= TYPE_FLAG_UNSIGNED;
10334 break;
75079b2b
TT
10335 case DW_ATE_UTF:
10336 /* We just treat this as an integer and then recognize the
10337 type by name elsewhere. */
10338 break;
10339
6ccb9162
UW
10340 default:
10341 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10342 dwarf_type_encoding_name (encoding));
10343 break;
c906108c 10344 }
6ccb9162 10345
0114d602
DJ
10346 type = init_type (code, size, type_flags, NULL, objfile);
10347 TYPE_NAME (type) = name;
6ccb9162
UW
10348 TYPE_TARGET_TYPE (type) = target_type;
10349
0114d602 10350 if (name && strcmp (name, "char") == 0)
876cecd0 10351 TYPE_NOSIGN (type) = 1;
0114d602 10352
f792889a 10353 return set_die_type (die, type, cu);
c906108c
SS
10354}
10355
a02abb62
JB
10356/* Read the given DW_AT_subrange DIE. */
10357
f792889a 10358static struct type *
a02abb62
JB
10359read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10360{
10361 struct type *base_type;
10362 struct type *range_type;
10363 struct attribute *attr;
4fae6e18
JK
10364 LONGEST low, high;
10365 int low_default_is_valid;
39cbfefa 10366 char *name;
43bbcdc2 10367 LONGEST negative_mask;
e77813c8 10368
a02abb62 10369 base_type = die_type (die, cu);
953ac07e
JK
10370 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10371 check_typedef (base_type);
a02abb62 10372
7e314c57
JK
10373 /* The die_type call above may have already set the type for this DIE. */
10374 range_type = get_die_type (die, cu);
10375 if (range_type)
10376 return range_type;
10377
4fae6e18
JK
10378 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10379 omitting DW_AT_lower_bound. */
10380 switch (cu->language)
6e70227d 10381 {
4fae6e18
JK
10382 case language_c:
10383 case language_cplus:
10384 low = 0;
10385 low_default_is_valid = 1;
10386 break;
10387 case language_fortran:
10388 low = 1;
10389 low_default_is_valid = 1;
10390 break;
10391 case language_d:
10392 case language_java:
10393 case language_objc:
10394 low = 0;
10395 low_default_is_valid = (cu->header.version >= 4);
10396 break;
10397 case language_ada:
10398 case language_m2:
10399 case language_pascal:
a02abb62 10400 low = 1;
4fae6e18
JK
10401 low_default_is_valid = (cu->header.version >= 4);
10402 break;
10403 default:
10404 low = 0;
10405 low_default_is_valid = 0;
10406 break;
a02abb62
JB
10407 }
10408
dd5e6932
DJ
10409 /* FIXME: For variable sized arrays either of these could be
10410 a variable rather than a constant value. We'll allow it,
10411 but we don't know how to handle it. */
e142c38c 10412 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 10413 if (attr)
4fae6e18
JK
10414 low = dwarf2_get_attr_constant_value (attr, low);
10415 else if (!low_default_is_valid)
10416 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10417 "- DIE at 0x%x [in module %s]"),
10418 die->offset.sect_off, cu->objfile->name);
a02abb62 10419
e142c38c 10420 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 10421 if (attr)
6e70227d 10422 {
d48323d8 10423 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
10424 {
10425 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 10426 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
10427 FIXME: GDB does not yet know how to handle dynamic
10428 arrays properly, treat them as arrays with unspecified
10429 length for now.
10430
10431 FIXME: jimb/2003-09-22: GDB does not really know
10432 how to handle arrays of unspecified length
10433 either; we just represent them as zero-length
10434 arrays. Choose an appropriate upper bound given
10435 the lower bound we've computed above. */
10436 high = low - 1;
10437 }
10438 else
10439 high = dwarf2_get_attr_constant_value (attr, 1);
10440 }
e77813c8
PM
10441 else
10442 {
10443 attr = dwarf2_attr (die, DW_AT_count, cu);
10444 if (attr)
10445 {
10446 int count = dwarf2_get_attr_constant_value (attr, 1);
10447 high = low + count - 1;
10448 }
c2ff108b
JK
10449 else
10450 {
10451 /* Unspecified array length. */
10452 high = low - 1;
10453 }
e77813c8
PM
10454 }
10455
10456 /* Dwarf-2 specifications explicitly allows to create subrange types
10457 without specifying a base type.
10458 In that case, the base type must be set to the type of
10459 the lower bound, upper bound or count, in that order, if any of these
10460 three attributes references an object that has a type.
10461 If no base type is found, the Dwarf-2 specifications say that
10462 a signed integer type of size equal to the size of an address should
10463 be used.
10464 For the following C code: `extern char gdb_int [];'
10465 GCC produces an empty range DIE.
10466 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 10467 high bound or count are not yet handled by this code. */
e77813c8
PM
10468 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10469 {
10470 struct objfile *objfile = cu->objfile;
10471 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10472 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10473 struct type *int_type = objfile_type (objfile)->builtin_int;
10474
10475 /* Test "int", "long int", and "long long int" objfile types,
10476 and select the first one having a size above or equal to the
10477 architecture address size. */
10478 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10479 base_type = int_type;
10480 else
10481 {
10482 int_type = objfile_type (objfile)->builtin_long;
10483 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10484 base_type = int_type;
10485 else
10486 {
10487 int_type = objfile_type (objfile)->builtin_long_long;
10488 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10489 base_type = int_type;
10490 }
10491 }
10492 }
a02abb62 10493
6e70227d 10494 negative_mask =
43bbcdc2
PH
10495 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10496 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10497 low |= negative_mask;
10498 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10499 high |= negative_mask;
10500
a02abb62
JB
10501 range_type = create_range_type (NULL, base_type, low, high);
10502
bbb0eef6
JK
10503 /* Mark arrays with dynamic length at least as an array of unspecified
10504 length. GDB could check the boundary but before it gets implemented at
10505 least allow accessing the array elements. */
d48323d8 10506 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
10507 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10508
c2ff108b
JK
10509 /* Ada expects an empty array on no boundary attributes. */
10510 if (attr == NULL && cu->language != language_ada)
10511 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10512
39cbfefa
DJ
10513 name = dwarf2_name (die, cu);
10514 if (name)
10515 TYPE_NAME (range_type) = name;
6e70227d 10516
e142c38c 10517 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
10518 if (attr)
10519 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10520
7e314c57
JK
10521 set_die_type (die, range_type, cu);
10522
10523 /* set_die_type should be already done. */
b4ba55a1
JB
10524 set_descriptive_type (range_type, die, cu);
10525
7e314c57 10526 return range_type;
a02abb62 10527}
6e70227d 10528
f792889a 10529static struct type *
81a17f79
JB
10530read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10531{
10532 struct type *type;
81a17f79 10533
81a17f79
JB
10534 /* For now, we only support the C meaning of an unspecified type: void. */
10535
0114d602
DJ
10536 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10537 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 10538
f792889a 10539 return set_die_type (die, type, cu);
81a17f79 10540}
a02abb62 10541
639d11d3
DC
10542/* Read a single die and all its descendents. Set the die's sibling
10543 field to NULL; set other fields in the die correctly, and set all
10544 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10545 location of the info_ptr after reading all of those dies. PARENT
10546 is the parent of the die in question. */
10547
10548static struct die_info *
dee91e82
DE
10549read_die_and_children (const struct die_reader_specs *reader,
10550 gdb_byte *info_ptr,
10551 gdb_byte **new_info_ptr,
10552 struct die_info *parent)
639d11d3
DC
10553{
10554 struct die_info *die;
fe1b8b76 10555 gdb_byte *cur_ptr;
639d11d3
DC
10556 int has_children;
10557
93311388 10558 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
10559 if (die == NULL)
10560 {
10561 *new_info_ptr = cur_ptr;
10562 return NULL;
10563 }
93311388 10564 store_in_ref_table (die, reader->cu);
639d11d3
DC
10565
10566 if (has_children)
348e048f 10567 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
10568 else
10569 {
10570 die->child = NULL;
10571 *new_info_ptr = cur_ptr;
10572 }
10573
10574 die->sibling = NULL;
10575 die->parent = parent;
10576 return die;
10577}
10578
10579/* Read a die, all of its descendents, and all of its siblings; set
10580 all of the fields of all of the dies correctly. Arguments are as
10581 in read_die_and_children. */
10582
10583static struct die_info *
93311388
DE
10584read_die_and_siblings (const struct die_reader_specs *reader,
10585 gdb_byte *info_ptr,
fe1b8b76 10586 gdb_byte **new_info_ptr,
639d11d3
DC
10587 struct die_info *parent)
10588{
10589 struct die_info *first_die, *last_sibling;
fe1b8b76 10590 gdb_byte *cur_ptr;
639d11d3 10591
c906108c 10592 cur_ptr = info_ptr;
639d11d3
DC
10593 first_die = last_sibling = NULL;
10594
10595 while (1)
c906108c 10596 {
639d11d3 10597 struct die_info *die
dee91e82 10598 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 10599
1d325ec1 10600 if (die == NULL)
c906108c 10601 {
639d11d3
DC
10602 *new_info_ptr = cur_ptr;
10603 return first_die;
c906108c 10604 }
1d325ec1
DJ
10605
10606 if (!first_die)
10607 first_die = die;
c906108c 10608 else
1d325ec1
DJ
10609 last_sibling->sibling = die;
10610
10611 last_sibling = die;
c906108c 10612 }
c906108c
SS
10613}
10614
3019eac3
DE
10615/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10616 attributes.
10617 The caller is responsible for filling in the extra attributes
10618 and updating (*DIEP)->num_attrs.
10619 Set DIEP to point to a newly allocated die with its information,
10620 except for its child, sibling, and parent fields.
10621 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
10622
10623static gdb_byte *
3019eac3
DE
10624read_full_die_1 (const struct die_reader_specs *reader,
10625 struct die_info **diep, gdb_byte *info_ptr,
10626 int *has_children, int num_extra_attrs)
93311388 10627{
b64f50a1
JK
10628 unsigned int abbrev_number, bytes_read, i;
10629 sect_offset offset;
93311388
DE
10630 struct abbrev_info *abbrev;
10631 struct die_info *die;
10632 struct dwarf2_cu *cu = reader->cu;
10633 bfd *abfd = reader->abfd;
10634
b64f50a1 10635 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
10636 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10637 info_ptr += bytes_read;
10638 if (!abbrev_number)
10639 {
10640 *diep = NULL;
10641 *has_children = 0;
10642 return info_ptr;
10643 }
10644
10645 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10646 if (!abbrev)
348e048f
DE
10647 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10648 abbrev_number,
10649 bfd_get_filename (abfd));
10650
3019eac3 10651 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
10652 die->offset = offset;
10653 die->tag = abbrev->tag;
10654 die->abbrev = abbrev_number;
10655
3019eac3
DE
10656 /* Make the result usable.
10657 The caller needs to update num_attrs after adding the extra
10658 attributes. */
93311388
DE
10659 die->num_attrs = abbrev->num_attrs;
10660
10661 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
10662 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10663 info_ptr);
93311388
DE
10664
10665 *diep = die;
10666 *has_children = abbrev->has_children;
10667 return info_ptr;
10668}
10669
3019eac3
DE
10670/* Read a die and all its attributes.
10671 Set DIEP to point to a newly allocated die with its information,
10672 except for its child, sibling, and parent fields.
10673 Set HAS_CHILDREN to tell whether the die has children or not. */
10674
10675static gdb_byte *
10676read_full_die (const struct die_reader_specs *reader,
10677 struct die_info **diep, gdb_byte *info_ptr,
10678 int *has_children)
10679{
10680 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10681}
10682
c906108c
SS
10683/* In DWARF version 2, the description of the debugging information is
10684 stored in a separate .debug_abbrev section. Before we read any
10685 dies from a section we read in all abbreviations and install them
72bf9492
DJ
10686 in a hash table. This function also sets flags in CU describing
10687 the data found in the abbrev table. */
c906108c
SS
10688
10689static void
dee91e82
DE
10690dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10691 struct dwarf2_section_info *abbrev_section)
10692
c906108c 10693{
dee91e82 10694 bfd *abfd = abbrev_section->asection->owner;
e7c27a73 10695 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 10696 gdb_byte *abbrev_ptr;
c906108c
SS
10697 struct abbrev_info *cur_abbrev;
10698 unsigned int abbrev_number, bytes_read, abbrev_name;
10699 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
10700 struct attr_abbrev *cur_attrs;
10701 unsigned int allocated_attrs;
c906108c 10702
0963b4bd 10703 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
10704 obstack_init (&cu->abbrev_obstack);
10705 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10706 (ABBREV_HASH_SIZE
10707 * sizeof (struct abbrev_info *)));
10708 memset (cu->dwarf2_abbrevs, 0,
10709 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 10710
dee91e82
DE
10711 dwarf2_read_section (cu->objfile, abbrev_section);
10712 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
c906108c
SS
10713 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10714 abbrev_ptr += bytes_read;
10715
f3dd6933
DJ
10716 allocated_attrs = ATTR_ALLOC_CHUNK;
10717 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 10718
0963b4bd 10719 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
10720 while (abbrev_number)
10721 {
f3dd6933 10722 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
10723
10724 /* read in abbrev header */
10725 cur_abbrev->number = abbrev_number;
10726 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10727 abbrev_ptr += bytes_read;
10728 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
10729 abbrev_ptr += 1;
10730
10731 /* now read in declarations */
10732 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10733 abbrev_ptr += bytes_read;
10734 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10735 abbrev_ptr += bytes_read;
10736 while (abbrev_name)
10737 {
f3dd6933 10738 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 10739 {
f3dd6933
DJ
10740 allocated_attrs += ATTR_ALLOC_CHUNK;
10741 cur_attrs
10742 = xrealloc (cur_attrs, (allocated_attrs
10743 * sizeof (struct attr_abbrev)));
c906108c 10744 }
ae038cb0 10745
f3dd6933
DJ
10746 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
10747 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
10748 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10749 abbrev_ptr += bytes_read;
10750 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10751 abbrev_ptr += bytes_read;
10752 }
10753
f3dd6933
DJ
10754 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
10755 (cur_abbrev->num_attrs
10756 * sizeof (struct attr_abbrev)));
10757 memcpy (cur_abbrev->attrs, cur_attrs,
10758 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
10759
c906108c 10760 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
10761 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
10762 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
10763
10764 /* Get next abbreviation.
10765 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
10766 always properly terminated with an abbrev number of 0.
10767 Exit loop if we encounter an abbreviation which we have
10768 already read (which means we are about to read the abbreviations
10769 for the next compile unit) or if the end of the abbreviation
10770 table is reached. */
dee91e82
DE
10771 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
10772 >= abbrev_section->size)
c906108c
SS
10773 break;
10774 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10775 abbrev_ptr += bytes_read;
e7c27a73 10776 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
10777 break;
10778 }
f3dd6933
DJ
10779
10780 xfree (cur_attrs);
c906108c
SS
10781}
10782
f3dd6933 10783/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 10784
c906108c 10785static void
f3dd6933 10786dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 10787{
f3dd6933 10788 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 10789
f3dd6933
DJ
10790 obstack_free (&cu->abbrev_obstack, NULL);
10791 cu->dwarf2_abbrevs = NULL;
c906108c
SS
10792}
10793
10794/* Lookup an abbrev_info structure in the abbrev hash table. */
10795
10796static struct abbrev_info *
e7c27a73 10797dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
10798{
10799 unsigned int hash_number;
10800 struct abbrev_info *abbrev;
10801
10802 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 10803 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
10804
10805 while (abbrev)
10806 {
10807 if (abbrev->number == number)
10808 return abbrev;
10809 else
10810 abbrev = abbrev->next;
10811 }
10812 return NULL;
10813}
10814
72bf9492
DJ
10815/* Returns nonzero if TAG represents a type that we might generate a partial
10816 symbol for. */
10817
10818static int
10819is_type_tag_for_partial (int tag)
10820{
10821 switch (tag)
10822 {
10823#if 0
10824 /* Some types that would be reasonable to generate partial symbols for,
10825 that we don't at present. */
10826 case DW_TAG_array_type:
10827 case DW_TAG_file_type:
10828 case DW_TAG_ptr_to_member_type:
10829 case DW_TAG_set_type:
10830 case DW_TAG_string_type:
10831 case DW_TAG_subroutine_type:
10832#endif
10833 case DW_TAG_base_type:
10834 case DW_TAG_class_type:
680b30c7 10835 case DW_TAG_interface_type:
72bf9492
DJ
10836 case DW_TAG_enumeration_type:
10837 case DW_TAG_structure_type:
10838 case DW_TAG_subrange_type:
10839 case DW_TAG_typedef:
10840 case DW_TAG_union_type:
10841 return 1;
10842 default:
10843 return 0;
10844 }
10845}
10846
10847/* Load all DIEs that are interesting for partial symbols into memory. */
10848
10849static struct partial_die_info *
dee91e82
DE
10850load_partial_dies (const struct die_reader_specs *reader,
10851 gdb_byte *info_ptr, int building_psymtab)
72bf9492 10852{
dee91e82 10853 struct dwarf2_cu *cu = reader->cu;
bb5ed363 10854 struct objfile *objfile = cu->objfile;
72bf9492
DJ
10855 struct partial_die_info *part_die;
10856 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
10857 struct abbrev_info *abbrev;
10858 unsigned int bytes_read;
5afb4e99 10859 unsigned int load_all = 0;
72bf9492
DJ
10860 int nesting_level = 1;
10861
10862 parent_die = NULL;
10863 last_die = NULL;
10864
7adf1e79
DE
10865 gdb_assert (cu->per_cu != NULL);
10866 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
10867 load_all = 1;
10868
72bf9492
DJ
10869 cu->partial_dies
10870 = htab_create_alloc_ex (cu->header.length / 12,
10871 partial_die_hash,
10872 partial_die_eq,
10873 NULL,
10874 &cu->comp_unit_obstack,
10875 hashtab_obstack_allocate,
10876 dummy_obstack_deallocate);
10877
10878 part_die = obstack_alloc (&cu->comp_unit_obstack,
10879 sizeof (struct partial_die_info));
10880
10881 while (1)
10882 {
10883 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
10884
10885 /* A NULL abbrev means the end of a series of children. */
10886 if (abbrev == NULL)
10887 {
10888 if (--nesting_level == 0)
10889 {
10890 /* PART_DIE was probably the last thing allocated on the
10891 comp_unit_obstack, so we could call obstack_free
10892 here. We don't do that because the waste is small,
10893 and will be cleaned up when we're done with this
10894 compilation unit. This way, we're also more robust
10895 against other users of the comp_unit_obstack. */
10896 return first_die;
10897 }
10898 info_ptr += bytes_read;
10899 last_die = parent_die;
10900 parent_die = parent_die->die_parent;
10901 continue;
10902 }
10903
98bfdba5
PA
10904 /* Check for template arguments. We never save these; if
10905 they're seen, we just mark the parent, and go on our way. */
10906 if (parent_die != NULL
10907 && cu->language == language_cplus
10908 && (abbrev->tag == DW_TAG_template_type_param
10909 || abbrev->tag == DW_TAG_template_value_param))
10910 {
10911 parent_die->has_template_arguments = 1;
10912
10913 if (!load_all)
10914 {
10915 /* We don't need a partial DIE for the template argument. */
dee91e82 10916 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
10917 continue;
10918 }
10919 }
10920
0d99eb77 10921 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
10922 Skip their other children. */
10923 if (!load_all
10924 && cu->language == language_cplus
10925 && parent_die != NULL
10926 && parent_die->tag == DW_TAG_subprogram)
10927 {
dee91e82 10928 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
10929 continue;
10930 }
10931
5afb4e99
DJ
10932 /* Check whether this DIE is interesting enough to save. Normally
10933 we would not be interested in members here, but there may be
10934 later variables referencing them via DW_AT_specification (for
10935 static members). */
10936 if (!load_all
10937 && !is_type_tag_for_partial (abbrev->tag)
72929c62 10938 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
10939 && abbrev->tag != DW_TAG_enumerator
10940 && abbrev->tag != DW_TAG_subprogram
bc30ff58 10941 && abbrev->tag != DW_TAG_lexical_block
72bf9492 10942 && abbrev->tag != DW_TAG_variable
5afb4e99 10943 && abbrev->tag != DW_TAG_namespace
f55ee35c 10944 && abbrev->tag != DW_TAG_module
95554aad
TT
10945 && abbrev->tag != DW_TAG_member
10946 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
10947 {
10948 /* Otherwise we skip to the next sibling, if any. */
dee91e82 10949 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
10950 continue;
10951 }
10952
dee91e82
DE
10953 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
10954 info_ptr);
72bf9492
DJ
10955
10956 /* This two-pass algorithm for processing partial symbols has a
10957 high cost in cache pressure. Thus, handle some simple cases
10958 here which cover the majority of C partial symbols. DIEs
10959 which neither have specification tags in them, nor could have
10960 specification tags elsewhere pointing at them, can simply be
10961 processed and discarded.
10962
10963 This segment is also optional; scan_partial_symbols and
10964 add_partial_symbol will handle these DIEs if we chain
10965 them in normally. When compilers which do not emit large
10966 quantities of duplicate debug information are more common,
10967 this code can probably be removed. */
10968
10969 /* Any complete simple types at the top level (pretty much all
10970 of them, for a language without namespaces), can be processed
10971 directly. */
10972 if (parent_die == NULL
10973 && part_die->has_specification == 0
10974 && part_die->is_declaration == 0
d8228535 10975 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
10976 || part_die->tag == DW_TAG_base_type
10977 || part_die->tag == DW_TAG_subrange_type))
10978 {
10979 if (building_psymtab && part_die->name != NULL)
04a679b8 10980 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 10981 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
10982 &objfile->static_psymbols,
10983 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 10984 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
10985 continue;
10986 }
10987
d8228535
JK
10988 /* The exception for DW_TAG_typedef with has_children above is
10989 a workaround of GCC PR debug/47510. In the case of this complaint
10990 type_name_no_tag_or_error will error on such types later.
10991
10992 GDB skipped children of DW_TAG_typedef by the shortcut above and then
10993 it could not find the child DIEs referenced later, this is checked
10994 above. In correct DWARF DW_TAG_typedef should have no children. */
10995
10996 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
10997 complaint (&symfile_complaints,
10998 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
10999 "- DIE at 0x%x [in module %s]"),
b64f50a1 11000 part_die->offset.sect_off, objfile->name);
d8228535 11001
72bf9492
DJ
11002 /* If we're at the second level, and we're an enumerator, and
11003 our parent has no specification (meaning possibly lives in a
11004 namespace elsewhere), then we can add the partial symbol now
11005 instead of queueing it. */
11006 if (part_die->tag == DW_TAG_enumerator
11007 && parent_die != NULL
11008 && parent_die->die_parent == NULL
11009 && parent_die->tag == DW_TAG_enumeration_type
11010 && parent_die->has_specification == 0)
11011 {
11012 if (part_die->name == NULL)
3e43a32a
MS
11013 complaint (&symfile_complaints,
11014 _("malformed enumerator DIE ignored"));
72bf9492 11015 else if (building_psymtab)
04a679b8 11016 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11017 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
11018 (cu->language == language_cplus
11019 || cu->language == language_java)
bb5ed363
DE
11020 ? &objfile->global_psymbols
11021 : &objfile->static_psymbols,
11022 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 11023
dee91e82 11024 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11025 continue;
11026 }
11027
11028 /* We'll save this DIE so link it in. */
11029 part_die->die_parent = parent_die;
11030 part_die->die_sibling = NULL;
11031 part_die->die_child = NULL;
11032
11033 if (last_die && last_die == parent_die)
11034 last_die->die_child = part_die;
11035 else if (last_die)
11036 last_die->die_sibling = part_die;
11037
11038 last_die = part_die;
11039
11040 if (first_die == NULL)
11041 first_die = part_die;
11042
11043 /* Maybe add the DIE to the hash table. Not all DIEs that we
11044 find interesting need to be in the hash table, because we
11045 also have the parent/sibling/child chains; only those that we
11046 might refer to by offset later during partial symbol reading.
11047
11048 For now this means things that might have be the target of a
11049 DW_AT_specification, DW_AT_abstract_origin, or
11050 DW_AT_extension. DW_AT_extension will refer only to
11051 namespaces; DW_AT_abstract_origin refers to functions (and
11052 many things under the function DIE, but we do not recurse
11053 into function DIEs during partial symbol reading) and
11054 possibly variables as well; DW_AT_specification refers to
11055 declarations. Declarations ought to have the DW_AT_declaration
11056 flag. It happens that GCC forgets to put it in sometimes, but
11057 only for functions, not for types.
11058
11059 Adding more things than necessary to the hash table is harmless
11060 except for the performance cost. Adding too few will result in
5afb4e99
DJ
11061 wasted time in find_partial_die, when we reread the compilation
11062 unit with load_all_dies set. */
72bf9492 11063
5afb4e99 11064 if (load_all
72929c62 11065 || abbrev->tag == DW_TAG_constant
5afb4e99 11066 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
11067 || abbrev->tag == DW_TAG_variable
11068 || abbrev->tag == DW_TAG_namespace
11069 || part_die->is_declaration)
11070 {
11071 void **slot;
11072
11073 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 11074 part_die->offset.sect_off, INSERT);
72bf9492
DJ
11075 *slot = part_die;
11076 }
11077
11078 part_die = obstack_alloc (&cu->comp_unit_obstack,
11079 sizeof (struct partial_die_info));
11080
11081 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 11082 we have no reason to follow the children of structures; for other
98bfdba5
PA
11083 languages we have to, so that we can get at method physnames
11084 to infer fully qualified class names, for DW_AT_specification,
11085 and for C++ template arguments. For C++, we also look one level
11086 inside functions to find template arguments (if the name of the
11087 function does not already contain the template arguments).
bc30ff58
JB
11088
11089 For Ada, we need to scan the children of subprograms and lexical
11090 blocks as well because Ada allows the definition of nested
11091 entities that could be interesting for the debugger, such as
11092 nested subprograms for instance. */
72bf9492 11093 if (last_die->has_children
5afb4e99
DJ
11094 && (load_all
11095 || last_die->tag == DW_TAG_namespace
f55ee35c 11096 || last_die->tag == DW_TAG_module
72bf9492 11097 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
11098 || (cu->language == language_cplus
11099 && last_die->tag == DW_TAG_subprogram
11100 && (last_die->name == NULL
11101 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
11102 || (cu->language != language_c
11103 && (last_die->tag == DW_TAG_class_type
680b30c7 11104 || last_die->tag == DW_TAG_interface_type
72bf9492 11105 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
11106 || last_die->tag == DW_TAG_union_type))
11107 || (cu->language == language_ada
11108 && (last_die->tag == DW_TAG_subprogram
11109 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
11110 {
11111 nesting_level++;
11112 parent_die = last_die;
11113 continue;
11114 }
11115
11116 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11117 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
11118
11119 /* Back to the top, do it again. */
11120 }
11121}
11122
c906108c
SS
11123/* Read a minimal amount of information into the minimal die structure. */
11124
fe1b8b76 11125static gdb_byte *
dee91e82
DE
11126read_partial_die (const struct die_reader_specs *reader,
11127 struct partial_die_info *part_die,
11128 struct abbrev_info *abbrev, unsigned int abbrev_len,
11129 gdb_byte *info_ptr)
c906108c 11130{
dee91e82 11131 struct dwarf2_cu *cu = reader->cu;
bb5ed363 11132 struct objfile *objfile = cu->objfile;
dee91e82 11133 gdb_byte *buffer = reader->buffer;
fa238c03 11134 unsigned int i;
c906108c 11135 struct attribute attr;
c5aa993b 11136 int has_low_pc_attr = 0;
c906108c 11137 int has_high_pc_attr = 0;
91da1414 11138 int high_pc_relative = 0;
c906108c 11139
72bf9492 11140 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 11141
b64f50a1 11142 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
11143
11144 info_ptr += abbrev_len;
11145
11146 if (abbrev == NULL)
11147 return info_ptr;
11148
c906108c
SS
11149 part_die->tag = abbrev->tag;
11150 part_die->has_children = abbrev->has_children;
c906108c
SS
11151
11152 for (i = 0; i < abbrev->num_attrs; ++i)
11153 {
dee91e82 11154 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
11155
11156 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 11157 partial symbol table. */
c906108c
SS
11158 switch (attr.name)
11159 {
11160 case DW_AT_name:
71c25dea
TT
11161 switch (part_die->tag)
11162 {
11163 case DW_TAG_compile_unit:
95554aad 11164 case DW_TAG_partial_unit:
348e048f 11165 case DW_TAG_type_unit:
71c25dea
TT
11166 /* Compilation units have a DW_AT_name that is a filename, not
11167 a source language identifier. */
11168 case DW_TAG_enumeration_type:
11169 case DW_TAG_enumerator:
11170 /* These tags always have simple identifiers already; no need
11171 to canonicalize them. */
11172 part_die->name = DW_STRING (&attr);
11173 break;
11174 default:
11175 part_die->name
11176 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 11177 &objfile->objfile_obstack);
71c25dea
TT
11178 break;
11179 }
c906108c 11180 break;
31ef98ae 11181 case DW_AT_linkage_name:
c906108c 11182 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
11183 /* Note that both forms of linkage name might appear. We
11184 assume they will be the same, and we only store the last
11185 one we see. */
94af9270
KS
11186 if (cu->language == language_ada)
11187 part_die->name = DW_STRING (&attr);
abc72ce4 11188 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
11189 break;
11190 case DW_AT_low_pc:
11191 has_low_pc_attr = 1;
11192 part_die->lowpc = DW_ADDR (&attr);
11193 break;
11194 case DW_AT_high_pc:
11195 has_high_pc_attr = 1;
3019eac3
DE
11196 if (attr.form == DW_FORM_addr
11197 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
11198 part_die->highpc = DW_ADDR (&attr);
11199 else
11200 {
11201 high_pc_relative = 1;
11202 part_die->highpc = DW_UNSND (&attr);
11203 }
c906108c
SS
11204 break;
11205 case DW_AT_location:
0963b4bd 11206 /* Support the .debug_loc offsets. */
8e19ed76
PS
11207 if (attr_form_is_block (&attr))
11208 {
95554aad 11209 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 11210 }
3690dd37 11211 else if (attr_form_is_section_offset (&attr))
8e19ed76 11212 {
4d3c2250 11213 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11214 }
11215 else
11216 {
4d3c2250
KB
11217 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11218 "partial symbol information");
8e19ed76 11219 }
c906108c 11220 break;
c906108c
SS
11221 case DW_AT_external:
11222 part_die->is_external = DW_UNSND (&attr);
11223 break;
11224 case DW_AT_declaration:
11225 part_die->is_declaration = DW_UNSND (&attr);
11226 break;
11227 case DW_AT_type:
11228 part_die->has_type = 1;
11229 break;
11230 case DW_AT_abstract_origin:
11231 case DW_AT_specification:
72bf9492
DJ
11232 case DW_AT_extension:
11233 part_die->has_specification = 1;
c764a876 11234 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
11235 break;
11236 case DW_AT_sibling:
11237 /* Ignore absolute siblings, they might point outside of
11238 the current compile unit. */
11239 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
11240 complaint (&symfile_complaints,
11241 _("ignoring absolute DW_AT_sibling"));
c906108c 11242 else
b64f50a1 11243 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 11244 break;
fa4028e9
JB
11245 case DW_AT_byte_size:
11246 part_die->has_byte_size = 1;
11247 break;
68511cec
CES
11248 case DW_AT_calling_convention:
11249 /* DWARF doesn't provide a way to identify a program's source-level
11250 entry point. DW_AT_calling_convention attributes are only meant
11251 to describe functions' calling conventions.
11252
11253 However, because it's a necessary piece of information in
11254 Fortran, and because DW_CC_program is the only piece of debugging
11255 information whose definition refers to a 'main program' at all,
11256 several compilers have begun marking Fortran main programs with
11257 DW_CC_program --- even when those functions use the standard
11258 calling conventions.
11259
11260 So until DWARF specifies a way to provide this information and
11261 compilers pick up the new representation, we'll support this
11262 practice. */
11263 if (DW_UNSND (&attr) == DW_CC_program
11264 && cu->language == language_fortran)
01f8c46d
JK
11265 {
11266 set_main_name (part_die->name);
11267
11268 /* As this DIE has a static linkage the name would be difficult
11269 to look up later. */
11270 language_of_main = language_fortran;
11271 }
68511cec 11272 break;
481860b3
GB
11273 case DW_AT_inline:
11274 if (DW_UNSND (&attr) == DW_INL_inlined
11275 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11276 part_die->may_be_inlined = 1;
11277 break;
95554aad
TT
11278
11279 case DW_AT_import:
11280 if (part_die->tag == DW_TAG_imported_unit)
11281 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11282 break;
11283
c906108c
SS
11284 default:
11285 break;
11286 }
11287 }
11288
91da1414
MW
11289 if (high_pc_relative)
11290 part_die->highpc += part_die->lowpc;
11291
9373cf26
JK
11292 if (has_low_pc_attr && has_high_pc_attr)
11293 {
11294 /* When using the GNU linker, .gnu.linkonce. sections are used to
11295 eliminate duplicate copies of functions and vtables and such.
11296 The linker will arbitrarily choose one and discard the others.
11297 The AT_*_pc values for such functions refer to local labels in
11298 these sections. If the section from that file was discarded, the
11299 labels are not in the output, so the relocs get a value of 0.
11300 If this is a discarded function, mark the pc bounds as invalid,
11301 so that GDB will ignore it. */
11302 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11303 {
bb5ed363 11304 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11305
11306 complaint (&symfile_complaints,
11307 _("DW_AT_low_pc %s is zero "
11308 "for DIE at 0x%x [in module %s]"),
11309 paddress (gdbarch, part_die->lowpc),
b64f50a1 11310 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11311 }
11312 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11313 else if (part_die->lowpc >= part_die->highpc)
11314 {
bb5ed363 11315 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11316
11317 complaint (&symfile_complaints,
11318 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11319 "for DIE at 0x%x [in module %s]"),
11320 paddress (gdbarch, part_die->lowpc),
11321 paddress (gdbarch, part_die->highpc),
b64f50a1 11322 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11323 }
11324 else
11325 part_die->has_pc_info = 1;
11326 }
85cbf3d3 11327
c906108c
SS
11328 return info_ptr;
11329}
11330
72bf9492
DJ
11331/* Find a cached partial DIE at OFFSET in CU. */
11332
11333static struct partial_die_info *
b64f50a1 11334find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
11335{
11336 struct partial_die_info *lookup_die = NULL;
11337 struct partial_die_info part_die;
11338
11339 part_die.offset = offset;
b64f50a1
JK
11340 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11341 offset.sect_off);
72bf9492 11342
72bf9492
DJ
11343 return lookup_die;
11344}
11345
348e048f
DE
11346/* Find a partial DIE at OFFSET, which may or may not be in CU,
11347 except in the case of .debug_types DIEs which do not reference
11348 outside their CU (they do however referencing other types via
55f1336d 11349 DW_FORM_ref_sig8). */
72bf9492
DJ
11350
11351static struct partial_die_info *
b64f50a1 11352find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
72bf9492 11353{
bb5ed363 11354 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
11355 struct dwarf2_per_cu_data *per_cu = NULL;
11356 struct partial_die_info *pd = NULL;
72bf9492 11357
45452591 11358 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
11359 {
11360 pd = find_partial_die_in_comp_unit (offset, cu);
11361 if (pd != NULL)
11362 return pd;
0d99eb77
DE
11363 /* We missed recording what we needed.
11364 Load all dies and try again. */
11365 per_cu = cu->per_cu;
5afb4e99 11366 }
0d99eb77
DE
11367 else
11368 {
11369 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 11370 if (cu->per_cu->is_debug_types)
0d99eb77
DE
11371 {
11372 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11373 " external reference to offset 0x%lx [in module %s].\n"),
11374 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11375 bfd_get_filename (objfile->obfd));
11376 }
11377 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
72bf9492 11378
0d99eb77
DE
11379 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11380 load_partial_comp_unit (per_cu);
ae038cb0 11381
0d99eb77
DE
11382 per_cu->cu->last_used = 0;
11383 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11384 }
5afb4e99 11385
dee91e82
DE
11386 /* If we didn't find it, and not all dies have been loaded,
11387 load them all and try again. */
11388
5afb4e99
DJ
11389 if (pd == NULL && per_cu->load_all_dies == 0)
11390 {
5afb4e99 11391 per_cu->load_all_dies = 1;
fd820528
DE
11392
11393 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11394 THIS_CU->cu may already be in use. So we can't just free it and
11395 replace its DIEs with the ones we read in. Instead, we leave those
11396 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11397 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11398 set. */
dee91e82 11399 load_partial_comp_unit (per_cu);
5afb4e99
DJ
11400
11401 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11402 }
11403
11404 if (pd == NULL)
11405 internal_error (__FILE__, __LINE__,
3e43a32a
MS
11406 _("could not find partial DIE 0x%x "
11407 "in cache [from module %s]\n"),
b64f50a1 11408 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 11409 return pd;
72bf9492
DJ
11410}
11411
abc72ce4
DE
11412/* See if we can figure out if the class lives in a namespace. We do
11413 this by looking for a member function; its demangled name will
11414 contain namespace info, if there is any. */
11415
11416static void
11417guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11418 struct dwarf2_cu *cu)
11419{
11420 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11421 what template types look like, because the demangler
11422 frequently doesn't give the same name as the debug info. We
11423 could fix this by only using the demangled name to get the
11424 prefix (but see comment in read_structure_type). */
11425
11426 struct partial_die_info *real_pdi;
11427 struct partial_die_info *child_pdi;
11428
11429 /* If this DIE (this DIE's specification, if any) has a parent, then
11430 we should not do this. We'll prepend the parent's fully qualified
11431 name when we create the partial symbol. */
11432
11433 real_pdi = struct_pdi;
11434 while (real_pdi->has_specification)
11435 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11436
11437 if (real_pdi->die_parent != NULL)
11438 return;
11439
11440 for (child_pdi = struct_pdi->die_child;
11441 child_pdi != NULL;
11442 child_pdi = child_pdi->die_sibling)
11443 {
11444 if (child_pdi->tag == DW_TAG_subprogram
11445 && child_pdi->linkage_name != NULL)
11446 {
11447 char *actual_class_name
11448 = language_class_name_from_physname (cu->language_defn,
11449 child_pdi->linkage_name);
11450 if (actual_class_name != NULL)
11451 {
11452 struct_pdi->name
11453 = obsavestring (actual_class_name,
11454 strlen (actual_class_name),
11455 &cu->objfile->objfile_obstack);
11456 xfree (actual_class_name);
11457 }
11458 break;
11459 }
11460 }
11461}
11462
72bf9492
DJ
11463/* Adjust PART_DIE before generating a symbol for it. This function
11464 may set the is_external flag or change the DIE's name. */
11465
11466static void
11467fixup_partial_die (struct partial_die_info *part_die,
11468 struct dwarf2_cu *cu)
11469{
abc72ce4
DE
11470 /* Once we've fixed up a die, there's no point in doing so again.
11471 This also avoids a memory leak if we were to call
11472 guess_partial_die_structure_name multiple times. */
11473 if (part_die->fixup_called)
11474 return;
11475
72bf9492
DJ
11476 /* If we found a reference attribute and the DIE has no name, try
11477 to find a name in the referred to DIE. */
11478
11479 if (part_die->name == NULL && part_die->has_specification)
11480 {
11481 struct partial_die_info *spec_die;
72bf9492 11482
10b3939b 11483 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 11484
10b3939b 11485 fixup_partial_die (spec_die, cu);
72bf9492
DJ
11486
11487 if (spec_die->name)
11488 {
11489 part_die->name = spec_die->name;
11490
11491 /* Copy DW_AT_external attribute if it is set. */
11492 if (spec_die->is_external)
11493 part_die->is_external = spec_die->is_external;
11494 }
11495 }
11496
11497 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
11498
11499 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 11500 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 11501
abc72ce4
DE
11502 /* If there is no parent die to provide a namespace, and there are
11503 children, see if we can determine the namespace from their linkage
122d1940 11504 name. */
abc72ce4 11505 if (cu->language == language_cplus
8b70b953 11506 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
11507 && part_die->die_parent == NULL
11508 && part_die->has_children
11509 && (part_die->tag == DW_TAG_class_type
11510 || part_die->tag == DW_TAG_structure_type
11511 || part_die->tag == DW_TAG_union_type))
11512 guess_partial_die_structure_name (part_die, cu);
11513
53832f31
TT
11514 /* GCC might emit a nameless struct or union that has a linkage
11515 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11516 if (part_die->name == NULL
96408a79
SA
11517 && (part_die->tag == DW_TAG_class_type
11518 || part_die->tag == DW_TAG_interface_type
11519 || part_die->tag == DW_TAG_structure_type
11520 || part_die->tag == DW_TAG_union_type)
53832f31
TT
11521 && part_die->linkage_name != NULL)
11522 {
11523 char *demangled;
11524
11525 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11526 if (demangled)
11527 {
96408a79
SA
11528 const char *base;
11529
11530 /* Strip any leading namespaces/classes, keep only the base name.
11531 DW_AT_name for named DIEs does not contain the prefixes. */
11532 base = strrchr (demangled, ':');
11533 if (base && base > demangled && base[-1] == ':')
11534 base++;
11535 else
11536 base = demangled;
11537
11538 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
11539 &cu->objfile->objfile_obstack);
11540 xfree (demangled);
11541 }
11542 }
11543
abc72ce4 11544 part_die->fixup_called = 1;
72bf9492
DJ
11545}
11546
a8329558 11547/* Read an attribute value described by an attribute form. */
c906108c 11548
fe1b8b76 11549static gdb_byte *
dee91e82
DE
11550read_attribute_value (const struct die_reader_specs *reader,
11551 struct attribute *attr, unsigned form,
11552 gdb_byte *info_ptr)
c906108c 11553{
dee91e82
DE
11554 struct dwarf2_cu *cu = reader->cu;
11555 bfd *abfd = reader->abfd;
e7c27a73 11556 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
11557 unsigned int bytes_read;
11558 struct dwarf_block *blk;
11559
a8329558
KW
11560 attr->form = form;
11561 switch (form)
c906108c 11562 {
c906108c 11563 case DW_FORM_ref_addr:
ae411497 11564 if (cu->header.version == 2)
4568ecf9 11565 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 11566 else
4568ecf9
DE
11567 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11568 &cu->header, &bytes_read);
ae411497
TT
11569 info_ptr += bytes_read;
11570 break;
11571 case DW_FORM_addr:
e7c27a73 11572 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 11573 info_ptr += bytes_read;
c906108c
SS
11574 break;
11575 case DW_FORM_block2:
7b5a2f43 11576 blk = dwarf_alloc_block (cu);
c906108c
SS
11577 blk->size = read_2_bytes (abfd, info_ptr);
11578 info_ptr += 2;
11579 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11580 info_ptr += blk->size;
11581 DW_BLOCK (attr) = blk;
11582 break;
11583 case DW_FORM_block4:
7b5a2f43 11584 blk = dwarf_alloc_block (cu);
c906108c
SS
11585 blk->size = read_4_bytes (abfd, info_ptr);
11586 info_ptr += 4;
11587 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11588 info_ptr += blk->size;
11589 DW_BLOCK (attr) = blk;
11590 break;
11591 case DW_FORM_data2:
11592 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11593 info_ptr += 2;
11594 break;
11595 case DW_FORM_data4:
11596 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11597 info_ptr += 4;
11598 break;
11599 case DW_FORM_data8:
11600 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11601 info_ptr += 8;
11602 break;
2dc7f7b3
TT
11603 case DW_FORM_sec_offset:
11604 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11605 info_ptr += bytes_read;
11606 break;
c906108c 11607 case DW_FORM_string:
9b1c24c8 11608 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 11609 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
11610 info_ptr += bytes_read;
11611 break;
4bdf3d34
JJ
11612 case DW_FORM_strp:
11613 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11614 &bytes_read);
8285870a 11615 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
11616 info_ptr += bytes_read;
11617 break;
2dc7f7b3 11618 case DW_FORM_exprloc:
c906108c 11619 case DW_FORM_block:
7b5a2f43 11620 blk = dwarf_alloc_block (cu);
c906108c
SS
11621 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11622 info_ptr += bytes_read;
11623 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11624 info_ptr += blk->size;
11625 DW_BLOCK (attr) = blk;
11626 break;
11627 case DW_FORM_block1:
7b5a2f43 11628 blk = dwarf_alloc_block (cu);
c906108c
SS
11629 blk->size = read_1_byte (abfd, info_ptr);
11630 info_ptr += 1;
11631 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11632 info_ptr += blk->size;
11633 DW_BLOCK (attr) = blk;
11634 break;
11635 case DW_FORM_data1:
11636 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11637 info_ptr += 1;
11638 break;
11639 case DW_FORM_flag:
11640 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11641 info_ptr += 1;
11642 break;
2dc7f7b3
TT
11643 case DW_FORM_flag_present:
11644 DW_UNSND (attr) = 1;
11645 break;
c906108c
SS
11646 case DW_FORM_sdata:
11647 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11648 info_ptr += bytes_read;
11649 break;
11650 case DW_FORM_udata:
11651 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11652 info_ptr += bytes_read;
11653 break;
11654 case DW_FORM_ref1:
4568ecf9
DE
11655 DW_UNSND (attr) = (cu->header.offset.sect_off
11656 + read_1_byte (abfd, info_ptr));
c906108c
SS
11657 info_ptr += 1;
11658 break;
11659 case DW_FORM_ref2:
4568ecf9
DE
11660 DW_UNSND (attr) = (cu->header.offset.sect_off
11661 + read_2_bytes (abfd, info_ptr));
c906108c
SS
11662 info_ptr += 2;
11663 break;
11664 case DW_FORM_ref4:
4568ecf9
DE
11665 DW_UNSND (attr) = (cu->header.offset.sect_off
11666 + read_4_bytes (abfd, info_ptr));
c906108c
SS
11667 info_ptr += 4;
11668 break;
613e1657 11669 case DW_FORM_ref8:
4568ecf9
DE
11670 DW_UNSND (attr) = (cu->header.offset.sect_off
11671 + read_8_bytes (abfd, info_ptr));
613e1657
KB
11672 info_ptr += 8;
11673 break;
55f1336d 11674 case DW_FORM_ref_sig8:
348e048f
DE
11675 /* Convert the signature to something we can record in DW_UNSND
11676 for later lookup.
11677 NOTE: This is NULL if the type wasn't found. */
11678 DW_SIGNATURED_TYPE (attr) =
e319fa28 11679 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
11680 info_ptr += 8;
11681 break;
c906108c 11682 case DW_FORM_ref_udata:
4568ecf9
DE
11683 DW_UNSND (attr) = (cu->header.offset.sect_off
11684 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
11685 info_ptr += bytes_read;
11686 break;
c906108c 11687 case DW_FORM_indirect:
a8329558
KW
11688 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11689 info_ptr += bytes_read;
dee91e82 11690 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 11691 break;
3019eac3
DE
11692 case DW_FORM_GNU_addr_index:
11693 if (reader->dwo_file == NULL)
11694 {
11695 /* For now flag a hard error.
11696 Later we can turn this into a complaint. */
11697 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11698 dwarf_form_name (form),
11699 bfd_get_filename (abfd));
11700 }
11701 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11702 info_ptr += bytes_read;
11703 break;
11704 case DW_FORM_GNU_str_index:
11705 if (reader->dwo_file == NULL)
11706 {
11707 /* For now flag a hard error.
11708 Later we can turn this into a complaint if warranted. */
11709 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11710 dwarf_form_name (form),
11711 bfd_get_filename (abfd));
11712 }
11713 {
11714 ULONGEST str_index =
11715 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11716
11717 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11718 DW_STRING_IS_CANONICAL (attr) = 0;
11719 info_ptr += bytes_read;
11720 }
11721 break;
c906108c 11722 default:
8a3fe4f8 11723 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
11724 dwarf_form_name (form),
11725 bfd_get_filename (abfd));
c906108c 11726 }
28e94949
JB
11727
11728 /* We have seen instances where the compiler tried to emit a byte
11729 size attribute of -1 which ended up being encoded as an unsigned
11730 0xffffffff. Although 0xffffffff is technically a valid size value,
11731 an object of this size seems pretty unlikely so we can relatively
11732 safely treat these cases as if the size attribute was invalid and
11733 treat them as zero by default. */
11734 if (attr->name == DW_AT_byte_size
11735 && form == DW_FORM_data4
11736 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
11737 {
11738 complaint
11739 (&symfile_complaints,
43bbcdc2
PH
11740 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
11741 hex_string (DW_UNSND (attr)));
01c66ae6
JB
11742 DW_UNSND (attr) = 0;
11743 }
28e94949 11744
c906108c
SS
11745 return info_ptr;
11746}
11747
a8329558
KW
11748/* Read an attribute described by an abbreviated attribute. */
11749
fe1b8b76 11750static gdb_byte *
dee91e82
DE
11751read_attribute (const struct die_reader_specs *reader,
11752 struct attribute *attr, struct attr_abbrev *abbrev,
11753 gdb_byte *info_ptr)
a8329558
KW
11754{
11755 attr->name = abbrev->name;
dee91e82 11756 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
11757}
11758
0963b4bd 11759/* Read dwarf information from a buffer. */
c906108c
SS
11760
11761static unsigned int
fe1b8b76 11762read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 11763{
fe1b8b76 11764 return bfd_get_8 (abfd, buf);
c906108c
SS
11765}
11766
11767static int
fe1b8b76 11768read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 11769{
fe1b8b76 11770 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
11771}
11772
11773static unsigned int
fe1b8b76 11774read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11775{
fe1b8b76 11776 return bfd_get_16 (abfd, buf);
c906108c
SS
11777}
11778
21ae7a4d
JK
11779static int
11780read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
11781{
11782 return bfd_get_signed_16 (abfd, buf);
11783}
11784
c906108c 11785static unsigned int
fe1b8b76 11786read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11787{
fe1b8b76 11788 return bfd_get_32 (abfd, buf);
c906108c
SS
11789}
11790
21ae7a4d
JK
11791static int
11792read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
11793{
11794 return bfd_get_signed_32 (abfd, buf);
11795}
11796
93311388 11797static ULONGEST
fe1b8b76 11798read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11799{
fe1b8b76 11800 return bfd_get_64 (abfd, buf);
c906108c
SS
11801}
11802
11803static CORE_ADDR
fe1b8b76 11804read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 11805 unsigned int *bytes_read)
c906108c 11806{
e7c27a73 11807 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
11808 CORE_ADDR retval = 0;
11809
107d2387 11810 if (cu_header->signed_addr_p)
c906108c 11811 {
107d2387
AC
11812 switch (cu_header->addr_size)
11813 {
11814 case 2:
fe1b8b76 11815 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
11816 break;
11817 case 4:
fe1b8b76 11818 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
11819 break;
11820 case 8:
fe1b8b76 11821 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
11822 break;
11823 default:
8e65ff28 11824 internal_error (__FILE__, __LINE__,
e2e0b3e5 11825 _("read_address: bad switch, signed [in module %s]"),
659b0389 11826 bfd_get_filename (abfd));
107d2387
AC
11827 }
11828 }
11829 else
11830 {
11831 switch (cu_header->addr_size)
11832 {
11833 case 2:
fe1b8b76 11834 retval = bfd_get_16 (abfd, buf);
107d2387
AC
11835 break;
11836 case 4:
fe1b8b76 11837 retval = bfd_get_32 (abfd, buf);
107d2387
AC
11838 break;
11839 case 8:
fe1b8b76 11840 retval = bfd_get_64 (abfd, buf);
107d2387
AC
11841 break;
11842 default:
8e65ff28 11843 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
11844 _("read_address: bad switch, "
11845 "unsigned [in module %s]"),
659b0389 11846 bfd_get_filename (abfd));
107d2387 11847 }
c906108c 11848 }
64367e0a 11849
107d2387
AC
11850 *bytes_read = cu_header->addr_size;
11851 return retval;
c906108c
SS
11852}
11853
f7ef9339 11854/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
11855 specification allows the initial length to take up either 4 bytes
11856 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
11857 bytes describe the length and all offsets will be 8 bytes in length
11858 instead of 4.
11859
f7ef9339
KB
11860 An older, non-standard 64-bit format is also handled by this
11861 function. The older format in question stores the initial length
11862 as an 8-byte quantity without an escape value. Lengths greater
11863 than 2^32 aren't very common which means that the initial 4 bytes
11864 is almost always zero. Since a length value of zero doesn't make
11865 sense for the 32-bit format, this initial zero can be considered to
11866 be an escape value which indicates the presence of the older 64-bit
11867 format. As written, the code can't detect (old format) lengths
917c78fc
MK
11868 greater than 4GB. If it becomes necessary to handle lengths
11869 somewhat larger than 4GB, we could allow other small values (such
11870 as the non-sensical values of 1, 2, and 3) to also be used as
11871 escape values indicating the presence of the old format.
f7ef9339 11872
917c78fc
MK
11873 The value returned via bytes_read should be used to increment the
11874 relevant pointer after calling read_initial_length().
c764a876 11875
613e1657
KB
11876 [ Note: read_initial_length() and read_offset() are based on the
11877 document entitled "DWARF Debugging Information Format", revision
f7ef9339 11878 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
11879 from:
11880
f7ef9339 11881 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 11882
613e1657
KB
11883 This document is only a draft and is subject to change. (So beware.)
11884
f7ef9339 11885 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
11886 determined empirically by examining 64-bit ELF files produced by
11887 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
11888
11889 - Kevin, July 16, 2002
613e1657
KB
11890 ] */
11891
11892static LONGEST
c764a876 11893read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 11894{
fe1b8b76 11895 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 11896
dd373385 11897 if (length == 0xffffffff)
613e1657 11898 {
fe1b8b76 11899 length = bfd_get_64 (abfd, buf + 4);
613e1657 11900 *bytes_read = 12;
613e1657 11901 }
dd373385 11902 else if (length == 0)
f7ef9339 11903 {
dd373385 11904 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 11905 length = bfd_get_64 (abfd, buf);
f7ef9339 11906 *bytes_read = 8;
f7ef9339 11907 }
613e1657
KB
11908 else
11909 {
11910 *bytes_read = 4;
613e1657
KB
11911 }
11912
c764a876
DE
11913 return length;
11914}
dd373385 11915
c764a876
DE
11916/* Cover function for read_initial_length.
11917 Returns the length of the object at BUF, and stores the size of the
11918 initial length in *BYTES_READ and stores the size that offsets will be in
11919 *OFFSET_SIZE.
11920 If the initial length size is not equivalent to that specified in
11921 CU_HEADER then issue a complaint.
11922 This is useful when reading non-comp-unit headers. */
dd373385 11923
c764a876
DE
11924static LONGEST
11925read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
11926 const struct comp_unit_head *cu_header,
11927 unsigned int *bytes_read,
11928 unsigned int *offset_size)
11929{
11930 LONGEST length = read_initial_length (abfd, buf, bytes_read);
11931
11932 gdb_assert (cu_header->initial_length_size == 4
11933 || cu_header->initial_length_size == 8
11934 || cu_header->initial_length_size == 12);
11935
11936 if (cu_header->initial_length_size != *bytes_read)
11937 complaint (&symfile_complaints,
11938 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 11939
c764a876 11940 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 11941 return length;
613e1657
KB
11942}
11943
11944/* Read an offset from the data stream. The size of the offset is
917c78fc 11945 given by cu_header->offset_size. */
613e1657
KB
11946
11947static LONGEST
fe1b8b76 11948read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 11949 unsigned int *bytes_read)
c764a876
DE
11950{
11951 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 11952
c764a876
DE
11953 *bytes_read = cu_header->offset_size;
11954 return offset;
11955}
11956
11957/* Read an offset from the data stream. */
11958
11959static LONGEST
11960read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
11961{
11962 LONGEST retval = 0;
11963
c764a876 11964 switch (offset_size)
613e1657
KB
11965 {
11966 case 4:
fe1b8b76 11967 retval = bfd_get_32 (abfd, buf);
613e1657
KB
11968 break;
11969 case 8:
fe1b8b76 11970 retval = bfd_get_64 (abfd, buf);
613e1657
KB
11971 break;
11972 default:
8e65ff28 11973 internal_error (__FILE__, __LINE__,
c764a876 11974 _("read_offset_1: bad switch [in module %s]"),
659b0389 11975 bfd_get_filename (abfd));
613e1657
KB
11976 }
11977
917c78fc 11978 return retval;
613e1657
KB
11979}
11980
fe1b8b76
JB
11981static gdb_byte *
11982read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
11983{
11984 /* If the size of a host char is 8 bits, we can return a pointer
11985 to the buffer, otherwise we have to copy the data to a buffer
11986 allocated on the temporary obstack. */
4bdf3d34 11987 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 11988 return buf;
c906108c
SS
11989}
11990
11991static char *
9b1c24c8 11992read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
11993{
11994 /* If the size of a host char is 8 bits, we can return a pointer
11995 to the string, otherwise we have to copy the string to a buffer
11996 allocated on the temporary obstack. */
4bdf3d34 11997 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
11998 if (*buf == '\0')
11999 {
12000 *bytes_read_ptr = 1;
12001 return NULL;
12002 }
fe1b8b76
JB
12003 *bytes_read_ptr = strlen ((char *) buf) + 1;
12004 return (char *) buf;
4bdf3d34
JJ
12005}
12006
12007static char *
cf2c3c16 12008read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 12009{
be391dca 12010 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 12011 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
12012 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12013 bfd_get_filename (abfd));
dce234bc 12014 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
12015 error (_("DW_FORM_strp pointing outside of "
12016 ".debug_str section [in module %s]"),
12017 bfd_get_filename (abfd));
4bdf3d34 12018 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 12019 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 12020 return NULL;
dce234bc 12021 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
12022}
12023
cf2c3c16
TT
12024static char *
12025read_indirect_string (bfd *abfd, gdb_byte *buf,
12026 const struct comp_unit_head *cu_header,
12027 unsigned int *bytes_read_ptr)
12028{
12029 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12030
12031 return read_indirect_string_at_offset (abfd, str_offset);
12032}
12033
12df843f 12034static ULONGEST
fe1b8b76 12035read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12036{
12df843f 12037 ULONGEST result;
ce5d95e1 12038 unsigned int num_read;
c906108c
SS
12039 int i, shift;
12040 unsigned char byte;
12041
12042 result = 0;
12043 shift = 0;
12044 num_read = 0;
12045 i = 0;
12046 while (1)
12047 {
fe1b8b76 12048 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12049 buf++;
12050 num_read++;
12df843f 12051 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
12052 if ((byte & 128) == 0)
12053 {
12054 break;
12055 }
12056 shift += 7;
12057 }
12058 *bytes_read_ptr = num_read;
12059 return result;
12060}
12061
12df843f 12062static LONGEST
fe1b8b76 12063read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12064{
12df843f 12065 LONGEST result;
77e0b926 12066 int i, shift, num_read;
c906108c
SS
12067 unsigned char byte;
12068
12069 result = 0;
12070 shift = 0;
c906108c
SS
12071 num_read = 0;
12072 i = 0;
12073 while (1)
12074 {
fe1b8b76 12075 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12076 buf++;
12077 num_read++;
12df843f 12078 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
12079 shift += 7;
12080 if ((byte & 128) == 0)
12081 {
12082 break;
12083 }
12084 }
77e0b926 12085 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 12086 result |= -(((LONGEST) 1) << shift);
c906108c
SS
12087 *bytes_read_ptr = num_read;
12088 return result;
12089}
12090
3019eac3
DE
12091/* Given index ADDR_INDEX in .debug_addr, fetch the value.
12092 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12093 ADDR_SIZE is the size of addresses from the CU header. */
12094
12095static CORE_ADDR
12096read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12097{
12098 struct objfile *objfile = dwarf2_per_objfile->objfile;
12099 bfd *abfd = objfile->obfd;
12100 const gdb_byte *info_ptr;
12101
12102 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12103 if (dwarf2_per_objfile->addr.buffer == NULL)
12104 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12105 objfile->name);
12106 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12107 error (_("DW_FORM_addr_index pointing outside of "
12108 ".debug_addr section [in module %s]"),
12109 objfile->name);
12110 info_ptr = (dwarf2_per_objfile->addr.buffer
12111 + addr_base + addr_index * addr_size);
12112 if (addr_size == 4)
12113 return bfd_get_32 (abfd, info_ptr);
12114 else
12115 return bfd_get_64 (abfd, info_ptr);
12116}
12117
12118/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12119
12120static CORE_ADDR
12121read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12122{
12123 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12124}
12125
12126/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12127
12128static CORE_ADDR
12129read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12130 unsigned int *bytes_read)
12131{
12132 bfd *abfd = cu->objfile->obfd;
12133 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12134
12135 return read_addr_index (cu, addr_index);
12136}
12137
12138/* Data structure to pass results from dwarf2_read_addr_index_reader
12139 back to dwarf2_read_addr_index. */
12140
12141struct dwarf2_read_addr_index_data
12142{
12143 ULONGEST addr_base;
12144 int addr_size;
12145};
12146
12147/* die_reader_func for dwarf2_read_addr_index. */
12148
12149static void
12150dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12151 gdb_byte *info_ptr,
12152 struct die_info *comp_unit_die,
12153 int has_children,
12154 void *data)
12155{
12156 struct dwarf2_cu *cu = reader->cu;
12157 struct dwarf2_read_addr_index_data *aidata =
12158 (struct dwarf2_read_addr_index_data *) data;
12159
12160 aidata->addr_base = cu->addr_base;
12161 aidata->addr_size = cu->header.addr_size;
12162}
12163
12164/* Given an index in .debug_addr, fetch the value.
12165 NOTE: This can be called during dwarf expression evaluation,
12166 long after the debug information has been read, and thus per_cu->cu
12167 may no longer exist. */
12168
12169CORE_ADDR
12170dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12171 unsigned int addr_index)
12172{
12173 struct objfile *objfile = per_cu->objfile;
12174 struct dwarf2_cu *cu = per_cu->cu;
12175 ULONGEST addr_base;
12176 int addr_size;
12177
12178 /* This is intended to be called from outside this file. */
12179 dw2_setup (objfile);
12180
12181 /* We need addr_base and addr_size.
12182 If we don't have PER_CU->cu, we have to get it.
12183 Nasty, but the alternative is storing the needed info in PER_CU,
12184 which at this point doesn't seem justified: it's not clear how frequently
12185 it would get used and it would increase the size of every PER_CU.
12186 Entry points like dwarf2_per_cu_addr_size do a similar thing
12187 so we're not in uncharted territory here.
12188 Alas we need to be a bit more complicated as addr_base is contained
12189 in the DIE.
12190
12191 We don't need to read the entire CU(/TU).
12192 We just need the header and top level die.
12193 IWBN to use the aging mechanism to let us lazily later discard the CU.
12194 See however init_cutu_and_read_dies_simple. */
12195
12196 if (cu != NULL)
12197 {
12198 addr_base = cu->addr_base;
12199 addr_size = cu->header.addr_size;
12200 }
12201 else
12202 {
12203 struct dwarf2_read_addr_index_data aidata;
12204
12205 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12206 &aidata);
12207 addr_base = aidata.addr_base;
12208 addr_size = aidata.addr_size;
12209 }
12210
12211 return read_addr_index_1 (addr_index, addr_base, addr_size);
12212}
12213
12214/* Given a DW_AT_str_index, fetch the string. */
12215
12216static char *
12217read_str_index (const struct die_reader_specs *reader,
12218 struct dwarf2_cu *cu, ULONGEST str_index)
12219{
12220 struct objfile *objfile = dwarf2_per_objfile->objfile;
12221 const char *dwo_name = objfile->name;
12222 bfd *abfd = objfile->obfd;
12223 struct dwo_sections *sections = &reader->dwo_file->sections;
12224 gdb_byte *info_ptr;
12225 ULONGEST str_offset;
12226
12227 dwarf2_read_section (objfile, &sections->str);
12228 dwarf2_read_section (objfile, &sections->str_offsets);
12229 if (sections->str.buffer == NULL)
12230 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12231 " in CU at offset 0x%lx [in module %s]"),
12232 (long) cu->header.offset.sect_off, dwo_name);
12233 if (sections->str_offsets.buffer == NULL)
12234 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12235 " in CU at offset 0x%lx [in module %s]"),
12236 (long) cu->header.offset.sect_off, dwo_name);
12237 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12238 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12239 " section in CU at offset 0x%lx [in module %s]"),
12240 (long) cu->header.offset.sect_off, dwo_name);
12241 info_ptr = (sections->str_offsets.buffer
12242 + str_index * cu->header.offset_size);
12243 if (cu->header.offset_size == 4)
12244 str_offset = bfd_get_32 (abfd, info_ptr);
12245 else
12246 str_offset = bfd_get_64 (abfd, info_ptr);
12247 if (str_offset >= sections->str.size)
12248 error (_("Offset from DW_FORM_str_index pointing outside of"
12249 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12250 (long) cu->header.offset.sect_off, dwo_name);
12251 return (char *) (sections->str.buffer + str_offset);
12252}
12253
4bb7a0a7
DJ
12254/* Return a pointer to just past the end of an LEB128 number in BUF. */
12255
fe1b8b76
JB
12256static gdb_byte *
12257skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
12258{
12259 int byte;
12260
12261 while (1)
12262 {
fe1b8b76 12263 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
12264 buf++;
12265 if ((byte & 128) == 0)
12266 return buf;
12267 }
12268}
12269
3019eac3
DE
12270/* Return the length of an LEB128 number in BUF. */
12271
12272static int
12273leb128_size (const gdb_byte *buf)
12274{
12275 const gdb_byte *begin = buf;
12276 gdb_byte byte;
12277
12278 while (1)
12279 {
12280 byte = *buf++;
12281 if ((byte & 128) == 0)
12282 return buf - begin;
12283 }
12284}
12285
c906108c 12286static void
e142c38c 12287set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
12288{
12289 switch (lang)
12290 {
12291 case DW_LANG_C89:
76bee0cc 12292 case DW_LANG_C99:
c906108c 12293 case DW_LANG_C:
e142c38c 12294 cu->language = language_c;
c906108c
SS
12295 break;
12296 case DW_LANG_C_plus_plus:
e142c38c 12297 cu->language = language_cplus;
c906108c 12298 break;
6aecb9c2
JB
12299 case DW_LANG_D:
12300 cu->language = language_d;
12301 break;
c906108c
SS
12302 case DW_LANG_Fortran77:
12303 case DW_LANG_Fortran90:
b21b22e0 12304 case DW_LANG_Fortran95:
e142c38c 12305 cu->language = language_fortran;
c906108c 12306 break;
a766d390
DE
12307 case DW_LANG_Go:
12308 cu->language = language_go;
12309 break;
c906108c 12310 case DW_LANG_Mips_Assembler:
e142c38c 12311 cu->language = language_asm;
c906108c 12312 break;
bebd888e 12313 case DW_LANG_Java:
e142c38c 12314 cu->language = language_java;
bebd888e 12315 break;
c906108c 12316 case DW_LANG_Ada83:
8aaf0b47 12317 case DW_LANG_Ada95:
bc5f45f8
JB
12318 cu->language = language_ada;
12319 break;
72019c9c
GM
12320 case DW_LANG_Modula2:
12321 cu->language = language_m2;
12322 break;
fe8e67fd
PM
12323 case DW_LANG_Pascal83:
12324 cu->language = language_pascal;
12325 break;
22566fbd
DJ
12326 case DW_LANG_ObjC:
12327 cu->language = language_objc;
12328 break;
c906108c
SS
12329 case DW_LANG_Cobol74:
12330 case DW_LANG_Cobol85:
c906108c 12331 default:
e142c38c 12332 cu->language = language_minimal;
c906108c
SS
12333 break;
12334 }
e142c38c 12335 cu->language_defn = language_def (cu->language);
c906108c
SS
12336}
12337
12338/* Return the named attribute or NULL if not there. */
12339
12340static struct attribute *
e142c38c 12341dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 12342{
a48e046c 12343 for (;;)
c906108c 12344 {
a48e046c
TT
12345 unsigned int i;
12346 struct attribute *spec = NULL;
12347
12348 for (i = 0; i < die->num_attrs; ++i)
12349 {
12350 if (die->attrs[i].name == name)
12351 return &die->attrs[i];
12352 if (die->attrs[i].name == DW_AT_specification
12353 || die->attrs[i].name == DW_AT_abstract_origin)
12354 spec = &die->attrs[i];
12355 }
12356
12357 if (!spec)
12358 break;
c906108c 12359
f2f0e013 12360 die = follow_die_ref (die, spec, &cu);
f2f0e013 12361 }
c5aa993b 12362
c906108c
SS
12363 return NULL;
12364}
12365
348e048f
DE
12366/* Return the named attribute or NULL if not there,
12367 but do not follow DW_AT_specification, etc.
12368 This is for use in contexts where we're reading .debug_types dies.
12369 Following DW_AT_specification, DW_AT_abstract_origin will take us
12370 back up the chain, and we want to go down. */
12371
12372static struct attribute *
12373dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12374 struct dwarf2_cu *cu)
12375{
12376 unsigned int i;
12377
12378 for (i = 0; i < die->num_attrs; ++i)
12379 if (die->attrs[i].name == name)
12380 return &die->attrs[i];
12381
12382 return NULL;
12383}
12384
05cf31d1
JB
12385/* Return non-zero iff the attribute NAME is defined for the given DIE,
12386 and holds a non-zero value. This function should only be used for
2dc7f7b3 12387 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
12388
12389static int
12390dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12391{
12392 struct attribute *attr = dwarf2_attr (die, name, cu);
12393
12394 return (attr && DW_UNSND (attr));
12395}
12396
3ca72b44 12397static int
e142c38c 12398die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 12399{
05cf31d1
JB
12400 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12401 which value is non-zero. However, we have to be careful with
12402 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12403 (via dwarf2_flag_true_p) follows this attribute. So we may
12404 end up accidently finding a declaration attribute that belongs
12405 to a different DIE referenced by the specification attribute,
12406 even though the given DIE does not have a declaration attribute. */
12407 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12408 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
12409}
12410
63d06c5c 12411/* Return the die giving the specification for DIE, if there is
f2f0e013 12412 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
12413 containing the return value on output. If there is no
12414 specification, but there is an abstract origin, that is
12415 returned. */
63d06c5c
DC
12416
12417static struct die_info *
f2f0e013 12418die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 12419{
f2f0e013
DJ
12420 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12421 *spec_cu);
63d06c5c 12422
edb3359d
DJ
12423 if (spec_attr == NULL)
12424 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12425
63d06c5c
DC
12426 if (spec_attr == NULL)
12427 return NULL;
12428 else
f2f0e013 12429 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 12430}
c906108c 12431
debd256d 12432/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
12433 refers to.
12434 NOTE: This is also used as a "cleanup" function. */
12435
debd256d
JB
12436static void
12437free_line_header (struct line_header *lh)
12438{
12439 if (lh->standard_opcode_lengths)
a8bc7b56 12440 xfree (lh->standard_opcode_lengths);
debd256d
JB
12441
12442 /* Remember that all the lh->file_names[i].name pointers are
12443 pointers into debug_line_buffer, and don't need to be freed. */
12444 if (lh->file_names)
a8bc7b56 12445 xfree (lh->file_names);
debd256d
JB
12446
12447 /* Similarly for the include directory names. */
12448 if (lh->include_dirs)
a8bc7b56 12449 xfree (lh->include_dirs);
debd256d 12450
a8bc7b56 12451 xfree (lh);
debd256d
JB
12452}
12453
debd256d 12454/* Add an entry to LH's include directory table. */
ae2de4f8 12455
debd256d
JB
12456static void
12457add_include_dir (struct line_header *lh, char *include_dir)
c906108c 12458{
debd256d
JB
12459 /* Grow the array if necessary. */
12460 if (lh->include_dirs_size == 0)
c5aa993b 12461 {
debd256d
JB
12462 lh->include_dirs_size = 1; /* for testing */
12463 lh->include_dirs = xmalloc (lh->include_dirs_size
12464 * sizeof (*lh->include_dirs));
12465 }
12466 else if (lh->num_include_dirs >= lh->include_dirs_size)
12467 {
12468 lh->include_dirs_size *= 2;
12469 lh->include_dirs = xrealloc (lh->include_dirs,
12470 (lh->include_dirs_size
12471 * sizeof (*lh->include_dirs)));
c5aa993b 12472 }
c906108c 12473
debd256d
JB
12474 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12475}
6e70227d 12476
debd256d 12477/* Add an entry to LH's file name table. */
ae2de4f8 12478
debd256d
JB
12479static void
12480add_file_name (struct line_header *lh,
12481 char *name,
12482 unsigned int dir_index,
12483 unsigned int mod_time,
12484 unsigned int length)
12485{
12486 struct file_entry *fe;
12487
12488 /* Grow the array if necessary. */
12489 if (lh->file_names_size == 0)
12490 {
12491 lh->file_names_size = 1; /* for testing */
12492 lh->file_names = xmalloc (lh->file_names_size
12493 * sizeof (*lh->file_names));
12494 }
12495 else if (lh->num_file_names >= lh->file_names_size)
12496 {
12497 lh->file_names_size *= 2;
12498 lh->file_names = xrealloc (lh->file_names,
12499 (lh->file_names_size
12500 * sizeof (*lh->file_names)));
12501 }
12502
12503 fe = &lh->file_names[lh->num_file_names++];
12504 fe->name = name;
12505 fe->dir_index = dir_index;
12506 fe->mod_time = mod_time;
12507 fe->length = length;
aaa75496 12508 fe->included_p = 0;
cb1df416 12509 fe->symtab = NULL;
debd256d 12510}
6e70227d 12511
debd256d 12512/* Read the statement program header starting at OFFSET in
3019eac3 12513 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 12514 to a struct line_header, allocated using xmalloc.
debd256d
JB
12515
12516 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
12517 the returned object point into the dwarf line section buffer,
12518 and must not be freed. */
ae2de4f8 12519
debd256d 12520static struct line_header *
3019eac3 12521dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
12522{
12523 struct cleanup *back_to;
12524 struct line_header *lh;
fe1b8b76 12525 gdb_byte *line_ptr;
c764a876 12526 unsigned int bytes_read, offset_size;
debd256d
JB
12527 int i;
12528 char *cur_dir, *cur_file;
3019eac3
DE
12529 struct dwarf2_section_info *section;
12530 bfd *abfd;
12531
12532 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12533 DWO file. */
12534 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12535 section = &cu->dwo_unit->dwo_file->sections.line;
12536 else
12537 section = &dwarf2_per_objfile->line;
debd256d 12538
3019eac3
DE
12539 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12540 if (section->buffer == NULL)
debd256d 12541 {
3019eac3
DE
12542 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12543 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12544 else
12545 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
12546 return 0;
12547 }
12548
fceca515
DE
12549 /* We can't do this until we know the section is non-empty.
12550 Only then do we know we have such a section. */
12551 abfd = section->asection->owner;
12552
a738430d
MK
12553 /* Make sure that at least there's room for the total_length field.
12554 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 12555 if (offset + 4 >= section->size)
debd256d 12556 {
4d3c2250 12557 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12558 return 0;
12559 }
12560
12561 lh = xmalloc (sizeof (*lh));
12562 memset (lh, 0, sizeof (*lh));
12563 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12564 (void *) lh);
12565
3019eac3 12566 line_ptr = section->buffer + offset;
debd256d 12567
a738430d 12568 /* Read in the header. */
6e70227d 12569 lh->total_length =
c764a876
DE
12570 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12571 &bytes_read, &offset_size);
debd256d 12572 line_ptr += bytes_read;
3019eac3 12573 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 12574 {
4d3c2250 12575 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12576 return 0;
12577 }
12578 lh->statement_program_end = line_ptr + lh->total_length;
12579 lh->version = read_2_bytes (abfd, line_ptr);
12580 line_ptr += 2;
c764a876
DE
12581 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12582 line_ptr += offset_size;
debd256d
JB
12583 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12584 line_ptr += 1;
2dc7f7b3
TT
12585 if (lh->version >= 4)
12586 {
12587 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12588 line_ptr += 1;
12589 }
12590 else
12591 lh->maximum_ops_per_instruction = 1;
12592
12593 if (lh->maximum_ops_per_instruction == 0)
12594 {
12595 lh->maximum_ops_per_instruction = 1;
12596 complaint (&symfile_complaints,
3e43a32a
MS
12597 _("invalid maximum_ops_per_instruction "
12598 "in `.debug_line' section"));
2dc7f7b3
TT
12599 }
12600
debd256d
JB
12601 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12602 line_ptr += 1;
12603 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12604 line_ptr += 1;
12605 lh->line_range = read_1_byte (abfd, line_ptr);
12606 line_ptr += 1;
12607 lh->opcode_base = read_1_byte (abfd, line_ptr);
12608 line_ptr += 1;
12609 lh->standard_opcode_lengths
fe1b8b76 12610 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
12611
12612 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12613 for (i = 1; i < lh->opcode_base; ++i)
12614 {
12615 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12616 line_ptr += 1;
12617 }
12618
a738430d 12619 /* Read directory table. */
9b1c24c8 12620 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12621 {
12622 line_ptr += bytes_read;
12623 add_include_dir (lh, cur_dir);
12624 }
12625 line_ptr += bytes_read;
12626
a738430d 12627 /* Read file name table. */
9b1c24c8 12628 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12629 {
12630 unsigned int dir_index, mod_time, length;
12631
12632 line_ptr += bytes_read;
12633 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12634 line_ptr += bytes_read;
12635 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12636 line_ptr += bytes_read;
12637 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12638 line_ptr += bytes_read;
12639
12640 add_file_name (lh, cur_file, dir_index, mod_time, length);
12641 }
12642 line_ptr += bytes_read;
6e70227d 12643 lh->statement_program_start = line_ptr;
debd256d 12644
3019eac3 12645 if (line_ptr > (section->buffer + section->size))
4d3c2250 12646 complaint (&symfile_complaints,
3e43a32a
MS
12647 _("line number info header doesn't "
12648 "fit in `.debug_line' section"));
debd256d
JB
12649
12650 discard_cleanups (back_to);
12651 return lh;
12652}
c906108c 12653
c6da4cef
DE
12654/* Subroutine of dwarf_decode_lines to simplify it.
12655 Return the file name of the psymtab for included file FILE_INDEX
12656 in line header LH of PST.
12657 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12658 If space for the result is malloc'd, it will be freed by a cleanup.
12659 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12660
12661static char *
12662psymtab_include_file_name (const struct line_header *lh, int file_index,
12663 const struct partial_symtab *pst,
12664 const char *comp_dir)
12665{
12666 const struct file_entry fe = lh->file_names [file_index];
12667 char *include_name = fe.name;
12668 char *include_name_to_compare = include_name;
12669 char *dir_name = NULL;
72b9f47f
TT
12670 const char *pst_filename;
12671 char *copied_name = NULL;
c6da4cef
DE
12672 int file_is_pst;
12673
12674 if (fe.dir_index)
12675 dir_name = lh->include_dirs[fe.dir_index - 1];
12676
12677 if (!IS_ABSOLUTE_PATH (include_name)
12678 && (dir_name != NULL || comp_dir != NULL))
12679 {
12680 /* Avoid creating a duplicate psymtab for PST.
12681 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12682 Before we do the comparison, however, we need to account
12683 for DIR_NAME and COMP_DIR.
12684 First prepend dir_name (if non-NULL). If we still don't
12685 have an absolute path prepend comp_dir (if non-NULL).
12686 However, the directory we record in the include-file's
12687 psymtab does not contain COMP_DIR (to match the
12688 corresponding symtab(s)).
12689
12690 Example:
12691
12692 bash$ cd /tmp
12693 bash$ gcc -g ./hello.c
12694 include_name = "hello.c"
12695 dir_name = "."
12696 DW_AT_comp_dir = comp_dir = "/tmp"
12697 DW_AT_name = "./hello.c" */
12698
12699 if (dir_name != NULL)
12700 {
12701 include_name = concat (dir_name, SLASH_STRING,
12702 include_name, (char *)NULL);
12703 include_name_to_compare = include_name;
12704 make_cleanup (xfree, include_name);
12705 }
12706 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12707 {
12708 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12709 include_name, (char *)NULL);
12710 }
12711 }
12712
12713 pst_filename = pst->filename;
12714 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12715 {
72b9f47f
TT
12716 copied_name = concat (pst->dirname, SLASH_STRING,
12717 pst_filename, (char *)NULL);
12718 pst_filename = copied_name;
c6da4cef
DE
12719 }
12720
1e3fad37 12721 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
12722
12723 if (include_name_to_compare != include_name)
12724 xfree (include_name_to_compare);
72b9f47f
TT
12725 if (copied_name != NULL)
12726 xfree (copied_name);
c6da4cef
DE
12727
12728 if (file_is_pst)
12729 return NULL;
12730 return include_name;
12731}
12732
c91513d8
PP
12733/* Ignore this record_line request. */
12734
12735static void
12736noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12737{
12738 return;
12739}
12740
f3f5162e
DE
12741/* Subroutine of dwarf_decode_lines to simplify it.
12742 Process the line number information in LH. */
debd256d 12743
c906108c 12744static void
f3f5162e
DE
12745dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
12746 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 12747{
a8c50c1f 12748 gdb_byte *line_ptr, *extended_end;
fe1b8b76 12749 gdb_byte *line_end;
a8c50c1f 12750 unsigned int bytes_read, extended_len;
c906108c 12751 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
12752 CORE_ADDR baseaddr;
12753 struct objfile *objfile = cu->objfile;
f3f5162e 12754 bfd *abfd = objfile->obfd;
fbf65064 12755 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 12756 const int decode_for_pst_p = (pst != NULL);
f3f5162e 12757 struct subfile *last_subfile = NULL;
c91513d8
PP
12758 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
12759 = record_line;
e142c38c
DJ
12760
12761 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 12762
debd256d
JB
12763 line_ptr = lh->statement_program_start;
12764 line_end = lh->statement_program_end;
c906108c
SS
12765
12766 /* Read the statement sequences until there's nothing left. */
12767 while (line_ptr < line_end)
12768 {
12769 /* state machine registers */
12770 CORE_ADDR address = 0;
12771 unsigned int file = 1;
12772 unsigned int line = 1;
12773 unsigned int column = 0;
debd256d 12774 int is_stmt = lh->default_is_stmt;
c906108c
SS
12775 int basic_block = 0;
12776 int end_sequence = 0;
fbf65064 12777 CORE_ADDR addr;
2dc7f7b3 12778 unsigned char op_index = 0;
c906108c 12779
aaa75496 12780 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 12781 {
aaa75496 12782 /* Start a subfile for the current file of the state machine. */
debd256d
JB
12783 /* lh->include_dirs and lh->file_names are 0-based, but the
12784 directory and file name numbers in the statement program
12785 are 1-based. */
12786 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 12787 char *dir = NULL;
a738430d 12788
debd256d
JB
12789 if (fe->dir_index)
12790 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
12791
12792 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
12793 }
12794
a738430d 12795 /* Decode the table. */
c5aa993b 12796 while (!end_sequence)
c906108c
SS
12797 {
12798 op_code = read_1_byte (abfd, line_ptr);
12799 line_ptr += 1;
59205f5a
JB
12800 if (line_ptr > line_end)
12801 {
12802 dwarf2_debug_line_missing_end_sequence_complaint ();
12803 break;
12804 }
9aa1fe7e 12805
debd256d 12806 if (op_code >= lh->opcode_base)
6e70227d 12807 {
a738430d 12808 /* Special operand. */
debd256d 12809 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
12810 address += (((op_index + (adj_opcode / lh->line_range))
12811 / lh->maximum_ops_per_instruction)
12812 * lh->minimum_instruction_length);
12813 op_index = ((op_index + (adj_opcode / lh->line_range))
12814 % lh->maximum_ops_per_instruction);
debd256d 12815 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 12816 if (lh->num_file_names < file || file == 0)
25e43795 12817 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
12818 /* For now we ignore lines not starting on an
12819 instruction boundary. */
12820 else if (op_index == 0)
25e43795
DJ
12821 {
12822 lh->file_names[file - 1].included_p = 1;
ca5f395d 12823 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
12824 {
12825 if (last_subfile != current_subfile)
12826 {
12827 addr = gdbarch_addr_bits_remove (gdbarch, address);
12828 if (last_subfile)
c91513d8 12829 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
12830 last_subfile = current_subfile;
12831 }
25e43795 12832 /* Append row to matrix using current values. */
7019d805 12833 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 12834 (*p_record_line) (current_subfile, line, addr);
366da635 12835 }
25e43795 12836 }
ca5f395d 12837 basic_block = 0;
9aa1fe7e
GK
12838 }
12839 else switch (op_code)
c906108c
SS
12840 {
12841 case DW_LNS_extended_op:
3e43a32a
MS
12842 extended_len = read_unsigned_leb128 (abfd, line_ptr,
12843 &bytes_read);
473b7be6 12844 line_ptr += bytes_read;
a8c50c1f 12845 extended_end = line_ptr + extended_len;
c906108c
SS
12846 extended_op = read_1_byte (abfd, line_ptr);
12847 line_ptr += 1;
12848 switch (extended_op)
12849 {
12850 case DW_LNE_end_sequence:
c91513d8 12851 p_record_line = record_line;
c906108c 12852 end_sequence = 1;
c906108c
SS
12853 break;
12854 case DW_LNE_set_address:
e7c27a73 12855 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
12856
12857 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
12858 {
12859 /* This line table is for a function which has been
12860 GCd by the linker. Ignore it. PR gdb/12528 */
12861
12862 long line_offset
12863 = line_ptr - dwarf2_per_objfile->line.buffer;
12864
12865 complaint (&symfile_complaints,
12866 _(".debug_line address at offset 0x%lx is 0 "
12867 "[in module %s]"),
bb5ed363 12868 line_offset, objfile->name);
c91513d8
PP
12869 p_record_line = noop_record_line;
12870 }
12871
2dc7f7b3 12872 op_index = 0;
107d2387
AC
12873 line_ptr += bytes_read;
12874 address += baseaddr;
c906108c
SS
12875 break;
12876 case DW_LNE_define_file:
debd256d
JB
12877 {
12878 char *cur_file;
12879 unsigned int dir_index, mod_time, length;
6e70227d 12880
3e43a32a
MS
12881 cur_file = read_direct_string (abfd, line_ptr,
12882 &bytes_read);
debd256d
JB
12883 line_ptr += bytes_read;
12884 dir_index =
12885 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12886 line_ptr += bytes_read;
12887 mod_time =
12888 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12889 line_ptr += bytes_read;
12890 length =
12891 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12892 line_ptr += bytes_read;
12893 add_file_name (lh, cur_file, dir_index, mod_time, length);
12894 }
c906108c 12895 break;
d0c6ba3d
CC
12896 case DW_LNE_set_discriminator:
12897 /* The discriminator is not interesting to the debugger;
12898 just ignore it. */
12899 line_ptr = extended_end;
12900 break;
c906108c 12901 default:
4d3c2250 12902 complaint (&symfile_complaints,
e2e0b3e5 12903 _("mangled .debug_line section"));
debd256d 12904 return;
c906108c 12905 }
a8c50c1f
DJ
12906 /* Make sure that we parsed the extended op correctly. If e.g.
12907 we expected a different address size than the producer used,
12908 we may have read the wrong number of bytes. */
12909 if (line_ptr != extended_end)
12910 {
12911 complaint (&symfile_complaints,
12912 _("mangled .debug_line section"));
12913 return;
12914 }
c906108c
SS
12915 break;
12916 case DW_LNS_copy:
59205f5a 12917 if (lh->num_file_names < file || file == 0)
25e43795
DJ
12918 dwarf2_debug_line_missing_file_complaint ();
12919 else
366da635 12920 {
25e43795 12921 lh->file_names[file - 1].included_p = 1;
ca5f395d 12922 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
12923 {
12924 if (last_subfile != current_subfile)
12925 {
12926 addr = gdbarch_addr_bits_remove (gdbarch, address);
12927 if (last_subfile)
c91513d8 12928 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
12929 last_subfile = current_subfile;
12930 }
7019d805 12931 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 12932 (*p_record_line) (current_subfile, line, addr);
fbf65064 12933 }
366da635 12934 }
c906108c
SS
12935 basic_block = 0;
12936 break;
12937 case DW_LNS_advance_pc:
2dc7f7b3
TT
12938 {
12939 CORE_ADDR adjust
12940 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12941
12942 address += (((op_index + adjust)
12943 / lh->maximum_ops_per_instruction)
12944 * lh->minimum_instruction_length);
12945 op_index = ((op_index + adjust)
12946 % lh->maximum_ops_per_instruction);
12947 line_ptr += bytes_read;
12948 }
c906108c
SS
12949 break;
12950 case DW_LNS_advance_line:
12951 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
12952 line_ptr += bytes_read;
12953 break;
12954 case DW_LNS_set_file:
debd256d 12955 {
a738430d
MK
12956 /* The arrays lh->include_dirs and lh->file_names are
12957 0-based, but the directory and file name numbers in
12958 the statement program are 1-based. */
debd256d 12959 struct file_entry *fe;
4f1520fb 12960 char *dir = NULL;
a738430d 12961
debd256d
JB
12962 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12963 line_ptr += bytes_read;
59205f5a 12964 if (lh->num_file_names < file || file == 0)
25e43795
DJ
12965 dwarf2_debug_line_missing_file_complaint ();
12966 else
12967 {
12968 fe = &lh->file_names[file - 1];
12969 if (fe->dir_index)
12970 dir = lh->include_dirs[fe->dir_index - 1];
12971 if (!decode_for_pst_p)
12972 {
12973 last_subfile = current_subfile;
12974 dwarf2_start_subfile (fe->name, dir, comp_dir);
12975 }
12976 }
debd256d 12977 }
c906108c
SS
12978 break;
12979 case DW_LNS_set_column:
12980 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12981 line_ptr += bytes_read;
12982 break;
12983 case DW_LNS_negate_stmt:
12984 is_stmt = (!is_stmt);
12985 break;
12986 case DW_LNS_set_basic_block:
12987 basic_block = 1;
12988 break;
c2c6d25f
JM
12989 /* Add to the address register of the state machine the
12990 address increment value corresponding to special opcode
a738430d
MK
12991 255. I.e., this value is scaled by the minimum
12992 instruction length since special opcode 255 would have
b021a221 12993 scaled the increment. */
c906108c 12994 case DW_LNS_const_add_pc:
2dc7f7b3
TT
12995 {
12996 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
12997
12998 address += (((op_index + adjust)
12999 / lh->maximum_ops_per_instruction)
13000 * lh->minimum_instruction_length);
13001 op_index = ((op_index + adjust)
13002 % lh->maximum_ops_per_instruction);
13003 }
c906108c
SS
13004 break;
13005 case DW_LNS_fixed_advance_pc:
13006 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 13007 op_index = 0;
c906108c
SS
13008 line_ptr += 2;
13009 break;
9aa1fe7e 13010 default:
a738430d
MK
13011 {
13012 /* Unknown standard opcode, ignore it. */
9aa1fe7e 13013 int i;
a738430d 13014
debd256d 13015 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
13016 {
13017 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13018 line_ptr += bytes_read;
13019 }
13020 }
c906108c
SS
13021 }
13022 }
59205f5a
JB
13023 if (lh->num_file_names < file || file == 0)
13024 dwarf2_debug_line_missing_file_complaint ();
13025 else
13026 {
13027 lh->file_names[file - 1].included_p = 1;
13028 if (!decode_for_pst_p)
fbf65064
UW
13029 {
13030 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13031 (*p_record_line) (current_subfile, 0, addr);
fbf65064 13032 }
59205f5a 13033 }
c906108c 13034 }
f3f5162e
DE
13035}
13036
13037/* Decode the Line Number Program (LNP) for the given line_header
13038 structure and CU. The actual information extracted and the type
13039 of structures created from the LNP depends on the value of PST.
13040
13041 1. If PST is NULL, then this procedure uses the data from the program
13042 to create all necessary symbol tables, and their linetables.
13043
13044 2. If PST is not NULL, this procedure reads the program to determine
13045 the list of files included by the unit represented by PST, and
13046 builds all the associated partial symbol tables.
13047
13048 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13049 It is used for relative paths in the line table.
13050 NOTE: When processing partial symtabs (pst != NULL),
13051 comp_dir == pst->dirname.
13052
13053 NOTE: It is important that psymtabs have the same file name (via strcmp)
13054 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13055 symtab we don't use it in the name of the psymtabs we create.
13056 E.g. expand_line_sal requires this when finding psymtabs to expand.
13057 A good testcase for this is mb-inline.exp. */
13058
13059static void
13060dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13061 struct dwarf2_cu *cu, struct partial_symtab *pst,
13062 int want_line_info)
13063{
13064 struct objfile *objfile = cu->objfile;
13065 const int decode_for_pst_p = (pst != NULL);
13066 struct subfile *first_subfile = current_subfile;
13067
13068 if (want_line_info)
13069 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
13070
13071 if (decode_for_pst_p)
13072 {
13073 int file_index;
13074
13075 /* Now that we're done scanning the Line Header Program, we can
13076 create the psymtab of each included file. */
13077 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13078 if (lh->file_names[file_index].included_p == 1)
13079 {
c6da4cef
DE
13080 char *include_name =
13081 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13082 if (include_name != NULL)
aaa75496
JB
13083 dwarf2_create_include_psymtab (include_name, pst, objfile);
13084 }
13085 }
cb1df416
DJ
13086 else
13087 {
13088 /* Make sure a symtab is created for every file, even files
13089 which contain only variables (i.e. no code with associated
13090 line numbers). */
cb1df416 13091 int i;
cb1df416
DJ
13092
13093 for (i = 0; i < lh->num_file_names; i++)
13094 {
13095 char *dir = NULL;
f3f5162e 13096 struct file_entry *fe;
9a619af0 13097
cb1df416
DJ
13098 fe = &lh->file_names[i];
13099 if (fe->dir_index)
13100 dir = lh->include_dirs[fe->dir_index - 1];
13101 dwarf2_start_subfile (fe->name, dir, comp_dir);
13102
13103 /* Skip the main file; we don't need it, and it must be
13104 allocated last, so that it will show up before the
13105 non-primary symtabs in the objfile's symtab list. */
13106 if (current_subfile == first_subfile)
13107 continue;
13108
13109 if (current_subfile->symtab == NULL)
13110 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 13111 objfile);
cb1df416
DJ
13112 fe->symtab = current_subfile->symtab;
13113 }
13114 }
c906108c
SS
13115}
13116
13117/* Start a subfile for DWARF. FILENAME is the name of the file and
13118 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
13119 or NULL if not known. COMP_DIR is the compilation directory for the
13120 linetable's compilation unit or NULL if not known.
c906108c
SS
13121 This routine tries to keep line numbers from identical absolute and
13122 relative file names in a common subfile.
13123
13124 Using the `list' example from the GDB testsuite, which resides in
13125 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13126 of /srcdir/list0.c yields the following debugging information for list0.c:
13127
c5aa993b
JM
13128 DW_AT_name: /srcdir/list0.c
13129 DW_AT_comp_dir: /compdir
357e46e7 13130 files.files[0].name: list0.h
c5aa993b 13131 files.files[0].dir: /srcdir
357e46e7 13132 files.files[1].name: list0.c
c5aa993b 13133 files.files[1].dir: /srcdir
c906108c
SS
13134
13135 The line number information for list0.c has to end up in a single
4f1520fb
FR
13136 subfile, so that `break /srcdir/list0.c:1' works as expected.
13137 start_subfile will ensure that this happens provided that we pass the
13138 concatenation of files.files[1].dir and files.files[1].name as the
13139 subfile's name. */
c906108c
SS
13140
13141static void
3e43a32a
MS
13142dwarf2_start_subfile (char *filename, const char *dirname,
13143 const char *comp_dir)
c906108c 13144{
4f1520fb
FR
13145 char *fullname;
13146
13147 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13148 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13149 second argument to start_subfile. To be consistent, we do the
13150 same here. In order not to lose the line information directory,
13151 we concatenate it to the filename when it makes sense.
13152 Note that the Dwarf3 standard says (speaking of filenames in line
13153 information): ``The directory index is ignored for file names
13154 that represent full path names''. Thus ignoring dirname in the
13155 `else' branch below isn't an issue. */
c906108c 13156
d5166ae1 13157 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
13158 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13159 else
13160 fullname = filename;
c906108c 13161
4f1520fb
FR
13162 start_subfile (fullname, comp_dir);
13163
13164 if (fullname != filename)
13165 xfree (fullname);
c906108c
SS
13166}
13167
4c2df51b
DJ
13168static void
13169var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 13170 struct dwarf2_cu *cu)
4c2df51b 13171{
e7c27a73
DJ
13172 struct objfile *objfile = cu->objfile;
13173 struct comp_unit_head *cu_header = &cu->header;
13174
4c2df51b
DJ
13175 /* NOTE drow/2003-01-30: There used to be a comment and some special
13176 code here to turn a symbol with DW_AT_external and a
13177 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13178 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13179 with some versions of binutils) where shared libraries could have
13180 relocations against symbols in their debug information - the
13181 minimal symbol would have the right address, but the debug info
13182 would not. It's no longer necessary, because we will explicitly
13183 apply relocations when we read in the debug information now. */
13184
13185 /* A DW_AT_location attribute with no contents indicates that a
13186 variable has been optimized away. */
13187 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13188 {
13189 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13190 return;
13191 }
13192
13193 /* Handle one degenerate form of location expression specially, to
13194 preserve GDB's previous behavior when section offsets are
3019eac3
DE
13195 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13196 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
13197
13198 if (attr_form_is_block (attr)
3019eac3
DE
13199 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13200 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13201 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13202 && (DW_BLOCK (attr)->size
13203 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 13204 {
891d2f0b 13205 unsigned int dummy;
4c2df51b 13206
3019eac3
DE
13207 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13208 SYMBOL_VALUE_ADDRESS (sym) =
13209 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13210 else
13211 SYMBOL_VALUE_ADDRESS (sym) =
13212 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 13213 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
13214 fixup_symbol_section (sym, objfile);
13215 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13216 SYMBOL_SECTION (sym));
4c2df51b
DJ
13217 return;
13218 }
13219
13220 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13221 expression evaluator, and use LOC_COMPUTED only when necessary
13222 (i.e. when the value of a register or memory location is
13223 referenced, or a thread-local block, etc.). Then again, it might
13224 not be worthwhile. I'm assuming that it isn't unless performance
13225 or memory numbers show me otherwise. */
13226
e7c27a73 13227 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 13228 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
13229
13230 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13231 cu->has_loclist = 1;
4c2df51b
DJ
13232}
13233
c906108c
SS
13234/* Given a pointer to a DWARF information entry, figure out if we need
13235 to make a symbol table entry for it, and if so, create a new entry
13236 and return a pointer to it.
13237 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
13238 used the passed type.
13239 If SPACE is not NULL, use it to hold the new symbol. If it is
13240 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
13241
13242static struct symbol *
34eaf542
TT
13243new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13244 struct symbol *space)
c906108c 13245{
e7c27a73 13246 struct objfile *objfile = cu->objfile;
c906108c
SS
13247 struct symbol *sym = NULL;
13248 char *name;
13249 struct attribute *attr = NULL;
13250 struct attribute *attr2 = NULL;
e142c38c 13251 CORE_ADDR baseaddr;
e37fd15a
SW
13252 struct pending **list_to_add = NULL;
13253
edb3359d 13254 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
13255
13256 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 13257
94af9270 13258 name = dwarf2_name (die, cu);
c906108c
SS
13259 if (name)
13260 {
94af9270 13261 const char *linkagename;
34eaf542 13262 int suppress_add = 0;
94af9270 13263
34eaf542
TT
13264 if (space)
13265 sym = space;
13266 else
13267 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 13268 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
13269
13270 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 13271 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
13272 linkagename = dwarf2_physname (name, die, cu);
13273 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 13274
f55ee35c
JK
13275 /* Fortran does not have mangling standard and the mangling does differ
13276 between gfortran, iFort etc. */
13277 if (cu->language == language_fortran
b250c185 13278 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
13279 symbol_set_demangled_name (&(sym->ginfo),
13280 (char *) dwarf2_full_name (name, die, cu),
13281 NULL);
f55ee35c 13282
c906108c 13283 /* Default assumptions.
c5aa993b 13284 Use the passed type or decode it from the die. */
176620f1 13285 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 13286 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
13287 if (type != NULL)
13288 SYMBOL_TYPE (sym) = type;
13289 else
e7c27a73 13290 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
13291 attr = dwarf2_attr (die,
13292 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13293 cu);
c906108c
SS
13294 if (attr)
13295 {
13296 SYMBOL_LINE (sym) = DW_UNSND (attr);
13297 }
cb1df416 13298
edb3359d
DJ
13299 attr = dwarf2_attr (die,
13300 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13301 cu);
cb1df416
DJ
13302 if (attr)
13303 {
13304 int file_index = DW_UNSND (attr);
9a619af0 13305
cb1df416
DJ
13306 if (cu->line_header == NULL
13307 || file_index > cu->line_header->num_file_names)
13308 complaint (&symfile_complaints,
13309 _("file index out of range"));
1c3d648d 13310 else if (file_index > 0)
cb1df416
DJ
13311 {
13312 struct file_entry *fe;
9a619af0 13313
cb1df416
DJ
13314 fe = &cu->line_header->file_names[file_index - 1];
13315 SYMBOL_SYMTAB (sym) = fe->symtab;
13316 }
13317 }
13318
c906108c
SS
13319 switch (die->tag)
13320 {
13321 case DW_TAG_label:
e142c38c 13322 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
13323 if (attr)
13324 {
13325 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13326 }
0f5238ed
TT
13327 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13328 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 13329 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 13330 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
13331 break;
13332 case DW_TAG_subprogram:
13333 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13334 finish_block. */
13335 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 13336 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
13337 if ((attr2 && (DW_UNSND (attr2) != 0))
13338 || cu->language == language_ada)
c906108c 13339 {
2cfa0c8d
JB
13340 /* Subprograms marked external are stored as a global symbol.
13341 Ada subprograms, whether marked external or not, are always
13342 stored as a global symbol, because we want to be able to
13343 access them globally. For instance, we want to be able
13344 to break on a nested subprogram without having to
13345 specify the context. */
e37fd15a 13346 list_to_add = &global_symbols;
c906108c
SS
13347 }
13348 else
13349 {
e37fd15a 13350 list_to_add = cu->list_in_scope;
c906108c
SS
13351 }
13352 break;
edb3359d
DJ
13353 case DW_TAG_inlined_subroutine:
13354 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13355 finish_block. */
13356 SYMBOL_CLASS (sym) = LOC_BLOCK;
13357 SYMBOL_INLINED (sym) = 1;
481860b3 13358 list_to_add = cu->list_in_scope;
edb3359d 13359 break;
34eaf542
TT
13360 case DW_TAG_template_value_param:
13361 suppress_add = 1;
13362 /* Fall through. */
72929c62 13363 case DW_TAG_constant:
c906108c 13364 case DW_TAG_variable:
254e6b9e 13365 case DW_TAG_member:
0963b4bd
MS
13366 /* Compilation with minimal debug info may result in
13367 variables with missing type entries. Change the
13368 misleading `void' type to something sensible. */
c906108c 13369 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 13370 SYMBOL_TYPE (sym)
46bf5051 13371 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 13372
e142c38c 13373 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
13374 /* In the case of DW_TAG_member, we should only be called for
13375 static const members. */
13376 if (die->tag == DW_TAG_member)
13377 {
3863f96c
DE
13378 /* dwarf2_add_field uses die_is_declaration,
13379 so we do the same. */
254e6b9e
DE
13380 gdb_assert (die_is_declaration (die, cu));
13381 gdb_assert (attr);
13382 }
c906108c
SS
13383 if (attr)
13384 {
e7c27a73 13385 dwarf2_const_value (attr, sym, cu);
e142c38c 13386 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 13387 if (!suppress_add)
34eaf542
TT
13388 {
13389 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 13390 list_to_add = &global_symbols;
34eaf542 13391 else
e37fd15a 13392 list_to_add = cu->list_in_scope;
34eaf542 13393 }
c906108c
SS
13394 break;
13395 }
e142c38c 13396 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13397 if (attr)
13398 {
e7c27a73 13399 var_decode_location (attr, sym, cu);
e142c38c 13400 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
13401 if (SYMBOL_CLASS (sym) == LOC_STATIC
13402 && SYMBOL_VALUE_ADDRESS (sym) == 0
13403 && !dwarf2_per_objfile->has_section_at_zero)
13404 {
13405 /* When a static variable is eliminated by the linker,
13406 the corresponding debug information is not stripped
13407 out, but the variable address is set to null;
13408 do not add such variables into symbol table. */
13409 }
13410 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 13411 {
f55ee35c
JK
13412 /* Workaround gfortran PR debug/40040 - it uses
13413 DW_AT_location for variables in -fPIC libraries which may
13414 get overriden by other libraries/executable and get
13415 a different address. Resolve it by the minimal symbol
13416 which may come from inferior's executable using copy
13417 relocation. Make this workaround only for gfortran as for
13418 other compilers GDB cannot guess the minimal symbol
13419 Fortran mangling kind. */
13420 if (cu->language == language_fortran && die->parent
13421 && die->parent->tag == DW_TAG_module
13422 && cu->producer
13423 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13424 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13425
1c809c68
TT
13426 /* A variable with DW_AT_external is never static,
13427 but it may be block-scoped. */
13428 list_to_add = (cu->list_in_scope == &file_symbols
13429 ? &global_symbols : cu->list_in_scope);
1c809c68 13430 }
c906108c 13431 else
e37fd15a 13432 list_to_add = cu->list_in_scope;
c906108c
SS
13433 }
13434 else
13435 {
13436 /* We do not know the address of this symbol.
c5aa993b
JM
13437 If it is an external symbol and we have type information
13438 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13439 The address of the variable will then be determined from
13440 the minimal symbol table whenever the variable is
13441 referenced. */
e142c38c 13442 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 13443 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 13444 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 13445 {
0fe7935b
DJ
13446 /* A variable with DW_AT_external is never static, but it
13447 may be block-scoped. */
13448 list_to_add = (cu->list_in_scope == &file_symbols
13449 ? &global_symbols : cu->list_in_scope);
13450
c906108c 13451 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 13452 }
442ddf59
JK
13453 else if (!die_is_declaration (die, cu))
13454 {
13455 /* Use the default LOC_OPTIMIZED_OUT class. */
13456 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
13457 if (!suppress_add)
13458 list_to_add = cu->list_in_scope;
442ddf59 13459 }
c906108c
SS
13460 }
13461 break;
13462 case DW_TAG_formal_parameter:
edb3359d
DJ
13463 /* If we are inside a function, mark this as an argument. If
13464 not, we might be looking at an argument to an inlined function
13465 when we do not have enough information to show inlined frames;
13466 pretend it's a local variable in that case so that the user can
13467 still see it. */
13468 if (context_stack_depth > 0
13469 && context_stack[context_stack_depth - 1].name != NULL)
13470 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 13471 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13472 if (attr)
13473 {
e7c27a73 13474 var_decode_location (attr, sym, cu);
c906108c 13475 }
e142c38c 13476 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13477 if (attr)
13478 {
e7c27a73 13479 dwarf2_const_value (attr, sym, cu);
c906108c 13480 }
f346a30d 13481
e37fd15a 13482 list_to_add = cu->list_in_scope;
c906108c
SS
13483 break;
13484 case DW_TAG_unspecified_parameters:
13485 /* From varargs functions; gdb doesn't seem to have any
13486 interest in this information, so just ignore it for now.
13487 (FIXME?) */
13488 break;
34eaf542
TT
13489 case DW_TAG_template_type_param:
13490 suppress_add = 1;
13491 /* Fall through. */
c906108c 13492 case DW_TAG_class_type:
680b30c7 13493 case DW_TAG_interface_type:
c906108c
SS
13494 case DW_TAG_structure_type:
13495 case DW_TAG_union_type:
72019c9c 13496 case DW_TAG_set_type:
c906108c
SS
13497 case DW_TAG_enumeration_type:
13498 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13499 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 13500
63d06c5c 13501 {
987504bb 13502 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
13503 really ever be static objects: otherwise, if you try
13504 to, say, break of a class's method and you're in a file
13505 which doesn't mention that class, it won't work unless
13506 the check for all static symbols in lookup_symbol_aux
13507 saves you. See the OtherFileClass tests in
13508 gdb.c++/namespace.exp. */
13509
e37fd15a 13510 if (!suppress_add)
34eaf542 13511 {
34eaf542
TT
13512 list_to_add = (cu->list_in_scope == &file_symbols
13513 && (cu->language == language_cplus
13514 || cu->language == language_java)
13515 ? &global_symbols : cu->list_in_scope);
63d06c5c 13516
64382290
TT
13517 /* The semantics of C++ state that "struct foo {
13518 ... }" also defines a typedef for "foo". A Java
13519 class declaration also defines a typedef for the
13520 class. */
13521 if (cu->language == language_cplus
13522 || cu->language == language_java
13523 || cu->language == language_ada)
13524 {
13525 /* The symbol's name is already allocated along
13526 with this objfile, so we don't need to
13527 duplicate it for the type. */
13528 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13529 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13530 }
63d06c5c
DC
13531 }
13532 }
c906108c
SS
13533 break;
13534 case DW_TAG_typedef:
63d06c5c
DC
13535 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13536 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13537 list_to_add = cu->list_in_scope;
63d06c5c 13538 break;
c906108c 13539 case DW_TAG_base_type:
a02abb62 13540 case DW_TAG_subrange_type:
c906108c 13541 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13542 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13543 list_to_add = cu->list_in_scope;
c906108c
SS
13544 break;
13545 case DW_TAG_enumerator:
e142c38c 13546 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13547 if (attr)
13548 {
e7c27a73 13549 dwarf2_const_value (attr, sym, cu);
c906108c 13550 }
63d06c5c
DC
13551 {
13552 /* NOTE: carlton/2003-11-10: See comment above in the
13553 DW_TAG_class_type, etc. block. */
13554
e142c38c 13555 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
13556 && (cu->language == language_cplus
13557 || cu->language == language_java)
e142c38c 13558 ? &global_symbols : cu->list_in_scope);
63d06c5c 13559 }
c906108c 13560 break;
5c4e30ca
DC
13561 case DW_TAG_namespace:
13562 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 13563 list_to_add = &global_symbols;
5c4e30ca 13564 break;
c906108c
SS
13565 default:
13566 /* Not a tag we recognize. Hopefully we aren't processing
13567 trash data, but since we must specifically ignore things
13568 we don't recognize, there is nothing else we should do at
0963b4bd 13569 this point. */
e2e0b3e5 13570 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 13571 dwarf_tag_name (die->tag));
c906108c
SS
13572 break;
13573 }
df8a16a1 13574
e37fd15a
SW
13575 if (suppress_add)
13576 {
13577 sym->hash_next = objfile->template_symbols;
13578 objfile->template_symbols = sym;
13579 list_to_add = NULL;
13580 }
13581
13582 if (list_to_add != NULL)
13583 add_symbol_to_list (sym, list_to_add);
13584
df8a16a1
DJ
13585 /* For the benefit of old versions of GCC, check for anonymous
13586 namespaces based on the demangled name. */
13587 if (!processing_has_namespace_info
94af9270 13588 && cu->language == language_cplus)
a10964d1 13589 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
13590 }
13591 return (sym);
13592}
13593
34eaf542
TT
13594/* A wrapper for new_symbol_full that always allocates a new symbol. */
13595
13596static struct symbol *
13597new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13598{
13599 return new_symbol_full (die, type, cu, NULL);
13600}
13601
98bfdba5
PA
13602/* Given an attr with a DW_FORM_dataN value in host byte order,
13603 zero-extend it as appropriate for the symbol's type. The DWARF
13604 standard (v4) is not entirely clear about the meaning of using
13605 DW_FORM_dataN for a constant with a signed type, where the type is
13606 wider than the data. The conclusion of a discussion on the DWARF
13607 list was that this is unspecified. We choose to always zero-extend
13608 because that is the interpretation long in use by GCC. */
c906108c 13609
98bfdba5
PA
13610static gdb_byte *
13611dwarf2_const_value_data (struct attribute *attr, struct type *type,
13612 const char *name, struct obstack *obstack,
12df843f 13613 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 13614{
e7c27a73 13615 struct objfile *objfile = cu->objfile;
e17a4113
UW
13616 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13617 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
13618 LONGEST l = DW_UNSND (attr);
13619
13620 if (bits < sizeof (*value) * 8)
13621 {
13622 l &= ((LONGEST) 1 << bits) - 1;
13623 *value = l;
13624 }
13625 else if (bits == sizeof (*value) * 8)
13626 *value = l;
13627 else
13628 {
13629 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13630 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13631 return bytes;
13632 }
13633
13634 return NULL;
13635}
13636
13637/* Read a constant value from an attribute. Either set *VALUE, or if
13638 the value does not fit in *VALUE, set *BYTES - either already
13639 allocated on the objfile obstack, or newly allocated on OBSTACK,
13640 or, set *BATON, if we translated the constant to a location
13641 expression. */
13642
13643static void
13644dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13645 const char *name, struct obstack *obstack,
13646 struct dwarf2_cu *cu,
12df843f 13647 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
13648 struct dwarf2_locexpr_baton **baton)
13649{
13650 struct objfile *objfile = cu->objfile;
13651 struct comp_unit_head *cu_header = &cu->header;
c906108c 13652 struct dwarf_block *blk;
98bfdba5
PA
13653 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13654 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13655
13656 *value = 0;
13657 *bytes = NULL;
13658 *baton = NULL;
c906108c
SS
13659
13660 switch (attr->form)
13661 {
13662 case DW_FORM_addr:
3019eac3 13663 case DW_FORM_GNU_addr_index:
ac56253d 13664 {
ac56253d
TT
13665 gdb_byte *data;
13666
98bfdba5
PA
13667 if (TYPE_LENGTH (type) != cu_header->addr_size)
13668 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 13669 cu_header->addr_size,
98bfdba5 13670 TYPE_LENGTH (type));
ac56253d
TT
13671 /* Symbols of this form are reasonably rare, so we just
13672 piggyback on the existing location code rather than writing
13673 a new implementation of symbol_computed_ops. */
98bfdba5
PA
13674 *baton = obstack_alloc (&objfile->objfile_obstack,
13675 sizeof (struct dwarf2_locexpr_baton));
13676 (*baton)->per_cu = cu->per_cu;
13677 gdb_assert ((*baton)->per_cu);
ac56253d 13678
98bfdba5
PA
13679 (*baton)->size = 2 + cu_header->addr_size;
13680 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13681 (*baton)->data = data;
ac56253d
TT
13682
13683 data[0] = DW_OP_addr;
13684 store_unsigned_integer (&data[1], cu_header->addr_size,
13685 byte_order, DW_ADDR (attr));
13686 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 13687 }
c906108c 13688 break;
4ac36638 13689 case DW_FORM_string:
93b5768b 13690 case DW_FORM_strp:
3019eac3 13691 case DW_FORM_GNU_str_index:
98bfdba5
PA
13692 /* DW_STRING is already allocated on the objfile obstack, point
13693 directly to it. */
13694 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 13695 break;
c906108c
SS
13696 case DW_FORM_block1:
13697 case DW_FORM_block2:
13698 case DW_FORM_block4:
13699 case DW_FORM_block:
2dc7f7b3 13700 case DW_FORM_exprloc:
c906108c 13701 blk = DW_BLOCK (attr);
98bfdba5
PA
13702 if (TYPE_LENGTH (type) != blk->size)
13703 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13704 TYPE_LENGTH (type));
13705 *bytes = blk->data;
c906108c 13706 break;
2df3850c
JM
13707
13708 /* The DW_AT_const_value attributes are supposed to carry the
13709 symbol's value "represented as it would be on the target
13710 architecture." By the time we get here, it's already been
13711 converted to host endianness, so we just need to sign- or
13712 zero-extend it as appropriate. */
13713 case DW_FORM_data1:
3e43a32a
MS
13714 *bytes = dwarf2_const_value_data (attr, type, name,
13715 obstack, cu, value, 8);
2df3850c 13716 break;
c906108c 13717 case DW_FORM_data2:
3e43a32a
MS
13718 *bytes = dwarf2_const_value_data (attr, type, name,
13719 obstack, cu, value, 16);
2df3850c 13720 break;
c906108c 13721 case DW_FORM_data4:
3e43a32a
MS
13722 *bytes = dwarf2_const_value_data (attr, type, name,
13723 obstack, cu, value, 32);
2df3850c 13724 break;
c906108c 13725 case DW_FORM_data8:
3e43a32a
MS
13726 *bytes = dwarf2_const_value_data (attr, type, name,
13727 obstack, cu, value, 64);
2df3850c
JM
13728 break;
13729
c906108c 13730 case DW_FORM_sdata:
98bfdba5 13731 *value = DW_SND (attr);
2df3850c
JM
13732 break;
13733
c906108c 13734 case DW_FORM_udata:
98bfdba5 13735 *value = DW_UNSND (attr);
c906108c 13736 break;
2df3850c 13737
c906108c 13738 default:
4d3c2250 13739 complaint (&symfile_complaints,
e2e0b3e5 13740 _("unsupported const value attribute form: '%s'"),
4d3c2250 13741 dwarf_form_name (attr->form));
98bfdba5 13742 *value = 0;
c906108c
SS
13743 break;
13744 }
13745}
13746
2df3850c 13747
98bfdba5
PA
13748/* Copy constant value from an attribute to a symbol. */
13749
2df3850c 13750static void
98bfdba5
PA
13751dwarf2_const_value (struct attribute *attr, struct symbol *sym,
13752 struct dwarf2_cu *cu)
2df3850c 13753{
98bfdba5
PA
13754 struct objfile *objfile = cu->objfile;
13755 struct comp_unit_head *cu_header = &cu->header;
12df843f 13756 LONGEST value;
98bfdba5
PA
13757 gdb_byte *bytes;
13758 struct dwarf2_locexpr_baton *baton;
2df3850c 13759
98bfdba5
PA
13760 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
13761 SYMBOL_PRINT_NAME (sym),
13762 &objfile->objfile_obstack, cu,
13763 &value, &bytes, &baton);
2df3850c 13764
98bfdba5
PA
13765 if (baton != NULL)
13766 {
13767 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13768 SYMBOL_LOCATION_BATON (sym) = baton;
13769 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13770 }
13771 else if (bytes != NULL)
13772 {
13773 SYMBOL_VALUE_BYTES (sym) = bytes;
13774 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
13775 }
13776 else
13777 {
13778 SYMBOL_VALUE (sym) = value;
13779 SYMBOL_CLASS (sym) = LOC_CONST;
13780 }
2df3850c
JM
13781}
13782
c906108c
SS
13783/* Return the type of the die in question using its DW_AT_type attribute. */
13784
13785static struct type *
e7c27a73 13786die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13787{
c906108c 13788 struct attribute *type_attr;
c906108c 13789
e142c38c 13790 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
13791 if (!type_attr)
13792 {
13793 /* A missing DW_AT_type represents a void type. */
46bf5051 13794 return objfile_type (cu->objfile)->builtin_void;
c906108c 13795 }
348e048f 13796
673bfd45 13797 return lookup_die_type (die, type_attr, cu);
c906108c
SS
13798}
13799
b4ba55a1
JB
13800/* True iff CU's producer generates GNAT Ada auxiliary information
13801 that allows to find parallel types through that information instead
13802 of having to do expensive parallel lookups by type name. */
13803
13804static int
13805need_gnat_info (struct dwarf2_cu *cu)
13806{
13807 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
13808 of GNAT produces this auxiliary information, without any indication
13809 that it is produced. Part of enhancing the FSF version of GNAT
13810 to produce that information will be to put in place an indicator
13811 that we can use in order to determine whether the descriptive type
13812 info is available or not. One suggestion that has been made is
13813 to use a new attribute, attached to the CU die. For now, assume
13814 that the descriptive type info is not available. */
13815 return 0;
13816}
13817
b4ba55a1
JB
13818/* Return the auxiliary type of the die in question using its
13819 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
13820 attribute is not present. */
13821
13822static struct type *
13823die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
13824{
b4ba55a1 13825 struct attribute *type_attr;
b4ba55a1
JB
13826
13827 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
13828 if (!type_attr)
13829 return NULL;
13830
673bfd45 13831 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
13832}
13833
13834/* If DIE has a descriptive_type attribute, then set the TYPE's
13835 descriptive type accordingly. */
13836
13837static void
13838set_descriptive_type (struct type *type, struct die_info *die,
13839 struct dwarf2_cu *cu)
13840{
13841 struct type *descriptive_type = die_descriptive_type (die, cu);
13842
13843 if (descriptive_type)
13844 {
13845 ALLOCATE_GNAT_AUX_TYPE (type);
13846 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
13847 }
13848}
13849
c906108c
SS
13850/* Return the containing type of the die in question using its
13851 DW_AT_containing_type attribute. */
13852
13853static struct type *
e7c27a73 13854die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13855{
c906108c 13856 struct attribute *type_attr;
c906108c 13857
e142c38c 13858 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
13859 if (!type_attr)
13860 error (_("Dwarf Error: Problem turning containing type into gdb type "
13861 "[in module %s]"), cu->objfile->name);
13862
673bfd45 13863 return lookup_die_type (die, type_attr, cu);
c906108c
SS
13864}
13865
673bfd45
DE
13866/* Look up the type of DIE in CU using its type attribute ATTR.
13867 If there is no type substitute an error marker. */
13868
c906108c 13869static struct type *
673bfd45
DE
13870lookup_die_type (struct die_info *die, struct attribute *attr,
13871 struct dwarf2_cu *cu)
c906108c 13872{
bb5ed363 13873 struct objfile *objfile = cu->objfile;
f792889a
DJ
13874 struct type *this_type;
13875
673bfd45
DE
13876 /* First see if we have it cached. */
13877
13878 if (is_ref_attr (attr))
13879 {
b64f50a1 13880 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
13881
13882 this_type = get_die_type_at_offset (offset, cu->per_cu);
13883 }
55f1336d 13884 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
13885 {
13886 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
13887
13888 /* sig_type will be NULL if the signatured type is missing from
13889 the debug info. */
13890 if (sig_type == NULL)
13891 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13892 "at 0x%x [in module %s]"),
b64f50a1 13893 die->offset.sect_off, objfile->name);
673bfd45 13894
3019eac3
DE
13895 gdb_assert (sig_type->per_cu.is_debug_types);
13896 /* If we haven't filled in type_offset_in_section yet, then we
13897 haven't read the type in yet. */
13898 this_type = NULL;
13899 if (sig_type->type_offset_in_section.sect_off != 0)
13900 {
13901 this_type =
13902 get_die_type_at_offset (sig_type->type_offset_in_section,
13903 &sig_type->per_cu);
13904 }
673bfd45
DE
13905 }
13906 else
13907 {
13908 dump_die_for_error (die);
13909 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 13910 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
13911 }
13912
13913 /* If not cached we need to read it in. */
13914
13915 if (this_type == NULL)
13916 {
13917 struct die_info *type_die;
13918 struct dwarf2_cu *type_cu = cu;
13919
13920 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
13921 /* If we found the type now, it's probably because the type came
13922 from an inter-CU reference and the type's CU got expanded before
13923 ours. */
13924 this_type = get_die_type (type_die, type_cu);
13925 if (this_type == NULL)
13926 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
13927 }
13928
13929 /* If we still don't have a type use an error marker. */
13930
13931 if (this_type == NULL)
c906108c 13932 {
b00fdb78
TT
13933 char *message, *saved;
13934
13935 /* read_type_die already issued a complaint. */
13936 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 13937 objfile->name,
b64f50a1
JK
13938 cu->header.offset.sect_off,
13939 die->offset.sect_off);
bb5ed363 13940 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
13941 message, strlen (message));
13942 xfree (message);
13943
bb5ed363 13944 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 13945 }
673bfd45 13946
f792889a 13947 return this_type;
c906108c
SS
13948}
13949
673bfd45
DE
13950/* Return the type in DIE, CU.
13951 Returns NULL for invalid types.
13952
13953 This first does a lookup in the appropriate type_hash table,
13954 and only reads the die in if necessary.
13955
13956 NOTE: This can be called when reading in partial or full symbols. */
13957
f792889a 13958static struct type *
e7c27a73 13959read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13960{
f792889a
DJ
13961 struct type *this_type;
13962
13963 this_type = get_die_type (die, cu);
13964 if (this_type)
13965 return this_type;
13966
673bfd45
DE
13967 return read_type_die_1 (die, cu);
13968}
13969
13970/* Read the type in DIE, CU.
13971 Returns NULL for invalid types. */
13972
13973static struct type *
13974read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
13975{
13976 struct type *this_type = NULL;
13977
c906108c
SS
13978 switch (die->tag)
13979 {
13980 case DW_TAG_class_type:
680b30c7 13981 case DW_TAG_interface_type:
c906108c
SS
13982 case DW_TAG_structure_type:
13983 case DW_TAG_union_type:
f792889a 13984 this_type = read_structure_type (die, cu);
c906108c
SS
13985 break;
13986 case DW_TAG_enumeration_type:
f792889a 13987 this_type = read_enumeration_type (die, cu);
c906108c
SS
13988 break;
13989 case DW_TAG_subprogram:
13990 case DW_TAG_subroutine_type:
edb3359d 13991 case DW_TAG_inlined_subroutine:
f792889a 13992 this_type = read_subroutine_type (die, cu);
c906108c
SS
13993 break;
13994 case DW_TAG_array_type:
f792889a 13995 this_type = read_array_type (die, cu);
c906108c 13996 break;
72019c9c 13997 case DW_TAG_set_type:
f792889a 13998 this_type = read_set_type (die, cu);
72019c9c 13999 break;
c906108c 14000 case DW_TAG_pointer_type:
f792889a 14001 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
14002 break;
14003 case DW_TAG_ptr_to_member_type:
f792889a 14004 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
14005 break;
14006 case DW_TAG_reference_type:
f792889a 14007 this_type = read_tag_reference_type (die, cu);
c906108c
SS
14008 break;
14009 case DW_TAG_const_type:
f792889a 14010 this_type = read_tag_const_type (die, cu);
c906108c
SS
14011 break;
14012 case DW_TAG_volatile_type:
f792889a 14013 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
14014 break;
14015 case DW_TAG_string_type:
f792889a 14016 this_type = read_tag_string_type (die, cu);
c906108c
SS
14017 break;
14018 case DW_TAG_typedef:
f792889a 14019 this_type = read_typedef (die, cu);
c906108c 14020 break;
a02abb62 14021 case DW_TAG_subrange_type:
f792889a 14022 this_type = read_subrange_type (die, cu);
a02abb62 14023 break;
c906108c 14024 case DW_TAG_base_type:
f792889a 14025 this_type = read_base_type (die, cu);
c906108c 14026 break;
81a17f79 14027 case DW_TAG_unspecified_type:
f792889a 14028 this_type = read_unspecified_type (die, cu);
81a17f79 14029 break;
0114d602
DJ
14030 case DW_TAG_namespace:
14031 this_type = read_namespace_type (die, cu);
14032 break;
f55ee35c
JK
14033 case DW_TAG_module:
14034 this_type = read_module_type (die, cu);
14035 break;
c906108c 14036 default:
3e43a32a
MS
14037 complaint (&symfile_complaints,
14038 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 14039 dwarf_tag_name (die->tag));
c906108c
SS
14040 break;
14041 }
63d06c5c 14042
f792889a 14043 return this_type;
63d06c5c
DC
14044}
14045
abc72ce4
DE
14046/* See if we can figure out if the class lives in a namespace. We do
14047 this by looking for a member function; its demangled name will
14048 contain namespace info, if there is any.
14049 Return the computed name or NULL.
14050 Space for the result is allocated on the objfile's obstack.
14051 This is the full-die version of guess_partial_die_structure_name.
14052 In this case we know DIE has no useful parent. */
14053
14054static char *
14055guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14056{
14057 struct die_info *spec_die;
14058 struct dwarf2_cu *spec_cu;
14059 struct die_info *child;
14060
14061 spec_cu = cu;
14062 spec_die = die_specification (die, &spec_cu);
14063 if (spec_die != NULL)
14064 {
14065 die = spec_die;
14066 cu = spec_cu;
14067 }
14068
14069 for (child = die->child;
14070 child != NULL;
14071 child = child->sibling)
14072 {
14073 if (child->tag == DW_TAG_subprogram)
14074 {
14075 struct attribute *attr;
14076
14077 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14078 if (attr == NULL)
14079 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14080 if (attr != NULL)
14081 {
14082 char *actual_name
14083 = language_class_name_from_physname (cu->language_defn,
14084 DW_STRING (attr));
14085 char *name = NULL;
14086
14087 if (actual_name != NULL)
14088 {
14089 char *die_name = dwarf2_name (die, cu);
14090
14091 if (die_name != NULL
14092 && strcmp (die_name, actual_name) != 0)
14093 {
14094 /* Strip off the class name from the full name.
14095 We want the prefix. */
14096 int die_name_len = strlen (die_name);
14097 int actual_name_len = strlen (actual_name);
14098
14099 /* Test for '::' as a sanity check. */
14100 if (actual_name_len > die_name_len + 2
3e43a32a
MS
14101 && actual_name[actual_name_len
14102 - die_name_len - 1] == ':')
abc72ce4
DE
14103 name =
14104 obsavestring (actual_name,
14105 actual_name_len - die_name_len - 2,
14106 &cu->objfile->objfile_obstack);
14107 }
14108 }
14109 xfree (actual_name);
14110 return name;
14111 }
14112 }
14113 }
14114
14115 return NULL;
14116}
14117
96408a79
SA
14118/* GCC might emit a nameless typedef that has a linkage name. Determine the
14119 prefix part in such case. See
14120 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14121
14122static char *
14123anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14124{
14125 struct attribute *attr;
14126 char *base;
14127
14128 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14129 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14130 return NULL;
14131
14132 attr = dwarf2_attr (die, DW_AT_name, cu);
14133 if (attr != NULL && DW_STRING (attr) != NULL)
14134 return NULL;
14135
14136 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14137 if (attr == NULL)
14138 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14139 if (attr == NULL || DW_STRING (attr) == NULL)
14140 return NULL;
14141
14142 /* dwarf2_name had to be already called. */
14143 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14144
14145 /* Strip the base name, keep any leading namespaces/classes. */
14146 base = strrchr (DW_STRING (attr), ':');
14147 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14148 return "";
14149
14150 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14151 &cu->objfile->objfile_obstack);
14152}
14153
fdde2d81 14154/* Return the name of the namespace/class that DIE is defined within,
0114d602 14155 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 14156
0114d602
DJ
14157 For example, if we're within the method foo() in the following
14158 code:
14159
14160 namespace N {
14161 class C {
14162 void foo () {
14163 }
14164 };
14165 }
14166
14167 then determine_prefix on foo's die will return "N::C". */
fdde2d81 14168
0d5cff50 14169static const char *
e142c38c 14170determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 14171{
0114d602
DJ
14172 struct die_info *parent, *spec_die;
14173 struct dwarf2_cu *spec_cu;
14174 struct type *parent_type;
96408a79 14175 char *retval;
63d06c5c 14176
f55ee35c
JK
14177 if (cu->language != language_cplus && cu->language != language_java
14178 && cu->language != language_fortran)
0114d602
DJ
14179 return "";
14180
96408a79
SA
14181 retval = anonymous_struct_prefix (die, cu);
14182 if (retval)
14183 return retval;
14184
0114d602
DJ
14185 /* We have to be careful in the presence of DW_AT_specification.
14186 For example, with GCC 3.4, given the code
14187
14188 namespace N {
14189 void foo() {
14190 // Definition of N::foo.
14191 }
14192 }
14193
14194 then we'll have a tree of DIEs like this:
14195
14196 1: DW_TAG_compile_unit
14197 2: DW_TAG_namespace // N
14198 3: DW_TAG_subprogram // declaration of N::foo
14199 4: DW_TAG_subprogram // definition of N::foo
14200 DW_AT_specification // refers to die #3
14201
14202 Thus, when processing die #4, we have to pretend that we're in
14203 the context of its DW_AT_specification, namely the contex of die
14204 #3. */
14205 spec_cu = cu;
14206 spec_die = die_specification (die, &spec_cu);
14207 if (spec_die == NULL)
14208 parent = die->parent;
14209 else
63d06c5c 14210 {
0114d602
DJ
14211 parent = spec_die->parent;
14212 cu = spec_cu;
63d06c5c 14213 }
0114d602
DJ
14214
14215 if (parent == NULL)
14216 return "";
98bfdba5
PA
14217 else if (parent->building_fullname)
14218 {
14219 const char *name;
14220 const char *parent_name;
14221
14222 /* It has been seen on RealView 2.2 built binaries,
14223 DW_TAG_template_type_param types actually _defined_ as
14224 children of the parent class:
14225
14226 enum E {};
14227 template class <class Enum> Class{};
14228 Class<enum E> class_e;
14229
14230 1: DW_TAG_class_type (Class)
14231 2: DW_TAG_enumeration_type (E)
14232 3: DW_TAG_enumerator (enum1:0)
14233 3: DW_TAG_enumerator (enum2:1)
14234 ...
14235 2: DW_TAG_template_type_param
14236 DW_AT_type DW_FORM_ref_udata (E)
14237
14238 Besides being broken debug info, it can put GDB into an
14239 infinite loop. Consider:
14240
14241 When we're building the full name for Class<E>, we'll start
14242 at Class, and go look over its template type parameters,
14243 finding E. We'll then try to build the full name of E, and
14244 reach here. We're now trying to build the full name of E,
14245 and look over the parent DIE for containing scope. In the
14246 broken case, if we followed the parent DIE of E, we'd again
14247 find Class, and once again go look at its template type
14248 arguments, etc., etc. Simply don't consider such parent die
14249 as source-level parent of this die (it can't be, the language
14250 doesn't allow it), and break the loop here. */
14251 name = dwarf2_name (die, cu);
14252 parent_name = dwarf2_name (parent, cu);
14253 complaint (&symfile_complaints,
14254 _("template param type '%s' defined within parent '%s'"),
14255 name ? name : "<unknown>",
14256 parent_name ? parent_name : "<unknown>");
14257 return "";
14258 }
63d06c5c 14259 else
0114d602
DJ
14260 switch (parent->tag)
14261 {
63d06c5c 14262 case DW_TAG_namespace:
0114d602 14263 parent_type = read_type_die (parent, cu);
acebe513
UW
14264 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14265 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14266 Work around this problem here. */
14267 if (cu->language == language_cplus
14268 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14269 return "";
0114d602
DJ
14270 /* We give a name to even anonymous namespaces. */
14271 return TYPE_TAG_NAME (parent_type);
63d06c5c 14272 case DW_TAG_class_type:
680b30c7 14273 case DW_TAG_interface_type:
63d06c5c 14274 case DW_TAG_structure_type:
0114d602 14275 case DW_TAG_union_type:
f55ee35c 14276 case DW_TAG_module:
0114d602
DJ
14277 parent_type = read_type_die (parent, cu);
14278 if (TYPE_TAG_NAME (parent_type) != NULL)
14279 return TYPE_TAG_NAME (parent_type);
14280 else
14281 /* An anonymous structure is only allowed non-static data
14282 members; no typedefs, no member functions, et cetera.
14283 So it does not need a prefix. */
14284 return "";
abc72ce4 14285 case DW_TAG_compile_unit:
95554aad 14286 case DW_TAG_partial_unit:
abc72ce4
DE
14287 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14288 if (cu->language == language_cplus
8b70b953 14289 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14290 && die->child != NULL
14291 && (die->tag == DW_TAG_class_type
14292 || die->tag == DW_TAG_structure_type
14293 || die->tag == DW_TAG_union_type))
14294 {
14295 char *name = guess_full_die_structure_name (die, cu);
14296 if (name != NULL)
14297 return name;
14298 }
14299 return "";
63d06c5c 14300 default:
8176b9b8 14301 return determine_prefix (parent, cu);
63d06c5c 14302 }
63d06c5c
DC
14303}
14304
3e43a32a
MS
14305/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14306 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14307 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14308 an obconcat, otherwise allocate storage for the result. The CU argument is
14309 used to determine the language and hence, the appropriate separator. */
987504bb 14310
f55ee35c 14311#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
14312
14313static char *
f55ee35c
JK
14314typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14315 int physname, struct dwarf2_cu *cu)
63d06c5c 14316{
f55ee35c 14317 const char *lead = "";
5c315b68 14318 const char *sep;
63d06c5c 14319
3e43a32a
MS
14320 if (suffix == NULL || suffix[0] == '\0'
14321 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
14322 sep = "";
14323 else if (cu->language == language_java)
14324 sep = ".";
f55ee35c
JK
14325 else if (cu->language == language_fortran && physname)
14326 {
14327 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14328 DW_AT_MIPS_linkage_name is preferred and used instead. */
14329
14330 lead = "__";
14331 sep = "_MOD_";
14332 }
987504bb
JJ
14333 else
14334 sep = "::";
63d06c5c 14335
6dd47d34
DE
14336 if (prefix == NULL)
14337 prefix = "";
14338 if (suffix == NULL)
14339 suffix = "";
14340
987504bb
JJ
14341 if (obs == NULL)
14342 {
3e43a32a
MS
14343 char *retval
14344 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 14345
f55ee35c
JK
14346 strcpy (retval, lead);
14347 strcat (retval, prefix);
6dd47d34
DE
14348 strcat (retval, sep);
14349 strcat (retval, suffix);
63d06c5c
DC
14350 return retval;
14351 }
987504bb
JJ
14352 else
14353 {
14354 /* We have an obstack. */
f55ee35c 14355 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 14356 }
63d06c5c
DC
14357}
14358
c906108c
SS
14359/* Return sibling of die, NULL if no sibling. */
14360
f9aca02d 14361static struct die_info *
fba45db2 14362sibling_die (struct die_info *die)
c906108c 14363{
639d11d3 14364 return die->sibling;
c906108c
SS
14365}
14366
71c25dea
TT
14367/* Get name of a die, return NULL if not found. */
14368
14369static char *
14370dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14371 struct obstack *obstack)
14372{
14373 if (name && cu->language == language_cplus)
14374 {
14375 char *canon_name = cp_canonicalize_string (name);
14376
14377 if (canon_name != NULL)
14378 {
14379 if (strcmp (canon_name, name) != 0)
14380 name = obsavestring (canon_name, strlen (canon_name),
14381 obstack);
14382 xfree (canon_name);
14383 }
14384 }
14385
14386 return name;
c906108c
SS
14387}
14388
9219021c
DC
14389/* Get name of a die, return NULL if not found. */
14390
14391static char *
e142c38c 14392dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
14393{
14394 struct attribute *attr;
14395
e142c38c 14396 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
14397 if ((!attr || !DW_STRING (attr))
14398 && die->tag != DW_TAG_class_type
14399 && die->tag != DW_TAG_interface_type
14400 && die->tag != DW_TAG_structure_type
14401 && die->tag != DW_TAG_union_type)
71c25dea
TT
14402 return NULL;
14403
14404 switch (die->tag)
14405 {
14406 case DW_TAG_compile_unit:
95554aad 14407 case DW_TAG_partial_unit:
71c25dea
TT
14408 /* Compilation units have a DW_AT_name that is a filename, not
14409 a source language identifier. */
14410 case DW_TAG_enumeration_type:
14411 case DW_TAG_enumerator:
14412 /* These tags always have simple identifiers already; no need
14413 to canonicalize them. */
14414 return DW_STRING (attr);
907af001 14415
418835cc
KS
14416 case DW_TAG_subprogram:
14417 /* Java constructors will all be named "<init>", so return
14418 the class name when we see this special case. */
14419 if (cu->language == language_java
14420 && DW_STRING (attr) != NULL
14421 && strcmp (DW_STRING (attr), "<init>") == 0)
14422 {
14423 struct dwarf2_cu *spec_cu = cu;
14424 struct die_info *spec_die;
14425
14426 /* GCJ will output '<init>' for Java constructor names.
14427 For this special case, return the name of the parent class. */
14428
14429 /* GCJ may output suprogram DIEs with AT_specification set.
14430 If so, use the name of the specified DIE. */
14431 spec_die = die_specification (die, &spec_cu);
14432 if (spec_die != NULL)
14433 return dwarf2_name (spec_die, spec_cu);
14434
14435 do
14436 {
14437 die = die->parent;
14438 if (die->tag == DW_TAG_class_type)
14439 return dwarf2_name (die, cu);
14440 }
95554aad
TT
14441 while (die->tag != DW_TAG_compile_unit
14442 && die->tag != DW_TAG_partial_unit);
418835cc 14443 }
907af001
UW
14444 break;
14445
14446 case DW_TAG_class_type:
14447 case DW_TAG_interface_type:
14448 case DW_TAG_structure_type:
14449 case DW_TAG_union_type:
14450 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14451 structures or unions. These were of the form "._%d" in GCC 4.1,
14452 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14453 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
14454 if (attr && DW_STRING (attr)
14455 && (strncmp (DW_STRING (attr), "._", 2) == 0
14456 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 14457 return NULL;
53832f31
TT
14458
14459 /* GCC might emit a nameless typedef that has a linkage name. See
14460 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14461 if (!attr || DW_STRING (attr) == NULL)
14462 {
df5c6c50 14463 char *demangled = NULL;
53832f31
TT
14464
14465 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14466 if (attr == NULL)
14467 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14468
14469 if (attr == NULL || DW_STRING (attr) == NULL)
14470 return NULL;
14471
df5c6c50
JK
14472 /* Avoid demangling DW_STRING (attr) the second time on a second
14473 call for the same DIE. */
14474 if (!DW_STRING_IS_CANONICAL (attr))
14475 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
14476
14477 if (demangled)
14478 {
96408a79
SA
14479 char *base;
14480
53832f31 14481 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
14482 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14483 &cu->objfile->objfile_obstack);
53832f31
TT
14484 DW_STRING_IS_CANONICAL (attr) = 1;
14485 xfree (demangled);
96408a79
SA
14486
14487 /* Strip any leading namespaces/classes, keep only the base name.
14488 DW_AT_name for named DIEs does not contain the prefixes. */
14489 base = strrchr (DW_STRING (attr), ':');
14490 if (base && base > DW_STRING (attr) && base[-1] == ':')
14491 return &base[1];
14492 else
14493 return DW_STRING (attr);
53832f31
TT
14494 }
14495 }
907af001
UW
14496 break;
14497
71c25dea 14498 default:
907af001
UW
14499 break;
14500 }
14501
14502 if (!DW_STRING_IS_CANONICAL (attr))
14503 {
14504 DW_STRING (attr)
14505 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14506 &cu->objfile->objfile_obstack);
14507 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 14508 }
907af001 14509 return DW_STRING (attr);
9219021c
DC
14510}
14511
14512/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
14513 is none. *EXT_CU is the CU containing DIE on input, and the CU
14514 containing the return value on output. */
9219021c
DC
14515
14516static struct die_info *
f2f0e013 14517dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
14518{
14519 struct attribute *attr;
9219021c 14520
f2f0e013 14521 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
14522 if (attr == NULL)
14523 return NULL;
14524
f2f0e013 14525 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
14526}
14527
c906108c
SS
14528/* Convert a DIE tag into its string name. */
14529
f39c6ffd 14530static const char *
aa1ee363 14531dwarf_tag_name (unsigned tag)
c906108c 14532{
f39c6ffd
TT
14533 const char *name = get_DW_TAG_name (tag);
14534
14535 if (name == NULL)
14536 return "DW_TAG_<unknown>";
14537
14538 return name;
c906108c
SS
14539}
14540
14541/* Convert a DWARF attribute code into its string name. */
14542
f39c6ffd 14543static const char *
aa1ee363 14544dwarf_attr_name (unsigned attr)
c906108c 14545{
f39c6ffd
TT
14546 const char *name;
14547
c764a876 14548#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
14549 if (attr == DW_AT_MIPS_fde)
14550 return "DW_AT_MIPS_fde";
14551#else
14552 if (attr == DW_AT_HP_block_index)
14553 return "DW_AT_HP_block_index";
c764a876 14554#endif
f39c6ffd
TT
14555
14556 name = get_DW_AT_name (attr);
14557
14558 if (name == NULL)
14559 return "DW_AT_<unknown>";
14560
14561 return name;
c906108c
SS
14562}
14563
14564/* Convert a DWARF value form code into its string name. */
14565
f39c6ffd 14566static const char *
aa1ee363 14567dwarf_form_name (unsigned form)
c906108c 14568{
f39c6ffd
TT
14569 const char *name = get_DW_FORM_name (form);
14570
14571 if (name == NULL)
14572 return "DW_FORM_<unknown>";
14573
14574 return name;
c906108c
SS
14575}
14576
14577static char *
fba45db2 14578dwarf_bool_name (unsigned mybool)
c906108c
SS
14579{
14580 if (mybool)
14581 return "TRUE";
14582 else
14583 return "FALSE";
14584}
14585
14586/* Convert a DWARF type code into its string name. */
14587
f39c6ffd 14588static const char *
aa1ee363 14589dwarf_type_encoding_name (unsigned enc)
c906108c 14590{
f39c6ffd 14591 const char *name = get_DW_ATE_name (enc);
c906108c 14592
f39c6ffd
TT
14593 if (name == NULL)
14594 return "DW_ATE_<unknown>";
c906108c 14595
f39c6ffd 14596 return name;
c906108c 14597}
c906108c 14598
f9aca02d 14599static void
d97bc12b 14600dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
14601{
14602 unsigned int i;
14603
d97bc12b
DE
14604 print_spaces (indent, f);
14605 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 14606 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
14607
14608 if (die->parent != NULL)
14609 {
14610 print_spaces (indent, f);
14611 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 14612 die->parent->offset.sect_off);
d97bc12b
DE
14613 }
14614
14615 print_spaces (indent, f);
14616 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 14617 dwarf_bool_name (die->child != NULL));
c906108c 14618
d97bc12b
DE
14619 print_spaces (indent, f);
14620 fprintf_unfiltered (f, " attributes:\n");
14621
c906108c
SS
14622 for (i = 0; i < die->num_attrs; ++i)
14623 {
d97bc12b
DE
14624 print_spaces (indent, f);
14625 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
14626 dwarf_attr_name (die->attrs[i].name),
14627 dwarf_form_name (die->attrs[i].form));
d97bc12b 14628
c906108c
SS
14629 switch (die->attrs[i].form)
14630 {
c906108c 14631 case DW_FORM_addr:
3019eac3 14632 case DW_FORM_GNU_addr_index:
d97bc12b 14633 fprintf_unfiltered (f, "address: ");
5af949e3 14634 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
14635 break;
14636 case DW_FORM_block2:
14637 case DW_FORM_block4:
14638 case DW_FORM_block:
14639 case DW_FORM_block1:
3e43a32a
MS
14640 fprintf_unfiltered (f, "block: size %d",
14641 DW_BLOCK (&die->attrs[i])->size);
c906108c 14642 break;
2dc7f7b3
TT
14643 case DW_FORM_exprloc:
14644 fprintf_unfiltered (f, "expression: size %u",
14645 DW_BLOCK (&die->attrs[i])->size);
14646 break;
4568ecf9
DE
14647 case DW_FORM_ref_addr:
14648 fprintf_unfiltered (f, "ref address: ");
14649 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14650 break;
10b3939b
DJ
14651 case DW_FORM_ref1:
14652 case DW_FORM_ref2:
14653 case DW_FORM_ref4:
4568ecf9
DE
14654 case DW_FORM_ref8:
14655 case DW_FORM_ref_udata:
d97bc12b 14656 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 14657 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 14658 break;
c906108c
SS
14659 case DW_FORM_data1:
14660 case DW_FORM_data2:
14661 case DW_FORM_data4:
ce5d95e1 14662 case DW_FORM_data8:
c906108c
SS
14663 case DW_FORM_udata:
14664 case DW_FORM_sdata:
43bbcdc2
PH
14665 fprintf_unfiltered (f, "constant: %s",
14666 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 14667 break;
2dc7f7b3
TT
14668 case DW_FORM_sec_offset:
14669 fprintf_unfiltered (f, "section offset: %s",
14670 pulongest (DW_UNSND (&die->attrs[i])));
14671 break;
55f1336d 14672 case DW_FORM_ref_sig8:
348e048f
DE
14673 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14674 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 14675 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
14676 else
14677 fprintf_unfiltered (f, "signatured type, offset: unknown");
14678 break;
c906108c 14679 case DW_FORM_string:
4bdf3d34 14680 case DW_FORM_strp:
3019eac3 14681 case DW_FORM_GNU_str_index:
8285870a 14682 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 14683 DW_STRING (&die->attrs[i])
8285870a
JK
14684 ? DW_STRING (&die->attrs[i]) : "",
14685 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
14686 break;
14687 case DW_FORM_flag:
14688 if (DW_UNSND (&die->attrs[i]))
d97bc12b 14689 fprintf_unfiltered (f, "flag: TRUE");
c906108c 14690 else
d97bc12b 14691 fprintf_unfiltered (f, "flag: FALSE");
c906108c 14692 break;
2dc7f7b3
TT
14693 case DW_FORM_flag_present:
14694 fprintf_unfiltered (f, "flag: TRUE");
14695 break;
a8329558 14696 case DW_FORM_indirect:
0963b4bd
MS
14697 /* The reader will have reduced the indirect form to
14698 the "base form" so this form should not occur. */
3e43a32a
MS
14699 fprintf_unfiltered (f,
14700 "unexpected attribute form: DW_FORM_indirect");
a8329558 14701 break;
c906108c 14702 default:
d97bc12b 14703 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 14704 die->attrs[i].form);
d97bc12b 14705 break;
c906108c 14706 }
d97bc12b 14707 fprintf_unfiltered (f, "\n");
c906108c
SS
14708 }
14709}
14710
f9aca02d 14711static void
d97bc12b 14712dump_die_for_error (struct die_info *die)
c906108c 14713{
d97bc12b
DE
14714 dump_die_shallow (gdb_stderr, 0, die);
14715}
14716
14717static void
14718dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14719{
14720 int indent = level * 4;
14721
14722 gdb_assert (die != NULL);
14723
14724 if (level >= max_level)
14725 return;
14726
14727 dump_die_shallow (f, indent, die);
14728
14729 if (die->child != NULL)
c906108c 14730 {
d97bc12b
DE
14731 print_spaces (indent, f);
14732 fprintf_unfiltered (f, " Children:");
14733 if (level + 1 < max_level)
14734 {
14735 fprintf_unfiltered (f, "\n");
14736 dump_die_1 (f, level + 1, max_level, die->child);
14737 }
14738 else
14739 {
3e43a32a
MS
14740 fprintf_unfiltered (f,
14741 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14742 }
14743 }
14744
14745 if (die->sibling != NULL && level > 0)
14746 {
14747 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14748 }
14749}
14750
d97bc12b
DE
14751/* This is called from the pdie macro in gdbinit.in.
14752 It's not static so gcc will keep a copy callable from gdb. */
14753
14754void
14755dump_die (struct die_info *die, int max_level)
14756{
14757 dump_die_1 (gdb_stdlog, 0, max_level, die);
14758}
14759
f9aca02d 14760static void
51545339 14761store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14762{
51545339 14763 void **slot;
c906108c 14764
b64f50a1
JK
14765 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14766 INSERT);
51545339
DJ
14767
14768 *slot = die;
c906108c
SS
14769}
14770
b64f50a1
JK
14771/* DW_ADDR is always stored already as sect_offset; despite for the forms
14772 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14773
93311388
DE
14774static int
14775is_ref_attr (struct attribute *attr)
c906108c 14776{
c906108c
SS
14777 switch (attr->form)
14778 {
14779 case DW_FORM_ref_addr:
c906108c
SS
14780 case DW_FORM_ref1:
14781 case DW_FORM_ref2:
14782 case DW_FORM_ref4:
613e1657 14783 case DW_FORM_ref8:
c906108c 14784 case DW_FORM_ref_udata:
93311388 14785 return 1;
c906108c 14786 default:
93311388 14787 return 0;
c906108c 14788 }
93311388
DE
14789}
14790
b64f50a1
JK
14791/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14792 required kind. */
14793
14794static sect_offset
93311388
DE
14795dwarf2_get_ref_die_offset (struct attribute *attr)
14796{
4568ecf9 14797 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 14798
93311388 14799 if (is_ref_attr (attr))
b64f50a1 14800 return retval;
93311388 14801
b64f50a1 14802 retval.sect_off = 0;
93311388
DE
14803 complaint (&symfile_complaints,
14804 _("unsupported die ref attribute form: '%s'"),
14805 dwarf_form_name (attr->form));
b64f50a1 14806 return retval;
c906108c
SS
14807}
14808
43bbcdc2
PH
14809/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14810 * the value held by the attribute is not constant. */
a02abb62 14811
43bbcdc2 14812static LONGEST
a02abb62
JB
14813dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14814{
14815 if (attr->form == DW_FORM_sdata)
14816 return DW_SND (attr);
14817 else if (attr->form == DW_FORM_udata
14818 || attr->form == DW_FORM_data1
14819 || attr->form == DW_FORM_data2
14820 || attr->form == DW_FORM_data4
14821 || attr->form == DW_FORM_data8)
14822 return DW_UNSND (attr);
14823 else
14824 {
3e43a32a
MS
14825 complaint (&symfile_complaints,
14826 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14827 dwarf_form_name (attr->form));
14828 return default_value;
14829 }
14830}
14831
03dd20cc 14832/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14833 unit and add it to our queue.
14834 The result is non-zero if PER_CU was queued, otherwise the result is zero
14835 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14836
348e048f 14837static int
03dd20cc 14838maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
95554aad
TT
14839 struct dwarf2_per_cu_data *per_cu,
14840 enum language pretend_language)
03dd20cc 14841{
98bfdba5
PA
14842 /* We may arrive here during partial symbol reading, if we need full
14843 DIEs to process an unusual case (e.g. template arguments). Do
14844 not queue PER_CU, just tell our caller to load its DIEs. */
14845 if (dwarf2_per_objfile->reading_partial_symbols)
14846 {
14847 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14848 return 1;
14849 return 0;
14850 }
14851
03dd20cc
DJ
14852 /* Mark the dependence relation so that we don't flush PER_CU
14853 too early. */
14854 dwarf2_add_dependence (this_cu, per_cu);
14855
14856 /* If it's already on the queue, we have nothing to do. */
14857 if (per_cu->queued)
348e048f 14858 return 0;
03dd20cc
DJ
14859
14860 /* If the compilation unit is already loaded, just mark it as
14861 used. */
14862 if (per_cu->cu != NULL)
14863 {
14864 per_cu->cu->last_used = 0;
348e048f 14865 return 0;
03dd20cc
DJ
14866 }
14867
14868 /* Add it to the queue. */
95554aad 14869 queue_comp_unit (per_cu, pretend_language);
348e048f
DE
14870
14871 return 1;
14872}
14873
14874/* Follow reference or signature attribute ATTR of SRC_DIE.
14875 On entry *REF_CU is the CU of SRC_DIE.
14876 On exit *REF_CU is the CU of the result. */
14877
14878static struct die_info *
14879follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14880 struct dwarf2_cu **ref_cu)
14881{
14882 struct die_info *die;
14883
14884 if (is_ref_attr (attr))
14885 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14886 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14887 die = follow_die_sig (src_die, attr, ref_cu);
14888 else
14889 {
14890 dump_die_for_error (src_die);
14891 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14892 (*ref_cu)->objfile->name);
14893 }
14894
14895 return die;
03dd20cc
DJ
14896}
14897
5c631832 14898/* Follow reference OFFSET.
673bfd45
DE
14899 On entry *REF_CU is the CU of the source die referencing OFFSET.
14900 On exit *REF_CU is the CU of the result.
14901 Returns NULL if OFFSET is invalid. */
f504f079 14902
f9aca02d 14903static struct die_info *
b64f50a1 14904follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
c906108c 14905{
10b3939b 14906 struct die_info temp_die;
f2f0e013 14907 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14908
348e048f
DE
14909 gdb_assert (cu->per_cu != NULL);
14910
98bfdba5
PA
14911 target_cu = cu;
14912
3019eac3 14913 if (cu->per_cu->is_debug_types)
348e048f
DE
14914 {
14915 /* .debug_types CUs cannot reference anything outside their CU.
14916 If they need to, they have to reference a signatured type via
55f1336d 14917 DW_FORM_ref_sig8. */
348e048f 14918 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14919 return NULL;
348e048f
DE
14920 }
14921 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14922 {
14923 struct dwarf2_per_cu_data *per_cu;
9a619af0 14924
45452591 14925 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14926
14927 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
14928 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
14929 load_full_comp_unit (per_cu, cu->language);
03dd20cc 14930
10b3939b
DJ
14931 target_cu = per_cu->cu;
14932 }
98bfdba5
PA
14933 else if (cu->dies == NULL)
14934 {
14935 /* We're loading full DIEs during partial symbol reading. */
14936 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 14937 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 14938 }
c906108c 14939
f2f0e013 14940 *ref_cu = target_cu;
51545339 14941 temp_die.offset = offset;
b64f50a1 14942 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 14943}
10b3939b 14944
5c631832
JK
14945/* Follow reference attribute ATTR of SRC_DIE.
14946 On entry *REF_CU is the CU of SRC_DIE.
14947 On exit *REF_CU is the CU of the result. */
14948
14949static struct die_info *
14950follow_die_ref (struct die_info *src_die, struct attribute *attr,
14951 struct dwarf2_cu **ref_cu)
14952{
b64f50a1 14953 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
14954 struct dwarf2_cu *cu = *ref_cu;
14955 struct die_info *die;
14956
14957 die = follow_die_offset (offset, ref_cu);
14958 if (!die)
14959 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14960 "at 0x%x [in module %s]"),
b64f50a1 14961 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 14962
5c631832
JK
14963 return die;
14964}
14965
d83e736b
JK
14966/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14967 Returned value is intended for DW_OP_call*. Returned
14968 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14969
14970struct dwarf2_locexpr_baton
b64f50a1 14971dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
14972 struct dwarf2_per_cu_data *per_cu,
14973 CORE_ADDR (*get_frame_pc) (void *baton),
14974 void *baton)
5c631832 14975{
b64f50a1 14976 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 14977 struct dwarf2_cu *cu;
5c631832
JK
14978 struct die_info *die;
14979 struct attribute *attr;
14980 struct dwarf2_locexpr_baton retval;
14981
8cf6f0b1
TT
14982 dw2_setup (per_cu->objfile);
14983
918dd910
JK
14984 if (per_cu->cu == NULL)
14985 load_cu (per_cu);
14986 cu = per_cu->cu;
14987
5c631832
JK
14988 die = follow_die_offset (offset, &cu);
14989 if (!die)
14990 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 14991 offset.sect_off, per_cu->objfile->name);
5c631832
JK
14992
14993 attr = dwarf2_attr (die, DW_AT_location, cu);
14994 if (!attr)
14995 {
e103e986
JK
14996 /* DWARF: "If there is no such attribute, then there is no effect.".
14997 DATA is ignored if SIZE is 0. */
5c631832 14998
e103e986 14999 retval.data = NULL;
5c631832
JK
15000 retval.size = 0;
15001 }
8cf6f0b1
TT
15002 else if (attr_form_is_section_offset (attr))
15003 {
15004 struct dwarf2_loclist_baton loclist_baton;
15005 CORE_ADDR pc = (*get_frame_pc) (baton);
15006 size_t size;
15007
15008 fill_in_loclist_baton (cu, &loclist_baton, attr);
15009
15010 retval.data = dwarf2_find_location_expression (&loclist_baton,
15011 &size, pc);
15012 retval.size = size;
15013 }
5c631832
JK
15014 else
15015 {
15016 if (!attr_form_is_block (attr))
15017 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15018 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 15019 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15020
15021 retval.data = DW_BLOCK (attr)->data;
15022 retval.size = DW_BLOCK (attr)->size;
15023 }
15024 retval.per_cu = cu->per_cu;
918dd910 15025
918dd910
JK
15026 age_cached_comp_units ();
15027
5c631832 15028 return retval;
348e048f
DE
15029}
15030
8a9b8146
TT
15031/* Return the type of the DIE at DIE_OFFSET in the CU named by
15032 PER_CU. */
15033
15034struct type *
b64f50a1 15035dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
15036 struct dwarf2_per_cu_data *per_cu)
15037{
b64f50a1
JK
15038 sect_offset die_offset_sect;
15039
8a9b8146 15040 dw2_setup (per_cu->objfile);
b64f50a1
JK
15041
15042 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15043 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
15044}
15045
348e048f
DE
15046/* Follow the signature attribute ATTR in SRC_DIE.
15047 On entry *REF_CU is the CU of SRC_DIE.
15048 On exit *REF_CU is the CU of the result. */
15049
15050static struct die_info *
15051follow_die_sig (struct die_info *src_die, struct attribute *attr,
15052 struct dwarf2_cu **ref_cu)
15053{
15054 struct objfile *objfile = (*ref_cu)->objfile;
15055 struct die_info temp_die;
15056 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15057 struct dwarf2_cu *sig_cu;
15058 struct die_info *die;
15059
15060 /* sig_type will be NULL if the signatured type is missing from
15061 the debug info. */
15062 if (sig_type == NULL)
15063 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15064 "at 0x%x [in module %s]"),
b64f50a1 15065 src_die->offset.sect_off, objfile->name);
348e048f
DE
15066
15067 /* If necessary, add it to the queue and load its DIEs. */
15068
95554aad 15069 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 15070 read_signatured_type (sig_type);
348e048f
DE
15071
15072 gdb_assert (sig_type->per_cu.cu != NULL);
15073
15074 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
15075 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15076 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
15077 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15078 temp_die.offset.sect_off);
348e048f
DE
15079 if (die)
15080 {
15081 *ref_cu = sig_cu;
15082 return die;
15083 }
15084
3e43a32a
MS
15085 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15086 "from DIE at 0x%x [in module %s]"),
b64f50a1 15087 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
15088}
15089
15090/* Given an offset of a signatured type, return its signatured_type. */
15091
15092static struct signatured_type *
8b70b953
TT
15093lookup_signatured_type_at_offset (struct objfile *objfile,
15094 struct dwarf2_section_info *section,
b64f50a1 15095 sect_offset offset)
348e048f 15096{
b64f50a1 15097 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
15098 unsigned int length, initial_length_size;
15099 unsigned int sig_offset;
52dc124a 15100 struct signatured_type find_entry, *sig_type;
348e048f
DE
15101
15102 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15103 sig_offset = (initial_length_size
15104 + 2 /*version*/
15105 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15106 + 1 /*address_size*/);
15107 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 15108 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
15109
15110 /* This is only used to lookup previously recorded types.
15111 If we didn't find it, it's our bug. */
52dc124a
DE
15112 gdb_assert (sig_type != NULL);
15113 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 15114
52dc124a 15115 return sig_type;
348e048f
DE
15116}
15117
e5fe5e75 15118/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
15119
15120static void
e5fe5e75 15121load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 15122{
e5fe5e75 15123 struct objfile *objfile = per_cu->objfile;
3019eac3 15124 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
b64f50a1 15125 sect_offset offset = per_cu->offset;
52dc124a 15126 struct signatured_type *sig_type;
348e048f 15127
8b70b953 15128 dwarf2_read_section (objfile, sect);
be391dca 15129
348e048f 15130 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
15131 hash table lookup. */
15132 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15133 the signature to assert we found the right one.
15134 Ok, but it's a lot of work. We should simplify things so any needed
15135 assert doesn't require all this clumsiness. */
52dc124a 15136 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f 15137
dee91e82 15138 gdb_assert (&sig_type->per_cu == per_cu);
52dc124a 15139 gdb_assert (sig_type->per_cu.cu == NULL);
348e048f 15140
52dc124a 15141 read_signatured_type (sig_type);
348e048f 15142
52dc124a 15143 gdb_assert (sig_type->per_cu.cu != NULL);
348e048f
DE
15144}
15145
dee91e82
DE
15146/* die_reader_func for read_signatured_type.
15147 This is identical to load_full_comp_unit_reader,
15148 but is kept separate for now. */
348e048f
DE
15149
15150static void
dee91e82
DE
15151read_signatured_type_reader (const struct die_reader_specs *reader,
15152 gdb_byte *info_ptr,
15153 struct die_info *comp_unit_die,
15154 int has_children,
15155 void *data)
348e048f 15156{
dee91e82
DE
15157 struct dwarf2_cu *cu = reader->cu;
15158 struct attribute *attr;
348e048f 15159
dee91e82
DE
15160 gdb_assert (cu->die_hash == NULL);
15161 cu->die_hash =
15162 htab_create_alloc_ex (cu->header.length / 12,
15163 die_hash,
15164 die_eq,
15165 NULL,
15166 &cu->comp_unit_obstack,
15167 hashtab_obstack_allocate,
15168 dummy_obstack_deallocate);
348e048f 15169
dee91e82
DE
15170 if (has_children)
15171 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15172 &info_ptr, comp_unit_die);
15173 cu->dies = comp_unit_die;
15174 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
15175
15176 /* We try not to read any attributes in this function, because not
9cdd5dbd 15177 all CUs needed for references have been loaded yet, and symbol
348e048f 15178 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
15179 or we won't be able to build types correctly.
15180 Similarly, if we do not read the producer, we can not apply
15181 producer-specific interpretation. */
95554aad 15182 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 15183}
348e048f 15184
3019eac3
DE
15185/* Read in a signatured type and build its CU and DIEs.
15186 If the type is a stub for the real type in a DWO file,
15187 read in the real type from the DWO file as well. */
dee91e82
DE
15188
15189static void
15190read_signatured_type (struct signatured_type *sig_type)
15191{
15192 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 15193
3019eac3 15194 gdb_assert (per_cu->is_debug_types);
dee91e82 15195 gdb_assert (per_cu->cu == NULL);
348e048f 15196
fd820528 15197 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
c906108c
SS
15198}
15199
c906108c
SS
15200/* Decode simple location descriptions.
15201 Given a pointer to a dwarf block that defines a location, compute
15202 the location and return the value.
15203
4cecd739
DJ
15204 NOTE drow/2003-11-18: This function is called in two situations
15205 now: for the address of static or global variables (partial symbols
15206 only) and for offsets into structures which are expected to be
15207 (more or less) constant. The partial symbol case should go away,
15208 and only the constant case should remain. That will let this
15209 function complain more accurately. A few special modes are allowed
15210 without complaint for global variables (for instance, global
15211 register values and thread-local values).
c906108c
SS
15212
15213 A location description containing no operations indicates that the
4cecd739 15214 object is optimized out. The return value is 0 for that case.
6b992462
DJ
15215 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15216 callers will only want a very basic result and this can become a
21ae7a4d
JK
15217 complaint.
15218
15219 Note that stack[0] is unused except as a default error return. */
c906108c
SS
15220
15221static CORE_ADDR
e7c27a73 15222decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 15223{
e7c27a73 15224 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
15225 int i;
15226 int size = blk->size;
15227 gdb_byte *data = blk->data;
15228 CORE_ADDR stack[64];
15229 int stacki;
15230 unsigned int bytes_read, unsnd;
15231 gdb_byte op;
c906108c 15232
21ae7a4d
JK
15233 i = 0;
15234 stacki = 0;
15235 stack[stacki] = 0;
15236 stack[++stacki] = 0;
15237
15238 while (i < size)
15239 {
15240 op = data[i++];
15241 switch (op)
15242 {
15243 case DW_OP_lit0:
15244 case DW_OP_lit1:
15245 case DW_OP_lit2:
15246 case DW_OP_lit3:
15247 case DW_OP_lit4:
15248 case DW_OP_lit5:
15249 case DW_OP_lit6:
15250 case DW_OP_lit7:
15251 case DW_OP_lit8:
15252 case DW_OP_lit9:
15253 case DW_OP_lit10:
15254 case DW_OP_lit11:
15255 case DW_OP_lit12:
15256 case DW_OP_lit13:
15257 case DW_OP_lit14:
15258 case DW_OP_lit15:
15259 case DW_OP_lit16:
15260 case DW_OP_lit17:
15261 case DW_OP_lit18:
15262 case DW_OP_lit19:
15263 case DW_OP_lit20:
15264 case DW_OP_lit21:
15265 case DW_OP_lit22:
15266 case DW_OP_lit23:
15267 case DW_OP_lit24:
15268 case DW_OP_lit25:
15269 case DW_OP_lit26:
15270 case DW_OP_lit27:
15271 case DW_OP_lit28:
15272 case DW_OP_lit29:
15273 case DW_OP_lit30:
15274 case DW_OP_lit31:
15275 stack[++stacki] = op - DW_OP_lit0;
15276 break;
f1bea926 15277
21ae7a4d
JK
15278 case DW_OP_reg0:
15279 case DW_OP_reg1:
15280 case DW_OP_reg2:
15281 case DW_OP_reg3:
15282 case DW_OP_reg4:
15283 case DW_OP_reg5:
15284 case DW_OP_reg6:
15285 case DW_OP_reg7:
15286 case DW_OP_reg8:
15287 case DW_OP_reg9:
15288 case DW_OP_reg10:
15289 case DW_OP_reg11:
15290 case DW_OP_reg12:
15291 case DW_OP_reg13:
15292 case DW_OP_reg14:
15293 case DW_OP_reg15:
15294 case DW_OP_reg16:
15295 case DW_OP_reg17:
15296 case DW_OP_reg18:
15297 case DW_OP_reg19:
15298 case DW_OP_reg20:
15299 case DW_OP_reg21:
15300 case DW_OP_reg22:
15301 case DW_OP_reg23:
15302 case DW_OP_reg24:
15303 case DW_OP_reg25:
15304 case DW_OP_reg26:
15305 case DW_OP_reg27:
15306 case DW_OP_reg28:
15307 case DW_OP_reg29:
15308 case DW_OP_reg30:
15309 case DW_OP_reg31:
15310 stack[++stacki] = op - DW_OP_reg0;
15311 if (i < size)
15312 dwarf2_complex_location_expr_complaint ();
15313 break;
c906108c 15314
21ae7a4d
JK
15315 case DW_OP_regx:
15316 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15317 i += bytes_read;
15318 stack[++stacki] = unsnd;
15319 if (i < size)
15320 dwarf2_complex_location_expr_complaint ();
15321 break;
c906108c 15322
21ae7a4d
JK
15323 case DW_OP_addr:
15324 stack[++stacki] = read_address (objfile->obfd, &data[i],
15325 cu, &bytes_read);
15326 i += bytes_read;
15327 break;
d53d4ac5 15328
21ae7a4d
JK
15329 case DW_OP_const1u:
15330 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15331 i += 1;
15332 break;
15333
15334 case DW_OP_const1s:
15335 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15336 i += 1;
15337 break;
15338
15339 case DW_OP_const2u:
15340 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15341 i += 2;
15342 break;
15343
15344 case DW_OP_const2s:
15345 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15346 i += 2;
15347 break;
d53d4ac5 15348
21ae7a4d
JK
15349 case DW_OP_const4u:
15350 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15351 i += 4;
15352 break;
15353
15354 case DW_OP_const4s:
15355 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15356 i += 4;
15357 break;
15358
585861ea
JK
15359 case DW_OP_const8u:
15360 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15361 i += 8;
15362 break;
15363
21ae7a4d
JK
15364 case DW_OP_constu:
15365 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15366 &bytes_read);
15367 i += bytes_read;
15368 break;
15369
15370 case DW_OP_consts:
15371 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15372 i += bytes_read;
15373 break;
15374
15375 case DW_OP_dup:
15376 stack[stacki + 1] = stack[stacki];
15377 stacki++;
15378 break;
15379
15380 case DW_OP_plus:
15381 stack[stacki - 1] += stack[stacki];
15382 stacki--;
15383 break;
15384
15385 case DW_OP_plus_uconst:
15386 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15387 &bytes_read);
15388 i += bytes_read;
15389 break;
15390
15391 case DW_OP_minus:
15392 stack[stacki - 1] -= stack[stacki];
15393 stacki--;
15394 break;
15395
15396 case DW_OP_deref:
15397 /* If we're not the last op, then we definitely can't encode
15398 this using GDB's address_class enum. This is valid for partial
15399 global symbols, although the variable's address will be bogus
15400 in the psymtab. */
15401 if (i < size)
15402 dwarf2_complex_location_expr_complaint ();
15403 break;
15404
15405 case DW_OP_GNU_push_tls_address:
15406 /* The top of the stack has the offset from the beginning
15407 of the thread control block at which the variable is located. */
15408 /* Nothing should follow this operator, so the top of stack would
15409 be returned. */
15410 /* This is valid for partial global symbols, but the variable's
585861ea
JK
15411 address will be bogus in the psymtab. Make it always at least
15412 non-zero to not look as a variable garbage collected by linker
15413 which have DW_OP_addr 0. */
21ae7a4d
JK
15414 if (i < size)
15415 dwarf2_complex_location_expr_complaint ();
585861ea 15416 stack[stacki]++;
21ae7a4d
JK
15417 break;
15418
15419 case DW_OP_GNU_uninit:
15420 break;
15421
3019eac3
DE
15422 case DW_OP_GNU_addr_index:
15423 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15424 &bytes_read);
15425 i += bytes_read;
15426 break;
15427
21ae7a4d
JK
15428 default:
15429 {
f39c6ffd 15430 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
15431
15432 if (name)
15433 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15434 name);
15435 else
15436 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15437 op);
15438 }
15439
15440 return (stack[stacki]);
d53d4ac5 15441 }
3c6e0cb3 15442
21ae7a4d
JK
15443 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15444 outside of the allocated space. Also enforce minimum>0. */
15445 if (stacki >= ARRAY_SIZE (stack) - 1)
15446 {
15447 complaint (&symfile_complaints,
15448 _("location description stack overflow"));
15449 return 0;
15450 }
15451
15452 if (stacki <= 0)
15453 {
15454 complaint (&symfile_complaints,
15455 _("location description stack underflow"));
15456 return 0;
15457 }
15458 }
15459 return (stack[stacki]);
c906108c
SS
15460}
15461
15462/* memory allocation interface */
15463
c906108c 15464static struct dwarf_block *
7b5a2f43 15465dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
15466{
15467 struct dwarf_block *blk;
15468
15469 blk = (struct dwarf_block *)
7b5a2f43 15470 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
15471 return (blk);
15472}
15473
15474static struct abbrev_info *
f3dd6933 15475dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
15476{
15477 struct abbrev_info *abbrev;
15478
f3dd6933
DJ
15479 abbrev = (struct abbrev_info *)
15480 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
15481 memset (abbrev, 0, sizeof (struct abbrev_info));
15482 return (abbrev);
15483}
15484
15485static struct die_info *
b60c80d6 15486dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
15487{
15488 struct die_info *die;
b60c80d6
DJ
15489 size_t size = sizeof (struct die_info);
15490
15491 if (num_attrs > 1)
15492 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 15493
b60c80d6 15494 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
15495 memset (die, 0, sizeof (struct die_info));
15496 return (die);
15497}
2e276125
JB
15498
15499\f
15500/* Macro support. */
15501
2e276125
JB
15502/* Return the full name of file number I in *LH's file name table.
15503 Use COMP_DIR as the name of the current directory of the
15504 compilation. The result is allocated using xmalloc; the caller is
15505 responsible for freeing it. */
15506static char *
15507file_full_name (int file, struct line_header *lh, const char *comp_dir)
15508{
6a83a1e6
EZ
15509 /* Is the file number a valid index into the line header's file name
15510 table? Remember that file numbers start with one, not zero. */
15511 if (1 <= file && file <= lh->num_file_names)
15512 {
15513 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 15514
6a83a1e6
EZ
15515 if (IS_ABSOLUTE_PATH (fe->name))
15516 return xstrdup (fe->name);
15517 else
15518 {
15519 const char *dir;
15520 int dir_len;
15521 char *full_name;
15522
15523 if (fe->dir_index)
15524 dir = lh->include_dirs[fe->dir_index - 1];
15525 else
15526 dir = comp_dir;
15527
15528 if (dir)
15529 {
15530 dir_len = strlen (dir);
15531 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15532 strcpy (full_name, dir);
15533 full_name[dir_len] = '/';
15534 strcpy (full_name + dir_len + 1, fe->name);
15535 return full_name;
15536 }
15537 else
15538 return xstrdup (fe->name);
15539 }
15540 }
2e276125
JB
15541 else
15542 {
6a83a1e6
EZ
15543 /* The compiler produced a bogus file number. We can at least
15544 record the macro definitions made in the file, even if we
15545 won't be able to find the file by name. */
15546 char fake_name[80];
9a619af0 15547
6a83a1e6 15548 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 15549
6e70227d 15550 complaint (&symfile_complaints,
6a83a1e6
EZ
15551 _("bad file number in macro information (%d)"),
15552 file);
2e276125 15553
6a83a1e6 15554 return xstrdup (fake_name);
2e276125
JB
15555 }
15556}
15557
15558
15559static struct macro_source_file *
15560macro_start_file (int file, int line,
15561 struct macro_source_file *current_file,
15562 const char *comp_dir,
15563 struct line_header *lh, struct objfile *objfile)
15564{
15565 /* The full name of this source file. */
15566 char *full_name = file_full_name (file, lh, comp_dir);
15567
15568 /* We don't create a macro table for this compilation unit
15569 at all until we actually get a filename. */
15570 if (! pending_macros)
4a146b47 15571 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 15572 objfile->macro_cache);
2e276125
JB
15573
15574 if (! current_file)
abc9d0dc
TT
15575 {
15576 /* If we have no current file, then this must be the start_file
15577 directive for the compilation unit's main source file. */
15578 current_file = macro_set_main (pending_macros, full_name);
15579 macro_define_special (pending_macros);
15580 }
2e276125
JB
15581 else
15582 current_file = macro_include (current_file, line, full_name);
15583
15584 xfree (full_name);
6e70227d 15585
2e276125
JB
15586 return current_file;
15587}
15588
15589
15590/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15591 followed by a null byte. */
15592static char *
15593copy_string (const char *buf, int len)
15594{
15595 char *s = xmalloc (len + 1);
9a619af0 15596
2e276125
JB
15597 memcpy (s, buf, len);
15598 s[len] = '\0';
2e276125
JB
15599 return s;
15600}
15601
15602
15603static const char *
15604consume_improper_spaces (const char *p, const char *body)
15605{
15606 if (*p == ' ')
15607 {
4d3c2250 15608 complaint (&symfile_complaints,
3e43a32a
MS
15609 _("macro definition contains spaces "
15610 "in formal argument list:\n`%s'"),
4d3c2250 15611 body);
2e276125
JB
15612
15613 while (*p == ' ')
15614 p++;
15615 }
15616
15617 return p;
15618}
15619
15620
15621static void
15622parse_macro_definition (struct macro_source_file *file, int line,
15623 const char *body)
15624{
15625 const char *p;
15626
15627 /* The body string takes one of two forms. For object-like macro
15628 definitions, it should be:
15629
15630 <macro name> " " <definition>
15631
15632 For function-like macro definitions, it should be:
15633
15634 <macro name> "() " <definition>
15635 or
15636 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15637
15638 Spaces may appear only where explicitly indicated, and in the
15639 <definition>.
15640
15641 The Dwarf 2 spec says that an object-like macro's name is always
15642 followed by a space, but versions of GCC around March 2002 omit
6e70227d 15643 the space when the macro's definition is the empty string.
2e276125
JB
15644
15645 The Dwarf 2 spec says that there should be no spaces between the
15646 formal arguments in a function-like macro's formal argument list,
15647 but versions of GCC around March 2002 include spaces after the
15648 commas. */
15649
15650
15651 /* Find the extent of the macro name. The macro name is terminated
15652 by either a space or null character (for an object-like macro) or
15653 an opening paren (for a function-like macro). */
15654 for (p = body; *p; p++)
15655 if (*p == ' ' || *p == '(')
15656 break;
15657
15658 if (*p == ' ' || *p == '\0')
15659 {
15660 /* It's an object-like macro. */
15661 int name_len = p - body;
15662 char *name = copy_string (body, name_len);
15663 const char *replacement;
15664
15665 if (*p == ' ')
15666 replacement = body + name_len + 1;
15667 else
15668 {
4d3c2250 15669 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15670 replacement = body + name_len;
15671 }
6e70227d 15672
2e276125
JB
15673 macro_define_object (file, line, name, replacement);
15674
15675 xfree (name);
15676 }
15677 else if (*p == '(')
15678 {
15679 /* It's a function-like macro. */
15680 char *name = copy_string (body, p - body);
15681 int argc = 0;
15682 int argv_size = 1;
15683 char **argv = xmalloc (argv_size * sizeof (*argv));
15684
15685 p++;
15686
15687 p = consume_improper_spaces (p, body);
15688
15689 /* Parse the formal argument list. */
15690 while (*p && *p != ')')
15691 {
15692 /* Find the extent of the current argument name. */
15693 const char *arg_start = p;
15694
15695 while (*p && *p != ',' && *p != ')' && *p != ' ')
15696 p++;
15697
15698 if (! *p || p == arg_start)
4d3c2250 15699 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15700 else
15701 {
15702 /* Make sure argv has room for the new argument. */
15703 if (argc >= argv_size)
15704 {
15705 argv_size *= 2;
15706 argv = xrealloc (argv, argv_size * sizeof (*argv));
15707 }
15708
15709 argv[argc++] = copy_string (arg_start, p - arg_start);
15710 }
15711
15712 p = consume_improper_spaces (p, body);
15713
15714 /* Consume the comma, if present. */
15715 if (*p == ',')
15716 {
15717 p++;
15718
15719 p = consume_improper_spaces (p, body);
15720 }
15721 }
15722
15723 if (*p == ')')
15724 {
15725 p++;
15726
15727 if (*p == ' ')
15728 /* Perfectly formed definition, no complaints. */
15729 macro_define_function (file, line, name,
6e70227d 15730 argc, (const char **) argv,
2e276125
JB
15731 p + 1);
15732 else if (*p == '\0')
15733 {
15734 /* Complain, but do define it. */
4d3c2250 15735 dwarf2_macro_malformed_definition_complaint (body);
2e276125 15736 macro_define_function (file, line, name,
6e70227d 15737 argc, (const char **) argv,
2e276125
JB
15738 p);
15739 }
15740 else
15741 /* Just complain. */
4d3c2250 15742 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15743 }
15744 else
15745 /* Just complain. */
4d3c2250 15746 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15747
15748 xfree (name);
15749 {
15750 int i;
15751
15752 for (i = 0; i < argc; i++)
15753 xfree (argv[i]);
15754 }
15755 xfree (argv);
15756 }
15757 else
4d3c2250 15758 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15759}
15760
cf2c3c16
TT
15761/* Skip some bytes from BYTES according to the form given in FORM.
15762 Returns the new pointer. */
2e276125 15763
cf2c3c16
TT
15764static gdb_byte *
15765skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15766 enum dwarf_form form,
15767 unsigned int offset_size,
15768 struct dwarf2_section_info *section)
2e276125 15769{
cf2c3c16 15770 unsigned int bytes_read;
2e276125 15771
cf2c3c16 15772 switch (form)
2e276125 15773 {
cf2c3c16
TT
15774 case DW_FORM_data1:
15775 case DW_FORM_flag:
15776 ++bytes;
15777 break;
15778
15779 case DW_FORM_data2:
15780 bytes += 2;
15781 break;
15782
15783 case DW_FORM_data4:
15784 bytes += 4;
15785 break;
15786
15787 case DW_FORM_data8:
15788 bytes += 8;
15789 break;
15790
15791 case DW_FORM_string:
15792 read_direct_string (abfd, bytes, &bytes_read);
15793 bytes += bytes_read;
15794 break;
15795
15796 case DW_FORM_sec_offset:
15797 case DW_FORM_strp:
15798 bytes += offset_size;
15799 break;
15800
15801 case DW_FORM_block:
15802 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15803 bytes += bytes_read;
15804 break;
15805
15806 case DW_FORM_block1:
15807 bytes += 1 + read_1_byte (abfd, bytes);
15808 break;
15809 case DW_FORM_block2:
15810 bytes += 2 + read_2_bytes (abfd, bytes);
15811 break;
15812 case DW_FORM_block4:
15813 bytes += 4 + read_4_bytes (abfd, bytes);
15814 break;
15815
15816 case DW_FORM_sdata:
15817 case DW_FORM_udata:
3019eac3
DE
15818 case DW_FORM_GNU_addr_index:
15819 case DW_FORM_GNU_str_index:
cf2c3c16
TT
15820 bytes = skip_leb128 (abfd, bytes);
15821 break;
15822
15823 default:
15824 {
15825 complain:
15826 complaint (&symfile_complaints,
15827 _("invalid form 0x%x in `%s'"),
15828 form,
15829 section->asection->name);
15830 return NULL;
15831 }
2e276125
JB
15832 }
15833
cf2c3c16
TT
15834 return bytes;
15835}
757a13d0 15836
cf2c3c16
TT
15837/* A helper for dwarf_decode_macros that handles skipping an unknown
15838 opcode. Returns an updated pointer to the macro data buffer; or,
15839 on error, issues a complaint and returns NULL. */
757a13d0 15840
cf2c3c16
TT
15841static gdb_byte *
15842skip_unknown_opcode (unsigned int opcode,
15843 gdb_byte **opcode_definitions,
15844 gdb_byte *mac_ptr,
15845 bfd *abfd,
15846 unsigned int offset_size,
15847 struct dwarf2_section_info *section)
15848{
15849 unsigned int bytes_read, i;
15850 unsigned long arg;
15851 gdb_byte *defn;
2e276125 15852
cf2c3c16 15853 if (opcode_definitions[opcode] == NULL)
2e276125 15854 {
cf2c3c16
TT
15855 complaint (&symfile_complaints,
15856 _("unrecognized DW_MACFINO opcode 0x%x"),
15857 opcode);
15858 return NULL;
15859 }
2e276125 15860
cf2c3c16
TT
15861 defn = opcode_definitions[opcode];
15862 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15863 defn += bytes_read;
2e276125 15864
cf2c3c16
TT
15865 for (i = 0; i < arg; ++i)
15866 {
15867 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15868 if (mac_ptr == NULL)
15869 {
15870 /* skip_form_bytes already issued the complaint. */
15871 return NULL;
15872 }
15873 }
757a13d0 15874
cf2c3c16
TT
15875 return mac_ptr;
15876}
757a13d0 15877
cf2c3c16
TT
15878/* A helper function which parses the header of a macro section.
15879 If the macro section is the extended (for now called "GNU") type,
15880 then this updates *OFFSET_SIZE. Returns a pointer to just after
15881 the header, or issues a complaint and returns NULL on error. */
757a13d0 15882
cf2c3c16
TT
15883static gdb_byte *
15884dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15885 bfd *abfd,
15886 gdb_byte *mac_ptr,
15887 unsigned int *offset_size,
15888 int section_is_gnu)
15889{
15890 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15891
cf2c3c16
TT
15892 if (section_is_gnu)
15893 {
15894 unsigned int version, flags;
757a13d0 15895
cf2c3c16
TT
15896 version = read_2_bytes (abfd, mac_ptr);
15897 if (version != 4)
15898 {
15899 complaint (&symfile_complaints,
15900 _("unrecognized version `%d' in .debug_macro section"),
15901 version);
15902 return NULL;
15903 }
15904 mac_ptr += 2;
757a13d0 15905
cf2c3c16
TT
15906 flags = read_1_byte (abfd, mac_ptr);
15907 ++mac_ptr;
15908 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15909
cf2c3c16
TT
15910 if ((flags & 2) != 0)
15911 /* We don't need the line table offset. */
15912 mac_ptr += *offset_size;
757a13d0 15913
cf2c3c16
TT
15914 /* Vendor opcode descriptions. */
15915 if ((flags & 4) != 0)
15916 {
15917 unsigned int i, count;
757a13d0 15918
cf2c3c16
TT
15919 count = read_1_byte (abfd, mac_ptr);
15920 ++mac_ptr;
15921 for (i = 0; i < count; ++i)
15922 {
15923 unsigned int opcode, bytes_read;
15924 unsigned long arg;
15925
15926 opcode = read_1_byte (abfd, mac_ptr);
15927 ++mac_ptr;
15928 opcode_definitions[opcode] = mac_ptr;
15929 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15930 mac_ptr += bytes_read;
15931 mac_ptr += arg;
15932 }
757a13d0 15933 }
cf2c3c16 15934 }
757a13d0 15935
cf2c3c16
TT
15936 return mac_ptr;
15937}
757a13d0 15938
cf2c3c16 15939/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 15940 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
15941
15942static void
15943dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15944 struct macro_source_file *current_file,
15945 struct line_header *lh, char *comp_dir,
15946 struct dwarf2_section_info *section,
15947 int section_is_gnu,
15948 unsigned int offset_size,
8fc3fc34
TT
15949 struct objfile *objfile,
15950 htab_t include_hash)
cf2c3c16
TT
15951{
15952 enum dwarf_macro_record_type macinfo_type;
15953 int at_commandline;
15954 gdb_byte *opcode_definitions[256];
757a13d0 15955
cf2c3c16
TT
15956 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15957 &offset_size, section_is_gnu);
15958 if (mac_ptr == NULL)
15959 {
15960 /* We already issued a complaint. */
15961 return;
15962 }
757a13d0
JK
15963
15964 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15965 GDB is still reading the definitions from command line. First
15966 DW_MACINFO_start_file will need to be ignored as it was already executed
15967 to create CURRENT_FILE for the main source holding also the command line
15968 definitions. On first met DW_MACINFO_start_file this flag is reset to
15969 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15970
15971 at_commandline = 1;
15972
15973 do
15974 {
15975 /* Do we at least have room for a macinfo type byte? */
15976 if (mac_ptr >= mac_end)
15977 {
cf2c3c16 15978 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15979 break;
15980 }
15981
15982 macinfo_type = read_1_byte (abfd, mac_ptr);
15983 mac_ptr++;
15984
cf2c3c16
TT
15985 /* Note that we rely on the fact that the corresponding GNU and
15986 DWARF constants are the same. */
757a13d0
JK
15987 switch (macinfo_type)
15988 {
15989 /* A zero macinfo type indicates the end of the macro
15990 information. */
15991 case 0:
15992 break;
2e276125 15993
cf2c3c16
TT
15994 case DW_MACRO_GNU_define:
15995 case DW_MACRO_GNU_undef:
15996 case DW_MACRO_GNU_define_indirect:
15997 case DW_MACRO_GNU_undef_indirect:
2e276125 15998 {
891d2f0b 15999 unsigned int bytes_read;
2e276125
JB
16000 int line;
16001 char *body;
cf2c3c16 16002 int is_define;
2e276125 16003
cf2c3c16
TT
16004 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16005 mac_ptr += bytes_read;
16006
16007 if (macinfo_type == DW_MACRO_GNU_define
16008 || macinfo_type == DW_MACRO_GNU_undef)
16009 {
16010 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16011 mac_ptr += bytes_read;
16012 }
16013 else
16014 {
16015 LONGEST str_offset;
16016
16017 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16018 mac_ptr += offset_size;
2e276125 16019
cf2c3c16
TT
16020 body = read_indirect_string_at_offset (abfd, str_offset);
16021 }
16022
16023 is_define = (macinfo_type == DW_MACRO_GNU_define
16024 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 16025 if (! current_file)
757a13d0
JK
16026 {
16027 /* DWARF violation as no main source is present. */
16028 complaint (&symfile_complaints,
16029 _("debug info with no main source gives macro %s "
16030 "on line %d: %s"),
cf2c3c16
TT
16031 is_define ? _("definition") : _("undefinition"),
16032 line, body);
757a13d0
JK
16033 break;
16034 }
3e43a32a
MS
16035 if ((line == 0 && !at_commandline)
16036 || (line != 0 && at_commandline))
4d3c2250 16037 complaint (&symfile_complaints,
757a13d0
JK
16038 _("debug info gives %s macro %s with %s line %d: %s"),
16039 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 16040 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
16041 line == 0 ? _("zero") : _("non-zero"), line, body);
16042
cf2c3c16 16043 if (is_define)
757a13d0 16044 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
16045 else
16046 {
16047 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16048 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16049 macro_undef (current_file, line, body);
16050 }
2e276125
JB
16051 }
16052 break;
16053
cf2c3c16 16054 case DW_MACRO_GNU_start_file:
2e276125 16055 {
891d2f0b 16056 unsigned int bytes_read;
2e276125
JB
16057 int line, file;
16058
16059 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16060 mac_ptr += bytes_read;
16061 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16062 mac_ptr += bytes_read;
16063
3e43a32a
MS
16064 if ((line == 0 && !at_commandline)
16065 || (line != 0 && at_commandline))
757a13d0
JK
16066 complaint (&symfile_complaints,
16067 _("debug info gives source %d included "
16068 "from %s at %s line %d"),
16069 file, at_commandline ? _("command-line") : _("file"),
16070 line == 0 ? _("zero") : _("non-zero"), line);
16071
16072 if (at_commandline)
16073 {
cf2c3c16
TT
16074 /* This DW_MACRO_GNU_start_file was executed in the
16075 pass one. */
757a13d0
JK
16076 at_commandline = 0;
16077 }
16078 else
16079 current_file = macro_start_file (file, line,
16080 current_file, comp_dir,
cf2c3c16 16081 lh, objfile);
2e276125
JB
16082 }
16083 break;
16084
cf2c3c16 16085 case DW_MACRO_GNU_end_file:
2e276125 16086 if (! current_file)
4d3c2250 16087 complaint (&symfile_complaints,
3e43a32a
MS
16088 _("macro debug info has an unmatched "
16089 "`close_file' directive"));
2e276125
JB
16090 else
16091 {
16092 current_file = current_file->included_by;
16093 if (! current_file)
16094 {
cf2c3c16 16095 enum dwarf_macro_record_type next_type;
2e276125
JB
16096
16097 /* GCC circa March 2002 doesn't produce the zero
16098 type byte marking the end of the compilation
16099 unit. Complain if it's not there, but exit no
16100 matter what. */
16101
16102 /* Do we at least have room for a macinfo type byte? */
16103 if (mac_ptr >= mac_end)
16104 {
cf2c3c16 16105 dwarf2_macros_too_long_complaint (section);
2e276125
JB
16106 return;
16107 }
16108
16109 /* We don't increment mac_ptr here, so this is just
16110 a look-ahead. */
16111 next_type = read_1_byte (abfd, mac_ptr);
16112 if (next_type != 0)
4d3c2250 16113 complaint (&symfile_complaints,
3e43a32a
MS
16114 _("no terminating 0-type entry for "
16115 "macros in `.debug_macinfo' section"));
2e276125
JB
16116
16117 return;
16118 }
16119 }
16120 break;
16121
cf2c3c16
TT
16122 case DW_MACRO_GNU_transparent_include:
16123 {
16124 LONGEST offset;
8fc3fc34 16125 void **slot;
cf2c3c16
TT
16126
16127 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16128 mac_ptr += offset_size;
16129
8fc3fc34
TT
16130 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16131 if (*slot != NULL)
16132 {
16133 /* This has actually happened; see
16134 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16135 complaint (&symfile_complaints,
16136 _("recursive DW_MACRO_GNU_transparent_include in "
16137 ".debug_macro section"));
16138 }
16139 else
16140 {
16141 *slot = mac_ptr;
16142
16143 dwarf_decode_macro_bytes (abfd,
16144 section->buffer + offset,
16145 mac_end, current_file,
16146 lh, comp_dir,
16147 section, section_is_gnu,
16148 offset_size, objfile, include_hash);
16149
16150 htab_remove_elt (include_hash, mac_ptr);
16151 }
cf2c3c16
TT
16152 }
16153 break;
16154
2e276125 16155 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
16156 if (!section_is_gnu)
16157 {
16158 unsigned int bytes_read;
16159 int constant;
2e276125 16160
cf2c3c16
TT
16161 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16162 mac_ptr += bytes_read;
16163 read_direct_string (abfd, mac_ptr, &bytes_read);
16164 mac_ptr += bytes_read;
2e276125 16165
cf2c3c16
TT
16166 /* We don't recognize any vendor extensions. */
16167 break;
16168 }
16169 /* FALLTHROUGH */
16170
16171 default:
16172 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16173 mac_ptr, abfd, offset_size,
16174 section);
16175 if (mac_ptr == NULL)
16176 return;
16177 break;
2e276125 16178 }
757a13d0 16179 } while (macinfo_type != 0);
2e276125 16180}
8e19ed76 16181
cf2c3c16
TT
16182static void
16183dwarf_decode_macros (struct line_header *lh, unsigned int offset,
16184 char *comp_dir, bfd *abfd,
16185 struct dwarf2_cu *cu,
16186 struct dwarf2_section_info *section,
fceca515 16187 int section_is_gnu, const char *section_name)
cf2c3c16 16188{
bb5ed363 16189 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
16190 gdb_byte *mac_ptr, *mac_end;
16191 struct macro_source_file *current_file = 0;
16192 enum dwarf_macro_record_type macinfo_type;
16193 unsigned int offset_size = cu->header.offset_size;
16194 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
16195 struct cleanup *cleanup;
16196 htab_t include_hash;
16197 void **slot;
cf2c3c16 16198
bb5ed363 16199 dwarf2_read_section (objfile, section);
cf2c3c16
TT
16200 if (section->buffer == NULL)
16201 {
fceca515 16202 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
16203 return;
16204 }
16205
16206 /* First pass: Find the name of the base filename.
16207 This filename is needed in order to process all macros whose definition
16208 (or undefinition) comes from the command line. These macros are defined
16209 before the first DW_MACINFO_start_file entry, and yet still need to be
16210 associated to the base file.
16211
16212 To determine the base file name, we scan the macro definitions until we
16213 reach the first DW_MACINFO_start_file entry. We then initialize
16214 CURRENT_FILE accordingly so that any macro definition found before the
16215 first DW_MACINFO_start_file can still be associated to the base file. */
16216
16217 mac_ptr = section->buffer + offset;
16218 mac_end = section->buffer + section->size;
16219
16220 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16221 &offset_size, section_is_gnu);
16222 if (mac_ptr == NULL)
16223 {
16224 /* We already issued a complaint. */
16225 return;
16226 }
16227
16228 do
16229 {
16230 /* Do we at least have room for a macinfo type byte? */
16231 if (mac_ptr >= mac_end)
16232 {
16233 /* Complaint is printed during the second pass as GDB will probably
16234 stop the first pass earlier upon finding
16235 DW_MACINFO_start_file. */
16236 break;
16237 }
16238
16239 macinfo_type = read_1_byte (abfd, mac_ptr);
16240 mac_ptr++;
16241
16242 /* Note that we rely on the fact that the corresponding GNU and
16243 DWARF constants are the same. */
16244 switch (macinfo_type)
16245 {
16246 /* A zero macinfo type indicates the end of the macro
16247 information. */
16248 case 0:
16249 break;
16250
16251 case DW_MACRO_GNU_define:
16252 case DW_MACRO_GNU_undef:
16253 /* Only skip the data by MAC_PTR. */
16254 {
16255 unsigned int bytes_read;
16256
16257 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16258 mac_ptr += bytes_read;
16259 read_direct_string (abfd, mac_ptr, &bytes_read);
16260 mac_ptr += bytes_read;
16261 }
16262 break;
16263
16264 case DW_MACRO_GNU_start_file:
16265 {
16266 unsigned int bytes_read;
16267 int line, file;
16268
16269 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16270 mac_ptr += bytes_read;
16271 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16272 mac_ptr += bytes_read;
16273
16274 current_file = macro_start_file (file, line, current_file,
bb5ed363 16275 comp_dir, lh, objfile);
cf2c3c16
TT
16276 }
16277 break;
16278
16279 case DW_MACRO_GNU_end_file:
16280 /* No data to skip by MAC_PTR. */
16281 break;
16282
16283 case DW_MACRO_GNU_define_indirect:
16284 case DW_MACRO_GNU_undef_indirect:
16285 {
16286 unsigned int bytes_read;
16287
16288 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16289 mac_ptr += bytes_read;
16290 mac_ptr += offset_size;
16291 }
16292 break;
16293
16294 case DW_MACRO_GNU_transparent_include:
16295 /* Note that, according to the spec, a transparent include
16296 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16297 skip this opcode. */
16298 mac_ptr += offset_size;
16299 break;
16300
16301 case DW_MACINFO_vendor_ext:
16302 /* Only skip the data by MAC_PTR. */
16303 if (!section_is_gnu)
16304 {
16305 unsigned int bytes_read;
16306
16307 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16308 mac_ptr += bytes_read;
16309 read_direct_string (abfd, mac_ptr, &bytes_read);
16310 mac_ptr += bytes_read;
16311 }
16312 /* FALLTHROUGH */
16313
16314 default:
16315 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16316 mac_ptr, abfd, offset_size,
16317 section);
16318 if (mac_ptr == NULL)
16319 return;
16320 break;
16321 }
16322 } while (macinfo_type != 0 && current_file == NULL);
16323
16324 /* Second pass: Process all entries.
16325
16326 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16327 command-line macro definitions/undefinitions. This flag is unset when we
16328 reach the first DW_MACINFO_start_file entry. */
16329
8fc3fc34
TT
16330 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16331 NULL, xcalloc, xfree);
16332 cleanup = make_cleanup_htab_delete (include_hash);
16333 mac_ptr = section->buffer + offset;
16334 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16335 *slot = mac_ptr;
16336 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
cf2c3c16 16337 current_file, lh, comp_dir, section, section_is_gnu,
8fc3fc34
TT
16338 offset_size, objfile, include_hash);
16339 do_cleanups (cleanup);
cf2c3c16
TT
16340}
16341
8e19ed76 16342/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 16343 if so return true else false. */
380bca97 16344
8e19ed76
PS
16345static int
16346attr_form_is_block (struct attribute *attr)
16347{
16348 return (attr == NULL ? 0 :
16349 attr->form == DW_FORM_block1
16350 || attr->form == DW_FORM_block2
16351 || attr->form == DW_FORM_block4
2dc7f7b3
TT
16352 || attr->form == DW_FORM_block
16353 || attr->form == DW_FORM_exprloc);
8e19ed76 16354}
4c2df51b 16355
c6a0999f
JB
16356/* Return non-zero if ATTR's value is a section offset --- classes
16357 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16358 You may use DW_UNSND (attr) to retrieve such offsets.
16359
16360 Section 7.5.4, "Attribute Encodings", explains that no attribute
16361 may have a value that belongs to more than one of these classes; it
16362 would be ambiguous if we did, because we use the same forms for all
16363 of them. */
380bca97 16364
3690dd37
JB
16365static int
16366attr_form_is_section_offset (struct attribute *attr)
16367{
16368 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
16369 || attr->form == DW_FORM_data8
16370 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
16371}
16372
3690dd37
JB
16373/* Return non-zero if ATTR's value falls in the 'constant' class, or
16374 zero otherwise. When this function returns true, you can apply
16375 dwarf2_get_attr_constant_value to it.
16376
16377 However, note that for some attributes you must check
16378 attr_form_is_section_offset before using this test. DW_FORM_data4
16379 and DW_FORM_data8 are members of both the constant class, and of
16380 the classes that contain offsets into other debug sections
16381 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16382 that, if an attribute's can be either a constant or one of the
16383 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16384 taken as section offsets, not constants. */
380bca97 16385
3690dd37
JB
16386static int
16387attr_form_is_constant (struct attribute *attr)
16388{
16389 switch (attr->form)
16390 {
16391 case DW_FORM_sdata:
16392 case DW_FORM_udata:
16393 case DW_FORM_data1:
16394 case DW_FORM_data2:
16395 case DW_FORM_data4:
16396 case DW_FORM_data8:
16397 return 1;
16398 default:
16399 return 0;
16400 }
16401}
16402
3019eac3
DE
16403/* Return the .debug_loc section to use for CU.
16404 For DWO files use .debug_loc.dwo. */
16405
16406static struct dwarf2_section_info *
16407cu_debug_loc_section (struct dwarf2_cu *cu)
16408{
16409 if (cu->dwo_unit)
16410 return &cu->dwo_unit->dwo_file->sections.loc;
16411 return &dwarf2_per_objfile->loc;
16412}
16413
8cf6f0b1
TT
16414/* A helper function that fills in a dwarf2_loclist_baton. */
16415
16416static void
16417fill_in_loclist_baton (struct dwarf2_cu *cu,
16418 struct dwarf2_loclist_baton *baton,
16419 struct attribute *attr)
16420{
3019eac3
DE
16421 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16422
16423 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
16424
16425 baton->per_cu = cu->per_cu;
16426 gdb_assert (baton->per_cu);
16427 /* We don't know how long the location list is, but make sure we
16428 don't run off the edge of the section. */
3019eac3
DE
16429 baton->size = section->size - DW_UNSND (attr);
16430 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1
TT
16431 baton->base_address = cu->base_address;
16432}
16433
4c2df51b
DJ
16434static void
16435dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 16436 struct dwarf2_cu *cu)
4c2df51b 16437{
bb5ed363 16438 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 16439 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 16440
3690dd37 16441 if (attr_form_is_section_offset (attr)
3019eac3 16442 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
16443 the section. If so, fall through to the complaint in the
16444 other branch. */
3019eac3 16445 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 16446 {
0d53c4c4 16447 struct dwarf2_loclist_baton *baton;
4c2df51b 16448
bb5ed363 16449 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16450 sizeof (struct dwarf2_loclist_baton));
4c2df51b 16451
8cf6f0b1 16452 fill_in_loclist_baton (cu, baton, attr);
be391dca 16453
d00adf39 16454 if (cu->base_known == 0)
0d53c4c4 16455 complaint (&symfile_complaints,
3e43a32a
MS
16456 _("Location list used without "
16457 "specifying the CU base address."));
4c2df51b 16458
768a979c 16459 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
16460 SYMBOL_LOCATION_BATON (sym) = baton;
16461 }
16462 else
16463 {
16464 struct dwarf2_locexpr_baton *baton;
16465
bb5ed363 16466 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16467 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
16468 baton->per_cu = cu->per_cu;
16469 gdb_assert (baton->per_cu);
0d53c4c4
DJ
16470
16471 if (attr_form_is_block (attr))
16472 {
16473 /* Note that we're just copying the block's data pointer
16474 here, not the actual data. We're still pointing into the
6502dd73
DJ
16475 info_buffer for SYM's objfile; right now we never release
16476 that buffer, but when we do clean up properly this may
16477 need to change. */
0d53c4c4
DJ
16478 baton->size = DW_BLOCK (attr)->size;
16479 baton->data = DW_BLOCK (attr)->data;
16480 }
16481 else
16482 {
16483 dwarf2_invalid_attrib_class_complaint ("location description",
16484 SYMBOL_NATURAL_NAME (sym));
16485 baton->size = 0;
0d53c4c4 16486 }
6e70227d 16487
768a979c 16488 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
16489 SYMBOL_LOCATION_BATON (sym) = baton;
16490 }
4c2df51b 16491}
6502dd73 16492
9aa1f1e3
TT
16493/* Return the OBJFILE associated with the compilation unit CU. If CU
16494 came from a separate debuginfo file, then the master objfile is
16495 returned. */
ae0d2f24
UW
16496
16497struct objfile *
16498dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16499{
9291a0cd 16500 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
16501
16502 /* Return the master objfile, so that we can report and look up the
16503 correct file containing this variable. */
16504 if (objfile->separate_debug_objfile_backlink)
16505 objfile = objfile->separate_debug_objfile_backlink;
16506
16507 return objfile;
16508}
16509
96408a79
SA
16510/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16511 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16512 CU_HEADERP first. */
16513
16514static const struct comp_unit_head *
16515per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16516 struct dwarf2_per_cu_data *per_cu)
16517{
16518 struct objfile *objfile;
16519 struct dwarf2_per_objfile *per_objfile;
16520 gdb_byte *info_ptr;
16521
16522 if (per_cu->cu)
16523 return &per_cu->cu->header;
16524
16525 objfile = per_cu->objfile;
16526 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
b64f50a1 16527 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
96408a79
SA
16528
16529 memset (cu_headerp, 0, sizeof (*cu_headerp));
16530 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16531
16532 return cu_headerp;
16533}
16534
ae0d2f24
UW
16535/* Return the address size given in the compilation unit header for CU. */
16536
98714339 16537int
ae0d2f24
UW
16538dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16539{
96408a79
SA
16540 struct comp_unit_head cu_header_local;
16541 const struct comp_unit_head *cu_headerp;
c471e790 16542
96408a79
SA
16543 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16544
16545 return cu_headerp->addr_size;
ae0d2f24
UW
16546}
16547
9eae7c52
TT
16548/* Return the offset size given in the compilation unit header for CU. */
16549
16550int
16551dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16552{
96408a79
SA
16553 struct comp_unit_head cu_header_local;
16554 const struct comp_unit_head *cu_headerp;
9c6c53f7 16555
96408a79
SA
16556 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16557
16558 return cu_headerp->offset_size;
16559}
16560
16561/* See its dwarf2loc.h declaration. */
16562
16563int
16564dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16565{
16566 struct comp_unit_head cu_header_local;
16567 const struct comp_unit_head *cu_headerp;
16568
16569 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16570
16571 if (cu_headerp->version == 2)
16572 return cu_headerp->addr_size;
16573 else
16574 return cu_headerp->offset_size;
181cebd4
JK
16575}
16576
9aa1f1e3
TT
16577/* Return the text offset of the CU. The returned offset comes from
16578 this CU's objfile. If this objfile came from a separate debuginfo
16579 file, then the offset may be different from the corresponding
16580 offset in the parent objfile. */
16581
16582CORE_ADDR
16583dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16584{
bb3fa9d0 16585 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
16586
16587 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16588}
16589
348e048f
DE
16590/* Locate the .debug_info compilation unit from CU's objfile which contains
16591 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
16592
16593static struct dwarf2_per_cu_data *
b64f50a1 16594dwarf2_find_containing_comp_unit (sect_offset offset,
ae038cb0
DJ
16595 struct objfile *objfile)
16596{
16597 struct dwarf2_per_cu_data *this_cu;
16598 int low, high;
16599
ae038cb0
DJ
16600 low = 0;
16601 high = dwarf2_per_objfile->n_comp_units - 1;
16602 while (high > low)
16603 {
16604 int mid = low + (high - low) / 2;
9a619af0 16605
b64f50a1
JK
16606 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16607 >= offset.sect_off)
ae038cb0
DJ
16608 high = mid;
16609 else
16610 low = mid + 1;
16611 }
16612 gdb_assert (low == high);
b64f50a1
JK
16613 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16614 > offset.sect_off)
ae038cb0 16615 {
10b3939b 16616 if (low == 0)
8a3fe4f8
AC
16617 error (_("Dwarf Error: could not find partial DIE containing "
16618 "offset 0x%lx [in module %s]"),
b64f50a1 16619 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 16620
b64f50a1
JK
16621 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16622 <= offset.sect_off);
ae038cb0
DJ
16623 return dwarf2_per_objfile->all_comp_units[low-1];
16624 }
16625 else
16626 {
16627 this_cu = dwarf2_per_objfile->all_comp_units[low];
16628 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
16629 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16630 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16631 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
16632 return this_cu;
16633 }
16634}
16635
23745b47 16636/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 16637
9816fde3 16638static void
23745b47 16639init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 16640{
9816fde3 16641 memset (cu, 0, sizeof (*cu));
23745b47
DE
16642 per_cu->cu = cu;
16643 cu->per_cu = per_cu;
16644 cu->objfile = per_cu->objfile;
93311388 16645 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
16646}
16647
16648/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16649
16650static void
95554aad
TT
16651prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
16652 enum language pretend_language)
9816fde3
JK
16653{
16654 struct attribute *attr;
16655
16656 /* Set the language we're debugging. */
16657 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16658 if (attr)
16659 set_cu_language (DW_UNSND (attr), cu);
16660 else
9cded63f 16661 {
95554aad 16662 cu->language = pretend_language;
9cded63f
TT
16663 cu->language_defn = language_def (cu->language);
16664 }
dee91e82
DE
16665
16666 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
16667 if (attr)
16668 cu->producer = DW_STRING (attr);
93311388
DE
16669}
16670
ae038cb0
DJ
16671/* Release one cached compilation unit, CU. We unlink it from the tree
16672 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
16673 the caller is responsible for that.
16674 NOTE: DATA is a void * because this function is also used as a
16675 cleanup routine. */
ae038cb0
DJ
16676
16677static void
68dc6402 16678free_heap_comp_unit (void *data)
ae038cb0
DJ
16679{
16680 struct dwarf2_cu *cu = data;
16681
23745b47
DE
16682 gdb_assert (cu->per_cu != NULL);
16683 cu->per_cu->cu = NULL;
ae038cb0
DJ
16684 cu->per_cu = NULL;
16685
16686 obstack_free (&cu->comp_unit_obstack, NULL);
16687
16688 xfree (cu);
16689}
16690
72bf9492 16691/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 16692 when we're finished with it. We can't free the pointer itself, but be
dee91e82 16693 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
16694
16695static void
16696free_stack_comp_unit (void *data)
16697{
16698 struct dwarf2_cu *cu = data;
16699
23745b47
DE
16700 gdb_assert (cu->per_cu != NULL);
16701 cu->per_cu->cu = NULL;
16702 cu->per_cu = NULL;
16703
72bf9492
DJ
16704 obstack_free (&cu->comp_unit_obstack, NULL);
16705 cu->partial_dies = NULL;
ae038cb0
DJ
16706}
16707
16708/* Free all cached compilation units. */
16709
16710static void
16711free_cached_comp_units (void *data)
16712{
16713 struct dwarf2_per_cu_data *per_cu, **last_chain;
16714
16715 per_cu = dwarf2_per_objfile->read_in_chain;
16716 last_chain = &dwarf2_per_objfile->read_in_chain;
16717 while (per_cu != NULL)
16718 {
16719 struct dwarf2_per_cu_data *next_cu;
16720
16721 next_cu = per_cu->cu->read_in_chain;
16722
68dc6402 16723 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16724 *last_chain = next_cu;
16725
16726 per_cu = next_cu;
16727 }
16728}
16729
16730/* Increase the age counter on each cached compilation unit, and free
16731 any that are too old. */
16732
16733static void
16734age_cached_comp_units (void)
16735{
16736 struct dwarf2_per_cu_data *per_cu, **last_chain;
16737
16738 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16739 per_cu = dwarf2_per_objfile->read_in_chain;
16740 while (per_cu != NULL)
16741 {
16742 per_cu->cu->last_used ++;
16743 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16744 dwarf2_mark (per_cu->cu);
16745 per_cu = per_cu->cu->read_in_chain;
16746 }
16747
16748 per_cu = dwarf2_per_objfile->read_in_chain;
16749 last_chain = &dwarf2_per_objfile->read_in_chain;
16750 while (per_cu != NULL)
16751 {
16752 struct dwarf2_per_cu_data *next_cu;
16753
16754 next_cu = per_cu->cu->read_in_chain;
16755
16756 if (!per_cu->cu->mark)
16757 {
68dc6402 16758 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16759 *last_chain = next_cu;
16760 }
16761 else
16762 last_chain = &per_cu->cu->read_in_chain;
16763
16764 per_cu = next_cu;
16765 }
16766}
16767
16768/* Remove a single compilation unit from the cache. */
16769
16770static void
dee91e82 16771free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
16772{
16773 struct dwarf2_per_cu_data *per_cu, **last_chain;
16774
16775 per_cu = dwarf2_per_objfile->read_in_chain;
16776 last_chain = &dwarf2_per_objfile->read_in_chain;
16777 while (per_cu != NULL)
16778 {
16779 struct dwarf2_per_cu_data *next_cu;
16780
16781 next_cu = per_cu->cu->read_in_chain;
16782
dee91e82 16783 if (per_cu == target_per_cu)
ae038cb0 16784 {
68dc6402 16785 free_heap_comp_unit (per_cu->cu);
dee91e82 16786 per_cu->cu = NULL;
ae038cb0
DJ
16787 *last_chain = next_cu;
16788 break;
16789 }
16790 else
16791 last_chain = &per_cu->cu->read_in_chain;
16792
16793 per_cu = next_cu;
16794 }
16795}
16796
fe3e1990
DJ
16797/* Release all extra memory associated with OBJFILE. */
16798
16799void
16800dwarf2_free_objfile (struct objfile *objfile)
16801{
16802 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16803
16804 if (dwarf2_per_objfile == NULL)
16805 return;
16806
16807 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16808 free_cached_comp_units (NULL);
16809
7b9f3c50
DE
16810 if (dwarf2_per_objfile->quick_file_names_table)
16811 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16812
fe3e1990
DJ
16813 /* Everything else should be on the objfile obstack. */
16814}
16815
dee91e82
DE
16816/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
16817 We store these in a hash table separate from the DIEs, and preserve them
16818 when the DIEs are flushed out of cache.
16819
16820 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
16821 uniquely identify the type. A file may have multiple .debug_types sections,
16822 or the type may come from a DWO file. We have to use something in
16823 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
16824 routine, get_die_type_at_offset, from outside this file, and thus won't
16825 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
16826 of the objfile. */
1c379e20 16827
dee91e82 16828struct dwarf2_per_cu_offset_and_type
1c379e20 16829{
dee91e82 16830 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 16831 sect_offset offset;
1c379e20
DJ
16832 struct type *type;
16833};
16834
dee91e82 16835/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
16836
16837static hashval_t
dee91e82 16838per_cu_offset_and_type_hash (const void *item)
1c379e20 16839{
dee91e82 16840 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 16841
dee91e82 16842 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
16843}
16844
dee91e82 16845/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
16846
16847static int
dee91e82 16848per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 16849{
dee91e82
DE
16850 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
16851 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16852
dee91e82
DE
16853 return (ofs_lhs->per_cu == ofs_rhs->per_cu
16854 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
16855}
16856
16857/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16858 table if necessary. For convenience, return TYPE.
16859
16860 The DIEs reading must have careful ordering to:
16861 * Not cause infite loops trying to read in DIEs as a prerequisite for
16862 reading current DIE.
16863 * Not trying to dereference contents of still incompletely read in types
16864 while reading in other DIEs.
16865 * Enable referencing still incompletely read in types just by a pointer to
16866 the type without accessing its fields.
16867
16868 Therefore caller should follow these rules:
16869 * Try to fetch any prerequisite types we may need to build this DIE type
16870 before building the type and calling set_die_type.
e71ec853 16871 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16872 possible before fetching more types to complete the current type.
16873 * Make the type as complete as possible before fetching more types. */
1c379e20 16874
f792889a 16875static struct type *
1c379e20
DJ
16876set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16877{
dee91e82 16878 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 16879 struct objfile *objfile = cu->objfile;
1c379e20 16880
b4ba55a1
JB
16881 /* For Ada types, make sure that the gnat-specific data is always
16882 initialized (if not already set). There are a few types where
16883 we should not be doing so, because the type-specific area is
16884 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16885 where the type-specific area is used to store the floatformat).
16886 But this is not a problem, because the gnat-specific information
16887 is actually not needed for these types. */
16888 if (need_gnat_info (cu)
16889 && TYPE_CODE (type) != TYPE_CODE_FUNC
16890 && TYPE_CODE (type) != TYPE_CODE_FLT
16891 && !HAVE_GNAT_AUX_INFO (type))
16892 INIT_GNAT_SPECIFIC (type);
16893
dee91e82 16894 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 16895 {
dee91e82
DE
16896 dwarf2_per_objfile->die_type_hash =
16897 htab_create_alloc_ex (127,
16898 per_cu_offset_and_type_hash,
16899 per_cu_offset_and_type_eq,
16900 NULL,
16901 &objfile->objfile_obstack,
16902 hashtab_obstack_allocate,
16903 dummy_obstack_deallocate);
f792889a 16904 }
1c379e20 16905
dee91e82 16906 ofs.per_cu = cu->per_cu;
1c379e20
DJ
16907 ofs.offset = die->offset;
16908 ofs.type = type;
dee91e82
DE
16909 slot = (struct dwarf2_per_cu_offset_and_type **)
16910 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
16911 if (*slot)
16912 complaint (&symfile_complaints,
16913 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 16914 die->offset.sect_off);
673bfd45 16915 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16916 **slot = ofs;
f792889a 16917 return type;
1c379e20
DJ
16918}
16919
380bca97 16920/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 16921 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16922
16923static struct type *
b64f50a1 16924get_die_type_at_offset (sect_offset offset,
673bfd45 16925 struct dwarf2_per_cu_data *per_cu)
1c379e20 16926{
dee91e82 16927 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 16928
dee91e82 16929 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 16930 return NULL;
1c379e20 16931
dee91e82 16932 ofs.per_cu = per_cu;
673bfd45 16933 ofs.offset = offset;
dee91e82 16934 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
16935 if (slot)
16936 return slot->type;
16937 else
16938 return NULL;
16939}
16940
673bfd45
DE
16941/* Look up the type for DIE in the appropriate type_hash table,
16942 or return NULL if DIE does not have a saved type. */
16943
16944static struct type *
16945get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16946{
16947 return get_die_type_at_offset (die->offset, cu->per_cu);
16948}
16949
10b3939b
DJ
16950/* Add a dependence relationship from CU to REF_PER_CU. */
16951
16952static void
16953dwarf2_add_dependence (struct dwarf2_cu *cu,
16954 struct dwarf2_per_cu_data *ref_per_cu)
16955{
16956 void **slot;
16957
16958 if (cu->dependencies == NULL)
16959 cu->dependencies
16960 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16961 NULL, &cu->comp_unit_obstack,
16962 hashtab_obstack_allocate,
16963 dummy_obstack_deallocate);
16964
16965 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16966 if (*slot == NULL)
16967 *slot = ref_per_cu;
16968}
1c379e20 16969
f504f079
DE
16970/* Subroutine of dwarf2_mark to pass to htab_traverse.
16971 Set the mark field in every compilation unit in the
ae038cb0
DJ
16972 cache that we must keep because we are keeping CU. */
16973
10b3939b
DJ
16974static int
16975dwarf2_mark_helper (void **slot, void *data)
16976{
16977 struct dwarf2_per_cu_data *per_cu;
16978
16979 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16980
16981 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16982 reading of the chain. As such dependencies remain valid it is not much
16983 useful to track and undo them during QUIT cleanups. */
16984 if (per_cu->cu == NULL)
16985 return 1;
16986
10b3939b
DJ
16987 if (per_cu->cu->mark)
16988 return 1;
16989 per_cu->cu->mark = 1;
16990
16991 if (per_cu->cu->dependencies != NULL)
16992 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16993
16994 return 1;
16995}
16996
f504f079
DE
16997/* Set the mark field in CU and in every other compilation unit in the
16998 cache that we must keep because we are keeping CU. */
16999
ae038cb0
DJ
17000static void
17001dwarf2_mark (struct dwarf2_cu *cu)
17002{
17003 if (cu->mark)
17004 return;
17005 cu->mark = 1;
10b3939b
DJ
17006 if (cu->dependencies != NULL)
17007 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
17008}
17009
17010static void
17011dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17012{
17013 while (per_cu)
17014 {
17015 per_cu->cu->mark = 0;
17016 per_cu = per_cu->cu->read_in_chain;
17017 }
72bf9492
DJ
17018}
17019
72bf9492
DJ
17020/* Trivial hash function for partial_die_info: the hash value of a DIE
17021 is its offset in .debug_info for this objfile. */
17022
17023static hashval_t
17024partial_die_hash (const void *item)
17025{
17026 const struct partial_die_info *part_die = item;
9a619af0 17027
b64f50a1 17028 return part_die->offset.sect_off;
72bf9492
DJ
17029}
17030
17031/* Trivial comparison function for partial_die_info structures: two DIEs
17032 are equal if they have the same offset. */
17033
17034static int
17035partial_die_eq (const void *item_lhs, const void *item_rhs)
17036{
17037 const struct partial_die_info *part_die_lhs = item_lhs;
17038 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 17039
b64f50a1 17040 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
17041}
17042
ae038cb0
DJ
17043static struct cmd_list_element *set_dwarf2_cmdlist;
17044static struct cmd_list_element *show_dwarf2_cmdlist;
17045
17046static void
17047set_dwarf2_cmd (char *args, int from_tty)
17048{
17049 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17050}
17051
17052static void
17053show_dwarf2_cmd (char *args, int from_tty)
6e70227d 17054{
ae038cb0
DJ
17055 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17056}
17057
dce234bc
PP
17058/* If section described by INFO was mmapped, munmap it now. */
17059
17060static void
17061munmap_section_buffer (struct dwarf2_section_info *info)
17062{
b315ab21 17063 if (info->map_addr != NULL)
dce234bc
PP
17064 {
17065#ifdef HAVE_MMAP
b315ab21 17066 int res;
9a619af0 17067
b315ab21
TG
17068 res = munmap (info->map_addr, info->map_len);
17069 gdb_assert (res == 0);
dce234bc
PP
17070#else
17071 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 17072 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
17073#endif
17074 }
17075}
17076
17077/* munmap debug sections for OBJFILE, if necessary. */
17078
17079static void
c1bd65d0 17080dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
17081{
17082 struct dwarf2_per_objfile *data = d;
8b70b953
TT
17083 int ix;
17084 struct dwarf2_section_info *section;
9a619af0 17085
16be1145
DE
17086 /* This is sorted according to the order they're defined in to make it easier
17087 to keep in sync. */
dce234bc
PP
17088 munmap_section_buffer (&data->info);
17089 munmap_section_buffer (&data->abbrev);
17090 munmap_section_buffer (&data->line);
16be1145 17091 munmap_section_buffer (&data->loc);
dce234bc 17092 munmap_section_buffer (&data->macinfo);
cf2c3c16 17093 munmap_section_buffer (&data->macro);
16be1145 17094 munmap_section_buffer (&data->str);
dce234bc 17095 munmap_section_buffer (&data->ranges);
3019eac3 17096 munmap_section_buffer (&data->addr);
dce234bc
PP
17097 munmap_section_buffer (&data->frame);
17098 munmap_section_buffer (&data->eh_frame);
9291a0cd 17099 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
17100
17101 for (ix = 0;
17102 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17103 ++ix)
17104 munmap_section_buffer (section);
17105
95554aad
TT
17106 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17107 VEC_free (dwarf2_per_cu_ptr,
17108 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17109
8b70b953 17110 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
17111
17112 if (data->dwo_files)
17113 free_dwo_files (data->dwo_files, objfile);
9291a0cd
TT
17114}
17115
17116\f
ae2de4f8 17117/* The "save gdb-index" command. */
9291a0cd
TT
17118
17119/* The contents of the hash table we create when building the string
17120 table. */
17121struct strtab_entry
17122{
17123 offset_type offset;
17124 const char *str;
17125};
17126
559a7a62
JK
17127/* Hash function for a strtab_entry.
17128
17129 Function is used only during write_hash_table so no index format backward
17130 compatibility is needed. */
b89be57b 17131
9291a0cd
TT
17132static hashval_t
17133hash_strtab_entry (const void *e)
17134{
17135 const struct strtab_entry *entry = e;
559a7a62 17136 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
17137}
17138
17139/* Equality function for a strtab_entry. */
b89be57b 17140
9291a0cd
TT
17141static int
17142eq_strtab_entry (const void *a, const void *b)
17143{
17144 const struct strtab_entry *ea = a;
17145 const struct strtab_entry *eb = b;
17146 return !strcmp (ea->str, eb->str);
17147}
17148
17149/* Create a strtab_entry hash table. */
b89be57b 17150
9291a0cd
TT
17151static htab_t
17152create_strtab (void)
17153{
17154 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17155 xfree, xcalloc, xfree);
17156}
17157
17158/* Add a string to the constant pool. Return the string's offset in
17159 host order. */
b89be57b 17160
9291a0cd
TT
17161static offset_type
17162add_string (htab_t table, struct obstack *cpool, const char *str)
17163{
17164 void **slot;
17165 struct strtab_entry entry;
17166 struct strtab_entry *result;
17167
17168 entry.str = str;
17169 slot = htab_find_slot (table, &entry, INSERT);
17170 if (*slot)
17171 result = *slot;
17172 else
17173 {
17174 result = XNEW (struct strtab_entry);
17175 result->offset = obstack_object_size (cpool);
17176 result->str = str;
17177 obstack_grow_str0 (cpool, str);
17178 *slot = result;
17179 }
17180 return result->offset;
17181}
17182
17183/* An entry in the symbol table. */
17184struct symtab_index_entry
17185{
17186 /* The name of the symbol. */
17187 const char *name;
17188 /* The offset of the name in the constant pool. */
17189 offset_type index_offset;
17190 /* A sorted vector of the indices of all the CUs that hold an object
17191 of this name. */
17192 VEC (offset_type) *cu_indices;
17193};
17194
17195/* The symbol table. This is a power-of-2-sized hash table. */
17196struct mapped_symtab
17197{
17198 offset_type n_elements;
17199 offset_type size;
17200 struct symtab_index_entry **data;
17201};
17202
17203/* Hash function for a symtab_index_entry. */
b89be57b 17204
9291a0cd
TT
17205static hashval_t
17206hash_symtab_entry (const void *e)
17207{
17208 const struct symtab_index_entry *entry = e;
17209 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17210 sizeof (offset_type) * VEC_length (offset_type,
17211 entry->cu_indices),
17212 0);
17213}
17214
17215/* Equality function for a symtab_index_entry. */
b89be57b 17216
9291a0cd
TT
17217static int
17218eq_symtab_entry (const void *a, const void *b)
17219{
17220 const struct symtab_index_entry *ea = a;
17221 const struct symtab_index_entry *eb = b;
17222 int len = VEC_length (offset_type, ea->cu_indices);
17223 if (len != VEC_length (offset_type, eb->cu_indices))
17224 return 0;
17225 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17226 VEC_address (offset_type, eb->cu_indices),
17227 sizeof (offset_type) * len);
17228}
17229
17230/* Destroy a symtab_index_entry. */
b89be57b 17231
9291a0cd
TT
17232static void
17233delete_symtab_entry (void *p)
17234{
17235 struct symtab_index_entry *entry = p;
17236 VEC_free (offset_type, entry->cu_indices);
17237 xfree (entry);
17238}
17239
17240/* Create a hash table holding symtab_index_entry objects. */
b89be57b 17241
9291a0cd 17242static htab_t
3876f04e 17243create_symbol_hash_table (void)
9291a0cd
TT
17244{
17245 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17246 delete_symtab_entry, xcalloc, xfree);
17247}
17248
17249/* Create a new mapped symtab object. */
b89be57b 17250
9291a0cd
TT
17251static struct mapped_symtab *
17252create_mapped_symtab (void)
17253{
17254 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17255 symtab->n_elements = 0;
17256 symtab->size = 1024;
17257 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17258 return symtab;
17259}
17260
17261/* Destroy a mapped_symtab. */
b89be57b 17262
9291a0cd
TT
17263static void
17264cleanup_mapped_symtab (void *p)
17265{
17266 struct mapped_symtab *symtab = p;
17267 /* The contents of the array are freed when the other hash table is
17268 destroyed. */
17269 xfree (symtab->data);
17270 xfree (symtab);
17271}
17272
17273/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
17274 the slot.
17275
17276 Function is used only during write_hash_table so no index format backward
17277 compatibility is needed. */
b89be57b 17278
9291a0cd
TT
17279static struct symtab_index_entry **
17280find_slot (struct mapped_symtab *symtab, const char *name)
17281{
559a7a62 17282 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
17283
17284 index = hash & (symtab->size - 1);
17285 step = ((hash * 17) & (symtab->size - 1)) | 1;
17286
17287 for (;;)
17288 {
17289 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17290 return &symtab->data[index];
17291 index = (index + step) & (symtab->size - 1);
17292 }
17293}
17294
17295/* Expand SYMTAB's hash table. */
b89be57b 17296
9291a0cd
TT
17297static void
17298hash_expand (struct mapped_symtab *symtab)
17299{
17300 offset_type old_size = symtab->size;
17301 offset_type i;
17302 struct symtab_index_entry **old_entries = symtab->data;
17303
17304 symtab->size *= 2;
17305 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17306
17307 for (i = 0; i < old_size; ++i)
17308 {
17309 if (old_entries[i])
17310 {
17311 struct symtab_index_entry **slot = find_slot (symtab,
17312 old_entries[i]->name);
17313 *slot = old_entries[i];
17314 }
17315 }
17316
17317 xfree (old_entries);
17318}
17319
17320/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
17321 is the index of the CU in which the symbol appears. */
b89be57b 17322
9291a0cd
TT
17323static void
17324add_index_entry (struct mapped_symtab *symtab, const char *name,
17325 offset_type cu_index)
17326{
17327 struct symtab_index_entry **slot;
17328
17329 ++symtab->n_elements;
17330 if (4 * symtab->n_elements / 3 >= symtab->size)
17331 hash_expand (symtab);
17332
17333 slot = find_slot (symtab, name);
17334 if (!*slot)
17335 {
17336 *slot = XNEW (struct symtab_index_entry);
17337 (*slot)->name = name;
17338 (*slot)->cu_indices = NULL;
17339 }
17340 /* Don't push an index twice. Due to how we add entries we only
17341 have to check the last one. */
17342 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 17343 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
17344 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
17345}
17346
17347/* Add a vector of indices to the constant pool. */
b89be57b 17348
9291a0cd 17349static offset_type
3876f04e 17350add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
17351 struct symtab_index_entry *entry)
17352{
17353 void **slot;
17354
3876f04e 17355 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
17356 if (!*slot)
17357 {
17358 offset_type len = VEC_length (offset_type, entry->cu_indices);
17359 offset_type val = MAYBE_SWAP (len);
17360 offset_type iter;
17361 int i;
17362
17363 *slot = entry;
17364 entry->index_offset = obstack_object_size (cpool);
17365
17366 obstack_grow (cpool, &val, sizeof (val));
17367 for (i = 0;
17368 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17369 ++i)
17370 {
17371 val = MAYBE_SWAP (iter);
17372 obstack_grow (cpool, &val, sizeof (val));
17373 }
17374 }
17375 else
17376 {
17377 struct symtab_index_entry *old_entry = *slot;
17378 entry->index_offset = old_entry->index_offset;
17379 entry = old_entry;
17380 }
17381 return entry->index_offset;
17382}
17383
17384/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17385 constant pool entries going into the obstack CPOOL. */
b89be57b 17386
9291a0cd
TT
17387static void
17388write_hash_table (struct mapped_symtab *symtab,
17389 struct obstack *output, struct obstack *cpool)
17390{
17391 offset_type i;
3876f04e 17392 htab_t symbol_hash_table;
9291a0cd
TT
17393 htab_t str_table;
17394
3876f04e 17395 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 17396 str_table = create_strtab ();
3876f04e 17397
9291a0cd
TT
17398 /* We add all the index vectors to the constant pool first, to
17399 ensure alignment is ok. */
17400 for (i = 0; i < symtab->size; ++i)
17401 {
17402 if (symtab->data[i])
3876f04e 17403 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
17404 }
17405
17406 /* Now write out the hash table. */
17407 for (i = 0; i < symtab->size; ++i)
17408 {
17409 offset_type str_off, vec_off;
17410
17411 if (symtab->data[i])
17412 {
17413 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17414 vec_off = symtab->data[i]->index_offset;
17415 }
17416 else
17417 {
17418 /* While 0 is a valid constant pool index, it is not valid
17419 to have 0 for both offsets. */
17420 str_off = 0;
17421 vec_off = 0;
17422 }
17423
17424 str_off = MAYBE_SWAP (str_off);
17425 vec_off = MAYBE_SWAP (vec_off);
17426
17427 obstack_grow (output, &str_off, sizeof (str_off));
17428 obstack_grow (output, &vec_off, sizeof (vec_off));
17429 }
17430
17431 htab_delete (str_table);
3876f04e 17432 htab_delete (symbol_hash_table);
9291a0cd
TT
17433}
17434
0a5429f6
DE
17435/* Struct to map psymtab to CU index in the index file. */
17436struct psymtab_cu_index_map
17437{
17438 struct partial_symtab *psymtab;
17439 unsigned int cu_index;
17440};
17441
17442static hashval_t
17443hash_psymtab_cu_index (const void *item)
17444{
17445 const struct psymtab_cu_index_map *map = item;
17446
17447 return htab_hash_pointer (map->psymtab);
17448}
17449
17450static int
17451eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17452{
17453 const struct psymtab_cu_index_map *lhs = item_lhs;
17454 const struct psymtab_cu_index_map *rhs = item_rhs;
17455
17456 return lhs->psymtab == rhs->psymtab;
17457}
17458
17459/* Helper struct for building the address table. */
17460struct addrmap_index_data
17461{
17462 struct objfile *objfile;
17463 struct obstack *addr_obstack;
17464 htab_t cu_index_htab;
17465
17466 /* Non-zero if the previous_* fields are valid.
17467 We can't write an entry until we see the next entry (since it is only then
17468 that we know the end of the entry). */
17469 int previous_valid;
17470 /* Index of the CU in the table of all CUs in the index file. */
17471 unsigned int previous_cu_index;
0963b4bd 17472 /* Start address of the CU. */
0a5429f6
DE
17473 CORE_ADDR previous_cu_start;
17474};
17475
17476/* Write an address entry to OBSTACK. */
b89be57b 17477
9291a0cd 17478static void
0a5429f6
DE
17479add_address_entry (struct objfile *objfile, struct obstack *obstack,
17480 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 17481{
0a5429f6 17482 offset_type cu_index_to_write;
9291a0cd
TT
17483 char addr[8];
17484 CORE_ADDR baseaddr;
17485
17486 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17487
0a5429f6
DE
17488 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17489 obstack_grow (obstack, addr, 8);
17490 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17491 obstack_grow (obstack, addr, 8);
17492 cu_index_to_write = MAYBE_SWAP (cu_index);
17493 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17494}
17495
17496/* Worker function for traversing an addrmap to build the address table. */
17497
17498static int
17499add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17500{
17501 struct addrmap_index_data *data = datap;
17502 struct partial_symtab *pst = obj;
17503 offset_type cu_index;
17504 void **slot;
17505
17506 if (data->previous_valid)
17507 add_address_entry (data->objfile, data->addr_obstack,
17508 data->previous_cu_start, start_addr,
17509 data->previous_cu_index);
17510
17511 data->previous_cu_start = start_addr;
17512 if (pst != NULL)
17513 {
17514 struct psymtab_cu_index_map find_map, *map;
17515 find_map.psymtab = pst;
17516 map = htab_find (data->cu_index_htab, &find_map);
17517 gdb_assert (map != NULL);
17518 data->previous_cu_index = map->cu_index;
17519 data->previous_valid = 1;
17520 }
17521 else
17522 data->previous_valid = 0;
17523
17524 return 0;
17525}
17526
17527/* Write OBJFILE's address map to OBSTACK.
17528 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17529 in the index file. */
17530
17531static void
17532write_address_map (struct objfile *objfile, struct obstack *obstack,
17533 htab_t cu_index_htab)
17534{
17535 struct addrmap_index_data addrmap_index_data;
17536
17537 /* When writing the address table, we have to cope with the fact that
17538 the addrmap iterator only provides the start of a region; we have to
17539 wait until the next invocation to get the start of the next region. */
17540
17541 addrmap_index_data.objfile = objfile;
17542 addrmap_index_data.addr_obstack = obstack;
17543 addrmap_index_data.cu_index_htab = cu_index_htab;
17544 addrmap_index_data.previous_valid = 0;
17545
17546 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17547 &addrmap_index_data);
17548
17549 /* It's highly unlikely the last entry (end address = 0xff...ff)
17550 is valid, but we should still handle it.
17551 The end address is recorded as the start of the next region, but that
17552 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17553 anyway. */
17554 if (addrmap_index_data.previous_valid)
17555 add_address_entry (objfile, obstack,
17556 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17557 addrmap_index_data.previous_cu_index);
9291a0cd
TT
17558}
17559
17560/* Add a list of partial symbols to SYMTAB. */
b89be57b 17561
9291a0cd
TT
17562static void
17563write_psymbols (struct mapped_symtab *symtab,
987d643c 17564 htab_t psyms_seen,
9291a0cd
TT
17565 struct partial_symbol **psymp,
17566 int count,
987d643c
TT
17567 offset_type cu_index,
17568 int is_static)
9291a0cd
TT
17569{
17570 for (; count-- > 0; ++psymp)
17571 {
987d643c
TT
17572 void **slot, *lookup;
17573
9291a0cd
TT
17574 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
17575 error (_("Ada is not currently supported by the index"));
987d643c
TT
17576
17577 /* We only want to add a given psymbol once. However, we also
17578 want to account for whether it is global or static. So, we
17579 may add it twice, using slightly different values. */
17580 if (is_static)
17581 {
17582 uintptr_t val = 1 | (uintptr_t) *psymp;
17583
17584 lookup = (void *) val;
17585 }
17586 else
17587 lookup = *psymp;
17588
17589 /* Only add a given psymbol once. */
17590 slot = htab_find_slot (psyms_seen, lookup, INSERT);
17591 if (!*slot)
17592 {
17593 *slot = lookup;
bb2f58dc 17594 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 17595 }
9291a0cd
TT
17596 }
17597}
17598
17599/* Write the contents of an ("unfinished") obstack to FILE. Throw an
17600 exception if there is an error. */
b89be57b 17601
9291a0cd
TT
17602static void
17603write_obstack (FILE *file, struct obstack *obstack)
17604{
17605 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17606 file)
17607 != obstack_object_size (obstack))
17608 error (_("couldn't data write to file"));
17609}
17610
17611/* Unlink a file if the argument is not NULL. */
b89be57b 17612
9291a0cd
TT
17613static void
17614unlink_if_set (void *p)
17615{
17616 char **filename = p;
17617 if (*filename)
17618 unlink (*filename);
17619}
17620
1fd400ff
TT
17621/* A helper struct used when iterating over debug_types. */
17622struct signatured_type_index_data
17623{
17624 struct objfile *objfile;
17625 struct mapped_symtab *symtab;
17626 struct obstack *types_list;
987d643c 17627 htab_t psyms_seen;
1fd400ff
TT
17628 int cu_index;
17629};
17630
17631/* A helper function that writes a single signatured_type to an
17632 obstack. */
b89be57b 17633
1fd400ff
TT
17634static int
17635write_one_signatured_type (void **slot, void *d)
17636{
17637 struct signatured_type_index_data *info = d;
17638 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
17639 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17640 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
17641 gdb_byte val[8];
17642
17643 write_psymbols (info->symtab,
987d643c 17644 info->psyms_seen,
3e43a32a
MS
17645 info->objfile->global_psymbols.list
17646 + psymtab->globals_offset,
987d643c
TT
17647 psymtab->n_global_syms, info->cu_index,
17648 0);
1fd400ff 17649 write_psymbols (info->symtab,
987d643c 17650 info->psyms_seen,
3e43a32a
MS
17651 info->objfile->static_psymbols.list
17652 + psymtab->statics_offset,
987d643c
TT
17653 psymtab->n_static_syms, info->cu_index,
17654 1);
1fd400ff 17655
b64f50a1
JK
17656 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17657 entry->per_cu.offset.sect_off);
1fd400ff 17658 obstack_grow (info->types_list, val, 8);
3019eac3
DE
17659 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17660 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
17661 obstack_grow (info->types_list, val, 8);
17662 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17663 obstack_grow (info->types_list, val, 8);
17664
17665 ++info->cu_index;
17666
17667 return 1;
17668}
17669
95554aad
TT
17670/* Recurse into all "included" dependencies and write their symbols as
17671 if they appeared in this psymtab. */
17672
17673static void
17674recursively_write_psymbols (struct objfile *objfile,
17675 struct partial_symtab *psymtab,
17676 struct mapped_symtab *symtab,
17677 htab_t psyms_seen,
17678 offset_type cu_index)
17679{
17680 int i;
17681
17682 for (i = 0; i < psymtab->number_of_dependencies; ++i)
17683 if (psymtab->dependencies[i]->user != NULL)
17684 recursively_write_psymbols (objfile, psymtab->dependencies[i],
17685 symtab, psyms_seen, cu_index);
17686
17687 write_psymbols (symtab,
17688 psyms_seen,
17689 objfile->global_psymbols.list + psymtab->globals_offset,
17690 psymtab->n_global_syms, cu_index,
17691 0);
17692 write_psymbols (symtab,
17693 psyms_seen,
17694 objfile->static_psymbols.list + psymtab->statics_offset,
17695 psymtab->n_static_syms, cu_index,
17696 1);
17697}
17698
9291a0cd 17699/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 17700
9291a0cd
TT
17701static void
17702write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17703{
17704 struct cleanup *cleanup;
17705 char *filename, *cleanup_filename;
1fd400ff
TT
17706 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
17707 struct obstack cu_list, types_cu_list;
9291a0cd
TT
17708 int i;
17709 FILE *out_file;
17710 struct mapped_symtab *symtab;
17711 offset_type val, size_of_contents, total_len;
17712 struct stat st;
17713 char buf[8];
987d643c 17714 htab_t psyms_seen;
0a5429f6
DE
17715 htab_t cu_index_htab;
17716 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 17717
b4f2f049 17718 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 17719 return;
b4f2f049 17720
9291a0cd
TT
17721 if (dwarf2_per_objfile->using_index)
17722 error (_("Cannot use an index to create the index"));
17723
8b70b953
TT
17724 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17725 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17726
9291a0cd 17727 if (stat (objfile->name, &st) < 0)
7e17e088 17728 perror_with_name (objfile->name);
9291a0cd
TT
17729
17730 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17731 INDEX_SUFFIX, (char *) NULL);
17732 cleanup = make_cleanup (xfree, filename);
17733
17734 out_file = fopen (filename, "wb");
17735 if (!out_file)
17736 error (_("Can't open `%s' for writing"), filename);
17737
17738 cleanup_filename = filename;
17739 make_cleanup (unlink_if_set, &cleanup_filename);
17740
17741 symtab = create_mapped_symtab ();
17742 make_cleanup (cleanup_mapped_symtab, symtab);
17743
17744 obstack_init (&addr_obstack);
17745 make_cleanup_obstack_free (&addr_obstack);
17746
17747 obstack_init (&cu_list);
17748 make_cleanup_obstack_free (&cu_list);
17749
1fd400ff
TT
17750 obstack_init (&types_cu_list);
17751 make_cleanup_obstack_free (&types_cu_list);
17752
987d643c
TT
17753 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17754 NULL, xcalloc, xfree);
96408a79 17755 make_cleanup_htab_delete (psyms_seen);
987d643c 17756
0a5429f6
DE
17757 /* While we're scanning CU's create a table that maps a psymtab pointer
17758 (which is what addrmap records) to its index (which is what is recorded
17759 in the index file). This will later be needed to write the address
17760 table. */
17761 cu_index_htab = htab_create_alloc (100,
17762 hash_psymtab_cu_index,
17763 eq_psymtab_cu_index,
17764 NULL, xcalloc, xfree);
96408a79 17765 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
17766 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17767 xmalloc (sizeof (struct psymtab_cu_index_map)
17768 * dwarf2_per_objfile->n_comp_units);
17769 make_cleanup (xfree, psymtab_cu_index_map);
17770
17771 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
17772 work here. Also, the debug_types entries do not appear in
17773 all_comp_units, but only in their own hash table. */
9291a0cd
TT
17774 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17775 {
3e43a32a
MS
17776 struct dwarf2_per_cu_data *per_cu
17777 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 17778 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 17779 gdb_byte val[8];
0a5429f6
DE
17780 struct psymtab_cu_index_map *map;
17781 void **slot;
9291a0cd 17782
95554aad
TT
17783 if (psymtab->user == NULL)
17784 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 17785
0a5429f6
DE
17786 map = &psymtab_cu_index_map[i];
17787 map->psymtab = psymtab;
17788 map->cu_index = i;
17789 slot = htab_find_slot (cu_index_htab, map, INSERT);
17790 gdb_assert (slot != NULL);
17791 gdb_assert (*slot == NULL);
17792 *slot = map;
9291a0cd 17793
b64f50a1
JK
17794 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17795 per_cu->offset.sect_off);
9291a0cd 17796 obstack_grow (&cu_list, val, 8);
e254ef6a 17797 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
17798 obstack_grow (&cu_list, val, 8);
17799 }
17800
0a5429f6
DE
17801 /* Dump the address map. */
17802 write_address_map (objfile, &addr_obstack, cu_index_htab);
17803
1fd400ff
TT
17804 /* Write out the .debug_type entries, if any. */
17805 if (dwarf2_per_objfile->signatured_types)
17806 {
17807 struct signatured_type_index_data sig_data;
17808
17809 sig_data.objfile = objfile;
17810 sig_data.symtab = symtab;
17811 sig_data.types_list = &types_cu_list;
987d643c 17812 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17813 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17814 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17815 write_one_signatured_type, &sig_data);
17816 }
17817
9291a0cd
TT
17818 obstack_init (&constant_pool);
17819 make_cleanup_obstack_free (&constant_pool);
17820 obstack_init (&symtab_obstack);
17821 make_cleanup_obstack_free (&symtab_obstack);
17822 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17823
17824 obstack_init (&contents);
17825 make_cleanup_obstack_free (&contents);
1fd400ff 17826 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17827 total_len = size_of_contents;
17828
17829 /* The version number. */
481860b3 17830 val = MAYBE_SWAP (6);
9291a0cd
TT
17831 obstack_grow (&contents, &val, sizeof (val));
17832
17833 /* The offset of the CU list from the start of the file. */
17834 val = MAYBE_SWAP (total_len);
17835 obstack_grow (&contents, &val, sizeof (val));
17836 total_len += obstack_object_size (&cu_list);
17837
1fd400ff
TT
17838 /* The offset of the types CU list from the start of the file. */
17839 val = MAYBE_SWAP (total_len);
17840 obstack_grow (&contents, &val, sizeof (val));
17841 total_len += obstack_object_size (&types_cu_list);
17842
9291a0cd
TT
17843 /* The offset of the address table from the start of the file. */
17844 val = MAYBE_SWAP (total_len);
17845 obstack_grow (&contents, &val, sizeof (val));
17846 total_len += obstack_object_size (&addr_obstack);
17847
17848 /* The offset of the symbol table from the start of the file. */
17849 val = MAYBE_SWAP (total_len);
17850 obstack_grow (&contents, &val, sizeof (val));
17851 total_len += obstack_object_size (&symtab_obstack);
17852
17853 /* The offset of the constant pool from the start of the file. */
17854 val = MAYBE_SWAP (total_len);
17855 obstack_grow (&contents, &val, sizeof (val));
17856 total_len += obstack_object_size (&constant_pool);
17857
17858 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17859
17860 write_obstack (out_file, &contents);
17861 write_obstack (out_file, &cu_list);
1fd400ff 17862 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17863 write_obstack (out_file, &addr_obstack);
17864 write_obstack (out_file, &symtab_obstack);
17865 write_obstack (out_file, &constant_pool);
17866
17867 fclose (out_file);
17868
17869 /* We want to keep the file, so we set cleanup_filename to NULL
17870 here. See unlink_if_set. */
17871 cleanup_filename = NULL;
17872
17873 do_cleanups (cleanup);
17874}
17875
90476074
TT
17876/* Implementation of the `save gdb-index' command.
17877
17878 Note that the file format used by this command is documented in the
17879 GDB manual. Any changes here must be documented there. */
11570e71 17880
9291a0cd
TT
17881static void
17882save_gdb_index_command (char *arg, int from_tty)
17883{
17884 struct objfile *objfile;
17885
17886 if (!arg || !*arg)
96d19272 17887 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17888
17889 ALL_OBJFILES (objfile)
17890 {
17891 struct stat st;
17892
17893 /* If the objfile does not correspond to an actual file, skip it. */
17894 if (stat (objfile->name, &st) < 0)
17895 continue;
17896
17897 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17898 if (dwarf2_per_objfile)
17899 {
17900 volatile struct gdb_exception except;
17901
17902 TRY_CATCH (except, RETURN_MASK_ERROR)
17903 {
17904 write_psymtabs_to_index (objfile, arg);
17905 }
17906 if (except.reason < 0)
17907 exception_fprintf (gdb_stderr, except,
17908 _("Error while writing index for `%s': "),
17909 objfile->name);
17910 }
17911 }
dce234bc
PP
17912}
17913
9291a0cd
TT
17914\f
17915
9eae7c52
TT
17916int dwarf2_always_disassemble;
17917
17918static void
17919show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17920 struct cmd_list_element *c, const char *value)
17921{
3e43a32a
MS
17922 fprintf_filtered (file,
17923 _("Whether to always disassemble "
17924 "DWARF expressions is %s.\n"),
9eae7c52
TT
17925 value);
17926}
17927
900e11f9
JK
17928static void
17929show_check_physname (struct ui_file *file, int from_tty,
17930 struct cmd_list_element *c, const char *value)
17931{
17932 fprintf_filtered (file,
17933 _("Whether to check \"physname\" is %s.\n"),
17934 value);
17935}
17936
6502dd73
DJ
17937void _initialize_dwarf2_read (void);
17938
17939void
17940_initialize_dwarf2_read (void)
17941{
96d19272
JK
17942 struct cmd_list_element *c;
17943
dce234bc 17944 dwarf2_objfile_data_key
c1bd65d0 17945 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17946
1bedd215
AC
17947 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17948Set DWARF 2 specific variables.\n\
17949Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17950 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17951 0/*allow-unknown*/, &maintenance_set_cmdlist);
17952
1bedd215
AC
17953 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17954Show DWARF 2 specific variables\n\
17955Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17956 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17957 0/*allow-unknown*/, &maintenance_show_cmdlist);
17958
17959 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17960 &dwarf2_max_cache_age, _("\
17961Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17962Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17963A higher limit means that cached compilation units will be stored\n\
17964in memory longer, and more total memory will be used. Zero disables\n\
17965caching, which can slow down startup."),
2c5b56ce 17966 NULL,
920d2a44 17967 show_dwarf2_max_cache_age,
2c5b56ce 17968 &set_dwarf2_cmdlist,
ae038cb0 17969 &show_dwarf2_cmdlist);
d97bc12b 17970
9eae7c52
TT
17971 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17972 &dwarf2_always_disassemble, _("\
17973Set whether `info address' always disassembles DWARF expressions."), _("\
17974Show whether `info address' always disassembles DWARF expressions."), _("\
17975When enabled, DWARF expressions are always printed in an assembly-like\n\
17976syntax. When disabled, expressions will be printed in a more\n\
17977conversational style, when possible."),
17978 NULL,
17979 show_dwarf2_always_disassemble,
17980 &set_dwarf2_cmdlist,
17981 &show_dwarf2_cmdlist);
17982
d97bc12b
DE
17983 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17984Set debugging of the dwarf2 DIE reader."), _("\
17985Show debugging of the dwarf2 DIE reader."), _("\
17986When enabled (non-zero), DIEs are dumped after they are read in.\n\
17987The value is the maximum depth to print."),
17988 NULL,
17989 NULL,
17990 &setdebuglist, &showdebuglist);
9291a0cd 17991
900e11f9
JK
17992 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17993Set cross-checking of \"physname\" code against demangler."), _("\
17994Show cross-checking of \"physname\" code against demangler."), _("\
17995When enabled, GDB's internal \"physname\" code is checked against\n\
17996the demangler."),
17997 NULL, show_check_physname,
17998 &setdebuglist, &showdebuglist);
17999
96d19272 18000 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 18001 _("\
fc1a9d6e 18002Save a gdb-index file.\n\
11570e71 18003Usage: save gdb-index DIRECTORY"),
96d19272
JK
18004 &save_cmdlist);
18005 set_cmd_completer (c, filename_completer);
6502dd73 18006}
This page took 2.378735 seconds and 4 git commands to generate.