Remove AT_SYMBOL
[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
9cdd5dbd
DE
151/* Collection of data recorded per objfile.
152 This hangs off of dwarf2_objfile_data_key. */
153
6502dd73
DJ
154struct dwarf2_per_objfile
155{
dce234bc
PP
156 struct dwarf2_section_info info;
157 struct dwarf2_section_info abbrev;
158 struct dwarf2_section_info line;
dce234bc
PP
159 struct dwarf2_section_info loc;
160 struct dwarf2_section_info macinfo;
cf2c3c16 161 struct dwarf2_section_info macro;
dce234bc
PP
162 struct dwarf2_section_info str;
163 struct dwarf2_section_info ranges;
3019eac3 164 struct dwarf2_section_info addr;
dce234bc
PP
165 struct dwarf2_section_info frame;
166 struct dwarf2_section_info eh_frame;
9291a0cd 167 struct dwarf2_section_info gdb_index;
ae038cb0 168
8b70b953
TT
169 VEC (dwarf2_section_info_def) *types;
170
be391dca
TT
171 /* Back link. */
172 struct objfile *objfile;
173
d467dd73 174 /* Table of all the compilation units. This is used to locate
10b3939b 175 the target compilation unit of a particular reference. */
ae038cb0
DJ
176 struct dwarf2_per_cu_data **all_comp_units;
177
178 /* The number of compilation units in ALL_COMP_UNITS. */
179 int n_comp_units;
180
1fd400ff 181 /* The number of .debug_types-related CUs. */
d467dd73 182 int n_type_units;
1fd400ff 183
d467dd73
DE
184 /* The .debug_types-related CUs (TUs). */
185 struct dwarf2_per_cu_data **all_type_units;
1fd400ff 186
ae038cb0
DJ
187 /* A chain of compilation units that are currently read in, so that
188 they can be freed later. */
189 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 190
348e048f
DE
191 /* A table mapping .debug_types signatures to its signatured_type entry.
192 This is NULL if the .debug_types section hasn't been read in yet. */
193 htab_t signatured_types;
194
3019eac3
DE
195 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
196 This is NULL if the table hasn't been allocated yet. */
197 htab_t dwo_files;
198
72dca2f5
FR
199 /* A flag indicating wether this objfile has a section loaded at a
200 VMA of 0. */
201 int has_section_at_zero;
9291a0cd 202
ae2de4f8
DE
203 /* True if we are using the mapped index,
204 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
205 unsigned char using_index;
206
ae2de4f8 207 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 208 struct mapped_index *index_table;
98bfdba5 209
7b9f3c50
DE
210 /* When using index_table, this keeps track of all quick_file_names entries.
211 TUs can share line table entries with CUs or other TUs, and there can be
212 a lot more TUs than unique line tables, so we maintain a separate table
213 of all line table entries to support the sharing. */
214 htab_t quick_file_names_table;
215
98bfdba5
PA
216 /* Set during partial symbol reading, to prevent queueing of full
217 symbols. */
218 int reading_partial_symbols;
673bfd45 219
dee91e82 220 /* Table mapping type DIEs to their struct type *.
673bfd45 221 This is NULL if not allocated yet.
dee91e82
DE
222 The mapping is done via (CU/TU signature + DIE offset) -> type. */
223 htab_t die_type_hash;
6502dd73
DJ
224};
225
226static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 227
251d32d9 228/* Default names of the debugging sections. */
c906108c 229
233a11ab
CS
230/* Note that if the debugging section has been compressed, it might
231 have a name like .zdebug_info. */
232
9cdd5dbd
DE
233static const struct dwarf2_debug_sections dwarf2_elf_names =
234{
251d32d9
TG
235 { ".debug_info", ".zdebug_info" },
236 { ".debug_abbrev", ".zdebug_abbrev" },
237 { ".debug_line", ".zdebug_line" },
238 { ".debug_loc", ".zdebug_loc" },
239 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 240 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
241 { ".debug_str", ".zdebug_str" },
242 { ".debug_ranges", ".zdebug_ranges" },
243 { ".debug_types", ".zdebug_types" },
3019eac3 244 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
245 { ".debug_frame", ".zdebug_frame" },
246 { ".eh_frame", NULL },
24d3216f
TT
247 { ".gdb_index", ".zgdb_index" },
248 23
251d32d9 249};
c906108c 250
3019eac3
DE
251/* List of DWO sections. */
252
253static const struct dwo_section_names
254{
255 struct dwarf2_section_names abbrev_dwo;
256 struct dwarf2_section_names info_dwo;
257 struct dwarf2_section_names line_dwo;
258 struct dwarf2_section_names loc_dwo;
259 struct dwarf2_section_names str_dwo;
260 struct dwarf2_section_names str_offsets_dwo;
261 struct dwarf2_section_names types_dwo;
262}
263dwo_section_names =
264{
265 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
266 { ".debug_info.dwo", ".zdebug_info.dwo" },
267 { ".debug_line.dwo", ".zdebug_line.dwo" },
268 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
269 { ".debug_str.dwo", ".zdebug_str.dwo" },
270 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
271 { ".debug_types.dwo", ".zdebug_types.dwo" },
272};
273
c906108c
SS
274/* local data types */
275
0963b4bd 276/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
277#ifndef ABBREV_HASH_SIZE
278#define ABBREV_HASH_SIZE 121
279#endif
280
107d2387
AC
281/* The data in a compilation unit header, after target2host
282 translation, looks like this. */
c906108c 283struct comp_unit_head
a738430d 284{
c764a876 285 unsigned int length;
a738430d 286 short version;
a738430d
MK
287 unsigned char addr_size;
288 unsigned char signed_addr_p;
b64f50a1 289 sect_offset abbrev_offset;
57349743 290
a738430d
MK
291 /* Size of file offsets; either 4 or 8. */
292 unsigned int offset_size;
57349743 293
a738430d
MK
294 /* Size of the length field; either 4 or 12. */
295 unsigned int initial_length_size;
57349743 296
a738430d
MK
297 /* Offset to the first byte of this compilation unit header in the
298 .debug_info section, for resolving relative reference dies. */
b64f50a1 299 sect_offset offset;
57349743 300
d00adf39
DE
301 /* Offset to first die in this cu from the start of the cu.
302 This will be the first byte following the compilation unit header. */
b64f50a1 303 cu_offset first_die_offset;
a738430d 304};
c906108c 305
3da10d80
KS
306/* Type used for delaying computation of method physnames.
307 See comments for compute_delayed_physnames. */
308struct delayed_method_info
309{
310 /* The type to which the method is attached, i.e., its parent class. */
311 struct type *type;
312
313 /* The index of the method in the type's function fieldlists. */
314 int fnfield_index;
315
316 /* The index of the method in the fieldlist. */
317 int index;
318
319 /* The name of the DIE. */
320 const char *name;
321
322 /* The DIE associated with this method. */
323 struct die_info *die;
324};
325
326typedef struct delayed_method_info delayed_method_info;
327DEF_VEC_O (delayed_method_info);
328
e7c27a73
DJ
329/* Internal state when decoding a particular compilation unit. */
330struct dwarf2_cu
331{
332 /* The objfile containing this compilation unit. */
333 struct objfile *objfile;
334
d00adf39 335 /* The header of the compilation unit. */
e7c27a73 336 struct comp_unit_head header;
e142c38c 337
d00adf39
DE
338 /* Base address of this compilation unit. */
339 CORE_ADDR base_address;
340
341 /* Non-zero if base_address has been set. */
342 int base_known;
343
e142c38c
DJ
344 /* The language we are debugging. */
345 enum language language;
346 const struct language_defn *language_defn;
347
b0f35d58
DL
348 const char *producer;
349
e142c38c
DJ
350 /* The generic symbol table building routines have separate lists for
351 file scope symbols and all all other scopes (local scopes). So
352 we need to select the right one to pass to add_symbol_to_list().
353 We do it by keeping a pointer to the correct list in list_in_scope.
354
355 FIXME: The original dwarf code just treated the file scope as the
356 first local scope, and all other local scopes as nested local
357 scopes, and worked fine. Check to see if we really need to
358 distinguish these in buildsym.c. */
359 struct pending **list_in_scope;
360
f3dd6933
DJ
361 /* DWARF abbreviation table associated with this compilation unit. */
362 struct abbrev_info **dwarf2_abbrevs;
363
364 /* Storage for the abbrev table. */
365 struct obstack abbrev_obstack;
72bf9492 366
b64f50a1
JK
367 /* Hash table holding all the loaded partial DIEs
368 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
369 htab_t partial_dies;
370
371 /* Storage for things with the same lifetime as this read-in compilation
372 unit, including partial DIEs. */
373 struct obstack comp_unit_obstack;
374
ae038cb0
DJ
375 /* When multiple dwarf2_cu structures are living in memory, this field
376 chains them all together, so that they can be released efficiently.
377 We will probably also want a generation counter so that most-recently-used
378 compilation units are cached... */
379 struct dwarf2_per_cu_data *read_in_chain;
380
381 /* Backchain to our per_cu entry if the tree has been built. */
382 struct dwarf2_per_cu_data *per_cu;
383
384 /* How many compilation units ago was this CU last referenced? */
385 int last_used;
386
b64f50a1
JK
387 /* A hash table of DIE cu_offset for following references with
388 die_info->offset.sect_off as hash. */
51545339 389 htab_t die_hash;
10b3939b
DJ
390
391 /* Full DIEs if read in. */
392 struct die_info *dies;
393
394 /* A set of pointers to dwarf2_per_cu_data objects for compilation
395 units referenced by this one. Only set during full symbol processing;
396 partial symbol tables do not have dependencies. */
397 htab_t dependencies;
398
cb1df416
DJ
399 /* Header data from the line table, during full symbol processing. */
400 struct line_header *line_header;
401
3da10d80
KS
402 /* A list of methods which need to have physnames computed
403 after all type information has been read. */
404 VEC (delayed_method_info) *method_list;
405
96408a79
SA
406 /* To be copied to symtab->call_site_htab. */
407 htab_t call_site_htab;
408
3019eac3
DE
409 /* Non-NULL if this CU came from a DWO file. */
410 struct dwo_unit *dwo_unit;
411
412 /* The DW_AT_addr_base attribute if present, zero otherwise
413 (zero is a valid value though).
414 Note this value comes from the stub CU/TU's DIE. */
415 ULONGEST addr_base;
416
ae038cb0
DJ
417 /* Mark used when releasing cached dies. */
418 unsigned int mark : 1;
419
8be455d7
JK
420 /* This CU references .debug_loc. See the symtab->locations_valid field.
421 This test is imperfect as there may exist optimized debug code not using
422 any location list and still facing inlining issues if handled as
423 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 424 unsigned int has_loclist : 1;
ba919b58
TT
425
426 /* These cache the results of producer_is_gxx_lt_4_6.
427 CHECKED_PRODUCER is set if PRODUCER_IS_GXX_LT_4_6 is valid. This
428 information is cached because profiling CU expansion showed
429 excessive time spent in producer_is_gxx_lt_4_6. */
430 unsigned int checked_producer : 1;
431 unsigned int producer_is_gxx_lt_4_6 : 1;
3019eac3
DE
432
433 /* Non-zero if DW_AT_addr_base was found.
434 Used when processing DWO files. */
435 unsigned int have_addr_base : 1;
e7c27a73
DJ
436};
437
10b3939b
DJ
438/* Persistent data held for a compilation unit, even when not
439 processing it. We put a pointer to this structure in the
28dee7f5 440 read_symtab_private field of the psymtab. */
10b3939b 441
ae038cb0
DJ
442struct dwarf2_per_cu_data
443{
348e048f 444 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 445 bytes should suffice to store the length of any compilation unit
45452591
DE
446 - if it doesn't, GDB will fall over anyway.
447 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
448 initial_length_size.
449 If the DIE refers to a DWO file, this is always of the original die,
450 not the DWO file. */
b64f50a1 451 sect_offset offset;
348e048f 452 unsigned int length : 29;
ae038cb0
DJ
453
454 /* Flag indicating this compilation unit will be read in before
455 any of the current compilation units are processed. */
c764a876 456 unsigned int queued : 1;
ae038cb0 457
0d99eb77
DE
458 /* This flag will be set when reading partial DIEs if we need to load
459 absolutely all DIEs for this compilation unit, instead of just the ones
460 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
461 hash table and don't find it. */
462 unsigned int load_all_dies : 1;
463
3019eac3
DE
464 /* Non-zero if this CU is from .debug_types. */
465 unsigned int is_debug_types : 1;
466
467 /* The section this CU/TU lives in.
468 If the DIE refers to a DWO file, this is always the original die,
469 not the DWO file. */
470 struct dwarf2_section_info *info_or_types_section;
348e048f 471
17ea53c3
JK
472 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
473 of the CU cache it gets reset to NULL again. */
ae038cb0 474 struct dwarf2_cu *cu;
1c379e20 475
9cdd5dbd
DE
476 /* The corresponding objfile.
477 Normally we can get the objfile from dwarf2_per_objfile.
478 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
479 struct objfile *objfile;
480
481 /* When using partial symbol tables, the 'psymtab' field is active.
482 Otherwise the 'quick' field is active. */
483 union
484 {
485 /* The partial symbol table associated with this compilation unit,
486 or NULL for partial units (which do not have an associated
487 symtab). */
488 struct partial_symtab *psymtab;
489
490 /* Data needed by the "quick" functions. */
491 struct dwarf2_per_cu_quick_data *quick;
492 } v;
ae038cb0
DJ
493};
494
348e048f
DE
495/* Entry in the signatured_types hash table. */
496
497struct signatured_type
498{
3019eac3 499 /* The type's signature. */
348e048f
DE
500 ULONGEST signature;
501
3019eac3
DE
502 /* Offset in the TU of the type's DIE, as read from the TU header.
503 If the definition lives in a DWO file, this value is unusable. */
504 cu_offset type_offset_in_tu;
505
506 /* Offset in the section of the type's DIE.
507 If the definition lives in a DWO file, this is the offset in the
508 .debug_types.dwo section.
509 The value is zero until the actual value is known.
510 Zero is otherwise not a valid section offset. */
511 sect_offset type_offset_in_section;
348e048f
DE
512
513 /* The CU(/TU) of this type. */
514 struct dwarf2_per_cu_data per_cu;
515};
516
3019eac3
DE
517/* These sections are what may appear in a "dwo" file. */
518
519struct dwo_sections
520{
521 struct dwarf2_section_info abbrev;
522 struct dwarf2_section_info info;
523 struct dwarf2_section_info line;
524 struct dwarf2_section_info loc;
525 struct dwarf2_section_info str;
526 struct dwarf2_section_info str_offsets;
527 VEC (dwarf2_section_info_def) *types;
528};
529
530/* Common bits of DWO CUs/TUs. */
531
532struct dwo_unit
533{
534 /* Backlink to the containing struct dwo_file. */
535 struct dwo_file *dwo_file;
536
537 /* The "id" that distinguishes this CU/TU.
538 .debug_info calls this "dwo_id", .debug_types calls this "signature".
539 Since signatures came first, we stick with it for consistency. */
540 ULONGEST signature;
541
542 /* The section this CU/TU lives in, in the DWO file. */
543 struct dwarf2_section_info *info_or_types_section;
544
545 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
546 sect_offset offset;
547 unsigned int length;
548
549 /* For types, offset in the type's DIE of the type defined by this TU. */
550 cu_offset type_offset_in_tu;
551};
552
553/* Data for one DWO file. */
554
555struct dwo_file
556{
557 /* The DW_AT_GNU_dwo_name attribute.
558 We don't manage space for this, it's an attribute. */
559 const char *dwo_name;
560
561 /* The bfd, when the file is open. Otherwise this is NULL. */
562 bfd *dwo_bfd;
563
564 /* Section info for this file. */
565 struct dwo_sections sections;
566
567 /* Table of CUs in the file.
568 Each element is a struct dwo_unit. */
569 htab_t cus;
570
571 /* Table of TUs in the file.
572 Each element is a struct dwo_unit. */
573 htab_t tus;
574};
575
0963b4bd
MS
576/* Struct used to pass misc. parameters to read_die_and_children, et
577 al. which are used for both .debug_info and .debug_types dies.
578 All parameters here are unchanging for the life of the call. This
dee91e82 579 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
580
581struct die_reader_specs
582{
dee91e82 583 /* die_section->asection->owner. */
93311388
DE
584 bfd* abfd;
585
586 /* The CU of the DIE we are parsing. */
587 struct dwarf2_cu *cu;
588
3019eac3
DE
589 /* Non-NULL if reading a DWO file. */
590 struct dwo_file *dwo_file;
591
dee91e82 592 /* The section the die comes from.
3019eac3 593 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
594 struct dwarf2_section_info *die_section;
595
596 /* die_section->buffer. */
597 gdb_byte *buffer;
93311388
DE
598};
599
fd820528 600/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
601typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
602 gdb_byte *info_ptr,
603 struct die_info *comp_unit_die,
604 int has_children,
605 void *data);
606
debd256d
JB
607/* The line number information for a compilation unit (found in the
608 .debug_line section) begins with a "statement program header",
609 which contains the following information. */
610struct line_header
611{
612 unsigned int total_length;
613 unsigned short version;
614 unsigned int header_length;
615 unsigned char minimum_instruction_length;
2dc7f7b3 616 unsigned char maximum_ops_per_instruction;
debd256d
JB
617 unsigned char default_is_stmt;
618 int line_base;
619 unsigned char line_range;
620 unsigned char opcode_base;
621
622 /* standard_opcode_lengths[i] is the number of operands for the
623 standard opcode whose value is i. This means that
624 standard_opcode_lengths[0] is unused, and the last meaningful
625 element is standard_opcode_lengths[opcode_base - 1]. */
626 unsigned char *standard_opcode_lengths;
627
628 /* The include_directories table. NOTE! These strings are not
629 allocated with xmalloc; instead, they are pointers into
630 debug_line_buffer. If you try to free them, `free' will get
631 indigestion. */
632 unsigned int num_include_dirs, include_dirs_size;
633 char **include_dirs;
634
635 /* The file_names table. NOTE! These strings are not allocated
636 with xmalloc; instead, they are pointers into debug_line_buffer.
637 Don't try to free them directly. */
638 unsigned int num_file_names, file_names_size;
639 struct file_entry
c906108c 640 {
debd256d
JB
641 char *name;
642 unsigned int dir_index;
643 unsigned int mod_time;
644 unsigned int length;
aaa75496 645 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 646 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
647 } *file_names;
648
649 /* The start and end of the statement program following this
6502dd73 650 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 651 gdb_byte *statement_program_start, *statement_program_end;
debd256d 652};
c906108c
SS
653
654/* When we construct a partial symbol table entry we only
0963b4bd 655 need this much information. */
c906108c
SS
656struct partial_die_info
657 {
72bf9492 658 /* Offset of this DIE. */
b64f50a1 659 sect_offset offset;
72bf9492
DJ
660
661 /* DWARF-2 tag for this DIE. */
662 ENUM_BITFIELD(dwarf_tag) tag : 16;
663
72bf9492
DJ
664 /* Assorted flags describing the data found in this DIE. */
665 unsigned int has_children : 1;
666 unsigned int is_external : 1;
667 unsigned int is_declaration : 1;
668 unsigned int has_type : 1;
669 unsigned int has_specification : 1;
670 unsigned int has_pc_info : 1;
481860b3 671 unsigned int may_be_inlined : 1;
72bf9492
DJ
672
673 /* Flag set if the SCOPE field of this structure has been
674 computed. */
675 unsigned int scope_set : 1;
676
fa4028e9
JB
677 /* Flag set if the DIE has a byte_size attribute. */
678 unsigned int has_byte_size : 1;
679
98bfdba5
PA
680 /* Flag set if any of the DIE's children are template arguments. */
681 unsigned int has_template_arguments : 1;
682
abc72ce4
DE
683 /* Flag set if fixup_partial_die has been called on this die. */
684 unsigned int fixup_called : 1;
685
72bf9492 686 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 687 sometimes a default name for unnamed DIEs. */
c906108c 688 char *name;
72bf9492 689
abc72ce4
DE
690 /* The linkage name, if present. */
691 const char *linkage_name;
692
72bf9492
DJ
693 /* The scope to prepend to our children. This is generally
694 allocated on the comp_unit_obstack, so will disappear
695 when this compilation unit leaves the cache. */
696 char *scope;
697
698 /* The location description associated with this DIE, if any. */
699 struct dwarf_block *locdesc;
700
701 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
702 CORE_ADDR lowpc;
703 CORE_ADDR highpc;
72bf9492 704
93311388 705 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 706 DW_AT_sibling, if any. */
abc72ce4
DE
707 /* NOTE: This member isn't strictly necessary, read_partial_die could
708 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 709 gdb_byte *sibling;
72bf9492
DJ
710
711 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
712 DW_AT_specification (or DW_AT_abstract_origin or
713 DW_AT_extension). */
b64f50a1 714 sect_offset spec_offset;
72bf9492
DJ
715
716 /* Pointers to this DIE's parent, first child, and next sibling,
717 if any. */
718 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
719 };
720
0963b4bd 721/* This data structure holds the information of an abbrev. */
c906108c
SS
722struct abbrev_info
723 {
724 unsigned int number; /* number identifying abbrev */
725 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
726 unsigned short has_children; /* boolean */
727 unsigned short num_attrs; /* number of attributes */
c906108c
SS
728 struct attr_abbrev *attrs; /* an array of attribute descriptions */
729 struct abbrev_info *next; /* next in chain */
730 };
731
732struct attr_abbrev
733 {
9d25dd43
DE
734 ENUM_BITFIELD(dwarf_attribute) name : 16;
735 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
736 };
737
0963b4bd 738/* Attributes have a name and a value. */
b60c80d6
DJ
739struct attribute
740 {
9d25dd43 741 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
742 ENUM_BITFIELD(dwarf_form) form : 15;
743
744 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
745 field should be in u.str (existing only for DW_STRING) but it is kept
746 here for better struct attribute alignment. */
747 unsigned int string_is_canonical : 1;
748
b60c80d6
DJ
749 union
750 {
751 char *str;
752 struct dwarf_block *blk;
43bbcdc2
PH
753 ULONGEST unsnd;
754 LONGEST snd;
b60c80d6 755 CORE_ADDR addr;
348e048f 756 struct signatured_type *signatured_type;
b60c80d6
DJ
757 }
758 u;
759 };
760
0963b4bd 761/* This data structure holds a complete die structure. */
c906108c
SS
762struct die_info
763 {
76815b17
DE
764 /* DWARF-2 tag for this DIE. */
765 ENUM_BITFIELD(dwarf_tag) tag : 16;
766
767 /* Number of attributes */
98bfdba5
PA
768 unsigned char num_attrs;
769
770 /* True if we're presently building the full type name for the
771 type derived from this DIE. */
772 unsigned char building_fullname : 1;
76815b17
DE
773
774 /* Abbrev number */
775 unsigned int abbrev;
776
93311388 777 /* Offset in .debug_info or .debug_types section. */
b64f50a1 778 sect_offset offset;
78ba4af6
JB
779
780 /* The dies in a compilation unit form an n-ary tree. PARENT
781 points to this die's parent; CHILD points to the first child of
782 this node; and all the children of a given node are chained
4950bc1c 783 together via their SIBLING fields. */
639d11d3
DC
784 struct die_info *child; /* Its first child, if any. */
785 struct die_info *sibling; /* Its next sibling, if any. */
786 struct die_info *parent; /* Its parent, if any. */
c906108c 787
b60c80d6
DJ
788 /* An array of attributes, with NUM_ATTRS elements. There may be
789 zero, but it's not common and zero-sized arrays are not
790 sufficiently portable C. */
791 struct attribute attrs[1];
c906108c
SS
792 };
793
0963b4bd 794/* Get at parts of an attribute structure. */
c906108c
SS
795
796#define DW_STRING(attr) ((attr)->u.str)
8285870a 797#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
798#define DW_UNSND(attr) ((attr)->u.unsnd)
799#define DW_BLOCK(attr) ((attr)->u.blk)
800#define DW_SND(attr) ((attr)->u.snd)
801#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 802#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 803
0963b4bd 804/* Blocks are a bunch of untyped bytes. */
c906108c
SS
805struct dwarf_block
806 {
807 unsigned int size;
1d6edc3c
JK
808
809 /* Valid only if SIZE is not zero. */
fe1b8b76 810 gdb_byte *data;
c906108c
SS
811 };
812
c906108c
SS
813#ifndef ATTR_ALLOC_CHUNK
814#define ATTR_ALLOC_CHUNK 4
815#endif
816
c906108c
SS
817/* Allocate fields for structs, unions and enums in this size. */
818#ifndef DW_FIELD_ALLOC_CHUNK
819#define DW_FIELD_ALLOC_CHUNK 4
820#endif
821
c906108c
SS
822/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
823 but this would require a corresponding change in unpack_field_as_long
824 and friends. */
825static int bits_per_byte = 8;
826
827/* The routines that read and process dies for a C struct or C++ class
828 pass lists of data member fields and lists of member function fields
829 in an instance of a field_info structure, as defined below. */
830struct field_info
c5aa993b 831 {
0963b4bd 832 /* List of data member and baseclasses fields. */
c5aa993b
JM
833 struct nextfield
834 {
835 struct nextfield *next;
836 int accessibility;
837 int virtuality;
838 struct field field;
839 }
7d0ccb61 840 *fields, *baseclasses;
c906108c 841
7d0ccb61 842 /* Number of fields (including baseclasses). */
c5aa993b 843 int nfields;
c906108c 844
c5aa993b
JM
845 /* Number of baseclasses. */
846 int nbaseclasses;
c906108c 847
c5aa993b
JM
848 /* Set if the accesibility of one of the fields is not public. */
849 int non_public_fields;
c906108c 850
c5aa993b
JM
851 /* Member function fields array, entries are allocated in the order they
852 are encountered in the object file. */
853 struct nextfnfield
854 {
855 struct nextfnfield *next;
856 struct fn_field fnfield;
857 }
858 *fnfields;
c906108c 859
c5aa993b
JM
860 /* Member function fieldlist array, contains name of possibly overloaded
861 member function, number of overloaded member functions and a pointer
862 to the head of the member function field chain. */
863 struct fnfieldlist
864 {
865 char *name;
866 int length;
867 struct nextfnfield *head;
868 }
869 *fnfieldlists;
c906108c 870
c5aa993b
JM
871 /* Number of entries in the fnfieldlists array. */
872 int nfnfields;
98751a41
JK
873
874 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
875 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
876 struct typedef_field_list
877 {
878 struct typedef_field field;
879 struct typedef_field_list *next;
880 }
881 *typedef_field_list;
882 unsigned typedef_field_list_count;
c5aa993b 883 };
c906108c 884
10b3939b
DJ
885/* One item on the queue of compilation units to read in full symbols
886 for. */
887struct dwarf2_queue_item
888{
889 struct dwarf2_per_cu_data *per_cu;
890 struct dwarf2_queue_item *next;
891};
892
893/* The current queue. */
894static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
895
ae038cb0
DJ
896/* Loaded secondary compilation units are kept in memory until they
897 have not been referenced for the processing of this many
898 compilation units. Set this to zero to disable caching. Cache
899 sizes of up to at least twenty will improve startup time for
900 typical inter-CU-reference binaries, at an obvious memory cost. */
901static int dwarf2_max_cache_age = 5;
920d2a44
AC
902static void
903show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
904 struct cmd_list_element *c, const char *value)
905{
3e43a32a
MS
906 fprintf_filtered (file, _("The upper bound on the age of cached "
907 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
908 value);
909}
910
ae038cb0 911
0963b4bd 912/* Various complaints about symbol reading that don't abort the process. */
c906108c 913
4d3c2250
KB
914static void
915dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 916{
4d3c2250 917 complaint (&symfile_complaints,
e2e0b3e5 918 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
919}
920
25e43795
DJ
921static void
922dwarf2_debug_line_missing_file_complaint (void)
923{
924 complaint (&symfile_complaints,
925 _(".debug_line section has line data without a file"));
926}
927
59205f5a
JB
928static void
929dwarf2_debug_line_missing_end_sequence_complaint (void)
930{
931 complaint (&symfile_complaints,
3e43a32a
MS
932 _(".debug_line section has line "
933 "program sequence without an end"));
59205f5a
JB
934}
935
4d3c2250
KB
936static void
937dwarf2_complex_location_expr_complaint (void)
2e276125 938{
e2e0b3e5 939 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
940}
941
4d3c2250
KB
942static void
943dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
944 int arg3)
2e276125 945{
4d3c2250 946 complaint (&symfile_complaints,
3e43a32a
MS
947 _("const value length mismatch for '%s', got %d, expected %d"),
948 arg1, arg2, arg3);
4d3c2250
KB
949}
950
951static void
cf2c3c16 952dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
2e276125 953{
4d3c2250 954 complaint (&symfile_complaints,
cf2c3c16
TT
955 _("macro info runs off end of `%s' section"),
956 section->asection->name);
4d3c2250
KB
957}
958
959static void
960dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 961{
4d3c2250 962 complaint (&symfile_complaints,
3e43a32a
MS
963 _("macro debug info contains a "
964 "malformed macro definition:\n`%s'"),
4d3c2250
KB
965 arg1);
966}
967
968static void
969dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 970{
4d3c2250 971 complaint (&symfile_complaints,
3e43a32a
MS
972 _("invalid attribute class or form for '%s' in '%s'"),
973 arg1, arg2);
4d3c2250 974}
c906108c 975
c906108c
SS
976/* local function prototypes */
977
4efb68b1 978static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 979
aaa75496
JB
980static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
981 struct objfile *);
982
918dd910
JK
983static void dwarf2_find_base_address (struct die_info *die,
984 struct dwarf2_cu *cu);
985
c67a9c90 986static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 987
72bf9492
DJ
988static void scan_partial_symbols (struct partial_die_info *,
989 CORE_ADDR *, CORE_ADDR *,
5734ee8b 990 int, struct dwarf2_cu *);
c906108c 991
72bf9492
DJ
992static void add_partial_symbol (struct partial_die_info *,
993 struct dwarf2_cu *);
63d06c5c 994
72bf9492
DJ
995static void add_partial_namespace (struct partial_die_info *pdi,
996 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 997 int need_pc, struct dwarf2_cu *cu);
63d06c5c 998
5d7cb8df
JK
999static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1000 CORE_ADDR *highpc, int need_pc,
1001 struct dwarf2_cu *cu);
1002
72bf9492
DJ
1003static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1004 struct dwarf2_cu *cu);
91c24f0a 1005
bc30ff58
JB
1006static void add_partial_subprogram (struct partial_die_info *pdi,
1007 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1008 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1009
a14ed312 1010static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 1011
a14ed312 1012static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1013
dee91e82
DE
1014static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1015 struct dwarf2_section_info *);
c906108c 1016
f3dd6933 1017static void dwarf2_free_abbrev_table (void *);
c906108c 1018
6caca83c
CC
1019static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1020
fe1b8b76 1021static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 1022 struct dwarf2_cu *);
72bf9492 1023
57349743 1024static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 1025 struct dwarf2_cu *);
c906108c 1026
dee91e82
DE
1027static struct partial_die_info *load_partial_dies
1028 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1029
dee91e82
DE
1030static gdb_byte *read_partial_die (const struct die_reader_specs *,
1031 struct partial_die_info *,
1032 struct abbrev_info *,
1033 unsigned int,
1034 gdb_byte *);
c906108c 1035
b64f50a1 1036static struct partial_die_info *find_partial_die (sect_offset,
10b3939b 1037 struct dwarf2_cu *);
72bf9492
DJ
1038
1039static void fixup_partial_die (struct partial_die_info *,
1040 struct dwarf2_cu *);
1041
dee91e82
DE
1042static gdb_byte *read_attribute (const struct die_reader_specs *,
1043 struct attribute *, struct attr_abbrev *,
1044 gdb_byte *);
a8329558 1045
fe1b8b76 1046static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 1047
fe1b8b76 1048static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 1049
fe1b8b76 1050static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 1051
fe1b8b76 1052static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 1053
93311388 1054static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 1055
fe1b8b76 1056static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1057 unsigned int *);
c906108c 1058
c764a876
DE
1059static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1060
1061static LONGEST read_checked_initial_length_and_offset
1062 (bfd *, gdb_byte *, const struct comp_unit_head *,
1063 unsigned int *, unsigned int *);
613e1657 1064
fe1b8b76 1065static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1066 unsigned int *);
1067
1068static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1069
fe1b8b76 1070static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1071
9b1c24c8 1072static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1073
fe1b8b76
JB
1074static char *read_indirect_string (bfd *, gdb_byte *,
1075 const struct comp_unit_head *,
1076 unsigned int *);
4bdf3d34 1077
12df843f 1078static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1079
12df843f 1080static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1081
3019eac3
DE
1082static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1083 unsigned int *);
1084
1085static char *read_str_index (const struct die_reader_specs *reader,
1086 struct dwarf2_cu *cu, ULONGEST str_index);
1087
fe1b8b76 1088static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 1089
e142c38c 1090static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1091
e142c38c
DJ
1092static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1093 struct dwarf2_cu *);
c906108c 1094
348e048f
DE
1095static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1096 unsigned int,
1097 struct dwarf2_cu *);
1098
05cf31d1
JB
1099static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1100 struct dwarf2_cu *cu);
1101
e142c38c 1102static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1103
e142c38c 1104static struct die_info *die_specification (struct die_info *die,
f2f0e013 1105 struct dwarf2_cu **);
63d06c5c 1106
debd256d
JB
1107static void free_line_header (struct line_header *lh);
1108
aaa75496
JB
1109static void add_file_name (struct line_header *, char *, unsigned int,
1110 unsigned int, unsigned int);
1111
3019eac3
DE
1112static struct line_header *dwarf_decode_line_header (unsigned int offset,
1113 struct dwarf2_cu *cu);
debd256d 1114
f3f5162e
DE
1115static void dwarf_decode_lines (struct line_header *, const char *,
1116 struct dwarf2_cu *, struct partial_symtab *,
1117 int);
c906108c 1118
72b9f47f 1119static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1120
a14ed312 1121static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1122 struct dwarf2_cu *);
c906108c 1123
34eaf542
TT
1124static struct symbol *new_symbol_full (struct die_info *, struct type *,
1125 struct dwarf2_cu *, struct symbol *);
1126
a14ed312 1127static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1128 struct dwarf2_cu *);
c906108c 1129
98bfdba5
PA
1130static void dwarf2_const_value_attr (struct attribute *attr,
1131 struct type *type,
1132 const char *name,
1133 struct obstack *obstack,
12df843f 1134 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1135 gdb_byte **bytes,
1136 struct dwarf2_locexpr_baton **baton);
2df3850c 1137
e7c27a73 1138static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1139
b4ba55a1
JB
1140static int need_gnat_info (struct dwarf2_cu *);
1141
3e43a32a
MS
1142static struct type *die_descriptive_type (struct die_info *,
1143 struct dwarf2_cu *);
b4ba55a1
JB
1144
1145static void set_descriptive_type (struct type *, struct die_info *,
1146 struct dwarf2_cu *);
1147
e7c27a73
DJ
1148static struct type *die_containing_type (struct die_info *,
1149 struct dwarf2_cu *);
c906108c 1150
673bfd45
DE
1151static struct type *lookup_die_type (struct die_info *, struct attribute *,
1152 struct dwarf2_cu *);
c906108c 1153
f792889a 1154static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1155
673bfd45
DE
1156static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1157
0d5cff50 1158static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1159
6e70227d 1160static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1161 const char *suffix, int physname,
1162 struct dwarf2_cu *cu);
63d06c5c 1163
e7c27a73 1164static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1165
348e048f
DE
1166static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1167
e7c27a73 1168static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1169
e7c27a73 1170static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1171
96408a79
SA
1172static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1173
ff013f42
JK
1174static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1175 struct dwarf2_cu *, struct partial_symtab *);
1176
a14ed312 1177static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1178 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1179 struct partial_symtab *);
c906108c 1180
fae299cd
DC
1181static void get_scope_pc_bounds (struct die_info *,
1182 CORE_ADDR *, CORE_ADDR *,
1183 struct dwarf2_cu *);
1184
801e3a5b
JB
1185static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1186 CORE_ADDR, struct dwarf2_cu *);
1187
a14ed312 1188static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1189 struct dwarf2_cu *);
c906108c 1190
a14ed312 1191static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1192 struct type *, struct dwarf2_cu *);
c906108c 1193
a14ed312 1194static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1195 struct die_info *, struct type *,
e7c27a73 1196 struct dwarf2_cu *);
c906108c 1197
a14ed312 1198static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1199 struct type *,
1200 struct dwarf2_cu *);
c906108c 1201
134d01f1 1202static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1203
e7c27a73 1204static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1205
e7c27a73 1206static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1207
5d7cb8df
JK
1208static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1209
27aa8d6a
SW
1210static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1211
f55ee35c
JK
1212static struct type *read_module_type (struct die_info *die,
1213 struct dwarf2_cu *cu);
1214
38d518c9 1215static const char *namespace_name (struct die_info *die,
e142c38c 1216 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1217
134d01f1 1218static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1219
e7c27a73 1220static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1221
6e70227d 1222static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1223 struct dwarf2_cu *);
1224
dee91e82 1225static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1226 gdb_byte *info_ptr,
fe1b8b76 1227 gdb_byte **new_info_ptr,
639d11d3
DC
1228 struct die_info *parent);
1229
dee91e82 1230static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1231 gdb_byte *info_ptr,
fe1b8b76 1232 gdb_byte **new_info_ptr,
639d11d3
DC
1233 struct die_info *parent);
1234
3019eac3
DE
1235static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1236 struct die_info **, gdb_byte *, int *, int);
1237
dee91e82
DE
1238static gdb_byte *read_full_die (const struct die_reader_specs *,
1239 struct die_info **, gdb_byte *, int *);
93311388 1240
e7c27a73 1241static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1242
71c25dea
TT
1243static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1244 struct obstack *);
1245
e142c38c 1246static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1247
98bfdba5
PA
1248static const char *dwarf2_full_name (char *name,
1249 struct die_info *die,
1250 struct dwarf2_cu *cu);
1251
e142c38c 1252static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1253 struct dwarf2_cu **);
9219021c 1254
f39c6ffd 1255static const char *dwarf_tag_name (unsigned int);
c906108c 1256
f39c6ffd 1257static const char *dwarf_attr_name (unsigned int);
c906108c 1258
f39c6ffd 1259static const char *dwarf_form_name (unsigned int);
c906108c 1260
a14ed312 1261static char *dwarf_bool_name (unsigned int);
c906108c 1262
f39c6ffd 1263static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1264
f9aca02d 1265static struct die_info *sibling_die (struct die_info *);
c906108c 1266
d97bc12b
DE
1267static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1268
1269static void dump_die_for_error (struct die_info *);
1270
1271static void dump_die_1 (struct ui_file *, int level, int max_level,
1272 struct die_info *);
c906108c 1273
d97bc12b 1274/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1275
51545339 1276static void store_in_ref_table (struct die_info *,
10b3939b 1277 struct dwarf2_cu *);
c906108c 1278
93311388
DE
1279static int is_ref_attr (struct attribute *);
1280
b64f50a1 1281static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1282
43bbcdc2 1283static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1284
348e048f
DE
1285static struct die_info *follow_die_ref_or_sig (struct die_info *,
1286 struct attribute *,
1287 struct dwarf2_cu **);
1288
10b3939b
DJ
1289static struct die_info *follow_die_ref (struct die_info *,
1290 struct attribute *,
f2f0e013 1291 struct dwarf2_cu **);
c906108c 1292
348e048f
DE
1293static struct die_info *follow_die_sig (struct die_info *,
1294 struct attribute *,
1295 struct dwarf2_cu **);
1296
6c83ed52
TT
1297static struct signatured_type *lookup_signatured_type_at_offset
1298 (struct objfile *objfile,
b64f50a1 1299 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1300
e5fe5e75 1301static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1302
52dc124a 1303static void read_signatured_type (struct signatured_type *);
348e048f 1304
c906108c
SS
1305/* memory allocation interface */
1306
7b5a2f43 1307static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1308
f3dd6933 1309static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1310
b60c80d6 1311static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1312
2e276125 1313static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1314 char *, bfd *, struct dwarf2_cu *,
1315 struct dwarf2_section_info *,
fceca515 1316 int, const char *);
2e276125 1317
8e19ed76
PS
1318static int attr_form_is_block (struct attribute *);
1319
3690dd37
JB
1320static int attr_form_is_section_offset (struct attribute *);
1321
1322static int attr_form_is_constant (struct attribute *);
1323
8cf6f0b1
TT
1324static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1325 struct dwarf2_loclist_baton *baton,
1326 struct attribute *attr);
1327
93e7bd98
DJ
1328static void dwarf2_symbol_mark_computed (struct attribute *attr,
1329 struct symbol *sym,
1330 struct dwarf2_cu *cu);
4c2df51b 1331
dee91e82
DE
1332static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1333 gdb_byte *info_ptr,
1334 struct abbrev_info *abbrev);
4bb7a0a7 1335
72bf9492
DJ
1336static void free_stack_comp_unit (void *);
1337
72bf9492
DJ
1338static hashval_t partial_die_hash (const void *item);
1339
1340static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1341
ae038cb0 1342static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
b64f50a1 1343 (sect_offset offset, struct objfile *objfile);
ae038cb0 1344
9816fde3 1345static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1346 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1347
1348static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1349 struct die_info *comp_unit_die);
93311388 1350
68dc6402 1351static void free_heap_comp_unit (void *);
ae038cb0
DJ
1352
1353static void free_cached_comp_units (void *);
1354
1355static void age_cached_comp_units (void);
1356
dee91e82 1357static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1358
f792889a
DJ
1359static struct type *set_die_type (struct die_info *, struct type *,
1360 struct dwarf2_cu *);
1c379e20 1361
ae038cb0
DJ
1362static void create_all_comp_units (struct objfile *);
1363
0e50663e 1364static int create_all_type_units (struct objfile *);
1fd400ff 1365
a0f42c21 1366static void load_full_comp_unit (struct dwarf2_per_cu_data *);
10b3939b
DJ
1367
1368static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1369
1370static void dwarf2_add_dependence (struct dwarf2_cu *,
1371 struct dwarf2_per_cu_data *);
1372
ae038cb0
DJ
1373static void dwarf2_mark (struct dwarf2_cu *);
1374
1375static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1376
b64f50a1 1377static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1378 struct dwarf2_per_cu_data *per_cu);
1379
f792889a 1380static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1381
9291a0cd
TT
1382static void dwarf2_release_queue (void *dummy);
1383
a0f42c21 1384static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
9291a0cd 1385
a0f42c21 1386static void process_queue (void);
9291a0cd
TT
1387
1388static void find_file_and_directory (struct die_info *die,
1389 struct dwarf2_cu *cu,
1390 char **name, char **comp_dir);
1391
1392static char *file_full_name (int file, struct line_header *lh,
1393 const char *comp_dir);
1394
9ff913ba
DE
1395static gdb_byte *read_and_check_comp_unit_head
1396 (struct comp_unit_head *header,
1397 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1398 int is_debug_types_section);
9291a0cd 1399
fd820528
DE
1400static void init_cutu_and_read_dies
1401 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
3019eac3
DE
1402 die_reader_func_ftype *die_reader_func, void *data);
1403
dee91e82
DE
1404static void init_cutu_and_read_dies_simple
1405 (struct dwarf2_per_cu_data *this_cu,
1406 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1407
673bfd45 1408static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1409
dee91e82
DE
1410static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *);
1411
3019eac3
DE
1412static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1413
1414static struct dwo_unit *lookup_dwo_comp_unit
1415 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1416
1417static struct dwo_unit *lookup_dwo_type_unit
1418 (struct signatured_type *, char *, const char *);
1419
1420static void free_dwo_file_cleanup (void *);
1421
1422static void munmap_section_buffer (struct dwarf2_section_info *);
1423
9291a0cd
TT
1424#if WORDS_BIGENDIAN
1425
1426/* Convert VALUE between big- and little-endian. */
1427static offset_type
1428byte_swap (offset_type value)
1429{
1430 offset_type result;
1431
1432 result = (value & 0xff) << 24;
1433 result |= (value & 0xff00) << 8;
1434 result |= (value & 0xff0000) >> 8;
1435 result |= (value & 0xff000000) >> 24;
1436 return result;
1437}
1438
1439#define MAYBE_SWAP(V) byte_swap (V)
1440
1441#else
1442#define MAYBE_SWAP(V) (V)
1443#endif /* WORDS_BIGENDIAN */
1444
1445/* The suffix for an index file. */
1446#define INDEX_SUFFIX ".gdb-index"
1447
3da10d80
KS
1448static const char *dwarf2_physname (char *name, struct die_info *die,
1449 struct dwarf2_cu *cu);
1450
c906108c 1451/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1452 information and return true if we have enough to do something.
1453 NAMES points to the dwarf2 section names, or is NULL if the standard
1454 ELF names are used. */
c906108c
SS
1455
1456int
251d32d9
TG
1457dwarf2_has_info (struct objfile *objfile,
1458 const struct dwarf2_debug_sections *names)
c906108c 1459{
be391dca
TT
1460 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1461 if (!dwarf2_per_objfile)
1462 {
1463 /* Initialize per-objfile state. */
1464 struct dwarf2_per_objfile *data
1465 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1466
be391dca
TT
1467 memset (data, 0, sizeof (*data));
1468 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1469 dwarf2_per_objfile = data;
6502dd73 1470
251d32d9
TG
1471 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1472 (void *) names);
be391dca
TT
1473 dwarf2_per_objfile->objfile = objfile;
1474 }
1475 return (dwarf2_per_objfile->info.asection != NULL
1476 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1477}
1478
251d32d9
TG
1479/* When loading sections, we look either for uncompressed section or for
1480 compressed section names. */
233a11ab
CS
1481
1482static int
251d32d9
TG
1483section_is_p (const char *section_name,
1484 const struct dwarf2_section_names *names)
233a11ab 1485{
251d32d9
TG
1486 if (names->normal != NULL
1487 && strcmp (section_name, names->normal) == 0)
1488 return 1;
1489 if (names->compressed != NULL
1490 && strcmp (section_name, names->compressed) == 0)
1491 return 1;
1492 return 0;
233a11ab
CS
1493}
1494
c906108c
SS
1495/* This function is mapped across the sections and remembers the
1496 offset and size of each of the debugging sections we are interested
1497 in. */
1498
1499static void
251d32d9 1500dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1501{
251d32d9
TG
1502 const struct dwarf2_debug_sections *names;
1503
1504 if (vnames == NULL)
1505 names = &dwarf2_elf_names;
1506 else
1507 names = (const struct dwarf2_debug_sections *) vnames;
1508
1509 if (section_is_p (sectp->name, &names->info))
c906108c 1510 {
dce234bc
PP
1511 dwarf2_per_objfile->info.asection = sectp;
1512 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1513 }
251d32d9 1514 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1515 {
dce234bc
PP
1516 dwarf2_per_objfile->abbrev.asection = sectp;
1517 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1518 }
251d32d9 1519 else if (section_is_p (sectp->name, &names->line))
c906108c 1520 {
dce234bc
PP
1521 dwarf2_per_objfile->line.asection = sectp;
1522 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1523 }
251d32d9 1524 else if (section_is_p (sectp->name, &names->loc))
c906108c 1525 {
dce234bc
PP
1526 dwarf2_per_objfile->loc.asection = sectp;
1527 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1528 }
251d32d9 1529 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1530 {
dce234bc
PP
1531 dwarf2_per_objfile->macinfo.asection = sectp;
1532 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1533 }
cf2c3c16
TT
1534 else if (section_is_p (sectp->name, &names->macro))
1535 {
1536 dwarf2_per_objfile->macro.asection = sectp;
1537 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1538 }
251d32d9 1539 else if (section_is_p (sectp->name, &names->str))
c906108c 1540 {
dce234bc
PP
1541 dwarf2_per_objfile->str.asection = sectp;
1542 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1543 }
3019eac3
DE
1544 else if (section_is_p (sectp->name, &names->addr))
1545 {
1546 dwarf2_per_objfile->addr.asection = sectp;
1547 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1548 }
251d32d9 1549 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1550 {
dce234bc
PP
1551 dwarf2_per_objfile->frame.asection = sectp;
1552 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1553 }
251d32d9 1554 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1555 {
3799ccc6 1556 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
9a619af0 1557
3799ccc6
EZ
1558 if (aflag & SEC_HAS_CONTENTS)
1559 {
dce234bc
PP
1560 dwarf2_per_objfile->eh_frame.asection = sectp;
1561 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1562 }
b6af0555 1563 }
251d32d9 1564 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1565 {
dce234bc
PP
1566 dwarf2_per_objfile->ranges.asection = sectp;
1567 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1568 }
251d32d9 1569 else if (section_is_p (sectp->name, &names->types))
348e048f 1570 {
8b70b953
TT
1571 struct dwarf2_section_info type_section;
1572
1573 memset (&type_section, 0, sizeof (type_section));
1574 type_section.asection = sectp;
1575 type_section.size = bfd_get_section_size (sectp);
1576
1577 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1578 &type_section);
348e048f 1579 }
251d32d9 1580 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1581 {
1582 dwarf2_per_objfile->gdb_index.asection = sectp;
1583 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1584 }
dce234bc 1585
72dca2f5
FR
1586 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1587 && bfd_section_vma (abfd, sectp) == 0)
1588 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1589}
1590
dce234bc
PP
1591/* Decompress a section that was compressed using zlib. Store the
1592 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1593
1594static void
dce234bc
PP
1595zlib_decompress_section (struct objfile *objfile, asection *sectp,
1596 gdb_byte **outbuf, bfd_size_type *outsize)
1597{
3019eac3 1598 bfd *abfd = sectp->owner;
dce234bc
PP
1599#ifndef HAVE_ZLIB_H
1600 error (_("Support for zlib-compressed DWARF data (from '%s') "
1601 "is disabled in this copy of GDB"),
1602 bfd_get_filename (abfd));
1603#else
1604 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1605 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1606 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1607 bfd_size_type uncompressed_size;
1608 gdb_byte *uncompressed_buffer;
1609 z_stream strm;
1610 int rc;
1611 int header_size = 12;
1612
1613 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1614 || bfd_bread (compressed_buffer,
1615 compressed_size, abfd) != compressed_size)
dce234bc
PP
1616 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1617 bfd_get_filename (abfd));
1618
1619 /* Read the zlib header. In this case, it should be "ZLIB" followed
1620 by the uncompressed section size, 8 bytes in big-endian order. */
1621 if (compressed_size < header_size
1622 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1623 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1624 bfd_get_filename (abfd));
1625 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1626 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1627 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1628 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1629 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1630 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1631 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1632 uncompressed_size += compressed_buffer[11];
1633
1634 /* It is possible the section consists of several compressed
1635 buffers concatenated together, so we uncompress in a loop. */
1636 strm.zalloc = NULL;
1637 strm.zfree = NULL;
1638 strm.opaque = NULL;
1639 strm.avail_in = compressed_size - header_size;
1640 strm.next_in = (Bytef*) compressed_buffer + header_size;
1641 strm.avail_out = uncompressed_size;
1642 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1643 uncompressed_size);
1644 rc = inflateInit (&strm);
1645 while (strm.avail_in > 0)
1646 {
1647 if (rc != Z_OK)
1648 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1649 bfd_get_filename (abfd), rc);
1650 strm.next_out = ((Bytef*) uncompressed_buffer
1651 + (uncompressed_size - strm.avail_out));
1652 rc = inflate (&strm, Z_FINISH);
1653 if (rc != Z_STREAM_END)
1654 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1655 bfd_get_filename (abfd), rc);
1656 rc = inflateReset (&strm);
1657 }
1658 rc = inflateEnd (&strm);
1659 if (rc != Z_OK
1660 || strm.avail_out != 0)
1661 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1662 bfd_get_filename (abfd), rc);
1663
affddf13 1664 do_cleanups (cleanup);
dce234bc
PP
1665 *outbuf = uncompressed_buffer;
1666 *outsize = uncompressed_size;
1667#endif
233a11ab
CS
1668}
1669
fceca515
DE
1670/* A helper function that decides whether a section is empty,
1671 or not present. */
9e0ac564
TT
1672
1673static int
1674dwarf2_section_empty_p (struct dwarf2_section_info *info)
1675{
1676 return info->asection == NULL || info->size == 0;
1677}
1678
3019eac3
DE
1679/* Read the contents of the section INFO.
1680 OBJFILE is the main object file, but not necessarily the file where
1681 the section comes from. E.g., for DWO files INFO->asection->owner
1682 is the bfd of the DWO file.
dce234bc 1683 If the section is compressed, uncompress it before returning. */
c906108c 1684
dce234bc
PP
1685static void
1686dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1687{
dce234bc 1688 asection *sectp = info->asection;
3019eac3 1689 bfd *abfd;
dce234bc
PP
1690 gdb_byte *buf, *retbuf;
1691 unsigned char header[4];
c906108c 1692
be391dca
TT
1693 if (info->readin)
1694 return;
dce234bc 1695 info->buffer = NULL;
b315ab21 1696 info->map_addr = NULL;
be391dca 1697 info->readin = 1;
188dd5d6 1698
9e0ac564 1699 if (dwarf2_section_empty_p (info))
dce234bc 1700 return;
c906108c 1701
3019eac3
DE
1702 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1703 abfd = sectp->owner;
1704
dce234bc
PP
1705 /* Check if the file has a 4-byte header indicating compression. */
1706 if (info->size > sizeof (header)
1707 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1708 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1709 {
1710 /* Upon decompression, update the buffer and its size. */
1711 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1712 {
1713 zlib_decompress_section (objfile, sectp, &info->buffer,
1714 &info->size);
1715 return;
1716 }
1717 }
4bdf3d34 1718
dce234bc
PP
1719#ifdef HAVE_MMAP
1720 if (pagesize == 0)
1721 pagesize = getpagesize ();
2e276125 1722
dce234bc
PP
1723 /* Only try to mmap sections which are large enough: we don't want to
1724 waste space due to fragmentation. Also, only try mmap for sections
1725 without relocations. */
1726
1727 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1728 {
b315ab21
TG
1729 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1730 MAP_PRIVATE, sectp->filepos,
1731 &info->map_addr, &info->map_len);
dce234bc 1732
b315ab21 1733 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1734 {
be391dca 1735#if HAVE_POSIX_MADVISE
b315ab21 1736 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1737#endif
dce234bc
PP
1738 return;
1739 }
1740 }
1741#endif
1742
1743 /* If we get here, we are a normal, not-compressed section. */
1744 info->buffer = buf
1745 = obstack_alloc (&objfile->objfile_obstack, info->size);
1746
1747 /* When debugging .o files, we may need to apply relocations; see
1748 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1749 We never compress sections in .o files, so we only need to
1750 try this when the section is not compressed. */
ac8035ab 1751 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1752 if (retbuf != NULL)
1753 {
1754 info->buffer = retbuf;
1755 return;
1756 }
1757
1758 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1759 || bfd_bread (buf, info->size, abfd) != info->size)
1760 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1761 bfd_get_filename (abfd));
1762}
1763
9e0ac564
TT
1764/* A helper function that returns the size of a section in a safe way.
1765 If you are positive that the section has been read before using the
1766 size, then it is safe to refer to the dwarf2_section_info object's
1767 "size" field directly. In other cases, you must call this
1768 function, because for compressed sections the size field is not set
1769 correctly until the section has been read. */
1770
1771static bfd_size_type
1772dwarf2_section_size (struct objfile *objfile,
1773 struct dwarf2_section_info *info)
1774{
1775 if (!info->readin)
1776 dwarf2_read_section (objfile, info);
1777 return info->size;
1778}
1779
dce234bc 1780/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1781 SECTION_NAME. */
af34e669 1782
dce234bc 1783void
3017a003
TG
1784dwarf2_get_section_info (struct objfile *objfile,
1785 enum dwarf2_section_enum sect,
dce234bc
PP
1786 asection **sectp, gdb_byte **bufp,
1787 bfd_size_type *sizep)
1788{
1789 struct dwarf2_per_objfile *data
1790 = objfile_data (objfile, dwarf2_objfile_data_key);
1791 struct dwarf2_section_info *info;
a3b2a86b
TT
1792
1793 /* We may see an objfile without any DWARF, in which case we just
1794 return nothing. */
1795 if (data == NULL)
1796 {
1797 *sectp = NULL;
1798 *bufp = NULL;
1799 *sizep = 0;
1800 return;
1801 }
3017a003
TG
1802 switch (sect)
1803 {
1804 case DWARF2_DEBUG_FRAME:
1805 info = &data->frame;
1806 break;
1807 case DWARF2_EH_FRAME:
1808 info = &data->eh_frame;
1809 break;
1810 default:
1811 gdb_assert_not_reached ("unexpected section");
1812 }
dce234bc 1813
9e0ac564 1814 dwarf2_read_section (objfile, info);
dce234bc
PP
1815
1816 *sectp = info->asection;
1817 *bufp = info->buffer;
1818 *sizep = info->size;
1819}
1820
9291a0cd 1821\f
7b9f3c50
DE
1822/* DWARF quick_symbols_functions support. */
1823
1824/* TUs can share .debug_line entries, and there can be a lot more TUs than
1825 unique line tables, so we maintain a separate table of all .debug_line
1826 derived entries to support the sharing.
1827 All the quick functions need is the list of file names. We discard the
1828 line_header when we're done and don't need to record it here. */
1829struct quick_file_names
1830{
1831 /* The offset in .debug_line of the line table. We hash on this. */
1832 unsigned int offset;
1833
1834 /* The number of entries in file_names, real_names. */
1835 unsigned int num_file_names;
1836
1837 /* The file names from the line table, after being run through
1838 file_full_name. */
1839 const char **file_names;
1840
1841 /* The file names from the line table after being run through
1842 gdb_realpath. These are computed lazily. */
1843 const char **real_names;
1844};
1845
1846/* When using the index (and thus not using psymtabs), each CU has an
1847 object of this type. This is used to hold information needed by
1848 the various "quick" methods. */
1849struct dwarf2_per_cu_quick_data
1850{
1851 /* The file table. This can be NULL if there was no file table
1852 or it's currently not read in.
1853 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1854 struct quick_file_names *file_names;
1855
1856 /* The corresponding symbol table. This is NULL if symbols for this
1857 CU have not yet been read. */
1858 struct symtab *symtab;
1859
1860 /* A temporary mark bit used when iterating over all CUs in
1861 expand_symtabs_matching. */
1862 unsigned int mark : 1;
1863
1864 /* True if we've tried to read the file table and found there isn't one.
1865 There will be no point in trying to read it again next time. */
1866 unsigned int no_file_data : 1;
1867};
1868
1869/* Hash function for a quick_file_names. */
1870
1871static hashval_t
1872hash_file_name_entry (const void *e)
1873{
1874 const struct quick_file_names *file_data = e;
1875
1876 return file_data->offset;
1877}
1878
1879/* Equality function for a quick_file_names. */
1880
1881static int
1882eq_file_name_entry (const void *a, const void *b)
1883{
1884 const struct quick_file_names *ea = a;
1885 const struct quick_file_names *eb = b;
1886
1887 return ea->offset == eb->offset;
1888}
1889
1890/* Delete function for a quick_file_names. */
1891
1892static void
1893delete_file_name_entry (void *e)
1894{
1895 struct quick_file_names *file_data = e;
1896 int i;
1897
1898 for (i = 0; i < file_data->num_file_names; ++i)
1899 {
1900 xfree ((void*) file_data->file_names[i]);
1901 if (file_data->real_names)
1902 xfree ((void*) file_data->real_names[i]);
1903 }
1904
1905 /* The space for the struct itself lives on objfile_obstack,
1906 so we don't free it here. */
1907}
1908
1909/* Create a quick_file_names hash table. */
1910
1911static htab_t
1912create_quick_file_names_table (unsigned int nr_initial_entries)
1913{
1914 return htab_create_alloc (nr_initial_entries,
1915 hash_file_name_entry, eq_file_name_entry,
1916 delete_file_name_entry, xcalloc, xfree);
1917}
9291a0cd 1918
918dd910
JK
1919/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1920 have to be created afterwards. You should call age_cached_comp_units after
1921 processing PER_CU->CU. dw2_setup must have been already called. */
1922
1923static void
1924load_cu (struct dwarf2_per_cu_data *per_cu)
1925{
3019eac3 1926 if (per_cu->is_debug_types)
e5fe5e75 1927 load_full_type_unit (per_cu);
918dd910 1928 else
a0f42c21 1929 load_full_comp_unit (per_cu);
918dd910 1930
918dd910 1931 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
1932
1933 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
1934}
1935
a0f42c21 1936/* Read in the symbols for PER_CU. */
2fdf6df6 1937
9291a0cd 1938static void
a0f42c21 1939dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1940{
1941 struct cleanup *back_to;
1942
1943 back_to = make_cleanup (dwarf2_release_queue, NULL);
1944
a0f42c21 1945 queue_comp_unit (per_cu);
9291a0cd 1946
918dd910 1947 load_cu (per_cu);
9291a0cd 1948
a0f42c21 1949 process_queue ();
9291a0cd
TT
1950
1951 /* Age the cache, releasing compilation units that have not
1952 been used recently. */
1953 age_cached_comp_units ();
1954
1955 do_cleanups (back_to);
1956}
1957
1958/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1959 the objfile from which this CU came. Returns the resulting symbol
1960 table. */
2fdf6df6 1961
9291a0cd 1962static struct symtab *
a0f42c21 1963dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1964{
1965 if (!per_cu->v.quick->symtab)
1966 {
1967 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1968 increment_reading_symtab ();
a0f42c21 1969 dw2_do_instantiate_symtab (per_cu);
9291a0cd
TT
1970 do_cleanups (back_to);
1971 }
1972 return per_cu->v.quick->symtab;
1973}
1974
1fd400ff 1975/* Return the CU given its index. */
2fdf6df6 1976
1fd400ff
TT
1977static struct dwarf2_per_cu_data *
1978dw2_get_cu (int index)
1979{
1980 if (index >= dwarf2_per_objfile->n_comp_units)
1981 {
1982 index -= dwarf2_per_objfile->n_comp_units;
d467dd73 1983 return dwarf2_per_objfile->all_type_units[index];
1fd400ff
TT
1984 }
1985 return dwarf2_per_objfile->all_comp_units[index];
1986}
1987
9291a0cd
TT
1988/* A helper function that knows how to read a 64-bit value in a way
1989 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1990 otherwise. */
2fdf6df6 1991
9291a0cd
TT
1992static int
1993extract_cu_value (const char *bytes, ULONGEST *result)
1994{
1995 if (sizeof (ULONGEST) < 8)
1996 {
1997 int i;
1998
1999 /* Ignore the upper 4 bytes if they are all zero. */
2000 for (i = 0; i < 4; ++i)
2001 if (bytes[i + 4] != 0)
2002 return 0;
2003
2004 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2005 }
2006 else
2007 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2008 return 1;
2009}
2010
2011/* Read the CU list from the mapped index, and use it to create all
2012 the CU objects for this objfile. Return 0 if something went wrong,
2013 1 if everything went ok. */
2fdf6df6 2014
9291a0cd 2015static int
1fd400ff
TT
2016create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2017 offset_type cu_list_elements)
9291a0cd
TT
2018{
2019 offset_type i;
9291a0cd
TT
2020
2021 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2022 dwarf2_per_objfile->all_comp_units
2023 = obstack_alloc (&objfile->objfile_obstack,
2024 dwarf2_per_objfile->n_comp_units
2025 * sizeof (struct dwarf2_per_cu_data *));
2026
2027 for (i = 0; i < cu_list_elements; i += 2)
2028 {
2029 struct dwarf2_per_cu_data *the_cu;
2030 ULONGEST offset, length;
2031
2032 if (!extract_cu_value (cu_list, &offset)
2033 || !extract_cu_value (cu_list + 8, &length))
2034 return 0;
2035 cu_list += 2 * 8;
2036
2037 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2038 struct dwarf2_per_cu_data);
b64f50a1 2039 the_cu->offset.sect_off = offset;
9291a0cd
TT
2040 the_cu->length = length;
2041 the_cu->objfile = objfile;
3019eac3 2042 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
9291a0cd
TT
2043 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2044 struct dwarf2_per_cu_quick_data);
2045 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2046 }
2047
2048 return 1;
2049}
2050
1fd400ff 2051/* Create the signatured type hash table from the index. */
673bfd45 2052
1fd400ff 2053static int
673bfd45 2054create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2055 struct dwarf2_section_info *section,
673bfd45
DE
2056 const gdb_byte *bytes,
2057 offset_type elements)
1fd400ff
TT
2058{
2059 offset_type i;
673bfd45 2060 htab_t sig_types_hash;
1fd400ff 2061
d467dd73
DE
2062 dwarf2_per_objfile->n_type_units = elements / 3;
2063 dwarf2_per_objfile->all_type_units
1fd400ff 2064 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2065 dwarf2_per_objfile->n_type_units
1fd400ff
TT
2066 * sizeof (struct dwarf2_per_cu_data *));
2067
673bfd45 2068 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2069
2070 for (i = 0; i < elements; i += 3)
2071 {
52dc124a
DE
2072 struct signatured_type *sig_type;
2073 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2074 void **slot;
2075
2076 if (!extract_cu_value (bytes, &offset)
52dc124a 2077 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
2078 return 0;
2079 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2080 bytes += 3 * 8;
2081
52dc124a 2082 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2083 struct signatured_type);
52dc124a 2084 sig_type->signature = signature;
3019eac3
DE
2085 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2086 sig_type->per_cu.is_debug_types = 1;
2087 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2088 sig_type->per_cu.offset.sect_off = offset;
2089 sig_type->per_cu.objfile = objfile;
2090 sig_type->per_cu.v.quick
1fd400ff
TT
2091 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2092 struct dwarf2_per_cu_quick_data);
2093
52dc124a
DE
2094 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2095 *slot = sig_type;
1fd400ff 2096
52dc124a 2097 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
1fd400ff
TT
2098 }
2099
673bfd45 2100 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2101
2102 return 1;
2103}
2104
9291a0cd
TT
2105/* Read the address map data from the mapped index, and use it to
2106 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2107
9291a0cd
TT
2108static void
2109create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2110{
2111 const gdb_byte *iter, *end;
2112 struct obstack temp_obstack;
2113 struct addrmap *mutable_map;
2114 struct cleanup *cleanup;
2115 CORE_ADDR baseaddr;
2116
2117 obstack_init (&temp_obstack);
2118 cleanup = make_cleanup_obstack_free (&temp_obstack);
2119 mutable_map = addrmap_create_mutable (&temp_obstack);
2120
2121 iter = index->address_table;
2122 end = iter + index->address_table_size;
2123
2124 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2125
2126 while (iter < end)
2127 {
2128 ULONGEST hi, lo, cu_index;
2129 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2130 iter += 8;
2131 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2132 iter += 8;
2133 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2134 iter += 4;
2135
2136 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2137 dw2_get_cu (cu_index));
9291a0cd
TT
2138 }
2139
2140 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2141 &objfile->objfile_obstack);
2142 do_cleanups (cleanup);
2143}
2144
59d7bcaf
JK
2145/* The hash function for strings in the mapped index. This is the same as
2146 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2147 implementation. This is necessary because the hash function is tied to the
2148 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2149 SYMBOL_HASH_NEXT.
2150
2151 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2152
9291a0cd 2153static hashval_t
559a7a62 2154mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2155{
2156 const unsigned char *str = (const unsigned char *) p;
2157 hashval_t r = 0;
2158 unsigned char c;
2159
2160 while ((c = *str++) != 0)
559a7a62
JK
2161 {
2162 if (index_version >= 5)
2163 c = tolower (c);
2164 r = r * 67 + c - 113;
2165 }
9291a0cd
TT
2166
2167 return r;
2168}
2169
2170/* Find a slot in the mapped index INDEX for the object named NAME.
2171 If NAME is found, set *VEC_OUT to point to the CU vector in the
2172 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2173
9291a0cd
TT
2174static int
2175find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2176 offset_type **vec_out)
2177{
0cf03b49
JK
2178 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2179 offset_type hash;
9291a0cd 2180 offset_type slot, step;
559a7a62 2181 int (*cmp) (const char *, const char *);
9291a0cd 2182
0cf03b49
JK
2183 if (current_language->la_language == language_cplus
2184 || current_language->la_language == language_java
2185 || current_language->la_language == language_fortran)
2186 {
2187 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2188 not contain any. */
2189 const char *paren = strchr (name, '(');
2190
2191 if (paren)
2192 {
2193 char *dup;
2194
2195 dup = xmalloc (paren - name + 1);
2196 memcpy (dup, name, paren - name);
2197 dup[paren - name] = 0;
2198
2199 make_cleanup (xfree, dup);
2200 name = dup;
2201 }
2202 }
2203
559a7a62 2204 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2205 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2206 simulate our NAME being searched is also lowercased. */
2207 hash = mapped_index_string_hash ((index->version == 4
2208 && case_sensitivity == case_sensitive_off
2209 ? 5 : index->version),
2210 name);
2211
3876f04e
DE
2212 slot = hash & (index->symbol_table_slots - 1);
2213 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2214 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2215
2216 for (;;)
2217 {
2218 /* Convert a slot number to an offset into the table. */
2219 offset_type i = 2 * slot;
2220 const char *str;
3876f04e 2221 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2222 {
2223 do_cleanups (back_to);
2224 return 0;
2225 }
9291a0cd 2226
3876f04e 2227 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2228 if (!cmp (name, str))
9291a0cd
TT
2229 {
2230 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2231 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2232 do_cleanups (back_to);
9291a0cd
TT
2233 return 1;
2234 }
2235
3876f04e 2236 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2237 }
2238}
2239
2240/* Read the index file. If everything went ok, initialize the "quick"
2241 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2242
9291a0cd
TT
2243static int
2244dwarf2_read_index (struct objfile *objfile)
2245{
9291a0cd
TT
2246 char *addr;
2247 struct mapped_index *map;
b3b272e1 2248 offset_type *metadata;
ac0b195c
KW
2249 const gdb_byte *cu_list;
2250 const gdb_byte *types_list = NULL;
2251 offset_type version, cu_list_elements;
2252 offset_type types_list_elements = 0;
1fd400ff 2253 int i;
9291a0cd 2254
9e0ac564 2255 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2256 return 0;
82430852
JK
2257
2258 /* Older elfutils strip versions could keep the section in the main
2259 executable while splitting it for the separate debug info file. */
2260 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2261 & SEC_HAS_CONTENTS) == 0)
2262 return 0;
2263
9291a0cd
TT
2264 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2265
2266 addr = dwarf2_per_objfile->gdb_index.buffer;
2267 /* Version check. */
1fd400ff 2268 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2269 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2270 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2271 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2272 indices. */
831adc1f 2273 if (version < 4)
481860b3
GB
2274 {
2275 static int warning_printed = 0;
2276 if (!warning_printed)
2277 {
2278 warning (_("Skipping obsolete .gdb_index section in %s."),
2279 objfile->name);
2280 warning_printed = 1;
2281 }
2282 return 0;
2283 }
2284 /* Index version 4 uses a different hash function than index version
2285 5 and later.
2286
2287 Versions earlier than 6 did not emit psymbols for inlined
2288 functions. Using these files will cause GDB not to be able to
2289 set breakpoints on inlined functions by name, so we ignore these
2290 indices unless the --use-deprecated-index-sections command line
2291 option was supplied. */
2292 if (version < 6 && !use_deprecated_index_sections)
2293 {
2294 static int warning_printed = 0;
2295 if (!warning_printed)
2296 {
2297 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2298 "--use-deprecated-index-sections to use them anyway"),
2299 objfile->name);
2300 warning_printed = 1;
2301 }
2302 return 0;
2303 }
2304 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2305 longer backward compatible. */
481860b3 2306 if (version > 6)
594e8718 2307 return 0;
9291a0cd
TT
2308
2309 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2310 map->version = version;
b3b272e1 2311 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2312
2313 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2314
2315 i = 0;
2316 cu_list = addr + MAYBE_SWAP (metadata[i]);
2317 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2318 / 8);
1fd400ff
TT
2319 ++i;
2320
987d643c
TT
2321 types_list = addr + MAYBE_SWAP (metadata[i]);
2322 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2323 - MAYBE_SWAP (metadata[i]))
2324 / 8);
2325 ++i;
1fd400ff
TT
2326
2327 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2328 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2329 - MAYBE_SWAP (metadata[i]));
2330 ++i;
2331
3876f04e
DE
2332 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2333 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2334 - MAYBE_SWAP (metadata[i]))
2335 / (2 * sizeof (offset_type)));
1fd400ff 2336 ++i;
9291a0cd 2337
1fd400ff
TT
2338 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2339
0fefef59
DE
2340 /* Don't use the index if it's empty. */
2341 if (map->symbol_table_slots == 0)
2342 return 0;
2343
1fd400ff
TT
2344 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2345 return 0;
2346
8b70b953
TT
2347 if (types_list_elements)
2348 {
2349 struct dwarf2_section_info *section;
2350
2351 /* We can only handle a single .debug_types when we have an
2352 index. */
2353 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2354 return 0;
2355
2356 section = VEC_index (dwarf2_section_info_def,
2357 dwarf2_per_objfile->types, 0);
2358
2359 if (!create_signatured_type_table_from_index (objfile, section,
2360 types_list,
2361 types_list_elements))
2362 return 0;
2363 }
9291a0cd
TT
2364
2365 create_addrmap_from_index (objfile, map);
2366
2367 dwarf2_per_objfile->index_table = map;
2368 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2369 dwarf2_per_objfile->quick_file_names_table =
2370 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2371
2372 return 1;
2373}
2374
2375/* A helper for the "quick" functions which sets the global
2376 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2377
9291a0cd
TT
2378static void
2379dw2_setup (struct objfile *objfile)
2380{
2381 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2382 gdb_assert (dwarf2_per_objfile);
2383}
2384
dee91e82 2385/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2386
dee91e82
DE
2387static void
2388dw2_get_file_names_reader (const struct die_reader_specs *reader,
2389 gdb_byte *info_ptr,
2390 struct die_info *comp_unit_die,
2391 int has_children,
2392 void *data)
9291a0cd 2393{
dee91e82
DE
2394 struct dwarf2_cu *cu = reader->cu;
2395 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2396 struct objfile *objfile = dwarf2_per_objfile->objfile;
7b9f3c50 2397 struct line_header *lh;
9291a0cd 2398 struct attribute *attr;
dee91e82 2399 int i;
9ff913ba 2400 unsigned int bytes_read;
9291a0cd 2401 char *name, *comp_dir;
7b9f3c50
DE
2402 void **slot;
2403 struct quick_file_names *qfn;
2404 unsigned int line_offset;
9291a0cd 2405
7b9f3c50
DE
2406 lh = NULL;
2407 slot = NULL;
2408 line_offset = 0;
dee91e82
DE
2409
2410 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2411 if (attr)
2412 {
7b9f3c50
DE
2413 struct quick_file_names find_entry;
2414
2415 line_offset = DW_UNSND (attr);
2416
2417 /* We may have already read in this line header (TU line header sharing).
2418 If we have we're done. */
2419 find_entry.offset = line_offset;
2420 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2421 &find_entry, INSERT);
2422 if (*slot != NULL)
2423 {
7b9f3c50 2424 this_cu->v.quick->file_names = *slot;
dee91e82 2425 return;
7b9f3c50
DE
2426 }
2427
3019eac3 2428 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2429 }
2430 if (lh == NULL)
2431 {
7b9f3c50 2432 this_cu->v.quick->no_file_data = 1;
dee91e82 2433 return;
9291a0cd
TT
2434 }
2435
7b9f3c50
DE
2436 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2437 qfn->offset = line_offset;
2438 gdb_assert (slot != NULL);
2439 *slot = qfn;
9291a0cd 2440
dee91e82 2441 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2442
7b9f3c50
DE
2443 qfn->num_file_names = lh->num_file_names;
2444 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2445 lh->num_file_names * sizeof (char *));
9291a0cd 2446 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2447 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2448 qfn->real_names = NULL;
9291a0cd 2449
7b9f3c50 2450 free_line_header (lh);
7b9f3c50
DE
2451
2452 this_cu->v.quick->file_names = qfn;
dee91e82
DE
2453}
2454
2455/* A helper for the "quick" functions which attempts to read the line
2456 table for THIS_CU. */
2457
2458static struct quick_file_names *
2459dw2_get_file_names (struct objfile *objfile,
2460 struct dwarf2_per_cu_data *this_cu)
2461{
2462 if (this_cu->v.quick->file_names != NULL)
2463 return this_cu->v.quick->file_names;
2464 /* If we know there is no line data, no point in looking again. */
2465 if (this_cu->v.quick->no_file_data)
2466 return NULL;
2467
3019eac3
DE
2468 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2469 in the stub for CUs, there's is no need to lookup the DWO file.
2470 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2471 DWO file. */
2472 if (this_cu->is_debug_types)
fd820528 2473 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
3019eac3
DE
2474 else
2475 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2476
2477 if (this_cu->v.quick->no_file_data)
2478 return NULL;
2479 return this_cu->v.quick->file_names;
9291a0cd
TT
2480}
2481
2482/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2483 real path for a given file name from the line table. */
2fdf6df6 2484
9291a0cd 2485static const char *
7b9f3c50
DE
2486dw2_get_real_path (struct objfile *objfile,
2487 struct quick_file_names *qfn, int index)
9291a0cd 2488{
7b9f3c50
DE
2489 if (qfn->real_names == NULL)
2490 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2491 qfn->num_file_names, sizeof (char *));
9291a0cd 2492
7b9f3c50
DE
2493 if (qfn->real_names[index] == NULL)
2494 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2495
7b9f3c50 2496 return qfn->real_names[index];
9291a0cd
TT
2497}
2498
2499static struct symtab *
2500dw2_find_last_source_symtab (struct objfile *objfile)
2501{
2502 int index;
ae2de4f8 2503
9291a0cd
TT
2504 dw2_setup (objfile);
2505 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2506 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2507}
2508
7b9f3c50
DE
2509/* Traversal function for dw2_forget_cached_source_info. */
2510
2511static int
2512dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2513{
7b9f3c50 2514 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2515
7b9f3c50 2516 if (file_data->real_names)
9291a0cd 2517 {
7b9f3c50 2518 int i;
9291a0cd 2519
7b9f3c50 2520 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2521 {
7b9f3c50
DE
2522 xfree ((void*) file_data->real_names[i]);
2523 file_data->real_names[i] = NULL;
9291a0cd
TT
2524 }
2525 }
7b9f3c50
DE
2526
2527 return 1;
2528}
2529
2530static void
2531dw2_forget_cached_source_info (struct objfile *objfile)
2532{
2533 dw2_setup (objfile);
2534
2535 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2536 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2537}
2538
f8eba3c6
TT
2539/* Helper function for dw2_map_symtabs_matching_filename that expands
2540 the symtabs and calls the iterator. */
2541
2542static int
2543dw2_map_expand_apply (struct objfile *objfile,
2544 struct dwarf2_per_cu_data *per_cu,
2545 const char *name,
2546 const char *full_path, const char *real_path,
2547 int (*callback) (struct symtab *, void *),
2548 void *data)
2549{
2550 struct symtab *last_made = objfile->symtabs;
2551
2552 /* Don't visit already-expanded CUs. */
2553 if (per_cu->v.quick->symtab)
2554 return 0;
2555
2556 /* This may expand more than one symtab, and we want to iterate over
2557 all of them. */
a0f42c21 2558 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2559
2560 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2561 objfile->symtabs, last_made);
2562}
2563
2564/* Implementation of the map_symtabs_matching_filename method. */
2565
9291a0cd 2566static int
f8eba3c6
TT
2567dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2568 const char *full_path, const char *real_path,
2569 int (*callback) (struct symtab *, void *),
2570 void *data)
9291a0cd
TT
2571{
2572 int i;
c011a4f4 2573 const char *name_basename = lbasename (name);
4aac40c8
TT
2574 int name_len = strlen (name);
2575 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
2576
2577 dw2_setup (objfile);
ae2de4f8 2578
1fd400ff 2579 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2580 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2581 {
2582 int j;
e254ef6a 2583 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2584 struct quick_file_names *file_data;
9291a0cd 2585
3d7bb9d9 2586 /* We only need to look at symtabs not already expanded. */
e254ef6a 2587 if (per_cu->v.quick->symtab)
9291a0cd
TT
2588 continue;
2589
7b9f3c50
DE
2590 file_data = dw2_get_file_names (objfile, per_cu);
2591 if (file_data == NULL)
9291a0cd
TT
2592 continue;
2593
7b9f3c50 2594 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2595 {
7b9f3c50 2596 const char *this_name = file_data->file_names[j];
9291a0cd 2597
4aac40c8
TT
2598 if (FILENAME_CMP (name, this_name) == 0
2599 || (!is_abs && compare_filenames_for_search (this_name,
2600 name, name_len)))
9291a0cd 2601 {
f8eba3c6
TT
2602 if (dw2_map_expand_apply (objfile, per_cu,
2603 name, full_path, real_path,
2604 callback, data))
2605 return 1;
4aac40c8 2606 }
9291a0cd 2607
c011a4f4
DE
2608 /* Before we invoke realpath, which can get expensive when many
2609 files are involved, do a quick comparison of the basenames. */
2610 if (! basenames_may_differ
2611 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2612 continue;
2613
9291a0cd
TT
2614 if (full_path != NULL)
2615 {
7b9f3c50
DE
2616 const char *this_real_name = dw2_get_real_path (objfile,
2617 file_data, j);
9291a0cd 2618
7b9f3c50 2619 if (this_real_name != NULL
4aac40c8
TT
2620 && (FILENAME_CMP (full_path, this_real_name) == 0
2621 || (!is_abs
2622 && compare_filenames_for_search (this_real_name,
2623 name, name_len))))
9291a0cd 2624 {
f8eba3c6
TT
2625 if (dw2_map_expand_apply (objfile, per_cu,
2626 name, full_path, real_path,
2627 callback, data))
2628 return 1;
9291a0cd
TT
2629 }
2630 }
2631
2632 if (real_path != NULL)
2633 {
7b9f3c50
DE
2634 const char *this_real_name = dw2_get_real_path (objfile,
2635 file_data, j);
9291a0cd 2636
7b9f3c50 2637 if (this_real_name != NULL
4aac40c8
TT
2638 && (FILENAME_CMP (real_path, this_real_name) == 0
2639 || (!is_abs
2640 && compare_filenames_for_search (this_real_name,
2641 name, name_len))))
9291a0cd 2642 {
f8eba3c6
TT
2643 if (dw2_map_expand_apply (objfile, per_cu,
2644 name, full_path, real_path,
2645 callback, data))
2646 return 1;
9291a0cd
TT
2647 }
2648 }
2649 }
2650 }
2651
9291a0cd
TT
2652 return 0;
2653}
2654
2655static struct symtab *
2656dw2_lookup_symbol (struct objfile *objfile, int block_index,
2657 const char *name, domain_enum domain)
2658{
774b6a14 2659 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2660 instead. */
2661 return NULL;
2662}
2663
2664/* A helper function that expands all symtabs that hold an object
2665 named NAME. */
2fdf6df6 2666
9291a0cd
TT
2667static void
2668dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2669{
2670 dw2_setup (objfile);
2671
ae2de4f8 2672 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2673 if (dwarf2_per_objfile->index_table)
2674 {
2675 offset_type *vec;
2676
2677 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2678 name, &vec))
2679 {
2680 offset_type i, len = MAYBE_SWAP (*vec);
2681 for (i = 0; i < len; ++i)
2682 {
2683 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2684 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2685
a0f42c21 2686 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2687 }
2688 }
2689 }
2690}
2691
774b6a14
TT
2692static void
2693dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2694 enum block_enum block_kind, const char *name,
774b6a14 2695 domain_enum domain)
9291a0cd 2696{
774b6a14 2697 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2698}
2699
2700static void
2701dw2_print_stats (struct objfile *objfile)
2702{
2703 int i, count;
2704
2705 dw2_setup (objfile);
2706 count = 0;
1fd400ff 2707 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2708 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2709 {
e254ef6a 2710 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2711
e254ef6a 2712 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2713 ++count;
2714 }
2715 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2716}
2717
2718static void
2719dw2_dump (struct objfile *objfile)
2720{
2721 /* Nothing worth printing. */
2722}
2723
2724static void
2725dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2726 struct section_offsets *delta)
2727{
2728 /* There's nothing to relocate here. */
2729}
2730
2731static void
2732dw2_expand_symtabs_for_function (struct objfile *objfile,
2733 const char *func_name)
2734{
2735 dw2_do_expand_symtabs_matching (objfile, func_name);
2736}
2737
2738static void
2739dw2_expand_all_symtabs (struct objfile *objfile)
2740{
2741 int i;
2742
2743 dw2_setup (objfile);
1fd400ff
TT
2744
2745 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2746 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2747 {
e254ef6a 2748 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2749
a0f42c21 2750 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2751 }
2752}
2753
2754static void
2755dw2_expand_symtabs_with_filename (struct objfile *objfile,
2756 const char *filename)
2757{
2758 int i;
2759
2760 dw2_setup (objfile);
d4637a04
DE
2761
2762 /* We don't need to consider type units here.
2763 This is only called for examining code, e.g. expand_line_sal.
2764 There can be an order of magnitude (or more) more type units
2765 than comp units, and we avoid them if we can. */
2766
2767 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2768 {
2769 int j;
e254ef6a 2770 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2771 struct quick_file_names *file_data;
9291a0cd 2772
3d7bb9d9 2773 /* We only need to look at symtabs not already expanded. */
e254ef6a 2774 if (per_cu->v.quick->symtab)
9291a0cd
TT
2775 continue;
2776
7b9f3c50
DE
2777 file_data = dw2_get_file_names (objfile, per_cu);
2778 if (file_data == NULL)
9291a0cd
TT
2779 continue;
2780
7b9f3c50 2781 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2782 {
7b9f3c50 2783 const char *this_name = file_data->file_names[j];
1ef75ecc 2784 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2785 {
a0f42c21 2786 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2787 break;
2788 }
2789 }
2790 }
2791}
2792
dd786858 2793static const char *
9291a0cd
TT
2794dw2_find_symbol_file (struct objfile *objfile, const char *name)
2795{
e254ef6a 2796 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2797 offset_type *vec;
7b9f3c50 2798 struct quick_file_names *file_data;
9291a0cd
TT
2799
2800 dw2_setup (objfile);
2801
ae2de4f8 2802 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2803 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2804 {
2805 struct symtab *s;
2806
2807 ALL_OBJFILE_SYMTABS (objfile, s)
2808 if (s->primary)
2809 {
2810 struct blockvector *bv = BLOCKVECTOR (s);
2811 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2812 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2813
2814 if (sym)
2815 return sym->symtab->filename;
2816 }
2817 return NULL;
2818 }
9291a0cd
TT
2819
2820 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2821 name, &vec))
2822 return NULL;
2823
2824 /* Note that this just looks at the very first one named NAME -- but
2825 actually we are looking for a function. find_main_filename
2826 should be rewritten so that it doesn't require a custom hook. It
2827 could just use the ordinary symbol tables. */
2828 /* vec[0] is the length, which must always be >0. */
e254ef6a 2829 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2830
7b9f3c50 2831 file_data = dw2_get_file_names (objfile, per_cu);
943cb756
DE
2832 if (file_data == NULL
2833 || file_data->num_file_names == 0)
9291a0cd
TT
2834 return NULL;
2835
7b9f3c50 2836 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2837}
2838
2839static void
40658b94
PH
2840dw2_map_matching_symbols (const char * name, domain_enum namespace,
2841 struct objfile *objfile, int global,
2842 int (*callback) (struct block *,
2843 struct symbol *, void *),
2edb89d3
JK
2844 void *data, symbol_compare_ftype *match,
2845 symbol_compare_ftype *ordered_compare)
9291a0cd 2846{
40658b94 2847 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2848 current language is Ada for a non-Ada objfile using GNU index. As Ada
2849 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2850}
2851
2852static void
f8eba3c6
TT
2853dw2_expand_symtabs_matching
2854 (struct objfile *objfile,
2855 int (*file_matcher) (const char *, void *),
e078317b 2856 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
2857 enum search_domain kind,
2858 void *data)
9291a0cd
TT
2859{
2860 int i;
2861 offset_type iter;
4b5246aa 2862 struct mapped_index *index;
9291a0cd
TT
2863
2864 dw2_setup (objfile);
ae2de4f8
DE
2865
2866 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2867 if (!dwarf2_per_objfile->index_table)
2868 return;
4b5246aa 2869 index = dwarf2_per_objfile->index_table;
9291a0cd 2870
7b08b9eb 2871 if (file_matcher != NULL)
24c79950
TT
2872 {
2873 struct cleanup *cleanup;
2874 htab_t visited_found, visited_not_found;
2875
2876 visited_found = htab_create_alloc (10,
2877 htab_hash_pointer, htab_eq_pointer,
2878 NULL, xcalloc, xfree);
2879 cleanup = make_cleanup_htab_delete (visited_found);
2880 visited_not_found = htab_create_alloc (10,
2881 htab_hash_pointer, htab_eq_pointer,
2882 NULL, xcalloc, xfree);
2883 make_cleanup_htab_delete (visited_not_found);
2884
2885 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2886 + dwarf2_per_objfile->n_type_units); ++i)
2887 {
2888 int j;
2889 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2890 struct quick_file_names *file_data;
2891 void **slot;
7b08b9eb 2892
24c79950 2893 per_cu->v.quick->mark = 0;
3d7bb9d9 2894
24c79950
TT
2895 /* We only need to look at symtabs not already expanded. */
2896 if (per_cu->v.quick->symtab)
2897 continue;
7b08b9eb 2898
24c79950
TT
2899 file_data = dw2_get_file_names (objfile, per_cu);
2900 if (file_data == NULL)
2901 continue;
7b08b9eb 2902
24c79950
TT
2903 if (htab_find (visited_not_found, file_data) != NULL)
2904 continue;
2905 else if (htab_find (visited_found, file_data) != NULL)
2906 {
2907 per_cu->v.quick->mark = 1;
2908 continue;
2909 }
2910
2911 for (j = 0; j < file_data->num_file_names; ++j)
2912 {
2913 if (file_matcher (file_data->file_names[j], data))
2914 {
2915 per_cu->v.quick->mark = 1;
2916 break;
2917 }
2918 }
2919
2920 slot = htab_find_slot (per_cu->v.quick->mark
2921 ? visited_found
2922 : visited_not_found,
2923 file_data, INSERT);
2924 *slot = file_data;
2925 }
2926
2927 do_cleanups (cleanup);
2928 }
9291a0cd 2929
3876f04e 2930 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2931 {
2932 offset_type idx = 2 * iter;
2933 const char *name;
2934 offset_type *vec, vec_len, vec_idx;
2935
3876f04e 2936 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2937 continue;
2938
3876f04e 2939 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 2940
e078317b 2941 if (! (*name_matcher) (name, data))
9291a0cd
TT
2942 continue;
2943
2944 /* The name was matched, now expand corresponding CUs that were
2945 marked. */
4b5246aa 2946 vec = (offset_type *) (index->constant_pool
3876f04e 2947 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2948 vec_len = MAYBE_SWAP (vec[0]);
2949 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2950 {
e254ef6a 2951 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2952
e254ef6a 2953 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2954 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 2955 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2956 }
2957 }
2958}
2959
2960static struct symtab *
2961dw2_find_pc_sect_symtab (struct objfile *objfile,
2962 struct minimal_symbol *msymbol,
2963 CORE_ADDR pc,
2964 struct obj_section *section,
2965 int warn_if_readin)
2966{
2967 struct dwarf2_per_cu_data *data;
2968
2969 dw2_setup (objfile);
2970
2971 if (!objfile->psymtabs_addrmap)
2972 return NULL;
2973
2974 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2975 if (!data)
2976 return NULL;
2977
2978 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 2979 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
2980 paddress (get_objfile_arch (objfile), pc));
2981
a0f42c21 2982 return dw2_instantiate_symtab (data);
9291a0cd
TT
2983}
2984
9291a0cd 2985static void
44b13c5a 2986dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 2987 void *data, int need_fullname)
9291a0cd
TT
2988{
2989 int i;
24c79950
TT
2990 struct cleanup *cleanup;
2991 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
2992 NULL, xcalloc, xfree);
9291a0cd 2993
24c79950 2994 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 2995 dw2_setup (objfile);
ae2de4f8 2996
24c79950
TT
2997 /* We can ignore file names coming from already-expanded CUs. */
2998 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2999 + dwarf2_per_objfile->n_type_units); ++i)
3000 {
3001 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3002
3003 if (per_cu->v.quick->symtab)
3004 {
3005 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3006 INSERT);
3007
3008 *slot = per_cu->v.quick->file_names;
3009 }
3010 }
3011
1fd400ff 3012 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3013 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
3014 {
3015 int j;
e254ef6a 3016 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3017 struct quick_file_names *file_data;
24c79950 3018 void **slot;
9291a0cd 3019
3d7bb9d9 3020 /* We only need to look at symtabs not already expanded. */
e254ef6a 3021 if (per_cu->v.quick->symtab)
9291a0cd
TT
3022 continue;
3023
7b9f3c50
DE
3024 file_data = dw2_get_file_names (objfile, per_cu);
3025 if (file_data == NULL)
9291a0cd
TT
3026 continue;
3027
24c79950
TT
3028 slot = htab_find_slot (visited, file_data, INSERT);
3029 if (*slot)
3030 {
3031 /* Already visited. */
3032 continue;
3033 }
3034 *slot = file_data;
3035
7b9f3c50 3036 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3037 {
74e2f255
DE
3038 const char *this_real_name;
3039
3040 if (need_fullname)
3041 this_real_name = dw2_get_real_path (objfile, file_data, j);
3042 else
3043 this_real_name = NULL;
7b9f3c50 3044 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3045 }
3046 }
24c79950
TT
3047
3048 do_cleanups (cleanup);
9291a0cd
TT
3049}
3050
3051static int
3052dw2_has_symbols (struct objfile *objfile)
3053{
3054 return 1;
3055}
3056
3057const struct quick_symbol_functions dwarf2_gdb_index_functions =
3058{
3059 dw2_has_symbols,
3060 dw2_find_last_source_symtab,
3061 dw2_forget_cached_source_info,
f8eba3c6 3062 dw2_map_symtabs_matching_filename,
9291a0cd 3063 dw2_lookup_symbol,
774b6a14 3064 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3065 dw2_print_stats,
3066 dw2_dump,
3067 dw2_relocate,
3068 dw2_expand_symtabs_for_function,
3069 dw2_expand_all_symtabs,
3070 dw2_expand_symtabs_with_filename,
3071 dw2_find_symbol_file,
40658b94 3072 dw2_map_matching_symbols,
9291a0cd
TT
3073 dw2_expand_symtabs_matching,
3074 dw2_find_pc_sect_symtab,
9291a0cd
TT
3075 dw2_map_symbol_filenames
3076};
3077
3078/* Initialize for reading DWARF for this objfile. Return 0 if this
3079 file will use psymtabs, or 1 if using the GNU index. */
3080
3081int
3082dwarf2_initialize_objfile (struct objfile *objfile)
3083{
3084 /* If we're about to read full symbols, don't bother with the
3085 indices. In this case we also don't care if some other debug
3086 format is making psymtabs, because they are all about to be
3087 expanded anyway. */
3088 if ((objfile->flags & OBJF_READNOW))
3089 {
3090 int i;
3091
3092 dwarf2_per_objfile->using_index = 1;
3093 create_all_comp_units (objfile);
0e50663e 3094 create_all_type_units (objfile);
7b9f3c50
DE
3095 dwarf2_per_objfile->quick_file_names_table =
3096 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3097
1fd400ff 3098 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3099 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3100 {
e254ef6a 3101 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3102
e254ef6a
DE
3103 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3104 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3105 }
3106
3107 /* Return 1 so that gdb sees the "quick" functions. However,
3108 these functions will be no-ops because we will have expanded
3109 all symtabs. */
3110 return 1;
3111 }
3112
3113 if (dwarf2_read_index (objfile))
3114 return 1;
3115
9291a0cd
TT
3116 return 0;
3117}
3118
3119\f
3120
dce234bc
PP
3121/* Build a partial symbol table. */
3122
3123void
f29dff0a 3124dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3125{
f29dff0a 3126 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3127 {
3128 init_psymbol_list (objfile, 1024);
3129 }
3130
d146bf1e 3131 dwarf2_build_psymtabs_hard (objfile);
c906108c 3132}
c906108c 3133
45452591
DE
3134/* Return TRUE if OFFSET is within CU_HEADER. */
3135
3136static inline int
b64f50a1 3137offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3138{
b64f50a1
JK
3139 sect_offset bottom = { cu_header->offset.sect_off };
3140 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3141 + cu_header->initial_length_size) };
9a619af0 3142
b64f50a1 3143 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3144}
3145
93311388
DE
3146/* Read in the comp unit header information from the debug_info at info_ptr.
3147 NOTE: This leaves members offset, first_die_offset to be filled in
3148 by the caller. */
107d2387 3149
fe1b8b76 3150static gdb_byte *
107d2387 3151read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3152 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3153{
3154 int signed_addr;
891d2f0b 3155 unsigned int bytes_read;
c764a876
DE
3156
3157 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3158 cu_header->initial_length_size = bytes_read;
3159 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3160 info_ptr += bytes_read;
107d2387
AC
3161 cu_header->version = read_2_bytes (abfd, info_ptr);
3162 info_ptr += 2;
b64f50a1
JK
3163 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3164 &bytes_read);
613e1657 3165 info_ptr += bytes_read;
107d2387
AC
3166 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3167 info_ptr += 1;
3168 signed_addr = bfd_get_sign_extend_vma (abfd);
3169 if (signed_addr < 0)
8e65ff28 3170 internal_error (__FILE__, __LINE__,
e2e0b3e5 3171 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3172 cu_header->signed_addr_p = signed_addr;
c764a876 3173
107d2387
AC
3174 return info_ptr;
3175}
3176
9ff913ba
DE
3177/* Subroutine of read_and_check_comp_unit_head and
3178 read_and_check_type_unit_head to simplify them.
3179 Perform various error checking on the header. */
3180
3181static void
3182error_check_comp_unit_head (struct comp_unit_head *header,
3183 struct dwarf2_section_info *section)
3184{
3185 bfd *abfd = section->asection->owner;
3186 const char *filename = bfd_get_filename (abfd);
3187
3188 if (header->version != 2 && header->version != 3 && header->version != 4)
3189 error (_("Dwarf Error: wrong version in compilation unit header "
3190 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3191 filename);
3192
b64f50a1 3193 if (header->abbrev_offset.sect_off
9ff913ba
DE
3194 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3195 &dwarf2_per_objfile->abbrev))
3196 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3197 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3198 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3199 filename);
3200
3201 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3202 avoid potential 32-bit overflow. */
b64f50a1 3203 if (((unsigned long) header->offset.sect_off
9ff913ba
DE
3204 + header->length + header->initial_length_size)
3205 > section->size)
3206 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3207 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3208 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3209 filename);
3210}
3211
3212/* Read in a CU/TU header and perform some basic error checking.
3213 The contents of the header are stored in HEADER.
3214 The result is a pointer to the start of the first DIE. */
adabb602 3215
fe1b8b76 3216static gdb_byte *
9ff913ba
DE
3217read_and_check_comp_unit_head (struct comp_unit_head *header,
3218 struct dwarf2_section_info *section,
3219 gdb_byte *info_ptr,
3220 int is_debug_types_section)
72bf9492 3221{
fe1b8b76 3222 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3223 bfd *abfd = section->asection->owner;
72bf9492 3224
b64f50a1 3225 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3226
72bf9492
DJ
3227 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3228
460c1c54
CC
3229 /* If we're reading a type unit, skip over the signature and
3230 type_offset fields. */
b0df02fd 3231 if (is_debug_types_section)
460c1c54
CC
3232 info_ptr += 8 /*signature*/ + header->offset_size;
3233
b64f50a1 3234 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3235
9ff913ba 3236 error_check_comp_unit_head (header, section);
72bf9492
DJ
3237
3238 return info_ptr;
3239}
3240
348e048f
DE
3241/* Read in the types comp unit header information from .debug_types entry at
3242 types_ptr. The result is a pointer to one past the end of the header. */
3243
3244static gdb_byte *
9ff913ba
DE
3245read_and_check_type_unit_head (struct comp_unit_head *header,
3246 struct dwarf2_section_info *section,
3247 gdb_byte *info_ptr,
dee91e82
DE
3248 ULONGEST *signature,
3249 cu_offset *type_offset_in_tu)
348e048f 3250{
9ff913ba
DE
3251 gdb_byte *beg_of_comp_unit = info_ptr;
3252 bfd *abfd = section->asection->owner;
348e048f 3253
b64f50a1 3254 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3255
9ff913ba 3256 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3257
9ff913ba
DE
3258 /* If we're reading a type unit, skip over the signature and
3259 type_offset fields. */
3260 if (signature != NULL)
3261 *signature = read_8_bytes (abfd, info_ptr);
3262 info_ptr += 8;
dee91e82
DE
3263 if (type_offset_in_tu != NULL)
3264 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3265 header->offset_size);
9ff913ba
DE
3266 info_ptr += header->offset_size;
3267
b64f50a1 3268 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3269
9ff913ba
DE
3270 error_check_comp_unit_head (header, section);
3271
3272 return info_ptr;
348e048f
DE
3273}
3274
aaa75496
JB
3275/* Allocate a new partial symtab for file named NAME and mark this new
3276 partial symtab as being an include of PST. */
3277
3278static void
3279dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3280 struct objfile *objfile)
3281{
3282 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3283
3284 subpst->section_offsets = pst->section_offsets;
3285 subpst->textlow = 0;
3286 subpst->texthigh = 0;
3287
3288 subpst->dependencies = (struct partial_symtab **)
3289 obstack_alloc (&objfile->objfile_obstack,
3290 sizeof (struct partial_symtab *));
3291 subpst->dependencies[0] = pst;
3292 subpst->number_of_dependencies = 1;
3293
3294 subpst->globals_offset = 0;
3295 subpst->n_global_syms = 0;
3296 subpst->statics_offset = 0;
3297 subpst->n_static_syms = 0;
3298 subpst->symtab = NULL;
3299 subpst->read_symtab = pst->read_symtab;
3300 subpst->readin = 0;
3301
3302 /* No private part is necessary for include psymtabs. This property
3303 can be used to differentiate between such include psymtabs and
10b3939b 3304 the regular ones. */
58a9656e 3305 subpst->read_symtab_private = NULL;
aaa75496
JB
3306}
3307
3308/* Read the Line Number Program data and extract the list of files
3309 included by the source file represented by PST. Build an include
d85a05f0 3310 partial symtab for each of these included files. */
aaa75496
JB
3311
3312static void
3313dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
3314 struct die_info *die,
3315 struct partial_symtab *pst)
aaa75496 3316{
d85a05f0
DJ
3317 struct line_header *lh = NULL;
3318 struct attribute *attr;
aaa75496 3319
d85a05f0
DJ
3320 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3321 if (attr)
3019eac3 3322 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
3323 if (lh == NULL)
3324 return; /* No linetable, so no includes. */
3325
c6da4cef 3326 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3327 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3328
3329 free_line_header (lh);
3330}
3331
348e048f 3332static hashval_t
52dc124a 3333hash_signatured_type (const void *item)
348e048f 3334{
52dc124a 3335 const struct signatured_type *sig_type = item;
9a619af0 3336
348e048f 3337 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 3338 return sig_type->signature;
348e048f
DE
3339}
3340
3341static int
52dc124a 3342eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
3343{
3344 const struct signatured_type *lhs = item_lhs;
3345 const struct signatured_type *rhs = item_rhs;
9a619af0 3346
348e048f
DE
3347 return lhs->signature == rhs->signature;
3348}
3349
1fd400ff
TT
3350/* Allocate a hash table for signatured types. */
3351
3352static htab_t
673bfd45 3353allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3354{
3355 return htab_create_alloc_ex (41,
52dc124a
DE
3356 hash_signatured_type,
3357 eq_signatured_type,
1fd400ff
TT
3358 NULL,
3359 &objfile->objfile_obstack,
3360 hashtab_obstack_allocate,
3361 dummy_obstack_deallocate);
3362}
3363
d467dd73 3364/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3365
3366static int
d467dd73 3367add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3368{
3369 struct signatured_type *sigt = *slot;
3370 struct dwarf2_per_cu_data ***datap = datum;
3371
3372 **datap = &sigt->per_cu;
3373 ++*datap;
3374
3375 return 1;
3376}
3377
3019eac3
DE
3378/* Create the hash table of all entries in the .debug_types section.
3379 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3380 The result is a pointer to the hash table or NULL if there are
3381 no types. */
348e048f 3382
3019eac3
DE
3383static htab_t
3384create_debug_types_hash_table (struct dwo_file *dwo_file,
3385 VEC (dwarf2_section_info_def) *types)
348e048f 3386{
3019eac3 3387 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 3388 htab_t types_htab = NULL;
8b70b953
TT
3389 int ix;
3390 struct dwarf2_section_info *section;
348e048f 3391
3019eac3
DE
3392 if (VEC_empty (dwarf2_section_info_def, types))
3393 return NULL;
348e048f 3394
8b70b953 3395 for (ix = 0;
3019eac3 3396 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
3397 ++ix)
3398 {
3019eac3 3399 bfd *abfd;
8b70b953 3400 gdb_byte *info_ptr, *end_ptr;
348e048f 3401
8b70b953
TT
3402 dwarf2_read_section (objfile, section);
3403 info_ptr = section->buffer;
348e048f 3404
8b70b953
TT
3405 if (info_ptr == NULL)
3406 continue;
348e048f 3407
3019eac3
DE
3408 /* We can't set abfd until now because the section may be empty or
3409 not present, in which case section->asection will be NULL. */
3410 abfd = section->asection->owner;
3411
8b70b953 3412 if (types_htab == NULL)
3019eac3
DE
3413 {
3414 if (dwo_file)
3415 types_htab = allocate_dwo_unit_table (objfile);
3416 else
3417 types_htab = allocate_signatured_type_table (objfile);
3418 }
348e048f 3419
8b70b953 3420 if (dwarf2_die_debug)
dee91e82
DE
3421 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3422 bfd_get_filename (abfd));
3423
3424 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3425 because we don't need to read any dies: the signature is in the
3426 header. */
8b70b953
TT
3427
3428 end_ptr = info_ptr + section->size;
3429 while (info_ptr < end_ptr)
3430 {
b64f50a1 3431 sect_offset offset;
3019eac3 3432 cu_offset type_offset_in_tu;
8b70b953 3433 ULONGEST signature;
52dc124a 3434 struct signatured_type *sig_type;
3019eac3 3435 struct dwo_unit *dwo_tu;
8b70b953
TT
3436 void **slot;
3437 gdb_byte *ptr = info_ptr;
9ff913ba 3438 struct comp_unit_head header;
dee91e82 3439 unsigned int length;
348e048f 3440
b64f50a1 3441 offset.sect_off = ptr - section->buffer;
348e048f 3442
8b70b953 3443 /* We need to read the type's signature in order to build the hash
9ff913ba 3444 table, but we don't need anything else just yet. */
348e048f 3445
9ff913ba 3446 ptr = read_and_check_type_unit_head (&header, section, ptr,
3019eac3 3447 &signature, &type_offset_in_tu);
6caca83c 3448
dee91e82
DE
3449 length = header.initial_length_size + header.length;
3450
6caca83c 3451 /* Skip dummy type units. */
dee91e82
DE
3452 if (ptr >= info_ptr + length
3453 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 3454 {
dee91e82 3455 info_ptr += header.initial_length_size + header.length;
6caca83c
CC
3456 continue;
3457 }
8b70b953 3458
3019eac3
DE
3459 if (dwo_file)
3460 {
3461 sig_type = NULL;
3462 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3463 struct dwo_unit);
3464 dwo_tu->dwo_file = dwo_file;
3465 dwo_tu->signature = signature;
3466 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3467 dwo_tu->info_or_types_section = section;
3468 dwo_tu->offset = offset;
3469 dwo_tu->length = length;
3470 }
3471 else
3472 {
3473 /* N.B.: type_offset is not usable if this type uses a DWO file.
3474 The real type_offset is in the DWO file. */
3475 dwo_tu = NULL;
3476 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3477 struct signatured_type);
3478 sig_type->signature = signature;
3479 sig_type->type_offset_in_tu = type_offset_in_tu;
3480 sig_type->per_cu.objfile = objfile;
3481 sig_type->per_cu.is_debug_types = 1;
3482 sig_type->per_cu.info_or_types_section = section;
3483 sig_type->per_cu.offset = offset;
3484 sig_type->per_cu.length = length;
3485 }
8b70b953 3486
3019eac3
DE
3487 slot = htab_find_slot (types_htab,
3488 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3489 INSERT);
8b70b953
TT
3490 gdb_assert (slot != NULL);
3491 if (*slot != NULL)
3492 {
3019eac3
DE
3493 sect_offset dup_offset;
3494
3495 if (dwo_file)
3496 {
3497 const struct dwo_unit *dup_tu = *slot;
3498
3499 dup_offset = dup_tu->offset;
3500 }
3501 else
3502 {
3503 const struct signatured_type *dup_tu = *slot;
3504
3505 dup_offset = dup_tu->per_cu.offset;
3506 }
b3c8eb43 3507
8b70b953
TT
3508 complaint (&symfile_complaints,
3509 _("debug type entry at offset 0x%x is duplicate to the "
3510 "entry at offset 0x%x, signature 0x%s"),
3019eac3 3511 offset.sect_off, dup_offset.sect_off,
8b70b953 3512 phex (signature, sizeof (signature)));
8b70b953 3513 }
3019eac3 3514 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 3515
8b70b953
TT
3516 if (dwarf2_die_debug)
3517 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
3518 offset.sect_off,
3519 phex (signature, sizeof (signature)));
348e048f 3520
dee91e82 3521 info_ptr += length;
8b70b953 3522 }
348e048f
DE
3523 }
3524
3019eac3
DE
3525 return types_htab;
3526}
3527
3528/* Create the hash table of all entries in the .debug_types section,
3529 and initialize all_type_units.
3530 The result is zero if there is an error (e.g. missing .debug_types section),
3531 otherwise non-zero. */
3532
3533static int
3534create_all_type_units (struct objfile *objfile)
3535{
3536 htab_t types_htab;
3537 struct dwarf2_per_cu_data **iter;
3538
3539 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3540 if (types_htab == NULL)
3541 {
3542 dwarf2_per_objfile->signatured_types = NULL;
3543 return 0;
3544 }
3545
348e048f
DE
3546 dwarf2_per_objfile->signatured_types = types_htab;
3547
d467dd73
DE
3548 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3549 dwarf2_per_objfile->all_type_units
1fd400ff 3550 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3551 dwarf2_per_objfile->n_type_units
1fd400ff 3552 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3553 iter = &dwarf2_per_objfile->all_type_units[0];
3554 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3555 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3556 == dwarf2_per_objfile->n_type_units);
1fd400ff 3557
348e048f
DE
3558 return 1;
3559}
3560
380bca97 3561/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 3562 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
3563
3564static struct signatured_type *
e319fa28 3565lookup_signatured_type (ULONGEST sig)
348e048f
DE
3566{
3567 struct signatured_type find_entry, *entry;
3568
3569 if (dwarf2_per_objfile->signatured_types == NULL)
3570 {
3571 complaint (&symfile_complaints,
55f1336d 3572 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 3573 return NULL;
348e048f
DE
3574 }
3575
3576 find_entry.signature = sig;
3577 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3578 return entry;
3579}
3580
d85a05f0
DJ
3581/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3582
3583static void
3584init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 3585 struct dwarf2_cu *cu,
3019eac3
DE
3586 struct dwarf2_section_info *section,
3587 struct dwo_file *dwo_file)
d85a05f0 3588{
fceca515 3589 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 3590 reader->abfd = section->asection->owner;
d85a05f0 3591 reader->cu = cu;
3019eac3 3592 reader->dwo_file = dwo_file;
dee91e82
DE
3593 reader->die_section = section;
3594 reader->buffer = section->buffer;
d85a05f0
DJ
3595}
3596
3597/* Find the base address of the compilation unit for range lists and
3598 location lists. It will normally be specified by DW_AT_low_pc.
3599 In DWARF-3 draft 4, the base address could be overridden by
3600 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3601 compilation units with discontinuous ranges. */
3602
3603static void
3604dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3605{
3606 struct attribute *attr;
3607
3608 cu->base_known = 0;
3609 cu->base_address = 0;
3610
3611 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3612 if (attr)
3613 {
3614 cu->base_address = DW_ADDR (attr);
3615 cu->base_known = 1;
3616 }
3617 else
3618 {
3619 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3620 if (attr)
3621 {
3622 cu->base_address = DW_ADDR (attr);
3623 cu->base_known = 1;
3624 }
3625 }
3626}
3627
fd820528 3628/* Initialize a CU (or TU) and read its DIEs.
3019eac3 3629 If the CU defers to a DWO file, read the DWO file as well.
dee91e82
DE
3630
3631 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3632 Otherwise, a new CU is allocated with xmalloc.
3633
3634 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3635 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3636
3637 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 3638 linker) then DIE_READER_FUNC will not get called. */
aaa75496 3639
70221824 3640static void
fd820528
DE
3641init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3642 int use_existing_cu, int keep,
3643 die_reader_func_ftype *die_reader_func,
3644 void *data)
c906108c 3645{
dee91e82 3646 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
3647 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3648 bfd *abfd = section->asection->owner;
dee91e82
DE
3649 struct dwarf2_cu *cu;
3650 gdb_byte *begin_info_ptr, *info_ptr;
3651 struct die_reader_specs reader;
d85a05f0 3652 struct die_info *comp_unit_die;
dee91e82 3653 int has_children;
d85a05f0 3654 struct attribute *attr;
dee91e82
DE
3655 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3656 struct signatured_type *sig_type = NULL;
c906108c 3657
dee91e82
DE
3658 if (use_existing_cu)
3659 gdb_assert (keep);
23745b47 3660
dee91e82
DE
3661 cleanups = make_cleanup (null_cleanup, NULL);
3662
3663 /* This is cheap if the section is already read in. */
3664 dwarf2_read_section (objfile, section);
3665
3666 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3667
3668 if (use_existing_cu && this_cu->cu != NULL)
3669 {
3670 cu = this_cu->cu;
3671 info_ptr += cu->header.first_die_offset.cu_off;
3672 }
3673 else
3674 {
3675 /* If !use_existing_cu, this_cu->cu must be NULL. */
3676 gdb_assert (this_cu->cu == NULL);
3677
3678 cu = xmalloc (sizeof (*cu));
3679 init_one_comp_unit (cu, this_cu);
3680
3681 /* If an error occurs while loading, release our storage. */
3682 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3683
3019eac3 3684 if (this_cu->is_debug_types)
dee91e82
DE
3685 {
3686 ULONGEST signature;
3687
3688 info_ptr = read_and_check_type_unit_head (&cu->header,
3689 section, info_ptr,
3690 &signature, NULL);
3691
3692 /* There's no way to get from PER_CU to its containing
3693 struct signatured_type.
3694 But we have the signature so we can use that. */
3695 sig_type = lookup_signatured_type (signature);
3696 /* We've already scanned all the signatured types,
3697 this must succeed. */
3698 gdb_assert (sig_type != NULL);
3699 gdb_assert (&sig_type->per_cu == this_cu);
3700 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3701
3702 /* LENGTH has not been set yet for type units. */
3703 this_cu->length = cu->header.length + cu->header.initial_length_size;
3019eac3
DE
3704
3705 /* Establish the type offset that can be used to lookup the type. */
3706 sig_type->type_offset_in_section.sect_off =
3707 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
3708 }
3709 else
3710 {
3711 info_ptr = read_and_check_comp_unit_head (&cu->header,
3712 section, info_ptr, 0);
3713
3714 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3715 gdb_assert (this_cu->length
3716 == cu->header.length + cu->header.initial_length_size);
3717 }
3718 }
10b3939b 3719
6caca83c 3720 /* Skip dummy compilation units. */
dee91e82 3721 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
3722 || peek_abbrev_code (abfd, info_ptr) == 0)
3723 {
dee91e82 3724 do_cleanups (cleanups);
21b2bd31 3725 return;
6caca83c
CC
3726 }
3727
93311388 3728 /* Read the abbrevs for this compilation unit into a table. */
dee91e82
DE
3729 if (cu->dwarf2_abbrevs == NULL)
3730 {
3731 dwarf2_read_abbrevs (cu, &dwarf2_per_objfile->abbrev);
3732 make_cleanup (dwarf2_free_abbrev_table, cu);
3733 }
af703f96 3734
dee91e82 3735 /* Read the top level CU/TU die. */
3019eac3 3736 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 3737 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 3738
3019eac3
DE
3739 /* If we have a DWO stub, process it and then read in the DWO file.
3740 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3741 a DWO CU, that this test will fail. */
3742 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3743 if (attr)
3744 {
3745 char *dwo_name = DW_STRING (attr);
3746 const char *comp_dir;
3747 struct dwo_unit *dwo_unit;
3748 ULONGEST signature; /* Or dwo_id. */
3749 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3750 int i,num_extra_attrs;
3751
3752 if (has_children)
3753 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3754 " has children (offset 0x%x) [in module %s]"),
3755 this_cu->offset.sect_off, bfd_get_filename (abfd));
3756
3757 /* These attributes aren't processed until later:
3758 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3759 However, the attribute is found in the stub which we won't have later.
3760 In order to not impose this complication on the rest of the code,
3761 we read them here and copy them to the DWO CU/TU die. */
3762 stmt_list = low_pc = high_pc = ranges = NULL;
3763
3764 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3765 DWO file. */
3766 if (! this_cu->is_debug_types)
3767 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3768 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3769 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3770 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3771
3772 /* There should be a DW_AT_addr_base attribute here (if needed).
3773 We need the value before we can process DW_FORM_GNU_addr_index. */
3774 cu->addr_base = 0;
3775 cu->have_addr_base = 0;
3776 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
3777 if (attr)
3778 {
3779 cu->addr_base = DW_UNSND (attr);
3780 cu->have_addr_base = 1;
3781 }
3782
3783 if (this_cu->is_debug_types)
3784 {
3785 gdb_assert (sig_type != NULL);
3786 signature = sig_type->signature;
3787 }
3788 else
3789 {
3790 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3791 if (! attr)
3792 error (_("Dwarf Error: missing dwo_id [in module %s]"),
3793 dwo_name);
3794 signature = DW_UNSND (attr);
3795 }
3796
3797 /* We may need the comp_dir in order to find the DWO file. */
3798 comp_dir = NULL;
3799 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3800 if (attr)
3801 comp_dir = DW_STRING (attr);
3802
3803 if (this_cu->is_debug_types)
3804 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
3805 else
3806 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
3807 signature);
3808
3809 if (dwo_unit == NULL)
3810 {
3811 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
3812 " with ID %s [in module %s]"),
3813 this_cu->offset.sect_off,
3814 phex (signature, sizeof (signature)),
3815 objfile->name);
3816 }
3817
3818 /* Set up for reading the DWO CU/TU. */
3819 cu->dwo_unit = dwo_unit;
3820 section = dwo_unit->info_or_types_section;
3821 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
3822 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
3823
3824 if (this_cu->is_debug_types)
3825 {
3826 ULONGEST signature;
3827
3828 info_ptr = read_and_check_type_unit_head (&cu->header,
3829 section, info_ptr,
3830 &signature, NULL);
3831 gdb_assert (sig_type->signature == signature);
3832 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3833 gdb_assert (dwo_unit->length
3834 == cu->header.length + cu->header.initial_length_size);
3835
3836 /* Establish the type offset that can be used to lookup the type.
3837 For DWO files, we don't know it until now. */
3838 sig_type->type_offset_in_section.sect_off =
3839 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
3840 }
3841 else
3842 {
3843 info_ptr = read_and_check_comp_unit_head (&cu->header,
3844 section, info_ptr, 0);
3845 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3846 gdb_assert (dwo_unit->length
3847 == cu->header.length + cu->header.initial_length_size);
3848 }
3849
3850 /* Discard the original CU's abbrev table, and read the DWO's. */
3851 dwarf2_free_abbrev_table (cu);
3852 dwarf2_read_abbrevs (cu, &dwo_unit->dwo_file->sections.abbrev);
3853
3854 /* Read in the die, but leave space to copy over the attributes
3855 from the stub. This has the benefit of simplifying the rest of
3856 the code - all the real work is done here. */
3857 num_extra_attrs = ((stmt_list != NULL)
3858 + (low_pc != NULL)
3859 + (high_pc != NULL)
3860 + (ranges != NULL));
3861 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
3862 &has_children, num_extra_attrs);
3863
3864 /* Copy over the attributes from the stub to the DWO die. */
3865 i = comp_unit_die->num_attrs;
3866 if (stmt_list != NULL)
3867 comp_unit_die->attrs[i++] = *stmt_list;
3868 if (low_pc != NULL)
3869 comp_unit_die->attrs[i++] = *low_pc;
3870 if (high_pc != NULL)
3871 comp_unit_die->attrs[i++] = *high_pc;
3872 if (ranges != NULL)
3873 comp_unit_die->attrs[i++] = *ranges;
3874 comp_unit_die->num_attrs += num_extra_attrs;
3875
3876 /* Skip dummy compilation units. */
3877 if (info_ptr >= begin_info_ptr + dwo_unit->length
3878 || peek_abbrev_code (abfd, info_ptr) == 0)
3879 {
3880 do_cleanups (cleanups);
3881 return;
3882 }
3883 }
3884
dee91e82
DE
3885 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3886
3887 if (free_cu_cleanup != NULL)
348e048f 3888 {
dee91e82
DE
3889 if (keep)
3890 {
3891 /* We've successfully allocated this compilation unit. Let our
3892 caller clean it up when finished with it. */
3893 discard_cleanups (free_cu_cleanup);
3894
3895 /* We can only discard free_cu_cleanup and all subsequent cleanups.
3896 So we have to manually free the abbrev table. */
3897 dwarf2_free_abbrev_table (cu);
3898
3899 /* Link this CU into read_in_chain. */
3900 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3901 dwarf2_per_objfile->read_in_chain = this_cu;
3902 }
3903 else
3904 do_cleanups (free_cu_cleanup);
348e048f 3905 }
dee91e82
DE
3906
3907 do_cleanups (cleanups);
3908}
3909
3019eac3
DE
3910/* Read CU/TU THIS_CU in section SECTION,
3911 but do not follow DW_AT_GNU_dwo_name if present.
3912 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
3913 have already done the lookup to find the DWO file).
dee91e82
DE
3914
3915 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 3916 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
3917
3918 We fill in THIS_CU->length.
3919
3920 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3921 linker) then DIE_READER_FUNC will not get called.
3922
3923 THIS_CU->cu is always freed when done.
3019eac3
DE
3924 This is done in order to not leave THIS_CU->cu in a state where we have
3925 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
3926
3927static void
3928init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3929 struct dwarf2_section_info *abbrev_section,
3019eac3 3930 struct dwo_file *dwo_file,
dee91e82
DE
3931 die_reader_func_ftype *die_reader_func,
3932 void *data)
3933{
3934 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
3935 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3936 bfd *abfd = section->asection->owner;
dee91e82
DE
3937 struct dwarf2_cu cu;
3938 gdb_byte *begin_info_ptr, *info_ptr;
3939 struct die_reader_specs reader;
3940 struct cleanup *cleanups;
3941 struct die_info *comp_unit_die;
3942 int has_children;
3943
3944 gdb_assert (this_cu->cu == NULL);
3945
dee91e82
DE
3946 /* This is cheap if the section is already read in. */
3947 dwarf2_read_section (objfile, section);
3948
3949 init_one_comp_unit (&cu, this_cu);
3950
3951 cleanups = make_cleanup (free_stack_comp_unit, &cu);
3952
3953 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3954 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3019eac3 3955 this_cu->is_debug_types);
dee91e82
DE
3956
3957 this_cu->length = cu.header.length + cu.header.initial_length_size;
3958
3959 /* Skip dummy compilation units. */
3960 if (info_ptr >= begin_info_ptr + this_cu->length
3961 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 3962 {
dee91e82 3963 do_cleanups (cleanups);
21b2bd31 3964 return;
93311388 3965 }
72bf9492 3966
dee91e82
DE
3967 dwarf2_read_abbrevs (&cu, abbrev_section);
3968 make_cleanup (dwarf2_free_abbrev_table, &cu);
3969
3019eac3 3970 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
3971 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
3972
3973 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3974
3975 do_cleanups (cleanups);
3976}
3977
3019eac3
DE
3978/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
3979 does not lookup the specified DWO file.
3980 This cannot be used to read DWO files.
dee91e82
DE
3981
3982 THIS_CU->cu is always freed when done.
3019eac3
DE
3983 This is done in order to not leave THIS_CU->cu in a state where we have
3984 to care whether it refers to the "main" CU or the DWO CU.
3985 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
3986
3987static void
3988init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
3989 die_reader_func_ftype *die_reader_func,
3990 void *data)
3991{
3992 init_cutu_and_read_dies_no_follow (this_cu,
3993 &dwarf2_per_objfile->abbrev,
3019eac3 3994 NULL,
dee91e82
DE
3995 die_reader_func, data);
3996}
3997
3998/* die_reader_func for process_psymtab_comp_unit. */
3999
4000static void
4001process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4002 gdb_byte *info_ptr,
4003 struct die_info *comp_unit_die,
4004 int has_children,
4005 void *data)
4006{
4007 struct dwarf2_cu *cu = reader->cu;
4008 struct objfile *objfile = cu->objfile;
4009 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4010 bfd *abfd = objfile->obfd;
4011 struct attribute *attr;
4012 CORE_ADDR baseaddr;
4013 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4014 struct partial_symtab *pst;
4015 int has_pc_info;
4016 const char *filename;
4017
4018 if (comp_unit_die->tag == DW_TAG_partial_unit)
4019 return;
4020
4021 prepare_one_comp_unit (cu, comp_unit_die);
4022
4023 cu->list_in_scope = &file_symbols;
c906108c 4024
93311388 4025 /* Allocate a new partial symbol table structure. */
dee91e82 4026 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4027 if (attr == NULL || !DW_STRING (attr))
4028 filename = "";
4029 else
4030 filename = DW_STRING (attr);
93311388 4031 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 4032 filename,
93311388
DE
4033 /* TEXTLOW and TEXTHIGH are set below. */
4034 0,
4035 objfile->global_psymbols.next,
4036 objfile->static_psymbols.next);
9750bca9 4037 pst->psymtabs_addrmap_supported = 1;
72bf9492 4038
dee91e82 4039 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4040 if (attr != NULL)
4041 pst->dirname = DW_STRING (attr);
72bf9492 4042
dee91e82 4043 pst->read_symtab_private = per_cu;
72bf9492 4044
93311388 4045 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4046
0963b4bd 4047 /* Store the function that reads in the rest of the symbol table. */
93311388 4048 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 4049
dee91e82 4050 per_cu->v.psymtab = pst;
c906108c 4051
dee91e82 4052 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4053
93311388
DE
4054 /* Possibly set the default values of LOWPC and HIGHPC from
4055 `DW_AT_ranges'. */
d85a05f0 4056 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4057 &best_highpc, cu, pst);
d85a05f0 4058 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4059 /* Store the contiguous range if it is not empty; it can be empty for
4060 CUs with no code. */
4061 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4062 best_lowpc + baseaddr,
4063 best_highpc + baseaddr - 1, pst);
93311388
DE
4064
4065 /* Check if comp unit has_children.
4066 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4067 If not, there's no more debug_info for this comp unit. */
d85a05f0 4068 if (has_children)
93311388
DE
4069 {
4070 struct partial_die_info *first_die;
4071 CORE_ADDR lowpc, highpc;
31ffec48 4072
93311388
DE
4073 lowpc = ((CORE_ADDR) -1);
4074 highpc = ((CORE_ADDR) 0);
c906108c 4075
dee91e82 4076 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4077
93311388 4078 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4079 ! has_pc_info, cu);
57c22c6c 4080
93311388
DE
4081 /* If we didn't find a lowpc, set it to highpc to avoid
4082 complaints from `maint check'. */
4083 if (lowpc == ((CORE_ADDR) -1))
4084 lowpc = highpc;
10b3939b 4085
93311388
DE
4086 /* If the compilation unit didn't have an explicit address range,
4087 then use the information extracted from its child dies. */
d85a05f0 4088 if (! has_pc_info)
93311388 4089 {
d85a05f0
DJ
4090 best_lowpc = lowpc;
4091 best_highpc = highpc;
93311388
DE
4092 }
4093 }
d85a05f0
DJ
4094 pst->textlow = best_lowpc + baseaddr;
4095 pst->texthigh = best_highpc + baseaddr;
c906108c 4096
93311388
DE
4097 pst->n_global_syms = objfile->global_psymbols.next -
4098 (objfile->global_psymbols.list + pst->globals_offset);
4099 pst->n_static_syms = objfile->static_psymbols.next -
4100 (objfile->static_psymbols.list + pst->statics_offset);
4101 sort_pst_symbols (pst);
c906108c 4102
3019eac3 4103 if (per_cu->is_debug_types)
348e048f
DE
4104 {
4105 /* It's not clear we want to do anything with stmt lists here.
4106 Waiting to see what gcc ultimately does. */
4107 }
d85a05f0 4108 else
93311388
DE
4109 {
4110 /* Get the list of files included in the current compilation unit,
4111 and build a psymtab for each of them. */
dee91e82 4112 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
93311388 4113 }
dee91e82 4114}
ae038cb0 4115
dee91e82
DE
4116/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4117 Process compilation unit THIS_CU for a psymtab. */
4118
4119static void
4120process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu)
4121{
4122 /* If this compilation unit was already read in, free the
4123 cached copy in order to read it in again. This is
4124 necessary because we skipped some symbols when we first
4125 read in the compilation unit (see load_partial_dies).
4126 This problem could be avoided, but the benefit is unclear. */
4127 if (this_cu->cu != NULL)
4128 free_one_cached_comp_unit (this_cu);
4129
3019eac3 4130 gdb_assert (! this_cu->is_debug_types);
fd820528
DE
4131 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
4132 NULL);
dee91e82
DE
4133
4134 /* Age out any secondary CUs. */
4135 age_cached_comp_units ();
93311388 4136}
ff013f42 4137
348e048f
DE
4138/* Traversal function for htab_traverse_noresize.
4139 Process one .debug_types comp-unit. */
4140
4141static int
dee91e82 4142process_psymtab_type_unit (void **slot, void *info)
348e048f 4143{
dee91e82
DE
4144 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4145 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 4146
fd820528 4147 gdb_assert (per_cu->is_debug_types);
a0f42c21 4148 gdb_assert (info == NULL);
348e048f 4149
dee91e82
DE
4150 /* If this compilation unit was already read in, free the
4151 cached copy in order to read it in again. This is
4152 necessary because we skipped some symbols when we first
4153 read in the compilation unit (see load_partial_dies).
4154 This problem could be avoided, but the benefit is unclear. */
4155 if (per_cu->cu != NULL)
4156 free_one_cached_comp_unit (per_cu);
4157
fd820528
DE
4158 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4159 NULL);
dee91e82
DE
4160
4161 /* Age out any secondary CUs. */
4162 age_cached_comp_units ();
348e048f
DE
4163
4164 return 1;
4165}
4166
4167/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4168 Build partial symbol tables for the .debug_types comp-units. */
4169
4170static void
4171build_type_psymtabs (struct objfile *objfile)
4172{
0e50663e 4173 if (! create_all_type_units (objfile))
348e048f
DE
4174 return;
4175
4176 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
dee91e82 4177 process_psymtab_type_unit, NULL);
348e048f
DE
4178}
4179
60606b2c
TT
4180/* A cleanup function that clears objfile's psymtabs_addrmap field. */
4181
4182static void
4183psymtabs_addrmap_cleanup (void *o)
4184{
4185 struct objfile *objfile = o;
ec61707d 4186
60606b2c
TT
4187 objfile->psymtabs_addrmap = NULL;
4188}
4189
93311388
DE
4190/* Build the partial symbol table by doing a quick pass through the
4191 .debug_info and .debug_abbrev sections. */
72bf9492 4192
93311388 4193static void
c67a9c90 4194dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 4195{
60606b2c
TT
4196 struct cleanup *back_to, *addrmap_cleanup;
4197 struct obstack temp_obstack;
21b2bd31 4198 int i;
93311388 4199
98bfdba5
PA
4200 dwarf2_per_objfile->reading_partial_symbols = 1;
4201
be391dca 4202 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 4203
93311388
DE
4204 /* Any cached compilation units will be linked by the per-objfile
4205 read_in_chain. Make sure to free them when we're done. */
4206 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 4207
348e048f
DE
4208 build_type_psymtabs (objfile);
4209
93311388 4210 create_all_comp_units (objfile);
c906108c 4211
60606b2c
TT
4212 /* Create a temporary address map on a temporary obstack. We later
4213 copy this to the final obstack. */
4214 obstack_init (&temp_obstack);
4215 make_cleanup_obstack_free (&temp_obstack);
4216 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4217 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 4218
21b2bd31 4219 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 4220 {
21b2bd31 4221 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 4222
dee91e82 4223 process_psymtab_comp_unit (per_cu);
c906108c 4224 }
ff013f42
JK
4225
4226 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4227 &objfile->objfile_obstack);
60606b2c 4228 discard_cleanups (addrmap_cleanup);
ff013f42 4229
ae038cb0
DJ
4230 do_cleanups (back_to);
4231}
4232
3019eac3 4233/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
4234
4235static void
dee91e82
DE
4236load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4237 gdb_byte *info_ptr,
4238 struct die_info *comp_unit_die,
4239 int has_children,
4240 void *data)
ae038cb0 4241{
dee91e82 4242 struct dwarf2_cu *cu = reader->cu;
ae038cb0 4243
9816fde3 4244 prepare_one_comp_unit (cu, comp_unit_die);
ae038cb0 4245
ae038cb0
DJ
4246 /* Check if comp unit has_children.
4247 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4248 If not, there's no more debug_info for this comp unit. */
d85a05f0 4249 if (has_children)
dee91e82
DE
4250 load_partial_dies (reader, info_ptr, 0);
4251}
98bfdba5 4252
dee91e82
DE
4253/* Load the partial DIEs for a secondary CU into memory.
4254 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 4255
dee91e82
DE
4256static void
4257load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4258{
fd820528 4259 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
4260}
4261
9cdd5dbd
DE
4262/* Create a list of all compilation units in OBJFILE.
4263 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
4264
4265static void
4266create_all_comp_units (struct objfile *objfile)
4267{
4268 int n_allocated;
4269 int n_comp_units;
4270 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
4271 gdb_byte *info_ptr;
4272
4273 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4274 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4275
4276 n_comp_units = 0;
4277 n_allocated = 10;
4278 all_comp_units = xmalloc (n_allocated
4279 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 4280
3e43a32a
MS
4281 while (info_ptr < dwarf2_per_objfile->info.buffer
4282 + dwarf2_per_objfile->info.size)
ae038cb0 4283 {
c764a876 4284 unsigned int length, initial_length_size;
ae038cb0 4285 struct dwarf2_per_cu_data *this_cu;
b64f50a1 4286 sect_offset offset;
ae038cb0 4287
b64f50a1 4288 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4289
4290 /* Read just enough information to find out where the next
4291 compilation unit is. */
c764a876
DE
4292 length = read_initial_length (objfile->obfd, info_ptr,
4293 &initial_length_size);
ae038cb0
DJ
4294
4295 /* Save the compilation unit for later lookup. */
4296 this_cu = obstack_alloc (&objfile->objfile_obstack,
4297 sizeof (struct dwarf2_per_cu_data));
4298 memset (this_cu, 0, sizeof (*this_cu));
4299 this_cu->offset = offset;
c764a876 4300 this_cu->length = length + initial_length_size;
9291a0cd 4301 this_cu->objfile = objfile;
3019eac3 4302 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
ae038cb0
DJ
4303
4304 if (n_comp_units == n_allocated)
4305 {
4306 n_allocated *= 2;
4307 all_comp_units = xrealloc (all_comp_units,
4308 n_allocated
4309 * sizeof (struct dwarf2_per_cu_data *));
4310 }
4311 all_comp_units[n_comp_units++] = this_cu;
4312
4313 info_ptr = info_ptr + this_cu->length;
4314 }
4315
4316 dwarf2_per_objfile->all_comp_units
4317 = obstack_alloc (&objfile->objfile_obstack,
4318 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4319 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4320 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4321 xfree (all_comp_units);
4322 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
4323}
4324
5734ee8b
DJ
4325/* Process all loaded DIEs for compilation unit CU, starting at
4326 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4327 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4328 DW_AT_ranges). If NEED_PC is set, then this function will set
4329 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4330 and record the covered ranges in the addrmap. */
c906108c 4331
72bf9492
DJ
4332static void
4333scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 4334 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 4335{
72bf9492 4336 struct partial_die_info *pdi;
c906108c 4337
91c24f0a
DC
4338 /* Now, march along the PDI's, descending into ones which have
4339 interesting children but skipping the children of the other ones,
4340 until we reach the end of the compilation unit. */
c906108c 4341
72bf9492 4342 pdi = first_die;
91c24f0a 4343
72bf9492
DJ
4344 while (pdi != NULL)
4345 {
4346 fixup_partial_die (pdi, cu);
c906108c 4347
f55ee35c 4348 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
4349 children, so we need to look at them. Ditto for anonymous
4350 enums. */
933c6fe4 4351
72bf9492 4352 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
f55ee35c 4353 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
c906108c 4354 {
72bf9492 4355 switch (pdi->tag)
c906108c
SS
4356 {
4357 case DW_TAG_subprogram:
5734ee8b 4358 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 4359 break;
72929c62 4360 case DW_TAG_constant:
c906108c
SS
4361 case DW_TAG_variable:
4362 case DW_TAG_typedef:
91c24f0a 4363 case DW_TAG_union_type:
72bf9492 4364 if (!pdi->is_declaration)
63d06c5c 4365 {
72bf9492 4366 add_partial_symbol (pdi, cu);
63d06c5c
DC
4367 }
4368 break;
c906108c 4369 case DW_TAG_class_type:
680b30c7 4370 case DW_TAG_interface_type:
c906108c 4371 case DW_TAG_structure_type:
72bf9492 4372 if (!pdi->is_declaration)
c906108c 4373 {
72bf9492 4374 add_partial_symbol (pdi, cu);
c906108c
SS
4375 }
4376 break;
91c24f0a 4377 case DW_TAG_enumeration_type:
72bf9492
DJ
4378 if (!pdi->is_declaration)
4379 add_partial_enumeration (pdi, cu);
c906108c
SS
4380 break;
4381 case DW_TAG_base_type:
a02abb62 4382 case DW_TAG_subrange_type:
c906108c 4383 /* File scope base type definitions are added to the partial
c5aa993b 4384 symbol table. */
72bf9492 4385 add_partial_symbol (pdi, cu);
c906108c 4386 break;
d9fa45fe 4387 case DW_TAG_namespace:
5734ee8b 4388 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 4389 break;
5d7cb8df
JK
4390 case DW_TAG_module:
4391 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4392 break;
c906108c
SS
4393 default:
4394 break;
4395 }
4396 }
4397
72bf9492
DJ
4398 /* If the die has a sibling, skip to the sibling. */
4399
4400 pdi = pdi->die_sibling;
4401 }
4402}
4403
4404/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 4405
72bf9492 4406 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
4407 name is concatenated with "::" and the partial DIE's name. For
4408 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
4409 Enumerators are an exception; they use the scope of their parent
4410 enumeration type, i.e. the name of the enumeration type is not
4411 prepended to the enumerator.
91c24f0a 4412
72bf9492
DJ
4413 There are two complexities. One is DW_AT_specification; in this
4414 case "parent" means the parent of the target of the specification,
4415 instead of the direct parent of the DIE. The other is compilers
4416 which do not emit DW_TAG_namespace; in this case we try to guess
4417 the fully qualified name of structure types from their members'
4418 linkage names. This must be done using the DIE's children rather
4419 than the children of any DW_AT_specification target. We only need
4420 to do this for structures at the top level, i.e. if the target of
4421 any DW_AT_specification (if any; otherwise the DIE itself) does not
4422 have a parent. */
4423
4424/* Compute the scope prefix associated with PDI's parent, in
4425 compilation unit CU. The result will be allocated on CU's
4426 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4427 field. NULL is returned if no prefix is necessary. */
4428static char *
4429partial_die_parent_scope (struct partial_die_info *pdi,
4430 struct dwarf2_cu *cu)
4431{
4432 char *grandparent_scope;
4433 struct partial_die_info *parent, *real_pdi;
91c24f0a 4434
72bf9492
DJ
4435 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4436 then this means the parent of the specification DIE. */
4437
4438 real_pdi = pdi;
72bf9492 4439 while (real_pdi->has_specification)
10b3939b 4440 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
4441
4442 parent = real_pdi->die_parent;
4443 if (parent == NULL)
4444 return NULL;
4445
4446 if (parent->scope_set)
4447 return parent->scope;
4448
4449 fixup_partial_die (parent, cu);
4450
10b3939b 4451 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 4452
acebe513
UW
4453 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4454 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4455 Work around this problem here. */
4456 if (cu->language == language_cplus
6e70227d 4457 && parent->tag == DW_TAG_namespace
acebe513
UW
4458 && strcmp (parent->name, "::") == 0
4459 && grandparent_scope == NULL)
4460 {
4461 parent->scope = NULL;
4462 parent->scope_set = 1;
4463 return NULL;
4464 }
4465
9c6c53f7
SA
4466 if (pdi->tag == DW_TAG_enumerator)
4467 /* Enumerators should not get the name of the enumeration as a prefix. */
4468 parent->scope = grandparent_scope;
4469 else if (parent->tag == DW_TAG_namespace
f55ee35c 4470 || parent->tag == DW_TAG_module
72bf9492
DJ
4471 || parent->tag == DW_TAG_structure_type
4472 || parent->tag == DW_TAG_class_type
680b30c7 4473 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
4474 || parent->tag == DW_TAG_union_type
4475 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
4476 {
4477 if (grandparent_scope == NULL)
4478 parent->scope = parent->name;
4479 else
3e43a32a
MS
4480 parent->scope = typename_concat (&cu->comp_unit_obstack,
4481 grandparent_scope,
f55ee35c 4482 parent->name, 0, cu);
72bf9492 4483 }
72bf9492
DJ
4484 else
4485 {
4486 /* FIXME drow/2004-04-01: What should we be doing with
4487 function-local names? For partial symbols, we should probably be
4488 ignoring them. */
4489 complaint (&symfile_complaints,
e2e0b3e5 4490 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 4491 parent->tag, pdi->offset.sect_off);
72bf9492 4492 parent->scope = grandparent_scope;
c906108c
SS
4493 }
4494
72bf9492
DJ
4495 parent->scope_set = 1;
4496 return parent->scope;
4497}
4498
4499/* Return the fully scoped name associated with PDI, from compilation unit
4500 CU. The result will be allocated with malloc. */
4568ecf9 4501
72bf9492
DJ
4502static char *
4503partial_die_full_name (struct partial_die_info *pdi,
4504 struct dwarf2_cu *cu)
4505{
4506 char *parent_scope;
4507
98bfdba5
PA
4508 /* If this is a template instantiation, we can not work out the
4509 template arguments from partial DIEs. So, unfortunately, we have
4510 to go through the full DIEs. At least any work we do building
4511 types here will be reused if full symbols are loaded later. */
4512 if (pdi->has_template_arguments)
4513 {
4514 fixup_partial_die (pdi, cu);
4515
4516 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4517 {
4518 struct die_info *die;
4519 struct attribute attr;
4520 struct dwarf2_cu *ref_cu = cu;
4521
b64f50a1 4522 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
4523 attr.name = 0;
4524 attr.form = DW_FORM_ref_addr;
4568ecf9 4525 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
4526 die = follow_die_ref (NULL, &attr, &ref_cu);
4527
4528 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4529 }
4530 }
4531
72bf9492
DJ
4532 parent_scope = partial_die_parent_scope (pdi, cu);
4533 if (parent_scope == NULL)
4534 return NULL;
4535 else
f55ee35c 4536 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4537}
4538
4539static void
72bf9492 4540add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4541{
e7c27a73 4542 struct objfile *objfile = cu->objfile;
c906108c 4543 CORE_ADDR addr = 0;
decbce07 4544 char *actual_name = NULL;
e142c38c 4545 CORE_ADDR baseaddr;
72bf9492 4546 int built_actual_name = 0;
e142c38c
DJ
4547
4548 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4549
94af9270
KS
4550 actual_name = partial_die_full_name (pdi, cu);
4551 if (actual_name)
4552 built_actual_name = 1;
63d06c5c 4553
72bf9492
DJ
4554 if (actual_name == NULL)
4555 actual_name = pdi->name;
4556
c906108c
SS
4557 switch (pdi->tag)
4558 {
4559 case DW_TAG_subprogram:
2cfa0c8d 4560 if (pdi->is_external || cu->language == language_ada)
c906108c 4561 {
2cfa0c8d
JB
4562 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4563 of the global scope. But in Ada, we want to be able to access
4564 nested procedures globally. So all Ada subprograms are stored
4565 in the global scope. */
f47fb265 4566 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4567 mst_text, objfile); */
f47fb265
MS
4568 add_psymbol_to_list (actual_name, strlen (actual_name),
4569 built_actual_name,
4570 VAR_DOMAIN, LOC_BLOCK,
4571 &objfile->global_psymbols,
4572 0, pdi->lowpc + baseaddr,
4573 cu->language, objfile);
c906108c
SS
4574 }
4575 else
4576 {
f47fb265 4577 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4578 mst_file_text, objfile); */
f47fb265
MS
4579 add_psymbol_to_list (actual_name, strlen (actual_name),
4580 built_actual_name,
4581 VAR_DOMAIN, LOC_BLOCK,
4582 &objfile->static_psymbols,
4583 0, pdi->lowpc + baseaddr,
4584 cu->language, objfile);
c906108c
SS
4585 }
4586 break;
72929c62
JB
4587 case DW_TAG_constant:
4588 {
4589 struct psymbol_allocation_list *list;
4590
4591 if (pdi->is_external)
4592 list = &objfile->global_psymbols;
4593 else
4594 list = &objfile->static_psymbols;
f47fb265
MS
4595 add_psymbol_to_list (actual_name, strlen (actual_name),
4596 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4597 list, 0, 0, cu->language, objfile);
72929c62
JB
4598 }
4599 break;
c906108c 4600 case DW_TAG_variable:
caac4577
JG
4601 if (pdi->locdesc)
4602 addr = decode_locdesc (pdi->locdesc, cu);
4603
4604 if (pdi->locdesc
4605 && addr == 0
4606 && !dwarf2_per_objfile->has_section_at_zero)
4607 {
4608 /* A global or static variable may also have been stripped
4609 out by the linker if unused, in which case its address
4610 will be nullified; do not add such variables into partial
4611 symbol table then. */
4612 }
4613 else if (pdi->is_external)
c906108c
SS
4614 {
4615 /* Global Variable.
4616 Don't enter into the minimal symbol tables as there is
4617 a minimal symbol table entry from the ELF symbols already.
4618 Enter into partial symbol table if it has a location
4619 descriptor or a type.
4620 If the location descriptor is missing, new_symbol will create
4621 a LOC_UNRESOLVED symbol, the address of the variable will then
4622 be determined from the minimal symbol table whenever the variable
4623 is referenced.
4624 The address for the partial symbol table entry is not
4625 used by GDB, but it comes in handy for debugging partial symbol
4626 table building. */
4627
c906108c 4628 if (pdi->locdesc || pdi->has_type)
f47fb265
MS
4629 add_psymbol_to_list (actual_name, strlen (actual_name),
4630 built_actual_name,
4631 VAR_DOMAIN, LOC_STATIC,
4632 &objfile->global_psymbols,
4633 0, addr + baseaddr,
4634 cu->language, objfile);
c906108c
SS
4635 }
4636 else
4637 {
0963b4bd 4638 /* Static Variable. Skip symbols without location descriptors. */
c906108c 4639 if (pdi->locdesc == NULL)
decbce07
MS
4640 {
4641 if (built_actual_name)
4642 xfree (actual_name);
4643 return;
4644 }
f47fb265 4645 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4646 mst_file_data, objfile); */
f47fb265
MS
4647 add_psymbol_to_list (actual_name, strlen (actual_name),
4648 built_actual_name,
4649 VAR_DOMAIN, LOC_STATIC,
4650 &objfile->static_psymbols,
4651 0, addr + baseaddr,
4652 cu->language, objfile);
c906108c
SS
4653 }
4654 break;
4655 case DW_TAG_typedef:
4656 case DW_TAG_base_type:
a02abb62 4657 case DW_TAG_subrange_type:
38d518c9 4658 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4659 built_actual_name,
176620f1 4660 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4661 &objfile->static_psymbols,
e142c38c 4662 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4663 break;
72bf9492
DJ
4664 case DW_TAG_namespace:
4665 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4666 built_actual_name,
72bf9492
DJ
4667 VAR_DOMAIN, LOC_TYPEDEF,
4668 &objfile->global_psymbols,
4669 0, (CORE_ADDR) 0, cu->language, objfile);
4670 break;
c906108c 4671 case DW_TAG_class_type:
680b30c7 4672 case DW_TAG_interface_type:
c906108c
SS
4673 case DW_TAG_structure_type:
4674 case DW_TAG_union_type:
4675 case DW_TAG_enumeration_type:
fa4028e9
JB
4676 /* Skip external references. The DWARF standard says in the section
4677 about "Structure, Union, and Class Type Entries": "An incomplete
4678 structure, union or class type is represented by a structure,
4679 union or class entry that does not have a byte size attribute
4680 and that has a DW_AT_declaration attribute." */
4681 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4682 {
4683 if (built_actual_name)
4684 xfree (actual_name);
4685 return;
4686 }
fa4028e9 4687
63d06c5c
DC
4688 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4689 static vs. global. */
38d518c9 4690 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4691 built_actual_name,
176620f1 4692 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4693 (cu->language == language_cplus
4694 || cu->language == language_java)
63d06c5c
DC
4695 ? &objfile->global_psymbols
4696 : &objfile->static_psymbols,
e142c38c 4697 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4698
c906108c
SS
4699 break;
4700 case DW_TAG_enumerator:
38d518c9 4701 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4702 built_actual_name,
176620f1 4703 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4704 (cu->language == language_cplus
4705 || cu->language == language_java)
f6fe98ef
DJ
4706 ? &objfile->global_psymbols
4707 : &objfile->static_psymbols,
e142c38c 4708 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4709 break;
4710 default:
4711 break;
4712 }
5c4e30ca 4713
72bf9492
DJ
4714 if (built_actual_name)
4715 xfree (actual_name);
c906108c
SS
4716}
4717
5c4e30ca
DC
4718/* Read a partial die corresponding to a namespace; also, add a symbol
4719 corresponding to that namespace to the symbol table. NAMESPACE is
4720 the name of the enclosing namespace. */
91c24f0a 4721
72bf9492
DJ
4722static void
4723add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4724 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4725 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4726{
72bf9492 4727 /* Add a symbol for the namespace. */
e7c27a73 4728
72bf9492 4729 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4730
4731 /* Now scan partial symbols in that namespace. */
4732
91c24f0a 4733 if (pdi->has_children)
5734ee8b 4734 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4735}
4736
5d7cb8df
JK
4737/* Read a partial die corresponding to a Fortran module. */
4738
4739static void
4740add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4741 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4742{
f55ee35c 4743 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4744
4745 if (pdi->has_children)
4746 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4747}
4748
bc30ff58
JB
4749/* Read a partial die corresponding to a subprogram and create a partial
4750 symbol for that subprogram. When the CU language allows it, this
4751 routine also defines a partial symbol for each nested subprogram
4752 that this subprogram contains.
6e70227d 4753
bc30ff58
JB
4754 DIE my also be a lexical block, in which case we simply search
4755 recursively for suprograms defined inside that lexical block.
4756 Again, this is only performed when the CU language allows this
4757 type of definitions. */
4758
4759static void
4760add_partial_subprogram (struct partial_die_info *pdi,
4761 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4762 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4763{
4764 if (pdi->tag == DW_TAG_subprogram)
4765 {
4766 if (pdi->has_pc_info)
4767 {
4768 if (pdi->lowpc < *lowpc)
4769 *lowpc = pdi->lowpc;
4770 if (pdi->highpc > *highpc)
4771 *highpc = pdi->highpc;
5734ee8b
DJ
4772 if (need_pc)
4773 {
4774 CORE_ADDR baseaddr;
4775 struct objfile *objfile = cu->objfile;
4776
4777 baseaddr = ANOFFSET (objfile->section_offsets,
4778 SECT_OFF_TEXT (objfile));
4779 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4780 pdi->lowpc + baseaddr,
4781 pdi->highpc - 1 + baseaddr,
9291a0cd 4782 cu->per_cu->v.psymtab);
5734ee8b 4783 }
481860b3
GB
4784 }
4785
4786 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4787 {
bc30ff58 4788 if (!pdi->is_declaration)
e8d05480
JB
4789 /* Ignore subprogram DIEs that do not have a name, they are
4790 illegal. Do not emit a complaint at this point, we will
4791 do so when we convert this psymtab into a symtab. */
4792 if (pdi->name)
4793 add_partial_symbol (pdi, cu);
bc30ff58
JB
4794 }
4795 }
6e70227d 4796
bc30ff58
JB
4797 if (! pdi->has_children)
4798 return;
4799
4800 if (cu->language == language_ada)
4801 {
4802 pdi = pdi->die_child;
4803 while (pdi != NULL)
4804 {
4805 fixup_partial_die (pdi, cu);
4806 if (pdi->tag == DW_TAG_subprogram
4807 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4808 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4809 pdi = pdi->die_sibling;
4810 }
4811 }
4812}
4813
91c24f0a
DC
4814/* Read a partial die corresponding to an enumeration type. */
4815
72bf9492
DJ
4816static void
4817add_partial_enumeration (struct partial_die_info *enum_pdi,
4818 struct dwarf2_cu *cu)
91c24f0a 4819{
72bf9492 4820 struct partial_die_info *pdi;
91c24f0a
DC
4821
4822 if (enum_pdi->name != NULL)
72bf9492
DJ
4823 add_partial_symbol (enum_pdi, cu);
4824
4825 pdi = enum_pdi->die_child;
4826 while (pdi)
91c24f0a 4827 {
72bf9492 4828 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4829 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4830 else
72bf9492
DJ
4831 add_partial_symbol (pdi, cu);
4832 pdi = pdi->die_sibling;
91c24f0a 4833 }
91c24f0a
DC
4834}
4835
6caca83c
CC
4836/* Return the initial uleb128 in the die at INFO_PTR. */
4837
4838static unsigned int
4839peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4840{
4841 unsigned int bytes_read;
4842
4843 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4844}
4845
4bb7a0a7
DJ
4846/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4847 Return the corresponding abbrev, or NULL if the number is zero (indicating
4848 an empty DIE). In either case *BYTES_READ will be set to the length of
4849 the initial number. */
4850
4851static struct abbrev_info *
fe1b8b76 4852peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4853 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4854{
4855 bfd *abfd = cu->objfile->obfd;
4856 unsigned int abbrev_number;
4857 struct abbrev_info *abbrev;
4858
4859 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4860
4861 if (abbrev_number == 0)
4862 return NULL;
4863
4864 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4865 if (!abbrev)
4866 {
3e43a32a
MS
4867 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4868 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4869 }
4870
4871 return abbrev;
4872}
4873
93311388
DE
4874/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4875 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
4876 DIE. Any children of the skipped DIEs will also be skipped. */
4877
fe1b8b76 4878static gdb_byte *
dee91e82 4879skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 4880{
dee91e82 4881 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
4882 struct abbrev_info *abbrev;
4883 unsigned int bytes_read;
4884
4885 while (1)
4886 {
4887 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4888 if (abbrev == NULL)
4889 return info_ptr + bytes_read;
4890 else
dee91e82 4891 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
4892 }
4893}
4894
93311388
DE
4895/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4896 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
4897 abbrev corresponding to that skipped uleb128 should be passed in
4898 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4899 children. */
4900
fe1b8b76 4901static gdb_byte *
dee91e82
DE
4902skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
4903 struct abbrev_info *abbrev)
4bb7a0a7
DJ
4904{
4905 unsigned int bytes_read;
4906 struct attribute attr;
dee91e82
DE
4907 bfd *abfd = reader->abfd;
4908 struct dwarf2_cu *cu = reader->cu;
4909 gdb_byte *buffer = reader->buffer;
4bb7a0a7
DJ
4910 unsigned int form, i;
4911
4912 for (i = 0; i < abbrev->num_attrs; i++)
4913 {
4914 /* The only abbrev we care about is DW_AT_sibling. */
4915 if (abbrev->attrs[i].name == DW_AT_sibling)
4916 {
dee91e82 4917 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 4918 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
4919 complaint (&symfile_complaints,
4920 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 4921 else
b64f50a1 4922 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
4923 }
4924
4925 /* If it isn't DW_AT_sibling, skip this attribute. */
4926 form = abbrev->attrs[i].form;
4927 skip_attribute:
4928 switch (form)
4929 {
4bb7a0a7 4930 case DW_FORM_ref_addr:
ae411497
TT
4931 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4932 and later it is offset sized. */
4933 if (cu->header.version == 2)
4934 info_ptr += cu->header.addr_size;
4935 else
4936 info_ptr += cu->header.offset_size;
4937 break;
4938 case DW_FORM_addr:
4bb7a0a7
DJ
4939 info_ptr += cu->header.addr_size;
4940 break;
4941 case DW_FORM_data1:
4942 case DW_FORM_ref1:
4943 case DW_FORM_flag:
4944 info_ptr += 1;
4945 break;
2dc7f7b3
TT
4946 case DW_FORM_flag_present:
4947 break;
4bb7a0a7
DJ
4948 case DW_FORM_data2:
4949 case DW_FORM_ref2:
4950 info_ptr += 2;
4951 break;
4952 case DW_FORM_data4:
4953 case DW_FORM_ref4:
4954 info_ptr += 4;
4955 break;
4956 case DW_FORM_data8:
4957 case DW_FORM_ref8:
55f1336d 4958 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
4959 info_ptr += 8;
4960 break;
4961 case DW_FORM_string:
9b1c24c8 4962 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
4963 info_ptr += bytes_read;
4964 break;
2dc7f7b3 4965 case DW_FORM_sec_offset:
4bb7a0a7
DJ
4966 case DW_FORM_strp:
4967 info_ptr += cu->header.offset_size;
4968 break;
2dc7f7b3 4969 case DW_FORM_exprloc:
4bb7a0a7
DJ
4970 case DW_FORM_block:
4971 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4972 info_ptr += bytes_read;
4973 break;
4974 case DW_FORM_block1:
4975 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4976 break;
4977 case DW_FORM_block2:
4978 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4979 break;
4980 case DW_FORM_block4:
4981 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4982 break;
4983 case DW_FORM_sdata:
4984 case DW_FORM_udata:
4985 case DW_FORM_ref_udata:
3019eac3
DE
4986 case DW_FORM_GNU_addr_index:
4987 case DW_FORM_GNU_str_index:
4bb7a0a7
DJ
4988 info_ptr = skip_leb128 (abfd, info_ptr);
4989 break;
4990 case DW_FORM_indirect:
4991 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4992 info_ptr += bytes_read;
4993 /* We need to continue parsing from here, so just go back to
4994 the top. */
4995 goto skip_attribute;
4996
4997 default:
3e43a32a
MS
4998 error (_("Dwarf Error: Cannot handle %s "
4999 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
5000 dwarf_form_name (form),
5001 bfd_get_filename (abfd));
5002 }
5003 }
5004
5005 if (abbrev->has_children)
dee91e82 5006 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
5007 else
5008 return info_ptr;
5009}
5010
93311388 5011/* Locate ORIG_PDI's sibling.
dee91e82 5012 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 5013
fe1b8b76 5014static gdb_byte *
dee91e82
DE
5015locate_pdi_sibling (const struct die_reader_specs *reader,
5016 struct partial_die_info *orig_pdi,
5017 gdb_byte *info_ptr)
91c24f0a
DC
5018{
5019 /* Do we know the sibling already? */
72bf9492 5020
91c24f0a
DC
5021 if (orig_pdi->sibling)
5022 return orig_pdi->sibling;
5023
5024 /* Are there any children to deal with? */
5025
5026 if (!orig_pdi->has_children)
5027 return info_ptr;
5028
4bb7a0a7 5029 /* Skip the children the long way. */
91c24f0a 5030
dee91e82 5031 return skip_children (reader, info_ptr);
91c24f0a
DC
5032}
5033
c906108c
SS
5034/* Expand this partial symbol table into a full symbol table. */
5035
5036static void
fba45db2 5037dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 5038{
c906108c
SS
5039 if (pst != NULL)
5040 {
5041 if (pst->readin)
5042 {
3e43a32a
MS
5043 warning (_("bug: psymtab for %s is already read in."),
5044 pst->filename);
c906108c
SS
5045 }
5046 else
5047 {
5048 if (info_verbose)
5049 {
3e43a32a
MS
5050 printf_filtered (_("Reading in symbols for %s..."),
5051 pst->filename);
c906108c
SS
5052 gdb_flush (gdb_stdout);
5053 }
5054
10b3939b
DJ
5055 /* Restore our global data. */
5056 dwarf2_per_objfile = objfile_data (pst->objfile,
5057 dwarf2_objfile_data_key);
5058
b2ab525c
KB
5059 /* If this psymtab is constructed from a debug-only objfile, the
5060 has_section_at_zero flag will not necessarily be correct. We
5061 can get the correct value for this flag by looking at the data
5062 associated with the (presumably stripped) associated objfile. */
5063 if (pst->objfile->separate_debug_objfile_backlink)
5064 {
5065 struct dwarf2_per_objfile *dpo_backlink
5066 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5067 dwarf2_objfile_data_key);
9a619af0 5068
b2ab525c
KB
5069 dwarf2_per_objfile->has_section_at_zero
5070 = dpo_backlink->has_section_at_zero;
5071 }
5072
98bfdba5
PA
5073 dwarf2_per_objfile->reading_partial_symbols = 0;
5074
c906108c
SS
5075 psymtab_to_symtab_1 (pst);
5076
5077 /* Finish up the debug error message. */
5078 if (info_verbose)
a3f17187 5079 printf_filtered (_("done.\n"));
c906108c
SS
5080 }
5081 }
5082}
9cdd5dbd
DE
5083\f
5084/* Reading in full CUs. */
c906108c 5085
10b3939b
DJ
5086/* Add PER_CU to the queue. */
5087
5088static void
a0f42c21 5089queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
10b3939b
DJ
5090{
5091 struct dwarf2_queue_item *item;
5092
5093 per_cu->queued = 1;
5094 item = xmalloc (sizeof (*item));
5095 item->per_cu = per_cu;
5096 item->next = NULL;
5097
5098 if (dwarf2_queue == NULL)
5099 dwarf2_queue = item;
5100 else
5101 dwarf2_queue_tail->next = item;
5102
5103 dwarf2_queue_tail = item;
5104}
5105
5106/* Process the queue. */
5107
5108static void
a0f42c21 5109process_queue (void)
10b3939b
DJ
5110{
5111 struct dwarf2_queue_item *item, *next_item;
5112
03dd20cc
DJ
5113 /* The queue starts out with one item, but following a DIE reference
5114 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
5115 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5116 {
9291a0cd
TT
5117 if (dwarf2_per_objfile->using_index
5118 ? !item->per_cu->v.quick->symtab
5119 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
10b3939b
DJ
5120 process_full_comp_unit (item->per_cu);
5121
5122 item->per_cu->queued = 0;
5123 next_item = item->next;
5124 xfree (item);
5125 }
5126
5127 dwarf2_queue_tail = NULL;
5128}
5129
5130/* Free all allocated queue entries. This function only releases anything if
5131 an error was thrown; if the queue was processed then it would have been
5132 freed as we went along. */
5133
5134static void
5135dwarf2_release_queue (void *dummy)
5136{
5137 struct dwarf2_queue_item *item, *last;
5138
5139 item = dwarf2_queue;
5140 while (item)
5141 {
5142 /* Anything still marked queued is likely to be in an
5143 inconsistent state, so discard it. */
5144 if (item->per_cu->queued)
5145 {
5146 if (item->per_cu->cu != NULL)
dee91e82 5147 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
5148 item->per_cu->queued = 0;
5149 }
5150
5151 last = item;
5152 item = item->next;
5153 xfree (last);
5154 }
5155
5156 dwarf2_queue = dwarf2_queue_tail = NULL;
5157}
5158
5159/* Read in full symbols for PST, and anything it depends on. */
5160
c906108c 5161static void
fba45db2 5162psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 5163{
10b3939b 5164 struct dwarf2_per_cu_data *per_cu;
c906108c 5165 struct cleanup *back_to;
aaa75496
JB
5166 int i;
5167
5168 for (i = 0; i < pst->number_of_dependencies; i++)
5169 if (!pst->dependencies[i]->readin)
5170 {
5171 /* Inform about additional files that need to be read in. */
5172 if (info_verbose)
5173 {
a3f17187 5174 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
5175 fputs_filtered (" ", gdb_stdout);
5176 wrap_here ("");
5177 fputs_filtered ("and ", gdb_stdout);
5178 wrap_here ("");
5179 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 5180 wrap_here (""); /* Flush output. */
aaa75496
JB
5181 gdb_flush (gdb_stdout);
5182 }
5183 psymtab_to_symtab_1 (pst->dependencies[i]);
5184 }
5185
e38df1d0 5186 per_cu = pst->read_symtab_private;
10b3939b
DJ
5187
5188 if (per_cu == NULL)
aaa75496
JB
5189 {
5190 /* It's an include file, no symbols to read for it.
5191 Everything is in the parent symtab. */
5192 pst->readin = 1;
5193 return;
5194 }
c906108c 5195
a0f42c21 5196 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
5197}
5198
dee91e82
DE
5199/* Trivial hash function for die_info: the hash value of a DIE
5200 is its offset in .debug_info for this objfile. */
10b3939b 5201
dee91e82
DE
5202static hashval_t
5203die_hash (const void *item)
10b3939b 5204{
dee91e82 5205 const struct die_info *die = item;
6502dd73 5206
dee91e82
DE
5207 return die->offset.sect_off;
5208}
63d06c5c 5209
dee91e82
DE
5210/* Trivial comparison function for die_info structures: two DIEs
5211 are equal if they have the same offset. */
98bfdba5 5212
dee91e82
DE
5213static int
5214die_eq (const void *item_lhs, const void *item_rhs)
5215{
5216 const struct die_info *die_lhs = item_lhs;
5217 const struct die_info *die_rhs = item_rhs;
c906108c 5218
dee91e82
DE
5219 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5220}
c906108c 5221
dee91e82
DE
5222/* die_reader_func for load_full_comp_unit.
5223 This is identical to read_signatured_type_reader,
5224 but is kept separate for now. */
c906108c 5225
dee91e82
DE
5226static void
5227load_full_comp_unit_reader (const struct die_reader_specs *reader,
5228 gdb_byte *info_ptr,
5229 struct die_info *comp_unit_die,
5230 int has_children,
5231 void *data)
5232{
5233 struct dwarf2_cu *cu = reader->cu;
5234 struct attribute *attr;
6caca83c 5235
dee91e82
DE
5236 gdb_assert (cu->die_hash == NULL);
5237 cu->die_hash =
5238 htab_create_alloc_ex (cu->header.length / 12,
5239 die_hash,
5240 die_eq,
5241 NULL,
5242 &cu->comp_unit_obstack,
5243 hashtab_obstack_allocate,
5244 dummy_obstack_deallocate);
e142c38c 5245
dee91e82
DE
5246 if (has_children)
5247 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5248 &info_ptr, comp_unit_die);
5249 cu->dies = comp_unit_die;
5250 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
5251
5252 /* We try not to read any attributes in this function, because not
9cdd5dbd 5253 all CUs needed for references have been loaded yet, and symbol
10b3939b 5254 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
5255 or we won't be able to build types correctly.
5256 Similarly, if we do not read the producer, we can not apply
5257 producer-specific interpretation. */
9816fde3 5258 prepare_one_comp_unit (cu, cu->dies);
dee91e82 5259}
10b3939b 5260
dee91e82 5261/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 5262
dee91e82
DE
5263static void
5264load_full_comp_unit (struct dwarf2_per_cu_data *this_cu)
5265{
3019eac3 5266 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 5267
fd820528 5268 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader, NULL);
10b3939b
DJ
5269}
5270
3da10d80
KS
5271/* Add a DIE to the delayed physname list. */
5272
5273static void
5274add_to_method_list (struct type *type, int fnfield_index, int index,
5275 const char *name, struct die_info *die,
5276 struct dwarf2_cu *cu)
5277{
5278 struct delayed_method_info mi;
5279 mi.type = type;
5280 mi.fnfield_index = fnfield_index;
5281 mi.index = index;
5282 mi.name = name;
5283 mi.die = die;
5284 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5285}
5286
5287/* A cleanup for freeing the delayed method list. */
5288
5289static void
5290free_delayed_list (void *ptr)
5291{
5292 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5293 if (cu->method_list != NULL)
5294 {
5295 VEC_free (delayed_method_info, cu->method_list);
5296 cu->method_list = NULL;
5297 }
5298}
5299
5300/* Compute the physnames of any methods on the CU's method list.
5301
5302 The computation of method physnames is delayed in order to avoid the
5303 (bad) condition that one of the method's formal parameters is of an as yet
5304 incomplete type. */
5305
5306static void
5307compute_delayed_physnames (struct dwarf2_cu *cu)
5308{
5309 int i;
5310 struct delayed_method_info *mi;
5311 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5312 {
1d06ead6 5313 const char *physname;
3da10d80
KS
5314 struct fn_fieldlist *fn_flp
5315 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 5316 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
5317 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5318 }
5319}
5320
a766d390
DE
5321/* Go objects should be embedded in a DW_TAG_module DIE,
5322 and it's not clear if/how imported objects will appear.
5323 To keep Go support simple until that's worked out,
5324 go back through what we've read and create something usable.
5325 We could do this while processing each DIE, and feels kinda cleaner,
5326 but that way is more invasive.
5327 This is to, for example, allow the user to type "p var" or "b main"
5328 without having to specify the package name, and allow lookups
5329 of module.object to work in contexts that use the expression
5330 parser. */
5331
5332static void
5333fixup_go_packaging (struct dwarf2_cu *cu)
5334{
5335 char *package_name = NULL;
5336 struct pending *list;
5337 int i;
5338
5339 for (list = global_symbols; list != NULL; list = list->next)
5340 {
5341 for (i = 0; i < list->nsyms; ++i)
5342 {
5343 struct symbol *sym = list->symbol[i];
5344
5345 if (SYMBOL_LANGUAGE (sym) == language_go
5346 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5347 {
5348 char *this_package_name = go_symbol_package_name (sym);
5349
5350 if (this_package_name == NULL)
5351 continue;
5352 if (package_name == NULL)
5353 package_name = this_package_name;
5354 else
5355 {
5356 if (strcmp (package_name, this_package_name) != 0)
5357 complaint (&symfile_complaints,
5358 _("Symtab %s has objects from two different Go packages: %s and %s"),
5359 (sym->symtab && sym->symtab->filename
5360 ? sym->symtab->filename
5361 : cu->objfile->name),
5362 this_package_name, package_name);
5363 xfree (this_package_name);
5364 }
5365 }
5366 }
5367 }
5368
5369 if (package_name != NULL)
5370 {
5371 struct objfile *objfile = cu->objfile;
5372 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5373 package_name, objfile);
5374 struct symbol *sym;
5375
5376 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5377
5378 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5379 SYMBOL_SET_LANGUAGE (sym, language_go);
5380 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5381 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5382 e.g., "main" finds the "main" module and not C's main(). */
5383 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5384 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5385 SYMBOL_TYPE (sym) = type;
5386
5387 add_symbol_to_list (sym, &global_symbols);
5388
5389 xfree (package_name);
5390 }
5391}
5392
9cdd5dbd 5393/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
5394 already been loaded into memory. */
5395
5396static void
5397process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
5398{
10b3939b 5399 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 5400 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
5401 CORE_ADDR lowpc, highpc;
5402 struct symtab *symtab;
3da10d80 5403 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
5404 CORE_ADDR baseaddr;
5405
5406 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5407
10b3939b
DJ
5408 buildsym_init ();
5409 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 5410 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
5411
5412 cu->list_in_scope = &file_symbols;
c906108c
SS
5413
5414 /* Do line number decoding in read_file_scope () */
10b3939b 5415 process_die (cu->dies, cu);
c906108c 5416
a766d390
DE
5417 /* For now fudge the Go package. */
5418 if (cu->language == language_go)
5419 fixup_go_packaging (cu);
5420
3da10d80
KS
5421 /* Now that we have processed all the DIEs in the CU, all the types
5422 should be complete, and it should now be safe to compute all of the
5423 physnames. */
5424 compute_delayed_physnames (cu);
5425 do_cleanups (delayed_list_cleanup);
5426
fae299cd
DC
5427 /* Some compilers don't define a DW_AT_high_pc attribute for the
5428 compilation unit. If the DW_AT_high_pc is missing, synthesize
5429 it, by scanning the DIE's below the compilation unit. */
10b3939b 5430 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 5431
613e1657 5432 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 5433
8be455d7 5434 if (symtab != NULL)
c906108c 5435 {
df15bd07 5436 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 5437
8be455d7
JK
5438 /* Set symtab language to language from DW_AT_language. If the
5439 compilation is from a C file generated by language preprocessors, do
5440 not set the language if it was already deduced by start_subfile. */
5441 if (!(cu->language == language_c && symtab->language != language_c))
5442 symtab->language = cu->language;
5443
5444 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5445 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
5446 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5447 there were bugs in prologue debug info, fixed later in GCC-4.5
5448 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
5449
5450 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5451 needed, it would be wrong due to missing DW_AT_producer there.
5452
5453 Still one can confuse GDB by using non-standard GCC compilation
5454 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5455 */
ab260dad 5456 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 5457 symtab->locations_valid = 1;
e0d00bc7
JK
5458
5459 if (gcc_4_minor >= 5)
5460 symtab->epilogue_unwind_valid = 1;
96408a79
SA
5461
5462 symtab->call_site_htab = cu->call_site_htab;
c906108c 5463 }
9291a0cd
TT
5464
5465 if (dwarf2_per_objfile->using_index)
5466 per_cu->v.quick->symtab = symtab;
5467 else
5468 {
5469 struct partial_symtab *pst = per_cu->v.psymtab;
5470 pst->symtab = symtab;
5471 pst->readin = 1;
5472 }
c906108c
SS
5473
5474 do_cleanups (back_to);
5475}
5476
5477/* Process a die and its children. */
5478
5479static void
e7c27a73 5480process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
5481{
5482 switch (die->tag)
5483 {
5484 case DW_TAG_padding:
5485 break;
5486 case DW_TAG_compile_unit:
e7c27a73 5487 read_file_scope (die, cu);
c906108c 5488 break;
348e048f
DE
5489 case DW_TAG_type_unit:
5490 read_type_unit_scope (die, cu);
5491 break;
c906108c 5492 case DW_TAG_subprogram:
c906108c 5493 case DW_TAG_inlined_subroutine:
edb3359d 5494 read_func_scope (die, cu);
c906108c
SS
5495 break;
5496 case DW_TAG_lexical_block:
14898363
L
5497 case DW_TAG_try_block:
5498 case DW_TAG_catch_block:
e7c27a73 5499 read_lexical_block_scope (die, cu);
c906108c 5500 break;
96408a79
SA
5501 case DW_TAG_GNU_call_site:
5502 read_call_site_scope (die, cu);
5503 break;
c906108c 5504 case DW_TAG_class_type:
680b30c7 5505 case DW_TAG_interface_type:
c906108c
SS
5506 case DW_TAG_structure_type:
5507 case DW_TAG_union_type:
134d01f1 5508 process_structure_scope (die, cu);
c906108c
SS
5509 break;
5510 case DW_TAG_enumeration_type:
134d01f1 5511 process_enumeration_scope (die, cu);
c906108c 5512 break;
134d01f1 5513
f792889a
DJ
5514 /* These dies have a type, but processing them does not create
5515 a symbol or recurse to process the children. Therefore we can
5516 read them on-demand through read_type_die. */
c906108c 5517 case DW_TAG_subroutine_type:
72019c9c 5518 case DW_TAG_set_type:
c906108c 5519 case DW_TAG_array_type:
c906108c 5520 case DW_TAG_pointer_type:
c906108c 5521 case DW_TAG_ptr_to_member_type:
c906108c 5522 case DW_TAG_reference_type:
c906108c 5523 case DW_TAG_string_type:
c906108c 5524 break;
134d01f1 5525
c906108c 5526 case DW_TAG_base_type:
a02abb62 5527 case DW_TAG_subrange_type:
cb249c71 5528 case DW_TAG_typedef:
134d01f1
DJ
5529 /* Add a typedef symbol for the type definition, if it has a
5530 DW_AT_name. */
f792889a 5531 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 5532 break;
c906108c 5533 case DW_TAG_common_block:
e7c27a73 5534 read_common_block (die, cu);
c906108c
SS
5535 break;
5536 case DW_TAG_common_inclusion:
5537 break;
d9fa45fe 5538 case DW_TAG_namespace:
63d06c5c 5539 processing_has_namespace_info = 1;
e7c27a73 5540 read_namespace (die, cu);
d9fa45fe 5541 break;
5d7cb8df 5542 case DW_TAG_module:
f55ee35c 5543 processing_has_namespace_info = 1;
5d7cb8df
JK
5544 read_module (die, cu);
5545 break;
d9fa45fe
DC
5546 case DW_TAG_imported_declaration:
5547 case DW_TAG_imported_module:
63d06c5c 5548 processing_has_namespace_info = 1;
27aa8d6a
SW
5549 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5550 || cu->language != language_fortran))
5551 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5552 dwarf_tag_name (die->tag));
5553 read_import_statement (die, cu);
d9fa45fe 5554 break;
c906108c 5555 default:
e7c27a73 5556 new_symbol (die, NULL, cu);
c906108c
SS
5557 break;
5558 }
5559}
5560
94af9270
KS
5561/* A helper function for dwarf2_compute_name which determines whether DIE
5562 needs to have the name of the scope prepended to the name listed in the
5563 die. */
5564
5565static int
5566die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5567{
1c809c68
TT
5568 struct attribute *attr;
5569
94af9270
KS
5570 switch (die->tag)
5571 {
5572 case DW_TAG_namespace:
5573 case DW_TAG_typedef:
5574 case DW_TAG_class_type:
5575 case DW_TAG_interface_type:
5576 case DW_TAG_structure_type:
5577 case DW_TAG_union_type:
5578 case DW_TAG_enumeration_type:
5579 case DW_TAG_enumerator:
5580 case DW_TAG_subprogram:
5581 case DW_TAG_member:
5582 return 1;
5583
5584 case DW_TAG_variable:
c2b0a229 5585 case DW_TAG_constant:
94af9270
KS
5586 /* We only need to prefix "globally" visible variables. These include
5587 any variable marked with DW_AT_external or any variable that
5588 lives in a namespace. [Variables in anonymous namespaces
5589 require prefixing, but they are not DW_AT_external.] */
5590
5591 if (dwarf2_attr (die, DW_AT_specification, cu))
5592 {
5593 struct dwarf2_cu *spec_cu = cu;
9a619af0 5594
94af9270
KS
5595 return die_needs_namespace (die_specification (die, &spec_cu),
5596 spec_cu);
5597 }
5598
1c809c68 5599 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
5600 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5601 && die->parent->tag != DW_TAG_module)
1c809c68
TT
5602 return 0;
5603 /* A variable in a lexical block of some kind does not need a
5604 namespace, even though in C++ such variables may be external
5605 and have a mangled name. */
5606 if (die->parent->tag == DW_TAG_lexical_block
5607 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5608 || die->parent->tag == DW_TAG_catch_block
5609 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5610 return 0;
5611 return 1;
94af9270
KS
5612
5613 default:
5614 return 0;
5615 }
5616}
5617
98bfdba5
PA
5618/* Retrieve the last character from a mem_file. */
5619
5620static void
5621do_ui_file_peek_last (void *object, const char *buffer, long length)
5622{
5623 char *last_char_p = (char *) object;
5624
5625 if (length > 0)
5626 *last_char_p = buffer[length - 1];
5627}
5628
94af9270 5629/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
5630 compute the physname for the object, which include a method's:
5631 - formal parameters (C++/Java),
5632 - receiver type (Go),
5633 - return type (Java).
5634
5635 The term "physname" is a bit confusing.
5636 For C++, for example, it is the demangled name.
5637 For Go, for example, it's the mangled name.
94af9270 5638
af6b7be1
JB
5639 For Ada, return the DIE's linkage name rather than the fully qualified
5640 name. PHYSNAME is ignored..
5641
94af9270
KS
5642 The result is allocated on the objfile_obstack and canonicalized. */
5643
5644static const char *
5645dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5646 int physname)
5647{
bb5ed363
DE
5648 struct objfile *objfile = cu->objfile;
5649
94af9270
KS
5650 if (name == NULL)
5651 name = dwarf2_name (die, cu);
5652
f55ee35c
JK
5653 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5654 compute it by typename_concat inside GDB. */
5655 if (cu->language == language_ada
5656 || (cu->language == language_fortran && physname))
5657 {
5658 /* For Ada unit, we prefer the linkage name over the name, as
5659 the former contains the exported name, which the user expects
5660 to be able to reference. Ideally, we want the user to be able
5661 to reference this entity using either natural or linkage name,
5662 but we haven't started looking at this enhancement yet. */
5663 struct attribute *attr;
5664
5665 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5666 if (attr == NULL)
5667 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5668 if (attr && DW_STRING (attr))
5669 return DW_STRING (attr);
5670 }
5671
94af9270
KS
5672 /* These are the only languages we know how to qualify names in. */
5673 if (name != NULL
f55ee35c
JK
5674 && (cu->language == language_cplus || cu->language == language_java
5675 || cu->language == language_fortran))
94af9270
KS
5676 {
5677 if (die_needs_namespace (die, cu))
5678 {
5679 long length;
0d5cff50 5680 const char *prefix;
94af9270
KS
5681 struct ui_file *buf;
5682
5683 prefix = determine_prefix (die, cu);
5684 buf = mem_fileopen ();
5685 if (*prefix != '\0')
5686 {
f55ee35c
JK
5687 char *prefixed_name = typename_concat (NULL, prefix, name,
5688 physname, cu);
9a619af0 5689
94af9270
KS
5690 fputs_unfiltered (prefixed_name, buf);
5691 xfree (prefixed_name);
5692 }
5693 else
62d5b8da 5694 fputs_unfiltered (name, buf);
94af9270 5695
98bfdba5
PA
5696 /* Template parameters may be specified in the DIE's DW_AT_name, or
5697 as children with DW_TAG_template_type_param or
5698 DW_TAG_value_type_param. If the latter, add them to the name
5699 here. If the name already has template parameters, then
5700 skip this step; some versions of GCC emit both, and
5701 it is more efficient to use the pre-computed name.
5702
5703 Something to keep in mind about this process: it is very
5704 unlikely, or in some cases downright impossible, to produce
5705 something that will match the mangled name of a function.
5706 If the definition of the function has the same debug info,
5707 we should be able to match up with it anyway. But fallbacks
5708 using the minimal symbol, for instance to find a method
5709 implemented in a stripped copy of libstdc++, will not work.
5710 If we do not have debug info for the definition, we will have to
5711 match them up some other way.
5712
5713 When we do name matching there is a related problem with function
5714 templates; two instantiated function templates are allowed to
5715 differ only by their return types, which we do not add here. */
5716
5717 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5718 {
5719 struct attribute *attr;
5720 struct die_info *child;
5721 int first = 1;
5722
5723 die->building_fullname = 1;
5724
5725 for (child = die->child; child != NULL; child = child->sibling)
5726 {
5727 struct type *type;
12df843f 5728 LONGEST value;
98bfdba5
PA
5729 gdb_byte *bytes;
5730 struct dwarf2_locexpr_baton *baton;
5731 struct value *v;
5732
5733 if (child->tag != DW_TAG_template_type_param
5734 && child->tag != DW_TAG_template_value_param)
5735 continue;
5736
5737 if (first)
5738 {
5739 fputs_unfiltered ("<", buf);
5740 first = 0;
5741 }
5742 else
5743 fputs_unfiltered (", ", buf);
5744
5745 attr = dwarf2_attr (child, DW_AT_type, cu);
5746 if (attr == NULL)
5747 {
5748 complaint (&symfile_complaints,
5749 _("template parameter missing DW_AT_type"));
5750 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5751 continue;
5752 }
5753 type = die_type (child, cu);
5754
5755 if (child->tag == DW_TAG_template_type_param)
5756 {
5757 c_print_type (type, "", buf, -1, 0);
5758 continue;
5759 }
5760
5761 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5762 if (attr == NULL)
5763 {
5764 complaint (&symfile_complaints,
3e43a32a
MS
5765 _("template parameter missing "
5766 "DW_AT_const_value"));
98bfdba5
PA
5767 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5768 continue;
5769 }
5770
5771 dwarf2_const_value_attr (attr, type, name,
5772 &cu->comp_unit_obstack, cu,
5773 &value, &bytes, &baton);
5774
5775 if (TYPE_NOSIGN (type))
5776 /* GDB prints characters as NUMBER 'CHAR'. If that's
5777 changed, this can use value_print instead. */
5778 c_printchar (value, type, buf);
5779 else
5780 {
5781 struct value_print_options opts;
5782
5783 if (baton != NULL)
5784 v = dwarf2_evaluate_loc_desc (type, NULL,
5785 baton->data,
5786 baton->size,
5787 baton->per_cu);
5788 else if (bytes != NULL)
5789 {
5790 v = allocate_value (type);
5791 memcpy (value_contents_writeable (v), bytes,
5792 TYPE_LENGTH (type));
5793 }
5794 else
5795 v = value_from_longest (type, value);
5796
3e43a32a
MS
5797 /* Specify decimal so that we do not depend on
5798 the radix. */
98bfdba5
PA
5799 get_formatted_print_options (&opts, 'd');
5800 opts.raw = 1;
5801 value_print (v, buf, &opts);
5802 release_value (v);
5803 value_free (v);
5804 }
5805 }
5806
5807 die->building_fullname = 0;
5808
5809 if (!first)
5810 {
5811 /* Close the argument list, with a space if necessary
5812 (nested templates). */
5813 char last_char = '\0';
5814 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5815 if (last_char == '>')
5816 fputs_unfiltered (" >", buf);
5817 else
5818 fputs_unfiltered (">", buf);
5819 }
5820 }
5821
94af9270
KS
5822 /* For Java and C++ methods, append formal parameter type
5823 information, if PHYSNAME. */
6e70227d 5824
94af9270
KS
5825 if (physname && die->tag == DW_TAG_subprogram
5826 && (cu->language == language_cplus
5827 || cu->language == language_java))
5828 {
5829 struct type *type = read_type_die (die, cu);
5830
3167638f 5831 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
5832
5833 if (cu->language == language_java)
5834 {
5835 /* For java, we must append the return type to method
0963b4bd 5836 names. */
94af9270
KS
5837 if (die->tag == DW_TAG_subprogram)
5838 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5839 0, 0);
5840 }
5841 else if (cu->language == language_cplus)
5842 {
60430eff
DJ
5843 /* Assume that an artificial first parameter is
5844 "this", but do not crash if it is not. RealView
5845 marks unnamed (and thus unused) parameters as
5846 artificial; there is no way to differentiate
5847 the two cases. */
94af9270
KS
5848 if (TYPE_NFIELDS (type) > 0
5849 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 5850 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
5851 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5852 0))))
94af9270
KS
5853 fputs_unfiltered (" const", buf);
5854 }
5855 }
5856
bb5ed363 5857 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
5858 &length);
5859 ui_file_delete (buf);
5860
5861 if (cu->language == language_cplus)
5862 {
5863 char *cname
5864 = dwarf2_canonicalize_name (name, cu,
bb5ed363 5865 &objfile->objfile_obstack);
9a619af0 5866
94af9270
KS
5867 if (cname != NULL)
5868 name = cname;
5869 }
5870 }
5871 }
5872
5873 return name;
5874}
5875
0114d602
DJ
5876/* Return the fully qualified name of DIE, based on its DW_AT_name.
5877 If scope qualifiers are appropriate they will be added. The result
5878 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
5879 not have a name. NAME may either be from a previous call to
5880 dwarf2_name or NULL.
5881
0963b4bd 5882 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
5883
5884static const char *
94af9270 5885dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 5886{
94af9270
KS
5887 return dwarf2_compute_name (name, die, cu, 0);
5888}
0114d602 5889
94af9270
KS
5890/* Construct a physname for the given DIE in CU. NAME may either be
5891 from a previous call to dwarf2_name or NULL. The result will be
5892 allocated on the objfile_objstack or NULL if the DIE does not have a
5893 name.
0114d602 5894
94af9270 5895 The output string will be canonicalized (if C++/Java). */
0114d602 5896
94af9270
KS
5897static const char *
5898dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5899{
bb5ed363 5900 struct objfile *objfile = cu->objfile;
900e11f9
JK
5901 struct attribute *attr;
5902 const char *retval, *mangled = NULL, *canon = NULL;
5903 struct cleanup *back_to;
5904 int need_copy = 1;
5905
5906 /* In this case dwarf2_compute_name is just a shortcut not building anything
5907 on its own. */
5908 if (!die_needs_namespace (die, cu))
5909 return dwarf2_compute_name (name, die, cu, 1);
5910
5911 back_to = make_cleanup (null_cleanup, NULL);
5912
5913 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5914 if (!attr)
5915 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5916
5917 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5918 has computed. */
5919 if (attr && DW_STRING (attr))
5920 {
5921 char *demangled;
5922
5923 mangled = DW_STRING (attr);
5924
5925 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5926 type. It is easier for GDB users to search for such functions as
5927 `name(params)' than `long name(params)'. In such case the minimal
5928 symbol names do not match the full symbol names but for template
5929 functions there is never a need to look up their definition from their
5930 declaration so the only disadvantage remains the minimal symbol
5931 variant `long name(params)' does not have the proper inferior type.
5932 */
5933
a766d390
DE
5934 if (cu->language == language_go)
5935 {
5936 /* This is a lie, but we already lie to the caller new_symbol_full.
5937 new_symbol_full assumes we return the mangled name.
5938 This just undoes that lie until things are cleaned up. */
5939 demangled = NULL;
5940 }
5941 else
5942 {
5943 demangled = cplus_demangle (mangled,
5944 (DMGL_PARAMS | DMGL_ANSI
5945 | (cu->language == language_java
5946 ? DMGL_JAVA | DMGL_RET_POSTFIX
5947 : DMGL_RET_DROP)));
5948 }
900e11f9
JK
5949 if (demangled)
5950 {
5951 make_cleanup (xfree, demangled);
5952 canon = demangled;
5953 }
5954 else
5955 {
5956 canon = mangled;
5957 need_copy = 0;
5958 }
5959 }
5960
5961 if (canon == NULL || check_physname)
5962 {
5963 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5964
5965 if (canon != NULL && strcmp (physname, canon) != 0)
5966 {
5967 /* It may not mean a bug in GDB. The compiler could also
5968 compute DW_AT_linkage_name incorrectly. But in such case
5969 GDB would need to be bug-to-bug compatible. */
5970
5971 complaint (&symfile_complaints,
5972 _("Computed physname <%s> does not match demangled <%s> "
5973 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 5974 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
5975
5976 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5977 is available here - over computed PHYSNAME. It is safer
5978 against both buggy GDB and buggy compilers. */
5979
5980 retval = canon;
5981 }
5982 else
5983 {
5984 retval = physname;
5985 need_copy = 0;
5986 }
5987 }
5988 else
5989 retval = canon;
5990
5991 if (need_copy)
5992 retval = obsavestring (retval, strlen (retval),
bb5ed363 5993 &objfile->objfile_obstack);
900e11f9
JK
5994
5995 do_cleanups (back_to);
5996 return retval;
0114d602
DJ
5997}
5998
27aa8d6a
SW
5999/* Read the import statement specified by the given die and record it. */
6000
6001static void
6002read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6003{
bb5ed363 6004 struct objfile *objfile = cu->objfile;
27aa8d6a 6005 struct attribute *import_attr;
32019081 6006 struct die_info *imported_die, *child_die;
de4affc9 6007 struct dwarf2_cu *imported_cu;
27aa8d6a 6008 const char *imported_name;
794684b6 6009 const char *imported_name_prefix;
13387711
SW
6010 const char *canonical_name;
6011 const char *import_alias;
6012 const char *imported_declaration = NULL;
794684b6 6013 const char *import_prefix;
32019081
JK
6014 VEC (const_char_ptr) *excludes = NULL;
6015 struct cleanup *cleanups;
13387711
SW
6016
6017 char *temp;
27aa8d6a
SW
6018
6019 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6020 if (import_attr == NULL)
6021 {
6022 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6023 dwarf_tag_name (die->tag));
6024 return;
6025 }
6026
de4affc9
CC
6027 imported_cu = cu;
6028 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6029 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
6030 if (imported_name == NULL)
6031 {
6032 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6033
6034 The import in the following code:
6035 namespace A
6036 {
6037 typedef int B;
6038 }
6039
6040 int main ()
6041 {
6042 using A::B;
6043 B b;
6044 return b;
6045 }
6046
6047 ...
6048 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6049 <52> DW_AT_decl_file : 1
6050 <53> DW_AT_decl_line : 6
6051 <54> DW_AT_import : <0x75>
6052 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6053 <59> DW_AT_name : B
6054 <5b> DW_AT_decl_file : 1
6055 <5c> DW_AT_decl_line : 2
6056 <5d> DW_AT_type : <0x6e>
6057 ...
6058 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6059 <76> DW_AT_byte_size : 4
6060 <77> DW_AT_encoding : 5 (signed)
6061
6062 imports the wrong die ( 0x75 instead of 0x58 ).
6063 This case will be ignored until the gcc bug is fixed. */
6064 return;
6065 }
6066
82856980
SW
6067 /* Figure out the local name after import. */
6068 import_alias = dwarf2_name (die, cu);
27aa8d6a 6069
794684b6
SW
6070 /* Figure out where the statement is being imported to. */
6071 import_prefix = determine_prefix (die, cu);
6072
6073 /* Figure out what the scope of the imported die is and prepend it
6074 to the name of the imported die. */
de4affc9 6075 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 6076
f55ee35c
JK
6077 if (imported_die->tag != DW_TAG_namespace
6078 && imported_die->tag != DW_TAG_module)
794684b6 6079 {
13387711
SW
6080 imported_declaration = imported_name;
6081 canonical_name = imported_name_prefix;
794684b6 6082 }
13387711 6083 else if (strlen (imported_name_prefix) > 0)
794684b6 6084 {
13387711
SW
6085 temp = alloca (strlen (imported_name_prefix)
6086 + 2 + strlen (imported_name) + 1);
6087 strcpy (temp, imported_name_prefix);
6088 strcat (temp, "::");
6089 strcat (temp, imported_name);
6090 canonical_name = temp;
794684b6 6091 }
13387711
SW
6092 else
6093 canonical_name = imported_name;
794684b6 6094
32019081
JK
6095 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6096
6097 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6098 for (child_die = die->child; child_die && child_die->tag;
6099 child_die = sibling_die (child_die))
6100 {
6101 /* DWARF-4: A Fortran use statement with a “rename list” may be
6102 represented by an imported module entry with an import attribute
6103 referring to the module and owned entries corresponding to those
6104 entities that are renamed as part of being imported. */
6105
6106 if (child_die->tag != DW_TAG_imported_declaration)
6107 {
6108 complaint (&symfile_complaints,
6109 _("child DW_TAG_imported_declaration expected "
6110 "- DIE at 0x%x [in module %s]"),
b64f50a1 6111 child_die->offset.sect_off, objfile->name);
32019081
JK
6112 continue;
6113 }
6114
6115 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6116 if (import_attr == NULL)
6117 {
6118 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6119 dwarf_tag_name (child_die->tag));
6120 continue;
6121 }
6122
6123 imported_cu = cu;
6124 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6125 &imported_cu);
6126 imported_name = dwarf2_name (imported_die, imported_cu);
6127 if (imported_name == NULL)
6128 {
6129 complaint (&symfile_complaints,
6130 _("child DW_TAG_imported_declaration has unknown "
6131 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 6132 child_die->offset.sect_off, objfile->name);
32019081
JK
6133 continue;
6134 }
6135
6136 VEC_safe_push (const_char_ptr, excludes, imported_name);
6137
6138 process_die (child_die, cu);
6139 }
6140
c0cc3a76
SW
6141 cp_add_using_directive (import_prefix,
6142 canonical_name,
6143 import_alias,
13387711 6144 imported_declaration,
32019081 6145 excludes,
bb5ed363 6146 &objfile->objfile_obstack);
32019081
JK
6147
6148 do_cleanups (cleanups);
27aa8d6a
SW
6149}
6150
ae2de4f8
DE
6151/* Cleanup function for read_file_scope. */
6152
cb1df416
DJ
6153static void
6154free_cu_line_header (void *arg)
6155{
6156 struct dwarf2_cu *cu = arg;
6157
6158 free_line_header (cu->line_header);
6159 cu->line_header = NULL;
6160}
6161
9291a0cd
TT
6162static void
6163find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6164 char **name, char **comp_dir)
6165{
6166 struct attribute *attr;
6167
6168 *name = NULL;
6169 *comp_dir = NULL;
6170
6171 /* Find the filename. Do not use dwarf2_name here, since the filename
6172 is not a source language identifier. */
6173 attr = dwarf2_attr (die, DW_AT_name, cu);
6174 if (attr)
6175 {
6176 *name = DW_STRING (attr);
6177 }
6178
6179 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6180 if (attr)
6181 *comp_dir = DW_STRING (attr);
6182 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6183 {
6184 *comp_dir = ldirname (*name);
6185 if (*comp_dir != NULL)
6186 make_cleanup (xfree, *comp_dir);
6187 }
6188 if (*comp_dir != NULL)
6189 {
6190 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6191 directory, get rid of it. */
6192 char *cp = strchr (*comp_dir, ':');
6193
6194 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6195 *comp_dir = cp + 1;
6196 }
6197
6198 if (*name == NULL)
6199 *name = "<unknown>";
6200}
6201
f3f5162e
DE
6202/* Handle DW_AT_stmt_list for a compilation unit or type unit.
6203 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6204 COMP_DIR is the compilation directory.
6205 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
6206
6207static void
6208handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 6209 const char *comp_dir, int want_line_info)
2ab95328
TT
6210{
6211 struct attribute *attr;
2ab95328 6212
2ab95328
TT
6213 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6214 if (attr)
6215 {
6216 unsigned int line_offset = DW_UNSND (attr);
6217 struct line_header *line_header
3019eac3 6218 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
6219
6220 if (line_header)
dee91e82
DE
6221 {
6222 cu->line_header = line_header;
6223 make_cleanup (free_cu_line_header, cu);
f3f5162e 6224 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
dee91e82 6225 }
2ab95328
TT
6226 }
6227}
6228
ae2de4f8
DE
6229/* Process DW_TAG_compile_unit. */
6230
c906108c 6231static void
e7c27a73 6232read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6233{
dee91e82 6234 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 6235 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 6236 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
6237 CORE_ADDR highpc = ((CORE_ADDR) 0);
6238 struct attribute *attr;
e1024ff1 6239 char *name = NULL;
c906108c
SS
6240 char *comp_dir = NULL;
6241 struct die_info *child_die;
6242 bfd *abfd = objfile->obfd;
e142c38c 6243 CORE_ADDR baseaddr;
6e70227d 6244
e142c38c 6245 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6246
fae299cd 6247 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
6248
6249 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6250 from finish_block. */
2acceee2 6251 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
6252 lowpc = highpc;
6253 lowpc += baseaddr;
6254 highpc += baseaddr;
6255
9291a0cd 6256 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 6257
dee91e82 6258 prepare_one_comp_unit (cu, die);
303b6f5d 6259
f4b8a18d
KW
6260 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6261 standardised yet. As a workaround for the language detection we fall
6262 back to the DW_AT_producer string. */
6263 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6264 cu->language = language_opencl;
6265
3019eac3
DE
6266 /* Similar hack for Go. */
6267 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6268 set_cu_language (DW_LANG_Go, cu);
6269
6270 /* We assume that we're processing GCC output. */
6271 processing_gcc_compilation = 2;
6272
6273 processing_has_namespace_info = 0;
6274
6275 start_symtab (name, comp_dir, lowpc);
6276 record_debugformat ("DWARF 2");
6277 record_producer (cu->producer);
6278
6279 /* Decode line number information if present. We do this before
6280 processing child DIEs, so that the line header table is available
6281 for DW_AT_decl_file. */
6282 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6283
6284 /* Process all dies in compilation unit. */
6285 if (die->child != NULL)
6286 {
6287 child_die = die->child;
6288 while (child_die && child_die->tag)
6289 {
6290 process_die (child_die, cu);
6291 child_die = sibling_die (child_die);
6292 }
6293 }
6294
6295 /* Decode macro information, if present. Dwarf 2 macro information
6296 refers to information in the line number info statement program
6297 header, so we can only read it if we've read the header
6298 successfully. */
6299 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6300 if (attr && cu->line_header)
6301 {
6302 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6303 complaint (&symfile_complaints,
6304 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6305
6306 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
6307 comp_dir, abfd, cu,
fceca515
DE
6308 &dwarf2_per_objfile->macro, 1,
6309 ".debug_macro");
3019eac3
DE
6310 }
6311 else
6312 {
6313 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6314 if (attr && cu->line_header)
6315 {
6316 unsigned int macro_offset = DW_UNSND (attr);
6317
6318 dwarf_decode_macros (cu->line_header, macro_offset,
6319 comp_dir, abfd, cu,
fceca515
DE
6320 &dwarf2_per_objfile->macinfo, 0,
6321 ".debug_macinfo");
3019eac3
DE
6322 }
6323 }
6324
6325 do_cleanups (back_to);
6326}
6327
6328/* Process DW_TAG_type_unit.
6329 For TUs we want to skip the first top level sibling if it's not the
6330 actual type being defined by this TU. In this case the first top
6331 level sibling is there to provide context only. */
6332
6333static void
6334read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6335{
6336 struct objfile *objfile = cu->objfile;
6337 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6338 CORE_ADDR lowpc;
6339 struct attribute *attr;
6340 char *name = NULL;
6341 char *comp_dir = NULL;
6342 struct die_info *child_die;
6343 bfd *abfd = objfile->obfd;
6344
6345 /* start_symtab needs a low pc, but we don't really have one.
6346 Do what read_file_scope would do in the absence of such info. */
6347 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6348
6349 /* Find the filename. Do not use dwarf2_name here, since the filename
6350 is not a source language identifier. */
6351 attr = dwarf2_attr (die, DW_AT_name, cu);
6352 if (attr)
6353 name = DW_STRING (attr);
6354
6355 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6356 if (attr)
6357 comp_dir = DW_STRING (attr);
6358 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6359 {
6360 comp_dir = ldirname (name);
6361 if (comp_dir != NULL)
6362 make_cleanup (xfree, comp_dir);
6363 }
6364
6365 if (name == NULL)
6366 name = "<unknown>";
6367
6368 prepare_one_comp_unit (cu, die);
6369
6370 /* We assume that we're processing GCC output. */
6371 processing_gcc_compilation = 2;
6372
6373 processing_has_namespace_info = 0;
6374
6375 start_symtab (name, comp_dir, lowpc);
6376 record_debugformat ("DWARF 2");
6377 record_producer (cu->producer);
6378
6379 /* Decode line number information if present. We do this before
6380 processing child DIEs, so that the line header table is available
6381 for DW_AT_decl_file.
6382 We don't need the pc/line-number mapping for type units. */
6383 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6384
6385 /* Process the dies in the type unit. */
6386 if (die->child == NULL)
6387 {
6388 dump_die_for_error (die);
6389 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6390 bfd_get_filename (abfd));
6391 }
6392
6393 child_die = die->child;
6394
6395 while (child_die && child_die->tag)
6396 {
6397 process_die (child_die, cu);
6398
6399 child_die = sibling_die (child_die);
6400 }
6401
6402 do_cleanups (back_to);
6403}
6404\f
6405/* DWO files. */
6406
6407static hashval_t
6408hash_dwo_file (const void *item)
6409{
6410 const struct dwo_file *dwo_file = item;
6411
6412 return htab_hash_string (dwo_file->dwo_name);
6413}
6414
6415static int
6416eq_dwo_file (const void *item_lhs, const void *item_rhs)
6417{
6418 const struct dwo_file *lhs = item_lhs;
6419 const struct dwo_file *rhs = item_rhs;
6420
6421 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6422}
6423
6424/* Allocate a hash table for DWO files. */
6425
6426static htab_t
6427allocate_dwo_file_hash_table (void)
6428{
6429 struct objfile *objfile = dwarf2_per_objfile->objfile;
6430
6431 return htab_create_alloc_ex (41,
6432 hash_dwo_file,
6433 eq_dwo_file,
6434 NULL,
6435 &objfile->objfile_obstack,
6436 hashtab_obstack_allocate,
6437 dummy_obstack_deallocate);
6438}
6439
6440static hashval_t
6441hash_dwo_unit (const void *item)
6442{
6443 const struct dwo_unit *dwo_unit = item;
6444
6445 /* This drops the top 32 bits of the id, but is ok for a hash. */
6446 return dwo_unit->signature;
6447}
6448
6449static int
6450eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6451{
6452 const struct dwo_unit *lhs = item_lhs;
6453 const struct dwo_unit *rhs = item_rhs;
6454
6455 /* The signature is assumed to be unique within the DWO file.
6456 So while object file CU dwo_id's always have the value zero,
6457 that's OK, assuming each object file DWO file has only one CU,
6458 and that's the rule for now. */
6459 return lhs->signature == rhs->signature;
6460}
6461
6462/* Allocate a hash table for DWO CUs,TUs.
6463 There is one of these tables for each of CUs,TUs for each DWO file. */
6464
6465static htab_t
6466allocate_dwo_unit_table (struct objfile *objfile)
6467{
6468 /* Start out with a pretty small number.
6469 Generally DWO files contain only one CU and maybe some TUs. */
6470 return htab_create_alloc_ex (3,
6471 hash_dwo_unit,
6472 eq_dwo_unit,
6473 NULL,
6474 &objfile->objfile_obstack,
6475 hashtab_obstack_allocate,
6476 dummy_obstack_deallocate);
6477}
6478
6479/* This function is mapped across the sections and remembers the offset and
6480 size of each of the DWO debugging sections we are interested in. */
6481
6482static void
6483dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6484{
6485 struct dwo_file *dwo_file = dwo_file_ptr;
6486 const struct dwo_section_names *names = &dwo_section_names;
6487
6488 if (section_is_p (sectp->name, &names->abbrev_dwo))
6489 {
6490 dwo_file->sections.abbrev.asection = sectp;
6491 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6492 }
6493 else if (section_is_p (sectp->name, &names->info_dwo))
6494 {
6495 dwo_file->sections.info.asection = sectp;
6496 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6497 }
6498 else if (section_is_p (sectp->name, &names->line_dwo))
6499 {
6500 dwo_file->sections.line.asection = sectp;
6501 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6502 }
6503 else if (section_is_p (sectp->name, &names->loc_dwo))
6504 {
6505 dwo_file->sections.loc.asection = sectp;
6506 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6507 }
6508 else if (section_is_p (sectp->name, &names->str_dwo))
6509 {
6510 dwo_file->sections.str.asection = sectp;
6511 dwo_file->sections.str.size = bfd_get_section_size (sectp);
6512 }
6513 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
6514 {
6515 dwo_file->sections.str_offsets.asection = sectp;
6516 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
6517 }
6518 else if (section_is_p (sectp->name, &names->types_dwo))
6519 {
6520 struct dwarf2_section_info type_section;
6521
6522 memset (&type_section, 0, sizeof (type_section));
6523 type_section.asection = sectp;
6524 type_section.size = bfd_get_section_size (sectp);
6525 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
6526 &type_section);
6527 }
6528}
6529
6530/* Structure used to pass data to create_debug_info_hash_table_reader. */
6531
6532struct create_dwo_info_table_data
6533{
6534 struct dwo_file *dwo_file;
6535 htab_t cu_htab;
6536};
6537
6538/* die_reader_func for create_debug_info_hash_table. */
6539
6540static void
6541create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
6542 gdb_byte *info_ptr,
6543 struct die_info *comp_unit_die,
6544 int has_children,
6545 void *datap)
6546{
6547 struct dwarf2_cu *cu = reader->cu;
6548 struct objfile *objfile = dwarf2_per_objfile->objfile;
6549 sect_offset offset = cu->per_cu->offset;
6550 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
6551 struct create_dwo_info_table_data *data = datap;
6552 struct dwo_file *dwo_file = data->dwo_file;
6553 htab_t cu_htab = data->cu_htab;
6554 void **slot;
6555 struct attribute *attr;
6556 struct dwo_unit *dwo_unit;
6557
6558 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6559 if (attr == NULL)
6560 {
6561 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
6562 " its dwo_id [in module %s]"),
6563 offset.sect_off, dwo_file->dwo_name);
6564 return;
6565 }
6566
6567 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
6568 dwo_unit->dwo_file = dwo_file;
6569 dwo_unit->signature = DW_UNSND (attr);
6570 dwo_unit->info_or_types_section = section;
6571 dwo_unit->offset = offset;
6572 dwo_unit->length = cu->per_cu->length;
6573
6574 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
6575 gdb_assert (slot != NULL);
6576 if (*slot != NULL)
6577 {
6578 const struct dwo_unit *dup_dwo_unit = *slot;
6579
6580 complaint (&symfile_complaints,
6581 _("debug entry at offset 0x%x is duplicate to the entry at"
6582 " offset 0x%x, dwo_id 0x%s [in module %s]"),
6583 offset.sect_off, dup_dwo_unit->offset.sect_off,
6584 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
6585 dwo_file->dwo_name);
6586 }
6587 else
6588 *slot = dwo_unit;
6589
6590 if (dwarf2_die_debug)
6591 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
6592 offset.sect_off,
6593 phex (dwo_unit->signature,
6594 sizeof (dwo_unit->signature)));
6595}
6596
6597/* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
6598
6599static htab_t
6600create_debug_info_hash_table (struct dwo_file *dwo_file)
6601{
6602 struct objfile *objfile = dwarf2_per_objfile->objfile;
6603 struct dwarf2_section_info *section = &dwo_file->sections.info;
6604 bfd *abfd;
6605 htab_t cu_htab;
6606 gdb_byte *info_ptr, *end_ptr;
6607 struct create_dwo_info_table_data create_dwo_info_table_data;
6608
6609 dwarf2_read_section (objfile, section);
6610 info_ptr = section->buffer;
6611
6612 if (info_ptr == NULL)
6613 return NULL;
6614
6615 /* We can't set abfd until now because the section may be empty or
6616 not present, in which case section->asection will be NULL. */
6617 abfd = section->asection->owner;
6618
6619 if (dwarf2_die_debug)
6620 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
6621 bfd_get_filename (abfd));
6622
6623 cu_htab = allocate_dwo_unit_table (objfile);
6624
6625 create_dwo_info_table_data.dwo_file = dwo_file;
6626 create_dwo_info_table_data.cu_htab = cu_htab;
6627
6628 end_ptr = info_ptr + section->size;
6629 while (info_ptr < end_ptr)
6630 {
6631 struct dwarf2_per_cu_data per_cu;
6632
6633 memset (&per_cu, 0, sizeof (per_cu));
6634 per_cu.objfile = objfile;
6635 per_cu.is_debug_types = 0;
6636 per_cu.offset.sect_off = info_ptr - section->buffer;
6637 per_cu.info_or_types_section = section;
6638
6639 init_cutu_and_read_dies_no_follow (&per_cu,
6640 &dwo_file->sections.abbrev,
6641 dwo_file,
6642 create_debug_info_hash_table_reader,
6643 &create_dwo_info_table_data);
6644
6645 info_ptr += per_cu.length;
6646 }
6647
6648 return cu_htab;
6649}
6650
6651/* Subroutine of open_dwo_file to simplify it.
6652 Open the file specified by FILE_NAME and hand it off to BFD for
6653 preliminary analysis. Return a newly initialized bfd *, which
6654 includes a canonicalized copy of FILE_NAME.
6655 In case of trouble, return NULL.
6656 NOTE: This function is derived from symfile_bfd_open. */
6657
6658static bfd *
6659try_open_dwo_file (const char *file_name)
6660{
6661 bfd *sym_bfd;
6662 int desc;
6663 char *absolute_name;
6664 char *name;
6665
6666 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
6667 O_RDONLY | O_BINARY, &absolute_name);
6668 if (desc < 0)
6669 return NULL;
6670
6671 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
6672 if (!sym_bfd)
6673 {
6674 close (desc);
6675 xfree (absolute_name);
6676 return NULL;
6677 }
6678 bfd_set_cacheable (sym_bfd, 1);
6679
6680 if (!bfd_check_format (sym_bfd, bfd_object))
6681 {
6682 bfd_close (sym_bfd); /* This also closes desc. */
6683 xfree (absolute_name);
6684 return NULL;
6685 }
6686
6687 /* bfd_usrdata exists for applications and libbfd must not touch it. */
6688 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
6689
6690 return sym_bfd;
6691}
6692
6693/* Try to open DWO file DWO_NAME.
6694 COMP_DIR is the DW_AT_comp_dir attribute.
6695 The result is the bfd handle of the file.
6696 If there is a problem finding or opening the file, return NULL.
6697 Upon success, the canonicalized path of the file is stored in the bfd,
6698 same as symfile_bfd_open. */
6699
6700static bfd *
6701open_dwo_file (const char *dwo_name, const char *comp_dir)
6702{
6703 bfd *abfd;
6704 char *path_to_try, *debug_dir;
6705
6706 if (IS_ABSOLUTE_PATH (dwo_name))
6707 return try_open_dwo_file (dwo_name);
6708
6709 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
6710
6711 if (comp_dir != NULL)
6712 {
6713 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
6714
6715 /* NOTE: If comp_dir is a relative path, this will also try the
6716 search path, which seems useful. */
6717 abfd = try_open_dwo_file (path_to_try);
6718 xfree (path_to_try);
6719 if (abfd != NULL)
6720 return abfd;
6721 }
6722
6723 /* That didn't work, try debug-file-directory, which, despite its name,
6724 is a list of paths. */
6725
6726 if (*debug_file_directory == '\0')
6727 return NULL;
6728
6729 return try_open_dwo_file (dwo_name);
6730}
6731
6732/* Initialize the use of the DWO file specified by DWO_NAME. */
6733
6734static struct dwo_file *
6735init_dwo_file (const char *dwo_name, const char *comp_dir)
6736{
6737 struct objfile *objfile = dwarf2_per_objfile->objfile;
6738 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6739 struct dwo_file);
6740 bfd *abfd;
6741 struct cleanup *cleanups;
6742
6743 if (dwarf2_die_debug)
6744 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
6745
6746 abfd = open_dwo_file (dwo_name, comp_dir);
6747 if (abfd == NULL)
6748 return NULL;
6749 dwo_file->dwo_name = dwo_name;
6750 dwo_file->dwo_bfd = abfd;
6751
6752 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
6753
6754 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
6755
6756 dwo_file->cus = create_debug_info_hash_table (dwo_file);
6757
6758 dwo_file->tus = create_debug_types_hash_table (dwo_file,
6759 dwo_file->sections.types);
6760
6761 discard_cleanups (cleanups);
6762
6763 return dwo_file;
6764}
6765
6766/* Lookup DWO file DWO_NAME. */
6767
6768static struct dwo_file *
6769lookup_dwo_file (char *dwo_name, const char *comp_dir)
6770{
6771 struct dwo_file *dwo_file;
6772 struct dwo_file find_entry;
6773 void **slot;
6774
6775 if (dwarf2_per_objfile->dwo_files == NULL)
6776 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
6777
6778 /* Have we already seen this DWO file? */
6779 find_entry.dwo_name = dwo_name;
6780 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
6781
6782 /* If not, read it in and build a table of the DWOs it contains. */
6783 if (*slot == NULL)
6784 *slot = init_dwo_file (dwo_name, comp_dir);
6785
6786 /* NOTE: This will be NULL if unable to open the file. */
6787 dwo_file = *slot;
6788
6789 return dwo_file;
6790}
6791
6792/* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
6793 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
6794 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
6795 nomenclature as TUs).
6796 The result is the DWO CU or NULL if we didn't find it
6797 (dwo_id mismatch or couldn't find the DWO file). */
6798
6799static struct dwo_unit *
6800lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
6801 char *dwo_name, const char *comp_dir,
6802 ULONGEST signature)
6803{
6804 struct objfile *objfile = dwarf2_per_objfile->objfile;
6805 struct dwo_file *dwo_file;
6806
6807 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
6808 if (dwo_file == NULL)
6809 return NULL;
6810
6811 /* Look up the DWO using its signature(dwo_id). */
6812
6813 if (dwo_file->cus != NULL)
6814 {
6815 struct dwo_unit find_dwo_cu, *dwo_cu;
6816
6817 find_dwo_cu.signature = signature;
6818 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
a766d390 6819
3019eac3
DE
6820 if (dwo_cu != NULL)
6821 return dwo_cu;
6822 }
c906108c 6823
3019eac3 6824 /* We didn't find it. This must mean a dwo_id mismatch. */
df8a16a1 6825
3019eac3
DE
6826 complaint (&symfile_complaints,
6827 _("Could not find DWO CU referenced by CU at offset 0x%x"
6828 " [in module %s]"),
6829 this_cu->offset.sect_off, objfile->name);
6830 return NULL;
6831}
c906108c 6832
3019eac3
DE
6833/* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
6834 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
6835 The result is the DWO CU or NULL if we didn't find it
6836 (dwo_id mismatch or couldn't find the DWO file). */
debd256d 6837
3019eac3
DE
6838static struct dwo_unit *
6839lookup_dwo_type_unit (struct signatured_type *this_tu,
6840 char *dwo_name, const char *comp_dir)
6841{
6842 struct objfile *objfile = dwarf2_per_objfile->objfile;
6843 struct dwo_file *dwo_file;
6844 struct dwo_unit find_dwo_tu, *dwo_tu;
cb1df416 6845
3019eac3
DE
6846 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
6847 if (dwo_file == NULL)
6848 return NULL;
cf2c3c16 6849
3019eac3
DE
6850 /* Look up the DWO using its signature(dwo_id). */
6851
6852 if (dwo_file->tus != NULL)
cf2c3c16 6853 {
3019eac3 6854 struct dwo_unit find_dwo_tu, *dwo_tu;
9a619af0 6855
3019eac3
DE
6856 find_dwo_tu.signature = this_tu->signature;
6857 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
6858
6859 if (dwo_tu != NULL)
6860 return dwo_tu;
2e276125 6861 }
9cdd5dbd 6862
3019eac3
DE
6863 /* We didn't find it. This must mean a dwo_id mismatch. */
6864
6865 complaint (&symfile_complaints,
6866 _("Could not find DWO TU referenced by TU at offset 0x%x"
6867 " [in module %s]"),
6868 this_tu->per_cu.offset.sect_off, objfile->name);
6869 return NULL;
5fb290d7
DJ
6870}
6871
3019eac3
DE
6872/* Free all resources associated with DWO_FILE.
6873 Close the DWO file and munmap the sections.
6874 All memory should be on the objfile obstack. */
348e048f
DE
6875
6876static void
3019eac3 6877free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 6878{
3019eac3
DE
6879 int ix;
6880 struct dwarf2_section_info *section;
348e048f 6881
3019eac3
DE
6882 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
6883 bfd_close (dwo_file->dwo_bfd);
348e048f 6884
3019eac3
DE
6885 munmap_section_buffer (&dwo_file->sections.abbrev);
6886 munmap_section_buffer (&dwo_file->sections.info);
6887 munmap_section_buffer (&dwo_file->sections.line);
6888 munmap_section_buffer (&dwo_file->sections.loc);
6889 munmap_section_buffer (&dwo_file->sections.str);
6890 munmap_section_buffer (&dwo_file->sections.str_offsets);
348e048f 6891
3019eac3
DE
6892 for (ix = 0;
6893 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
6894 ix, section);
6895 ++ix)
6896 munmap_section_buffer (section);
348e048f 6897
3019eac3
DE
6898 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
6899}
348e048f 6900
3019eac3 6901/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 6902
3019eac3
DE
6903static void
6904free_dwo_file_cleanup (void *arg)
6905{
6906 struct dwo_file *dwo_file = (struct dwo_file *) arg;
6907 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 6908
3019eac3
DE
6909 free_dwo_file (dwo_file, objfile);
6910}
348e048f 6911
3019eac3 6912/* Traversal function for free_dwo_files. */
2ab95328 6913
3019eac3
DE
6914static int
6915free_dwo_file_from_slot (void **slot, void *info)
6916{
6917 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
6918 struct objfile *objfile = (struct objfile *) info;
348e048f 6919
3019eac3 6920 free_dwo_file (dwo_file, objfile);
348e048f 6921
3019eac3
DE
6922 return 1;
6923}
348e048f 6924
3019eac3 6925/* Free all resources associated with DWO_FILES. */
348e048f 6926
3019eac3
DE
6927static void
6928free_dwo_files (htab_t dwo_files, struct objfile *objfile)
6929{
6930 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 6931}
3019eac3
DE
6932\f
6933/* Read in various DIEs. */
348e048f 6934
d389af10
JK
6935/* qsort helper for inherit_abstract_dies. */
6936
6937static int
6938unsigned_int_compar (const void *ap, const void *bp)
6939{
6940 unsigned int a = *(unsigned int *) ap;
6941 unsigned int b = *(unsigned int *) bp;
6942
6943 return (a > b) - (b > a);
6944}
6945
6946/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
6947 Inherit only the children of the DW_AT_abstract_origin DIE not being
6948 already referenced by DW_AT_abstract_origin from the children of the
6949 current DIE. */
d389af10
JK
6950
6951static void
6952inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
6953{
6954 struct die_info *child_die;
6955 unsigned die_children_count;
6956 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
6957 sect_offset *offsets;
6958 sect_offset *offsets_end, *offsetp;
d389af10
JK
6959 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
6960 struct die_info *origin_die;
6961 /* Iterator of the ORIGIN_DIE children. */
6962 struct die_info *origin_child_die;
6963 struct cleanup *cleanups;
6964 struct attribute *attr;
cd02d79d
PA
6965 struct dwarf2_cu *origin_cu;
6966 struct pending **origin_previous_list_in_scope;
d389af10
JK
6967
6968 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6969 if (!attr)
6970 return;
6971
cd02d79d
PA
6972 /* Note that following die references may follow to a die in a
6973 different cu. */
6974
6975 origin_cu = cu;
6976 origin_die = follow_die_ref (die, attr, &origin_cu);
6977
6978 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
6979 symbols in. */
6980 origin_previous_list_in_scope = origin_cu->list_in_scope;
6981 origin_cu->list_in_scope = cu->list_in_scope;
6982
edb3359d
DJ
6983 if (die->tag != origin_die->tag
6984 && !(die->tag == DW_TAG_inlined_subroutine
6985 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
6986 complaint (&symfile_complaints,
6987 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 6988 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
6989
6990 child_die = die->child;
6991 die_children_count = 0;
6992 while (child_die && child_die->tag)
6993 {
6994 child_die = sibling_die (child_die);
6995 die_children_count++;
6996 }
6997 offsets = xmalloc (sizeof (*offsets) * die_children_count);
6998 cleanups = make_cleanup (xfree, offsets);
6999
7000 offsets_end = offsets;
7001 child_die = die->child;
7002 while (child_die && child_die->tag)
7003 {
c38f313d
DJ
7004 /* For each CHILD_DIE, find the corresponding child of
7005 ORIGIN_DIE. If there is more than one layer of
7006 DW_AT_abstract_origin, follow them all; there shouldn't be,
7007 but GCC versions at least through 4.4 generate this (GCC PR
7008 40573). */
7009 struct die_info *child_origin_die = child_die;
cd02d79d 7010 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 7011
c38f313d
DJ
7012 while (1)
7013 {
cd02d79d
PA
7014 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7015 child_origin_cu);
c38f313d
DJ
7016 if (attr == NULL)
7017 break;
cd02d79d
PA
7018 child_origin_die = follow_die_ref (child_origin_die, attr,
7019 &child_origin_cu);
c38f313d
DJ
7020 }
7021
d389af10
JK
7022 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7023 counterpart may exist. */
c38f313d 7024 if (child_origin_die != child_die)
d389af10 7025 {
edb3359d
DJ
7026 if (child_die->tag != child_origin_die->tag
7027 && !(child_die->tag == DW_TAG_inlined_subroutine
7028 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7029 complaint (&symfile_complaints,
7030 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7031 "different tags"), child_die->offset.sect_off,
7032 child_origin_die->offset.sect_off);
c38f313d
DJ
7033 if (child_origin_die->parent != origin_die)
7034 complaint (&symfile_complaints,
7035 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7036 "different parents"), child_die->offset.sect_off,
7037 child_origin_die->offset.sect_off);
c38f313d
DJ
7038 else
7039 *offsets_end++ = child_origin_die->offset;
d389af10
JK
7040 }
7041 child_die = sibling_die (child_die);
7042 }
7043 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7044 unsigned_int_compar);
7045 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 7046 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
7047 complaint (&symfile_complaints,
7048 _("Multiple children of DIE 0x%x refer "
7049 "to DIE 0x%x as their abstract origin"),
b64f50a1 7050 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
7051
7052 offsetp = offsets;
7053 origin_child_die = origin_die->child;
7054 while (origin_child_die && origin_child_die->tag)
7055 {
7056 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
7057 while (offsetp < offsets_end
7058 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 7059 offsetp++;
b64f50a1
JK
7060 if (offsetp >= offsets_end
7061 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
7062 {
7063 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 7064 process_die (origin_child_die, origin_cu);
d389af10
JK
7065 }
7066 origin_child_die = sibling_die (origin_child_die);
7067 }
cd02d79d 7068 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
7069
7070 do_cleanups (cleanups);
7071}
7072
c906108c 7073static void
e7c27a73 7074read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7075{
e7c27a73 7076 struct objfile *objfile = cu->objfile;
52f0bd74 7077 struct context_stack *new;
c906108c
SS
7078 CORE_ADDR lowpc;
7079 CORE_ADDR highpc;
7080 struct die_info *child_die;
edb3359d 7081 struct attribute *attr, *call_line, *call_file;
c906108c 7082 char *name;
e142c38c 7083 CORE_ADDR baseaddr;
801e3a5b 7084 struct block *block;
edb3359d 7085 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
7086 VEC (symbolp) *template_args = NULL;
7087 struct template_symbol *templ_func = NULL;
edb3359d
DJ
7088
7089 if (inlined_func)
7090 {
7091 /* If we do not have call site information, we can't show the
7092 caller of this inlined function. That's too confusing, so
7093 only use the scope for local variables. */
7094 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7095 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7096 if (call_line == NULL || call_file == NULL)
7097 {
7098 read_lexical_block_scope (die, cu);
7099 return;
7100 }
7101 }
c906108c 7102
e142c38c
DJ
7103 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7104
94af9270 7105 name = dwarf2_name (die, cu);
c906108c 7106
e8d05480
JB
7107 /* Ignore functions with missing or empty names. These are actually
7108 illegal according to the DWARF standard. */
7109 if (name == NULL)
7110 {
7111 complaint (&symfile_complaints,
b64f50a1
JK
7112 _("missing name for subprogram DIE at %d"),
7113 die->offset.sect_off);
e8d05480
JB
7114 return;
7115 }
7116
7117 /* Ignore functions with missing or invalid low and high pc attributes. */
7118 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7119 {
ae4d0c03
PM
7120 attr = dwarf2_attr (die, DW_AT_external, cu);
7121 if (!attr || !DW_UNSND (attr))
7122 complaint (&symfile_complaints,
3e43a32a
MS
7123 _("cannot get low and high bounds "
7124 "for subprogram DIE at %d"),
b64f50a1 7125 die->offset.sect_off);
e8d05480
JB
7126 return;
7127 }
c906108c
SS
7128
7129 lowpc += baseaddr;
7130 highpc += baseaddr;
7131
34eaf542
TT
7132 /* If we have any template arguments, then we must allocate a
7133 different sort of symbol. */
7134 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7135 {
7136 if (child_die->tag == DW_TAG_template_type_param
7137 || child_die->tag == DW_TAG_template_value_param)
7138 {
7139 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7140 struct template_symbol);
7141 templ_func->base.is_cplus_template_function = 1;
7142 break;
7143 }
7144 }
7145
c906108c 7146 new = push_context (0, lowpc);
34eaf542
TT
7147 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7148 (struct symbol *) templ_func);
4c2df51b 7149
4cecd739
DJ
7150 /* If there is a location expression for DW_AT_frame_base, record
7151 it. */
e142c38c 7152 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 7153 if (attr)
c034e007
AC
7154 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7155 expression is being recorded directly in the function's symbol
7156 and not in a separate frame-base object. I guess this hack is
7157 to avoid adding some sort of frame-base adjunct/annex to the
7158 function's symbol :-(. The problem with doing this is that it
7159 results in a function symbol with a location expression that
7160 has nothing to do with the location of the function, ouch! The
7161 relationship should be: a function's symbol has-a frame base; a
7162 frame-base has-a location expression. */
e7c27a73 7163 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 7164
e142c38c 7165 cu->list_in_scope = &local_symbols;
c906108c 7166
639d11d3 7167 if (die->child != NULL)
c906108c 7168 {
639d11d3 7169 child_die = die->child;
c906108c
SS
7170 while (child_die && child_die->tag)
7171 {
34eaf542
TT
7172 if (child_die->tag == DW_TAG_template_type_param
7173 || child_die->tag == DW_TAG_template_value_param)
7174 {
7175 struct symbol *arg = new_symbol (child_die, NULL, cu);
7176
f1078f66
DJ
7177 if (arg != NULL)
7178 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7179 }
7180 else
7181 process_die (child_die, cu);
c906108c
SS
7182 child_die = sibling_die (child_die);
7183 }
7184 }
7185
d389af10
JK
7186 inherit_abstract_dies (die, cu);
7187
4a811a97
UW
7188 /* If we have a DW_AT_specification, we might need to import using
7189 directives from the context of the specification DIE. See the
7190 comment in determine_prefix. */
7191 if (cu->language == language_cplus
7192 && dwarf2_attr (die, DW_AT_specification, cu))
7193 {
7194 struct dwarf2_cu *spec_cu = cu;
7195 struct die_info *spec_die = die_specification (die, &spec_cu);
7196
7197 while (spec_die)
7198 {
7199 child_die = spec_die->child;
7200 while (child_die && child_die->tag)
7201 {
7202 if (child_die->tag == DW_TAG_imported_module)
7203 process_die (child_die, spec_cu);
7204 child_die = sibling_die (child_die);
7205 }
7206
7207 /* In some cases, GCC generates specification DIEs that
7208 themselves contain DW_AT_specification attributes. */
7209 spec_die = die_specification (spec_die, &spec_cu);
7210 }
7211 }
7212
c906108c
SS
7213 new = pop_context ();
7214 /* Make a block for the local symbols within. */
801e3a5b
JB
7215 block = finish_block (new->name, &local_symbols, new->old_blocks,
7216 lowpc, highpc, objfile);
7217
df8a16a1 7218 /* For C++, set the block's scope. */
f55ee35c 7219 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 7220 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 7221 determine_prefix (die, cu),
df8a16a1
DJ
7222 processing_has_namespace_info);
7223
801e3a5b
JB
7224 /* If we have address ranges, record them. */
7225 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 7226
34eaf542
TT
7227 /* Attach template arguments to function. */
7228 if (! VEC_empty (symbolp, template_args))
7229 {
7230 gdb_assert (templ_func != NULL);
7231
7232 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7233 templ_func->template_arguments
7234 = obstack_alloc (&objfile->objfile_obstack,
7235 (templ_func->n_template_arguments
7236 * sizeof (struct symbol *)));
7237 memcpy (templ_func->template_arguments,
7238 VEC_address (symbolp, template_args),
7239 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7240 VEC_free (symbolp, template_args);
7241 }
7242
208d8187
JB
7243 /* In C++, we can have functions nested inside functions (e.g., when
7244 a function declares a class that has methods). This means that
7245 when we finish processing a function scope, we may need to go
7246 back to building a containing block's symbol lists. */
7247 local_symbols = new->locals;
7248 param_symbols = new->params;
27aa8d6a 7249 using_directives = new->using_directives;
208d8187 7250
921e78cf
JB
7251 /* If we've finished processing a top-level function, subsequent
7252 symbols go in the file symbol list. */
7253 if (outermost_context_p ())
e142c38c 7254 cu->list_in_scope = &file_symbols;
c906108c
SS
7255}
7256
7257/* Process all the DIES contained within a lexical block scope. Start
7258 a new scope, process the dies, and then close the scope. */
7259
7260static void
e7c27a73 7261read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7262{
e7c27a73 7263 struct objfile *objfile = cu->objfile;
52f0bd74 7264 struct context_stack *new;
c906108c
SS
7265 CORE_ADDR lowpc, highpc;
7266 struct die_info *child_die;
e142c38c
DJ
7267 CORE_ADDR baseaddr;
7268
7269 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
7270
7271 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
7272 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7273 as multiple lexical blocks? Handling children in a sane way would
6e70227d 7274 be nasty. Might be easier to properly extend generic blocks to
af34e669 7275 describe ranges. */
d85a05f0 7276 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
7277 return;
7278 lowpc += baseaddr;
7279 highpc += baseaddr;
7280
7281 push_context (0, lowpc);
639d11d3 7282 if (die->child != NULL)
c906108c 7283 {
639d11d3 7284 child_die = die->child;
c906108c
SS
7285 while (child_die && child_die->tag)
7286 {
e7c27a73 7287 process_die (child_die, cu);
c906108c
SS
7288 child_die = sibling_die (child_die);
7289 }
7290 }
7291 new = pop_context ();
7292
8540c487 7293 if (local_symbols != NULL || using_directives != NULL)
c906108c 7294 {
801e3a5b
JB
7295 struct block *block
7296 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7297 highpc, objfile);
7298
7299 /* Note that recording ranges after traversing children, as we
7300 do here, means that recording a parent's ranges entails
7301 walking across all its children's ranges as they appear in
7302 the address map, which is quadratic behavior.
7303
7304 It would be nicer to record the parent's ranges before
7305 traversing its children, simply overriding whatever you find
7306 there. But since we don't even decide whether to create a
7307 block until after we've traversed its children, that's hard
7308 to do. */
7309 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
7310 }
7311 local_symbols = new->locals;
27aa8d6a 7312 using_directives = new->using_directives;
c906108c
SS
7313}
7314
96408a79
SA
7315/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7316
7317static void
7318read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7319{
7320 struct objfile *objfile = cu->objfile;
7321 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7322 CORE_ADDR pc, baseaddr;
7323 struct attribute *attr;
7324 struct call_site *call_site, call_site_local;
7325 void **slot;
7326 int nparams;
7327 struct die_info *child_die;
7328
7329 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7330
7331 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7332 if (!attr)
7333 {
7334 complaint (&symfile_complaints,
7335 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7336 "DIE 0x%x [in module %s]"),
b64f50a1 7337 die->offset.sect_off, objfile->name);
96408a79
SA
7338 return;
7339 }
7340 pc = DW_ADDR (attr) + baseaddr;
7341
7342 if (cu->call_site_htab == NULL)
7343 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7344 NULL, &objfile->objfile_obstack,
7345 hashtab_obstack_allocate, NULL);
7346 call_site_local.pc = pc;
7347 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7348 if (*slot != NULL)
7349 {
7350 complaint (&symfile_complaints,
7351 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7352 "DIE 0x%x [in module %s]"),
b64f50a1 7353 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
7354 return;
7355 }
7356
7357 /* Count parameters at the caller. */
7358
7359 nparams = 0;
7360 for (child_die = die->child; child_die && child_die->tag;
7361 child_die = sibling_die (child_die))
7362 {
7363 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7364 {
7365 complaint (&symfile_complaints,
7366 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7367 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7368 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
7369 continue;
7370 }
7371
7372 nparams++;
7373 }
7374
7375 call_site = obstack_alloc (&objfile->objfile_obstack,
7376 (sizeof (*call_site)
7377 + (sizeof (*call_site->parameter)
7378 * (nparams - 1))));
7379 *slot = call_site;
7380 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7381 call_site->pc = pc;
7382
7383 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7384 {
7385 struct die_info *func_die;
7386
7387 /* Skip also over DW_TAG_inlined_subroutine. */
7388 for (func_die = die->parent;
7389 func_die && func_die->tag != DW_TAG_subprogram
7390 && func_die->tag != DW_TAG_subroutine_type;
7391 func_die = func_die->parent);
7392
7393 /* DW_AT_GNU_all_call_sites is a superset
7394 of DW_AT_GNU_all_tail_call_sites. */
7395 if (func_die
7396 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7397 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7398 {
7399 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7400 not complete. But keep CALL_SITE for look ups via call_site_htab,
7401 both the initial caller containing the real return address PC and
7402 the final callee containing the current PC of a chain of tail
7403 calls do not need to have the tail call list complete. But any
7404 function candidate for a virtual tail call frame searched via
7405 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7406 determined unambiguously. */
7407 }
7408 else
7409 {
7410 struct type *func_type = NULL;
7411
7412 if (func_die)
7413 func_type = get_die_type (func_die, cu);
7414 if (func_type != NULL)
7415 {
7416 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7417
7418 /* Enlist this call site to the function. */
7419 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7420 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7421 }
7422 else
7423 complaint (&symfile_complaints,
7424 _("Cannot find function owning DW_TAG_GNU_call_site "
7425 "DIE 0x%x [in module %s]"),
b64f50a1 7426 die->offset.sect_off, objfile->name);
96408a79
SA
7427 }
7428 }
7429
7430 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7431 if (attr == NULL)
7432 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7433 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7434 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7435 /* Keep NULL DWARF_BLOCK. */;
7436 else if (attr_form_is_block (attr))
7437 {
7438 struct dwarf2_locexpr_baton *dlbaton;
7439
7440 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7441 dlbaton->data = DW_BLOCK (attr)->data;
7442 dlbaton->size = DW_BLOCK (attr)->size;
7443 dlbaton->per_cu = cu->per_cu;
7444
7445 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7446 }
7447 else if (is_ref_attr (attr))
7448 {
96408a79
SA
7449 struct dwarf2_cu *target_cu = cu;
7450 struct die_info *target_die;
7451
7452 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7453 gdb_assert (target_cu->objfile == objfile);
7454 if (die_is_declaration (target_die, target_cu))
7455 {
7456 const char *target_physname;
7457
7458 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7459 if (target_physname == NULL)
7460 complaint (&symfile_complaints,
7461 _("DW_AT_GNU_call_site_target target DIE has invalid "
7462 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 7463 die->offset.sect_off, objfile->name);
96408a79
SA
7464 else
7465 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7466 }
7467 else
7468 {
7469 CORE_ADDR lowpc;
7470
7471 /* DW_AT_entry_pc should be preferred. */
7472 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7473 complaint (&symfile_complaints,
7474 _("DW_AT_GNU_call_site_target target DIE has invalid "
7475 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 7476 die->offset.sect_off, objfile->name);
96408a79
SA
7477 else
7478 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7479 }
7480 }
7481 else
7482 complaint (&symfile_complaints,
7483 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7484 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 7485 die->offset.sect_off, objfile->name);
96408a79
SA
7486
7487 call_site->per_cu = cu->per_cu;
7488
7489 for (child_die = die->child;
7490 child_die && child_die->tag;
7491 child_die = sibling_die (child_die))
7492 {
7493 struct dwarf2_locexpr_baton *dlbaton;
7494 struct call_site_parameter *parameter;
7495
7496 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7497 {
7498 /* Already printed the complaint above. */
7499 continue;
7500 }
7501
7502 gdb_assert (call_site->parameter_count < nparams);
7503 parameter = &call_site->parameter[call_site->parameter_count];
7504
7505 /* DW_AT_location specifies the register number. Value of the data
7506 assumed for the register is contained in DW_AT_GNU_call_site_value. */
7507
7508 attr = dwarf2_attr (child_die, DW_AT_location, cu);
7509 if (!attr || !attr_form_is_block (attr))
7510 {
7511 complaint (&symfile_complaints,
7512 _("No DW_FORM_block* DW_AT_location for "
7513 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7514 child_die->offset.sect_off, objfile->name);
96408a79
SA
7515 continue;
7516 }
7517 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
7518 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
7519 if (parameter->dwarf_reg == -1
7520 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
7521 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
7522 &parameter->fb_offset))
7523 {
7524 complaint (&symfile_complaints,
7525 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
7526 "for DW_FORM_block* DW_AT_location for "
7527 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7528 child_die->offset.sect_off, objfile->name);
96408a79
SA
7529 continue;
7530 }
7531
7532 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
7533 if (!attr_form_is_block (attr))
7534 {
7535 complaint (&symfile_complaints,
7536 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
7537 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7538 child_die->offset.sect_off, objfile->name);
96408a79
SA
7539 continue;
7540 }
7541 parameter->value = DW_BLOCK (attr)->data;
7542 parameter->value_size = DW_BLOCK (attr)->size;
7543
7544 /* Parameters are not pre-cleared by memset above. */
7545 parameter->data_value = NULL;
7546 parameter->data_value_size = 0;
7547 call_site->parameter_count++;
7548
7549 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
7550 if (attr)
7551 {
7552 if (!attr_form_is_block (attr))
7553 complaint (&symfile_complaints,
7554 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
7555 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7556 child_die->offset.sect_off, objfile->name);
96408a79
SA
7557 else
7558 {
7559 parameter->data_value = DW_BLOCK (attr)->data;
7560 parameter->data_value_size = DW_BLOCK (attr)->size;
7561 }
7562 }
7563 }
7564}
7565
43039443 7566/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
7567 Return 1 if the attributes are present and valid, otherwise, return 0.
7568 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
7569
7570static int
7571dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
7572 CORE_ADDR *high_return, struct dwarf2_cu *cu,
7573 struct partial_symtab *ranges_pst)
43039443
JK
7574{
7575 struct objfile *objfile = cu->objfile;
7576 struct comp_unit_head *cu_header = &cu->header;
7577 bfd *obfd = objfile->obfd;
7578 unsigned int addr_size = cu_header->addr_size;
7579 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7580 /* Base address selection entry. */
7581 CORE_ADDR base;
7582 int found_base;
7583 unsigned int dummy;
7584 gdb_byte *buffer;
7585 CORE_ADDR marker;
7586 int low_set;
7587 CORE_ADDR low = 0;
7588 CORE_ADDR high = 0;
ff013f42 7589 CORE_ADDR baseaddr;
43039443 7590
d00adf39
DE
7591 found_base = cu->base_known;
7592 base = cu->base_address;
43039443 7593
be391dca 7594 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 7595 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
7596 {
7597 complaint (&symfile_complaints,
7598 _("Offset %d out of bounds for DW_AT_ranges attribute"),
7599 offset);
7600 return 0;
7601 }
dce234bc 7602 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
7603
7604 /* Read in the largest possible address. */
7605 marker = read_address (obfd, buffer, cu, &dummy);
7606 if ((marker & mask) == mask)
7607 {
7608 /* If we found the largest possible address, then
7609 read the base address. */
7610 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7611 buffer += 2 * addr_size;
7612 offset += 2 * addr_size;
7613 found_base = 1;
7614 }
7615
7616 low_set = 0;
7617
e7030f15 7618 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 7619
43039443
JK
7620 while (1)
7621 {
7622 CORE_ADDR range_beginning, range_end;
7623
7624 range_beginning = read_address (obfd, buffer, cu, &dummy);
7625 buffer += addr_size;
7626 range_end = read_address (obfd, buffer, cu, &dummy);
7627 buffer += addr_size;
7628 offset += 2 * addr_size;
7629
7630 /* An end of list marker is a pair of zero addresses. */
7631 if (range_beginning == 0 && range_end == 0)
7632 /* Found the end of list entry. */
7633 break;
7634
7635 /* Each base address selection entry is a pair of 2 values.
7636 The first is the largest possible address, the second is
7637 the base address. Check for a base address here. */
7638 if ((range_beginning & mask) == mask)
7639 {
7640 /* If we found the largest possible address, then
7641 read the base address. */
7642 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7643 found_base = 1;
7644 continue;
7645 }
7646
7647 if (!found_base)
7648 {
7649 /* We have no valid base address for the ranges
7650 data. */
7651 complaint (&symfile_complaints,
7652 _("Invalid .debug_ranges data (no base address)"));
7653 return 0;
7654 }
7655
9277c30c
UW
7656 if (range_beginning > range_end)
7657 {
7658 /* Inverted range entries are invalid. */
7659 complaint (&symfile_complaints,
7660 _("Invalid .debug_ranges data (inverted range)"));
7661 return 0;
7662 }
7663
7664 /* Empty range entries have no effect. */
7665 if (range_beginning == range_end)
7666 continue;
7667
43039443
JK
7668 range_beginning += base;
7669 range_end += base;
7670
9277c30c 7671 if (ranges_pst != NULL)
ff013f42 7672 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
7673 range_beginning + baseaddr,
7674 range_end - 1 + baseaddr,
ff013f42
JK
7675 ranges_pst);
7676
43039443
JK
7677 /* FIXME: This is recording everything as a low-high
7678 segment of consecutive addresses. We should have a
7679 data structure for discontiguous block ranges
7680 instead. */
7681 if (! low_set)
7682 {
7683 low = range_beginning;
7684 high = range_end;
7685 low_set = 1;
7686 }
7687 else
7688 {
7689 if (range_beginning < low)
7690 low = range_beginning;
7691 if (range_end > high)
7692 high = range_end;
7693 }
7694 }
7695
7696 if (! low_set)
7697 /* If the first entry is an end-of-list marker, the range
7698 describes an empty scope, i.e. no instructions. */
7699 return 0;
7700
7701 if (low_return)
7702 *low_return = low;
7703 if (high_return)
7704 *high_return = high;
7705 return 1;
7706}
7707
af34e669
DJ
7708/* Get low and high pc attributes from a die. Return 1 if the attributes
7709 are present and valid, otherwise, return 0. Return -1 if the range is
7710 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 7711
c906108c 7712static int
af34e669 7713dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
7714 CORE_ADDR *highpc, struct dwarf2_cu *cu,
7715 struct partial_symtab *pst)
c906108c
SS
7716{
7717 struct attribute *attr;
91da1414 7718 struct attribute *attr_high;
af34e669
DJ
7719 CORE_ADDR low = 0;
7720 CORE_ADDR high = 0;
7721 int ret = 0;
c906108c 7722
91da1414
MW
7723 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
7724 if (attr_high)
af34e669 7725 {
e142c38c 7726 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 7727 if (attr)
91da1414
MW
7728 {
7729 low = DW_ADDR (attr);
3019eac3
DE
7730 if (attr_high->form == DW_FORM_addr
7731 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
7732 high = DW_ADDR (attr_high);
7733 else
7734 high = low + DW_UNSND (attr_high);
7735 }
af34e669
DJ
7736 else
7737 /* Found high w/o low attribute. */
7738 return 0;
7739
7740 /* Found consecutive range of addresses. */
7741 ret = 1;
7742 }
c906108c 7743 else
af34e669 7744 {
e142c38c 7745 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
7746 if (attr != NULL)
7747 {
af34e669 7748 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 7749 .debug_ranges section. */
d85a05f0 7750 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 7751 return 0;
43039443 7752 /* Found discontinuous range of addresses. */
af34e669
DJ
7753 ret = -1;
7754 }
7755 }
c906108c 7756
9373cf26
JK
7757 /* read_partial_die has also the strict LOW < HIGH requirement. */
7758 if (high <= low)
c906108c
SS
7759 return 0;
7760
7761 /* When using the GNU linker, .gnu.linkonce. sections are used to
7762 eliminate duplicate copies of functions and vtables and such.
7763 The linker will arbitrarily choose one and discard the others.
7764 The AT_*_pc values for such functions refer to local labels in
7765 these sections. If the section from that file was discarded, the
7766 labels are not in the output, so the relocs get a value of 0.
7767 If this is a discarded function, mark the pc bounds as invalid,
7768 so that GDB will ignore it. */
72dca2f5 7769 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
7770 return 0;
7771
7772 *lowpc = low;
96408a79
SA
7773 if (highpc)
7774 *highpc = high;
af34e669 7775 return ret;
c906108c
SS
7776}
7777
b084d499
JB
7778/* Assuming that DIE represents a subprogram DIE or a lexical block, get
7779 its low and high PC addresses. Do nothing if these addresses could not
7780 be determined. Otherwise, set LOWPC to the low address if it is smaller,
7781 and HIGHPC to the high address if greater than HIGHPC. */
7782
7783static void
7784dwarf2_get_subprogram_pc_bounds (struct die_info *die,
7785 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7786 struct dwarf2_cu *cu)
7787{
7788 CORE_ADDR low, high;
7789 struct die_info *child = die->child;
7790
d85a05f0 7791 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
7792 {
7793 *lowpc = min (*lowpc, low);
7794 *highpc = max (*highpc, high);
7795 }
7796
7797 /* If the language does not allow nested subprograms (either inside
7798 subprograms or lexical blocks), we're done. */
7799 if (cu->language != language_ada)
7800 return;
6e70227d 7801
b084d499
JB
7802 /* Check all the children of the given DIE. If it contains nested
7803 subprograms, then check their pc bounds. Likewise, we need to
7804 check lexical blocks as well, as they may also contain subprogram
7805 definitions. */
7806 while (child && child->tag)
7807 {
7808 if (child->tag == DW_TAG_subprogram
7809 || child->tag == DW_TAG_lexical_block)
7810 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
7811 child = sibling_die (child);
7812 }
7813}
7814
fae299cd
DC
7815/* Get the low and high pc's represented by the scope DIE, and store
7816 them in *LOWPC and *HIGHPC. If the correct values can't be
7817 determined, set *LOWPC to -1 and *HIGHPC to 0. */
7818
7819static void
7820get_scope_pc_bounds (struct die_info *die,
7821 CORE_ADDR *lowpc, CORE_ADDR *highpc,
7822 struct dwarf2_cu *cu)
7823{
7824 CORE_ADDR best_low = (CORE_ADDR) -1;
7825 CORE_ADDR best_high = (CORE_ADDR) 0;
7826 CORE_ADDR current_low, current_high;
7827
d85a05f0 7828 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
7829 {
7830 best_low = current_low;
7831 best_high = current_high;
7832 }
7833 else
7834 {
7835 struct die_info *child = die->child;
7836
7837 while (child && child->tag)
7838 {
7839 switch (child->tag) {
7840 case DW_TAG_subprogram:
b084d499 7841 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
7842 break;
7843 case DW_TAG_namespace:
f55ee35c 7844 case DW_TAG_module:
fae299cd
DC
7845 /* FIXME: carlton/2004-01-16: Should we do this for
7846 DW_TAG_class_type/DW_TAG_structure_type, too? I think
7847 that current GCC's always emit the DIEs corresponding
7848 to definitions of methods of classes as children of a
7849 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
7850 the DIEs giving the declarations, which could be
7851 anywhere). But I don't see any reason why the
7852 standards says that they have to be there. */
7853 get_scope_pc_bounds (child, &current_low, &current_high, cu);
7854
7855 if (current_low != ((CORE_ADDR) -1))
7856 {
7857 best_low = min (best_low, current_low);
7858 best_high = max (best_high, current_high);
7859 }
7860 break;
7861 default:
0963b4bd 7862 /* Ignore. */
fae299cd
DC
7863 break;
7864 }
7865
7866 child = sibling_die (child);
7867 }
7868 }
7869
7870 *lowpc = best_low;
7871 *highpc = best_high;
7872}
7873
801e3a5b
JB
7874/* Record the address ranges for BLOCK, offset by BASEADDR, as given
7875 in DIE. */
380bca97 7876
801e3a5b
JB
7877static void
7878dwarf2_record_block_ranges (struct die_info *die, struct block *block,
7879 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
7880{
bb5ed363 7881 struct objfile *objfile = cu->objfile;
801e3a5b 7882 struct attribute *attr;
91da1414 7883 struct attribute *attr_high;
801e3a5b 7884
91da1414
MW
7885 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
7886 if (attr_high)
801e3a5b 7887 {
801e3a5b
JB
7888 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7889 if (attr)
7890 {
7891 CORE_ADDR low = DW_ADDR (attr);
91da1414 7892 CORE_ADDR high;
3019eac3
DE
7893 if (attr_high->form == DW_FORM_addr
7894 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
7895 high = DW_ADDR (attr_high);
7896 else
7897 high = low + DW_UNSND (attr_high);
9a619af0 7898
801e3a5b
JB
7899 record_block_range (block, baseaddr + low, baseaddr + high - 1);
7900 }
7901 }
7902
7903 attr = dwarf2_attr (die, DW_AT_ranges, cu);
7904 if (attr)
7905 {
bb5ed363 7906 bfd *obfd = objfile->obfd;
801e3a5b
JB
7907
7908 /* The value of the DW_AT_ranges attribute is the offset of the
7909 address range list in the .debug_ranges section. */
7910 unsigned long offset = DW_UNSND (attr);
dce234bc 7911 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
7912
7913 /* For some target architectures, but not others, the
7914 read_address function sign-extends the addresses it returns.
7915 To recognize base address selection entries, we need a
7916 mask. */
7917 unsigned int addr_size = cu->header.addr_size;
7918 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7919
7920 /* The base address, to which the next pair is relative. Note
7921 that this 'base' is a DWARF concept: most entries in a range
7922 list are relative, to reduce the number of relocs against the
7923 debugging information. This is separate from this function's
7924 'baseaddr' argument, which GDB uses to relocate debugging
7925 information from a shared library based on the address at
7926 which the library was loaded. */
d00adf39
DE
7927 CORE_ADDR base = cu->base_address;
7928 int base_known = cu->base_known;
801e3a5b 7929
be391dca 7930 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 7931 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
7932 {
7933 complaint (&symfile_complaints,
7934 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
7935 offset);
7936 return;
7937 }
7938
7939 for (;;)
7940 {
7941 unsigned int bytes_read;
7942 CORE_ADDR start, end;
7943
7944 start = read_address (obfd, buffer, cu, &bytes_read);
7945 buffer += bytes_read;
7946 end = read_address (obfd, buffer, cu, &bytes_read);
7947 buffer += bytes_read;
7948
7949 /* Did we find the end of the range list? */
7950 if (start == 0 && end == 0)
7951 break;
7952
7953 /* Did we find a base address selection entry? */
7954 else if ((start & base_select_mask) == base_select_mask)
7955 {
7956 base = end;
7957 base_known = 1;
7958 }
7959
7960 /* We found an ordinary address range. */
7961 else
7962 {
7963 if (!base_known)
7964 {
7965 complaint (&symfile_complaints,
3e43a32a
MS
7966 _("Invalid .debug_ranges data "
7967 "(no base address)"));
801e3a5b
JB
7968 return;
7969 }
7970
9277c30c
UW
7971 if (start > end)
7972 {
7973 /* Inverted range entries are invalid. */
7974 complaint (&symfile_complaints,
7975 _("Invalid .debug_ranges data "
7976 "(inverted range)"));
7977 return;
7978 }
7979
7980 /* Empty range entries have no effect. */
7981 if (start == end)
7982 continue;
7983
6e70227d
DE
7984 record_block_range (block,
7985 baseaddr + base + start,
801e3a5b
JB
7986 baseaddr + base + end - 1);
7987 }
7988 }
7989 }
7990}
7991
60d5a603
JK
7992/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
7993 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
7994 during 4.6.0 experimental. */
7995
7996static int
7997producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
7998{
7999 const char *cs;
8000 int major, minor, release;
ba919b58 8001 int result = 0;
60d5a603
JK
8002
8003 if (cu->producer == NULL)
8004 {
8005 /* For unknown compilers expect their behavior is DWARF version
8006 compliant.
8007
8008 GCC started to support .debug_types sections by -gdwarf-4 since
8009 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8010 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8011 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8012 interpreted incorrectly by GDB now - GCC PR debug/48229. */
8013
8014 return 0;
8015 }
8016
ba919b58
TT
8017 if (cu->checked_producer)
8018 return cu->producer_is_gxx_lt_4_6;
8019
60d5a603
JK
8020 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8021
8022 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
8023 {
8024 /* For non-GCC compilers expect their behavior is DWARF version
8025 compliant. */
60d5a603 8026 }
ba919b58 8027 else
60d5a603 8028 {
ba919b58
TT
8029 cs = &cu->producer[strlen ("GNU ")];
8030 while (*cs && !isdigit (*cs))
8031 cs++;
8032 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8033 {
8034 /* Not recognized as GCC. */
8035 }
8036 else
8037 result = major < 4 || (major == 4 && minor < 6);
60d5a603
JK
8038 }
8039
ba919b58
TT
8040 cu->checked_producer = 1;
8041 cu->producer_is_gxx_lt_4_6 = result;
8042
8043 return result;
60d5a603
JK
8044}
8045
8046/* Return the default accessibility type if it is not overriden by
8047 DW_AT_accessibility. */
8048
8049static enum dwarf_access_attribute
8050dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8051{
8052 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8053 {
8054 /* The default DWARF 2 accessibility for members is public, the default
8055 accessibility for inheritance is private. */
8056
8057 if (die->tag != DW_TAG_inheritance)
8058 return DW_ACCESS_public;
8059 else
8060 return DW_ACCESS_private;
8061 }
8062 else
8063 {
8064 /* DWARF 3+ defines the default accessibility a different way. The same
8065 rules apply now for DW_TAG_inheritance as for the members and it only
8066 depends on the container kind. */
8067
8068 if (die->parent->tag == DW_TAG_class_type)
8069 return DW_ACCESS_private;
8070 else
8071 return DW_ACCESS_public;
8072 }
8073}
8074
74ac6d43
TT
8075/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8076 offset. If the attribute was not found return 0, otherwise return
8077 1. If it was found but could not properly be handled, set *OFFSET
8078 to 0. */
8079
8080static int
8081handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8082 LONGEST *offset)
8083{
8084 struct attribute *attr;
8085
8086 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8087 if (attr != NULL)
8088 {
8089 *offset = 0;
8090
8091 /* Note that we do not check for a section offset first here.
8092 This is because DW_AT_data_member_location is new in DWARF 4,
8093 so if we see it, we can assume that a constant form is really
8094 a constant and not a section offset. */
8095 if (attr_form_is_constant (attr))
8096 *offset = dwarf2_get_attr_constant_value (attr, 0);
8097 else if (attr_form_is_section_offset (attr))
8098 dwarf2_complex_location_expr_complaint ();
8099 else if (attr_form_is_block (attr))
8100 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8101 else
8102 dwarf2_complex_location_expr_complaint ();
8103
8104 return 1;
8105 }
8106
8107 return 0;
8108}
8109
c906108c
SS
8110/* Add an aggregate field to the field list. */
8111
8112static void
107d2387 8113dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 8114 struct dwarf2_cu *cu)
6e70227d 8115{
e7c27a73 8116 struct objfile *objfile = cu->objfile;
5e2b427d 8117 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8118 struct nextfield *new_field;
8119 struct attribute *attr;
8120 struct field *fp;
8121 char *fieldname = "";
8122
8123 /* Allocate a new field list entry and link it in. */
8124 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 8125 make_cleanup (xfree, new_field);
c906108c 8126 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
8127
8128 if (die->tag == DW_TAG_inheritance)
8129 {
8130 new_field->next = fip->baseclasses;
8131 fip->baseclasses = new_field;
8132 }
8133 else
8134 {
8135 new_field->next = fip->fields;
8136 fip->fields = new_field;
8137 }
c906108c
SS
8138 fip->nfields++;
8139
e142c38c 8140 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
8141 if (attr)
8142 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
8143 else
8144 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
8145 if (new_field->accessibility != DW_ACCESS_public)
8146 fip->non_public_fields = 1;
60d5a603 8147
e142c38c 8148 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
8149 if (attr)
8150 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
8151 else
8152 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
8153
8154 fp = &new_field->field;
a9a9bd0f 8155
e142c38c 8156 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 8157 {
74ac6d43
TT
8158 LONGEST offset;
8159
a9a9bd0f 8160 /* Data member other than a C++ static data member. */
6e70227d 8161
c906108c 8162 /* Get type of field. */
e7c27a73 8163 fp->type = die_type (die, cu);
c906108c 8164
d6a843b5 8165 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 8166
c906108c 8167 /* Get bit size of field (zero if none). */
e142c38c 8168 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
8169 if (attr)
8170 {
8171 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8172 }
8173 else
8174 {
8175 FIELD_BITSIZE (*fp) = 0;
8176 }
8177
8178 /* Get bit offset of field. */
74ac6d43
TT
8179 if (handle_data_member_location (die, cu, &offset))
8180 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 8181 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
8182 if (attr)
8183 {
5e2b427d 8184 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
8185 {
8186 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
8187 additional bit offset from the MSB of the containing
8188 anonymous object to the MSB of the field. We don't
8189 have to do anything special since we don't need to
8190 know the size of the anonymous object. */
f41f5e61 8191 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
8192 }
8193 else
8194 {
8195 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
8196 MSB of the anonymous object, subtract off the number of
8197 bits from the MSB of the field to the MSB of the
8198 object, and then subtract off the number of bits of
8199 the field itself. The result is the bit offset of
8200 the LSB of the field. */
c906108c
SS
8201 int anonymous_size;
8202 int bit_offset = DW_UNSND (attr);
8203
e142c38c 8204 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8205 if (attr)
8206 {
8207 /* The size of the anonymous object containing
8208 the bit field is explicit, so use the
8209 indicated size (in bytes). */
8210 anonymous_size = DW_UNSND (attr);
8211 }
8212 else
8213 {
8214 /* The size of the anonymous object containing
8215 the bit field must be inferred from the type
8216 attribute of the data member containing the
8217 bit field. */
8218 anonymous_size = TYPE_LENGTH (fp->type);
8219 }
f41f5e61
PA
8220 SET_FIELD_BITPOS (*fp,
8221 (FIELD_BITPOS (*fp)
8222 + anonymous_size * bits_per_byte
8223 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
8224 }
8225 }
8226
8227 /* Get name of field. */
39cbfefa
DJ
8228 fieldname = dwarf2_name (die, cu);
8229 if (fieldname == NULL)
8230 fieldname = "";
d8151005
DJ
8231
8232 /* The name is already allocated along with this objfile, so we don't
8233 need to duplicate it for the type. */
8234 fp->name = fieldname;
c906108c
SS
8235
8236 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 8237 pointer or virtual base class pointer) to private. */
e142c38c 8238 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 8239 {
d48cc9dd 8240 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
8241 new_field->accessibility = DW_ACCESS_private;
8242 fip->non_public_fields = 1;
8243 }
8244 }
a9a9bd0f 8245 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 8246 {
a9a9bd0f
DC
8247 /* C++ static member. */
8248
8249 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8250 is a declaration, but all versions of G++ as of this writing
8251 (so through at least 3.2.1) incorrectly generate
8252 DW_TAG_variable tags. */
6e70227d 8253
ff355380 8254 const char *physname;
c906108c 8255
a9a9bd0f 8256 /* Get name of field. */
39cbfefa
DJ
8257 fieldname = dwarf2_name (die, cu);
8258 if (fieldname == NULL)
c906108c
SS
8259 return;
8260
254e6b9e 8261 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
8262 if (attr
8263 /* Only create a symbol if this is an external value.
8264 new_symbol checks this and puts the value in the global symbol
8265 table, which we want. If it is not external, new_symbol
8266 will try to put the value in cu->list_in_scope which is wrong. */
8267 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
8268 {
8269 /* A static const member, not much different than an enum as far as
8270 we're concerned, except that we can support more types. */
8271 new_symbol (die, NULL, cu);
8272 }
8273
2df3850c 8274 /* Get physical name. */
ff355380 8275 physname = dwarf2_physname (fieldname, die, cu);
c906108c 8276
d8151005
DJ
8277 /* The name is already allocated along with this objfile, so we don't
8278 need to duplicate it for the type. */
8279 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 8280 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 8281 FIELD_NAME (*fp) = fieldname;
c906108c
SS
8282 }
8283 else if (die->tag == DW_TAG_inheritance)
8284 {
74ac6d43 8285 LONGEST offset;
d4b96c9a 8286
74ac6d43
TT
8287 /* C++ base class field. */
8288 if (handle_data_member_location (die, cu, &offset))
8289 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 8290 FIELD_BITSIZE (*fp) = 0;
e7c27a73 8291 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
8292 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8293 fip->nbaseclasses++;
8294 }
8295}
8296
98751a41
JK
8297/* Add a typedef defined in the scope of the FIP's class. */
8298
8299static void
8300dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8301 struct dwarf2_cu *cu)
6e70227d 8302{
98751a41 8303 struct objfile *objfile = cu->objfile;
98751a41
JK
8304 struct typedef_field_list *new_field;
8305 struct attribute *attr;
8306 struct typedef_field *fp;
8307 char *fieldname = "";
8308
8309 /* Allocate a new field list entry and link it in. */
8310 new_field = xzalloc (sizeof (*new_field));
8311 make_cleanup (xfree, new_field);
8312
8313 gdb_assert (die->tag == DW_TAG_typedef);
8314
8315 fp = &new_field->field;
8316
8317 /* Get name of field. */
8318 fp->name = dwarf2_name (die, cu);
8319 if (fp->name == NULL)
8320 return;
8321
8322 fp->type = read_type_die (die, cu);
8323
8324 new_field->next = fip->typedef_field_list;
8325 fip->typedef_field_list = new_field;
8326 fip->typedef_field_list_count++;
8327}
8328
c906108c
SS
8329/* Create the vector of fields, and attach it to the type. */
8330
8331static void
fba45db2 8332dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8333 struct dwarf2_cu *cu)
c906108c
SS
8334{
8335 int nfields = fip->nfields;
8336
8337 /* Record the field count, allocate space for the array of fields,
8338 and create blank accessibility bitfields if necessary. */
8339 TYPE_NFIELDS (type) = nfields;
8340 TYPE_FIELDS (type) = (struct field *)
8341 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8342 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8343
b4ba55a1 8344 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
8345 {
8346 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8347
8348 TYPE_FIELD_PRIVATE_BITS (type) =
8349 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8350 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8351
8352 TYPE_FIELD_PROTECTED_BITS (type) =
8353 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8354 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8355
774b6a14
TT
8356 TYPE_FIELD_IGNORE_BITS (type) =
8357 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8358 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
8359 }
8360
8361 /* If the type has baseclasses, allocate and clear a bit vector for
8362 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 8363 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
8364 {
8365 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 8366 unsigned char *pointer;
c906108c
SS
8367
8368 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
8369 pointer = TYPE_ALLOC (type, num_bytes);
8370 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
8371 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8372 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8373 }
8374
3e43a32a
MS
8375 /* Copy the saved-up fields into the field vector. Start from the head of
8376 the list, adding to the tail of the field array, so that they end up in
8377 the same order in the array in which they were added to the list. */
c906108c
SS
8378 while (nfields-- > 0)
8379 {
7d0ccb61
DJ
8380 struct nextfield *fieldp;
8381
8382 if (fip->fields)
8383 {
8384 fieldp = fip->fields;
8385 fip->fields = fieldp->next;
8386 }
8387 else
8388 {
8389 fieldp = fip->baseclasses;
8390 fip->baseclasses = fieldp->next;
8391 }
8392
8393 TYPE_FIELD (type, nfields) = fieldp->field;
8394 switch (fieldp->accessibility)
c906108c 8395 {
c5aa993b 8396 case DW_ACCESS_private:
b4ba55a1
JB
8397 if (cu->language != language_ada)
8398 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 8399 break;
c906108c 8400
c5aa993b 8401 case DW_ACCESS_protected:
b4ba55a1
JB
8402 if (cu->language != language_ada)
8403 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 8404 break;
c906108c 8405
c5aa993b
JM
8406 case DW_ACCESS_public:
8407 break;
c906108c 8408
c5aa993b
JM
8409 default:
8410 /* Unknown accessibility. Complain and treat it as public. */
8411 {
e2e0b3e5 8412 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 8413 fieldp->accessibility);
c5aa993b
JM
8414 }
8415 break;
c906108c
SS
8416 }
8417 if (nfields < fip->nbaseclasses)
8418 {
7d0ccb61 8419 switch (fieldp->virtuality)
c906108c 8420 {
c5aa993b
JM
8421 case DW_VIRTUALITY_virtual:
8422 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 8423 if (cu->language == language_ada)
a73c6dcd 8424 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
8425 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8426 break;
c906108c
SS
8427 }
8428 }
c906108c
SS
8429 }
8430}
8431
c906108c
SS
8432/* Add a member function to the proper fieldlist. */
8433
8434static void
107d2387 8435dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 8436 struct type *type, struct dwarf2_cu *cu)
c906108c 8437{
e7c27a73 8438 struct objfile *objfile = cu->objfile;
c906108c
SS
8439 struct attribute *attr;
8440 struct fnfieldlist *flp;
8441 int i;
8442 struct fn_field *fnp;
8443 char *fieldname;
c906108c 8444 struct nextfnfield *new_fnfield;
f792889a 8445 struct type *this_type;
60d5a603 8446 enum dwarf_access_attribute accessibility;
c906108c 8447
b4ba55a1 8448 if (cu->language == language_ada)
a73c6dcd 8449 error (_("unexpected member function in Ada type"));
b4ba55a1 8450
2df3850c 8451 /* Get name of member function. */
39cbfefa
DJ
8452 fieldname = dwarf2_name (die, cu);
8453 if (fieldname == NULL)
2df3850c 8454 return;
c906108c 8455
c906108c
SS
8456 /* Look up member function name in fieldlist. */
8457 for (i = 0; i < fip->nfnfields; i++)
8458 {
27bfe10e 8459 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
8460 break;
8461 }
8462
8463 /* Create new list element if necessary. */
8464 if (i < fip->nfnfields)
8465 flp = &fip->fnfieldlists[i];
8466 else
8467 {
8468 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8469 {
8470 fip->fnfieldlists = (struct fnfieldlist *)
8471 xrealloc (fip->fnfieldlists,
8472 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8473 * sizeof (struct fnfieldlist));
c906108c 8474 if (fip->nfnfields == 0)
c13c43fd 8475 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
8476 }
8477 flp = &fip->fnfieldlists[fip->nfnfields];
8478 flp->name = fieldname;
8479 flp->length = 0;
8480 flp->head = NULL;
3da10d80 8481 i = fip->nfnfields++;
c906108c
SS
8482 }
8483
8484 /* Create a new member function field and chain it to the field list
0963b4bd 8485 entry. */
c906108c 8486 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 8487 make_cleanup (xfree, new_fnfield);
c906108c
SS
8488 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8489 new_fnfield->next = flp->head;
8490 flp->head = new_fnfield;
8491 flp->length++;
8492
8493 /* Fill in the member function field info. */
8494 fnp = &new_fnfield->fnfield;
3da10d80
KS
8495
8496 /* Delay processing of the physname until later. */
8497 if (cu->language == language_cplus || cu->language == language_java)
8498 {
8499 add_to_method_list (type, i, flp->length - 1, fieldname,
8500 die, cu);
8501 }
8502 else
8503 {
1d06ead6 8504 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
8505 fnp->physname = physname ? physname : "";
8506 }
8507
c906108c 8508 fnp->type = alloc_type (objfile);
f792889a
DJ
8509 this_type = read_type_die (die, cu);
8510 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 8511 {
f792889a 8512 int nparams = TYPE_NFIELDS (this_type);
c906108c 8513
f792889a 8514 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
8515 of the method itself (TYPE_CODE_METHOD). */
8516 smash_to_method_type (fnp->type, type,
f792889a
DJ
8517 TYPE_TARGET_TYPE (this_type),
8518 TYPE_FIELDS (this_type),
8519 TYPE_NFIELDS (this_type),
8520 TYPE_VARARGS (this_type));
c906108c
SS
8521
8522 /* Handle static member functions.
c5aa993b 8523 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
8524 member functions. G++ helps GDB by marking the first
8525 parameter for non-static member functions (which is the this
8526 pointer) as artificial. We obtain this information from
8527 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 8528 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
8529 fnp->voffset = VOFFSET_STATIC;
8530 }
8531 else
e2e0b3e5 8532 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 8533 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
8534
8535 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 8536 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 8537 fnp->fcontext = die_containing_type (die, cu);
c906108c 8538
3e43a32a
MS
8539 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
8540 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
8541
8542 /* Get accessibility. */
e142c38c 8543 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 8544 if (attr)
60d5a603
JK
8545 accessibility = DW_UNSND (attr);
8546 else
8547 accessibility = dwarf2_default_access_attribute (die, cu);
8548 switch (accessibility)
c906108c 8549 {
60d5a603
JK
8550 case DW_ACCESS_private:
8551 fnp->is_private = 1;
8552 break;
8553 case DW_ACCESS_protected:
8554 fnp->is_protected = 1;
8555 break;
c906108c
SS
8556 }
8557
b02dede2 8558 /* Check for artificial methods. */
e142c38c 8559 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
8560 if (attr && DW_UNSND (attr) != 0)
8561 fnp->is_artificial = 1;
8562
0d564a31 8563 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
8564 function. For older versions of GCC, this is an offset in the
8565 appropriate virtual table, as specified by DW_AT_containing_type.
8566 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
8567 to the object address. */
8568
e142c38c 8569 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 8570 if (attr)
8e19ed76 8571 {
aec5aa8b 8572 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 8573 {
aec5aa8b
TT
8574 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
8575 {
8576 /* Old-style GCC. */
8577 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8578 }
8579 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
8580 || (DW_BLOCK (attr)->size > 1
8581 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
8582 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
8583 {
8584 struct dwarf_block blk;
8585 int offset;
8586
8587 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
8588 ? 1 : 2);
8589 blk.size = DW_BLOCK (attr)->size - offset;
8590 blk.data = DW_BLOCK (attr)->data + offset;
8591 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
8592 if ((fnp->voffset % cu->header.addr_size) != 0)
8593 dwarf2_complex_location_expr_complaint ();
8594 else
8595 fnp->voffset /= cu->header.addr_size;
8596 fnp->voffset += 2;
8597 }
8598 else
8599 dwarf2_complex_location_expr_complaint ();
8600
8601 if (!fnp->fcontext)
8602 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
8603 }
3690dd37 8604 else if (attr_form_is_section_offset (attr))
8e19ed76 8605 {
4d3c2250 8606 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8607 }
8608 else
8609 {
4d3c2250
KB
8610 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
8611 fieldname);
8e19ed76 8612 }
0d564a31 8613 }
d48cc9dd
DJ
8614 else
8615 {
8616 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8617 if (attr && DW_UNSND (attr))
8618 {
8619 /* GCC does this, as of 2008-08-25; PR debug/37237. */
8620 complaint (&symfile_complaints,
3e43a32a
MS
8621 _("Member function \"%s\" (offset %d) is virtual "
8622 "but the vtable offset is not specified"),
b64f50a1 8623 fieldname, die->offset.sect_off);
9655fd1a 8624 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
8625 TYPE_CPLUS_DYNAMIC (type) = 1;
8626 }
8627 }
c906108c
SS
8628}
8629
8630/* Create the vector of member function fields, and attach it to the type. */
8631
8632static void
fba45db2 8633dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8634 struct dwarf2_cu *cu)
c906108c
SS
8635{
8636 struct fnfieldlist *flp;
c906108c
SS
8637 int i;
8638
b4ba55a1 8639 if (cu->language == language_ada)
a73c6dcd 8640 error (_("unexpected member functions in Ada type"));
b4ba55a1 8641
c906108c
SS
8642 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8643 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
8644 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
8645
8646 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
8647 {
8648 struct nextfnfield *nfp = flp->head;
8649 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
8650 int k;
8651
8652 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
8653 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
8654 fn_flp->fn_fields = (struct fn_field *)
8655 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
8656 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 8657 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
8658 }
8659
8660 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
8661}
8662
1168df01
JB
8663/* Returns non-zero if NAME is the name of a vtable member in CU's
8664 language, zero otherwise. */
8665static int
8666is_vtable_name (const char *name, struct dwarf2_cu *cu)
8667{
8668 static const char vptr[] = "_vptr";
987504bb 8669 static const char vtable[] = "vtable";
1168df01 8670
987504bb
JJ
8671 /* Look for the C++ and Java forms of the vtable. */
8672 if ((cu->language == language_java
8673 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
8674 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
8675 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
8676 return 1;
8677
8678 return 0;
8679}
8680
c0dd20ea 8681/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
8682 functions, with the ABI-specified layout. If TYPE describes
8683 such a structure, smash it into a member function type.
61049d3b
DJ
8684
8685 GCC shouldn't do this; it should just output pointer to member DIEs.
8686 This is GCC PR debug/28767. */
c0dd20ea 8687
0b92b5bb
TT
8688static void
8689quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 8690{
0b92b5bb 8691 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
8692
8693 /* Check for a structure with no name and two children. */
0b92b5bb
TT
8694 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
8695 return;
c0dd20ea
DJ
8696
8697 /* Check for __pfn and __delta members. */
0b92b5bb
TT
8698 if (TYPE_FIELD_NAME (type, 0) == NULL
8699 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
8700 || TYPE_FIELD_NAME (type, 1) == NULL
8701 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
8702 return;
c0dd20ea
DJ
8703
8704 /* Find the type of the method. */
0b92b5bb 8705 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
8706 if (pfn_type == NULL
8707 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
8708 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 8709 return;
c0dd20ea
DJ
8710
8711 /* Look for the "this" argument. */
8712 pfn_type = TYPE_TARGET_TYPE (pfn_type);
8713 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 8714 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 8715 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 8716 return;
c0dd20ea
DJ
8717
8718 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
8719 new_type = alloc_type (objfile);
8720 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
8721 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
8722 TYPE_VARARGS (pfn_type));
0b92b5bb 8723 smash_to_methodptr_type (type, new_type);
c0dd20ea 8724}
1168df01 8725
c906108c 8726/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
8727 (definition) to create a type for the structure or union. Fill in
8728 the type's name and general properties; the members will not be
8729 processed until process_structure_type.
c906108c 8730
c767944b
DJ
8731 NOTE: we need to call these functions regardless of whether or not the
8732 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
8733 structure or union. This gets the type entered into our set of
8734 user defined types.
8735
8736 However, if the structure is incomplete (an opaque struct/union)
8737 then suppress creating a symbol table entry for it since gdb only
8738 wants to find the one with the complete definition. Note that if
8739 it is complete, we just call new_symbol, which does it's own
8740 checking about whether the struct/union is anonymous or not (and
8741 suppresses creating a symbol table entry itself). */
8742
f792889a 8743static struct type *
134d01f1 8744read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 8745{
e7c27a73 8746 struct objfile *objfile = cu->objfile;
c906108c
SS
8747 struct type *type;
8748 struct attribute *attr;
39cbfefa 8749 char *name;
c906108c 8750
348e048f
DE
8751 /* If the definition of this type lives in .debug_types, read that type.
8752 Don't follow DW_AT_specification though, that will take us back up
8753 the chain and we want to go down. */
8754 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
8755 if (attr)
8756 {
8757 struct dwarf2_cu *type_cu = cu;
8758 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 8759
348e048f
DE
8760 /* We could just recurse on read_structure_type, but we need to call
8761 get_die_type to ensure only one type for this DIE is created.
8762 This is important, for example, because for c++ classes we need
8763 TYPE_NAME set which is only done by new_symbol. Blech. */
8764 type = read_type_die (type_die, type_cu);
9dc481d3
DE
8765
8766 /* TYPE_CU may not be the same as CU.
8767 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
8768 return set_die_type (die, type, cu);
8769 }
8770
c0dd20ea 8771 type = alloc_type (objfile);
c906108c 8772 INIT_CPLUS_SPECIFIC (type);
93311388 8773
39cbfefa
DJ
8774 name = dwarf2_name (die, cu);
8775 if (name != NULL)
c906108c 8776 {
987504bb
JJ
8777 if (cu->language == language_cplus
8778 || cu->language == language_java)
63d06c5c 8779 {
3da10d80
KS
8780 char *full_name = (char *) dwarf2_full_name (name, die, cu);
8781
8782 /* dwarf2_full_name might have already finished building the DIE's
8783 type. If so, there is no need to continue. */
8784 if (get_die_type (die, cu) != NULL)
8785 return get_die_type (die, cu);
8786
8787 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
8788 if (die->tag == DW_TAG_structure_type
8789 || die->tag == DW_TAG_class_type)
8790 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
8791 }
8792 else
8793 {
d8151005
DJ
8794 /* The name is already allocated along with this objfile, so
8795 we don't need to duplicate it for the type. */
94af9270
KS
8796 TYPE_TAG_NAME (type) = (char *) name;
8797 if (die->tag == DW_TAG_class_type)
8798 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 8799 }
c906108c
SS
8800 }
8801
8802 if (die->tag == DW_TAG_structure_type)
8803 {
8804 TYPE_CODE (type) = TYPE_CODE_STRUCT;
8805 }
8806 else if (die->tag == DW_TAG_union_type)
8807 {
8808 TYPE_CODE (type) = TYPE_CODE_UNION;
8809 }
8810 else
8811 {
c906108c
SS
8812 TYPE_CODE (type) = TYPE_CODE_CLASS;
8813 }
8814
0cc2414c
TT
8815 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
8816 TYPE_DECLARED_CLASS (type) = 1;
8817
e142c38c 8818 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8819 if (attr)
8820 {
8821 TYPE_LENGTH (type) = DW_UNSND (attr);
8822 }
8823 else
8824 {
8825 TYPE_LENGTH (type) = 0;
8826 }
8827
876cecd0 8828 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 8829 if (die_is_declaration (die, cu))
876cecd0 8830 TYPE_STUB (type) = 1;
a6c727b2
DJ
8831 else if (attr == NULL && die->child == NULL
8832 && producer_is_realview (cu->producer))
8833 /* RealView does not output the required DW_AT_declaration
8834 on incomplete types. */
8835 TYPE_STUB (type) = 1;
dc718098 8836
c906108c
SS
8837 /* We need to add the type field to the die immediately so we don't
8838 infinitely recurse when dealing with pointers to the structure
0963b4bd 8839 type within the structure itself. */
1c379e20 8840 set_die_type (die, type, cu);
c906108c 8841
7e314c57
JK
8842 /* set_die_type should be already done. */
8843 set_descriptive_type (type, die, cu);
8844
c767944b
DJ
8845 return type;
8846}
8847
8848/* Finish creating a structure or union type, including filling in
8849 its members and creating a symbol for it. */
8850
8851static void
8852process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
8853{
8854 struct objfile *objfile = cu->objfile;
8855 struct die_info *child_die = die->child;
8856 struct type *type;
8857
8858 type = get_die_type (die, cu);
8859 if (type == NULL)
8860 type = read_structure_type (die, cu);
8861
e142c38c 8862 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
8863 {
8864 struct field_info fi;
8865 struct die_info *child_die;
34eaf542 8866 VEC (symbolp) *template_args = NULL;
c767944b 8867 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
8868
8869 memset (&fi, 0, sizeof (struct field_info));
8870
639d11d3 8871 child_die = die->child;
c906108c
SS
8872
8873 while (child_die && child_die->tag)
8874 {
a9a9bd0f
DC
8875 if (child_die->tag == DW_TAG_member
8876 || child_die->tag == DW_TAG_variable)
c906108c 8877 {
a9a9bd0f
DC
8878 /* NOTE: carlton/2002-11-05: A C++ static data member
8879 should be a DW_TAG_member that is a declaration, but
8880 all versions of G++ as of this writing (so through at
8881 least 3.2.1) incorrectly generate DW_TAG_variable
8882 tags for them instead. */
e7c27a73 8883 dwarf2_add_field (&fi, child_die, cu);
c906108c 8884 }
8713b1b1 8885 else if (child_die->tag == DW_TAG_subprogram)
c906108c 8886 {
0963b4bd 8887 /* C++ member function. */
e7c27a73 8888 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
8889 }
8890 else if (child_die->tag == DW_TAG_inheritance)
8891 {
8892 /* C++ base class field. */
e7c27a73 8893 dwarf2_add_field (&fi, child_die, cu);
c906108c 8894 }
98751a41
JK
8895 else if (child_die->tag == DW_TAG_typedef)
8896 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
8897 else if (child_die->tag == DW_TAG_template_type_param
8898 || child_die->tag == DW_TAG_template_value_param)
8899 {
8900 struct symbol *arg = new_symbol (child_die, NULL, cu);
8901
f1078f66
DJ
8902 if (arg != NULL)
8903 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
8904 }
8905
c906108c
SS
8906 child_die = sibling_die (child_die);
8907 }
8908
34eaf542
TT
8909 /* Attach template arguments to type. */
8910 if (! VEC_empty (symbolp, template_args))
8911 {
8912 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8913 TYPE_N_TEMPLATE_ARGUMENTS (type)
8914 = VEC_length (symbolp, template_args);
8915 TYPE_TEMPLATE_ARGUMENTS (type)
8916 = obstack_alloc (&objfile->objfile_obstack,
8917 (TYPE_N_TEMPLATE_ARGUMENTS (type)
8918 * sizeof (struct symbol *)));
8919 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
8920 VEC_address (symbolp, template_args),
8921 (TYPE_N_TEMPLATE_ARGUMENTS (type)
8922 * sizeof (struct symbol *)));
8923 VEC_free (symbolp, template_args);
8924 }
8925
c906108c
SS
8926 /* Attach fields and member functions to the type. */
8927 if (fi.nfields)
e7c27a73 8928 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
8929 if (fi.nfnfields)
8930 {
e7c27a73 8931 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 8932
c5aa993b 8933 /* Get the type which refers to the base class (possibly this
c906108c 8934 class itself) which contains the vtable pointer for the current
0d564a31
DJ
8935 class from the DW_AT_containing_type attribute. This use of
8936 DW_AT_containing_type is a GNU extension. */
c906108c 8937
e142c38c 8938 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 8939 {
e7c27a73 8940 struct type *t = die_containing_type (die, cu);
c906108c
SS
8941
8942 TYPE_VPTR_BASETYPE (type) = t;
8943 if (type == t)
8944 {
c906108c
SS
8945 int i;
8946
8947 /* Our own class provides vtbl ptr. */
8948 for (i = TYPE_NFIELDS (t) - 1;
8949 i >= TYPE_N_BASECLASSES (t);
8950 --i)
8951 {
0d5cff50 8952 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 8953
1168df01 8954 if (is_vtable_name (fieldname, cu))
c906108c
SS
8955 {
8956 TYPE_VPTR_FIELDNO (type) = i;
8957 break;
8958 }
8959 }
8960
8961 /* Complain if virtual function table field not found. */
8962 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 8963 complaint (&symfile_complaints,
3e43a32a
MS
8964 _("virtual function table pointer "
8965 "not found when defining class '%s'"),
4d3c2250
KB
8966 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
8967 "");
c906108c
SS
8968 }
8969 else
8970 {
8971 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
8972 }
8973 }
f6235d4c
EZ
8974 else if (cu->producer
8975 && strncmp (cu->producer,
8976 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
8977 {
8978 /* The IBM XLC compiler does not provide direct indication
8979 of the containing type, but the vtable pointer is
8980 always named __vfp. */
8981
8982 int i;
8983
8984 for (i = TYPE_NFIELDS (type) - 1;
8985 i >= TYPE_N_BASECLASSES (type);
8986 --i)
8987 {
8988 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
8989 {
8990 TYPE_VPTR_FIELDNO (type) = i;
8991 TYPE_VPTR_BASETYPE (type) = type;
8992 break;
8993 }
8994 }
8995 }
c906108c 8996 }
98751a41
JK
8997
8998 /* Copy fi.typedef_field_list linked list elements content into the
8999 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9000 if (fi.typedef_field_list)
9001 {
9002 int i = fi.typedef_field_list_count;
9003
a0d7a4ff 9004 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
9005 TYPE_TYPEDEF_FIELD_ARRAY (type)
9006 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9007 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9008
9009 /* Reverse the list order to keep the debug info elements order. */
9010 while (--i >= 0)
9011 {
9012 struct typedef_field *dest, *src;
6e70227d 9013
98751a41
JK
9014 dest = &TYPE_TYPEDEF_FIELD (type, i);
9015 src = &fi.typedef_field_list->field;
9016 fi.typedef_field_list = fi.typedef_field_list->next;
9017 *dest = *src;
9018 }
9019 }
c767944b
DJ
9020
9021 do_cleanups (back_to);
eb2a6f42
TT
9022
9023 if (HAVE_CPLUS_STRUCT (type))
9024 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 9025 }
63d06c5c 9026
bb5ed363 9027 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 9028
90aeadfc
DC
9029 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9030 snapshots) has been known to create a die giving a declaration
9031 for a class that has, as a child, a die giving a definition for a
9032 nested class. So we have to process our children even if the
9033 current die is a declaration. Normally, of course, a declaration
9034 won't have any children at all. */
134d01f1 9035
90aeadfc
DC
9036 while (child_die != NULL && child_die->tag)
9037 {
9038 if (child_die->tag == DW_TAG_member
9039 || child_die->tag == DW_TAG_variable
34eaf542
TT
9040 || child_die->tag == DW_TAG_inheritance
9041 || child_die->tag == DW_TAG_template_value_param
9042 || child_die->tag == DW_TAG_template_type_param)
134d01f1 9043 {
90aeadfc 9044 /* Do nothing. */
134d01f1 9045 }
90aeadfc
DC
9046 else
9047 process_die (child_die, cu);
134d01f1 9048
90aeadfc 9049 child_die = sibling_die (child_die);
134d01f1
DJ
9050 }
9051
fa4028e9
JB
9052 /* Do not consider external references. According to the DWARF standard,
9053 these DIEs are identified by the fact that they have no byte_size
9054 attribute, and a declaration attribute. */
9055 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9056 || !die_is_declaration (die, cu))
c767944b 9057 new_symbol (die, type, cu);
134d01f1
DJ
9058}
9059
9060/* Given a DW_AT_enumeration_type die, set its type. We do not
9061 complete the type's fields yet, or create any symbols. */
c906108c 9062
f792889a 9063static struct type *
134d01f1 9064read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9065{
e7c27a73 9066 struct objfile *objfile = cu->objfile;
c906108c 9067 struct type *type;
c906108c 9068 struct attribute *attr;
0114d602 9069 const char *name;
134d01f1 9070
348e048f
DE
9071 /* If the definition of this type lives in .debug_types, read that type.
9072 Don't follow DW_AT_specification though, that will take us back up
9073 the chain and we want to go down. */
9074 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9075 if (attr)
9076 {
9077 struct dwarf2_cu *type_cu = cu;
9078 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9079
348e048f 9080 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9081
9082 /* TYPE_CU may not be the same as CU.
9083 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9084 return set_die_type (die, type, cu);
9085 }
9086
c906108c
SS
9087 type = alloc_type (objfile);
9088
9089 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 9090 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 9091 if (name != NULL)
0114d602 9092 TYPE_TAG_NAME (type) = (char *) name;
c906108c 9093
e142c38c 9094 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9095 if (attr)
9096 {
9097 TYPE_LENGTH (type) = DW_UNSND (attr);
9098 }
9099 else
9100 {
9101 TYPE_LENGTH (type) = 0;
9102 }
9103
137033e9
JB
9104 /* The enumeration DIE can be incomplete. In Ada, any type can be
9105 declared as private in the package spec, and then defined only
9106 inside the package body. Such types are known as Taft Amendment
9107 Types. When another package uses such a type, an incomplete DIE
9108 may be generated by the compiler. */
02eb380e 9109 if (die_is_declaration (die, cu))
876cecd0 9110 TYPE_STUB (type) = 1;
02eb380e 9111
f792889a 9112 return set_die_type (die, type, cu);
134d01f1
DJ
9113}
9114
9115/* Given a pointer to a die which begins an enumeration, process all
9116 the dies that define the members of the enumeration, and create the
9117 symbol for the enumeration type.
9118
9119 NOTE: We reverse the order of the element list. */
9120
9121static void
9122process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9123{
f792889a 9124 struct type *this_type;
134d01f1 9125
f792889a
DJ
9126 this_type = get_die_type (die, cu);
9127 if (this_type == NULL)
9128 this_type = read_enumeration_type (die, cu);
9dc481d3 9129
639d11d3 9130 if (die->child != NULL)
c906108c 9131 {
9dc481d3
DE
9132 struct die_info *child_die;
9133 struct symbol *sym;
9134 struct field *fields = NULL;
9135 int num_fields = 0;
9136 int unsigned_enum = 1;
9137 char *name;
cafec441
TT
9138 int flag_enum = 1;
9139 ULONGEST mask = 0;
9dc481d3 9140
639d11d3 9141 child_die = die->child;
c906108c
SS
9142 while (child_die && child_die->tag)
9143 {
9144 if (child_die->tag != DW_TAG_enumerator)
9145 {
e7c27a73 9146 process_die (child_die, cu);
c906108c
SS
9147 }
9148 else
9149 {
39cbfefa
DJ
9150 name = dwarf2_name (child_die, cu);
9151 if (name)
c906108c 9152 {
f792889a 9153 sym = new_symbol (child_die, this_type, cu);
c906108c 9154 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
9155 {
9156 unsigned_enum = 0;
9157 flag_enum = 0;
9158 }
9159 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9160 flag_enum = 0;
9161 else
9162 mask |= SYMBOL_VALUE (sym);
c906108c
SS
9163
9164 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9165 {
9166 fields = (struct field *)
9167 xrealloc (fields,
9168 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 9169 * sizeof (struct field));
c906108c
SS
9170 }
9171
3567439c 9172 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 9173 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 9174 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
9175 FIELD_BITSIZE (fields[num_fields]) = 0;
9176
9177 num_fields++;
9178 }
9179 }
9180
9181 child_die = sibling_die (child_die);
9182 }
9183
9184 if (num_fields)
9185 {
f792889a
DJ
9186 TYPE_NFIELDS (this_type) = num_fields;
9187 TYPE_FIELDS (this_type) = (struct field *)
9188 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9189 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 9190 sizeof (struct field) * num_fields);
b8c9b27d 9191 xfree (fields);
c906108c
SS
9192 }
9193 if (unsigned_enum)
876cecd0 9194 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
9195 if (flag_enum)
9196 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 9197 }
134d01f1 9198
6c83ed52
TT
9199 /* If we are reading an enum from a .debug_types unit, and the enum
9200 is a declaration, and the enum is not the signatured type in the
9201 unit, then we do not want to add a symbol for it. Adding a
9202 symbol would in some cases obscure the true definition of the
9203 enum, giving users an incomplete type when the definition is
9204 actually available. Note that we do not want to do this for all
9205 enums which are just declarations, because C++0x allows forward
9206 enum declarations. */
3019eac3 9207 if (cu->per_cu->is_debug_types
6c83ed52
TT
9208 && die_is_declaration (die, cu))
9209 {
52dc124a 9210 struct signatured_type *sig_type;
6c83ed52 9211
52dc124a 9212 sig_type
6c83ed52 9213 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 9214 cu->per_cu->info_or_types_section,
6c83ed52 9215 cu->per_cu->offset);
3019eac3
DE
9216 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9217 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
9218 return;
9219 }
9220
f792889a 9221 new_symbol (die, this_type, cu);
c906108c
SS
9222}
9223
9224/* Extract all information from a DW_TAG_array_type DIE and put it in
9225 the DIE's type field. For now, this only handles one dimensional
9226 arrays. */
9227
f792889a 9228static struct type *
e7c27a73 9229read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9230{
e7c27a73 9231 struct objfile *objfile = cu->objfile;
c906108c 9232 struct die_info *child_die;
7e314c57 9233 struct type *type;
c906108c
SS
9234 struct type *element_type, *range_type, *index_type;
9235 struct type **range_types = NULL;
9236 struct attribute *attr;
9237 int ndim = 0;
9238 struct cleanup *back_to;
39cbfefa 9239 char *name;
c906108c 9240
e7c27a73 9241 element_type = die_type (die, cu);
c906108c 9242
7e314c57
JK
9243 /* The die_type call above may have already set the type for this DIE. */
9244 type = get_die_type (die, cu);
9245 if (type)
9246 return type;
9247
c906108c
SS
9248 /* Irix 6.2 native cc creates array types without children for
9249 arrays with unspecified length. */
639d11d3 9250 if (die->child == NULL)
c906108c 9251 {
46bf5051 9252 index_type = objfile_type (objfile)->builtin_int;
c906108c 9253 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
9254 type = create_array_type (NULL, element_type, range_type);
9255 return set_die_type (die, type, cu);
c906108c
SS
9256 }
9257
9258 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 9259 child_die = die->child;
c906108c
SS
9260 while (child_die && child_die->tag)
9261 {
9262 if (child_die->tag == DW_TAG_subrange_type)
9263 {
f792889a 9264 struct type *child_type = read_type_die (child_die, cu);
9a619af0 9265
f792889a 9266 if (child_type != NULL)
a02abb62 9267 {
0963b4bd
MS
9268 /* The range type was succesfully read. Save it for the
9269 array type creation. */
a02abb62
JB
9270 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9271 {
9272 range_types = (struct type **)
9273 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9274 * sizeof (struct type *));
9275 if (ndim == 0)
9276 make_cleanup (free_current_contents, &range_types);
9277 }
f792889a 9278 range_types[ndim++] = child_type;
a02abb62 9279 }
c906108c
SS
9280 }
9281 child_die = sibling_die (child_die);
9282 }
9283
9284 /* Dwarf2 dimensions are output from left to right, create the
9285 necessary array types in backwards order. */
7ca2d3a3 9286
c906108c 9287 type = element_type;
7ca2d3a3
DL
9288
9289 if (read_array_order (die, cu) == DW_ORD_col_major)
9290 {
9291 int i = 0;
9a619af0 9292
7ca2d3a3
DL
9293 while (i < ndim)
9294 type = create_array_type (NULL, type, range_types[i++]);
9295 }
9296 else
9297 {
9298 while (ndim-- > 0)
9299 type = create_array_type (NULL, type, range_types[ndim]);
9300 }
c906108c 9301
f5f8a009
EZ
9302 /* Understand Dwarf2 support for vector types (like they occur on
9303 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9304 array type. This is not part of the Dwarf2/3 standard yet, but a
9305 custom vendor extension. The main difference between a regular
9306 array and the vector variant is that vectors are passed by value
9307 to functions. */
e142c38c 9308 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 9309 if (attr)
ea37ba09 9310 make_vector_type (type);
f5f8a009 9311
dbc98a8b
KW
9312 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9313 implementation may choose to implement triple vectors using this
9314 attribute. */
9315 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9316 if (attr)
9317 {
9318 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9319 TYPE_LENGTH (type) = DW_UNSND (attr);
9320 else
3e43a32a
MS
9321 complaint (&symfile_complaints,
9322 _("DW_AT_byte_size for array type smaller "
9323 "than the total size of elements"));
dbc98a8b
KW
9324 }
9325
39cbfefa
DJ
9326 name = dwarf2_name (die, cu);
9327 if (name)
9328 TYPE_NAME (type) = name;
6e70227d 9329
0963b4bd 9330 /* Install the type in the die. */
7e314c57
JK
9331 set_die_type (die, type, cu);
9332
9333 /* set_die_type should be already done. */
b4ba55a1
JB
9334 set_descriptive_type (type, die, cu);
9335
c906108c
SS
9336 do_cleanups (back_to);
9337
7e314c57 9338 return type;
c906108c
SS
9339}
9340
7ca2d3a3 9341static enum dwarf_array_dim_ordering
6e70227d 9342read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
9343{
9344 struct attribute *attr;
9345
9346 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9347
9348 if (attr) return DW_SND (attr);
9349
0963b4bd
MS
9350 /* GNU F77 is a special case, as at 08/2004 array type info is the
9351 opposite order to the dwarf2 specification, but data is still
9352 laid out as per normal fortran.
7ca2d3a3 9353
0963b4bd
MS
9354 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9355 version checking. */
7ca2d3a3 9356
905e0470
PM
9357 if (cu->language == language_fortran
9358 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
9359 {
9360 return DW_ORD_row_major;
9361 }
9362
6e70227d 9363 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
9364 {
9365 case array_column_major:
9366 return DW_ORD_col_major;
9367 case array_row_major:
9368 default:
9369 return DW_ORD_row_major;
9370 };
9371}
9372
72019c9c 9373/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 9374 the DIE's type field. */
72019c9c 9375
f792889a 9376static struct type *
72019c9c
GM
9377read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9378{
7e314c57
JK
9379 struct type *domain_type, *set_type;
9380 struct attribute *attr;
f792889a 9381
7e314c57
JK
9382 domain_type = die_type (die, cu);
9383
9384 /* The die_type call above may have already set the type for this DIE. */
9385 set_type = get_die_type (die, cu);
9386 if (set_type)
9387 return set_type;
9388
9389 set_type = create_set_type (NULL, domain_type);
9390
9391 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
9392 if (attr)
9393 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 9394
f792889a 9395 return set_die_type (die, set_type, cu);
72019c9c 9396}
7ca2d3a3 9397
c906108c
SS
9398/* First cut: install each common block member as a global variable. */
9399
9400static void
e7c27a73 9401read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
9402{
9403 struct die_info *child_die;
9404 struct attribute *attr;
9405 struct symbol *sym;
9406 CORE_ADDR base = (CORE_ADDR) 0;
9407
e142c38c 9408 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
9409 if (attr)
9410 {
0963b4bd 9411 /* Support the .debug_loc offsets. */
8e19ed76
PS
9412 if (attr_form_is_block (attr))
9413 {
e7c27a73 9414 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 9415 }
3690dd37 9416 else if (attr_form_is_section_offset (attr))
8e19ed76 9417 {
4d3c2250 9418 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9419 }
9420 else
9421 {
4d3c2250
KB
9422 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9423 "common block member");
8e19ed76 9424 }
c906108c 9425 }
639d11d3 9426 if (die->child != NULL)
c906108c 9427 {
639d11d3 9428 child_die = die->child;
c906108c
SS
9429 while (child_die && child_die->tag)
9430 {
74ac6d43
TT
9431 LONGEST offset;
9432
e7c27a73 9433 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
9434 if (sym != NULL
9435 && handle_data_member_location (child_die, cu, &offset))
c906108c 9436 {
74ac6d43 9437 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
9438 add_symbol_to_list (sym, &global_symbols);
9439 }
9440 child_die = sibling_die (child_die);
9441 }
9442 }
9443}
9444
0114d602 9445/* Create a type for a C++ namespace. */
d9fa45fe 9446
0114d602
DJ
9447static struct type *
9448read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 9449{
e7c27a73 9450 struct objfile *objfile = cu->objfile;
0114d602 9451 const char *previous_prefix, *name;
9219021c 9452 int is_anonymous;
0114d602
DJ
9453 struct type *type;
9454
9455 /* For extensions, reuse the type of the original namespace. */
9456 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9457 {
9458 struct die_info *ext_die;
9459 struct dwarf2_cu *ext_cu = cu;
9a619af0 9460
0114d602
DJ
9461 ext_die = dwarf2_extension (die, &ext_cu);
9462 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
9463
9464 /* EXT_CU may not be the same as CU.
9465 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
9466 return set_die_type (die, type, cu);
9467 }
9219021c 9468
e142c38c 9469 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
9470
9471 /* Now build the name of the current namespace. */
9472
0114d602
DJ
9473 previous_prefix = determine_prefix (die, cu);
9474 if (previous_prefix[0] != '\0')
9475 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 9476 previous_prefix, name, 0, cu);
0114d602
DJ
9477
9478 /* Create the type. */
9479 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
9480 objfile);
9481 TYPE_NAME (type) = (char *) name;
9482 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9483
60531b24 9484 return set_die_type (die, type, cu);
0114d602
DJ
9485}
9486
9487/* Read a C++ namespace. */
9488
9489static void
9490read_namespace (struct die_info *die, struct dwarf2_cu *cu)
9491{
9492 struct objfile *objfile = cu->objfile;
0114d602 9493 int is_anonymous;
9219021c 9494
5c4e30ca
DC
9495 /* Add a symbol associated to this if we haven't seen the namespace
9496 before. Also, add a using directive if it's an anonymous
9497 namespace. */
9219021c 9498
f2f0e013 9499 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
9500 {
9501 struct type *type;
9502
0114d602 9503 type = read_type_die (die, cu);
e7c27a73 9504 new_symbol (die, type, cu);
5c4e30ca 9505
e8e80198 9506 namespace_name (die, &is_anonymous, cu);
5c4e30ca 9507 if (is_anonymous)
0114d602
DJ
9508 {
9509 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 9510
c0cc3a76 9511 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 9512 NULL, NULL, &objfile->objfile_obstack);
0114d602 9513 }
5c4e30ca 9514 }
9219021c 9515
639d11d3 9516 if (die->child != NULL)
d9fa45fe 9517 {
639d11d3 9518 struct die_info *child_die = die->child;
6e70227d 9519
d9fa45fe
DC
9520 while (child_die && child_die->tag)
9521 {
e7c27a73 9522 process_die (child_die, cu);
d9fa45fe
DC
9523 child_die = sibling_die (child_die);
9524 }
9525 }
38d518c9
EZ
9526}
9527
f55ee35c
JK
9528/* Read a Fortran module as type. This DIE can be only a declaration used for
9529 imported module. Still we need that type as local Fortran "use ... only"
9530 declaration imports depend on the created type in determine_prefix. */
9531
9532static struct type *
9533read_module_type (struct die_info *die, struct dwarf2_cu *cu)
9534{
9535 struct objfile *objfile = cu->objfile;
9536 char *module_name;
9537 struct type *type;
9538
9539 module_name = dwarf2_name (die, cu);
9540 if (!module_name)
3e43a32a
MS
9541 complaint (&symfile_complaints,
9542 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 9543 die->offset.sect_off);
f55ee35c
JK
9544 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
9545
9546 /* determine_prefix uses TYPE_TAG_NAME. */
9547 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9548
9549 return set_die_type (die, type, cu);
9550}
9551
5d7cb8df
JK
9552/* Read a Fortran module. */
9553
9554static void
9555read_module (struct die_info *die, struct dwarf2_cu *cu)
9556{
9557 struct die_info *child_die = die->child;
9558
5d7cb8df
JK
9559 while (child_die && child_die->tag)
9560 {
9561 process_die (child_die, cu);
9562 child_die = sibling_die (child_die);
9563 }
9564}
9565
38d518c9
EZ
9566/* Return the name of the namespace represented by DIE. Set
9567 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
9568 namespace. */
9569
9570static const char *
e142c38c 9571namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
9572{
9573 struct die_info *current_die;
9574 const char *name = NULL;
9575
9576 /* Loop through the extensions until we find a name. */
9577
9578 for (current_die = die;
9579 current_die != NULL;
f2f0e013 9580 current_die = dwarf2_extension (die, &cu))
38d518c9 9581 {
e142c38c 9582 name = dwarf2_name (current_die, cu);
38d518c9
EZ
9583 if (name != NULL)
9584 break;
9585 }
9586
9587 /* Is it an anonymous namespace? */
9588
9589 *is_anonymous = (name == NULL);
9590 if (*is_anonymous)
2b1dbab0 9591 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
9592
9593 return name;
d9fa45fe
DC
9594}
9595
c906108c
SS
9596/* Extract all information from a DW_TAG_pointer_type DIE and add to
9597 the user defined type vector. */
9598
f792889a 9599static struct type *
e7c27a73 9600read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9601{
5e2b427d 9602 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 9603 struct comp_unit_head *cu_header = &cu->header;
c906108c 9604 struct type *type;
8b2dbe47
KB
9605 struct attribute *attr_byte_size;
9606 struct attribute *attr_address_class;
9607 int byte_size, addr_class;
7e314c57
JK
9608 struct type *target_type;
9609
9610 target_type = die_type (die, cu);
c906108c 9611
7e314c57
JK
9612 /* The die_type call above may have already set the type for this DIE. */
9613 type = get_die_type (die, cu);
9614 if (type)
9615 return type;
9616
9617 type = lookup_pointer_type (target_type);
8b2dbe47 9618
e142c38c 9619 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
9620 if (attr_byte_size)
9621 byte_size = DW_UNSND (attr_byte_size);
c906108c 9622 else
8b2dbe47
KB
9623 byte_size = cu_header->addr_size;
9624
e142c38c 9625 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
9626 if (attr_address_class)
9627 addr_class = DW_UNSND (attr_address_class);
9628 else
9629 addr_class = DW_ADDR_none;
9630
9631 /* If the pointer size or address class is different than the
9632 default, create a type variant marked as such and set the
9633 length accordingly. */
9634 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 9635 {
5e2b427d 9636 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
9637 {
9638 int type_flags;
9639
849957d9 9640 type_flags = gdbarch_address_class_type_flags
5e2b427d 9641 (gdbarch, byte_size, addr_class);
876cecd0
TT
9642 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
9643 == 0);
8b2dbe47
KB
9644 type = make_type_with_address_space (type, type_flags);
9645 }
9646 else if (TYPE_LENGTH (type) != byte_size)
9647 {
3e43a32a
MS
9648 complaint (&symfile_complaints,
9649 _("invalid pointer size %d"), byte_size);
8b2dbe47 9650 }
6e70227d 9651 else
9a619af0
MS
9652 {
9653 /* Should we also complain about unhandled address classes? */
9654 }
c906108c 9655 }
8b2dbe47
KB
9656
9657 TYPE_LENGTH (type) = byte_size;
f792889a 9658 return set_die_type (die, type, cu);
c906108c
SS
9659}
9660
9661/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
9662 the user defined type vector. */
9663
f792889a 9664static struct type *
e7c27a73 9665read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
9666{
9667 struct type *type;
9668 struct type *to_type;
9669 struct type *domain;
9670
e7c27a73
DJ
9671 to_type = die_type (die, cu);
9672 domain = die_containing_type (die, cu);
0d5de010 9673
7e314c57
JK
9674 /* The calls above may have already set the type for this DIE. */
9675 type = get_die_type (die, cu);
9676 if (type)
9677 return type;
9678
0d5de010
DJ
9679 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
9680 type = lookup_methodptr_type (to_type);
9681 else
9682 type = lookup_memberptr_type (to_type, domain);
c906108c 9683
f792889a 9684 return set_die_type (die, type, cu);
c906108c
SS
9685}
9686
9687/* Extract all information from a DW_TAG_reference_type DIE and add to
9688 the user defined type vector. */
9689
f792889a 9690static struct type *
e7c27a73 9691read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9692{
e7c27a73 9693 struct comp_unit_head *cu_header = &cu->header;
7e314c57 9694 struct type *type, *target_type;
c906108c
SS
9695 struct attribute *attr;
9696
7e314c57
JK
9697 target_type = die_type (die, cu);
9698
9699 /* The die_type call above may have already set the type for this DIE. */
9700 type = get_die_type (die, cu);
9701 if (type)
9702 return type;
9703
9704 type = lookup_reference_type (target_type);
e142c38c 9705 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9706 if (attr)
9707 {
9708 TYPE_LENGTH (type) = DW_UNSND (attr);
9709 }
9710 else
9711 {
107d2387 9712 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 9713 }
f792889a 9714 return set_die_type (die, type, cu);
c906108c
SS
9715}
9716
f792889a 9717static struct type *
e7c27a73 9718read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9719{
f792889a 9720 struct type *base_type, *cv_type;
c906108c 9721
e7c27a73 9722 base_type = die_type (die, cu);
7e314c57
JK
9723
9724 /* The die_type call above may have already set the type for this DIE. */
9725 cv_type = get_die_type (die, cu);
9726 if (cv_type)
9727 return cv_type;
9728
2f608a3a
KW
9729 /* In case the const qualifier is applied to an array type, the element type
9730 is so qualified, not the array type (section 6.7.3 of C99). */
9731 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
9732 {
9733 struct type *el_type, *inner_array;
9734
9735 base_type = copy_type (base_type);
9736 inner_array = base_type;
9737
9738 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
9739 {
9740 TYPE_TARGET_TYPE (inner_array) =
9741 copy_type (TYPE_TARGET_TYPE (inner_array));
9742 inner_array = TYPE_TARGET_TYPE (inner_array);
9743 }
9744
9745 el_type = TYPE_TARGET_TYPE (inner_array);
9746 TYPE_TARGET_TYPE (inner_array) =
9747 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
9748
9749 return set_die_type (die, base_type, cu);
9750 }
9751
f792889a
DJ
9752 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
9753 return set_die_type (die, cv_type, cu);
c906108c
SS
9754}
9755
f792889a 9756static struct type *
e7c27a73 9757read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9758{
f792889a 9759 struct type *base_type, *cv_type;
c906108c 9760
e7c27a73 9761 base_type = die_type (die, cu);
7e314c57
JK
9762
9763 /* The die_type call above may have already set the type for this DIE. */
9764 cv_type = get_die_type (die, cu);
9765 if (cv_type)
9766 return cv_type;
9767
f792889a
DJ
9768 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
9769 return set_die_type (die, cv_type, cu);
c906108c
SS
9770}
9771
9772/* Extract all information from a DW_TAG_string_type DIE and add to
9773 the user defined type vector. It isn't really a user defined type,
9774 but it behaves like one, with other DIE's using an AT_user_def_type
9775 attribute to reference it. */
9776
f792889a 9777static struct type *
e7c27a73 9778read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9779{
e7c27a73 9780 struct objfile *objfile = cu->objfile;
3b7538c0 9781 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
9782 struct type *type, *range_type, *index_type, *char_type;
9783 struct attribute *attr;
9784 unsigned int length;
9785
e142c38c 9786 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
9787 if (attr)
9788 {
9789 length = DW_UNSND (attr);
9790 }
9791 else
9792 {
0963b4bd 9793 /* Check for the DW_AT_byte_size attribute. */
e142c38c 9794 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
9795 if (attr)
9796 {
9797 length = DW_UNSND (attr);
9798 }
9799 else
9800 {
9801 length = 1;
9802 }
c906108c 9803 }
6ccb9162 9804
46bf5051 9805 index_type = objfile_type (objfile)->builtin_int;
c906108c 9806 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
9807 char_type = language_string_char_type (cu->language_defn, gdbarch);
9808 type = create_string_type (NULL, char_type, range_type);
6ccb9162 9809
f792889a 9810 return set_die_type (die, type, cu);
c906108c
SS
9811}
9812
9813/* Handle DIES due to C code like:
9814
9815 struct foo
c5aa993b
JM
9816 {
9817 int (*funcp)(int a, long l);
9818 int b;
9819 };
c906108c 9820
0963b4bd 9821 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 9822
f792889a 9823static struct type *
e7c27a73 9824read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9825{
bb5ed363 9826 struct objfile *objfile = cu->objfile;
0963b4bd
MS
9827 struct type *type; /* Type that this function returns. */
9828 struct type *ftype; /* Function that returns above type. */
c906108c
SS
9829 struct attribute *attr;
9830
e7c27a73 9831 type = die_type (die, cu);
7e314c57
JK
9832
9833 /* The die_type call above may have already set the type for this DIE. */
9834 ftype = get_die_type (die, cu);
9835 if (ftype)
9836 return ftype;
9837
0c8b41f1 9838 ftype = lookup_function_type (type);
c906108c 9839
5b8101ae 9840 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 9841 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 9842 if ((attr && (DW_UNSND (attr) != 0))
987504bb 9843 || cu->language == language_cplus
5b8101ae
PM
9844 || cu->language == language_java
9845 || cu->language == language_pascal)
876cecd0 9846 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
9847 else if (producer_is_realview (cu->producer))
9848 /* RealView does not emit DW_AT_prototyped. We can not
9849 distinguish prototyped and unprototyped functions; default to
9850 prototyped, since that is more common in modern code (and
9851 RealView warns about unprototyped functions). */
9852 TYPE_PROTOTYPED (ftype) = 1;
c906108c 9853
c055b101
CV
9854 /* Store the calling convention in the type if it's available in
9855 the subroutine die. Otherwise set the calling convention to
9856 the default value DW_CC_normal. */
9857 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
9858 if (attr)
9859 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
9860 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
9861 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
9862 else
9863 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
9864
9865 /* We need to add the subroutine type to the die immediately so
9866 we don't infinitely recurse when dealing with parameters
0963b4bd 9867 declared as the same subroutine type. */
76c10ea2 9868 set_die_type (die, ftype, cu);
6e70227d 9869
639d11d3 9870 if (die->child != NULL)
c906108c 9871 {
bb5ed363 9872 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 9873 struct die_info *child_die;
8072405b 9874 int nparams, iparams;
c906108c
SS
9875
9876 /* Count the number of parameters.
9877 FIXME: GDB currently ignores vararg functions, but knows about
9878 vararg member functions. */
8072405b 9879 nparams = 0;
639d11d3 9880 child_die = die->child;
c906108c
SS
9881 while (child_die && child_die->tag)
9882 {
9883 if (child_die->tag == DW_TAG_formal_parameter)
9884 nparams++;
9885 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 9886 TYPE_VARARGS (ftype) = 1;
c906108c
SS
9887 child_die = sibling_die (child_die);
9888 }
9889
9890 /* Allocate storage for parameters and fill them in. */
9891 TYPE_NFIELDS (ftype) = nparams;
9892 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 9893 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 9894
8072405b
JK
9895 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
9896 even if we error out during the parameters reading below. */
9897 for (iparams = 0; iparams < nparams; iparams++)
9898 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
9899
9900 iparams = 0;
639d11d3 9901 child_die = die->child;
c906108c
SS
9902 while (child_die && child_die->tag)
9903 {
9904 if (child_die->tag == DW_TAG_formal_parameter)
9905 {
3ce3b1ba
PA
9906 struct type *arg_type;
9907
9908 /* DWARF version 2 has no clean way to discern C++
9909 static and non-static member functions. G++ helps
9910 GDB by marking the first parameter for non-static
9911 member functions (which is the this pointer) as
9912 artificial. We pass this information to
9913 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
9914
9915 DWARF version 3 added DW_AT_object_pointer, which GCC
9916 4.5 does not yet generate. */
e142c38c 9917 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
9918 if (attr)
9919 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
9920 else
418835cc
KS
9921 {
9922 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
9923
9924 /* GCC/43521: In java, the formal parameter
9925 "this" is sometimes not marked with DW_AT_artificial. */
9926 if (cu->language == language_java)
9927 {
9928 const char *name = dwarf2_name (child_die, cu);
9a619af0 9929
418835cc
KS
9930 if (name && !strcmp (name, "this"))
9931 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
9932 }
9933 }
3ce3b1ba
PA
9934 arg_type = die_type (child_die, cu);
9935
9936 /* RealView does not mark THIS as const, which the testsuite
9937 expects. GCC marks THIS as const in method definitions,
9938 but not in the class specifications (GCC PR 43053). */
9939 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
9940 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
9941 {
9942 int is_this = 0;
9943 struct dwarf2_cu *arg_cu = cu;
9944 const char *name = dwarf2_name (child_die, cu);
9945
9946 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
9947 if (attr)
9948 {
9949 /* If the compiler emits this, use it. */
9950 if (follow_die_ref (die, attr, &arg_cu) == child_die)
9951 is_this = 1;
9952 }
9953 else if (name && strcmp (name, "this") == 0)
9954 /* Function definitions will have the argument names. */
9955 is_this = 1;
9956 else if (name == NULL && iparams == 0)
9957 /* Declarations may not have the names, so like
9958 elsewhere in GDB, assume an artificial first
9959 argument is "this". */
9960 is_this = 1;
9961
9962 if (is_this)
9963 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
9964 arg_type, 0);
9965 }
9966
9967 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
9968 iparams++;
9969 }
9970 child_die = sibling_die (child_die);
9971 }
9972 }
9973
76c10ea2 9974 return ftype;
c906108c
SS
9975}
9976
f792889a 9977static struct type *
e7c27a73 9978read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9979{
e7c27a73 9980 struct objfile *objfile = cu->objfile;
0114d602 9981 const char *name = NULL;
3c8e0968 9982 struct type *this_type, *target_type;
c906108c 9983
94af9270 9984 name = dwarf2_full_name (NULL, die, cu);
f792889a 9985 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
9986 TYPE_FLAG_TARGET_STUB, NULL, objfile);
9987 TYPE_NAME (this_type) = (char *) name;
f792889a 9988 set_die_type (die, this_type, cu);
3c8e0968
DE
9989 target_type = die_type (die, cu);
9990 if (target_type != this_type)
9991 TYPE_TARGET_TYPE (this_type) = target_type;
9992 else
9993 {
9994 /* Self-referential typedefs are, it seems, not allowed by the DWARF
9995 spec and cause infinite loops in GDB. */
9996 complaint (&symfile_complaints,
9997 _("Self-referential DW_TAG_typedef "
9998 "- DIE at 0x%x [in module %s]"),
b64f50a1 9999 die->offset.sect_off, objfile->name);
3c8e0968
DE
10000 TYPE_TARGET_TYPE (this_type) = NULL;
10001 }
f792889a 10002 return this_type;
c906108c
SS
10003}
10004
10005/* Find a representation of a given base type and install
10006 it in the TYPE field of the die. */
10007
f792889a 10008static struct type *
e7c27a73 10009read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10010{
e7c27a73 10011 struct objfile *objfile = cu->objfile;
c906108c
SS
10012 struct type *type;
10013 struct attribute *attr;
10014 int encoding = 0, size = 0;
39cbfefa 10015 char *name;
6ccb9162
UW
10016 enum type_code code = TYPE_CODE_INT;
10017 int type_flags = 0;
10018 struct type *target_type = NULL;
c906108c 10019
e142c38c 10020 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
10021 if (attr)
10022 {
10023 encoding = DW_UNSND (attr);
10024 }
e142c38c 10025 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10026 if (attr)
10027 {
10028 size = DW_UNSND (attr);
10029 }
39cbfefa 10030 name = dwarf2_name (die, cu);
6ccb9162 10031 if (!name)
c906108c 10032 {
6ccb9162
UW
10033 complaint (&symfile_complaints,
10034 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 10035 }
6ccb9162
UW
10036
10037 switch (encoding)
c906108c 10038 {
6ccb9162
UW
10039 case DW_ATE_address:
10040 /* Turn DW_ATE_address into a void * pointer. */
10041 code = TYPE_CODE_PTR;
10042 type_flags |= TYPE_FLAG_UNSIGNED;
10043 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10044 break;
10045 case DW_ATE_boolean:
10046 code = TYPE_CODE_BOOL;
10047 type_flags |= TYPE_FLAG_UNSIGNED;
10048 break;
10049 case DW_ATE_complex_float:
10050 code = TYPE_CODE_COMPLEX;
10051 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10052 break;
10053 case DW_ATE_decimal_float:
10054 code = TYPE_CODE_DECFLOAT;
10055 break;
10056 case DW_ATE_float:
10057 code = TYPE_CODE_FLT;
10058 break;
10059 case DW_ATE_signed:
10060 break;
10061 case DW_ATE_unsigned:
10062 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
10063 if (cu->language == language_fortran
10064 && name
10065 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10066 code = TYPE_CODE_CHAR;
6ccb9162
UW
10067 break;
10068 case DW_ATE_signed_char:
6e70227d 10069 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10070 || cu->language == language_pascal
10071 || cu->language == language_fortran)
6ccb9162
UW
10072 code = TYPE_CODE_CHAR;
10073 break;
10074 case DW_ATE_unsigned_char:
868a0084 10075 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10076 || cu->language == language_pascal
10077 || cu->language == language_fortran)
6ccb9162
UW
10078 code = TYPE_CODE_CHAR;
10079 type_flags |= TYPE_FLAG_UNSIGNED;
10080 break;
75079b2b
TT
10081 case DW_ATE_UTF:
10082 /* We just treat this as an integer and then recognize the
10083 type by name elsewhere. */
10084 break;
10085
6ccb9162
UW
10086 default:
10087 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10088 dwarf_type_encoding_name (encoding));
10089 break;
c906108c 10090 }
6ccb9162 10091
0114d602
DJ
10092 type = init_type (code, size, type_flags, NULL, objfile);
10093 TYPE_NAME (type) = name;
6ccb9162
UW
10094 TYPE_TARGET_TYPE (type) = target_type;
10095
0114d602 10096 if (name && strcmp (name, "char") == 0)
876cecd0 10097 TYPE_NOSIGN (type) = 1;
0114d602 10098
f792889a 10099 return set_die_type (die, type, cu);
c906108c
SS
10100}
10101
a02abb62
JB
10102/* Read the given DW_AT_subrange DIE. */
10103
f792889a 10104static struct type *
a02abb62
JB
10105read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10106{
10107 struct type *base_type;
10108 struct type *range_type;
10109 struct attribute *attr;
4fae6e18
JK
10110 LONGEST low, high;
10111 int low_default_is_valid;
39cbfefa 10112 char *name;
43bbcdc2 10113 LONGEST negative_mask;
e77813c8 10114
a02abb62 10115 base_type = die_type (die, cu);
953ac07e
JK
10116 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10117 check_typedef (base_type);
a02abb62 10118
7e314c57
JK
10119 /* The die_type call above may have already set the type for this DIE. */
10120 range_type = get_die_type (die, cu);
10121 if (range_type)
10122 return range_type;
10123
4fae6e18
JK
10124 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10125 omitting DW_AT_lower_bound. */
10126 switch (cu->language)
6e70227d 10127 {
4fae6e18
JK
10128 case language_c:
10129 case language_cplus:
10130 low = 0;
10131 low_default_is_valid = 1;
10132 break;
10133 case language_fortran:
10134 low = 1;
10135 low_default_is_valid = 1;
10136 break;
10137 case language_d:
10138 case language_java:
10139 case language_objc:
10140 low = 0;
10141 low_default_is_valid = (cu->header.version >= 4);
10142 break;
10143 case language_ada:
10144 case language_m2:
10145 case language_pascal:
a02abb62 10146 low = 1;
4fae6e18
JK
10147 low_default_is_valid = (cu->header.version >= 4);
10148 break;
10149 default:
10150 low = 0;
10151 low_default_is_valid = 0;
10152 break;
a02abb62
JB
10153 }
10154
dd5e6932
DJ
10155 /* FIXME: For variable sized arrays either of these could be
10156 a variable rather than a constant value. We'll allow it,
10157 but we don't know how to handle it. */
e142c38c 10158 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 10159 if (attr)
4fae6e18
JK
10160 low = dwarf2_get_attr_constant_value (attr, low);
10161 else if (!low_default_is_valid)
10162 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10163 "- DIE at 0x%x [in module %s]"),
10164 die->offset.sect_off, cu->objfile->name);
a02abb62 10165
e142c38c 10166 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 10167 if (attr)
6e70227d 10168 {
d48323d8 10169 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
10170 {
10171 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 10172 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
10173 FIXME: GDB does not yet know how to handle dynamic
10174 arrays properly, treat them as arrays with unspecified
10175 length for now.
10176
10177 FIXME: jimb/2003-09-22: GDB does not really know
10178 how to handle arrays of unspecified length
10179 either; we just represent them as zero-length
10180 arrays. Choose an appropriate upper bound given
10181 the lower bound we've computed above. */
10182 high = low - 1;
10183 }
10184 else
10185 high = dwarf2_get_attr_constant_value (attr, 1);
10186 }
e77813c8
PM
10187 else
10188 {
10189 attr = dwarf2_attr (die, DW_AT_count, cu);
10190 if (attr)
10191 {
10192 int count = dwarf2_get_attr_constant_value (attr, 1);
10193 high = low + count - 1;
10194 }
c2ff108b
JK
10195 else
10196 {
10197 /* Unspecified array length. */
10198 high = low - 1;
10199 }
e77813c8
PM
10200 }
10201
10202 /* Dwarf-2 specifications explicitly allows to create subrange types
10203 without specifying a base type.
10204 In that case, the base type must be set to the type of
10205 the lower bound, upper bound or count, in that order, if any of these
10206 three attributes references an object that has a type.
10207 If no base type is found, the Dwarf-2 specifications say that
10208 a signed integer type of size equal to the size of an address should
10209 be used.
10210 For the following C code: `extern char gdb_int [];'
10211 GCC produces an empty range DIE.
10212 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 10213 high bound or count are not yet handled by this code. */
e77813c8
PM
10214 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10215 {
10216 struct objfile *objfile = cu->objfile;
10217 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10218 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10219 struct type *int_type = objfile_type (objfile)->builtin_int;
10220
10221 /* Test "int", "long int", and "long long int" objfile types,
10222 and select the first one having a size above or equal to the
10223 architecture address size. */
10224 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10225 base_type = int_type;
10226 else
10227 {
10228 int_type = objfile_type (objfile)->builtin_long;
10229 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10230 base_type = int_type;
10231 else
10232 {
10233 int_type = objfile_type (objfile)->builtin_long_long;
10234 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10235 base_type = int_type;
10236 }
10237 }
10238 }
a02abb62 10239
6e70227d 10240 negative_mask =
43bbcdc2
PH
10241 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10242 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10243 low |= negative_mask;
10244 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10245 high |= negative_mask;
10246
a02abb62
JB
10247 range_type = create_range_type (NULL, base_type, low, high);
10248
bbb0eef6
JK
10249 /* Mark arrays with dynamic length at least as an array of unspecified
10250 length. GDB could check the boundary but before it gets implemented at
10251 least allow accessing the array elements. */
d48323d8 10252 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
10253 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10254
c2ff108b
JK
10255 /* Ada expects an empty array on no boundary attributes. */
10256 if (attr == NULL && cu->language != language_ada)
10257 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10258
39cbfefa
DJ
10259 name = dwarf2_name (die, cu);
10260 if (name)
10261 TYPE_NAME (range_type) = name;
6e70227d 10262
e142c38c 10263 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
10264 if (attr)
10265 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10266
7e314c57
JK
10267 set_die_type (die, range_type, cu);
10268
10269 /* set_die_type should be already done. */
b4ba55a1
JB
10270 set_descriptive_type (range_type, die, cu);
10271
7e314c57 10272 return range_type;
a02abb62 10273}
6e70227d 10274
f792889a 10275static struct type *
81a17f79
JB
10276read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10277{
10278 struct type *type;
81a17f79 10279
81a17f79
JB
10280 /* For now, we only support the C meaning of an unspecified type: void. */
10281
0114d602
DJ
10282 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10283 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 10284
f792889a 10285 return set_die_type (die, type, cu);
81a17f79 10286}
a02abb62 10287
639d11d3
DC
10288/* Read a single die and all its descendents. Set the die's sibling
10289 field to NULL; set other fields in the die correctly, and set all
10290 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10291 location of the info_ptr after reading all of those dies. PARENT
10292 is the parent of the die in question. */
10293
10294static struct die_info *
dee91e82
DE
10295read_die_and_children (const struct die_reader_specs *reader,
10296 gdb_byte *info_ptr,
10297 gdb_byte **new_info_ptr,
10298 struct die_info *parent)
639d11d3
DC
10299{
10300 struct die_info *die;
fe1b8b76 10301 gdb_byte *cur_ptr;
639d11d3
DC
10302 int has_children;
10303
93311388 10304 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
10305 if (die == NULL)
10306 {
10307 *new_info_ptr = cur_ptr;
10308 return NULL;
10309 }
93311388 10310 store_in_ref_table (die, reader->cu);
639d11d3
DC
10311
10312 if (has_children)
348e048f 10313 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
10314 else
10315 {
10316 die->child = NULL;
10317 *new_info_ptr = cur_ptr;
10318 }
10319
10320 die->sibling = NULL;
10321 die->parent = parent;
10322 return die;
10323}
10324
10325/* Read a die, all of its descendents, and all of its siblings; set
10326 all of the fields of all of the dies correctly. Arguments are as
10327 in read_die_and_children. */
10328
10329static struct die_info *
93311388
DE
10330read_die_and_siblings (const struct die_reader_specs *reader,
10331 gdb_byte *info_ptr,
fe1b8b76 10332 gdb_byte **new_info_ptr,
639d11d3
DC
10333 struct die_info *parent)
10334{
10335 struct die_info *first_die, *last_sibling;
fe1b8b76 10336 gdb_byte *cur_ptr;
639d11d3 10337
c906108c 10338 cur_ptr = info_ptr;
639d11d3
DC
10339 first_die = last_sibling = NULL;
10340
10341 while (1)
c906108c 10342 {
639d11d3 10343 struct die_info *die
dee91e82 10344 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 10345
1d325ec1 10346 if (die == NULL)
c906108c 10347 {
639d11d3
DC
10348 *new_info_ptr = cur_ptr;
10349 return first_die;
c906108c 10350 }
1d325ec1
DJ
10351
10352 if (!first_die)
10353 first_die = die;
c906108c 10354 else
1d325ec1
DJ
10355 last_sibling->sibling = die;
10356
10357 last_sibling = die;
c906108c 10358 }
c906108c
SS
10359}
10360
3019eac3
DE
10361/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10362 attributes.
10363 The caller is responsible for filling in the extra attributes
10364 and updating (*DIEP)->num_attrs.
10365 Set DIEP to point to a newly allocated die with its information,
10366 except for its child, sibling, and parent fields.
10367 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
10368
10369static gdb_byte *
3019eac3
DE
10370read_full_die_1 (const struct die_reader_specs *reader,
10371 struct die_info **diep, gdb_byte *info_ptr,
10372 int *has_children, int num_extra_attrs)
93311388 10373{
b64f50a1
JK
10374 unsigned int abbrev_number, bytes_read, i;
10375 sect_offset offset;
93311388
DE
10376 struct abbrev_info *abbrev;
10377 struct die_info *die;
10378 struct dwarf2_cu *cu = reader->cu;
10379 bfd *abfd = reader->abfd;
10380
b64f50a1 10381 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
10382 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10383 info_ptr += bytes_read;
10384 if (!abbrev_number)
10385 {
10386 *diep = NULL;
10387 *has_children = 0;
10388 return info_ptr;
10389 }
10390
10391 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10392 if (!abbrev)
348e048f
DE
10393 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10394 abbrev_number,
10395 bfd_get_filename (abfd));
10396
3019eac3 10397 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
10398 die->offset = offset;
10399 die->tag = abbrev->tag;
10400 die->abbrev = abbrev_number;
10401
3019eac3
DE
10402 /* Make the result usable.
10403 The caller needs to update num_attrs after adding the extra
10404 attributes. */
93311388
DE
10405 die->num_attrs = abbrev->num_attrs;
10406
10407 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
10408 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10409 info_ptr);
93311388
DE
10410
10411 *diep = die;
10412 *has_children = abbrev->has_children;
10413 return info_ptr;
10414}
10415
3019eac3
DE
10416/* Read a die and all its attributes.
10417 Set DIEP to point to a newly allocated die with its information,
10418 except for its child, sibling, and parent fields.
10419 Set HAS_CHILDREN to tell whether the die has children or not. */
10420
10421static gdb_byte *
10422read_full_die (const struct die_reader_specs *reader,
10423 struct die_info **diep, gdb_byte *info_ptr,
10424 int *has_children)
10425{
10426 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10427}
10428
c906108c
SS
10429/* In DWARF version 2, the description of the debugging information is
10430 stored in a separate .debug_abbrev section. Before we read any
10431 dies from a section we read in all abbreviations and install them
72bf9492
DJ
10432 in a hash table. This function also sets flags in CU describing
10433 the data found in the abbrev table. */
c906108c
SS
10434
10435static void
dee91e82
DE
10436dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10437 struct dwarf2_section_info *abbrev_section)
10438
c906108c 10439{
dee91e82 10440 bfd *abfd = abbrev_section->asection->owner;
e7c27a73 10441 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 10442 gdb_byte *abbrev_ptr;
c906108c
SS
10443 struct abbrev_info *cur_abbrev;
10444 unsigned int abbrev_number, bytes_read, abbrev_name;
10445 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
10446 struct attr_abbrev *cur_attrs;
10447 unsigned int allocated_attrs;
c906108c 10448
0963b4bd 10449 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
10450 obstack_init (&cu->abbrev_obstack);
10451 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10452 (ABBREV_HASH_SIZE
10453 * sizeof (struct abbrev_info *)));
10454 memset (cu->dwarf2_abbrevs, 0,
10455 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 10456
dee91e82
DE
10457 dwarf2_read_section (cu->objfile, abbrev_section);
10458 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
c906108c
SS
10459 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10460 abbrev_ptr += bytes_read;
10461
f3dd6933
DJ
10462 allocated_attrs = ATTR_ALLOC_CHUNK;
10463 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 10464
0963b4bd 10465 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
10466 while (abbrev_number)
10467 {
f3dd6933 10468 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
10469
10470 /* read in abbrev header */
10471 cur_abbrev->number = abbrev_number;
10472 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10473 abbrev_ptr += bytes_read;
10474 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
10475 abbrev_ptr += 1;
10476
10477 /* now read in declarations */
10478 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10479 abbrev_ptr += bytes_read;
10480 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10481 abbrev_ptr += bytes_read;
10482 while (abbrev_name)
10483 {
f3dd6933 10484 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 10485 {
f3dd6933
DJ
10486 allocated_attrs += ATTR_ALLOC_CHUNK;
10487 cur_attrs
10488 = xrealloc (cur_attrs, (allocated_attrs
10489 * sizeof (struct attr_abbrev)));
c906108c 10490 }
ae038cb0 10491
f3dd6933
DJ
10492 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
10493 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
10494 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10495 abbrev_ptr += bytes_read;
10496 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10497 abbrev_ptr += bytes_read;
10498 }
10499
f3dd6933
DJ
10500 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
10501 (cur_abbrev->num_attrs
10502 * sizeof (struct attr_abbrev)));
10503 memcpy (cur_abbrev->attrs, cur_attrs,
10504 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
10505
c906108c 10506 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
10507 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
10508 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
10509
10510 /* Get next abbreviation.
10511 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
10512 always properly terminated with an abbrev number of 0.
10513 Exit loop if we encounter an abbreviation which we have
10514 already read (which means we are about to read the abbreviations
10515 for the next compile unit) or if the end of the abbreviation
10516 table is reached. */
dee91e82
DE
10517 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
10518 >= abbrev_section->size)
c906108c
SS
10519 break;
10520 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10521 abbrev_ptr += bytes_read;
e7c27a73 10522 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
10523 break;
10524 }
f3dd6933
DJ
10525
10526 xfree (cur_attrs);
c906108c
SS
10527}
10528
f3dd6933 10529/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 10530
c906108c 10531static void
f3dd6933 10532dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 10533{
f3dd6933 10534 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 10535
f3dd6933
DJ
10536 obstack_free (&cu->abbrev_obstack, NULL);
10537 cu->dwarf2_abbrevs = NULL;
c906108c
SS
10538}
10539
10540/* Lookup an abbrev_info structure in the abbrev hash table. */
10541
10542static struct abbrev_info *
e7c27a73 10543dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
10544{
10545 unsigned int hash_number;
10546 struct abbrev_info *abbrev;
10547
10548 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 10549 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
10550
10551 while (abbrev)
10552 {
10553 if (abbrev->number == number)
10554 return abbrev;
10555 else
10556 abbrev = abbrev->next;
10557 }
10558 return NULL;
10559}
10560
72bf9492
DJ
10561/* Returns nonzero if TAG represents a type that we might generate a partial
10562 symbol for. */
10563
10564static int
10565is_type_tag_for_partial (int tag)
10566{
10567 switch (tag)
10568 {
10569#if 0
10570 /* Some types that would be reasonable to generate partial symbols for,
10571 that we don't at present. */
10572 case DW_TAG_array_type:
10573 case DW_TAG_file_type:
10574 case DW_TAG_ptr_to_member_type:
10575 case DW_TAG_set_type:
10576 case DW_TAG_string_type:
10577 case DW_TAG_subroutine_type:
10578#endif
10579 case DW_TAG_base_type:
10580 case DW_TAG_class_type:
680b30c7 10581 case DW_TAG_interface_type:
72bf9492
DJ
10582 case DW_TAG_enumeration_type:
10583 case DW_TAG_structure_type:
10584 case DW_TAG_subrange_type:
10585 case DW_TAG_typedef:
10586 case DW_TAG_union_type:
10587 return 1;
10588 default:
10589 return 0;
10590 }
10591}
10592
10593/* Load all DIEs that are interesting for partial symbols into memory. */
10594
10595static struct partial_die_info *
dee91e82
DE
10596load_partial_dies (const struct die_reader_specs *reader,
10597 gdb_byte *info_ptr, int building_psymtab)
72bf9492 10598{
dee91e82 10599 struct dwarf2_cu *cu = reader->cu;
bb5ed363 10600 struct objfile *objfile = cu->objfile;
72bf9492
DJ
10601 struct partial_die_info *part_die;
10602 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
10603 struct abbrev_info *abbrev;
10604 unsigned int bytes_read;
5afb4e99 10605 unsigned int load_all = 0;
72bf9492
DJ
10606 int nesting_level = 1;
10607
10608 parent_die = NULL;
10609 last_die = NULL;
10610
7adf1e79
DE
10611 gdb_assert (cu->per_cu != NULL);
10612 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
10613 load_all = 1;
10614
72bf9492
DJ
10615 cu->partial_dies
10616 = htab_create_alloc_ex (cu->header.length / 12,
10617 partial_die_hash,
10618 partial_die_eq,
10619 NULL,
10620 &cu->comp_unit_obstack,
10621 hashtab_obstack_allocate,
10622 dummy_obstack_deallocate);
10623
10624 part_die = obstack_alloc (&cu->comp_unit_obstack,
10625 sizeof (struct partial_die_info));
10626
10627 while (1)
10628 {
10629 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
10630
10631 /* A NULL abbrev means the end of a series of children. */
10632 if (abbrev == NULL)
10633 {
10634 if (--nesting_level == 0)
10635 {
10636 /* PART_DIE was probably the last thing allocated on the
10637 comp_unit_obstack, so we could call obstack_free
10638 here. We don't do that because the waste is small,
10639 and will be cleaned up when we're done with this
10640 compilation unit. This way, we're also more robust
10641 against other users of the comp_unit_obstack. */
10642 return first_die;
10643 }
10644 info_ptr += bytes_read;
10645 last_die = parent_die;
10646 parent_die = parent_die->die_parent;
10647 continue;
10648 }
10649
98bfdba5
PA
10650 /* Check for template arguments. We never save these; if
10651 they're seen, we just mark the parent, and go on our way. */
10652 if (parent_die != NULL
10653 && cu->language == language_cplus
10654 && (abbrev->tag == DW_TAG_template_type_param
10655 || abbrev->tag == DW_TAG_template_value_param))
10656 {
10657 parent_die->has_template_arguments = 1;
10658
10659 if (!load_all)
10660 {
10661 /* We don't need a partial DIE for the template argument. */
dee91e82 10662 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
10663 continue;
10664 }
10665 }
10666
0d99eb77 10667 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
10668 Skip their other children. */
10669 if (!load_all
10670 && cu->language == language_cplus
10671 && parent_die != NULL
10672 && parent_die->tag == DW_TAG_subprogram)
10673 {
dee91e82 10674 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
10675 continue;
10676 }
10677
5afb4e99
DJ
10678 /* Check whether this DIE is interesting enough to save. Normally
10679 we would not be interested in members here, but there may be
10680 later variables referencing them via DW_AT_specification (for
10681 static members). */
10682 if (!load_all
10683 && !is_type_tag_for_partial (abbrev->tag)
72929c62 10684 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
10685 && abbrev->tag != DW_TAG_enumerator
10686 && abbrev->tag != DW_TAG_subprogram
bc30ff58 10687 && abbrev->tag != DW_TAG_lexical_block
72bf9492 10688 && abbrev->tag != DW_TAG_variable
5afb4e99 10689 && abbrev->tag != DW_TAG_namespace
f55ee35c 10690 && abbrev->tag != DW_TAG_module
5afb4e99 10691 && abbrev->tag != DW_TAG_member)
72bf9492
DJ
10692 {
10693 /* Otherwise we skip to the next sibling, if any. */
dee91e82 10694 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
10695 continue;
10696 }
10697
dee91e82
DE
10698 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
10699 info_ptr);
72bf9492
DJ
10700
10701 /* This two-pass algorithm for processing partial symbols has a
10702 high cost in cache pressure. Thus, handle some simple cases
10703 here which cover the majority of C partial symbols. DIEs
10704 which neither have specification tags in them, nor could have
10705 specification tags elsewhere pointing at them, can simply be
10706 processed and discarded.
10707
10708 This segment is also optional; scan_partial_symbols and
10709 add_partial_symbol will handle these DIEs if we chain
10710 them in normally. When compilers which do not emit large
10711 quantities of duplicate debug information are more common,
10712 this code can probably be removed. */
10713
10714 /* Any complete simple types at the top level (pretty much all
10715 of them, for a language without namespaces), can be processed
10716 directly. */
10717 if (parent_die == NULL
10718 && part_die->has_specification == 0
10719 && part_die->is_declaration == 0
d8228535 10720 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
10721 || part_die->tag == DW_TAG_base_type
10722 || part_die->tag == DW_TAG_subrange_type))
10723 {
10724 if (building_psymtab && part_die->name != NULL)
04a679b8 10725 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 10726 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
10727 &objfile->static_psymbols,
10728 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 10729 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
10730 continue;
10731 }
10732
d8228535
JK
10733 /* The exception for DW_TAG_typedef with has_children above is
10734 a workaround of GCC PR debug/47510. In the case of this complaint
10735 type_name_no_tag_or_error will error on such types later.
10736
10737 GDB skipped children of DW_TAG_typedef by the shortcut above and then
10738 it could not find the child DIEs referenced later, this is checked
10739 above. In correct DWARF DW_TAG_typedef should have no children. */
10740
10741 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
10742 complaint (&symfile_complaints,
10743 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
10744 "- DIE at 0x%x [in module %s]"),
b64f50a1 10745 part_die->offset.sect_off, objfile->name);
d8228535 10746
72bf9492
DJ
10747 /* If we're at the second level, and we're an enumerator, and
10748 our parent has no specification (meaning possibly lives in a
10749 namespace elsewhere), then we can add the partial symbol now
10750 instead of queueing it. */
10751 if (part_die->tag == DW_TAG_enumerator
10752 && parent_die != NULL
10753 && parent_die->die_parent == NULL
10754 && parent_die->tag == DW_TAG_enumeration_type
10755 && parent_die->has_specification == 0)
10756 {
10757 if (part_die->name == NULL)
3e43a32a
MS
10758 complaint (&symfile_complaints,
10759 _("malformed enumerator DIE ignored"));
72bf9492 10760 else if (building_psymtab)
04a679b8 10761 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 10762 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
10763 (cu->language == language_cplus
10764 || cu->language == language_java)
bb5ed363
DE
10765 ? &objfile->global_psymbols
10766 : &objfile->static_psymbols,
10767 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 10768
dee91e82 10769 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
10770 continue;
10771 }
10772
10773 /* We'll save this DIE so link it in. */
10774 part_die->die_parent = parent_die;
10775 part_die->die_sibling = NULL;
10776 part_die->die_child = NULL;
10777
10778 if (last_die && last_die == parent_die)
10779 last_die->die_child = part_die;
10780 else if (last_die)
10781 last_die->die_sibling = part_die;
10782
10783 last_die = part_die;
10784
10785 if (first_die == NULL)
10786 first_die = part_die;
10787
10788 /* Maybe add the DIE to the hash table. Not all DIEs that we
10789 find interesting need to be in the hash table, because we
10790 also have the parent/sibling/child chains; only those that we
10791 might refer to by offset later during partial symbol reading.
10792
10793 For now this means things that might have be the target of a
10794 DW_AT_specification, DW_AT_abstract_origin, or
10795 DW_AT_extension. DW_AT_extension will refer only to
10796 namespaces; DW_AT_abstract_origin refers to functions (and
10797 many things under the function DIE, but we do not recurse
10798 into function DIEs during partial symbol reading) and
10799 possibly variables as well; DW_AT_specification refers to
10800 declarations. Declarations ought to have the DW_AT_declaration
10801 flag. It happens that GCC forgets to put it in sometimes, but
10802 only for functions, not for types.
10803
10804 Adding more things than necessary to the hash table is harmless
10805 except for the performance cost. Adding too few will result in
5afb4e99
DJ
10806 wasted time in find_partial_die, when we reread the compilation
10807 unit with load_all_dies set. */
72bf9492 10808
5afb4e99 10809 if (load_all
72929c62 10810 || abbrev->tag == DW_TAG_constant
5afb4e99 10811 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
10812 || abbrev->tag == DW_TAG_variable
10813 || abbrev->tag == DW_TAG_namespace
10814 || part_die->is_declaration)
10815 {
10816 void **slot;
10817
10818 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 10819 part_die->offset.sect_off, INSERT);
72bf9492
DJ
10820 *slot = part_die;
10821 }
10822
10823 part_die = obstack_alloc (&cu->comp_unit_obstack,
10824 sizeof (struct partial_die_info));
10825
10826 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 10827 we have no reason to follow the children of structures; for other
98bfdba5
PA
10828 languages we have to, so that we can get at method physnames
10829 to infer fully qualified class names, for DW_AT_specification,
10830 and for C++ template arguments. For C++, we also look one level
10831 inside functions to find template arguments (if the name of the
10832 function does not already contain the template arguments).
bc30ff58
JB
10833
10834 For Ada, we need to scan the children of subprograms and lexical
10835 blocks as well because Ada allows the definition of nested
10836 entities that could be interesting for the debugger, such as
10837 nested subprograms for instance. */
72bf9492 10838 if (last_die->has_children
5afb4e99
DJ
10839 && (load_all
10840 || last_die->tag == DW_TAG_namespace
f55ee35c 10841 || last_die->tag == DW_TAG_module
72bf9492 10842 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
10843 || (cu->language == language_cplus
10844 && last_die->tag == DW_TAG_subprogram
10845 && (last_die->name == NULL
10846 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
10847 || (cu->language != language_c
10848 && (last_die->tag == DW_TAG_class_type
680b30c7 10849 || last_die->tag == DW_TAG_interface_type
72bf9492 10850 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
10851 || last_die->tag == DW_TAG_union_type))
10852 || (cu->language == language_ada
10853 && (last_die->tag == DW_TAG_subprogram
10854 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
10855 {
10856 nesting_level++;
10857 parent_die = last_die;
10858 continue;
10859 }
10860
10861 /* Otherwise we skip to the next sibling, if any. */
dee91e82 10862 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
10863
10864 /* Back to the top, do it again. */
10865 }
10866}
10867
c906108c
SS
10868/* Read a minimal amount of information into the minimal die structure. */
10869
fe1b8b76 10870static gdb_byte *
dee91e82
DE
10871read_partial_die (const struct die_reader_specs *reader,
10872 struct partial_die_info *part_die,
10873 struct abbrev_info *abbrev, unsigned int abbrev_len,
10874 gdb_byte *info_ptr)
c906108c 10875{
dee91e82 10876 struct dwarf2_cu *cu = reader->cu;
bb5ed363 10877 struct objfile *objfile = cu->objfile;
dee91e82 10878 gdb_byte *buffer = reader->buffer;
fa238c03 10879 unsigned int i;
c906108c 10880 struct attribute attr;
c5aa993b 10881 int has_low_pc_attr = 0;
c906108c 10882 int has_high_pc_attr = 0;
91da1414 10883 int high_pc_relative = 0;
c906108c 10884
72bf9492 10885 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 10886
b64f50a1 10887 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
10888
10889 info_ptr += abbrev_len;
10890
10891 if (abbrev == NULL)
10892 return info_ptr;
10893
c906108c
SS
10894 part_die->tag = abbrev->tag;
10895 part_die->has_children = abbrev->has_children;
c906108c
SS
10896
10897 for (i = 0; i < abbrev->num_attrs; ++i)
10898 {
dee91e82 10899 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
10900
10901 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 10902 partial symbol table. */
c906108c
SS
10903 switch (attr.name)
10904 {
10905 case DW_AT_name:
71c25dea
TT
10906 switch (part_die->tag)
10907 {
10908 case DW_TAG_compile_unit:
348e048f 10909 case DW_TAG_type_unit:
71c25dea
TT
10910 /* Compilation units have a DW_AT_name that is a filename, not
10911 a source language identifier. */
10912 case DW_TAG_enumeration_type:
10913 case DW_TAG_enumerator:
10914 /* These tags always have simple identifiers already; no need
10915 to canonicalize them. */
10916 part_die->name = DW_STRING (&attr);
10917 break;
10918 default:
10919 part_die->name
10920 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 10921 &objfile->objfile_obstack);
71c25dea
TT
10922 break;
10923 }
c906108c 10924 break;
31ef98ae 10925 case DW_AT_linkage_name:
c906108c 10926 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
10927 /* Note that both forms of linkage name might appear. We
10928 assume they will be the same, and we only store the last
10929 one we see. */
94af9270
KS
10930 if (cu->language == language_ada)
10931 part_die->name = DW_STRING (&attr);
abc72ce4 10932 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
10933 break;
10934 case DW_AT_low_pc:
10935 has_low_pc_attr = 1;
10936 part_die->lowpc = DW_ADDR (&attr);
10937 break;
10938 case DW_AT_high_pc:
10939 has_high_pc_attr = 1;
3019eac3
DE
10940 if (attr.form == DW_FORM_addr
10941 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
10942 part_die->highpc = DW_ADDR (&attr);
10943 else
10944 {
10945 high_pc_relative = 1;
10946 part_die->highpc = DW_UNSND (&attr);
10947 }
c906108c
SS
10948 break;
10949 case DW_AT_location:
0963b4bd 10950 /* Support the .debug_loc offsets. */
8e19ed76
PS
10951 if (attr_form_is_block (&attr))
10952 {
10953 part_die->locdesc = DW_BLOCK (&attr);
10954 }
3690dd37 10955 else if (attr_form_is_section_offset (&attr))
8e19ed76 10956 {
4d3c2250 10957 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
10958 }
10959 else
10960 {
4d3c2250
KB
10961 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
10962 "partial symbol information");
8e19ed76 10963 }
c906108c 10964 break;
c906108c
SS
10965 case DW_AT_external:
10966 part_die->is_external = DW_UNSND (&attr);
10967 break;
10968 case DW_AT_declaration:
10969 part_die->is_declaration = DW_UNSND (&attr);
10970 break;
10971 case DW_AT_type:
10972 part_die->has_type = 1;
10973 break;
10974 case DW_AT_abstract_origin:
10975 case DW_AT_specification:
72bf9492
DJ
10976 case DW_AT_extension:
10977 part_die->has_specification = 1;
c764a876 10978 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
10979 break;
10980 case DW_AT_sibling:
10981 /* Ignore absolute siblings, they might point outside of
10982 the current compile unit. */
10983 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
10984 complaint (&symfile_complaints,
10985 _("ignoring absolute DW_AT_sibling"));
c906108c 10986 else
b64f50a1 10987 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 10988 break;
fa4028e9
JB
10989 case DW_AT_byte_size:
10990 part_die->has_byte_size = 1;
10991 break;
68511cec
CES
10992 case DW_AT_calling_convention:
10993 /* DWARF doesn't provide a way to identify a program's source-level
10994 entry point. DW_AT_calling_convention attributes are only meant
10995 to describe functions' calling conventions.
10996
10997 However, because it's a necessary piece of information in
10998 Fortran, and because DW_CC_program is the only piece of debugging
10999 information whose definition refers to a 'main program' at all,
11000 several compilers have begun marking Fortran main programs with
11001 DW_CC_program --- even when those functions use the standard
11002 calling conventions.
11003
11004 So until DWARF specifies a way to provide this information and
11005 compilers pick up the new representation, we'll support this
11006 practice. */
11007 if (DW_UNSND (&attr) == DW_CC_program
11008 && cu->language == language_fortran)
01f8c46d
JK
11009 {
11010 set_main_name (part_die->name);
11011
11012 /* As this DIE has a static linkage the name would be difficult
11013 to look up later. */
11014 language_of_main = language_fortran;
11015 }
68511cec 11016 break;
481860b3
GB
11017 case DW_AT_inline:
11018 if (DW_UNSND (&attr) == DW_INL_inlined
11019 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11020 part_die->may_be_inlined = 1;
11021 break;
c906108c
SS
11022 default:
11023 break;
11024 }
11025 }
11026
91da1414
MW
11027 if (high_pc_relative)
11028 part_die->highpc += part_die->lowpc;
11029
9373cf26
JK
11030 if (has_low_pc_attr && has_high_pc_attr)
11031 {
11032 /* When using the GNU linker, .gnu.linkonce. sections are used to
11033 eliminate duplicate copies of functions and vtables and such.
11034 The linker will arbitrarily choose one and discard the others.
11035 The AT_*_pc values for such functions refer to local labels in
11036 these sections. If the section from that file was discarded, the
11037 labels are not in the output, so the relocs get a value of 0.
11038 If this is a discarded function, mark the pc bounds as invalid,
11039 so that GDB will ignore it. */
11040 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11041 {
bb5ed363 11042 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11043
11044 complaint (&symfile_complaints,
11045 _("DW_AT_low_pc %s is zero "
11046 "for DIE at 0x%x [in module %s]"),
11047 paddress (gdbarch, part_die->lowpc),
b64f50a1 11048 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11049 }
11050 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11051 else if (part_die->lowpc >= part_die->highpc)
11052 {
bb5ed363 11053 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11054
11055 complaint (&symfile_complaints,
11056 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11057 "for DIE at 0x%x [in module %s]"),
11058 paddress (gdbarch, part_die->lowpc),
11059 paddress (gdbarch, part_die->highpc),
b64f50a1 11060 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11061 }
11062 else
11063 part_die->has_pc_info = 1;
11064 }
85cbf3d3 11065
c906108c
SS
11066 return info_ptr;
11067}
11068
72bf9492
DJ
11069/* Find a cached partial DIE at OFFSET in CU. */
11070
11071static struct partial_die_info *
b64f50a1 11072find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
11073{
11074 struct partial_die_info *lookup_die = NULL;
11075 struct partial_die_info part_die;
11076
11077 part_die.offset = offset;
b64f50a1
JK
11078 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11079 offset.sect_off);
72bf9492 11080
72bf9492
DJ
11081 return lookup_die;
11082}
11083
348e048f
DE
11084/* Find a partial DIE at OFFSET, which may or may not be in CU,
11085 except in the case of .debug_types DIEs which do not reference
11086 outside their CU (they do however referencing other types via
55f1336d 11087 DW_FORM_ref_sig8). */
72bf9492
DJ
11088
11089static struct partial_die_info *
b64f50a1 11090find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
72bf9492 11091{
bb5ed363 11092 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
11093 struct dwarf2_per_cu_data *per_cu = NULL;
11094 struct partial_die_info *pd = NULL;
72bf9492 11095
45452591 11096 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
11097 {
11098 pd = find_partial_die_in_comp_unit (offset, cu);
11099 if (pd != NULL)
11100 return pd;
0d99eb77
DE
11101 /* We missed recording what we needed.
11102 Load all dies and try again. */
11103 per_cu = cu->per_cu;
5afb4e99 11104 }
0d99eb77
DE
11105 else
11106 {
11107 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 11108 if (cu->per_cu->is_debug_types)
0d99eb77
DE
11109 {
11110 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11111 " external reference to offset 0x%lx [in module %s].\n"),
11112 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11113 bfd_get_filename (objfile->obfd));
11114 }
11115 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
72bf9492 11116
0d99eb77
DE
11117 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11118 load_partial_comp_unit (per_cu);
ae038cb0 11119
0d99eb77
DE
11120 per_cu->cu->last_used = 0;
11121 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11122 }
5afb4e99 11123
dee91e82
DE
11124 /* If we didn't find it, and not all dies have been loaded,
11125 load them all and try again. */
11126
5afb4e99
DJ
11127 if (pd == NULL && per_cu->load_all_dies == 0)
11128 {
5afb4e99 11129 per_cu->load_all_dies = 1;
fd820528
DE
11130
11131 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11132 THIS_CU->cu may already be in use. So we can't just free it and
11133 replace its DIEs with the ones we read in. Instead, we leave those
11134 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11135 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11136 set. */
dee91e82 11137 load_partial_comp_unit (per_cu);
5afb4e99
DJ
11138
11139 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11140 }
11141
11142 if (pd == NULL)
11143 internal_error (__FILE__, __LINE__,
3e43a32a
MS
11144 _("could not find partial DIE 0x%x "
11145 "in cache [from module %s]\n"),
b64f50a1 11146 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 11147 return pd;
72bf9492
DJ
11148}
11149
abc72ce4
DE
11150/* See if we can figure out if the class lives in a namespace. We do
11151 this by looking for a member function; its demangled name will
11152 contain namespace info, if there is any. */
11153
11154static void
11155guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11156 struct dwarf2_cu *cu)
11157{
11158 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11159 what template types look like, because the demangler
11160 frequently doesn't give the same name as the debug info. We
11161 could fix this by only using the demangled name to get the
11162 prefix (but see comment in read_structure_type). */
11163
11164 struct partial_die_info *real_pdi;
11165 struct partial_die_info *child_pdi;
11166
11167 /* If this DIE (this DIE's specification, if any) has a parent, then
11168 we should not do this. We'll prepend the parent's fully qualified
11169 name when we create the partial symbol. */
11170
11171 real_pdi = struct_pdi;
11172 while (real_pdi->has_specification)
11173 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11174
11175 if (real_pdi->die_parent != NULL)
11176 return;
11177
11178 for (child_pdi = struct_pdi->die_child;
11179 child_pdi != NULL;
11180 child_pdi = child_pdi->die_sibling)
11181 {
11182 if (child_pdi->tag == DW_TAG_subprogram
11183 && child_pdi->linkage_name != NULL)
11184 {
11185 char *actual_class_name
11186 = language_class_name_from_physname (cu->language_defn,
11187 child_pdi->linkage_name);
11188 if (actual_class_name != NULL)
11189 {
11190 struct_pdi->name
11191 = obsavestring (actual_class_name,
11192 strlen (actual_class_name),
11193 &cu->objfile->objfile_obstack);
11194 xfree (actual_class_name);
11195 }
11196 break;
11197 }
11198 }
11199}
11200
72bf9492
DJ
11201/* Adjust PART_DIE before generating a symbol for it. This function
11202 may set the is_external flag or change the DIE's name. */
11203
11204static void
11205fixup_partial_die (struct partial_die_info *part_die,
11206 struct dwarf2_cu *cu)
11207{
abc72ce4
DE
11208 /* Once we've fixed up a die, there's no point in doing so again.
11209 This also avoids a memory leak if we were to call
11210 guess_partial_die_structure_name multiple times. */
11211 if (part_die->fixup_called)
11212 return;
11213
72bf9492
DJ
11214 /* If we found a reference attribute and the DIE has no name, try
11215 to find a name in the referred to DIE. */
11216
11217 if (part_die->name == NULL && part_die->has_specification)
11218 {
11219 struct partial_die_info *spec_die;
72bf9492 11220
10b3939b 11221 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 11222
10b3939b 11223 fixup_partial_die (spec_die, cu);
72bf9492
DJ
11224
11225 if (spec_die->name)
11226 {
11227 part_die->name = spec_die->name;
11228
11229 /* Copy DW_AT_external attribute if it is set. */
11230 if (spec_die->is_external)
11231 part_die->is_external = spec_die->is_external;
11232 }
11233 }
11234
11235 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
11236
11237 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 11238 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 11239
abc72ce4
DE
11240 /* If there is no parent die to provide a namespace, and there are
11241 children, see if we can determine the namespace from their linkage
122d1940 11242 name. */
abc72ce4 11243 if (cu->language == language_cplus
8b70b953 11244 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
11245 && part_die->die_parent == NULL
11246 && part_die->has_children
11247 && (part_die->tag == DW_TAG_class_type
11248 || part_die->tag == DW_TAG_structure_type
11249 || part_die->tag == DW_TAG_union_type))
11250 guess_partial_die_structure_name (part_die, cu);
11251
53832f31
TT
11252 /* GCC might emit a nameless struct or union that has a linkage
11253 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11254 if (part_die->name == NULL
96408a79
SA
11255 && (part_die->tag == DW_TAG_class_type
11256 || part_die->tag == DW_TAG_interface_type
11257 || part_die->tag == DW_TAG_structure_type
11258 || part_die->tag == DW_TAG_union_type)
53832f31
TT
11259 && part_die->linkage_name != NULL)
11260 {
11261 char *demangled;
11262
11263 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11264 if (demangled)
11265 {
96408a79
SA
11266 const char *base;
11267
11268 /* Strip any leading namespaces/classes, keep only the base name.
11269 DW_AT_name for named DIEs does not contain the prefixes. */
11270 base = strrchr (demangled, ':');
11271 if (base && base > demangled && base[-1] == ':')
11272 base++;
11273 else
11274 base = demangled;
11275
11276 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
11277 &cu->objfile->objfile_obstack);
11278 xfree (demangled);
11279 }
11280 }
11281
abc72ce4 11282 part_die->fixup_called = 1;
72bf9492
DJ
11283}
11284
a8329558 11285/* Read an attribute value described by an attribute form. */
c906108c 11286
fe1b8b76 11287static gdb_byte *
dee91e82
DE
11288read_attribute_value (const struct die_reader_specs *reader,
11289 struct attribute *attr, unsigned form,
11290 gdb_byte *info_ptr)
c906108c 11291{
dee91e82
DE
11292 struct dwarf2_cu *cu = reader->cu;
11293 bfd *abfd = reader->abfd;
e7c27a73 11294 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
11295 unsigned int bytes_read;
11296 struct dwarf_block *blk;
11297
a8329558
KW
11298 attr->form = form;
11299 switch (form)
c906108c 11300 {
c906108c 11301 case DW_FORM_ref_addr:
ae411497 11302 if (cu->header.version == 2)
4568ecf9 11303 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 11304 else
4568ecf9
DE
11305 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11306 &cu->header, &bytes_read);
ae411497
TT
11307 info_ptr += bytes_read;
11308 break;
11309 case DW_FORM_addr:
e7c27a73 11310 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 11311 info_ptr += bytes_read;
c906108c
SS
11312 break;
11313 case DW_FORM_block2:
7b5a2f43 11314 blk = dwarf_alloc_block (cu);
c906108c
SS
11315 blk->size = read_2_bytes (abfd, info_ptr);
11316 info_ptr += 2;
11317 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11318 info_ptr += blk->size;
11319 DW_BLOCK (attr) = blk;
11320 break;
11321 case DW_FORM_block4:
7b5a2f43 11322 blk = dwarf_alloc_block (cu);
c906108c
SS
11323 blk->size = read_4_bytes (abfd, info_ptr);
11324 info_ptr += 4;
11325 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11326 info_ptr += blk->size;
11327 DW_BLOCK (attr) = blk;
11328 break;
11329 case DW_FORM_data2:
11330 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11331 info_ptr += 2;
11332 break;
11333 case DW_FORM_data4:
11334 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11335 info_ptr += 4;
11336 break;
11337 case DW_FORM_data8:
11338 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11339 info_ptr += 8;
11340 break;
2dc7f7b3
TT
11341 case DW_FORM_sec_offset:
11342 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11343 info_ptr += bytes_read;
11344 break;
c906108c 11345 case DW_FORM_string:
9b1c24c8 11346 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 11347 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
11348 info_ptr += bytes_read;
11349 break;
4bdf3d34
JJ
11350 case DW_FORM_strp:
11351 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11352 &bytes_read);
8285870a 11353 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
11354 info_ptr += bytes_read;
11355 break;
2dc7f7b3 11356 case DW_FORM_exprloc:
c906108c 11357 case DW_FORM_block:
7b5a2f43 11358 blk = dwarf_alloc_block (cu);
c906108c
SS
11359 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11360 info_ptr += bytes_read;
11361 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11362 info_ptr += blk->size;
11363 DW_BLOCK (attr) = blk;
11364 break;
11365 case DW_FORM_block1:
7b5a2f43 11366 blk = dwarf_alloc_block (cu);
c906108c
SS
11367 blk->size = read_1_byte (abfd, info_ptr);
11368 info_ptr += 1;
11369 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11370 info_ptr += blk->size;
11371 DW_BLOCK (attr) = blk;
11372 break;
11373 case DW_FORM_data1:
11374 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11375 info_ptr += 1;
11376 break;
11377 case DW_FORM_flag:
11378 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11379 info_ptr += 1;
11380 break;
2dc7f7b3
TT
11381 case DW_FORM_flag_present:
11382 DW_UNSND (attr) = 1;
11383 break;
c906108c
SS
11384 case DW_FORM_sdata:
11385 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11386 info_ptr += bytes_read;
11387 break;
11388 case DW_FORM_udata:
11389 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11390 info_ptr += bytes_read;
11391 break;
11392 case DW_FORM_ref1:
4568ecf9
DE
11393 DW_UNSND (attr) = (cu->header.offset.sect_off
11394 + read_1_byte (abfd, info_ptr));
c906108c
SS
11395 info_ptr += 1;
11396 break;
11397 case DW_FORM_ref2:
4568ecf9
DE
11398 DW_UNSND (attr) = (cu->header.offset.sect_off
11399 + read_2_bytes (abfd, info_ptr));
c906108c
SS
11400 info_ptr += 2;
11401 break;
11402 case DW_FORM_ref4:
4568ecf9
DE
11403 DW_UNSND (attr) = (cu->header.offset.sect_off
11404 + read_4_bytes (abfd, info_ptr));
c906108c
SS
11405 info_ptr += 4;
11406 break;
613e1657 11407 case DW_FORM_ref8:
4568ecf9
DE
11408 DW_UNSND (attr) = (cu->header.offset.sect_off
11409 + read_8_bytes (abfd, info_ptr));
613e1657
KB
11410 info_ptr += 8;
11411 break;
55f1336d 11412 case DW_FORM_ref_sig8:
348e048f
DE
11413 /* Convert the signature to something we can record in DW_UNSND
11414 for later lookup.
11415 NOTE: This is NULL if the type wasn't found. */
11416 DW_SIGNATURED_TYPE (attr) =
e319fa28 11417 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
11418 info_ptr += 8;
11419 break;
c906108c 11420 case DW_FORM_ref_udata:
4568ecf9
DE
11421 DW_UNSND (attr) = (cu->header.offset.sect_off
11422 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
11423 info_ptr += bytes_read;
11424 break;
c906108c 11425 case DW_FORM_indirect:
a8329558
KW
11426 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11427 info_ptr += bytes_read;
dee91e82 11428 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 11429 break;
3019eac3
DE
11430 case DW_FORM_GNU_addr_index:
11431 if (reader->dwo_file == NULL)
11432 {
11433 /* For now flag a hard error.
11434 Later we can turn this into a complaint. */
11435 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11436 dwarf_form_name (form),
11437 bfd_get_filename (abfd));
11438 }
11439 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11440 info_ptr += bytes_read;
11441 break;
11442 case DW_FORM_GNU_str_index:
11443 if (reader->dwo_file == NULL)
11444 {
11445 /* For now flag a hard error.
11446 Later we can turn this into a complaint if warranted. */
11447 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11448 dwarf_form_name (form),
11449 bfd_get_filename (abfd));
11450 }
11451 {
11452 ULONGEST str_index =
11453 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11454
11455 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11456 DW_STRING_IS_CANONICAL (attr) = 0;
11457 info_ptr += bytes_read;
11458 }
11459 break;
c906108c 11460 default:
8a3fe4f8 11461 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
11462 dwarf_form_name (form),
11463 bfd_get_filename (abfd));
c906108c 11464 }
28e94949
JB
11465
11466 /* We have seen instances where the compiler tried to emit a byte
11467 size attribute of -1 which ended up being encoded as an unsigned
11468 0xffffffff. Although 0xffffffff is technically a valid size value,
11469 an object of this size seems pretty unlikely so we can relatively
11470 safely treat these cases as if the size attribute was invalid and
11471 treat them as zero by default. */
11472 if (attr->name == DW_AT_byte_size
11473 && form == DW_FORM_data4
11474 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
11475 {
11476 complaint
11477 (&symfile_complaints,
43bbcdc2
PH
11478 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
11479 hex_string (DW_UNSND (attr)));
01c66ae6
JB
11480 DW_UNSND (attr) = 0;
11481 }
28e94949 11482
c906108c
SS
11483 return info_ptr;
11484}
11485
a8329558
KW
11486/* Read an attribute described by an abbreviated attribute. */
11487
fe1b8b76 11488static gdb_byte *
dee91e82
DE
11489read_attribute (const struct die_reader_specs *reader,
11490 struct attribute *attr, struct attr_abbrev *abbrev,
11491 gdb_byte *info_ptr)
a8329558
KW
11492{
11493 attr->name = abbrev->name;
dee91e82 11494 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
11495}
11496
0963b4bd 11497/* Read dwarf information from a buffer. */
c906108c
SS
11498
11499static unsigned int
fe1b8b76 11500read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 11501{
fe1b8b76 11502 return bfd_get_8 (abfd, buf);
c906108c
SS
11503}
11504
11505static int
fe1b8b76 11506read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 11507{
fe1b8b76 11508 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
11509}
11510
11511static unsigned int
fe1b8b76 11512read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11513{
fe1b8b76 11514 return bfd_get_16 (abfd, buf);
c906108c
SS
11515}
11516
21ae7a4d
JK
11517static int
11518read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
11519{
11520 return bfd_get_signed_16 (abfd, buf);
11521}
11522
c906108c 11523static unsigned int
fe1b8b76 11524read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11525{
fe1b8b76 11526 return bfd_get_32 (abfd, buf);
c906108c
SS
11527}
11528
21ae7a4d
JK
11529static int
11530read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
11531{
11532 return bfd_get_signed_32 (abfd, buf);
11533}
11534
93311388 11535static ULONGEST
fe1b8b76 11536read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11537{
fe1b8b76 11538 return bfd_get_64 (abfd, buf);
c906108c
SS
11539}
11540
11541static CORE_ADDR
fe1b8b76 11542read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 11543 unsigned int *bytes_read)
c906108c 11544{
e7c27a73 11545 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
11546 CORE_ADDR retval = 0;
11547
107d2387 11548 if (cu_header->signed_addr_p)
c906108c 11549 {
107d2387
AC
11550 switch (cu_header->addr_size)
11551 {
11552 case 2:
fe1b8b76 11553 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
11554 break;
11555 case 4:
fe1b8b76 11556 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
11557 break;
11558 case 8:
fe1b8b76 11559 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
11560 break;
11561 default:
8e65ff28 11562 internal_error (__FILE__, __LINE__,
e2e0b3e5 11563 _("read_address: bad switch, signed [in module %s]"),
659b0389 11564 bfd_get_filename (abfd));
107d2387
AC
11565 }
11566 }
11567 else
11568 {
11569 switch (cu_header->addr_size)
11570 {
11571 case 2:
fe1b8b76 11572 retval = bfd_get_16 (abfd, buf);
107d2387
AC
11573 break;
11574 case 4:
fe1b8b76 11575 retval = bfd_get_32 (abfd, buf);
107d2387
AC
11576 break;
11577 case 8:
fe1b8b76 11578 retval = bfd_get_64 (abfd, buf);
107d2387
AC
11579 break;
11580 default:
8e65ff28 11581 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
11582 _("read_address: bad switch, "
11583 "unsigned [in module %s]"),
659b0389 11584 bfd_get_filename (abfd));
107d2387 11585 }
c906108c 11586 }
64367e0a 11587
107d2387
AC
11588 *bytes_read = cu_header->addr_size;
11589 return retval;
c906108c
SS
11590}
11591
f7ef9339 11592/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
11593 specification allows the initial length to take up either 4 bytes
11594 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
11595 bytes describe the length and all offsets will be 8 bytes in length
11596 instead of 4.
11597
f7ef9339
KB
11598 An older, non-standard 64-bit format is also handled by this
11599 function. The older format in question stores the initial length
11600 as an 8-byte quantity without an escape value. Lengths greater
11601 than 2^32 aren't very common which means that the initial 4 bytes
11602 is almost always zero. Since a length value of zero doesn't make
11603 sense for the 32-bit format, this initial zero can be considered to
11604 be an escape value which indicates the presence of the older 64-bit
11605 format. As written, the code can't detect (old format) lengths
917c78fc
MK
11606 greater than 4GB. If it becomes necessary to handle lengths
11607 somewhat larger than 4GB, we could allow other small values (such
11608 as the non-sensical values of 1, 2, and 3) to also be used as
11609 escape values indicating the presence of the old format.
f7ef9339 11610
917c78fc
MK
11611 The value returned via bytes_read should be used to increment the
11612 relevant pointer after calling read_initial_length().
c764a876 11613
613e1657
KB
11614 [ Note: read_initial_length() and read_offset() are based on the
11615 document entitled "DWARF Debugging Information Format", revision
f7ef9339 11616 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
11617 from:
11618
f7ef9339 11619 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 11620
613e1657
KB
11621 This document is only a draft and is subject to change. (So beware.)
11622
f7ef9339 11623 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
11624 determined empirically by examining 64-bit ELF files produced by
11625 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
11626
11627 - Kevin, July 16, 2002
613e1657
KB
11628 ] */
11629
11630static LONGEST
c764a876 11631read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 11632{
fe1b8b76 11633 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 11634
dd373385 11635 if (length == 0xffffffff)
613e1657 11636 {
fe1b8b76 11637 length = bfd_get_64 (abfd, buf + 4);
613e1657 11638 *bytes_read = 12;
613e1657 11639 }
dd373385 11640 else if (length == 0)
f7ef9339 11641 {
dd373385 11642 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 11643 length = bfd_get_64 (abfd, buf);
f7ef9339 11644 *bytes_read = 8;
f7ef9339 11645 }
613e1657
KB
11646 else
11647 {
11648 *bytes_read = 4;
613e1657
KB
11649 }
11650
c764a876
DE
11651 return length;
11652}
dd373385 11653
c764a876
DE
11654/* Cover function for read_initial_length.
11655 Returns the length of the object at BUF, and stores the size of the
11656 initial length in *BYTES_READ and stores the size that offsets will be in
11657 *OFFSET_SIZE.
11658 If the initial length size is not equivalent to that specified in
11659 CU_HEADER then issue a complaint.
11660 This is useful when reading non-comp-unit headers. */
dd373385 11661
c764a876
DE
11662static LONGEST
11663read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
11664 const struct comp_unit_head *cu_header,
11665 unsigned int *bytes_read,
11666 unsigned int *offset_size)
11667{
11668 LONGEST length = read_initial_length (abfd, buf, bytes_read);
11669
11670 gdb_assert (cu_header->initial_length_size == 4
11671 || cu_header->initial_length_size == 8
11672 || cu_header->initial_length_size == 12);
11673
11674 if (cu_header->initial_length_size != *bytes_read)
11675 complaint (&symfile_complaints,
11676 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 11677
c764a876 11678 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 11679 return length;
613e1657
KB
11680}
11681
11682/* Read an offset from the data stream. The size of the offset is
917c78fc 11683 given by cu_header->offset_size. */
613e1657
KB
11684
11685static LONGEST
fe1b8b76 11686read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 11687 unsigned int *bytes_read)
c764a876
DE
11688{
11689 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 11690
c764a876
DE
11691 *bytes_read = cu_header->offset_size;
11692 return offset;
11693}
11694
11695/* Read an offset from the data stream. */
11696
11697static LONGEST
11698read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
11699{
11700 LONGEST retval = 0;
11701
c764a876 11702 switch (offset_size)
613e1657
KB
11703 {
11704 case 4:
fe1b8b76 11705 retval = bfd_get_32 (abfd, buf);
613e1657
KB
11706 break;
11707 case 8:
fe1b8b76 11708 retval = bfd_get_64 (abfd, buf);
613e1657
KB
11709 break;
11710 default:
8e65ff28 11711 internal_error (__FILE__, __LINE__,
c764a876 11712 _("read_offset_1: bad switch [in module %s]"),
659b0389 11713 bfd_get_filename (abfd));
613e1657
KB
11714 }
11715
917c78fc 11716 return retval;
613e1657
KB
11717}
11718
fe1b8b76
JB
11719static gdb_byte *
11720read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
11721{
11722 /* If the size of a host char is 8 bits, we can return a pointer
11723 to the buffer, otherwise we have to copy the data to a buffer
11724 allocated on the temporary obstack. */
4bdf3d34 11725 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 11726 return buf;
c906108c
SS
11727}
11728
11729static char *
9b1c24c8 11730read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
11731{
11732 /* If the size of a host char is 8 bits, we can return a pointer
11733 to the string, otherwise we have to copy the string to a buffer
11734 allocated on the temporary obstack. */
4bdf3d34 11735 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
11736 if (*buf == '\0')
11737 {
11738 *bytes_read_ptr = 1;
11739 return NULL;
11740 }
fe1b8b76
JB
11741 *bytes_read_ptr = strlen ((char *) buf) + 1;
11742 return (char *) buf;
4bdf3d34
JJ
11743}
11744
11745static char *
cf2c3c16 11746read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 11747{
be391dca 11748 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 11749 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
11750 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
11751 bfd_get_filename (abfd));
dce234bc 11752 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
11753 error (_("DW_FORM_strp pointing outside of "
11754 ".debug_str section [in module %s]"),
11755 bfd_get_filename (abfd));
4bdf3d34 11756 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 11757 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 11758 return NULL;
dce234bc 11759 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
11760}
11761
cf2c3c16
TT
11762static char *
11763read_indirect_string (bfd *abfd, gdb_byte *buf,
11764 const struct comp_unit_head *cu_header,
11765 unsigned int *bytes_read_ptr)
11766{
11767 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
11768
11769 return read_indirect_string_at_offset (abfd, str_offset);
11770}
11771
12df843f 11772static ULONGEST
fe1b8b76 11773read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 11774{
12df843f 11775 ULONGEST result;
ce5d95e1 11776 unsigned int num_read;
c906108c
SS
11777 int i, shift;
11778 unsigned char byte;
11779
11780 result = 0;
11781 shift = 0;
11782 num_read = 0;
11783 i = 0;
11784 while (1)
11785 {
fe1b8b76 11786 byte = bfd_get_8 (abfd, buf);
c906108c
SS
11787 buf++;
11788 num_read++;
12df843f 11789 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
11790 if ((byte & 128) == 0)
11791 {
11792 break;
11793 }
11794 shift += 7;
11795 }
11796 *bytes_read_ptr = num_read;
11797 return result;
11798}
11799
12df843f 11800static LONGEST
fe1b8b76 11801read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 11802{
12df843f 11803 LONGEST result;
77e0b926 11804 int i, shift, num_read;
c906108c
SS
11805 unsigned char byte;
11806
11807 result = 0;
11808 shift = 0;
c906108c
SS
11809 num_read = 0;
11810 i = 0;
11811 while (1)
11812 {
fe1b8b76 11813 byte = bfd_get_8 (abfd, buf);
c906108c
SS
11814 buf++;
11815 num_read++;
12df843f 11816 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
11817 shift += 7;
11818 if ((byte & 128) == 0)
11819 {
11820 break;
11821 }
11822 }
77e0b926 11823 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 11824 result |= -(((LONGEST) 1) << shift);
c906108c
SS
11825 *bytes_read_ptr = num_read;
11826 return result;
11827}
11828
3019eac3
DE
11829/* Given index ADDR_INDEX in .debug_addr, fetch the value.
11830 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
11831 ADDR_SIZE is the size of addresses from the CU header. */
11832
11833static CORE_ADDR
11834read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
11835{
11836 struct objfile *objfile = dwarf2_per_objfile->objfile;
11837 bfd *abfd = objfile->obfd;
11838 const gdb_byte *info_ptr;
11839
11840 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
11841 if (dwarf2_per_objfile->addr.buffer == NULL)
11842 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
11843 objfile->name);
11844 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
11845 error (_("DW_FORM_addr_index pointing outside of "
11846 ".debug_addr section [in module %s]"),
11847 objfile->name);
11848 info_ptr = (dwarf2_per_objfile->addr.buffer
11849 + addr_base + addr_index * addr_size);
11850 if (addr_size == 4)
11851 return bfd_get_32 (abfd, info_ptr);
11852 else
11853 return bfd_get_64 (abfd, info_ptr);
11854}
11855
11856/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
11857
11858static CORE_ADDR
11859read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
11860{
11861 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
11862}
11863
11864/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
11865
11866static CORE_ADDR
11867read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
11868 unsigned int *bytes_read)
11869{
11870 bfd *abfd = cu->objfile->obfd;
11871 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
11872
11873 return read_addr_index (cu, addr_index);
11874}
11875
11876/* Data structure to pass results from dwarf2_read_addr_index_reader
11877 back to dwarf2_read_addr_index. */
11878
11879struct dwarf2_read_addr_index_data
11880{
11881 ULONGEST addr_base;
11882 int addr_size;
11883};
11884
11885/* die_reader_func for dwarf2_read_addr_index. */
11886
11887static void
11888dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
11889 gdb_byte *info_ptr,
11890 struct die_info *comp_unit_die,
11891 int has_children,
11892 void *data)
11893{
11894 struct dwarf2_cu *cu = reader->cu;
11895 struct dwarf2_read_addr_index_data *aidata =
11896 (struct dwarf2_read_addr_index_data *) data;
11897
11898 aidata->addr_base = cu->addr_base;
11899 aidata->addr_size = cu->header.addr_size;
11900}
11901
11902/* Given an index in .debug_addr, fetch the value.
11903 NOTE: This can be called during dwarf expression evaluation,
11904 long after the debug information has been read, and thus per_cu->cu
11905 may no longer exist. */
11906
11907CORE_ADDR
11908dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
11909 unsigned int addr_index)
11910{
11911 struct objfile *objfile = per_cu->objfile;
11912 struct dwarf2_cu *cu = per_cu->cu;
11913 ULONGEST addr_base;
11914 int addr_size;
11915
11916 /* This is intended to be called from outside this file. */
11917 dw2_setup (objfile);
11918
11919 /* We need addr_base and addr_size.
11920 If we don't have PER_CU->cu, we have to get it.
11921 Nasty, but the alternative is storing the needed info in PER_CU,
11922 which at this point doesn't seem justified: it's not clear how frequently
11923 it would get used and it would increase the size of every PER_CU.
11924 Entry points like dwarf2_per_cu_addr_size do a similar thing
11925 so we're not in uncharted territory here.
11926 Alas we need to be a bit more complicated as addr_base is contained
11927 in the DIE.
11928
11929 We don't need to read the entire CU(/TU).
11930 We just need the header and top level die.
11931 IWBN to use the aging mechanism to let us lazily later discard the CU.
11932 See however init_cutu_and_read_dies_simple. */
11933
11934 if (cu != NULL)
11935 {
11936 addr_base = cu->addr_base;
11937 addr_size = cu->header.addr_size;
11938 }
11939 else
11940 {
11941 struct dwarf2_read_addr_index_data aidata;
11942
11943 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
11944 &aidata);
11945 addr_base = aidata.addr_base;
11946 addr_size = aidata.addr_size;
11947 }
11948
11949 return read_addr_index_1 (addr_index, addr_base, addr_size);
11950}
11951
11952/* Given a DW_AT_str_index, fetch the string. */
11953
11954static char *
11955read_str_index (const struct die_reader_specs *reader,
11956 struct dwarf2_cu *cu, ULONGEST str_index)
11957{
11958 struct objfile *objfile = dwarf2_per_objfile->objfile;
11959 const char *dwo_name = objfile->name;
11960 bfd *abfd = objfile->obfd;
11961 struct dwo_sections *sections = &reader->dwo_file->sections;
11962 gdb_byte *info_ptr;
11963 ULONGEST str_offset;
11964
11965 dwarf2_read_section (objfile, &sections->str);
11966 dwarf2_read_section (objfile, &sections->str_offsets);
11967 if (sections->str.buffer == NULL)
11968 error (_("DW_FORM_str_index used without .debug_str.dwo section"
11969 " in CU at offset 0x%lx [in module %s]"),
11970 (long) cu->header.offset.sect_off, dwo_name);
11971 if (sections->str_offsets.buffer == NULL)
11972 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
11973 " in CU at offset 0x%lx [in module %s]"),
11974 (long) cu->header.offset.sect_off, dwo_name);
11975 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
11976 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
11977 " section in CU at offset 0x%lx [in module %s]"),
11978 (long) cu->header.offset.sect_off, dwo_name);
11979 info_ptr = (sections->str_offsets.buffer
11980 + str_index * cu->header.offset_size);
11981 if (cu->header.offset_size == 4)
11982 str_offset = bfd_get_32 (abfd, info_ptr);
11983 else
11984 str_offset = bfd_get_64 (abfd, info_ptr);
11985 if (str_offset >= sections->str.size)
11986 error (_("Offset from DW_FORM_str_index pointing outside of"
11987 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
11988 (long) cu->header.offset.sect_off, dwo_name);
11989 return (char *) (sections->str.buffer + str_offset);
11990}
11991
4bb7a0a7
DJ
11992/* Return a pointer to just past the end of an LEB128 number in BUF. */
11993
fe1b8b76
JB
11994static gdb_byte *
11995skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
11996{
11997 int byte;
11998
11999 while (1)
12000 {
fe1b8b76 12001 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
12002 buf++;
12003 if ((byte & 128) == 0)
12004 return buf;
12005 }
12006}
12007
3019eac3
DE
12008/* Return the length of an LEB128 number in BUF. */
12009
12010static int
12011leb128_size (const gdb_byte *buf)
12012{
12013 const gdb_byte *begin = buf;
12014 gdb_byte byte;
12015
12016 while (1)
12017 {
12018 byte = *buf++;
12019 if ((byte & 128) == 0)
12020 return buf - begin;
12021 }
12022}
12023
c906108c 12024static void
e142c38c 12025set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
12026{
12027 switch (lang)
12028 {
12029 case DW_LANG_C89:
76bee0cc 12030 case DW_LANG_C99:
c906108c 12031 case DW_LANG_C:
e142c38c 12032 cu->language = language_c;
c906108c
SS
12033 break;
12034 case DW_LANG_C_plus_plus:
e142c38c 12035 cu->language = language_cplus;
c906108c 12036 break;
6aecb9c2
JB
12037 case DW_LANG_D:
12038 cu->language = language_d;
12039 break;
c906108c
SS
12040 case DW_LANG_Fortran77:
12041 case DW_LANG_Fortran90:
b21b22e0 12042 case DW_LANG_Fortran95:
e142c38c 12043 cu->language = language_fortran;
c906108c 12044 break;
a766d390
DE
12045 case DW_LANG_Go:
12046 cu->language = language_go;
12047 break;
c906108c 12048 case DW_LANG_Mips_Assembler:
e142c38c 12049 cu->language = language_asm;
c906108c 12050 break;
bebd888e 12051 case DW_LANG_Java:
e142c38c 12052 cu->language = language_java;
bebd888e 12053 break;
c906108c 12054 case DW_LANG_Ada83:
8aaf0b47 12055 case DW_LANG_Ada95:
bc5f45f8
JB
12056 cu->language = language_ada;
12057 break;
72019c9c
GM
12058 case DW_LANG_Modula2:
12059 cu->language = language_m2;
12060 break;
fe8e67fd
PM
12061 case DW_LANG_Pascal83:
12062 cu->language = language_pascal;
12063 break;
22566fbd
DJ
12064 case DW_LANG_ObjC:
12065 cu->language = language_objc;
12066 break;
c906108c
SS
12067 case DW_LANG_Cobol74:
12068 case DW_LANG_Cobol85:
c906108c 12069 default:
e142c38c 12070 cu->language = language_minimal;
c906108c
SS
12071 break;
12072 }
e142c38c 12073 cu->language_defn = language_def (cu->language);
c906108c
SS
12074}
12075
12076/* Return the named attribute or NULL if not there. */
12077
12078static struct attribute *
e142c38c 12079dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 12080{
a48e046c 12081 for (;;)
c906108c 12082 {
a48e046c
TT
12083 unsigned int i;
12084 struct attribute *spec = NULL;
12085
12086 for (i = 0; i < die->num_attrs; ++i)
12087 {
12088 if (die->attrs[i].name == name)
12089 return &die->attrs[i];
12090 if (die->attrs[i].name == DW_AT_specification
12091 || die->attrs[i].name == DW_AT_abstract_origin)
12092 spec = &die->attrs[i];
12093 }
12094
12095 if (!spec)
12096 break;
c906108c 12097
f2f0e013 12098 die = follow_die_ref (die, spec, &cu);
f2f0e013 12099 }
c5aa993b 12100
c906108c
SS
12101 return NULL;
12102}
12103
348e048f
DE
12104/* Return the named attribute or NULL if not there,
12105 but do not follow DW_AT_specification, etc.
12106 This is for use in contexts where we're reading .debug_types dies.
12107 Following DW_AT_specification, DW_AT_abstract_origin will take us
12108 back up the chain, and we want to go down. */
12109
12110static struct attribute *
12111dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12112 struct dwarf2_cu *cu)
12113{
12114 unsigned int i;
12115
12116 for (i = 0; i < die->num_attrs; ++i)
12117 if (die->attrs[i].name == name)
12118 return &die->attrs[i];
12119
12120 return NULL;
12121}
12122
05cf31d1
JB
12123/* Return non-zero iff the attribute NAME is defined for the given DIE,
12124 and holds a non-zero value. This function should only be used for
2dc7f7b3 12125 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
12126
12127static int
12128dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12129{
12130 struct attribute *attr = dwarf2_attr (die, name, cu);
12131
12132 return (attr && DW_UNSND (attr));
12133}
12134
3ca72b44 12135static int
e142c38c 12136die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 12137{
05cf31d1
JB
12138 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12139 which value is non-zero. However, we have to be careful with
12140 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12141 (via dwarf2_flag_true_p) follows this attribute. So we may
12142 end up accidently finding a declaration attribute that belongs
12143 to a different DIE referenced by the specification attribute,
12144 even though the given DIE does not have a declaration attribute. */
12145 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12146 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
12147}
12148
63d06c5c 12149/* Return the die giving the specification for DIE, if there is
f2f0e013 12150 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
12151 containing the return value on output. If there is no
12152 specification, but there is an abstract origin, that is
12153 returned. */
63d06c5c
DC
12154
12155static struct die_info *
f2f0e013 12156die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 12157{
f2f0e013
DJ
12158 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12159 *spec_cu);
63d06c5c 12160
edb3359d
DJ
12161 if (spec_attr == NULL)
12162 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12163
63d06c5c
DC
12164 if (spec_attr == NULL)
12165 return NULL;
12166 else
f2f0e013 12167 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 12168}
c906108c 12169
debd256d 12170/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
12171 refers to.
12172 NOTE: This is also used as a "cleanup" function. */
12173
debd256d
JB
12174static void
12175free_line_header (struct line_header *lh)
12176{
12177 if (lh->standard_opcode_lengths)
a8bc7b56 12178 xfree (lh->standard_opcode_lengths);
debd256d
JB
12179
12180 /* Remember that all the lh->file_names[i].name pointers are
12181 pointers into debug_line_buffer, and don't need to be freed. */
12182 if (lh->file_names)
a8bc7b56 12183 xfree (lh->file_names);
debd256d
JB
12184
12185 /* Similarly for the include directory names. */
12186 if (lh->include_dirs)
a8bc7b56 12187 xfree (lh->include_dirs);
debd256d 12188
a8bc7b56 12189 xfree (lh);
debd256d
JB
12190}
12191
debd256d 12192/* Add an entry to LH's include directory table. */
ae2de4f8 12193
debd256d
JB
12194static void
12195add_include_dir (struct line_header *lh, char *include_dir)
c906108c 12196{
debd256d
JB
12197 /* Grow the array if necessary. */
12198 if (lh->include_dirs_size == 0)
c5aa993b 12199 {
debd256d
JB
12200 lh->include_dirs_size = 1; /* for testing */
12201 lh->include_dirs = xmalloc (lh->include_dirs_size
12202 * sizeof (*lh->include_dirs));
12203 }
12204 else if (lh->num_include_dirs >= lh->include_dirs_size)
12205 {
12206 lh->include_dirs_size *= 2;
12207 lh->include_dirs = xrealloc (lh->include_dirs,
12208 (lh->include_dirs_size
12209 * sizeof (*lh->include_dirs)));
c5aa993b 12210 }
c906108c 12211
debd256d
JB
12212 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12213}
6e70227d 12214
debd256d 12215/* Add an entry to LH's file name table. */
ae2de4f8 12216
debd256d
JB
12217static void
12218add_file_name (struct line_header *lh,
12219 char *name,
12220 unsigned int dir_index,
12221 unsigned int mod_time,
12222 unsigned int length)
12223{
12224 struct file_entry *fe;
12225
12226 /* Grow the array if necessary. */
12227 if (lh->file_names_size == 0)
12228 {
12229 lh->file_names_size = 1; /* for testing */
12230 lh->file_names = xmalloc (lh->file_names_size
12231 * sizeof (*lh->file_names));
12232 }
12233 else if (lh->num_file_names >= lh->file_names_size)
12234 {
12235 lh->file_names_size *= 2;
12236 lh->file_names = xrealloc (lh->file_names,
12237 (lh->file_names_size
12238 * sizeof (*lh->file_names)));
12239 }
12240
12241 fe = &lh->file_names[lh->num_file_names++];
12242 fe->name = name;
12243 fe->dir_index = dir_index;
12244 fe->mod_time = mod_time;
12245 fe->length = length;
aaa75496 12246 fe->included_p = 0;
cb1df416 12247 fe->symtab = NULL;
debd256d 12248}
6e70227d 12249
debd256d 12250/* Read the statement program header starting at OFFSET in
3019eac3 12251 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 12252 to a struct line_header, allocated using xmalloc.
debd256d
JB
12253
12254 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
12255 the returned object point into the dwarf line section buffer,
12256 and must not be freed. */
ae2de4f8 12257
debd256d 12258static struct line_header *
3019eac3 12259dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
12260{
12261 struct cleanup *back_to;
12262 struct line_header *lh;
fe1b8b76 12263 gdb_byte *line_ptr;
c764a876 12264 unsigned int bytes_read, offset_size;
debd256d
JB
12265 int i;
12266 char *cur_dir, *cur_file;
3019eac3
DE
12267 struct dwarf2_section_info *section;
12268 bfd *abfd;
12269
12270 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12271 DWO file. */
12272 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12273 section = &cu->dwo_unit->dwo_file->sections.line;
12274 else
12275 section = &dwarf2_per_objfile->line;
debd256d 12276
3019eac3
DE
12277 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12278 if (section->buffer == NULL)
debd256d 12279 {
3019eac3
DE
12280 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12281 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12282 else
12283 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
12284 return 0;
12285 }
12286
fceca515
DE
12287 /* We can't do this until we know the section is non-empty.
12288 Only then do we know we have such a section. */
12289 abfd = section->asection->owner;
12290
a738430d
MK
12291 /* Make sure that at least there's room for the total_length field.
12292 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 12293 if (offset + 4 >= section->size)
debd256d 12294 {
4d3c2250 12295 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12296 return 0;
12297 }
12298
12299 lh = xmalloc (sizeof (*lh));
12300 memset (lh, 0, sizeof (*lh));
12301 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12302 (void *) lh);
12303
3019eac3 12304 line_ptr = section->buffer + offset;
debd256d 12305
a738430d 12306 /* Read in the header. */
6e70227d 12307 lh->total_length =
c764a876
DE
12308 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12309 &bytes_read, &offset_size);
debd256d 12310 line_ptr += bytes_read;
3019eac3 12311 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 12312 {
4d3c2250 12313 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12314 return 0;
12315 }
12316 lh->statement_program_end = line_ptr + lh->total_length;
12317 lh->version = read_2_bytes (abfd, line_ptr);
12318 line_ptr += 2;
c764a876
DE
12319 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12320 line_ptr += offset_size;
debd256d
JB
12321 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12322 line_ptr += 1;
2dc7f7b3
TT
12323 if (lh->version >= 4)
12324 {
12325 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12326 line_ptr += 1;
12327 }
12328 else
12329 lh->maximum_ops_per_instruction = 1;
12330
12331 if (lh->maximum_ops_per_instruction == 0)
12332 {
12333 lh->maximum_ops_per_instruction = 1;
12334 complaint (&symfile_complaints,
3e43a32a
MS
12335 _("invalid maximum_ops_per_instruction "
12336 "in `.debug_line' section"));
2dc7f7b3
TT
12337 }
12338
debd256d
JB
12339 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12340 line_ptr += 1;
12341 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12342 line_ptr += 1;
12343 lh->line_range = read_1_byte (abfd, line_ptr);
12344 line_ptr += 1;
12345 lh->opcode_base = read_1_byte (abfd, line_ptr);
12346 line_ptr += 1;
12347 lh->standard_opcode_lengths
fe1b8b76 12348 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
12349
12350 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12351 for (i = 1; i < lh->opcode_base; ++i)
12352 {
12353 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12354 line_ptr += 1;
12355 }
12356
a738430d 12357 /* Read directory table. */
9b1c24c8 12358 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12359 {
12360 line_ptr += bytes_read;
12361 add_include_dir (lh, cur_dir);
12362 }
12363 line_ptr += bytes_read;
12364
a738430d 12365 /* Read file name table. */
9b1c24c8 12366 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12367 {
12368 unsigned int dir_index, mod_time, length;
12369
12370 line_ptr += bytes_read;
12371 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12372 line_ptr += bytes_read;
12373 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12374 line_ptr += bytes_read;
12375 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12376 line_ptr += bytes_read;
12377
12378 add_file_name (lh, cur_file, dir_index, mod_time, length);
12379 }
12380 line_ptr += bytes_read;
6e70227d 12381 lh->statement_program_start = line_ptr;
debd256d 12382
3019eac3 12383 if (line_ptr > (section->buffer + section->size))
4d3c2250 12384 complaint (&symfile_complaints,
3e43a32a
MS
12385 _("line number info header doesn't "
12386 "fit in `.debug_line' section"));
debd256d
JB
12387
12388 discard_cleanups (back_to);
12389 return lh;
12390}
c906108c 12391
c6da4cef
DE
12392/* Subroutine of dwarf_decode_lines to simplify it.
12393 Return the file name of the psymtab for included file FILE_INDEX
12394 in line header LH of PST.
12395 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12396 If space for the result is malloc'd, it will be freed by a cleanup.
12397 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12398
12399static char *
12400psymtab_include_file_name (const struct line_header *lh, int file_index,
12401 const struct partial_symtab *pst,
12402 const char *comp_dir)
12403{
12404 const struct file_entry fe = lh->file_names [file_index];
12405 char *include_name = fe.name;
12406 char *include_name_to_compare = include_name;
12407 char *dir_name = NULL;
72b9f47f
TT
12408 const char *pst_filename;
12409 char *copied_name = NULL;
c6da4cef
DE
12410 int file_is_pst;
12411
12412 if (fe.dir_index)
12413 dir_name = lh->include_dirs[fe.dir_index - 1];
12414
12415 if (!IS_ABSOLUTE_PATH (include_name)
12416 && (dir_name != NULL || comp_dir != NULL))
12417 {
12418 /* Avoid creating a duplicate psymtab for PST.
12419 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12420 Before we do the comparison, however, we need to account
12421 for DIR_NAME and COMP_DIR.
12422 First prepend dir_name (if non-NULL). If we still don't
12423 have an absolute path prepend comp_dir (if non-NULL).
12424 However, the directory we record in the include-file's
12425 psymtab does not contain COMP_DIR (to match the
12426 corresponding symtab(s)).
12427
12428 Example:
12429
12430 bash$ cd /tmp
12431 bash$ gcc -g ./hello.c
12432 include_name = "hello.c"
12433 dir_name = "."
12434 DW_AT_comp_dir = comp_dir = "/tmp"
12435 DW_AT_name = "./hello.c" */
12436
12437 if (dir_name != NULL)
12438 {
12439 include_name = concat (dir_name, SLASH_STRING,
12440 include_name, (char *)NULL);
12441 include_name_to_compare = include_name;
12442 make_cleanup (xfree, include_name);
12443 }
12444 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12445 {
12446 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12447 include_name, (char *)NULL);
12448 }
12449 }
12450
12451 pst_filename = pst->filename;
12452 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12453 {
72b9f47f
TT
12454 copied_name = concat (pst->dirname, SLASH_STRING,
12455 pst_filename, (char *)NULL);
12456 pst_filename = copied_name;
c6da4cef
DE
12457 }
12458
1e3fad37 12459 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
12460
12461 if (include_name_to_compare != include_name)
12462 xfree (include_name_to_compare);
72b9f47f
TT
12463 if (copied_name != NULL)
12464 xfree (copied_name);
c6da4cef
DE
12465
12466 if (file_is_pst)
12467 return NULL;
12468 return include_name;
12469}
12470
c91513d8
PP
12471/* Ignore this record_line request. */
12472
12473static void
12474noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12475{
12476 return;
12477}
12478
f3f5162e
DE
12479/* Subroutine of dwarf_decode_lines to simplify it.
12480 Process the line number information in LH. */
debd256d 12481
c906108c 12482static void
f3f5162e
DE
12483dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
12484 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 12485{
a8c50c1f 12486 gdb_byte *line_ptr, *extended_end;
fe1b8b76 12487 gdb_byte *line_end;
a8c50c1f 12488 unsigned int bytes_read, extended_len;
c906108c 12489 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
12490 CORE_ADDR baseaddr;
12491 struct objfile *objfile = cu->objfile;
f3f5162e 12492 bfd *abfd = objfile->obfd;
fbf65064 12493 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 12494 const int decode_for_pst_p = (pst != NULL);
f3f5162e 12495 struct subfile *last_subfile = NULL;
c91513d8
PP
12496 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
12497 = record_line;
e142c38c
DJ
12498
12499 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 12500
debd256d
JB
12501 line_ptr = lh->statement_program_start;
12502 line_end = lh->statement_program_end;
c906108c
SS
12503
12504 /* Read the statement sequences until there's nothing left. */
12505 while (line_ptr < line_end)
12506 {
12507 /* state machine registers */
12508 CORE_ADDR address = 0;
12509 unsigned int file = 1;
12510 unsigned int line = 1;
12511 unsigned int column = 0;
debd256d 12512 int is_stmt = lh->default_is_stmt;
c906108c
SS
12513 int basic_block = 0;
12514 int end_sequence = 0;
fbf65064 12515 CORE_ADDR addr;
2dc7f7b3 12516 unsigned char op_index = 0;
c906108c 12517
aaa75496 12518 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 12519 {
aaa75496 12520 /* Start a subfile for the current file of the state machine. */
debd256d
JB
12521 /* lh->include_dirs and lh->file_names are 0-based, but the
12522 directory and file name numbers in the statement program
12523 are 1-based. */
12524 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 12525 char *dir = NULL;
a738430d 12526
debd256d
JB
12527 if (fe->dir_index)
12528 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
12529
12530 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
12531 }
12532
a738430d 12533 /* Decode the table. */
c5aa993b 12534 while (!end_sequence)
c906108c
SS
12535 {
12536 op_code = read_1_byte (abfd, line_ptr);
12537 line_ptr += 1;
59205f5a
JB
12538 if (line_ptr > line_end)
12539 {
12540 dwarf2_debug_line_missing_end_sequence_complaint ();
12541 break;
12542 }
9aa1fe7e 12543
debd256d 12544 if (op_code >= lh->opcode_base)
6e70227d 12545 {
a738430d 12546 /* Special operand. */
debd256d 12547 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
12548 address += (((op_index + (adj_opcode / lh->line_range))
12549 / lh->maximum_ops_per_instruction)
12550 * lh->minimum_instruction_length);
12551 op_index = ((op_index + (adj_opcode / lh->line_range))
12552 % lh->maximum_ops_per_instruction);
debd256d 12553 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 12554 if (lh->num_file_names < file || file == 0)
25e43795 12555 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
12556 /* For now we ignore lines not starting on an
12557 instruction boundary. */
12558 else if (op_index == 0)
25e43795
DJ
12559 {
12560 lh->file_names[file - 1].included_p = 1;
ca5f395d 12561 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
12562 {
12563 if (last_subfile != current_subfile)
12564 {
12565 addr = gdbarch_addr_bits_remove (gdbarch, address);
12566 if (last_subfile)
c91513d8 12567 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
12568 last_subfile = current_subfile;
12569 }
25e43795 12570 /* Append row to matrix using current values. */
7019d805 12571 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 12572 (*p_record_line) (current_subfile, line, addr);
366da635 12573 }
25e43795 12574 }
ca5f395d 12575 basic_block = 0;
9aa1fe7e
GK
12576 }
12577 else switch (op_code)
c906108c
SS
12578 {
12579 case DW_LNS_extended_op:
3e43a32a
MS
12580 extended_len = read_unsigned_leb128 (abfd, line_ptr,
12581 &bytes_read);
473b7be6 12582 line_ptr += bytes_read;
a8c50c1f 12583 extended_end = line_ptr + extended_len;
c906108c
SS
12584 extended_op = read_1_byte (abfd, line_ptr);
12585 line_ptr += 1;
12586 switch (extended_op)
12587 {
12588 case DW_LNE_end_sequence:
c91513d8 12589 p_record_line = record_line;
c906108c 12590 end_sequence = 1;
c906108c
SS
12591 break;
12592 case DW_LNE_set_address:
e7c27a73 12593 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
12594
12595 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
12596 {
12597 /* This line table is for a function which has been
12598 GCd by the linker. Ignore it. PR gdb/12528 */
12599
12600 long line_offset
12601 = line_ptr - dwarf2_per_objfile->line.buffer;
12602
12603 complaint (&symfile_complaints,
12604 _(".debug_line address at offset 0x%lx is 0 "
12605 "[in module %s]"),
bb5ed363 12606 line_offset, objfile->name);
c91513d8
PP
12607 p_record_line = noop_record_line;
12608 }
12609
2dc7f7b3 12610 op_index = 0;
107d2387
AC
12611 line_ptr += bytes_read;
12612 address += baseaddr;
c906108c
SS
12613 break;
12614 case DW_LNE_define_file:
debd256d
JB
12615 {
12616 char *cur_file;
12617 unsigned int dir_index, mod_time, length;
6e70227d 12618
3e43a32a
MS
12619 cur_file = read_direct_string (abfd, line_ptr,
12620 &bytes_read);
debd256d
JB
12621 line_ptr += bytes_read;
12622 dir_index =
12623 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12624 line_ptr += bytes_read;
12625 mod_time =
12626 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12627 line_ptr += bytes_read;
12628 length =
12629 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12630 line_ptr += bytes_read;
12631 add_file_name (lh, cur_file, dir_index, mod_time, length);
12632 }
c906108c 12633 break;
d0c6ba3d
CC
12634 case DW_LNE_set_discriminator:
12635 /* The discriminator is not interesting to the debugger;
12636 just ignore it. */
12637 line_ptr = extended_end;
12638 break;
c906108c 12639 default:
4d3c2250 12640 complaint (&symfile_complaints,
e2e0b3e5 12641 _("mangled .debug_line section"));
debd256d 12642 return;
c906108c 12643 }
a8c50c1f
DJ
12644 /* Make sure that we parsed the extended op correctly. If e.g.
12645 we expected a different address size than the producer used,
12646 we may have read the wrong number of bytes. */
12647 if (line_ptr != extended_end)
12648 {
12649 complaint (&symfile_complaints,
12650 _("mangled .debug_line section"));
12651 return;
12652 }
c906108c
SS
12653 break;
12654 case DW_LNS_copy:
59205f5a 12655 if (lh->num_file_names < file || file == 0)
25e43795
DJ
12656 dwarf2_debug_line_missing_file_complaint ();
12657 else
366da635 12658 {
25e43795 12659 lh->file_names[file - 1].included_p = 1;
ca5f395d 12660 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
12661 {
12662 if (last_subfile != current_subfile)
12663 {
12664 addr = gdbarch_addr_bits_remove (gdbarch, address);
12665 if (last_subfile)
c91513d8 12666 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
12667 last_subfile = current_subfile;
12668 }
7019d805 12669 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 12670 (*p_record_line) (current_subfile, line, addr);
fbf65064 12671 }
366da635 12672 }
c906108c
SS
12673 basic_block = 0;
12674 break;
12675 case DW_LNS_advance_pc:
2dc7f7b3
TT
12676 {
12677 CORE_ADDR adjust
12678 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12679
12680 address += (((op_index + adjust)
12681 / lh->maximum_ops_per_instruction)
12682 * lh->minimum_instruction_length);
12683 op_index = ((op_index + adjust)
12684 % lh->maximum_ops_per_instruction);
12685 line_ptr += bytes_read;
12686 }
c906108c
SS
12687 break;
12688 case DW_LNS_advance_line:
12689 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
12690 line_ptr += bytes_read;
12691 break;
12692 case DW_LNS_set_file:
debd256d 12693 {
a738430d
MK
12694 /* The arrays lh->include_dirs and lh->file_names are
12695 0-based, but the directory and file name numbers in
12696 the statement program are 1-based. */
debd256d 12697 struct file_entry *fe;
4f1520fb 12698 char *dir = NULL;
a738430d 12699
debd256d
JB
12700 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12701 line_ptr += bytes_read;
59205f5a 12702 if (lh->num_file_names < file || file == 0)
25e43795
DJ
12703 dwarf2_debug_line_missing_file_complaint ();
12704 else
12705 {
12706 fe = &lh->file_names[file - 1];
12707 if (fe->dir_index)
12708 dir = lh->include_dirs[fe->dir_index - 1];
12709 if (!decode_for_pst_p)
12710 {
12711 last_subfile = current_subfile;
12712 dwarf2_start_subfile (fe->name, dir, comp_dir);
12713 }
12714 }
debd256d 12715 }
c906108c
SS
12716 break;
12717 case DW_LNS_set_column:
12718 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12719 line_ptr += bytes_read;
12720 break;
12721 case DW_LNS_negate_stmt:
12722 is_stmt = (!is_stmt);
12723 break;
12724 case DW_LNS_set_basic_block:
12725 basic_block = 1;
12726 break;
c2c6d25f
JM
12727 /* Add to the address register of the state machine the
12728 address increment value corresponding to special opcode
a738430d
MK
12729 255. I.e., this value is scaled by the minimum
12730 instruction length since special opcode 255 would have
b021a221 12731 scaled the increment. */
c906108c 12732 case DW_LNS_const_add_pc:
2dc7f7b3
TT
12733 {
12734 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
12735
12736 address += (((op_index + adjust)
12737 / lh->maximum_ops_per_instruction)
12738 * lh->minimum_instruction_length);
12739 op_index = ((op_index + adjust)
12740 % lh->maximum_ops_per_instruction);
12741 }
c906108c
SS
12742 break;
12743 case DW_LNS_fixed_advance_pc:
12744 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 12745 op_index = 0;
c906108c
SS
12746 line_ptr += 2;
12747 break;
9aa1fe7e 12748 default:
a738430d
MK
12749 {
12750 /* Unknown standard opcode, ignore it. */
9aa1fe7e 12751 int i;
a738430d 12752
debd256d 12753 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
12754 {
12755 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12756 line_ptr += bytes_read;
12757 }
12758 }
c906108c
SS
12759 }
12760 }
59205f5a
JB
12761 if (lh->num_file_names < file || file == 0)
12762 dwarf2_debug_line_missing_file_complaint ();
12763 else
12764 {
12765 lh->file_names[file - 1].included_p = 1;
12766 if (!decode_for_pst_p)
fbf65064
UW
12767 {
12768 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 12769 (*p_record_line) (current_subfile, 0, addr);
fbf65064 12770 }
59205f5a 12771 }
c906108c 12772 }
f3f5162e
DE
12773}
12774
12775/* Decode the Line Number Program (LNP) for the given line_header
12776 structure and CU. The actual information extracted and the type
12777 of structures created from the LNP depends on the value of PST.
12778
12779 1. If PST is NULL, then this procedure uses the data from the program
12780 to create all necessary symbol tables, and their linetables.
12781
12782 2. If PST is not NULL, this procedure reads the program to determine
12783 the list of files included by the unit represented by PST, and
12784 builds all the associated partial symbol tables.
12785
12786 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12787 It is used for relative paths in the line table.
12788 NOTE: When processing partial symtabs (pst != NULL),
12789 comp_dir == pst->dirname.
12790
12791 NOTE: It is important that psymtabs have the same file name (via strcmp)
12792 as the corresponding symtab. Since COMP_DIR is not used in the name of the
12793 symtab we don't use it in the name of the psymtabs we create.
12794 E.g. expand_line_sal requires this when finding psymtabs to expand.
12795 A good testcase for this is mb-inline.exp. */
12796
12797static void
12798dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
12799 struct dwarf2_cu *cu, struct partial_symtab *pst,
12800 int want_line_info)
12801{
12802 struct objfile *objfile = cu->objfile;
12803 const int decode_for_pst_p = (pst != NULL);
12804 struct subfile *first_subfile = current_subfile;
12805
12806 if (want_line_info)
12807 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
12808
12809 if (decode_for_pst_p)
12810 {
12811 int file_index;
12812
12813 /* Now that we're done scanning the Line Header Program, we can
12814 create the psymtab of each included file. */
12815 for (file_index = 0; file_index < lh->num_file_names; file_index++)
12816 if (lh->file_names[file_index].included_p == 1)
12817 {
c6da4cef
DE
12818 char *include_name =
12819 psymtab_include_file_name (lh, file_index, pst, comp_dir);
12820 if (include_name != NULL)
aaa75496
JB
12821 dwarf2_create_include_psymtab (include_name, pst, objfile);
12822 }
12823 }
cb1df416
DJ
12824 else
12825 {
12826 /* Make sure a symtab is created for every file, even files
12827 which contain only variables (i.e. no code with associated
12828 line numbers). */
cb1df416 12829 int i;
cb1df416
DJ
12830
12831 for (i = 0; i < lh->num_file_names; i++)
12832 {
12833 char *dir = NULL;
f3f5162e 12834 struct file_entry *fe;
9a619af0 12835
cb1df416
DJ
12836 fe = &lh->file_names[i];
12837 if (fe->dir_index)
12838 dir = lh->include_dirs[fe->dir_index - 1];
12839 dwarf2_start_subfile (fe->name, dir, comp_dir);
12840
12841 /* Skip the main file; we don't need it, and it must be
12842 allocated last, so that it will show up before the
12843 non-primary symtabs in the objfile's symtab list. */
12844 if (current_subfile == first_subfile)
12845 continue;
12846
12847 if (current_subfile->symtab == NULL)
12848 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 12849 objfile);
cb1df416
DJ
12850 fe->symtab = current_subfile->symtab;
12851 }
12852 }
c906108c
SS
12853}
12854
12855/* Start a subfile for DWARF. FILENAME is the name of the file and
12856 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
12857 or NULL if not known. COMP_DIR is the compilation directory for the
12858 linetable's compilation unit or NULL if not known.
c906108c
SS
12859 This routine tries to keep line numbers from identical absolute and
12860 relative file names in a common subfile.
12861
12862 Using the `list' example from the GDB testsuite, which resides in
12863 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
12864 of /srcdir/list0.c yields the following debugging information for list0.c:
12865
c5aa993b
JM
12866 DW_AT_name: /srcdir/list0.c
12867 DW_AT_comp_dir: /compdir
357e46e7 12868 files.files[0].name: list0.h
c5aa993b 12869 files.files[0].dir: /srcdir
357e46e7 12870 files.files[1].name: list0.c
c5aa993b 12871 files.files[1].dir: /srcdir
c906108c
SS
12872
12873 The line number information for list0.c has to end up in a single
4f1520fb
FR
12874 subfile, so that `break /srcdir/list0.c:1' works as expected.
12875 start_subfile will ensure that this happens provided that we pass the
12876 concatenation of files.files[1].dir and files.files[1].name as the
12877 subfile's name. */
c906108c
SS
12878
12879static void
3e43a32a
MS
12880dwarf2_start_subfile (char *filename, const char *dirname,
12881 const char *comp_dir)
c906108c 12882{
4f1520fb
FR
12883 char *fullname;
12884
12885 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
12886 `start_symtab' will always pass the contents of DW_AT_comp_dir as
12887 second argument to start_subfile. To be consistent, we do the
12888 same here. In order not to lose the line information directory,
12889 we concatenate it to the filename when it makes sense.
12890 Note that the Dwarf3 standard says (speaking of filenames in line
12891 information): ``The directory index is ignored for file names
12892 that represent full path names''. Thus ignoring dirname in the
12893 `else' branch below isn't an issue. */
c906108c 12894
d5166ae1 12895 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
12896 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
12897 else
12898 fullname = filename;
c906108c 12899
4f1520fb
FR
12900 start_subfile (fullname, comp_dir);
12901
12902 if (fullname != filename)
12903 xfree (fullname);
c906108c
SS
12904}
12905
4c2df51b
DJ
12906static void
12907var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 12908 struct dwarf2_cu *cu)
4c2df51b 12909{
e7c27a73
DJ
12910 struct objfile *objfile = cu->objfile;
12911 struct comp_unit_head *cu_header = &cu->header;
12912
4c2df51b
DJ
12913 /* NOTE drow/2003-01-30: There used to be a comment and some special
12914 code here to turn a symbol with DW_AT_external and a
12915 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
12916 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
12917 with some versions of binutils) where shared libraries could have
12918 relocations against symbols in their debug information - the
12919 minimal symbol would have the right address, but the debug info
12920 would not. It's no longer necessary, because we will explicitly
12921 apply relocations when we read in the debug information now. */
12922
12923 /* A DW_AT_location attribute with no contents indicates that a
12924 variable has been optimized away. */
12925 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
12926 {
12927 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
12928 return;
12929 }
12930
12931 /* Handle one degenerate form of location expression specially, to
12932 preserve GDB's previous behavior when section offsets are
3019eac3
DE
12933 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
12934 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
12935
12936 if (attr_form_is_block (attr)
3019eac3
DE
12937 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
12938 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
12939 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
12940 && (DW_BLOCK (attr)->size
12941 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 12942 {
891d2f0b 12943 unsigned int dummy;
4c2df51b 12944
3019eac3
DE
12945 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
12946 SYMBOL_VALUE_ADDRESS (sym) =
12947 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
12948 else
12949 SYMBOL_VALUE_ADDRESS (sym) =
12950 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 12951 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
12952 fixup_symbol_section (sym, objfile);
12953 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
12954 SYMBOL_SECTION (sym));
4c2df51b
DJ
12955 return;
12956 }
12957
12958 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
12959 expression evaluator, and use LOC_COMPUTED only when necessary
12960 (i.e. when the value of a register or memory location is
12961 referenced, or a thread-local block, etc.). Then again, it might
12962 not be worthwhile. I'm assuming that it isn't unless performance
12963 or memory numbers show me otherwise. */
12964
e7c27a73 12965 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 12966 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
12967
12968 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
12969 cu->has_loclist = 1;
4c2df51b
DJ
12970}
12971
c906108c
SS
12972/* Given a pointer to a DWARF information entry, figure out if we need
12973 to make a symbol table entry for it, and if so, create a new entry
12974 and return a pointer to it.
12975 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
12976 used the passed type.
12977 If SPACE is not NULL, use it to hold the new symbol. If it is
12978 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
12979
12980static struct symbol *
34eaf542
TT
12981new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
12982 struct symbol *space)
c906108c 12983{
e7c27a73 12984 struct objfile *objfile = cu->objfile;
c906108c
SS
12985 struct symbol *sym = NULL;
12986 char *name;
12987 struct attribute *attr = NULL;
12988 struct attribute *attr2 = NULL;
e142c38c 12989 CORE_ADDR baseaddr;
e37fd15a
SW
12990 struct pending **list_to_add = NULL;
12991
edb3359d 12992 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
12993
12994 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 12995
94af9270 12996 name = dwarf2_name (die, cu);
c906108c
SS
12997 if (name)
12998 {
94af9270 12999 const char *linkagename;
34eaf542 13000 int suppress_add = 0;
94af9270 13001
34eaf542
TT
13002 if (space)
13003 sym = space;
13004 else
13005 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 13006 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
13007
13008 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 13009 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
13010 linkagename = dwarf2_physname (name, die, cu);
13011 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 13012
f55ee35c
JK
13013 /* Fortran does not have mangling standard and the mangling does differ
13014 between gfortran, iFort etc. */
13015 if (cu->language == language_fortran
b250c185 13016 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
13017 symbol_set_demangled_name (&(sym->ginfo),
13018 (char *) dwarf2_full_name (name, die, cu),
13019 NULL);
f55ee35c 13020
c906108c 13021 /* Default assumptions.
c5aa993b 13022 Use the passed type or decode it from the die. */
176620f1 13023 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 13024 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
13025 if (type != NULL)
13026 SYMBOL_TYPE (sym) = type;
13027 else
e7c27a73 13028 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
13029 attr = dwarf2_attr (die,
13030 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13031 cu);
c906108c
SS
13032 if (attr)
13033 {
13034 SYMBOL_LINE (sym) = DW_UNSND (attr);
13035 }
cb1df416 13036
edb3359d
DJ
13037 attr = dwarf2_attr (die,
13038 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13039 cu);
cb1df416
DJ
13040 if (attr)
13041 {
13042 int file_index = DW_UNSND (attr);
9a619af0 13043
cb1df416
DJ
13044 if (cu->line_header == NULL
13045 || file_index > cu->line_header->num_file_names)
13046 complaint (&symfile_complaints,
13047 _("file index out of range"));
1c3d648d 13048 else if (file_index > 0)
cb1df416
DJ
13049 {
13050 struct file_entry *fe;
9a619af0 13051
cb1df416
DJ
13052 fe = &cu->line_header->file_names[file_index - 1];
13053 SYMBOL_SYMTAB (sym) = fe->symtab;
13054 }
13055 }
13056
c906108c
SS
13057 switch (die->tag)
13058 {
13059 case DW_TAG_label:
e142c38c 13060 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
13061 if (attr)
13062 {
13063 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13064 }
0f5238ed
TT
13065 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13066 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 13067 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 13068 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
13069 break;
13070 case DW_TAG_subprogram:
13071 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13072 finish_block. */
13073 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 13074 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
13075 if ((attr2 && (DW_UNSND (attr2) != 0))
13076 || cu->language == language_ada)
c906108c 13077 {
2cfa0c8d
JB
13078 /* Subprograms marked external are stored as a global symbol.
13079 Ada subprograms, whether marked external or not, are always
13080 stored as a global symbol, because we want to be able to
13081 access them globally. For instance, we want to be able
13082 to break on a nested subprogram without having to
13083 specify the context. */
e37fd15a 13084 list_to_add = &global_symbols;
c906108c
SS
13085 }
13086 else
13087 {
e37fd15a 13088 list_to_add = cu->list_in_scope;
c906108c
SS
13089 }
13090 break;
edb3359d
DJ
13091 case DW_TAG_inlined_subroutine:
13092 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13093 finish_block. */
13094 SYMBOL_CLASS (sym) = LOC_BLOCK;
13095 SYMBOL_INLINED (sym) = 1;
481860b3 13096 list_to_add = cu->list_in_scope;
edb3359d 13097 break;
34eaf542
TT
13098 case DW_TAG_template_value_param:
13099 suppress_add = 1;
13100 /* Fall through. */
72929c62 13101 case DW_TAG_constant:
c906108c 13102 case DW_TAG_variable:
254e6b9e 13103 case DW_TAG_member:
0963b4bd
MS
13104 /* Compilation with minimal debug info may result in
13105 variables with missing type entries. Change the
13106 misleading `void' type to something sensible. */
c906108c 13107 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 13108 SYMBOL_TYPE (sym)
46bf5051 13109 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 13110
e142c38c 13111 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
13112 /* In the case of DW_TAG_member, we should only be called for
13113 static const members. */
13114 if (die->tag == DW_TAG_member)
13115 {
3863f96c
DE
13116 /* dwarf2_add_field uses die_is_declaration,
13117 so we do the same. */
254e6b9e
DE
13118 gdb_assert (die_is_declaration (die, cu));
13119 gdb_assert (attr);
13120 }
c906108c
SS
13121 if (attr)
13122 {
e7c27a73 13123 dwarf2_const_value (attr, sym, cu);
e142c38c 13124 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 13125 if (!suppress_add)
34eaf542
TT
13126 {
13127 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 13128 list_to_add = &global_symbols;
34eaf542 13129 else
e37fd15a 13130 list_to_add = cu->list_in_scope;
34eaf542 13131 }
c906108c
SS
13132 break;
13133 }
e142c38c 13134 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13135 if (attr)
13136 {
e7c27a73 13137 var_decode_location (attr, sym, cu);
e142c38c 13138 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
13139 if (SYMBOL_CLASS (sym) == LOC_STATIC
13140 && SYMBOL_VALUE_ADDRESS (sym) == 0
13141 && !dwarf2_per_objfile->has_section_at_zero)
13142 {
13143 /* When a static variable is eliminated by the linker,
13144 the corresponding debug information is not stripped
13145 out, but the variable address is set to null;
13146 do not add such variables into symbol table. */
13147 }
13148 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 13149 {
f55ee35c
JK
13150 /* Workaround gfortran PR debug/40040 - it uses
13151 DW_AT_location for variables in -fPIC libraries which may
13152 get overriden by other libraries/executable and get
13153 a different address. Resolve it by the minimal symbol
13154 which may come from inferior's executable using copy
13155 relocation. Make this workaround only for gfortran as for
13156 other compilers GDB cannot guess the minimal symbol
13157 Fortran mangling kind. */
13158 if (cu->language == language_fortran && die->parent
13159 && die->parent->tag == DW_TAG_module
13160 && cu->producer
13161 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13162 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13163
1c809c68
TT
13164 /* A variable with DW_AT_external is never static,
13165 but it may be block-scoped. */
13166 list_to_add = (cu->list_in_scope == &file_symbols
13167 ? &global_symbols : cu->list_in_scope);
1c809c68 13168 }
c906108c 13169 else
e37fd15a 13170 list_to_add = cu->list_in_scope;
c906108c
SS
13171 }
13172 else
13173 {
13174 /* We do not know the address of this symbol.
c5aa993b
JM
13175 If it is an external symbol and we have type information
13176 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13177 The address of the variable will then be determined from
13178 the minimal symbol table whenever the variable is
13179 referenced. */
e142c38c 13180 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 13181 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 13182 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 13183 {
0fe7935b
DJ
13184 /* A variable with DW_AT_external is never static, but it
13185 may be block-scoped. */
13186 list_to_add = (cu->list_in_scope == &file_symbols
13187 ? &global_symbols : cu->list_in_scope);
13188
c906108c 13189 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 13190 }
442ddf59
JK
13191 else if (!die_is_declaration (die, cu))
13192 {
13193 /* Use the default LOC_OPTIMIZED_OUT class. */
13194 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
13195 if (!suppress_add)
13196 list_to_add = cu->list_in_scope;
442ddf59 13197 }
c906108c
SS
13198 }
13199 break;
13200 case DW_TAG_formal_parameter:
edb3359d
DJ
13201 /* If we are inside a function, mark this as an argument. If
13202 not, we might be looking at an argument to an inlined function
13203 when we do not have enough information to show inlined frames;
13204 pretend it's a local variable in that case so that the user can
13205 still see it. */
13206 if (context_stack_depth > 0
13207 && context_stack[context_stack_depth - 1].name != NULL)
13208 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 13209 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13210 if (attr)
13211 {
e7c27a73 13212 var_decode_location (attr, sym, cu);
c906108c 13213 }
e142c38c 13214 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13215 if (attr)
13216 {
e7c27a73 13217 dwarf2_const_value (attr, sym, cu);
c906108c 13218 }
f346a30d 13219
e37fd15a 13220 list_to_add = cu->list_in_scope;
c906108c
SS
13221 break;
13222 case DW_TAG_unspecified_parameters:
13223 /* From varargs functions; gdb doesn't seem to have any
13224 interest in this information, so just ignore it for now.
13225 (FIXME?) */
13226 break;
34eaf542
TT
13227 case DW_TAG_template_type_param:
13228 suppress_add = 1;
13229 /* Fall through. */
c906108c 13230 case DW_TAG_class_type:
680b30c7 13231 case DW_TAG_interface_type:
c906108c
SS
13232 case DW_TAG_structure_type:
13233 case DW_TAG_union_type:
72019c9c 13234 case DW_TAG_set_type:
c906108c
SS
13235 case DW_TAG_enumeration_type:
13236 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13237 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 13238
63d06c5c 13239 {
987504bb 13240 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
13241 really ever be static objects: otherwise, if you try
13242 to, say, break of a class's method and you're in a file
13243 which doesn't mention that class, it won't work unless
13244 the check for all static symbols in lookup_symbol_aux
13245 saves you. See the OtherFileClass tests in
13246 gdb.c++/namespace.exp. */
13247
e37fd15a 13248 if (!suppress_add)
34eaf542 13249 {
34eaf542
TT
13250 list_to_add = (cu->list_in_scope == &file_symbols
13251 && (cu->language == language_cplus
13252 || cu->language == language_java)
13253 ? &global_symbols : cu->list_in_scope);
63d06c5c 13254
64382290
TT
13255 /* The semantics of C++ state that "struct foo {
13256 ... }" also defines a typedef for "foo". A Java
13257 class declaration also defines a typedef for the
13258 class. */
13259 if (cu->language == language_cplus
13260 || cu->language == language_java
13261 || cu->language == language_ada)
13262 {
13263 /* The symbol's name is already allocated along
13264 with this objfile, so we don't need to
13265 duplicate it for the type. */
13266 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13267 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13268 }
63d06c5c
DC
13269 }
13270 }
c906108c
SS
13271 break;
13272 case DW_TAG_typedef:
63d06c5c
DC
13273 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13274 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13275 list_to_add = cu->list_in_scope;
63d06c5c 13276 break;
c906108c 13277 case DW_TAG_base_type:
a02abb62 13278 case DW_TAG_subrange_type:
c906108c 13279 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13280 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13281 list_to_add = cu->list_in_scope;
c906108c
SS
13282 break;
13283 case DW_TAG_enumerator:
e142c38c 13284 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13285 if (attr)
13286 {
e7c27a73 13287 dwarf2_const_value (attr, sym, cu);
c906108c 13288 }
63d06c5c
DC
13289 {
13290 /* NOTE: carlton/2003-11-10: See comment above in the
13291 DW_TAG_class_type, etc. block. */
13292
e142c38c 13293 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
13294 && (cu->language == language_cplus
13295 || cu->language == language_java)
e142c38c 13296 ? &global_symbols : cu->list_in_scope);
63d06c5c 13297 }
c906108c 13298 break;
5c4e30ca
DC
13299 case DW_TAG_namespace:
13300 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 13301 list_to_add = &global_symbols;
5c4e30ca 13302 break;
c906108c
SS
13303 default:
13304 /* Not a tag we recognize. Hopefully we aren't processing
13305 trash data, but since we must specifically ignore things
13306 we don't recognize, there is nothing else we should do at
0963b4bd 13307 this point. */
e2e0b3e5 13308 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 13309 dwarf_tag_name (die->tag));
c906108c
SS
13310 break;
13311 }
df8a16a1 13312
e37fd15a
SW
13313 if (suppress_add)
13314 {
13315 sym->hash_next = objfile->template_symbols;
13316 objfile->template_symbols = sym;
13317 list_to_add = NULL;
13318 }
13319
13320 if (list_to_add != NULL)
13321 add_symbol_to_list (sym, list_to_add);
13322
df8a16a1
DJ
13323 /* For the benefit of old versions of GCC, check for anonymous
13324 namespaces based on the demangled name. */
13325 if (!processing_has_namespace_info
94af9270 13326 && cu->language == language_cplus)
a10964d1 13327 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
13328 }
13329 return (sym);
13330}
13331
34eaf542
TT
13332/* A wrapper for new_symbol_full that always allocates a new symbol. */
13333
13334static struct symbol *
13335new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13336{
13337 return new_symbol_full (die, type, cu, NULL);
13338}
13339
98bfdba5
PA
13340/* Given an attr with a DW_FORM_dataN value in host byte order,
13341 zero-extend it as appropriate for the symbol's type. The DWARF
13342 standard (v4) is not entirely clear about the meaning of using
13343 DW_FORM_dataN for a constant with a signed type, where the type is
13344 wider than the data. The conclusion of a discussion on the DWARF
13345 list was that this is unspecified. We choose to always zero-extend
13346 because that is the interpretation long in use by GCC. */
c906108c 13347
98bfdba5
PA
13348static gdb_byte *
13349dwarf2_const_value_data (struct attribute *attr, struct type *type,
13350 const char *name, struct obstack *obstack,
12df843f 13351 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 13352{
e7c27a73 13353 struct objfile *objfile = cu->objfile;
e17a4113
UW
13354 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13355 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
13356 LONGEST l = DW_UNSND (attr);
13357
13358 if (bits < sizeof (*value) * 8)
13359 {
13360 l &= ((LONGEST) 1 << bits) - 1;
13361 *value = l;
13362 }
13363 else if (bits == sizeof (*value) * 8)
13364 *value = l;
13365 else
13366 {
13367 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13368 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13369 return bytes;
13370 }
13371
13372 return NULL;
13373}
13374
13375/* Read a constant value from an attribute. Either set *VALUE, or if
13376 the value does not fit in *VALUE, set *BYTES - either already
13377 allocated on the objfile obstack, or newly allocated on OBSTACK,
13378 or, set *BATON, if we translated the constant to a location
13379 expression. */
13380
13381static void
13382dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13383 const char *name, struct obstack *obstack,
13384 struct dwarf2_cu *cu,
12df843f 13385 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
13386 struct dwarf2_locexpr_baton **baton)
13387{
13388 struct objfile *objfile = cu->objfile;
13389 struct comp_unit_head *cu_header = &cu->header;
c906108c 13390 struct dwarf_block *blk;
98bfdba5
PA
13391 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13392 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13393
13394 *value = 0;
13395 *bytes = NULL;
13396 *baton = NULL;
c906108c
SS
13397
13398 switch (attr->form)
13399 {
13400 case DW_FORM_addr:
3019eac3 13401 case DW_FORM_GNU_addr_index:
ac56253d 13402 {
ac56253d
TT
13403 gdb_byte *data;
13404
98bfdba5
PA
13405 if (TYPE_LENGTH (type) != cu_header->addr_size)
13406 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 13407 cu_header->addr_size,
98bfdba5 13408 TYPE_LENGTH (type));
ac56253d
TT
13409 /* Symbols of this form are reasonably rare, so we just
13410 piggyback on the existing location code rather than writing
13411 a new implementation of symbol_computed_ops. */
98bfdba5
PA
13412 *baton = obstack_alloc (&objfile->objfile_obstack,
13413 sizeof (struct dwarf2_locexpr_baton));
13414 (*baton)->per_cu = cu->per_cu;
13415 gdb_assert ((*baton)->per_cu);
ac56253d 13416
98bfdba5
PA
13417 (*baton)->size = 2 + cu_header->addr_size;
13418 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13419 (*baton)->data = data;
ac56253d
TT
13420
13421 data[0] = DW_OP_addr;
13422 store_unsigned_integer (&data[1], cu_header->addr_size,
13423 byte_order, DW_ADDR (attr));
13424 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 13425 }
c906108c 13426 break;
4ac36638 13427 case DW_FORM_string:
93b5768b 13428 case DW_FORM_strp:
3019eac3 13429 case DW_FORM_GNU_str_index:
98bfdba5
PA
13430 /* DW_STRING is already allocated on the objfile obstack, point
13431 directly to it. */
13432 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 13433 break;
c906108c
SS
13434 case DW_FORM_block1:
13435 case DW_FORM_block2:
13436 case DW_FORM_block4:
13437 case DW_FORM_block:
2dc7f7b3 13438 case DW_FORM_exprloc:
c906108c 13439 blk = DW_BLOCK (attr);
98bfdba5
PA
13440 if (TYPE_LENGTH (type) != blk->size)
13441 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13442 TYPE_LENGTH (type));
13443 *bytes = blk->data;
c906108c 13444 break;
2df3850c
JM
13445
13446 /* The DW_AT_const_value attributes are supposed to carry the
13447 symbol's value "represented as it would be on the target
13448 architecture." By the time we get here, it's already been
13449 converted to host endianness, so we just need to sign- or
13450 zero-extend it as appropriate. */
13451 case DW_FORM_data1:
3e43a32a
MS
13452 *bytes = dwarf2_const_value_data (attr, type, name,
13453 obstack, cu, value, 8);
2df3850c 13454 break;
c906108c 13455 case DW_FORM_data2:
3e43a32a
MS
13456 *bytes = dwarf2_const_value_data (attr, type, name,
13457 obstack, cu, value, 16);
2df3850c 13458 break;
c906108c 13459 case DW_FORM_data4:
3e43a32a
MS
13460 *bytes = dwarf2_const_value_data (attr, type, name,
13461 obstack, cu, value, 32);
2df3850c 13462 break;
c906108c 13463 case DW_FORM_data8:
3e43a32a
MS
13464 *bytes = dwarf2_const_value_data (attr, type, name,
13465 obstack, cu, value, 64);
2df3850c
JM
13466 break;
13467
c906108c 13468 case DW_FORM_sdata:
98bfdba5 13469 *value = DW_SND (attr);
2df3850c
JM
13470 break;
13471
c906108c 13472 case DW_FORM_udata:
98bfdba5 13473 *value = DW_UNSND (attr);
c906108c 13474 break;
2df3850c 13475
c906108c 13476 default:
4d3c2250 13477 complaint (&symfile_complaints,
e2e0b3e5 13478 _("unsupported const value attribute form: '%s'"),
4d3c2250 13479 dwarf_form_name (attr->form));
98bfdba5 13480 *value = 0;
c906108c
SS
13481 break;
13482 }
13483}
13484
2df3850c 13485
98bfdba5
PA
13486/* Copy constant value from an attribute to a symbol. */
13487
2df3850c 13488static void
98bfdba5
PA
13489dwarf2_const_value (struct attribute *attr, struct symbol *sym,
13490 struct dwarf2_cu *cu)
2df3850c 13491{
98bfdba5
PA
13492 struct objfile *objfile = cu->objfile;
13493 struct comp_unit_head *cu_header = &cu->header;
12df843f 13494 LONGEST value;
98bfdba5
PA
13495 gdb_byte *bytes;
13496 struct dwarf2_locexpr_baton *baton;
2df3850c 13497
98bfdba5
PA
13498 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
13499 SYMBOL_PRINT_NAME (sym),
13500 &objfile->objfile_obstack, cu,
13501 &value, &bytes, &baton);
2df3850c 13502
98bfdba5
PA
13503 if (baton != NULL)
13504 {
13505 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13506 SYMBOL_LOCATION_BATON (sym) = baton;
13507 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13508 }
13509 else if (bytes != NULL)
13510 {
13511 SYMBOL_VALUE_BYTES (sym) = bytes;
13512 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
13513 }
13514 else
13515 {
13516 SYMBOL_VALUE (sym) = value;
13517 SYMBOL_CLASS (sym) = LOC_CONST;
13518 }
2df3850c
JM
13519}
13520
c906108c
SS
13521/* Return the type of the die in question using its DW_AT_type attribute. */
13522
13523static struct type *
e7c27a73 13524die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13525{
c906108c 13526 struct attribute *type_attr;
c906108c 13527
e142c38c 13528 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
13529 if (!type_attr)
13530 {
13531 /* A missing DW_AT_type represents a void type. */
46bf5051 13532 return objfile_type (cu->objfile)->builtin_void;
c906108c 13533 }
348e048f 13534
673bfd45 13535 return lookup_die_type (die, type_attr, cu);
c906108c
SS
13536}
13537
b4ba55a1
JB
13538/* True iff CU's producer generates GNAT Ada auxiliary information
13539 that allows to find parallel types through that information instead
13540 of having to do expensive parallel lookups by type name. */
13541
13542static int
13543need_gnat_info (struct dwarf2_cu *cu)
13544{
13545 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
13546 of GNAT produces this auxiliary information, without any indication
13547 that it is produced. Part of enhancing the FSF version of GNAT
13548 to produce that information will be to put in place an indicator
13549 that we can use in order to determine whether the descriptive type
13550 info is available or not. One suggestion that has been made is
13551 to use a new attribute, attached to the CU die. For now, assume
13552 that the descriptive type info is not available. */
13553 return 0;
13554}
13555
b4ba55a1
JB
13556/* Return the auxiliary type of the die in question using its
13557 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
13558 attribute is not present. */
13559
13560static struct type *
13561die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
13562{
b4ba55a1 13563 struct attribute *type_attr;
b4ba55a1
JB
13564
13565 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
13566 if (!type_attr)
13567 return NULL;
13568
673bfd45 13569 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
13570}
13571
13572/* If DIE has a descriptive_type attribute, then set the TYPE's
13573 descriptive type accordingly. */
13574
13575static void
13576set_descriptive_type (struct type *type, struct die_info *die,
13577 struct dwarf2_cu *cu)
13578{
13579 struct type *descriptive_type = die_descriptive_type (die, cu);
13580
13581 if (descriptive_type)
13582 {
13583 ALLOCATE_GNAT_AUX_TYPE (type);
13584 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
13585 }
13586}
13587
c906108c
SS
13588/* Return the containing type of the die in question using its
13589 DW_AT_containing_type attribute. */
13590
13591static struct type *
e7c27a73 13592die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13593{
c906108c 13594 struct attribute *type_attr;
c906108c 13595
e142c38c 13596 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
13597 if (!type_attr)
13598 error (_("Dwarf Error: Problem turning containing type into gdb type "
13599 "[in module %s]"), cu->objfile->name);
13600
673bfd45 13601 return lookup_die_type (die, type_attr, cu);
c906108c
SS
13602}
13603
673bfd45
DE
13604/* Look up the type of DIE in CU using its type attribute ATTR.
13605 If there is no type substitute an error marker. */
13606
c906108c 13607static struct type *
673bfd45
DE
13608lookup_die_type (struct die_info *die, struct attribute *attr,
13609 struct dwarf2_cu *cu)
c906108c 13610{
bb5ed363 13611 struct objfile *objfile = cu->objfile;
f792889a
DJ
13612 struct type *this_type;
13613
673bfd45
DE
13614 /* First see if we have it cached. */
13615
13616 if (is_ref_attr (attr))
13617 {
b64f50a1 13618 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
13619
13620 this_type = get_die_type_at_offset (offset, cu->per_cu);
13621 }
55f1336d 13622 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
13623 {
13624 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
13625
13626 /* sig_type will be NULL if the signatured type is missing from
13627 the debug info. */
13628 if (sig_type == NULL)
13629 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13630 "at 0x%x [in module %s]"),
b64f50a1 13631 die->offset.sect_off, objfile->name);
673bfd45 13632
3019eac3
DE
13633 gdb_assert (sig_type->per_cu.is_debug_types);
13634 /* If we haven't filled in type_offset_in_section yet, then we
13635 haven't read the type in yet. */
13636 this_type = NULL;
13637 if (sig_type->type_offset_in_section.sect_off != 0)
13638 {
13639 this_type =
13640 get_die_type_at_offset (sig_type->type_offset_in_section,
13641 &sig_type->per_cu);
13642 }
673bfd45
DE
13643 }
13644 else
13645 {
13646 dump_die_for_error (die);
13647 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 13648 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
13649 }
13650
13651 /* If not cached we need to read it in. */
13652
13653 if (this_type == NULL)
13654 {
13655 struct die_info *type_die;
13656 struct dwarf2_cu *type_cu = cu;
13657
13658 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
13659 /* If we found the type now, it's probably because the type came
13660 from an inter-CU reference and the type's CU got expanded before
13661 ours. */
13662 this_type = get_die_type (type_die, type_cu);
13663 if (this_type == NULL)
13664 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
13665 }
13666
13667 /* If we still don't have a type use an error marker. */
13668
13669 if (this_type == NULL)
c906108c 13670 {
b00fdb78
TT
13671 char *message, *saved;
13672
13673 /* read_type_die already issued a complaint. */
13674 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 13675 objfile->name,
b64f50a1
JK
13676 cu->header.offset.sect_off,
13677 die->offset.sect_off);
bb5ed363 13678 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
13679 message, strlen (message));
13680 xfree (message);
13681
bb5ed363 13682 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 13683 }
673bfd45 13684
f792889a 13685 return this_type;
c906108c
SS
13686}
13687
673bfd45
DE
13688/* Return the type in DIE, CU.
13689 Returns NULL for invalid types.
13690
13691 This first does a lookup in the appropriate type_hash table,
13692 and only reads the die in if necessary.
13693
13694 NOTE: This can be called when reading in partial or full symbols. */
13695
f792889a 13696static struct type *
e7c27a73 13697read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13698{
f792889a
DJ
13699 struct type *this_type;
13700
13701 this_type = get_die_type (die, cu);
13702 if (this_type)
13703 return this_type;
13704
673bfd45
DE
13705 return read_type_die_1 (die, cu);
13706}
13707
13708/* Read the type in DIE, CU.
13709 Returns NULL for invalid types. */
13710
13711static struct type *
13712read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
13713{
13714 struct type *this_type = NULL;
13715
c906108c
SS
13716 switch (die->tag)
13717 {
13718 case DW_TAG_class_type:
680b30c7 13719 case DW_TAG_interface_type:
c906108c
SS
13720 case DW_TAG_structure_type:
13721 case DW_TAG_union_type:
f792889a 13722 this_type = read_structure_type (die, cu);
c906108c
SS
13723 break;
13724 case DW_TAG_enumeration_type:
f792889a 13725 this_type = read_enumeration_type (die, cu);
c906108c
SS
13726 break;
13727 case DW_TAG_subprogram:
13728 case DW_TAG_subroutine_type:
edb3359d 13729 case DW_TAG_inlined_subroutine:
f792889a 13730 this_type = read_subroutine_type (die, cu);
c906108c
SS
13731 break;
13732 case DW_TAG_array_type:
f792889a 13733 this_type = read_array_type (die, cu);
c906108c 13734 break;
72019c9c 13735 case DW_TAG_set_type:
f792889a 13736 this_type = read_set_type (die, cu);
72019c9c 13737 break;
c906108c 13738 case DW_TAG_pointer_type:
f792889a 13739 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
13740 break;
13741 case DW_TAG_ptr_to_member_type:
f792889a 13742 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
13743 break;
13744 case DW_TAG_reference_type:
f792889a 13745 this_type = read_tag_reference_type (die, cu);
c906108c
SS
13746 break;
13747 case DW_TAG_const_type:
f792889a 13748 this_type = read_tag_const_type (die, cu);
c906108c
SS
13749 break;
13750 case DW_TAG_volatile_type:
f792889a 13751 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
13752 break;
13753 case DW_TAG_string_type:
f792889a 13754 this_type = read_tag_string_type (die, cu);
c906108c
SS
13755 break;
13756 case DW_TAG_typedef:
f792889a 13757 this_type = read_typedef (die, cu);
c906108c 13758 break;
a02abb62 13759 case DW_TAG_subrange_type:
f792889a 13760 this_type = read_subrange_type (die, cu);
a02abb62 13761 break;
c906108c 13762 case DW_TAG_base_type:
f792889a 13763 this_type = read_base_type (die, cu);
c906108c 13764 break;
81a17f79 13765 case DW_TAG_unspecified_type:
f792889a 13766 this_type = read_unspecified_type (die, cu);
81a17f79 13767 break;
0114d602
DJ
13768 case DW_TAG_namespace:
13769 this_type = read_namespace_type (die, cu);
13770 break;
f55ee35c
JK
13771 case DW_TAG_module:
13772 this_type = read_module_type (die, cu);
13773 break;
c906108c 13774 default:
3e43a32a
MS
13775 complaint (&symfile_complaints,
13776 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 13777 dwarf_tag_name (die->tag));
c906108c
SS
13778 break;
13779 }
63d06c5c 13780
f792889a 13781 return this_type;
63d06c5c
DC
13782}
13783
abc72ce4
DE
13784/* See if we can figure out if the class lives in a namespace. We do
13785 this by looking for a member function; its demangled name will
13786 contain namespace info, if there is any.
13787 Return the computed name or NULL.
13788 Space for the result is allocated on the objfile's obstack.
13789 This is the full-die version of guess_partial_die_structure_name.
13790 In this case we know DIE has no useful parent. */
13791
13792static char *
13793guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
13794{
13795 struct die_info *spec_die;
13796 struct dwarf2_cu *spec_cu;
13797 struct die_info *child;
13798
13799 spec_cu = cu;
13800 spec_die = die_specification (die, &spec_cu);
13801 if (spec_die != NULL)
13802 {
13803 die = spec_die;
13804 cu = spec_cu;
13805 }
13806
13807 for (child = die->child;
13808 child != NULL;
13809 child = child->sibling)
13810 {
13811 if (child->tag == DW_TAG_subprogram)
13812 {
13813 struct attribute *attr;
13814
13815 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
13816 if (attr == NULL)
13817 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
13818 if (attr != NULL)
13819 {
13820 char *actual_name
13821 = language_class_name_from_physname (cu->language_defn,
13822 DW_STRING (attr));
13823 char *name = NULL;
13824
13825 if (actual_name != NULL)
13826 {
13827 char *die_name = dwarf2_name (die, cu);
13828
13829 if (die_name != NULL
13830 && strcmp (die_name, actual_name) != 0)
13831 {
13832 /* Strip off the class name from the full name.
13833 We want the prefix. */
13834 int die_name_len = strlen (die_name);
13835 int actual_name_len = strlen (actual_name);
13836
13837 /* Test for '::' as a sanity check. */
13838 if (actual_name_len > die_name_len + 2
3e43a32a
MS
13839 && actual_name[actual_name_len
13840 - die_name_len - 1] == ':')
abc72ce4
DE
13841 name =
13842 obsavestring (actual_name,
13843 actual_name_len - die_name_len - 2,
13844 &cu->objfile->objfile_obstack);
13845 }
13846 }
13847 xfree (actual_name);
13848 return name;
13849 }
13850 }
13851 }
13852
13853 return NULL;
13854}
13855
96408a79
SA
13856/* GCC might emit a nameless typedef that has a linkage name. Determine the
13857 prefix part in such case. See
13858 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13859
13860static char *
13861anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
13862{
13863 struct attribute *attr;
13864 char *base;
13865
13866 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
13867 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
13868 return NULL;
13869
13870 attr = dwarf2_attr (die, DW_AT_name, cu);
13871 if (attr != NULL && DW_STRING (attr) != NULL)
13872 return NULL;
13873
13874 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
13875 if (attr == NULL)
13876 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
13877 if (attr == NULL || DW_STRING (attr) == NULL)
13878 return NULL;
13879
13880 /* dwarf2_name had to be already called. */
13881 gdb_assert (DW_STRING_IS_CANONICAL (attr));
13882
13883 /* Strip the base name, keep any leading namespaces/classes. */
13884 base = strrchr (DW_STRING (attr), ':');
13885 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
13886 return "";
13887
13888 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
13889 &cu->objfile->objfile_obstack);
13890}
13891
fdde2d81 13892/* Return the name of the namespace/class that DIE is defined within,
0114d602 13893 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 13894
0114d602
DJ
13895 For example, if we're within the method foo() in the following
13896 code:
13897
13898 namespace N {
13899 class C {
13900 void foo () {
13901 }
13902 };
13903 }
13904
13905 then determine_prefix on foo's die will return "N::C". */
fdde2d81 13906
0d5cff50 13907static const char *
e142c38c 13908determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 13909{
0114d602
DJ
13910 struct die_info *parent, *spec_die;
13911 struct dwarf2_cu *spec_cu;
13912 struct type *parent_type;
96408a79 13913 char *retval;
63d06c5c 13914
f55ee35c
JK
13915 if (cu->language != language_cplus && cu->language != language_java
13916 && cu->language != language_fortran)
0114d602
DJ
13917 return "";
13918
96408a79
SA
13919 retval = anonymous_struct_prefix (die, cu);
13920 if (retval)
13921 return retval;
13922
0114d602
DJ
13923 /* We have to be careful in the presence of DW_AT_specification.
13924 For example, with GCC 3.4, given the code
13925
13926 namespace N {
13927 void foo() {
13928 // Definition of N::foo.
13929 }
13930 }
13931
13932 then we'll have a tree of DIEs like this:
13933
13934 1: DW_TAG_compile_unit
13935 2: DW_TAG_namespace // N
13936 3: DW_TAG_subprogram // declaration of N::foo
13937 4: DW_TAG_subprogram // definition of N::foo
13938 DW_AT_specification // refers to die #3
13939
13940 Thus, when processing die #4, we have to pretend that we're in
13941 the context of its DW_AT_specification, namely the contex of die
13942 #3. */
13943 spec_cu = cu;
13944 spec_die = die_specification (die, &spec_cu);
13945 if (spec_die == NULL)
13946 parent = die->parent;
13947 else
63d06c5c 13948 {
0114d602
DJ
13949 parent = spec_die->parent;
13950 cu = spec_cu;
63d06c5c 13951 }
0114d602
DJ
13952
13953 if (parent == NULL)
13954 return "";
98bfdba5
PA
13955 else if (parent->building_fullname)
13956 {
13957 const char *name;
13958 const char *parent_name;
13959
13960 /* It has been seen on RealView 2.2 built binaries,
13961 DW_TAG_template_type_param types actually _defined_ as
13962 children of the parent class:
13963
13964 enum E {};
13965 template class <class Enum> Class{};
13966 Class<enum E> class_e;
13967
13968 1: DW_TAG_class_type (Class)
13969 2: DW_TAG_enumeration_type (E)
13970 3: DW_TAG_enumerator (enum1:0)
13971 3: DW_TAG_enumerator (enum2:1)
13972 ...
13973 2: DW_TAG_template_type_param
13974 DW_AT_type DW_FORM_ref_udata (E)
13975
13976 Besides being broken debug info, it can put GDB into an
13977 infinite loop. Consider:
13978
13979 When we're building the full name for Class<E>, we'll start
13980 at Class, and go look over its template type parameters,
13981 finding E. We'll then try to build the full name of E, and
13982 reach here. We're now trying to build the full name of E,
13983 and look over the parent DIE for containing scope. In the
13984 broken case, if we followed the parent DIE of E, we'd again
13985 find Class, and once again go look at its template type
13986 arguments, etc., etc. Simply don't consider such parent die
13987 as source-level parent of this die (it can't be, the language
13988 doesn't allow it), and break the loop here. */
13989 name = dwarf2_name (die, cu);
13990 parent_name = dwarf2_name (parent, cu);
13991 complaint (&symfile_complaints,
13992 _("template param type '%s' defined within parent '%s'"),
13993 name ? name : "<unknown>",
13994 parent_name ? parent_name : "<unknown>");
13995 return "";
13996 }
63d06c5c 13997 else
0114d602
DJ
13998 switch (parent->tag)
13999 {
63d06c5c 14000 case DW_TAG_namespace:
0114d602 14001 parent_type = read_type_die (parent, cu);
acebe513
UW
14002 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14003 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14004 Work around this problem here. */
14005 if (cu->language == language_cplus
14006 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14007 return "";
0114d602
DJ
14008 /* We give a name to even anonymous namespaces. */
14009 return TYPE_TAG_NAME (parent_type);
63d06c5c 14010 case DW_TAG_class_type:
680b30c7 14011 case DW_TAG_interface_type:
63d06c5c 14012 case DW_TAG_structure_type:
0114d602 14013 case DW_TAG_union_type:
f55ee35c 14014 case DW_TAG_module:
0114d602
DJ
14015 parent_type = read_type_die (parent, cu);
14016 if (TYPE_TAG_NAME (parent_type) != NULL)
14017 return TYPE_TAG_NAME (parent_type);
14018 else
14019 /* An anonymous structure is only allowed non-static data
14020 members; no typedefs, no member functions, et cetera.
14021 So it does not need a prefix. */
14022 return "";
abc72ce4
DE
14023 case DW_TAG_compile_unit:
14024 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14025 if (cu->language == language_cplus
8b70b953 14026 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14027 && die->child != NULL
14028 && (die->tag == DW_TAG_class_type
14029 || die->tag == DW_TAG_structure_type
14030 || die->tag == DW_TAG_union_type))
14031 {
14032 char *name = guess_full_die_structure_name (die, cu);
14033 if (name != NULL)
14034 return name;
14035 }
14036 return "";
63d06c5c 14037 default:
8176b9b8 14038 return determine_prefix (parent, cu);
63d06c5c 14039 }
63d06c5c
DC
14040}
14041
3e43a32a
MS
14042/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14043 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14044 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14045 an obconcat, otherwise allocate storage for the result. The CU argument is
14046 used to determine the language and hence, the appropriate separator. */
987504bb 14047
f55ee35c 14048#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
14049
14050static char *
f55ee35c
JK
14051typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14052 int physname, struct dwarf2_cu *cu)
63d06c5c 14053{
f55ee35c 14054 const char *lead = "";
5c315b68 14055 const char *sep;
63d06c5c 14056
3e43a32a
MS
14057 if (suffix == NULL || suffix[0] == '\0'
14058 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
14059 sep = "";
14060 else if (cu->language == language_java)
14061 sep = ".";
f55ee35c
JK
14062 else if (cu->language == language_fortran && physname)
14063 {
14064 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14065 DW_AT_MIPS_linkage_name is preferred and used instead. */
14066
14067 lead = "__";
14068 sep = "_MOD_";
14069 }
987504bb
JJ
14070 else
14071 sep = "::";
63d06c5c 14072
6dd47d34
DE
14073 if (prefix == NULL)
14074 prefix = "";
14075 if (suffix == NULL)
14076 suffix = "";
14077
987504bb
JJ
14078 if (obs == NULL)
14079 {
3e43a32a
MS
14080 char *retval
14081 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 14082
f55ee35c
JK
14083 strcpy (retval, lead);
14084 strcat (retval, prefix);
6dd47d34
DE
14085 strcat (retval, sep);
14086 strcat (retval, suffix);
63d06c5c
DC
14087 return retval;
14088 }
987504bb
JJ
14089 else
14090 {
14091 /* We have an obstack. */
f55ee35c 14092 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 14093 }
63d06c5c
DC
14094}
14095
c906108c
SS
14096/* Return sibling of die, NULL if no sibling. */
14097
f9aca02d 14098static struct die_info *
fba45db2 14099sibling_die (struct die_info *die)
c906108c 14100{
639d11d3 14101 return die->sibling;
c906108c
SS
14102}
14103
71c25dea
TT
14104/* Get name of a die, return NULL if not found. */
14105
14106static char *
14107dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14108 struct obstack *obstack)
14109{
14110 if (name && cu->language == language_cplus)
14111 {
14112 char *canon_name = cp_canonicalize_string (name);
14113
14114 if (canon_name != NULL)
14115 {
14116 if (strcmp (canon_name, name) != 0)
14117 name = obsavestring (canon_name, strlen (canon_name),
14118 obstack);
14119 xfree (canon_name);
14120 }
14121 }
14122
14123 return name;
c906108c
SS
14124}
14125
9219021c
DC
14126/* Get name of a die, return NULL if not found. */
14127
14128static char *
e142c38c 14129dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
14130{
14131 struct attribute *attr;
14132
e142c38c 14133 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
14134 if ((!attr || !DW_STRING (attr))
14135 && die->tag != DW_TAG_class_type
14136 && die->tag != DW_TAG_interface_type
14137 && die->tag != DW_TAG_structure_type
14138 && die->tag != DW_TAG_union_type)
71c25dea
TT
14139 return NULL;
14140
14141 switch (die->tag)
14142 {
14143 case DW_TAG_compile_unit:
14144 /* Compilation units have a DW_AT_name that is a filename, not
14145 a source language identifier. */
14146 case DW_TAG_enumeration_type:
14147 case DW_TAG_enumerator:
14148 /* These tags always have simple identifiers already; no need
14149 to canonicalize them. */
14150 return DW_STRING (attr);
907af001 14151
418835cc
KS
14152 case DW_TAG_subprogram:
14153 /* Java constructors will all be named "<init>", so return
14154 the class name when we see this special case. */
14155 if (cu->language == language_java
14156 && DW_STRING (attr) != NULL
14157 && strcmp (DW_STRING (attr), "<init>") == 0)
14158 {
14159 struct dwarf2_cu *spec_cu = cu;
14160 struct die_info *spec_die;
14161
14162 /* GCJ will output '<init>' for Java constructor names.
14163 For this special case, return the name of the parent class. */
14164
14165 /* GCJ may output suprogram DIEs with AT_specification set.
14166 If so, use the name of the specified DIE. */
14167 spec_die = die_specification (die, &spec_cu);
14168 if (spec_die != NULL)
14169 return dwarf2_name (spec_die, spec_cu);
14170
14171 do
14172 {
14173 die = die->parent;
14174 if (die->tag == DW_TAG_class_type)
14175 return dwarf2_name (die, cu);
14176 }
14177 while (die->tag != DW_TAG_compile_unit);
14178 }
907af001
UW
14179 break;
14180
14181 case DW_TAG_class_type:
14182 case DW_TAG_interface_type:
14183 case DW_TAG_structure_type:
14184 case DW_TAG_union_type:
14185 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14186 structures or unions. These were of the form "._%d" in GCC 4.1,
14187 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14188 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
14189 if (attr && DW_STRING (attr)
14190 && (strncmp (DW_STRING (attr), "._", 2) == 0
14191 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 14192 return NULL;
53832f31
TT
14193
14194 /* GCC might emit a nameless typedef that has a linkage name. See
14195 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14196 if (!attr || DW_STRING (attr) == NULL)
14197 {
df5c6c50 14198 char *demangled = NULL;
53832f31
TT
14199
14200 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14201 if (attr == NULL)
14202 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14203
14204 if (attr == NULL || DW_STRING (attr) == NULL)
14205 return NULL;
14206
df5c6c50
JK
14207 /* Avoid demangling DW_STRING (attr) the second time on a second
14208 call for the same DIE. */
14209 if (!DW_STRING_IS_CANONICAL (attr))
14210 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
14211
14212 if (demangled)
14213 {
96408a79
SA
14214 char *base;
14215
53832f31 14216 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
14217 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14218 &cu->objfile->objfile_obstack);
53832f31
TT
14219 DW_STRING_IS_CANONICAL (attr) = 1;
14220 xfree (demangled);
96408a79
SA
14221
14222 /* Strip any leading namespaces/classes, keep only the base name.
14223 DW_AT_name for named DIEs does not contain the prefixes. */
14224 base = strrchr (DW_STRING (attr), ':');
14225 if (base && base > DW_STRING (attr) && base[-1] == ':')
14226 return &base[1];
14227 else
14228 return DW_STRING (attr);
53832f31
TT
14229 }
14230 }
907af001
UW
14231 break;
14232
71c25dea 14233 default:
907af001
UW
14234 break;
14235 }
14236
14237 if (!DW_STRING_IS_CANONICAL (attr))
14238 {
14239 DW_STRING (attr)
14240 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14241 &cu->objfile->objfile_obstack);
14242 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 14243 }
907af001 14244 return DW_STRING (attr);
9219021c
DC
14245}
14246
14247/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
14248 is none. *EXT_CU is the CU containing DIE on input, and the CU
14249 containing the return value on output. */
9219021c
DC
14250
14251static struct die_info *
f2f0e013 14252dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
14253{
14254 struct attribute *attr;
9219021c 14255
f2f0e013 14256 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
14257 if (attr == NULL)
14258 return NULL;
14259
f2f0e013 14260 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
14261}
14262
c906108c
SS
14263/* Convert a DIE tag into its string name. */
14264
f39c6ffd 14265static const char *
aa1ee363 14266dwarf_tag_name (unsigned tag)
c906108c 14267{
f39c6ffd
TT
14268 const char *name = get_DW_TAG_name (tag);
14269
14270 if (name == NULL)
14271 return "DW_TAG_<unknown>";
14272
14273 return name;
c906108c
SS
14274}
14275
14276/* Convert a DWARF attribute code into its string name. */
14277
f39c6ffd 14278static const char *
aa1ee363 14279dwarf_attr_name (unsigned attr)
c906108c 14280{
f39c6ffd
TT
14281 const char *name;
14282
c764a876 14283#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
14284 if (attr == DW_AT_MIPS_fde)
14285 return "DW_AT_MIPS_fde";
14286#else
14287 if (attr == DW_AT_HP_block_index)
14288 return "DW_AT_HP_block_index";
c764a876 14289#endif
f39c6ffd
TT
14290
14291 name = get_DW_AT_name (attr);
14292
14293 if (name == NULL)
14294 return "DW_AT_<unknown>";
14295
14296 return name;
c906108c
SS
14297}
14298
14299/* Convert a DWARF value form code into its string name. */
14300
f39c6ffd 14301static const char *
aa1ee363 14302dwarf_form_name (unsigned form)
c906108c 14303{
f39c6ffd
TT
14304 const char *name = get_DW_FORM_name (form);
14305
14306 if (name == NULL)
14307 return "DW_FORM_<unknown>";
14308
14309 return name;
c906108c
SS
14310}
14311
14312static char *
fba45db2 14313dwarf_bool_name (unsigned mybool)
c906108c
SS
14314{
14315 if (mybool)
14316 return "TRUE";
14317 else
14318 return "FALSE";
14319}
14320
14321/* Convert a DWARF type code into its string name. */
14322
f39c6ffd 14323static const char *
aa1ee363 14324dwarf_type_encoding_name (unsigned enc)
c906108c 14325{
f39c6ffd 14326 const char *name = get_DW_ATE_name (enc);
c906108c 14327
f39c6ffd
TT
14328 if (name == NULL)
14329 return "DW_ATE_<unknown>";
c906108c 14330
f39c6ffd 14331 return name;
c906108c 14332}
c906108c 14333
f9aca02d 14334static void
d97bc12b 14335dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
14336{
14337 unsigned int i;
14338
d97bc12b
DE
14339 print_spaces (indent, f);
14340 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 14341 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
14342
14343 if (die->parent != NULL)
14344 {
14345 print_spaces (indent, f);
14346 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 14347 die->parent->offset.sect_off);
d97bc12b
DE
14348 }
14349
14350 print_spaces (indent, f);
14351 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 14352 dwarf_bool_name (die->child != NULL));
c906108c 14353
d97bc12b
DE
14354 print_spaces (indent, f);
14355 fprintf_unfiltered (f, " attributes:\n");
14356
c906108c
SS
14357 for (i = 0; i < die->num_attrs; ++i)
14358 {
d97bc12b
DE
14359 print_spaces (indent, f);
14360 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
14361 dwarf_attr_name (die->attrs[i].name),
14362 dwarf_form_name (die->attrs[i].form));
d97bc12b 14363
c906108c
SS
14364 switch (die->attrs[i].form)
14365 {
c906108c 14366 case DW_FORM_addr:
3019eac3 14367 case DW_FORM_GNU_addr_index:
d97bc12b 14368 fprintf_unfiltered (f, "address: ");
5af949e3 14369 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
14370 break;
14371 case DW_FORM_block2:
14372 case DW_FORM_block4:
14373 case DW_FORM_block:
14374 case DW_FORM_block1:
3e43a32a
MS
14375 fprintf_unfiltered (f, "block: size %d",
14376 DW_BLOCK (&die->attrs[i])->size);
c906108c 14377 break;
2dc7f7b3
TT
14378 case DW_FORM_exprloc:
14379 fprintf_unfiltered (f, "expression: size %u",
14380 DW_BLOCK (&die->attrs[i])->size);
14381 break;
4568ecf9
DE
14382 case DW_FORM_ref_addr:
14383 fprintf_unfiltered (f, "ref address: ");
14384 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14385 break;
10b3939b
DJ
14386 case DW_FORM_ref1:
14387 case DW_FORM_ref2:
14388 case DW_FORM_ref4:
4568ecf9
DE
14389 case DW_FORM_ref8:
14390 case DW_FORM_ref_udata:
d97bc12b 14391 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 14392 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 14393 break;
c906108c
SS
14394 case DW_FORM_data1:
14395 case DW_FORM_data2:
14396 case DW_FORM_data4:
ce5d95e1 14397 case DW_FORM_data8:
c906108c
SS
14398 case DW_FORM_udata:
14399 case DW_FORM_sdata:
43bbcdc2
PH
14400 fprintf_unfiltered (f, "constant: %s",
14401 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 14402 break;
2dc7f7b3
TT
14403 case DW_FORM_sec_offset:
14404 fprintf_unfiltered (f, "section offset: %s",
14405 pulongest (DW_UNSND (&die->attrs[i])));
14406 break;
55f1336d 14407 case DW_FORM_ref_sig8:
348e048f
DE
14408 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14409 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 14410 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
14411 else
14412 fprintf_unfiltered (f, "signatured type, offset: unknown");
14413 break;
c906108c 14414 case DW_FORM_string:
4bdf3d34 14415 case DW_FORM_strp:
3019eac3 14416 case DW_FORM_GNU_str_index:
8285870a 14417 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 14418 DW_STRING (&die->attrs[i])
8285870a
JK
14419 ? DW_STRING (&die->attrs[i]) : "",
14420 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
14421 break;
14422 case DW_FORM_flag:
14423 if (DW_UNSND (&die->attrs[i]))
d97bc12b 14424 fprintf_unfiltered (f, "flag: TRUE");
c906108c 14425 else
d97bc12b 14426 fprintf_unfiltered (f, "flag: FALSE");
c906108c 14427 break;
2dc7f7b3
TT
14428 case DW_FORM_flag_present:
14429 fprintf_unfiltered (f, "flag: TRUE");
14430 break;
a8329558 14431 case DW_FORM_indirect:
0963b4bd
MS
14432 /* The reader will have reduced the indirect form to
14433 the "base form" so this form should not occur. */
3e43a32a
MS
14434 fprintf_unfiltered (f,
14435 "unexpected attribute form: DW_FORM_indirect");
a8329558 14436 break;
c906108c 14437 default:
d97bc12b 14438 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 14439 die->attrs[i].form);
d97bc12b 14440 break;
c906108c 14441 }
d97bc12b 14442 fprintf_unfiltered (f, "\n");
c906108c
SS
14443 }
14444}
14445
f9aca02d 14446static void
d97bc12b 14447dump_die_for_error (struct die_info *die)
c906108c 14448{
d97bc12b
DE
14449 dump_die_shallow (gdb_stderr, 0, die);
14450}
14451
14452static void
14453dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14454{
14455 int indent = level * 4;
14456
14457 gdb_assert (die != NULL);
14458
14459 if (level >= max_level)
14460 return;
14461
14462 dump_die_shallow (f, indent, die);
14463
14464 if (die->child != NULL)
c906108c 14465 {
d97bc12b
DE
14466 print_spaces (indent, f);
14467 fprintf_unfiltered (f, " Children:");
14468 if (level + 1 < max_level)
14469 {
14470 fprintf_unfiltered (f, "\n");
14471 dump_die_1 (f, level + 1, max_level, die->child);
14472 }
14473 else
14474 {
3e43a32a
MS
14475 fprintf_unfiltered (f,
14476 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14477 }
14478 }
14479
14480 if (die->sibling != NULL && level > 0)
14481 {
14482 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14483 }
14484}
14485
d97bc12b
DE
14486/* This is called from the pdie macro in gdbinit.in.
14487 It's not static so gcc will keep a copy callable from gdb. */
14488
14489void
14490dump_die (struct die_info *die, int max_level)
14491{
14492 dump_die_1 (gdb_stdlog, 0, max_level, die);
14493}
14494
f9aca02d 14495static void
51545339 14496store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14497{
51545339 14498 void **slot;
c906108c 14499
b64f50a1
JK
14500 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14501 INSERT);
51545339
DJ
14502
14503 *slot = die;
c906108c
SS
14504}
14505
b64f50a1
JK
14506/* DW_ADDR is always stored already as sect_offset; despite for the forms
14507 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14508
93311388
DE
14509static int
14510is_ref_attr (struct attribute *attr)
c906108c 14511{
c906108c
SS
14512 switch (attr->form)
14513 {
14514 case DW_FORM_ref_addr:
c906108c
SS
14515 case DW_FORM_ref1:
14516 case DW_FORM_ref2:
14517 case DW_FORM_ref4:
613e1657 14518 case DW_FORM_ref8:
c906108c 14519 case DW_FORM_ref_udata:
93311388 14520 return 1;
c906108c 14521 default:
93311388 14522 return 0;
c906108c 14523 }
93311388
DE
14524}
14525
b64f50a1
JK
14526/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14527 required kind. */
14528
14529static sect_offset
93311388
DE
14530dwarf2_get_ref_die_offset (struct attribute *attr)
14531{
4568ecf9 14532 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 14533
93311388 14534 if (is_ref_attr (attr))
b64f50a1 14535 return retval;
93311388 14536
b64f50a1 14537 retval.sect_off = 0;
93311388
DE
14538 complaint (&symfile_complaints,
14539 _("unsupported die ref attribute form: '%s'"),
14540 dwarf_form_name (attr->form));
b64f50a1 14541 return retval;
c906108c
SS
14542}
14543
43bbcdc2
PH
14544/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14545 * the value held by the attribute is not constant. */
a02abb62 14546
43bbcdc2 14547static LONGEST
a02abb62
JB
14548dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14549{
14550 if (attr->form == DW_FORM_sdata)
14551 return DW_SND (attr);
14552 else if (attr->form == DW_FORM_udata
14553 || attr->form == DW_FORM_data1
14554 || attr->form == DW_FORM_data2
14555 || attr->form == DW_FORM_data4
14556 || attr->form == DW_FORM_data8)
14557 return DW_UNSND (attr);
14558 else
14559 {
3e43a32a
MS
14560 complaint (&symfile_complaints,
14561 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14562 dwarf_form_name (attr->form));
14563 return default_value;
14564 }
14565}
14566
03dd20cc 14567/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14568 unit and add it to our queue.
14569 The result is non-zero if PER_CU was queued, otherwise the result is zero
14570 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14571
348e048f 14572static int
03dd20cc
DJ
14573maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14574 struct dwarf2_per_cu_data *per_cu)
14575{
98bfdba5
PA
14576 /* We may arrive here during partial symbol reading, if we need full
14577 DIEs to process an unusual case (e.g. template arguments). Do
14578 not queue PER_CU, just tell our caller to load its DIEs. */
14579 if (dwarf2_per_objfile->reading_partial_symbols)
14580 {
14581 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14582 return 1;
14583 return 0;
14584 }
14585
03dd20cc
DJ
14586 /* Mark the dependence relation so that we don't flush PER_CU
14587 too early. */
14588 dwarf2_add_dependence (this_cu, per_cu);
14589
14590 /* If it's already on the queue, we have nothing to do. */
14591 if (per_cu->queued)
348e048f 14592 return 0;
03dd20cc
DJ
14593
14594 /* If the compilation unit is already loaded, just mark it as
14595 used. */
14596 if (per_cu->cu != NULL)
14597 {
14598 per_cu->cu->last_used = 0;
348e048f 14599 return 0;
03dd20cc
DJ
14600 }
14601
14602 /* Add it to the queue. */
a0f42c21 14603 queue_comp_unit (per_cu);
348e048f
DE
14604
14605 return 1;
14606}
14607
14608/* Follow reference or signature attribute ATTR of SRC_DIE.
14609 On entry *REF_CU is the CU of SRC_DIE.
14610 On exit *REF_CU is the CU of the result. */
14611
14612static struct die_info *
14613follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14614 struct dwarf2_cu **ref_cu)
14615{
14616 struct die_info *die;
14617
14618 if (is_ref_attr (attr))
14619 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14620 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14621 die = follow_die_sig (src_die, attr, ref_cu);
14622 else
14623 {
14624 dump_die_for_error (src_die);
14625 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14626 (*ref_cu)->objfile->name);
14627 }
14628
14629 return die;
03dd20cc
DJ
14630}
14631
5c631832 14632/* Follow reference OFFSET.
673bfd45
DE
14633 On entry *REF_CU is the CU of the source die referencing OFFSET.
14634 On exit *REF_CU is the CU of the result.
14635 Returns NULL if OFFSET is invalid. */
f504f079 14636
f9aca02d 14637static struct die_info *
b64f50a1 14638follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
c906108c 14639{
10b3939b 14640 struct die_info temp_die;
f2f0e013 14641 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14642
348e048f
DE
14643 gdb_assert (cu->per_cu != NULL);
14644
98bfdba5
PA
14645 target_cu = cu;
14646
3019eac3 14647 if (cu->per_cu->is_debug_types)
348e048f
DE
14648 {
14649 /* .debug_types CUs cannot reference anything outside their CU.
14650 If they need to, they have to reference a signatured type via
55f1336d 14651 DW_FORM_ref_sig8. */
348e048f 14652 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14653 return NULL;
348e048f
DE
14654 }
14655 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14656 {
14657 struct dwarf2_per_cu_data *per_cu;
9a619af0 14658
45452591 14659 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14660
14661 /* If necessary, add it to the queue and load its DIEs. */
348e048f 14662 if (maybe_queue_comp_unit (cu, per_cu))
a0f42c21 14663 load_full_comp_unit (per_cu);
03dd20cc 14664
10b3939b
DJ
14665 target_cu = per_cu->cu;
14666 }
98bfdba5
PA
14667 else if (cu->dies == NULL)
14668 {
14669 /* We're loading full DIEs during partial symbol reading. */
14670 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
a0f42c21 14671 load_full_comp_unit (cu->per_cu);
98bfdba5 14672 }
c906108c 14673
f2f0e013 14674 *ref_cu = target_cu;
51545339 14675 temp_die.offset = offset;
b64f50a1 14676 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 14677}
10b3939b 14678
5c631832
JK
14679/* Follow reference attribute ATTR of SRC_DIE.
14680 On entry *REF_CU is the CU of SRC_DIE.
14681 On exit *REF_CU is the CU of the result. */
14682
14683static struct die_info *
14684follow_die_ref (struct die_info *src_die, struct attribute *attr,
14685 struct dwarf2_cu **ref_cu)
14686{
b64f50a1 14687 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
14688 struct dwarf2_cu *cu = *ref_cu;
14689 struct die_info *die;
14690
14691 die = follow_die_offset (offset, ref_cu);
14692 if (!die)
14693 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14694 "at 0x%x [in module %s]"),
b64f50a1 14695 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 14696
5c631832
JK
14697 return die;
14698}
14699
d83e736b
JK
14700/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14701 Returned value is intended for DW_OP_call*. Returned
14702 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14703
14704struct dwarf2_locexpr_baton
b64f50a1 14705dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
14706 struct dwarf2_per_cu_data *per_cu,
14707 CORE_ADDR (*get_frame_pc) (void *baton),
14708 void *baton)
5c631832 14709{
b64f50a1 14710 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 14711 struct dwarf2_cu *cu;
5c631832
JK
14712 struct die_info *die;
14713 struct attribute *attr;
14714 struct dwarf2_locexpr_baton retval;
14715
8cf6f0b1
TT
14716 dw2_setup (per_cu->objfile);
14717
918dd910
JK
14718 if (per_cu->cu == NULL)
14719 load_cu (per_cu);
14720 cu = per_cu->cu;
14721
5c631832
JK
14722 die = follow_die_offset (offset, &cu);
14723 if (!die)
14724 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 14725 offset.sect_off, per_cu->objfile->name);
5c631832
JK
14726
14727 attr = dwarf2_attr (die, DW_AT_location, cu);
14728 if (!attr)
14729 {
e103e986
JK
14730 /* DWARF: "If there is no such attribute, then there is no effect.".
14731 DATA is ignored if SIZE is 0. */
5c631832 14732
e103e986 14733 retval.data = NULL;
5c631832
JK
14734 retval.size = 0;
14735 }
8cf6f0b1
TT
14736 else if (attr_form_is_section_offset (attr))
14737 {
14738 struct dwarf2_loclist_baton loclist_baton;
14739 CORE_ADDR pc = (*get_frame_pc) (baton);
14740 size_t size;
14741
14742 fill_in_loclist_baton (cu, &loclist_baton, attr);
14743
14744 retval.data = dwarf2_find_location_expression (&loclist_baton,
14745 &size, pc);
14746 retval.size = size;
14747 }
5c631832
JK
14748 else
14749 {
14750 if (!attr_form_is_block (attr))
14751 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14752 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 14753 offset.sect_off, per_cu->objfile->name);
5c631832
JK
14754
14755 retval.data = DW_BLOCK (attr)->data;
14756 retval.size = DW_BLOCK (attr)->size;
14757 }
14758 retval.per_cu = cu->per_cu;
918dd910 14759
918dd910
JK
14760 age_cached_comp_units ();
14761
5c631832 14762 return retval;
348e048f
DE
14763}
14764
8a9b8146
TT
14765/* Return the type of the DIE at DIE_OFFSET in the CU named by
14766 PER_CU. */
14767
14768struct type *
b64f50a1 14769dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
14770 struct dwarf2_per_cu_data *per_cu)
14771{
b64f50a1
JK
14772 sect_offset die_offset_sect;
14773
8a9b8146 14774 dw2_setup (per_cu->objfile);
b64f50a1
JK
14775
14776 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
14777 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
14778}
14779
348e048f
DE
14780/* Follow the signature attribute ATTR in SRC_DIE.
14781 On entry *REF_CU is the CU of SRC_DIE.
14782 On exit *REF_CU is the CU of the result. */
14783
14784static struct die_info *
14785follow_die_sig (struct die_info *src_die, struct attribute *attr,
14786 struct dwarf2_cu **ref_cu)
14787{
14788 struct objfile *objfile = (*ref_cu)->objfile;
14789 struct die_info temp_die;
14790 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14791 struct dwarf2_cu *sig_cu;
14792 struct die_info *die;
14793
14794 /* sig_type will be NULL if the signatured type is missing from
14795 the debug info. */
14796 if (sig_type == NULL)
14797 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14798 "at 0x%x [in module %s]"),
b64f50a1 14799 src_die->offset.sect_off, objfile->name);
348e048f
DE
14800
14801 /* If necessary, add it to the queue and load its DIEs. */
14802
14803 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
a0f42c21 14804 read_signatured_type (sig_type);
348e048f
DE
14805
14806 gdb_assert (sig_type->per_cu.cu != NULL);
14807
14808 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
14809 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
14810 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
14811 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
14812 temp_die.offset.sect_off);
348e048f
DE
14813 if (die)
14814 {
14815 *ref_cu = sig_cu;
14816 return die;
14817 }
14818
3e43a32a
MS
14819 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14820 "from DIE at 0x%x [in module %s]"),
b64f50a1 14821 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
14822}
14823
14824/* Given an offset of a signatured type, return its signatured_type. */
14825
14826static struct signatured_type *
8b70b953
TT
14827lookup_signatured_type_at_offset (struct objfile *objfile,
14828 struct dwarf2_section_info *section,
b64f50a1 14829 sect_offset offset)
348e048f 14830{
b64f50a1 14831 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
14832 unsigned int length, initial_length_size;
14833 unsigned int sig_offset;
52dc124a 14834 struct signatured_type find_entry, *sig_type;
348e048f
DE
14835
14836 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14837 sig_offset = (initial_length_size
14838 + 2 /*version*/
14839 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14840 + 1 /*address_size*/);
14841 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 14842 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
14843
14844 /* This is only used to lookup previously recorded types.
14845 If we didn't find it, it's our bug. */
52dc124a
DE
14846 gdb_assert (sig_type != NULL);
14847 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 14848
52dc124a 14849 return sig_type;
348e048f
DE
14850}
14851
e5fe5e75 14852/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
14853
14854static void
e5fe5e75 14855load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 14856{
e5fe5e75 14857 struct objfile *objfile = per_cu->objfile;
3019eac3 14858 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
b64f50a1 14859 sect_offset offset = per_cu->offset;
52dc124a 14860 struct signatured_type *sig_type;
348e048f 14861
8b70b953 14862 dwarf2_read_section (objfile, sect);
be391dca 14863
348e048f 14864 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
14865 hash table lookup. */
14866 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14867 the signature to assert we found the right one.
14868 Ok, but it's a lot of work. We should simplify things so any needed
14869 assert doesn't require all this clumsiness. */
52dc124a 14870 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f 14871
dee91e82 14872 gdb_assert (&sig_type->per_cu == per_cu);
52dc124a 14873 gdb_assert (sig_type->per_cu.cu == NULL);
348e048f 14874
52dc124a 14875 read_signatured_type (sig_type);
348e048f 14876
52dc124a 14877 gdb_assert (sig_type->per_cu.cu != NULL);
348e048f
DE
14878}
14879
dee91e82
DE
14880/* die_reader_func for read_signatured_type.
14881 This is identical to load_full_comp_unit_reader,
14882 but is kept separate for now. */
348e048f
DE
14883
14884static void
dee91e82
DE
14885read_signatured_type_reader (const struct die_reader_specs *reader,
14886 gdb_byte *info_ptr,
14887 struct die_info *comp_unit_die,
14888 int has_children,
14889 void *data)
348e048f 14890{
dee91e82
DE
14891 struct dwarf2_cu *cu = reader->cu;
14892 struct attribute *attr;
348e048f 14893
dee91e82
DE
14894 gdb_assert (cu->die_hash == NULL);
14895 cu->die_hash =
14896 htab_create_alloc_ex (cu->header.length / 12,
14897 die_hash,
14898 die_eq,
14899 NULL,
14900 &cu->comp_unit_obstack,
14901 hashtab_obstack_allocate,
14902 dummy_obstack_deallocate);
348e048f 14903
dee91e82
DE
14904 if (has_children)
14905 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
14906 &info_ptr, comp_unit_die);
14907 cu->dies = comp_unit_die;
14908 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
14909
14910 /* We try not to read any attributes in this function, because not
9cdd5dbd 14911 all CUs needed for references have been loaded yet, and symbol
348e048f 14912 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
14913 or we won't be able to build types correctly.
14914 Similarly, if we do not read the producer, we can not apply
14915 producer-specific interpretation. */
9816fde3 14916 prepare_one_comp_unit (cu, cu->dies);
dee91e82 14917}
348e048f 14918
3019eac3
DE
14919/* Read in a signatured type and build its CU and DIEs.
14920 If the type is a stub for the real type in a DWO file,
14921 read in the real type from the DWO file as well. */
dee91e82
DE
14922
14923static void
14924read_signatured_type (struct signatured_type *sig_type)
14925{
14926 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 14927
3019eac3 14928 gdb_assert (per_cu->is_debug_types);
dee91e82 14929 gdb_assert (per_cu->cu == NULL);
348e048f 14930
fd820528 14931 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
c906108c
SS
14932}
14933
c906108c
SS
14934/* Decode simple location descriptions.
14935 Given a pointer to a dwarf block that defines a location, compute
14936 the location and return the value.
14937
4cecd739
DJ
14938 NOTE drow/2003-11-18: This function is called in two situations
14939 now: for the address of static or global variables (partial symbols
14940 only) and for offsets into structures which are expected to be
14941 (more or less) constant. The partial symbol case should go away,
14942 and only the constant case should remain. That will let this
14943 function complain more accurately. A few special modes are allowed
14944 without complaint for global variables (for instance, global
14945 register values and thread-local values).
c906108c
SS
14946
14947 A location description containing no operations indicates that the
4cecd739 14948 object is optimized out. The return value is 0 for that case.
6b992462
DJ
14949 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14950 callers will only want a very basic result and this can become a
21ae7a4d
JK
14951 complaint.
14952
14953 Note that stack[0] is unused except as a default error return. */
c906108c
SS
14954
14955static CORE_ADDR
e7c27a73 14956decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 14957{
e7c27a73 14958 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
14959 int i;
14960 int size = blk->size;
14961 gdb_byte *data = blk->data;
14962 CORE_ADDR stack[64];
14963 int stacki;
14964 unsigned int bytes_read, unsnd;
14965 gdb_byte op;
c906108c 14966
21ae7a4d
JK
14967 i = 0;
14968 stacki = 0;
14969 stack[stacki] = 0;
14970 stack[++stacki] = 0;
14971
14972 while (i < size)
14973 {
14974 op = data[i++];
14975 switch (op)
14976 {
14977 case DW_OP_lit0:
14978 case DW_OP_lit1:
14979 case DW_OP_lit2:
14980 case DW_OP_lit3:
14981 case DW_OP_lit4:
14982 case DW_OP_lit5:
14983 case DW_OP_lit6:
14984 case DW_OP_lit7:
14985 case DW_OP_lit8:
14986 case DW_OP_lit9:
14987 case DW_OP_lit10:
14988 case DW_OP_lit11:
14989 case DW_OP_lit12:
14990 case DW_OP_lit13:
14991 case DW_OP_lit14:
14992 case DW_OP_lit15:
14993 case DW_OP_lit16:
14994 case DW_OP_lit17:
14995 case DW_OP_lit18:
14996 case DW_OP_lit19:
14997 case DW_OP_lit20:
14998 case DW_OP_lit21:
14999 case DW_OP_lit22:
15000 case DW_OP_lit23:
15001 case DW_OP_lit24:
15002 case DW_OP_lit25:
15003 case DW_OP_lit26:
15004 case DW_OP_lit27:
15005 case DW_OP_lit28:
15006 case DW_OP_lit29:
15007 case DW_OP_lit30:
15008 case DW_OP_lit31:
15009 stack[++stacki] = op - DW_OP_lit0;
15010 break;
f1bea926 15011
21ae7a4d
JK
15012 case DW_OP_reg0:
15013 case DW_OP_reg1:
15014 case DW_OP_reg2:
15015 case DW_OP_reg3:
15016 case DW_OP_reg4:
15017 case DW_OP_reg5:
15018 case DW_OP_reg6:
15019 case DW_OP_reg7:
15020 case DW_OP_reg8:
15021 case DW_OP_reg9:
15022 case DW_OP_reg10:
15023 case DW_OP_reg11:
15024 case DW_OP_reg12:
15025 case DW_OP_reg13:
15026 case DW_OP_reg14:
15027 case DW_OP_reg15:
15028 case DW_OP_reg16:
15029 case DW_OP_reg17:
15030 case DW_OP_reg18:
15031 case DW_OP_reg19:
15032 case DW_OP_reg20:
15033 case DW_OP_reg21:
15034 case DW_OP_reg22:
15035 case DW_OP_reg23:
15036 case DW_OP_reg24:
15037 case DW_OP_reg25:
15038 case DW_OP_reg26:
15039 case DW_OP_reg27:
15040 case DW_OP_reg28:
15041 case DW_OP_reg29:
15042 case DW_OP_reg30:
15043 case DW_OP_reg31:
15044 stack[++stacki] = op - DW_OP_reg0;
15045 if (i < size)
15046 dwarf2_complex_location_expr_complaint ();
15047 break;
c906108c 15048
21ae7a4d
JK
15049 case DW_OP_regx:
15050 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15051 i += bytes_read;
15052 stack[++stacki] = unsnd;
15053 if (i < size)
15054 dwarf2_complex_location_expr_complaint ();
15055 break;
c906108c 15056
21ae7a4d
JK
15057 case DW_OP_addr:
15058 stack[++stacki] = read_address (objfile->obfd, &data[i],
15059 cu, &bytes_read);
15060 i += bytes_read;
15061 break;
d53d4ac5 15062
21ae7a4d
JK
15063 case DW_OP_const1u:
15064 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15065 i += 1;
15066 break;
15067
15068 case DW_OP_const1s:
15069 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15070 i += 1;
15071 break;
15072
15073 case DW_OP_const2u:
15074 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15075 i += 2;
15076 break;
15077
15078 case DW_OP_const2s:
15079 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15080 i += 2;
15081 break;
d53d4ac5 15082
21ae7a4d
JK
15083 case DW_OP_const4u:
15084 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15085 i += 4;
15086 break;
15087
15088 case DW_OP_const4s:
15089 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15090 i += 4;
15091 break;
15092
585861ea
JK
15093 case DW_OP_const8u:
15094 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15095 i += 8;
15096 break;
15097
21ae7a4d
JK
15098 case DW_OP_constu:
15099 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15100 &bytes_read);
15101 i += bytes_read;
15102 break;
15103
15104 case DW_OP_consts:
15105 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15106 i += bytes_read;
15107 break;
15108
15109 case DW_OP_dup:
15110 stack[stacki + 1] = stack[stacki];
15111 stacki++;
15112 break;
15113
15114 case DW_OP_plus:
15115 stack[stacki - 1] += stack[stacki];
15116 stacki--;
15117 break;
15118
15119 case DW_OP_plus_uconst:
15120 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15121 &bytes_read);
15122 i += bytes_read;
15123 break;
15124
15125 case DW_OP_minus:
15126 stack[stacki - 1] -= stack[stacki];
15127 stacki--;
15128 break;
15129
15130 case DW_OP_deref:
15131 /* If we're not the last op, then we definitely can't encode
15132 this using GDB's address_class enum. This is valid for partial
15133 global symbols, although the variable's address will be bogus
15134 in the psymtab. */
15135 if (i < size)
15136 dwarf2_complex_location_expr_complaint ();
15137 break;
15138
15139 case DW_OP_GNU_push_tls_address:
15140 /* The top of the stack has the offset from the beginning
15141 of the thread control block at which the variable is located. */
15142 /* Nothing should follow this operator, so the top of stack would
15143 be returned. */
15144 /* This is valid for partial global symbols, but the variable's
585861ea
JK
15145 address will be bogus in the psymtab. Make it always at least
15146 non-zero to not look as a variable garbage collected by linker
15147 which have DW_OP_addr 0. */
21ae7a4d
JK
15148 if (i < size)
15149 dwarf2_complex_location_expr_complaint ();
585861ea 15150 stack[stacki]++;
21ae7a4d
JK
15151 break;
15152
15153 case DW_OP_GNU_uninit:
15154 break;
15155
3019eac3
DE
15156 case DW_OP_GNU_addr_index:
15157 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15158 &bytes_read);
15159 i += bytes_read;
15160 break;
15161
21ae7a4d
JK
15162 default:
15163 {
f39c6ffd 15164 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
15165
15166 if (name)
15167 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15168 name);
15169 else
15170 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15171 op);
15172 }
15173
15174 return (stack[stacki]);
d53d4ac5 15175 }
3c6e0cb3 15176
21ae7a4d
JK
15177 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15178 outside of the allocated space. Also enforce minimum>0. */
15179 if (stacki >= ARRAY_SIZE (stack) - 1)
15180 {
15181 complaint (&symfile_complaints,
15182 _("location description stack overflow"));
15183 return 0;
15184 }
15185
15186 if (stacki <= 0)
15187 {
15188 complaint (&symfile_complaints,
15189 _("location description stack underflow"));
15190 return 0;
15191 }
15192 }
15193 return (stack[stacki]);
c906108c
SS
15194}
15195
15196/* memory allocation interface */
15197
c906108c 15198static struct dwarf_block *
7b5a2f43 15199dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
15200{
15201 struct dwarf_block *blk;
15202
15203 blk = (struct dwarf_block *)
7b5a2f43 15204 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
15205 return (blk);
15206}
15207
15208static struct abbrev_info *
f3dd6933 15209dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
15210{
15211 struct abbrev_info *abbrev;
15212
f3dd6933
DJ
15213 abbrev = (struct abbrev_info *)
15214 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
15215 memset (abbrev, 0, sizeof (struct abbrev_info));
15216 return (abbrev);
15217}
15218
15219static struct die_info *
b60c80d6 15220dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
15221{
15222 struct die_info *die;
b60c80d6
DJ
15223 size_t size = sizeof (struct die_info);
15224
15225 if (num_attrs > 1)
15226 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 15227
b60c80d6 15228 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
15229 memset (die, 0, sizeof (struct die_info));
15230 return (die);
15231}
2e276125
JB
15232
15233\f
15234/* Macro support. */
15235
2e276125
JB
15236/* Return the full name of file number I in *LH's file name table.
15237 Use COMP_DIR as the name of the current directory of the
15238 compilation. The result is allocated using xmalloc; the caller is
15239 responsible for freeing it. */
15240static char *
15241file_full_name (int file, struct line_header *lh, const char *comp_dir)
15242{
6a83a1e6
EZ
15243 /* Is the file number a valid index into the line header's file name
15244 table? Remember that file numbers start with one, not zero. */
15245 if (1 <= file && file <= lh->num_file_names)
15246 {
15247 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 15248
6a83a1e6
EZ
15249 if (IS_ABSOLUTE_PATH (fe->name))
15250 return xstrdup (fe->name);
15251 else
15252 {
15253 const char *dir;
15254 int dir_len;
15255 char *full_name;
15256
15257 if (fe->dir_index)
15258 dir = lh->include_dirs[fe->dir_index - 1];
15259 else
15260 dir = comp_dir;
15261
15262 if (dir)
15263 {
15264 dir_len = strlen (dir);
15265 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15266 strcpy (full_name, dir);
15267 full_name[dir_len] = '/';
15268 strcpy (full_name + dir_len + 1, fe->name);
15269 return full_name;
15270 }
15271 else
15272 return xstrdup (fe->name);
15273 }
15274 }
2e276125
JB
15275 else
15276 {
6a83a1e6
EZ
15277 /* The compiler produced a bogus file number. We can at least
15278 record the macro definitions made in the file, even if we
15279 won't be able to find the file by name. */
15280 char fake_name[80];
9a619af0 15281
6a83a1e6 15282 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 15283
6e70227d 15284 complaint (&symfile_complaints,
6a83a1e6
EZ
15285 _("bad file number in macro information (%d)"),
15286 file);
2e276125 15287
6a83a1e6 15288 return xstrdup (fake_name);
2e276125
JB
15289 }
15290}
15291
15292
15293static struct macro_source_file *
15294macro_start_file (int file, int line,
15295 struct macro_source_file *current_file,
15296 const char *comp_dir,
15297 struct line_header *lh, struct objfile *objfile)
15298{
15299 /* The full name of this source file. */
15300 char *full_name = file_full_name (file, lh, comp_dir);
15301
15302 /* We don't create a macro table for this compilation unit
15303 at all until we actually get a filename. */
15304 if (! pending_macros)
4a146b47 15305 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 15306 objfile->macro_cache);
2e276125
JB
15307
15308 if (! current_file)
15309 /* If we have no current file, then this must be the start_file
15310 directive for the compilation unit's main source file. */
15311 current_file = macro_set_main (pending_macros, full_name);
15312 else
15313 current_file = macro_include (current_file, line, full_name);
15314
15315 xfree (full_name);
6e70227d 15316
2e276125
JB
15317 return current_file;
15318}
15319
15320
15321/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15322 followed by a null byte. */
15323static char *
15324copy_string (const char *buf, int len)
15325{
15326 char *s = xmalloc (len + 1);
9a619af0 15327
2e276125
JB
15328 memcpy (s, buf, len);
15329 s[len] = '\0';
2e276125
JB
15330 return s;
15331}
15332
15333
15334static const char *
15335consume_improper_spaces (const char *p, const char *body)
15336{
15337 if (*p == ' ')
15338 {
4d3c2250 15339 complaint (&symfile_complaints,
3e43a32a
MS
15340 _("macro definition contains spaces "
15341 "in formal argument list:\n`%s'"),
4d3c2250 15342 body);
2e276125
JB
15343
15344 while (*p == ' ')
15345 p++;
15346 }
15347
15348 return p;
15349}
15350
15351
15352static void
15353parse_macro_definition (struct macro_source_file *file, int line,
15354 const char *body)
15355{
15356 const char *p;
15357
15358 /* The body string takes one of two forms. For object-like macro
15359 definitions, it should be:
15360
15361 <macro name> " " <definition>
15362
15363 For function-like macro definitions, it should be:
15364
15365 <macro name> "() " <definition>
15366 or
15367 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15368
15369 Spaces may appear only where explicitly indicated, and in the
15370 <definition>.
15371
15372 The Dwarf 2 spec says that an object-like macro's name is always
15373 followed by a space, but versions of GCC around March 2002 omit
6e70227d 15374 the space when the macro's definition is the empty string.
2e276125
JB
15375
15376 The Dwarf 2 spec says that there should be no spaces between the
15377 formal arguments in a function-like macro's formal argument list,
15378 but versions of GCC around March 2002 include spaces after the
15379 commas. */
15380
15381
15382 /* Find the extent of the macro name. The macro name is terminated
15383 by either a space or null character (for an object-like macro) or
15384 an opening paren (for a function-like macro). */
15385 for (p = body; *p; p++)
15386 if (*p == ' ' || *p == '(')
15387 break;
15388
15389 if (*p == ' ' || *p == '\0')
15390 {
15391 /* It's an object-like macro. */
15392 int name_len = p - body;
15393 char *name = copy_string (body, name_len);
15394 const char *replacement;
15395
15396 if (*p == ' ')
15397 replacement = body + name_len + 1;
15398 else
15399 {
4d3c2250 15400 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15401 replacement = body + name_len;
15402 }
6e70227d 15403
2e276125
JB
15404 macro_define_object (file, line, name, replacement);
15405
15406 xfree (name);
15407 }
15408 else if (*p == '(')
15409 {
15410 /* It's a function-like macro. */
15411 char *name = copy_string (body, p - body);
15412 int argc = 0;
15413 int argv_size = 1;
15414 char **argv = xmalloc (argv_size * sizeof (*argv));
15415
15416 p++;
15417
15418 p = consume_improper_spaces (p, body);
15419
15420 /* Parse the formal argument list. */
15421 while (*p && *p != ')')
15422 {
15423 /* Find the extent of the current argument name. */
15424 const char *arg_start = p;
15425
15426 while (*p && *p != ',' && *p != ')' && *p != ' ')
15427 p++;
15428
15429 if (! *p || p == arg_start)
4d3c2250 15430 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15431 else
15432 {
15433 /* Make sure argv has room for the new argument. */
15434 if (argc >= argv_size)
15435 {
15436 argv_size *= 2;
15437 argv = xrealloc (argv, argv_size * sizeof (*argv));
15438 }
15439
15440 argv[argc++] = copy_string (arg_start, p - arg_start);
15441 }
15442
15443 p = consume_improper_spaces (p, body);
15444
15445 /* Consume the comma, if present. */
15446 if (*p == ',')
15447 {
15448 p++;
15449
15450 p = consume_improper_spaces (p, body);
15451 }
15452 }
15453
15454 if (*p == ')')
15455 {
15456 p++;
15457
15458 if (*p == ' ')
15459 /* Perfectly formed definition, no complaints. */
15460 macro_define_function (file, line, name,
6e70227d 15461 argc, (const char **) argv,
2e276125
JB
15462 p + 1);
15463 else if (*p == '\0')
15464 {
15465 /* Complain, but do define it. */
4d3c2250 15466 dwarf2_macro_malformed_definition_complaint (body);
2e276125 15467 macro_define_function (file, line, name,
6e70227d 15468 argc, (const char **) argv,
2e276125
JB
15469 p);
15470 }
15471 else
15472 /* Just complain. */
4d3c2250 15473 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15474 }
15475 else
15476 /* Just complain. */
4d3c2250 15477 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15478
15479 xfree (name);
15480 {
15481 int i;
15482
15483 for (i = 0; i < argc; i++)
15484 xfree (argv[i]);
15485 }
15486 xfree (argv);
15487 }
15488 else
4d3c2250 15489 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15490}
15491
cf2c3c16
TT
15492/* Skip some bytes from BYTES according to the form given in FORM.
15493 Returns the new pointer. */
2e276125 15494
cf2c3c16
TT
15495static gdb_byte *
15496skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15497 enum dwarf_form form,
15498 unsigned int offset_size,
15499 struct dwarf2_section_info *section)
2e276125 15500{
cf2c3c16 15501 unsigned int bytes_read;
2e276125 15502
cf2c3c16 15503 switch (form)
2e276125 15504 {
cf2c3c16
TT
15505 case DW_FORM_data1:
15506 case DW_FORM_flag:
15507 ++bytes;
15508 break;
15509
15510 case DW_FORM_data2:
15511 bytes += 2;
15512 break;
15513
15514 case DW_FORM_data4:
15515 bytes += 4;
15516 break;
15517
15518 case DW_FORM_data8:
15519 bytes += 8;
15520 break;
15521
15522 case DW_FORM_string:
15523 read_direct_string (abfd, bytes, &bytes_read);
15524 bytes += bytes_read;
15525 break;
15526
15527 case DW_FORM_sec_offset:
15528 case DW_FORM_strp:
15529 bytes += offset_size;
15530 break;
15531
15532 case DW_FORM_block:
15533 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15534 bytes += bytes_read;
15535 break;
15536
15537 case DW_FORM_block1:
15538 bytes += 1 + read_1_byte (abfd, bytes);
15539 break;
15540 case DW_FORM_block2:
15541 bytes += 2 + read_2_bytes (abfd, bytes);
15542 break;
15543 case DW_FORM_block4:
15544 bytes += 4 + read_4_bytes (abfd, bytes);
15545 break;
15546
15547 case DW_FORM_sdata:
15548 case DW_FORM_udata:
3019eac3
DE
15549 case DW_FORM_GNU_addr_index:
15550 case DW_FORM_GNU_str_index:
cf2c3c16
TT
15551 bytes = skip_leb128 (abfd, bytes);
15552 break;
15553
15554 default:
15555 {
15556 complain:
15557 complaint (&symfile_complaints,
15558 _("invalid form 0x%x in `%s'"),
15559 form,
15560 section->asection->name);
15561 return NULL;
15562 }
2e276125
JB
15563 }
15564
cf2c3c16
TT
15565 return bytes;
15566}
757a13d0 15567
cf2c3c16
TT
15568/* A helper for dwarf_decode_macros that handles skipping an unknown
15569 opcode. Returns an updated pointer to the macro data buffer; or,
15570 on error, issues a complaint and returns NULL. */
757a13d0 15571
cf2c3c16
TT
15572static gdb_byte *
15573skip_unknown_opcode (unsigned int opcode,
15574 gdb_byte **opcode_definitions,
15575 gdb_byte *mac_ptr,
15576 bfd *abfd,
15577 unsigned int offset_size,
15578 struct dwarf2_section_info *section)
15579{
15580 unsigned int bytes_read, i;
15581 unsigned long arg;
15582 gdb_byte *defn;
2e276125 15583
cf2c3c16 15584 if (opcode_definitions[opcode] == NULL)
2e276125 15585 {
cf2c3c16
TT
15586 complaint (&symfile_complaints,
15587 _("unrecognized DW_MACFINO opcode 0x%x"),
15588 opcode);
15589 return NULL;
15590 }
2e276125 15591
cf2c3c16
TT
15592 defn = opcode_definitions[opcode];
15593 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15594 defn += bytes_read;
2e276125 15595
cf2c3c16
TT
15596 for (i = 0; i < arg; ++i)
15597 {
15598 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15599 if (mac_ptr == NULL)
15600 {
15601 /* skip_form_bytes already issued the complaint. */
15602 return NULL;
15603 }
15604 }
757a13d0 15605
cf2c3c16
TT
15606 return mac_ptr;
15607}
757a13d0 15608
cf2c3c16
TT
15609/* A helper function which parses the header of a macro section.
15610 If the macro section is the extended (for now called "GNU") type,
15611 then this updates *OFFSET_SIZE. Returns a pointer to just after
15612 the header, or issues a complaint and returns NULL on error. */
757a13d0 15613
cf2c3c16
TT
15614static gdb_byte *
15615dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15616 bfd *abfd,
15617 gdb_byte *mac_ptr,
15618 unsigned int *offset_size,
15619 int section_is_gnu)
15620{
15621 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15622
cf2c3c16
TT
15623 if (section_is_gnu)
15624 {
15625 unsigned int version, flags;
757a13d0 15626
cf2c3c16
TT
15627 version = read_2_bytes (abfd, mac_ptr);
15628 if (version != 4)
15629 {
15630 complaint (&symfile_complaints,
15631 _("unrecognized version `%d' in .debug_macro section"),
15632 version);
15633 return NULL;
15634 }
15635 mac_ptr += 2;
757a13d0 15636
cf2c3c16
TT
15637 flags = read_1_byte (abfd, mac_ptr);
15638 ++mac_ptr;
15639 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15640
cf2c3c16
TT
15641 if ((flags & 2) != 0)
15642 /* We don't need the line table offset. */
15643 mac_ptr += *offset_size;
757a13d0 15644
cf2c3c16
TT
15645 /* Vendor opcode descriptions. */
15646 if ((flags & 4) != 0)
15647 {
15648 unsigned int i, count;
757a13d0 15649
cf2c3c16
TT
15650 count = read_1_byte (abfd, mac_ptr);
15651 ++mac_ptr;
15652 for (i = 0; i < count; ++i)
15653 {
15654 unsigned int opcode, bytes_read;
15655 unsigned long arg;
15656
15657 opcode = read_1_byte (abfd, mac_ptr);
15658 ++mac_ptr;
15659 opcode_definitions[opcode] = mac_ptr;
15660 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15661 mac_ptr += bytes_read;
15662 mac_ptr += arg;
15663 }
757a13d0 15664 }
cf2c3c16 15665 }
757a13d0 15666
cf2c3c16
TT
15667 return mac_ptr;
15668}
757a13d0 15669
cf2c3c16 15670/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 15671 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
15672
15673static void
15674dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15675 struct macro_source_file *current_file,
15676 struct line_header *lh, char *comp_dir,
15677 struct dwarf2_section_info *section,
15678 int section_is_gnu,
15679 unsigned int offset_size,
8fc3fc34
TT
15680 struct objfile *objfile,
15681 htab_t include_hash)
cf2c3c16
TT
15682{
15683 enum dwarf_macro_record_type macinfo_type;
15684 int at_commandline;
15685 gdb_byte *opcode_definitions[256];
757a13d0 15686
cf2c3c16
TT
15687 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15688 &offset_size, section_is_gnu);
15689 if (mac_ptr == NULL)
15690 {
15691 /* We already issued a complaint. */
15692 return;
15693 }
757a13d0
JK
15694
15695 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15696 GDB is still reading the definitions from command line. First
15697 DW_MACINFO_start_file will need to be ignored as it was already executed
15698 to create CURRENT_FILE for the main source holding also the command line
15699 definitions. On first met DW_MACINFO_start_file this flag is reset to
15700 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15701
15702 at_commandline = 1;
15703
15704 do
15705 {
15706 /* Do we at least have room for a macinfo type byte? */
15707 if (mac_ptr >= mac_end)
15708 {
cf2c3c16 15709 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
15710 break;
15711 }
15712
15713 macinfo_type = read_1_byte (abfd, mac_ptr);
15714 mac_ptr++;
15715
cf2c3c16
TT
15716 /* Note that we rely on the fact that the corresponding GNU and
15717 DWARF constants are the same. */
757a13d0
JK
15718 switch (macinfo_type)
15719 {
15720 /* A zero macinfo type indicates the end of the macro
15721 information. */
15722 case 0:
15723 break;
2e276125 15724
cf2c3c16
TT
15725 case DW_MACRO_GNU_define:
15726 case DW_MACRO_GNU_undef:
15727 case DW_MACRO_GNU_define_indirect:
15728 case DW_MACRO_GNU_undef_indirect:
2e276125 15729 {
891d2f0b 15730 unsigned int bytes_read;
2e276125
JB
15731 int line;
15732 char *body;
cf2c3c16 15733 int is_define;
2e276125 15734
cf2c3c16
TT
15735 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15736 mac_ptr += bytes_read;
15737
15738 if (macinfo_type == DW_MACRO_GNU_define
15739 || macinfo_type == DW_MACRO_GNU_undef)
15740 {
15741 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15742 mac_ptr += bytes_read;
15743 }
15744 else
15745 {
15746 LONGEST str_offset;
15747
15748 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15749 mac_ptr += offset_size;
2e276125 15750
cf2c3c16
TT
15751 body = read_indirect_string_at_offset (abfd, str_offset);
15752 }
15753
15754 is_define = (macinfo_type == DW_MACRO_GNU_define
15755 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 15756 if (! current_file)
757a13d0
JK
15757 {
15758 /* DWARF violation as no main source is present. */
15759 complaint (&symfile_complaints,
15760 _("debug info with no main source gives macro %s "
15761 "on line %d: %s"),
cf2c3c16
TT
15762 is_define ? _("definition") : _("undefinition"),
15763 line, body);
757a13d0
JK
15764 break;
15765 }
3e43a32a
MS
15766 if ((line == 0 && !at_commandline)
15767 || (line != 0 && at_commandline))
4d3c2250 15768 complaint (&symfile_complaints,
757a13d0
JK
15769 _("debug info gives %s macro %s with %s line %d: %s"),
15770 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 15771 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
15772 line == 0 ? _("zero") : _("non-zero"), line, body);
15773
cf2c3c16 15774 if (is_define)
757a13d0 15775 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
15776 else
15777 {
15778 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15779 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15780 macro_undef (current_file, line, body);
15781 }
2e276125
JB
15782 }
15783 break;
15784
cf2c3c16 15785 case DW_MACRO_GNU_start_file:
2e276125 15786 {
891d2f0b 15787 unsigned int bytes_read;
2e276125
JB
15788 int line, file;
15789
15790 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15791 mac_ptr += bytes_read;
15792 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15793 mac_ptr += bytes_read;
15794
3e43a32a
MS
15795 if ((line == 0 && !at_commandline)
15796 || (line != 0 && at_commandline))
757a13d0
JK
15797 complaint (&symfile_complaints,
15798 _("debug info gives source %d included "
15799 "from %s at %s line %d"),
15800 file, at_commandline ? _("command-line") : _("file"),
15801 line == 0 ? _("zero") : _("non-zero"), line);
15802
15803 if (at_commandline)
15804 {
cf2c3c16
TT
15805 /* This DW_MACRO_GNU_start_file was executed in the
15806 pass one. */
757a13d0
JK
15807 at_commandline = 0;
15808 }
15809 else
15810 current_file = macro_start_file (file, line,
15811 current_file, comp_dir,
cf2c3c16 15812 lh, objfile);
2e276125
JB
15813 }
15814 break;
15815
cf2c3c16 15816 case DW_MACRO_GNU_end_file:
2e276125 15817 if (! current_file)
4d3c2250 15818 complaint (&symfile_complaints,
3e43a32a
MS
15819 _("macro debug info has an unmatched "
15820 "`close_file' directive"));
2e276125
JB
15821 else
15822 {
15823 current_file = current_file->included_by;
15824 if (! current_file)
15825 {
cf2c3c16 15826 enum dwarf_macro_record_type next_type;
2e276125
JB
15827
15828 /* GCC circa March 2002 doesn't produce the zero
15829 type byte marking the end of the compilation
15830 unit. Complain if it's not there, but exit no
15831 matter what. */
15832
15833 /* Do we at least have room for a macinfo type byte? */
15834 if (mac_ptr >= mac_end)
15835 {
cf2c3c16 15836 dwarf2_macros_too_long_complaint (section);
2e276125
JB
15837 return;
15838 }
15839
15840 /* We don't increment mac_ptr here, so this is just
15841 a look-ahead. */
15842 next_type = read_1_byte (abfd, mac_ptr);
15843 if (next_type != 0)
4d3c2250 15844 complaint (&symfile_complaints,
3e43a32a
MS
15845 _("no terminating 0-type entry for "
15846 "macros in `.debug_macinfo' section"));
2e276125
JB
15847
15848 return;
15849 }
15850 }
15851 break;
15852
cf2c3c16
TT
15853 case DW_MACRO_GNU_transparent_include:
15854 {
15855 LONGEST offset;
8fc3fc34 15856 void **slot;
cf2c3c16
TT
15857
15858 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15859 mac_ptr += offset_size;
15860
8fc3fc34
TT
15861 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15862 if (*slot != NULL)
15863 {
15864 /* This has actually happened; see
15865 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
15866 complaint (&symfile_complaints,
15867 _("recursive DW_MACRO_GNU_transparent_include in "
15868 ".debug_macro section"));
15869 }
15870 else
15871 {
15872 *slot = mac_ptr;
15873
15874 dwarf_decode_macro_bytes (abfd,
15875 section->buffer + offset,
15876 mac_end, current_file,
15877 lh, comp_dir,
15878 section, section_is_gnu,
15879 offset_size, objfile, include_hash);
15880
15881 htab_remove_elt (include_hash, mac_ptr);
15882 }
cf2c3c16
TT
15883 }
15884 break;
15885
2e276125 15886 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
15887 if (!section_is_gnu)
15888 {
15889 unsigned int bytes_read;
15890 int constant;
2e276125 15891
cf2c3c16
TT
15892 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15893 mac_ptr += bytes_read;
15894 read_direct_string (abfd, mac_ptr, &bytes_read);
15895 mac_ptr += bytes_read;
2e276125 15896
cf2c3c16
TT
15897 /* We don't recognize any vendor extensions. */
15898 break;
15899 }
15900 /* FALLTHROUGH */
15901
15902 default:
15903 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15904 mac_ptr, abfd, offset_size,
15905 section);
15906 if (mac_ptr == NULL)
15907 return;
15908 break;
2e276125 15909 }
757a13d0 15910 } while (macinfo_type != 0);
2e276125 15911}
8e19ed76 15912
cf2c3c16
TT
15913static void
15914dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15915 char *comp_dir, bfd *abfd,
15916 struct dwarf2_cu *cu,
15917 struct dwarf2_section_info *section,
fceca515 15918 int section_is_gnu, const char *section_name)
cf2c3c16 15919{
bb5ed363 15920 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
15921 gdb_byte *mac_ptr, *mac_end;
15922 struct macro_source_file *current_file = 0;
15923 enum dwarf_macro_record_type macinfo_type;
15924 unsigned int offset_size = cu->header.offset_size;
15925 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
15926 struct cleanup *cleanup;
15927 htab_t include_hash;
15928 void **slot;
cf2c3c16 15929
bb5ed363 15930 dwarf2_read_section (objfile, section);
cf2c3c16
TT
15931 if (section->buffer == NULL)
15932 {
fceca515 15933 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
15934 return;
15935 }
15936
15937 /* First pass: Find the name of the base filename.
15938 This filename is needed in order to process all macros whose definition
15939 (or undefinition) comes from the command line. These macros are defined
15940 before the first DW_MACINFO_start_file entry, and yet still need to be
15941 associated to the base file.
15942
15943 To determine the base file name, we scan the macro definitions until we
15944 reach the first DW_MACINFO_start_file entry. We then initialize
15945 CURRENT_FILE accordingly so that any macro definition found before the
15946 first DW_MACINFO_start_file can still be associated to the base file. */
15947
15948 mac_ptr = section->buffer + offset;
15949 mac_end = section->buffer + section->size;
15950
15951 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15952 &offset_size, section_is_gnu);
15953 if (mac_ptr == NULL)
15954 {
15955 /* We already issued a complaint. */
15956 return;
15957 }
15958
15959 do
15960 {
15961 /* Do we at least have room for a macinfo type byte? */
15962 if (mac_ptr >= mac_end)
15963 {
15964 /* Complaint is printed during the second pass as GDB will probably
15965 stop the first pass earlier upon finding
15966 DW_MACINFO_start_file. */
15967 break;
15968 }
15969
15970 macinfo_type = read_1_byte (abfd, mac_ptr);
15971 mac_ptr++;
15972
15973 /* Note that we rely on the fact that the corresponding GNU and
15974 DWARF constants are the same. */
15975 switch (macinfo_type)
15976 {
15977 /* A zero macinfo type indicates the end of the macro
15978 information. */
15979 case 0:
15980 break;
15981
15982 case DW_MACRO_GNU_define:
15983 case DW_MACRO_GNU_undef:
15984 /* Only skip the data by MAC_PTR. */
15985 {
15986 unsigned int bytes_read;
15987
15988 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15989 mac_ptr += bytes_read;
15990 read_direct_string (abfd, mac_ptr, &bytes_read);
15991 mac_ptr += bytes_read;
15992 }
15993 break;
15994
15995 case DW_MACRO_GNU_start_file:
15996 {
15997 unsigned int bytes_read;
15998 int line, file;
15999
16000 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16001 mac_ptr += bytes_read;
16002 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16003 mac_ptr += bytes_read;
16004
16005 current_file = macro_start_file (file, line, current_file,
bb5ed363 16006 comp_dir, lh, objfile);
cf2c3c16
TT
16007 }
16008 break;
16009
16010 case DW_MACRO_GNU_end_file:
16011 /* No data to skip by MAC_PTR. */
16012 break;
16013
16014 case DW_MACRO_GNU_define_indirect:
16015 case DW_MACRO_GNU_undef_indirect:
16016 {
16017 unsigned int bytes_read;
16018
16019 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16020 mac_ptr += bytes_read;
16021 mac_ptr += offset_size;
16022 }
16023 break;
16024
16025 case DW_MACRO_GNU_transparent_include:
16026 /* Note that, according to the spec, a transparent include
16027 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16028 skip this opcode. */
16029 mac_ptr += offset_size;
16030 break;
16031
16032 case DW_MACINFO_vendor_ext:
16033 /* Only skip the data by MAC_PTR. */
16034 if (!section_is_gnu)
16035 {
16036 unsigned int bytes_read;
16037
16038 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16039 mac_ptr += bytes_read;
16040 read_direct_string (abfd, mac_ptr, &bytes_read);
16041 mac_ptr += bytes_read;
16042 }
16043 /* FALLTHROUGH */
16044
16045 default:
16046 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16047 mac_ptr, abfd, offset_size,
16048 section);
16049 if (mac_ptr == NULL)
16050 return;
16051 break;
16052 }
16053 } while (macinfo_type != 0 && current_file == NULL);
16054
16055 /* Second pass: Process all entries.
16056
16057 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16058 command-line macro definitions/undefinitions. This flag is unset when we
16059 reach the first DW_MACINFO_start_file entry. */
16060
8fc3fc34
TT
16061 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16062 NULL, xcalloc, xfree);
16063 cleanup = make_cleanup_htab_delete (include_hash);
16064 mac_ptr = section->buffer + offset;
16065 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16066 *slot = mac_ptr;
16067 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
cf2c3c16 16068 current_file, lh, comp_dir, section, section_is_gnu,
8fc3fc34
TT
16069 offset_size, objfile, include_hash);
16070 do_cleanups (cleanup);
cf2c3c16
TT
16071}
16072
8e19ed76 16073/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 16074 if so return true else false. */
380bca97 16075
8e19ed76
PS
16076static int
16077attr_form_is_block (struct attribute *attr)
16078{
16079 return (attr == NULL ? 0 :
16080 attr->form == DW_FORM_block1
16081 || attr->form == DW_FORM_block2
16082 || attr->form == DW_FORM_block4
2dc7f7b3
TT
16083 || attr->form == DW_FORM_block
16084 || attr->form == DW_FORM_exprloc);
8e19ed76 16085}
4c2df51b 16086
c6a0999f
JB
16087/* Return non-zero if ATTR's value is a section offset --- classes
16088 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16089 You may use DW_UNSND (attr) to retrieve such offsets.
16090
16091 Section 7.5.4, "Attribute Encodings", explains that no attribute
16092 may have a value that belongs to more than one of these classes; it
16093 would be ambiguous if we did, because we use the same forms for all
16094 of them. */
380bca97 16095
3690dd37
JB
16096static int
16097attr_form_is_section_offset (struct attribute *attr)
16098{
16099 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
16100 || attr->form == DW_FORM_data8
16101 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
16102}
16103
3690dd37
JB
16104/* Return non-zero if ATTR's value falls in the 'constant' class, or
16105 zero otherwise. When this function returns true, you can apply
16106 dwarf2_get_attr_constant_value to it.
16107
16108 However, note that for some attributes you must check
16109 attr_form_is_section_offset before using this test. DW_FORM_data4
16110 and DW_FORM_data8 are members of both the constant class, and of
16111 the classes that contain offsets into other debug sections
16112 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16113 that, if an attribute's can be either a constant or one of the
16114 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16115 taken as section offsets, not constants. */
380bca97 16116
3690dd37
JB
16117static int
16118attr_form_is_constant (struct attribute *attr)
16119{
16120 switch (attr->form)
16121 {
16122 case DW_FORM_sdata:
16123 case DW_FORM_udata:
16124 case DW_FORM_data1:
16125 case DW_FORM_data2:
16126 case DW_FORM_data4:
16127 case DW_FORM_data8:
16128 return 1;
16129 default:
16130 return 0;
16131 }
16132}
16133
3019eac3
DE
16134/* Return the .debug_loc section to use for CU.
16135 For DWO files use .debug_loc.dwo. */
16136
16137static struct dwarf2_section_info *
16138cu_debug_loc_section (struct dwarf2_cu *cu)
16139{
16140 if (cu->dwo_unit)
16141 return &cu->dwo_unit->dwo_file->sections.loc;
16142 return &dwarf2_per_objfile->loc;
16143}
16144
8cf6f0b1
TT
16145/* A helper function that fills in a dwarf2_loclist_baton. */
16146
16147static void
16148fill_in_loclist_baton (struct dwarf2_cu *cu,
16149 struct dwarf2_loclist_baton *baton,
16150 struct attribute *attr)
16151{
3019eac3
DE
16152 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16153
16154 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
16155
16156 baton->per_cu = cu->per_cu;
16157 gdb_assert (baton->per_cu);
16158 /* We don't know how long the location list is, but make sure we
16159 don't run off the edge of the section. */
3019eac3
DE
16160 baton->size = section->size - DW_UNSND (attr);
16161 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1
TT
16162 baton->base_address = cu->base_address;
16163}
16164
4c2df51b
DJ
16165static void
16166dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 16167 struct dwarf2_cu *cu)
4c2df51b 16168{
bb5ed363 16169 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 16170 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 16171
3690dd37 16172 if (attr_form_is_section_offset (attr)
3019eac3 16173 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
16174 the section. If so, fall through to the complaint in the
16175 other branch. */
3019eac3 16176 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 16177 {
0d53c4c4 16178 struct dwarf2_loclist_baton *baton;
4c2df51b 16179
bb5ed363 16180 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16181 sizeof (struct dwarf2_loclist_baton));
4c2df51b 16182
8cf6f0b1 16183 fill_in_loclist_baton (cu, baton, attr);
be391dca 16184
d00adf39 16185 if (cu->base_known == 0)
0d53c4c4 16186 complaint (&symfile_complaints,
3e43a32a
MS
16187 _("Location list used without "
16188 "specifying the CU base address."));
4c2df51b 16189
768a979c 16190 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
16191 SYMBOL_LOCATION_BATON (sym) = baton;
16192 }
16193 else
16194 {
16195 struct dwarf2_locexpr_baton *baton;
16196
bb5ed363 16197 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16198 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
16199 baton->per_cu = cu->per_cu;
16200 gdb_assert (baton->per_cu);
0d53c4c4
DJ
16201
16202 if (attr_form_is_block (attr))
16203 {
16204 /* Note that we're just copying the block's data pointer
16205 here, not the actual data. We're still pointing into the
6502dd73
DJ
16206 info_buffer for SYM's objfile; right now we never release
16207 that buffer, but when we do clean up properly this may
16208 need to change. */
0d53c4c4
DJ
16209 baton->size = DW_BLOCK (attr)->size;
16210 baton->data = DW_BLOCK (attr)->data;
16211 }
16212 else
16213 {
16214 dwarf2_invalid_attrib_class_complaint ("location description",
16215 SYMBOL_NATURAL_NAME (sym));
16216 baton->size = 0;
0d53c4c4 16217 }
6e70227d 16218
768a979c 16219 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
16220 SYMBOL_LOCATION_BATON (sym) = baton;
16221 }
4c2df51b 16222}
6502dd73 16223
9aa1f1e3
TT
16224/* Return the OBJFILE associated with the compilation unit CU. If CU
16225 came from a separate debuginfo file, then the master objfile is
16226 returned. */
ae0d2f24
UW
16227
16228struct objfile *
16229dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16230{
9291a0cd 16231 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
16232
16233 /* Return the master objfile, so that we can report and look up the
16234 correct file containing this variable. */
16235 if (objfile->separate_debug_objfile_backlink)
16236 objfile = objfile->separate_debug_objfile_backlink;
16237
16238 return objfile;
16239}
16240
96408a79
SA
16241/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16242 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16243 CU_HEADERP first. */
16244
16245static const struct comp_unit_head *
16246per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16247 struct dwarf2_per_cu_data *per_cu)
16248{
16249 struct objfile *objfile;
16250 struct dwarf2_per_objfile *per_objfile;
16251 gdb_byte *info_ptr;
16252
16253 if (per_cu->cu)
16254 return &per_cu->cu->header;
16255
16256 objfile = per_cu->objfile;
16257 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
b64f50a1 16258 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
96408a79
SA
16259
16260 memset (cu_headerp, 0, sizeof (*cu_headerp));
16261 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16262
16263 return cu_headerp;
16264}
16265
ae0d2f24
UW
16266/* Return the address size given in the compilation unit header for CU. */
16267
98714339 16268int
ae0d2f24
UW
16269dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16270{
96408a79
SA
16271 struct comp_unit_head cu_header_local;
16272 const struct comp_unit_head *cu_headerp;
c471e790 16273
96408a79
SA
16274 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16275
16276 return cu_headerp->addr_size;
ae0d2f24
UW
16277}
16278
9eae7c52
TT
16279/* Return the offset size given in the compilation unit header for CU. */
16280
16281int
16282dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16283{
96408a79
SA
16284 struct comp_unit_head cu_header_local;
16285 const struct comp_unit_head *cu_headerp;
9c6c53f7 16286
96408a79
SA
16287 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16288
16289 return cu_headerp->offset_size;
16290}
16291
16292/* See its dwarf2loc.h declaration. */
16293
16294int
16295dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16296{
16297 struct comp_unit_head cu_header_local;
16298 const struct comp_unit_head *cu_headerp;
16299
16300 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16301
16302 if (cu_headerp->version == 2)
16303 return cu_headerp->addr_size;
16304 else
16305 return cu_headerp->offset_size;
181cebd4
JK
16306}
16307
9aa1f1e3
TT
16308/* Return the text offset of the CU. The returned offset comes from
16309 this CU's objfile. If this objfile came from a separate debuginfo
16310 file, then the offset may be different from the corresponding
16311 offset in the parent objfile. */
16312
16313CORE_ADDR
16314dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16315{
bb3fa9d0 16316 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
16317
16318 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16319}
16320
348e048f
DE
16321/* Locate the .debug_info compilation unit from CU's objfile which contains
16322 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
16323
16324static struct dwarf2_per_cu_data *
b64f50a1 16325dwarf2_find_containing_comp_unit (sect_offset offset,
ae038cb0
DJ
16326 struct objfile *objfile)
16327{
16328 struct dwarf2_per_cu_data *this_cu;
16329 int low, high;
16330
ae038cb0
DJ
16331 low = 0;
16332 high = dwarf2_per_objfile->n_comp_units - 1;
16333 while (high > low)
16334 {
16335 int mid = low + (high - low) / 2;
9a619af0 16336
b64f50a1
JK
16337 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16338 >= offset.sect_off)
ae038cb0
DJ
16339 high = mid;
16340 else
16341 low = mid + 1;
16342 }
16343 gdb_assert (low == high);
b64f50a1
JK
16344 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16345 > offset.sect_off)
ae038cb0 16346 {
10b3939b 16347 if (low == 0)
8a3fe4f8
AC
16348 error (_("Dwarf Error: could not find partial DIE containing "
16349 "offset 0x%lx [in module %s]"),
b64f50a1 16350 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 16351
b64f50a1
JK
16352 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16353 <= offset.sect_off);
ae038cb0
DJ
16354 return dwarf2_per_objfile->all_comp_units[low-1];
16355 }
16356 else
16357 {
16358 this_cu = dwarf2_per_objfile->all_comp_units[low];
16359 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
16360 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16361 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16362 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
16363 return this_cu;
16364 }
16365}
16366
23745b47 16367/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 16368
9816fde3 16369static void
23745b47 16370init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 16371{
9816fde3 16372 memset (cu, 0, sizeof (*cu));
23745b47
DE
16373 per_cu->cu = cu;
16374 cu->per_cu = per_cu;
16375 cu->objfile = per_cu->objfile;
93311388 16376 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
16377}
16378
16379/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16380
16381static void
16382prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
16383{
16384 struct attribute *attr;
16385
16386 /* Set the language we're debugging. */
16387 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16388 if (attr)
16389 set_cu_language (DW_UNSND (attr), cu);
16390 else
9cded63f
TT
16391 {
16392 cu->language = language_minimal;
16393 cu->language_defn = language_def (cu->language);
16394 }
dee91e82
DE
16395
16396 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
16397 if (attr)
16398 cu->producer = DW_STRING (attr);
93311388
DE
16399}
16400
ae038cb0
DJ
16401/* Release one cached compilation unit, CU. We unlink it from the tree
16402 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
16403 the caller is responsible for that.
16404 NOTE: DATA is a void * because this function is also used as a
16405 cleanup routine. */
ae038cb0
DJ
16406
16407static void
68dc6402 16408free_heap_comp_unit (void *data)
ae038cb0
DJ
16409{
16410 struct dwarf2_cu *cu = data;
16411
23745b47
DE
16412 gdb_assert (cu->per_cu != NULL);
16413 cu->per_cu->cu = NULL;
ae038cb0
DJ
16414 cu->per_cu = NULL;
16415
16416 obstack_free (&cu->comp_unit_obstack, NULL);
16417
16418 xfree (cu);
16419}
16420
72bf9492 16421/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 16422 when we're finished with it. We can't free the pointer itself, but be
dee91e82 16423 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
16424
16425static void
16426free_stack_comp_unit (void *data)
16427{
16428 struct dwarf2_cu *cu = data;
16429
23745b47
DE
16430 gdb_assert (cu->per_cu != NULL);
16431 cu->per_cu->cu = NULL;
16432 cu->per_cu = NULL;
16433
72bf9492
DJ
16434 obstack_free (&cu->comp_unit_obstack, NULL);
16435 cu->partial_dies = NULL;
ae038cb0
DJ
16436}
16437
16438/* Free all cached compilation units. */
16439
16440static void
16441free_cached_comp_units (void *data)
16442{
16443 struct dwarf2_per_cu_data *per_cu, **last_chain;
16444
16445 per_cu = dwarf2_per_objfile->read_in_chain;
16446 last_chain = &dwarf2_per_objfile->read_in_chain;
16447 while (per_cu != NULL)
16448 {
16449 struct dwarf2_per_cu_data *next_cu;
16450
16451 next_cu = per_cu->cu->read_in_chain;
16452
68dc6402 16453 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16454 *last_chain = next_cu;
16455
16456 per_cu = next_cu;
16457 }
16458}
16459
16460/* Increase the age counter on each cached compilation unit, and free
16461 any that are too old. */
16462
16463static void
16464age_cached_comp_units (void)
16465{
16466 struct dwarf2_per_cu_data *per_cu, **last_chain;
16467
16468 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16469 per_cu = dwarf2_per_objfile->read_in_chain;
16470 while (per_cu != NULL)
16471 {
16472 per_cu->cu->last_used ++;
16473 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16474 dwarf2_mark (per_cu->cu);
16475 per_cu = per_cu->cu->read_in_chain;
16476 }
16477
16478 per_cu = dwarf2_per_objfile->read_in_chain;
16479 last_chain = &dwarf2_per_objfile->read_in_chain;
16480 while (per_cu != NULL)
16481 {
16482 struct dwarf2_per_cu_data *next_cu;
16483
16484 next_cu = per_cu->cu->read_in_chain;
16485
16486 if (!per_cu->cu->mark)
16487 {
68dc6402 16488 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16489 *last_chain = next_cu;
16490 }
16491 else
16492 last_chain = &per_cu->cu->read_in_chain;
16493
16494 per_cu = next_cu;
16495 }
16496}
16497
16498/* Remove a single compilation unit from the cache. */
16499
16500static void
dee91e82 16501free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
16502{
16503 struct dwarf2_per_cu_data *per_cu, **last_chain;
16504
16505 per_cu = dwarf2_per_objfile->read_in_chain;
16506 last_chain = &dwarf2_per_objfile->read_in_chain;
16507 while (per_cu != NULL)
16508 {
16509 struct dwarf2_per_cu_data *next_cu;
16510
16511 next_cu = per_cu->cu->read_in_chain;
16512
dee91e82 16513 if (per_cu == target_per_cu)
ae038cb0 16514 {
68dc6402 16515 free_heap_comp_unit (per_cu->cu);
dee91e82 16516 per_cu->cu = NULL;
ae038cb0
DJ
16517 *last_chain = next_cu;
16518 break;
16519 }
16520 else
16521 last_chain = &per_cu->cu->read_in_chain;
16522
16523 per_cu = next_cu;
16524 }
16525}
16526
fe3e1990
DJ
16527/* Release all extra memory associated with OBJFILE. */
16528
16529void
16530dwarf2_free_objfile (struct objfile *objfile)
16531{
16532 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16533
16534 if (dwarf2_per_objfile == NULL)
16535 return;
16536
16537 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16538 free_cached_comp_units (NULL);
16539
7b9f3c50
DE
16540 if (dwarf2_per_objfile->quick_file_names_table)
16541 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16542
fe3e1990
DJ
16543 /* Everything else should be on the objfile obstack. */
16544}
16545
dee91e82
DE
16546/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
16547 We store these in a hash table separate from the DIEs, and preserve them
16548 when the DIEs are flushed out of cache.
16549
16550 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
16551 uniquely identify the type. A file may have multiple .debug_types sections,
16552 or the type may come from a DWO file. We have to use something in
16553 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
16554 routine, get_die_type_at_offset, from outside this file, and thus won't
16555 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
16556 of the objfile. */
1c379e20 16557
dee91e82 16558struct dwarf2_per_cu_offset_and_type
1c379e20 16559{
dee91e82 16560 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 16561 sect_offset offset;
1c379e20
DJ
16562 struct type *type;
16563};
16564
dee91e82 16565/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
16566
16567static hashval_t
dee91e82 16568per_cu_offset_and_type_hash (const void *item)
1c379e20 16569{
dee91e82 16570 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 16571
dee91e82 16572 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
16573}
16574
dee91e82 16575/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
16576
16577static int
dee91e82 16578per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 16579{
dee91e82
DE
16580 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
16581 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16582
dee91e82
DE
16583 return (ofs_lhs->per_cu == ofs_rhs->per_cu
16584 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
16585}
16586
16587/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16588 table if necessary. For convenience, return TYPE.
16589
16590 The DIEs reading must have careful ordering to:
16591 * Not cause infite loops trying to read in DIEs as a prerequisite for
16592 reading current DIE.
16593 * Not trying to dereference contents of still incompletely read in types
16594 while reading in other DIEs.
16595 * Enable referencing still incompletely read in types just by a pointer to
16596 the type without accessing its fields.
16597
16598 Therefore caller should follow these rules:
16599 * Try to fetch any prerequisite types we may need to build this DIE type
16600 before building the type and calling set_die_type.
e71ec853 16601 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16602 possible before fetching more types to complete the current type.
16603 * Make the type as complete as possible before fetching more types. */
1c379e20 16604
f792889a 16605static struct type *
1c379e20
DJ
16606set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16607{
dee91e82 16608 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 16609 struct objfile *objfile = cu->objfile;
1c379e20 16610
b4ba55a1
JB
16611 /* For Ada types, make sure that the gnat-specific data is always
16612 initialized (if not already set). There are a few types where
16613 we should not be doing so, because the type-specific area is
16614 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16615 where the type-specific area is used to store the floatformat).
16616 But this is not a problem, because the gnat-specific information
16617 is actually not needed for these types. */
16618 if (need_gnat_info (cu)
16619 && TYPE_CODE (type) != TYPE_CODE_FUNC
16620 && TYPE_CODE (type) != TYPE_CODE_FLT
16621 && !HAVE_GNAT_AUX_INFO (type))
16622 INIT_GNAT_SPECIFIC (type);
16623
dee91e82 16624 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 16625 {
dee91e82
DE
16626 dwarf2_per_objfile->die_type_hash =
16627 htab_create_alloc_ex (127,
16628 per_cu_offset_and_type_hash,
16629 per_cu_offset_and_type_eq,
16630 NULL,
16631 &objfile->objfile_obstack,
16632 hashtab_obstack_allocate,
16633 dummy_obstack_deallocate);
f792889a 16634 }
1c379e20 16635
dee91e82 16636 ofs.per_cu = cu->per_cu;
1c379e20
DJ
16637 ofs.offset = die->offset;
16638 ofs.type = type;
dee91e82
DE
16639 slot = (struct dwarf2_per_cu_offset_and_type **)
16640 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
16641 if (*slot)
16642 complaint (&symfile_complaints,
16643 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 16644 die->offset.sect_off);
673bfd45 16645 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16646 **slot = ofs;
f792889a 16647 return type;
1c379e20
DJ
16648}
16649
380bca97 16650/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 16651 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16652
16653static struct type *
b64f50a1 16654get_die_type_at_offset (sect_offset offset,
673bfd45 16655 struct dwarf2_per_cu_data *per_cu)
1c379e20 16656{
dee91e82 16657 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 16658
dee91e82 16659 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 16660 return NULL;
1c379e20 16661
dee91e82 16662 ofs.per_cu = per_cu;
673bfd45 16663 ofs.offset = offset;
dee91e82 16664 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
16665 if (slot)
16666 return slot->type;
16667 else
16668 return NULL;
16669}
16670
673bfd45
DE
16671/* Look up the type for DIE in the appropriate type_hash table,
16672 or return NULL if DIE does not have a saved type. */
16673
16674static struct type *
16675get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16676{
16677 return get_die_type_at_offset (die->offset, cu->per_cu);
16678}
16679
10b3939b
DJ
16680/* Add a dependence relationship from CU to REF_PER_CU. */
16681
16682static void
16683dwarf2_add_dependence (struct dwarf2_cu *cu,
16684 struct dwarf2_per_cu_data *ref_per_cu)
16685{
16686 void **slot;
16687
16688 if (cu->dependencies == NULL)
16689 cu->dependencies
16690 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16691 NULL, &cu->comp_unit_obstack,
16692 hashtab_obstack_allocate,
16693 dummy_obstack_deallocate);
16694
16695 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16696 if (*slot == NULL)
16697 *slot = ref_per_cu;
16698}
1c379e20 16699
f504f079
DE
16700/* Subroutine of dwarf2_mark to pass to htab_traverse.
16701 Set the mark field in every compilation unit in the
ae038cb0
DJ
16702 cache that we must keep because we are keeping CU. */
16703
10b3939b
DJ
16704static int
16705dwarf2_mark_helper (void **slot, void *data)
16706{
16707 struct dwarf2_per_cu_data *per_cu;
16708
16709 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16710
16711 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16712 reading of the chain. As such dependencies remain valid it is not much
16713 useful to track and undo them during QUIT cleanups. */
16714 if (per_cu->cu == NULL)
16715 return 1;
16716
10b3939b
DJ
16717 if (per_cu->cu->mark)
16718 return 1;
16719 per_cu->cu->mark = 1;
16720
16721 if (per_cu->cu->dependencies != NULL)
16722 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16723
16724 return 1;
16725}
16726
f504f079
DE
16727/* Set the mark field in CU and in every other compilation unit in the
16728 cache that we must keep because we are keeping CU. */
16729
ae038cb0
DJ
16730static void
16731dwarf2_mark (struct dwarf2_cu *cu)
16732{
16733 if (cu->mark)
16734 return;
16735 cu->mark = 1;
10b3939b
DJ
16736 if (cu->dependencies != NULL)
16737 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
16738}
16739
16740static void
16741dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16742{
16743 while (per_cu)
16744 {
16745 per_cu->cu->mark = 0;
16746 per_cu = per_cu->cu->read_in_chain;
16747 }
72bf9492
DJ
16748}
16749
72bf9492
DJ
16750/* Trivial hash function for partial_die_info: the hash value of a DIE
16751 is its offset in .debug_info for this objfile. */
16752
16753static hashval_t
16754partial_die_hash (const void *item)
16755{
16756 const struct partial_die_info *part_die = item;
9a619af0 16757
b64f50a1 16758 return part_die->offset.sect_off;
72bf9492
DJ
16759}
16760
16761/* Trivial comparison function for partial_die_info structures: two DIEs
16762 are equal if they have the same offset. */
16763
16764static int
16765partial_die_eq (const void *item_lhs, const void *item_rhs)
16766{
16767 const struct partial_die_info *part_die_lhs = item_lhs;
16768 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 16769
b64f50a1 16770 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
16771}
16772
ae038cb0
DJ
16773static struct cmd_list_element *set_dwarf2_cmdlist;
16774static struct cmd_list_element *show_dwarf2_cmdlist;
16775
16776static void
16777set_dwarf2_cmd (char *args, int from_tty)
16778{
16779 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16780}
16781
16782static void
16783show_dwarf2_cmd (char *args, int from_tty)
6e70227d 16784{
ae038cb0
DJ
16785 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16786}
16787
dce234bc
PP
16788/* If section described by INFO was mmapped, munmap it now. */
16789
16790static void
16791munmap_section_buffer (struct dwarf2_section_info *info)
16792{
b315ab21 16793 if (info->map_addr != NULL)
dce234bc
PP
16794 {
16795#ifdef HAVE_MMAP
b315ab21 16796 int res;
9a619af0 16797
b315ab21
TG
16798 res = munmap (info->map_addr, info->map_len);
16799 gdb_assert (res == 0);
dce234bc
PP
16800#else
16801 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 16802 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
16803#endif
16804 }
16805}
16806
16807/* munmap debug sections for OBJFILE, if necessary. */
16808
16809static void
c1bd65d0 16810dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
16811{
16812 struct dwarf2_per_objfile *data = d;
8b70b953
TT
16813 int ix;
16814 struct dwarf2_section_info *section;
9a619af0 16815
16be1145
DE
16816 /* This is sorted according to the order they're defined in to make it easier
16817 to keep in sync. */
dce234bc
PP
16818 munmap_section_buffer (&data->info);
16819 munmap_section_buffer (&data->abbrev);
16820 munmap_section_buffer (&data->line);
16be1145 16821 munmap_section_buffer (&data->loc);
dce234bc 16822 munmap_section_buffer (&data->macinfo);
cf2c3c16 16823 munmap_section_buffer (&data->macro);
16be1145 16824 munmap_section_buffer (&data->str);
dce234bc 16825 munmap_section_buffer (&data->ranges);
3019eac3 16826 munmap_section_buffer (&data->addr);
dce234bc
PP
16827 munmap_section_buffer (&data->frame);
16828 munmap_section_buffer (&data->eh_frame);
9291a0cd 16829 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
16830
16831 for (ix = 0;
16832 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16833 ++ix)
16834 munmap_section_buffer (section);
16835
16836 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
16837
16838 if (data->dwo_files)
16839 free_dwo_files (data->dwo_files, objfile);
9291a0cd
TT
16840}
16841
16842\f
ae2de4f8 16843/* The "save gdb-index" command. */
9291a0cd
TT
16844
16845/* The contents of the hash table we create when building the string
16846 table. */
16847struct strtab_entry
16848{
16849 offset_type offset;
16850 const char *str;
16851};
16852
559a7a62
JK
16853/* Hash function for a strtab_entry.
16854
16855 Function is used only during write_hash_table so no index format backward
16856 compatibility is needed. */
b89be57b 16857
9291a0cd
TT
16858static hashval_t
16859hash_strtab_entry (const void *e)
16860{
16861 const struct strtab_entry *entry = e;
559a7a62 16862 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
16863}
16864
16865/* Equality function for a strtab_entry. */
b89be57b 16866
9291a0cd
TT
16867static int
16868eq_strtab_entry (const void *a, const void *b)
16869{
16870 const struct strtab_entry *ea = a;
16871 const struct strtab_entry *eb = b;
16872 return !strcmp (ea->str, eb->str);
16873}
16874
16875/* Create a strtab_entry hash table. */
b89be57b 16876
9291a0cd
TT
16877static htab_t
16878create_strtab (void)
16879{
16880 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16881 xfree, xcalloc, xfree);
16882}
16883
16884/* Add a string to the constant pool. Return the string's offset in
16885 host order. */
b89be57b 16886
9291a0cd
TT
16887static offset_type
16888add_string (htab_t table, struct obstack *cpool, const char *str)
16889{
16890 void **slot;
16891 struct strtab_entry entry;
16892 struct strtab_entry *result;
16893
16894 entry.str = str;
16895 slot = htab_find_slot (table, &entry, INSERT);
16896 if (*slot)
16897 result = *slot;
16898 else
16899 {
16900 result = XNEW (struct strtab_entry);
16901 result->offset = obstack_object_size (cpool);
16902 result->str = str;
16903 obstack_grow_str0 (cpool, str);
16904 *slot = result;
16905 }
16906 return result->offset;
16907}
16908
16909/* An entry in the symbol table. */
16910struct symtab_index_entry
16911{
16912 /* The name of the symbol. */
16913 const char *name;
16914 /* The offset of the name in the constant pool. */
16915 offset_type index_offset;
16916 /* A sorted vector of the indices of all the CUs that hold an object
16917 of this name. */
16918 VEC (offset_type) *cu_indices;
16919};
16920
16921/* The symbol table. This is a power-of-2-sized hash table. */
16922struct mapped_symtab
16923{
16924 offset_type n_elements;
16925 offset_type size;
16926 struct symtab_index_entry **data;
16927};
16928
16929/* Hash function for a symtab_index_entry. */
b89be57b 16930
9291a0cd
TT
16931static hashval_t
16932hash_symtab_entry (const void *e)
16933{
16934 const struct symtab_index_entry *entry = e;
16935 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16936 sizeof (offset_type) * VEC_length (offset_type,
16937 entry->cu_indices),
16938 0);
16939}
16940
16941/* Equality function for a symtab_index_entry. */
b89be57b 16942
9291a0cd
TT
16943static int
16944eq_symtab_entry (const void *a, const void *b)
16945{
16946 const struct symtab_index_entry *ea = a;
16947 const struct symtab_index_entry *eb = b;
16948 int len = VEC_length (offset_type, ea->cu_indices);
16949 if (len != VEC_length (offset_type, eb->cu_indices))
16950 return 0;
16951 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16952 VEC_address (offset_type, eb->cu_indices),
16953 sizeof (offset_type) * len);
16954}
16955
16956/* Destroy a symtab_index_entry. */
b89be57b 16957
9291a0cd
TT
16958static void
16959delete_symtab_entry (void *p)
16960{
16961 struct symtab_index_entry *entry = p;
16962 VEC_free (offset_type, entry->cu_indices);
16963 xfree (entry);
16964}
16965
16966/* Create a hash table holding symtab_index_entry objects. */
b89be57b 16967
9291a0cd 16968static htab_t
3876f04e 16969create_symbol_hash_table (void)
9291a0cd
TT
16970{
16971 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16972 delete_symtab_entry, xcalloc, xfree);
16973}
16974
16975/* Create a new mapped symtab object. */
b89be57b 16976
9291a0cd
TT
16977static struct mapped_symtab *
16978create_mapped_symtab (void)
16979{
16980 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16981 symtab->n_elements = 0;
16982 symtab->size = 1024;
16983 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16984 return symtab;
16985}
16986
16987/* Destroy a mapped_symtab. */
b89be57b 16988
9291a0cd
TT
16989static void
16990cleanup_mapped_symtab (void *p)
16991{
16992 struct mapped_symtab *symtab = p;
16993 /* The contents of the array are freed when the other hash table is
16994 destroyed. */
16995 xfree (symtab->data);
16996 xfree (symtab);
16997}
16998
16999/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
17000 the slot.
17001
17002 Function is used only during write_hash_table so no index format backward
17003 compatibility is needed. */
b89be57b 17004
9291a0cd
TT
17005static struct symtab_index_entry **
17006find_slot (struct mapped_symtab *symtab, const char *name)
17007{
559a7a62 17008 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
17009
17010 index = hash & (symtab->size - 1);
17011 step = ((hash * 17) & (symtab->size - 1)) | 1;
17012
17013 for (;;)
17014 {
17015 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17016 return &symtab->data[index];
17017 index = (index + step) & (symtab->size - 1);
17018 }
17019}
17020
17021/* Expand SYMTAB's hash table. */
b89be57b 17022
9291a0cd
TT
17023static void
17024hash_expand (struct mapped_symtab *symtab)
17025{
17026 offset_type old_size = symtab->size;
17027 offset_type i;
17028 struct symtab_index_entry **old_entries = symtab->data;
17029
17030 symtab->size *= 2;
17031 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17032
17033 for (i = 0; i < old_size; ++i)
17034 {
17035 if (old_entries[i])
17036 {
17037 struct symtab_index_entry **slot = find_slot (symtab,
17038 old_entries[i]->name);
17039 *slot = old_entries[i];
17040 }
17041 }
17042
17043 xfree (old_entries);
17044}
17045
17046/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
17047 is the index of the CU in which the symbol appears. */
b89be57b 17048
9291a0cd
TT
17049static void
17050add_index_entry (struct mapped_symtab *symtab, const char *name,
17051 offset_type cu_index)
17052{
17053 struct symtab_index_entry **slot;
17054
17055 ++symtab->n_elements;
17056 if (4 * symtab->n_elements / 3 >= symtab->size)
17057 hash_expand (symtab);
17058
17059 slot = find_slot (symtab, name);
17060 if (!*slot)
17061 {
17062 *slot = XNEW (struct symtab_index_entry);
17063 (*slot)->name = name;
17064 (*slot)->cu_indices = NULL;
17065 }
17066 /* Don't push an index twice. Due to how we add entries we only
17067 have to check the last one. */
17068 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 17069 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
17070 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
17071}
17072
17073/* Add a vector of indices to the constant pool. */
b89be57b 17074
9291a0cd 17075static offset_type
3876f04e 17076add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
17077 struct symtab_index_entry *entry)
17078{
17079 void **slot;
17080
3876f04e 17081 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
17082 if (!*slot)
17083 {
17084 offset_type len = VEC_length (offset_type, entry->cu_indices);
17085 offset_type val = MAYBE_SWAP (len);
17086 offset_type iter;
17087 int i;
17088
17089 *slot = entry;
17090 entry->index_offset = obstack_object_size (cpool);
17091
17092 obstack_grow (cpool, &val, sizeof (val));
17093 for (i = 0;
17094 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17095 ++i)
17096 {
17097 val = MAYBE_SWAP (iter);
17098 obstack_grow (cpool, &val, sizeof (val));
17099 }
17100 }
17101 else
17102 {
17103 struct symtab_index_entry *old_entry = *slot;
17104 entry->index_offset = old_entry->index_offset;
17105 entry = old_entry;
17106 }
17107 return entry->index_offset;
17108}
17109
17110/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17111 constant pool entries going into the obstack CPOOL. */
b89be57b 17112
9291a0cd
TT
17113static void
17114write_hash_table (struct mapped_symtab *symtab,
17115 struct obstack *output, struct obstack *cpool)
17116{
17117 offset_type i;
3876f04e 17118 htab_t symbol_hash_table;
9291a0cd
TT
17119 htab_t str_table;
17120
3876f04e 17121 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 17122 str_table = create_strtab ();
3876f04e 17123
9291a0cd
TT
17124 /* We add all the index vectors to the constant pool first, to
17125 ensure alignment is ok. */
17126 for (i = 0; i < symtab->size; ++i)
17127 {
17128 if (symtab->data[i])
3876f04e 17129 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
17130 }
17131
17132 /* Now write out the hash table. */
17133 for (i = 0; i < symtab->size; ++i)
17134 {
17135 offset_type str_off, vec_off;
17136
17137 if (symtab->data[i])
17138 {
17139 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17140 vec_off = symtab->data[i]->index_offset;
17141 }
17142 else
17143 {
17144 /* While 0 is a valid constant pool index, it is not valid
17145 to have 0 for both offsets. */
17146 str_off = 0;
17147 vec_off = 0;
17148 }
17149
17150 str_off = MAYBE_SWAP (str_off);
17151 vec_off = MAYBE_SWAP (vec_off);
17152
17153 obstack_grow (output, &str_off, sizeof (str_off));
17154 obstack_grow (output, &vec_off, sizeof (vec_off));
17155 }
17156
17157 htab_delete (str_table);
3876f04e 17158 htab_delete (symbol_hash_table);
9291a0cd
TT
17159}
17160
0a5429f6
DE
17161/* Struct to map psymtab to CU index in the index file. */
17162struct psymtab_cu_index_map
17163{
17164 struct partial_symtab *psymtab;
17165 unsigned int cu_index;
17166};
17167
17168static hashval_t
17169hash_psymtab_cu_index (const void *item)
17170{
17171 const struct psymtab_cu_index_map *map = item;
17172
17173 return htab_hash_pointer (map->psymtab);
17174}
17175
17176static int
17177eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17178{
17179 const struct psymtab_cu_index_map *lhs = item_lhs;
17180 const struct psymtab_cu_index_map *rhs = item_rhs;
17181
17182 return lhs->psymtab == rhs->psymtab;
17183}
17184
17185/* Helper struct for building the address table. */
17186struct addrmap_index_data
17187{
17188 struct objfile *objfile;
17189 struct obstack *addr_obstack;
17190 htab_t cu_index_htab;
17191
17192 /* Non-zero if the previous_* fields are valid.
17193 We can't write an entry until we see the next entry (since it is only then
17194 that we know the end of the entry). */
17195 int previous_valid;
17196 /* Index of the CU in the table of all CUs in the index file. */
17197 unsigned int previous_cu_index;
0963b4bd 17198 /* Start address of the CU. */
0a5429f6
DE
17199 CORE_ADDR previous_cu_start;
17200};
17201
17202/* Write an address entry to OBSTACK. */
b89be57b 17203
9291a0cd 17204static void
0a5429f6
DE
17205add_address_entry (struct objfile *objfile, struct obstack *obstack,
17206 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 17207{
0a5429f6 17208 offset_type cu_index_to_write;
9291a0cd
TT
17209 char addr[8];
17210 CORE_ADDR baseaddr;
17211
17212 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17213
0a5429f6
DE
17214 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17215 obstack_grow (obstack, addr, 8);
17216 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17217 obstack_grow (obstack, addr, 8);
17218 cu_index_to_write = MAYBE_SWAP (cu_index);
17219 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17220}
17221
17222/* Worker function for traversing an addrmap to build the address table. */
17223
17224static int
17225add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17226{
17227 struct addrmap_index_data *data = datap;
17228 struct partial_symtab *pst = obj;
17229 offset_type cu_index;
17230 void **slot;
17231
17232 if (data->previous_valid)
17233 add_address_entry (data->objfile, data->addr_obstack,
17234 data->previous_cu_start, start_addr,
17235 data->previous_cu_index);
17236
17237 data->previous_cu_start = start_addr;
17238 if (pst != NULL)
17239 {
17240 struct psymtab_cu_index_map find_map, *map;
17241 find_map.psymtab = pst;
17242 map = htab_find (data->cu_index_htab, &find_map);
17243 gdb_assert (map != NULL);
17244 data->previous_cu_index = map->cu_index;
17245 data->previous_valid = 1;
17246 }
17247 else
17248 data->previous_valid = 0;
17249
17250 return 0;
17251}
17252
17253/* Write OBJFILE's address map to OBSTACK.
17254 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17255 in the index file. */
17256
17257static void
17258write_address_map (struct objfile *objfile, struct obstack *obstack,
17259 htab_t cu_index_htab)
17260{
17261 struct addrmap_index_data addrmap_index_data;
17262
17263 /* When writing the address table, we have to cope with the fact that
17264 the addrmap iterator only provides the start of a region; we have to
17265 wait until the next invocation to get the start of the next region. */
17266
17267 addrmap_index_data.objfile = objfile;
17268 addrmap_index_data.addr_obstack = obstack;
17269 addrmap_index_data.cu_index_htab = cu_index_htab;
17270 addrmap_index_data.previous_valid = 0;
17271
17272 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17273 &addrmap_index_data);
17274
17275 /* It's highly unlikely the last entry (end address = 0xff...ff)
17276 is valid, but we should still handle it.
17277 The end address is recorded as the start of the next region, but that
17278 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17279 anyway. */
17280 if (addrmap_index_data.previous_valid)
17281 add_address_entry (objfile, obstack,
17282 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17283 addrmap_index_data.previous_cu_index);
9291a0cd
TT
17284}
17285
17286/* Add a list of partial symbols to SYMTAB. */
b89be57b 17287
9291a0cd
TT
17288static void
17289write_psymbols (struct mapped_symtab *symtab,
987d643c 17290 htab_t psyms_seen,
9291a0cd
TT
17291 struct partial_symbol **psymp,
17292 int count,
987d643c
TT
17293 offset_type cu_index,
17294 int is_static)
9291a0cd
TT
17295{
17296 for (; count-- > 0; ++psymp)
17297 {
987d643c
TT
17298 void **slot, *lookup;
17299
9291a0cd
TT
17300 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
17301 error (_("Ada is not currently supported by the index"));
987d643c
TT
17302
17303 /* We only want to add a given psymbol once. However, we also
17304 want to account for whether it is global or static. So, we
17305 may add it twice, using slightly different values. */
17306 if (is_static)
17307 {
17308 uintptr_t val = 1 | (uintptr_t) *psymp;
17309
17310 lookup = (void *) val;
17311 }
17312 else
17313 lookup = *psymp;
17314
17315 /* Only add a given psymbol once. */
17316 slot = htab_find_slot (psyms_seen, lookup, INSERT);
17317 if (!*slot)
17318 {
17319 *slot = lookup;
bb2f58dc 17320 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 17321 }
9291a0cd
TT
17322 }
17323}
17324
17325/* Write the contents of an ("unfinished") obstack to FILE. Throw an
17326 exception if there is an error. */
b89be57b 17327
9291a0cd
TT
17328static void
17329write_obstack (FILE *file, struct obstack *obstack)
17330{
17331 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17332 file)
17333 != obstack_object_size (obstack))
17334 error (_("couldn't data write to file"));
17335}
17336
17337/* Unlink a file if the argument is not NULL. */
b89be57b 17338
9291a0cd
TT
17339static void
17340unlink_if_set (void *p)
17341{
17342 char **filename = p;
17343 if (*filename)
17344 unlink (*filename);
17345}
17346
1fd400ff
TT
17347/* A helper struct used when iterating over debug_types. */
17348struct signatured_type_index_data
17349{
17350 struct objfile *objfile;
17351 struct mapped_symtab *symtab;
17352 struct obstack *types_list;
987d643c 17353 htab_t psyms_seen;
1fd400ff
TT
17354 int cu_index;
17355};
17356
17357/* A helper function that writes a single signatured_type to an
17358 obstack. */
b89be57b 17359
1fd400ff
TT
17360static int
17361write_one_signatured_type (void **slot, void *d)
17362{
17363 struct signatured_type_index_data *info = d;
17364 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
17365 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17366 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
17367 gdb_byte val[8];
17368
17369 write_psymbols (info->symtab,
987d643c 17370 info->psyms_seen,
3e43a32a
MS
17371 info->objfile->global_psymbols.list
17372 + psymtab->globals_offset,
987d643c
TT
17373 psymtab->n_global_syms, info->cu_index,
17374 0);
1fd400ff 17375 write_psymbols (info->symtab,
987d643c 17376 info->psyms_seen,
3e43a32a
MS
17377 info->objfile->static_psymbols.list
17378 + psymtab->statics_offset,
987d643c
TT
17379 psymtab->n_static_syms, info->cu_index,
17380 1);
1fd400ff 17381
b64f50a1
JK
17382 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17383 entry->per_cu.offset.sect_off);
1fd400ff 17384 obstack_grow (info->types_list, val, 8);
3019eac3
DE
17385 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17386 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
17387 obstack_grow (info->types_list, val, 8);
17388 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17389 obstack_grow (info->types_list, val, 8);
17390
17391 ++info->cu_index;
17392
17393 return 1;
17394}
17395
9291a0cd 17396/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 17397
9291a0cd
TT
17398static void
17399write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17400{
17401 struct cleanup *cleanup;
17402 char *filename, *cleanup_filename;
1fd400ff
TT
17403 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
17404 struct obstack cu_list, types_cu_list;
9291a0cd
TT
17405 int i;
17406 FILE *out_file;
17407 struct mapped_symtab *symtab;
17408 offset_type val, size_of_contents, total_len;
17409 struct stat st;
17410 char buf[8];
987d643c 17411 htab_t psyms_seen;
0a5429f6
DE
17412 htab_t cu_index_htab;
17413 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 17414
b4f2f049 17415 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 17416 return;
b4f2f049 17417
9291a0cd
TT
17418 if (dwarf2_per_objfile->using_index)
17419 error (_("Cannot use an index to create the index"));
17420
8b70b953
TT
17421 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17422 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17423
9291a0cd 17424 if (stat (objfile->name, &st) < 0)
7e17e088 17425 perror_with_name (objfile->name);
9291a0cd
TT
17426
17427 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17428 INDEX_SUFFIX, (char *) NULL);
17429 cleanup = make_cleanup (xfree, filename);
17430
17431 out_file = fopen (filename, "wb");
17432 if (!out_file)
17433 error (_("Can't open `%s' for writing"), filename);
17434
17435 cleanup_filename = filename;
17436 make_cleanup (unlink_if_set, &cleanup_filename);
17437
17438 symtab = create_mapped_symtab ();
17439 make_cleanup (cleanup_mapped_symtab, symtab);
17440
17441 obstack_init (&addr_obstack);
17442 make_cleanup_obstack_free (&addr_obstack);
17443
17444 obstack_init (&cu_list);
17445 make_cleanup_obstack_free (&cu_list);
17446
1fd400ff
TT
17447 obstack_init (&types_cu_list);
17448 make_cleanup_obstack_free (&types_cu_list);
17449
987d643c
TT
17450 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17451 NULL, xcalloc, xfree);
96408a79 17452 make_cleanup_htab_delete (psyms_seen);
987d643c 17453
0a5429f6
DE
17454 /* While we're scanning CU's create a table that maps a psymtab pointer
17455 (which is what addrmap records) to its index (which is what is recorded
17456 in the index file). This will later be needed to write the address
17457 table. */
17458 cu_index_htab = htab_create_alloc (100,
17459 hash_psymtab_cu_index,
17460 eq_psymtab_cu_index,
17461 NULL, xcalloc, xfree);
96408a79 17462 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
17463 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17464 xmalloc (sizeof (struct psymtab_cu_index_map)
17465 * dwarf2_per_objfile->n_comp_units);
17466 make_cleanup (xfree, psymtab_cu_index_map);
17467
17468 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
17469 work here. Also, the debug_types entries do not appear in
17470 all_comp_units, but only in their own hash table. */
9291a0cd
TT
17471 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17472 {
3e43a32a
MS
17473 struct dwarf2_per_cu_data *per_cu
17474 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 17475 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 17476 gdb_byte val[8];
0a5429f6
DE
17477 struct psymtab_cu_index_map *map;
17478 void **slot;
9291a0cd
TT
17479
17480 write_psymbols (symtab,
987d643c 17481 psyms_seen,
9291a0cd 17482 objfile->global_psymbols.list + psymtab->globals_offset,
987d643c
TT
17483 psymtab->n_global_syms, i,
17484 0);
9291a0cd 17485 write_psymbols (symtab,
987d643c 17486 psyms_seen,
9291a0cd 17487 objfile->static_psymbols.list + psymtab->statics_offset,
987d643c
TT
17488 psymtab->n_static_syms, i,
17489 1);
9291a0cd 17490
0a5429f6
DE
17491 map = &psymtab_cu_index_map[i];
17492 map->psymtab = psymtab;
17493 map->cu_index = i;
17494 slot = htab_find_slot (cu_index_htab, map, INSERT);
17495 gdb_assert (slot != NULL);
17496 gdb_assert (*slot == NULL);
17497 *slot = map;
9291a0cd 17498
b64f50a1
JK
17499 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17500 per_cu->offset.sect_off);
9291a0cd 17501 obstack_grow (&cu_list, val, 8);
e254ef6a 17502 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
17503 obstack_grow (&cu_list, val, 8);
17504 }
17505
0a5429f6
DE
17506 /* Dump the address map. */
17507 write_address_map (objfile, &addr_obstack, cu_index_htab);
17508
1fd400ff
TT
17509 /* Write out the .debug_type entries, if any. */
17510 if (dwarf2_per_objfile->signatured_types)
17511 {
17512 struct signatured_type_index_data sig_data;
17513
17514 sig_data.objfile = objfile;
17515 sig_data.symtab = symtab;
17516 sig_data.types_list = &types_cu_list;
987d643c 17517 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17518 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17519 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17520 write_one_signatured_type, &sig_data);
17521 }
17522
9291a0cd
TT
17523 obstack_init (&constant_pool);
17524 make_cleanup_obstack_free (&constant_pool);
17525 obstack_init (&symtab_obstack);
17526 make_cleanup_obstack_free (&symtab_obstack);
17527 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17528
17529 obstack_init (&contents);
17530 make_cleanup_obstack_free (&contents);
1fd400ff 17531 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17532 total_len = size_of_contents;
17533
17534 /* The version number. */
481860b3 17535 val = MAYBE_SWAP (6);
9291a0cd
TT
17536 obstack_grow (&contents, &val, sizeof (val));
17537
17538 /* The offset of the CU list from the start of the file. */
17539 val = MAYBE_SWAP (total_len);
17540 obstack_grow (&contents, &val, sizeof (val));
17541 total_len += obstack_object_size (&cu_list);
17542
1fd400ff
TT
17543 /* The offset of the types CU list from the start of the file. */
17544 val = MAYBE_SWAP (total_len);
17545 obstack_grow (&contents, &val, sizeof (val));
17546 total_len += obstack_object_size (&types_cu_list);
17547
9291a0cd
TT
17548 /* The offset of the address table from the start of the file. */
17549 val = MAYBE_SWAP (total_len);
17550 obstack_grow (&contents, &val, sizeof (val));
17551 total_len += obstack_object_size (&addr_obstack);
17552
17553 /* The offset of the symbol table from the start of the file. */
17554 val = MAYBE_SWAP (total_len);
17555 obstack_grow (&contents, &val, sizeof (val));
17556 total_len += obstack_object_size (&symtab_obstack);
17557
17558 /* The offset of the constant pool from the start of the file. */
17559 val = MAYBE_SWAP (total_len);
17560 obstack_grow (&contents, &val, sizeof (val));
17561 total_len += obstack_object_size (&constant_pool);
17562
17563 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17564
17565 write_obstack (out_file, &contents);
17566 write_obstack (out_file, &cu_list);
1fd400ff 17567 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17568 write_obstack (out_file, &addr_obstack);
17569 write_obstack (out_file, &symtab_obstack);
17570 write_obstack (out_file, &constant_pool);
17571
17572 fclose (out_file);
17573
17574 /* We want to keep the file, so we set cleanup_filename to NULL
17575 here. See unlink_if_set. */
17576 cleanup_filename = NULL;
17577
17578 do_cleanups (cleanup);
17579}
17580
90476074
TT
17581/* Implementation of the `save gdb-index' command.
17582
17583 Note that the file format used by this command is documented in the
17584 GDB manual. Any changes here must be documented there. */
11570e71 17585
9291a0cd
TT
17586static void
17587save_gdb_index_command (char *arg, int from_tty)
17588{
17589 struct objfile *objfile;
17590
17591 if (!arg || !*arg)
96d19272 17592 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17593
17594 ALL_OBJFILES (objfile)
17595 {
17596 struct stat st;
17597
17598 /* If the objfile does not correspond to an actual file, skip it. */
17599 if (stat (objfile->name, &st) < 0)
17600 continue;
17601
17602 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17603 if (dwarf2_per_objfile)
17604 {
17605 volatile struct gdb_exception except;
17606
17607 TRY_CATCH (except, RETURN_MASK_ERROR)
17608 {
17609 write_psymtabs_to_index (objfile, arg);
17610 }
17611 if (except.reason < 0)
17612 exception_fprintf (gdb_stderr, except,
17613 _("Error while writing index for `%s': "),
17614 objfile->name);
17615 }
17616 }
dce234bc
PP
17617}
17618
9291a0cd
TT
17619\f
17620
9eae7c52
TT
17621int dwarf2_always_disassemble;
17622
17623static void
17624show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17625 struct cmd_list_element *c, const char *value)
17626{
3e43a32a
MS
17627 fprintf_filtered (file,
17628 _("Whether to always disassemble "
17629 "DWARF expressions is %s.\n"),
9eae7c52
TT
17630 value);
17631}
17632
900e11f9
JK
17633static void
17634show_check_physname (struct ui_file *file, int from_tty,
17635 struct cmd_list_element *c, const char *value)
17636{
17637 fprintf_filtered (file,
17638 _("Whether to check \"physname\" is %s.\n"),
17639 value);
17640}
17641
6502dd73
DJ
17642void _initialize_dwarf2_read (void);
17643
17644void
17645_initialize_dwarf2_read (void)
17646{
96d19272
JK
17647 struct cmd_list_element *c;
17648
dce234bc 17649 dwarf2_objfile_data_key
c1bd65d0 17650 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17651
1bedd215
AC
17652 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17653Set DWARF 2 specific variables.\n\
17654Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17655 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17656 0/*allow-unknown*/, &maintenance_set_cmdlist);
17657
1bedd215
AC
17658 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17659Show DWARF 2 specific variables\n\
17660Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17661 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17662 0/*allow-unknown*/, &maintenance_show_cmdlist);
17663
17664 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17665 &dwarf2_max_cache_age, _("\
17666Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17667Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17668A higher limit means that cached compilation units will be stored\n\
17669in memory longer, and more total memory will be used. Zero disables\n\
17670caching, which can slow down startup."),
2c5b56ce 17671 NULL,
920d2a44 17672 show_dwarf2_max_cache_age,
2c5b56ce 17673 &set_dwarf2_cmdlist,
ae038cb0 17674 &show_dwarf2_cmdlist);
d97bc12b 17675
9eae7c52
TT
17676 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17677 &dwarf2_always_disassemble, _("\
17678Set whether `info address' always disassembles DWARF expressions."), _("\
17679Show whether `info address' always disassembles DWARF expressions."), _("\
17680When enabled, DWARF expressions are always printed in an assembly-like\n\
17681syntax. When disabled, expressions will be printed in a more\n\
17682conversational style, when possible."),
17683 NULL,
17684 show_dwarf2_always_disassemble,
17685 &set_dwarf2_cmdlist,
17686 &show_dwarf2_cmdlist);
17687
d97bc12b
DE
17688 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17689Set debugging of the dwarf2 DIE reader."), _("\
17690Show debugging of the dwarf2 DIE reader."), _("\
17691When enabled (non-zero), DIEs are dumped after they are read in.\n\
17692The value is the maximum depth to print."),
17693 NULL,
17694 NULL,
17695 &setdebuglist, &showdebuglist);
9291a0cd 17696
900e11f9
JK
17697 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17698Set cross-checking of \"physname\" code against demangler."), _("\
17699Show cross-checking of \"physname\" code against demangler."), _("\
17700When enabled, GDB's internal \"physname\" code is checked against\n\
17701the demangler."),
17702 NULL, show_check_physname,
17703 &setdebuglist, &showdebuglist);
17704
96d19272 17705 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 17706 _("\
fc1a9d6e 17707Save a gdb-index file.\n\
11570e71 17708Usage: save gdb-index DIRECTORY"),
96d19272
JK
17709 &save_cmdlist);
17710 set_cmd_completer (c, filename_completer);
6502dd73 17711}
This page took 2.518144 seconds and 4 git commands to generate.