daily update
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
0b302171 3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
c906108c
SS
33#include "symtab.h"
34#include "gdbtypes.h"
c906108c 35#include "objfiles.h"
fa8f86ff 36#include "dwarf2.h"
c906108c
SS
37#include "buildsym.h"
38#include "demangle.h"
50f182aa 39#include "gdb-demangle.h"
c906108c 40#include "expression.h"
d5166ae1 41#include "filenames.h" /* for DOSish file names */
2e276125 42#include "macrotab.h"
c906108c
SS
43#include "language.h"
44#include "complaints.h"
357e46e7 45#include "bcache.h"
4c2df51b
DJ
46#include "dwarf2expr.h"
47#include "dwarf2loc.h"
9219021c 48#include "cp-support.h"
72bf9492 49#include "hashtab.h"
ae038cb0
DJ
50#include "command.h"
51#include "gdbcmd.h"
edb3359d 52#include "block.h"
ff013f42 53#include "addrmap.h"
94af9270
KS
54#include "typeprint.h"
55#include "jv-lang.h"
ccefe4c4 56#include "psympriv.h"
9291a0cd
TT
57#include "exceptions.h"
58#include "gdb_stat.h"
96d19272 59#include "completer.h"
34eaf542 60#include "vec.h"
98bfdba5 61#include "c-lang.h"
a766d390 62#include "go-lang.h"
98bfdba5 63#include "valprint.h"
3019eac3 64#include "gdbcore.h" /* for gnutarget */
60d5a603 65#include <ctype.h>
4c2df51b 66
c906108c
SS
67#include <fcntl.h>
68#include "gdb_string.h"
4bdf3d34 69#include "gdb_assert.h"
c906108c 70#include <sys/types.h>
233a11ab
CS
71#ifdef HAVE_ZLIB_H
72#include <zlib.h>
73#endif
dce234bc
PP
74#ifdef HAVE_MMAP
75#include <sys/mman.h>
85d9bd0e
TT
76#ifndef MAP_FAILED
77#define MAP_FAILED ((void *) -1)
78#endif
dce234bc 79#endif
d8151005 80
34eaf542
TT
81typedef struct symbol *symbolp;
82DEF_VEC_P (symbolp);
83
d97bc12b
DE
84/* When non-zero, dump DIEs after they are read in. */
85static int dwarf2_die_debug = 0;
86
900e11f9
JK
87/* When non-zero, cross-check physname against demangler. */
88static int check_physname = 0;
89
481860b3
GB
90/* When non-zero, do not reject deprecated .gdb_index sections. */
91int use_deprecated_index_sections = 0;
92
dce234bc
PP
93static int pagesize;
94
df8a16a1
DJ
95/* When set, the file that we're processing is known to have debugging
96 info for C++ namespaces. GCC 3.3.x did not produce this information,
97 but later versions do. */
98
99static int processing_has_namespace_info;
100
6502dd73
DJ
101static const struct objfile_data *dwarf2_objfile_data_key;
102
dce234bc
PP
103struct dwarf2_section_info
104{
105 asection *asection;
106 gdb_byte *buffer;
107 bfd_size_type size;
b315ab21
TG
108 /* Not NULL if the section was actually mmapped. */
109 void *map_addr;
110 /* Page aligned size of mmapped area. */
111 bfd_size_type map_len;
be391dca
TT
112 /* True if we have tried to read this section. */
113 int readin;
dce234bc
PP
114};
115
8b70b953
TT
116typedef struct dwarf2_section_info dwarf2_section_info_def;
117DEF_VEC_O (dwarf2_section_info_def);
118
9291a0cd
TT
119/* All offsets in the index are of this type. It must be
120 architecture-independent. */
121typedef uint32_t offset_type;
122
123DEF_VEC_I (offset_type);
124
125/* A description of the mapped index. The file format is described in
126 a comment by the code that writes the index. */
127struct mapped_index
128{
559a7a62
JK
129 /* Index data format version. */
130 int version;
131
9291a0cd
TT
132 /* The total length of the buffer. */
133 off_t total_size;
b11b1f88 134
9291a0cd
TT
135 /* A pointer to the address table data. */
136 const gdb_byte *address_table;
b11b1f88 137
9291a0cd
TT
138 /* Size of the address table data in bytes. */
139 offset_type address_table_size;
b11b1f88 140
3876f04e
DE
141 /* The symbol table, implemented as a hash table. */
142 const offset_type *symbol_table;
b11b1f88 143
9291a0cd 144 /* Size in slots, each slot is 2 offset_types. */
3876f04e 145 offset_type symbol_table_slots;
b11b1f88 146
9291a0cd
TT
147 /* A pointer to the constant pool. */
148 const char *constant_pool;
149};
150
95554aad
TT
151typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
152DEF_VEC_P (dwarf2_per_cu_ptr);
153
9cdd5dbd
DE
154/* Collection of data recorded per objfile.
155 This hangs off of dwarf2_objfile_data_key. */
156
6502dd73
DJ
157struct dwarf2_per_objfile
158{
dce234bc
PP
159 struct dwarf2_section_info info;
160 struct dwarf2_section_info abbrev;
161 struct dwarf2_section_info line;
dce234bc
PP
162 struct dwarf2_section_info loc;
163 struct dwarf2_section_info macinfo;
cf2c3c16 164 struct dwarf2_section_info macro;
dce234bc
PP
165 struct dwarf2_section_info str;
166 struct dwarf2_section_info ranges;
3019eac3 167 struct dwarf2_section_info addr;
dce234bc
PP
168 struct dwarf2_section_info frame;
169 struct dwarf2_section_info eh_frame;
9291a0cd 170 struct dwarf2_section_info gdb_index;
ae038cb0 171
8b70b953
TT
172 VEC (dwarf2_section_info_def) *types;
173
be391dca
TT
174 /* Back link. */
175 struct objfile *objfile;
176
d467dd73 177 /* Table of all the compilation units. This is used to locate
10b3939b 178 the target compilation unit of a particular reference. */
ae038cb0
DJ
179 struct dwarf2_per_cu_data **all_comp_units;
180
181 /* The number of compilation units in ALL_COMP_UNITS. */
182 int n_comp_units;
183
1fd400ff 184 /* The number of .debug_types-related CUs. */
d467dd73 185 int n_type_units;
1fd400ff 186
d467dd73
DE
187 /* The .debug_types-related CUs (TUs). */
188 struct dwarf2_per_cu_data **all_type_units;
1fd400ff 189
ae038cb0
DJ
190 /* A chain of compilation units that are currently read in, so that
191 they can be freed later. */
192 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 193
348e048f
DE
194 /* A table mapping .debug_types signatures to its signatured_type entry.
195 This is NULL if the .debug_types section hasn't been read in yet. */
196 htab_t signatured_types;
197
3019eac3
DE
198 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
199 This is NULL if the table hasn't been allocated yet. */
200 htab_t dwo_files;
201
72dca2f5
FR
202 /* A flag indicating wether this objfile has a section loaded at a
203 VMA of 0. */
204 int has_section_at_zero;
9291a0cd 205
ae2de4f8
DE
206 /* True if we are using the mapped index,
207 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
208 unsigned char using_index;
209
ae2de4f8 210 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 211 struct mapped_index *index_table;
98bfdba5 212
7b9f3c50
DE
213 /* When using index_table, this keeps track of all quick_file_names entries.
214 TUs can share line table entries with CUs or other TUs, and there can be
215 a lot more TUs than unique line tables, so we maintain a separate table
216 of all line table entries to support the sharing. */
217 htab_t quick_file_names_table;
218
98bfdba5
PA
219 /* Set during partial symbol reading, to prevent queueing of full
220 symbols. */
221 int reading_partial_symbols;
673bfd45 222
dee91e82 223 /* Table mapping type DIEs to their struct type *.
673bfd45 224 This is NULL if not allocated yet.
dee91e82
DE
225 The mapping is done via (CU/TU signature + DIE offset) -> type. */
226 htab_t die_type_hash;
95554aad
TT
227
228 /* The CUs we recently read. */
229 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
230};
231
232static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 233
251d32d9 234/* Default names of the debugging sections. */
c906108c 235
233a11ab
CS
236/* Note that if the debugging section has been compressed, it might
237 have a name like .zdebug_info. */
238
9cdd5dbd
DE
239static const struct dwarf2_debug_sections dwarf2_elf_names =
240{
251d32d9
TG
241 { ".debug_info", ".zdebug_info" },
242 { ".debug_abbrev", ".zdebug_abbrev" },
243 { ".debug_line", ".zdebug_line" },
244 { ".debug_loc", ".zdebug_loc" },
245 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 246 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
247 { ".debug_str", ".zdebug_str" },
248 { ".debug_ranges", ".zdebug_ranges" },
249 { ".debug_types", ".zdebug_types" },
3019eac3 250 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
251 { ".debug_frame", ".zdebug_frame" },
252 { ".eh_frame", NULL },
24d3216f
TT
253 { ".gdb_index", ".zgdb_index" },
254 23
251d32d9 255};
c906108c 256
3019eac3
DE
257/* List of DWO sections. */
258
259static const struct dwo_section_names
260{
261 struct dwarf2_section_names abbrev_dwo;
262 struct dwarf2_section_names info_dwo;
263 struct dwarf2_section_names line_dwo;
264 struct dwarf2_section_names loc_dwo;
265 struct dwarf2_section_names str_dwo;
266 struct dwarf2_section_names str_offsets_dwo;
267 struct dwarf2_section_names types_dwo;
268}
269dwo_section_names =
270{
271 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
272 { ".debug_info.dwo", ".zdebug_info.dwo" },
273 { ".debug_line.dwo", ".zdebug_line.dwo" },
274 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
275 { ".debug_str.dwo", ".zdebug_str.dwo" },
276 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
277 { ".debug_types.dwo", ".zdebug_types.dwo" },
278};
279
c906108c
SS
280/* local data types */
281
0963b4bd 282/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
283#ifndef ABBREV_HASH_SIZE
284#define ABBREV_HASH_SIZE 121
285#endif
286
107d2387
AC
287/* The data in a compilation unit header, after target2host
288 translation, looks like this. */
c906108c 289struct comp_unit_head
a738430d 290{
c764a876 291 unsigned int length;
a738430d 292 short version;
a738430d
MK
293 unsigned char addr_size;
294 unsigned char signed_addr_p;
b64f50a1 295 sect_offset abbrev_offset;
57349743 296
a738430d
MK
297 /* Size of file offsets; either 4 or 8. */
298 unsigned int offset_size;
57349743 299
a738430d
MK
300 /* Size of the length field; either 4 or 12. */
301 unsigned int initial_length_size;
57349743 302
a738430d
MK
303 /* Offset to the first byte of this compilation unit header in the
304 .debug_info section, for resolving relative reference dies. */
b64f50a1 305 sect_offset offset;
57349743 306
d00adf39
DE
307 /* Offset to first die in this cu from the start of the cu.
308 This will be the first byte following the compilation unit header. */
b64f50a1 309 cu_offset first_die_offset;
a738430d 310};
c906108c 311
3da10d80
KS
312/* Type used for delaying computation of method physnames.
313 See comments for compute_delayed_physnames. */
314struct delayed_method_info
315{
316 /* The type to which the method is attached, i.e., its parent class. */
317 struct type *type;
318
319 /* The index of the method in the type's function fieldlists. */
320 int fnfield_index;
321
322 /* The index of the method in the fieldlist. */
323 int index;
324
325 /* The name of the DIE. */
326 const char *name;
327
328 /* The DIE associated with this method. */
329 struct die_info *die;
330};
331
332typedef struct delayed_method_info delayed_method_info;
333DEF_VEC_O (delayed_method_info);
334
e7c27a73
DJ
335/* Internal state when decoding a particular compilation unit. */
336struct dwarf2_cu
337{
338 /* The objfile containing this compilation unit. */
339 struct objfile *objfile;
340
d00adf39 341 /* The header of the compilation unit. */
e7c27a73 342 struct comp_unit_head header;
e142c38c 343
d00adf39
DE
344 /* Base address of this compilation unit. */
345 CORE_ADDR base_address;
346
347 /* Non-zero if base_address has been set. */
348 int base_known;
349
e142c38c
DJ
350 /* The language we are debugging. */
351 enum language language;
352 const struct language_defn *language_defn;
353
b0f35d58
DL
354 const char *producer;
355
e142c38c
DJ
356 /* The generic symbol table building routines have separate lists for
357 file scope symbols and all all other scopes (local scopes). So
358 we need to select the right one to pass to add_symbol_to_list().
359 We do it by keeping a pointer to the correct list in list_in_scope.
360
361 FIXME: The original dwarf code just treated the file scope as the
362 first local scope, and all other local scopes as nested local
363 scopes, and worked fine. Check to see if we really need to
364 distinguish these in buildsym.c. */
365 struct pending **list_in_scope;
366
f3dd6933
DJ
367 /* DWARF abbreviation table associated with this compilation unit. */
368 struct abbrev_info **dwarf2_abbrevs;
369
370 /* Storage for the abbrev table. */
371 struct obstack abbrev_obstack;
72bf9492 372
b64f50a1
JK
373 /* Hash table holding all the loaded partial DIEs
374 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
375 htab_t partial_dies;
376
377 /* Storage for things with the same lifetime as this read-in compilation
378 unit, including partial DIEs. */
379 struct obstack comp_unit_obstack;
380
ae038cb0
DJ
381 /* When multiple dwarf2_cu structures are living in memory, this field
382 chains them all together, so that they can be released efficiently.
383 We will probably also want a generation counter so that most-recently-used
384 compilation units are cached... */
385 struct dwarf2_per_cu_data *read_in_chain;
386
387 /* Backchain to our per_cu entry if the tree has been built. */
388 struct dwarf2_per_cu_data *per_cu;
389
390 /* How many compilation units ago was this CU last referenced? */
391 int last_used;
392
b64f50a1
JK
393 /* A hash table of DIE cu_offset for following references with
394 die_info->offset.sect_off as hash. */
51545339 395 htab_t die_hash;
10b3939b
DJ
396
397 /* Full DIEs if read in. */
398 struct die_info *dies;
399
400 /* A set of pointers to dwarf2_per_cu_data objects for compilation
401 units referenced by this one. Only set during full symbol processing;
402 partial symbol tables do not have dependencies. */
403 htab_t dependencies;
404
cb1df416
DJ
405 /* Header data from the line table, during full symbol processing. */
406 struct line_header *line_header;
407
3da10d80
KS
408 /* A list of methods which need to have physnames computed
409 after all type information has been read. */
410 VEC (delayed_method_info) *method_list;
411
96408a79
SA
412 /* To be copied to symtab->call_site_htab. */
413 htab_t call_site_htab;
414
3019eac3
DE
415 /* Non-NULL if this CU came from a DWO file. */
416 struct dwo_unit *dwo_unit;
417
418 /* The DW_AT_addr_base attribute if present, zero otherwise
419 (zero is a valid value though).
420 Note this value comes from the stub CU/TU's DIE. */
421 ULONGEST addr_base;
422
ae038cb0
DJ
423 /* Mark used when releasing cached dies. */
424 unsigned int mark : 1;
425
8be455d7
JK
426 /* This CU references .debug_loc. See the symtab->locations_valid field.
427 This test is imperfect as there may exist optimized debug code not using
428 any location list and still facing inlining issues if handled as
429 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 430 unsigned int has_loclist : 1;
ba919b58 431
685b1105
JK
432 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
433 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
434 are valid. This information is cached because profiling CU expansion
435 showed excessive time spent in producer_is_gxx_lt_4_6. */
ba919b58
TT
436 unsigned int checked_producer : 1;
437 unsigned int producer_is_gxx_lt_4_6 : 1;
685b1105 438 unsigned int producer_is_icc : 1;
3019eac3
DE
439
440 /* Non-zero if DW_AT_addr_base was found.
441 Used when processing DWO files. */
442 unsigned int have_addr_base : 1;
e7c27a73
DJ
443};
444
10b3939b
DJ
445/* Persistent data held for a compilation unit, even when not
446 processing it. We put a pointer to this structure in the
28dee7f5 447 read_symtab_private field of the psymtab. */
10b3939b 448
ae038cb0
DJ
449struct dwarf2_per_cu_data
450{
348e048f 451 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 452 bytes should suffice to store the length of any compilation unit
45452591
DE
453 - if it doesn't, GDB will fall over anyway.
454 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
455 initial_length_size.
456 If the DIE refers to a DWO file, this is always of the original die,
457 not the DWO file. */
b64f50a1 458 sect_offset offset;
348e048f 459 unsigned int length : 29;
ae038cb0
DJ
460
461 /* Flag indicating this compilation unit will be read in before
462 any of the current compilation units are processed. */
c764a876 463 unsigned int queued : 1;
ae038cb0 464
0d99eb77
DE
465 /* This flag will be set when reading partial DIEs if we need to load
466 absolutely all DIEs for this compilation unit, instead of just the ones
467 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
468 hash table and don't find it. */
469 unsigned int load_all_dies : 1;
470
3019eac3
DE
471 /* Non-zero if this CU is from .debug_types. */
472 unsigned int is_debug_types : 1;
473
474 /* The section this CU/TU lives in.
475 If the DIE refers to a DWO file, this is always the original die,
476 not the DWO file. */
477 struct dwarf2_section_info *info_or_types_section;
348e048f 478
17ea53c3
JK
479 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
480 of the CU cache it gets reset to NULL again. */
ae038cb0 481 struct dwarf2_cu *cu;
1c379e20 482
9cdd5dbd
DE
483 /* The corresponding objfile.
484 Normally we can get the objfile from dwarf2_per_objfile.
485 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
486 struct objfile *objfile;
487
488 /* When using partial symbol tables, the 'psymtab' field is active.
489 Otherwise the 'quick' field is active. */
490 union
491 {
492 /* The partial symbol table associated with this compilation unit,
95554aad 493 or NULL for unread partial units. */
9291a0cd
TT
494 struct partial_symtab *psymtab;
495
496 /* Data needed by the "quick" functions. */
497 struct dwarf2_per_cu_quick_data *quick;
498 } v;
95554aad
TT
499
500 /* The CUs we import using DW_TAG_imported_unit. This is filled in
501 while reading psymtabs, used to compute the psymtab dependencies,
502 and then cleared. Then it is filled in again while reading full
503 symbols, and only deleted when the objfile is destroyed. */
504 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
505};
506
348e048f
DE
507/* Entry in the signatured_types hash table. */
508
509struct signatured_type
510{
3019eac3 511 /* The type's signature. */
348e048f
DE
512 ULONGEST signature;
513
3019eac3
DE
514 /* Offset in the TU of the type's DIE, as read from the TU header.
515 If the definition lives in a DWO file, this value is unusable. */
516 cu_offset type_offset_in_tu;
517
518 /* Offset in the section of the type's DIE.
519 If the definition lives in a DWO file, this is the offset in the
520 .debug_types.dwo section.
521 The value is zero until the actual value is known.
522 Zero is otherwise not a valid section offset. */
523 sect_offset type_offset_in_section;
348e048f
DE
524
525 /* The CU(/TU) of this type. */
526 struct dwarf2_per_cu_data per_cu;
527};
528
3019eac3
DE
529/* These sections are what may appear in a "dwo" file. */
530
531struct dwo_sections
532{
533 struct dwarf2_section_info abbrev;
534 struct dwarf2_section_info info;
535 struct dwarf2_section_info line;
536 struct dwarf2_section_info loc;
537 struct dwarf2_section_info str;
538 struct dwarf2_section_info str_offsets;
539 VEC (dwarf2_section_info_def) *types;
540};
541
542/* Common bits of DWO CUs/TUs. */
543
544struct dwo_unit
545{
546 /* Backlink to the containing struct dwo_file. */
547 struct dwo_file *dwo_file;
548
549 /* The "id" that distinguishes this CU/TU.
550 .debug_info calls this "dwo_id", .debug_types calls this "signature".
551 Since signatures came first, we stick with it for consistency. */
552 ULONGEST signature;
553
554 /* The section this CU/TU lives in, in the DWO file. */
555 struct dwarf2_section_info *info_or_types_section;
556
557 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
558 sect_offset offset;
559 unsigned int length;
560
561 /* For types, offset in the type's DIE of the type defined by this TU. */
562 cu_offset type_offset_in_tu;
563};
564
565/* Data for one DWO file. */
566
567struct dwo_file
568{
569 /* The DW_AT_GNU_dwo_name attribute.
570 We don't manage space for this, it's an attribute. */
571 const char *dwo_name;
572
573 /* The bfd, when the file is open. Otherwise this is NULL. */
574 bfd *dwo_bfd;
575
576 /* Section info for this file. */
577 struct dwo_sections sections;
578
579 /* Table of CUs in the file.
580 Each element is a struct dwo_unit. */
581 htab_t cus;
582
583 /* Table of TUs in the file.
584 Each element is a struct dwo_unit. */
585 htab_t tus;
586};
587
0963b4bd
MS
588/* Struct used to pass misc. parameters to read_die_and_children, et
589 al. which are used for both .debug_info and .debug_types dies.
590 All parameters here are unchanging for the life of the call. This
dee91e82 591 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
592
593struct die_reader_specs
594{
dee91e82 595 /* die_section->asection->owner. */
93311388
DE
596 bfd* abfd;
597
598 /* The CU of the DIE we are parsing. */
599 struct dwarf2_cu *cu;
600
3019eac3
DE
601 /* Non-NULL if reading a DWO file. */
602 struct dwo_file *dwo_file;
603
dee91e82 604 /* The section the die comes from.
3019eac3 605 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
606 struct dwarf2_section_info *die_section;
607
608 /* die_section->buffer. */
609 gdb_byte *buffer;
f664829e
DE
610
611 /* The end of the buffer. */
612 const gdb_byte *buffer_end;
93311388
DE
613};
614
fd820528 615/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
616typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
617 gdb_byte *info_ptr,
618 struct die_info *comp_unit_die,
619 int has_children,
620 void *data);
621
debd256d
JB
622/* The line number information for a compilation unit (found in the
623 .debug_line section) begins with a "statement program header",
624 which contains the following information. */
625struct line_header
626{
627 unsigned int total_length;
628 unsigned short version;
629 unsigned int header_length;
630 unsigned char minimum_instruction_length;
2dc7f7b3 631 unsigned char maximum_ops_per_instruction;
debd256d
JB
632 unsigned char default_is_stmt;
633 int line_base;
634 unsigned char line_range;
635 unsigned char opcode_base;
636
637 /* standard_opcode_lengths[i] is the number of operands for the
638 standard opcode whose value is i. This means that
639 standard_opcode_lengths[0] is unused, and the last meaningful
640 element is standard_opcode_lengths[opcode_base - 1]. */
641 unsigned char *standard_opcode_lengths;
642
643 /* The include_directories table. NOTE! These strings are not
644 allocated with xmalloc; instead, they are pointers into
645 debug_line_buffer. If you try to free them, `free' will get
646 indigestion. */
647 unsigned int num_include_dirs, include_dirs_size;
648 char **include_dirs;
649
650 /* The file_names table. NOTE! These strings are not allocated
651 with xmalloc; instead, they are pointers into debug_line_buffer.
652 Don't try to free them directly. */
653 unsigned int num_file_names, file_names_size;
654 struct file_entry
c906108c 655 {
debd256d
JB
656 char *name;
657 unsigned int dir_index;
658 unsigned int mod_time;
659 unsigned int length;
aaa75496 660 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 661 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
662 } *file_names;
663
664 /* The start and end of the statement program following this
6502dd73 665 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 666 gdb_byte *statement_program_start, *statement_program_end;
debd256d 667};
c906108c
SS
668
669/* When we construct a partial symbol table entry we only
0963b4bd 670 need this much information. */
c906108c
SS
671struct partial_die_info
672 {
72bf9492 673 /* Offset of this DIE. */
b64f50a1 674 sect_offset offset;
72bf9492
DJ
675
676 /* DWARF-2 tag for this DIE. */
677 ENUM_BITFIELD(dwarf_tag) tag : 16;
678
72bf9492
DJ
679 /* Assorted flags describing the data found in this DIE. */
680 unsigned int has_children : 1;
681 unsigned int is_external : 1;
682 unsigned int is_declaration : 1;
683 unsigned int has_type : 1;
684 unsigned int has_specification : 1;
685 unsigned int has_pc_info : 1;
481860b3 686 unsigned int may_be_inlined : 1;
72bf9492
DJ
687
688 /* Flag set if the SCOPE field of this structure has been
689 computed. */
690 unsigned int scope_set : 1;
691
fa4028e9
JB
692 /* Flag set if the DIE has a byte_size attribute. */
693 unsigned int has_byte_size : 1;
694
98bfdba5
PA
695 /* Flag set if any of the DIE's children are template arguments. */
696 unsigned int has_template_arguments : 1;
697
abc72ce4
DE
698 /* Flag set if fixup_partial_die has been called on this die. */
699 unsigned int fixup_called : 1;
700
72bf9492 701 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 702 sometimes a default name for unnamed DIEs. */
c906108c 703 char *name;
72bf9492 704
abc72ce4
DE
705 /* The linkage name, if present. */
706 const char *linkage_name;
707
72bf9492
DJ
708 /* The scope to prepend to our children. This is generally
709 allocated on the comp_unit_obstack, so will disappear
710 when this compilation unit leaves the cache. */
711 char *scope;
712
95554aad
TT
713 /* Some data associated with the partial DIE. The tag determines
714 which field is live. */
715 union
716 {
717 /* The location description associated with this DIE, if any. */
718 struct dwarf_block *locdesc;
719 /* The offset of an import, for DW_TAG_imported_unit. */
720 sect_offset offset;
721 } d;
72bf9492
DJ
722
723 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
724 CORE_ADDR lowpc;
725 CORE_ADDR highpc;
72bf9492 726
93311388 727 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 728 DW_AT_sibling, if any. */
abc72ce4
DE
729 /* NOTE: This member isn't strictly necessary, read_partial_die could
730 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 731 gdb_byte *sibling;
72bf9492
DJ
732
733 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
734 DW_AT_specification (or DW_AT_abstract_origin or
735 DW_AT_extension). */
b64f50a1 736 sect_offset spec_offset;
72bf9492
DJ
737
738 /* Pointers to this DIE's parent, first child, and next sibling,
739 if any. */
740 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
741 };
742
0963b4bd 743/* This data structure holds the information of an abbrev. */
c906108c
SS
744struct abbrev_info
745 {
746 unsigned int number; /* number identifying abbrev */
747 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
748 unsigned short has_children; /* boolean */
749 unsigned short num_attrs; /* number of attributes */
c906108c
SS
750 struct attr_abbrev *attrs; /* an array of attribute descriptions */
751 struct abbrev_info *next; /* next in chain */
752 };
753
754struct attr_abbrev
755 {
9d25dd43
DE
756 ENUM_BITFIELD(dwarf_attribute) name : 16;
757 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
758 };
759
0963b4bd 760/* Attributes have a name and a value. */
b60c80d6
DJ
761struct attribute
762 {
9d25dd43 763 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
764 ENUM_BITFIELD(dwarf_form) form : 15;
765
766 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
767 field should be in u.str (existing only for DW_STRING) but it is kept
768 here for better struct attribute alignment. */
769 unsigned int string_is_canonical : 1;
770
b60c80d6
DJ
771 union
772 {
773 char *str;
774 struct dwarf_block *blk;
43bbcdc2
PH
775 ULONGEST unsnd;
776 LONGEST snd;
b60c80d6 777 CORE_ADDR addr;
348e048f 778 struct signatured_type *signatured_type;
b60c80d6
DJ
779 }
780 u;
781 };
782
0963b4bd 783/* This data structure holds a complete die structure. */
c906108c
SS
784struct die_info
785 {
76815b17
DE
786 /* DWARF-2 tag for this DIE. */
787 ENUM_BITFIELD(dwarf_tag) tag : 16;
788
789 /* Number of attributes */
98bfdba5
PA
790 unsigned char num_attrs;
791
792 /* True if we're presently building the full type name for the
793 type derived from this DIE. */
794 unsigned char building_fullname : 1;
76815b17
DE
795
796 /* Abbrev number */
797 unsigned int abbrev;
798
93311388 799 /* Offset in .debug_info or .debug_types section. */
b64f50a1 800 sect_offset offset;
78ba4af6
JB
801
802 /* The dies in a compilation unit form an n-ary tree. PARENT
803 points to this die's parent; CHILD points to the first child of
804 this node; and all the children of a given node are chained
4950bc1c 805 together via their SIBLING fields. */
639d11d3
DC
806 struct die_info *child; /* Its first child, if any. */
807 struct die_info *sibling; /* Its next sibling, if any. */
808 struct die_info *parent; /* Its parent, if any. */
c906108c 809
b60c80d6
DJ
810 /* An array of attributes, with NUM_ATTRS elements. There may be
811 zero, but it's not common and zero-sized arrays are not
812 sufficiently portable C. */
813 struct attribute attrs[1];
c906108c
SS
814 };
815
0963b4bd 816/* Get at parts of an attribute structure. */
c906108c
SS
817
818#define DW_STRING(attr) ((attr)->u.str)
8285870a 819#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
820#define DW_UNSND(attr) ((attr)->u.unsnd)
821#define DW_BLOCK(attr) ((attr)->u.blk)
822#define DW_SND(attr) ((attr)->u.snd)
823#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 824#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 825
0963b4bd 826/* Blocks are a bunch of untyped bytes. */
c906108c
SS
827struct dwarf_block
828 {
829 unsigned int size;
1d6edc3c
JK
830
831 /* Valid only if SIZE is not zero. */
fe1b8b76 832 gdb_byte *data;
c906108c
SS
833 };
834
c906108c
SS
835#ifndef ATTR_ALLOC_CHUNK
836#define ATTR_ALLOC_CHUNK 4
837#endif
838
c906108c
SS
839/* Allocate fields for structs, unions and enums in this size. */
840#ifndef DW_FIELD_ALLOC_CHUNK
841#define DW_FIELD_ALLOC_CHUNK 4
842#endif
843
c906108c
SS
844/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
845 but this would require a corresponding change in unpack_field_as_long
846 and friends. */
847static int bits_per_byte = 8;
848
849/* The routines that read and process dies for a C struct or C++ class
850 pass lists of data member fields and lists of member function fields
851 in an instance of a field_info structure, as defined below. */
852struct field_info
c5aa993b 853 {
0963b4bd 854 /* List of data member and baseclasses fields. */
c5aa993b
JM
855 struct nextfield
856 {
857 struct nextfield *next;
858 int accessibility;
859 int virtuality;
860 struct field field;
861 }
7d0ccb61 862 *fields, *baseclasses;
c906108c 863
7d0ccb61 864 /* Number of fields (including baseclasses). */
c5aa993b 865 int nfields;
c906108c 866
c5aa993b
JM
867 /* Number of baseclasses. */
868 int nbaseclasses;
c906108c 869
c5aa993b
JM
870 /* Set if the accesibility of one of the fields is not public. */
871 int non_public_fields;
c906108c 872
c5aa993b
JM
873 /* Member function fields array, entries are allocated in the order they
874 are encountered in the object file. */
875 struct nextfnfield
876 {
877 struct nextfnfield *next;
878 struct fn_field fnfield;
879 }
880 *fnfields;
c906108c 881
c5aa993b
JM
882 /* Member function fieldlist array, contains name of possibly overloaded
883 member function, number of overloaded member functions and a pointer
884 to the head of the member function field chain. */
885 struct fnfieldlist
886 {
887 char *name;
888 int length;
889 struct nextfnfield *head;
890 }
891 *fnfieldlists;
c906108c 892
c5aa993b
JM
893 /* Number of entries in the fnfieldlists array. */
894 int nfnfields;
98751a41
JK
895
896 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
897 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
898 struct typedef_field_list
899 {
900 struct typedef_field field;
901 struct typedef_field_list *next;
902 }
903 *typedef_field_list;
904 unsigned typedef_field_list_count;
c5aa993b 905 };
c906108c 906
10b3939b
DJ
907/* One item on the queue of compilation units to read in full symbols
908 for. */
909struct dwarf2_queue_item
910{
911 struct dwarf2_per_cu_data *per_cu;
95554aad 912 enum language pretend_language;
10b3939b
DJ
913 struct dwarf2_queue_item *next;
914};
915
916/* The current queue. */
917static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
918
ae038cb0
DJ
919/* Loaded secondary compilation units are kept in memory until they
920 have not been referenced for the processing of this many
921 compilation units. Set this to zero to disable caching. Cache
922 sizes of up to at least twenty will improve startup time for
923 typical inter-CU-reference binaries, at an obvious memory cost. */
924static int dwarf2_max_cache_age = 5;
920d2a44
AC
925static void
926show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
927 struct cmd_list_element *c, const char *value)
928{
3e43a32a
MS
929 fprintf_filtered (file, _("The upper bound on the age of cached "
930 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
931 value);
932}
933
ae038cb0 934
0963b4bd 935/* Various complaints about symbol reading that don't abort the process. */
c906108c 936
4d3c2250
KB
937static void
938dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 939{
4d3c2250 940 complaint (&symfile_complaints,
e2e0b3e5 941 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
942}
943
25e43795
DJ
944static void
945dwarf2_debug_line_missing_file_complaint (void)
946{
947 complaint (&symfile_complaints,
948 _(".debug_line section has line data without a file"));
949}
950
59205f5a
JB
951static void
952dwarf2_debug_line_missing_end_sequence_complaint (void)
953{
954 complaint (&symfile_complaints,
3e43a32a
MS
955 _(".debug_line section has line "
956 "program sequence without an end"));
59205f5a
JB
957}
958
4d3c2250
KB
959static void
960dwarf2_complex_location_expr_complaint (void)
2e276125 961{
e2e0b3e5 962 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
963}
964
4d3c2250
KB
965static void
966dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
967 int arg3)
2e276125 968{
4d3c2250 969 complaint (&symfile_complaints,
3e43a32a
MS
970 _("const value length mismatch for '%s', got %d, expected %d"),
971 arg1, arg2, arg3);
4d3c2250
KB
972}
973
974static void
f664829e 975dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 976{
4d3c2250 977 complaint (&symfile_complaints,
f664829e
DE
978 _("debug info runs off end of %s section"
979 " [in module %s]"),
980 section->asection->name,
981 bfd_get_filename (section->asection->owner));
4d3c2250
KB
982}
983
984static void
985dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 986{
4d3c2250 987 complaint (&symfile_complaints,
3e43a32a
MS
988 _("macro debug info contains a "
989 "malformed macro definition:\n`%s'"),
4d3c2250
KB
990 arg1);
991}
992
993static void
994dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 995{
4d3c2250 996 complaint (&symfile_complaints,
3e43a32a
MS
997 _("invalid attribute class or form for '%s' in '%s'"),
998 arg1, arg2);
4d3c2250 999}
c906108c 1000
c906108c
SS
1001/* local function prototypes */
1002
4efb68b1 1003static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1004
aaa75496
JB
1005static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1006 struct objfile *);
1007
918dd910
JK
1008static void dwarf2_find_base_address (struct die_info *die,
1009 struct dwarf2_cu *cu);
1010
c67a9c90 1011static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1012
72bf9492
DJ
1013static void scan_partial_symbols (struct partial_die_info *,
1014 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1015 int, struct dwarf2_cu *);
c906108c 1016
72bf9492
DJ
1017static void add_partial_symbol (struct partial_die_info *,
1018 struct dwarf2_cu *);
63d06c5c 1019
72bf9492
DJ
1020static void add_partial_namespace (struct partial_die_info *pdi,
1021 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1022 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1023
5d7cb8df
JK
1024static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1025 CORE_ADDR *highpc, int need_pc,
1026 struct dwarf2_cu *cu);
1027
72bf9492
DJ
1028static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1029 struct dwarf2_cu *cu);
91c24f0a 1030
bc30ff58
JB
1031static void add_partial_subprogram (struct partial_die_info *pdi,
1032 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1033 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1034
a14ed312 1035static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 1036
a14ed312 1037static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1038
dee91e82
DE
1039static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1040 struct dwarf2_section_info *);
c906108c 1041
f3dd6933 1042static void dwarf2_free_abbrev_table (void *);
c906108c 1043
6caca83c
CC
1044static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1045
fe1b8b76 1046static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 1047 struct dwarf2_cu *);
72bf9492 1048
57349743 1049static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 1050 struct dwarf2_cu *);
c906108c 1051
dee91e82
DE
1052static struct partial_die_info *load_partial_dies
1053 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1054
dee91e82
DE
1055static gdb_byte *read_partial_die (const struct die_reader_specs *,
1056 struct partial_die_info *,
1057 struct abbrev_info *,
1058 unsigned int,
1059 gdb_byte *);
c906108c 1060
b64f50a1 1061static struct partial_die_info *find_partial_die (sect_offset,
10b3939b 1062 struct dwarf2_cu *);
72bf9492
DJ
1063
1064static void fixup_partial_die (struct partial_die_info *,
1065 struct dwarf2_cu *);
1066
dee91e82
DE
1067static gdb_byte *read_attribute (const struct die_reader_specs *,
1068 struct attribute *, struct attr_abbrev *,
1069 gdb_byte *);
a8329558 1070
fe1b8b76 1071static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 1072
fe1b8b76 1073static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 1074
fe1b8b76 1075static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 1076
fe1b8b76 1077static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 1078
93311388 1079static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 1080
fe1b8b76 1081static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1082 unsigned int *);
c906108c 1083
c764a876
DE
1084static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1085
1086static LONGEST read_checked_initial_length_and_offset
1087 (bfd *, gdb_byte *, const struct comp_unit_head *,
1088 unsigned int *, unsigned int *);
613e1657 1089
fe1b8b76 1090static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1091 unsigned int *);
1092
1093static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1094
fe1b8b76 1095static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1096
9b1c24c8 1097static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1098
fe1b8b76
JB
1099static char *read_indirect_string (bfd *, gdb_byte *,
1100 const struct comp_unit_head *,
1101 unsigned int *);
4bdf3d34 1102
12df843f 1103static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1104
12df843f 1105static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1106
3019eac3
DE
1107static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1108 unsigned int *);
1109
1110static char *read_str_index (const struct die_reader_specs *reader,
1111 struct dwarf2_cu *cu, ULONGEST str_index);
1112
e142c38c 1113static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1114
e142c38c
DJ
1115static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1116 struct dwarf2_cu *);
c906108c 1117
348e048f
DE
1118static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1119 unsigned int,
1120 struct dwarf2_cu *);
1121
05cf31d1
JB
1122static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1123 struct dwarf2_cu *cu);
1124
e142c38c 1125static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1126
e142c38c 1127static struct die_info *die_specification (struct die_info *die,
f2f0e013 1128 struct dwarf2_cu **);
63d06c5c 1129
debd256d
JB
1130static void free_line_header (struct line_header *lh);
1131
aaa75496
JB
1132static void add_file_name (struct line_header *, char *, unsigned int,
1133 unsigned int, unsigned int);
1134
3019eac3
DE
1135static struct line_header *dwarf_decode_line_header (unsigned int offset,
1136 struct dwarf2_cu *cu);
debd256d 1137
f3f5162e
DE
1138static void dwarf_decode_lines (struct line_header *, const char *,
1139 struct dwarf2_cu *, struct partial_symtab *,
1140 int);
c906108c 1141
72b9f47f 1142static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1143
a14ed312 1144static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1145 struct dwarf2_cu *);
c906108c 1146
34eaf542
TT
1147static struct symbol *new_symbol_full (struct die_info *, struct type *,
1148 struct dwarf2_cu *, struct symbol *);
1149
a14ed312 1150static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1151 struct dwarf2_cu *);
c906108c 1152
98bfdba5
PA
1153static void dwarf2_const_value_attr (struct attribute *attr,
1154 struct type *type,
1155 const char *name,
1156 struct obstack *obstack,
12df843f 1157 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1158 gdb_byte **bytes,
1159 struct dwarf2_locexpr_baton **baton);
2df3850c 1160
e7c27a73 1161static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1162
b4ba55a1
JB
1163static int need_gnat_info (struct dwarf2_cu *);
1164
3e43a32a
MS
1165static struct type *die_descriptive_type (struct die_info *,
1166 struct dwarf2_cu *);
b4ba55a1
JB
1167
1168static void set_descriptive_type (struct type *, struct die_info *,
1169 struct dwarf2_cu *);
1170
e7c27a73
DJ
1171static struct type *die_containing_type (struct die_info *,
1172 struct dwarf2_cu *);
c906108c 1173
673bfd45
DE
1174static struct type *lookup_die_type (struct die_info *, struct attribute *,
1175 struct dwarf2_cu *);
c906108c 1176
f792889a 1177static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1178
673bfd45
DE
1179static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1180
0d5cff50 1181static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1182
6e70227d 1183static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1184 const char *suffix, int physname,
1185 struct dwarf2_cu *cu);
63d06c5c 1186
e7c27a73 1187static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1188
348e048f
DE
1189static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1190
e7c27a73 1191static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1192
e7c27a73 1193static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1194
96408a79
SA
1195static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1196
ff013f42
JK
1197static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1198 struct dwarf2_cu *, struct partial_symtab *);
1199
a14ed312 1200static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1201 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1202 struct partial_symtab *);
c906108c 1203
fae299cd
DC
1204static void get_scope_pc_bounds (struct die_info *,
1205 CORE_ADDR *, CORE_ADDR *,
1206 struct dwarf2_cu *);
1207
801e3a5b
JB
1208static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1209 CORE_ADDR, struct dwarf2_cu *);
1210
a14ed312 1211static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1212 struct dwarf2_cu *);
c906108c 1213
a14ed312 1214static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1215 struct type *, struct dwarf2_cu *);
c906108c 1216
a14ed312 1217static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1218 struct die_info *, struct type *,
e7c27a73 1219 struct dwarf2_cu *);
c906108c 1220
a14ed312 1221static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1222 struct type *,
1223 struct dwarf2_cu *);
c906108c 1224
134d01f1 1225static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1226
e7c27a73 1227static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1228
e7c27a73 1229static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1230
5d7cb8df
JK
1231static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1232
27aa8d6a
SW
1233static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1234
f55ee35c
JK
1235static struct type *read_module_type (struct die_info *die,
1236 struct dwarf2_cu *cu);
1237
38d518c9 1238static const char *namespace_name (struct die_info *die,
e142c38c 1239 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1240
134d01f1 1241static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1242
e7c27a73 1243static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1244
6e70227d 1245static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1246 struct dwarf2_cu *);
1247
dee91e82 1248static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1249 gdb_byte *info_ptr,
fe1b8b76 1250 gdb_byte **new_info_ptr,
639d11d3
DC
1251 struct die_info *parent);
1252
dee91e82 1253static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1254 gdb_byte *info_ptr,
fe1b8b76 1255 gdb_byte **new_info_ptr,
639d11d3
DC
1256 struct die_info *parent);
1257
3019eac3
DE
1258static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1259 struct die_info **, gdb_byte *, int *, int);
1260
dee91e82
DE
1261static gdb_byte *read_full_die (const struct die_reader_specs *,
1262 struct die_info **, gdb_byte *, int *);
93311388 1263
e7c27a73 1264static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1265
71c25dea
TT
1266static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1267 struct obstack *);
1268
e142c38c 1269static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1270
98bfdba5
PA
1271static const char *dwarf2_full_name (char *name,
1272 struct die_info *die,
1273 struct dwarf2_cu *cu);
1274
e142c38c 1275static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1276 struct dwarf2_cu **);
9219021c 1277
f39c6ffd 1278static const char *dwarf_tag_name (unsigned int);
c906108c 1279
f39c6ffd 1280static const char *dwarf_attr_name (unsigned int);
c906108c 1281
f39c6ffd 1282static const char *dwarf_form_name (unsigned int);
c906108c 1283
a14ed312 1284static char *dwarf_bool_name (unsigned int);
c906108c 1285
f39c6ffd 1286static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1287
f9aca02d 1288static struct die_info *sibling_die (struct die_info *);
c906108c 1289
d97bc12b
DE
1290static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1291
1292static void dump_die_for_error (struct die_info *);
1293
1294static void dump_die_1 (struct ui_file *, int level, int max_level,
1295 struct die_info *);
c906108c 1296
d97bc12b 1297/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1298
51545339 1299static void store_in_ref_table (struct die_info *,
10b3939b 1300 struct dwarf2_cu *);
c906108c 1301
93311388
DE
1302static int is_ref_attr (struct attribute *);
1303
b64f50a1 1304static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1305
43bbcdc2 1306static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1307
348e048f
DE
1308static struct die_info *follow_die_ref_or_sig (struct die_info *,
1309 struct attribute *,
1310 struct dwarf2_cu **);
1311
10b3939b
DJ
1312static struct die_info *follow_die_ref (struct die_info *,
1313 struct attribute *,
f2f0e013 1314 struct dwarf2_cu **);
c906108c 1315
348e048f
DE
1316static struct die_info *follow_die_sig (struct die_info *,
1317 struct attribute *,
1318 struct dwarf2_cu **);
1319
6c83ed52
TT
1320static struct signatured_type *lookup_signatured_type_at_offset
1321 (struct objfile *objfile,
b64f50a1 1322 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1323
e5fe5e75 1324static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1325
52dc124a 1326static void read_signatured_type (struct signatured_type *);
348e048f 1327
c906108c
SS
1328/* memory allocation interface */
1329
7b5a2f43 1330static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1331
f3dd6933 1332static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1333
b60c80d6 1334static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1335
2e276125 1336static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1337 char *, bfd *, struct dwarf2_cu *,
1338 struct dwarf2_section_info *,
fceca515 1339 int, const char *);
2e276125 1340
8e19ed76
PS
1341static int attr_form_is_block (struct attribute *);
1342
3690dd37
JB
1343static int attr_form_is_section_offset (struct attribute *);
1344
1345static int attr_form_is_constant (struct attribute *);
1346
8cf6f0b1
TT
1347static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1348 struct dwarf2_loclist_baton *baton,
1349 struct attribute *attr);
1350
93e7bd98
DJ
1351static void dwarf2_symbol_mark_computed (struct attribute *attr,
1352 struct symbol *sym,
1353 struct dwarf2_cu *cu);
4c2df51b 1354
dee91e82
DE
1355static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1356 gdb_byte *info_ptr,
1357 struct abbrev_info *abbrev);
4bb7a0a7 1358
72bf9492
DJ
1359static void free_stack_comp_unit (void *);
1360
72bf9492
DJ
1361static hashval_t partial_die_hash (const void *item);
1362
1363static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1364
ae038cb0 1365static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
b64f50a1 1366 (sect_offset offset, struct objfile *objfile);
ae038cb0 1367
9816fde3 1368static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1369 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1370
1371static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1372 struct die_info *comp_unit_die,
1373 enum language pretend_language);
93311388 1374
68dc6402 1375static void free_heap_comp_unit (void *);
ae038cb0
DJ
1376
1377static void free_cached_comp_units (void *);
1378
1379static void age_cached_comp_units (void);
1380
dee91e82 1381static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1382
f792889a
DJ
1383static struct type *set_die_type (struct die_info *, struct type *,
1384 struct dwarf2_cu *);
1c379e20 1385
ae038cb0
DJ
1386static void create_all_comp_units (struct objfile *);
1387
0e50663e 1388static int create_all_type_units (struct objfile *);
1fd400ff 1389
95554aad
TT
1390static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1391 enum language);
10b3939b 1392
95554aad
TT
1393static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1394 enum language);
10b3939b
DJ
1395
1396static void dwarf2_add_dependence (struct dwarf2_cu *,
1397 struct dwarf2_per_cu_data *);
1398
ae038cb0
DJ
1399static void dwarf2_mark (struct dwarf2_cu *);
1400
1401static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1402
b64f50a1 1403static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1404 struct dwarf2_per_cu_data *per_cu);
1405
f792889a 1406static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1407
9291a0cd
TT
1408static void dwarf2_release_queue (void *dummy);
1409
95554aad
TT
1410static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1411 enum language pretend_language);
1412
1413static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1414 struct dwarf2_per_cu_data *per_cu,
1415 enum language pretend_language);
9291a0cd 1416
a0f42c21 1417static void process_queue (void);
9291a0cd
TT
1418
1419static void find_file_and_directory (struct die_info *die,
1420 struct dwarf2_cu *cu,
1421 char **name, char **comp_dir);
1422
1423static char *file_full_name (int file, struct line_header *lh,
1424 const char *comp_dir);
1425
9ff913ba
DE
1426static gdb_byte *read_and_check_comp_unit_head
1427 (struct comp_unit_head *header,
1428 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1429 int is_debug_types_section);
9291a0cd 1430
fd820528
DE
1431static void init_cutu_and_read_dies
1432 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
3019eac3
DE
1433 die_reader_func_ftype *die_reader_func, void *data);
1434
dee91e82
DE
1435static void init_cutu_and_read_dies_simple
1436 (struct dwarf2_per_cu_data *this_cu,
1437 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1438
673bfd45 1439static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1440
95554aad 1441static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *, int);
dee91e82 1442
3019eac3
DE
1443static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1444
1445static struct dwo_unit *lookup_dwo_comp_unit
1446 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1447
1448static struct dwo_unit *lookup_dwo_type_unit
1449 (struct signatured_type *, char *, const char *);
1450
1451static void free_dwo_file_cleanup (void *);
1452
1453static void munmap_section_buffer (struct dwarf2_section_info *);
1454
95554aad
TT
1455static void process_cu_includes (void);
1456
9291a0cd
TT
1457#if WORDS_BIGENDIAN
1458
1459/* Convert VALUE between big- and little-endian. */
1460static offset_type
1461byte_swap (offset_type value)
1462{
1463 offset_type result;
1464
1465 result = (value & 0xff) << 24;
1466 result |= (value & 0xff00) << 8;
1467 result |= (value & 0xff0000) >> 8;
1468 result |= (value & 0xff000000) >> 24;
1469 return result;
1470}
1471
1472#define MAYBE_SWAP(V) byte_swap (V)
1473
1474#else
1475#define MAYBE_SWAP(V) (V)
1476#endif /* WORDS_BIGENDIAN */
1477
1478/* The suffix for an index file. */
1479#define INDEX_SUFFIX ".gdb-index"
1480
3da10d80
KS
1481static const char *dwarf2_physname (char *name, struct die_info *die,
1482 struct dwarf2_cu *cu);
1483
c906108c 1484/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1485 information and return true if we have enough to do something.
1486 NAMES points to the dwarf2 section names, or is NULL if the standard
1487 ELF names are used. */
c906108c
SS
1488
1489int
251d32d9
TG
1490dwarf2_has_info (struct objfile *objfile,
1491 const struct dwarf2_debug_sections *names)
c906108c 1492{
be391dca
TT
1493 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1494 if (!dwarf2_per_objfile)
1495 {
1496 /* Initialize per-objfile state. */
1497 struct dwarf2_per_objfile *data
1498 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1499
be391dca
TT
1500 memset (data, 0, sizeof (*data));
1501 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1502 dwarf2_per_objfile = data;
6502dd73 1503
251d32d9
TG
1504 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1505 (void *) names);
be391dca
TT
1506 dwarf2_per_objfile->objfile = objfile;
1507 }
1508 return (dwarf2_per_objfile->info.asection != NULL
1509 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1510}
1511
251d32d9
TG
1512/* When loading sections, we look either for uncompressed section or for
1513 compressed section names. */
233a11ab
CS
1514
1515static int
251d32d9
TG
1516section_is_p (const char *section_name,
1517 const struct dwarf2_section_names *names)
233a11ab 1518{
251d32d9
TG
1519 if (names->normal != NULL
1520 && strcmp (section_name, names->normal) == 0)
1521 return 1;
1522 if (names->compressed != NULL
1523 && strcmp (section_name, names->compressed) == 0)
1524 return 1;
1525 return 0;
233a11ab
CS
1526}
1527
c906108c
SS
1528/* This function is mapped across the sections and remembers the
1529 offset and size of each of the debugging sections we are interested
1530 in. */
1531
1532static void
251d32d9 1533dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1534{
251d32d9
TG
1535 const struct dwarf2_debug_sections *names;
1536
1537 if (vnames == NULL)
1538 names = &dwarf2_elf_names;
1539 else
1540 names = (const struct dwarf2_debug_sections *) vnames;
1541
1542 if (section_is_p (sectp->name, &names->info))
c906108c 1543 {
dce234bc
PP
1544 dwarf2_per_objfile->info.asection = sectp;
1545 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1546 }
251d32d9 1547 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1548 {
dce234bc
PP
1549 dwarf2_per_objfile->abbrev.asection = sectp;
1550 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1551 }
251d32d9 1552 else if (section_is_p (sectp->name, &names->line))
c906108c 1553 {
dce234bc
PP
1554 dwarf2_per_objfile->line.asection = sectp;
1555 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1556 }
251d32d9 1557 else if (section_is_p (sectp->name, &names->loc))
c906108c 1558 {
dce234bc
PP
1559 dwarf2_per_objfile->loc.asection = sectp;
1560 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1561 }
251d32d9 1562 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1563 {
dce234bc
PP
1564 dwarf2_per_objfile->macinfo.asection = sectp;
1565 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1566 }
cf2c3c16
TT
1567 else if (section_is_p (sectp->name, &names->macro))
1568 {
1569 dwarf2_per_objfile->macro.asection = sectp;
1570 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1571 }
251d32d9 1572 else if (section_is_p (sectp->name, &names->str))
c906108c 1573 {
dce234bc
PP
1574 dwarf2_per_objfile->str.asection = sectp;
1575 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1576 }
3019eac3
DE
1577 else if (section_is_p (sectp->name, &names->addr))
1578 {
1579 dwarf2_per_objfile->addr.asection = sectp;
1580 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1581 }
251d32d9 1582 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1583 {
dce234bc
PP
1584 dwarf2_per_objfile->frame.asection = sectp;
1585 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1586 }
251d32d9 1587 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1588 {
81b9b86e 1589 flagword aflag = bfd_get_section_flags (abfd, sectp);
9a619af0 1590
3799ccc6
EZ
1591 if (aflag & SEC_HAS_CONTENTS)
1592 {
dce234bc
PP
1593 dwarf2_per_objfile->eh_frame.asection = sectp;
1594 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1595 }
b6af0555 1596 }
251d32d9 1597 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1598 {
dce234bc
PP
1599 dwarf2_per_objfile->ranges.asection = sectp;
1600 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1601 }
251d32d9 1602 else if (section_is_p (sectp->name, &names->types))
348e048f 1603 {
8b70b953
TT
1604 struct dwarf2_section_info type_section;
1605
1606 memset (&type_section, 0, sizeof (type_section));
1607 type_section.asection = sectp;
1608 type_section.size = bfd_get_section_size (sectp);
1609
1610 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1611 &type_section);
348e048f 1612 }
251d32d9 1613 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1614 {
1615 dwarf2_per_objfile->gdb_index.asection = sectp;
1616 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1617 }
dce234bc 1618
72dca2f5
FR
1619 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1620 && bfd_section_vma (abfd, sectp) == 0)
1621 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1622}
1623
dce234bc
PP
1624/* Decompress a section that was compressed using zlib. Store the
1625 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1626
1627static void
dce234bc
PP
1628zlib_decompress_section (struct objfile *objfile, asection *sectp,
1629 gdb_byte **outbuf, bfd_size_type *outsize)
1630{
3019eac3 1631 bfd *abfd = sectp->owner;
dce234bc
PP
1632#ifndef HAVE_ZLIB_H
1633 error (_("Support for zlib-compressed DWARF data (from '%s') "
1634 "is disabled in this copy of GDB"),
1635 bfd_get_filename (abfd));
1636#else
1637 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1638 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1639 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1640 bfd_size_type uncompressed_size;
1641 gdb_byte *uncompressed_buffer;
1642 z_stream strm;
1643 int rc;
1644 int header_size = 12;
1645
1646 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1647 || bfd_bread (compressed_buffer,
1648 compressed_size, abfd) != compressed_size)
dce234bc
PP
1649 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1650 bfd_get_filename (abfd));
1651
1652 /* Read the zlib header. In this case, it should be "ZLIB" followed
1653 by the uncompressed section size, 8 bytes in big-endian order. */
1654 if (compressed_size < header_size
1655 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1656 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1657 bfd_get_filename (abfd));
1658 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1659 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1660 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1661 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1662 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1663 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1664 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1665 uncompressed_size += compressed_buffer[11];
1666
1667 /* It is possible the section consists of several compressed
1668 buffers concatenated together, so we uncompress in a loop. */
1669 strm.zalloc = NULL;
1670 strm.zfree = NULL;
1671 strm.opaque = NULL;
1672 strm.avail_in = compressed_size - header_size;
1673 strm.next_in = (Bytef*) compressed_buffer + header_size;
1674 strm.avail_out = uncompressed_size;
1675 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1676 uncompressed_size);
1677 rc = inflateInit (&strm);
1678 while (strm.avail_in > 0)
1679 {
1680 if (rc != Z_OK)
1681 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1682 bfd_get_filename (abfd), rc);
1683 strm.next_out = ((Bytef*) uncompressed_buffer
1684 + (uncompressed_size - strm.avail_out));
1685 rc = inflate (&strm, Z_FINISH);
1686 if (rc != Z_STREAM_END)
1687 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1688 bfd_get_filename (abfd), rc);
1689 rc = inflateReset (&strm);
1690 }
1691 rc = inflateEnd (&strm);
1692 if (rc != Z_OK
1693 || strm.avail_out != 0)
1694 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1695 bfd_get_filename (abfd), rc);
1696
affddf13 1697 do_cleanups (cleanup);
dce234bc
PP
1698 *outbuf = uncompressed_buffer;
1699 *outsize = uncompressed_size;
1700#endif
233a11ab
CS
1701}
1702
fceca515
DE
1703/* A helper function that decides whether a section is empty,
1704 or not present. */
9e0ac564
TT
1705
1706static int
1707dwarf2_section_empty_p (struct dwarf2_section_info *info)
1708{
1709 return info->asection == NULL || info->size == 0;
1710}
1711
3019eac3
DE
1712/* Read the contents of the section INFO.
1713 OBJFILE is the main object file, but not necessarily the file where
1714 the section comes from. E.g., for DWO files INFO->asection->owner
1715 is the bfd of the DWO file.
dce234bc 1716 If the section is compressed, uncompress it before returning. */
c906108c 1717
dce234bc
PP
1718static void
1719dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1720{
dce234bc 1721 asection *sectp = info->asection;
3019eac3 1722 bfd *abfd;
dce234bc
PP
1723 gdb_byte *buf, *retbuf;
1724 unsigned char header[4];
c906108c 1725
be391dca
TT
1726 if (info->readin)
1727 return;
dce234bc 1728 info->buffer = NULL;
b315ab21 1729 info->map_addr = NULL;
be391dca 1730 info->readin = 1;
188dd5d6 1731
9e0ac564 1732 if (dwarf2_section_empty_p (info))
dce234bc 1733 return;
c906108c 1734
3019eac3
DE
1735 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1736 abfd = sectp->owner;
1737
dce234bc
PP
1738 /* Check if the file has a 4-byte header indicating compression. */
1739 if (info->size > sizeof (header)
1740 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1741 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1742 {
1743 /* Upon decompression, update the buffer and its size. */
1744 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1745 {
1746 zlib_decompress_section (objfile, sectp, &info->buffer,
1747 &info->size);
1748 return;
1749 }
1750 }
4bdf3d34 1751
dce234bc
PP
1752#ifdef HAVE_MMAP
1753 if (pagesize == 0)
1754 pagesize = getpagesize ();
2e276125 1755
dce234bc
PP
1756 /* Only try to mmap sections which are large enough: we don't want to
1757 waste space due to fragmentation. Also, only try mmap for sections
1758 without relocations. */
1759
1760 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1761 {
b315ab21
TG
1762 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1763 MAP_PRIVATE, sectp->filepos,
1764 &info->map_addr, &info->map_len);
dce234bc 1765
b315ab21 1766 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1767 {
be391dca 1768#if HAVE_POSIX_MADVISE
b315ab21 1769 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1770#endif
dce234bc
PP
1771 return;
1772 }
1773 }
1774#endif
1775
1776 /* If we get here, we are a normal, not-compressed section. */
1777 info->buffer = buf
1778 = obstack_alloc (&objfile->objfile_obstack, info->size);
1779
1780 /* When debugging .o files, we may need to apply relocations; see
1781 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1782 We never compress sections in .o files, so we only need to
1783 try this when the section is not compressed. */
ac8035ab 1784 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1785 if (retbuf != NULL)
1786 {
1787 info->buffer = retbuf;
1788 return;
1789 }
1790
1791 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1792 || bfd_bread (buf, info->size, abfd) != info->size)
1793 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1794 bfd_get_filename (abfd));
1795}
1796
9e0ac564
TT
1797/* A helper function that returns the size of a section in a safe way.
1798 If you are positive that the section has been read before using the
1799 size, then it is safe to refer to the dwarf2_section_info object's
1800 "size" field directly. In other cases, you must call this
1801 function, because for compressed sections the size field is not set
1802 correctly until the section has been read. */
1803
1804static bfd_size_type
1805dwarf2_section_size (struct objfile *objfile,
1806 struct dwarf2_section_info *info)
1807{
1808 if (!info->readin)
1809 dwarf2_read_section (objfile, info);
1810 return info->size;
1811}
1812
dce234bc 1813/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1814 SECTION_NAME. */
af34e669 1815
dce234bc 1816void
3017a003
TG
1817dwarf2_get_section_info (struct objfile *objfile,
1818 enum dwarf2_section_enum sect,
dce234bc
PP
1819 asection **sectp, gdb_byte **bufp,
1820 bfd_size_type *sizep)
1821{
1822 struct dwarf2_per_objfile *data
1823 = objfile_data (objfile, dwarf2_objfile_data_key);
1824 struct dwarf2_section_info *info;
a3b2a86b
TT
1825
1826 /* We may see an objfile without any DWARF, in which case we just
1827 return nothing. */
1828 if (data == NULL)
1829 {
1830 *sectp = NULL;
1831 *bufp = NULL;
1832 *sizep = 0;
1833 return;
1834 }
3017a003
TG
1835 switch (sect)
1836 {
1837 case DWARF2_DEBUG_FRAME:
1838 info = &data->frame;
1839 break;
1840 case DWARF2_EH_FRAME:
1841 info = &data->eh_frame;
1842 break;
1843 default:
1844 gdb_assert_not_reached ("unexpected section");
1845 }
dce234bc 1846
9e0ac564 1847 dwarf2_read_section (objfile, info);
dce234bc
PP
1848
1849 *sectp = info->asection;
1850 *bufp = info->buffer;
1851 *sizep = info->size;
1852}
1853
9291a0cd 1854\f
7b9f3c50
DE
1855/* DWARF quick_symbols_functions support. */
1856
1857/* TUs can share .debug_line entries, and there can be a lot more TUs than
1858 unique line tables, so we maintain a separate table of all .debug_line
1859 derived entries to support the sharing.
1860 All the quick functions need is the list of file names. We discard the
1861 line_header when we're done and don't need to record it here. */
1862struct quick_file_names
1863{
1864 /* The offset in .debug_line of the line table. We hash on this. */
1865 unsigned int offset;
1866
1867 /* The number of entries in file_names, real_names. */
1868 unsigned int num_file_names;
1869
1870 /* The file names from the line table, after being run through
1871 file_full_name. */
1872 const char **file_names;
1873
1874 /* The file names from the line table after being run through
1875 gdb_realpath. These are computed lazily. */
1876 const char **real_names;
1877};
1878
1879/* When using the index (and thus not using psymtabs), each CU has an
1880 object of this type. This is used to hold information needed by
1881 the various "quick" methods. */
1882struct dwarf2_per_cu_quick_data
1883{
1884 /* The file table. This can be NULL if there was no file table
1885 or it's currently not read in.
1886 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1887 struct quick_file_names *file_names;
1888
1889 /* The corresponding symbol table. This is NULL if symbols for this
1890 CU have not yet been read. */
1891 struct symtab *symtab;
1892
1893 /* A temporary mark bit used when iterating over all CUs in
1894 expand_symtabs_matching. */
1895 unsigned int mark : 1;
1896
1897 /* True if we've tried to read the file table and found there isn't one.
1898 There will be no point in trying to read it again next time. */
1899 unsigned int no_file_data : 1;
1900};
1901
1902/* Hash function for a quick_file_names. */
1903
1904static hashval_t
1905hash_file_name_entry (const void *e)
1906{
1907 const struct quick_file_names *file_data = e;
1908
1909 return file_data->offset;
1910}
1911
1912/* Equality function for a quick_file_names. */
1913
1914static int
1915eq_file_name_entry (const void *a, const void *b)
1916{
1917 const struct quick_file_names *ea = a;
1918 const struct quick_file_names *eb = b;
1919
1920 return ea->offset == eb->offset;
1921}
1922
1923/* Delete function for a quick_file_names. */
1924
1925static void
1926delete_file_name_entry (void *e)
1927{
1928 struct quick_file_names *file_data = e;
1929 int i;
1930
1931 for (i = 0; i < file_data->num_file_names; ++i)
1932 {
1933 xfree ((void*) file_data->file_names[i]);
1934 if (file_data->real_names)
1935 xfree ((void*) file_data->real_names[i]);
1936 }
1937
1938 /* The space for the struct itself lives on objfile_obstack,
1939 so we don't free it here. */
1940}
1941
1942/* Create a quick_file_names hash table. */
1943
1944static htab_t
1945create_quick_file_names_table (unsigned int nr_initial_entries)
1946{
1947 return htab_create_alloc (nr_initial_entries,
1948 hash_file_name_entry, eq_file_name_entry,
1949 delete_file_name_entry, xcalloc, xfree);
1950}
9291a0cd 1951
918dd910
JK
1952/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1953 have to be created afterwards. You should call age_cached_comp_units after
1954 processing PER_CU->CU. dw2_setup must have been already called. */
1955
1956static void
1957load_cu (struct dwarf2_per_cu_data *per_cu)
1958{
3019eac3 1959 if (per_cu->is_debug_types)
e5fe5e75 1960 load_full_type_unit (per_cu);
918dd910 1961 else
95554aad 1962 load_full_comp_unit (per_cu, language_minimal);
918dd910 1963
918dd910 1964 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
1965
1966 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
1967}
1968
a0f42c21 1969/* Read in the symbols for PER_CU. */
2fdf6df6 1970
9291a0cd 1971static void
a0f42c21 1972dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1973{
1974 struct cleanup *back_to;
1975
1976 back_to = make_cleanup (dwarf2_release_queue, NULL);
1977
95554aad
TT
1978 if (dwarf2_per_objfile->using_index
1979 ? per_cu->v.quick->symtab == NULL
1980 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
1981 {
1982 queue_comp_unit (per_cu, language_minimal);
1983 load_cu (per_cu);
1984 }
9291a0cd 1985
a0f42c21 1986 process_queue ();
9291a0cd
TT
1987
1988 /* Age the cache, releasing compilation units that have not
1989 been used recently. */
1990 age_cached_comp_units ();
1991
1992 do_cleanups (back_to);
1993}
1994
1995/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1996 the objfile from which this CU came. Returns the resulting symbol
1997 table. */
2fdf6df6 1998
9291a0cd 1999static struct symtab *
a0f42c21 2000dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2001{
95554aad 2002 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2003 if (!per_cu->v.quick->symtab)
2004 {
2005 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2006 increment_reading_symtab ();
a0f42c21 2007 dw2_do_instantiate_symtab (per_cu);
95554aad 2008 process_cu_includes ();
9291a0cd
TT
2009 do_cleanups (back_to);
2010 }
2011 return per_cu->v.quick->symtab;
2012}
2013
1fd400ff 2014/* Return the CU given its index. */
2fdf6df6 2015
1fd400ff
TT
2016static struct dwarf2_per_cu_data *
2017dw2_get_cu (int index)
2018{
2019 if (index >= dwarf2_per_objfile->n_comp_units)
2020 {
2021 index -= dwarf2_per_objfile->n_comp_units;
d467dd73 2022 return dwarf2_per_objfile->all_type_units[index];
1fd400ff
TT
2023 }
2024 return dwarf2_per_objfile->all_comp_units[index];
2025}
2026
9291a0cd
TT
2027/* A helper function that knows how to read a 64-bit value in a way
2028 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2029 otherwise. */
2fdf6df6 2030
9291a0cd
TT
2031static int
2032extract_cu_value (const char *bytes, ULONGEST *result)
2033{
2034 if (sizeof (ULONGEST) < 8)
2035 {
2036 int i;
2037
2038 /* Ignore the upper 4 bytes if they are all zero. */
2039 for (i = 0; i < 4; ++i)
2040 if (bytes[i + 4] != 0)
2041 return 0;
2042
2043 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2044 }
2045 else
2046 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2047 return 1;
2048}
2049
2050/* Read the CU list from the mapped index, and use it to create all
2051 the CU objects for this objfile. Return 0 if something went wrong,
2052 1 if everything went ok. */
2fdf6df6 2053
9291a0cd 2054static int
1fd400ff
TT
2055create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2056 offset_type cu_list_elements)
9291a0cd
TT
2057{
2058 offset_type i;
9291a0cd
TT
2059
2060 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2061 dwarf2_per_objfile->all_comp_units
2062 = obstack_alloc (&objfile->objfile_obstack,
2063 dwarf2_per_objfile->n_comp_units
2064 * sizeof (struct dwarf2_per_cu_data *));
2065
2066 for (i = 0; i < cu_list_elements; i += 2)
2067 {
2068 struct dwarf2_per_cu_data *the_cu;
2069 ULONGEST offset, length;
2070
2071 if (!extract_cu_value (cu_list, &offset)
2072 || !extract_cu_value (cu_list + 8, &length))
2073 return 0;
2074 cu_list += 2 * 8;
2075
2076 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2077 struct dwarf2_per_cu_data);
b64f50a1 2078 the_cu->offset.sect_off = offset;
9291a0cd
TT
2079 the_cu->length = length;
2080 the_cu->objfile = objfile;
3019eac3 2081 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
9291a0cd
TT
2082 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2083 struct dwarf2_per_cu_quick_data);
2084 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2085 }
2086
2087 return 1;
2088}
2089
1fd400ff 2090/* Create the signatured type hash table from the index. */
673bfd45 2091
1fd400ff 2092static int
673bfd45 2093create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2094 struct dwarf2_section_info *section,
673bfd45
DE
2095 const gdb_byte *bytes,
2096 offset_type elements)
1fd400ff
TT
2097{
2098 offset_type i;
673bfd45 2099 htab_t sig_types_hash;
1fd400ff 2100
d467dd73
DE
2101 dwarf2_per_objfile->n_type_units = elements / 3;
2102 dwarf2_per_objfile->all_type_units
1fd400ff 2103 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2104 dwarf2_per_objfile->n_type_units
1fd400ff
TT
2105 * sizeof (struct dwarf2_per_cu_data *));
2106
673bfd45 2107 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2108
2109 for (i = 0; i < elements; i += 3)
2110 {
52dc124a
DE
2111 struct signatured_type *sig_type;
2112 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2113 void **slot;
2114
2115 if (!extract_cu_value (bytes, &offset)
52dc124a 2116 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
2117 return 0;
2118 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2119 bytes += 3 * 8;
2120
52dc124a 2121 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2122 struct signatured_type);
52dc124a 2123 sig_type->signature = signature;
3019eac3
DE
2124 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2125 sig_type->per_cu.is_debug_types = 1;
2126 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2127 sig_type->per_cu.offset.sect_off = offset;
2128 sig_type->per_cu.objfile = objfile;
2129 sig_type->per_cu.v.quick
1fd400ff
TT
2130 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2131 struct dwarf2_per_cu_quick_data);
2132
52dc124a
DE
2133 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2134 *slot = sig_type;
1fd400ff 2135
52dc124a 2136 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
1fd400ff
TT
2137 }
2138
673bfd45 2139 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2140
2141 return 1;
2142}
2143
9291a0cd
TT
2144/* Read the address map data from the mapped index, and use it to
2145 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2146
9291a0cd
TT
2147static void
2148create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2149{
2150 const gdb_byte *iter, *end;
2151 struct obstack temp_obstack;
2152 struct addrmap *mutable_map;
2153 struct cleanup *cleanup;
2154 CORE_ADDR baseaddr;
2155
2156 obstack_init (&temp_obstack);
2157 cleanup = make_cleanup_obstack_free (&temp_obstack);
2158 mutable_map = addrmap_create_mutable (&temp_obstack);
2159
2160 iter = index->address_table;
2161 end = iter + index->address_table_size;
2162
2163 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2164
2165 while (iter < end)
2166 {
2167 ULONGEST hi, lo, cu_index;
2168 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2169 iter += 8;
2170 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2171 iter += 8;
2172 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2173 iter += 4;
2174
2175 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2176 dw2_get_cu (cu_index));
9291a0cd
TT
2177 }
2178
2179 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2180 &objfile->objfile_obstack);
2181 do_cleanups (cleanup);
2182}
2183
59d7bcaf
JK
2184/* The hash function for strings in the mapped index. This is the same as
2185 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2186 implementation. This is necessary because the hash function is tied to the
2187 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2188 SYMBOL_HASH_NEXT.
2189
2190 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2191
9291a0cd 2192static hashval_t
559a7a62 2193mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2194{
2195 const unsigned char *str = (const unsigned char *) p;
2196 hashval_t r = 0;
2197 unsigned char c;
2198
2199 while ((c = *str++) != 0)
559a7a62
JK
2200 {
2201 if (index_version >= 5)
2202 c = tolower (c);
2203 r = r * 67 + c - 113;
2204 }
9291a0cd
TT
2205
2206 return r;
2207}
2208
2209/* Find a slot in the mapped index INDEX for the object named NAME.
2210 If NAME is found, set *VEC_OUT to point to the CU vector in the
2211 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2212
9291a0cd
TT
2213static int
2214find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2215 offset_type **vec_out)
2216{
0cf03b49
JK
2217 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2218 offset_type hash;
9291a0cd 2219 offset_type slot, step;
559a7a62 2220 int (*cmp) (const char *, const char *);
9291a0cd 2221
0cf03b49
JK
2222 if (current_language->la_language == language_cplus
2223 || current_language->la_language == language_java
2224 || current_language->la_language == language_fortran)
2225 {
2226 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2227 not contain any. */
2228 const char *paren = strchr (name, '(');
2229
2230 if (paren)
2231 {
2232 char *dup;
2233
2234 dup = xmalloc (paren - name + 1);
2235 memcpy (dup, name, paren - name);
2236 dup[paren - name] = 0;
2237
2238 make_cleanup (xfree, dup);
2239 name = dup;
2240 }
2241 }
2242
559a7a62 2243 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2244 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2245 simulate our NAME being searched is also lowercased. */
2246 hash = mapped_index_string_hash ((index->version == 4
2247 && case_sensitivity == case_sensitive_off
2248 ? 5 : index->version),
2249 name);
2250
3876f04e
DE
2251 slot = hash & (index->symbol_table_slots - 1);
2252 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2253 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2254
2255 for (;;)
2256 {
2257 /* Convert a slot number to an offset into the table. */
2258 offset_type i = 2 * slot;
2259 const char *str;
3876f04e 2260 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2261 {
2262 do_cleanups (back_to);
2263 return 0;
2264 }
9291a0cd 2265
3876f04e 2266 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2267 if (!cmp (name, str))
9291a0cd
TT
2268 {
2269 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2270 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2271 do_cleanups (back_to);
9291a0cd
TT
2272 return 1;
2273 }
2274
3876f04e 2275 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2276 }
2277}
2278
2279/* Read the index file. If everything went ok, initialize the "quick"
2280 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2281
9291a0cd
TT
2282static int
2283dwarf2_read_index (struct objfile *objfile)
2284{
9291a0cd
TT
2285 char *addr;
2286 struct mapped_index *map;
b3b272e1 2287 offset_type *metadata;
ac0b195c
KW
2288 const gdb_byte *cu_list;
2289 const gdb_byte *types_list = NULL;
2290 offset_type version, cu_list_elements;
2291 offset_type types_list_elements = 0;
1fd400ff 2292 int i;
9291a0cd 2293
9e0ac564 2294 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2295 return 0;
82430852
JK
2296
2297 /* Older elfutils strip versions could keep the section in the main
2298 executable while splitting it for the separate debug info file. */
2299 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2300 & SEC_HAS_CONTENTS) == 0)
2301 return 0;
2302
9291a0cd
TT
2303 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2304
2305 addr = dwarf2_per_objfile->gdb_index.buffer;
2306 /* Version check. */
1fd400ff 2307 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2308 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2309 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2310 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2311 indices. */
831adc1f 2312 if (version < 4)
481860b3
GB
2313 {
2314 static int warning_printed = 0;
2315 if (!warning_printed)
2316 {
2317 warning (_("Skipping obsolete .gdb_index section in %s."),
2318 objfile->name);
2319 warning_printed = 1;
2320 }
2321 return 0;
2322 }
2323 /* Index version 4 uses a different hash function than index version
2324 5 and later.
2325
2326 Versions earlier than 6 did not emit psymbols for inlined
2327 functions. Using these files will cause GDB not to be able to
2328 set breakpoints on inlined functions by name, so we ignore these
2329 indices unless the --use-deprecated-index-sections command line
2330 option was supplied. */
2331 if (version < 6 && !use_deprecated_index_sections)
2332 {
2333 static int warning_printed = 0;
2334 if (!warning_printed)
2335 {
2336 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2337 "--use-deprecated-index-sections to use them anyway"),
2338 objfile->name);
2339 warning_printed = 1;
2340 }
2341 return 0;
2342 }
2343 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2344 longer backward compatible. */
481860b3 2345 if (version > 6)
594e8718 2346 return 0;
9291a0cd
TT
2347
2348 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2349 map->version = version;
b3b272e1 2350 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2351
2352 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2353
2354 i = 0;
2355 cu_list = addr + MAYBE_SWAP (metadata[i]);
2356 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2357 / 8);
1fd400ff
TT
2358 ++i;
2359
987d643c
TT
2360 types_list = addr + MAYBE_SWAP (metadata[i]);
2361 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2362 - MAYBE_SWAP (metadata[i]))
2363 / 8);
2364 ++i;
1fd400ff
TT
2365
2366 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2367 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2368 - MAYBE_SWAP (metadata[i]));
2369 ++i;
2370
3876f04e
DE
2371 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2372 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2373 - MAYBE_SWAP (metadata[i]))
2374 / (2 * sizeof (offset_type)));
1fd400ff 2375 ++i;
9291a0cd 2376
1fd400ff
TT
2377 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2378
0fefef59
DE
2379 /* Don't use the index if it's empty. */
2380 if (map->symbol_table_slots == 0)
2381 return 0;
2382
1fd400ff
TT
2383 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2384 return 0;
2385
8b70b953
TT
2386 if (types_list_elements)
2387 {
2388 struct dwarf2_section_info *section;
2389
2390 /* We can only handle a single .debug_types when we have an
2391 index. */
2392 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2393 return 0;
2394
2395 section = VEC_index (dwarf2_section_info_def,
2396 dwarf2_per_objfile->types, 0);
2397
2398 if (!create_signatured_type_table_from_index (objfile, section,
2399 types_list,
2400 types_list_elements))
2401 return 0;
2402 }
9291a0cd
TT
2403
2404 create_addrmap_from_index (objfile, map);
2405
2406 dwarf2_per_objfile->index_table = map;
2407 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2408 dwarf2_per_objfile->quick_file_names_table =
2409 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2410
2411 return 1;
2412}
2413
2414/* A helper for the "quick" functions which sets the global
2415 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2416
9291a0cd
TT
2417static void
2418dw2_setup (struct objfile *objfile)
2419{
2420 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2421 gdb_assert (dwarf2_per_objfile);
2422}
2423
dee91e82 2424/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2425
dee91e82
DE
2426static void
2427dw2_get_file_names_reader (const struct die_reader_specs *reader,
2428 gdb_byte *info_ptr,
2429 struct die_info *comp_unit_die,
2430 int has_children,
2431 void *data)
9291a0cd 2432{
dee91e82
DE
2433 struct dwarf2_cu *cu = reader->cu;
2434 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2435 struct objfile *objfile = dwarf2_per_objfile->objfile;
7b9f3c50 2436 struct line_header *lh;
9291a0cd 2437 struct attribute *attr;
dee91e82 2438 int i;
9291a0cd 2439 char *name, *comp_dir;
7b9f3c50
DE
2440 void **slot;
2441 struct quick_file_names *qfn;
2442 unsigned int line_offset;
9291a0cd 2443
7b9f3c50
DE
2444 lh = NULL;
2445 slot = NULL;
2446 line_offset = 0;
dee91e82
DE
2447
2448 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2449 if (attr)
2450 {
7b9f3c50
DE
2451 struct quick_file_names find_entry;
2452
2453 line_offset = DW_UNSND (attr);
2454
2455 /* We may have already read in this line header (TU line header sharing).
2456 If we have we're done. */
2457 find_entry.offset = line_offset;
2458 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2459 &find_entry, INSERT);
2460 if (*slot != NULL)
2461 {
7b9f3c50 2462 this_cu->v.quick->file_names = *slot;
dee91e82 2463 return;
7b9f3c50
DE
2464 }
2465
3019eac3 2466 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2467 }
2468 if (lh == NULL)
2469 {
7b9f3c50 2470 this_cu->v.quick->no_file_data = 1;
dee91e82 2471 return;
9291a0cd
TT
2472 }
2473
7b9f3c50
DE
2474 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2475 qfn->offset = line_offset;
2476 gdb_assert (slot != NULL);
2477 *slot = qfn;
9291a0cd 2478
dee91e82 2479 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2480
7b9f3c50
DE
2481 qfn->num_file_names = lh->num_file_names;
2482 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2483 lh->num_file_names * sizeof (char *));
9291a0cd 2484 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2485 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2486 qfn->real_names = NULL;
9291a0cd 2487
7b9f3c50 2488 free_line_header (lh);
7b9f3c50
DE
2489
2490 this_cu->v.quick->file_names = qfn;
dee91e82
DE
2491}
2492
2493/* A helper for the "quick" functions which attempts to read the line
2494 table for THIS_CU. */
2495
2496static struct quick_file_names *
2497dw2_get_file_names (struct objfile *objfile,
2498 struct dwarf2_per_cu_data *this_cu)
2499{
2500 if (this_cu->v.quick->file_names != NULL)
2501 return this_cu->v.quick->file_names;
2502 /* If we know there is no line data, no point in looking again. */
2503 if (this_cu->v.quick->no_file_data)
2504 return NULL;
2505
3019eac3
DE
2506 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2507 in the stub for CUs, there's is no need to lookup the DWO file.
2508 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2509 DWO file. */
2510 if (this_cu->is_debug_types)
fd820528 2511 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
3019eac3
DE
2512 else
2513 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2514
2515 if (this_cu->v.quick->no_file_data)
2516 return NULL;
2517 return this_cu->v.quick->file_names;
9291a0cd
TT
2518}
2519
2520/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2521 real path for a given file name from the line table. */
2fdf6df6 2522
9291a0cd 2523static const char *
7b9f3c50
DE
2524dw2_get_real_path (struct objfile *objfile,
2525 struct quick_file_names *qfn, int index)
9291a0cd 2526{
7b9f3c50
DE
2527 if (qfn->real_names == NULL)
2528 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2529 qfn->num_file_names, sizeof (char *));
9291a0cd 2530
7b9f3c50
DE
2531 if (qfn->real_names[index] == NULL)
2532 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2533
7b9f3c50 2534 return qfn->real_names[index];
9291a0cd
TT
2535}
2536
2537static struct symtab *
2538dw2_find_last_source_symtab (struct objfile *objfile)
2539{
2540 int index;
ae2de4f8 2541
9291a0cd
TT
2542 dw2_setup (objfile);
2543 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2544 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2545}
2546
7b9f3c50
DE
2547/* Traversal function for dw2_forget_cached_source_info. */
2548
2549static int
2550dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2551{
7b9f3c50 2552 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2553
7b9f3c50 2554 if (file_data->real_names)
9291a0cd 2555 {
7b9f3c50 2556 int i;
9291a0cd 2557
7b9f3c50 2558 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2559 {
7b9f3c50
DE
2560 xfree ((void*) file_data->real_names[i]);
2561 file_data->real_names[i] = NULL;
9291a0cd
TT
2562 }
2563 }
7b9f3c50
DE
2564
2565 return 1;
2566}
2567
2568static void
2569dw2_forget_cached_source_info (struct objfile *objfile)
2570{
2571 dw2_setup (objfile);
2572
2573 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2574 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2575}
2576
f8eba3c6
TT
2577/* Helper function for dw2_map_symtabs_matching_filename that expands
2578 the symtabs and calls the iterator. */
2579
2580static int
2581dw2_map_expand_apply (struct objfile *objfile,
2582 struct dwarf2_per_cu_data *per_cu,
2583 const char *name,
2584 const char *full_path, const char *real_path,
2585 int (*callback) (struct symtab *, void *),
2586 void *data)
2587{
2588 struct symtab *last_made = objfile->symtabs;
2589
2590 /* Don't visit already-expanded CUs. */
2591 if (per_cu->v.quick->symtab)
2592 return 0;
2593
2594 /* This may expand more than one symtab, and we want to iterate over
2595 all of them. */
a0f42c21 2596 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2597
2598 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2599 objfile->symtabs, last_made);
2600}
2601
2602/* Implementation of the map_symtabs_matching_filename method. */
2603
9291a0cd 2604static int
f8eba3c6
TT
2605dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2606 const char *full_path, const char *real_path,
2607 int (*callback) (struct symtab *, void *),
2608 void *data)
9291a0cd
TT
2609{
2610 int i;
c011a4f4 2611 const char *name_basename = lbasename (name);
4aac40c8
TT
2612 int name_len = strlen (name);
2613 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
2614
2615 dw2_setup (objfile);
ae2de4f8 2616
1fd400ff 2617 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2618 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2619 {
2620 int j;
e254ef6a 2621 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2622 struct quick_file_names *file_data;
9291a0cd 2623
3d7bb9d9 2624 /* We only need to look at symtabs not already expanded. */
e254ef6a 2625 if (per_cu->v.quick->symtab)
9291a0cd
TT
2626 continue;
2627
7b9f3c50
DE
2628 file_data = dw2_get_file_names (objfile, per_cu);
2629 if (file_data == NULL)
9291a0cd
TT
2630 continue;
2631
7b9f3c50 2632 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2633 {
7b9f3c50 2634 const char *this_name = file_data->file_names[j];
9291a0cd 2635
4aac40c8
TT
2636 if (FILENAME_CMP (name, this_name) == 0
2637 || (!is_abs && compare_filenames_for_search (this_name,
2638 name, name_len)))
9291a0cd 2639 {
f8eba3c6
TT
2640 if (dw2_map_expand_apply (objfile, per_cu,
2641 name, full_path, real_path,
2642 callback, data))
2643 return 1;
4aac40c8 2644 }
9291a0cd 2645
c011a4f4
DE
2646 /* Before we invoke realpath, which can get expensive when many
2647 files are involved, do a quick comparison of the basenames. */
2648 if (! basenames_may_differ
2649 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2650 continue;
2651
9291a0cd
TT
2652 if (full_path != NULL)
2653 {
7b9f3c50
DE
2654 const char *this_real_name = dw2_get_real_path (objfile,
2655 file_data, j);
9291a0cd 2656
7b9f3c50 2657 if (this_real_name != NULL
4aac40c8
TT
2658 && (FILENAME_CMP (full_path, this_real_name) == 0
2659 || (!is_abs
2660 && compare_filenames_for_search (this_real_name,
2661 name, name_len))))
9291a0cd 2662 {
f8eba3c6
TT
2663 if (dw2_map_expand_apply (objfile, per_cu,
2664 name, full_path, real_path,
2665 callback, data))
2666 return 1;
9291a0cd
TT
2667 }
2668 }
2669
2670 if (real_path != NULL)
2671 {
7b9f3c50
DE
2672 const char *this_real_name = dw2_get_real_path (objfile,
2673 file_data, j);
9291a0cd 2674
7b9f3c50 2675 if (this_real_name != NULL
4aac40c8
TT
2676 && (FILENAME_CMP (real_path, this_real_name) == 0
2677 || (!is_abs
2678 && compare_filenames_for_search (this_real_name,
2679 name, name_len))))
9291a0cd 2680 {
f8eba3c6
TT
2681 if (dw2_map_expand_apply (objfile, per_cu,
2682 name, full_path, real_path,
2683 callback, data))
2684 return 1;
9291a0cd
TT
2685 }
2686 }
2687 }
2688 }
2689
9291a0cd
TT
2690 return 0;
2691}
2692
2693static struct symtab *
2694dw2_lookup_symbol (struct objfile *objfile, int block_index,
2695 const char *name, domain_enum domain)
2696{
774b6a14 2697 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2698 instead. */
2699 return NULL;
2700}
2701
2702/* A helper function that expands all symtabs that hold an object
2703 named NAME. */
2fdf6df6 2704
9291a0cd
TT
2705static void
2706dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2707{
2708 dw2_setup (objfile);
2709
ae2de4f8 2710 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2711 if (dwarf2_per_objfile->index_table)
2712 {
2713 offset_type *vec;
2714
2715 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2716 name, &vec))
2717 {
2718 offset_type i, len = MAYBE_SWAP (*vec);
2719 for (i = 0; i < len; ++i)
2720 {
2721 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2722 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2723
a0f42c21 2724 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2725 }
2726 }
2727 }
2728}
2729
774b6a14
TT
2730static void
2731dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2732 enum block_enum block_kind, const char *name,
774b6a14 2733 domain_enum domain)
9291a0cd 2734{
774b6a14 2735 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2736}
2737
2738static void
2739dw2_print_stats (struct objfile *objfile)
2740{
2741 int i, count;
2742
2743 dw2_setup (objfile);
2744 count = 0;
1fd400ff 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
e254ef6a 2750 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2751 ++count;
2752 }
2753 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2754}
2755
2756static void
2757dw2_dump (struct objfile *objfile)
2758{
2759 /* Nothing worth printing. */
2760}
2761
2762static void
2763dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2764 struct section_offsets *delta)
2765{
2766 /* There's nothing to relocate here. */
2767}
2768
2769static void
2770dw2_expand_symtabs_for_function (struct objfile *objfile,
2771 const char *func_name)
2772{
2773 dw2_do_expand_symtabs_matching (objfile, func_name);
2774}
2775
2776static void
2777dw2_expand_all_symtabs (struct objfile *objfile)
2778{
2779 int i;
2780
2781 dw2_setup (objfile);
1fd400ff
TT
2782
2783 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2784 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2785 {
e254ef6a 2786 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2787
a0f42c21 2788 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2789 }
2790}
2791
2792static void
2793dw2_expand_symtabs_with_filename (struct objfile *objfile,
2794 const char *filename)
2795{
2796 int i;
2797
2798 dw2_setup (objfile);
d4637a04
DE
2799
2800 /* We don't need to consider type units here.
2801 This is only called for examining code, e.g. expand_line_sal.
2802 There can be an order of magnitude (or more) more type units
2803 than comp units, and we avoid them if we can. */
2804
2805 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2806 {
2807 int j;
e254ef6a 2808 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2809 struct quick_file_names *file_data;
9291a0cd 2810
3d7bb9d9 2811 /* We only need to look at symtabs not already expanded. */
e254ef6a 2812 if (per_cu->v.quick->symtab)
9291a0cd
TT
2813 continue;
2814
7b9f3c50
DE
2815 file_data = dw2_get_file_names (objfile, per_cu);
2816 if (file_data == NULL)
9291a0cd
TT
2817 continue;
2818
7b9f3c50 2819 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2820 {
7b9f3c50 2821 const char *this_name = file_data->file_names[j];
1ef75ecc 2822 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2823 {
a0f42c21 2824 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2825 break;
2826 }
2827 }
2828 }
2829}
2830
dd786858 2831static const char *
9291a0cd
TT
2832dw2_find_symbol_file (struct objfile *objfile, const char *name)
2833{
e254ef6a 2834 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2835 offset_type *vec;
7b9f3c50 2836 struct quick_file_names *file_data;
9291a0cd
TT
2837
2838 dw2_setup (objfile);
2839
ae2de4f8 2840 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2841 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2842 {
2843 struct symtab *s;
2844
2845 ALL_OBJFILE_SYMTABS (objfile, s)
2846 if (s->primary)
2847 {
2848 struct blockvector *bv = BLOCKVECTOR (s);
2849 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2850 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2851
2852 if (sym)
2853 return sym->symtab->filename;
2854 }
2855 return NULL;
2856 }
9291a0cd
TT
2857
2858 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2859 name, &vec))
2860 return NULL;
2861
2862 /* Note that this just looks at the very first one named NAME -- but
2863 actually we are looking for a function. find_main_filename
2864 should be rewritten so that it doesn't require a custom hook. It
2865 could just use the ordinary symbol tables. */
2866 /* vec[0] is the length, which must always be >0. */
e254ef6a 2867 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2868
7b9f3c50 2869 file_data = dw2_get_file_names (objfile, per_cu);
943cb756
DE
2870 if (file_data == NULL
2871 || file_data->num_file_names == 0)
9291a0cd
TT
2872 return NULL;
2873
7b9f3c50 2874 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2875}
2876
2877static void
40658b94
PH
2878dw2_map_matching_symbols (const char * name, domain_enum namespace,
2879 struct objfile *objfile, int global,
2880 int (*callback) (struct block *,
2881 struct symbol *, void *),
2edb89d3
JK
2882 void *data, symbol_compare_ftype *match,
2883 symbol_compare_ftype *ordered_compare)
9291a0cd 2884{
40658b94 2885 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2886 current language is Ada for a non-Ada objfile using GNU index. As Ada
2887 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2888}
2889
2890static void
f8eba3c6
TT
2891dw2_expand_symtabs_matching
2892 (struct objfile *objfile,
2893 int (*file_matcher) (const char *, void *),
e078317b 2894 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
2895 enum search_domain kind,
2896 void *data)
9291a0cd
TT
2897{
2898 int i;
2899 offset_type iter;
4b5246aa 2900 struct mapped_index *index;
9291a0cd
TT
2901
2902 dw2_setup (objfile);
ae2de4f8
DE
2903
2904 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2905 if (!dwarf2_per_objfile->index_table)
2906 return;
4b5246aa 2907 index = dwarf2_per_objfile->index_table;
9291a0cd 2908
7b08b9eb 2909 if (file_matcher != NULL)
24c79950
TT
2910 {
2911 struct cleanup *cleanup;
2912 htab_t visited_found, visited_not_found;
2913
2914 visited_found = htab_create_alloc (10,
2915 htab_hash_pointer, htab_eq_pointer,
2916 NULL, xcalloc, xfree);
2917 cleanup = make_cleanup_htab_delete (visited_found);
2918 visited_not_found = htab_create_alloc (10,
2919 htab_hash_pointer, htab_eq_pointer,
2920 NULL, xcalloc, xfree);
2921 make_cleanup_htab_delete (visited_not_found);
2922
2923 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2924 + dwarf2_per_objfile->n_type_units); ++i)
2925 {
2926 int j;
2927 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2928 struct quick_file_names *file_data;
2929 void **slot;
7b08b9eb 2930
24c79950 2931 per_cu->v.quick->mark = 0;
3d7bb9d9 2932
24c79950
TT
2933 /* We only need to look at symtabs not already expanded. */
2934 if (per_cu->v.quick->symtab)
2935 continue;
7b08b9eb 2936
24c79950
TT
2937 file_data = dw2_get_file_names (objfile, per_cu);
2938 if (file_data == NULL)
2939 continue;
7b08b9eb 2940
24c79950
TT
2941 if (htab_find (visited_not_found, file_data) != NULL)
2942 continue;
2943 else if (htab_find (visited_found, file_data) != NULL)
2944 {
2945 per_cu->v.quick->mark = 1;
2946 continue;
2947 }
2948
2949 for (j = 0; j < file_data->num_file_names; ++j)
2950 {
2951 if (file_matcher (file_data->file_names[j], data))
2952 {
2953 per_cu->v.quick->mark = 1;
2954 break;
2955 }
2956 }
2957
2958 slot = htab_find_slot (per_cu->v.quick->mark
2959 ? visited_found
2960 : visited_not_found,
2961 file_data, INSERT);
2962 *slot = file_data;
2963 }
2964
2965 do_cleanups (cleanup);
2966 }
9291a0cd 2967
3876f04e 2968 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2969 {
2970 offset_type idx = 2 * iter;
2971 const char *name;
2972 offset_type *vec, vec_len, vec_idx;
2973
3876f04e 2974 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2975 continue;
2976
3876f04e 2977 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 2978
e078317b 2979 if (! (*name_matcher) (name, data))
9291a0cd
TT
2980 continue;
2981
2982 /* The name was matched, now expand corresponding CUs that were
2983 marked. */
4b5246aa 2984 vec = (offset_type *) (index->constant_pool
3876f04e 2985 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2986 vec_len = MAYBE_SWAP (vec[0]);
2987 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2988 {
e254ef6a 2989 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2990
e254ef6a 2991 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2992 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 2993 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2994 }
2995 }
2996}
2997
9703b513
TT
2998/* A helper for dw2_find_pc_sect_symtab which finds the most specific
2999 symtab. */
3000
3001static struct symtab *
3002recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3003{
3004 int i;
3005
3006 if (BLOCKVECTOR (symtab) != NULL
3007 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3008 return symtab;
3009
a3ec0bb1
DE
3010 if (symtab->includes == NULL)
3011 return NULL;
3012
9703b513
TT
3013 for (i = 0; symtab->includes[i]; ++i)
3014 {
a3ec0bb1 3015 struct symtab *s = symtab->includes[i];
9703b513
TT
3016
3017 s = recursively_find_pc_sect_symtab (s, pc);
3018 if (s != NULL)
3019 return s;
3020 }
3021
3022 return NULL;
3023}
3024
9291a0cd
TT
3025static struct symtab *
3026dw2_find_pc_sect_symtab (struct objfile *objfile,
3027 struct minimal_symbol *msymbol,
3028 CORE_ADDR pc,
3029 struct obj_section *section,
3030 int warn_if_readin)
3031{
3032 struct dwarf2_per_cu_data *data;
9703b513 3033 struct symtab *result;
9291a0cd
TT
3034
3035 dw2_setup (objfile);
3036
3037 if (!objfile->psymtabs_addrmap)
3038 return NULL;
3039
3040 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3041 if (!data)
3042 return NULL;
3043
3044 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3045 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3046 paddress (get_objfile_arch (objfile), pc));
3047
9703b513
TT
3048 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3049 gdb_assert (result != NULL);
3050 return result;
9291a0cd
TT
3051}
3052
9291a0cd 3053static void
44b13c5a 3054dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3055 void *data, int need_fullname)
9291a0cd
TT
3056{
3057 int i;
24c79950
TT
3058 struct cleanup *cleanup;
3059 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3060 NULL, xcalloc, xfree);
9291a0cd 3061
24c79950 3062 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3063 dw2_setup (objfile);
ae2de4f8 3064
24c79950
TT
3065 /* We can ignore file names coming from already-expanded CUs. */
3066 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3067 + dwarf2_per_objfile->n_type_units); ++i)
3068 {
3069 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3070
3071 if (per_cu->v.quick->symtab)
3072 {
3073 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3074 INSERT);
3075
3076 *slot = per_cu->v.quick->file_names;
3077 }
3078 }
3079
1fd400ff 3080 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3081 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
3082 {
3083 int j;
e254ef6a 3084 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3085 struct quick_file_names *file_data;
24c79950 3086 void **slot;
9291a0cd 3087
3d7bb9d9 3088 /* We only need to look at symtabs not already expanded. */
e254ef6a 3089 if (per_cu->v.quick->symtab)
9291a0cd
TT
3090 continue;
3091
7b9f3c50
DE
3092 file_data = dw2_get_file_names (objfile, per_cu);
3093 if (file_data == NULL)
9291a0cd
TT
3094 continue;
3095
24c79950
TT
3096 slot = htab_find_slot (visited, file_data, INSERT);
3097 if (*slot)
3098 {
3099 /* Already visited. */
3100 continue;
3101 }
3102 *slot = file_data;
3103
7b9f3c50 3104 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3105 {
74e2f255
DE
3106 const char *this_real_name;
3107
3108 if (need_fullname)
3109 this_real_name = dw2_get_real_path (objfile, file_data, j);
3110 else
3111 this_real_name = NULL;
7b9f3c50 3112 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3113 }
3114 }
24c79950
TT
3115
3116 do_cleanups (cleanup);
9291a0cd
TT
3117}
3118
3119static int
3120dw2_has_symbols (struct objfile *objfile)
3121{
3122 return 1;
3123}
3124
3125const struct quick_symbol_functions dwarf2_gdb_index_functions =
3126{
3127 dw2_has_symbols,
3128 dw2_find_last_source_symtab,
3129 dw2_forget_cached_source_info,
f8eba3c6 3130 dw2_map_symtabs_matching_filename,
9291a0cd 3131 dw2_lookup_symbol,
774b6a14 3132 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3133 dw2_print_stats,
3134 dw2_dump,
3135 dw2_relocate,
3136 dw2_expand_symtabs_for_function,
3137 dw2_expand_all_symtabs,
3138 dw2_expand_symtabs_with_filename,
3139 dw2_find_symbol_file,
40658b94 3140 dw2_map_matching_symbols,
9291a0cd
TT
3141 dw2_expand_symtabs_matching,
3142 dw2_find_pc_sect_symtab,
9291a0cd
TT
3143 dw2_map_symbol_filenames
3144};
3145
3146/* Initialize for reading DWARF for this objfile. Return 0 if this
3147 file will use psymtabs, or 1 if using the GNU index. */
3148
3149int
3150dwarf2_initialize_objfile (struct objfile *objfile)
3151{
3152 /* If we're about to read full symbols, don't bother with the
3153 indices. In this case we also don't care if some other debug
3154 format is making psymtabs, because they are all about to be
3155 expanded anyway. */
3156 if ((objfile->flags & OBJF_READNOW))
3157 {
3158 int i;
3159
3160 dwarf2_per_objfile->using_index = 1;
3161 create_all_comp_units (objfile);
0e50663e 3162 create_all_type_units (objfile);
7b9f3c50
DE
3163 dwarf2_per_objfile->quick_file_names_table =
3164 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3165
1fd400ff 3166 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3167 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3168 {
e254ef6a 3169 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3170
e254ef6a
DE
3171 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3172 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3173 }
3174
3175 /* Return 1 so that gdb sees the "quick" functions. However,
3176 these functions will be no-ops because we will have expanded
3177 all symtabs. */
3178 return 1;
3179 }
3180
3181 if (dwarf2_read_index (objfile))
3182 return 1;
3183
9291a0cd
TT
3184 return 0;
3185}
3186
3187\f
3188
dce234bc
PP
3189/* Build a partial symbol table. */
3190
3191void
f29dff0a 3192dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3193{
f29dff0a 3194 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3195 {
3196 init_psymbol_list (objfile, 1024);
3197 }
3198
d146bf1e 3199 dwarf2_build_psymtabs_hard (objfile);
c906108c 3200}
c906108c 3201
45452591
DE
3202/* Return TRUE if OFFSET is within CU_HEADER. */
3203
3204static inline int
b64f50a1 3205offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3206{
b64f50a1
JK
3207 sect_offset bottom = { cu_header->offset.sect_off };
3208 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3209 + cu_header->initial_length_size) };
9a619af0 3210
b64f50a1 3211 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3212}
3213
93311388
DE
3214/* Read in the comp unit header information from the debug_info at info_ptr.
3215 NOTE: This leaves members offset, first_die_offset to be filled in
3216 by the caller. */
107d2387 3217
fe1b8b76 3218static gdb_byte *
107d2387 3219read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3220 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3221{
3222 int signed_addr;
891d2f0b 3223 unsigned int bytes_read;
c764a876
DE
3224
3225 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3226 cu_header->initial_length_size = bytes_read;
3227 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3228 info_ptr += bytes_read;
107d2387
AC
3229 cu_header->version = read_2_bytes (abfd, info_ptr);
3230 info_ptr += 2;
b64f50a1
JK
3231 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3232 &bytes_read);
613e1657 3233 info_ptr += bytes_read;
107d2387
AC
3234 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3235 info_ptr += 1;
3236 signed_addr = bfd_get_sign_extend_vma (abfd);
3237 if (signed_addr < 0)
8e65ff28 3238 internal_error (__FILE__, __LINE__,
e2e0b3e5 3239 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3240 cu_header->signed_addr_p = signed_addr;
c764a876 3241
107d2387
AC
3242 return info_ptr;
3243}
3244
9ff913ba
DE
3245/* Subroutine of read_and_check_comp_unit_head and
3246 read_and_check_type_unit_head to simplify them.
3247 Perform various error checking on the header. */
3248
3249static void
3250error_check_comp_unit_head (struct comp_unit_head *header,
3251 struct dwarf2_section_info *section)
3252{
3253 bfd *abfd = section->asection->owner;
3254 const char *filename = bfd_get_filename (abfd);
3255
3256 if (header->version != 2 && header->version != 3 && header->version != 4)
3257 error (_("Dwarf Error: wrong version in compilation unit header "
3258 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3259 filename);
3260
b64f50a1 3261 if (header->abbrev_offset.sect_off
9ff913ba
DE
3262 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3263 &dwarf2_per_objfile->abbrev))
3264 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3265 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3266 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3267 filename);
3268
3269 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3270 avoid potential 32-bit overflow. */
b64f50a1 3271 if (((unsigned long) header->offset.sect_off
9ff913ba
DE
3272 + header->length + header->initial_length_size)
3273 > section->size)
3274 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3275 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3276 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3277 filename);
3278}
3279
3280/* Read in a CU/TU header and perform some basic error checking.
3281 The contents of the header are stored in HEADER.
3282 The result is a pointer to the start of the first DIE. */
adabb602 3283
fe1b8b76 3284static gdb_byte *
9ff913ba
DE
3285read_and_check_comp_unit_head (struct comp_unit_head *header,
3286 struct dwarf2_section_info *section,
3287 gdb_byte *info_ptr,
3288 int is_debug_types_section)
72bf9492 3289{
fe1b8b76 3290 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3291 bfd *abfd = section->asection->owner;
72bf9492 3292
b64f50a1 3293 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3294
72bf9492
DJ
3295 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3296
460c1c54
CC
3297 /* If we're reading a type unit, skip over the signature and
3298 type_offset fields. */
b0df02fd 3299 if (is_debug_types_section)
460c1c54
CC
3300 info_ptr += 8 /*signature*/ + header->offset_size;
3301
b64f50a1 3302 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3303
9ff913ba 3304 error_check_comp_unit_head (header, section);
72bf9492
DJ
3305
3306 return info_ptr;
3307}
3308
348e048f
DE
3309/* Read in the types comp unit header information from .debug_types entry at
3310 types_ptr. The result is a pointer to one past the end of the header. */
3311
3312static gdb_byte *
9ff913ba
DE
3313read_and_check_type_unit_head (struct comp_unit_head *header,
3314 struct dwarf2_section_info *section,
3315 gdb_byte *info_ptr,
dee91e82
DE
3316 ULONGEST *signature,
3317 cu_offset *type_offset_in_tu)
348e048f 3318{
9ff913ba
DE
3319 gdb_byte *beg_of_comp_unit = info_ptr;
3320 bfd *abfd = section->asection->owner;
348e048f 3321
b64f50a1 3322 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3323
9ff913ba 3324 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3325
9ff913ba
DE
3326 /* If we're reading a type unit, skip over the signature and
3327 type_offset fields. */
3328 if (signature != NULL)
3329 *signature = read_8_bytes (abfd, info_ptr);
3330 info_ptr += 8;
dee91e82
DE
3331 if (type_offset_in_tu != NULL)
3332 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3333 header->offset_size);
9ff913ba
DE
3334 info_ptr += header->offset_size;
3335
b64f50a1 3336 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3337
9ff913ba
DE
3338 error_check_comp_unit_head (header, section);
3339
3340 return info_ptr;
348e048f
DE
3341}
3342
aaa75496
JB
3343/* Allocate a new partial symtab for file named NAME and mark this new
3344 partial symtab as being an include of PST. */
3345
3346static void
3347dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3348 struct objfile *objfile)
3349{
3350 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3351
3352 subpst->section_offsets = pst->section_offsets;
3353 subpst->textlow = 0;
3354 subpst->texthigh = 0;
3355
3356 subpst->dependencies = (struct partial_symtab **)
3357 obstack_alloc (&objfile->objfile_obstack,
3358 sizeof (struct partial_symtab *));
3359 subpst->dependencies[0] = pst;
3360 subpst->number_of_dependencies = 1;
3361
3362 subpst->globals_offset = 0;
3363 subpst->n_global_syms = 0;
3364 subpst->statics_offset = 0;
3365 subpst->n_static_syms = 0;
3366 subpst->symtab = NULL;
3367 subpst->read_symtab = pst->read_symtab;
3368 subpst->readin = 0;
3369
3370 /* No private part is necessary for include psymtabs. This property
3371 can be used to differentiate between such include psymtabs and
10b3939b 3372 the regular ones. */
58a9656e 3373 subpst->read_symtab_private = NULL;
aaa75496
JB
3374}
3375
3376/* Read the Line Number Program data and extract the list of files
3377 included by the source file represented by PST. Build an include
d85a05f0 3378 partial symtab for each of these included files. */
aaa75496
JB
3379
3380static void
3381dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
3382 struct die_info *die,
3383 struct partial_symtab *pst)
aaa75496 3384{
d85a05f0
DJ
3385 struct line_header *lh = NULL;
3386 struct attribute *attr;
aaa75496 3387
d85a05f0
DJ
3388 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3389 if (attr)
3019eac3 3390 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
3391 if (lh == NULL)
3392 return; /* No linetable, so no includes. */
3393
c6da4cef 3394 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3395 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3396
3397 free_line_header (lh);
3398}
3399
348e048f 3400static hashval_t
52dc124a 3401hash_signatured_type (const void *item)
348e048f 3402{
52dc124a 3403 const struct signatured_type *sig_type = item;
9a619af0 3404
348e048f 3405 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 3406 return sig_type->signature;
348e048f
DE
3407}
3408
3409static int
52dc124a 3410eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
3411{
3412 const struct signatured_type *lhs = item_lhs;
3413 const struct signatured_type *rhs = item_rhs;
9a619af0 3414
348e048f
DE
3415 return lhs->signature == rhs->signature;
3416}
3417
1fd400ff
TT
3418/* Allocate a hash table for signatured types. */
3419
3420static htab_t
673bfd45 3421allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3422{
3423 return htab_create_alloc_ex (41,
52dc124a
DE
3424 hash_signatured_type,
3425 eq_signatured_type,
1fd400ff
TT
3426 NULL,
3427 &objfile->objfile_obstack,
3428 hashtab_obstack_allocate,
3429 dummy_obstack_deallocate);
3430}
3431
d467dd73 3432/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3433
3434static int
d467dd73 3435add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3436{
3437 struct signatured_type *sigt = *slot;
3438 struct dwarf2_per_cu_data ***datap = datum;
3439
3440 **datap = &sigt->per_cu;
3441 ++*datap;
3442
3443 return 1;
3444}
3445
3019eac3
DE
3446/* Create the hash table of all entries in the .debug_types section.
3447 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3448 The result is a pointer to the hash table or NULL if there are
3449 no types. */
348e048f 3450
3019eac3
DE
3451static htab_t
3452create_debug_types_hash_table (struct dwo_file *dwo_file,
3453 VEC (dwarf2_section_info_def) *types)
348e048f 3454{
3019eac3 3455 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 3456 htab_t types_htab = NULL;
8b70b953
TT
3457 int ix;
3458 struct dwarf2_section_info *section;
348e048f 3459
3019eac3
DE
3460 if (VEC_empty (dwarf2_section_info_def, types))
3461 return NULL;
348e048f 3462
8b70b953 3463 for (ix = 0;
3019eac3 3464 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
3465 ++ix)
3466 {
3019eac3 3467 bfd *abfd;
8b70b953 3468 gdb_byte *info_ptr, *end_ptr;
348e048f 3469
8b70b953
TT
3470 dwarf2_read_section (objfile, section);
3471 info_ptr = section->buffer;
348e048f 3472
8b70b953
TT
3473 if (info_ptr == NULL)
3474 continue;
348e048f 3475
3019eac3
DE
3476 /* We can't set abfd until now because the section may be empty or
3477 not present, in which case section->asection will be NULL. */
3478 abfd = section->asection->owner;
3479
8b70b953 3480 if (types_htab == NULL)
3019eac3
DE
3481 {
3482 if (dwo_file)
3483 types_htab = allocate_dwo_unit_table (objfile);
3484 else
3485 types_htab = allocate_signatured_type_table (objfile);
3486 }
348e048f 3487
8b70b953 3488 if (dwarf2_die_debug)
dee91e82
DE
3489 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3490 bfd_get_filename (abfd));
3491
3492 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3493 because we don't need to read any dies: the signature is in the
3494 header. */
8b70b953
TT
3495
3496 end_ptr = info_ptr + section->size;
3497 while (info_ptr < end_ptr)
3498 {
b64f50a1 3499 sect_offset offset;
3019eac3 3500 cu_offset type_offset_in_tu;
8b70b953 3501 ULONGEST signature;
52dc124a 3502 struct signatured_type *sig_type;
3019eac3 3503 struct dwo_unit *dwo_tu;
8b70b953
TT
3504 void **slot;
3505 gdb_byte *ptr = info_ptr;
9ff913ba 3506 struct comp_unit_head header;
dee91e82 3507 unsigned int length;
348e048f 3508
b64f50a1 3509 offset.sect_off = ptr - section->buffer;
348e048f 3510
8b70b953 3511 /* We need to read the type's signature in order to build the hash
9ff913ba 3512 table, but we don't need anything else just yet. */
348e048f 3513
9ff913ba 3514 ptr = read_and_check_type_unit_head (&header, section, ptr,
3019eac3 3515 &signature, &type_offset_in_tu);
6caca83c 3516
dee91e82
DE
3517 length = header.initial_length_size + header.length;
3518
6caca83c 3519 /* Skip dummy type units. */
dee91e82
DE
3520 if (ptr >= info_ptr + length
3521 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 3522 {
dee91e82 3523 info_ptr += header.initial_length_size + header.length;
6caca83c
CC
3524 continue;
3525 }
8b70b953 3526
3019eac3
DE
3527 if (dwo_file)
3528 {
3529 sig_type = NULL;
3530 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3531 struct dwo_unit);
3532 dwo_tu->dwo_file = dwo_file;
3533 dwo_tu->signature = signature;
3534 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3535 dwo_tu->info_or_types_section = section;
3536 dwo_tu->offset = offset;
3537 dwo_tu->length = length;
3538 }
3539 else
3540 {
3541 /* N.B.: type_offset is not usable if this type uses a DWO file.
3542 The real type_offset is in the DWO file. */
3543 dwo_tu = NULL;
3544 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3545 struct signatured_type);
3546 sig_type->signature = signature;
3547 sig_type->type_offset_in_tu = type_offset_in_tu;
3548 sig_type->per_cu.objfile = objfile;
3549 sig_type->per_cu.is_debug_types = 1;
3550 sig_type->per_cu.info_or_types_section = section;
3551 sig_type->per_cu.offset = offset;
3552 sig_type->per_cu.length = length;
3553 }
8b70b953 3554
3019eac3
DE
3555 slot = htab_find_slot (types_htab,
3556 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3557 INSERT);
8b70b953
TT
3558 gdb_assert (slot != NULL);
3559 if (*slot != NULL)
3560 {
3019eac3
DE
3561 sect_offset dup_offset;
3562
3563 if (dwo_file)
3564 {
3565 const struct dwo_unit *dup_tu = *slot;
3566
3567 dup_offset = dup_tu->offset;
3568 }
3569 else
3570 {
3571 const struct signatured_type *dup_tu = *slot;
3572
3573 dup_offset = dup_tu->per_cu.offset;
3574 }
b3c8eb43 3575
8b70b953
TT
3576 complaint (&symfile_complaints,
3577 _("debug type entry at offset 0x%x is duplicate to the "
3578 "entry at offset 0x%x, signature 0x%s"),
3019eac3 3579 offset.sect_off, dup_offset.sect_off,
8b70b953 3580 phex (signature, sizeof (signature)));
8b70b953 3581 }
3019eac3 3582 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 3583
8b70b953
TT
3584 if (dwarf2_die_debug)
3585 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
3586 offset.sect_off,
3587 phex (signature, sizeof (signature)));
348e048f 3588
dee91e82 3589 info_ptr += length;
8b70b953 3590 }
348e048f
DE
3591 }
3592
3019eac3
DE
3593 return types_htab;
3594}
3595
3596/* Create the hash table of all entries in the .debug_types section,
3597 and initialize all_type_units.
3598 The result is zero if there is an error (e.g. missing .debug_types section),
3599 otherwise non-zero. */
3600
3601static int
3602create_all_type_units (struct objfile *objfile)
3603{
3604 htab_t types_htab;
3605 struct dwarf2_per_cu_data **iter;
3606
3607 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3608 if (types_htab == NULL)
3609 {
3610 dwarf2_per_objfile->signatured_types = NULL;
3611 return 0;
3612 }
3613
348e048f
DE
3614 dwarf2_per_objfile->signatured_types = types_htab;
3615
d467dd73
DE
3616 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3617 dwarf2_per_objfile->all_type_units
1fd400ff 3618 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3619 dwarf2_per_objfile->n_type_units
1fd400ff 3620 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3621 iter = &dwarf2_per_objfile->all_type_units[0];
3622 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3623 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3624 == dwarf2_per_objfile->n_type_units);
1fd400ff 3625
348e048f
DE
3626 return 1;
3627}
3628
380bca97 3629/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 3630 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
3631
3632static struct signatured_type *
e319fa28 3633lookup_signatured_type (ULONGEST sig)
348e048f
DE
3634{
3635 struct signatured_type find_entry, *entry;
3636
3637 if (dwarf2_per_objfile->signatured_types == NULL)
3638 {
3639 complaint (&symfile_complaints,
55f1336d 3640 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 3641 return NULL;
348e048f
DE
3642 }
3643
3644 find_entry.signature = sig;
3645 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3646 return entry;
3647}
3648
d85a05f0
DJ
3649/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3650
3651static void
3652init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 3653 struct dwarf2_cu *cu,
3019eac3
DE
3654 struct dwarf2_section_info *section,
3655 struct dwo_file *dwo_file)
d85a05f0 3656{
fceca515 3657 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 3658 reader->abfd = section->asection->owner;
d85a05f0 3659 reader->cu = cu;
3019eac3 3660 reader->dwo_file = dwo_file;
dee91e82
DE
3661 reader->die_section = section;
3662 reader->buffer = section->buffer;
f664829e 3663 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
3664}
3665
3666/* Find the base address of the compilation unit for range lists and
3667 location lists. It will normally be specified by DW_AT_low_pc.
3668 In DWARF-3 draft 4, the base address could be overridden by
3669 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3670 compilation units with discontinuous ranges. */
3671
3672static void
3673dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3674{
3675 struct attribute *attr;
3676
3677 cu->base_known = 0;
3678 cu->base_address = 0;
3679
3680 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3681 if (attr)
3682 {
3683 cu->base_address = DW_ADDR (attr);
3684 cu->base_known = 1;
3685 }
3686 else
3687 {
3688 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3689 if (attr)
3690 {
3691 cu->base_address = DW_ADDR (attr);
3692 cu->base_known = 1;
3693 }
3694 }
3695}
3696
fd820528 3697/* Initialize a CU (or TU) and read its DIEs.
3019eac3 3698 If the CU defers to a DWO file, read the DWO file as well.
dee91e82
DE
3699
3700 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3701 Otherwise, a new CU is allocated with xmalloc.
3702
3703 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3704 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3705
3706 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 3707 linker) then DIE_READER_FUNC will not get called. */
aaa75496 3708
70221824 3709static void
fd820528
DE
3710init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3711 int use_existing_cu, int keep,
3712 die_reader_func_ftype *die_reader_func,
3713 void *data)
c906108c 3714{
dee91e82 3715 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
3716 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3717 bfd *abfd = section->asection->owner;
dee91e82
DE
3718 struct dwarf2_cu *cu;
3719 gdb_byte *begin_info_ptr, *info_ptr;
3720 struct die_reader_specs reader;
d85a05f0 3721 struct die_info *comp_unit_die;
dee91e82 3722 int has_children;
d85a05f0 3723 struct attribute *attr;
dee91e82
DE
3724 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3725 struct signatured_type *sig_type = NULL;
c906108c 3726
dee91e82
DE
3727 if (use_existing_cu)
3728 gdb_assert (keep);
23745b47 3729
dee91e82
DE
3730 cleanups = make_cleanup (null_cleanup, NULL);
3731
3732 /* This is cheap if the section is already read in. */
3733 dwarf2_read_section (objfile, section);
3734
3735 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3736
3737 if (use_existing_cu && this_cu->cu != NULL)
3738 {
3739 cu = this_cu->cu;
3740 info_ptr += cu->header.first_die_offset.cu_off;
3741 }
3742 else
3743 {
3744 /* If !use_existing_cu, this_cu->cu must be NULL. */
3745 gdb_assert (this_cu->cu == NULL);
3746
3747 cu = xmalloc (sizeof (*cu));
3748 init_one_comp_unit (cu, this_cu);
3749
3750 /* If an error occurs while loading, release our storage. */
3751 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3752
3019eac3 3753 if (this_cu->is_debug_types)
dee91e82
DE
3754 {
3755 ULONGEST signature;
3756
3757 info_ptr = read_and_check_type_unit_head (&cu->header,
3758 section, info_ptr,
3759 &signature, NULL);
3760
3761 /* There's no way to get from PER_CU to its containing
3762 struct signatured_type.
3763 But we have the signature so we can use that. */
3764 sig_type = lookup_signatured_type (signature);
3765 /* We've already scanned all the signatured types,
3766 this must succeed. */
3767 gdb_assert (sig_type != NULL);
3768 gdb_assert (&sig_type->per_cu == this_cu);
3769 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3770
3771 /* LENGTH has not been set yet for type units. */
3772 this_cu->length = cu->header.length + cu->header.initial_length_size;
3019eac3
DE
3773
3774 /* Establish the type offset that can be used to lookup the type. */
3775 sig_type->type_offset_in_section.sect_off =
3776 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
3777 }
3778 else
3779 {
3780 info_ptr = read_and_check_comp_unit_head (&cu->header,
3781 section, info_ptr, 0);
3782
3783 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3784 gdb_assert (this_cu->length
3785 == cu->header.length + cu->header.initial_length_size);
3786 }
3787 }
10b3939b 3788
6caca83c 3789 /* Skip dummy compilation units. */
dee91e82 3790 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
3791 || peek_abbrev_code (abfd, info_ptr) == 0)
3792 {
dee91e82 3793 do_cleanups (cleanups);
21b2bd31 3794 return;
6caca83c
CC
3795 }
3796
93311388 3797 /* Read the abbrevs for this compilation unit into a table. */
dee91e82
DE
3798 if (cu->dwarf2_abbrevs == NULL)
3799 {
3800 dwarf2_read_abbrevs (cu, &dwarf2_per_objfile->abbrev);
3801 make_cleanup (dwarf2_free_abbrev_table, cu);
3802 }
af703f96 3803
dee91e82 3804 /* Read the top level CU/TU die. */
3019eac3 3805 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 3806 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 3807
3019eac3
DE
3808 /* If we have a DWO stub, process it and then read in the DWO file.
3809 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3810 a DWO CU, that this test will fail. */
3811 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3812 if (attr)
3813 {
3814 char *dwo_name = DW_STRING (attr);
3815 const char *comp_dir;
3816 struct dwo_unit *dwo_unit;
3817 ULONGEST signature; /* Or dwo_id. */
3818 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3819 int i,num_extra_attrs;
3820
3821 if (has_children)
3822 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3823 " has children (offset 0x%x) [in module %s]"),
3824 this_cu->offset.sect_off, bfd_get_filename (abfd));
3825
3826 /* These attributes aren't processed until later:
3827 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3828 However, the attribute is found in the stub which we won't have later.
3829 In order to not impose this complication on the rest of the code,
3830 we read them here and copy them to the DWO CU/TU die. */
3831 stmt_list = low_pc = high_pc = ranges = NULL;
3832
3833 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3834 DWO file. */
3835 if (! this_cu->is_debug_types)
3836 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3837 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3838 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3839 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3840
3841 /* There should be a DW_AT_addr_base attribute here (if needed).
3842 We need the value before we can process DW_FORM_GNU_addr_index. */
3843 cu->addr_base = 0;
3844 cu->have_addr_base = 0;
3845 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
3846 if (attr)
3847 {
3848 cu->addr_base = DW_UNSND (attr);
3849 cu->have_addr_base = 1;
3850 }
3851
3852 if (this_cu->is_debug_types)
3853 {
3854 gdb_assert (sig_type != NULL);
3855 signature = sig_type->signature;
3856 }
3857 else
3858 {
3859 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3860 if (! attr)
3861 error (_("Dwarf Error: missing dwo_id [in module %s]"),
3862 dwo_name);
3863 signature = DW_UNSND (attr);
3864 }
3865
3866 /* We may need the comp_dir in order to find the DWO file. */
3867 comp_dir = NULL;
3868 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3869 if (attr)
3870 comp_dir = DW_STRING (attr);
3871
3872 if (this_cu->is_debug_types)
3873 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
3874 else
3875 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
3876 signature);
3877
3878 if (dwo_unit == NULL)
3879 {
3880 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
3881 " with ID %s [in module %s]"),
3882 this_cu->offset.sect_off,
3883 phex (signature, sizeof (signature)),
3884 objfile->name);
3885 }
3886
3887 /* Set up for reading the DWO CU/TU. */
3888 cu->dwo_unit = dwo_unit;
3889 section = dwo_unit->info_or_types_section;
3890 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
3891 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
3892
3893 if (this_cu->is_debug_types)
3894 {
3895 ULONGEST signature;
3896
3897 info_ptr = read_and_check_type_unit_head (&cu->header,
3898 section, info_ptr,
3899 &signature, NULL);
3900 gdb_assert (sig_type->signature == signature);
3901 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3902 gdb_assert (dwo_unit->length
3903 == cu->header.length + cu->header.initial_length_size);
3904
3905 /* Establish the type offset that can be used to lookup the type.
3906 For DWO files, we don't know it until now. */
3907 sig_type->type_offset_in_section.sect_off =
3908 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
3909 }
3910 else
3911 {
3912 info_ptr = read_and_check_comp_unit_head (&cu->header,
3913 section, info_ptr, 0);
3914 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3915 gdb_assert (dwo_unit->length
3916 == cu->header.length + cu->header.initial_length_size);
3917 }
3918
3919 /* Discard the original CU's abbrev table, and read the DWO's. */
3920 dwarf2_free_abbrev_table (cu);
3921 dwarf2_read_abbrevs (cu, &dwo_unit->dwo_file->sections.abbrev);
3922
3923 /* Read in the die, but leave space to copy over the attributes
3924 from the stub. This has the benefit of simplifying the rest of
3925 the code - all the real work is done here. */
3926 num_extra_attrs = ((stmt_list != NULL)
3927 + (low_pc != NULL)
3928 + (high_pc != NULL)
3929 + (ranges != NULL));
3930 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
3931 &has_children, num_extra_attrs);
3932
3933 /* Copy over the attributes from the stub to the DWO die. */
3934 i = comp_unit_die->num_attrs;
3935 if (stmt_list != NULL)
3936 comp_unit_die->attrs[i++] = *stmt_list;
3937 if (low_pc != NULL)
3938 comp_unit_die->attrs[i++] = *low_pc;
3939 if (high_pc != NULL)
3940 comp_unit_die->attrs[i++] = *high_pc;
3941 if (ranges != NULL)
3942 comp_unit_die->attrs[i++] = *ranges;
3943 comp_unit_die->num_attrs += num_extra_attrs;
3944
3945 /* Skip dummy compilation units. */
3946 if (info_ptr >= begin_info_ptr + dwo_unit->length
3947 || peek_abbrev_code (abfd, info_ptr) == 0)
3948 {
3949 do_cleanups (cleanups);
3950 return;
3951 }
3952 }
3953
dee91e82
DE
3954 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3955
3956 if (free_cu_cleanup != NULL)
348e048f 3957 {
dee91e82
DE
3958 if (keep)
3959 {
3960 /* We've successfully allocated this compilation unit. Let our
3961 caller clean it up when finished with it. */
3962 discard_cleanups (free_cu_cleanup);
3963
3964 /* We can only discard free_cu_cleanup and all subsequent cleanups.
3965 So we have to manually free the abbrev table. */
3966 dwarf2_free_abbrev_table (cu);
3967
3968 /* Link this CU into read_in_chain. */
3969 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3970 dwarf2_per_objfile->read_in_chain = this_cu;
3971 }
3972 else
3973 do_cleanups (free_cu_cleanup);
348e048f 3974 }
dee91e82
DE
3975
3976 do_cleanups (cleanups);
3977}
3978
3019eac3
DE
3979/* Read CU/TU THIS_CU in section SECTION,
3980 but do not follow DW_AT_GNU_dwo_name if present.
3981 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
3982 have already done the lookup to find the DWO file).
dee91e82
DE
3983
3984 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 3985 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
3986
3987 We fill in THIS_CU->length.
3988
3989 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3990 linker) then DIE_READER_FUNC will not get called.
3991
3992 THIS_CU->cu is always freed when done.
3019eac3
DE
3993 This is done in order to not leave THIS_CU->cu in a state where we have
3994 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
3995
3996static void
3997init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3998 struct dwarf2_section_info *abbrev_section,
3019eac3 3999 struct dwo_file *dwo_file,
dee91e82
DE
4000 die_reader_func_ftype *die_reader_func,
4001 void *data)
4002{
4003 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4004 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4005 bfd *abfd = section->asection->owner;
dee91e82
DE
4006 struct dwarf2_cu cu;
4007 gdb_byte *begin_info_ptr, *info_ptr;
4008 struct die_reader_specs reader;
4009 struct cleanup *cleanups;
4010 struct die_info *comp_unit_die;
4011 int has_children;
4012
4013 gdb_assert (this_cu->cu == NULL);
4014
dee91e82
DE
4015 /* This is cheap if the section is already read in. */
4016 dwarf2_read_section (objfile, section);
4017
4018 init_one_comp_unit (&cu, this_cu);
4019
4020 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4021
4022 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4023 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3019eac3 4024 this_cu->is_debug_types);
dee91e82
DE
4025
4026 this_cu->length = cu.header.length + cu.header.initial_length_size;
4027
4028 /* Skip dummy compilation units. */
4029 if (info_ptr >= begin_info_ptr + this_cu->length
4030 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4031 {
dee91e82 4032 do_cleanups (cleanups);
21b2bd31 4033 return;
93311388 4034 }
72bf9492 4035
dee91e82
DE
4036 dwarf2_read_abbrevs (&cu, abbrev_section);
4037 make_cleanup (dwarf2_free_abbrev_table, &cu);
4038
3019eac3 4039 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4040 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4041
4042 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4043
4044 do_cleanups (cleanups);
4045}
4046
3019eac3
DE
4047/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4048 does not lookup the specified DWO file.
4049 This cannot be used to read DWO files.
dee91e82
DE
4050
4051 THIS_CU->cu is always freed when done.
3019eac3
DE
4052 This is done in order to not leave THIS_CU->cu in a state where we have
4053 to care whether it refers to the "main" CU or the DWO CU.
4054 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4055
4056static void
4057init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4058 die_reader_func_ftype *die_reader_func,
4059 void *data)
4060{
4061 init_cutu_and_read_dies_no_follow (this_cu,
4062 &dwarf2_per_objfile->abbrev,
3019eac3 4063 NULL,
dee91e82
DE
4064 die_reader_func, data);
4065}
4066
4067/* die_reader_func for process_psymtab_comp_unit. */
4068
4069static void
4070process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4071 gdb_byte *info_ptr,
4072 struct die_info *comp_unit_die,
4073 int has_children,
4074 void *data)
4075{
4076 struct dwarf2_cu *cu = reader->cu;
4077 struct objfile *objfile = cu->objfile;
4078 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4079 struct attribute *attr;
4080 CORE_ADDR baseaddr;
4081 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4082 struct partial_symtab *pst;
4083 int has_pc_info;
4084 const char *filename;
95554aad 4085 int *want_partial_unit_ptr = data;
dee91e82 4086
95554aad
TT
4087 if (comp_unit_die->tag == DW_TAG_partial_unit
4088 && (want_partial_unit_ptr == NULL
4089 || !*want_partial_unit_ptr))
dee91e82
DE
4090 return;
4091
95554aad 4092 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4093
4094 cu->list_in_scope = &file_symbols;
c906108c 4095
93311388 4096 /* Allocate a new partial symbol table structure. */
dee91e82 4097 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4098 if (attr == NULL || !DW_STRING (attr))
4099 filename = "";
4100 else
4101 filename = DW_STRING (attr);
93311388 4102 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 4103 filename,
93311388
DE
4104 /* TEXTLOW and TEXTHIGH are set below. */
4105 0,
4106 objfile->global_psymbols.next,
4107 objfile->static_psymbols.next);
9750bca9 4108 pst->psymtabs_addrmap_supported = 1;
72bf9492 4109
dee91e82 4110 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4111 if (attr != NULL)
4112 pst->dirname = DW_STRING (attr);
72bf9492 4113
dee91e82 4114 pst->read_symtab_private = per_cu;
72bf9492 4115
93311388 4116 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4117
0963b4bd 4118 /* Store the function that reads in the rest of the symbol table. */
93311388 4119 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 4120
dee91e82 4121 per_cu->v.psymtab = pst;
c906108c 4122
dee91e82 4123 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4124
93311388
DE
4125 /* Possibly set the default values of LOWPC and HIGHPC from
4126 `DW_AT_ranges'. */
d85a05f0 4127 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4128 &best_highpc, cu, pst);
d85a05f0 4129 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4130 /* Store the contiguous range if it is not empty; it can be empty for
4131 CUs with no code. */
4132 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4133 best_lowpc + baseaddr,
4134 best_highpc + baseaddr - 1, pst);
93311388
DE
4135
4136 /* Check if comp unit has_children.
4137 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4138 If not, there's no more debug_info for this comp unit. */
d85a05f0 4139 if (has_children)
93311388
DE
4140 {
4141 struct partial_die_info *first_die;
4142 CORE_ADDR lowpc, highpc;
31ffec48 4143
93311388
DE
4144 lowpc = ((CORE_ADDR) -1);
4145 highpc = ((CORE_ADDR) 0);
c906108c 4146
dee91e82 4147 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4148
93311388 4149 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4150 ! has_pc_info, cu);
57c22c6c 4151
93311388
DE
4152 /* If we didn't find a lowpc, set it to highpc to avoid
4153 complaints from `maint check'. */
4154 if (lowpc == ((CORE_ADDR) -1))
4155 lowpc = highpc;
10b3939b 4156
93311388
DE
4157 /* If the compilation unit didn't have an explicit address range,
4158 then use the information extracted from its child dies. */
d85a05f0 4159 if (! has_pc_info)
93311388 4160 {
d85a05f0
DJ
4161 best_lowpc = lowpc;
4162 best_highpc = highpc;
93311388
DE
4163 }
4164 }
d85a05f0
DJ
4165 pst->textlow = best_lowpc + baseaddr;
4166 pst->texthigh = best_highpc + baseaddr;
c906108c 4167
93311388
DE
4168 pst->n_global_syms = objfile->global_psymbols.next -
4169 (objfile->global_psymbols.list + pst->globals_offset);
4170 pst->n_static_syms = objfile->static_psymbols.next -
4171 (objfile->static_psymbols.list + pst->statics_offset);
4172 sort_pst_symbols (pst);
c906108c 4173
95554aad
TT
4174 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4175 {
4176 int i;
4177 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4178 struct dwarf2_per_cu_data *iter;
4179
4180 /* Fill in 'dependencies' here; we fill in 'users' in a
4181 post-pass. */
4182 pst->number_of_dependencies = len;
4183 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4184 len * sizeof (struct symtab *));
4185 for (i = 0;
4186 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4187 i, iter);
4188 ++i)
4189 pst->dependencies[i] = iter->v.psymtab;
4190
4191 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4192 }
4193
3019eac3 4194 if (per_cu->is_debug_types)
348e048f
DE
4195 {
4196 /* It's not clear we want to do anything with stmt lists here.
4197 Waiting to see what gcc ultimately does. */
4198 }
d85a05f0 4199 else
93311388
DE
4200 {
4201 /* Get the list of files included in the current compilation unit,
4202 and build a psymtab for each of them. */
dee91e82 4203 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
93311388 4204 }
dee91e82 4205}
ae038cb0 4206
dee91e82
DE
4207/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4208 Process compilation unit THIS_CU for a psymtab. */
4209
4210static void
95554aad
TT
4211process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4212 int want_partial_unit)
dee91e82
DE
4213{
4214 /* If this compilation unit was already read in, free the
4215 cached copy in order to read it in again. This is
4216 necessary because we skipped some symbols when we first
4217 read in the compilation unit (see load_partial_dies).
4218 This problem could be avoided, but the benefit is unclear. */
4219 if (this_cu->cu != NULL)
4220 free_one_cached_comp_unit (this_cu);
4221
3019eac3 4222 gdb_assert (! this_cu->is_debug_types);
fd820528 4223 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
95554aad 4224 &want_partial_unit);
dee91e82
DE
4225
4226 /* Age out any secondary CUs. */
4227 age_cached_comp_units ();
93311388 4228}
ff013f42 4229
348e048f
DE
4230/* Traversal function for htab_traverse_noresize.
4231 Process one .debug_types comp-unit. */
4232
4233static int
dee91e82 4234process_psymtab_type_unit (void **slot, void *info)
348e048f 4235{
dee91e82
DE
4236 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4237 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 4238
fd820528 4239 gdb_assert (per_cu->is_debug_types);
a0f42c21 4240 gdb_assert (info == NULL);
348e048f 4241
dee91e82
DE
4242 /* If this compilation unit was already read in, free the
4243 cached copy in order to read it in again. This is
4244 necessary because we skipped some symbols when we first
4245 read in the compilation unit (see load_partial_dies).
4246 This problem could be avoided, but the benefit is unclear. */
4247 if (per_cu->cu != NULL)
4248 free_one_cached_comp_unit (per_cu);
4249
fd820528
DE
4250 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4251 NULL);
dee91e82
DE
4252
4253 /* Age out any secondary CUs. */
4254 age_cached_comp_units ();
348e048f
DE
4255
4256 return 1;
4257}
4258
4259/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4260 Build partial symbol tables for the .debug_types comp-units. */
4261
4262static void
4263build_type_psymtabs (struct objfile *objfile)
4264{
0e50663e 4265 if (! create_all_type_units (objfile))
348e048f
DE
4266 return;
4267
4268 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
dee91e82 4269 process_psymtab_type_unit, NULL);
348e048f
DE
4270}
4271
60606b2c
TT
4272/* A cleanup function that clears objfile's psymtabs_addrmap field. */
4273
4274static void
4275psymtabs_addrmap_cleanup (void *o)
4276{
4277 struct objfile *objfile = o;
ec61707d 4278
60606b2c
TT
4279 objfile->psymtabs_addrmap = NULL;
4280}
4281
95554aad
TT
4282/* Compute the 'user' field for each psymtab in OBJFILE. */
4283
4284static void
4285set_partial_user (struct objfile *objfile)
4286{
4287 int i;
4288
4289 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4290 {
4291 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4292 struct partial_symtab *pst = per_cu->v.psymtab;
4293 int j;
4294
4295 for (j = 0; j < pst->number_of_dependencies; ++j)
4296 {
4297 /* Set the 'user' field only if it is not already set. */
4298 if (pst->dependencies[j]->user == NULL)
4299 pst->dependencies[j]->user = pst;
4300 }
4301 }
4302}
4303
93311388
DE
4304/* Build the partial symbol table by doing a quick pass through the
4305 .debug_info and .debug_abbrev sections. */
72bf9492 4306
93311388 4307static void
c67a9c90 4308dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 4309{
60606b2c
TT
4310 struct cleanup *back_to, *addrmap_cleanup;
4311 struct obstack temp_obstack;
21b2bd31 4312 int i;
93311388 4313
98bfdba5
PA
4314 dwarf2_per_objfile->reading_partial_symbols = 1;
4315
be391dca 4316 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 4317
93311388
DE
4318 /* Any cached compilation units will be linked by the per-objfile
4319 read_in_chain. Make sure to free them when we're done. */
4320 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 4321
348e048f
DE
4322 build_type_psymtabs (objfile);
4323
93311388 4324 create_all_comp_units (objfile);
c906108c 4325
60606b2c
TT
4326 /* Create a temporary address map on a temporary obstack. We later
4327 copy this to the final obstack. */
4328 obstack_init (&temp_obstack);
4329 make_cleanup_obstack_free (&temp_obstack);
4330 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4331 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 4332
21b2bd31 4333 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 4334 {
21b2bd31 4335 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 4336
95554aad 4337 process_psymtab_comp_unit (per_cu, 0);
c906108c 4338 }
ff013f42 4339
95554aad
TT
4340 set_partial_user (objfile);
4341
ff013f42
JK
4342 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4343 &objfile->objfile_obstack);
60606b2c 4344 discard_cleanups (addrmap_cleanup);
ff013f42 4345
ae038cb0
DJ
4346 do_cleanups (back_to);
4347}
4348
3019eac3 4349/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
4350
4351static void
dee91e82
DE
4352load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4353 gdb_byte *info_ptr,
4354 struct die_info *comp_unit_die,
4355 int has_children,
4356 void *data)
ae038cb0 4357{
dee91e82 4358 struct dwarf2_cu *cu = reader->cu;
ae038cb0 4359
95554aad 4360 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 4361
ae038cb0
DJ
4362 /* Check if comp unit has_children.
4363 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4364 If not, there's no more debug_info for this comp unit. */
d85a05f0 4365 if (has_children)
dee91e82
DE
4366 load_partial_dies (reader, info_ptr, 0);
4367}
98bfdba5 4368
dee91e82
DE
4369/* Load the partial DIEs for a secondary CU into memory.
4370 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 4371
dee91e82
DE
4372static void
4373load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4374{
fd820528 4375 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
4376}
4377
9cdd5dbd
DE
4378/* Create a list of all compilation units in OBJFILE.
4379 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
4380
4381static void
4382create_all_comp_units (struct objfile *objfile)
4383{
4384 int n_allocated;
4385 int n_comp_units;
4386 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
4387 gdb_byte *info_ptr;
4388
4389 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4390 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4391
4392 n_comp_units = 0;
4393 n_allocated = 10;
4394 all_comp_units = xmalloc (n_allocated
4395 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 4396
3e43a32a
MS
4397 while (info_ptr < dwarf2_per_objfile->info.buffer
4398 + dwarf2_per_objfile->info.size)
ae038cb0 4399 {
c764a876 4400 unsigned int length, initial_length_size;
ae038cb0 4401 struct dwarf2_per_cu_data *this_cu;
b64f50a1 4402 sect_offset offset;
ae038cb0 4403
b64f50a1 4404 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4405
4406 /* Read just enough information to find out where the next
4407 compilation unit is. */
c764a876
DE
4408 length = read_initial_length (objfile->obfd, info_ptr,
4409 &initial_length_size);
ae038cb0
DJ
4410
4411 /* Save the compilation unit for later lookup. */
4412 this_cu = obstack_alloc (&objfile->objfile_obstack,
4413 sizeof (struct dwarf2_per_cu_data));
4414 memset (this_cu, 0, sizeof (*this_cu));
4415 this_cu->offset = offset;
c764a876 4416 this_cu->length = length + initial_length_size;
9291a0cd 4417 this_cu->objfile = objfile;
3019eac3 4418 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
ae038cb0
DJ
4419
4420 if (n_comp_units == n_allocated)
4421 {
4422 n_allocated *= 2;
4423 all_comp_units = xrealloc (all_comp_units,
4424 n_allocated
4425 * sizeof (struct dwarf2_per_cu_data *));
4426 }
4427 all_comp_units[n_comp_units++] = this_cu;
4428
4429 info_ptr = info_ptr + this_cu->length;
4430 }
4431
4432 dwarf2_per_objfile->all_comp_units
4433 = obstack_alloc (&objfile->objfile_obstack,
4434 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4435 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4436 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4437 xfree (all_comp_units);
4438 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
4439}
4440
5734ee8b
DJ
4441/* Process all loaded DIEs for compilation unit CU, starting at
4442 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4443 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4444 DW_AT_ranges). If NEED_PC is set, then this function will set
4445 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4446 and record the covered ranges in the addrmap. */
c906108c 4447
72bf9492
DJ
4448static void
4449scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 4450 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 4451{
72bf9492 4452 struct partial_die_info *pdi;
c906108c 4453
91c24f0a
DC
4454 /* Now, march along the PDI's, descending into ones which have
4455 interesting children but skipping the children of the other ones,
4456 until we reach the end of the compilation unit. */
c906108c 4457
72bf9492 4458 pdi = first_die;
91c24f0a 4459
72bf9492
DJ
4460 while (pdi != NULL)
4461 {
4462 fixup_partial_die (pdi, cu);
c906108c 4463
f55ee35c 4464 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
4465 children, so we need to look at them. Ditto for anonymous
4466 enums. */
933c6fe4 4467
72bf9492 4468 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
4469 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4470 || pdi->tag == DW_TAG_imported_unit)
c906108c 4471 {
72bf9492 4472 switch (pdi->tag)
c906108c
SS
4473 {
4474 case DW_TAG_subprogram:
5734ee8b 4475 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 4476 break;
72929c62 4477 case DW_TAG_constant:
c906108c
SS
4478 case DW_TAG_variable:
4479 case DW_TAG_typedef:
91c24f0a 4480 case DW_TAG_union_type:
72bf9492 4481 if (!pdi->is_declaration)
63d06c5c 4482 {
72bf9492 4483 add_partial_symbol (pdi, cu);
63d06c5c
DC
4484 }
4485 break;
c906108c 4486 case DW_TAG_class_type:
680b30c7 4487 case DW_TAG_interface_type:
c906108c 4488 case DW_TAG_structure_type:
72bf9492 4489 if (!pdi->is_declaration)
c906108c 4490 {
72bf9492 4491 add_partial_symbol (pdi, cu);
c906108c
SS
4492 }
4493 break;
91c24f0a 4494 case DW_TAG_enumeration_type:
72bf9492
DJ
4495 if (!pdi->is_declaration)
4496 add_partial_enumeration (pdi, cu);
c906108c
SS
4497 break;
4498 case DW_TAG_base_type:
a02abb62 4499 case DW_TAG_subrange_type:
c906108c 4500 /* File scope base type definitions are added to the partial
c5aa993b 4501 symbol table. */
72bf9492 4502 add_partial_symbol (pdi, cu);
c906108c 4503 break;
d9fa45fe 4504 case DW_TAG_namespace:
5734ee8b 4505 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 4506 break;
5d7cb8df
JK
4507 case DW_TAG_module:
4508 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4509 break;
95554aad
TT
4510 case DW_TAG_imported_unit:
4511 {
4512 struct dwarf2_per_cu_data *per_cu;
4513
4514 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4515 cu->objfile);
4516
4517 /* Go read the partial unit, if needed. */
4518 if (per_cu->v.psymtab == NULL)
4519 process_psymtab_comp_unit (per_cu, 1);
4520
4521 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4522 per_cu);
4523 }
4524 break;
c906108c
SS
4525 default:
4526 break;
4527 }
4528 }
4529
72bf9492
DJ
4530 /* If the die has a sibling, skip to the sibling. */
4531
4532 pdi = pdi->die_sibling;
4533 }
4534}
4535
4536/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 4537
72bf9492 4538 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
4539 name is concatenated with "::" and the partial DIE's name. For
4540 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
4541 Enumerators are an exception; they use the scope of their parent
4542 enumeration type, i.e. the name of the enumeration type is not
4543 prepended to the enumerator.
91c24f0a 4544
72bf9492
DJ
4545 There are two complexities. One is DW_AT_specification; in this
4546 case "parent" means the parent of the target of the specification,
4547 instead of the direct parent of the DIE. The other is compilers
4548 which do not emit DW_TAG_namespace; in this case we try to guess
4549 the fully qualified name of structure types from their members'
4550 linkage names. This must be done using the DIE's children rather
4551 than the children of any DW_AT_specification target. We only need
4552 to do this for structures at the top level, i.e. if the target of
4553 any DW_AT_specification (if any; otherwise the DIE itself) does not
4554 have a parent. */
4555
4556/* Compute the scope prefix associated with PDI's parent, in
4557 compilation unit CU. The result will be allocated on CU's
4558 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4559 field. NULL is returned if no prefix is necessary. */
4560static char *
4561partial_die_parent_scope (struct partial_die_info *pdi,
4562 struct dwarf2_cu *cu)
4563{
4564 char *grandparent_scope;
4565 struct partial_die_info *parent, *real_pdi;
91c24f0a 4566
72bf9492
DJ
4567 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4568 then this means the parent of the specification DIE. */
4569
4570 real_pdi = pdi;
72bf9492 4571 while (real_pdi->has_specification)
10b3939b 4572 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
4573
4574 parent = real_pdi->die_parent;
4575 if (parent == NULL)
4576 return NULL;
4577
4578 if (parent->scope_set)
4579 return parent->scope;
4580
4581 fixup_partial_die (parent, cu);
4582
10b3939b 4583 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 4584
acebe513
UW
4585 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4586 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4587 Work around this problem here. */
4588 if (cu->language == language_cplus
6e70227d 4589 && parent->tag == DW_TAG_namespace
acebe513
UW
4590 && strcmp (parent->name, "::") == 0
4591 && grandparent_scope == NULL)
4592 {
4593 parent->scope = NULL;
4594 parent->scope_set = 1;
4595 return NULL;
4596 }
4597
9c6c53f7
SA
4598 if (pdi->tag == DW_TAG_enumerator)
4599 /* Enumerators should not get the name of the enumeration as a prefix. */
4600 parent->scope = grandparent_scope;
4601 else if (parent->tag == DW_TAG_namespace
f55ee35c 4602 || parent->tag == DW_TAG_module
72bf9492
DJ
4603 || parent->tag == DW_TAG_structure_type
4604 || parent->tag == DW_TAG_class_type
680b30c7 4605 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
4606 || parent->tag == DW_TAG_union_type
4607 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
4608 {
4609 if (grandparent_scope == NULL)
4610 parent->scope = parent->name;
4611 else
3e43a32a
MS
4612 parent->scope = typename_concat (&cu->comp_unit_obstack,
4613 grandparent_scope,
f55ee35c 4614 parent->name, 0, cu);
72bf9492 4615 }
72bf9492
DJ
4616 else
4617 {
4618 /* FIXME drow/2004-04-01: What should we be doing with
4619 function-local names? For partial symbols, we should probably be
4620 ignoring them. */
4621 complaint (&symfile_complaints,
e2e0b3e5 4622 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 4623 parent->tag, pdi->offset.sect_off);
72bf9492 4624 parent->scope = grandparent_scope;
c906108c
SS
4625 }
4626
72bf9492
DJ
4627 parent->scope_set = 1;
4628 return parent->scope;
4629}
4630
4631/* Return the fully scoped name associated with PDI, from compilation unit
4632 CU. The result will be allocated with malloc. */
4568ecf9 4633
72bf9492
DJ
4634static char *
4635partial_die_full_name (struct partial_die_info *pdi,
4636 struct dwarf2_cu *cu)
4637{
4638 char *parent_scope;
4639
98bfdba5
PA
4640 /* If this is a template instantiation, we can not work out the
4641 template arguments from partial DIEs. So, unfortunately, we have
4642 to go through the full DIEs. At least any work we do building
4643 types here will be reused if full symbols are loaded later. */
4644 if (pdi->has_template_arguments)
4645 {
4646 fixup_partial_die (pdi, cu);
4647
4648 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4649 {
4650 struct die_info *die;
4651 struct attribute attr;
4652 struct dwarf2_cu *ref_cu = cu;
4653
b64f50a1 4654 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
4655 attr.name = 0;
4656 attr.form = DW_FORM_ref_addr;
4568ecf9 4657 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
4658 die = follow_die_ref (NULL, &attr, &ref_cu);
4659
4660 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4661 }
4662 }
4663
72bf9492
DJ
4664 parent_scope = partial_die_parent_scope (pdi, cu);
4665 if (parent_scope == NULL)
4666 return NULL;
4667 else
f55ee35c 4668 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4669}
4670
4671static void
72bf9492 4672add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4673{
e7c27a73 4674 struct objfile *objfile = cu->objfile;
c906108c 4675 CORE_ADDR addr = 0;
decbce07 4676 char *actual_name = NULL;
e142c38c 4677 CORE_ADDR baseaddr;
72bf9492 4678 int built_actual_name = 0;
e142c38c
DJ
4679
4680 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4681
94af9270
KS
4682 actual_name = partial_die_full_name (pdi, cu);
4683 if (actual_name)
4684 built_actual_name = 1;
63d06c5c 4685
72bf9492
DJ
4686 if (actual_name == NULL)
4687 actual_name = pdi->name;
4688
c906108c
SS
4689 switch (pdi->tag)
4690 {
4691 case DW_TAG_subprogram:
2cfa0c8d 4692 if (pdi->is_external || cu->language == language_ada)
c906108c 4693 {
2cfa0c8d
JB
4694 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4695 of the global scope. But in Ada, we want to be able to access
4696 nested procedures globally. So all Ada subprograms are stored
4697 in the global scope. */
f47fb265 4698 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4699 mst_text, objfile); */
f47fb265
MS
4700 add_psymbol_to_list (actual_name, strlen (actual_name),
4701 built_actual_name,
4702 VAR_DOMAIN, LOC_BLOCK,
4703 &objfile->global_psymbols,
4704 0, pdi->lowpc + baseaddr,
4705 cu->language, objfile);
c906108c
SS
4706 }
4707 else
4708 {
f47fb265 4709 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4710 mst_file_text, objfile); */
f47fb265
MS
4711 add_psymbol_to_list (actual_name, strlen (actual_name),
4712 built_actual_name,
4713 VAR_DOMAIN, LOC_BLOCK,
4714 &objfile->static_psymbols,
4715 0, pdi->lowpc + baseaddr,
4716 cu->language, objfile);
c906108c
SS
4717 }
4718 break;
72929c62
JB
4719 case DW_TAG_constant:
4720 {
4721 struct psymbol_allocation_list *list;
4722
4723 if (pdi->is_external)
4724 list = &objfile->global_psymbols;
4725 else
4726 list = &objfile->static_psymbols;
f47fb265
MS
4727 add_psymbol_to_list (actual_name, strlen (actual_name),
4728 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4729 list, 0, 0, cu->language, objfile);
72929c62
JB
4730 }
4731 break;
c906108c 4732 case DW_TAG_variable:
95554aad
TT
4733 if (pdi->d.locdesc)
4734 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 4735
95554aad 4736 if (pdi->d.locdesc
caac4577
JG
4737 && addr == 0
4738 && !dwarf2_per_objfile->has_section_at_zero)
4739 {
4740 /* A global or static variable may also have been stripped
4741 out by the linker if unused, in which case its address
4742 will be nullified; do not add such variables into partial
4743 symbol table then. */
4744 }
4745 else if (pdi->is_external)
c906108c
SS
4746 {
4747 /* Global Variable.
4748 Don't enter into the minimal symbol tables as there is
4749 a minimal symbol table entry from the ELF symbols already.
4750 Enter into partial symbol table if it has a location
4751 descriptor or a type.
4752 If the location descriptor is missing, new_symbol will create
4753 a LOC_UNRESOLVED symbol, the address of the variable will then
4754 be determined from the minimal symbol table whenever the variable
4755 is referenced.
4756 The address for the partial symbol table entry is not
4757 used by GDB, but it comes in handy for debugging partial symbol
4758 table building. */
4759
95554aad 4760 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
4761 add_psymbol_to_list (actual_name, strlen (actual_name),
4762 built_actual_name,
4763 VAR_DOMAIN, LOC_STATIC,
4764 &objfile->global_psymbols,
4765 0, addr + baseaddr,
4766 cu->language, objfile);
c906108c
SS
4767 }
4768 else
4769 {
0963b4bd 4770 /* Static Variable. Skip symbols without location descriptors. */
95554aad 4771 if (pdi->d.locdesc == NULL)
decbce07
MS
4772 {
4773 if (built_actual_name)
4774 xfree (actual_name);
4775 return;
4776 }
f47fb265 4777 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4778 mst_file_data, objfile); */
f47fb265
MS
4779 add_psymbol_to_list (actual_name, strlen (actual_name),
4780 built_actual_name,
4781 VAR_DOMAIN, LOC_STATIC,
4782 &objfile->static_psymbols,
4783 0, addr + baseaddr,
4784 cu->language, objfile);
c906108c
SS
4785 }
4786 break;
4787 case DW_TAG_typedef:
4788 case DW_TAG_base_type:
a02abb62 4789 case DW_TAG_subrange_type:
38d518c9 4790 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4791 built_actual_name,
176620f1 4792 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4793 &objfile->static_psymbols,
e142c38c 4794 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4795 break;
72bf9492
DJ
4796 case DW_TAG_namespace:
4797 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4798 built_actual_name,
72bf9492
DJ
4799 VAR_DOMAIN, LOC_TYPEDEF,
4800 &objfile->global_psymbols,
4801 0, (CORE_ADDR) 0, cu->language, objfile);
4802 break;
c906108c 4803 case DW_TAG_class_type:
680b30c7 4804 case DW_TAG_interface_type:
c906108c
SS
4805 case DW_TAG_structure_type:
4806 case DW_TAG_union_type:
4807 case DW_TAG_enumeration_type:
fa4028e9
JB
4808 /* Skip external references. The DWARF standard says in the section
4809 about "Structure, Union, and Class Type Entries": "An incomplete
4810 structure, union or class type is represented by a structure,
4811 union or class entry that does not have a byte size attribute
4812 and that has a DW_AT_declaration attribute." */
4813 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4814 {
4815 if (built_actual_name)
4816 xfree (actual_name);
4817 return;
4818 }
fa4028e9 4819
63d06c5c
DC
4820 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4821 static vs. global. */
38d518c9 4822 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4823 built_actual_name,
176620f1 4824 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4825 (cu->language == language_cplus
4826 || cu->language == language_java)
63d06c5c
DC
4827 ? &objfile->global_psymbols
4828 : &objfile->static_psymbols,
e142c38c 4829 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4830
c906108c
SS
4831 break;
4832 case DW_TAG_enumerator:
38d518c9 4833 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4834 built_actual_name,
176620f1 4835 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4836 (cu->language == language_cplus
4837 || cu->language == language_java)
f6fe98ef
DJ
4838 ? &objfile->global_psymbols
4839 : &objfile->static_psymbols,
e142c38c 4840 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4841 break;
4842 default:
4843 break;
4844 }
5c4e30ca 4845
72bf9492
DJ
4846 if (built_actual_name)
4847 xfree (actual_name);
c906108c
SS
4848}
4849
5c4e30ca
DC
4850/* Read a partial die corresponding to a namespace; also, add a symbol
4851 corresponding to that namespace to the symbol table. NAMESPACE is
4852 the name of the enclosing namespace. */
91c24f0a 4853
72bf9492
DJ
4854static void
4855add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4856 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4857 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4858{
72bf9492 4859 /* Add a symbol for the namespace. */
e7c27a73 4860
72bf9492 4861 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4862
4863 /* Now scan partial symbols in that namespace. */
4864
91c24f0a 4865 if (pdi->has_children)
5734ee8b 4866 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4867}
4868
5d7cb8df
JK
4869/* Read a partial die corresponding to a Fortran module. */
4870
4871static void
4872add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4873 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4874{
f55ee35c 4875 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4876
4877 if (pdi->has_children)
4878 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4879}
4880
bc30ff58
JB
4881/* Read a partial die corresponding to a subprogram and create a partial
4882 symbol for that subprogram. When the CU language allows it, this
4883 routine also defines a partial symbol for each nested subprogram
4884 that this subprogram contains.
6e70227d 4885
bc30ff58
JB
4886 DIE my also be a lexical block, in which case we simply search
4887 recursively for suprograms defined inside that lexical block.
4888 Again, this is only performed when the CU language allows this
4889 type of definitions. */
4890
4891static void
4892add_partial_subprogram (struct partial_die_info *pdi,
4893 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4894 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4895{
4896 if (pdi->tag == DW_TAG_subprogram)
4897 {
4898 if (pdi->has_pc_info)
4899 {
4900 if (pdi->lowpc < *lowpc)
4901 *lowpc = pdi->lowpc;
4902 if (pdi->highpc > *highpc)
4903 *highpc = pdi->highpc;
5734ee8b
DJ
4904 if (need_pc)
4905 {
4906 CORE_ADDR baseaddr;
4907 struct objfile *objfile = cu->objfile;
4908
4909 baseaddr = ANOFFSET (objfile->section_offsets,
4910 SECT_OFF_TEXT (objfile));
4911 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4912 pdi->lowpc + baseaddr,
4913 pdi->highpc - 1 + baseaddr,
9291a0cd 4914 cu->per_cu->v.psymtab);
5734ee8b 4915 }
481860b3
GB
4916 }
4917
4918 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4919 {
bc30ff58 4920 if (!pdi->is_declaration)
e8d05480
JB
4921 /* Ignore subprogram DIEs that do not have a name, they are
4922 illegal. Do not emit a complaint at this point, we will
4923 do so when we convert this psymtab into a symtab. */
4924 if (pdi->name)
4925 add_partial_symbol (pdi, cu);
bc30ff58
JB
4926 }
4927 }
6e70227d 4928
bc30ff58
JB
4929 if (! pdi->has_children)
4930 return;
4931
4932 if (cu->language == language_ada)
4933 {
4934 pdi = pdi->die_child;
4935 while (pdi != NULL)
4936 {
4937 fixup_partial_die (pdi, cu);
4938 if (pdi->tag == DW_TAG_subprogram
4939 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4940 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4941 pdi = pdi->die_sibling;
4942 }
4943 }
4944}
4945
91c24f0a
DC
4946/* Read a partial die corresponding to an enumeration type. */
4947
72bf9492
DJ
4948static void
4949add_partial_enumeration (struct partial_die_info *enum_pdi,
4950 struct dwarf2_cu *cu)
91c24f0a 4951{
72bf9492 4952 struct partial_die_info *pdi;
91c24f0a
DC
4953
4954 if (enum_pdi->name != NULL)
72bf9492
DJ
4955 add_partial_symbol (enum_pdi, cu);
4956
4957 pdi = enum_pdi->die_child;
4958 while (pdi)
91c24f0a 4959 {
72bf9492 4960 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4961 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4962 else
72bf9492
DJ
4963 add_partial_symbol (pdi, cu);
4964 pdi = pdi->die_sibling;
91c24f0a 4965 }
91c24f0a
DC
4966}
4967
6caca83c
CC
4968/* Return the initial uleb128 in the die at INFO_PTR. */
4969
4970static unsigned int
4971peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4972{
4973 unsigned int bytes_read;
4974
4975 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4976}
4977
4bb7a0a7
DJ
4978/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4979 Return the corresponding abbrev, or NULL if the number is zero (indicating
4980 an empty DIE). In either case *BYTES_READ will be set to the length of
4981 the initial number. */
4982
4983static struct abbrev_info *
fe1b8b76 4984peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4985 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4986{
4987 bfd *abfd = cu->objfile->obfd;
4988 unsigned int abbrev_number;
4989 struct abbrev_info *abbrev;
4990
4991 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4992
4993 if (abbrev_number == 0)
4994 return NULL;
4995
4996 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4997 if (!abbrev)
4998 {
3e43a32a
MS
4999 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
5000 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
5001 }
5002
5003 return abbrev;
5004}
5005
93311388
DE
5006/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5007 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
5008 DIE. Any children of the skipped DIEs will also be skipped. */
5009
fe1b8b76 5010static gdb_byte *
dee91e82 5011skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 5012{
dee91e82 5013 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
5014 struct abbrev_info *abbrev;
5015 unsigned int bytes_read;
5016
5017 while (1)
5018 {
5019 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5020 if (abbrev == NULL)
5021 return info_ptr + bytes_read;
5022 else
dee91e82 5023 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
5024 }
5025}
5026
93311388
DE
5027/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5028 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
5029 abbrev corresponding to that skipped uleb128 should be passed in
5030 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5031 children. */
5032
fe1b8b76 5033static gdb_byte *
dee91e82
DE
5034skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5035 struct abbrev_info *abbrev)
4bb7a0a7
DJ
5036{
5037 unsigned int bytes_read;
5038 struct attribute attr;
dee91e82
DE
5039 bfd *abfd = reader->abfd;
5040 struct dwarf2_cu *cu = reader->cu;
5041 gdb_byte *buffer = reader->buffer;
f664829e
DE
5042 const gdb_byte *buffer_end = reader->buffer_end;
5043 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
5044 unsigned int form, i;
5045
5046 for (i = 0; i < abbrev->num_attrs; i++)
5047 {
5048 /* The only abbrev we care about is DW_AT_sibling. */
5049 if (abbrev->attrs[i].name == DW_AT_sibling)
5050 {
dee91e82 5051 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 5052 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
5053 complaint (&symfile_complaints,
5054 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 5055 else
b64f50a1 5056 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
5057 }
5058
5059 /* If it isn't DW_AT_sibling, skip this attribute. */
5060 form = abbrev->attrs[i].form;
5061 skip_attribute:
5062 switch (form)
5063 {
4bb7a0a7 5064 case DW_FORM_ref_addr:
ae411497
TT
5065 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5066 and later it is offset sized. */
5067 if (cu->header.version == 2)
5068 info_ptr += cu->header.addr_size;
5069 else
5070 info_ptr += cu->header.offset_size;
5071 break;
5072 case DW_FORM_addr:
4bb7a0a7
DJ
5073 info_ptr += cu->header.addr_size;
5074 break;
5075 case DW_FORM_data1:
5076 case DW_FORM_ref1:
5077 case DW_FORM_flag:
5078 info_ptr += 1;
5079 break;
2dc7f7b3
TT
5080 case DW_FORM_flag_present:
5081 break;
4bb7a0a7
DJ
5082 case DW_FORM_data2:
5083 case DW_FORM_ref2:
5084 info_ptr += 2;
5085 break;
5086 case DW_FORM_data4:
5087 case DW_FORM_ref4:
5088 info_ptr += 4;
5089 break;
5090 case DW_FORM_data8:
5091 case DW_FORM_ref8:
55f1336d 5092 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
5093 info_ptr += 8;
5094 break;
5095 case DW_FORM_string:
9b1c24c8 5096 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
5097 info_ptr += bytes_read;
5098 break;
2dc7f7b3 5099 case DW_FORM_sec_offset:
4bb7a0a7
DJ
5100 case DW_FORM_strp:
5101 info_ptr += cu->header.offset_size;
5102 break;
2dc7f7b3 5103 case DW_FORM_exprloc:
4bb7a0a7
DJ
5104 case DW_FORM_block:
5105 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5106 info_ptr += bytes_read;
5107 break;
5108 case DW_FORM_block1:
5109 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5110 break;
5111 case DW_FORM_block2:
5112 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5113 break;
5114 case DW_FORM_block4:
5115 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5116 break;
5117 case DW_FORM_sdata:
5118 case DW_FORM_udata:
5119 case DW_FORM_ref_udata:
3019eac3
DE
5120 case DW_FORM_GNU_addr_index:
5121 case DW_FORM_GNU_str_index:
f664829e 5122 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
5123 break;
5124 case DW_FORM_indirect:
5125 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5126 info_ptr += bytes_read;
5127 /* We need to continue parsing from here, so just go back to
5128 the top. */
5129 goto skip_attribute;
5130
5131 default:
3e43a32a
MS
5132 error (_("Dwarf Error: Cannot handle %s "
5133 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
5134 dwarf_form_name (form),
5135 bfd_get_filename (abfd));
5136 }
5137 }
5138
5139 if (abbrev->has_children)
dee91e82 5140 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
5141 else
5142 return info_ptr;
5143}
5144
93311388 5145/* Locate ORIG_PDI's sibling.
dee91e82 5146 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 5147
fe1b8b76 5148static gdb_byte *
dee91e82
DE
5149locate_pdi_sibling (const struct die_reader_specs *reader,
5150 struct partial_die_info *orig_pdi,
5151 gdb_byte *info_ptr)
91c24f0a
DC
5152{
5153 /* Do we know the sibling already? */
72bf9492 5154
91c24f0a
DC
5155 if (orig_pdi->sibling)
5156 return orig_pdi->sibling;
5157
5158 /* Are there any children to deal with? */
5159
5160 if (!orig_pdi->has_children)
5161 return info_ptr;
5162
4bb7a0a7 5163 /* Skip the children the long way. */
91c24f0a 5164
dee91e82 5165 return skip_children (reader, info_ptr);
91c24f0a
DC
5166}
5167
c906108c
SS
5168/* Expand this partial symbol table into a full symbol table. */
5169
5170static void
fba45db2 5171dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 5172{
c906108c
SS
5173 if (pst != NULL)
5174 {
5175 if (pst->readin)
5176 {
3e43a32a
MS
5177 warning (_("bug: psymtab for %s is already read in."),
5178 pst->filename);
c906108c
SS
5179 }
5180 else
5181 {
5182 if (info_verbose)
5183 {
3e43a32a
MS
5184 printf_filtered (_("Reading in symbols for %s..."),
5185 pst->filename);
c906108c
SS
5186 gdb_flush (gdb_stdout);
5187 }
5188
10b3939b
DJ
5189 /* Restore our global data. */
5190 dwarf2_per_objfile = objfile_data (pst->objfile,
5191 dwarf2_objfile_data_key);
5192
b2ab525c
KB
5193 /* If this psymtab is constructed from a debug-only objfile, the
5194 has_section_at_zero flag will not necessarily be correct. We
5195 can get the correct value for this flag by looking at the data
5196 associated with the (presumably stripped) associated objfile. */
5197 if (pst->objfile->separate_debug_objfile_backlink)
5198 {
5199 struct dwarf2_per_objfile *dpo_backlink
5200 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5201 dwarf2_objfile_data_key);
9a619af0 5202
b2ab525c
KB
5203 dwarf2_per_objfile->has_section_at_zero
5204 = dpo_backlink->has_section_at_zero;
5205 }
5206
98bfdba5
PA
5207 dwarf2_per_objfile->reading_partial_symbols = 0;
5208
c906108c
SS
5209 psymtab_to_symtab_1 (pst);
5210
5211 /* Finish up the debug error message. */
5212 if (info_verbose)
a3f17187 5213 printf_filtered (_("done.\n"));
c906108c
SS
5214 }
5215 }
95554aad
TT
5216
5217 process_cu_includes ();
c906108c 5218}
9cdd5dbd
DE
5219\f
5220/* Reading in full CUs. */
c906108c 5221
10b3939b
DJ
5222/* Add PER_CU to the queue. */
5223
5224static void
95554aad
TT
5225queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5226 enum language pretend_language)
10b3939b
DJ
5227{
5228 struct dwarf2_queue_item *item;
5229
5230 per_cu->queued = 1;
5231 item = xmalloc (sizeof (*item));
5232 item->per_cu = per_cu;
95554aad 5233 item->pretend_language = pretend_language;
10b3939b
DJ
5234 item->next = NULL;
5235
5236 if (dwarf2_queue == NULL)
5237 dwarf2_queue = item;
5238 else
5239 dwarf2_queue_tail->next = item;
5240
5241 dwarf2_queue_tail = item;
5242}
5243
5244/* Process the queue. */
5245
5246static void
a0f42c21 5247process_queue (void)
10b3939b
DJ
5248{
5249 struct dwarf2_queue_item *item, *next_item;
5250
03dd20cc
DJ
5251 /* The queue starts out with one item, but following a DIE reference
5252 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
5253 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5254 {
9291a0cd
TT
5255 if (dwarf2_per_objfile->using_index
5256 ? !item->per_cu->v.quick->symtab
5257 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
95554aad 5258 process_full_comp_unit (item->per_cu, item->pretend_language);
10b3939b
DJ
5259
5260 item->per_cu->queued = 0;
5261 next_item = item->next;
5262 xfree (item);
5263 }
5264
5265 dwarf2_queue_tail = NULL;
5266}
5267
5268/* Free all allocated queue entries. This function only releases anything if
5269 an error was thrown; if the queue was processed then it would have been
5270 freed as we went along. */
5271
5272static void
5273dwarf2_release_queue (void *dummy)
5274{
5275 struct dwarf2_queue_item *item, *last;
5276
5277 item = dwarf2_queue;
5278 while (item)
5279 {
5280 /* Anything still marked queued is likely to be in an
5281 inconsistent state, so discard it. */
5282 if (item->per_cu->queued)
5283 {
5284 if (item->per_cu->cu != NULL)
dee91e82 5285 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
5286 item->per_cu->queued = 0;
5287 }
5288
5289 last = item;
5290 item = item->next;
5291 xfree (last);
5292 }
5293
5294 dwarf2_queue = dwarf2_queue_tail = NULL;
5295}
5296
5297/* Read in full symbols for PST, and anything it depends on. */
5298
c906108c 5299static void
fba45db2 5300psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 5301{
10b3939b 5302 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
5303 int i;
5304
95554aad
TT
5305 if (pst->readin)
5306 return;
5307
aaa75496 5308 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
5309 if (!pst->dependencies[i]->readin
5310 && pst->dependencies[i]->user == NULL)
aaa75496
JB
5311 {
5312 /* Inform about additional files that need to be read in. */
5313 if (info_verbose)
5314 {
a3f17187 5315 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
5316 fputs_filtered (" ", gdb_stdout);
5317 wrap_here ("");
5318 fputs_filtered ("and ", gdb_stdout);
5319 wrap_here ("");
5320 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 5321 wrap_here (""); /* Flush output. */
aaa75496
JB
5322 gdb_flush (gdb_stdout);
5323 }
5324 psymtab_to_symtab_1 (pst->dependencies[i]);
5325 }
5326
e38df1d0 5327 per_cu = pst->read_symtab_private;
10b3939b
DJ
5328
5329 if (per_cu == NULL)
aaa75496
JB
5330 {
5331 /* It's an include file, no symbols to read for it.
5332 Everything is in the parent symtab. */
5333 pst->readin = 1;
5334 return;
5335 }
c906108c 5336
a0f42c21 5337 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
5338}
5339
dee91e82
DE
5340/* Trivial hash function for die_info: the hash value of a DIE
5341 is its offset in .debug_info for this objfile. */
10b3939b 5342
dee91e82
DE
5343static hashval_t
5344die_hash (const void *item)
10b3939b 5345{
dee91e82 5346 const struct die_info *die = item;
6502dd73 5347
dee91e82
DE
5348 return die->offset.sect_off;
5349}
63d06c5c 5350
dee91e82
DE
5351/* Trivial comparison function for die_info structures: two DIEs
5352 are equal if they have the same offset. */
98bfdba5 5353
dee91e82
DE
5354static int
5355die_eq (const void *item_lhs, const void *item_rhs)
5356{
5357 const struct die_info *die_lhs = item_lhs;
5358 const struct die_info *die_rhs = item_rhs;
c906108c 5359
dee91e82
DE
5360 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5361}
c906108c 5362
dee91e82
DE
5363/* die_reader_func for load_full_comp_unit.
5364 This is identical to read_signatured_type_reader,
5365 but is kept separate for now. */
c906108c 5366
dee91e82
DE
5367static void
5368load_full_comp_unit_reader (const struct die_reader_specs *reader,
5369 gdb_byte *info_ptr,
5370 struct die_info *comp_unit_die,
5371 int has_children,
5372 void *data)
5373{
5374 struct dwarf2_cu *cu = reader->cu;
95554aad 5375 enum language *language_ptr = data;
6caca83c 5376
dee91e82
DE
5377 gdb_assert (cu->die_hash == NULL);
5378 cu->die_hash =
5379 htab_create_alloc_ex (cu->header.length / 12,
5380 die_hash,
5381 die_eq,
5382 NULL,
5383 &cu->comp_unit_obstack,
5384 hashtab_obstack_allocate,
5385 dummy_obstack_deallocate);
e142c38c 5386
dee91e82
DE
5387 if (has_children)
5388 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5389 &info_ptr, comp_unit_die);
5390 cu->dies = comp_unit_die;
5391 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
5392
5393 /* We try not to read any attributes in this function, because not
9cdd5dbd 5394 all CUs needed for references have been loaded yet, and symbol
10b3939b 5395 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
5396 or we won't be able to build types correctly.
5397 Similarly, if we do not read the producer, we can not apply
5398 producer-specific interpretation. */
95554aad 5399 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 5400}
10b3939b 5401
dee91e82 5402/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 5403
dee91e82 5404static void
95554aad
TT
5405load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5406 enum language pretend_language)
dee91e82 5407{
3019eac3 5408 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 5409
95554aad
TT
5410 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5411 &pretend_language);
10b3939b
DJ
5412}
5413
3da10d80
KS
5414/* Add a DIE to the delayed physname list. */
5415
5416static void
5417add_to_method_list (struct type *type, int fnfield_index, int index,
5418 const char *name, struct die_info *die,
5419 struct dwarf2_cu *cu)
5420{
5421 struct delayed_method_info mi;
5422 mi.type = type;
5423 mi.fnfield_index = fnfield_index;
5424 mi.index = index;
5425 mi.name = name;
5426 mi.die = die;
5427 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5428}
5429
5430/* A cleanup for freeing the delayed method list. */
5431
5432static void
5433free_delayed_list (void *ptr)
5434{
5435 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5436 if (cu->method_list != NULL)
5437 {
5438 VEC_free (delayed_method_info, cu->method_list);
5439 cu->method_list = NULL;
5440 }
5441}
5442
5443/* Compute the physnames of any methods on the CU's method list.
5444
5445 The computation of method physnames is delayed in order to avoid the
5446 (bad) condition that one of the method's formal parameters is of an as yet
5447 incomplete type. */
5448
5449static void
5450compute_delayed_physnames (struct dwarf2_cu *cu)
5451{
5452 int i;
5453 struct delayed_method_info *mi;
5454 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5455 {
1d06ead6 5456 const char *physname;
3da10d80
KS
5457 struct fn_fieldlist *fn_flp
5458 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 5459 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
5460 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5461 }
5462}
5463
a766d390
DE
5464/* Go objects should be embedded in a DW_TAG_module DIE,
5465 and it's not clear if/how imported objects will appear.
5466 To keep Go support simple until that's worked out,
5467 go back through what we've read and create something usable.
5468 We could do this while processing each DIE, and feels kinda cleaner,
5469 but that way is more invasive.
5470 This is to, for example, allow the user to type "p var" or "b main"
5471 without having to specify the package name, and allow lookups
5472 of module.object to work in contexts that use the expression
5473 parser. */
5474
5475static void
5476fixup_go_packaging (struct dwarf2_cu *cu)
5477{
5478 char *package_name = NULL;
5479 struct pending *list;
5480 int i;
5481
5482 for (list = global_symbols; list != NULL; list = list->next)
5483 {
5484 for (i = 0; i < list->nsyms; ++i)
5485 {
5486 struct symbol *sym = list->symbol[i];
5487
5488 if (SYMBOL_LANGUAGE (sym) == language_go
5489 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5490 {
5491 char *this_package_name = go_symbol_package_name (sym);
5492
5493 if (this_package_name == NULL)
5494 continue;
5495 if (package_name == NULL)
5496 package_name = this_package_name;
5497 else
5498 {
5499 if (strcmp (package_name, this_package_name) != 0)
5500 complaint (&symfile_complaints,
5501 _("Symtab %s has objects from two different Go packages: %s and %s"),
5502 (sym->symtab && sym->symtab->filename
5503 ? sym->symtab->filename
5504 : cu->objfile->name),
5505 this_package_name, package_name);
5506 xfree (this_package_name);
5507 }
5508 }
5509 }
5510 }
5511
5512 if (package_name != NULL)
5513 {
5514 struct objfile *objfile = cu->objfile;
5515 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5516 package_name, objfile);
5517 struct symbol *sym;
5518
5519 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5520
5521 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5522 SYMBOL_SET_LANGUAGE (sym, language_go);
5523 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5524 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5525 e.g., "main" finds the "main" module and not C's main(). */
5526 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5527 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5528 SYMBOL_TYPE (sym) = type;
5529
5530 add_symbol_to_list (sym, &global_symbols);
5531
5532 xfree (package_name);
5533 }
5534}
5535
95554aad
TT
5536static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5537
5538/* Return the symtab for PER_CU. This works properly regardless of
5539 whether we're using the index or psymtabs. */
5540
5541static struct symtab *
5542get_symtab (struct dwarf2_per_cu_data *per_cu)
5543{
5544 return (dwarf2_per_objfile->using_index
5545 ? per_cu->v.quick->symtab
5546 : per_cu->v.psymtab->symtab);
5547}
5548
5549/* A helper function for computing the list of all symbol tables
5550 included by PER_CU. */
5551
5552static void
5553recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5554 htab_t all_children,
5555 struct dwarf2_per_cu_data *per_cu)
5556{
5557 void **slot;
5558 int ix;
5559 struct dwarf2_per_cu_data *iter;
5560
5561 slot = htab_find_slot (all_children, per_cu, INSERT);
5562 if (*slot != NULL)
5563 {
5564 /* This inclusion and its children have been processed. */
5565 return;
5566 }
5567
5568 *slot = per_cu;
5569 /* Only add a CU if it has a symbol table. */
5570 if (get_symtab (per_cu) != NULL)
5571 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5572
5573 for (ix = 0;
5574 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5575 ++ix)
5576 recursively_compute_inclusions (result, all_children, iter);
5577}
5578
5579/* Compute the symtab 'includes' fields for the symtab related to
5580 PER_CU. */
5581
5582static void
5583compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5584{
5585 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5586 {
5587 int ix, len;
5588 struct dwarf2_per_cu_data *iter;
5589 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5590 htab_t all_children;
5591 struct symtab *symtab = get_symtab (per_cu);
5592
5593 /* If we don't have a symtab, we can just skip this case. */
5594 if (symtab == NULL)
5595 return;
5596
5597 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5598 NULL, xcalloc, xfree);
5599
5600 for (ix = 0;
5601 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5602 ix, iter);
5603 ++ix)
5604 recursively_compute_inclusions (&result_children, all_children, iter);
5605
5606 /* Now we have a transitive closure of all the included CUs, so
5607 we can convert it to a list of symtabs. */
5608 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5609 symtab->includes
5610 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5611 (len + 1) * sizeof (struct symtab *));
5612 for (ix = 0;
5613 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5614 ++ix)
5615 symtab->includes[ix] = get_symtab (iter);
5616 symtab->includes[len] = NULL;
5617
5618 VEC_free (dwarf2_per_cu_ptr, result_children);
5619 htab_delete (all_children);
5620 }
5621}
5622
5623/* Compute the 'includes' field for the symtabs of all the CUs we just
5624 read. */
5625
5626static void
5627process_cu_includes (void)
5628{
5629 int ix;
5630 struct dwarf2_per_cu_data *iter;
5631
5632 for (ix = 0;
5633 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5634 ix, iter);
5635 ++ix)
5636 compute_symtab_includes (iter);
5637
5638 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5639}
5640
9cdd5dbd 5641/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
5642 already been loaded into memory. */
5643
5644static void
95554aad
TT
5645process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5646 enum language pretend_language)
10b3939b 5647{
10b3939b 5648 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 5649 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
5650 CORE_ADDR lowpc, highpc;
5651 struct symtab *symtab;
3da10d80 5652 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
5653 CORE_ADDR baseaddr;
5654
5655 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5656
10b3939b
DJ
5657 buildsym_init ();
5658 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 5659 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
5660
5661 cu->list_in_scope = &file_symbols;
c906108c 5662
95554aad
TT
5663 cu->language = pretend_language;
5664 cu->language_defn = language_def (cu->language);
5665
c906108c 5666 /* Do line number decoding in read_file_scope () */
10b3939b 5667 process_die (cu->dies, cu);
c906108c 5668
a766d390
DE
5669 /* For now fudge the Go package. */
5670 if (cu->language == language_go)
5671 fixup_go_packaging (cu);
5672
3da10d80
KS
5673 /* Now that we have processed all the DIEs in the CU, all the types
5674 should be complete, and it should now be safe to compute all of the
5675 physnames. */
5676 compute_delayed_physnames (cu);
5677 do_cleanups (delayed_list_cleanup);
5678
fae299cd
DC
5679 /* Some compilers don't define a DW_AT_high_pc attribute for the
5680 compilation unit. If the DW_AT_high_pc is missing, synthesize
5681 it, by scanning the DIE's below the compilation unit. */
10b3939b 5682 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 5683
613e1657 5684 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 5685
8be455d7 5686 if (symtab != NULL)
c906108c 5687 {
df15bd07 5688 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 5689
8be455d7
JK
5690 /* Set symtab language to language from DW_AT_language. If the
5691 compilation is from a C file generated by language preprocessors, do
5692 not set the language if it was already deduced by start_subfile. */
5693 if (!(cu->language == language_c && symtab->language != language_c))
5694 symtab->language = cu->language;
5695
5696 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5697 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
5698 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5699 there were bugs in prologue debug info, fixed later in GCC-4.5
5700 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
5701
5702 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5703 needed, it would be wrong due to missing DW_AT_producer there.
5704
5705 Still one can confuse GDB by using non-standard GCC compilation
5706 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5707 */
ab260dad 5708 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 5709 symtab->locations_valid = 1;
e0d00bc7
JK
5710
5711 if (gcc_4_minor >= 5)
5712 symtab->epilogue_unwind_valid = 1;
96408a79
SA
5713
5714 symtab->call_site_htab = cu->call_site_htab;
c906108c 5715 }
9291a0cd
TT
5716
5717 if (dwarf2_per_objfile->using_index)
5718 per_cu->v.quick->symtab = symtab;
5719 else
5720 {
5721 struct partial_symtab *pst = per_cu->v.psymtab;
5722 pst->symtab = symtab;
5723 pst->readin = 1;
5724 }
c906108c 5725
95554aad
TT
5726 /* Push it for inclusion processing later. */
5727 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5728
c906108c
SS
5729 do_cleanups (back_to);
5730}
5731
95554aad
TT
5732/* Process an imported unit DIE. */
5733
5734static void
5735process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5736{
5737 struct attribute *attr;
5738
5739 attr = dwarf2_attr (die, DW_AT_import, cu);
5740 if (attr != NULL)
5741 {
5742 struct dwarf2_per_cu_data *per_cu;
5743 struct symtab *imported_symtab;
5744 sect_offset offset;
5745
5746 offset = dwarf2_get_ref_die_offset (attr);
5747 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5748
5749 /* Queue the unit, if needed. */
5750 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
5751 load_full_comp_unit (per_cu, cu->language);
5752
5753 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5754 per_cu);
5755 }
5756}
5757
c906108c
SS
5758/* Process a die and its children. */
5759
5760static void
e7c27a73 5761process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
5762{
5763 switch (die->tag)
5764 {
5765 case DW_TAG_padding:
5766 break;
5767 case DW_TAG_compile_unit:
95554aad 5768 case DW_TAG_partial_unit:
e7c27a73 5769 read_file_scope (die, cu);
c906108c 5770 break;
348e048f
DE
5771 case DW_TAG_type_unit:
5772 read_type_unit_scope (die, cu);
5773 break;
c906108c 5774 case DW_TAG_subprogram:
c906108c 5775 case DW_TAG_inlined_subroutine:
edb3359d 5776 read_func_scope (die, cu);
c906108c
SS
5777 break;
5778 case DW_TAG_lexical_block:
14898363
L
5779 case DW_TAG_try_block:
5780 case DW_TAG_catch_block:
e7c27a73 5781 read_lexical_block_scope (die, cu);
c906108c 5782 break;
96408a79
SA
5783 case DW_TAG_GNU_call_site:
5784 read_call_site_scope (die, cu);
5785 break;
c906108c 5786 case DW_TAG_class_type:
680b30c7 5787 case DW_TAG_interface_type:
c906108c
SS
5788 case DW_TAG_structure_type:
5789 case DW_TAG_union_type:
134d01f1 5790 process_structure_scope (die, cu);
c906108c
SS
5791 break;
5792 case DW_TAG_enumeration_type:
134d01f1 5793 process_enumeration_scope (die, cu);
c906108c 5794 break;
134d01f1 5795
f792889a
DJ
5796 /* These dies have a type, but processing them does not create
5797 a symbol or recurse to process the children. Therefore we can
5798 read them on-demand through read_type_die. */
c906108c 5799 case DW_TAG_subroutine_type:
72019c9c 5800 case DW_TAG_set_type:
c906108c 5801 case DW_TAG_array_type:
c906108c 5802 case DW_TAG_pointer_type:
c906108c 5803 case DW_TAG_ptr_to_member_type:
c906108c 5804 case DW_TAG_reference_type:
c906108c 5805 case DW_TAG_string_type:
c906108c 5806 break;
134d01f1 5807
c906108c 5808 case DW_TAG_base_type:
a02abb62 5809 case DW_TAG_subrange_type:
cb249c71 5810 case DW_TAG_typedef:
134d01f1
DJ
5811 /* Add a typedef symbol for the type definition, if it has a
5812 DW_AT_name. */
f792889a 5813 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 5814 break;
c906108c 5815 case DW_TAG_common_block:
e7c27a73 5816 read_common_block (die, cu);
c906108c
SS
5817 break;
5818 case DW_TAG_common_inclusion:
5819 break;
d9fa45fe 5820 case DW_TAG_namespace:
63d06c5c 5821 processing_has_namespace_info = 1;
e7c27a73 5822 read_namespace (die, cu);
d9fa45fe 5823 break;
5d7cb8df 5824 case DW_TAG_module:
f55ee35c 5825 processing_has_namespace_info = 1;
5d7cb8df
JK
5826 read_module (die, cu);
5827 break;
d9fa45fe
DC
5828 case DW_TAG_imported_declaration:
5829 case DW_TAG_imported_module:
63d06c5c 5830 processing_has_namespace_info = 1;
27aa8d6a
SW
5831 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5832 || cu->language != language_fortran))
5833 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5834 dwarf_tag_name (die->tag));
5835 read_import_statement (die, cu);
d9fa45fe 5836 break;
95554aad
TT
5837
5838 case DW_TAG_imported_unit:
5839 process_imported_unit_die (die, cu);
5840 break;
5841
c906108c 5842 default:
e7c27a73 5843 new_symbol (die, NULL, cu);
c906108c
SS
5844 break;
5845 }
5846}
5847
94af9270
KS
5848/* A helper function for dwarf2_compute_name which determines whether DIE
5849 needs to have the name of the scope prepended to the name listed in the
5850 die. */
5851
5852static int
5853die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5854{
1c809c68
TT
5855 struct attribute *attr;
5856
94af9270
KS
5857 switch (die->tag)
5858 {
5859 case DW_TAG_namespace:
5860 case DW_TAG_typedef:
5861 case DW_TAG_class_type:
5862 case DW_TAG_interface_type:
5863 case DW_TAG_structure_type:
5864 case DW_TAG_union_type:
5865 case DW_TAG_enumeration_type:
5866 case DW_TAG_enumerator:
5867 case DW_TAG_subprogram:
5868 case DW_TAG_member:
5869 return 1;
5870
5871 case DW_TAG_variable:
c2b0a229 5872 case DW_TAG_constant:
94af9270
KS
5873 /* We only need to prefix "globally" visible variables. These include
5874 any variable marked with DW_AT_external or any variable that
5875 lives in a namespace. [Variables in anonymous namespaces
5876 require prefixing, but they are not DW_AT_external.] */
5877
5878 if (dwarf2_attr (die, DW_AT_specification, cu))
5879 {
5880 struct dwarf2_cu *spec_cu = cu;
9a619af0 5881
94af9270
KS
5882 return die_needs_namespace (die_specification (die, &spec_cu),
5883 spec_cu);
5884 }
5885
1c809c68 5886 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
5887 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5888 && die->parent->tag != DW_TAG_module)
1c809c68
TT
5889 return 0;
5890 /* A variable in a lexical block of some kind does not need a
5891 namespace, even though in C++ such variables may be external
5892 and have a mangled name. */
5893 if (die->parent->tag == DW_TAG_lexical_block
5894 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5895 || die->parent->tag == DW_TAG_catch_block
5896 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5897 return 0;
5898 return 1;
94af9270
KS
5899
5900 default:
5901 return 0;
5902 }
5903}
5904
98bfdba5
PA
5905/* Retrieve the last character from a mem_file. */
5906
5907static void
5908do_ui_file_peek_last (void *object, const char *buffer, long length)
5909{
5910 char *last_char_p = (char *) object;
5911
5912 if (length > 0)
5913 *last_char_p = buffer[length - 1];
5914}
5915
94af9270 5916/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
5917 compute the physname for the object, which include a method's:
5918 - formal parameters (C++/Java),
5919 - receiver type (Go),
5920 - return type (Java).
5921
5922 The term "physname" is a bit confusing.
5923 For C++, for example, it is the demangled name.
5924 For Go, for example, it's the mangled name.
94af9270 5925
af6b7be1
JB
5926 For Ada, return the DIE's linkage name rather than the fully qualified
5927 name. PHYSNAME is ignored..
5928
94af9270
KS
5929 The result is allocated on the objfile_obstack and canonicalized. */
5930
5931static const char *
5932dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5933 int physname)
5934{
bb5ed363
DE
5935 struct objfile *objfile = cu->objfile;
5936
94af9270
KS
5937 if (name == NULL)
5938 name = dwarf2_name (die, cu);
5939
f55ee35c
JK
5940 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5941 compute it by typename_concat inside GDB. */
5942 if (cu->language == language_ada
5943 || (cu->language == language_fortran && physname))
5944 {
5945 /* For Ada unit, we prefer the linkage name over the name, as
5946 the former contains the exported name, which the user expects
5947 to be able to reference. Ideally, we want the user to be able
5948 to reference this entity using either natural or linkage name,
5949 but we haven't started looking at this enhancement yet. */
5950 struct attribute *attr;
5951
5952 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5953 if (attr == NULL)
5954 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5955 if (attr && DW_STRING (attr))
5956 return DW_STRING (attr);
5957 }
5958
94af9270
KS
5959 /* These are the only languages we know how to qualify names in. */
5960 if (name != NULL
f55ee35c
JK
5961 && (cu->language == language_cplus || cu->language == language_java
5962 || cu->language == language_fortran))
94af9270
KS
5963 {
5964 if (die_needs_namespace (die, cu))
5965 {
5966 long length;
0d5cff50 5967 const char *prefix;
94af9270
KS
5968 struct ui_file *buf;
5969
5970 prefix = determine_prefix (die, cu);
5971 buf = mem_fileopen ();
5972 if (*prefix != '\0')
5973 {
f55ee35c
JK
5974 char *prefixed_name = typename_concat (NULL, prefix, name,
5975 physname, cu);
9a619af0 5976
94af9270
KS
5977 fputs_unfiltered (prefixed_name, buf);
5978 xfree (prefixed_name);
5979 }
5980 else
62d5b8da 5981 fputs_unfiltered (name, buf);
94af9270 5982
98bfdba5
PA
5983 /* Template parameters may be specified in the DIE's DW_AT_name, or
5984 as children with DW_TAG_template_type_param or
5985 DW_TAG_value_type_param. If the latter, add them to the name
5986 here. If the name already has template parameters, then
5987 skip this step; some versions of GCC emit both, and
5988 it is more efficient to use the pre-computed name.
5989
5990 Something to keep in mind about this process: it is very
5991 unlikely, or in some cases downright impossible, to produce
5992 something that will match the mangled name of a function.
5993 If the definition of the function has the same debug info,
5994 we should be able to match up with it anyway. But fallbacks
5995 using the minimal symbol, for instance to find a method
5996 implemented in a stripped copy of libstdc++, will not work.
5997 If we do not have debug info for the definition, we will have to
5998 match them up some other way.
5999
6000 When we do name matching there is a related problem with function
6001 templates; two instantiated function templates are allowed to
6002 differ only by their return types, which we do not add here. */
6003
6004 if (cu->language == language_cplus && strchr (name, '<') == NULL)
6005 {
6006 struct attribute *attr;
6007 struct die_info *child;
6008 int first = 1;
6009
6010 die->building_fullname = 1;
6011
6012 for (child = die->child; child != NULL; child = child->sibling)
6013 {
6014 struct type *type;
12df843f 6015 LONGEST value;
98bfdba5
PA
6016 gdb_byte *bytes;
6017 struct dwarf2_locexpr_baton *baton;
6018 struct value *v;
6019
6020 if (child->tag != DW_TAG_template_type_param
6021 && child->tag != DW_TAG_template_value_param)
6022 continue;
6023
6024 if (first)
6025 {
6026 fputs_unfiltered ("<", buf);
6027 first = 0;
6028 }
6029 else
6030 fputs_unfiltered (", ", buf);
6031
6032 attr = dwarf2_attr (child, DW_AT_type, cu);
6033 if (attr == NULL)
6034 {
6035 complaint (&symfile_complaints,
6036 _("template parameter missing DW_AT_type"));
6037 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6038 continue;
6039 }
6040 type = die_type (child, cu);
6041
6042 if (child->tag == DW_TAG_template_type_param)
6043 {
6044 c_print_type (type, "", buf, -1, 0);
6045 continue;
6046 }
6047
6048 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6049 if (attr == NULL)
6050 {
6051 complaint (&symfile_complaints,
3e43a32a
MS
6052 _("template parameter missing "
6053 "DW_AT_const_value"));
98bfdba5
PA
6054 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6055 continue;
6056 }
6057
6058 dwarf2_const_value_attr (attr, type, name,
6059 &cu->comp_unit_obstack, cu,
6060 &value, &bytes, &baton);
6061
6062 if (TYPE_NOSIGN (type))
6063 /* GDB prints characters as NUMBER 'CHAR'. If that's
6064 changed, this can use value_print instead. */
6065 c_printchar (value, type, buf);
6066 else
6067 {
6068 struct value_print_options opts;
6069
6070 if (baton != NULL)
6071 v = dwarf2_evaluate_loc_desc (type, NULL,
6072 baton->data,
6073 baton->size,
6074 baton->per_cu);
6075 else if (bytes != NULL)
6076 {
6077 v = allocate_value (type);
6078 memcpy (value_contents_writeable (v), bytes,
6079 TYPE_LENGTH (type));
6080 }
6081 else
6082 v = value_from_longest (type, value);
6083
3e43a32a
MS
6084 /* Specify decimal so that we do not depend on
6085 the radix. */
98bfdba5
PA
6086 get_formatted_print_options (&opts, 'd');
6087 opts.raw = 1;
6088 value_print (v, buf, &opts);
6089 release_value (v);
6090 value_free (v);
6091 }
6092 }
6093
6094 die->building_fullname = 0;
6095
6096 if (!first)
6097 {
6098 /* Close the argument list, with a space if necessary
6099 (nested templates). */
6100 char last_char = '\0';
6101 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6102 if (last_char == '>')
6103 fputs_unfiltered (" >", buf);
6104 else
6105 fputs_unfiltered (">", buf);
6106 }
6107 }
6108
94af9270
KS
6109 /* For Java and C++ methods, append formal parameter type
6110 information, if PHYSNAME. */
6e70227d 6111
94af9270
KS
6112 if (physname && die->tag == DW_TAG_subprogram
6113 && (cu->language == language_cplus
6114 || cu->language == language_java))
6115 {
6116 struct type *type = read_type_die (die, cu);
6117
3167638f 6118 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
6119
6120 if (cu->language == language_java)
6121 {
6122 /* For java, we must append the return type to method
0963b4bd 6123 names. */
94af9270
KS
6124 if (die->tag == DW_TAG_subprogram)
6125 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6126 0, 0);
6127 }
6128 else if (cu->language == language_cplus)
6129 {
60430eff
DJ
6130 /* Assume that an artificial first parameter is
6131 "this", but do not crash if it is not. RealView
6132 marks unnamed (and thus unused) parameters as
6133 artificial; there is no way to differentiate
6134 the two cases. */
94af9270
KS
6135 if (TYPE_NFIELDS (type) > 0
6136 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 6137 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
6138 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6139 0))))
94af9270
KS
6140 fputs_unfiltered (" const", buf);
6141 }
6142 }
6143
bb5ed363 6144 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
6145 &length);
6146 ui_file_delete (buf);
6147
6148 if (cu->language == language_cplus)
6149 {
6150 char *cname
6151 = dwarf2_canonicalize_name (name, cu,
bb5ed363 6152 &objfile->objfile_obstack);
9a619af0 6153
94af9270
KS
6154 if (cname != NULL)
6155 name = cname;
6156 }
6157 }
6158 }
6159
6160 return name;
6161}
6162
0114d602
DJ
6163/* Return the fully qualified name of DIE, based on its DW_AT_name.
6164 If scope qualifiers are appropriate they will be added. The result
6165 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
6166 not have a name. NAME may either be from a previous call to
6167 dwarf2_name or NULL.
6168
0963b4bd 6169 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
6170
6171static const char *
94af9270 6172dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 6173{
94af9270
KS
6174 return dwarf2_compute_name (name, die, cu, 0);
6175}
0114d602 6176
94af9270
KS
6177/* Construct a physname for the given DIE in CU. NAME may either be
6178 from a previous call to dwarf2_name or NULL. The result will be
6179 allocated on the objfile_objstack or NULL if the DIE does not have a
6180 name.
0114d602 6181
94af9270 6182 The output string will be canonicalized (if C++/Java). */
0114d602 6183
94af9270
KS
6184static const char *
6185dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6186{
bb5ed363 6187 struct objfile *objfile = cu->objfile;
900e11f9
JK
6188 struct attribute *attr;
6189 const char *retval, *mangled = NULL, *canon = NULL;
6190 struct cleanup *back_to;
6191 int need_copy = 1;
6192
6193 /* In this case dwarf2_compute_name is just a shortcut not building anything
6194 on its own. */
6195 if (!die_needs_namespace (die, cu))
6196 return dwarf2_compute_name (name, die, cu, 1);
6197
6198 back_to = make_cleanup (null_cleanup, NULL);
6199
6200 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6201 if (!attr)
6202 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6203
6204 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6205 has computed. */
6206 if (attr && DW_STRING (attr))
6207 {
6208 char *demangled;
6209
6210 mangled = DW_STRING (attr);
6211
6212 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6213 type. It is easier for GDB users to search for such functions as
6214 `name(params)' than `long name(params)'. In such case the minimal
6215 symbol names do not match the full symbol names but for template
6216 functions there is never a need to look up their definition from their
6217 declaration so the only disadvantage remains the minimal symbol
6218 variant `long name(params)' does not have the proper inferior type.
6219 */
6220
a766d390
DE
6221 if (cu->language == language_go)
6222 {
6223 /* This is a lie, but we already lie to the caller new_symbol_full.
6224 new_symbol_full assumes we return the mangled name.
6225 This just undoes that lie until things are cleaned up. */
6226 demangled = NULL;
6227 }
6228 else
6229 {
6230 demangled = cplus_demangle (mangled,
6231 (DMGL_PARAMS | DMGL_ANSI
6232 | (cu->language == language_java
6233 ? DMGL_JAVA | DMGL_RET_POSTFIX
6234 : DMGL_RET_DROP)));
6235 }
900e11f9
JK
6236 if (demangled)
6237 {
6238 make_cleanup (xfree, demangled);
6239 canon = demangled;
6240 }
6241 else
6242 {
6243 canon = mangled;
6244 need_copy = 0;
6245 }
6246 }
6247
6248 if (canon == NULL || check_physname)
6249 {
6250 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6251
6252 if (canon != NULL && strcmp (physname, canon) != 0)
6253 {
6254 /* It may not mean a bug in GDB. The compiler could also
6255 compute DW_AT_linkage_name incorrectly. But in such case
6256 GDB would need to be bug-to-bug compatible. */
6257
6258 complaint (&symfile_complaints,
6259 _("Computed physname <%s> does not match demangled <%s> "
6260 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 6261 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
6262
6263 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6264 is available here - over computed PHYSNAME. It is safer
6265 against both buggy GDB and buggy compilers. */
6266
6267 retval = canon;
6268 }
6269 else
6270 {
6271 retval = physname;
6272 need_copy = 0;
6273 }
6274 }
6275 else
6276 retval = canon;
6277
6278 if (need_copy)
6279 retval = obsavestring (retval, strlen (retval),
bb5ed363 6280 &objfile->objfile_obstack);
900e11f9
JK
6281
6282 do_cleanups (back_to);
6283 return retval;
0114d602
DJ
6284}
6285
27aa8d6a
SW
6286/* Read the import statement specified by the given die and record it. */
6287
6288static void
6289read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6290{
bb5ed363 6291 struct objfile *objfile = cu->objfile;
27aa8d6a 6292 struct attribute *import_attr;
32019081 6293 struct die_info *imported_die, *child_die;
de4affc9 6294 struct dwarf2_cu *imported_cu;
27aa8d6a 6295 const char *imported_name;
794684b6 6296 const char *imported_name_prefix;
13387711
SW
6297 const char *canonical_name;
6298 const char *import_alias;
6299 const char *imported_declaration = NULL;
794684b6 6300 const char *import_prefix;
32019081
JK
6301 VEC (const_char_ptr) *excludes = NULL;
6302 struct cleanup *cleanups;
13387711
SW
6303
6304 char *temp;
27aa8d6a
SW
6305
6306 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6307 if (import_attr == NULL)
6308 {
6309 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6310 dwarf_tag_name (die->tag));
6311 return;
6312 }
6313
de4affc9
CC
6314 imported_cu = cu;
6315 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6316 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
6317 if (imported_name == NULL)
6318 {
6319 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6320
6321 The import in the following code:
6322 namespace A
6323 {
6324 typedef int B;
6325 }
6326
6327 int main ()
6328 {
6329 using A::B;
6330 B b;
6331 return b;
6332 }
6333
6334 ...
6335 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6336 <52> DW_AT_decl_file : 1
6337 <53> DW_AT_decl_line : 6
6338 <54> DW_AT_import : <0x75>
6339 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6340 <59> DW_AT_name : B
6341 <5b> DW_AT_decl_file : 1
6342 <5c> DW_AT_decl_line : 2
6343 <5d> DW_AT_type : <0x6e>
6344 ...
6345 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6346 <76> DW_AT_byte_size : 4
6347 <77> DW_AT_encoding : 5 (signed)
6348
6349 imports the wrong die ( 0x75 instead of 0x58 ).
6350 This case will be ignored until the gcc bug is fixed. */
6351 return;
6352 }
6353
82856980
SW
6354 /* Figure out the local name after import. */
6355 import_alias = dwarf2_name (die, cu);
27aa8d6a 6356
794684b6
SW
6357 /* Figure out where the statement is being imported to. */
6358 import_prefix = determine_prefix (die, cu);
6359
6360 /* Figure out what the scope of the imported die is and prepend it
6361 to the name of the imported die. */
de4affc9 6362 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 6363
f55ee35c
JK
6364 if (imported_die->tag != DW_TAG_namespace
6365 && imported_die->tag != DW_TAG_module)
794684b6 6366 {
13387711
SW
6367 imported_declaration = imported_name;
6368 canonical_name = imported_name_prefix;
794684b6 6369 }
13387711 6370 else if (strlen (imported_name_prefix) > 0)
794684b6 6371 {
13387711
SW
6372 temp = alloca (strlen (imported_name_prefix)
6373 + 2 + strlen (imported_name) + 1);
6374 strcpy (temp, imported_name_prefix);
6375 strcat (temp, "::");
6376 strcat (temp, imported_name);
6377 canonical_name = temp;
794684b6 6378 }
13387711
SW
6379 else
6380 canonical_name = imported_name;
794684b6 6381
32019081
JK
6382 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6383
6384 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6385 for (child_die = die->child; child_die && child_die->tag;
6386 child_die = sibling_die (child_die))
6387 {
6388 /* DWARF-4: A Fortran use statement with a “rename list” may be
6389 represented by an imported module entry with an import attribute
6390 referring to the module and owned entries corresponding to those
6391 entities that are renamed as part of being imported. */
6392
6393 if (child_die->tag != DW_TAG_imported_declaration)
6394 {
6395 complaint (&symfile_complaints,
6396 _("child DW_TAG_imported_declaration expected "
6397 "- DIE at 0x%x [in module %s]"),
b64f50a1 6398 child_die->offset.sect_off, objfile->name);
32019081
JK
6399 continue;
6400 }
6401
6402 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6403 if (import_attr == NULL)
6404 {
6405 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6406 dwarf_tag_name (child_die->tag));
6407 continue;
6408 }
6409
6410 imported_cu = cu;
6411 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6412 &imported_cu);
6413 imported_name = dwarf2_name (imported_die, imported_cu);
6414 if (imported_name == NULL)
6415 {
6416 complaint (&symfile_complaints,
6417 _("child DW_TAG_imported_declaration has unknown "
6418 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 6419 child_die->offset.sect_off, objfile->name);
32019081
JK
6420 continue;
6421 }
6422
6423 VEC_safe_push (const_char_ptr, excludes, imported_name);
6424
6425 process_die (child_die, cu);
6426 }
6427
c0cc3a76
SW
6428 cp_add_using_directive (import_prefix,
6429 canonical_name,
6430 import_alias,
13387711 6431 imported_declaration,
32019081 6432 excludes,
bb5ed363 6433 &objfile->objfile_obstack);
32019081
JK
6434
6435 do_cleanups (cleanups);
27aa8d6a
SW
6436}
6437
ae2de4f8
DE
6438/* Cleanup function for read_file_scope. */
6439
cb1df416
DJ
6440static void
6441free_cu_line_header (void *arg)
6442{
6443 struct dwarf2_cu *cu = arg;
6444
6445 free_line_header (cu->line_header);
6446 cu->line_header = NULL;
6447}
6448
9291a0cd
TT
6449static void
6450find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6451 char **name, char **comp_dir)
6452{
6453 struct attribute *attr;
6454
6455 *name = NULL;
6456 *comp_dir = NULL;
6457
6458 /* Find the filename. Do not use dwarf2_name here, since the filename
6459 is not a source language identifier. */
6460 attr = dwarf2_attr (die, DW_AT_name, cu);
6461 if (attr)
6462 {
6463 *name = DW_STRING (attr);
6464 }
6465
6466 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6467 if (attr)
6468 *comp_dir = DW_STRING (attr);
6469 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6470 {
6471 *comp_dir = ldirname (*name);
6472 if (*comp_dir != NULL)
6473 make_cleanup (xfree, *comp_dir);
6474 }
6475 if (*comp_dir != NULL)
6476 {
6477 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6478 directory, get rid of it. */
6479 char *cp = strchr (*comp_dir, ':');
6480
6481 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6482 *comp_dir = cp + 1;
6483 }
6484
6485 if (*name == NULL)
6486 *name = "<unknown>";
6487}
6488
f3f5162e
DE
6489/* Handle DW_AT_stmt_list for a compilation unit or type unit.
6490 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6491 COMP_DIR is the compilation directory.
6492 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
6493
6494static void
6495handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 6496 const char *comp_dir, int want_line_info)
2ab95328
TT
6497{
6498 struct attribute *attr;
2ab95328 6499
2ab95328
TT
6500 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6501 if (attr)
6502 {
6503 unsigned int line_offset = DW_UNSND (attr);
6504 struct line_header *line_header
3019eac3 6505 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
6506
6507 if (line_header)
dee91e82
DE
6508 {
6509 cu->line_header = line_header;
6510 make_cleanup (free_cu_line_header, cu);
f3f5162e 6511 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
dee91e82 6512 }
2ab95328
TT
6513 }
6514}
6515
95554aad 6516/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 6517
c906108c 6518static void
e7c27a73 6519read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6520{
dee91e82 6521 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 6522 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 6523 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
6524 CORE_ADDR highpc = ((CORE_ADDR) 0);
6525 struct attribute *attr;
e1024ff1 6526 char *name = NULL;
c906108c
SS
6527 char *comp_dir = NULL;
6528 struct die_info *child_die;
6529 bfd *abfd = objfile->obfd;
e142c38c 6530 CORE_ADDR baseaddr;
6e70227d 6531
e142c38c 6532 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6533
fae299cd 6534 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
6535
6536 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6537 from finish_block. */
2acceee2 6538 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
6539 lowpc = highpc;
6540 lowpc += baseaddr;
6541 highpc += baseaddr;
6542
9291a0cd 6543 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 6544
95554aad 6545 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 6546
f4b8a18d
KW
6547 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6548 standardised yet. As a workaround for the language detection we fall
6549 back to the DW_AT_producer string. */
6550 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6551 cu->language = language_opencl;
6552
3019eac3
DE
6553 /* Similar hack for Go. */
6554 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6555 set_cu_language (DW_LANG_Go, cu);
6556
6557 /* We assume that we're processing GCC output. */
6558 processing_gcc_compilation = 2;
6559
6560 processing_has_namespace_info = 0;
6561
6562 start_symtab (name, comp_dir, lowpc);
6563 record_debugformat ("DWARF 2");
6564 record_producer (cu->producer);
6565
6566 /* Decode line number information if present. We do this before
6567 processing child DIEs, so that the line header table is available
6568 for DW_AT_decl_file. */
6569 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6570
6571 /* Process all dies in compilation unit. */
6572 if (die->child != NULL)
6573 {
6574 child_die = die->child;
6575 while (child_die && child_die->tag)
6576 {
6577 process_die (child_die, cu);
6578 child_die = sibling_die (child_die);
6579 }
6580 }
6581
6582 /* Decode macro information, if present. Dwarf 2 macro information
6583 refers to information in the line number info statement program
6584 header, so we can only read it if we've read the header
6585 successfully. */
6586 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6587 if (attr && cu->line_header)
6588 {
6589 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6590 complaint (&symfile_complaints,
6591 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6592
6593 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
6594 comp_dir, abfd, cu,
fceca515
DE
6595 &dwarf2_per_objfile->macro, 1,
6596 ".debug_macro");
3019eac3
DE
6597 }
6598 else
6599 {
6600 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6601 if (attr && cu->line_header)
6602 {
6603 unsigned int macro_offset = DW_UNSND (attr);
6604
6605 dwarf_decode_macros (cu->line_header, macro_offset,
6606 comp_dir, abfd, cu,
fceca515
DE
6607 &dwarf2_per_objfile->macinfo, 0,
6608 ".debug_macinfo");
3019eac3
DE
6609 }
6610 }
6611
6612 do_cleanups (back_to);
6613}
6614
6615/* Process DW_TAG_type_unit.
6616 For TUs we want to skip the first top level sibling if it's not the
6617 actual type being defined by this TU. In this case the first top
6618 level sibling is there to provide context only. */
6619
6620static void
6621read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6622{
6623 struct objfile *objfile = cu->objfile;
6624 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6625 CORE_ADDR lowpc;
6626 struct attribute *attr;
6627 char *name = NULL;
6628 char *comp_dir = NULL;
6629 struct die_info *child_die;
6630 bfd *abfd = objfile->obfd;
6631
6632 /* start_symtab needs a low pc, but we don't really have one.
6633 Do what read_file_scope would do in the absence of such info. */
6634 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6635
6636 /* Find the filename. Do not use dwarf2_name here, since the filename
6637 is not a source language identifier. */
6638 attr = dwarf2_attr (die, DW_AT_name, cu);
6639 if (attr)
6640 name = DW_STRING (attr);
6641
6642 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6643 if (attr)
6644 comp_dir = DW_STRING (attr);
6645 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6646 {
6647 comp_dir = ldirname (name);
6648 if (comp_dir != NULL)
6649 make_cleanup (xfree, comp_dir);
6650 }
6651
6652 if (name == NULL)
6653 name = "<unknown>";
6654
95554aad 6655 prepare_one_comp_unit (cu, die, language_minimal);
3019eac3
DE
6656
6657 /* We assume that we're processing GCC output. */
6658 processing_gcc_compilation = 2;
6659
6660 processing_has_namespace_info = 0;
6661
6662 start_symtab (name, comp_dir, lowpc);
6663 record_debugformat ("DWARF 2");
6664 record_producer (cu->producer);
6665
6666 /* Decode line number information if present. We do this before
6667 processing child DIEs, so that the line header table is available
6668 for DW_AT_decl_file.
6669 We don't need the pc/line-number mapping for type units. */
6670 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6671
6672 /* Process the dies in the type unit. */
6673 if (die->child == NULL)
6674 {
6675 dump_die_for_error (die);
6676 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6677 bfd_get_filename (abfd));
6678 }
6679
6680 child_die = die->child;
6681
6682 while (child_die && child_die->tag)
6683 {
6684 process_die (child_die, cu);
6685
6686 child_die = sibling_die (child_die);
6687 }
6688
6689 do_cleanups (back_to);
6690}
6691\f
6692/* DWO files. */
6693
6694static hashval_t
6695hash_dwo_file (const void *item)
6696{
6697 const struct dwo_file *dwo_file = item;
6698
6699 return htab_hash_string (dwo_file->dwo_name);
6700}
6701
6702static int
6703eq_dwo_file (const void *item_lhs, const void *item_rhs)
6704{
6705 const struct dwo_file *lhs = item_lhs;
6706 const struct dwo_file *rhs = item_rhs;
6707
6708 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6709}
6710
6711/* Allocate a hash table for DWO files. */
6712
6713static htab_t
6714allocate_dwo_file_hash_table (void)
6715{
6716 struct objfile *objfile = dwarf2_per_objfile->objfile;
6717
6718 return htab_create_alloc_ex (41,
6719 hash_dwo_file,
6720 eq_dwo_file,
6721 NULL,
6722 &objfile->objfile_obstack,
6723 hashtab_obstack_allocate,
6724 dummy_obstack_deallocate);
6725}
6726
6727static hashval_t
6728hash_dwo_unit (const void *item)
6729{
6730 const struct dwo_unit *dwo_unit = item;
6731
6732 /* This drops the top 32 bits of the id, but is ok for a hash. */
6733 return dwo_unit->signature;
6734}
6735
6736static int
6737eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6738{
6739 const struct dwo_unit *lhs = item_lhs;
6740 const struct dwo_unit *rhs = item_rhs;
6741
6742 /* The signature is assumed to be unique within the DWO file.
6743 So while object file CU dwo_id's always have the value zero,
6744 that's OK, assuming each object file DWO file has only one CU,
6745 and that's the rule for now. */
6746 return lhs->signature == rhs->signature;
6747}
6748
6749/* Allocate a hash table for DWO CUs,TUs.
6750 There is one of these tables for each of CUs,TUs for each DWO file. */
6751
6752static htab_t
6753allocate_dwo_unit_table (struct objfile *objfile)
6754{
6755 /* Start out with a pretty small number.
6756 Generally DWO files contain only one CU and maybe some TUs. */
6757 return htab_create_alloc_ex (3,
6758 hash_dwo_unit,
6759 eq_dwo_unit,
6760 NULL,
6761 &objfile->objfile_obstack,
6762 hashtab_obstack_allocate,
6763 dummy_obstack_deallocate);
6764}
6765
6766/* This function is mapped across the sections and remembers the offset and
6767 size of each of the DWO debugging sections we are interested in. */
6768
6769static void
6770dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6771{
6772 struct dwo_file *dwo_file = dwo_file_ptr;
6773 const struct dwo_section_names *names = &dwo_section_names;
6774
6775 if (section_is_p (sectp->name, &names->abbrev_dwo))
6776 {
6777 dwo_file->sections.abbrev.asection = sectp;
6778 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6779 }
6780 else if (section_is_p (sectp->name, &names->info_dwo))
6781 {
6782 dwo_file->sections.info.asection = sectp;
6783 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6784 }
6785 else if (section_is_p (sectp->name, &names->line_dwo))
6786 {
6787 dwo_file->sections.line.asection = sectp;
6788 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6789 }
6790 else if (section_is_p (sectp->name, &names->loc_dwo))
6791 {
6792 dwo_file->sections.loc.asection = sectp;
6793 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6794 }
6795 else if (section_is_p (sectp->name, &names->str_dwo))
6796 {
6797 dwo_file->sections.str.asection = sectp;
6798 dwo_file->sections.str.size = bfd_get_section_size (sectp);
6799 }
6800 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
6801 {
6802 dwo_file->sections.str_offsets.asection = sectp;
6803 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
6804 }
6805 else if (section_is_p (sectp->name, &names->types_dwo))
6806 {
6807 struct dwarf2_section_info type_section;
6808
6809 memset (&type_section, 0, sizeof (type_section));
6810 type_section.asection = sectp;
6811 type_section.size = bfd_get_section_size (sectp);
6812 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
6813 &type_section);
6814 }
6815}
6816
6817/* Structure used to pass data to create_debug_info_hash_table_reader. */
6818
6819struct create_dwo_info_table_data
6820{
6821 struct dwo_file *dwo_file;
6822 htab_t cu_htab;
6823};
6824
6825/* die_reader_func for create_debug_info_hash_table. */
6826
6827static void
6828create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
6829 gdb_byte *info_ptr,
6830 struct die_info *comp_unit_die,
6831 int has_children,
6832 void *datap)
6833{
6834 struct dwarf2_cu *cu = reader->cu;
6835 struct objfile *objfile = dwarf2_per_objfile->objfile;
6836 sect_offset offset = cu->per_cu->offset;
6837 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
6838 struct create_dwo_info_table_data *data = datap;
6839 struct dwo_file *dwo_file = data->dwo_file;
6840 htab_t cu_htab = data->cu_htab;
6841 void **slot;
6842 struct attribute *attr;
6843 struct dwo_unit *dwo_unit;
6844
6845 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6846 if (attr == NULL)
6847 {
6848 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
6849 " its dwo_id [in module %s]"),
6850 offset.sect_off, dwo_file->dwo_name);
6851 return;
6852 }
6853
6854 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
6855 dwo_unit->dwo_file = dwo_file;
6856 dwo_unit->signature = DW_UNSND (attr);
6857 dwo_unit->info_or_types_section = section;
6858 dwo_unit->offset = offset;
6859 dwo_unit->length = cu->per_cu->length;
6860
6861 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
6862 gdb_assert (slot != NULL);
6863 if (*slot != NULL)
6864 {
6865 const struct dwo_unit *dup_dwo_unit = *slot;
6866
6867 complaint (&symfile_complaints,
6868 _("debug entry at offset 0x%x is duplicate to the entry at"
6869 " offset 0x%x, dwo_id 0x%s [in module %s]"),
6870 offset.sect_off, dup_dwo_unit->offset.sect_off,
6871 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
6872 dwo_file->dwo_name);
6873 }
6874 else
6875 *slot = dwo_unit;
6876
6877 if (dwarf2_die_debug)
6878 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
6879 offset.sect_off,
6880 phex (dwo_unit->signature,
6881 sizeof (dwo_unit->signature)));
6882}
6883
6884/* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
6885
6886static htab_t
6887create_debug_info_hash_table (struct dwo_file *dwo_file)
6888{
6889 struct objfile *objfile = dwarf2_per_objfile->objfile;
6890 struct dwarf2_section_info *section = &dwo_file->sections.info;
6891 bfd *abfd;
6892 htab_t cu_htab;
6893 gdb_byte *info_ptr, *end_ptr;
6894 struct create_dwo_info_table_data create_dwo_info_table_data;
6895
6896 dwarf2_read_section (objfile, section);
6897 info_ptr = section->buffer;
6898
6899 if (info_ptr == NULL)
6900 return NULL;
6901
6902 /* We can't set abfd until now because the section may be empty or
6903 not present, in which case section->asection will be NULL. */
6904 abfd = section->asection->owner;
6905
6906 if (dwarf2_die_debug)
6907 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
6908 bfd_get_filename (abfd));
6909
6910 cu_htab = allocate_dwo_unit_table (objfile);
6911
6912 create_dwo_info_table_data.dwo_file = dwo_file;
6913 create_dwo_info_table_data.cu_htab = cu_htab;
6914
6915 end_ptr = info_ptr + section->size;
6916 while (info_ptr < end_ptr)
6917 {
6918 struct dwarf2_per_cu_data per_cu;
6919
6920 memset (&per_cu, 0, sizeof (per_cu));
6921 per_cu.objfile = objfile;
6922 per_cu.is_debug_types = 0;
6923 per_cu.offset.sect_off = info_ptr - section->buffer;
6924 per_cu.info_or_types_section = section;
6925
6926 init_cutu_and_read_dies_no_follow (&per_cu,
6927 &dwo_file->sections.abbrev,
6928 dwo_file,
6929 create_debug_info_hash_table_reader,
6930 &create_dwo_info_table_data);
6931
6932 info_ptr += per_cu.length;
6933 }
6934
6935 return cu_htab;
6936}
6937
6938/* Subroutine of open_dwo_file to simplify it.
6939 Open the file specified by FILE_NAME and hand it off to BFD for
6940 preliminary analysis. Return a newly initialized bfd *, which
6941 includes a canonicalized copy of FILE_NAME.
6942 In case of trouble, return NULL.
6943 NOTE: This function is derived from symfile_bfd_open. */
6944
6945static bfd *
6946try_open_dwo_file (const char *file_name)
6947{
6948 bfd *sym_bfd;
6949 int desc;
6950 char *absolute_name;
3019eac3
DE
6951
6952 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
6953 O_RDONLY | O_BINARY, &absolute_name);
6954 if (desc < 0)
6955 return NULL;
6956
6957 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
6958 if (!sym_bfd)
6959 {
3019eac3
DE
6960 xfree (absolute_name);
6961 return NULL;
6962 }
6963 bfd_set_cacheable (sym_bfd, 1);
6964
6965 if (!bfd_check_format (sym_bfd, bfd_object))
6966 {
6967 bfd_close (sym_bfd); /* This also closes desc. */
6968 xfree (absolute_name);
6969 return NULL;
6970 }
6971
6972 /* bfd_usrdata exists for applications and libbfd must not touch it. */
6973 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
6974
6975 return sym_bfd;
6976}
6977
6978/* Try to open DWO file DWO_NAME.
6979 COMP_DIR is the DW_AT_comp_dir attribute.
6980 The result is the bfd handle of the file.
6981 If there is a problem finding or opening the file, return NULL.
6982 Upon success, the canonicalized path of the file is stored in the bfd,
6983 same as symfile_bfd_open. */
6984
6985static bfd *
6986open_dwo_file (const char *dwo_name, const char *comp_dir)
6987{
6988 bfd *abfd;
3019eac3
DE
6989
6990 if (IS_ABSOLUTE_PATH (dwo_name))
6991 return try_open_dwo_file (dwo_name);
6992
6993 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
6994
6995 if (comp_dir != NULL)
6996 {
6997 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
6998
6999 /* NOTE: If comp_dir is a relative path, this will also try the
7000 search path, which seems useful. */
7001 abfd = try_open_dwo_file (path_to_try);
7002 xfree (path_to_try);
7003 if (abfd != NULL)
7004 return abfd;
7005 }
7006
7007 /* That didn't work, try debug-file-directory, which, despite its name,
7008 is a list of paths. */
7009
7010 if (*debug_file_directory == '\0')
7011 return NULL;
7012
7013 return try_open_dwo_file (dwo_name);
7014}
7015
7016/* Initialize the use of the DWO file specified by DWO_NAME. */
7017
7018static struct dwo_file *
7019init_dwo_file (const char *dwo_name, const char *comp_dir)
7020{
7021 struct objfile *objfile = dwarf2_per_objfile->objfile;
7022 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7023 struct dwo_file);
7024 bfd *abfd;
7025 struct cleanup *cleanups;
7026
7027 if (dwarf2_die_debug)
7028 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7029
7030 abfd = open_dwo_file (dwo_name, comp_dir);
7031 if (abfd == NULL)
7032 return NULL;
7033 dwo_file->dwo_name = dwo_name;
7034 dwo_file->dwo_bfd = abfd;
7035
7036 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7037
7038 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7039
7040 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7041
7042 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7043 dwo_file->sections.types);
7044
7045 discard_cleanups (cleanups);
7046
7047 return dwo_file;
7048}
7049
7050/* Lookup DWO file DWO_NAME. */
7051
7052static struct dwo_file *
7053lookup_dwo_file (char *dwo_name, const char *comp_dir)
7054{
7055 struct dwo_file *dwo_file;
7056 struct dwo_file find_entry;
7057 void **slot;
7058
7059 if (dwarf2_per_objfile->dwo_files == NULL)
7060 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7061
7062 /* Have we already seen this DWO file? */
7063 find_entry.dwo_name = dwo_name;
7064 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7065
7066 /* If not, read it in and build a table of the DWOs it contains. */
7067 if (*slot == NULL)
7068 *slot = init_dwo_file (dwo_name, comp_dir);
7069
7070 /* NOTE: This will be NULL if unable to open the file. */
7071 dwo_file = *slot;
7072
7073 return dwo_file;
7074}
7075
7076/* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7077 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7078 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7079 nomenclature as TUs).
7080 The result is the DWO CU or NULL if we didn't find it
7081 (dwo_id mismatch or couldn't find the DWO file). */
7082
7083static struct dwo_unit *
7084lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7085 char *dwo_name, const char *comp_dir,
7086 ULONGEST signature)
7087{
7088 struct objfile *objfile = dwarf2_per_objfile->objfile;
7089 struct dwo_file *dwo_file;
7090
7091 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7092 if (dwo_file == NULL)
7093 return NULL;
7094
7095 /* Look up the DWO using its signature(dwo_id). */
7096
7097 if (dwo_file->cus != NULL)
7098 {
7099 struct dwo_unit find_dwo_cu, *dwo_cu;
7100
7101 find_dwo_cu.signature = signature;
7102 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
a766d390 7103
3019eac3
DE
7104 if (dwo_cu != NULL)
7105 return dwo_cu;
7106 }
c906108c 7107
3019eac3 7108 /* We didn't find it. This must mean a dwo_id mismatch. */
df8a16a1 7109
3019eac3
DE
7110 complaint (&symfile_complaints,
7111 _("Could not find DWO CU referenced by CU at offset 0x%x"
7112 " [in module %s]"),
7113 this_cu->offset.sect_off, objfile->name);
7114 return NULL;
7115}
c906108c 7116
3019eac3
DE
7117/* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7118 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7119 The result is the DWO CU or NULL if we didn't find it
7120 (dwo_id mismatch or couldn't find the DWO file). */
debd256d 7121
3019eac3
DE
7122static struct dwo_unit *
7123lookup_dwo_type_unit (struct signatured_type *this_tu,
7124 char *dwo_name, const char *comp_dir)
7125{
7126 struct objfile *objfile = dwarf2_per_objfile->objfile;
7127 struct dwo_file *dwo_file;
cb1df416 7128
3019eac3
DE
7129 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7130 if (dwo_file == NULL)
7131 return NULL;
cf2c3c16 7132
3019eac3
DE
7133 /* Look up the DWO using its signature(dwo_id). */
7134
7135 if (dwo_file->tus != NULL)
cf2c3c16 7136 {
3019eac3 7137 struct dwo_unit find_dwo_tu, *dwo_tu;
9a619af0 7138
3019eac3
DE
7139 find_dwo_tu.signature = this_tu->signature;
7140 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7141
7142 if (dwo_tu != NULL)
7143 return dwo_tu;
2e276125 7144 }
9cdd5dbd 7145
3019eac3
DE
7146 /* We didn't find it. This must mean a dwo_id mismatch. */
7147
7148 complaint (&symfile_complaints,
7149 _("Could not find DWO TU referenced by TU at offset 0x%x"
7150 " [in module %s]"),
7151 this_tu->per_cu.offset.sect_off, objfile->name);
7152 return NULL;
5fb290d7
DJ
7153}
7154
3019eac3
DE
7155/* Free all resources associated with DWO_FILE.
7156 Close the DWO file and munmap the sections.
7157 All memory should be on the objfile obstack. */
348e048f
DE
7158
7159static void
3019eac3 7160free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 7161{
3019eac3
DE
7162 int ix;
7163 struct dwarf2_section_info *section;
348e048f 7164
3019eac3
DE
7165 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7166 bfd_close (dwo_file->dwo_bfd);
348e048f 7167
3019eac3
DE
7168 munmap_section_buffer (&dwo_file->sections.abbrev);
7169 munmap_section_buffer (&dwo_file->sections.info);
7170 munmap_section_buffer (&dwo_file->sections.line);
7171 munmap_section_buffer (&dwo_file->sections.loc);
7172 munmap_section_buffer (&dwo_file->sections.str);
7173 munmap_section_buffer (&dwo_file->sections.str_offsets);
348e048f 7174
3019eac3
DE
7175 for (ix = 0;
7176 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7177 ix, section);
7178 ++ix)
7179 munmap_section_buffer (section);
348e048f 7180
3019eac3
DE
7181 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7182}
348e048f 7183
3019eac3 7184/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 7185
3019eac3
DE
7186static void
7187free_dwo_file_cleanup (void *arg)
7188{
7189 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7190 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 7191
3019eac3
DE
7192 free_dwo_file (dwo_file, objfile);
7193}
348e048f 7194
3019eac3 7195/* Traversal function for free_dwo_files. */
2ab95328 7196
3019eac3
DE
7197static int
7198free_dwo_file_from_slot (void **slot, void *info)
7199{
7200 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7201 struct objfile *objfile = (struct objfile *) info;
348e048f 7202
3019eac3 7203 free_dwo_file (dwo_file, objfile);
348e048f 7204
3019eac3
DE
7205 return 1;
7206}
348e048f 7207
3019eac3 7208/* Free all resources associated with DWO_FILES. */
348e048f 7209
3019eac3
DE
7210static void
7211free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7212{
7213 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 7214}
3019eac3
DE
7215\f
7216/* Read in various DIEs. */
348e048f 7217
d389af10
JK
7218/* qsort helper for inherit_abstract_dies. */
7219
7220static int
7221unsigned_int_compar (const void *ap, const void *bp)
7222{
7223 unsigned int a = *(unsigned int *) ap;
7224 unsigned int b = *(unsigned int *) bp;
7225
7226 return (a > b) - (b > a);
7227}
7228
7229/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
7230 Inherit only the children of the DW_AT_abstract_origin DIE not being
7231 already referenced by DW_AT_abstract_origin from the children of the
7232 current DIE. */
d389af10
JK
7233
7234static void
7235inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7236{
7237 struct die_info *child_die;
7238 unsigned die_children_count;
7239 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
7240 sect_offset *offsets;
7241 sect_offset *offsets_end, *offsetp;
d389af10
JK
7242 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7243 struct die_info *origin_die;
7244 /* Iterator of the ORIGIN_DIE children. */
7245 struct die_info *origin_child_die;
7246 struct cleanup *cleanups;
7247 struct attribute *attr;
cd02d79d
PA
7248 struct dwarf2_cu *origin_cu;
7249 struct pending **origin_previous_list_in_scope;
d389af10
JK
7250
7251 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7252 if (!attr)
7253 return;
7254
cd02d79d
PA
7255 /* Note that following die references may follow to a die in a
7256 different cu. */
7257
7258 origin_cu = cu;
7259 origin_die = follow_die_ref (die, attr, &origin_cu);
7260
7261 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7262 symbols in. */
7263 origin_previous_list_in_scope = origin_cu->list_in_scope;
7264 origin_cu->list_in_scope = cu->list_in_scope;
7265
edb3359d
DJ
7266 if (die->tag != origin_die->tag
7267 && !(die->tag == DW_TAG_inlined_subroutine
7268 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7269 complaint (&symfile_complaints,
7270 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 7271 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
7272
7273 child_die = die->child;
7274 die_children_count = 0;
7275 while (child_die && child_die->tag)
7276 {
7277 child_die = sibling_die (child_die);
7278 die_children_count++;
7279 }
7280 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7281 cleanups = make_cleanup (xfree, offsets);
7282
7283 offsets_end = offsets;
7284 child_die = die->child;
7285 while (child_die && child_die->tag)
7286 {
c38f313d
DJ
7287 /* For each CHILD_DIE, find the corresponding child of
7288 ORIGIN_DIE. If there is more than one layer of
7289 DW_AT_abstract_origin, follow them all; there shouldn't be,
7290 but GCC versions at least through 4.4 generate this (GCC PR
7291 40573). */
7292 struct die_info *child_origin_die = child_die;
cd02d79d 7293 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 7294
c38f313d
DJ
7295 while (1)
7296 {
cd02d79d
PA
7297 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7298 child_origin_cu);
c38f313d
DJ
7299 if (attr == NULL)
7300 break;
cd02d79d
PA
7301 child_origin_die = follow_die_ref (child_origin_die, attr,
7302 &child_origin_cu);
c38f313d
DJ
7303 }
7304
d389af10
JK
7305 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7306 counterpart may exist. */
c38f313d 7307 if (child_origin_die != child_die)
d389af10 7308 {
edb3359d
DJ
7309 if (child_die->tag != child_origin_die->tag
7310 && !(child_die->tag == DW_TAG_inlined_subroutine
7311 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7312 complaint (&symfile_complaints,
7313 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7314 "different tags"), child_die->offset.sect_off,
7315 child_origin_die->offset.sect_off);
c38f313d
DJ
7316 if (child_origin_die->parent != origin_die)
7317 complaint (&symfile_complaints,
7318 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7319 "different parents"), child_die->offset.sect_off,
7320 child_origin_die->offset.sect_off);
c38f313d
DJ
7321 else
7322 *offsets_end++ = child_origin_die->offset;
d389af10
JK
7323 }
7324 child_die = sibling_die (child_die);
7325 }
7326 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7327 unsigned_int_compar);
7328 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 7329 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
7330 complaint (&symfile_complaints,
7331 _("Multiple children of DIE 0x%x refer "
7332 "to DIE 0x%x as their abstract origin"),
b64f50a1 7333 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
7334
7335 offsetp = offsets;
7336 origin_child_die = origin_die->child;
7337 while (origin_child_die && origin_child_die->tag)
7338 {
7339 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
7340 while (offsetp < offsets_end
7341 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 7342 offsetp++;
b64f50a1
JK
7343 if (offsetp >= offsets_end
7344 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
7345 {
7346 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 7347 process_die (origin_child_die, origin_cu);
d389af10
JK
7348 }
7349 origin_child_die = sibling_die (origin_child_die);
7350 }
cd02d79d 7351 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
7352
7353 do_cleanups (cleanups);
7354}
7355
c906108c 7356static void
e7c27a73 7357read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7358{
e7c27a73 7359 struct objfile *objfile = cu->objfile;
52f0bd74 7360 struct context_stack *new;
c906108c
SS
7361 CORE_ADDR lowpc;
7362 CORE_ADDR highpc;
7363 struct die_info *child_die;
edb3359d 7364 struct attribute *attr, *call_line, *call_file;
c906108c 7365 char *name;
e142c38c 7366 CORE_ADDR baseaddr;
801e3a5b 7367 struct block *block;
edb3359d 7368 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
7369 VEC (symbolp) *template_args = NULL;
7370 struct template_symbol *templ_func = NULL;
edb3359d
DJ
7371
7372 if (inlined_func)
7373 {
7374 /* If we do not have call site information, we can't show the
7375 caller of this inlined function. That's too confusing, so
7376 only use the scope for local variables. */
7377 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7378 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7379 if (call_line == NULL || call_file == NULL)
7380 {
7381 read_lexical_block_scope (die, cu);
7382 return;
7383 }
7384 }
c906108c 7385
e142c38c
DJ
7386 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7387
94af9270 7388 name = dwarf2_name (die, cu);
c906108c 7389
e8d05480
JB
7390 /* Ignore functions with missing or empty names. These are actually
7391 illegal according to the DWARF standard. */
7392 if (name == NULL)
7393 {
7394 complaint (&symfile_complaints,
b64f50a1
JK
7395 _("missing name for subprogram DIE at %d"),
7396 die->offset.sect_off);
e8d05480
JB
7397 return;
7398 }
7399
7400 /* Ignore functions with missing or invalid low and high pc attributes. */
7401 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7402 {
ae4d0c03
PM
7403 attr = dwarf2_attr (die, DW_AT_external, cu);
7404 if (!attr || !DW_UNSND (attr))
7405 complaint (&symfile_complaints,
3e43a32a
MS
7406 _("cannot get low and high bounds "
7407 "for subprogram DIE at %d"),
b64f50a1 7408 die->offset.sect_off);
e8d05480
JB
7409 return;
7410 }
c906108c
SS
7411
7412 lowpc += baseaddr;
7413 highpc += baseaddr;
7414
34eaf542
TT
7415 /* If we have any template arguments, then we must allocate a
7416 different sort of symbol. */
7417 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7418 {
7419 if (child_die->tag == DW_TAG_template_type_param
7420 || child_die->tag == DW_TAG_template_value_param)
7421 {
7422 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7423 struct template_symbol);
7424 templ_func->base.is_cplus_template_function = 1;
7425 break;
7426 }
7427 }
7428
c906108c 7429 new = push_context (0, lowpc);
34eaf542
TT
7430 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7431 (struct symbol *) templ_func);
4c2df51b 7432
4cecd739
DJ
7433 /* If there is a location expression for DW_AT_frame_base, record
7434 it. */
e142c38c 7435 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 7436 if (attr)
c034e007
AC
7437 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7438 expression is being recorded directly in the function's symbol
7439 and not in a separate frame-base object. I guess this hack is
7440 to avoid adding some sort of frame-base adjunct/annex to the
7441 function's symbol :-(. The problem with doing this is that it
7442 results in a function symbol with a location expression that
7443 has nothing to do with the location of the function, ouch! The
7444 relationship should be: a function's symbol has-a frame base; a
7445 frame-base has-a location expression. */
e7c27a73 7446 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 7447
e142c38c 7448 cu->list_in_scope = &local_symbols;
c906108c 7449
639d11d3 7450 if (die->child != NULL)
c906108c 7451 {
639d11d3 7452 child_die = die->child;
c906108c
SS
7453 while (child_die && child_die->tag)
7454 {
34eaf542
TT
7455 if (child_die->tag == DW_TAG_template_type_param
7456 || child_die->tag == DW_TAG_template_value_param)
7457 {
7458 struct symbol *arg = new_symbol (child_die, NULL, cu);
7459
f1078f66
DJ
7460 if (arg != NULL)
7461 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7462 }
7463 else
7464 process_die (child_die, cu);
c906108c
SS
7465 child_die = sibling_die (child_die);
7466 }
7467 }
7468
d389af10
JK
7469 inherit_abstract_dies (die, cu);
7470
4a811a97
UW
7471 /* If we have a DW_AT_specification, we might need to import using
7472 directives from the context of the specification DIE. See the
7473 comment in determine_prefix. */
7474 if (cu->language == language_cplus
7475 && dwarf2_attr (die, DW_AT_specification, cu))
7476 {
7477 struct dwarf2_cu *spec_cu = cu;
7478 struct die_info *spec_die = die_specification (die, &spec_cu);
7479
7480 while (spec_die)
7481 {
7482 child_die = spec_die->child;
7483 while (child_die && child_die->tag)
7484 {
7485 if (child_die->tag == DW_TAG_imported_module)
7486 process_die (child_die, spec_cu);
7487 child_die = sibling_die (child_die);
7488 }
7489
7490 /* In some cases, GCC generates specification DIEs that
7491 themselves contain DW_AT_specification attributes. */
7492 spec_die = die_specification (spec_die, &spec_cu);
7493 }
7494 }
7495
c906108c
SS
7496 new = pop_context ();
7497 /* Make a block for the local symbols within. */
801e3a5b
JB
7498 block = finish_block (new->name, &local_symbols, new->old_blocks,
7499 lowpc, highpc, objfile);
7500
df8a16a1 7501 /* For C++, set the block's scope. */
f55ee35c 7502 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 7503 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 7504 determine_prefix (die, cu),
df8a16a1
DJ
7505 processing_has_namespace_info);
7506
801e3a5b
JB
7507 /* If we have address ranges, record them. */
7508 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 7509
34eaf542
TT
7510 /* Attach template arguments to function. */
7511 if (! VEC_empty (symbolp, template_args))
7512 {
7513 gdb_assert (templ_func != NULL);
7514
7515 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7516 templ_func->template_arguments
7517 = obstack_alloc (&objfile->objfile_obstack,
7518 (templ_func->n_template_arguments
7519 * sizeof (struct symbol *)));
7520 memcpy (templ_func->template_arguments,
7521 VEC_address (symbolp, template_args),
7522 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7523 VEC_free (symbolp, template_args);
7524 }
7525
208d8187
JB
7526 /* In C++, we can have functions nested inside functions (e.g., when
7527 a function declares a class that has methods). This means that
7528 when we finish processing a function scope, we may need to go
7529 back to building a containing block's symbol lists. */
7530 local_symbols = new->locals;
7531 param_symbols = new->params;
27aa8d6a 7532 using_directives = new->using_directives;
208d8187 7533
921e78cf
JB
7534 /* If we've finished processing a top-level function, subsequent
7535 symbols go in the file symbol list. */
7536 if (outermost_context_p ())
e142c38c 7537 cu->list_in_scope = &file_symbols;
c906108c
SS
7538}
7539
7540/* Process all the DIES contained within a lexical block scope. Start
7541 a new scope, process the dies, and then close the scope. */
7542
7543static void
e7c27a73 7544read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7545{
e7c27a73 7546 struct objfile *objfile = cu->objfile;
52f0bd74 7547 struct context_stack *new;
c906108c
SS
7548 CORE_ADDR lowpc, highpc;
7549 struct die_info *child_die;
e142c38c
DJ
7550 CORE_ADDR baseaddr;
7551
7552 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
7553
7554 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
7555 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7556 as multiple lexical blocks? Handling children in a sane way would
6e70227d 7557 be nasty. Might be easier to properly extend generic blocks to
af34e669 7558 describe ranges. */
d85a05f0 7559 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
7560 return;
7561 lowpc += baseaddr;
7562 highpc += baseaddr;
7563
7564 push_context (0, lowpc);
639d11d3 7565 if (die->child != NULL)
c906108c 7566 {
639d11d3 7567 child_die = die->child;
c906108c
SS
7568 while (child_die && child_die->tag)
7569 {
e7c27a73 7570 process_die (child_die, cu);
c906108c
SS
7571 child_die = sibling_die (child_die);
7572 }
7573 }
7574 new = pop_context ();
7575
8540c487 7576 if (local_symbols != NULL || using_directives != NULL)
c906108c 7577 {
801e3a5b
JB
7578 struct block *block
7579 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7580 highpc, objfile);
7581
7582 /* Note that recording ranges after traversing children, as we
7583 do here, means that recording a parent's ranges entails
7584 walking across all its children's ranges as they appear in
7585 the address map, which is quadratic behavior.
7586
7587 It would be nicer to record the parent's ranges before
7588 traversing its children, simply overriding whatever you find
7589 there. But since we don't even decide whether to create a
7590 block until after we've traversed its children, that's hard
7591 to do. */
7592 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
7593 }
7594 local_symbols = new->locals;
27aa8d6a 7595 using_directives = new->using_directives;
c906108c
SS
7596}
7597
96408a79
SA
7598/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7599
7600static void
7601read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7602{
7603 struct objfile *objfile = cu->objfile;
7604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7605 CORE_ADDR pc, baseaddr;
7606 struct attribute *attr;
7607 struct call_site *call_site, call_site_local;
7608 void **slot;
7609 int nparams;
7610 struct die_info *child_die;
7611
7612 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7613
7614 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7615 if (!attr)
7616 {
7617 complaint (&symfile_complaints,
7618 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7619 "DIE 0x%x [in module %s]"),
b64f50a1 7620 die->offset.sect_off, objfile->name);
96408a79
SA
7621 return;
7622 }
7623 pc = DW_ADDR (attr) + baseaddr;
7624
7625 if (cu->call_site_htab == NULL)
7626 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7627 NULL, &objfile->objfile_obstack,
7628 hashtab_obstack_allocate, NULL);
7629 call_site_local.pc = pc;
7630 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7631 if (*slot != NULL)
7632 {
7633 complaint (&symfile_complaints,
7634 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7635 "DIE 0x%x [in module %s]"),
b64f50a1 7636 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
7637 return;
7638 }
7639
7640 /* Count parameters at the caller. */
7641
7642 nparams = 0;
7643 for (child_die = die->child; child_die && child_die->tag;
7644 child_die = sibling_die (child_die))
7645 {
7646 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7647 {
7648 complaint (&symfile_complaints,
7649 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7650 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7651 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
7652 continue;
7653 }
7654
7655 nparams++;
7656 }
7657
7658 call_site = obstack_alloc (&objfile->objfile_obstack,
7659 (sizeof (*call_site)
7660 + (sizeof (*call_site->parameter)
7661 * (nparams - 1))));
7662 *slot = call_site;
7663 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7664 call_site->pc = pc;
7665
7666 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7667 {
7668 struct die_info *func_die;
7669
7670 /* Skip also over DW_TAG_inlined_subroutine. */
7671 for (func_die = die->parent;
7672 func_die && func_die->tag != DW_TAG_subprogram
7673 && func_die->tag != DW_TAG_subroutine_type;
7674 func_die = func_die->parent);
7675
7676 /* DW_AT_GNU_all_call_sites is a superset
7677 of DW_AT_GNU_all_tail_call_sites. */
7678 if (func_die
7679 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7680 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7681 {
7682 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7683 not complete. But keep CALL_SITE for look ups via call_site_htab,
7684 both the initial caller containing the real return address PC and
7685 the final callee containing the current PC of a chain of tail
7686 calls do not need to have the tail call list complete. But any
7687 function candidate for a virtual tail call frame searched via
7688 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7689 determined unambiguously. */
7690 }
7691 else
7692 {
7693 struct type *func_type = NULL;
7694
7695 if (func_die)
7696 func_type = get_die_type (func_die, cu);
7697 if (func_type != NULL)
7698 {
7699 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7700
7701 /* Enlist this call site to the function. */
7702 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7703 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7704 }
7705 else
7706 complaint (&symfile_complaints,
7707 _("Cannot find function owning DW_TAG_GNU_call_site "
7708 "DIE 0x%x [in module %s]"),
b64f50a1 7709 die->offset.sect_off, objfile->name);
96408a79
SA
7710 }
7711 }
7712
7713 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7714 if (attr == NULL)
7715 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7716 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7717 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7718 /* Keep NULL DWARF_BLOCK. */;
7719 else if (attr_form_is_block (attr))
7720 {
7721 struct dwarf2_locexpr_baton *dlbaton;
7722
7723 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7724 dlbaton->data = DW_BLOCK (attr)->data;
7725 dlbaton->size = DW_BLOCK (attr)->size;
7726 dlbaton->per_cu = cu->per_cu;
7727
7728 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7729 }
7730 else if (is_ref_attr (attr))
7731 {
96408a79
SA
7732 struct dwarf2_cu *target_cu = cu;
7733 struct die_info *target_die;
7734
7735 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7736 gdb_assert (target_cu->objfile == objfile);
7737 if (die_is_declaration (target_die, target_cu))
7738 {
7739 const char *target_physname;
7740
7741 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7742 if (target_physname == NULL)
7743 complaint (&symfile_complaints,
7744 _("DW_AT_GNU_call_site_target target DIE has invalid "
7745 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 7746 die->offset.sect_off, objfile->name);
96408a79
SA
7747 else
7748 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7749 }
7750 else
7751 {
7752 CORE_ADDR lowpc;
7753
7754 /* DW_AT_entry_pc should be preferred. */
7755 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7756 complaint (&symfile_complaints,
7757 _("DW_AT_GNU_call_site_target target DIE has invalid "
7758 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 7759 die->offset.sect_off, objfile->name);
96408a79
SA
7760 else
7761 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7762 }
7763 }
7764 else
7765 complaint (&symfile_complaints,
7766 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7767 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 7768 die->offset.sect_off, objfile->name);
96408a79
SA
7769
7770 call_site->per_cu = cu->per_cu;
7771
7772 for (child_die = die->child;
7773 child_die && child_die->tag;
7774 child_die = sibling_die (child_die))
7775 {
96408a79
SA
7776 struct call_site_parameter *parameter;
7777
7778 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7779 {
7780 /* Already printed the complaint above. */
7781 continue;
7782 }
7783
7784 gdb_assert (call_site->parameter_count < nparams);
7785 parameter = &call_site->parameter[call_site->parameter_count];
7786
7787 /* DW_AT_location specifies the register number. Value of the data
7788 assumed for the register is contained in DW_AT_GNU_call_site_value. */
7789
7790 attr = dwarf2_attr (child_die, DW_AT_location, cu);
7791 if (!attr || !attr_form_is_block (attr))
7792 {
7793 complaint (&symfile_complaints,
7794 _("No DW_FORM_block* DW_AT_location for "
7795 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7796 child_die->offset.sect_off, objfile->name);
96408a79
SA
7797 continue;
7798 }
7799 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
7800 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
7801 if (parameter->dwarf_reg == -1
7802 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
7803 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
7804 &parameter->fb_offset))
7805 {
7806 complaint (&symfile_complaints,
7807 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
7808 "for DW_FORM_block* DW_AT_location for "
7809 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7810 child_die->offset.sect_off, objfile->name);
96408a79
SA
7811 continue;
7812 }
7813
7814 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
7815 if (!attr_form_is_block (attr))
7816 {
7817 complaint (&symfile_complaints,
7818 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
7819 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7820 child_die->offset.sect_off, objfile->name);
96408a79
SA
7821 continue;
7822 }
7823 parameter->value = DW_BLOCK (attr)->data;
7824 parameter->value_size = DW_BLOCK (attr)->size;
7825
7826 /* Parameters are not pre-cleared by memset above. */
7827 parameter->data_value = NULL;
7828 parameter->data_value_size = 0;
7829 call_site->parameter_count++;
7830
7831 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
7832 if (attr)
7833 {
7834 if (!attr_form_is_block (attr))
7835 complaint (&symfile_complaints,
7836 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
7837 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7838 child_die->offset.sect_off, objfile->name);
96408a79
SA
7839 else
7840 {
7841 parameter->data_value = DW_BLOCK (attr)->data;
7842 parameter->data_value_size = DW_BLOCK (attr)->size;
7843 }
7844 }
7845 }
7846}
7847
43039443 7848/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
7849 Return 1 if the attributes are present and valid, otherwise, return 0.
7850 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
7851
7852static int
7853dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
7854 CORE_ADDR *high_return, struct dwarf2_cu *cu,
7855 struct partial_symtab *ranges_pst)
43039443
JK
7856{
7857 struct objfile *objfile = cu->objfile;
7858 struct comp_unit_head *cu_header = &cu->header;
7859 bfd *obfd = objfile->obfd;
7860 unsigned int addr_size = cu_header->addr_size;
7861 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7862 /* Base address selection entry. */
7863 CORE_ADDR base;
7864 int found_base;
7865 unsigned int dummy;
7866 gdb_byte *buffer;
7867 CORE_ADDR marker;
7868 int low_set;
7869 CORE_ADDR low = 0;
7870 CORE_ADDR high = 0;
ff013f42 7871 CORE_ADDR baseaddr;
43039443 7872
d00adf39
DE
7873 found_base = cu->base_known;
7874 base = cu->base_address;
43039443 7875
be391dca 7876 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 7877 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
7878 {
7879 complaint (&symfile_complaints,
7880 _("Offset %d out of bounds for DW_AT_ranges attribute"),
7881 offset);
7882 return 0;
7883 }
dce234bc 7884 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
7885
7886 /* Read in the largest possible address. */
7887 marker = read_address (obfd, buffer, cu, &dummy);
7888 if ((marker & mask) == mask)
7889 {
7890 /* If we found the largest possible address, then
7891 read the base address. */
7892 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7893 buffer += 2 * addr_size;
7894 offset += 2 * addr_size;
7895 found_base = 1;
7896 }
7897
7898 low_set = 0;
7899
e7030f15 7900 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 7901
43039443
JK
7902 while (1)
7903 {
7904 CORE_ADDR range_beginning, range_end;
7905
7906 range_beginning = read_address (obfd, buffer, cu, &dummy);
7907 buffer += addr_size;
7908 range_end = read_address (obfd, buffer, cu, &dummy);
7909 buffer += addr_size;
7910 offset += 2 * addr_size;
7911
7912 /* An end of list marker is a pair of zero addresses. */
7913 if (range_beginning == 0 && range_end == 0)
7914 /* Found the end of list entry. */
7915 break;
7916
7917 /* Each base address selection entry is a pair of 2 values.
7918 The first is the largest possible address, the second is
7919 the base address. Check for a base address here. */
7920 if ((range_beginning & mask) == mask)
7921 {
7922 /* If we found the largest possible address, then
7923 read the base address. */
7924 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7925 found_base = 1;
7926 continue;
7927 }
7928
7929 if (!found_base)
7930 {
7931 /* We have no valid base address for the ranges
7932 data. */
7933 complaint (&symfile_complaints,
7934 _("Invalid .debug_ranges data (no base address)"));
7935 return 0;
7936 }
7937
9277c30c
UW
7938 if (range_beginning > range_end)
7939 {
7940 /* Inverted range entries are invalid. */
7941 complaint (&symfile_complaints,
7942 _("Invalid .debug_ranges data (inverted range)"));
7943 return 0;
7944 }
7945
7946 /* Empty range entries have no effect. */
7947 if (range_beginning == range_end)
7948 continue;
7949
43039443
JK
7950 range_beginning += base;
7951 range_end += base;
7952
9277c30c 7953 if (ranges_pst != NULL)
ff013f42 7954 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
7955 range_beginning + baseaddr,
7956 range_end - 1 + baseaddr,
ff013f42
JK
7957 ranges_pst);
7958
43039443
JK
7959 /* FIXME: This is recording everything as a low-high
7960 segment of consecutive addresses. We should have a
7961 data structure for discontiguous block ranges
7962 instead. */
7963 if (! low_set)
7964 {
7965 low = range_beginning;
7966 high = range_end;
7967 low_set = 1;
7968 }
7969 else
7970 {
7971 if (range_beginning < low)
7972 low = range_beginning;
7973 if (range_end > high)
7974 high = range_end;
7975 }
7976 }
7977
7978 if (! low_set)
7979 /* If the first entry is an end-of-list marker, the range
7980 describes an empty scope, i.e. no instructions. */
7981 return 0;
7982
7983 if (low_return)
7984 *low_return = low;
7985 if (high_return)
7986 *high_return = high;
7987 return 1;
7988}
7989
af34e669
DJ
7990/* Get low and high pc attributes from a die. Return 1 if the attributes
7991 are present and valid, otherwise, return 0. Return -1 if the range is
7992 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 7993
c906108c 7994static int
af34e669 7995dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
7996 CORE_ADDR *highpc, struct dwarf2_cu *cu,
7997 struct partial_symtab *pst)
c906108c
SS
7998{
7999 struct attribute *attr;
91da1414 8000 struct attribute *attr_high;
af34e669
DJ
8001 CORE_ADDR low = 0;
8002 CORE_ADDR high = 0;
8003 int ret = 0;
c906108c 8004
91da1414
MW
8005 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8006 if (attr_high)
af34e669 8007 {
e142c38c 8008 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 8009 if (attr)
91da1414
MW
8010 {
8011 low = DW_ADDR (attr);
3019eac3
DE
8012 if (attr_high->form == DW_FORM_addr
8013 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8014 high = DW_ADDR (attr_high);
8015 else
8016 high = low + DW_UNSND (attr_high);
8017 }
af34e669
DJ
8018 else
8019 /* Found high w/o low attribute. */
8020 return 0;
8021
8022 /* Found consecutive range of addresses. */
8023 ret = 1;
8024 }
c906108c 8025 else
af34e669 8026 {
e142c38c 8027 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
8028 if (attr != NULL)
8029 {
af34e669 8030 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 8031 .debug_ranges section. */
d85a05f0 8032 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 8033 return 0;
43039443 8034 /* Found discontinuous range of addresses. */
af34e669
DJ
8035 ret = -1;
8036 }
8037 }
c906108c 8038
9373cf26
JK
8039 /* read_partial_die has also the strict LOW < HIGH requirement. */
8040 if (high <= low)
c906108c
SS
8041 return 0;
8042
8043 /* When using the GNU linker, .gnu.linkonce. sections are used to
8044 eliminate duplicate copies of functions and vtables and such.
8045 The linker will arbitrarily choose one and discard the others.
8046 The AT_*_pc values for such functions refer to local labels in
8047 these sections. If the section from that file was discarded, the
8048 labels are not in the output, so the relocs get a value of 0.
8049 If this is a discarded function, mark the pc bounds as invalid,
8050 so that GDB will ignore it. */
72dca2f5 8051 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
8052 return 0;
8053
8054 *lowpc = low;
96408a79
SA
8055 if (highpc)
8056 *highpc = high;
af34e669 8057 return ret;
c906108c
SS
8058}
8059
b084d499
JB
8060/* Assuming that DIE represents a subprogram DIE or a lexical block, get
8061 its low and high PC addresses. Do nothing if these addresses could not
8062 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8063 and HIGHPC to the high address if greater than HIGHPC. */
8064
8065static void
8066dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8067 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8068 struct dwarf2_cu *cu)
8069{
8070 CORE_ADDR low, high;
8071 struct die_info *child = die->child;
8072
d85a05f0 8073 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
8074 {
8075 *lowpc = min (*lowpc, low);
8076 *highpc = max (*highpc, high);
8077 }
8078
8079 /* If the language does not allow nested subprograms (either inside
8080 subprograms or lexical blocks), we're done. */
8081 if (cu->language != language_ada)
8082 return;
6e70227d 8083
b084d499
JB
8084 /* Check all the children of the given DIE. If it contains nested
8085 subprograms, then check their pc bounds. Likewise, we need to
8086 check lexical blocks as well, as they may also contain subprogram
8087 definitions. */
8088 while (child && child->tag)
8089 {
8090 if (child->tag == DW_TAG_subprogram
8091 || child->tag == DW_TAG_lexical_block)
8092 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8093 child = sibling_die (child);
8094 }
8095}
8096
fae299cd
DC
8097/* Get the low and high pc's represented by the scope DIE, and store
8098 them in *LOWPC and *HIGHPC. If the correct values can't be
8099 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8100
8101static void
8102get_scope_pc_bounds (struct die_info *die,
8103 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8104 struct dwarf2_cu *cu)
8105{
8106 CORE_ADDR best_low = (CORE_ADDR) -1;
8107 CORE_ADDR best_high = (CORE_ADDR) 0;
8108 CORE_ADDR current_low, current_high;
8109
d85a05f0 8110 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
8111 {
8112 best_low = current_low;
8113 best_high = current_high;
8114 }
8115 else
8116 {
8117 struct die_info *child = die->child;
8118
8119 while (child && child->tag)
8120 {
8121 switch (child->tag) {
8122 case DW_TAG_subprogram:
b084d499 8123 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
8124 break;
8125 case DW_TAG_namespace:
f55ee35c 8126 case DW_TAG_module:
fae299cd
DC
8127 /* FIXME: carlton/2004-01-16: Should we do this for
8128 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8129 that current GCC's always emit the DIEs corresponding
8130 to definitions of methods of classes as children of a
8131 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8132 the DIEs giving the declarations, which could be
8133 anywhere). But I don't see any reason why the
8134 standards says that they have to be there. */
8135 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8136
8137 if (current_low != ((CORE_ADDR) -1))
8138 {
8139 best_low = min (best_low, current_low);
8140 best_high = max (best_high, current_high);
8141 }
8142 break;
8143 default:
0963b4bd 8144 /* Ignore. */
fae299cd
DC
8145 break;
8146 }
8147
8148 child = sibling_die (child);
8149 }
8150 }
8151
8152 *lowpc = best_low;
8153 *highpc = best_high;
8154}
8155
801e3a5b
JB
8156/* Record the address ranges for BLOCK, offset by BASEADDR, as given
8157 in DIE. */
380bca97 8158
801e3a5b
JB
8159static void
8160dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8161 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8162{
bb5ed363 8163 struct objfile *objfile = cu->objfile;
801e3a5b 8164 struct attribute *attr;
91da1414 8165 struct attribute *attr_high;
801e3a5b 8166
91da1414
MW
8167 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8168 if (attr_high)
801e3a5b 8169 {
801e3a5b
JB
8170 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8171 if (attr)
8172 {
8173 CORE_ADDR low = DW_ADDR (attr);
91da1414 8174 CORE_ADDR high;
3019eac3
DE
8175 if (attr_high->form == DW_FORM_addr
8176 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8177 high = DW_ADDR (attr_high);
8178 else
8179 high = low + DW_UNSND (attr_high);
9a619af0 8180
801e3a5b
JB
8181 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8182 }
8183 }
8184
8185 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8186 if (attr)
8187 {
bb5ed363 8188 bfd *obfd = objfile->obfd;
801e3a5b
JB
8189
8190 /* The value of the DW_AT_ranges attribute is the offset of the
8191 address range list in the .debug_ranges section. */
8192 unsigned long offset = DW_UNSND (attr);
dce234bc 8193 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
8194
8195 /* For some target architectures, but not others, the
8196 read_address function sign-extends the addresses it returns.
8197 To recognize base address selection entries, we need a
8198 mask. */
8199 unsigned int addr_size = cu->header.addr_size;
8200 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8201
8202 /* The base address, to which the next pair is relative. Note
8203 that this 'base' is a DWARF concept: most entries in a range
8204 list are relative, to reduce the number of relocs against the
8205 debugging information. This is separate from this function's
8206 'baseaddr' argument, which GDB uses to relocate debugging
8207 information from a shared library based on the address at
8208 which the library was loaded. */
d00adf39
DE
8209 CORE_ADDR base = cu->base_address;
8210 int base_known = cu->base_known;
801e3a5b 8211
be391dca 8212 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 8213 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
8214 {
8215 complaint (&symfile_complaints,
8216 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8217 offset);
8218 return;
8219 }
8220
8221 for (;;)
8222 {
8223 unsigned int bytes_read;
8224 CORE_ADDR start, end;
8225
8226 start = read_address (obfd, buffer, cu, &bytes_read);
8227 buffer += bytes_read;
8228 end = read_address (obfd, buffer, cu, &bytes_read);
8229 buffer += bytes_read;
8230
8231 /* Did we find the end of the range list? */
8232 if (start == 0 && end == 0)
8233 break;
8234
8235 /* Did we find a base address selection entry? */
8236 else if ((start & base_select_mask) == base_select_mask)
8237 {
8238 base = end;
8239 base_known = 1;
8240 }
8241
8242 /* We found an ordinary address range. */
8243 else
8244 {
8245 if (!base_known)
8246 {
8247 complaint (&symfile_complaints,
3e43a32a
MS
8248 _("Invalid .debug_ranges data "
8249 "(no base address)"));
801e3a5b
JB
8250 return;
8251 }
8252
9277c30c
UW
8253 if (start > end)
8254 {
8255 /* Inverted range entries are invalid. */
8256 complaint (&symfile_complaints,
8257 _("Invalid .debug_ranges data "
8258 "(inverted range)"));
8259 return;
8260 }
8261
8262 /* Empty range entries have no effect. */
8263 if (start == end)
8264 continue;
8265
6e70227d
DE
8266 record_block_range (block,
8267 baseaddr + base + start,
801e3a5b
JB
8268 baseaddr + base + end - 1);
8269 }
8270 }
8271 }
8272}
8273
685b1105
JK
8274/* Check whether the producer field indicates either of GCC < 4.6, or the
8275 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 8276
685b1105
JK
8277static void
8278check_producer (struct dwarf2_cu *cu)
60d5a603
JK
8279{
8280 const char *cs;
8281 int major, minor, release;
8282
8283 if (cu->producer == NULL)
8284 {
8285 /* For unknown compilers expect their behavior is DWARF version
8286 compliant.
8287
8288 GCC started to support .debug_types sections by -gdwarf-4 since
8289 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8290 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8291 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8292 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 8293 }
685b1105 8294 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 8295 {
685b1105
JK
8296 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8297
ba919b58
TT
8298 cs = &cu->producer[strlen ("GNU ")];
8299 while (*cs && !isdigit (*cs))
8300 cs++;
8301 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8302 {
8303 /* Not recognized as GCC. */
8304 }
8305 else
685b1105
JK
8306 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
8307 }
8308 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
8309 cu->producer_is_icc = 1;
8310 else
8311 {
8312 /* For other non-GCC compilers, expect their behavior is DWARF version
8313 compliant. */
60d5a603
JK
8314 }
8315
ba919b58 8316 cu->checked_producer = 1;
685b1105 8317}
ba919b58 8318
685b1105
JK
8319/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8320 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8321 during 4.6.0 experimental. */
8322
8323static int
8324producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8325{
8326 if (!cu->checked_producer)
8327 check_producer (cu);
8328
8329 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
8330}
8331
8332/* Return the default accessibility type if it is not overriden by
8333 DW_AT_accessibility. */
8334
8335static enum dwarf_access_attribute
8336dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8337{
8338 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8339 {
8340 /* The default DWARF 2 accessibility for members is public, the default
8341 accessibility for inheritance is private. */
8342
8343 if (die->tag != DW_TAG_inheritance)
8344 return DW_ACCESS_public;
8345 else
8346 return DW_ACCESS_private;
8347 }
8348 else
8349 {
8350 /* DWARF 3+ defines the default accessibility a different way. The same
8351 rules apply now for DW_TAG_inheritance as for the members and it only
8352 depends on the container kind. */
8353
8354 if (die->parent->tag == DW_TAG_class_type)
8355 return DW_ACCESS_private;
8356 else
8357 return DW_ACCESS_public;
8358 }
8359}
8360
74ac6d43
TT
8361/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8362 offset. If the attribute was not found return 0, otherwise return
8363 1. If it was found but could not properly be handled, set *OFFSET
8364 to 0. */
8365
8366static int
8367handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8368 LONGEST *offset)
8369{
8370 struct attribute *attr;
8371
8372 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8373 if (attr != NULL)
8374 {
8375 *offset = 0;
8376
8377 /* Note that we do not check for a section offset first here.
8378 This is because DW_AT_data_member_location is new in DWARF 4,
8379 so if we see it, we can assume that a constant form is really
8380 a constant and not a section offset. */
8381 if (attr_form_is_constant (attr))
8382 *offset = dwarf2_get_attr_constant_value (attr, 0);
8383 else if (attr_form_is_section_offset (attr))
8384 dwarf2_complex_location_expr_complaint ();
8385 else if (attr_form_is_block (attr))
8386 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8387 else
8388 dwarf2_complex_location_expr_complaint ();
8389
8390 return 1;
8391 }
8392
8393 return 0;
8394}
8395
c906108c
SS
8396/* Add an aggregate field to the field list. */
8397
8398static void
107d2387 8399dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 8400 struct dwarf2_cu *cu)
6e70227d 8401{
e7c27a73 8402 struct objfile *objfile = cu->objfile;
5e2b427d 8403 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8404 struct nextfield *new_field;
8405 struct attribute *attr;
8406 struct field *fp;
8407 char *fieldname = "";
8408
8409 /* Allocate a new field list entry and link it in. */
8410 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 8411 make_cleanup (xfree, new_field);
c906108c 8412 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
8413
8414 if (die->tag == DW_TAG_inheritance)
8415 {
8416 new_field->next = fip->baseclasses;
8417 fip->baseclasses = new_field;
8418 }
8419 else
8420 {
8421 new_field->next = fip->fields;
8422 fip->fields = new_field;
8423 }
c906108c
SS
8424 fip->nfields++;
8425
e142c38c 8426 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
8427 if (attr)
8428 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
8429 else
8430 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
8431 if (new_field->accessibility != DW_ACCESS_public)
8432 fip->non_public_fields = 1;
60d5a603 8433
e142c38c 8434 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
8435 if (attr)
8436 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
8437 else
8438 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
8439
8440 fp = &new_field->field;
a9a9bd0f 8441
e142c38c 8442 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 8443 {
74ac6d43
TT
8444 LONGEST offset;
8445
a9a9bd0f 8446 /* Data member other than a C++ static data member. */
6e70227d 8447
c906108c 8448 /* Get type of field. */
e7c27a73 8449 fp->type = die_type (die, cu);
c906108c 8450
d6a843b5 8451 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 8452
c906108c 8453 /* Get bit size of field (zero if none). */
e142c38c 8454 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
8455 if (attr)
8456 {
8457 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8458 }
8459 else
8460 {
8461 FIELD_BITSIZE (*fp) = 0;
8462 }
8463
8464 /* Get bit offset of field. */
74ac6d43
TT
8465 if (handle_data_member_location (die, cu, &offset))
8466 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 8467 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
8468 if (attr)
8469 {
5e2b427d 8470 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
8471 {
8472 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
8473 additional bit offset from the MSB of the containing
8474 anonymous object to the MSB of the field. We don't
8475 have to do anything special since we don't need to
8476 know the size of the anonymous object. */
f41f5e61 8477 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
8478 }
8479 else
8480 {
8481 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
8482 MSB of the anonymous object, subtract off the number of
8483 bits from the MSB of the field to the MSB of the
8484 object, and then subtract off the number of bits of
8485 the field itself. The result is the bit offset of
8486 the LSB of the field. */
c906108c
SS
8487 int anonymous_size;
8488 int bit_offset = DW_UNSND (attr);
8489
e142c38c 8490 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8491 if (attr)
8492 {
8493 /* The size of the anonymous object containing
8494 the bit field is explicit, so use the
8495 indicated size (in bytes). */
8496 anonymous_size = DW_UNSND (attr);
8497 }
8498 else
8499 {
8500 /* The size of the anonymous object containing
8501 the bit field must be inferred from the type
8502 attribute of the data member containing the
8503 bit field. */
8504 anonymous_size = TYPE_LENGTH (fp->type);
8505 }
f41f5e61
PA
8506 SET_FIELD_BITPOS (*fp,
8507 (FIELD_BITPOS (*fp)
8508 + anonymous_size * bits_per_byte
8509 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
8510 }
8511 }
8512
8513 /* Get name of field. */
39cbfefa
DJ
8514 fieldname = dwarf2_name (die, cu);
8515 if (fieldname == NULL)
8516 fieldname = "";
d8151005
DJ
8517
8518 /* The name is already allocated along with this objfile, so we don't
8519 need to duplicate it for the type. */
8520 fp->name = fieldname;
c906108c
SS
8521
8522 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 8523 pointer or virtual base class pointer) to private. */
e142c38c 8524 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 8525 {
d48cc9dd 8526 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
8527 new_field->accessibility = DW_ACCESS_private;
8528 fip->non_public_fields = 1;
8529 }
8530 }
a9a9bd0f 8531 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 8532 {
a9a9bd0f
DC
8533 /* C++ static member. */
8534
8535 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8536 is a declaration, but all versions of G++ as of this writing
8537 (so through at least 3.2.1) incorrectly generate
8538 DW_TAG_variable tags. */
6e70227d 8539
ff355380 8540 const char *physname;
c906108c 8541
a9a9bd0f 8542 /* Get name of field. */
39cbfefa
DJ
8543 fieldname = dwarf2_name (die, cu);
8544 if (fieldname == NULL)
c906108c
SS
8545 return;
8546
254e6b9e 8547 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
8548 if (attr
8549 /* Only create a symbol if this is an external value.
8550 new_symbol checks this and puts the value in the global symbol
8551 table, which we want. If it is not external, new_symbol
8552 will try to put the value in cu->list_in_scope which is wrong. */
8553 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
8554 {
8555 /* A static const member, not much different than an enum as far as
8556 we're concerned, except that we can support more types. */
8557 new_symbol (die, NULL, cu);
8558 }
8559
2df3850c 8560 /* Get physical name. */
ff355380 8561 physname = dwarf2_physname (fieldname, die, cu);
c906108c 8562
d8151005
DJ
8563 /* The name is already allocated along with this objfile, so we don't
8564 need to duplicate it for the type. */
8565 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 8566 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 8567 FIELD_NAME (*fp) = fieldname;
c906108c
SS
8568 }
8569 else if (die->tag == DW_TAG_inheritance)
8570 {
74ac6d43 8571 LONGEST offset;
d4b96c9a 8572
74ac6d43
TT
8573 /* C++ base class field. */
8574 if (handle_data_member_location (die, cu, &offset))
8575 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 8576 FIELD_BITSIZE (*fp) = 0;
e7c27a73 8577 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
8578 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8579 fip->nbaseclasses++;
8580 }
8581}
8582
98751a41
JK
8583/* Add a typedef defined in the scope of the FIP's class. */
8584
8585static void
8586dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8587 struct dwarf2_cu *cu)
6e70227d 8588{
98751a41 8589 struct objfile *objfile = cu->objfile;
98751a41
JK
8590 struct typedef_field_list *new_field;
8591 struct attribute *attr;
8592 struct typedef_field *fp;
8593 char *fieldname = "";
8594
8595 /* Allocate a new field list entry and link it in. */
8596 new_field = xzalloc (sizeof (*new_field));
8597 make_cleanup (xfree, new_field);
8598
8599 gdb_assert (die->tag == DW_TAG_typedef);
8600
8601 fp = &new_field->field;
8602
8603 /* Get name of field. */
8604 fp->name = dwarf2_name (die, cu);
8605 if (fp->name == NULL)
8606 return;
8607
8608 fp->type = read_type_die (die, cu);
8609
8610 new_field->next = fip->typedef_field_list;
8611 fip->typedef_field_list = new_field;
8612 fip->typedef_field_list_count++;
8613}
8614
c906108c
SS
8615/* Create the vector of fields, and attach it to the type. */
8616
8617static void
fba45db2 8618dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8619 struct dwarf2_cu *cu)
c906108c
SS
8620{
8621 int nfields = fip->nfields;
8622
8623 /* Record the field count, allocate space for the array of fields,
8624 and create blank accessibility bitfields if necessary. */
8625 TYPE_NFIELDS (type) = nfields;
8626 TYPE_FIELDS (type) = (struct field *)
8627 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8628 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8629
b4ba55a1 8630 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
8631 {
8632 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8633
8634 TYPE_FIELD_PRIVATE_BITS (type) =
8635 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8636 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8637
8638 TYPE_FIELD_PROTECTED_BITS (type) =
8639 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8640 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8641
774b6a14
TT
8642 TYPE_FIELD_IGNORE_BITS (type) =
8643 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8644 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
8645 }
8646
8647 /* If the type has baseclasses, allocate and clear a bit vector for
8648 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 8649 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
8650 {
8651 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 8652 unsigned char *pointer;
c906108c
SS
8653
8654 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
8655 pointer = TYPE_ALLOC (type, num_bytes);
8656 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
8657 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8658 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8659 }
8660
3e43a32a
MS
8661 /* Copy the saved-up fields into the field vector. Start from the head of
8662 the list, adding to the tail of the field array, so that they end up in
8663 the same order in the array in which they were added to the list. */
c906108c
SS
8664 while (nfields-- > 0)
8665 {
7d0ccb61
DJ
8666 struct nextfield *fieldp;
8667
8668 if (fip->fields)
8669 {
8670 fieldp = fip->fields;
8671 fip->fields = fieldp->next;
8672 }
8673 else
8674 {
8675 fieldp = fip->baseclasses;
8676 fip->baseclasses = fieldp->next;
8677 }
8678
8679 TYPE_FIELD (type, nfields) = fieldp->field;
8680 switch (fieldp->accessibility)
c906108c 8681 {
c5aa993b 8682 case DW_ACCESS_private:
b4ba55a1
JB
8683 if (cu->language != language_ada)
8684 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 8685 break;
c906108c 8686
c5aa993b 8687 case DW_ACCESS_protected:
b4ba55a1
JB
8688 if (cu->language != language_ada)
8689 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 8690 break;
c906108c 8691
c5aa993b
JM
8692 case DW_ACCESS_public:
8693 break;
c906108c 8694
c5aa993b
JM
8695 default:
8696 /* Unknown accessibility. Complain and treat it as public. */
8697 {
e2e0b3e5 8698 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 8699 fieldp->accessibility);
c5aa993b
JM
8700 }
8701 break;
c906108c
SS
8702 }
8703 if (nfields < fip->nbaseclasses)
8704 {
7d0ccb61 8705 switch (fieldp->virtuality)
c906108c 8706 {
c5aa993b
JM
8707 case DW_VIRTUALITY_virtual:
8708 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 8709 if (cu->language == language_ada)
a73c6dcd 8710 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
8711 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8712 break;
c906108c
SS
8713 }
8714 }
c906108c
SS
8715 }
8716}
8717
c906108c
SS
8718/* Add a member function to the proper fieldlist. */
8719
8720static void
107d2387 8721dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 8722 struct type *type, struct dwarf2_cu *cu)
c906108c 8723{
e7c27a73 8724 struct objfile *objfile = cu->objfile;
c906108c
SS
8725 struct attribute *attr;
8726 struct fnfieldlist *flp;
8727 int i;
8728 struct fn_field *fnp;
8729 char *fieldname;
c906108c 8730 struct nextfnfield *new_fnfield;
f792889a 8731 struct type *this_type;
60d5a603 8732 enum dwarf_access_attribute accessibility;
c906108c 8733
b4ba55a1 8734 if (cu->language == language_ada)
a73c6dcd 8735 error (_("unexpected member function in Ada type"));
b4ba55a1 8736
2df3850c 8737 /* Get name of member function. */
39cbfefa
DJ
8738 fieldname = dwarf2_name (die, cu);
8739 if (fieldname == NULL)
2df3850c 8740 return;
c906108c 8741
c906108c
SS
8742 /* Look up member function name in fieldlist. */
8743 for (i = 0; i < fip->nfnfields; i++)
8744 {
27bfe10e 8745 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
8746 break;
8747 }
8748
8749 /* Create new list element if necessary. */
8750 if (i < fip->nfnfields)
8751 flp = &fip->fnfieldlists[i];
8752 else
8753 {
8754 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8755 {
8756 fip->fnfieldlists = (struct fnfieldlist *)
8757 xrealloc (fip->fnfieldlists,
8758 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8759 * sizeof (struct fnfieldlist));
c906108c 8760 if (fip->nfnfields == 0)
c13c43fd 8761 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
8762 }
8763 flp = &fip->fnfieldlists[fip->nfnfields];
8764 flp->name = fieldname;
8765 flp->length = 0;
8766 flp->head = NULL;
3da10d80 8767 i = fip->nfnfields++;
c906108c
SS
8768 }
8769
8770 /* Create a new member function field and chain it to the field list
0963b4bd 8771 entry. */
c906108c 8772 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 8773 make_cleanup (xfree, new_fnfield);
c906108c
SS
8774 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8775 new_fnfield->next = flp->head;
8776 flp->head = new_fnfield;
8777 flp->length++;
8778
8779 /* Fill in the member function field info. */
8780 fnp = &new_fnfield->fnfield;
3da10d80
KS
8781
8782 /* Delay processing of the physname until later. */
8783 if (cu->language == language_cplus || cu->language == language_java)
8784 {
8785 add_to_method_list (type, i, flp->length - 1, fieldname,
8786 die, cu);
8787 }
8788 else
8789 {
1d06ead6 8790 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
8791 fnp->physname = physname ? physname : "";
8792 }
8793
c906108c 8794 fnp->type = alloc_type (objfile);
f792889a
DJ
8795 this_type = read_type_die (die, cu);
8796 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 8797 {
f792889a 8798 int nparams = TYPE_NFIELDS (this_type);
c906108c 8799
f792889a 8800 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
8801 of the method itself (TYPE_CODE_METHOD). */
8802 smash_to_method_type (fnp->type, type,
f792889a
DJ
8803 TYPE_TARGET_TYPE (this_type),
8804 TYPE_FIELDS (this_type),
8805 TYPE_NFIELDS (this_type),
8806 TYPE_VARARGS (this_type));
c906108c
SS
8807
8808 /* Handle static member functions.
c5aa993b 8809 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
8810 member functions. G++ helps GDB by marking the first
8811 parameter for non-static member functions (which is the this
8812 pointer) as artificial. We obtain this information from
8813 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 8814 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
8815 fnp->voffset = VOFFSET_STATIC;
8816 }
8817 else
e2e0b3e5 8818 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 8819 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
8820
8821 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 8822 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 8823 fnp->fcontext = die_containing_type (die, cu);
c906108c 8824
3e43a32a
MS
8825 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
8826 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
8827
8828 /* Get accessibility. */
e142c38c 8829 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 8830 if (attr)
60d5a603
JK
8831 accessibility = DW_UNSND (attr);
8832 else
8833 accessibility = dwarf2_default_access_attribute (die, cu);
8834 switch (accessibility)
c906108c 8835 {
60d5a603
JK
8836 case DW_ACCESS_private:
8837 fnp->is_private = 1;
8838 break;
8839 case DW_ACCESS_protected:
8840 fnp->is_protected = 1;
8841 break;
c906108c
SS
8842 }
8843
b02dede2 8844 /* Check for artificial methods. */
e142c38c 8845 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
8846 if (attr && DW_UNSND (attr) != 0)
8847 fnp->is_artificial = 1;
8848
0d564a31 8849 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
8850 function. For older versions of GCC, this is an offset in the
8851 appropriate virtual table, as specified by DW_AT_containing_type.
8852 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
8853 to the object address. */
8854
e142c38c 8855 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 8856 if (attr)
8e19ed76 8857 {
aec5aa8b 8858 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 8859 {
aec5aa8b
TT
8860 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
8861 {
8862 /* Old-style GCC. */
8863 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8864 }
8865 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
8866 || (DW_BLOCK (attr)->size > 1
8867 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
8868 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
8869 {
8870 struct dwarf_block blk;
8871 int offset;
8872
8873 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
8874 ? 1 : 2);
8875 blk.size = DW_BLOCK (attr)->size - offset;
8876 blk.data = DW_BLOCK (attr)->data + offset;
8877 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
8878 if ((fnp->voffset % cu->header.addr_size) != 0)
8879 dwarf2_complex_location_expr_complaint ();
8880 else
8881 fnp->voffset /= cu->header.addr_size;
8882 fnp->voffset += 2;
8883 }
8884 else
8885 dwarf2_complex_location_expr_complaint ();
8886
8887 if (!fnp->fcontext)
8888 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
8889 }
3690dd37 8890 else if (attr_form_is_section_offset (attr))
8e19ed76 8891 {
4d3c2250 8892 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8893 }
8894 else
8895 {
4d3c2250
KB
8896 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
8897 fieldname);
8e19ed76 8898 }
0d564a31 8899 }
d48cc9dd
DJ
8900 else
8901 {
8902 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8903 if (attr && DW_UNSND (attr))
8904 {
8905 /* GCC does this, as of 2008-08-25; PR debug/37237. */
8906 complaint (&symfile_complaints,
3e43a32a
MS
8907 _("Member function \"%s\" (offset %d) is virtual "
8908 "but the vtable offset is not specified"),
b64f50a1 8909 fieldname, die->offset.sect_off);
9655fd1a 8910 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
8911 TYPE_CPLUS_DYNAMIC (type) = 1;
8912 }
8913 }
c906108c
SS
8914}
8915
8916/* Create the vector of member function fields, and attach it to the type. */
8917
8918static void
fba45db2 8919dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8920 struct dwarf2_cu *cu)
c906108c
SS
8921{
8922 struct fnfieldlist *flp;
c906108c
SS
8923 int i;
8924
b4ba55a1 8925 if (cu->language == language_ada)
a73c6dcd 8926 error (_("unexpected member functions in Ada type"));
b4ba55a1 8927
c906108c
SS
8928 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8929 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
8930 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
8931
8932 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
8933 {
8934 struct nextfnfield *nfp = flp->head;
8935 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
8936 int k;
8937
8938 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
8939 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
8940 fn_flp->fn_fields = (struct fn_field *)
8941 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
8942 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 8943 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
8944 }
8945
8946 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
8947}
8948
1168df01
JB
8949/* Returns non-zero if NAME is the name of a vtable member in CU's
8950 language, zero otherwise. */
8951static int
8952is_vtable_name (const char *name, struct dwarf2_cu *cu)
8953{
8954 static const char vptr[] = "_vptr";
987504bb 8955 static const char vtable[] = "vtable";
1168df01 8956
987504bb
JJ
8957 /* Look for the C++ and Java forms of the vtable. */
8958 if ((cu->language == language_java
8959 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
8960 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
8961 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
8962 return 1;
8963
8964 return 0;
8965}
8966
c0dd20ea 8967/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
8968 functions, with the ABI-specified layout. If TYPE describes
8969 such a structure, smash it into a member function type.
61049d3b
DJ
8970
8971 GCC shouldn't do this; it should just output pointer to member DIEs.
8972 This is GCC PR debug/28767. */
c0dd20ea 8973
0b92b5bb
TT
8974static void
8975quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 8976{
0b92b5bb 8977 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
8978
8979 /* Check for a structure with no name and two children. */
0b92b5bb
TT
8980 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
8981 return;
c0dd20ea
DJ
8982
8983 /* Check for __pfn and __delta members. */
0b92b5bb
TT
8984 if (TYPE_FIELD_NAME (type, 0) == NULL
8985 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
8986 || TYPE_FIELD_NAME (type, 1) == NULL
8987 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
8988 return;
c0dd20ea
DJ
8989
8990 /* Find the type of the method. */
0b92b5bb 8991 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
8992 if (pfn_type == NULL
8993 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
8994 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 8995 return;
c0dd20ea
DJ
8996
8997 /* Look for the "this" argument. */
8998 pfn_type = TYPE_TARGET_TYPE (pfn_type);
8999 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 9000 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 9001 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 9002 return;
c0dd20ea
DJ
9003
9004 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
9005 new_type = alloc_type (objfile);
9006 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
9007 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
9008 TYPE_VARARGS (pfn_type));
0b92b5bb 9009 smash_to_methodptr_type (type, new_type);
c0dd20ea 9010}
1168df01 9011
685b1105
JK
9012/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
9013 (icc). */
9014
9015static int
9016producer_is_icc (struct dwarf2_cu *cu)
9017{
9018 if (!cu->checked_producer)
9019 check_producer (cu);
9020
9021 return cu->producer_is_icc;
9022}
9023
c906108c 9024/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
9025 (definition) to create a type for the structure or union. Fill in
9026 the type's name and general properties; the members will not be
9027 processed until process_structure_type.
c906108c 9028
c767944b
DJ
9029 NOTE: we need to call these functions regardless of whether or not the
9030 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
9031 structure or union. This gets the type entered into our set of
9032 user defined types.
9033
9034 However, if the structure is incomplete (an opaque struct/union)
9035 then suppress creating a symbol table entry for it since gdb only
9036 wants to find the one with the complete definition. Note that if
9037 it is complete, we just call new_symbol, which does it's own
9038 checking about whether the struct/union is anonymous or not (and
9039 suppresses creating a symbol table entry itself). */
9040
f792889a 9041static struct type *
134d01f1 9042read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9043{
e7c27a73 9044 struct objfile *objfile = cu->objfile;
c906108c
SS
9045 struct type *type;
9046 struct attribute *attr;
39cbfefa 9047 char *name;
c906108c 9048
348e048f
DE
9049 /* If the definition of this type lives in .debug_types, read that type.
9050 Don't follow DW_AT_specification though, that will take us back up
9051 the chain and we want to go down. */
9052 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9053 if (attr)
9054 {
9055 struct dwarf2_cu *type_cu = cu;
9056 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9057
348e048f
DE
9058 /* We could just recurse on read_structure_type, but we need to call
9059 get_die_type to ensure only one type for this DIE is created.
9060 This is important, for example, because for c++ classes we need
9061 TYPE_NAME set which is only done by new_symbol. Blech. */
9062 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9063
9064 /* TYPE_CU may not be the same as CU.
9065 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9066 return set_die_type (die, type, cu);
9067 }
9068
c0dd20ea 9069 type = alloc_type (objfile);
c906108c 9070 INIT_CPLUS_SPECIFIC (type);
93311388 9071
39cbfefa
DJ
9072 name = dwarf2_name (die, cu);
9073 if (name != NULL)
c906108c 9074 {
987504bb
JJ
9075 if (cu->language == language_cplus
9076 || cu->language == language_java)
63d06c5c 9077 {
3da10d80
KS
9078 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9079
9080 /* dwarf2_full_name might have already finished building the DIE's
9081 type. If so, there is no need to continue. */
9082 if (get_die_type (die, cu) != NULL)
9083 return get_die_type (die, cu);
9084
9085 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
9086 if (die->tag == DW_TAG_structure_type
9087 || die->tag == DW_TAG_class_type)
9088 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
9089 }
9090 else
9091 {
d8151005
DJ
9092 /* The name is already allocated along with this objfile, so
9093 we don't need to duplicate it for the type. */
94af9270
KS
9094 TYPE_TAG_NAME (type) = (char *) name;
9095 if (die->tag == DW_TAG_class_type)
9096 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 9097 }
c906108c
SS
9098 }
9099
9100 if (die->tag == DW_TAG_structure_type)
9101 {
9102 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9103 }
9104 else if (die->tag == DW_TAG_union_type)
9105 {
9106 TYPE_CODE (type) = TYPE_CODE_UNION;
9107 }
9108 else
9109 {
c906108c
SS
9110 TYPE_CODE (type) = TYPE_CODE_CLASS;
9111 }
9112
0cc2414c
TT
9113 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9114 TYPE_DECLARED_CLASS (type) = 1;
9115
e142c38c 9116 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9117 if (attr)
9118 {
9119 TYPE_LENGTH (type) = DW_UNSND (attr);
9120 }
9121 else
9122 {
9123 TYPE_LENGTH (type) = 0;
9124 }
9125
685b1105
JK
9126 if (producer_is_icc (cu))
9127 {
9128 /* ICC does not output the required DW_AT_declaration
9129 on incomplete types, but gives them a size of zero. */
9130 }
9131 else
9132 TYPE_STUB_SUPPORTED (type) = 1;
9133
dc718098 9134 if (die_is_declaration (die, cu))
876cecd0 9135 TYPE_STUB (type) = 1;
a6c727b2
DJ
9136 else if (attr == NULL && die->child == NULL
9137 && producer_is_realview (cu->producer))
9138 /* RealView does not output the required DW_AT_declaration
9139 on incomplete types. */
9140 TYPE_STUB (type) = 1;
dc718098 9141
c906108c
SS
9142 /* We need to add the type field to the die immediately so we don't
9143 infinitely recurse when dealing with pointers to the structure
0963b4bd 9144 type within the structure itself. */
1c379e20 9145 set_die_type (die, type, cu);
c906108c 9146
7e314c57
JK
9147 /* set_die_type should be already done. */
9148 set_descriptive_type (type, die, cu);
9149
c767944b
DJ
9150 return type;
9151}
9152
9153/* Finish creating a structure or union type, including filling in
9154 its members and creating a symbol for it. */
9155
9156static void
9157process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9158{
9159 struct objfile *objfile = cu->objfile;
9160 struct die_info *child_die = die->child;
9161 struct type *type;
9162
9163 type = get_die_type (die, cu);
9164 if (type == NULL)
9165 type = read_structure_type (die, cu);
9166
e142c38c 9167 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
9168 {
9169 struct field_info fi;
9170 struct die_info *child_die;
34eaf542 9171 VEC (symbolp) *template_args = NULL;
c767944b 9172 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
9173
9174 memset (&fi, 0, sizeof (struct field_info));
9175
639d11d3 9176 child_die = die->child;
c906108c
SS
9177
9178 while (child_die && child_die->tag)
9179 {
a9a9bd0f
DC
9180 if (child_die->tag == DW_TAG_member
9181 || child_die->tag == DW_TAG_variable)
c906108c 9182 {
a9a9bd0f
DC
9183 /* NOTE: carlton/2002-11-05: A C++ static data member
9184 should be a DW_TAG_member that is a declaration, but
9185 all versions of G++ as of this writing (so through at
9186 least 3.2.1) incorrectly generate DW_TAG_variable
9187 tags for them instead. */
e7c27a73 9188 dwarf2_add_field (&fi, child_die, cu);
c906108c 9189 }
8713b1b1 9190 else if (child_die->tag == DW_TAG_subprogram)
c906108c 9191 {
0963b4bd 9192 /* C++ member function. */
e7c27a73 9193 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
9194 }
9195 else if (child_die->tag == DW_TAG_inheritance)
9196 {
9197 /* C++ base class field. */
e7c27a73 9198 dwarf2_add_field (&fi, child_die, cu);
c906108c 9199 }
98751a41
JK
9200 else if (child_die->tag == DW_TAG_typedef)
9201 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
9202 else if (child_die->tag == DW_TAG_template_type_param
9203 || child_die->tag == DW_TAG_template_value_param)
9204 {
9205 struct symbol *arg = new_symbol (child_die, NULL, cu);
9206
f1078f66
DJ
9207 if (arg != NULL)
9208 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9209 }
9210
c906108c
SS
9211 child_die = sibling_die (child_die);
9212 }
9213
34eaf542
TT
9214 /* Attach template arguments to type. */
9215 if (! VEC_empty (symbolp, template_args))
9216 {
9217 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9218 TYPE_N_TEMPLATE_ARGUMENTS (type)
9219 = VEC_length (symbolp, template_args);
9220 TYPE_TEMPLATE_ARGUMENTS (type)
9221 = obstack_alloc (&objfile->objfile_obstack,
9222 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9223 * sizeof (struct symbol *)));
9224 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9225 VEC_address (symbolp, template_args),
9226 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9227 * sizeof (struct symbol *)));
9228 VEC_free (symbolp, template_args);
9229 }
9230
c906108c
SS
9231 /* Attach fields and member functions to the type. */
9232 if (fi.nfields)
e7c27a73 9233 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
9234 if (fi.nfnfields)
9235 {
e7c27a73 9236 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 9237
c5aa993b 9238 /* Get the type which refers to the base class (possibly this
c906108c 9239 class itself) which contains the vtable pointer for the current
0d564a31
DJ
9240 class from the DW_AT_containing_type attribute. This use of
9241 DW_AT_containing_type is a GNU extension. */
c906108c 9242
e142c38c 9243 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 9244 {
e7c27a73 9245 struct type *t = die_containing_type (die, cu);
c906108c
SS
9246
9247 TYPE_VPTR_BASETYPE (type) = t;
9248 if (type == t)
9249 {
c906108c
SS
9250 int i;
9251
9252 /* Our own class provides vtbl ptr. */
9253 for (i = TYPE_NFIELDS (t) - 1;
9254 i >= TYPE_N_BASECLASSES (t);
9255 --i)
9256 {
0d5cff50 9257 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 9258
1168df01 9259 if (is_vtable_name (fieldname, cu))
c906108c
SS
9260 {
9261 TYPE_VPTR_FIELDNO (type) = i;
9262 break;
9263 }
9264 }
9265
9266 /* Complain if virtual function table field not found. */
9267 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 9268 complaint (&symfile_complaints,
3e43a32a
MS
9269 _("virtual function table pointer "
9270 "not found when defining class '%s'"),
4d3c2250
KB
9271 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9272 "");
c906108c
SS
9273 }
9274 else
9275 {
9276 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9277 }
9278 }
f6235d4c
EZ
9279 else if (cu->producer
9280 && strncmp (cu->producer,
9281 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9282 {
9283 /* The IBM XLC compiler does not provide direct indication
9284 of the containing type, but the vtable pointer is
9285 always named __vfp. */
9286
9287 int i;
9288
9289 for (i = TYPE_NFIELDS (type) - 1;
9290 i >= TYPE_N_BASECLASSES (type);
9291 --i)
9292 {
9293 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9294 {
9295 TYPE_VPTR_FIELDNO (type) = i;
9296 TYPE_VPTR_BASETYPE (type) = type;
9297 break;
9298 }
9299 }
9300 }
c906108c 9301 }
98751a41
JK
9302
9303 /* Copy fi.typedef_field_list linked list elements content into the
9304 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9305 if (fi.typedef_field_list)
9306 {
9307 int i = fi.typedef_field_list_count;
9308
a0d7a4ff 9309 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
9310 TYPE_TYPEDEF_FIELD_ARRAY (type)
9311 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9312 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9313
9314 /* Reverse the list order to keep the debug info elements order. */
9315 while (--i >= 0)
9316 {
9317 struct typedef_field *dest, *src;
6e70227d 9318
98751a41
JK
9319 dest = &TYPE_TYPEDEF_FIELD (type, i);
9320 src = &fi.typedef_field_list->field;
9321 fi.typedef_field_list = fi.typedef_field_list->next;
9322 *dest = *src;
9323 }
9324 }
c767944b
DJ
9325
9326 do_cleanups (back_to);
eb2a6f42
TT
9327
9328 if (HAVE_CPLUS_STRUCT (type))
9329 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 9330 }
63d06c5c 9331
bb5ed363 9332 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 9333
90aeadfc
DC
9334 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9335 snapshots) has been known to create a die giving a declaration
9336 for a class that has, as a child, a die giving a definition for a
9337 nested class. So we have to process our children even if the
9338 current die is a declaration. Normally, of course, a declaration
9339 won't have any children at all. */
134d01f1 9340
90aeadfc
DC
9341 while (child_die != NULL && child_die->tag)
9342 {
9343 if (child_die->tag == DW_TAG_member
9344 || child_die->tag == DW_TAG_variable
34eaf542
TT
9345 || child_die->tag == DW_TAG_inheritance
9346 || child_die->tag == DW_TAG_template_value_param
9347 || child_die->tag == DW_TAG_template_type_param)
134d01f1 9348 {
90aeadfc 9349 /* Do nothing. */
134d01f1 9350 }
90aeadfc
DC
9351 else
9352 process_die (child_die, cu);
134d01f1 9353
90aeadfc 9354 child_die = sibling_die (child_die);
134d01f1
DJ
9355 }
9356
fa4028e9
JB
9357 /* Do not consider external references. According to the DWARF standard,
9358 these DIEs are identified by the fact that they have no byte_size
9359 attribute, and a declaration attribute. */
9360 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9361 || !die_is_declaration (die, cu))
c767944b 9362 new_symbol (die, type, cu);
134d01f1
DJ
9363}
9364
9365/* Given a DW_AT_enumeration_type die, set its type. We do not
9366 complete the type's fields yet, or create any symbols. */
c906108c 9367
f792889a 9368static struct type *
134d01f1 9369read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9370{
e7c27a73 9371 struct objfile *objfile = cu->objfile;
c906108c 9372 struct type *type;
c906108c 9373 struct attribute *attr;
0114d602 9374 const char *name;
134d01f1 9375
348e048f
DE
9376 /* If the definition of this type lives in .debug_types, read that type.
9377 Don't follow DW_AT_specification though, that will take us back up
9378 the chain and we want to go down. */
9379 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9380 if (attr)
9381 {
9382 struct dwarf2_cu *type_cu = cu;
9383 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9384
348e048f 9385 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9386
9387 /* TYPE_CU may not be the same as CU.
9388 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9389 return set_die_type (die, type, cu);
9390 }
9391
c906108c
SS
9392 type = alloc_type (objfile);
9393
9394 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 9395 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 9396 if (name != NULL)
0114d602 9397 TYPE_TAG_NAME (type) = (char *) name;
c906108c 9398
e142c38c 9399 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9400 if (attr)
9401 {
9402 TYPE_LENGTH (type) = DW_UNSND (attr);
9403 }
9404 else
9405 {
9406 TYPE_LENGTH (type) = 0;
9407 }
9408
137033e9
JB
9409 /* The enumeration DIE can be incomplete. In Ada, any type can be
9410 declared as private in the package spec, and then defined only
9411 inside the package body. Such types are known as Taft Amendment
9412 Types. When another package uses such a type, an incomplete DIE
9413 may be generated by the compiler. */
02eb380e 9414 if (die_is_declaration (die, cu))
876cecd0 9415 TYPE_STUB (type) = 1;
02eb380e 9416
f792889a 9417 return set_die_type (die, type, cu);
134d01f1
DJ
9418}
9419
9420/* Given a pointer to a die which begins an enumeration, process all
9421 the dies that define the members of the enumeration, and create the
9422 symbol for the enumeration type.
9423
9424 NOTE: We reverse the order of the element list. */
9425
9426static void
9427process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9428{
f792889a 9429 struct type *this_type;
134d01f1 9430
f792889a
DJ
9431 this_type = get_die_type (die, cu);
9432 if (this_type == NULL)
9433 this_type = read_enumeration_type (die, cu);
9dc481d3 9434
639d11d3 9435 if (die->child != NULL)
c906108c 9436 {
9dc481d3
DE
9437 struct die_info *child_die;
9438 struct symbol *sym;
9439 struct field *fields = NULL;
9440 int num_fields = 0;
9441 int unsigned_enum = 1;
9442 char *name;
cafec441
TT
9443 int flag_enum = 1;
9444 ULONGEST mask = 0;
9dc481d3 9445
639d11d3 9446 child_die = die->child;
c906108c
SS
9447 while (child_die && child_die->tag)
9448 {
9449 if (child_die->tag != DW_TAG_enumerator)
9450 {
e7c27a73 9451 process_die (child_die, cu);
c906108c
SS
9452 }
9453 else
9454 {
39cbfefa
DJ
9455 name = dwarf2_name (child_die, cu);
9456 if (name)
c906108c 9457 {
f792889a 9458 sym = new_symbol (child_die, this_type, cu);
c906108c 9459 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
9460 {
9461 unsigned_enum = 0;
9462 flag_enum = 0;
9463 }
9464 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9465 flag_enum = 0;
9466 else
9467 mask |= SYMBOL_VALUE (sym);
c906108c
SS
9468
9469 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9470 {
9471 fields = (struct field *)
9472 xrealloc (fields,
9473 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 9474 * sizeof (struct field));
c906108c
SS
9475 }
9476
3567439c 9477 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 9478 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 9479 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
9480 FIELD_BITSIZE (fields[num_fields]) = 0;
9481
9482 num_fields++;
9483 }
9484 }
9485
9486 child_die = sibling_die (child_die);
9487 }
9488
9489 if (num_fields)
9490 {
f792889a
DJ
9491 TYPE_NFIELDS (this_type) = num_fields;
9492 TYPE_FIELDS (this_type) = (struct field *)
9493 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9494 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 9495 sizeof (struct field) * num_fields);
b8c9b27d 9496 xfree (fields);
c906108c
SS
9497 }
9498 if (unsigned_enum)
876cecd0 9499 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
9500 if (flag_enum)
9501 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 9502 }
134d01f1 9503
6c83ed52
TT
9504 /* If we are reading an enum from a .debug_types unit, and the enum
9505 is a declaration, and the enum is not the signatured type in the
9506 unit, then we do not want to add a symbol for it. Adding a
9507 symbol would in some cases obscure the true definition of the
9508 enum, giving users an incomplete type when the definition is
9509 actually available. Note that we do not want to do this for all
9510 enums which are just declarations, because C++0x allows forward
9511 enum declarations. */
3019eac3 9512 if (cu->per_cu->is_debug_types
6c83ed52
TT
9513 && die_is_declaration (die, cu))
9514 {
52dc124a 9515 struct signatured_type *sig_type;
6c83ed52 9516
52dc124a 9517 sig_type
6c83ed52 9518 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 9519 cu->per_cu->info_or_types_section,
6c83ed52 9520 cu->per_cu->offset);
3019eac3
DE
9521 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9522 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
9523 return;
9524 }
9525
f792889a 9526 new_symbol (die, this_type, cu);
c906108c
SS
9527}
9528
9529/* Extract all information from a DW_TAG_array_type DIE and put it in
9530 the DIE's type field. For now, this only handles one dimensional
9531 arrays. */
9532
f792889a 9533static struct type *
e7c27a73 9534read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9535{
e7c27a73 9536 struct objfile *objfile = cu->objfile;
c906108c 9537 struct die_info *child_die;
7e314c57 9538 struct type *type;
c906108c
SS
9539 struct type *element_type, *range_type, *index_type;
9540 struct type **range_types = NULL;
9541 struct attribute *attr;
9542 int ndim = 0;
9543 struct cleanup *back_to;
39cbfefa 9544 char *name;
c906108c 9545
e7c27a73 9546 element_type = die_type (die, cu);
c906108c 9547
7e314c57
JK
9548 /* The die_type call above may have already set the type for this DIE. */
9549 type = get_die_type (die, cu);
9550 if (type)
9551 return type;
9552
c906108c
SS
9553 /* Irix 6.2 native cc creates array types without children for
9554 arrays with unspecified length. */
639d11d3 9555 if (die->child == NULL)
c906108c 9556 {
46bf5051 9557 index_type = objfile_type (objfile)->builtin_int;
c906108c 9558 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
9559 type = create_array_type (NULL, element_type, range_type);
9560 return set_die_type (die, type, cu);
c906108c
SS
9561 }
9562
9563 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 9564 child_die = die->child;
c906108c
SS
9565 while (child_die && child_die->tag)
9566 {
9567 if (child_die->tag == DW_TAG_subrange_type)
9568 {
f792889a 9569 struct type *child_type = read_type_die (child_die, cu);
9a619af0 9570
f792889a 9571 if (child_type != NULL)
a02abb62 9572 {
0963b4bd
MS
9573 /* The range type was succesfully read. Save it for the
9574 array type creation. */
a02abb62
JB
9575 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9576 {
9577 range_types = (struct type **)
9578 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9579 * sizeof (struct type *));
9580 if (ndim == 0)
9581 make_cleanup (free_current_contents, &range_types);
9582 }
f792889a 9583 range_types[ndim++] = child_type;
a02abb62 9584 }
c906108c
SS
9585 }
9586 child_die = sibling_die (child_die);
9587 }
9588
9589 /* Dwarf2 dimensions are output from left to right, create the
9590 necessary array types in backwards order. */
7ca2d3a3 9591
c906108c 9592 type = element_type;
7ca2d3a3
DL
9593
9594 if (read_array_order (die, cu) == DW_ORD_col_major)
9595 {
9596 int i = 0;
9a619af0 9597
7ca2d3a3
DL
9598 while (i < ndim)
9599 type = create_array_type (NULL, type, range_types[i++]);
9600 }
9601 else
9602 {
9603 while (ndim-- > 0)
9604 type = create_array_type (NULL, type, range_types[ndim]);
9605 }
c906108c 9606
f5f8a009
EZ
9607 /* Understand Dwarf2 support for vector types (like they occur on
9608 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9609 array type. This is not part of the Dwarf2/3 standard yet, but a
9610 custom vendor extension. The main difference between a regular
9611 array and the vector variant is that vectors are passed by value
9612 to functions. */
e142c38c 9613 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 9614 if (attr)
ea37ba09 9615 make_vector_type (type);
f5f8a009 9616
dbc98a8b
KW
9617 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9618 implementation may choose to implement triple vectors using this
9619 attribute. */
9620 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9621 if (attr)
9622 {
9623 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9624 TYPE_LENGTH (type) = DW_UNSND (attr);
9625 else
3e43a32a
MS
9626 complaint (&symfile_complaints,
9627 _("DW_AT_byte_size for array type smaller "
9628 "than the total size of elements"));
dbc98a8b
KW
9629 }
9630
39cbfefa
DJ
9631 name = dwarf2_name (die, cu);
9632 if (name)
9633 TYPE_NAME (type) = name;
6e70227d 9634
0963b4bd 9635 /* Install the type in the die. */
7e314c57
JK
9636 set_die_type (die, type, cu);
9637
9638 /* set_die_type should be already done. */
b4ba55a1
JB
9639 set_descriptive_type (type, die, cu);
9640
c906108c
SS
9641 do_cleanups (back_to);
9642
7e314c57 9643 return type;
c906108c
SS
9644}
9645
7ca2d3a3 9646static enum dwarf_array_dim_ordering
6e70227d 9647read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
9648{
9649 struct attribute *attr;
9650
9651 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9652
9653 if (attr) return DW_SND (attr);
9654
0963b4bd
MS
9655 /* GNU F77 is a special case, as at 08/2004 array type info is the
9656 opposite order to the dwarf2 specification, but data is still
9657 laid out as per normal fortran.
7ca2d3a3 9658
0963b4bd
MS
9659 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9660 version checking. */
7ca2d3a3 9661
905e0470
PM
9662 if (cu->language == language_fortran
9663 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
9664 {
9665 return DW_ORD_row_major;
9666 }
9667
6e70227d 9668 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
9669 {
9670 case array_column_major:
9671 return DW_ORD_col_major;
9672 case array_row_major:
9673 default:
9674 return DW_ORD_row_major;
9675 };
9676}
9677
72019c9c 9678/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 9679 the DIE's type field. */
72019c9c 9680
f792889a 9681static struct type *
72019c9c
GM
9682read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9683{
7e314c57
JK
9684 struct type *domain_type, *set_type;
9685 struct attribute *attr;
f792889a 9686
7e314c57
JK
9687 domain_type = die_type (die, cu);
9688
9689 /* The die_type call above may have already set the type for this DIE. */
9690 set_type = get_die_type (die, cu);
9691 if (set_type)
9692 return set_type;
9693
9694 set_type = create_set_type (NULL, domain_type);
9695
9696 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
9697 if (attr)
9698 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 9699
f792889a 9700 return set_die_type (die, set_type, cu);
72019c9c 9701}
7ca2d3a3 9702
c906108c
SS
9703/* First cut: install each common block member as a global variable. */
9704
9705static void
e7c27a73 9706read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
9707{
9708 struct die_info *child_die;
9709 struct attribute *attr;
9710 struct symbol *sym;
9711 CORE_ADDR base = (CORE_ADDR) 0;
9712
e142c38c 9713 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
9714 if (attr)
9715 {
0963b4bd 9716 /* Support the .debug_loc offsets. */
8e19ed76
PS
9717 if (attr_form_is_block (attr))
9718 {
e7c27a73 9719 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 9720 }
3690dd37 9721 else if (attr_form_is_section_offset (attr))
8e19ed76 9722 {
4d3c2250 9723 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9724 }
9725 else
9726 {
4d3c2250
KB
9727 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9728 "common block member");
8e19ed76 9729 }
c906108c 9730 }
639d11d3 9731 if (die->child != NULL)
c906108c 9732 {
639d11d3 9733 child_die = die->child;
c906108c
SS
9734 while (child_die && child_die->tag)
9735 {
74ac6d43
TT
9736 LONGEST offset;
9737
e7c27a73 9738 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
9739 if (sym != NULL
9740 && handle_data_member_location (child_die, cu, &offset))
c906108c 9741 {
74ac6d43 9742 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
9743 add_symbol_to_list (sym, &global_symbols);
9744 }
9745 child_die = sibling_die (child_die);
9746 }
9747 }
9748}
9749
0114d602 9750/* Create a type for a C++ namespace. */
d9fa45fe 9751
0114d602
DJ
9752static struct type *
9753read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 9754{
e7c27a73 9755 struct objfile *objfile = cu->objfile;
0114d602 9756 const char *previous_prefix, *name;
9219021c 9757 int is_anonymous;
0114d602
DJ
9758 struct type *type;
9759
9760 /* For extensions, reuse the type of the original namespace. */
9761 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9762 {
9763 struct die_info *ext_die;
9764 struct dwarf2_cu *ext_cu = cu;
9a619af0 9765
0114d602
DJ
9766 ext_die = dwarf2_extension (die, &ext_cu);
9767 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
9768
9769 /* EXT_CU may not be the same as CU.
9770 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
9771 return set_die_type (die, type, cu);
9772 }
9219021c 9773
e142c38c 9774 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
9775
9776 /* Now build the name of the current namespace. */
9777
0114d602
DJ
9778 previous_prefix = determine_prefix (die, cu);
9779 if (previous_prefix[0] != '\0')
9780 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 9781 previous_prefix, name, 0, cu);
0114d602
DJ
9782
9783 /* Create the type. */
9784 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
9785 objfile);
9786 TYPE_NAME (type) = (char *) name;
9787 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9788
60531b24 9789 return set_die_type (die, type, cu);
0114d602
DJ
9790}
9791
9792/* Read a C++ namespace. */
9793
9794static void
9795read_namespace (struct die_info *die, struct dwarf2_cu *cu)
9796{
9797 struct objfile *objfile = cu->objfile;
0114d602 9798 int is_anonymous;
9219021c 9799
5c4e30ca
DC
9800 /* Add a symbol associated to this if we haven't seen the namespace
9801 before. Also, add a using directive if it's an anonymous
9802 namespace. */
9219021c 9803
f2f0e013 9804 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
9805 {
9806 struct type *type;
9807
0114d602 9808 type = read_type_die (die, cu);
e7c27a73 9809 new_symbol (die, type, cu);
5c4e30ca 9810
e8e80198 9811 namespace_name (die, &is_anonymous, cu);
5c4e30ca 9812 if (is_anonymous)
0114d602
DJ
9813 {
9814 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 9815
c0cc3a76 9816 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 9817 NULL, NULL, &objfile->objfile_obstack);
0114d602 9818 }
5c4e30ca 9819 }
9219021c 9820
639d11d3 9821 if (die->child != NULL)
d9fa45fe 9822 {
639d11d3 9823 struct die_info *child_die = die->child;
6e70227d 9824
d9fa45fe
DC
9825 while (child_die && child_die->tag)
9826 {
e7c27a73 9827 process_die (child_die, cu);
d9fa45fe
DC
9828 child_die = sibling_die (child_die);
9829 }
9830 }
38d518c9
EZ
9831}
9832
f55ee35c
JK
9833/* Read a Fortran module as type. This DIE can be only a declaration used for
9834 imported module. Still we need that type as local Fortran "use ... only"
9835 declaration imports depend on the created type in determine_prefix. */
9836
9837static struct type *
9838read_module_type (struct die_info *die, struct dwarf2_cu *cu)
9839{
9840 struct objfile *objfile = cu->objfile;
9841 char *module_name;
9842 struct type *type;
9843
9844 module_name = dwarf2_name (die, cu);
9845 if (!module_name)
3e43a32a
MS
9846 complaint (&symfile_complaints,
9847 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 9848 die->offset.sect_off);
f55ee35c
JK
9849 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
9850
9851 /* determine_prefix uses TYPE_TAG_NAME. */
9852 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9853
9854 return set_die_type (die, type, cu);
9855}
9856
5d7cb8df
JK
9857/* Read a Fortran module. */
9858
9859static void
9860read_module (struct die_info *die, struct dwarf2_cu *cu)
9861{
9862 struct die_info *child_die = die->child;
9863
5d7cb8df
JK
9864 while (child_die && child_die->tag)
9865 {
9866 process_die (child_die, cu);
9867 child_die = sibling_die (child_die);
9868 }
9869}
9870
38d518c9
EZ
9871/* Return the name of the namespace represented by DIE. Set
9872 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
9873 namespace. */
9874
9875static const char *
e142c38c 9876namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
9877{
9878 struct die_info *current_die;
9879 const char *name = NULL;
9880
9881 /* Loop through the extensions until we find a name. */
9882
9883 for (current_die = die;
9884 current_die != NULL;
f2f0e013 9885 current_die = dwarf2_extension (die, &cu))
38d518c9 9886 {
e142c38c 9887 name = dwarf2_name (current_die, cu);
38d518c9
EZ
9888 if (name != NULL)
9889 break;
9890 }
9891
9892 /* Is it an anonymous namespace? */
9893
9894 *is_anonymous = (name == NULL);
9895 if (*is_anonymous)
2b1dbab0 9896 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
9897
9898 return name;
d9fa45fe
DC
9899}
9900
c906108c
SS
9901/* Extract all information from a DW_TAG_pointer_type DIE and add to
9902 the user defined type vector. */
9903
f792889a 9904static struct type *
e7c27a73 9905read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9906{
5e2b427d 9907 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 9908 struct comp_unit_head *cu_header = &cu->header;
c906108c 9909 struct type *type;
8b2dbe47
KB
9910 struct attribute *attr_byte_size;
9911 struct attribute *attr_address_class;
9912 int byte_size, addr_class;
7e314c57
JK
9913 struct type *target_type;
9914
9915 target_type = die_type (die, cu);
c906108c 9916
7e314c57
JK
9917 /* The die_type call above may have already set the type for this DIE. */
9918 type = get_die_type (die, cu);
9919 if (type)
9920 return type;
9921
9922 type = lookup_pointer_type (target_type);
8b2dbe47 9923
e142c38c 9924 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
9925 if (attr_byte_size)
9926 byte_size = DW_UNSND (attr_byte_size);
c906108c 9927 else
8b2dbe47
KB
9928 byte_size = cu_header->addr_size;
9929
e142c38c 9930 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
9931 if (attr_address_class)
9932 addr_class = DW_UNSND (attr_address_class);
9933 else
9934 addr_class = DW_ADDR_none;
9935
9936 /* If the pointer size or address class is different than the
9937 default, create a type variant marked as such and set the
9938 length accordingly. */
9939 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 9940 {
5e2b427d 9941 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
9942 {
9943 int type_flags;
9944
849957d9 9945 type_flags = gdbarch_address_class_type_flags
5e2b427d 9946 (gdbarch, byte_size, addr_class);
876cecd0
TT
9947 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
9948 == 0);
8b2dbe47
KB
9949 type = make_type_with_address_space (type, type_flags);
9950 }
9951 else if (TYPE_LENGTH (type) != byte_size)
9952 {
3e43a32a
MS
9953 complaint (&symfile_complaints,
9954 _("invalid pointer size %d"), byte_size);
8b2dbe47 9955 }
6e70227d 9956 else
9a619af0
MS
9957 {
9958 /* Should we also complain about unhandled address classes? */
9959 }
c906108c 9960 }
8b2dbe47
KB
9961
9962 TYPE_LENGTH (type) = byte_size;
f792889a 9963 return set_die_type (die, type, cu);
c906108c
SS
9964}
9965
9966/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
9967 the user defined type vector. */
9968
f792889a 9969static struct type *
e7c27a73 9970read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
9971{
9972 struct type *type;
9973 struct type *to_type;
9974 struct type *domain;
9975
e7c27a73
DJ
9976 to_type = die_type (die, cu);
9977 domain = die_containing_type (die, cu);
0d5de010 9978
7e314c57
JK
9979 /* The calls above may have already set the type for this DIE. */
9980 type = get_die_type (die, cu);
9981 if (type)
9982 return type;
9983
0d5de010
DJ
9984 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
9985 type = lookup_methodptr_type (to_type);
9986 else
9987 type = lookup_memberptr_type (to_type, domain);
c906108c 9988
f792889a 9989 return set_die_type (die, type, cu);
c906108c
SS
9990}
9991
9992/* Extract all information from a DW_TAG_reference_type DIE and add to
9993 the user defined type vector. */
9994
f792889a 9995static struct type *
e7c27a73 9996read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9997{
e7c27a73 9998 struct comp_unit_head *cu_header = &cu->header;
7e314c57 9999 struct type *type, *target_type;
c906108c
SS
10000 struct attribute *attr;
10001
7e314c57
JK
10002 target_type = die_type (die, cu);
10003
10004 /* The die_type call above may have already set the type for this DIE. */
10005 type = get_die_type (die, cu);
10006 if (type)
10007 return type;
10008
10009 type = lookup_reference_type (target_type);
e142c38c 10010 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10011 if (attr)
10012 {
10013 TYPE_LENGTH (type) = DW_UNSND (attr);
10014 }
10015 else
10016 {
107d2387 10017 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 10018 }
f792889a 10019 return set_die_type (die, type, cu);
c906108c
SS
10020}
10021
f792889a 10022static struct type *
e7c27a73 10023read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10024{
f792889a 10025 struct type *base_type, *cv_type;
c906108c 10026
e7c27a73 10027 base_type = die_type (die, cu);
7e314c57
JK
10028
10029 /* The die_type call above may have already set the type for this DIE. */
10030 cv_type = get_die_type (die, cu);
10031 if (cv_type)
10032 return cv_type;
10033
2f608a3a
KW
10034 /* In case the const qualifier is applied to an array type, the element type
10035 is so qualified, not the array type (section 6.7.3 of C99). */
10036 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
10037 {
10038 struct type *el_type, *inner_array;
10039
10040 base_type = copy_type (base_type);
10041 inner_array = base_type;
10042
10043 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10044 {
10045 TYPE_TARGET_TYPE (inner_array) =
10046 copy_type (TYPE_TARGET_TYPE (inner_array));
10047 inner_array = TYPE_TARGET_TYPE (inner_array);
10048 }
10049
10050 el_type = TYPE_TARGET_TYPE (inner_array);
10051 TYPE_TARGET_TYPE (inner_array) =
10052 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10053
10054 return set_die_type (die, base_type, cu);
10055 }
10056
f792889a
DJ
10057 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10058 return set_die_type (die, cv_type, cu);
c906108c
SS
10059}
10060
f792889a 10061static struct type *
e7c27a73 10062read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10063{
f792889a 10064 struct type *base_type, *cv_type;
c906108c 10065
e7c27a73 10066 base_type = die_type (die, cu);
7e314c57
JK
10067
10068 /* The die_type call above may have already set the type for this DIE. */
10069 cv_type = get_die_type (die, cu);
10070 if (cv_type)
10071 return cv_type;
10072
f792889a
DJ
10073 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10074 return set_die_type (die, cv_type, cu);
c906108c
SS
10075}
10076
10077/* Extract all information from a DW_TAG_string_type DIE and add to
10078 the user defined type vector. It isn't really a user defined type,
10079 but it behaves like one, with other DIE's using an AT_user_def_type
10080 attribute to reference it. */
10081
f792889a 10082static struct type *
e7c27a73 10083read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10084{
e7c27a73 10085 struct objfile *objfile = cu->objfile;
3b7538c0 10086 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10087 struct type *type, *range_type, *index_type, *char_type;
10088 struct attribute *attr;
10089 unsigned int length;
10090
e142c38c 10091 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
10092 if (attr)
10093 {
10094 length = DW_UNSND (attr);
10095 }
10096 else
10097 {
0963b4bd 10098 /* Check for the DW_AT_byte_size attribute. */
e142c38c 10099 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
10100 if (attr)
10101 {
10102 length = DW_UNSND (attr);
10103 }
10104 else
10105 {
10106 length = 1;
10107 }
c906108c 10108 }
6ccb9162 10109
46bf5051 10110 index_type = objfile_type (objfile)->builtin_int;
c906108c 10111 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
10112 char_type = language_string_char_type (cu->language_defn, gdbarch);
10113 type = create_string_type (NULL, char_type, range_type);
6ccb9162 10114
f792889a 10115 return set_die_type (die, type, cu);
c906108c
SS
10116}
10117
10118/* Handle DIES due to C code like:
10119
10120 struct foo
c5aa993b
JM
10121 {
10122 int (*funcp)(int a, long l);
10123 int b;
10124 };
c906108c 10125
0963b4bd 10126 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 10127
f792889a 10128static struct type *
e7c27a73 10129read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10130{
bb5ed363 10131 struct objfile *objfile = cu->objfile;
0963b4bd
MS
10132 struct type *type; /* Type that this function returns. */
10133 struct type *ftype; /* Function that returns above type. */
c906108c
SS
10134 struct attribute *attr;
10135
e7c27a73 10136 type = die_type (die, cu);
7e314c57
JK
10137
10138 /* The die_type call above may have already set the type for this DIE. */
10139 ftype = get_die_type (die, cu);
10140 if (ftype)
10141 return ftype;
10142
0c8b41f1 10143 ftype = lookup_function_type (type);
c906108c 10144
5b8101ae 10145 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 10146 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 10147 if ((attr && (DW_UNSND (attr) != 0))
987504bb 10148 || cu->language == language_cplus
5b8101ae
PM
10149 || cu->language == language_java
10150 || cu->language == language_pascal)
876cecd0 10151 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
10152 else if (producer_is_realview (cu->producer))
10153 /* RealView does not emit DW_AT_prototyped. We can not
10154 distinguish prototyped and unprototyped functions; default to
10155 prototyped, since that is more common in modern code (and
10156 RealView warns about unprototyped functions). */
10157 TYPE_PROTOTYPED (ftype) = 1;
c906108c 10158
c055b101
CV
10159 /* Store the calling convention in the type if it's available in
10160 the subroutine die. Otherwise set the calling convention to
10161 the default value DW_CC_normal. */
10162 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
10163 if (attr)
10164 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10165 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10166 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10167 else
10168 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
10169
10170 /* We need to add the subroutine type to the die immediately so
10171 we don't infinitely recurse when dealing with parameters
0963b4bd 10172 declared as the same subroutine type. */
76c10ea2 10173 set_die_type (die, ftype, cu);
6e70227d 10174
639d11d3 10175 if (die->child != NULL)
c906108c 10176 {
bb5ed363 10177 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 10178 struct die_info *child_die;
8072405b 10179 int nparams, iparams;
c906108c
SS
10180
10181 /* Count the number of parameters.
10182 FIXME: GDB currently ignores vararg functions, but knows about
10183 vararg member functions. */
8072405b 10184 nparams = 0;
639d11d3 10185 child_die = die->child;
c906108c
SS
10186 while (child_die && child_die->tag)
10187 {
10188 if (child_die->tag == DW_TAG_formal_parameter)
10189 nparams++;
10190 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 10191 TYPE_VARARGS (ftype) = 1;
c906108c
SS
10192 child_die = sibling_die (child_die);
10193 }
10194
10195 /* Allocate storage for parameters and fill them in. */
10196 TYPE_NFIELDS (ftype) = nparams;
10197 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 10198 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 10199
8072405b
JK
10200 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10201 even if we error out during the parameters reading below. */
10202 for (iparams = 0; iparams < nparams; iparams++)
10203 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10204
10205 iparams = 0;
639d11d3 10206 child_die = die->child;
c906108c
SS
10207 while (child_die && child_die->tag)
10208 {
10209 if (child_die->tag == DW_TAG_formal_parameter)
10210 {
3ce3b1ba
PA
10211 struct type *arg_type;
10212
10213 /* DWARF version 2 has no clean way to discern C++
10214 static and non-static member functions. G++ helps
10215 GDB by marking the first parameter for non-static
10216 member functions (which is the this pointer) as
10217 artificial. We pass this information to
10218 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10219
10220 DWARF version 3 added DW_AT_object_pointer, which GCC
10221 4.5 does not yet generate. */
e142c38c 10222 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
10223 if (attr)
10224 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10225 else
418835cc
KS
10226 {
10227 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10228
10229 /* GCC/43521: In java, the formal parameter
10230 "this" is sometimes not marked with DW_AT_artificial. */
10231 if (cu->language == language_java)
10232 {
10233 const char *name = dwarf2_name (child_die, cu);
9a619af0 10234
418835cc
KS
10235 if (name && !strcmp (name, "this"))
10236 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10237 }
10238 }
3ce3b1ba
PA
10239 arg_type = die_type (child_die, cu);
10240
10241 /* RealView does not mark THIS as const, which the testsuite
10242 expects. GCC marks THIS as const in method definitions,
10243 but not in the class specifications (GCC PR 43053). */
10244 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10245 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10246 {
10247 int is_this = 0;
10248 struct dwarf2_cu *arg_cu = cu;
10249 const char *name = dwarf2_name (child_die, cu);
10250
10251 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10252 if (attr)
10253 {
10254 /* If the compiler emits this, use it. */
10255 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10256 is_this = 1;
10257 }
10258 else if (name && strcmp (name, "this") == 0)
10259 /* Function definitions will have the argument names. */
10260 is_this = 1;
10261 else if (name == NULL && iparams == 0)
10262 /* Declarations may not have the names, so like
10263 elsewhere in GDB, assume an artificial first
10264 argument is "this". */
10265 is_this = 1;
10266
10267 if (is_this)
10268 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10269 arg_type, 0);
10270 }
10271
10272 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
10273 iparams++;
10274 }
10275 child_die = sibling_die (child_die);
10276 }
10277 }
10278
76c10ea2 10279 return ftype;
c906108c
SS
10280}
10281
f792889a 10282static struct type *
e7c27a73 10283read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10284{
e7c27a73 10285 struct objfile *objfile = cu->objfile;
0114d602 10286 const char *name = NULL;
3c8e0968 10287 struct type *this_type, *target_type;
c906108c 10288
94af9270 10289 name = dwarf2_full_name (NULL, die, cu);
f792889a 10290 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
10291 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10292 TYPE_NAME (this_type) = (char *) name;
f792889a 10293 set_die_type (die, this_type, cu);
3c8e0968
DE
10294 target_type = die_type (die, cu);
10295 if (target_type != this_type)
10296 TYPE_TARGET_TYPE (this_type) = target_type;
10297 else
10298 {
10299 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10300 spec and cause infinite loops in GDB. */
10301 complaint (&symfile_complaints,
10302 _("Self-referential DW_TAG_typedef "
10303 "- DIE at 0x%x [in module %s]"),
b64f50a1 10304 die->offset.sect_off, objfile->name);
3c8e0968
DE
10305 TYPE_TARGET_TYPE (this_type) = NULL;
10306 }
f792889a 10307 return this_type;
c906108c
SS
10308}
10309
10310/* Find a representation of a given base type and install
10311 it in the TYPE field of the die. */
10312
f792889a 10313static struct type *
e7c27a73 10314read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10315{
e7c27a73 10316 struct objfile *objfile = cu->objfile;
c906108c
SS
10317 struct type *type;
10318 struct attribute *attr;
10319 int encoding = 0, size = 0;
39cbfefa 10320 char *name;
6ccb9162
UW
10321 enum type_code code = TYPE_CODE_INT;
10322 int type_flags = 0;
10323 struct type *target_type = NULL;
c906108c 10324
e142c38c 10325 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
10326 if (attr)
10327 {
10328 encoding = DW_UNSND (attr);
10329 }
e142c38c 10330 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10331 if (attr)
10332 {
10333 size = DW_UNSND (attr);
10334 }
39cbfefa 10335 name = dwarf2_name (die, cu);
6ccb9162 10336 if (!name)
c906108c 10337 {
6ccb9162
UW
10338 complaint (&symfile_complaints,
10339 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 10340 }
6ccb9162
UW
10341
10342 switch (encoding)
c906108c 10343 {
6ccb9162
UW
10344 case DW_ATE_address:
10345 /* Turn DW_ATE_address into a void * pointer. */
10346 code = TYPE_CODE_PTR;
10347 type_flags |= TYPE_FLAG_UNSIGNED;
10348 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10349 break;
10350 case DW_ATE_boolean:
10351 code = TYPE_CODE_BOOL;
10352 type_flags |= TYPE_FLAG_UNSIGNED;
10353 break;
10354 case DW_ATE_complex_float:
10355 code = TYPE_CODE_COMPLEX;
10356 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10357 break;
10358 case DW_ATE_decimal_float:
10359 code = TYPE_CODE_DECFLOAT;
10360 break;
10361 case DW_ATE_float:
10362 code = TYPE_CODE_FLT;
10363 break;
10364 case DW_ATE_signed:
10365 break;
10366 case DW_ATE_unsigned:
10367 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
10368 if (cu->language == language_fortran
10369 && name
10370 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10371 code = TYPE_CODE_CHAR;
6ccb9162
UW
10372 break;
10373 case DW_ATE_signed_char:
6e70227d 10374 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10375 || cu->language == language_pascal
10376 || cu->language == language_fortran)
6ccb9162
UW
10377 code = TYPE_CODE_CHAR;
10378 break;
10379 case DW_ATE_unsigned_char:
868a0084 10380 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10381 || cu->language == language_pascal
10382 || cu->language == language_fortran)
6ccb9162
UW
10383 code = TYPE_CODE_CHAR;
10384 type_flags |= TYPE_FLAG_UNSIGNED;
10385 break;
75079b2b
TT
10386 case DW_ATE_UTF:
10387 /* We just treat this as an integer and then recognize the
10388 type by name elsewhere. */
10389 break;
10390
6ccb9162
UW
10391 default:
10392 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10393 dwarf_type_encoding_name (encoding));
10394 break;
c906108c 10395 }
6ccb9162 10396
0114d602
DJ
10397 type = init_type (code, size, type_flags, NULL, objfile);
10398 TYPE_NAME (type) = name;
6ccb9162
UW
10399 TYPE_TARGET_TYPE (type) = target_type;
10400
0114d602 10401 if (name && strcmp (name, "char") == 0)
876cecd0 10402 TYPE_NOSIGN (type) = 1;
0114d602 10403
f792889a 10404 return set_die_type (die, type, cu);
c906108c
SS
10405}
10406
a02abb62
JB
10407/* Read the given DW_AT_subrange DIE. */
10408
f792889a 10409static struct type *
a02abb62
JB
10410read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10411{
10412 struct type *base_type;
10413 struct type *range_type;
10414 struct attribute *attr;
4fae6e18
JK
10415 LONGEST low, high;
10416 int low_default_is_valid;
39cbfefa 10417 char *name;
43bbcdc2 10418 LONGEST negative_mask;
e77813c8 10419
a02abb62 10420 base_type = die_type (die, cu);
953ac07e
JK
10421 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10422 check_typedef (base_type);
a02abb62 10423
7e314c57
JK
10424 /* The die_type call above may have already set the type for this DIE. */
10425 range_type = get_die_type (die, cu);
10426 if (range_type)
10427 return range_type;
10428
4fae6e18
JK
10429 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10430 omitting DW_AT_lower_bound. */
10431 switch (cu->language)
6e70227d 10432 {
4fae6e18
JK
10433 case language_c:
10434 case language_cplus:
10435 low = 0;
10436 low_default_is_valid = 1;
10437 break;
10438 case language_fortran:
10439 low = 1;
10440 low_default_is_valid = 1;
10441 break;
10442 case language_d:
10443 case language_java:
10444 case language_objc:
10445 low = 0;
10446 low_default_is_valid = (cu->header.version >= 4);
10447 break;
10448 case language_ada:
10449 case language_m2:
10450 case language_pascal:
a02abb62 10451 low = 1;
4fae6e18
JK
10452 low_default_is_valid = (cu->header.version >= 4);
10453 break;
10454 default:
10455 low = 0;
10456 low_default_is_valid = 0;
10457 break;
a02abb62
JB
10458 }
10459
dd5e6932
DJ
10460 /* FIXME: For variable sized arrays either of these could be
10461 a variable rather than a constant value. We'll allow it,
10462 but we don't know how to handle it. */
e142c38c 10463 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 10464 if (attr)
4fae6e18
JK
10465 low = dwarf2_get_attr_constant_value (attr, low);
10466 else if (!low_default_is_valid)
10467 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10468 "- DIE at 0x%x [in module %s]"),
10469 die->offset.sect_off, cu->objfile->name);
a02abb62 10470
e142c38c 10471 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 10472 if (attr)
6e70227d 10473 {
d48323d8 10474 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
10475 {
10476 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 10477 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
10478 FIXME: GDB does not yet know how to handle dynamic
10479 arrays properly, treat them as arrays with unspecified
10480 length for now.
10481
10482 FIXME: jimb/2003-09-22: GDB does not really know
10483 how to handle arrays of unspecified length
10484 either; we just represent them as zero-length
10485 arrays. Choose an appropriate upper bound given
10486 the lower bound we've computed above. */
10487 high = low - 1;
10488 }
10489 else
10490 high = dwarf2_get_attr_constant_value (attr, 1);
10491 }
e77813c8
PM
10492 else
10493 {
10494 attr = dwarf2_attr (die, DW_AT_count, cu);
10495 if (attr)
10496 {
10497 int count = dwarf2_get_attr_constant_value (attr, 1);
10498 high = low + count - 1;
10499 }
c2ff108b
JK
10500 else
10501 {
10502 /* Unspecified array length. */
10503 high = low - 1;
10504 }
e77813c8
PM
10505 }
10506
10507 /* Dwarf-2 specifications explicitly allows to create subrange types
10508 without specifying a base type.
10509 In that case, the base type must be set to the type of
10510 the lower bound, upper bound or count, in that order, if any of these
10511 three attributes references an object that has a type.
10512 If no base type is found, the Dwarf-2 specifications say that
10513 a signed integer type of size equal to the size of an address should
10514 be used.
10515 For the following C code: `extern char gdb_int [];'
10516 GCC produces an empty range DIE.
10517 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 10518 high bound or count are not yet handled by this code. */
e77813c8
PM
10519 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10520 {
10521 struct objfile *objfile = cu->objfile;
10522 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10523 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10524 struct type *int_type = objfile_type (objfile)->builtin_int;
10525
10526 /* Test "int", "long int", and "long long int" objfile types,
10527 and select the first one having a size above or equal to the
10528 architecture address size. */
10529 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10530 base_type = int_type;
10531 else
10532 {
10533 int_type = objfile_type (objfile)->builtin_long;
10534 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10535 base_type = int_type;
10536 else
10537 {
10538 int_type = objfile_type (objfile)->builtin_long_long;
10539 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10540 base_type = int_type;
10541 }
10542 }
10543 }
a02abb62 10544
6e70227d 10545 negative_mask =
43bbcdc2
PH
10546 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10547 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10548 low |= negative_mask;
10549 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10550 high |= negative_mask;
10551
a02abb62
JB
10552 range_type = create_range_type (NULL, base_type, low, high);
10553
bbb0eef6
JK
10554 /* Mark arrays with dynamic length at least as an array of unspecified
10555 length. GDB could check the boundary but before it gets implemented at
10556 least allow accessing the array elements. */
d48323d8 10557 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
10558 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10559
c2ff108b
JK
10560 /* Ada expects an empty array on no boundary attributes. */
10561 if (attr == NULL && cu->language != language_ada)
10562 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10563
39cbfefa
DJ
10564 name = dwarf2_name (die, cu);
10565 if (name)
10566 TYPE_NAME (range_type) = name;
6e70227d 10567
e142c38c 10568 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
10569 if (attr)
10570 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10571
7e314c57
JK
10572 set_die_type (die, range_type, cu);
10573
10574 /* set_die_type should be already done. */
b4ba55a1
JB
10575 set_descriptive_type (range_type, die, cu);
10576
7e314c57 10577 return range_type;
a02abb62 10578}
6e70227d 10579
f792889a 10580static struct type *
81a17f79
JB
10581read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10582{
10583 struct type *type;
81a17f79 10584
81a17f79
JB
10585 /* For now, we only support the C meaning of an unspecified type: void. */
10586
0114d602
DJ
10587 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10588 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 10589
f792889a 10590 return set_die_type (die, type, cu);
81a17f79 10591}
a02abb62 10592
639d11d3
DC
10593/* Read a single die and all its descendents. Set the die's sibling
10594 field to NULL; set other fields in the die correctly, and set all
10595 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10596 location of the info_ptr after reading all of those dies. PARENT
10597 is the parent of the die in question. */
10598
10599static struct die_info *
dee91e82
DE
10600read_die_and_children (const struct die_reader_specs *reader,
10601 gdb_byte *info_ptr,
10602 gdb_byte **new_info_ptr,
10603 struct die_info *parent)
639d11d3
DC
10604{
10605 struct die_info *die;
fe1b8b76 10606 gdb_byte *cur_ptr;
639d11d3
DC
10607 int has_children;
10608
93311388 10609 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
10610 if (die == NULL)
10611 {
10612 *new_info_ptr = cur_ptr;
10613 return NULL;
10614 }
93311388 10615 store_in_ref_table (die, reader->cu);
639d11d3
DC
10616
10617 if (has_children)
348e048f 10618 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
10619 else
10620 {
10621 die->child = NULL;
10622 *new_info_ptr = cur_ptr;
10623 }
10624
10625 die->sibling = NULL;
10626 die->parent = parent;
10627 return die;
10628}
10629
10630/* Read a die, all of its descendents, and all of its siblings; set
10631 all of the fields of all of the dies correctly. Arguments are as
10632 in read_die_and_children. */
10633
10634static struct die_info *
93311388
DE
10635read_die_and_siblings (const struct die_reader_specs *reader,
10636 gdb_byte *info_ptr,
fe1b8b76 10637 gdb_byte **new_info_ptr,
639d11d3
DC
10638 struct die_info *parent)
10639{
10640 struct die_info *first_die, *last_sibling;
fe1b8b76 10641 gdb_byte *cur_ptr;
639d11d3 10642
c906108c 10643 cur_ptr = info_ptr;
639d11d3
DC
10644 first_die = last_sibling = NULL;
10645
10646 while (1)
c906108c 10647 {
639d11d3 10648 struct die_info *die
dee91e82 10649 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 10650
1d325ec1 10651 if (die == NULL)
c906108c 10652 {
639d11d3
DC
10653 *new_info_ptr = cur_ptr;
10654 return first_die;
c906108c 10655 }
1d325ec1
DJ
10656
10657 if (!first_die)
10658 first_die = die;
c906108c 10659 else
1d325ec1
DJ
10660 last_sibling->sibling = die;
10661
10662 last_sibling = die;
c906108c 10663 }
c906108c
SS
10664}
10665
3019eac3
DE
10666/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10667 attributes.
10668 The caller is responsible for filling in the extra attributes
10669 and updating (*DIEP)->num_attrs.
10670 Set DIEP to point to a newly allocated die with its information,
10671 except for its child, sibling, and parent fields.
10672 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
10673
10674static gdb_byte *
3019eac3
DE
10675read_full_die_1 (const struct die_reader_specs *reader,
10676 struct die_info **diep, gdb_byte *info_ptr,
10677 int *has_children, int num_extra_attrs)
93311388 10678{
b64f50a1
JK
10679 unsigned int abbrev_number, bytes_read, i;
10680 sect_offset offset;
93311388
DE
10681 struct abbrev_info *abbrev;
10682 struct die_info *die;
10683 struct dwarf2_cu *cu = reader->cu;
10684 bfd *abfd = reader->abfd;
10685
b64f50a1 10686 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
10687 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10688 info_ptr += bytes_read;
10689 if (!abbrev_number)
10690 {
10691 *diep = NULL;
10692 *has_children = 0;
10693 return info_ptr;
10694 }
10695
10696 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10697 if (!abbrev)
348e048f
DE
10698 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10699 abbrev_number,
10700 bfd_get_filename (abfd));
10701
3019eac3 10702 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
10703 die->offset = offset;
10704 die->tag = abbrev->tag;
10705 die->abbrev = abbrev_number;
10706
3019eac3
DE
10707 /* Make the result usable.
10708 The caller needs to update num_attrs after adding the extra
10709 attributes. */
93311388
DE
10710 die->num_attrs = abbrev->num_attrs;
10711
10712 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
10713 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10714 info_ptr);
93311388
DE
10715
10716 *diep = die;
10717 *has_children = abbrev->has_children;
10718 return info_ptr;
10719}
10720
3019eac3
DE
10721/* Read a die and all its attributes.
10722 Set DIEP to point to a newly allocated die with its information,
10723 except for its child, sibling, and parent fields.
10724 Set HAS_CHILDREN to tell whether the die has children or not. */
10725
10726static gdb_byte *
10727read_full_die (const struct die_reader_specs *reader,
10728 struct die_info **diep, gdb_byte *info_ptr,
10729 int *has_children)
10730{
10731 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10732}
10733
c906108c
SS
10734/* In DWARF version 2, the description of the debugging information is
10735 stored in a separate .debug_abbrev section. Before we read any
10736 dies from a section we read in all abbreviations and install them
72bf9492
DJ
10737 in a hash table. This function also sets flags in CU describing
10738 the data found in the abbrev table. */
c906108c
SS
10739
10740static void
dee91e82
DE
10741dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10742 struct dwarf2_section_info *abbrev_section)
10743
c906108c 10744{
dee91e82 10745 bfd *abfd = abbrev_section->asection->owner;
e7c27a73 10746 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 10747 gdb_byte *abbrev_ptr;
c906108c
SS
10748 struct abbrev_info *cur_abbrev;
10749 unsigned int abbrev_number, bytes_read, abbrev_name;
10750 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
10751 struct attr_abbrev *cur_attrs;
10752 unsigned int allocated_attrs;
c906108c 10753
0963b4bd 10754 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
10755 obstack_init (&cu->abbrev_obstack);
10756 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10757 (ABBREV_HASH_SIZE
10758 * sizeof (struct abbrev_info *)));
10759 memset (cu->dwarf2_abbrevs, 0,
10760 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 10761
dee91e82
DE
10762 dwarf2_read_section (cu->objfile, abbrev_section);
10763 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
c906108c
SS
10764 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10765 abbrev_ptr += bytes_read;
10766
f3dd6933
DJ
10767 allocated_attrs = ATTR_ALLOC_CHUNK;
10768 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 10769
0963b4bd 10770 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
10771 while (abbrev_number)
10772 {
f3dd6933 10773 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
10774
10775 /* read in abbrev header */
10776 cur_abbrev->number = abbrev_number;
10777 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10778 abbrev_ptr += bytes_read;
10779 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
10780 abbrev_ptr += 1;
10781
10782 /* now read in declarations */
10783 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10784 abbrev_ptr += bytes_read;
10785 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10786 abbrev_ptr += bytes_read;
10787 while (abbrev_name)
10788 {
f3dd6933 10789 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 10790 {
f3dd6933
DJ
10791 allocated_attrs += ATTR_ALLOC_CHUNK;
10792 cur_attrs
10793 = xrealloc (cur_attrs, (allocated_attrs
10794 * sizeof (struct attr_abbrev)));
c906108c 10795 }
ae038cb0 10796
f3dd6933
DJ
10797 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
10798 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
10799 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10800 abbrev_ptr += bytes_read;
10801 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10802 abbrev_ptr += bytes_read;
10803 }
10804
f3dd6933
DJ
10805 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
10806 (cur_abbrev->num_attrs
10807 * sizeof (struct attr_abbrev)));
10808 memcpy (cur_abbrev->attrs, cur_attrs,
10809 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
10810
c906108c 10811 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
10812 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
10813 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
10814
10815 /* Get next abbreviation.
10816 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
10817 always properly terminated with an abbrev number of 0.
10818 Exit loop if we encounter an abbreviation which we have
10819 already read (which means we are about to read the abbreviations
10820 for the next compile unit) or if the end of the abbreviation
10821 table is reached. */
dee91e82
DE
10822 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
10823 >= abbrev_section->size)
c906108c
SS
10824 break;
10825 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10826 abbrev_ptr += bytes_read;
e7c27a73 10827 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
10828 break;
10829 }
f3dd6933
DJ
10830
10831 xfree (cur_attrs);
c906108c
SS
10832}
10833
f3dd6933 10834/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 10835
c906108c 10836static void
f3dd6933 10837dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 10838{
f3dd6933 10839 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 10840
f3dd6933
DJ
10841 obstack_free (&cu->abbrev_obstack, NULL);
10842 cu->dwarf2_abbrevs = NULL;
c906108c
SS
10843}
10844
10845/* Lookup an abbrev_info structure in the abbrev hash table. */
10846
10847static struct abbrev_info *
e7c27a73 10848dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
10849{
10850 unsigned int hash_number;
10851 struct abbrev_info *abbrev;
10852
10853 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 10854 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
10855
10856 while (abbrev)
10857 {
10858 if (abbrev->number == number)
10859 return abbrev;
10860 else
10861 abbrev = abbrev->next;
10862 }
10863 return NULL;
10864}
10865
72bf9492
DJ
10866/* Returns nonzero if TAG represents a type that we might generate a partial
10867 symbol for. */
10868
10869static int
10870is_type_tag_for_partial (int tag)
10871{
10872 switch (tag)
10873 {
10874#if 0
10875 /* Some types that would be reasonable to generate partial symbols for,
10876 that we don't at present. */
10877 case DW_TAG_array_type:
10878 case DW_TAG_file_type:
10879 case DW_TAG_ptr_to_member_type:
10880 case DW_TAG_set_type:
10881 case DW_TAG_string_type:
10882 case DW_TAG_subroutine_type:
10883#endif
10884 case DW_TAG_base_type:
10885 case DW_TAG_class_type:
680b30c7 10886 case DW_TAG_interface_type:
72bf9492
DJ
10887 case DW_TAG_enumeration_type:
10888 case DW_TAG_structure_type:
10889 case DW_TAG_subrange_type:
10890 case DW_TAG_typedef:
10891 case DW_TAG_union_type:
10892 return 1;
10893 default:
10894 return 0;
10895 }
10896}
10897
10898/* Load all DIEs that are interesting for partial symbols into memory. */
10899
10900static struct partial_die_info *
dee91e82
DE
10901load_partial_dies (const struct die_reader_specs *reader,
10902 gdb_byte *info_ptr, int building_psymtab)
72bf9492 10903{
dee91e82 10904 struct dwarf2_cu *cu = reader->cu;
bb5ed363 10905 struct objfile *objfile = cu->objfile;
72bf9492
DJ
10906 struct partial_die_info *part_die;
10907 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
10908 struct abbrev_info *abbrev;
10909 unsigned int bytes_read;
5afb4e99 10910 unsigned int load_all = 0;
72bf9492
DJ
10911 int nesting_level = 1;
10912
10913 parent_die = NULL;
10914 last_die = NULL;
10915
7adf1e79
DE
10916 gdb_assert (cu->per_cu != NULL);
10917 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
10918 load_all = 1;
10919
72bf9492
DJ
10920 cu->partial_dies
10921 = htab_create_alloc_ex (cu->header.length / 12,
10922 partial_die_hash,
10923 partial_die_eq,
10924 NULL,
10925 &cu->comp_unit_obstack,
10926 hashtab_obstack_allocate,
10927 dummy_obstack_deallocate);
10928
10929 part_die = obstack_alloc (&cu->comp_unit_obstack,
10930 sizeof (struct partial_die_info));
10931
10932 while (1)
10933 {
10934 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
10935
10936 /* A NULL abbrev means the end of a series of children. */
10937 if (abbrev == NULL)
10938 {
10939 if (--nesting_level == 0)
10940 {
10941 /* PART_DIE was probably the last thing allocated on the
10942 comp_unit_obstack, so we could call obstack_free
10943 here. We don't do that because the waste is small,
10944 and will be cleaned up when we're done with this
10945 compilation unit. This way, we're also more robust
10946 against other users of the comp_unit_obstack. */
10947 return first_die;
10948 }
10949 info_ptr += bytes_read;
10950 last_die = parent_die;
10951 parent_die = parent_die->die_parent;
10952 continue;
10953 }
10954
98bfdba5
PA
10955 /* Check for template arguments. We never save these; if
10956 they're seen, we just mark the parent, and go on our way. */
10957 if (parent_die != NULL
10958 && cu->language == language_cplus
10959 && (abbrev->tag == DW_TAG_template_type_param
10960 || abbrev->tag == DW_TAG_template_value_param))
10961 {
10962 parent_die->has_template_arguments = 1;
10963
10964 if (!load_all)
10965 {
10966 /* We don't need a partial DIE for the template argument. */
dee91e82 10967 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
10968 continue;
10969 }
10970 }
10971
0d99eb77 10972 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
10973 Skip their other children. */
10974 if (!load_all
10975 && cu->language == language_cplus
10976 && parent_die != NULL
10977 && parent_die->tag == DW_TAG_subprogram)
10978 {
dee91e82 10979 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
10980 continue;
10981 }
10982
5afb4e99
DJ
10983 /* Check whether this DIE is interesting enough to save. Normally
10984 we would not be interested in members here, but there may be
10985 later variables referencing them via DW_AT_specification (for
10986 static members). */
10987 if (!load_all
10988 && !is_type_tag_for_partial (abbrev->tag)
72929c62 10989 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
10990 && abbrev->tag != DW_TAG_enumerator
10991 && abbrev->tag != DW_TAG_subprogram
bc30ff58 10992 && abbrev->tag != DW_TAG_lexical_block
72bf9492 10993 && abbrev->tag != DW_TAG_variable
5afb4e99 10994 && abbrev->tag != DW_TAG_namespace
f55ee35c 10995 && abbrev->tag != DW_TAG_module
95554aad
TT
10996 && abbrev->tag != DW_TAG_member
10997 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
10998 {
10999 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11000 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
11001 continue;
11002 }
11003
dee91e82
DE
11004 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
11005 info_ptr);
72bf9492
DJ
11006
11007 /* This two-pass algorithm for processing partial symbols has a
11008 high cost in cache pressure. Thus, handle some simple cases
11009 here which cover the majority of C partial symbols. DIEs
11010 which neither have specification tags in them, nor could have
11011 specification tags elsewhere pointing at them, can simply be
11012 processed and discarded.
11013
11014 This segment is also optional; scan_partial_symbols and
11015 add_partial_symbol will handle these DIEs if we chain
11016 them in normally. When compilers which do not emit large
11017 quantities of duplicate debug information are more common,
11018 this code can probably be removed. */
11019
11020 /* Any complete simple types at the top level (pretty much all
11021 of them, for a language without namespaces), can be processed
11022 directly. */
11023 if (parent_die == NULL
11024 && part_die->has_specification == 0
11025 && part_die->is_declaration == 0
d8228535 11026 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
11027 || part_die->tag == DW_TAG_base_type
11028 || part_die->tag == DW_TAG_subrange_type))
11029 {
11030 if (building_psymtab && part_die->name != NULL)
04a679b8 11031 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11032 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
11033 &objfile->static_psymbols,
11034 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 11035 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11036 continue;
11037 }
11038
d8228535
JK
11039 /* The exception for DW_TAG_typedef with has_children above is
11040 a workaround of GCC PR debug/47510. In the case of this complaint
11041 type_name_no_tag_or_error will error on such types later.
11042
11043 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11044 it could not find the child DIEs referenced later, this is checked
11045 above. In correct DWARF DW_TAG_typedef should have no children. */
11046
11047 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11048 complaint (&symfile_complaints,
11049 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11050 "- DIE at 0x%x [in module %s]"),
b64f50a1 11051 part_die->offset.sect_off, objfile->name);
d8228535 11052
72bf9492
DJ
11053 /* If we're at the second level, and we're an enumerator, and
11054 our parent has no specification (meaning possibly lives in a
11055 namespace elsewhere), then we can add the partial symbol now
11056 instead of queueing it. */
11057 if (part_die->tag == DW_TAG_enumerator
11058 && parent_die != NULL
11059 && parent_die->die_parent == NULL
11060 && parent_die->tag == DW_TAG_enumeration_type
11061 && parent_die->has_specification == 0)
11062 {
11063 if (part_die->name == NULL)
3e43a32a
MS
11064 complaint (&symfile_complaints,
11065 _("malformed enumerator DIE ignored"));
72bf9492 11066 else if (building_psymtab)
04a679b8 11067 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11068 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
11069 (cu->language == language_cplus
11070 || cu->language == language_java)
bb5ed363
DE
11071 ? &objfile->global_psymbols
11072 : &objfile->static_psymbols,
11073 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 11074
dee91e82 11075 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11076 continue;
11077 }
11078
11079 /* We'll save this DIE so link it in. */
11080 part_die->die_parent = parent_die;
11081 part_die->die_sibling = NULL;
11082 part_die->die_child = NULL;
11083
11084 if (last_die && last_die == parent_die)
11085 last_die->die_child = part_die;
11086 else if (last_die)
11087 last_die->die_sibling = part_die;
11088
11089 last_die = part_die;
11090
11091 if (first_die == NULL)
11092 first_die = part_die;
11093
11094 /* Maybe add the DIE to the hash table. Not all DIEs that we
11095 find interesting need to be in the hash table, because we
11096 also have the parent/sibling/child chains; only those that we
11097 might refer to by offset later during partial symbol reading.
11098
11099 For now this means things that might have be the target of a
11100 DW_AT_specification, DW_AT_abstract_origin, or
11101 DW_AT_extension. DW_AT_extension will refer only to
11102 namespaces; DW_AT_abstract_origin refers to functions (and
11103 many things under the function DIE, but we do not recurse
11104 into function DIEs during partial symbol reading) and
11105 possibly variables as well; DW_AT_specification refers to
11106 declarations. Declarations ought to have the DW_AT_declaration
11107 flag. It happens that GCC forgets to put it in sometimes, but
11108 only for functions, not for types.
11109
11110 Adding more things than necessary to the hash table is harmless
11111 except for the performance cost. Adding too few will result in
5afb4e99
DJ
11112 wasted time in find_partial_die, when we reread the compilation
11113 unit with load_all_dies set. */
72bf9492 11114
5afb4e99 11115 if (load_all
72929c62 11116 || abbrev->tag == DW_TAG_constant
5afb4e99 11117 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
11118 || abbrev->tag == DW_TAG_variable
11119 || abbrev->tag == DW_TAG_namespace
11120 || part_die->is_declaration)
11121 {
11122 void **slot;
11123
11124 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 11125 part_die->offset.sect_off, INSERT);
72bf9492
DJ
11126 *slot = part_die;
11127 }
11128
11129 part_die = obstack_alloc (&cu->comp_unit_obstack,
11130 sizeof (struct partial_die_info));
11131
11132 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 11133 we have no reason to follow the children of structures; for other
98bfdba5
PA
11134 languages we have to, so that we can get at method physnames
11135 to infer fully qualified class names, for DW_AT_specification,
11136 and for C++ template arguments. For C++, we also look one level
11137 inside functions to find template arguments (if the name of the
11138 function does not already contain the template arguments).
bc30ff58
JB
11139
11140 For Ada, we need to scan the children of subprograms and lexical
11141 blocks as well because Ada allows the definition of nested
11142 entities that could be interesting for the debugger, such as
11143 nested subprograms for instance. */
72bf9492 11144 if (last_die->has_children
5afb4e99
DJ
11145 && (load_all
11146 || last_die->tag == DW_TAG_namespace
f55ee35c 11147 || last_die->tag == DW_TAG_module
72bf9492 11148 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
11149 || (cu->language == language_cplus
11150 && last_die->tag == DW_TAG_subprogram
11151 && (last_die->name == NULL
11152 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
11153 || (cu->language != language_c
11154 && (last_die->tag == DW_TAG_class_type
680b30c7 11155 || last_die->tag == DW_TAG_interface_type
72bf9492 11156 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
11157 || last_die->tag == DW_TAG_union_type))
11158 || (cu->language == language_ada
11159 && (last_die->tag == DW_TAG_subprogram
11160 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
11161 {
11162 nesting_level++;
11163 parent_die = last_die;
11164 continue;
11165 }
11166
11167 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11168 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
11169
11170 /* Back to the top, do it again. */
11171 }
11172}
11173
c906108c
SS
11174/* Read a minimal amount of information into the minimal die structure. */
11175
fe1b8b76 11176static gdb_byte *
dee91e82
DE
11177read_partial_die (const struct die_reader_specs *reader,
11178 struct partial_die_info *part_die,
11179 struct abbrev_info *abbrev, unsigned int abbrev_len,
11180 gdb_byte *info_ptr)
c906108c 11181{
dee91e82 11182 struct dwarf2_cu *cu = reader->cu;
bb5ed363 11183 struct objfile *objfile = cu->objfile;
dee91e82 11184 gdb_byte *buffer = reader->buffer;
fa238c03 11185 unsigned int i;
c906108c 11186 struct attribute attr;
c5aa993b 11187 int has_low_pc_attr = 0;
c906108c 11188 int has_high_pc_attr = 0;
91da1414 11189 int high_pc_relative = 0;
c906108c 11190
72bf9492 11191 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 11192
b64f50a1 11193 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
11194
11195 info_ptr += abbrev_len;
11196
11197 if (abbrev == NULL)
11198 return info_ptr;
11199
c906108c
SS
11200 part_die->tag = abbrev->tag;
11201 part_die->has_children = abbrev->has_children;
c906108c
SS
11202
11203 for (i = 0; i < abbrev->num_attrs; ++i)
11204 {
dee91e82 11205 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
11206
11207 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 11208 partial symbol table. */
c906108c
SS
11209 switch (attr.name)
11210 {
11211 case DW_AT_name:
71c25dea
TT
11212 switch (part_die->tag)
11213 {
11214 case DW_TAG_compile_unit:
95554aad 11215 case DW_TAG_partial_unit:
348e048f 11216 case DW_TAG_type_unit:
71c25dea
TT
11217 /* Compilation units have a DW_AT_name that is a filename, not
11218 a source language identifier. */
11219 case DW_TAG_enumeration_type:
11220 case DW_TAG_enumerator:
11221 /* These tags always have simple identifiers already; no need
11222 to canonicalize them. */
11223 part_die->name = DW_STRING (&attr);
11224 break;
11225 default:
11226 part_die->name
11227 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 11228 &objfile->objfile_obstack);
71c25dea
TT
11229 break;
11230 }
c906108c 11231 break;
31ef98ae 11232 case DW_AT_linkage_name:
c906108c 11233 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
11234 /* Note that both forms of linkage name might appear. We
11235 assume they will be the same, and we only store the last
11236 one we see. */
94af9270
KS
11237 if (cu->language == language_ada)
11238 part_die->name = DW_STRING (&attr);
abc72ce4 11239 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
11240 break;
11241 case DW_AT_low_pc:
11242 has_low_pc_attr = 1;
11243 part_die->lowpc = DW_ADDR (&attr);
11244 break;
11245 case DW_AT_high_pc:
11246 has_high_pc_attr = 1;
3019eac3
DE
11247 if (attr.form == DW_FORM_addr
11248 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
11249 part_die->highpc = DW_ADDR (&attr);
11250 else
11251 {
11252 high_pc_relative = 1;
11253 part_die->highpc = DW_UNSND (&attr);
11254 }
c906108c
SS
11255 break;
11256 case DW_AT_location:
0963b4bd 11257 /* Support the .debug_loc offsets. */
8e19ed76
PS
11258 if (attr_form_is_block (&attr))
11259 {
95554aad 11260 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 11261 }
3690dd37 11262 else if (attr_form_is_section_offset (&attr))
8e19ed76 11263 {
4d3c2250 11264 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11265 }
11266 else
11267 {
4d3c2250
KB
11268 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11269 "partial symbol information");
8e19ed76 11270 }
c906108c 11271 break;
c906108c
SS
11272 case DW_AT_external:
11273 part_die->is_external = DW_UNSND (&attr);
11274 break;
11275 case DW_AT_declaration:
11276 part_die->is_declaration = DW_UNSND (&attr);
11277 break;
11278 case DW_AT_type:
11279 part_die->has_type = 1;
11280 break;
11281 case DW_AT_abstract_origin:
11282 case DW_AT_specification:
72bf9492
DJ
11283 case DW_AT_extension:
11284 part_die->has_specification = 1;
c764a876 11285 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
11286 break;
11287 case DW_AT_sibling:
11288 /* Ignore absolute siblings, they might point outside of
11289 the current compile unit. */
11290 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
11291 complaint (&symfile_complaints,
11292 _("ignoring absolute DW_AT_sibling"));
c906108c 11293 else
b64f50a1 11294 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 11295 break;
fa4028e9
JB
11296 case DW_AT_byte_size:
11297 part_die->has_byte_size = 1;
11298 break;
68511cec
CES
11299 case DW_AT_calling_convention:
11300 /* DWARF doesn't provide a way to identify a program's source-level
11301 entry point. DW_AT_calling_convention attributes are only meant
11302 to describe functions' calling conventions.
11303
11304 However, because it's a necessary piece of information in
11305 Fortran, and because DW_CC_program is the only piece of debugging
11306 information whose definition refers to a 'main program' at all,
11307 several compilers have begun marking Fortran main programs with
11308 DW_CC_program --- even when those functions use the standard
11309 calling conventions.
11310
11311 So until DWARF specifies a way to provide this information and
11312 compilers pick up the new representation, we'll support this
11313 practice. */
11314 if (DW_UNSND (&attr) == DW_CC_program
11315 && cu->language == language_fortran)
01f8c46d
JK
11316 {
11317 set_main_name (part_die->name);
11318
11319 /* As this DIE has a static linkage the name would be difficult
11320 to look up later. */
11321 language_of_main = language_fortran;
11322 }
68511cec 11323 break;
481860b3
GB
11324 case DW_AT_inline:
11325 if (DW_UNSND (&attr) == DW_INL_inlined
11326 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11327 part_die->may_be_inlined = 1;
11328 break;
95554aad
TT
11329
11330 case DW_AT_import:
11331 if (part_die->tag == DW_TAG_imported_unit)
11332 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11333 break;
11334
c906108c
SS
11335 default:
11336 break;
11337 }
11338 }
11339
91da1414
MW
11340 if (high_pc_relative)
11341 part_die->highpc += part_die->lowpc;
11342
9373cf26
JK
11343 if (has_low_pc_attr && has_high_pc_attr)
11344 {
11345 /* When using the GNU linker, .gnu.linkonce. sections are used to
11346 eliminate duplicate copies of functions and vtables and such.
11347 The linker will arbitrarily choose one and discard the others.
11348 The AT_*_pc values for such functions refer to local labels in
11349 these sections. If the section from that file was discarded, the
11350 labels are not in the output, so the relocs get a value of 0.
11351 If this is a discarded function, mark the pc bounds as invalid,
11352 so that GDB will ignore it. */
11353 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11354 {
bb5ed363 11355 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11356
11357 complaint (&symfile_complaints,
11358 _("DW_AT_low_pc %s is zero "
11359 "for DIE at 0x%x [in module %s]"),
11360 paddress (gdbarch, part_die->lowpc),
b64f50a1 11361 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11362 }
11363 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11364 else if (part_die->lowpc >= part_die->highpc)
11365 {
bb5ed363 11366 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11367
11368 complaint (&symfile_complaints,
11369 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11370 "for DIE at 0x%x [in module %s]"),
11371 paddress (gdbarch, part_die->lowpc),
11372 paddress (gdbarch, part_die->highpc),
b64f50a1 11373 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11374 }
11375 else
11376 part_die->has_pc_info = 1;
11377 }
85cbf3d3 11378
c906108c
SS
11379 return info_ptr;
11380}
11381
72bf9492
DJ
11382/* Find a cached partial DIE at OFFSET in CU. */
11383
11384static struct partial_die_info *
b64f50a1 11385find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
11386{
11387 struct partial_die_info *lookup_die = NULL;
11388 struct partial_die_info part_die;
11389
11390 part_die.offset = offset;
b64f50a1
JK
11391 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11392 offset.sect_off);
72bf9492 11393
72bf9492
DJ
11394 return lookup_die;
11395}
11396
348e048f
DE
11397/* Find a partial DIE at OFFSET, which may or may not be in CU,
11398 except in the case of .debug_types DIEs which do not reference
11399 outside their CU (they do however referencing other types via
55f1336d 11400 DW_FORM_ref_sig8). */
72bf9492
DJ
11401
11402static struct partial_die_info *
b64f50a1 11403find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
72bf9492 11404{
bb5ed363 11405 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
11406 struct dwarf2_per_cu_data *per_cu = NULL;
11407 struct partial_die_info *pd = NULL;
72bf9492 11408
45452591 11409 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
11410 {
11411 pd = find_partial_die_in_comp_unit (offset, cu);
11412 if (pd != NULL)
11413 return pd;
0d99eb77
DE
11414 /* We missed recording what we needed.
11415 Load all dies and try again. */
11416 per_cu = cu->per_cu;
5afb4e99 11417 }
0d99eb77
DE
11418 else
11419 {
11420 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 11421 if (cu->per_cu->is_debug_types)
0d99eb77
DE
11422 {
11423 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11424 " external reference to offset 0x%lx [in module %s].\n"),
11425 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11426 bfd_get_filename (objfile->obfd));
11427 }
11428 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
72bf9492 11429
0d99eb77
DE
11430 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11431 load_partial_comp_unit (per_cu);
ae038cb0 11432
0d99eb77
DE
11433 per_cu->cu->last_used = 0;
11434 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11435 }
5afb4e99 11436
dee91e82
DE
11437 /* If we didn't find it, and not all dies have been loaded,
11438 load them all and try again. */
11439
5afb4e99
DJ
11440 if (pd == NULL && per_cu->load_all_dies == 0)
11441 {
5afb4e99 11442 per_cu->load_all_dies = 1;
fd820528
DE
11443
11444 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11445 THIS_CU->cu may already be in use. So we can't just free it and
11446 replace its DIEs with the ones we read in. Instead, we leave those
11447 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11448 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11449 set. */
dee91e82 11450 load_partial_comp_unit (per_cu);
5afb4e99
DJ
11451
11452 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11453 }
11454
11455 if (pd == NULL)
11456 internal_error (__FILE__, __LINE__,
3e43a32a
MS
11457 _("could not find partial DIE 0x%x "
11458 "in cache [from module %s]\n"),
b64f50a1 11459 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 11460 return pd;
72bf9492
DJ
11461}
11462
abc72ce4
DE
11463/* See if we can figure out if the class lives in a namespace. We do
11464 this by looking for a member function; its demangled name will
11465 contain namespace info, if there is any. */
11466
11467static void
11468guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11469 struct dwarf2_cu *cu)
11470{
11471 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11472 what template types look like, because the demangler
11473 frequently doesn't give the same name as the debug info. We
11474 could fix this by only using the demangled name to get the
11475 prefix (but see comment in read_structure_type). */
11476
11477 struct partial_die_info *real_pdi;
11478 struct partial_die_info *child_pdi;
11479
11480 /* If this DIE (this DIE's specification, if any) has a parent, then
11481 we should not do this. We'll prepend the parent's fully qualified
11482 name when we create the partial symbol. */
11483
11484 real_pdi = struct_pdi;
11485 while (real_pdi->has_specification)
11486 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11487
11488 if (real_pdi->die_parent != NULL)
11489 return;
11490
11491 for (child_pdi = struct_pdi->die_child;
11492 child_pdi != NULL;
11493 child_pdi = child_pdi->die_sibling)
11494 {
11495 if (child_pdi->tag == DW_TAG_subprogram
11496 && child_pdi->linkage_name != NULL)
11497 {
11498 char *actual_class_name
11499 = language_class_name_from_physname (cu->language_defn,
11500 child_pdi->linkage_name);
11501 if (actual_class_name != NULL)
11502 {
11503 struct_pdi->name
11504 = obsavestring (actual_class_name,
11505 strlen (actual_class_name),
11506 &cu->objfile->objfile_obstack);
11507 xfree (actual_class_name);
11508 }
11509 break;
11510 }
11511 }
11512}
11513
72bf9492
DJ
11514/* Adjust PART_DIE before generating a symbol for it. This function
11515 may set the is_external flag or change the DIE's name. */
11516
11517static void
11518fixup_partial_die (struct partial_die_info *part_die,
11519 struct dwarf2_cu *cu)
11520{
abc72ce4
DE
11521 /* Once we've fixed up a die, there's no point in doing so again.
11522 This also avoids a memory leak if we were to call
11523 guess_partial_die_structure_name multiple times. */
11524 if (part_die->fixup_called)
11525 return;
11526
72bf9492
DJ
11527 /* If we found a reference attribute and the DIE has no name, try
11528 to find a name in the referred to DIE. */
11529
11530 if (part_die->name == NULL && part_die->has_specification)
11531 {
11532 struct partial_die_info *spec_die;
72bf9492 11533
10b3939b 11534 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 11535
10b3939b 11536 fixup_partial_die (spec_die, cu);
72bf9492
DJ
11537
11538 if (spec_die->name)
11539 {
11540 part_die->name = spec_die->name;
11541
11542 /* Copy DW_AT_external attribute if it is set. */
11543 if (spec_die->is_external)
11544 part_die->is_external = spec_die->is_external;
11545 }
11546 }
11547
11548 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
11549
11550 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 11551 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 11552
abc72ce4
DE
11553 /* If there is no parent die to provide a namespace, and there are
11554 children, see if we can determine the namespace from their linkage
122d1940 11555 name. */
abc72ce4 11556 if (cu->language == language_cplus
8b70b953 11557 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
11558 && part_die->die_parent == NULL
11559 && part_die->has_children
11560 && (part_die->tag == DW_TAG_class_type
11561 || part_die->tag == DW_TAG_structure_type
11562 || part_die->tag == DW_TAG_union_type))
11563 guess_partial_die_structure_name (part_die, cu);
11564
53832f31
TT
11565 /* GCC might emit a nameless struct or union that has a linkage
11566 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11567 if (part_die->name == NULL
96408a79
SA
11568 && (part_die->tag == DW_TAG_class_type
11569 || part_die->tag == DW_TAG_interface_type
11570 || part_die->tag == DW_TAG_structure_type
11571 || part_die->tag == DW_TAG_union_type)
53832f31
TT
11572 && part_die->linkage_name != NULL)
11573 {
11574 char *demangled;
11575
11576 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11577 if (demangled)
11578 {
96408a79
SA
11579 const char *base;
11580
11581 /* Strip any leading namespaces/classes, keep only the base name.
11582 DW_AT_name for named DIEs does not contain the prefixes. */
11583 base = strrchr (demangled, ':');
11584 if (base && base > demangled && base[-1] == ':')
11585 base++;
11586 else
11587 base = demangled;
11588
11589 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
11590 &cu->objfile->objfile_obstack);
11591 xfree (demangled);
11592 }
11593 }
11594
abc72ce4 11595 part_die->fixup_called = 1;
72bf9492
DJ
11596}
11597
a8329558 11598/* Read an attribute value described by an attribute form. */
c906108c 11599
fe1b8b76 11600static gdb_byte *
dee91e82
DE
11601read_attribute_value (const struct die_reader_specs *reader,
11602 struct attribute *attr, unsigned form,
11603 gdb_byte *info_ptr)
c906108c 11604{
dee91e82
DE
11605 struct dwarf2_cu *cu = reader->cu;
11606 bfd *abfd = reader->abfd;
e7c27a73 11607 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
11608 unsigned int bytes_read;
11609 struct dwarf_block *blk;
11610
a8329558
KW
11611 attr->form = form;
11612 switch (form)
c906108c 11613 {
c906108c 11614 case DW_FORM_ref_addr:
ae411497 11615 if (cu->header.version == 2)
4568ecf9 11616 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 11617 else
4568ecf9
DE
11618 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11619 &cu->header, &bytes_read);
ae411497
TT
11620 info_ptr += bytes_read;
11621 break;
11622 case DW_FORM_addr:
e7c27a73 11623 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 11624 info_ptr += bytes_read;
c906108c
SS
11625 break;
11626 case DW_FORM_block2:
7b5a2f43 11627 blk = dwarf_alloc_block (cu);
c906108c
SS
11628 blk->size = read_2_bytes (abfd, info_ptr);
11629 info_ptr += 2;
11630 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11631 info_ptr += blk->size;
11632 DW_BLOCK (attr) = blk;
11633 break;
11634 case DW_FORM_block4:
7b5a2f43 11635 blk = dwarf_alloc_block (cu);
c906108c
SS
11636 blk->size = read_4_bytes (abfd, info_ptr);
11637 info_ptr += 4;
11638 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11639 info_ptr += blk->size;
11640 DW_BLOCK (attr) = blk;
11641 break;
11642 case DW_FORM_data2:
11643 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11644 info_ptr += 2;
11645 break;
11646 case DW_FORM_data4:
11647 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11648 info_ptr += 4;
11649 break;
11650 case DW_FORM_data8:
11651 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11652 info_ptr += 8;
11653 break;
2dc7f7b3
TT
11654 case DW_FORM_sec_offset:
11655 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11656 info_ptr += bytes_read;
11657 break;
c906108c 11658 case DW_FORM_string:
9b1c24c8 11659 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 11660 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
11661 info_ptr += bytes_read;
11662 break;
4bdf3d34
JJ
11663 case DW_FORM_strp:
11664 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11665 &bytes_read);
8285870a 11666 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
11667 info_ptr += bytes_read;
11668 break;
2dc7f7b3 11669 case DW_FORM_exprloc:
c906108c 11670 case DW_FORM_block:
7b5a2f43 11671 blk = dwarf_alloc_block (cu);
c906108c
SS
11672 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11673 info_ptr += bytes_read;
11674 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11675 info_ptr += blk->size;
11676 DW_BLOCK (attr) = blk;
11677 break;
11678 case DW_FORM_block1:
7b5a2f43 11679 blk = dwarf_alloc_block (cu);
c906108c
SS
11680 blk->size = read_1_byte (abfd, info_ptr);
11681 info_ptr += 1;
11682 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11683 info_ptr += blk->size;
11684 DW_BLOCK (attr) = blk;
11685 break;
11686 case DW_FORM_data1:
11687 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11688 info_ptr += 1;
11689 break;
11690 case DW_FORM_flag:
11691 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11692 info_ptr += 1;
11693 break;
2dc7f7b3
TT
11694 case DW_FORM_flag_present:
11695 DW_UNSND (attr) = 1;
11696 break;
c906108c
SS
11697 case DW_FORM_sdata:
11698 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11699 info_ptr += bytes_read;
11700 break;
11701 case DW_FORM_udata:
11702 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11703 info_ptr += bytes_read;
11704 break;
11705 case DW_FORM_ref1:
4568ecf9
DE
11706 DW_UNSND (attr) = (cu->header.offset.sect_off
11707 + read_1_byte (abfd, info_ptr));
c906108c
SS
11708 info_ptr += 1;
11709 break;
11710 case DW_FORM_ref2:
4568ecf9
DE
11711 DW_UNSND (attr) = (cu->header.offset.sect_off
11712 + read_2_bytes (abfd, info_ptr));
c906108c
SS
11713 info_ptr += 2;
11714 break;
11715 case DW_FORM_ref4:
4568ecf9
DE
11716 DW_UNSND (attr) = (cu->header.offset.sect_off
11717 + read_4_bytes (abfd, info_ptr));
c906108c
SS
11718 info_ptr += 4;
11719 break;
613e1657 11720 case DW_FORM_ref8:
4568ecf9
DE
11721 DW_UNSND (attr) = (cu->header.offset.sect_off
11722 + read_8_bytes (abfd, info_ptr));
613e1657
KB
11723 info_ptr += 8;
11724 break;
55f1336d 11725 case DW_FORM_ref_sig8:
348e048f
DE
11726 /* Convert the signature to something we can record in DW_UNSND
11727 for later lookup.
11728 NOTE: This is NULL if the type wasn't found. */
11729 DW_SIGNATURED_TYPE (attr) =
e319fa28 11730 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
11731 info_ptr += 8;
11732 break;
c906108c 11733 case DW_FORM_ref_udata:
4568ecf9
DE
11734 DW_UNSND (attr) = (cu->header.offset.sect_off
11735 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
11736 info_ptr += bytes_read;
11737 break;
c906108c 11738 case DW_FORM_indirect:
a8329558
KW
11739 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11740 info_ptr += bytes_read;
dee91e82 11741 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 11742 break;
3019eac3
DE
11743 case DW_FORM_GNU_addr_index:
11744 if (reader->dwo_file == NULL)
11745 {
11746 /* For now flag a hard error.
11747 Later we can turn this into a complaint. */
11748 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11749 dwarf_form_name (form),
11750 bfd_get_filename (abfd));
11751 }
11752 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11753 info_ptr += bytes_read;
11754 break;
11755 case DW_FORM_GNU_str_index:
11756 if (reader->dwo_file == NULL)
11757 {
11758 /* For now flag a hard error.
11759 Later we can turn this into a complaint if warranted. */
11760 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11761 dwarf_form_name (form),
11762 bfd_get_filename (abfd));
11763 }
11764 {
11765 ULONGEST str_index =
11766 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11767
11768 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11769 DW_STRING_IS_CANONICAL (attr) = 0;
11770 info_ptr += bytes_read;
11771 }
11772 break;
c906108c 11773 default:
8a3fe4f8 11774 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
11775 dwarf_form_name (form),
11776 bfd_get_filename (abfd));
c906108c 11777 }
28e94949
JB
11778
11779 /* We have seen instances where the compiler tried to emit a byte
11780 size attribute of -1 which ended up being encoded as an unsigned
11781 0xffffffff. Although 0xffffffff is technically a valid size value,
11782 an object of this size seems pretty unlikely so we can relatively
11783 safely treat these cases as if the size attribute was invalid and
11784 treat them as zero by default. */
11785 if (attr->name == DW_AT_byte_size
11786 && form == DW_FORM_data4
11787 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
11788 {
11789 complaint
11790 (&symfile_complaints,
43bbcdc2
PH
11791 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
11792 hex_string (DW_UNSND (attr)));
01c66ae6
JB
11793 DW_UNSND (attr) = 0;
11794 }
28e94949 11795
c906108c
SS
11796 return info_ptr;
11797}
11798
a8329558
KW
11799/* Read an attribute described by an abbreviated attribute. */
11800
fe1b8b76 11801static gdb_byte *
dee91e82
DE
11802read_attribute (const struct die_reader_specs *reader,
11803 struct attribute *attr, struct attr_abbrev *abbrev,
11804 gdb_byte *info_ptr)
a8329558
KW
11805{
11806 attr->name = abbrev->name;
dee91e82 11807 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
11808}
11809
0963b4bd 11810/* Read dwarf information from a buffer. */
c906108c
SS
11811
11812static unsigned int
fe1b8b76 11813read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 11814{
fe1b8b76 11815 return bfd_get_8 (abfd, buf);
c906108c
SS
11816}
11817
11818static int
fe1b8b76 11819read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 11820{
fe1b8b76 11821 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
11822}
11823
11824static unsigned int
fe1b8b76 11825read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11826{
fe1b8b76 11827 return bfd_get_16 (abfd, buf);
c906108c
SS
11828}
11829
21ae7a4d
JK
11830static int
11831read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
11832{
11833 return bfd_get_signed_16 (abfd, buf);
11834}
11835
c906108c 11836static unsigned int
fe1b8b76 11837read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11838{
fe1b8b76 11839 return bfd_get_32 (abfd, buf);
c906108c
SS
11840}
11841
21ae7a4d
JK
11842static int
11843read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
11844{
11845 return bfd_get_signed_32 (abfd, buf);
11846}
11847
93311388 11848static ULONGEST
fe1b8b76 11849read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11850{
fe1b8b76 11851 return bfd_get_64 (abfd, buf);
c906108c
SS
11852}
11853
11854static CORE_ADDR
fe1b8b76 11855read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 11856 unsigned int *bytes_read)
c906108c 11857{
e7c27a73 11858 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
11859 CORE_ADDR retval = 0;
11860
107d2387 11861 if (cu_header->signed_addr_p)
c906108c 11862 {
107d2387
AC
11863 switch (cu_header->addr_size)
11864 {
11865 case 2:
fe1b8b76 11866 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
11867 break;
11868 case 4:
fe1b8b76 11869 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
11870 break;
11871 case 8:
fe1b8b76 11872 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
11873 break;
11874 default:
8e65ff28 11875 internal_error (__FILE__, __LINE__,
e2e0b3e5 11876 _("read_address: bad switch, signed [in module %s]"),
659b0389 11877 bfd_get_filename (abfd));
107d2387
AC
11878 }
11879 }
11880 else
11881 {
11882 switch (cu_header->addr_size)
11883 {
11884 case 2:
fe1b8b76 11885 retval = bfd_get_16 (abfd, buf);
107d2387
AC
11886 break;
11887 case 4:
fe1b8b76 11888 retval = bfd_get_32 (abfd, buf);
107d2387
AC
11889 break;
11890 case 8:
fe1b8b76 11891 retval = bfd_get_64 (abfd, buf);
107d2387
AC
11892 break;
11893 default:
8e65ff28 11894 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
11895 _("read_address: bad switch, "
11896 "unsigned [in module %s]"),
659b0389 11897 bfd_get_filename (abfd));
107d2387 11898 }
c906108c 11899 }
64367e0a 11900
107d2387
AC
11901 *bytes_read = cu_header->addr_size;
11902 return retval;
c906108c
SS
11903}
11904
f7ef9339 11905/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
11906 specification allows the initial length to take up either 4 bytes
11907 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
11908 bytes describe the length and all offsets will be 8 bytes in length
11909 instead of 4.
11910
f7ef9339
KB
11911 An older, non-standard 64-bit format is also handled by this
11912 function. The older format in question stores the initial length
11913 as an 8-byte quantity without an escape value. Lengths greater
11914 than 2^32 aren't very common which means that the initial 4 bytes
11915 is almost always zero. Since a length value of zero doesn't make
11916 sense for the 32-bit format, this initial zero can be considered to
11917 be an escape value which indicates the presence of the older 64-bit
11918 format. As written, the code can't detect (old format) lengths
917c78fc
MK
11919 greater than 4GB. If it becomes necessary to handle lengths
11920 somewhat larger than 4GB, we could allow other small values (such
11921 as the non-sensical values of 1, 2, and 3) to also be used as
11922 escape values indicating the presence of the old format.
f7ef9339 11923
917c78fc
MK
11924 The value returned via bytes_read should be used to increment the
11925 relevant pointer after calling read_initial_length().
c764a876 11926
613e1657
KB
11927 [ Note: read_initial_length() and read_offset() are based on the
11928 document entitled "DWARF Debugging Information Format", revision
f7ef9339 11929 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
11930 from:
11931
f7ef9339 11932 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 11933
613e1657
KB
11934 This document is only a draft and is subject to change. (So beware.)
11935
f7ef9339 11936 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
11937 determined empirically by examining 64-bit ELF files produced by
11938 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
11939
11940 - Kevin, July 16, 2002
613e1657
KB
11941 ] */
11942
11943static LONGEST
c764a876 11944read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 11945{
fe1b8b76 11946 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 11947
dd373385 11948 if (length == 0xffffffff)
613e1657 11949 {
fe1b8b76 11950 length = bfd_get_64 (abfd, buf + 4);
613e1657 11951 *bytes_read = 12;
613e1657 11952 }
dd373385 11953 else if (length == 0)
f7ef9339 11954 {
dd373385 11955 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 11956 length = bfd_get_64 (abfd, buf);
f7ef9339 11957 *bytes_read = 8;
f7ef9339 11958 }
613e1657
KB
11959 else
11960 {
11961 *bytes_read = 4;
613e1657
KB
11962 }
11963
c764a876
DE
11964 return length;
11965}
dd373385 11966
c764a876
DE
11967/* Cover function for read_initial_length.
11968 Returns the length of the object at BUF, and stores the size of the
11969 initial length in *BYTES_READ and stores the size that offsets will be in
11970 *OFFSET_SIZE.
11971 If the initial length size is not equivalent to that specified in
11972 CU_HEADER then issue a complaint.
11973 This is useful when reading non-comp-unit headers. */
dd373385 11974
c764a876
DE
11975static LONGEST
11976read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
11977 const struct comp_unit_head *cu_header,
11978 unsigned int *bytes_read,
11979 unsigned int *offset_size)
11980{
11981 LONGEST length = read_initial_length (abfd, buf, bytes_read);
11982
11983 gdb_assert (cu_header->initial_length_size == 4
11984 || cu_header->initial_length_size == 8
11985 || cu_header->initial_length_size == 12);
11986
11987 if (cu_header->initial_length_size != *bytes_read)
11988 complaint (&symfile_complaints,
11989 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 11990
c764a876 11991 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 11992 return length;
613e1657
KB
11993}
11994
11995/* Read an offset from the data stream. The size of the offset is
917c78fc 11996 given by cu_header->offset_size. */
613e1657
KB
11997
11998static LONGEST
fe1b8b76 11999read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 12000 unsigned int *bytes_read)
c764a876
DE
12001{
12002 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 12003
c764a876
DE
12004 *bytes_read = cu_header->offset_size;
12005 return offset;
12006}
12007
12008/* Read an offset from the data stream. */
12009
12010static LONGEST
12011read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
12012{
12013 LONGEST retval = 0;
12014
c764a876 12015 switch (offset_size)
613e1657
KB
12016 {
12017 case 4:
fe1b8b76 12018 retval = bfd_get_32 (abfd, buf);
613e1657
KB
12019 break;
12020 case 8:
fe1b8b76 12021 retval = bfd_get_64 (abfd, buf);
613e1657
KB
12022 break;
12023 default:
8e65ff28 12024 internal_error (__FILE__, __LINE__,
c764a876 12025 _("read_offset_1: bad switch [in module %s]"),
659b0389 12026 bfd_get_filename (abfd));
613e1657
KB
12027 }
12028
917c78fc 12029 return retval;
613e1657
KB
12030}
12031
fe1b8b76
JB
12032static gdb_byte *
12033read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
12034{
12035 /* If the size of a host char is 8 bits, we can return a pointer
12036 to the buffer, otherwise we have to copy the data to a buffer
12037 allocated on the temporary obstack. */
4bdf3d34 12038 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 12039 return buf;
c906108c
SS
12040}
12041
12042static char *
9b1c24c8 12043read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
12044{
12045 /* If the size of a host char is 8 bits, we can return a pointer
12046 to the string, otherwise we have to copy the string to a buffer
12047 allocated on the temporary obstack. */
4bdf3d34 12048 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
12049 if (*buf == '\0')
12050 {
12051 *bytes_read_ptr = 1;
12052 return NULL;
12053 }
fe1b8b76
JB
12054 *bytes_read_ptr = strlen ((char *) buf) + 1;
12055 return (char *) buf;
4bdf3d34
JJ
12056}
12057
12058static char *
cf2c3c16 12059read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 12060{
be391dca 12061 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 12062 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
12063 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12064 bfd_get_filename (abfd));
dce234bc 12065 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
12066 error (_("DW_FORM_strp pointing outside of "
12067 ".debug_str section [in module %s]"),
12068 bfd_get_filename (abfd));
4bdf3d34 12069 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 12070 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 12071 return NULL;
dce234bc 12072 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
12073}
12074
cf2c3c16
TT
12075static char *
12076read_indirect_string (bfd *abfd, gdb_byte *buf,
12077 const struct comp_unit_head *cu_header,
12078 unsigned int *bytes_read_ptr)
12079{
12080 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12081
12082 return read_indirect_string_at_offset (abfd, str_offset);
12083}
12084
12df843f 12085static ULONGEST
fe1b8b76 12086read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12087{
12df843f 12088 ULONGEST result;
ce5d95e1 12089 unsigned int num_read;
c906108c
SS
12090 int i, shift;
12091 unsigned char byte;
12092
12093 result = 0;
12094 shift = 0;
12095 num_read = 0;
12096 i = 0;
12097 while (1)
12098 {
fe1b8b76 12099 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12100 buf++;
12101 num_read++;
12df843f 12102 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
12103 if ((byte & 128) == 0)
12104 {
12105 break;
12106 }
12107 shift += 7;
12108 }
12109 *bytes_read_ptr = num_read;
12110 return result;
12111}
12112
12df843f 12113static LONGEST
fe1b8b76 12114read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12115{
12df843f 12116 LONGEST result;
77e0b926 12117 int i, shift, num_read;
c906108c
SS
12118 unsigned char byte;
12119
12120 result = 0;
12121 shift = 0;
c906108c
SS
12122 num_read = 0;
12123 i = 0;
12124 while (1)
12125 {
fe1b8b76 12126 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12127 buf++;
12128 num_read++;
12df843f 12129 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
12130 shift += 7;
12131 if ((byte & 128) == 0)
12132 {
12133 break;
12134 }
12135 }
77e0b926 12136 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 12137 result |= -(((LONGEST) 1) << shift);
c906108c
SS
12138 *bytes_read_ptr = num_read;
12139 return result;
12140}
12141
3019eac3
DE
12142/* Given index ADDR_INDEX in .debug_addr, fetch the value.
12143 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12144 ADDR_SIZE is the size of addresses from the CU header. */
12145
12146static CORE_ADDR
12147read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12148{
12149 struct objfile *objfile = dwarf2_per_objfile->objfile;
12150 bfd *abfd = objfile->obfd;
12151 const gdb_byte *info_ptr;
12152
12153 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12154 if (dwarf2_per_objfile->addr.buffer == NULL)
12155 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12156 objfile->name);
12157 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12158 error (_("DW_FORM_addr_index pointing outside of "
12159 ".debug_addr section [in module %s]"),
12160 objfile->name);
12161 info_ptr = (dwarf2_per_objfile->addr.buffer
12162 + addr_base + addr_index * addr_size);
12163 if (addr_size == 4)
12164 return bfd_get_32 (abfd, info_ptr);
12165 else
12166 return bfd_get_64 (abfd, info_ptr);
12167}
12168
12169/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12170
12171static CORE_ADDR
12172read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12173{
12174 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12175}
12176
12177/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12178
12179static CORE_ADDR
12180read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12181 unsigned int *bytes_read)
12182{
12183 bfd *abfd = cu->objfile->obfd;
12184 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12185
12186 return read_addr_index (cu, addr_index);
12187}
12188
12189/* Data structure to pass results from dwarf2_read_addr_index_reader
12190 back to dwarf2_read_addr_index. */
12191
12192struct dwarf2_read_addr_index_data
12193{
12194 ULONGEST addr_base;
12195 int addr_size;
12196};
12197
12198/* die_reader_func for dwarf2_read_addr_index. */
12199
12200static void
12201dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12202 gdb_byte *info_ptr,
12203 struct die_info *comp_unit_die,
12204 int has_children,
12205 void *data)
12206{
12207 struct dwarf2_cu *cu = reader->cu;
12208 struct dwarf2_read_addr_index_data *aidata =
12209 (struct dwarf2_read_addr_index_data *) data;
12210
12211 aidata->addr_base = cu->addr_base;
12212 aidata->addr_size = cu->header.addr_size;
12213}
12214
12215/* Given an index in .debug_addr, fetch the value.
12216 NOTE: This can be called during dwarf expression evaluation,
12217 long after the debug information has been read, and thus per_cu->cu
12218 may no longer exist. */
12219
12220CORE_ADDR
12221dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12222 unsigned int addr_index)
12223{
12224 struct objfile *objfile = per_cu->objfile;
12225 struct dwarf2_cu *cu = per_cu->cu;
12226 ULONGEST addr_base;
12227 int addr_size;
12228
12229 /* This is intended to be called from outside this file. */
12230 dw2_setup (objfile);
12231
12232 /* We need addr_base and addr_size.
12233 If we don't have PER_CU->cu, we have to get it.
12234 Nasty, but the alternative is storing the needed info in PER_CU,
12235 which at this point doesn't seem justified: it's not clear how frequently
12236 it would get used and it would increase the size of every PER_CU.
12237 Entry points like dwarf2_per_cu_addr_size do a similar thing
12238 so we're not in uncharted territory here.
12239 Alas we need to be a bit more complicated as addr_base is contained
12240 in the DIE.
12241
12242 We don't need to read the entire CU(/TU).
12243 We just need the header and top level die.
12244 IWBN to use the aging mechanism to let us lazily later discard the CU.
12245 See however init_cutu_and_read_dies_simple. */
12246
12247 if (cu != NULL)
12248 {
12249 addr_base = cu->addr_base;
12250 addr_size = cu->header.addr_size;
12251 }
12252 else
12253 {
12254 struct dwarf2_read_addr_index_data aidata;
12255
12256 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12257 &aidata);
12258 addr_base = aidata.addr_base;
12259 addr_size = aidata.addr_size;
12260 }
12261
12262 return read_addr_index_1 (addr_index, addr_base, addr_size);
12263}
12264
12265/* Given a DW_AT_str_index, fetch the string. */
12266
12267static char *
12268read_str_index (const struct die_reader_specs *reader,
12269 struct dwarf2_cu *cu, ULONGEST str_index)
12270{
12271 struct objfile *objfile = dwarf2_per_objfile->objfile;
12272 const char *dwo_name = objfile->name;
12273 bfd *abfd = objfile->obfd;
12274 struct dwo_sections *sections = &reader->dwo_file->sections;
12275 gdb_byte *info_ptr;
12276 ULONGEST str_offset;
12277
12278 dwarf2_read_section (objfile, &sections->str);
12279 dwarf2_read_section (objfile, &sections->str_offsets);
12280 if (sections->str.buffer == NULL)
12281 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12282 " in CU at offset 0x%lx [in module %s]"),
12283 (long) cu->header.offset.sect_off, dwo_name);
12284 if (sections->str_offsets.buffer == NULL)
12285 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12286 " in CU at offset 0x%lx [in module %s]"),
12287 (long) cu->header.offset.sect_off, dwo_name);
12288 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12289 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12290 " section in CU at offset 0x%lx [in module %s]"),
12291 (long) cu->header.offset.sect_off, dwo_name);
12292 info_ptr = (sections->str_offsets.buffer
12293 + str_index * cu->header.offset_size);
12294 if (cu->header.offset_size == 4)
12295 str_offset = bfd_get_32 (abfd, info_ptr);
12296 else
12297 str_offset = bfd_get_64 (abfd, info_ptr);
12298 if (str_offset >= sections->str.size)
12299 error (_("Offset from DW_FORM_str_index pointing outside of"
12300 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12301 (long) cu->header.offset.sect_off, dwo_name);
12302 return (char *) (sections->str.buffer + str_offset);
12303}
12304
3019eac3
DE
12305/* Return the length of an LEB128 number in BUF. */
12306
12307static int
12308leb128_size (const gdb_byte *buf)
12309{
12310 const gdb_byte *begin = buf;
12311 gdb_byte byte;
12312
12313 while (1)
12314 {
12315 byte = *buf++;
12316 if ((byte & 128) == 0)
12317 return buf - begin;
12318 }
12319}
12320
c906108c 12321static void
e142c38c 12322set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
12323{
12324 switch (lang)
12325 {
12326 case DW_LANG_C89:
76bee0cc 12327 case DW_LANG_C99:
c906108c 12328 case DW_LANG_C:
e142c38c 12329 cu->language = language_c;
c906108c
SS
12330 break;
12331 case DW_LANG_C_plus_plus:
e142c38c 12332 cu->language = language_cplus;
c906108c 12333 break;
6aecb9c2
JB
12334 case DW_LANG_D:
12335 cu->language = language_d;
12336 break;
c906108c
SS
12337 case DW_LANG_Fortran77:
12338 case DW_LANG_Fortran90:
b21b22e0 12339 case DW_LANG_Fortran95:
e142c38c 12340 cu->language = language_fortran;
c906108c 12341 break;
a766d390
DE
12342 case DW_LANG_Go:
12343 cu->language = language_go;
12344 break;
c906108c 12345 case DW_LANG_Mips_Assembler:
e142c38c 12346 cu->language = language_asm;
c906108c 12347 break;
bebd888e 12348 case DW_LANG_Java:
e142c38c 12349 cu->language = language_java;
bebd888e 12350 break;
c906108c 12351 case DW_LANG_Ada83:
8aaf0b47 12352 case DW_LANG_Ada95:
bc5f45f8
JB
12353 cu->language = language_ada;
12354 break;
72019c9c
GM
12355 case DW_LANG_Modula2:
12356 cu->language = language_m2;
12357 break;
fe8e67fd
PM
12358 case DW_LANG_Pascal83:
12359 cu->language = language_pascal;
12360 break;
22566fbd
DJ
12361 case DW_LANG_ObjC:
12362 cu->language = language_objc;
12363 break;
c906108c
SS
12364 case DW_LANG_Cobol74:
12365 case DW_LANG_Cobol85:
c906108c 12366 default:
e142c38c 12367 cu->language = language_minimal;
c906108c
SS
12368 break;
12369 }
e142c38c 12370 cu->language_defn = language_def (cu->language);
c906108c
SS
12371}
12372
12373/* Return the named attribute or NULL if not there. */
12374
12375static struct attribute *
e142c38c 12376dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 12377{
a48e046c 12378 for (;;)
c906108c 12379 {
a48e046c
TT
12380 unsigned int i;
12381 struct attribute *spec = NULL;
12382
12383 for (i = 0; i < die->num_attrs; ++i)
12384 {
12385 if (die->attrs[i].name == name)
12386 return &die->attrs[i];
12387 if (die->attrs[i].name == DW_AT_specification
12388 || die->attrs[i].name == DW_AT_abstract_origin)
12389 spec = &die->attrs[i];
12390 }
12391
12392 if (!spec)
12393 break;
c906108c 12394
f2f0e013 12395 die = follow_die_ref (die, spec, &cu);
f2f0e013 12396 }
c5aa993b 12397
c906108c
SS
12398 return NULL;
12399}
12400
348e048f
DE
12401/* Return the named attribute or NULL if not there,
12402 but do not follow DW_AT_specification, etc.
12403 This is for use in contexts where we're reading .debug_types dies.
12404 Following DW_AT_specification, DW_AT_abstract_origin will take us
12405 back up the chain, and we want to go down. */
12406
12407static struct attribute *
12408dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12409 struct dwarf2_cu *cu)
12410{
12411 unsigned int i;
12412
12413 for (i = 0; i < die->num_attrs; ++i)
12414 if (die->attrs[i].name == name)
12415 return &die->attrs[i];
12416
12417 return NULL;
12418}
12419
05cf31d1
JB
12420/* Return non-zero iff the attribute NAME is defined for the given DIE,
12421 and holds a non-zero value. This function should only be used for
2dc7f7b3 12422 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
12423
12424static int
12425dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12426{
12427 struct attribute *attr = dwarf2_attr (die, name, cu);
12428
12429 return (attr && DW_UNSND (attr));
12430}
12431
3ca72b44 12432static int
e142c38c 12433die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 12434{
05cf31d1
JB
12435 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12436 which value is non-zero. However, we have to be careful with
12437 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12438 (via dwarf2_flag_true_p) follows this attribute. So we may
12439 end up accidently finding a declaration attribute that belongs
12440 to a different DIE referenced by the specification attribute,
12441 even though the given DIE does not have a declaration attribute. */
12442 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12443 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
12444}
12445
63d06c5c 12446/* Return the die giving the specification for DIE, if there is
f2f0e013 12447 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
12448 containing the return value on output. If there is no
12449 specification, but there is an abstract origin, that is
12450 returned. */
63d06c5c
DC
12451
12452static struct die_info *
f2f0e013 12453die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 12454{
f2f0e013
DJ
12455 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12456 *spec_cu);
63d06c5c 12457
edb3359d
DJ
12458 if (spec_attr == NULL)
12459 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12460
63d06c5c
DC
12461 if (spec_attr == NULL)
12462 return NULL;
12463 else
f2f0e013 12464 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 12465}
c906108c 12466
debd256d 12467/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
12468 refers to.
12469 NOTE: This is also used as a "cleanup" function. */
12470
debd256d
JB
12471static void
12472free_line_header (struct line_header *lh)
12473{
12474 if (lh->standard_opcode_lengths)
a8bc7b56 12475 xfree (lh->standard_opcode_lengths);
debd256d
JB
12476
12477 /* Remember that all the lh->file_names[i].name pointers are
12478 pointers into debug_line_buffer, and don't need to be freed. */
12479 if (lh->file_names)
a8bc7b56 12480 xfree (lh->file_names);
debd256d
JB
12481
12482 /* Similarly for the include directory names. */
12483 if (lh->include_dirs)
a8bc7b56 12484 xfree (lh->include_dirs);
debd256d 12485
a8bc7b56 12486 xfree (lh);
debd256d
JB
12487}
12488
debd256d 12489/* Add an entry to LH's include directory table. */
ae2de4f8 12490
debd256d
JB
12491static void
12492add_include_dir (struct line_header *lh, char *include_dir)
c906108c 12493{
debd256d
JB
12494 /* Grow the array if necessary. */
12495 if (lh->include_dirs_size == 0)
c5aa993b 12496 {
debd256d
JB
12497 lh->include_dirs_size = 1; /* for testing */
12498 lh->include_dirs = xmalloc (lh->include_dirs_size
12499 * sizeof (*lh->include_dirs));
12500 }
12501 else if (lh->num_include_dirs >= lh->include_dirs_size)
12502 {
12503 lh->include_dirs_size *= 2;
12504 lh->include_dirs = xrealloc (lh->include_dirs,
12505 (lh->include_dirs_size
12506 * sizeof (*lh->include_dirs)));
c5aa993b 12507 }
c906108c 12508
debd256d
JB
12509 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12510}
6e70227d 12511
debd256d 12512/* Add an entry to LH's file name table. */
ae2de4f8 12513
debd256d
JB
12514static void
12515add_file_name (struct line_header *lh,
12516 char *name,
12517 unsigned int dir_index,
12518 unsigned int mod_time,
12519 unsigned int length)
12520{
12521 struct file_entry *fe;
12522
12523 /* Grow the array if necessary. */
12524 if (lh->file_names_size == 0)
12525 {
12526 lh->file_names_size = 1; /* for testing */
12527 lh->file_names = xmalloc (lh->file_names_size
12528 * sizeof (*lh->file_names));
12529 }
12530 else if (lh->num_file_names >= lh->file_names_size)
12531 {
12532 lh->file_names_size *= 2;
12533 lh->file_names = xrealloc (lh->file_names,
12534 (lh->file_names_size
12535 * sizeof (*lh->file_names)));
12536 }
12537
12538 fe = &lh->file_names[lh->num_file_names++];
12539 fe->name = name;
12540 fe->dir_index = dir_index;
12541 fe->mod_time = mod_time;
12542 fe->length = length;
aaa75496 12543 fe->included_p = 0;
cb1df416 12544 fe->symtab = NULL;
debd256d 12545}
6e70227d 12546
debd256d 12547/* Read the statement program header starting at OFFSET in
3019eac3 12548 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 12549 to a struct line_header, allocated using xmalloc.
debd256d
JB
12550
12551 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
12552 the returned object point into the dwarf line section buffer,
12553 and must not be freed. */
ae2de4f8 12554
debd256d 12555static struct line_header *
3019eac3 12556dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
12557{
12558 struct cleanup *back_to;
12559 struct line_header *lh;
fe1b8b76 12560 gdb_byte *line_ptr;
c764a876 12561 unsigned int bytes_read, offset_size;
debd256d
JB
12562 int i;
12563 char *cur_dir, *cur_file;
3019eac3
DE
12564 struct dwarf2_section_info *section;
12565 bfd *abfd;
12566
12567 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12568 DWO file. */
12569 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12570 section = &cu->dwo_unit->dwo_file->sections.line;
12571 else
12572 section = &dwarf2_per_objfile->line;
debd256d 12573
3019eac3
DE
12574 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12575 if (section->buffer == NULL)
debd256d 12576 {
3019eac3
DE
12577 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12578 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12579 else
12580 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
12581 return 0;
12582 }
12583
fceca515
DE
12584 /* We can't do this until we know the section is non-empty.
12585 Only then do we know we have such a section. */
12586 abfd = section->asection->owner;
12587
a738430d
MK
12588 /* Make sure that at least there's room for the total_length field.
12589 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 12590 if (offset + 4 >= section->size)
debd256d 12591 {
4d3c2250 12592 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12593 return 0;
12594 }
12595
12596 lh = xmalloc (sizeof (*lh));
12597 memset (lh, 0, sizeof (*lh));
12598 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12599 (void *) lh);
12600
3019eac3 12601 line_ptr = section->buffer + offset;
debd256d 12602
a738430d 12603 /* Read in the header. */
6e70227d 12604 lh->total_length =
c764a876
DE
12605 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12606 &bytes_read, &offset_size);
debd256d 12607 line_ptr += bytes_read;
3019eac3 12608 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 12609 {
4d3c2250 12610 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12611 return 0;
12612 }
12613 lh->statement_program_end = line_ptr + lh->total_length;
12614 lh->version = read_2_bytes (abfd, line_ptr);
12615 line_ptr += 2;
c764a876
DE
12616 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12617 line_ptr += offset_size;
debd256d
JB
12618 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12619 line_ptr += 1;
2dc7f7b3
TT
12620 if (lh->version >= 4)
12621 {
12622 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12623 line_ptr += 1;
12624 }
12625 else
12626 lh->maximum_ops_per_instruction = 1;
12627
12628 if (lh->maximum_ops_per_instruction == 0)
12629 {
12630 lh->maximum_ops_per_instruction = 1;
12631 complaint (&symfile_complaints,
3e43a32a
MS
12632 _("invalid maximum_ops_per_instruction "
12633 "in `.debug_line' section"));
2dc7f7b3
TT
12634 }
12635
debd256d
JB
12636 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12637 line_ptr += 1;
12638 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12639 line_ptr += 1;
12640 lh->line_range = read_1_byte (abfd, line_ptr);
12641 line_ptr += 1;
12642 lh->opcode_base = read_1_byte (abfd, line_ptr);
12643 line_ptr += 1;
12644 lh->standard_opcode_lengths
fe1b8b76 12645 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
12646
12647 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12648 for (i = 1; i < lh->opcode_base; ++i)
12649 {
12650 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12651 line_ptr += 1;
12652 }
12653
a738430d 12654 /* Read directory table. */
9b1c24c8 12655 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12656 {
12657 line_ptr += bytes_read;
12658 add_include_dir (lh, cur_dir);
12659 }
12660 line_ptr += bytes_read;
12661
a738430d 12662 /* Read file name table. */
9b1c24c8 12663 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12664 {
12665 unsigned int dir_index, mod_time, length;
12666
12667 line_ptr += bytes_read;
12668 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12669 line_ptr += bytes_read;
12670 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12671 line_ptr += bytes_read;
12672 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12673 line_ptr += bytes_read;
12674
12675 add_file_name (lh, cur_file, dir_index, mod_time, length);
12676 }
12677 line_ptr += bytes_read;
6e70227d 12678 lh->statement_program_start = line_ptr;
debd256d 12679
3019eac3 12680 if (line_ptr > (section->buffer + section->size))
4d3c2250 12681 complaint (&symfile_complaints,
3e43a32a
MS
12682 _("line number info header doesn't "
12683 "fit in `.debug_line' section"));
debd256d
JB
12684
12685 discard_cleanups (back_to);
12686 return lh;
12687}
c906108c 12688
c6da4cef
DE
12689/* Subroutine of dwarf_decode_lines to simplify it.
12690 Return the file name of the psymtab for included file FILE_INDEX
12691 in line header LH of PST.
12692 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12693 If space for the result is malloc'd, it will be freed by a cleanup.
12694 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12695
12696static char *
12697psymtab_include_file_name (const struct line_header *lh, int file_index,
12698 const struct partial_symtab *pst,
12699 const char *comp_dir)
12700{
12701 const struct file_entry fe = lh->file_names [file_index];
12702 char *include_name = fe.name;
12703 char *include_name_to_compare = include_name;
12704 char *dir_name = NULL;
72b9f47f
TT
12705 const char *pst_filename;
12706 char *copied_name = NULL;
c6da4cef
DE
12707 int file_is_pst;
12708
12709 if (fe.dir_index)
12710 dir_name = lh->include_dirs[fe.dir_index - 1];
12711
12712 if (!IS_ABSOLUTE_PATH (include_name)
12713 && (dir_name != NULL || comp_dir != NULL))
12714 {
12715 /* Avoid creating a duplicate psymtab for PST.
12716 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12717 Before we do the comparison, however, we need to account
12718 for DIR_NAME and COMP_DIR.
12719 First prepend dir_name (if non-NULL). If we still don't
12720 have an absolute path prepend comp_dir (if non-NULL).
12721 However, the directory we record in the include-file's
12722 psymtab does not contain COMP_DIR (to match the
12723 corresponding symtab(s)).
12724
12725 Example:
12726
12727 bash$ cd /tmp
12728 bash$ gcc -g ./hello.c
12729 include_name = "hello.c"
12730 dir_name = "."
12731 DW_AT_comp_dir = comp_dir = "/tmp"
12732 DW_AT_name = "./hello.c" */
12733
12734 if (dir_name != NULL)
12735 {
12736 include_name = concat (dir_name, SLASH_STRING,
12737 include_name, (char *)NULL);
12738 include_name_to_compare = include_name;
12739 make_cleanup (xfree, include_name);
12740 }
12741 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12742 {
12743 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12744 include_name, (char *)NULL);
12745 }
12746 }
12747
12748 pst_filename = pst->filename;
12749 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12750 {
72b9f47f
TT
12751 copied_name = concat (pst->dirname, SLASH_STRING,
12752 pst_filename, (char *)NULL);
12753 pst_filename = copied_name;
c6da4cef
DE
12754 }
12755
1e3fad37 12756 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
12757
12758 if (include_name_to_compare != include_name)
12759 xfree (include_name_to_compare);
72b9f47f
TT
12760 if (copied_name != NULL)
12761 xfree (copied_name);
c6da4cef
DE
12762
12763 if (file_is_pst)
12764 return NULL;
12765 return include_name;
12766}
12767
c91513d8
PP
12768/* Ignore this record_line request. */
12769
12770static void
12771noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12772{
12773 return;
12774}
12775
f3f5162e
DE
12776/* Subroutine of dwarf_decode_lines to simplify it.
12777 Process the line number information in LH. */
debd256d 12778
c906108c 12779static void
f3f5162e
DE
12780dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
12781 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 12782{
a8c50c1f 12783 gdb_byte *line_ptr, *extended_end;
fe1b8b76 12784 gdb_byte *line_end;
a8c50c1f 12785 unsigned int bytes_read, extended_len;
c906108c 12786 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
12787 CORE_ADDR baseaddr;
12788 struct objfile *objfile = cu->objfile;
f3f5162e 12789 bfd *abfd = objfile->obfd;
fbf65064 12790 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 12791 const int decode_for_pst_p = (pst != NULL);
f3f5162e 12792 struct subfile *last_subfile = NULL;
c91513d8
PP
12793 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
12794 = record_line;
e142c38c
DJ
12795
12796 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 12797
debd256d
JB
12798 line_ptr = lh->statement_program_start;
12799 line_end = lh->statement_program_end;
c906108c
SS
12800
12801 /* Read the statement sequences until there's nothing left. */
12802 while (line_ptr < line_end)
12803 {
12804 /* state machine registers */
12805 CORE_ADDR address = 0;
12806 unsigned int file = 1;
12807 unsigned int line = 1;
12808 unsigned int column = 0;
debd256d 12809 int is_stmt = lh->default_is_stmt;
c906108c
SS
12810 int basic_block = 0;
12811 int end_sequence = 0;
fbf65064 12812 CORE_ADDR addr;
2dc7f7b3 12813 unsigned char op_index = 0;
c906108c 12814
aaa75496 12815 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 12816 {
aaa75496 12817 /* Start a subfile for the current file of the state machine. */
debd256d
JB
12818 /* lh->include_dirs and lh->file_names are 0-based, but the
12819 directory and file name numbers in the statement program
12820 are 1-based. */
12821 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 12822 char *dir = NULL;
a738430d 12823
debd256d
JB
12824 if (fe->dir_index)
12825 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
12826
12827 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
12828 }
12829
a738430d 12830 /* Decode the table. */
c5aa993b 12831 while (!end_sequence)
c906108c
SS
12832 {
12833 op_code = read_1_byte (abfd, line_ptr);
12834 line_ptr += 1;
59205f5a
JB
12835 if (line_ptr > line_end)
12836 {
12837 dwarf2_debug_line_missing_end_sequence_complaint ();
12838 break;
12839 }
9aa1fe7e 12840
debd256d 12841 if (op_code >= lh->opcode_base)
6e70227d 12842 {
a738430d 12843 /* Special operand. */
debd256d 12844 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
12845 address += (((op_index + (adj_opcode / lh->line_range))
12846 / lh->maximum_ops_per_instruction)
12847 * lh->minimum_instruction_length);
12848 op_index = ((op_index + (adj_opcode / lh->line_range))
12849 % lh->maximum_ops_per_instruction);
debd256d 12850 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 12851 if (lh->num_file_names < file || file == 0)
25e43795 12852 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
12853 /* For now we ignore lines not starting on an
12854 instruction boundary. */
12855 else if (op_index == 0)
25e43795
DJ
12856 {
12857 lh->file_names[file - 1].included_p = 1;
ca5f395d 12858 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
12859 {
12860 if (last_subfile != current_subfile)
12861 {
12862 addr = gdbarch_addr_bits_remove (gdbarch, address);
12863 if (last_subfile)
c91513d8 12864 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
12865 last_subfile = current_subfile;
12866 }
25e43795 12867 /* Append row to matrix using current values. */
7019d805 12868 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 12869 (*p_record_line) (current_subfile, line, addr);
366da635 12870 }
25e43795 12871 }
ca5f395d 12872 basic_block = 0;
9aa1fe7e
GK
12873 }
12874 else switch (op_code)
c906108c
SS
12875 {
12876 case DW_LNS_extended_op:
3e43a32a
MS
12877 extended_len = read_unsigned_leb128 (abfd, line_ptr,
12878 &bytes_read);
473b7be6 12879 line_ptr += bytes_read;
a8c50c1f 12880 extended_end = line_ptr + extended_len;
c906108c
SS
12881 extended_op = read_1_byte (abfd, line_ptr);
12882 line_ptr += 1;
12883 switch (extended_op)
12884 {
12885 case DW_LNE_end_sequence:
c91513d8 12886 p_record_line = record_line;
c906108c 12887 end_sequence = 1;
c906108c
SS
12888 break;
12889 case DW_LNE_set_address:
e7c27a73 12890 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
12891
12892 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
12893 {
12894 /* This line table is for a function which has been
12895 GCd by the linker. Ignore it. PR gdb/12528 */
12896
12897 long line_offset
12898 = line_ptr - dwarf2_per_objfile->line.buffer;
12899
12900 complaint (&symfile_complaints,
12901 _(".debug_line address at offset 0x%lx is 0 "
12902 "[in module %s]"),
bb5ed363 12903 line_offset, objfile->name);
c91513d8
PP
12904 p_record_line = noop_record_line;
12905 }
12906
2dc7f7b3 12907 op_index = 0;
107d2387
AC
12908 line_ptr += bytes_read;
12909 address += baseaddr;
c906108c
SS
12910 break;
12911 case DW_LNE_define_file:
debd256d
JB
12912 {
12913 char *cur_file;
12914 unsigned int dir_index, mod_time, length;
6e70227d 12915
3e43a32a
MS
12916 cur_file = read_direct_string (abfd, line_ptr,
12917 &bytes_read);
debd256d
JB
12918 line_ptr += bytes_read;
12919 dir_index =
12920 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12921 line_ptr += bytes_read;
12922 mod_time =
12923 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12924 line_ptr += bytes_read;
12925 length =
12926 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12927 line_ptr += bytes_read;
12928 add_file_name (lh, cur_file, dir_index, mod_time, length);
12929 }
c906108c 12930 break;
d0c6ba3d
CC
12931 case DW_LNE_set_discriminator:
12932 /* The discriminator is not interesting to the debugger;
12933 just ignore it. */
12934 line_ptr = extended_end;
12935 break;
c906108c 12936 default:
4d3c2250 12937 complaint (&symfile_complaints,
e2e0b3e5 12938 _("mangled .debug_line section"));
debd256d 12939 return;
c906108c 12940 }
a8c50c1f
DJ
12941 /* Make sure that we parsed the extended op correctly. If e.g.
12942 we expected a different address size than the producer used,
12943 we may have read the wrong number of bytes. */
12944 if (line_ptr != extended_end)
12945 {
12946 complaint (&symfile_complaints,
12947 _("mangled .debug_line section"));
12948 return;
12949 }
c906108c
SS
12950 break;
12951 case DW_LNS_copy:
59205f5a 12952 if (lh->num_file_names < file || file == 0)
25e43795
DJ
12953 dwarf2_debug_line_missing_file_complaint ();
12954 else
366da635 12955 {
25e43795 12956 lh->file_names[file - 1].included_p = 1;
ca5f395d 12957 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
12958 {
12959 if (last_subfile != current_subfile)
12960 {
12961 addr = gdbarch_addr_bits_remove (gdbarch, address);
12962 if (last_subfile)
c91513d8 12963 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
12964 last_subfile = current_subfile;
12965 }
7019d805 12966 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 12967 (*p_record_line) (current_subfile, line, addr);
fbf65064 12968 }
366da635 12969 }
c906108c
SS
12970 basic_block = 0;
12971 break;
12972 case DW_LNS_advance_pc:
2dc7f7b3
TT
12973 {
12974 CORE_ADDR adjust
12975 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12976
12977 address += (((op_index + adjust)
12978 / lh->maximum_ops_per_instruction)
12979 * lh->minimum_instruction_length);
12980 op_index = ((op_index + adjust)
12981 % lh->maximum_ops_per_instruction);
12982 line_ptr += bytes_read;
12983 }
c906108c
SS
12984 break;
12985 case DW_LNS_advance_line:
12986 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
12987 line_ptr += bytes_read;
12988 break;
12989 case DW_LNS_set_file:
debd256d 12990 {
a738430d
MK
12991 /* The arrays lh->include_dirs and lh->file_names are
12992 0-based, but the directory and file name numbers in
12993 the statement program are 1-based. */
debd256d 12994 struct file_entry *fe;
4f1520fb 12995 char *dir = NULL;
a738430d 12996
debd256d
JB
12997 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12998 line_ptr += bytes_read;
59205f5a 12999 if (lh->num_file_names < file || file == 0)
25e43795
DJ
13000 dwarf2_debug_line_missing_file_complaint ();
13001 else
13002 {
13003 fe = &lh->file_names[file - 1];
13004 if (fe->dir_index)
13005 dir = lh->include_dirs[fe->dir_index - 1];
13006 if (!decode_for_pst_p)
13007 {
13008 last_subfile = current_subfile;
13009 dwarf2_start_subfile (fe->name, dir, comp_dir);
13010 }
13011 }
debd256d 13012 }
c906108c
SS
13013 break;
13014 case DW_LNS_set_column:
13015 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13016 line_ptr += bytes_read;
13017 break;
13018 case DW_LNS_negate_stmt:
13019 is_stmt = (!is_stmt);
13020 break;
13021 case DW_LNS_set_basic_block:
13022 basic_block = 1;
13023 break;
c2c6d25f
JM
13024 /* Add to the address register of the state machine the
13025 address increment value corresponding to special opcode
a738430d
MK
13026 255. I.e., this value is scaled by the minimum
13027 instruction length since special opcode 255 would have
b021a221 13028 scaled the increment. */
c906108c 13029 case DW_LNS_const_add_pc:
2dc7f7b3
TT
13030 {
13031 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13032
13033 address += (((op_index + adjust)
13034 / lh->maximum_ops_per_instruction)
13035 * lh->minimum_instruction_length);
13036 op_index = ((op_index + adjust)
13037 % lh->maximum_ops_per_instruction);
13038 }
c906108c
SS
13039 break;
13040 case DW_LNS_fixed_advance_pc:
13041 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 13042 op_index = 0;
c906108c
SS
13043 line_ptr += 2;
13044 break;
9aa1fe7e 13045 default:
a738430d
MK
13046 {
13047 /* Unknown standard opcode, ignore it. */
9aa1fe7e 13048 int i;
a738430d 13049
debd256d 13050 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
13051 {
13052 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13053 line_ptr += bytes_read;
13054 }
13055 }
c906108c
SS
13056 }
13057 }
59205f5a
JB
13058 if (lh->num_file_names < file || file == 0)
13059 dwarf2_debug_line_missing_file_complaint ();
13060 else
13061 {
13062 lh->file_names[file - 1].included_p = 1;
13063 if (!decode_for_pst_p)
fbf65064
UW
13064 {
13065 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13066 (*p_record_line) (current_subfile, 0, addr);
fbf65064 13067 }
59205f5a 13068 }
c906108c 13069 }
f3f5162e
DE
13070}
13071
13072/* Decode the Line Number Program (LNP) for the given line_header
13073 structure and CU. The actual information extracted and the type
13074 of structures created from the LNP depends on the value of PST.
13075
13076 1. If PST is NULL, then this procedure uses the data from the program
13077 to create all necessary symbol tables, and their linetables.
13078
13079 2. If PST is not NULL, this procedure reads the program to determine
13080 the list of files included by the unit represented by PST, and
13081 builds all the associated partial symbol tables.
13082
13083 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13084 It is used for relative paths in the line table.
13085 NOTE: When processing partial symtabs (pst != NULL),
13086 comp_dir == pst->dirname.
13087
13088 NOTE: It is important that psymtabs have the same file name (via strcmp)
13089 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13090 symtab we don't use it in the name of the psymtabs we create.
13091 E.g. expand_line_sal requires this when finding psymtabs to expand.
13092 A good testcase for this is mb-inline.exp. */
13093
13094static void
13095dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13096 struct dwarf2_cu *cu, struct partial_symtab *pst,
13097 int want_line_info)
13098{
13099 struct objfile *objfile = cu->objfile;
13100 const int decode_for_pst_p = (pst != NULL);
13101 struct subfile *first_subfile = current_subfile;
13102
13103 if (want_line_info)
13104 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
13105
13106 if (decode_for_pst_p)
13107 {
13108 int file_index;
13109
13110 /* Now that we're done scanning the Line Header Program, we can
13111 create the psymtab of each included file. */
13112 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13113 if (lh->file_names[file_index].included_p == 1)
13114 {
c6da4cef
DE
13115 char *include_name =
13116 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13117 if (include_name != NULL)
aaa75496
JB
13118 dwarf2_create_include_psymtab (include_name, pst, objfile);
13119 }
13120 }
cb1df416
DJ
13121 else
13122 {
13123 /* Make sure a symtab is created for every file, even files
13124 which contain only variables (i.e. no code with associated
13125 line numbers). */
cb1df416 13126 int i;
cb1df416
DJ
13127
13128 for (i = 0; i < lh->num_file_names; i++)
13129 {
13130 char *dir = NULL;
f3f5162e 13131 struct file_entry *fe;
9a619af0 13132
cb1df416
DJ
13133 fe = &lh->file_names[i];
13134 if (fe->dir_index)
13135 dir = lh->include_dirs[fe->dir_index - 1];
13136 dwarf2_start_subfile (fe->name, dir, comp_dir);
13137
13138 /* Skip the main file; we don't need it, and it must be
13139 allocated last, so that it will show up before the
13140 non-primary symtabs in the objfile's symtab list. */
13141 if (current_subfile == first_subfile)
13142 continue;
13143
13144 if (current_subfile->symtab == NULL)
13145 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 13146 objfile);
cb1df416
DJ
13147 fe->symtab = current_subfile->symtab;
13148 }
13149 }
c906108c
SS
13150}
13151
13152/* Start a subfile for DWARF. FILENAME is the name of the file and
13153 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
13154 or NULL if not known. COMP_DIR is the compilation directory for the
13155 linetable's compilation unit or NULL if not known.
c906108c
SS
13156 This routine tries to keep line numbers from identical absolute and
13157 relative file names in a common subfile.
13158
13159 Using the `list' example from the GDB testsuite, which resides in
13160 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13161 of /srcdir/list0.c yields the following debugging information for list0.c:
13162
c5aa993b
JM
13163 DW_AT_name: /srcdir/list0.c
13164 DW_AT_comp_dir: /compdir
357e46e7 13165 files.files[0].name: list0.h
c5aa993b 13166 files.files[0].dir: /srcdir
357e46e7 13167 files.files[1].name: list0.c
c5aa993b 13168 files.files[1].dir: /srcdir
c906108c
SS
13169
13170 The line number information for list0.c has to end up in a single
4f1520fb
FR
13171 subfile, so that `break /srcdir/list0.c:1' works as expected.
13172 start_subfile will ensure that this happens provided that we pass the
13173 concatenation of files.files[1].dir and files.files[1].name as the
13174 subfile's name. */
c906108c
SS
13175
13176static void
3e43a32a
MS
13177dwarf2_start_subfile (char *filename, const char *dirname,
13178 const char *comp_dir)
c906108c 13179{
4f1520fb
FR
13180 char *fullname;
13181
13182 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13183 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13184 second argument to start_subfile. To be consistent, we do the
13185 same here. In order not to lose the line information directory,
13186 we concatenate it to the filename when it makes sense.
13187 Note that the Dwarf3 standard says (speaking of filenames in line
13188 information): ``The directory index is ignored for file names
13189 that represent full path names''. Thus ignoring dirname in the
13190 `else' branch below isn't an issue. */
c906108c 13191
d5166ae1 13192 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
13193 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13194 else
13195 fullname = filename;
c906108c 13196
4f1520fb
FR
13197 start_subfile (fullname, comp_dir);
13198
13199 if (fullname != filename)
13200 xfree (fullname);
c906108c
SS
13201}
13202
4c2df51b
DJ
13203static void
13204var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 13205 struct dwarf2_cu *cu)
4c2df51b 13206{
e7c27a73
DJ
13207 struct objfile *objfile = cu->objfile;
13208 struct comp_unit_head *cu_header = &cu->header;
13209
4c2df51b
DJ
13210 /* NOTE drow/2003-01-30: There used to be a comment and some special
13211 code here to turn a symbol with DW_AT_external and a
13212 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13213 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13214 with some versions of binutils) where shared libraries could have
13215 relocations against symbols in their debug information - the
13216 minimal symbol would have the right address, but the debug info
13217 would not. It's no longer necessary, because we will explicitly
13218 apply relocations when we read in the debug information now. */
13219
13220 /* A DW_AT_location attribute with no contents indicates that a
13221 variable has been optimized away. */
13222 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13223 {
13224 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13225 return;
13226 }
13227
13228 /* Handle one degenerate form of location expression specially, to
13229 preserve GDB's previous behavior when section offsets are
3019eac3
DE
13230 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13231 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
13232
13233 if (attr_form_is_block (attr)
3019eac3
DE
13234 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13235 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13236 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13237 && (DW_BLOCK (attr)->size
13238 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 13239 {
891d2f0b 13240 unsigned int dummy;
4c2df51b 13241
3019eac3
DE
13242 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13243 SYMBOL_VALUE_ADDRESS (sym) =
13244 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13245 else
13246 SYMBOL_VALUE_ADDRESS (sym) =
13247 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 13248 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
13249 fixup_symbol_section (sym, objfile);
13250 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13251 SYMBOL_SECTION (sym));
4c2df51b
DJ
13252 return;
13253 }
13254
13255 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13256 expression evaluator, and use LOC_COMPUTED only when necessary
13257 (i.e. when the value of a register or memory location is
13258 referenced, or a thread-local block, etc.). Then again, it might
13259 not be worthwhile. I'm assuming that it isn't unless performance
13260 or memory numbers show me otherwise. */
13261
e7c27a73 13262 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 13263 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
13264
13265 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13266 cu->has_loclist = 1;
4c2df51b
DJ
13267}
13268
c906108c
SS
13269/* Given a pointer to a DWARF information entry, figure out if we need
13270 to make a symbol table entry for it, and if so, create a new entry
13271 and return a pointer to it.
13272 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
13273 used the passed type.
13274 If SPACE is not NULL, use it to hold the new symbol. If it is
13275 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
13276
13277static struct symbol *
34eaf542
TT
13278new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13279 struct symbol *space)
c906108c 13280{
e7c27a73 13281 struct objfile *objfile = cu->objfile;
c906108c
SS
13282 struct symbol *sym = NULL;
13283 char *name;
13284 struct attribute *attr = NULL;
13285 struct attribute *attr2 = NULL;
e142c38c 13286 CORE_ADDR baseaddr;
e37fd15a
SW
13287 struct pending **list_to_add = NULL;
13288
edb3359d 13289 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
13290
13291 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 13292
94af9270 13293 name = dwarf2_name (die, cu);
c906108c
SS
13294 if (name)
13295 {
94af9270 13296 const char *linkagename;
34eaf542 13297 int suppress_add = 0;
94af9270 13298
34eaf542
TT
13299 if (space)
13300 sym = space;
13301 else
13302 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 13303 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
13304
13305 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 13306 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
13307 linkagename = dwarf2_physname (name, die, cu);
13308 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 13309
f55ee35c
JK
13310 /* Fortran does not have mangling standard and the mangling does differ
13311 between gfortran, iFort etc. */
13312 if (cu->language == language_fortran
b250c185 13313 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
13314 symbol_set_demangled_name (&(sym->ginfo),
13315 (char *) dwarf2_full_name (name, die, cu),
13316 NULL);
f55ee35c 13317
c906108c 13318 /* Default assumptions.
c5aa993b 13319 Use the passed type or decode it from the die. */
176620f1 13320 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 13321 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
13322 if (type != NULL)
13323 SYMBOL_TYPE (sym) = type;
13324 else
e7c27a73 13325 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
13326 attr = dwarf2_attr (die,
13327 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13328 cu);
c906108c
SS
13329 if (attr)
13330 {
13331 SYMBOL_LINE (sym) = DW_UNSND (attr);
13332 }
cb1df416 13333
edb3359d
DJ
13334 attr = dwarf2_attr (die,
13335 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13336 cu);
cb1df416
DJ
13337 if (attr)
13338 {
13339 int file_index = DW_UNSND (attr);
9a619af0 13340
cb1df416
DJ
13341 if (cu->line_header == NULL
13342 || file_index > cu->line_header->num_file_names)
13343 complaint (&symfile_complaints,
13344 _("file index out of range"));
1c3d648d 13345 else if (file_index > 0)
cb1df416
DJ
13346 {
13347 struct file_entry *fe;
9a619af0 13348
cb1df416
DJ
13349 fe = &cu->line_header->file_names[file_index - 1];
13350 SYMBOL_SYMTAB (sym) = fe->symtab;
13351 }
13352 }
13353
c906108c
SS
13354 switch (die->tag)
13355 {
13356 case DW_TAG_label:
e142c38c 13357 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
13358 if (attr)
13359 {
13360 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13361 }
0f5238ed
TT
13362 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13363 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 13364 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 13365 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
13366 break;
13367 case DW_TAG_subprogram:
13368 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13369 finish_block. */
13370 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 13371 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
13372 if ((attr2 && (DW_UNSND (attr2) != 0))
13373 || cu->language == language_ada)
c906108c 13374 {
2cfa0c8d
JB
13375 /* Subprograms marked external are stored as a global symbol.
13376 Ada subprograms, whether marked external or not, are always
13377 stored as a global symbol, because we want to be able to
13378 access them globally. For instance, we want to be able
13379 to break on a nested subprogram without having to
13380 specify the context. */
e37fd15a 13381 list_to_add = &global_symbols;
c906108c
SS
13382 }
13383 else
13384 {
e37fd15a 13385 list_to_add = cu->list_in_scope;
c906108c
SS
13386 }
13387 break;
edb3359d
DJ
13388 case DW_TAG_inlined_subroutine:
13389 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13390 finish_block. */
13391 SYMBOL_CLASS (sym) = LOC_BLOCK;
13392 SYMBOL_INLINED (sym) = 1;
481860b3 13393 list_to_add = cu->list_in_scope;
edb3359d 13394 break;
34eaf542
TT
13395 case DW_TAG_template_value_param:
13396 suppress_add = 1;
13397 /* Fall through. */
72929c62 13398 case DW_TAG_constant:
c906108c 13399 case DW_TAG_variable:
254e6b9e 13400 case DW_TAG_member:
0963b4bd
MS
13401 /* Compilation with minimal debug info may result in
13402 variables with missing type entries. Change the
13403 misleading `void' type to something sensible. */
c906108c 13404 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 13405 SYMBOL_TYPE (sym)
46bf5051 13406 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 13407
e142c38c 13408 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
13409 /* In the case of DW_TAG_member, we should only be called for
13410 static const members. */
13411 if (die->tag == DW_TAG_member)
13412 {
3863f96c
DE
13413 /* dwarf2_add_field uses die_is_declaration,
13414 so we do the same. */
254e6b9e
DE
13415 gdb_assert (die_is_declaration (die, cu));
13416 gdb_assert (attr);
13417 }
c906108c
SS
13418 if (attr)
13419 {
e7c27a73 13420 dwarf2_const_value (attr, sym, cu);
e142c38c 13421 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 13422 if (!suppress_add)
34eaf542
TT
13423 {
13424 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 13425 list_to_add = &global_symbols;
34eaf542 13426 else
e37fd15a 13427 list_to_add = cu->list_in_scope;
34eaf542 13428 }
c906108c
SS
13429 break;
13430 }
e142c38c 13431 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13432 if (attr)
13433 {
e7c27a73 13434 var_decode_location (attr, sym, cu);
e142c38c 13435 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
13436 if (SYMBOL_CLASS (sym) == LOC_STATIC
13437 && SYMBOL_VALUE_ADDRESS (sym) == 0
13438 && !dwarf2_per_objfile->has_section_at_zero)
13439 {
13440 /* When a static variable is eliminated by the linker,
13441 the corresponding debug information is not stripped
13442 out, but the variable address is set to null;
13443 do not add such variables into symbol table. */
13444 }
13445 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 13446 {
f55ee35c
JK
13447 /* Workaround gfortran PR debug/40040 - it uses
13448 DW_AT_location for variables in -fPIC libraries which may
13449 get overriden by other libraries/executable and get
13450 a different address. Resolve it by the minimal symbol
13451 which may come from inferior's executable using copy
13452 relocation. Make this workaround only for gfortran as for
13453 other compilers GDB cannot guess the minimal symbol
13454 Fortran mangling kind. */
13455 if (cu->language == language_fortran && die->parent
13456 && die->parent->tag == DW_TAG_module
13457 && cu->producer
13458 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13459 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13460
1c809c68
TT
13461 /* A variable with DW_AT_external is never static,
13462 but it may be block-scoped. */
13463 list_to_add = (cu->list_in_scope == &file_symbols
13464 ? &global_symbols : cu->list_in_scope);
1c809c68 13465 }
c906108c 13466 else
e37fd15a 13467 list_to_add = cu->list_in_scope;
c906108c
SS
13468 }
13469 else
13470 {
13471 /* We do not know the address of this symbol.
c5aa993b
JM
13472 If it is an external symbol and we have type information
13473 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13474 The address of the variable will then be determined from
13475 the minimal symbol table whenever the variable is
13476 referenced. */
e142c38c 13477 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 13478 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 13479 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 13480 {
0fe7935b
DJ
13481 /* A variable with DW_AT_external is never static, but it
13482 may be block-scoped. */
13483 list_to_add = (cu->list_in_scope == &file_symbols
13484 ? &global_symbols : cu->list_in_scope);
13485
c906108c 13486 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 13487 }
442ddf59
JK
13488 else if (!die_is_declaration (die, cu))
13489 {
13490 /* Use the default LOC_OPTIMIZED_OUT class. */
13491 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
13492 if (!suppress_add)
13493 list_to_add = cu->list_in_scope;
442ddf59 13494 }
c906108c
SS
13495 }
13496 break;
13497 case DW_TAG_formal_parameter:
edb3359d
DJ
13498 /* If we are inside a function, mark this as an argument. If
13499 not, we might be looking at an argument to an inlined function
13500 when we do not have enough information to show inlined frames;
13501 pretend it's a local variable in that case so that the user can
13502 still see it. */
13503 if (context_stack_depth > 0
13504 && context_stack[context_stack_depth - 1].name != NULL)
13505 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 13506 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13507 if (attr)
13508 {
e7c27a73 13509 var_decode_location (attr, sym, cu);
c906108c 13510 }
e142c38c 13511 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13512 if (attr)
13513 {
e7c27a73 13514 dwarf2_const_value (attr, sym, cu);
c906108c 13515 }
f346a30d 13516
e37fd15a 13517 list_to_add = cu->list_in_scope;
c906108c
SS
13518 break;
13519 case DW_TAG_unspecified_parameters:
13520 /* From varargs functions; gdb doesn't seem to have any
13521 interest in this information, so just ignore it for now.
13522 (FIXME?) */
13523 break;
34eaf542
TT
13524 case DW_TAG_template_type_param:
13525 suppress_add = 1;
13526 /* Fall through. */
c906108c 13527 case DW_TAG_class_type:
680b30c7 13528 case DW_TAG_interface_type:
c906108c
SS
13529 case DW_TAG_structure_type:
13530 case DW_TAG_union_type:
72019c9c 13531 case DW_TAG_set_type:
c906108c
SS
13532 case DW_TAG_enumeration_type:
13533 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13534 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 13535
63d06c5c 13536 {
987504bb 13537 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
13538 really ever be static objects: otherwise, if you try
13539 to, say, break of a class's method and you're in a file
13540 which doesn't mention that class, it won't work unless
13541 the check for all static symbols in lookup_symbol_aux
13542 saves you. See the OtherFileClass tests in
13543 gdb.c++/namespace.exp. */
13544
e37fd15a 13545 if (!suppress_add)
34eaf542 13546 {
34eaf542
TT
13547 list_to_add = (cu->list_in_scope == &file_symbols
13548 && (cu->language == language_cplus
13549 || cu->language == language_java)
13550 ? &global_symbols : cu->list_in_scope);
63d06c5c 13551
64382290
TT
13552 /* The semantics of C++ state that "struct foo {
13553 ... }" also defines a typedef for "foo". A Java
13554 class declaration also defines a typedef for the
13555 class. */
13556 if (cu->language == language_cplus
13557 || cu->language == language_java
13558 || cu->language == language_ada)
13559 {
13560 /* The symbol's name is already allocated along
13561 with this objfile, so we don't need to
13562 duplicate it for the type. */
13563 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13564 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13565 }
63d06c5c
DC
13566 }
13567 }
c906108c
SS
13568 break;
13569 case DW_TAG_typedef:
63d06c5c
DC
13570 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13571 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13572 list_to_add = cu->list_in_scope;
63d06c5c 13573 break;
c906108c 13574 case DW_TAG_base_type:
a02abb62 13575 case DW_TAG_subrange_type:
c906108c 13576 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13577 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13578 list_to_add = cu->list_in_scope;
c906108c
SS
13579 break;
13580 case DW_TAG_enumerator:
e142c38c 13581 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13582 if (attr)
13583 {
e7c27a73 13584 dwarf2_const_value (attr, sym, cu);
c906108c 13585 }
63d06c5c
DC
13586 {
13587 /* NOTE: carlton/2003-11-10: See comment above in the
13588 DW_TAG_class_type, etc. block. */
13589
e142c38c 13590 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
13591 && (cu->language == language_cplus
13592 || cu->language == language_java)
e142c38c 13593 ? &global_symbols : cu->list_in_scope);
63d06c5c 13594 }
c906108c 13595 break;
5c4e30ca
DC
13596 case DW_TAG_namespace:
13597 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 13598 list_to_add = &global_symbols;
5c4e30ca 13599 break;
c906108c
SS
13600 default:
13601 /* Not a tag we recognize. Hopefully we aren't processing
13602 trash data, but since we must specifically ignore things
13603 we don't recognize, there is nothing else we should do at
0963b4bd 13604 this point. */
e2e0b3e5 13605 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 13606 dwarf_tag_name (die->tag));
c906108c
SS
13607 break;
13608 }
df8a16a1 13609
e37fd15a
SW
13610 if (suppress_add)
13611 {
13612 sym->hash_next = objfile->template_symbols;
13613 objfile->template_symbols = sym;
13614 list_to_add = NULL;
13615 }
13616
13617 if (list_to_add != NULL)
13618 add_symbol_to_list (sym, list_to_add);
13619
df8a16a1
DJ
13620 /* For the benefit of old versions of GCC, check for anonymous
13621 namespaces based on the demangled name. */
13622 if (!processing_has_namespace_info
94af9270 13623 && cu->language == language_cplus)
a10964d1 13624 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
13625 }
13626 return (sym);
13627}
13628
34eaf542
TT
13629/* A wrapper for new_symbol_full that always allocates a new symbol. */
13630
13631static struct symbol *
13632new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13633{
13634 return new_symbol_full (die, type, cu, NULL);
13635}
13636
98bfdba5
PA
13637/* Given an attr with a DW_FORM_dataN value in host byte order,
13638 zero-extend it as appropriate for the symbol's type. The DWARF
13639 standard (v4) is not entirely clear about the meaning of using
13640 DW_FORM_dataN for a constant with a signed type, where the type is
13641 wider than the data. The conclusion of a discussion on the DWARF
13642 list was that this is unspecified. We choose to always zero-extend
13643 because that is the interpretation long in use by GCC. */
c906108c 13644
98bfdba5
PA
13645static gdb_byte *
13646dwarf2_const_value_data (struct attribute *attr, struct type *type,
13647 const char *name, struct obstack *obstack,
12df843f 13648 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 13649{
e7c27a73 13650 struct objfile *objfile = cu->objfile;
e17a4113
UW
13651 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13652 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
13653 LONGEST l = DW_UNSND (attr);
13654
13655 if (bits < sizeof (*value) * 8)
13656 {
13657 l &= ((LONGEST) 1 << bits) - 1;
13658 *value = l;
13659 }
13660 else if (bits == sizeof (*value) * 8)
13661 *value = l;
13662 else
13663 {
13664 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13665 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13666 return bytes;
13667 }
13668
13669 return NULL;
13670}
13671
13672/* Read a constant value from an attribute. Either set *VALUE, or if
13673 the value does not fit in *VALUE, set *BYTES - either already
13674 allocated on the objfile obstack, or newly allocated on OBSTACK,
13675 or, set *BATON, if we translated the constant to a location
13676 expression. */
13677
13678static void
13679dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13680 const char *name, struct obstack *obstack,
13681 struct dwarf2_cu *cu,
12df843f 13682 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
13683 struct dwarf2_locexpr_baton **baton)
13684{
13685 struct objfile *objfile = cu->objfile;
13686 struct comp_unit_head *cu_header = &cu->header;
c906108c 13687 struct dwarf_block *blk;
98bfdba5
PA
13688 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13689 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13690
13691 *value = 0;
13692 *bytes = NULL;
13693 *baton = NULL;
c906108c
SS
13694
13695 switch (attr->form)
13696 {
13697 case DW_FORM_addr:
3019eac3 13698 case DW_FORM_GNU_addr_index:
ac56253d 13699 {
ac56253d
TT
13700 gdb_byte *data;
13701
98bfdba5
PA
13702 if (TYPE_LENGTH (type) != cu_header->addr_size)
13703 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 13704 cu_header->addr_size,
98bfdba5 13705 TYPE_LENGTH (type));
ac56253d
TT
13706 /* Symbols of this form are reasonably rare, so we just
13707 piggyback on the existing location code rather than writing
13708 a new implementation of symbol_computed_ops. */
98bfdba5
PA
13709 *baton = obstack_alloc (&objfile->objfile_obstack,
13710 sizeof (struct dwarf2_locexpr_baton));
13711 (*baton)->per_cu = cu->per_cu;
13712 gdb_assert ((*baton)->per_cu);
ac56253d 13713
98bfdba5
PA
13714 (*baton)->size = 2 + cu_header->addr_size;
13715 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13716 (*baton)->data = data;
ac56253d
TT
13717
13718 data[0] = DW_OP_addr;
13719 store_unsigned_integer (&data[1], cu_header->addr_size,
13720 byte_order, DW_ADDR (attr));
13721 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 13722 }
c906108c 13723 break;
4ac36638 13724 case DW_FORM_string:
93b5768b 13725 case DW_FORM_strp:
3019eac3 13726 case DW_FORM_GNU_str_index:
98bfdba5
PA
13727 /* DW_STRING is already allocated on the objfile obstack, point
13728 directly to it. */
13729 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 13730 break;
c906108c
SS
13731 case DW_FORM_block1:
13732 case DW_FORM_block2:
13733 case DW_FORM_block4:
13734 case DW_FORM_block:
2dc7f7b3 13735 case DW_FORM_exprloc:
c906108c 13736 blk = DW_BLOCK (attr);
98bfdba5
PA
13737 if (TYPE_LENGTH (type) != blk->size)
13738 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13739 TYPE_LENGTH (type));
13740 *bytes = blk->data;
c906108c 13741 break;
2df3850c
JM
13742
13743 /* The DW_AT_const_value attributes are supposed to carry the
13744 symbol's value "represented as it would be on the target
13745 architecture." By the time we get here, it's already been
13746 converted to host endianness, so we just need to sign- or
13747 zero-extend it as appropriate. */
13748 case DW_FORM_data1:
3e43a32a
MS
13749 *bytes = dwarf2_const_value_data (attr, type, name,
13750 obstack, cu, value, 8);
2df3850c 13751 break;
c906108c 13752 case DW_FORM_data2:
3e43a32a
MS
13753 *bytes = dwarf2_const_value_data (attr, type, name,
13754 obstack, cu, value, 16);
2df3850c 13755 break;
c906108c 13756 case DW_FORM_data4:
3e43a32a
MS
13757 *bytes = dwarf2_const_value_data (attr, type, name,
13758 obstack, cu, value, 32);
2df3850c 13759 break;
c906108c 13760 case DW_FORM_data8:
3e43a32a
MS
13761 *bytes = dwarf2_const_value_data (attr, type, name,
13762 obstack, cu, value, 64);
2df3850c
JM
13763 break;
13764
c906108c 13765 case DW_FORM_sdata:
98bfdba5 13766 *value = DW_SND (attr);
2df3850c
JM
13767 break;
13768
c906108c 13769 case DW_FORM_udata:
98bfdba5 13770 *value = DW_UNSND (attr);
c906108c 13771 break;
2df3850c 13772
c906108c 13773 default:
4d3c2250 13774 complaint (&symfile_complaints,
e2e0b3e5 13775 _("unsupported const value attribute form: '%s'"),
4d3c2250 13776 dwarf_form_name (attr->form));
98bfdba5 13777 *value = 0;
c906108c
SS
13778 break;
13779 }
13780}
13781
2df3850c 13782
98bfdba5
PA
13783/* Copy constant value from an attribute to a symbol. */
13784
2df3850c 13785static void
98bfdba5
PA
13786dwarf2_const_value (struct attribute *attr, struct symbol *sym,
13787 struct dwarf2_cu *cu)
2df3850c 13788{
98bfdba5
PA
13789 struct objfile *objfile = cu->objfile;
13790 struct comp_unit_head *cu_header = &cu->header;
12df843f 13791 LONGEST value;
98bfdba5
PA
13792 gdb_byte *bytes;
13793 struct dwarf2_locexpr_baton *baton;
2df3850c 13794
98bfdba5
PA
13795 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
13796 SYMBOL_PRINT_NAME (sym),
13797 &objfile->objfile_obstack, cu,
13798 &value, &bytes, &baton);
2df3850c 13799
98bfdba5
PA
13800 if (baton != NULL)
13801 {
13802 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13803 SYMBOL_LOCATION_BATON (sym) = baton;
13804 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13805 }
13806 else if (bytes != NULL)
13807 {
13808 SYMBOL_VALUE_BYTES (sym) = bytes;
13809 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
13810 }
13811 else
13812 {
13813 SYMBOL_VALUE (sym) = value;
13814 SYMBOL_CLASS (sym) = LOC_CONST;
13815 }
2df3850c
JM
13816}
13817
c906108c
SS
13818/* Return the type of the die in question using its DW_AT_type attribute. */
13819
13820static struct type *
e7c27a73 13821die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13822{
c906108c 13823 struct attribute *type_attr;
c906108c 13824
e142c38c 13825 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
13826 if (!type_attr)
13827 {
13828 /* A missing DW_AT_type represents a void type. */
46bf5051 13829 return objfile_type (cu->objfile)->builtin_void;
c906108c 13830 }
348e048f 13831
673bfd45 13832 return lookup_die_type (die, type_attr, cu);
c906108c
SS
13833}
13834
b4ba55a1
JB
13835/* True iff CU's producer generates GNAT Ada auxiliary information
13836 that allows to find parallel types through that information instead
13837 of having to do expensive parallel lookups by type name. */
13838
13839static int
13840need_gnat_info (struct dwarf2_cu *cu)
13841{
13842 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
13843 of GNAT produces this auxiliary information, without any indication
13844 that it is produced. Part of enhancing the FSF version of GNAT
13845 to produce that information will be to put in place an indicator
13846 that we can use in order to determine whether the descriptive type
13847 info is available or not. One suggestion that has been made is
13848 to use a new attribute, attached to the CU die. For now, assume
13849 that the descriptive type info is not available. */
13850 return 0;
13851}
13852
b4ba55a1
JB
13853/* Return the auxiliary type of the die in question using its
13854 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
13855 attribute is not present. */
13856
13857static struct type *
13858die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
13859{
b4ba55a1 13860 struct attribute *type_attr;
b4ba55a1
JB
13861
13862 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
13863 if (!type_attr)
13864 return NULL;
13865
673bfd45 13866 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
13867}
13868
13869/* If DIE has a descriptive_type attribute, then set the TYPE's
13870 descriptive type accordingly. */
13871
13872static void
13873set_descriptive_type (struct type *type, struct die_info *die,
13874 struct dwarf2_cu *cu)
13875{
13876 struct type *descriptive_type = die_descriptive_type (die, cu);
13877
13878 if (descriptive_type)
13879 {
13880 ALLOCATE_GNAT_AUX_TYPE (type);
13881 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
13882 }
13883}
13884
c906108c
SS
13885/* Return the containing type of the die in question using its
13886 DW_AT_containing_type attribute. */
13887
13888static struct type *
e7c27a73 13889die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13890{
c906108c 13891 struct attribute *type_attr;
c906108c 13892
e142c38c 13893 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
13894 if (!type_attr)
13895 error (_("Dwarf Error: Problem turning containing type into gdb type "
13896 "[in module %s]"), cu->objfile->name);
13897
673bfd45 13898 return lookup_die_type (die, type_attr, cu);
c906108c
SS
13899}
13900
673bfd45
DE
13901/* Look up the type of DIE in CU using its type attribute ATTR.
13902 If there is no type substitute an error marker. */
13903
c906108c 13904static struct type *
673bfd45
DE
13905lookup_die_type (struct die_info *die, struct attribute *attr,
13906 struct dwarf2_cu *cu)
c906108c 13907{
bb5ed363 13908 struct objfile *objfile = cu->objfile;
f792889a
DJ
13909 struct type *this_type;
13910
673bfd45
DE
13911 /* First see if we have it cached. */
13912
13913 if (is_ref_attr (attr))
13914 {
b64f50a1 13915 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
13916
13917 this_type = get_die_type_at_offset (offset, cu->per_cu);
13918 }
55f1336d 13919 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
13920 {
13921 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
13922
13923 /* sig_type will be NULL if the signatured type is missing from
13924 the debug info. */
13925 if (sig_type == NULL)
13926 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13927 "at 0x%x [in module %s]"),
b64f50a1 13928 die->offset.sect_off, objfile->name);
673bfd45 13929
3019eac3
DE
13930 gdb_assert (sig_type->per_cu.is_debug_types);
13931 /* If we haven't filled in type_offset_in_section yet, then we
13932 haven't read the type in yet. */
13933 this_type = NULL;
13934 if (sig_type->type_offset_in_section.sect_off != 0)
13935 {
13936 this_type =
13937 get_die_type_at_offset (sig_type->type_offset_in_section,
13938 &sig_type->per_cu);
13939 }
673bfd45
DE
13940 }
13941 else
13942 {
13943 dump_die_for_error (die);
13944 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 13945 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
13946 }
13947
13948 /* If not cached we need to read it in. */
13949
13950 if (this_type == NULL)
13951 {
13952 struct die_info *type_die;
13953 struct dwarf2_cu *type_cu = cu;
13954
13955 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
13956 /* If we found the type now, it's probably because the type came
13957 from an inter-CU reference and the type's CU got expanded before
13958 ours. */
13959 this_type = get_die_type (type_die, type_cu);
13960 if (this_type == NULL)
13961 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
13962 }
13963
13964 /* If we still don't have a type use an error marker. */
13965
13966 if (this_type == NULL)
c906108c 13967 {
b00fdb78
TT
13968 char *message, *saved;
13969
13970 /* read_type_die already issued a complaint. */
13971 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 13972 objfile->name,
b64f50a1
JK
13973 cu->header.offset.sect_off,
13974 die->offset.sect_off);
bb5ed363 13975 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
13976 message, strlen (message));
13977 xfree (message);
13978
bb5ed363 13979 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 13980 }
673bfd45 13981
f792889a 13982 return this_type;
c906108c
SS
13983}
13984
673bfd45
DE
13985/* Return the type in DIE, CU.
13986 Returns NULL for invalid types.
13987
13988 This first does a lookup in the appropriate type_hash table,
13989 and only reads the die in if necessary.
13990
13991 NOTE: This can be called when reading in partial or full symbols. */
13992
f792889a 13993static struct type *
e7c27a73 13994read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13995{
f792889a
DJ
13996 struct type *this_type;
13997
13998 this_type = get_die_type (die, cu);
13999 if (this_type)
14000 return this_type;
14001
673bfd45
DE
14002 return read_type_die_1 (die, cu);
14003}
14004
14005/* Read the type in DIE, CU.
14006 Returns NULL for invalid types. */
14007
14008static struct type *
14009read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
14010{
14011 struct type *this_type = NULL;
14012
c906108c
SS
14013 switch (die->tag)
14014 {
14015 case DW_TAG_class_type:
680b30c7 14016 case DW_TAG_interface_type:
c906108c
SS
14017 case DW_TAG_structure_type:
14018 case DW_TAG_union_type:
f792889a 14019 this_type = read_structure_type (die, cu);
c906108c
SS
14020 break;
14021 case DW_TAG_enumeration_type:
f792889a 14022 this_type = read_enumeration_type (die, cu);
c906108c
SS
14023 break;
14024 case DW_TAG_subprogram:
14025 case DW_TAG_subroutine_type:
edb3359d 14026 case DW_TAG_inlined_subroutine:
f792889a 14027 this_type = read_subroutine_type (die, cu);
c906108c
SS
14028 break;
14029 case DW_TAG_array_type:
f792889a 14030 this_type = read_array_type (die, cu);
c906108c 14031 break;
72019c9c 14032 case DW_TAG_set_type:
f792889a 14033 this_type = read_set_type (die, cu);
72019c9c 14034 break;
c906108c 14035 case DW_TAG_pointer_type:
f792889a 14036 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
14037 break;
14038 case DW_TAG_ptr_to_member_type:
f792889a 14039 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
14040 break;
14041 case DW_TAG_reference_type:
f792889a 14042 this_type = read_tag_reference_type (die, cu);
c906108c
SS
14043 break;
14044 case DW_TAG_const_type:
f792889a 14045 this_type = read_tag_const_type (die, cu);
c906108c
SS
14046 break;
14047 case DW_TAG_volatile_type:
f792889a 14048 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
14049 break;
14050 case DW_TAG_string_type:
f792889a 14051 this_type = read_tag_string_type (die, cu);
c906108c
SS
14052 break;
14053 case DW_TAG_typedef:
f792889a 14054 this_type = read_typedef (die, cu);
c906108c 14055 break;
a02abb62 14056 case DW_TAG_subrange_type:
f792889a 14057 this_type = read_subrange_type (die, cu);
a02abb62 14058 break;
c906108c 14059 case DW_TAG_base_type:
f792889a 14060 this_type = read_base_type (die, cu);
c906108c 14061 break;
81a17f79 14062 case DW_TAG_unspecified_type:
f792889a 14063 this_type = read_unspecified_type (die, cu);
81a17f79 14064 break;
0114d602
DJ
14065 case DW_TAG_namespace:
14066 this_type = read_namespace_type (die, cu);
14067 break;
f55ee35c
JK
14068 case DW_TAG_module:
14069 this_type = read_module_type (die, cu);
14070 break;
c906108c 14071 default:
3e43a32a
MS
14072 complaint (&symfile_complaints,
14073 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 14074 dwarf_tag_name (die->tag));
c906108c
SS
14075 break;
14076 }
63d06c5c 14077
f792889a 14078 return this_type;
63d06c5c
DC
14079}
14080
abc72ce4
DE
14081/* See if we can figure out if the class lives in a namespace. We do
14082 this by looking for a member function; its demangled name will
14083 contain namespace info, if there is any.
14084 Return the computed name or NULL.
14085 Space for the result is allocated on the objfile's obstack.
14086 This is the full-die version of guess_partial_die_structure_name.
14087 In this case we know DIE has no useful parent. */
14088
14089static char *
14090guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14091{
14092 struct die_info *spec_die;
14093 struct dwarf2_cu *spec_cu;
14094 struct die_info *child;
14095
14096 spec_cu = cu;
14097 spec_die = die_specification (die, &spec_cu);
14098 if (spec_die != NULL)
14099 {
14100 die = spec_die;
14101 cu = spec_cu;
14102 }
14103
14104 for (child = die->child;
14105 child != NULL;
14106 child = child->sibling)
14107 {
14108 if (child->tag == DW_TAG_subprogram)
14109 {
14110 struct attribute *attr;
14111
14112 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14113 if (attr == NULL)
14114 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14115 if (attr != NULL)
14116 {
14117 char *actual_name
14118 = language_class_name_from_physname (cu->language_defn,
14119 DW_STRING (attr));
14120 char *name = NULL;
14121
14122 if (actual_name != NULL)
14123 {
14124 char *die_name = dwarf2_name (die, cu);
14125
14126 if (die_name != NULL
14127 && strcmp (die_name, actual_name) != 0)
14128 {
14129 /* Strip off the class name from the full name.
14130 We want the prefix. */
14131 int die_name_len = strlen (die_name);
14132 int actual_name_len = strlen (actual_name);
14133
14134 /* Test for '::' as a sanity check. */
14135 if (actual_name_len > die_name_len + 2
3e43a32a
MS
14136 && actual_name[actual_name_len
14137 - die_name_len - 1] == ':')
abc72ce4
DE
14138 name =
14139 obsavestring (actual_name,
14140 actual_name_len - die_name_len - 2,
14141 &cu->objfile->objfile_obstack);
14142 }
14143 }
14144 xfree (actual_name);
14145 return name;
14146 }
14147 }
14148 }
14149
14150 return NULL;
14151}
14152
96408a79
SA
14153/* GCC might emit a nameless typedef that has a linkage name. Determine the
14154 prefix part in such case. See
14155 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14156
14157static char *
14158anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14159{
14160 struct attribute *attr;
14161 char *base;
14162
14163 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14164 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14165 return NULL;
14166
14167 attr = dwarf2_attr (die, DW_AT_name, cu);
14168 if (attr != NULL && DW_STRING (attr) != NULL)
14169 return NULL;
14170
14171 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14172 if (attr == NULL)
14173 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14174 if (attr == NULL || DW_STRING (attr) == NULL)
14175 return NULL;
14176
14177 /* dwarf2_name had to be already called. */
14178 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14179
14180 /* Strip the base name, keep any leading namespaces/classes. */
14181 base = strrchr (DW_STRING (attr), ':');
14182 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14183 return "";
14184
14185 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14186 &cu->objfile->objfile_obstack);
14187}
14188
fdde2d81 14189/* Return the name of the namespace/class that DIE is defined within,
0114d602 14190 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 14191
0114d602
DJ
14192 For example, if we're within the method foo() in the following
14193 code:
14194
14195 namespace N {
14196 class C {
14197 void foo () {
14198 }
14199 };
14200 }
14201
14202 then determine_prefix on foo's die will return "N::C". */
fdde2d81 14203
0d5cff50 14204static const char *
e142c38c 14205determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 14206{
0114d602
DJ
14207 struct die_info *parent, *spec_die;
14208 struct dwarf2_cu *spec_cu;
14209 struct type *parent_type;
96408a79 14210 char *retval;
63d06c5c 14211
f55ee35c
JK
14212 if (cu->language != language_cplus && cu->language != language_java
14213 && cu->language != language_fortran)
0114d602
DJ
14214 return "";
14215
96408a79
SA
14216 retval = anonymous_struct_prefix (die, cu);
14217 if (retval)
14218 return retval;
14219
0114d602
DJ
14220 /* We have to be careful in the presence of DW_AT_specification.
14221 For example, with GCC 3.4, given the code
14222
14223 namespace N {
14224 void foo() {
14225 // Definition of N::foo.
14226 }
14227 }
14228
14229 then we'll have a tree of DIEs like this:
14230
14231 1: DW_TAG_compile_unit
14232 2: DW_TAG_namespace // N
14233 3: DW_TAG_subprogram // declaration of N::foo
14234 4: DW_TAG_subprogram // definition of N::foo
14235 DW_AT_specification // refers to die #3
14236
14237 Thus, when processing die #4, we have to pretend that we're in
14238 the context of its DW_AT_specification, namely the contex of die
14239 #3. */
14240 spec_cu = cu;
14241 spec_die = die_specification (die, &spec_cu);
14242 if (spec_die == NULL)
14243 parent = die->parent;
14244 else
63d06c5c 14245 {
0114d602
DJ
14246 parent = spec_die->parent;
14247 cu = spec_cu;
63d06c5c 14248 }
0114d602
DJ
14249
14250 if (parent == NULL)
14251 return "";
98bfdba5
PA
14252 else if (parent->building_fullname)
14253 {
14254 const char *name;
14255 const char *parent_name;
14256
14257 /* It has been seen on RealView 2.2 built binaries,
14258 DW_TAG_template_type_param types actually _defined_ as
14259 children of the parent class:
14260
14261 enum E {};
14262 template class <class Enum> Class{};
14263 Class<enum E> class_e;
14264
14265 1: DW_TAG_class_type (Class)
14266 2: DW_TAG_enumeration_type (E)
14267 3: DW_TAG_enumerator (enum1:0)
14268 3: DW_TAG_enumerator (enum2:1)
14269 ...
14270 2: DW_TAG_template_type_param
14271 DW_AT_type DW_FORM_ref_udata (E)
14272
14273 Besides being broken debug info, it can put GDB into an
14274 infinite loop. Consider:
14275
14276 When we're building the full name for Class<E>, we'll start
14277 at Class, and go look over its template type parameters,
14278 finding E. We'll then try to build the full name of E, and
14279 reach here. We're now trying to build the full name of E,
14280 and look over the parent DIE for containing scope. In the
14281 broken case, if we followed the parent DIE of E, we'd again
14282 find Class, and once again go look at its template type
14283 arguments, etc., etc. Simply don't consider such parent die
14284 as source-level parent of this die (it can't be, the language
14285 doesn't allow it), and break the loop here. */
14286 name = dwarf2_name (die, cu);
14287 parent_name = dwarf2_name (parent, cu);
14288 complaint (&symfile_complaints,
14289 _("template param type '%s' defined within parent '%s'"),
14290 name ? name : "<unknown>",
14291 parent_name ? parent_name : "<unknown>");
14292 return "";
14293 }
63d06c5c 14294 else
0114d602
DJ
14295 switch (parent->tag)
14296 {
63d06c5c 14297 case DW_TAG_namespace:
0114d602 14298 parent_type = read_type_die (parent, cu);
acebe513
UW
14299 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14300 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14301 Work around this problem here. */
14302 if (cu->language == language_cplus
14303 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14304 return "";
0114d602
DJ
14305 /* We give a name to even anonymous namespaces. */
14306 return TYPE_TAG_NAME (parent_type);
63d06c5c 14307 case DW_TAG_class_type:
680b30c7 14308 case DW_TAG_interface_type:
63d06c5c 14309 case DW_TAG_structure_type:
0114d602 14310 case DW_TAG_union_type:
f55ee35c 14311 case DW_TAG_module:
0114d602
DJ
14312 parent_type = read_type_die (parent, cu);
14313 if (TYPE_TAG_NAME (parent_type) != NULL)
14314 return TYPE_TAG_NAME (parent_type);
14315 else
14316 /* An anonymous structure is only allowed non-static data
14317 members; no typedefs, no member functions, et cetera.
14318 So it does not need a prefix. */
14319 return "";
abc72ce4 14320 case DW_TAG_compile_unit:
95554aad 14321 case DW_TAG_partial_unit:
abc72ce4
DE
14322 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14323 if (cu->language == language_cplus
8b70b953 14324 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14325 && die->child != NULL
14326 && (die->tag == DW_TAG_class_type
14327 || die->tag == DW_TAG_structure_type
14328 || die->tag == DW_TAG_union_type))
14329 {
14330 char *name = guess_full_die_structure_name (die, cu);
14331 if (name != NULL)
14332 return name;
14333 }
14334 return "";
63d06c5c 14335 default:
8176b9b8 14336 return determine_prefix (parent, cu);
63d06c5c 14337 }
63d06c5c
DC
14338}
14339
3e43a32a
MS
14340/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14341 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14342 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14343 an obconcat, otherwise allocate storage for the result. The CU argument is
14344 used to determine the language and hence, the appropriate separator. */
987504bb 14345
f55ee35c 14346#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
14347
14348static char *
f55ee35c
JK
14349typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14350 int physname, struct dwarf2_cu *cu)
63d06c5c 14351{
f55ee35c 14352 const char *lead = "";
5c315b68 14353 const char *sep;
63d06c5c 14354
3e43a32a
MS
14355 if (suffix == NULL || suffix[0] == '\0'
14356 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
14357 sep = "";
14358 else if (cu->language == language_java)
14359 sep = ".";
f55ee35c
JK
14360 else if (cu->language == language_fortran && physname)
14361 {
14362 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14363 DW_AT_MIPS_linkage_name is preferred and used instead. */
14364
14365 lead = "__";
14366 sep = "_MOD_";
14367 }
987504bb
JJ
14368 else
14369 sep = "::";
63d06c5c 14370
6dd47d34
DE
14371 if (prefix == NULL)
14372 prefix = "";
14373 if (suffix == NULL)
14374 suffix = "";
14375
987504bb
JJ
14376 if (obs == NULL)
14377 {
3e43a32a
MS
14378 char *retval
14379 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 14380
f55ee35c
JK
14381 strcpy (retval, lead);
14382 strcat (retval, prefix);
6dd47d34
DE
14383 strcat (retval, sep);
14384 strcat (retval, suffix);
63d06c5c
DC
14385 return retval;
14386 }
987504bb
JJ
14387 else
14388 {
14389 /* We have an obstack. */
f55ee35c 14390 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 14391 }
63d06c5c
DC
14392}
14393
c906108c
SS
14394/* Return sibling of die, NULL if no sibling. */
14395
f9aca02d 14396static struct die_info *
fba45db2 14397sibling_die (struct die_info *die)
c906108c 14398{
639d11d3 14399 return die->sibling;
c906108c
SS
14400}
14401
71c25dea
TT
14402/* Get name of a die, return NULL if not found. */
14403
14404static char *
14405dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14406 struct obstack *obstack)
14407{
14408 if (name && cu->language == language_cplus)
14409 {
14410 char *canon_name = cp_canonicalize_string (name);
14411
14412 if (canon_name != NULL)
14413 {
14414 if (strcmp (canon_name, name) != 0)
14415 name = obsavestring (canon_name, strlen (canon_name),
14416 obstack);
14417 xfree (canon_name);
14418 }
14419 }
14420
14421 return name;
c906108c
SS
14422}
14423
9219021c
DC
14424/* Get name of a die, return NULL if not found. */
14425
14426static char *
e142c38c 14427dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
14428{
14429 struct attribute *attr;
14430
e142c38c 14431 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
14432 if ((!attr || !DW_STRING (attr))
14433 && die->tag != DW_TAG_class_type
14434 && die->tag != DW_TAG_interface_type
14435 && die->tag != DW_TAG_structure_type
14436 && die->tag != DW_TAG_union_type)
71c25dea
TT
14437 return NULL;
14438
14439 switch (die->tag)
14440 {
14441 case DW_TAG_compile_unit:
95554aad 14442 case DW_TAG_partial_unit:
71c25dea
TT
14443 /* Compilation units have a DW_AT_name that is a filename, not
14444 a source language identifier. */
14445 case DW_TAG_enumeration_type:
14446 case DW_TAG_enumerator:
14447 /* These tags always have simple identifiers already; no need
14448 to canonicalize them. */
14449 return DW_STRING (attr);
907af001 14450
418835cc
KS
14451 case DW_TAG_subprogram:
14452 /* Java constructors will all be named "<init>", so return
14453 the class name when we see this special case. */
14454 if (cu->language == language_java
14455 && DW_STRING (attr) != NULL
14456 && strcmp (DW_STRING (attr), "<init>") == 0)
14457 {
14458 struct dwarf2_cu *spec_cu = cu;
14459 struct die_info *spec_die;
14460
14461 /* GCJ will output '<init>' for Java constructor names.
14462 For this special case, return the name of the parent class. */
14463
14464 /* GCJ may output suprogram DIEs with AT_specification set.
14465 If so, use the name of the specified DIE. */
14466 spec_die = die_specification (die, &spec_cu);
14467 if (spec_die != NULL)
14468 return dwarf2_name (spec_die, spec_cu);
14469
14470 do
14471 {
14472 die = die->parent;
14473 if (die->tag == DW_TAG_class_type)
14474 return dwarf2_name (die, cu);
14475 }
95554aad
TT
14476 while (die->tag != DW_TAG_compile_unit
14477 && die->tag != DW_TAG_partial_unit);
418835cc 14478 }
907af001
UW
14479 break;
14480
14481 case DW_TAG_class_type:
14482 case DW_TAG_interface_type:
14483 case DW_TAG_structure_type:
14484 case DW_TAG_union_type:
14485 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14486 structures or unions. These were of the form "._%d" in GCC 4.1,
14487 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14488 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
14489 if (attr && DW_STRING (attr)
14490 && (strncmp (DW_STRING (attr), "._", 2) == 0
14491 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 14492 return NULL;
53832f31
TT
14493
14494 /* GCC might emit a nameless typedef that has a linkage name. See
14495 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14496 if (!attr || DW_STRING (attr) == NULL)
14497 {
df5c6c50 14498 char *demangled = NULL;
53832f31
TT
14499
14500 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14501 if (attr == NULL)
14502 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14503
14504 if (attr == NULL || DW_STRING (attr) == NULL)
14505 return NULL;
14506
df5c6c50
JK
14507 /* Avoid demangling DW_STRING (attr) the second time on a second
14508 call for the same DIE. */
14509 if (!DW_STRING_IS_CANONICAL (attr))
14510 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
14511
14512 if (demangled)
14513 {
96408a79
SA
14514 char *base;
14515
53832f31 14516 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
14517 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14518 &cu->objfile->objfile_obstack);
53832f31
TT
14519 DW_STRING_IS_CANONICAL (attr) = 1;
14520 xfree (demangled);
96408a79
SA
14521
14522 /* Strip any leading namespaces/classes, keep only the base name.
14523 DW_AT_name for named DIEs does not contain the prefixes. */
14524 base = strrchr (DW_STRING (attr), ':');
14525 if (base && base > DW_STRING (attr) && base[-1] == ':')
14526 return &base[1];
14527 else
14528 return DW_STRING (attr);
53832f31
TT
14529 }
14530 }
907af001
UW
14531 break;
14532
71c25dea 14533 default:
907af001
UW
14534 break;
14535 }
14536
14537 if (!DW_STRING_IS_CANONICAL (attr))
14538 {
14539 DW_STRING (attr)
14540 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14541 &cu->objfile->objfile_obstack);
14542 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 14543 }
907af001 14544 return DW_STRING (attr);
9219021c
DC
14545}
14546
14547/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
14548 is none. *EXT_CU is the CU containing DIE on input, and the CU
14549 containing the return value on output. */
9219021c
DC
14550
14551static struct die_info *
f2f0e013 14552dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
14553{
14554 struct attribute *attr;
9219021c 14555
f2f0e013 14556 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
14557 if (attr == NULL)
14558 return NULL;
14559
f2f0e013 14560 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
14561}
14562
c906108c
SS
14563/* Convert a DIE tag into its string name. */
14564
f39c6ffd 14565static const char *
aa1ee363 14566dwarf_tag_name (unsigned tag)
c906108c 14567{
f39c6ffd
TT
14568 const char *name = get_DW_TAG_name (tag);
14569
14570 if (name == NULL)
14571 return "DW_TAG_<unknown>";
14572
14573 return name;
c906108c
SS
14574}
14575
14576/* Convert a DWARF attribute code into its string name. */
14577
f39c6ffd 14578static const char *
aa1ee363 14579dwarf_attr_name (unsigned attr)
c906108c 14580{
f39c6ffd
TT
14581 const char *name;
14582
c764a876 14583#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
14584 if (attr == DW_AT_MIPS_fde)
14585 return "DW_AT_MIPS_fde";
14586#else
14587 if (attr == DW_AT_HP_block_index)
14588 return "DW_AT_HP_block_index";
c764a876 14589#endif
f39c6ffd
TT
14590
14591 name = get_DW_AT_name (attr);
14592
14593 if (name == NULL)
14594 return "DW_AT_<unknown>";
14595
14596 return name;
c906108c
SS
14597}
14598
14599/* Convert a DWARF value form code into its string name. */
14600
f39c6ffd 14601static const char *
aa1ee363 14602dwarf_form_name (unsigned form)
c906108c 14603{
f39c6ffd
TT
14604 const char *name = get_DW_FORM_name (form);
14605
14606 if (name == NULL)
14607 return "DW_FORM_<unknown>";
14608
14609 return name;
c906108c
SS
14610}
14611
14612static char *
fba45db2 14613dwarf_bool_name (unsigned mybool)
c906108c
SS
14614{
14615 if (mybool)
14616 return "TRUE";
14617 else
14618 return "FALSE";
14619}
14620
14621/* Convert a DWARF type code into its string name. */
14622
f39c6ffd 14623static const char *
aa1ee363 14624dwarf_type_encoding_name (unsigned enc)
c906108c 14625{
f39c6ffd 14626 const char *name = get_DW_ATE_name (enc);
c906108c 14627
f39c6ffd
TT
14628 if (name == NULL)
14629 return "DW_ATE_<unknown>";
c906108c 14630
f39c6ffd 14631 return name;
c906108c 14632}
c906108c 14633
f9aca02d 14634static void
d97bc12b 14635dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
14636{
14637 unsigned int i;
14638
d97bc12b
DE
14639 print_spaces (indent, f);
14640 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 14641 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
14642
14643 if (die->parent != NULL)
14644 {
14645 print_spaces (indent, f);
14646 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 14647 die->parent->offset.sect_off);
d97bc12b
DE
14648 }
14649
14650 print_spaces (indent, f);
14651 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 14652 dwarf_bool_name (die->child != NULL));
c906108c 14653
d97bc12b
DE
14654 print_spaces (indent, f);
14655 fprintf_unfiltered (f, " attributes:\n");
14656
c906108c
SS
14657 for (i = 0; i < die->num_attrs; ++i)
14658 {
d97bc12b
DE
14659 print_spaces (indent, f);
14660 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
14661 dwarf_attr_name (die->attrs[i].name),
14662 dwarf_form_name (die->attrs[i].form));
d97bc12b 14663
c906108c
SS
14664 switch (die->attrs[i].form)
14665 {
c906108c 14666 case DW_FORM_addr:
3019eac3 14667 case DW_FORM_GNU_addr_index:
d97bc12b 14668 fprintf_unfiltered (f, "address: ");
5af949e3 14669 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
14670 break;
14671 case DW_FORM_block2:
14672 case DW_FORM_block4:
14673 case DW_FORM_block:
14674 case DW_FORM_block1:
3e43a32a
MS
14675 fprintf_unfiltered (f, "block: size %d",
14676 DW_BLOCK (&die->attrs[i])->size);
c906108c 14677 break;
2dc7f7b3
TT
14678 case DW_FORM_exprloc:
14679 fprintf_unfiltered (f, "expression: size %u",
14680 DW_BLOCK (&die->attrs[i])->size);
14681 break;
4568ecf9
DE
14682 case DW_FORM_ref_addr:
14683 fprintf_unfiltered (f, "ref address: ");
14684 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14685 break;
10b3939b
DJ
14686 case DW_FORM_ref1:
14687 case DW_FORM_ref2:
14688 case DW_FORM_ref4:
4568ecf9
DE
14689 case DW_FORM_ref8:
14690 case DW_FORM_ref_udata:
d97bc12b 14691 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 14692 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 14693 break;
c906108c
SS
14694 case DW_FORM_data1:
14695 case DW_FORM_data2:
14696 case DW_FORM_data4:
ce5d95e1 14697 case DW_FORM_data8:
c906108c
SS
14698 case DW_FORM_udata:
14699 case DW_FORM_sdata:
43bbcdc2
PH
14700 fprintf_unfiltered (f, "constant: %s",
14701 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 14702 break;
2dc7f7b3
TT
14703 case DW_FORM_sec_offset:
14704 fprintf_unfiltered (f, "section offset: %s",
14705 pulongest (DW_UNSND (&die->attrs[i])));
14706 break;
55f1336d 14707 case DW_FORM_ref_sig8:
348e048f
DE
14708 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14709 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 14710 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
14711 else
14712 fprintf_unfiltered (f, "signatured type, offset: unknown");
14713 break;
c906108c 14714 case DW_FORM_string:
4bdf3d34 14715 case DW_FORM_strp:
3019eac3 14716 case DW_FORM_GNU_str_index:
8285870a 14717 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 14718 DW_STRING (&die->attrs[i])
8285870a
JK
14719 ? DW_STRING (&die->attrs[i]) : "",
14720 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
14721 break;
14722 case DW_FORM_flag:
14723 if (DW_UNSND (&die->attrs[i]))
d97bc12b 14724 fprintf_unfiltered (f, "flag: TRUE");
c906108c 14725 else
d97bc12b 14726 fprintf_unfiltered (f, "flag: FALSE");
c906108c 14727 break;
2dc7f7b3
TT
14728 case DW_FORM_flag_present:
14729 fprintf_unfiltered (f, "flag: TRUE");
14730 break;
a8329558 14731 case DW_FORM_indirect:
0963b4bd
MS
14732 /* The reader will have reduced the indirect form to
14733 the "base form" so this form should not occur. */
3e43a32a
MS
14734 fprintf_unfiltered (f,
14735 "unexpected attribute form: DW_FORM_indirect");
a8329558 14736 break;
c906108c 14737 default:
d97bc12b 14738 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 14739 die->attrs[i].form);
d97bc12b 14740 break;
c906108c 14741 }
d97bc12b 14742 fprintf_unfiltered (f, "\n");
c906108c
SS
14743 }
14744}
14745
f9aca02d 14746static void
d97bc12b 14747dump_die_for_error (struct die_info *die)
c906108c 14748{
d97bc12b
DE
14749 dump_die_shallow (gdb_stderr, 0, die);
14750}
14751
14752static void
14753dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14754{
14755 int indent = level * 4;
14756
14757 gdb_assert (die != NULL);
14758
14759 if (level >= max_level)
14760 return;
14761
14762 dump_die_shallow (f, indent, die);
14763
14764 if (die->child != NULL)
c906108c 14765 {
d97bc12b
DE
14766 print_spaces (indent, f);
14767 fprintf_unfiltered (f, " Children:");
14768 if (level + 1 < max_level)
14769 {
14770 fprintf_unfiltered (f, "\n");
14771 dump_die_1 (f, level + 1, max_level, die->child);
14772 }
14773 else
14774 {
3e43a32a
MS
14775 fprintf_unfiltered (f,
14776 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14777 }
14778 }
14779
14780 if (die->sibling != NULL && level > 0)
14781 {
14782 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14783 }
14784}
14785
d97bc12b
DE
14786/* This is called from the pdie macro in gdbinit.in.
14787 It's not static so gcc will keep a copy callable from gdb. */
14788
14789void
14790dump_die (struct die_info *die, int max_level)
14791{
14792 dump_die_1 (gdb_stdlog, 0, max_level, die);
14793}
14794
f9aca02d 14795static void
51545339 14796store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14797{
51545339 14798 void **slot;
c906108c 14799
b64f50a1
JK
14800 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14801 INSERT);
51545339
DJ
14802
14803 *slot = die;
c906108c
SS
14804}
14805
b64f50a1
JK
14806/* DW_ADDR is always stored already as sect_offset; despite for the forms
14807 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14808
93311388
DE
14809static int
14810is_ref_attr (struct attribute *attr)
c906108c 14811{
c906108c
SS
14812 switch (attr->form)
14813 {
14814 case DW_FORM_ref_addr:
c906108c
SS
14815 case DW_FORM_ref1:
14816 case DW_FORM_ref2:
14817 case DW_FORM_ref4:
613e1657 14818 case DW_FORM_ref8:
c906108c 14819 case DW_FORM_ref_udata:
93311388 14820 return 1;
c906108c 14821 default:
93311388 14822 return 0;
c906108c 14823 }
93311388
DE
14824}
14825
b64f50a1
JK
14826/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14827 required kind. */
14828
14829static sect_offset
93311388
DE
14830dwarf2_get_ref_die_offset (struct attribute *attr)
14831{
4568ecf9 14832 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 14833
93311388 14834 if (is_ref_attr (attr))
b64f50a1 14835 return retval;
93311388 14836
b64f50a1 14837 retval.sect_off = 0;
93311388
DE
14838 complaint (&symfile_complaints,
14839 _("unsupported die ref attribute form: '%s'"),
14840 dwarf_form_name (attr->form));
b64f50a1 14841 return retval;
c906108c
SS
14842}
14843
43bbcdc2
PH
14844/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14845 * the value held by the attribute is not constant. */
a02abb62 14846
43bbcdc2 14847static LONGEST
a02abb62
JB
14848dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14849{
14850 if (attr->form == DW_FORM_sdata)
14851 return DW_SND (attr);
14852 else if (attr->form == DW_FORM_udata
14853 || attr->form == DW_FORM_data1
14854 || attr->form == DW_FORM_data2
14855 || attr->form == DW_FORM_data4
14856 || attr->form == DW_FORM_data8)
14857 return DW_UNSND (attr);
14858 else
14859 {
3e43a32a
MS
14860 complaint (&symfile_complaints,
14861 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14862 dwarf_form_name (attr->form));
14863 return default_value;
14864 }
14865}
14866
03dd20cc 14867/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14868 unit and add it to our queue.
14869 The result is non-zero if PER_CU was queued, otherwise the result is zero
14870 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14871
348e048f 14872static int
03dd20cc 14873maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
95554aad
TT
14874 struct dwarf2_per_cu_data *per_cu,
14875 enum language pretend_language)
03dd20cc 14876{
98bfdba5
PA
14877 /* We may arrive here during partial symbol reading, if we need full
14878 DIEs to process an unusual case (e.g. template arguments). Do
14879 not queue PER_CU, just tell our caller to load its DIEs. */
14880 if (dwarf2_per_objfile->reading_partial_symbols)
14881 {
14882 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14883 return 1;
14884 return 0;
14885 }
14886
03dd20cc
DJ
14887 /* Mark the dependence relation so that we don't flush PER_CU
14888 too early. */
14889 dwarf2_add_dependence (this_cu, per_cu);
14890
14891 /* If it's already on the queue, we have nothing to do. */
14892 if (per_cu->queued)
348e048f 14893 return 0;
03dd20cc
DJ
14894
14895 /* If the compilation unit is already loaded, just mark it as
14896 used. */
14897 if (per_cu->cu != NULL)
14898 {
14899 per_cu->cu->last_used = 0;
348e048f 14900 return 0;
03dd20cc
DJ
14901 }
14902
14903 /* Add it to the queue. */
95554aad 14904 queue_comp_unit (per_cu, pretend_language);
348e048f
DE
14905
14906 return 1;
14907}
14908
14909/* Follow reference or signature attribute ATTR of SRC_DIE.
14910 On entry *REF_CU is the CU of SRC_DIE.
14911 On exit *REF_CU is the CU of the result. */
14912
14913static struct die_info *
14914follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14915 struct dwarf2_cu **ref_cu)
14916{
14917 struct die_info *die;
14918
14919 if (is_ref_attr (attr))
14920 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14921 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14922 die = follow_die_sig (src_die, attr, ref_cu);
14923 else
14924 {
14925 dump_die_for_error (src_die);
14926 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14927 (*ref_cu)->objfile->name);
14928 }
14929
14930 return die;
03dd20cc
DJ
14931}
14932
5c631832 14933/* Follow reference OFFSET.
673bfd45
DE
14934 On entry *REF_CU is the CU of the source die referencing OFFSET.
14935 On exit *REF_CU is the CU of the result.
14936 Returns NULL if OFFSET is invalid. */
f504f079 14937
f9aca02d 14938static struct die_info *
b64f50a1 14939follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
c906108c 14940{
10b3939b 14941 struct die_info temp_die;
f2f0e013 14942 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14943
348e048f
DE
14944 gdb_assert (cu->per_cu != NULL);
14945
98bfdba5
PA
14946 target_cu = cu;
14947
3019eac3 14948 if (cu->per_cu->is_debug_types)
348e048f
DE
14949 {
14950 /* .debug_types CUs cannot reference anything outside their CU.
14951 If they need to, they have to reference a signatured type via
55f1336d 14952 DW_FORM_ref_sig8. */
348e048f 14953 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14954 return NULL;
348e048f
DE
14955 }
14956 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14957 {
14958 struct dwarf2_per_cu_data *per_cu;
9a619af0 14959
45452591 14960 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14961
14962 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
14963 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
14964 load_full_comp_unit (per_cu, cu->language);
03dd20cc 14965
10b3939b
DJ
14966 target_cu = per_cu->cu;
14967 }
98bfdba5
PA
14968 else if (cu->dies == NULL)
14969 {
14970 /* We're loading full DIEs during partial symbol reading. */
14971 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 14972 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 14973 }
c906108c 14974
f2f0e013 14975 *ref_cu = target_cu;
51545339 14976 temp_die.offset = offset;
b64f50a1 14977 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 14978}
10b3939b 14979
5c631832
JK
14980/* Follow reference attribute ATTR of SRC_DIE.
14981 On entry *REF_CU is the CU of SRC_DIE.
14982 On exit *REF_CU is the CU of the result. */
14983
14984static struct die_info *
14985follow_die_ref (struct die_info *src_die, struct attribute *attr,
14986 struct dwarf2_cu **ref_cu)
14987{
b64f50a1 14988 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
14989 struct dwarf2_cu *cu = *ref_cu;
14990 struct die_info *die;
14991
14992 die = follow_die_offset (offset, ref_cu);
14993 if (!die)
14994 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14995 "at 0x%x [in module %s]"),
b64f50a1 14996 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 14997
5c631832
JK
14998 return die;
14999}
15000
d83e736b
JK
15001/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
15002 Returned value is intended for DW_OP_call*. Returned
15003 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
15004
15005struct dwarf2_locexpr_baton
b64f50a1 15006dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
15007 struct dwarf2_per_cu_data *per_cu,
15008 CORE_ADDR (*get_frame_pc) (void *baton),
15009 void *baton)
5c631832 15010{
b64f50a1 15011 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 15012 struct dwarf2_cu *cu;
5c631832
JK
15013 struct die_info *die;
15014 struct attribute *attr;
15015 struct dwarf2_locexpr_baton retval;
15016
8cf6f0b1
TT
15017 dw2_setup (per_cu->objfile);
15018
918dd910
JK
15019 if (per_cu->cu == NULL)
15020 load_cu (per_cu);
15021 cu = per_cu->cu;
15022
5c631832
JK
15023 die = follow_die_offset (offset, &cu);
15024 if (!die)
15025 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 15026 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15027
15028 attr = dwarf2_attr (die, DW_AT_location, cu);
15029 if (!attr)
15030 {
e103e986
JK
15031 /* DWARF: "If there is no such attribute, then there is no effect.".
15032 DATA is ignored if SIZE is 0. */
5c631832 15033
e103e986 15034 retval.data = NULL;
5c631832
JK
15035 retval.size = 0;
15036 }
8cf6f0b1
TT
15037 else if (attr_form_is_section_offset (attr))
15038 {
15039 struct dwarf2_loclist_baton loclist_baton;
15040 CORE_ADDR pc = (*get_frame_pc) (baton);
15041 size_t size;
15042
15043 fill_in_loclist_baton (cu, &loclist_baton, attr);
15044
15045 retval.data = dwarf2_find_location_expression (&loclist_baton,
15046 &size, pc);
15047 retval.size = size;
15048 }
5c631832
JK
15049 else
15050 {
15051 if (!attr_form_is_block (attr))
15052 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15053 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 15054 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15055
15056 retval.data = DW_BLOCK (attr)->data;
15057 retval.size = DW_BLOCK (attr)->size;
15058 }
15059 retval.per_cu = cu->per_cu;
918dd910 15060
918dd910
JK
15061 age_cached_comp_units ();
15062
5c631832 15063 return retval;
348e048f
DE
15064}
15065
8a9b8146
TT
15066/* Return the type of the DIE at DIE_OFFSET in the CU named by
15067 PER_CU. */
15068
15069struct type *
b64f50a1 15070dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
15071 struct dwarf2_per_cu_data *per_cu)
15072{
b64f50a1
JK
15073 sect_offset die_offset_sect;
15074
8a9b8146 15075 dw2_setup (per_cu->objfile);
b64f50a1
JK
15076
15077 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15078 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
15079}
15080
348e048f
DE
15081/* Follow the signature attribute ATTR in SRC_DIE.
15082 On entry *REF_CU is the CU of SRC_DIE.
15083 On exit *REF_CU is the CU of the result. */
15084
15085static struct die_info *
15086follow_die_sig (struct die_info *src_die, struct attribute *attr,
15087 struct dwarf2_cu **ref_cu)
15088{
15089 struct objfile *objfile = (*ref_cu)->objfile;
15090 struct die_info temp_die;
15091 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15092 struct dwarf2_cu *sig_cu;
15093 struct die_info *die;
15094
15095 /* sig_type will be NULL if the signatured type is missing from
15096 the debug info. */
15097 if (sig_type == NULL)
15098 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15099 "at 0x%x [in module %s]"),
b64f50a1 15100 src_die->offset.sect_off, objfile->name);
348e048f
DE
15101
15102 /* If necessary, add it to the queue and load its DIEs. */
15103
95554aad 15104 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 15105 read_signatured_type (sig_type);
348e048f
DE
15106
15107 gdb_assert (sig_type->per_cu.cu != NULL);
15108
15109 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
15110 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15111 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
15112 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15113 temp_die.offset.sect_off);
348e048f
DE
15114 if (die)
15115 {
15116 *ref_cu = sig_cu;
15117 return die;
15118 }
15119
3e43a32a
MS
15120 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15121 "from DIE at 0x%x [in module %s]"),
b64f50a1 15122 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
15123}
15124
15125/* Given an offset of a signatured type, return its signatured_type. */
15126
15127static struct signatured_type *
8b70b953
TT
15128lookup_signatured_type_at_offset (struct objfile *objfile,
15129 struct dwarf2_section_info *section,
b64f50a1 15130 sect_offset offset)
348e048f 15131{
b64f50a1 15132 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
15133 unsigned int length, initial_length_size;
15134 unsigned int sig_offset;
52dc124a 15135 struct signatured_type find_entry, *sig_type;
348e048f
DE
15136
15137 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15138 sig_offset = (initial_length_size
15139 + 2 /*version*/
15140 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15141 + 1 /*address_size*/);
15142 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 15143 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
15144
15145 /* This is only used to lookup previously recorded types.
15146 If we didn't find it, it's our bug. */
52dc124a
DE
15147 gdb_assert (sig_type != NULL);
15148 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 15149
52dc124a 15150 return sig_type;
348e048f
DE
15151}
15152
e5fe5e75 15153/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
15154
15155static void
e5fe5e75 15156load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 15157{
e5fe5e75 15158 struct objfile *objfile = per_cu->objfile;
3019eac3 15159 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
b64f50a1 15160 sect_offset offset = per_cu->offset;
52dc124a 15161 struct signatured_type *sig_type;
348e048f 15162
8b70b953 15163 dwarf2_read_section (objfile, sect);
be391dca 15164
348e048f 15165 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
15166 hash table lookup. */
15167 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15168 the signature to assert we found the right one.
15169 Ok, but it's a lot of work. We should simplify things so any needed
15170 assert doesn't require all this clumsiness. */
52dc124a 15171 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f 15172
dee91e82 15173 gdb_assert (&sig_type->per_cu == per_cu);
52dc124a 15174 gdb_assert (sig_type->per_cu.cu == NULL);
348e048f 15175
52dc124a 15176 read_signatured_type (sig_type);
348e048f 15177
52dc124a 15178 gdb_assert (sig_type->per_cu.cu != NULL);
348e048f
DE
15179}
15180
dee91e82
DE
15181/* die_reader_func for read_signatured_type.
15182 This is identical to load_full_comp_unit_reader,
15183 but is kept separate for now. */
348e048f
DE
15184
15185static void
dee91e82
DE
15186read_signatured_type_reader (const struct die_reader_specs *reader,
15187 gdb_byte *info_ptr,
15188 struct die_info *comp_unit_die,
15189 int has_children,
15190 void *data)
348e048f 15191{
dee91e82 15192 struct dwarf2_cu *cu = reader->cu;
348e048f 15193
dee91e82
DE
15194 gdb_assert (cu->die_hash == NULL);
15195 cu->die_hash =
15196 htab_create_alloc_ex (cu->header.length / 12,
15197 die_hash,
15198 die_eq,
15199 NULL,
15200 &cu->comp_unit_obstack,
15201 hashtab_obstack_allocate,
15202 dummy_obstack_deallocate);
348e048f 15203
dee91e82
DE
15204 if (has_children)
15205 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15206 &info_ptr, comp_unit_die);
15207 cu->dies = comp_unit_die;
15208 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
15209
15210 /* We try not to read any attributes in this function, because not
9cdd5dbd 15211 all CUs needed for references have been loaded yet, and symbol
348e048f 15212 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
15213 or we won't be able to build types correctly.
15214 Similarly, if we do not read the producer, we can not apply
15215 producer-specific interpretation. */
95554aad 15216 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 15217}
348e048f 15218
3019eac3
DE
15219/* Read in a signatured type and build its CU and DIEs.
15220 If the type is a stub for the real type in a DWO file,
15221 read in the real type from the DWO file as well. */
dee91e82
DE
15222
15223static void
15224read_signatured_type (struct signatured_type *sig_type)
15225{
15226 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 15227
3019eac3 15228 gdb_assert (per_cu->is_debug_types);
dee91e82 15229 gdb_assert (per_cu->cu == NULL);
348e048f 15230
fd820528 15231 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
c906108c
SS
15232}
15233
c906108c
SS
15234/* Decode simple location descriptions.
15235 Given a pointer to a dwarf block that defines a location, compute
15236 the location and return the value.
15237
4cecd739
DJ
15238 NOTE drow/2003-11-18: This function is called in two situations
15239 now: for the address of static or global variables (partial symbols
15240 only) and for offsets into structures which are expected to be
15241 (more or less) constant. The partial symbol case should go away,
15242 and only the constant case should remain. That will let this
15243 function complain more accurately. A few special modes are allowed
15244 without complaint for global variables (for instance, global
15245 register values and thread-local values).
c906108c
SS
15246
15247 A location description containing no operations indicates that the
4cecd739 15248 object is optimized out. The return value is 0 for that case.
6b992462
DJ
15249 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15250 callers will only want a very basic result and this can become a
21ae7a4d
JK
15251 complaint.
15252
15253 Note that stack[0] is unused except as a default error return. */
c906108c
SS
15254
15255static CORE_ADDR
e7c27a73 15256decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 15257{
e7c27a73 15258 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
15259 int i;
15260 int size = blk->size;
15261 gdb_byte *data = blk->data;
15262 CORE_ADDR stack[64];
15263 int stacki;
15264 unsigned int bytes_read, unsnd;
15265 gdb_byte op;
c906108c 15266
21ae7a4d
JK
15267 i = 0;
15268 stacki = 0;
15269 stack[stacki] = 0;
15270 stack[++stacki] = 0;
15271
15272 while (i < size)
15273 {
15274 op = data[i++];
15275 switch (op)
15276 {
15277 case DW_OP_lit0:
15278 case DW_OP_lit1:
15279 case DW_OP_lit2:
15280 case DW_OP_lit3:
15281 case DW_OP_lit4:
15282 case DW_OP_lit5:
15283 case DW_OP_lit6:
15284 case DW_OP_lit7:
15285 case DW_OP_lit8:
15286 case DW_OP_lit9:
15287 case DW_OP_lit10:
15288 case DW_OP_lit11:
15289 case DW_OP_lit12:
15290 case DW_OP_lit13:
15291 case DW_OP_lit14:
15292 case DW_OP_lit15:
15293 case DW_OP_lit16:
15294 case DW_OP_lit17:
15295 case DW_OP_lit18:
15296 case DW_OP_lit19:
15297 case DW_OP_lit20:
15298 case DW_OP_lit21:
15299 case DW_OP_lit22:
15300 case DW_OP_lit23:
15301 case DW_OP_lit24:
15302 case DW_OP_lit25:
15303 case DW_OP_lit26:
15304 case DW_OP_lit27:
15305 case DW_OP_lit28:
15306 case DW_OP_lit29:
15307 case DW_OP_lit30:
15308 case DW_OP_lit31:
15309 stack[++stacki] = op - DW_OP_lit0;
15310 break;
f1bea926 15311
21ae7a4d
JK
15312 case DW_OP_reg0:
15313 case DW_OP_reg1:
15314 case DW_OP_reg2:
15315 case DW_OP_reg3:
15316 case DW_OP_reg4:
15317 case DW_OP_reg5:
15318 case DW_OP_reg6:
15319 case DW_OP_reg7:
15320 case DW_OP_reg8:
15321 case DW_OP_reg9:
15322 case DW_OP_reg10:
15323 case DW_OP_reg11:
15324 case DW_OP_reg12:
15325 case DW_OP_reg13:
15326 case DW_OP_reg14:
15327 case DW_OP_reg15:
15328 case DW_OP_reg16:
15329 case DW_OP_reg17:
15330 case DW_OP_reg18:
15331 case DW_OP_reg19:
15332 case DW_OP_reg20:
15333 case DW_OP_reg21:
15334 case DW_OP_reg22:
15335 case DW_OP_reg23:
15336 case DW_OP_reg24:
15337 case DW_OP_reg25:
15338 case DW_OP_reg26:
15339 case DW_OP_reg27:
15340 case DW_OP_reg28:
15341 case DW_OP_reg29:
15342 case DW_OP_reg30:
15343 case DW_OP_reg31:
15344 stack[++stacki] = op - DW_OP_reg0;
15345 if (i < size)
15346 dwarf2_complex_location_expr_complaint ();
15347 break;
c906108c 15348
21ae7a4d
JK
15349 case DW_OP_regx:
15350 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15351 i += bytes_read;
15352 stack[++stacki] = unsnd;
15353 if (i < size)
15354 dwarf2_complex_location_expr_complaint ();
15355 break;
c906108c 15356
21ae7a4d
JK
15357 case DW_OP_addr:
15358 stack[++stacki] = read_address (objfile->obfd, &data[i],
15359 cu, &bytes_read);
15360 i += bytes_read;
15361 break;
d53d4ac5 15362
21ae7a4d
JK
15363 case DW_OP_const1u:
15364 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15365 i += 1;
15366 break;
15367
15368 case DW_OP_const1s:
15369 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15370 i += 1;
15371 break;
15372
15373 case DW_OP_const2u:
15374 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15375 i += 2;
15376 break;
15377
15378 case DW_OP_const2s:
15379 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15380 i += 2;
15381 break;
d53d4ac5 15382
21ae7a4d
JK
15383 case DW_OP_const4u:
15384 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15385 i += 4;
15386 break;
15387
15388 case DW_OP_const4s:
15389 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15390 i += 4;
15391 break;
15392
585861ea
JK
15393 case DW_OP_const8u:
15394 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15395 i += 8;
15396 break;
15397
21ae7a4d
JK
15398 case DW_OP_constu:
15399 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15400 &bytes_read);
15401 i += bytes_read;
15402 break;
15403
15404 case DW_OP_consts:
15405 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15406 i += bytes_read;
15407 break;
15408
15409 case DW_OP_dup:
15410 stack[stacki + 1] = stack[stacki];
15411 stacki++;
15412 break;
15413
15414 case DW_OP_plus:
15415 stack[stacki - 1] += stack[stacki];
15416 stacki--;
15417 break;
15418
15419 case DW_OP_plus_uconst:
15420 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15421 &bytes_read);
15422 i += bytes_read;
15423 break;
15424
15425 case DW_OP_minus:
15426 stack[stacki - 1] -= stack[stacki];
15427 stacki--;
15428 break;
15429
15430 case DW_OP_deref:
15431 /* If we're not the last op, then we definitely can't encode
15432 this using GDB's address_class enum. This is valid for partial
15433 global symbols, although the variable's address will be bogus
15434 in the psymtab. */
15435 if (i < size)
15436 dwarf2_complex_location_expr_complaint ();
15437 break;
15438
15439 case DW_OP_GNU_push_tls_address:
15440 /* The top of the stack has the offset from the beginning
15441 of the thread control block at which the variable is located. */
15442 /* Nothing should follow this operator, so the top of stack would
15443 be returned. */
15444 /* This is valid for partial global symbols, but the variable's
585861ea
JK
15445 address will be bogus in the psymtab. Make it always at least
15446 non-zero to not look as a variable garbage collected by linker
15447 which have DW_OP_addr 0. */
21ae7a4d
JK
15448 if (i < size)
15449 dwarf2_complex_location_expr_complaint ();
585861ea 15450 stack[stacki]++;
21ae7a4d
JK
15451 break;
15452
15453 case DW_OP_GNU_uninit:
15454 break;
15455
3019eac3
DE
15456 case DW_OP_GNU_addr_index:
15457 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15458 &bytes_read);
15459 i += bytes_read;
15460 break;
15461
21ae7a4d
JK
15462 default:
15463 {
f39c6ffd 15464 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
15465
15466 if (name)
15467 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15468 name);
15469 else
15470 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15471 op);
15472 }
15473
15474 return (stack[stacki]);
d53d4ac5 15475 }
3c6e0cb3 15476
21ae7a4d
JK
15477 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15478 outside of the allocated space. Also enforce minimum>0. */
15479 if (stacki >= ARRAY_SIZE (stack) - 1)
15480 {
15481 complaint (&symfile_complaints,
15482 _("location description stack overflow"));
15483 return 0;
15484 }
15485
15486 if (stacki <= 0)
15487 {
15488 complaint (&symfile_complaints,
15489 _("location description stack underflow"));
15490 return 0;
15491 }
15492 }
15493 return (stack[stacki]);
c906108c
SS
15494}
15495
15496/* memory allocation interface */
15497
c906108c 15498static struct dwarf_block *
7b5a2f43 15499dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
15500{
15501 struct dwarf_block *blk;
15502
15503 blk = (struct dwarf_block *)
7b5a2f43 15504 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
15505 return (blk);
15506}
15507
15508static struct abbrev_info *
f3dd6933 15509dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
15510{
15511 struct abbrev_info *abbrev;
15512
f3dd6933
DJ
15513 abbrev = (struct abbrev_info *)
15514 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
15515 memset (abbrev, 0, sizeof (struct abbrev_info));
15516 return (abbrev);
15517}
15518
15519static struct die_info *
b60c80d6 15520dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
15521{
15522 struct die_info *die;
b60c80d6
DJ
15523 size_t size = sizeof (struct die_info);
15524
15525 if (num_attrs > 1)
15526 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 15527
b60c80d6 15528 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
15529 memset (die, 0, sizeof (struct die_info));
15530 return (die);
15531}
2e276125
JB
15532
15533\f
15534/* Macro support. */
15535
2e276125
JB
15536/* Return the full name of file number I in *LH's file name table.
15537 Use COMP_DIR as the name of the current directory of the
15538 compilation. The result is allocated using xmalloc; the caller is
15539 responsible for freeing it. */
15540static char *
15541file_full_name (int file, struct line_header *lh, const char *comp_dir)
15542{
6a83a1e6
EZ
15543 /* Is the file number a valid index into the line header's file name
15544 table? Remember that file numbers start with one, not zero. */
15545 if (1 <= file && file <= lh->num_file_names)
15546 {
15547 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 15548
6a83a1e6
EZ
15549 if (IS_ABSOLUTE_PATH (fe->name))
15550 return xstrdup (fe->name);
15551 else
15552 {
15553 const char *dir;
15554 int dir_len;
15555 char *full_name;
15556
15557 if (fe->dir_index)
15558 dir = lh->include_dirs[fe->dir_index - 1];
15559 else
15560 dir = comp_dir;
15561
15562 if (dir)
15563 {
15564 dir_len = strlen (dir);
15565 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15566 strcpy (full_name, dir);
15567 full_name[dir_len] = '/';
15568 strcpy (full_name + dir_len + 1, fe->name);
15569 return full_name;
15570 }
15571 else
15572 return xstrdup (fe->name);
15573 }
15574 }
2e276125
JB
15575 else
15576 {
6a83a1e6
EZ
15577 /* The compiler produced a bogus file number. We can at least
15578 record the macro definitions made in the file, even if we
15579 won't be able to find the file by name. */
15580 char fake_name[80];
9a619af0 15581
6a83a1e6 15582 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 15583
6e70227d 15584 complaint (&symfile_complaints,
6a83a1e6
EZ
15585 _("bad file number in macro information (%d)"),
15586 file);
2e276125 15587
6a83a1e6 15588 return xstrdup (fake_name);
2e276125
JB
15589 }
15590}
15591
15592
15593static struct macro_source_file *
15594macro_start_file (int file, int line,
15595 struct macro_source_file *current_file,
15596 const char *comp_dir,
15597 struct line_header *lh, struct objfile *objfile)
15598{
15599 /* The full name of this source file. */
15600 char *full_name = file_full_name (file, lh, comp_dir);
15601
15602 /* We don't create a macro table for this compilation unit
15603 at all until we actually get a filename. */
15604 if (! pending_macros)
4a146b47 15605 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 15606 objfile->macro_cache);
2e276125
JB
15607
15608 if (! current_file)
abc9d0dc
TT
15609 {
15610 /* If we have no current file, then this must be the start_file
15611 directive for the compilation unit's main source file. */
15612 current_file = macro_set_main (pending_macros, full_name);
15613 macro_define_special (pending_macros);
15614 }
2e276125
JB
15615 else
15616 current_file = macro_include (current_file, line, full_name);
15617
15618 xfree (full_name);
6e70227d 15619
2e276125
JB
15620 return current_file;
15621}
15622
15623
15624/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15625 followed by a null byte. */
15626static char *
15627copy_string (const char *buf, int len)
15628{
15629 char *s = xmalloc (len + 1);
9a619af0 15630
2e276125
JB
15631 memcpy (s, buf, len);
15632 s[len] = '\0';
2e276125
JB
15633 return s;
15634}
15635
15636
15637static const char *
15638consume_improper_spaces (const char *p, const char *body)
15639{
15640 if (*p == ' ')
15641 {
4d3c2250 15642 complaint (&symfile_complaints,
3e43a32a
MS
15643 _("macro definition contains spaces "
15644 "in formal argument list:\n`%s'"),
4d3c2250 15645 body);
2e276125
JB
15646
15647 while (*p == ' ')
15648 p++;
15649 }
15650
15651 return p;
15652}
15653
15654
15655static void
15656parse_macro_definition (struct macro_source_file *file, int line,
15657 const char *body)
15658{
15659 const char *p;
15660
15661 /* The body string takes one of two forms. For object-like macro
15662 definitions, it should be:
15663
15664 <macro name> " " <definition>
15665
15666 For function-like macro definitions, it should be:
15667
15668 <macro name> "() " <definition>
15669 or
15670 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15671
15672 Spaces may appear only where explicitly indicated, and in the
15673 <definition>.
15674
15675 The Dwarf 2 spec says that an object-like macro's name is always
15676 followed by a space, but versions of GCC around March 2002 omit
6e70227d 15677 the space when the macro's definition is the empty string.
2e276125
JB
15678
15679 The Dwarf 2 spec says that there should be no spaces between the
15680 formal arguments in a function-like macro's formal argument list,
15681 but versions of GCC around March 2002 include spaces after the
15682 commas. */
15683
15684
15685 /* Find the extent of the macro name. The macro name is terminated
15686 by either a space or null character (for an object-like macro) or
15687 an opening paren (for a function-like macro). */
15688 for (p = body; *p; p++)
15689 if (*p == ' ' || *p == '(')
15690 break;
15691
15692 if (*p == ' ' || *p == '\0')
15693 {
15694 /* It's an object-like macro. */
15695 int name_len = p - body;
15696 char *name = copy_string (body, name_len);
15697 const char *replacement;
15698
15699 if (*p == ' ')
15700 replacement = body + name_len + 1;
15701 else
15702 {
4d3c2250 15703 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15704 replacement = body + name_len;
15705 }
6e70227d 15706
2e276125
JB
15707 macro_define_object (file, line, name, replacement);
15708
15709 xfree (name);
15710 }
15711 else if (*p == '(')
15712 {
15713 /* It's a function-like macro. */
15714 char *name = copy_string (body, p - body);
15715 int argc = 0;
15716 int argv_size = 1;
15717 char **argv = xmalloc (argv_size * sizeof (*argv));
15718
15719 p++;
15720
15721 p = consume_improper_spaces (p, body);
15722
15723 /* Parse the formal argument list. */
15724 while (*p && *p != ')')
15725 {
15726 /* Find the extent of the current argument name. */
15727 const char *arg_start = p;
15728
15729 while (*p && *p != ',' && *p != ')' && *p != ' ')
15730 p++;
15731
15732 if (! *p || p == arg_start)
4d3c2250 15733 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15734 else
15735 {
15736 /* Make sure argv has room for the new argument. */
15737 if (argc >= argv_size)
15738 {
15739 argv_size *= 2;
15740 argv = xrealloc (argv, argv_size * sizeof (*argv));
15741 }
15742
15743 argv[argc++] = copy_string (arg_start, p - arg_start);
15744 }
15745
15746 p = consume_improper_spaces (p, body);
15747
15748 /* Consume the comma, if present. */
15749 if (*p == ',')
15750 {
15751 p++;
15752
15753 p = consume_improper_spaces (p, body);
15754 }
15755 }
15756
15757 if (*p == ')')
15758 {
15759 p++;
15760
15761 if (*p == ' ')
15762 /* Perfectly formed definition, no complaints. */
15763 macro_define_function (file, line, name,
6e70227d 15764 argc, (const char **) argv,
2e276125
JB
15765 p + 1);
15766 else if (*p == '\0')
15767 {
15768 /* Complain, but do define it. */
4d3c2250 15769 dwarf2_macro_malformed_definition_complaint (body);
2e276125 15770 macro_define_function (file, line, name,
6e70227d 15771 argc, (const char **) argv,
2e276125
JB
15772 p);
15773 }
15774 else
15775 /* Just complain. */
4d3c2250 15776 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15777 }
15778 else
15779 /* Just complain. */
4d3c2250 15780 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15781
15782 xfree (name);
15783 {
15784 int i;
15785
15786 for (i = 0; i < argc; i++)
15787 xfree (argv[i]);
15788 }
15789 xfree (argv);
15790 }
15791 else
4d3c2250 15792 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15793}
15794
cf2c3c16
TT
15795/* Skip some bytes from BYTES according to the form given in FORM.
15796 Returns the new pointer. */
2e276125 15797
cf2c3c16 15798static gdb_byte *
f664829e 15799skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
15800 enum dwarf_form form,
15801 unsigned int offset_size,
15802 struct dwarf2_section_info *section)
2e276125 15803{
cf2c3c16 15804 unsigned int bytes_read;
2e276125 15805
cf2c3c16 15806 switch (form)
2e276125 15807 {
cf2c3c16
TT
15808 case DW_FORM_data1:
15809 case DW_FORM_flag:
15810 ++bytes;
15811 break;
15812
15813 case DW_FORM_data2:
15814 bytes += 2;
15815 break;
15816
15817 case DW_FORM_data4:
15818 bytes += 4;
15819 break;
15820
15821 case DW_FORM_data8:
15822 bytes += 8;
15823 break;
15824
15825 case DW_FORM_string:
15826 read_direct_string (abfd, bytes, &bytes_read);
15827 bytes += bytes_read;
15828 break;
15829
15830 case DW_FORM_sec_offset:
15831 case DW_FORM_strp:
15832 bytes += offset_size;
15833 break;
15834
15835 case DW_FORM_block:
15836 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15837 bytes += bytes_read;
15838 break;
15839
15840 case DW_FORM_block1:
15841 bytes += 1 + read_1_byte (abfd, bytes);
15842 break;
15843 case DW_FORM_block2:
15844 bytes += 2 + read_2_bytes (abfd, bytes);
15845 break;
15846 case DW_FORM_block4:
15847 bytes += 4 + read_4_bytes (abfd, bytes);
15848 break;
15849
15850 case DW_FORM_sdata:
15851 case DW_FORM_udata:
3019eac3
DE
15852 case DW_FORM_GNU_addr_index:
15853 case DW_FORM_GNU_str_index:
f664829e
DE
15854 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
15855 if (bytes == NULL)
15856 {
15857 dwarf2_section_buffer_overflow_complaint (section);
15858 return NULL;
15859 }
cf2c3c16
TT
15860 break;
15861
15862 default:
15863 {
15864 complain:
15865 complaint (&symfile_complaints,
15866 _("invalid form 0x%x in `%s'"),
15867 form,
15868 section->asection->name);
15869 return NULL;
15870 }
2e276125
JB
15871 }
15872
cf2c3c16
TT
15873 return bytes;
15874}
757a13d0 15875
cf2c3c16
TT
15876/* A helper for dwarf_decode_macros that handles skipping an unknown
15877 opcode. Returns an updated pointer to the macro data buffer; or,
15878 on error, issues a complaint and returns NULL. */
757a13d0 15879
cf2c3c16
TT
15880static gdb_byte *
15881skip_unknown_opcode (unsigned int opcode,
15882 gdb_byte **opcode_definitions,
f664829e 15883 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
15884 bfd *abfd,
15885 unsigned int offset_size,
15886 struct dwarf2_section_info *section)
15887{
15888 unsigned int bytes_read, i;
15889 unsigned long arg;
15890 gdb_byte *defn;
2e276125 15891
cf2c3c16 15892 if (opcode_definitions[opcode] == NULL)
2e276125 15893 {
cf2c3c16
TT
15894 complaint (&symfile_complaints,
15895 _("unrecognized DW_MACFINO opcode 0x%x"),
15896 opcode);
15897 return NULL;
15898 }
2e276125 15899
cf2c3c16
TT
15900 defn = opcode_definitions[opcode];
15901 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15902 defn += bytes_read;
2e276125 15903
cf2c3c16
TT
15904 for (i = 0; i < arg; ++i)
15905 {
f664829e
DE
15906 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
15907 section);
cf2c3c16
TT
15908 if (mac_ptr == NULL)
15909 {
15910 /* skip_form_bytes already issued the complaint. */
15911 return NULL;
15912 }
15913 }
757a13d0 15914
cf2c3c16
TT
15915 return mac_ptr;
15916}
757a13d0 15917
cf2c3c16
TT
15918/* A helper function which parses the header of a macro section.
15919 If the macro section is the extended (for now called "GNU") type,
15920 then this updates *OFFSET_SIZE. Returns a pointer to just after
15921 the header, or issues a complaint and returns NULL on error. */
757a13d0 15922
cf2c3c16
TT
15923static gdb_byte *
15924dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15925 bfd *abfd,
15926 gdb_byte *mac_ptr,
15927 unsigned int *offset_size,
15928 int section_is_gnu)
15929{
15930 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15931
cf2c3c16
TT
15932 if (section_is_gnu)
15933 {
15934 unsigned int version, flags;
757a13d0 15935
cf2c3c16
TT
15936 version = read_2_bytes (abfd, mac_ptr);
15937 if (version != 4)
15938 {
15939 complaint (&symfile_complaints,
15940 _("unrecognized version `%d' in .debug_macro section"),
15941 version);
15942 return NULL;
15943 }
15944 mac_ptr += 2;
757a13d0 15945
cf2c3c16
TT
15946 flags = read_1_byte (abfd, mac_ptr);
15947 ++mac_ptr;
15948 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15949
cf2c3c16
TT
15950 if ((flags & 2) != 0)
15951 /* We don't need the line table offset. */
15952 mac_ptr += *offset_size;
757a13d0 15953
cf2c3c16
TT
15954 /* Vendor opcode descriptions. */
15955 if ((flags & 4) != 0)
15956 {
15957 unsigned int i, count;
757a13d0 15958
cf2c3c16
TT
15959 count = read_1_byte (abfd, mac_ptr);
15960 ++mac_ptr;
15961 for (i = 0; i < count; ++i)
15962 {
15963 unsigned int opcode, bytes_read;
15964 unsigned long arg;
15965
15966 opcode = read_1_byte (abfd, mac_ptr);
15967 ++mac_ptr;
15968 opcode_definitions[opcode] = mac_ptr;
15969 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15970 mac_ptr += bytes_read;
15971 mac_ptr += arg;
15972 }
757a13d0 15973 }
cf2c3c16 15974 }
757a13d0 15975
cf2c3c16
TT
15976 return mac_ptr;
15977}
757a13d0 15978
cf2c3c16 15979/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 15980 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
15981
15982static void
15983dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15984 struct macro_source_file *current_file,
15985 struct line_header *lh, char *comp_dir,
15986 struct dwarf2_section_info *section,
15987 int section_is_gnu,
15988 unsigned int offset_size,
8fc3fc34
TT
15989 struct objfile *objfile,
15990 htab_t include_hash)
cf2c3c16
TT
15991{
15992 enum dwarf_macro_record_type macinfo_type;
15993 int at_commandline;
15994 gdb_byte *opcode_definitions[256];
757a13d0 15995
cf2c3c16
TT
15996 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15997 &offset_size, section_is_gnu);
15998 if (mac_ptr == NULL)
15999 {
16000 /* We already issued a complaint. */
16001 return;
16002 }
757a13d0
JK
16003
16004 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
16005 GDB is still reading the definitions from command line. First
16006 DW_MACINFO_start_file will need to be ignored as it was already executed
16007 to create CURRENT_FILE for the main source holding also the command line
16008 definitions. On first met DW_MACINFO_start_file this flag is reset to
16009 normally execute all the remaining DW_MACINFO_start_file macinfos. */
16010
16011 at_commandline = 1;
16012
16013 do
16014 {
16015 /* Do we at least have room for a macinfo type byte? */
16016 if (mac_ptr >= mac_end)
16017 {
f664829e 16018 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
16019 break;
16020 }
16021
16022 macinfo_type = read_1_byte (abfd, mac_ptr);
16023 mac_ptr++;
16024
cf2c3c16
TT
16025 /* Note that we rely on the fact that the corresponding GNU and
16026 DWARF constants are the same. */
757a13d0
JK
16027 switch (macinfo_type)
16028 {
16029 /* A zero macinfo type indicates the end of the macro
16030 information. */
16031 case 0:
16032 break;
2e276125 16033
cf2c3c16
TT
16034 case DW_MACRO_GNU_define:
16035 case DW_MACRO_GNU_undef:
16036 case DW_MACRO_GNU_define_indirect:
16037 case DW_MACRO_GNU_undef_indirect:
2e276125 16038 {
891d2f0b 16039 unsigned int bytes_read;
2e276125
JB
16040 int line;
16041 char *body;
cf2c3c16 16042 int is_define;
2e276125 16043
cf2c3c16
TT
16044 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16045 mac_ptr += bytes_read;
16046
16047 if (macinfo_type == DW_MACRO_GNU_define
16048 || macinfo_type == DW_MACRO_GNU_undef)
16049 {
16050 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16051 mac_ptr += bytes_read;
16052 }
16053 else
16054 {
16055 LONGEST str_offset;
16056
16057 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16058 mac_ptr += offset_size;
2e276125 16059
cf2c3c16
TT
16060 body = read_indirect_string_at_offset (abfd, str_offset);
16061 }
16062
16063 is_define = (macinfo_type == DW_MACRO_GNU_define
16064 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 16065 if (! current_file)
757a13d0
JK
16066 {
16067 /* DWARF violation as no main source is present. */
16068 complaint (&symfile_complaints,
16069 _("debug info with no main source gives macro %s "
16070 "on line %d: %s"),
cf2c3c16
TT
16071 is_define ? _("definition") : _("undefinition"),
16072 line, body);
757a13d0
JK
16073 break;
16074 }
3e43a32a
MS
16075 if ((line == 0 && !at_commandline)
16076 || (line != 0 && at_commandline))
4d3c2250 16077 complaint (&symfile_complaints,
757a13d0
JK
16078 _("debug info gives %s macro %s with %s line %d: %s"),
16079 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 16080 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
16081 line == 0 ? _("zero") : _("non-zero"), line, body);
16082
cf2c3c16 16083 if (is_define)
757a13d0 16084 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
16085 else
16086 {
16087 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16088 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16089 macro_undef (current_file, line, body);
16090 }
2e276125
JB
16091 }
16092 break;
16093
cf2c3c16 16094 case DW_MACRO_GNU_start_file:
2e276125 16095 {
891d2f0b 16096 unsigned int bytes_read;
2e276125
JB
16097 int line, file;
16098
16099 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16100 mac_ptr += bytes_read;
16101 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16102 mac_ptr += bytes_read;
16103
3e43a32a
MS
16104 if ((line == 0 && !at_commandline)
16105 || (line != 0 && at_commandline))
757a13d0
JK
16106 complaint (&symfile_complaints,
16107 _("debug info gives source %d included "
16108 "from %s at %s line %d"),
16109 file, at_commandline ? _("command-line") : _("file"),
16110 line == 0 ? _("zero") : _("non-zero"), line);
16111
16112 if (at_commandline)
16113 {
cf2c3c16
TT
16114 /* This DW_MACRO_GNU_start_file was executed in the
16115 pass one. */
757a13d0
JK
16116 at_commandline = 0;
16117 }
16118 else
16119 current_file = macro_start_file (file, line,
16120 current_file, comp_dir,
cf2c3c16 16121 lh, objfile);
2e276125
JB
16122 }
16123 break;
16124
cf2c3c16 16125 case DW_MACRO_GNU_end_file:
2e276125 16126 if (! current_file)
4d3c2250 16127 complaint (&symfile_complaints,
3e43a32a
MS
16128 _("macro debug info has an unmatched "
16129 "`close_file' directive"));
2e276125
JB
16130 else
16131 {
16132 current_file = current_file->included_by;
16133 if (! current_file)
16134 {
cf2c3c16 16135 enum dwarf_macro_record_type next_type;
2e276125
JB
16136
16137 /* GCC circa March 2002 doesn't produce the zero
16138 type byte marking the end of the compilation
16139 unit. Complain if it's not there, but exit no
16140 matter what. */
16141
16142 /* Do we at least have room for a macinfo type byte? */
16143 if (mac_ptr >= mac_end)
16144 {
f664829e 16145 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
16146 return;
16147 }
16148
16149 /* We don't increment mac_ptr here, so this is just
16150 a look-ahead. */
16151 next_type = read_1_byte (abfd, mac_ptr);
16152 if (next_type != 0)
4d3c2250 16153 complaint (&symfile_complaints,
3e43a32a
MS
16154 _("no terminating 0-type entry for "
16155 "macros in `.debug_macinfo' section"));
2e276125
JB
16156
16157 return;
16158 }
16159 }
16160 break;
16161
cf2c3c16
TT
16162 case DW_MACRO_GNU_transparent_include:
16163 {
16164 LONGEST offset;
8fc3fc34 16165 void **slot;
cf2c3c16
TT
16166
16167 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16168 mac_ptr += offset_size;
16169
8fc3fc34
TT
16170 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16171 if (*slot != NULL)
16172 {
16173 /* This has actually happened; see
16174 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16175 complaint (&symfile_complaints,
16176 _("recursive DW_MACRO_GNU_transparent_include in "
16177 ".debug_macro section"));
16178 }
16179 else
16180 {
16181 *slot = mac_ptr;
16182
16183 dwarf_decode_macro_bytes (abfd,
16184 section->buffer + offset,
16185 mac_end, current_file,
16186 lh, comp_dir,
16187 section, section_is_gnu,
16188 offset_size, objfile, include_hash);
16189
16190 htab_remove_elt (include_hash, mac_ptr);
16191 }
cf2c3c16
TT
16192 }
16193 break;
16194
2e276125 16195 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
16196 if (!section_is_gnu)
16197 {
16198 unsigned int bytes_read;
16199 int constant;
2e276125 16200
cf2c3c16
TT
16201 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16202 mac_ptr += bytes_read;
16203 read_direct_string (abfd, mac_ptr, &bytes_read);
16204 mac_ptr += bytes_read;
2e276125 16205
cf2c3c16
TT
16206 /* We don't recognize any vendor extensions. */
16207 break;
16208 }
16209 /* FALLTHROUGH */
16210
16211 default:
16212 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 16213 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
16214 section);
16215 if (mac_ptr == NULL)
16216 return;
16217 break;
2e276125 16218 }
757a13d0 16219 } while (macinfo_type != 0);
2e276125 16220}
8e19ed76 16221
cf2c3c16
TT
16222static void
16223dwarf_decode_macros (struct line_header *lh, unsigned int offset,
16224 char *comp_dir, bfd *abfd,
16225 struct dwarf2_cu *cu,
16226 struct dwarf2_section_info *section,
fceca515 16227 int section_is_gnu, const char *section_name)
cf2c3c16 16228{
bb5ed363 16229 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
16230 gdb_byte *mac_ptr, *mac_end;
16231 struct macro_source_file *current_file = 0;
16232 enum dwarf_macro_record_type macinfo_type;
16233 unsigned int offset_size = cu->header.offset_size;
16234 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
16235 struct cleanup *cleanup;
16236 htab_t include_hash;
16237 void **slot;
cf2c3c16 16238
bb5ed363 16239 dwarf2_read_section (objfile, section);
cf2c3c16
TT
16240 if (section->buffer == NULL)
16241 {
fceca515 16242 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
16243 return;
16244 }
16245
16246 /* First pass: Find the name of the base filename.
16247 This filename is needed in order to process all macros whose definition
16248 (or undefinition) comes from the command line. These macros are defined
16249 before the first DW_MACINFO_start_file entry, and yet still need to be
16250 associated to the base file.
16251
16252 To determine the base file name, we scan the macro definitions until we
16253 reach the first DW_MACINFO_start_file entry. We then initialize
16254 CURRENT_FILE accordingly so that any macro definition found before the
16255 first DW_MACINFO_start_file can still be associated to the base file. */
16256
16257 mac_ptr = section->buffer + offset;
16258 mac_end = section->buffer + section->size;
16259
16260 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16261 &offset_size, section_is_gnu);
16262 if (mac_ptr == NULL)
16263 {
16264 /* We already issued a complaint. */
16265 return;
16266 }
16267
16268 do
16269 {
16270 /* Do we at least have room for a macinfo type byte? */
16271 if (mac_ptr >= mac_end)
16272 {
16273 /* Complaint is printed during the second pass as GDB will probably
16274 stop the first pass earlier upon finding
16275 DW_MACINFO_start_file. */
16276 break;
16277 }
16278
16279 macinfo_type = read_1_byte (abfd, mac_ptr);
16280 mac_ptr++;
16281
16282 /* Note that we rely on the fact that the corresponding GNU and
16283 DWARF constants are the same. */
16284 switch (macinfo_type)
16285 {
16286 /* A zero macinfo type indicates the end of the macro
16287 information. */
16288 case 0:
16289 break;
16290
16291 case DW_MACRO_GNU_define:
16292 case DW_MACRO_GNU_undef:
16293 /* Only skip the data by MAC_PTR. */
16294 {
16295 unsigned int bytes_read;
16296
16297 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16298 mac_ptr += bytes_read;
16299 read_direct_string (abfd, mac_ptr, &bytes_read);
16300 mac_ptr += bytes_read;
16301 }
16302 break;
16303
16304 case DW_MACRO_GNU_start_file:
16305 {
16306 unsigned int bytes_read;
16307 int line, file;
16308
16309 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16310 mac_ptr += bytes_read;
16311 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16312 mac_ptr += bytes_read;
16313
16314 current_file = macro_start_file (file, line, current_file,
bb5ed363 16315 comp_dir, lh, objfile);
cf2c3c16
TT
16316 }
16317 break;
16318
16319 case DW_MACRO_GNU_end_file:
16320 /* No data to skip by MAC_PTR. */
16321 break;
16322
16323 case DW_MACRO_GNU_define_indirect:
16324 case DW_MACRO_GNU_undef_indirect:
16325 {
16326 unsigned int bytes_read;
16327
16328 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16329 mac_ptr += bytes_read;
16330 mac_ptr += offset_size;
16331 }
16332 break;
16333
16334 case DW_MACRO_GNU_transparent_include:
16335 /* Note that, according to the spec, a transparent include
16336 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16337 skip this opcode. */
16338 mac_ptr += offset_size;
16339 break;
16340
16341 case DW_MACINFO_vendor_ext:
16342 /* Only skip the data by MAC_PTR. */
16343 if (!section_is_gnu)
16344 {
16345 unsigned int bytes_read;
16346
16347 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16348 mac_ptr += bytes_read;
16349 read_direct_string (abfd, mac_ptr, &bytes_read);
16350 mac_ptr += bytes_read;
16351 }
16352 /* FALLTHROUGH */
16353
16354 default:
16355 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 16356 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
16357 section);
16358 if (mac_ptr == NULL)
16359 return;
16360 break;
16361 }
16362 } while (macinfo_type != 0 && current_file == NULL);
16363
16364 /* Second pass: Process all entries.
16365
16366 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16367 command-line macro definitions/undefinitions. This flag is unset when we
16368 reach the first DW_MACINFO_start_file entry. */
16369
8fc3fc34
TT
16370 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16371 NULL, xcalloc, xfree);
16372 cleanup = make_cleanup_htab_delete (include_hash);
16373 mac_ptr = section->buffer + offset;
16374 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16375 *slot = mac_ptr;
16376 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
cf2c3c16 16377 current_file, lh, comp_dir, section, section_is_gnu,
8fc3fc34
TT
16378 offset_size, objfile, include_hash);
16379 do_cleanups (cleanup);
cf2c3c16
TT
16380}
16381
8e19ed76 16382/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 16383 if so return true else false. */
380bca97 16384
8e19ed76
PS
16385static int
16386attr_form_is_block (struct attribute *attr)
16387{
16388 return (attr == NULL ? 0 :
16389 attr->form == DW_FORM_block1
16390 || attr->form == DW_FORM_block2
16391 || attr->form == DW_FORM_block4
2dc7f7b3
TT
16392 || attr->form == DW_FORM_block
16393 || attr->form == DW_FORM_exprloc);
8e19ed76 16394}
4c2df51b 16395
c6a0999f
JB
16396/* Return non-zero if ATTR's value is a section offset --- classes
16397 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16398 You may use DW_UNSND (attr) to retrieve such offsets.
16399
16400 Section 7.5.4, "Attribute Encodings", explains that no attribute
16401 may have a value that belongs to more than one of these classes; it
16402 would be ambiguous if we did, because we use the same forms for all
16403 of them. */
380bca97 16404
3690dd37
JB
16405static int
16406attr_form_is_section_offset (struct attribute *attr)
16407{
16408 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
16409 || attr->form == DW_FORM_data8
16410 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
16411}
16412
3690dd37
JB
16413/* Return non-zero if ATTR's value falls in the 'constant' class, or
16414 zero otherwise. When this function returns true, you can apply
16415 dwarf2_get_attr_constant_value to it.
16416
16417 However, note that for some attributes you must check
16418 attr_form_is_section_offset before using this test. DW_FORM_data4
16419 and DW_FORM_data8 are members of both the constant class, and of
16420 the classes that contain offsets into other debug sections
16421 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16422 that, if an attribute's can be either a constant or one of the
16423 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16424 taken as section offsets, not constants. */
380bca97 16425
3690dd37
JB
16426static int
16427attr_form_is_constant (struct attribute *attr)
16428{
16429 switch (attr->form)
16430 {
16431 case DW_FORM_sdata:
16432 case DW_FORM_udata:
16433 case DW_FORM_data1:
16434 case DW_FORM_data2:
16435 case DW_FORM_data4:
16436 case DW_FORM_data8:
16437 return 1;
16438 default:
16439 return 0;
16440 }
16441}
16442
3019eac3
DE
16443/* Return the .debug_loc section to use for CU.
16444 For DWO files use .debug_loc.dwo. */
16445
16446static struct dwarf2_section_info *
16447cu_debug_loc_section (struct dwarf2_cu *cu)
16448{
16449 if (cu->dwo_unit)
16450 return &cu->dwo_unit->dwo_file->sections.loc;
16451 return &dwarf2_per_objfile->loc;
16452}
16453
8cf6f0b1
TT
16454/* A helper function that fills in a dwarf2_loclist_baton. */
16455
16456static void
16457fill_in_loclist_baton (struct dwarf2_cu *cu,
16458 struct dwarf2_loclist_baton *baton,
16459 struct attribute *attr)
16460{
3019eac3
DE
16461 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16462
16463 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
16464
16465 baton->per_cu = cu->per_cu;
16466 gdb_assert (baton->per_cu);
16467 /* We don't know how long the location list is, but make sure we
16468 don't run off the edge of the section. */
3019eac3
DE
16469 baton->size = section->size - DW_UNSND (attr);
16470 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 16471 baton->base_address = cu->base_address;
f664829e 16472 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
16473}
16474
4c2df51b
DJ
16475static void
16476dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 16477 struct dwarf2_cu *cu)
4c2df51b 16478{
bb5ed363 16479 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 16480 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 16481
3690dd37 16482 if (attr_form_is_section_offset (attr)
3019eac3 16483 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
16484 the section. If so, fall through to the complaint in the
16485 other branch. */
3019eac3 16486 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 16487 {
0d53c4c4 16488 struct dwarf2_loclist_baton *baton;
4c2df51b 16489
bb5ed363 16490 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16491 sizeof (struct dwarf2_loclist_baton));
4c2df51b 16492
8cf6f0b1 16493 fill_in_loclist_baton (cu, baton, attr);
be391dca 16494
d00adf39 16495 if (cu->base_known == 0)
0d53c4c4 16496 complaint (&symfile_complaints,
3e43a32a
MS
16497 _("Location list used without "
16498 "specifying the CU base address."));
4c2df51b 16499
768a979c 16500 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
16501 SYMBOL_LOCATION_BATON (sym) = baton;
16502 }
16503 else
16504 {
16505 struct dwarf2_locexpr_baton *baton;
16506
bb5ed363 16507 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16508 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
16509 baton->per_cu = cu->per_cu;
16510 gdb_assert (baton->per_cu);
0d53c4c4
DJ
16511
16512 if (attr_form_is_block (attr))
16513 {
16514 /* Note that we're just copying the block's data pointer
16515 here, not the actual data. We're still pointing into the
6502dd73
DJ
16516 info_buffer for SYM's objfile; right now we never release
16517 that buffer, but when we do clean up properly this may
16518 need to change. */
0d53c4c4
DJ
16519 baton->size = DW_BLOCK (attr)->size;
16520 baton->data = DW_BLOCK (attr)->data;
16521 }
16522 else
16523 {
16524 dwarf2_invalid_attrib_class_complaint ("location description",
16525 SYMBOL_NATURAL_NAME (sym));
16526 baton->size = 0;
0d53c4c4 16527 }
6e70227d 16528
768a979c 16529 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
16530 SYMBOL_LOCATION_BATON (sym) = baton;
16531 }
4c2df51b 16532}
6502dd73 16533
9aa1f1e3
TT
16534/* Return the OBJFILE associated with the compilation unit CU. If CU
16535 came from a separate debuginfo file, then the master objfile is
16536 returned. */
ae0d2f24
UW
16537
16538struct objfile *
16539dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16540{
9291a0cd 16541 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
16542
16543 /* Return the master objfile, so that we can report and look up the
16544 correct file containing this variable. */
16545 if (objfile->separate_debug_objfile_backlink)
16546 objfile = objfile->separate_debug_objfile_backlink;
16547
16548 return objfile;
16549}
16550
96408a79
SA
16551/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16552 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16553 CU_HEADERP first. */
16554
16555static const struct comp_unit_head *
16556per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16557 struct dwarf2_per_cu_data *per_cu)
16558{
16559 struct objfile *objfile;
16560 struct dwarf2_per_objfile *per_objfile;
16561 gdb_byte *info_ptr;
16562
16563 if (per_cu->cu)
16564 return &per_cu->cu->header;
16565
16566 objfile = per_cu->objfile;
16567 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
b64f50a1 16568 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
96408a79
SA
16569
16570 memset (cu_headerp, 0, sizeof (*cu_headerp));
16571 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16572
16573 return cu_headerp;
16574}
16575
ae0d2f24
UW
16576/* Return the address size given in the compilation unit header for CU. */
16577
98714339 16578int
ae0d2f24
UW
16579dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16580{
96408a79
SA
16581 struct comp_unit_head cu_header_local;
16582 const struct comp_unit_head *cu_headerp;
c471e790 16583
96408a79
SA
16584 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16585
16586 return cu_headerp->addr_size;
ae0d2f24
UW
16587}
16588
9eae7c52
TT
16589/* Return the offset size given in the compilation unit header for CU. */
16590
16591int
16592dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16593{
96408a79
SA
16594 struct comp_unit_head cu_header_local;
16595 const struct comp_unit_head *cu_headerp;
9c6c53f7 16596
96408a79
SA
16597 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16598
16599 return cu_headerp->offset_size;
16600}
16601
16602/* See its dwarf2loc.h declaration. */
16603
16604int
16605dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16606{
16607 struct comp_unit_head cu_header_local;
16608 const struct comp_unit_head *cu_headerp;
16609
16610 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16611
16612 if (cu_headerp->version == 2)
16613 return cu_headerp->addr_size;
16614 else
16615 return cu_headerp->offset_size;
181cebd4
JK
16616}
16617
9aa1f1e3
TT
16618/* Return the text offset of the CU. The returned offset comes from
16619 this CU's objfile. If this objfile came from a separate debuginfo
16620 file, then the offset may be different from the corresponding
16621 offset in the parent objfile. */
16622
16623CORE_ADDR
16624dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16625{
bb3fa9d0 16626 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
16627
16628 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16629}
16630
348e048f
DE
16631/* Locate the .debug_info compilation unit from CU's objfile which contains
16632 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
16633
16634static struct dwarf2_per_cu_data *
b64f50a1 16635dwarf2_find_containing_comp_unit (sect_offset offset,
ae038cb0
DJ
16636 struct objfile *objfile)
16637{
16638 struct dwarf2_per_cu_data *this_cu;
16639 int low, high;
16640
ae038cb0
DJ
16641 low = 0;
16642 high = dwarf2_per_objfile->n_comp_units - 1;
16643 while (high > low)
16644 {
16645 int mid = low + (high - low) / 2;
9a619af0 16646
b64f50a1
JK
16647 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16648 >= offset.sect_off)
ae038cb0
DJ
16649 high = mid;
16650 else
16651 low = mid + 1;
16652 }
16653 gdb_assert (low == high);
b64f50a1
JK
16654 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16655 > offset.sect_off)
ae038cb0 16656 {
10b3939b 16657 if (low == 0)
8a3fe4f8
AC
16658 error (_("Dwarf Error: could not find partial DIE containing "
16659 "offset 0x%lx [in module %s]"),
b64f50a1 16660 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 16661
b64f50a1
JK
16662 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16663 <= offset.sect_off);
ae038cb0
DJ
16664 return dwarf2_per_objfile->all_comp_units[low-1];
16665 }
16666 else
16667 {
16668 this_cu = dwarf2_per_objfile->all_comp_units[low];
16669 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
16670 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16671 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16672 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
16673 return this_cu;
16674 }
16675}
16676
23745b47 16677/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 16678
9816fde3 16679static void
23745b47 16680init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 16681{
9816fde3 16682 memset (cu, 0, sizeof (*cu));
23745b47
DE
16683 per_cu->cu = cu;
16684 cu->per_cu = per_cu;
16685 cu->objfile = per_cu->objfile;
93311388 16686 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
16687}
16688
16689/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16690
16691static void
95554aad
TT
16692prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
16693 enum language pretend_language)
9816fde3
JK
16694{
16695 struct attribute *attr;
16696
16697 /* Set the language we're debugging. */
16698 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16699 if (attr)
16700 set_cu_language (DW_UNSND (attr), cu);
16701 else
9cded63f 16702 {
95554aad 16703 cu->language = pretend_language;
9cded63f
TT
16704 cu->language_defn = language_def (cu->language);
16705 }
dee91e82
DE
16706
16707 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
16708 if (attr)
16709 cu->producer = DW_STRING (attr);
93311388
DE
16710}
16711
ae038cb0
DJ
16712/* Release one cached compilation unit, CU. We unlink it from the tree
16713 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
16714 the caller is responsible for that.
16715 NOTE: DATA is a void * because this function is also used as a
16716 cleanup routine. */
ae038cb0
DJ
16717
16718static void
68dc6402 16719free_heap_comp_unit (void *data)
ae038cb0
DJ
16720{
16721 struct dwarf2_cu *cu = data;
16722
23745b47
DE
16723 gdb_assert (cu->per_cu != NULL);
16724 cu->per_cu->cu = NULL;
ae038cb0
DJ
16725 cu->per_cu = NULL;
16726
16727 obstack_free (&cu->comp_unit_obstack, NULL);
16728
16729 xfree (cu);
16730}
16731
72bf9492 16732/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 16733 when we're finished with it. We can't free the pointer itself, but be
dee91e82 16734 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
16735
16736static void
16737free_stack_comp_unit (void *data)
16738{
16739 struct dwarf2_cu *cu = data;
16740
23745b47
DE
16741 gdb_assert (cu->per_cu != NULL);
16742 cu->per_cu->cu = NULL;
16743 cu->per_cu = NULL;
16744
72bf9492
DJ
16745 obstack_free (&cu->comp_unit_obstack, NULL);
16746 cu->partial_dies = NULL;
ae038cb0
DJ
16747}
16748
16749/* Free all cached compilation units. */
16750
16751static void
16752free_cached_comp_units (void *data)
16753{
16754 struct dwarf2_per_cu_data *per_cu, **last_chain;
16755
16756 per_cu = dwarf2_per_objfile->read_in_chain;
16757 last_chain = &dwarf2_per_objfile->read_in_chain;
16758 while (per_cu != NULL)
16759 {
16760 struct dwarf2_per_cu_data *next_cu;
16761
16762 next_cu = per_cu->cu->read_in_chain;
16763
68dc6402 16764 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16765 *last_chain = next_cu;
16766
16767 per_cu = next_cu;
16768 }
16769}
16770
16771/* Increase the age counter on each cached compilation unit, and free
16772 any that are too old. */
16773
16774static void
16775age_cached_comp_units (void)
16776{
16777 struct dwarf2_per_cu_data *per_cu, **last_chain;
16778
16779 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16780 per_cu = dwarf2_per_objfile->read_in_chain;
16781 while (per_cu != NULL)
16782 {
16783 per_cu->cu->last_used ++;
16784 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16785 dwarf2_mark (per_cu->cu);
16786 per_cu = per_cu->cu->read_in_chain;
16787 }
16788
16789 per_cu = dwarf2_per_objfile->read_in_chain;
16790 last_chain = &dwarf2_per_objfile->read_in_chain;
16791 while (per_cu != NULL)
16792 {
16793 struct dwarf2_per_cu_data *next_cu;
16794
16795 next_cu = per_cu->cu->read_in_chain;
16796
16797 if (!per_cu->cu->mark)
16798 {
68dc6402 16799 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16800 *last_chain = next_cu;
16801 }
16802 else
16803 last_chain = &per_cu->cu->read_in_chain;
16804
16805 per_cu = next_cu;
16806 }
16807}
16808
16809/* Remove a single compilation unit from the cache. */
16810
16811static void
dee91e82 16812free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
16813{
16814 struct dwarf2_per_cu_data *per_cu, **last_chain;
16815
16816 per_cu = dwarf2_per_objfile->read_in_chain;
16817 last_chain = &dwarf2_per_objfile->read_in_chain;
16818 while (per_cu != NULL)
16819 {
16820 struct dwarf2_per_cu_data *next_cu;
16821
16822 next_cu = per_cu->cu->read_in_chain;
16823
dee91e82 16824 if (per_cu == target_per_cu)
ae038cb0 16825 {
68dc6402 16826 free_heap_comp_unit (per_cu->cu);
dee91e82 16827 per_cu->cu = NULL;
ae038cb0
DJ
16828 *last_chain = next_cu;
16829 break;
16830 }
16831 else
16832 last_chain = &per_cu->cu->read_in_chain;
16833
16834 per_cu = next_cu;
16835 }
16836}
16837
fe3e1990
DJ
16838/* Release all extra memory associated with OBJFILE. */
16839
16840void
16841dwarf2_free_objfile (struct objfile *objfile)
16842{
16843 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16844
16845 if (dwarf2_per_objfile == NULL)
16846 return;
16847
16848 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16849 free_cached_comp_units (NULL);
16850
7b9f3c50
DE
16851 if (dwarf2_per_objfile->quick_file_names_table)
16852 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16853
fe3e1990
DJ
16854 /* Everything else should be on the objfile obstack. */
16855}
16856
dee91e82
DE
16857/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
16858 We store these in a hash table separate from the DIEs, and preserve them
16859 when the DIEs are flushed out of cache.
16860
16861 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
16862 uniquely identify the type. A file may have multiple .debug_types sections,
16863 or the type may come from a DWO file. We have to use something in
16864 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
16865 routine, get_die_type_at_offset, from outside this file, and thus won't
16866 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
16867 of the objfile. */
1c379e20 16868
dee91e82 16869struct dwarf2_per_cu_offset_and_type
1c379e20 16870{
dee91e82 16871 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 16872 sect_offset offset;
1c379e20
DJ
16873 struct type *type;
16874};
16875
dee91e82 16876/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
16877
16878static hashval_t
dee91e82 16879per_cu_offset_and_type_hash (const void *item)
1c379e20 16880{
dee91e82 16881 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 16882
dee91e82 16883 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
16884}
16885
dee91e82 16886/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
16887
16888static int
dee91e82 16889per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 16890{
dee91e82
DE
16891 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
16892 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16893
dee91e82
DE
16894 return (ofs_lhs->per_cu == ofs_rhs->per_cu
16895 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
16896}
16897
16898/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16899 table if necessary. For convenience, return TYPE.
16900
16901 The DIEs reading must have careful ordering to:
16902 * Not cause infite loops trying to read in DIEs as a prerequisite for
16903 reading current DIE.
16904 * Not trying to dereference contents of still incompletely read in types
16905 while reading in other DIEs.
16906 * Enable referencing still incompletely read in types just by a pointer to
16907 the type without accessing its fields.
16908
16909 Therefore caller should follow these rules:
16910 * Try to fetch any prerequisite types we may need to build this DIE type
16911 before building the type and calling set_die_type.
e71ec853 16912 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16913 possible before fetching more types to complete the current type.
16914 * Make the type as complete as possible before fetching more types. */
1c379e20 16915
f792889a 16916static struct type *
1c379e20
DJ
16917set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16918{
dee91e82 16919 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 16920 struct objfile *objfile = cu->objfile;
1c379e20 16921
b4ba55a1
JB
16922 /* For Ada types, make sure that the gnat-specific data is always
16923 initialized (if not already set). There are a few types where
16924 we should not be doing so, because the type-specific area is
16925 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16926 where the type-specific area is used to store the floatformat).
16927 But this is not a problem, because the gnat-specific information
16928 is actually not needed for these types. */
16929 if (need_gnat_info (cu)
16930 && TYPE_CODE (type) != TYPE_CODE_FUNC
16931 && TYPE_CODE (type) != TYPE_CODE_FLT
16932 && !HAVE_GNAT_AUX_INFO (type))
16933 INIT_GNAT_SPECIFIC (type);
16934
dee91e82 16935 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 16936 {
dee91e82
DE
16937 dwarf2_per_objfile->die_type_hash =
16938 htab_create_alloc_ex (127,
16939 per_cu_offset_and_type_hash,
16940 per_cu_offset_and_type_eq,
16941 NULL,
16942 &objfile->objfile_obstack,
16943 hashtab_obstack_allocate,
16944 dummy_obstack_deallocate);
f792889a 16945 }
1c379e20 16946
dee91e82 16947 ofs.per_cu = cu->per_cu;
1c379e20
DJ
16948 ofs.offset = die->offset;
16949 ofs.type = type;
dee91e82
DE
16950 slot = (struct dwarf2_per_cu_offset_and_type **)
16951 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
16952 if (*slot)
16953 complaint (&symfile_complaints,
16954 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 16955 die->offset.sect_off);
673bfd45 16956 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16957 **slot = ofs;
f792889a 16958 return type;
1c379e20
DJ
16959}
16960
380bca97 16961/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 16962 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16963
16964static struct type *
b64f50a1 16965get_die_type_at_offset (sect_offset offset,
673bfd45 16966 struct dwarf2_per_cu_data *per_cu)
1c379e20 16967{
dee91e82 16968 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 16969
dee91e82 16970 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 16971 return NULL;
1c379e20 16972
dee91e82 16973 ofs.per_cu = per_cu;
673bfd45 16974 ofs.offset = offset;
dee91e82 16975 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
16976 if (slot)
16977 return slot->type;
16978 else
16979 return NULL;
16980}
16981
673bfd45
DE
16982/* Look up the type for DIE in the appropriate type_hash table,
16983 or return NULL if DIE does not have a saved type. */
16984
16985static struct type *
16986get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16987{
16988 return get_die_type_at_offset (die->offset, cu->per_cu);
16989}
16990
10b3939b
DJ
16991/* Add a dependence relationship from CU to REF_PER_CU. */
16992
16993static void
16994dwarf2_add_dependence (struct dwarf2_cu *cu,
16995 struct dwarf2_per_cu_data *ref_per_cu)
16996{
16997 void **slot;
16998
16999 if (cu->dependencies == NULL)
17000 cu->dependencies
17001 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
17002 NULL, &cu->comp_unit_obstack,
17003 hashtab_obstack_allocate,
17004 dummy_obstack_deallocate);
17005
17006 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
17007 if (*slot == NULL)
17008 *slot = ref_per_cu;
17009}
1c379e20 17010
f504f079
DE
17011/* Subroutine of dwarf2_mark to pass to htab_traverse.
17012 Set the mark field in every compilation unit in the
ae038cb0
DJ
17013 cache that we must keep because we are keeping CU. */
17014
10b3939b
DJ
17015static int
17016dwarf2_mark_helper (void **slot, void *data)
17017{
17018 struct dwarf2_per_cu_data *per_cu;
17019
17020 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
17021
17022 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17023 reading of the chain. As such dependencies remain valid it is not much
17024 useful to track and undo them during QUIT cleanups. */
17025 if (per_cu->cu == NULL)
17026 return 1;
17027
10b3939b
DJ
17028 if (per_cu->cu->mark)
17029 return 1;
17030 per_cu->cu->mark = 1;
17031
17032 if (per_cu->cu->dependencies != NULL)
17033 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17034
17035 return 1;
17036}
17037
f504f079
DE
17038/* Set the mark field in CU and in every other compilation unit in the
17039 cache that we must keep because we are keeping CU. */
17040
ae038cb0
DJ
17041static void
17042dwarf2_mark (struct dwarf2_cu *cu)
17043{
17044 if (cu->mark)
17045 return;
17046 cu->mark = 1;
10b3939b
DJ
17047 if (cu->dependencies != NULL)
17048 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
17049}
17050
17051static void
17052dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17053{
17054 while (per_cu)
17055 {
17056 per_cu->cu->mark = 0;
17057 per_cu = per_cu->cu->read_in_chain;
17058 }
72bf9492
DJ
17059}
17060
72bf9492
DJ
17061/* Trivial hash function for partial_die_info: the hash value of a DIE
17062 is its offset in .debug_info for this objfile. */
17063
17064static hashval_t
17065partial_die_hash (const void *item)
17066{
17067 const struct partial_die_info *part_die = item;
9a619af0 17068
b64f50a1 17069 return part_die->offset.sect_off;
72bf9492
DJ
17070}
17071
17072/* Trivial comparison function for partial_die_info structures: two DIEs
17073 are equal if they have the same offset. */
17074
17075static int
17076partial_die_eq (const void *item_lhs, const void *item_rhs)
17077{
17078 const struct partial_die_info *part_die_lhs = item_lhs;
17079 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 17080
b64f50a1 17081 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
17082}
17083
ae038cb0
DJ
17084static struct cmd_list_element *set_dwarf2_cmdlist;
17085static struct cmd_list_element *show_dwarf2_cmdlist;
17086
17087static void
17088set_dwarf2_cmd (char *args, int from_tty)
17089{
17090 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17091}
17092
17093static void
17094show_dwarf2_cmd (char *args, int from_tty)
6e70227d 17095{
ae038cb0
DJ
17096 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17097}
17098
dce234bc
PP
17099/* If section described by INFO was mmapped, munmap it now. */
17100
17101static void
17102munmap_section_buffer (struct dwarf2_section_info *info)
17103{
b315ab21 17104 if (info->map_addr != NULL)
dce234bc
PP
17105 {
17106#ifdef HAVE_MMAP
b315ab21 17107 int res;
9a619af0 17108
b315ab21
TG
17109 res = munmap (info->map_addr, info->map_len);
17110 gdb_assert (res == 0);
dce234bc
PP
17111#else
17112 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 17113 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
17114#endif
17115 }
17116}
17117
17118/* munmap debug sections for OBJFILE, if necessary. */
17119
17120static void
c1bd65d0 17121dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
17122{
17123 struct dwarf2_per_objfile *data = d;
8b70b953
TT
17124 int ix;
17125 struct dwarf2_section_info *section;
9a619af0 17126
16be1145
DE
17127 /* This is sorted according to the order they're defined in to make it easier
17128 to keep in sync. */
dce234bc
PP
17129 munmap_section_buffer (&data->info);
17130 munmap_section_buffer (&data->abbrev);
17131 munmap_section_buffer (&data->line);
16be1145 17132 munmap_section_buffer (&data->loc);
dce234bc 17133 munmap_section_buffer (&data->macinfo);
cf2c3c16 17134 munmap_section_buffer (&data->macro);
16be1145 17135 munmap_section_buffer (&data->str);
dce234bc 17136 munmap_section_buffer (&data->ranges);
3019eac3 17137 munmap_section_buffer (&data->addr);
dce234bc
PP
17138 munmap_section_buffer (&data->frame);
17139 munmap_section_buffer (&data->eh_frame);
9291a0cd 17140 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
17141
17142 for (ix = 0;
17143 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17144 ++ix)
17145 munmap_section_buffer (section);
17146
95554aad
TT
17147 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17148 VEC_free (dwarf2_per_cu_ptr,
17149 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17150
8b70b953 17151 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
17152
17153 if (data->dwo_files)
17154 free_dwo_files (data->dwo_files, objfile);
9291a0cd
TT
17155}
17156
17157\f
ae2de4f8 17158/* The "save gdb-index" command. */
9291a0cd
TT
17159
17160/* The contents of the hash table we create when building the string
17161 table. */
17162struct strtab_entry
17163{
17164 offset_type offset;
17165 const char *str;
17166};
17167
559a7a62
JK
17168/* Hash function for a strtab_entry.
17169
17170 Function is used only during write_hash_table so no index format backward
17171 compatibility is needed. */
b89be57b 17172
9291a0cd
TT
17173static hashval_t
17174hash_strtab_entry (const void *e)
17175{
17176 const struct strtab_entry *entry = e;
559a7a62 17177 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
17178}
17179
17180/* Equality function for a strtab_entry. */
b89be57b 17181
9291a0cd
TT
17182static int
17183eq_strtab_entry (const void *a, const void *b)
17184{
17185 const struct strtab_entry *ea = a;
17186 const struct strtab_entry *eb = b;
17187 return !strcmp (ea->str, eb->str);
17188}
17189
17190/* Create a strtab_entry hash table. */
b89be57b 17191
9291a0cd
TT
17192static htab_t
17193create_strtab (void)
17194{
17195 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17196 xfree, xcalloc, xfree);
17197}
17198
17199/* Add a string to the constant pool. Return the string's offset in
17200 host order. */
b89be57b 17201
9291a0cd
TT
17202static offset_type
17203add_string (htab_t table, struct obstack *cpool, const char *str)
17204{
17205 void **slot;
17206 struct strtab_entry entry;
17207 struct strtab_entry *result;
17208
17209 entry.str = str;
17210 slot = htab_find_slot (table, &entry, INSERT);
17211 if (*slot)
17212 result = *slot;
17213 else
17214 {
17215 result = XNEW (struct strtab_entry);
17216 result->offset = obstack_object_size (cpool);
17217 result->str = str;
17218 obstack_grow_str0 (cpool, str);
17219 *slot = result;
17220 }
17221 return result->offset;
17222}
17223
17224/* An entry in the symbol table. */
17225struct symtab_index_entry
17226{
17227 /* The name of the symbol. */
17228 const char *name;
17229 /* The offset of the name in the constant pool. */
17230 offset_type index_offset;
17231 /* A sorted vector of the indices of all the CUs that hold an object
17232 of this name. */
17233 VEC (offset_type) *cu_indices;
17234};
17235
17236/* The symbol table. This is a power-of-2-sized hash table. */
17237struct mapped_symtab
17238{
17239 offset_type n_elements;
17240 offset_type size;
17241 struct symtab_index_entry **data;
17242};
17243
17244/* Hash function for a symtab_index_entry. */
b89be57b 17245
9291a0cd
TT
17246static hashval_t
17247hash_symtab_entry (const void *e)
17248{
17249 const struct symtab_index_entry *entry = e;
17250 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17251 sizeof (offset_type) * VEC_length (offset_type,
17252 entry->cu_indices),
17253 0);
17254}
17255
17256/* Equality function for a symtab_index_entry. */
b89be57b 17257
9291a0cd
TT
17258static int
17259eq_symtab_entry (const void *a, const void *b)
17260{
17261 const struct symtab_index_entry *ea = a;
17262 const struct symtab_index_entry *eb = b;
17263 int len = VEC_length (offset_type, ea->cu_indices);
17264 if (len != VEC_length (offset_type, eb->cu_indices))
17265 return 0;
17266 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17267 VEC_address (offset_type, eb->cu_indices),
17268 sizeof (offset_type) * len);
17269}
17270
17271/* Destroy a symtab_index_entry. */
b89be57b 17272
9291a0cd
TT
17273static void
17274delete_symtab_entry (void *p)
17275{
17276 struct symtab_index_entry *entry = p;
17277 VEC_free (offset_type, entry->cu_indices);
17278 xfree (entry);
17279}
17280
17281/* Create a hash table holding symtab_index_entry objects. */
b89be57b 17282
9291a0cd 17283static htab_t
3876f04e 17284create_symbol_hash_table (void)
9291a0cd
TT
17285{
17286 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17287 delete_symtab_entry, xcalloc, xfree);
17288}
17289
17290/* Create a new mapped symtab object. */
b89be57b 17291
9291a0cd
TT
17292static struct mapped_symtab *
17293create_mapped_symtab (void)
17294{
17295 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17296 symtab->n_elements = 0;
17297 symtab->size = 1024;
17298 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17299 return symtab;
17300}
17301
17302/* Destroy a mapped_symtab. */
b89be57b 17303
9291a0cd
TT
17304static void
17305cleanup_mapped_symtab (void *p)
17306{
17307 struct mapped_symtab *symtab = p;
17308 /* The contents of the array are freed when the other hash table is
17309 destroyed. */
17310 xfree (symtab->data);
17311 xfree (symtab);
17312}
17313
17314/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
17315 the slot.
17316
17317 Function is used only during write_hash_table so no index format backward
17318 compatibility is needed. */
b89be57b 17319
9291a0cd
TT
17320static struct symtab_index_entry **
17321find_slot (struct mapped_symtab *symtab, const char *name)
17322{
559a7a62 17323 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
17324
17325 index = hash & (symtab->size - 1);
17326 step = ((hash * 17) & (symtab->size - 1)) | 1;
17327
17328 for (;;)
17329 {
17330 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17331 return &symtab->data[index];
17332 index = (index + step) & (symtab->size - 1);
17333 }
17334}
17335
17336/* Expand SYMTAB's hash table. */
b89be57b 17337
9291a0cd
TT
17338static void
17339hash_expand (struct mapped_symtab *symtab)
17340{
17341 offset_type old_size = symtab->size;
17342 offset_type i;
17343 struct symtab_index_entry **old_entries = symtab->data;
17344
17345 symtab->size *= 2;
17346 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17347
17348 for (i = 0; i < old_size; ++i)
17349 {
17350 if (old_entries[i])
17351 {
17352 struct symtab_index_entry **slot = find_slot (symtab,
17353 old_entries[i]->name);
17354 *slot = old_entries[i];
17355 }
17356 }
17357
17358 xfree (old_entries);
17359}
17360
17361/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
17362 is the index of the CU in which the symbol appears. */
b89be57b 17363
9291a0cd
TT
17364static void
17365add_index_entry (struct mapped_symtab *symtab, const char *name,
17366 offset_type cu_index)
17367{
17368 struct symtab_index_entry **slot;
17369
17370 ++symtab->n_elements;
17371 if (4 * symtab->n_elements / 3 >= symtab->size)
17372 hash_expand (symtab);
17373
17374 slot = find_slot (symtab, name);
17375 if (!*slot)
17376 {
17377 *slot = XNEW (struct symtab_index_entry);
17378 (*slot)->name = name;
17379 (*slot)->cu_indices = NULL;
17380 }
17381 /* Don't push an index twice. Due to how we add entries we only
17382 have to check the last one. */
17383 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 17384 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
17385 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
17386}
17387
17388/* Add a vector of indices to the constant pool. */
b89be57b 17389
9291a0cd 17390static offset_type
3876f04e 17391add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
17392 struct symtab_index_entry *entry)
17393{
17394 void **slot;
17395
3876f04e 17396 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
17397 if (!*slot)
17398 {
17399 offset_type len = VEC_length (offset_type, entry->cu_indices);
17400 offset_type val = MAYBE_SWAP (len);
17401 offset_type iter;
17402 int i;
17403
17404 *slot = entry;
17405 entry->index_offset = obstack_object_size (cpool);
17406
17407 obstack_grow (cpool, &val, sizeof (val));
17408 for (i = 0;
17409 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17410 ++i)
17411 {
17412 val = MAYBE_SWAP (iter);
17413 obstack_grow (cpool, &val, sizeof (val));
17414 }
17415 }
17416 else
17417 {
17418 struct symtab_index_entry *old_entry = *slot;
17419 entry->index_offset = old_entry->index_offset;
17420 entry = old_entry;
17421 }
17422 return entry->index_offset;
17423}
17424
17425/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17426 constant pool entries going into the obstack CPOOL. */
b89be57b 17427
9291a0cd
TT
17428static void
17429write_hash_table (struct mapped_symtab *symtab,
17430 struct obstack *output, struct obstack *cpool)
17431{
17432 offset_type i;
3876f04e 17433 htab_t symbol_hash_table;
9291a0cd
TT
17434 htab_t str_table;
17435
3876f04e 17436 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 17437 str_table = create_strtab ();
3876f04e 17438
9291a0cd
TT
17439 /* We add all the index vectors to the constant pool first, to
17440 ensure alignment is ok. */
17441 for (i = 0; i < symtab->size; ++i)
17442 {
17443 if (symtab->data[i])
3876f04e 17444 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
17445 }
17446
17447 /* Now write out the hash table. */
17448 for (i = 0; i < symtab->size; ++i)
17449 {
17450 offset_type str_off, vec_off;
17451
17452 if (symtab->data[i])
17453 {
17454 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17455 vec_off = symtab->data[i]->index_offset;
17456 }
17457 else
17458 {
17459 /* While 0 is a valid constant pool index, it is not valid
17460 to have 0 for both offsets. */
17461 str_off = 0;
17462 vec_off = 0;
17463 }
17464
17465 str_off = MAYBE_SWAP (str_off);
17466 vec_off = MAYBE_SWAP (vec_off);
17467
17468 obstack_grow (output, &str_off, sizeof (str_off));
17469 obstack_grow (output, &vec_off, sizeof (vec_off));
17470 }
17471
17472 htab_delete (str_table);
3876f04e 17473 htab_delete (symbol_hash_table);
9291a0cd
TT
17474}
17475
0a5429f6
DE
17476/* Struct to map psymtab to CU index in the index file. */
17477struct psymtab_cu_index_map
17478{
17479 struct partial_symtab *psymtab;
17480 unsigned int cu_index;
17481};
17482
17483static hashval_t
17484hash_psymtab_cu_index (const void *item)
17485{
17486 const struct psymtab_cu_index_map *map = item;
17487
17488 return htab_hash_pointer (map->psymtab);
17489}
17490
17491static int
17492eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17493{
17494 const struct psymtab_cu_index_map *lhs = item_lhs;
17495 const struct psymtab_cu_index_map *rhs = item_rhs;
17496
17497 return lhs->psymtab == rhs->psymtab;
17498}
17499
17500/* Helper struct for building the address table. */
17501struct addrmap_index_data
17502{
17503 struct objfile *objfile;
17504 struct obstack *addr_obstack;
17505 htab_t cu_index_htab;
17506
17507 /* Non-zero if the previous_* fields are valid.
17508 We can't write an entry until we see the next entry (since it is only then
17509 that we know the end of the entry). */
17510 int previous_valid;
17511 /* Index of the CU in the table of all CUs in the index file. */
17512 unsigned int previous_cu_index;
0963b4bd 17513 /* Start address of the CU. */
0a5429f6
DE
17514 CORE_ADDR previous_cu_start;
17515};
17516
17517/* Write an address entry to OBSTACK. */
b89be57b 17518
9291a0cd 17519static void
0a5429f6
DE
17520add_address_entry (struct objfile *objfile, struct obstack *obstack,
17521 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 17522{
0a5429f6 17523 offset_type cu_index_to_write;
9291a0cd
TT
17524 char addr[8];
17525 CORE_ADDR baseaddr;
17526
17527 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17528
0a5429f6
DE
17529 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17530 obstack_grow (obstack, addr, 8);
17531 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17532 obstack_grow (obstack, addr, 8);
17533 cu_index_to_write = MAYBE_SWAP (cu_index);
17534 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17535}
17536
17537/* Worker function for traversing an addrmap to build the address table. */
17538
17539static int
17540add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17541{
17542 struct addrmap_index_data *data = datap;
17543 struct partial_symtab *pst = obj;
0a5429f6
DE
17544
17545 if (data->previous_valid)
17546 add_address_entry (data->objfile, data->addr_obstack,
17547 data->previous_cu_start, start_addr,
17548 data->previous_cu_index);
17549
17550 data->previous_cu_start = start_addr;
17551 if (pst != NULL)
17552 {
17553 struct psymtab_cu_index_map find_map, *map;
17554 find_map.psymtab = pst;
17555 map = htab_find (data->cu_index_htab, &find_map);
17556 gdb_assert (map != NULL);
17557 data->previous_cu_index = map->cu_index;
17558 data->previous_valid = 1;
17559 }
17560 else
17561 data->previous_valid = 0;
17562
17563 return 0;
17564}
17565
17566/* Write OBJFILE's address map to OBSTACK.
17567 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17568 in the index file. */
17569
17570static void
17571write_address_map (struct objfile *objfile, struct obstack *obstack,
17572 htab_t cu_index_htab)
17573{
17574 struct addrmap_index_data addrmap_index_data;
17575
17576 /* When writing the address table, we have to cope with the fact that
17577 the addrmap iterator only provides the start of a region; we have to
17578 wait until the next invocation to get the start of the next region. */
17579
17580 addrmap_index_data.objfile = objfile;
17581 addrmap_index_data.addr_obstack = obstack;
17582 addrmap_index_data.cu_index_htab = cu_index_htab;
17583 addrmap_index_data.previous_valid = 0;
17584
17585 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17586 &addrmap_index_data);
17587
17588 /* It's highly unlikely the last entry (end address = 0xff...ff)
17589 is valid, but we should still handle it.
17590 The end address is recorded as the start of the next region, but that
17591 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17592 anyway. */
17593 if (addrmap_index_data.previous_valid)
17594 add_address_entry (objfile, obstack,
17595 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17596 addrmap_index_data.previous_cu_index);
9291a0cd
TT
17597}
17598
17599/* Add a list of partial symbols to SYMTAB. */
b89be57b 17600
9291a0cd
TT
17601static void
17602write_psymbols (struct mapped_symtab *symtab,
987d643c 17603 htab_t psyms_seen,
9291a0cd
TT
17604 struct partial_symbol **psymp,
17605 int count,
987d643c
TT
17606 offset_type cu_index,
17607 int is_static)
9291a0cd
TT
17608{
17609 for (; count-- > 0; ++psymp)
17610 {
987d643c
TT
17611 void **slot, *lookup;
17612
9291a0cd
TT
17613 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
17614 error (_("Ada is not currently supported by the index"));
987d643c
TT
17615
17616 /* We only want to add a given psymbol once. However, we also
17617 want to account for whether it is global or static. So, we
17618 may add it twice, using slightly different values. */
17619 if (is_static)
17620 {
17621 uintptr_t val = 1 | (uintptr_t) *psymp;
17622
17623 lookup = (void *) val;
17624 }
17625 else
17626 lookup = *psymp;
17627
17628 /* Only add a given psymbol once. */
17629 slot = htab_find_slot (psyms_seen, lookup, INSERT);
17630 if (!*slot)
17631 {
17632 *slot = lookup;
bb2f58dc 17633 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 17634 }
9291a0cd
TT
17635 }
17636}
17637
17638/* Write the contents of an ("unfinished") obstack to FILE. Throw an
17639 exception if there is an error. */
b89be57b 17640
9291a0cd
TT
17641static void
17642write_obstack (FILE *file, struct obstack *obstack)
17643{
17644 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17645 file)
17646 != obstack_object_size (obstack))
17647 error (_("couldn't data write to file"));
17648}
17649
17650/* Unlink a file if the argument is not NULL. */
b89be57b 17651
9291a0cd
TT
17652static void
17653unlink_if_set (void *p)
17654{
17655 char **filename = p;
17656 if (*filename)
17657 unlink (*filename);
17658}
17659
1fd400ff
TT
17660/* A helper struct used when iterating over debug_types. */
17661struct signatured_type_index_data
17662{
17663 struct objfile *objfile;
17664 struct mapped_symtab *symtab;
17665 struct obstack *types_list;
987d643c 17666 htab_t psyms_seen;
1fd400ff
TT
17667 int cu_index;
17668};
17669
17670/* A helper function that writes a single signatured_type to an
17671 obstack. */
b89be57b 17672
1fd400ff
TT
17673static int
17674write_one_signatured_type (void **slot, void *d)
17675{
17676 struct signatured_type_index_data *info = d;
17677 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
17678 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17679 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
17680 gdb_byte val[8];
17681
17682 write_psymbols (info->symtab,
987d643c 17683 info->psyms_seen,
3e43a32a
MS
17684 info->objfile->global_psymbols.list
17685 + psymtab->globals_offset,
987d643c
TT
17686 psymtab->n_global_syms, info->cu_index,
17687 0);
1fd400ff 17688 write_psymbols (info->symtab,
987d643c 17689 info->psyms_seen,
3e43a32a
MS
17690 info->objfile->static_psymbols.list
17691 + psymtab->statics_offset,
987d643c
TT
17692 psymtab->n_static_syms, info->cu_index,
17693 1);
1fd400ff 17694
b64f50a1
JK
17695 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17696 entry->per_cu.offset.sect_off);
1fd400ff 17697 obstack_grow (info->types_list, val, 8);
3019eac3
DE
17698 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17699 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
17700 obstack_grow (info->types_list, val, 8);
17701 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17702 obstack_grow (info->types_list, val, 8);
17703
17704 ++info->cu_index;
17705
17706 return 1;
17707}
17708
95554aad
TT
17709/* Recurse into all "included" dependencies and write their symbols as
17710 if they appeared in this psymtab. */
17711
17712static void
17713recursively_write_psymbols (struct objfile *objfile,
17714 struct partial_symtab *psymtab,
17715 struct mapped_symtab *symtab,
17716 htab_t psyms_seen,
17717 offset_type cu_index)
17718{
17719 int i;
17720
17721 for (i = 0; i < psymtab->number_of_dependencies; ++i)
17722 if (psymtab->dependencies[i]->user != NULL)
17723 recursively_write_psymbols (objfile, psymtab->dependencies[i],
17724 symtab, psyms_seen, cu_index);
17725
17726 write_psymbols (symtab,
17727 psyms_seen,
17728 objfile->global_psymbols.list + psymtab->globals_offset,
17729 psymtab->n_global_syms, cu_index,
17730 0);
17731 write_psymbols (symtab,
17732 psyms_seen,
17733 objfile->static_psymbols.list + psymtab->statics_offset,
17734 psymtab->n_static_syms, cu_index,
17735 1);
17736}
17737
9291a0cd 17738/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 17739
9291a0cd
TT
17740static void
17741write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17742{
17743 struct cleanup *cleanup;
17744 char *filename, *cleanup_filename;
1fd400ff
TT
17745 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
17746 struct obstack cu_list, types_cu_list;
9291a0cd
TT
17747 int i;
17748 FILE *out_file;
17749 struct mapped_symtab *symtab;
17750 offset_type val, size_of_contents, total_len;
17751 struct stat st;
987d643c 17752 htab_t psyms_seen;
0a5429f6
DE
17753 htab_t cu_index_htab;
17754 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 17755
b4f2f049 17756 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 17757 return;
b4f2f049 17758
9291a0cd
TT
17759 if (dwarf2_per_objfile->using_index)
17760 error (_("Cannot use an index to create the index"));
17761
8b70b953
TT
17762 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17763 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17764
9291a0cd 17765 if (stat (objfile->name, &st) < 0)
7e17e088 17766 perror_with_name (objfile->name);
9291a0cd
TT
17767
17768 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17769 INDEX_SUFFIX, (char *) NULL);
17770 cleanup = make_cleanup (xfree, filename);
17771
17772 out_file = fopen (filename, "wb");
17773 if (!out_file)
17774 error (_("Can't open `%s' for writing"), filename);
17775
17776 cleanup_filename = filename;
17777 make_cleanup (unlink_if_set, &cleanup_filename);
17778
17779 symtab = create_mapped_symtab ();
17780 make_cleanup (cleanup_mapped_symtab, symtab);
17781
17782 obstack_init (&addr_obstack);
17783 make_cleanup_obstack_free (&addr_obstack);
17784
17785 obstack_init (&cu_list);
17786 make_cleanup_obstack_free (&cu_list);
17787
1fd400ff
TT
17788 obstack_init (&types_cu_list);
17789 make_cleanup_obstack_free (&types_cu_list);
17790
987d643c
TT
17791 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17792 NULL, xcalloc, xfree);
96408a79 17793 make_cleanup_htab_delete (psyms_seen);
987d643c 17794
0a5429f6
DE
17795 /* While we're scanning CU's create a table that maps a psymtab pointer
17796 (which is what addrmap records) to its index (which is what is recorded
17797 in the index file). This will later be needed to write the address
17798 table. */
17799 cu_index_htab = htab_create_alloc (100,
17800 hash_psymtab_cu_index,
17801 eq_psymtab_cu_index,
17802 NULL, xcalloc, xfree);
96408a79 17803 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
17804 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17805 xmalloc (sizeof (struct psymtab_cu_index_map)
17806 * dwarf2_per_objfile->n_comp_units);
17807 make_cleanup (xfree, psymtab_cu_index_map);
17808
17809 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
17810 work here. Also, the debug_types entries do not appear in
17811 all_comp_units, but only in their own hash table. */
9291a0cd
TT
17812 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17813 {
3e43a32a
MS
17814 struct dwarf2_per_cu_data *per_cu
17815 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 17816 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 17817 gdb_byte val[8];
0a5429f6
DE
17818 struct psymtab_cu_index_map *map;
17819 void **slot;
9291a0cd 17820
95554aad
TT
17821 if (psymtab->user == NULL)
17822 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 17823
0a5429f6
DE
17824 map = &psymtab_cu_index_map[i];
17825 map->psymtab = psymtab;
17826 map->cu_index = i;
17827 slot = htab_find_slot (cu_index_htab, map, INSERT);
17828 gdb_assert (slot != NULL);
17829 gdb_assert (*slot == NULL);
17830 *slot = map;
9291a0cd 17831
b64f50a1
JK
17832 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17833 per_cu->offset.sect_off);
9291a0cd 17834 obstack_grow (&cu_list, val, 8);
e254ef6a 17835 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
17836 obstack_grow (&cu_list, val, 8);
17837 }
17838
0a5429f6
DE
17839 /* Dump the address map. */
17840 write_address_map (objfile, &addr_obstack, cu_index_htab);
17841
1fd400ff
TT
17842 /* Write out the .debug_type entries, if any. */
17843 if (dwarf2_per_objfile->signatured_types)
17844 {
17845 struct signatured_type_index_data sig_data;
17846
17847 sig_data.objfile = objfile;
17848 sig_data.symtab = symtab;
17849 sig_data.types_list = &types_cu_list;
987d643c 17850 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17851 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17852 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17853 write_one_signatured_type, &sig_data);
17854 }
17855
9291a0cd
TT
17856 obstack_init (&constant_pool);
17857 make_cleanup_obstack_free (&constant_pool);
17858 obstack_init (&symtab_obstack);
17859 make_cleanup_obstack_free (&symtab_obstack);
17860 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17861
17862 obstack_init (&contents);
17863 make_cleanup_obstack_free (&contents);
1fd400ff 17864 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17865 total_len = size_of_contents;
17866
17867 /* The version number. */
481860b3 17868 val = MAYBE_SWAP (6);
9291a0cd
TT
17869 obstack_grow (&contents, &val, sizeof (val));
17870
17871 /* The offset of the CU list from the start of the file. */
17872 val = MAYBE_SWAP (total_len);
17873 obstack_grow (&contents, &val, sizeof (val));
17874 total_len += obstack_object_size (&cu_list);
17875
1fd400ff
TT
17876 /* The offset of the types CU list from the start of the file. */
17877 val = MAYBE_SWAP (total_len);
17878 obstack_grow (&contents, &val, sizeof (val));
17879 total_len += obstack_object_size (&types_cu_list);
17880
9291a0cd
TT
17881 /* The offset of the address table from the start of the file. */
17882 val = MAYBE_SWAP (total_len);
17883 obstack_grow (&contents, &val, sizeof (val));
17884 total_len += obstack_object_size (&addr_obstack);
17885
17886 /* The offset of the symbol table from the start of the file. */
17887 val = MAYBE_SWAP (total_len);
17888 obstack_grow (&contents, &val, sizeof (val));
17889 total_len += obstack_object_size (&symtab_obstack);
17890
17891 /* The offset of the constant pool from the start of the file. */
17892 val = MAYBE_SWAP (total_len);
17893 obstack_grow (&contents, &val, sizeof (val));
17894 total_len += obstack_object_size (&constant_pool);
17895
17896 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17897
17898 write_obstack (out_file, &contents);
17899 write_obstack (out_file, &cu_list);
1fd400ff 17900 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17901 write_obstack (out_file, &addr_obstack);
17902 write_obstack (out_file, &symtab_obstack);
17903 write_obstack (out_file, &constant_pool);
17904
17905 fclose (out_file);
17906
17907 /* We want to keep the file, so we set cleanup_filename to NULL
17908 here. See unlink_if_set. */
17909 cleanup_filename = NULL;
17910
17911 do_cleanups (cleanup);
17912}
17913
90476074
TT
17914/* Implementation of the `save gdb-index' command.
17915
17916 Note that the file format used by this command is documented in the
17917 GDB manual. Any changes here must be documented there. */
11570e71 17918
9291a0cd
TT
17919static void
17920save_gdb_index_command (char *arg, int from_tty)
17921{
17922 struct objfile *objfile;
17923
17924 if (!arg || !*arg)
96d19272 17925 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17926
17927 ALL_OBJFILES (objfile)
17928 {
17929 struct stat st;
17930
17931 /* If the objfile does not correspond to an actual file, skip it. */
17932 if (stat (objfile->name, &st) < 0)
17933 continue;
17934
17935 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17936 if (dwarf2_per_objfile)
17937 {
17938 volatile struct gdb_exception except;
17939
17940 TRY_CATCH (except, RETURN_MASK_ERROR)
17941 {
17942 write_psymtabs_to_index (objfile, arg);
17943 }
17944 if (except.reason < 0)
17945 exception_fprintf (gdb_stderr, except,
17946 _("Error while writing index for `%s': "),
17947 objfile->name);
17948 }
17949 }
dce234bc
PP
17950}
17951
9291a0cd
TT
17952\f
17953
9eae7c52
TT
17954int dwarf2_always_disassemble;
17955
17956static void
17957show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17958 struct cmd_list_element *c, const char *value)
17959{
3e43a32a
MS
17960 fprintf_filtered (file,
17961 _("Whether to always disassemble "
17962 "DWARF expressions is %s.\n"),
9eae7c52
TT
17963 value);
17964}
17965
900e11f9
JK
17966static void
17967show_check_physname (struct ui_file *file, int from_tty,
17968 struct cmd_list_element *c, const char *value)
17969{
17970 fprintf_filtered (file,
17971 _("Whether to check \"physname\" is %s.\n"),
17972 value);
17973}
17974
6502dd73
DJ
17975void _initialize_dwarf2_read (void);
17976
17977void
17978_initialize_dwarf2_read (void)
17979{
96d19272
JK
17980 struct cmd_list_element *c;
17981
dce234bc 17982 dwarf2_objfile_data_key
c1bd65d0 17983 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17984
1bedd215
AC
17985 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17986Set DWARF 2 specific variables.\n\
17987Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17988 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17989 0/*allow-unknown*/, &maintenance_set_cmdlist);
17990
1bedd215
AC
17991 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17992Show DWARF 2 specific variables\n\
17993Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17994 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17995 0/*allow-unknown*/, &maintenance_show_cmdlist);
17996
17997 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17998 &dwarf2_max_cache_age, _("\
17999Set the upper bound on the age of cached dwarf2 compilation units."), _("\
18000Show the upper bound on the age of cached dwarf2 compilation units."), _("\
18001A higher limit means that cached compilation units will be stored\n\
18002in memory longer, and more total memory will be used. Zero disables\n\
18003caching, which can slow down startup."),
2c5b56ce 18004 NULL,
920d2a44 18005 show_dwarf2_max_cache_age,
2c5b56ce 18006 &set_dwarf2_cmdlist,
ae038cb0 18007 &show_dwarf2_cmdlist);
d97bc12b 18008
9eae7c52
TT
18009 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
18010 &dwarf2_always_disassemble, _("\
18011Set whether `info address' always disassembles DWARF expressions."), _("\
18012Show whether `info address' always disassembles DWARF expressions."), _("\
18013When enabled, DWARF expressions are always printed in an assembly-like\n\
18014syntax. When disabled, expressions will be printed in a more\n\
18015conversational style, when possible."),
18016 NULL,
18017 show_dwarf2_always_disassemble,
18018 &set_dwarf2_cmdlist,
18019 &show_dwarf2_cmdlist);
18020
d97bc12b
DE
18021 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18022Set debugging of the dwarf2 DIE reader."), _("\
18023Show debugging of the dwarf2 DIE reader."), _("\
18024When enabled (non-zero), DIEs are dumped after they are read in.\n\
18025The value is the maximum depth to print."),
18026 NULL,
18027 NULL,
18028 &setdebuglist, &showdebuglist);
9291a0cd 18029
900e11f9
JK
18030 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18031Set cross-checking of \"physname\" code against demangler."), _("\
18032Show cross-checking of \"physname\" code against demangler."), _("\
18033When enabled, GDB's internal \"physname\" code is checked against\n\
18034the demangler."),
18035 NULL, show_check_physname,
18036 &setdebuglist, &showdebuglist);
18037
96d19272 18038 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 18039 _("\
fc1a9d6e 18040Save a gdb-index file.\n\
11570e71 18041Usage: save gdb-index DIRECTORY"),
96d19272
JK
18042 &save_cmdlist);
18043 set_cmd_completer (c, filename_completer);
6502dd73 18044}
This page took 4.102346 seconds and 4 git commands to generate.