* psymtab.c (find_pc_sect_symtab_from_partial): Return the symtab
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
0b302171 3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
c906108c
SS
33#include "symtab.h"
34#include "gdbtypes.h"
c906108c 35#include "objfiles.h"
fa8f86ff 36#include "dwarf2.h"
c906108c
SS
37#include "buildsym.h"
38#include "demangle.h"
50f182aa 39#include "gdb-demangle.h"
c906108c 40#include "expression.h"
d5166ae1 41#include "filenames.h" /* for DOSish file names */
2e276125 42#include "macrotab.h"
c906108c
SS
43#include "language.h"
44#include "complaints.h"
357e46e7 45#include "bcache.h"
4c2df51b
DJ
46#include "dwarf2expr.h"
47#include "dwarf2loc.h"
9219021c 48#include "cp-support.h"
72bf9492 49#include "hashtab.h"
ae038cb0
DJ
50#include "command.h"
51#include "gdbcmd.h"
edb3359d 52#include "block.h"
ff013f42 53#include "addrmap.h"
94af9270
KS
54#include "typeprint.h"
55#include "jv-lang.h"
ccefe4c4 56#include "psympriv.h"
9291a0cd
TT
57#include "exceptions.h"
58#include "gdb_stat.h"
96d19272 59#include "completer.h"
34eaf542 60#include "vec.h"
98bfdba5 61#include "c-lang.h"
a766d390 62#include "go-lang.h"
98bfdba5 63#include "valprint.h"
3019eac3 64#include "gdbcore.h" /* for gnutarget */
60d5a603 65#include <ctype.h>
4c2df51b 66
c906108c
SS
67#include <fcntl.h>
68#include "gdb_string.h"
4bdf3d34 69#include "gdb_assert.h"
c906108c 70#include <sys/types.h>
233a11ab
CS
71#ifdef HAVE_ZLIB_H
72#include <zlib.h>
73#endif
dce234bc
PP
74#ifdef HAVE_MMAP
75#include <sys/mman.h>
85d9bd0e
TT
76#ifndef MAP_FAILED
77#define MAP_FAILED ((void *) -1)
78#endif
dce234bc 79#endif
d8151005 80
34eaf542
TT
81typedef struct symbol *symbolp;
82DEF_VEC_P (symbolp);
83
d97bc12b
DE
84/* When non-zero, dump DIEs after they are read in. */
85static int dwarf2_die_debug = 0;
86
900e11f9
JK
87/* When non-zero, cross-check physname against demangler. */
88static int check_physname = 0;
89
481860b3
GB
90/* When non-zero, do not reject deprecated .gdb_index sections. */
91int use_deprecated_index_sections = 0;
92
dce234bc
PP
93static int pagesize;
94
df8a16a1
DJ
95/* When set, the file that we're processing is known to have debugging
96 info for C++ namespaces. GCC 3.3.x did not produce this information,
97 but later versions do. */
98
99static int processing_has_namespace_info;
100
6502dd73
DJ
101static const struct objfile_data *dwarf2_objfile_data_key;
102
dce234bc
PP
103struct dwarf2_section_info
104{
105 asection *asection;
106 gdb_byte *buffer;
107 bfd_size_type size;
b315ab21
TG
108 /* Not NULL if the section was actually mmapped. */
109 void *map_addr;
110 /* Page aligned size of mmapped area. */
111 bfd_size_type map_len;
be391dca
TT
112 /* True if we have tried to read this section. */
113 int readin;
dce234bc
PP
114};
115
8b70b953
TT
116typedef struct dwarf2_section_info dwarf2_section_info_def;
117DEF_VEC_O (dwarf2_section_info_def);
118
9291a0cd
TT
119/* All offsets in the index are of this type. It must be
120 architecture-independent. */
121typedef uint32_t offset_type;
122
123DEF_VEC_I (offset_type);
124
125/* A description of the mapped index. The file format is described in
126 a comment by the code that writes the index. */
127struct mapped_index
128{
559a7a62
JK
129 /* Index data format version. */
130 int version;
131
9291a0cd
TT
132 /* The total length of the buffer. */
133 off_t total_size;
b11b1f88 134
9291a0cd
TT
135 /* A pointer to the address table data. */
136 const gdb_byte *address_table;
b11b1f88 137
9291a0cd
TT
138 /* Size of the address table data in bytes. */
139 offset_type address_table_size;
b11b1f88 140
3876f04e
DE
141 /* The symbol table, implemented as a hash table. */
142 const offset_type *symbol_table;
b11b1f88 143
9291a0cd 144 /* Size in slots, each slot is 2 offset_types. */
3876f04e 145 offset_type symbol_table_slots;
b11b1f88 146
9291a0cd
TT
147 /* A pointer to the constant pool. */
148 const char *constant_pool;
149};
150
95554aad
TT
151typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
152DEF_VEC_P (dwarf2_per_cu_ptr);
153
9cdd5dbd
DE
154/* Collection of data recorded per objfile.
155 This hangs off of dwarf2_objfile_data_key. */
156
6502dd73
DJ
157struct dwarf2_per_objfile
158{
dce234bc
PP
159 struct dwarf2_section_info info;
160 struct dwarf2_section_info abbrev;
161 struct dwarf2_section_info line;
dce234bc
PP
162 struct dwarf2_section_info loc;
163 struct dwarf2_section_info macinfo;
cf2c3c16 164 struct dwarf2_section_info macro;
dce234bc
PP
165 struct dwarf2_section_info str;
166 struct dwarf2_section_info ranges;
3019eac3 167 struct dwarf2_section_info addr;
dce234bc
PP
168 struct dwarf2_section_info frame;
169 struct dwarf2_section_info eh_frame;
9291a0cd 170 struct dwarf2_section_info gdb_index;
ae038cb0 171
8b70b953
TT
172 VEC (dwarf2_section_info_def) *types;
173
be391dca
TT
174 /* Back link. */
175 struct objfile *objfile;
176
d467dd73 177 /* Table of all the compilation units. This is used to locate
10b3939b 178 the target compilation unit of a particular reference. */
ae038cb0
DJ
179 struct dwarf2_per_cu_data **all_comp_units;
180
181 /* The number of compilation units in ALL_COMP_UNITS. */
182 int n_comp_units;
183
1fd400ff 184 /* The number of .debug_types-related CUs. */
d467dd73 185 int n_type_units;
1fd400ff 186
d467dd73
DE
187 /* The .debug_types-related CUs (TUs). */
188 struct dwarf2_per_cu_data **all_type_units;
1fd400ff 189
ae038cb0
DJ
190 /* A chain of compilation units that are currently read in, so that
191 they can be freed later. */
192 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 193
348e048f
DE
194 /* A table mapping .debug_types signatures to its signatured_type entry.
195 This is NULL if the .debug_types section hasn't been read in yet. */
196 htab_t signatured_types;
197
3019eac3
DE
198 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
199 This is NULL if the table hasn't been allocated yet. */
200 htab_t dwo_files;
201
72dca2f5
FR
202 /* A flag indicating wether this objfile has a section loaded at a
203 VMA of 0. */
204 int has_section_at_zero;
9291a0cd 205
ae2de4f8
DE
206 /* True if we are using the mapped index,
207 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
208 unsigned char using_index;
209
ae2de4f8 210 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 211 struct mapped_index *index_table;
98bfdba5 212
7b9f3c50
DE
213 /* When using index_table, this keeps track of all quick_file_names entries.
214 TUs can share line table entries with CUs or other TUs, and there can be
215 a lot more TUs than unique line tables, so we maintain a separate table
216 of all line table entries to support the sharing. */
217 htab_t quick_file_names_table;
218
98bfdba5
PA
219 /* Set during partial symbol reading, to prevent queueing of full
220 symbols. */
221 int reading_partial_symbols;
673bfd45 222
dee91e82 223 /* Table mapping type DIEs to their struct type *.
673bfd45 224 This is NULL if not allocated yet.
dee91e82
DE
225 The mapping is done via (CU/TU signature + DIE offset) -> type. */
226 htab_t die_type_hash;
95554aad
TT
227
228 /* The CUs we recently read. */
229 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
230};
231
232static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 233
251d32d9 234/* Default names of the debugging sections. */
c906108c 235
233a11ab
CS
236/* Note that if the debugging section has been compressed, it might
237 have a name like .zdebug_info. */
238
9cdd5dbd
DE
239static const struct dwarf2_debug_sections dwarf2_elf_names =
240{
251d32d9
TG
241 { ".debug_info", ".zdebug_info" },
242 { ".debug_abbrev", ".zdebug_abbrev" },
243 { ".debug_line", ".zdebug_line" },
244 { ".debug_loc", ".zdebug_loc" },
245 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 246 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
247 { ".debug_str", ".zdebug_str" },
248 { ".debug_ranges", ".zdebug_ranges" },
249 { ".debug_types", ".zdebug_types" },
3019eac3 250 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
251 { ".debug_frame", ".zdebug_frame" },
252 { ".eh_frame", NULL },
24d3216f
TT
253 { ".gdb_index", ".zgdb_index" },
254 23
251d32d9 255};
c906108c 256
3019eac3
DE
257/* List of DWO sections. */
258
259static const struct dwo_section_names
260{
261 struct dwarf2_section_names abbrev_dwo;
262 struct dwarf2_section_names info_dwo;
263 struct dwarf2_section_names line_dwo;
264 struct dwarf2_section_names loc_dwo;
265 struct dwarf2_section_names str_dwo;
266 struct dwarf2_section_names str_offsets_dwo;
267 struct dwarf2_section_names types_dwo;
268}
269dwo_section_names =
270{
271 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
272 { ".debug_info.dwo", ".zdebug_info.dwo" },
273 { ".debug_line.dwo", ".zdebug_line.dwo" },
274 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
275 { ".debug_str.dwo", ".zdebug_str.dwo" },
276 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
277 { ".debug_types.dwo", ".zdebug_types.dwo" },
278};
279
c906108c
SS
280/* local data types */
281
0963b4bd 282/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
283#ifndef ABBREV_HASH_SIZE
284#define ABBREV_HASH_SIZE 121
285#endif
286
107d2387
AC
287/* The data in a compilation unit header, after target2host
288 translation, looks like this. */
c906108c 289struct comp_unit_head
a738430d 290{
c764a876 291 unsigned int length;
a738430d 292 short version;
a738430d
MK
293 unsigned char addr_size;
294 unsigned char signed_addr_p;
b64f50a1 295 sect_offset abbrev_offset;
57349743 296
a738430d
MK
297 /* Size of file offsets; either 4 or 8. */
298 unsigned int offset_size;
57349743 299
a738430d
MK
300 /* Size of the length field; either 4 or 12. */
301 unsigned int initial_length_size;
57349743 302
a738430d
MK
303 /* Offset to the first byte of this compilation unit header in the
304 .debug_info section, for resolving relative reference dies. */
b64f50a1 305 sect_offset offset;
57349743 306
d00adf39
DE
307 /* Offset to first die in this cu from the start of the cu.
308 This will be the first byte following the compilation unit header. */
b64f50a1 309 cu_offset first_die_offset;
a738430d 310};
c906108c 311
3da10d80
KS
312/* Type used for delaying computation of method physnames.
313 See comments for compute_delayed_physnames. */
314struct delayed_method_info
315{
316 /* The type to which the method is attached, i.e., its parent class. */
317 struct type *type;
318
319 /* The index of the method in the type's function fieldlists. */
320 int fnfield_index;
321
322 /* The index of the method in the fieldlist. */
323 int index;
324
325 /* The name of the DIE. */
326 const char *name;
327
328 /* The DIE associated with this method. */
329 struct die_info *die;
330};
331
332typedef struct delayed_method_info delayed_method_info;
333DEF_VEC_O (delayed_method_info);
334
e7c27a73
DJ
335/* Internal state when decoding a particular compilation unit. */
336struct dwarf2_cu
337{
338 /* The objfile containing this compilation unit. */
339 struct objfile *objfile;
340
d00adf39 341 /* The header of the compilation unit. */
e7c27a73 342 struct comp_unit_head header;
e142c38c 343
d00adf39
DE
344 /* Base address of this compilation unit. */
345 CORE_ADDR base_address;
346
347 /* Non-zero if base_address has been set. */
348 int base_known;
349
e142c38c
DJ
350 /* The language we are debugging. */
351 enum language language;
352 const struct language_defn *language_defn;
353
b0f35d58
DL
354 const char *producer;
355
e142c38c
DJ
356 /* The generic symbol table building routines have separate lists for
357 file scope symbols and all all other scopes (local scopes). So
358 we need to select the right one to pass to add_symbol_to_list().
359 We do it by keeping a pointer to the correct list in list_in_scope.
360
361 FIXME: The original dwarf code just treated the file scope as the
362 first local scope, and all other local scopes as nested local
363 scopes, and worked fine. Check to see if we really need to
364 distinguish these in buildsym.c. */
365 struct pending **list_in_scope;
366
f3dd6933
DJ
367 /* DWARF abbreviation table associated with this compilation unit. */
368 struct abbrev_info **dwarf2_abbrevs;
369
370 /* Storage for the abbrev table. */
371 struct obstack abbrev_obstack;
72bf9492 372
b64f50a1
JK
373 /* Hash table holding all the loaded partial DIEs
374 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
375 htab_t partial_dies;
376
377 /* Storage for things with the same lifetime as this read-in compilation
378 unit, including partial DIEs. */
379 struct obstack comp_unit_obstack;
380
ae038cb0
DJ
381 /* When multiple dwarf2_cu structures are living in memory, this field
382 chains them all together, so that they can be released efficiently.
383 We will probably also want a generation counter so that most-recently-used
384 compilation units are cached... */
385 struct dwarf2_per_cu_data *read_in_chain;
386
387 /* Backchain to our per_cu entry if the tree has been built. */
388 struct dwarf2_per_cu_data *per_cu;
389
390 /* How many compilation units ago was this CU last referenced? */
391 int last_used;
392
b64f50a1
JK
393 /* A hash table of DIE cu_offset for following references with
394 die_info->offset.sect_off as hash. */
51545339 395 htab_t die_hash;
10b3939b
DJ
396
397 /* Full DIEs if read in. */
398 struct die_info *dies;
399
400 /* A set of pointers to dwarf2_per_cu_data objects for compilation
401 units referenced by this one. Only set during full symbol processing;
402 partial symbol tables do not have dependencies. */
403 htab_t dependencies;
404
cb1df416
DJ
405 /* Header data from the line table, during full symbol processing. */
406 struct line_header *line_header;
407
3da10d80
KS
408 /* A list of methods which need to have physnames computed
409 after all type information has been read. */
410 VEC (delayed_method_info) *method_list;
411
96408a79
SA
412 /* To be copied to symtab->call_site_htab. */
413 htab_t call_site_htab;
414
3019eac3
DE
415 /* Non-NULL if this CU came from a DWO file. */
416 struct dwo_unit *dwo_unit;
417
418 /* The DW_AT_addr_base attribute if present, zero otherwise
419 (zero is a valid value though).
420 Note this value comes from the stub CU/TU's DIE. */
421 ULONGEST addr_base;
422
ae038cb0
DJ
423 /* Mark used when releasing cached dies. */
424 unsigned int mark : 1;
425
8be455d7
JK
426 /* This CU references .debug_loc. See the symtab->locations_valid field.
427 This test is imperfect as there may exist optimized debug code not using
428 any location list and still facing inlining issues if handled as
429 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 430 unsigned int has_loclist : 1;
ba919b58
TT
431
432 /* These cache the results of producer_is_gxx_lt_4_6.
433 CHECKED_PRODUCER is set if PRODUCER_IS_GXX_LT_4_6 is valid. This
434 information is cached because profiling CU expansion showed
435 excessive time spent in producer_is_gxx_lt_4_6. */
436 unsigned int checked_producer : 1;
437 unsigned int producer_is_gxx_lt_4_6 : 1;
3019eac3
DE
438
439 /* Non-zero if DW_AT_addr_base was found.
440 Used when processing DWO files. */
441 unsigned int have_addr_base : 1;
e7c27a73
DJ
442};
443
10b3939b
DJ
444/* Persistent data held for a compilation unit, even when not
445 processing it. We put a pointer to this structure in the
28dee7f5 446 read_symtab_private field of the psymtab. */
10b3939b 447
ae038cb0
DJ
448struct dwarf2_per_cu_data
449{
348e048f 450 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 451 bytes should suffice to store the length of any compilation unit
45452591
DE
452 - if it doesn't, GDB will fall over anyway.
453 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
454 initial_length_size.
455 If the DIE refers to a DWO file, this is always of the original die,
456 not the DWO file. */
b64f50a1 457 sect_offset offset;
348e048f 458 unsigned int length : 29;
ae038cb0
DJ
459
460 /* Flag indicating this compilation unit will be read in before
461 any of the current compilation units are processed. */
c764a876 462 unsigned int queued : 1;
ae038cb0 463
0d99eb77
DE
464 /* This flag will be set when reading partial DIEs if we need to load
465 absolutely all DIEs for this compilation unit, instead of just the ones
466 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
467 hash table and don't find it. */
468 unsigned int load_all_dies : 1;
469
3019eac3
DE
470 /* Non-zero if this CU is from .debug_types. */
471 unsigned int is_debug_types : 1;
472
473 /* The section this CU/TU lives in.
474 If the DIE refers to a DWO file, this is always the original die,
475 not the DWO file. */
476 struct dwarf2_section_info *info_or_types_section;
348e048f 477
17ea53c3
JK
478 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
479 of the CU cache it gets reset to NULL again. */
ae038cb0 480 struct dwarf2_cu *cu;
1c379e20 481
9cdd5dbd
DE
482 /* The corresponding objfile.
483 Normally we can get the objfile from dwarf2_per_objfile.
484 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
485 struct objfile *objfile;
486
487 /* When using partial symbol tables, the 'psymtab' field is active.
488 Otherwise the 'quick' field is active. */
489 union
490 {
491 /* The partial symbol table associated with this compilation unit,
95554aad 492 or NULL for unread partial units. */
9291a0cd
TT
493 struct partial_symtab *psymtab;
494
495 /* Data needed by the "quick" functions. */
496 struct dwarf2_per_cu_quick_data *quick;
497 } v;
95554aad
TT
498
499 /* The CUs we import using DW_TAG_imported_unit. This is filled in
500 while reading psymtabs, used to compute the psymtab dependencies,
501 and then cleared. Then it is filled in again while reading full
502 symbols, and only deleted when the objfile is destroyed. */
503 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
504};
505
348e048f
DE
506/* Entry in the signatured_types hash table. */
507
508struct signatured_type
509{
3019eac3 510 /* The type's signature. */
348e048f
DE
511 ULONGEST signature;
512
3019eac3
DE
513 /* Offset in the TU of the type's DIE, as read from the TU header.
514 If the definition lives in a DWO file, this value is unusable. */
515 cu_offset type_offset_in_tu;
516
517 /* Offset in the section of the type's DIE.
518 If the definition lives in a DWO file, this is the offset in the
519 .debug_types.dwo section.
520 The value is zero until the actual value is known.
521 Zero is otherwise not a valid section offset. */
522 sect_offset type_offset_in_section;
348e048f
DE
523
524 /* The CU(/TU) of this type. */
525 struct dwarf2_per_cu_data per_cu;
526};
527
3019eac3
DE
528/* These sections are what may appear in a "dwo" file. */
529
530struct dwo_sections
531{
532 struct dwarf2_section_info abbrev;
533 struct dwarf2_section_info info;
534 struct dwarf2_section_info line;
535 struct dwarf2_section_info loc;
536 struct dwarf2_section_info str;
537 struct dwarf2_section_info str_offsets;
538 VEC (dwarf2_section_info_def) *types;
539};
540
541/* Common bits of DWO CUs/TUs. */
542
543struct dwo_unit
544{
545 /* Backlink to the containing struct dwo_file. */
546 struct dwo_file *dwo_file;
547
548 /* The "id" that distinguishes this CU/TU.
549 .debug_info calls this "dwo_id", .debug_types calls this "signature".
550 Since signatures came first, we stick with it for consistency. */
551 ULONGEST signature;
552
553 /* The section this CU/TU lives in, in the DWO file. */
554 struct dwarf2_section_info *info_or_types_section;
555
556 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
557 sect_offset offset;
558 unsigned int length;
559
560 /* For types, offset in the type's DIE of the type defined by this TU. */
561 cu_offset type_offset_in_tu;
562};
563
564/* Data for one DWO file. */
565
566struct dwo_file
567{
568 /* The DW_AT_GNU_dwo_name attribute.
569 We don't manage space for this, it's an attribute. */
570 const char *dwo_name;
571
572 /* The bfd, when the file is open. Otherwise this is NULL. */
573 bfd *dwo_bfd;
574
575 /* Section info for this file. */
576 struct dwo_sections sections;
577
578 /* Table of CUs in the file.
579 Each element is a struct dwo_unit. */
580 htab_t cus;
581
582 /* Table of TUs in the file.
583 Each element is a struct dwo_unit. */
584 htab_t tus;
585};
586
0963b4bd
MS
587/* Struct used to pass misc. parameters to read_die_and_children, et
588 al. which are used for both .debug_info and .debug_types dies.
589 All parameters here are unchanging for the life of the call. This
dee91e82 590 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
591
592struct die_reader_specs
593{
dee91e82 594 /* die_section->asection->owner. */
93311388
DE
595 bfd* abfd;
596
597 /* The CU of the DIE we are parsing. */
598 struct dwarf2_cu *cu;
599
3019eac3
DE
600 /* Non-NULL if reading a DWO file. */
601 struct dwo_file *dwo_file;
602
dee91e82 603 /* The section the die comes from.
3019eac3 604 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
605 struct dwarf2_section_info *die_section;
606
607 /* die_section->buffer. */
608 gdb_byte *buffer;
93311388
DE
609};
610
fd820528 611/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
612typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
613 gdb_byte *info_ptr,
614 struct die_info *comp_unit_die,
615 int has_children,
616 void *data);
617
debd256d
JB
618/* The line number information for a compilation unit (found in the
619 .debug_line section) begins with a "statement program header",
620 which contains the following information. */
621struct line_header
622{
623 unsigned int total_length;
624 unsigned short version;
625 unsigned int header_length;
626 unsigned char minimum_instruction_length;
2dc7f7b3 627 unsigned char maximum_ops_per_instruction;
debd256d
JB
628 unsigned char default_is_stmt;
629 int line_base;
630 unsigned char line_range;
631 unsigned char opcode_base;
632
633 /* standard_opcode_lengths[i] is the number of operands for the
634 standard opcode whose value is i. This means that
635 standard_opcode_lengths[0] is unused, and the last meaningful
636 element is standard_opcode_lengths[opcode_base - 1]. */
637 unsigned char *standard_opcode_lengths;
638
639 /* The include_directories table. NOTE! These strings are not
640 allocated with xmalloc; instead, they are pointers into
641 debug_line_buffer. If you try to free them, `free' will get
642 indigestion. */
643 unsigned int num_include_dirs, include_dirs_size;
644 char **include_dirs;
645
646 /* The file_names table. NOTE! These strings are not allocated
647 with xmalloc; instead, they are pointers into debug_line_buffer.
648 Don't try to free them directly. */
649 unsigned int num_file_names, file_names_size;
650 struct file_entry
c906108c 651 {
debd256d
JB
652 char *name;
653 unsigned int dir_index;
654 unsigned int mod_time;
655 unsigned int length;
aaa75496 656 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 657 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
658 } *file_names;
659
660 /* The start and end of the statement program following this
6502dd73 661 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 662 gdb_byte *statement_program_start, *statement_program_end;
debd256d 663};
c906108c
SS
664
665/* When we construct a partial symbol table entry we only
0963b4bd 666 need this much information. */
c906108c
SS
667struct partial_die_info
668 {
72bf9492 669 /* Offset of this DIE. */
b64f50a1 670 sect_offset offset;
72bf9492
DJ
671
672 /* DWARF-2 tag for this DIE. */
673 ENUM_BITFIELD(dwarf_tag) tag : 16;
674
72bf9492
DJ
675 /* Assorted flags describing the data found in this DIE. */
676 unsigned int has_children : 1;
677 unsigned int is_external : 1;
678 unsigned int is_declaration : 1;
679 unsigned int has_type : 1;
680 unsigned int has_specification : 1;
681 unsigned int has_pc_info : 1;
481860b3 682 unsigned int may_be_inlined : 1;
72bf9492
DJ
683
684 /* Flag set if the SCOPE field of this structure has been
685 computed. */
686 unsigned int scope_set : 1;
687
fa4028e9
JB
688 /* Flag set if the DIE has a byte_size attribute. */
689 unsigned int has_byte_size : 1;
690
98bfdba5
PA
691 /* Flag set if any of the DIE's children are template arguments. */
692 unsigned int has_template_arguments : 1;
693
abc72ce4
DE
694 /* Flag set if fixup_partial_die has been called on this die. */
695 unsigned int fixup_called : 1;
696
72bf9492 697 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 698 sometimes a default name for unnamed DIEs. */
c906108c 699 char *name;
72bf9492 700
abc72ce4
DE
701 /* The linkage name, if present. */
702 const char *linkage_name;
703
72bf9492
DJ
704 /* The scope to prepend to our children. This is generally
705 allocated on the comp_unit_obstack, so will disappear
706 when this compilation unit leaves the cache. */
707 char *scope;
708
95554aad
TT
709 /* Some data associated with the partial DIE. The tag determines
710 which field is live. */
711 union
712 {
713 /* The location description associated with this DIE, if any. */
714 struct dwarf_block *locdesc;
715 /* The offset of an import, for DW_TAG_imported_unit. */
716 sect_offset offset;
717 } d;
72bf9492
DJ
718
719 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
720 CORE_ADDR lowpc;
721 CORE_ADDR highpc;
72bf9492 722
93311388 723 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 724 DW_AT_sibling, if any. */
abc72ce4
DE
725 /* NOTE: This member isn't strictly necessary, read_partial_die could
726 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 727 gdb_byte *sibling;
72bf9492
DJ
728
729 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
730 DW_AT_specification (or DW_AT_abstract_origin or
731 DW_AT_extension). */
b64f50a1 732 sect_offset spec_offset;
72bf9492
DJ
733
734 /* Pointers to this DIE's parent, first child, and next sibling,
735 if any. */
736 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
737 };
738
0963b4bd 739/* This data structure holds the information of an abbrev. */
c906108c
SS
740struct abbrev_info
741 {
742 unsigned int number; /* number identifying abbrev */
743 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
744 unsigned short has_children; /* boolean */
745 unsigned short num_attrs; /* number of attributes */
c906108c
SS
746 struct attr_abbrev *attrs; /* an array of attribute descriptions */
747 struct abbrev_info *next; /* next in chain */
748 };
749
750struct attr_abbrev
751 {
9d25dd43
DE
752 ENUM_BITFIELD(dwarf_attribute) name : 16;
753 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
754 };
755
0963b4bd 756/* Attributes have a name and a value. */
b60c80d6
DJ
757struct attribute
758 {
9d25dd43 759 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
760 ENUM_BITFIELD(dwarf_form) form : 15;
761
762 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
763 field should be in u.str (existing only for DW_STRING) but it is kept
764 here for better struct attribute alignment. */
765 unsigned int string_is_canonical : 1;
766
b60c80d6
DJ
767 union
768 {
769 char *str;
770 struct dwarf_block *blk;
43bbcdc2
PH
771 ULONGEST unsnd;
772 LONGEST snd;
b60c80d6 773 CORE_ADDR addr;
348e048f 774 struct signatured_type *signatured_type;
b60c80d6
DJ
775 }
776 u;
777 };
778
0963b4bd 779/* This data structure holds a complete die structure. */
c906108c
SS
780struct die_info
781 {
76815b17
DE
782 /* DWARF-2 tag for this DIE. */
783 ENUM_BITFIELD(dwarf_tag) tag : 16;
784
785 /* Number of attributes */
98bfdba5
PA
786 unsigned char num_attrs;
787
788 /* True if we're presently building the full type name for the
789 type derived from this DIE. */
790 unsigned char building_fullname : 1;
76815b17
DE
791
792 /* Abbrev number */
793 unsigned int abbrev;
794
93311388 795 /* Offset in .debug_info or .debug_types section. */
b64f50a1 796 sect_offset offset;
78ba4af6
JB
797
798 /* The dies in a compilation unit form an n-ary tree. PARENT
799 points to this die's parent; CHILD points to the first child of
800 this node; and all the children of a given node are chained
4950bc1c 801 together via their SIBLING fields. */
639d11d3
DC
802 struct die_info *child; /* Its first child, if any. */
803 struct die_info *sibling; /* Its next sibling, if any. */
804 struct die_info *parent; /* Its parent, if any. */
c906108c 805
b60c80d6
DJ
806 /* An array of attributes, with NUM_ATTRS elements. There may be
807 zero, but it's not common and zero-sized arrays are not
808 sufficiently portable C. */
809 struct attribute attrs[1];
c906108c
SS
810 };
811
0963b4bd 812/* Get at parts of an attribute structure. */
c906108c
SS
813
814#define DW_STRING(attr) ((attr)->u.str)
8285870a 815#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
816#define DW_UNSND(attr) ((attr)->u.unsnd)
817#define DW_BLOCK(attr) ((attr)->u.blk)
818#define DW_SND(attr) ((attr)->u.snd)
819#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 820#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 821
0963b4bd 822/* Blocks are a bunch of untyped bytes. */
c906108c
SS
823struct dwarf_block
824 {
825 unsigned int size;
1d6edc3c
JK
826
827 /* Valid only if SIZE is not zero. */
fe1b8b76 828 gdb_byte *data;
c906108c
SS
829 };
830
c906108c
SS
831#ifndef ATTR_ALLOC_CHUNK
832#define ATTR_ALLOC_CHUNK 4
833#endif
834
c906108c
SS
835/* Allocate fields for structs, unions and enums in this size. */
836#ifndef DW_FIELD_ALLOC_CHUNK
837#define DW_FIELD_ALLOC_CHUNK 4
838#endif
839
c906108c
SS
840/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
841 but this would require a corresponding change in unpack_field_as_long
842 and friends. */
843static int bits_per_byte = 8;
844
845/* The routines that read and process dies for a C struct or C++ class
846 pass lists of data member fields and lists of member function fields
847 in an instance of a field_info structure, as defined below. */
848struct field_info
c5aa993b 849 {
0963b4bd 850 /* List of data member and baseclasses fields. */
c5aa993b
JM
851 struct nextfield
852 {
853 struct nextfield *next;
854 int accessibility;
855 int virtuality;
856 struct field field;
857 }
7d0ccb61 858 *fields, *baseclasses;
c906108c 859
7d0ccb61 860 /* Number of fields (including baseclasses). */
c5aa993b 861 int nfields;
c906108c 862
c5aa993b
JM
863 /* Number of baseclasses. */
864 int nbaseclasses;
c906108c 865
c5aa993b
JM
866 /* Set if the accesibility of one of the fields is not public. */
867 int non_public_fields;
c906108c 868
c5aa993b
JM
869 /* Member function fields array, entries are allocated in the order they
870 are encountered in the object file. */
871 struct nextfnfield
872 {
873 struct nextfnfield *next;
874 struct fn_field fnfield;
875 }
876 *fnfields;
c906108c 877
c5aa993b
JM
878 /* Member function fieldlist array, contains name of possibly overloaded
879 member function, number of overloaded member functions and a pointer
880 to the head of the member function field chain. */
881 struct fnfieldlist
882 {
883 char *name;
884 int length;
885 struct nextfnfield *head;
886 }
887 *fnfieldlists;
c906108c 888
c5aa993b
JM
889 /* Number of entries in the fnfieldlists array. */
890 int nfnfields;
98751a41
JK
891
892 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
893 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
894 struct typedef_field_list
895 {
896 struct typedef_field field;
897 struct typedef_field_list *next;
898 }
899 *typedef_field_list;
900 unsigned typedef_field_list_count;
c5aa993b 901 };
c906108c 902
10b3939b
DJ
903/* One item on the queue of compilation units to read in full symbols
904 for. */
905struct dwarf2_queue_item
906{
907 struct dwarf2_per_cu_data *per_cu;
95554aad 908 enum language pretend_language;
10b3939b
DJ
909 struct dwarf2_queue_item *next;
910};
911
912/* The current queue. */
913static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
914
ae038cb0
DJ
915/* Loaded secondary compilation units are kept in memory until they
916 have not been referenced for the processing of this many
917 compilation units. Set this to zero to disable caching. Cache
918 sizes of up to at least twenty will improve startup time for
919 typical inter-CU-reference binaries, at an obvious memory cost. */
920static int dwarf2_max_cache_age = 5;
920d2a44
AC
921static void
922show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
923 struct cmd_list_element *c, const char *value)
924{
3e43a32a
MS
925 fprintf_filtered (file, _("The upper bound on the age of cached "
926 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
927 value);
928}
929
ae038cb0 930
0963b4bd 931/* Various complaints about symbol reading that don't abort the process. */
c906108c 932
4d3c2250
KB
933static void
934dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 935{
4d3c2250 936 complaint (&symfile_complaints,
e2e0b3e5 937 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
938}
939
25e43795
DJ
940static void
941dwarf2_debug_line_missing_file_complaint (void)
942{
943 complaint (&symfile_complaints,
944 _(".debug_line section has line data without a file"));
945}
946
59205f5a
JB
947static void
948dwarf2_debug_line_missing_end_sequence_complaint (void)
949{
950 complaint (&symfile_complaints,
3e43a32a
MS
951 _(".debug_line section has line "
952 "program sequence without an end"));
59205f5a
JB
953}
954
4d3c2250
KB
955static void
956dwarf2_complex_location_expr_complaint (void)
2e276125 957{
e2e0b3e5 958 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
959}
960
4d3c2250
KB
961static void
962dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
963 int arg3)
2e276125 964{
4d3c2250 965 complaint (&symfile_complaints,
3e43a32a
MS
966 _("const value length mismatch for '%s', got %d, expected %d"),
967 arg1, arg2, arg3);
4d3c2250
KB
968}
969
970static void
cf2c3c16 971dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
2e276125 972{
4d3c2250 973 complaint (&symfile_complaints,
cf2c3c16
TT
974 _("macro info runs off end of `%s' section"),
975 section->asection->name);
4d3c2250
KB
976}
977
978static void
979dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 980{
4d3c2250 981 complaint (&symfile_complaints,
3e43a32a
MS
982 _("macro debug info contains a "
983 "malformed macro definition:\n`%s'"),
4d3c2250
KB
984 arg1);
985}
986
987static void
988dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 989{
4d3c2250 990 complaint (&symfile_complaints,
3e43a32a
MS
991 _("invalid attribute class or form for '%s' in '%s'"),
992 arg1, arg2);
4d3c2250 993}
c906108c 994
c906108c
SS
995/* local function prototypes */
996
4efb68b1 997static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 998
aaa75496
JB
999static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1000 struct objfile *);
1001
918dd910
JK
1002static void dwarf2_find_base_address (struct die_info *die,
1003 struct dwarf2_cu *cu);
1004
c67a9c90 1005static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1006
72bf9492
DJ
1007static void scan_partial_symbols (struct partial_die_info *,
1008 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1009 int, struct dwarf2_cu *);
c906108c 1010
72bf9492
DJ
1011static void add_partial_symbol (struct partial_die_info *,
1012 struct dwarf2_cu *);
63d06c5c 1013
72bf9492
DJ
1014static void add_partial_namespace (struct partial_die_info *pdi,
1015 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1016 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1017
5d7cb8df
JK
1018static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1019 CORE_ADDR *highpc, int need_pc,
1020 struct dwarf2_cu *cu);
1021
72bf9492
DJ
1022static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1023 struct dwarf2_cu *cu);
91c24f0a 1024
bc30ff58
JB
1025static void add_partial_subprogram (struct partial_die_info *pdi,
1026 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1027 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1028
a14ed312 1029static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 1030
a14ed312 1031static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1032
dee91e82
DE
1033static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1034 struct dwarf2_section_info *);
c906108c 1035
f3dd6933 1036static void dwarf2_free_abbrev_table (void *);
c906108c 1037
6caca83c
CC
1038static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1039
fe1b8b76 1040static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 1041 struct dwarf2_cu *);
72bf9492 1042
57349743 1043static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 1044 struct dwarf2_cu *);
c906108c 1045
dee91e82
DE
1046static struct partial_die_info *load_partial_dies
1047 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1048
dee91e82
DE
1049static gdb_byte *read_partial_die (const struct die_reader_specs *,
1050 struct partial_die_info *,
1051 struct abbrev_info *,
1052 unsigned int,
1053 gdb_byte *);
c906108c 1054
b64f50a1 1055static struct partial_die_info *find_partial_die (sect_offset,
10b3939b 1056 struct dwarf2_cu *);
72bf9492
DJ
1057
1058static void fixup_partial_die (struct partial_die_info *,
1059 struct dwarf2_cu *);
1060
dee91e82
DE
1061static gdb_byte *read_attribute (const struct die_reader_specs *,
1062 struct attribute *, struct attr_abbrev *,
1063 gdb_byte *);
a8329558 1064
fe1b8b76 1065static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 1066
fe1b8b76 1067static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 1068
fe1b8b76 1069static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 1070
fe1b8b76 1071static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 1072
93311388 1073static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 1074
fe1b8b76 1075static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1076 unsigned int *);
c906108c 1077
c764a876
DE
1078static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1079
1080static LONGEST read_checked_initial_length_and_offset
1081 (bfd *, gdb_byte *, const struct comp_unit_head *,
1082 unsigned int *, unsigned int *);
613e1657 1083
fe1b8b76 1084static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1085 unsigned int *);
1086
1087static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1088
fe1b8b76 1089static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1090
9b1c24c8 1091static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1092
fe1b8b76
JB
1093static char *read_indirect_string (bfd *, gdb_byte *,
1094 const struct comp_unit_head *,
1095 unsigned int *);
4bdf3d34 1096
12df843f 1097static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1098
12df843f 1099static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1100
3019eac3
DE
1101static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1102 unsigned int *);
1103
1104static char *read_str_index (const struct die_reader_specs *reader,
1105 struct dwarf2_cu *cu, ULONGEST str_index);
1106
fe1b8b76 1107static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
4bb7a0a7 1108
e142c38c 1109static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1110
e142c38c
DJ
1111static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1112 struct dwarf2_cu *);
c906108c 1113
348e048f
DE
1114static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1115 unsigned int,
1116 struct dwarf2_cu *);
1117
05cf31d1
JB
1118static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1119 struct dwarf2_cu *cu);
1120
e142c38c 1121static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1122
e142c38c 1123static struct die_info *die_specification (struct die_info *die,
f2f0e013 1124 struct dwarf2_cu **);
63d06c5c 1125
debd256d
JB
1126static void free_line_header (struct line_header *lh);
1127
aaa75496
JB
1128static void add_file_name (struct line_header *, char *, unsigned int,
1129 unsigned int, unsigned int);
1130
3019eac3
DE
1131static struct line_header *dwarf_decode_line_header (unsigned int offset,
1132 struct dwarf2_cu *cu);
debd256d 1133
f3f5162e
DE
1134static void dwarf_decode_lines (struct line_header *, const char *,
1135 struct dwarf2_cu *, struct partial_symtab *,
1136 int);
c906108c 1137
72b9f47f 1138static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1139
a14ed312 1140static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1141 struct dwarf2_cu *);
c906108c 1142
34eaf542
TT
1143static struct symbol *new_symbol_full (struct die_info *, struct type *,
1144 struct dwarf2_cu *, struct symbol *);
1145
a14ed312 1146static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1147 struct dwarf2_cu *);
c906108c 1148
98bfdba5
PA
1149static void dwarf2_const_value_attr (struct attribute *attr,
1150 struct type *type,
1151 const char *name,
1152 struct obstack *obstack,
12df843f 1153 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1154 gdb_byte **bytes,
1155 struct dwarf2_locexpr_baton **baton);
2df3850c 1156
e7c27a73 1157static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1158
b4ba55a1
JB
1159static int need_gnat_info (struct dwarf2_cu *);
1160
3e43a32a
MS
1161static struct type *die_descriptive_type (struct die_info *,
1162 struct dwarf2_cu *);
b4ba55a1
JB
1163
1164static void set_descriptive_type (struct type *, struct die_info *,
1165 struct dwarf2_cu *);
1166
e7c27a73
DJ
1167static struct type *die_containing_type (struct die_info *,
1168 struct dwarf2_cu *);
c906108c 1169
673bfd45
DE
1170static struct type *lookup_die_type (struct die_info *, struct attribute *,
1171 struct dwarf2_cu *);
c906108c 1172
f792889a 1173static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1174
673bfd45
DE
1175static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1176
0d5cff50 1177static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1178
6e70227d 1179static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1180 const char *suffix, int physname,
1181 struct dwarf2_cu *cu);
63d06c5c 1182
e7c27a73 1183static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1184
348e048f
DE
1185static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1186
e7c27a73 1187static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1188
e7c27a73 1189static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1190
96408a79
SA
1191static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1192
ff013f42
JK
1193static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1194 struct dwarf2_cu *, struct partial_symtab *);
1195
a14ed312 1196static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1197 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1198 struct partial_symtab *);
c906108c 1199
fae299cd
DC
1200static void get_scope_pc_bounds (struct die_info *,
1201 CORE_ADDR *, CORE_ADDR *,
1202 struct dwarf2_cu *);
1203
801e3a5b
JB
1204static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1205 CORE_ADDR, struct dwarf2_cu *);
1206
a14ed312 1207static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1208 struct dwarf2_cu *);
c906108c 1209
a14ed312 1210static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1211 struct type *, struct dwarf2_cu *);
c906108c 1212
a14ed312 1213static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1214 struct die_info *, struct type *,
e7c27a73 1215 struct dwarf2_cu *);
c906108c 1216
a14ed312 1217static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1218 struct type *,
1219 struct dwarf2_cu *);
c906108c 1220
134d01f1 1221static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1222
e7c27a73 1223static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1224
e7c27a73 1225static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1226
5d7cb8df
JK
1227static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1228
27aa8d6a
SW
1229static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1230
f55ee35c
JK
1231static struct type *read_module_type (struct die_info *die,
1232 struct dwarf2_cu *cu);
1233
38d518c9 1234static const char *namespace_name (struct die_info *die,
e142c38c 1235 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1236
134d01f1 1237static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1238
e7c27a73 1239static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1240
6e70227d 1241static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1242 struct dwarf2_cu *);
1243
dee91e82 1244static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1245 gdb_byte *info_ptr,
fe1b8b76 1246 gdb_byte **new_info_ptr,
639d11d3
DC
1247 struct die_info *parent);
1248
dee91e82 1249static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1250 gdb_byte *info_ptr,
fe1b8b76 1251 gdb_byte **new_info_ptr,
639d11d3
DC
1252 struct die_info *parent);
1253
3019eac3
DE
1254static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1255 struct die_info **, gdb_byte *, int *, int);
1256
dee91e82
DE
1257static gdb_byte *read_full_die (const struct die_reader_specs *,
1258 struct die_info **, gdb_byte *, int *);
93311388 1259
e7c27a73 1260static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1261
71c25dea
TT
1262static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1263 struct obstack *);
1264
e142c38c 1265static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1266
98bfdba5
PA
1267static const char *dwarf2_full_name (char *name,
1268 struct die_info *die,
1269 struct dwarf2_cu *cu);
1270
e142c38c 1271static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1272 struct dwarf2_cu **);
9219021c 1273
f39c6ffd 1274static const char *dwarf_tag_name (unsigned int);
c906108c 1275
f39c6ffd 1276static const char *dwarf_attr_name (unsigned int);
c906108c 1277
f39c6ffd 1278static const char *dwarf_form_name (unsigned int);
c906108c 1279
a14ed312 1280static char *dwarf_bool_name (unsigned int);
c906108c 1281
f39c6ffd 1282static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1283
f9aca02d 1284static struct die_info *sibling_die (struct die_info *);
c906108c 1285
d97bc12b
DE
1286static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1287
1288static void dump_die_for_error (struct die_info *);
1289
1290static void dump_die_1 (struct ui_file *, int level, int max_level,
1291 struct die_info *);
c906108c 1292
d97bc12b 1293/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1294
51545339 1295static void store_in_ref_table (struct die_info *,
10b3939b 1296 struct dwarf2_cu *);
c906108c 1297
93311388
DE
1298static int is_ref_attr (struct attribute *);
1299
b64f50a1 1300static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1301
43bbcdc2 1302static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1303
348e048f
DE
1304static struct die_info *follow_die_ref_or_sig (struct die_info *,
1305 struct attribute *,
1306 struct dwarf2_cu **);
1307
10b3939b
DJ
1308static struct die_info *follow_die_ref (struct die_info *,
1309 struct attribute *,
f2f0e013 1310 struct dwarf2_cu **);
c906108c 1311
348e048f
DE
1312static struct die_info *follow_die_sig (struct die_info *,
1313 struct attribute *,
1314 struct dwarf2_cu **);
1315
6c83ed52
TT
1316static struct signatured_type *lookup_signatured_type_at_offset
1317 (struct objfile *objfile,
b64f50a1 1318 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1319
e5fe5e75 1320static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1321
52dc124a 1322static void read_signatured_type (struct signatured_type *);
348e048f 1323
c906108c
SS
1324/* memory allocation interface */
1325
7b5a2f43 1326static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1327
f3dd6933 1328static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1329
b60c80d6 1330static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1331
2e276125 1332static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1333 char *, bfd *, struct dwarf2_cu *,
1334 struct dwarf2_section_info *,
fceca515 1335 int, const char *);
2e276125 1336
8e19ed76
PS
1337static int attr_form_is_block (struct attribute *);
1338
3690dd37
JB
1339static int attr_form_is_section_offset (struct attribute *);
1340
1341static int attr_form_is_constant (struct attribute *);
1342
8cf6f0b1
TT
1343static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1344 struct dwarf2_loclist_baton *baton,
1345 struct attribute *attr);
1346
93e7bd98
DJ
1347static void dwarf2_symbol_mark_computed (struct attribute *attr,
1348 struct symbol *sym,
1349 struct dwarf2_cu *cu);
4c2df51b 1350
dee91e82
DE
1351static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1352 gdb_byte *info_ptr,
1353 struct abbrev_info *abbrev);
4bb7a0a7 1354
72bf9492
DJ
1355static void free_stack_comp_unit (void *);
1356
72bf9492
DJ
1357static hashval_t partial_die_hash (const void *item);
1358
1359static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1360
ae038cb0 1361static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
b64f50a1 1362 (sect_offset offset, struct objfile *objfile);
ae038cb0 1363
9816fde3 1364static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1365 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1366
1367static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1368 struct die_info *comp_unit_die,
1369 enum language pretend_language);
93311388 1370
68dc6402 1371static void free_heap_comp_unit (void *);
ae038cb0
DJ
1372
1373static void free_cached_comp_units (void *);
1374
1375static void age_cached_comp_units (void);
1376
dee91e82 1377static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1378
f792889a
DJ
1379static struct type *set_die_type (struct die_info *, struct type *,
1380 struct dwarf2_cu *);
1c379e20 1381
ae038cb0
DJ
1382static void create_all_comp_units (struct objfile *);
1383
0e50663e 1384static int create_all_type_units (struct objfile *);
1fd400ff 1385
95554aad
TT
1386static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1387 enum language);
10b3939b 1388
95554aad
TT
1389static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1390 enum language);
10b3939b
DJ
1391
1392static void dwarf2_add_dependence (struct dwarf2_cu *,
1393 struct dwarf2_per_cu_data *);
1394
ae038cb0
DJ
1395static void dwarf2_mark (struct dwarf2_cu *);
1396
1397static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1398
b64f50a1 1399static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1400 struct dwarf2_per_cu_data *per_cu);
1401
f792889a 1402static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1403
9291a0cd
TT
1404static void dwarf2_release_queue (void *dummy);
1405
95554aad
TT
1406static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1407 enum language pretend_language);
1408
1409static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1410 struct dwarf2_per_cu_data *per_cu,
1411 enum language pretend_language);
9291a0cd 1412
a0f42c21 1413static void process_queue (void);
9291a0cd
TT
1414
1415static void find_file_and_directory (struct die_info *die,
1416 struct dwarf2_cu *cu,
1417 char **name, char **comp_dir);
1418
1419static char *file_full_name (int file, struct line_header *lh,
1420 const char *comp_dir);
1421
9ff913ba
DE
1422static gdb_byte *read_and_check_comp_unit_head
1423 (struct comp_unit_head *header,
1424 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1425 int is_debug_types_section);
9291a0cd 1426
fd820528
DE
1427static void init_cutu_and_read_dies
1428 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
3019eac3
DE
1429 die_reader_func_ftype *die_reader_func, void *data);
1430
dee91e82
DE
1431static void init_cutu_and_read_dies_simple
1432 (struct dwarf2_per_cu_data *this_cu,
1433 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1434
673bfd45 1435static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1436
95554aad 1437static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *, int);
dee91e82 1438
3019eac3
DE
1439static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1440
1441static struct dwo_unit *lookup_dwo_comp_unit
1442 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1443
1444static struct dwo_unit *lookup_dwo_type_unit
1445 (struct signatured_type *, char *, const char *);
1446
1447static void free_dwo_file_cleanup (void *);
1448
1449static void munmap_section_buffer (struct dwarf2_section_info *);
1450
95554aad
TT
1451static void process_cu_includes (void);
1452
9291a0cd
TT
1453#if WORDS_BIGENDIAN
1454
1455/* Convert VALUE between big- and little-endian. */
1456static offset_type
1457byte_swap (offset_type value)
1458{
1459 offset_type result;
1460
1461 result = (value & 0xff) << 24;
1462 result |= (value & 0xff00) << 8;
1463 result |= (value & 0xff0000) >> 8;
1464 result |= (value & 0xff000000) >> 24;
1465 return result;
1466}
1467
1468#define MAYBE_SWAP(V) byte_swap (V)
1469
1470#else
1471#define MAYBE_SWAP(V) (V)
1472#endif /* WORDS_BIGENDIAN */
1473
1474/* The suffix for an index file. */
1475#define INDEX_SUFFIX ".gdb-index"
1476
3da10d80
KS
1477static const char *dwarf2_physname (char *name, struct die_info *die,
1478 struct dwarf2_cu *cu);
1479
c906108c 1480/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1481 information and return true if we have enough to do something.
1482 NAMES points to the dwarf2 section names, or is NULL if the standard
1483 ELF names are used. */
c906108c
SS
1484
1485int
251d32d9
TG
1486dwarf2_has_info (struct objfile *objfile,
1487 const struct dwarf2_debug_sections *names)
c906108c 1488{
be391dca
TT
1489 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1490 if (!dwarf2_per_objfile)
1491 {
1492 /* Initialize per-objfile state. */
1493 struct dwarf2_per_objfile *data
1494 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1495
be391dca
TT
1496 memset (data, 0, sizeof (*data));
1497 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1498 dwarf2_per_objfile = data;
6502dd73 1499
251d32d9
TG
1500 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1501 (void *) names);
be391dca
TT
1502 dwarf2_per_objfile->objfile = objfile;
1503 }
1504 return (dwarf2_per_objfile->info.asection != NULL
1505 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1506}
1507
251d32d9
TG
1508/* When loading sections, we look either for uncompressed section or for
1509 compressed section names. */
233a11ab
CS
1510
1511static int
251d32d9
TG
1512section_is_p (const char *section_name,
1513 const struct dwarf2_section_names *names)
233a11ab 1514{
251d32d9
TG
1515 if (names->normal != NULL
1516 && strcmp (section_name, names->normal) == 0)
1517 return 1;
1518 if (names->compressed != NULL
1519 && strcmp (section_name, names->compressed) == 0)
1520 return 1;
1521 return 0;
233a11ab
CS
1522}
1523
c906108c
SS
1524/* This function is mapped across the sections and remembers the
1525 offset and size of each of the debugging sections we are interested
1526 in. */
1527
1528static void
251d32d9 1529dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1530{
251d32d9
TG
1531 const struct dwarf2_debug_sections *names;
1532
1533 if (vnames == NULL)
1534 names = &dwarf2_elf_names;
1535 else
1536 names = (const struct dwarf2_debug_sections *) vnames;
1537
1538 if (section_is_p (sectp->name, &names->info))
c906108c 1539 {
dce234bc
PP
1540 dwarf2_per_objfile->info.asection = sectp;
1541 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1542 }
251d32d9 1543 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1544 {
dce234bc
PP
1545 dwarf2_per_objfile->abbrev.asection = sectp;
1546 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1547 }
251d32d9 1548 else if (section_is_p (sectp->name, &names->line))
c906108c 1549 {
dce234bc
PP
1550 dwarf2_per_objfile->line.asection = sectp;
1551 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1552 }
251d32d9 1553 else if (section_is_p (sectp->name, &names->loc))
c906108c 1554 {
dce234bc
PP
1555 dwarf2_per_objfile->loc.asection = sectp;
1556 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1557 }
251d32d9 1558 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1559 {
dce234bc
PP
1560 dwarf2_per_objfile->macinfo.asection = sectp;
1561 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1562 }
cf2c3c16
TT
1563 else if (section_is_p (sectp->name, &names->macro))
1564 {
1565 dwarf2_per_objfile->macro.asection = sectp;
1566 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1567 }
251d32d9 1568 else if (section_is_p (sectp->name, &names->str))
c906108c 1569 {
dce234bc
PP
1570 dwarf2_per_objfile->str.asection = sectp;
1571 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1572 }
3019eac3
DE
1573 else if (section_is_p (sectp->name, &names->addr))
1574 {
1575 dwarf2_per_objfile->addr.asection = sectp;
1576 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1577 }
251d32d9 1578 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1579 {
dce234bc
PP
1580 dwarf2_per_objfile->frame.asection = sectp;
1581 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1582 }
251d32d9 1583 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1584 {
81b9b86e 1585 flagword aflag = bfd_get_section_flags (abfd, sectp);
9a619af0 1586
3799ccc6
EZ
1587 if (aflag & SEC_HAS_CONTENTS)
1588 {
dce234bc
PP
1589 dwarf2_per_objfile->eh_frame.asection = sectp;
1590 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1591 }
b6af0555 1592 }
251d32d9 1593 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1594 {
dce234bc
PP
1595 dwarf2_per_objfile->ranges.asection = sectp;
1596 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1597 }
251d32d9 1598 else if (section_is_p (sectp->name, &names->types))
348e048f 1599 {
8b70b953
TT
1600 struct dwarf2_section_info type_section;
1601
1602 memset (&type_section, 0, sizeof (type_section));
1603 type_section.asection = sectp;
1604 type_section.size = bfd_get_section_size (sectp);
1605
1606 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1607 &type_section);
348e048f 1608 }
251d32d9 1609 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1610 {
1611 dwarf2_per_objfile->gdb_index.asection = sectp;
1612 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1613 }
dce234bc 1614
72dca2f5
FR
1615 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1616 && bfd_section_vma (abfd, sectp) == 0)
1617 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1618}
1619
dce234bc
PP
1620/* Decompress a section that was compressed using zlib. Store the
1621 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1622
1623static void
dce234bc
PP
1624zlib_decompress_section (struct objfile *objfile, asection *sectp,
1625 gdb_byte **outbuf, bfd_size_type *outsize)
1626{
3019eac3 1627 bfd *abfd = sectp->owner;
dce234bc
PP
1628#ifndef HAVE_ZLIB_H
1629 error (_("Support for zlib-compressed DWARF data (from '%s') "
1630 "is disabled in this copy of GDB"),
1631 bfd_get_filename (abfd));
1632#else
1633 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1634 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1635 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1636 bfd_size_type uncompressed_size;
1637 gdb_byte *uncompressed_buffer;
1638 z_stream strm;
1639 int rc;
1640 int header_size = 12;
1641
1642 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1643 || bfd_bread (compressed_buffer,
1644 compressed_size, abfd) != compressed_size)
dce234bc
PP
1645 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1646 bfd_get_filename (abfd));
1647
1648 /* Read the zlib header. In this case, it should be "ZLIB" followed
1649 by the uncompressed section size, 8 bytes in big-endian order. */
1650 if (compressed_size < header_size
1651 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1652 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1653 bfd_get_filename (abfd));
1654 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1655 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1656 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1657 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1658 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1659 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1660 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1661 uncompressed_size += compressed_buffer[11];
1662
1663 /* It is possible the section consists of several compressed
1664 buffers concatenated together, so we uncompress in a loop. */
1665 strm.zalloc = NULL;
1666 strm.zfree = NULL;
1667 strm.opaque = NULL;
1668 strm.avail_in = compressed_size - header_size;
1669 strm.next_in = (Bytef*) compressed_buffer + header_size;
1670 strm.avail_out = uncompressed_size;
1671 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1672 uncompressed_size);
1673 rc = inflateInit (&strm);
1674 while (strm.avail_in > 0)
1675 {
1676 if (rc != Z_OK)
1677 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1678 bfd_get_filename (abfd), rc);
1679 strm.next_out = ((Bytef*) uncompressed_buffer
1680 + (uncompressed_size - strm.avail_out));
1681 rc = inflate (&strm, Z_FINISH);
1682 if (rc != Z_STREAM_END)
1683 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1684 bfd_get_filename (abfd), rc);
1685 rc = inflateReset (&strm);
1686 }
1687 rc = inflateEnd (&strm);
1688 if (rc != Z_OK
1689 || strm.avail_out != 0)
1690 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1691 bfd_get_filename (abfd), rc);
1692
affddf13 1693 do_cleanups (cleanup);
dce234bc
PP
1694 *outbuf = uncompressed_buffer;
1695 *outsize = uncompressed_size;
1696#endif
233a11ab
CS
1697}
1698
fceca515
DE
1699/* A helper function that decides whether a section is empty,
1700 or not present. */
9e0ac564
TT
1701
1702static int
1703dwarf2_section_empty_p (struct dwarf2_section_info *info)
1704{
1705 return info->asection == NULL || info->size == 0;
1706}
1707
3019eac3
DE
1708/* Read the contents of the section INFO.
1709 OBJFILE is the main object file, but not necessarily the file where
1710 the section comes from. E.g., for DWO files INFO->asection->owner
1711 is the bfd of the DWO file.
dce234bc 1712 If the section is compressed, uncompress it before returning. */
c906108c 1713
dce234bc
PP
1714static void
1715dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1716{
dce234bc 1717 asection *sectp = info->asection;
3019eac3 1718 bfd *abfd;
dce234bc
PP
1719 gdb_byte *buf, *retbuf;
1720 unsigned char header[4];
c906108c 1721
be391dca
TT
1722 if (info->readin)
1723 return;
dce234bc 1724 info->buffer = NULL;
b315ab21 1725 info->map_addr = NULL;
be391dca 1726 info->readin = 1;
188dd5d6 1727
9e0ac564 1728 if (dwarf2_section_empty_p (info))
dce234bc 1729 return;
c906108c 1730
3019eac3
DE
1731 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1732 abfd = sectp->owner;
1733
dce234bc
PP
1734 /* Check if the file has a 4-byte header indicating compression. */
1735 if (info->size > sizeof (header)
1736 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1737 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1738 {
1739 /* Upon decompression, update the buffer and its size. */
1740 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1741 {
1742 zlib_decompress_section (objfile, sectp, &info->buffer,
1743 &info->size);
1744 return;
1745 }
1746 }
4bdf3d34 1747
dce234bc
PP
1748#ifdef HAVE_MMAP
1749 if (pagesize == 0)
1750 pagesize = getpagesize ();
2e276125 1751
dce234bc
PP
1752 /* Only try to mmap sections which are large enough: we don't want to
1753 waste space due to fragmentation. Also, only try mmap for sections
1754 without relocations. */
1755
1756 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1757 {
b315ab21
TG
1758 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1759 MAP_PRIVATE, sectp->filepos,
1760 &info->map_addr, &info->map_len);
dce234bc 1761
b315ab21 1762 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1763 {
be391dca 1764#if HAVE_POSIX_MADVISE
b315ab21 1765 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1766#endif
dce234bc
PP
1767 return;
1768 }
1769 }
1770#endif
1771
1772 /* If we get here, we are a normal, not-compressed section. */
1773 info->buffer = buf
1774 = obstack_alloc (&objfile->objfile_obstack, info->size);
1775
1776 /* When debugging .o files, we may need to apply relocations; see
1777 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1778 We never compress sections in .o files, so we only need to
1779 try this when the section is not compressed. */
ac8035ab 1780 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1781 if (retbuf != NULL)
1782 {
1783 info->buffer = retbuf;
1784 return;
1785 }
1786
1787 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1788 || bfd_bread (buf, info->size, abfd) != info->size)
1789 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1790 bfd_get_filename (abfd));
1791}
1792
9e0ac564
TT
1793/* A helper function that returns the size of a section in a safe way.
1794 If you are positive that the section has been read before using the
1795 size, then it is safe to refer to the dwarf2_section_info object's
1796 "size" field directly. In other cases, you must call this
1797 function, because for compressed sections the size field is not set
1798 correctly until the section has been read. */
1799
1800static bfd_size_type
1801dwarf2_section_size (struct objfile *objfile,
1802 struct dwarf2_section_info *info)
1803{
1804 if (!info->readin)
1805 dwarf2_read_section (objfile, info);
1806 return info->size;
1807}
1808
dce234bc 1809/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1810 SECTION_NAME. */
af34e669 1811
dce234bc 1812void
3017a003
TG
1813dwarf2_get_section_info (struct objfile *objfile,
1814 enum dwarf2_section_enum sect,
dce234bc
PP
1815 asection **sectp, gdb_byte **bufp,
1816 bfd_size_type *sizep)
1817{
1818 struct dwarf2_per_objfile *data
1819 = objfile_data (objfile, dwarf2_objfile_data_key);
1820 struct dwarf2_section_info *info;
a3b2a86b
TT
1821
1822 /* We may see an objfile without any DWARF, in which case we just
1823 return nothing. */
1824 if (data == NULL)
1825 {
1826 *sectp = NULL;
1827 *bufp = NULL;
1828 *sizep = 0;
1829 return;
1830 }
3017a003
TG
1831 switch (sect)
1832 {
1833 case DWARF2_DEBUG_FRAME:
1834 info = &data->frame;
1835 break;
1836 case DWARF2_EH_FRAME:
1837 info = &data->eh_frame;
1838 break;
1839 default:
1840 gdb_assert_not_reached ("unexpected section");
1841 }
dce234bc 1842
9e0ac564 1843 dwarf2_read_section (objfile, info);
dce234bc
PP
1844
1845 *sectp = info->asection;
1846 *bufp = info->buffer;
1847 *sizep = info->size;
1848}
1849
9291a0cd 1850\f
7b9f3c50
DE
1851/* DWARF quick_symbols_functions support. */
1852
1853/* TUs can share .debug_line entries, and there can be a lot more TUs than
1854 unique line tables, so we maintain a separate table of all .debug_line
1855 derived entries to support the sharing.
1856 All the quick functions need is the list of file names. We discard the
1857 line_header when we're done and don't need to record it here. */
1858struct quick_file_names
1859{
1860 /* The offset in .debug_line of the line table. We hash on this. */
1861 unsigned int offset;
1862
1863 /* The number of entries in file_names, real_names. */
1864 unsigned int num_file_names;
1865
1866 /* The file names from the line table, after being run through
1867 file_full_name. */
1868 const char **file_names;
1869
1870 /* The file names from the line table after being run through
1871 gdb_realpath. These are computed lazily. */
1872 const char **real_names;
1873};
1874
1875/* When using the index (and thus not using psymtabs), each CU has an
1876 object of this type. This is used to hold information needed by
1877 the various "quick" methods. */
1878struct dwarf2_per_cu_quick_data
1879{
1880 /* The file table. This can be NULL if there was no file table
1881 or it's currently not read in.
1882 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1883 struct quick_file_names *file_names;
1884
1885 /* The corresponding symbol table. This is NULL if symbols for this
1886 CU have not yet been read. */
1887 struct symtab *symtab;
1888
1889 /* A temporary mark bit used when iterating over all CUs in
1890 expand_symtabs_matching. */
1891 unsigned int mark : 1;
1892
1893 /* True if we've tried to read the file table and found there isn't one.
1894 There will be no point in trying to read it again next time. */
1895 unsigned int no_file_data : 1;
1896};
1897
1898/* Hash function for a quick_file_names. */
1899
1900static hashval_t
1901hash_file_name_entry (const void *e)
1902{
1903 const struct quick_file_names *file_data = e;
1904
1905 return file_data->offset;
1906}
1907
1908/* Equality function for a quick_file_names. */
1909
1910static int
1911eq_file_name_entry (const void *a, const void *b)
1912{
1913 const struct quick_file_names *ea = a;
1914 const struct quick_file_names *eb = b;
1915
1916 return ea->offset == eb->offset;
1917}
1918
1919/* Delete function for a quick_file_names. */
1920
1921static void
1922delete_file_name_entry (void *e)
1923{
1924 struct quick_file_names *file_data = e;
1925 int i;
1926
1927 for (i = 0; i < file_data->num_file_names; ++i)
1928 {
1929 xfree ((void*) file_data->file_names[i]);
1930 if (file_data->real_names)
1931 xfree ((void*) file_data->real_names[i]);
1932 }
1933
1934 /* The space for the struct itself lives on objfile_obstack,
1935 so we don't free it here. */
1936}
1937
1938/* Create a quick_file_names hash table. */
1939
1940static htab_t
1941create_quick_file_names_table (unsigned int nr_initial_entries)
1942{
1943 return htab_create_alloc (nr_initial_entries,
1944 hash_file_name_entry, eq_file_name_entry,
1945 delete_file_name_entry, xcalloc, xfree);
1946}
9291a0cd 1947
918dd910
JK
1948/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1949 have to be created afterwards. You should call age_cached_comp_units after
1950 processing PER_CU->CU. dw2_setup must have been already called. */
1951
1952static void
1953load_cu (struct dwarf2_per_cu_data *per_cu)
1954{
3019eac3 1955 if (per_cu->is_debug_types)
e5fe5e75 1956 load_full_type_unit (per_cu);
918dd910 1957 else
95554aad 1958 load_full_comp_unit (per_cu, language_minimal);
918dd910 1959
918dd910 1960 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
1961
1962 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
1963}
1964
a0f42c21 1965/* Read in the symbols for PER_CU. */
2fdf6df6 1966
9291a0cd 1967static void
a0f42c21 1968dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1969{
1970 struct cleanup *back_to;
1971
1972 back_to = make_cleanup (dwarf2_release_queue, NULL);
1973
95554aad
TT
1974 if (dwarf2_per_objfile->using_index
1975 ? per_cu->v.quick->symtab == NULL
1976 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
1977 {
1978 queue_comp_unit (per_cu, language_minimal);
1979 load_cu (per_cu);
1980 }
9291a0cd 1981
a0f42c21 1982 process_queue ();
9291a0cd
TT
1983
1984 /* Age the cache, releasing compilation units that have not
1985 been used recently. */
1986 age_cached_comp_units ();
1987
1988 do_cleanups (back_to);
1989}
1990
1991/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1992 the objfile from which this CU came. Returns the resulting symbol
1993 table. */
2fdf6df6 1994
9291a0cd 1995static struct symtab *
a0f42c21 1996dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 1997{
95554aad 1998 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
1999 if (!per_cu->v.quick->symtab)
2000 {
2001 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2002 increment_reading_symtab ();
a0f42c21 2003 dw2_do_instantiate_symtab (per_cu);
95554aad 2004 process_cu_includes ();
9291a0cd
TT
2005 do_cleanups (back_to);
2006 }
2007 return per_cu->v.quick->symtab;
2008}
2009
1fd400ff 2010/* Return the CU given its index. */
2fdf6df6 2011
1fd400ff
TT
2012static struct dwarf2_per_cu_data *
2013dw2_get_cu (int index)
2014{
2015 if (index >= dwarf2_per_objfile->n_comp_units)
2016 {
2017 index -= dwarf2_per_objfile->n_comp_units;
d467dd73 2018 return dwarf2_per_objfile->all_type_units[index];
1fd400ff
TT
2019 }
2020 return dwarf2_per_objfile->all_comp_units[index];
2021}
2022
9291a0cd
TT
2023/* A helper function that knows how to read a 64-bit value in a way
2024 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2025 otherwise. */
2fdf6df6 2026
9291a0cd
TT
2027static int
2028extract_cu_value (const char *bytes, ULONGEST *result)
2029{
2030 if (sizeof (ULONGEST) < 8)
2031 {
2032 int i;
2033
2034 /* Ignore the upper 4 bytes if they are all zero. */
2035 for (i = 0; i < 4; ++i)
2036 if (bytes[i + 4] != 0)
2037 return 0;
2038
2039 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2040 }
2041 else
2042 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2043 return 1;
2044}
2045
2046/* Read the CU list from the mapped index, and use it to create all
2047 the CU objects for this objfile. Return 0 if something went wrong,
2048 1 if everything went ok. */
2fdf6df6 2049
9291a0cd 2050static int
1fd400ff
TT
2051create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2052 offset_type cu_list_elements)
9291a0cd
TT
2053{
2054 offset_type i;
9291a0cd
TT
2055
2056 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2057 dwarf2_per_objfile->all_comp_units
2058 = obstack_alloc (&objfile->objfile_obstack,
2059 dwarf2_per_objfile->n_comp_units
2060 * sizeof (struct dwarf2_per_cu_data *));
2061
2062 for (i = 0; i < cu_list_elements; i += 2)
2063 {
2064 struct dwarf2_per_cu_data *the_cu;
2065 ULONGEST offset, length;
2066
2067 if (!extract_cu_value (cu_list, &offset)
2068 || !extract_cu_value (cu_list + 8, &length))
2069 return 0;
2070 cu_list += 2 * 8;
2071
2072 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2073 struct dwarf2_per_cu_data);
b64f50a1 2074 the_cu->offset.sect_off = offset;
9291a0cd
TT
2075 the_cu->length = length;
2076 the_cu->objfile = objfile;
3019eac3 2077 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
9291a0cd
TT
2078 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2079 struct dwarf2_per_cu_quick_data);
2080 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2081 }
2082
2083 return 1;
2084}
2085
1fd400ff 2086/* Create the signatured type hash table from the index. */
673bfd45 2087
1fd400ff 2088static int
673bfd45 2089create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2090 struct dwarf2_section_info *section,
673bfd45
DE
2091 const gdb_byte *bytes,
2092 offset_type elements)
1fd400ff
TT
2093{
2094 offset_type i;
673bfd45 2095 htab_t sig_types_hash;
1fd400ff 2096
d467dd73
DE
2097 dwarf2_per_objfile->n_type_units = elements / 3;
2098 dwarf2_per_objfile->all_type_units
1fd400ff 2099 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2100 dwarf2_per_objfile->n_type_units
1fd400ff
TT
2101 * sizeof (struct dwarf2_per_cu_data *));
2102
673bfd45 2103 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2104
2105 for (i = 0; i < elements; i += 3)
2106 {
52dc124a
DE
2107 struct signatured_type *sig_type;
2108 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2109 void **slot;
2110
2111 if (!extract_cu_value (bytes, &offset)
52dc124a 2112 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
2113 return 0;
2114 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2115 bytes += 3 * 8;
2116
52dc124a 2117 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2118 struct signatured_type);
52dc124a 2119 sig_type->signature = signature;
3019eac3
DE
2120 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2121 sig_type->per_cu.is_debug_types = 1;
2122 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2123 sig_type->per_cu.offset.sect_off = offset;
2124 sig_type->per_cu.objfile = objfile;
2125 sig_type->per_cu.v.quick
1fd400ff
TT
2126 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2127 struct dwarf2_per_cu_quick_data);
2128
52dc124a
DE
2129 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2130 *slot = sig_type;
1fd400ff 2131
52dc124a 2132 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
1fd400ff
TT
2133 }
2134
673bfd45 2135 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2136
2137 return 1;
2138}
2139
9291a0cd
TT
2140/* Read the address map data from the mapped index, and use it to
2141 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2142
9291a0cd
TT
2143static void
2144create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2145{
2146 const gdb_byte *iter, *end;
2147 struct obstack temp_obstack;
2148 struct addrmap *mutable_map;
2149 struct cleanup *cleanup;
2150 CORE_ADDR baseaddr;
2151
2152 obstack_init (&temp_obstack);
2153 cleanup = make_cleanup_obstack_free (&temp_obstack);
2154 mutable_map = addrmap_create_mutable (&temp_obstack);
2155
2156 iter = index->address_table;
2157 end = iter + index->address_table_size;
2158
2159 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2160
2161 while (iter < end)
2162 {
2163 ULONGEST hi, lo, cu_index;
2164 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2165 iter += 8;
2166 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2167 iter += 8;
2168 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2169 iter += 4;
2170
2171 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2172 dw2_get_cu (cu_index));
9291a0cd
TT
2173 }
2174
2175 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2176 &objfile->objfile_obstack);
2177 do_cleanups (cleanup);
2178}
2179
59d7bcaf
JK
2180/* The hash function for strings in the mapped index. This is the same as
2181 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2182 implementation. This is necessary because the hash function is tied to the
2183 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2184 SYMBOL_HASH_NEXT.
2185
2186 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2187
9291a0cd 2188static hashval_t
559a7a62 2189mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2190{
2191 const unsigned char *str = (const unsigned char *) p;
2192 hashval_t r = 0;
2193 unsigned char c;
2194
2195 while ((c = *str++) != 0)
559a7a62
JK
2196 {
2197 if (index_version >= 5)
2198 c = tolower (c);
2199 r = r * 67 + c - 113;
2200 }
9291a0cd
TT
2201
2202 return r;
2203}
2204
2205/* Find a slot in the mapped index INDEX for the object named NAME.
2206 If NAME is found, set *VEC_OUT to point to the CU vector in the
2207 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2208
9291a0cd
TT
2209static int
2210find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2211 offset_type **vec_out)
2212{
0cf03b49
JK
2213 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2214 offset_type hash;
9291a0cd 2215 offset_type slot, step;
559a7a62 2216 int (*cmp) (const char *, const char *);
9291a0cd 2217
0cf03b49
JK
2218 if (current_language->la_language == language_cplus
2219 || current_language->la_language == language_java
2220 || current_language->la_language == language_fortran)
2221 {
2222 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2223 not contain any. */
2224 const char *paren = strchr (name, '(');
2225
2226 if (paren)
2227 {
2228 char *dup;
2229
2230 dup = xmalloc (paren - name + 1);
2231 memcpy (dup, name, paren - name);
2232 dup[paren - name] = 0;
2233
2234 make_cleanup (xfree, dup);
2235 name = dup;
2236 }
2237 }
2238
559a7a62 2239 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2240 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2241 simulate our NAME being searched is also lowercased. */
2242 hash = mapped_index_string_hash ((index->version == 4
2243 && case_sensitivity == case_sensitive_off
2244 ? 5 : index->version),
2245 name);
2246
3876f04e
DE
2247 slot = hash & (index->symbol_table_slots - 1);
2248 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2249 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2250
2251 for (;;)
2252 {
2253 /* Convert a slot number to an offset into the table. */
2254 offset_type i = 2 * slot;
2255 const char *str;
3876f04e 2256 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2257 {
2258 do_cleanups (back_to);
2259 return 0;
2260 }
9291a0cd 2261
3876f04e 2262 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2263 if (!cmp (name, str))
9291a0cd
TT
2264 {
2265 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2266 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2267 do_cleanups (back_to);
9291a0cd
TT
2268 return 1;
2269 }
2270
3876f04e 2271 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2272 }
2273}
2274
2275/* Read the index file. If everything went ok, initialize the "quick"
2276 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2277
9291a0cd
TT
2278static int
2279dwarf2_read_index (struct objfile *objfile)
2280{
9291a0cd
TT
2281 char *addr;
2282 struct mapped_index *map;
b3b272e1 2283 offset_type *metadata;
ac0b195c
KW
2284 const gdb_byte *cu_list;
2285 const gdb_byte *types_list = NULL;
2286 offset_type version, cu_list_elements;
2287 offset_type types_list_elements = 0;
1fd400ff 2288 int i;
9291a0cd 2289
9e0ac564 2290 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2291 return 0;
82430852
JK
2292
2293 /* Older elfutils strip versions could keep the section in the main
2294 executable while splitting it for the separate debug info file. */
2295 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2296 & SEC_HAS_CONTENTS) == 0)
2297 return 0;
2298
9291a0cd
TT
2299 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2300
2301 addr = dwarf2_per_objfile->gdb_index.buffer;
2302 /* Version check. */
1fd400ff 2303 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2304 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2305 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2306 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2307 indices. */
831adc1f 2308 if (version < 4)
481860b3
GB
2309 {
2310 static int warning_printed = 0;
2311 if (!warning_printed)
2312 {
2313 warning (_("Skipping obsolete .gdb_index section in %s."),
2314 objfile->name);
2315 warning_printed = 1;
2316 }
2317 return 0;
2318 }
2319 /* Index version 4 uses a different hash function than index version
2320 5 and later.
2321
2322 Versions earlier than 6 did not emit psymbols for inlined
2323 functions. Using these files will cause GDB not to be able to
2324 set breakpoints on inlined functions by name, so we ignore these
2325 indices unless the --use-deprecated-index-sections command line
2326 option was supplied. */
2327 if (version < 6 && !use_deprecated_index_sections)
2328 {
2329 static int warning_printed = 0;
2330 if (!warning_printed)
2331 {
2332 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2333 "--use-deprecated-index-sections to use them anyway"),
2334 objfile->name);
2335 warning_printed = 1;
2336 }
2337 return 0;
2338 }
2339 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2340 longer backward compatible. */
481860b3 2341 if (version > 6)
594e8718 2342 return 0;
9291a0cd
TT
2343
2344 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2345 map->version = version;
b3b272e1 2346 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2347
2348 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2349
2350 i = 0;
2351 cu_list = addr + MAYBE_SWAP (metadata[i]);
2352 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2353 / 8);
1fd400ff
TT
2354 ++i;
2355
987d643c
TT
2356 types_list = addr + MAYBE_SWAP (metadata[i]);
2357 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2358 - MAYBE_SWAP (metadata[i]))
2359 / 8);
2360 ++i;
1fd400ff
TT
2361
2362 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2363 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2364 - MAYBE_SWAP (metadata[i]));
2365 ++i;
2366
3876f04e
DE
2367 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2368 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2369 - MAYBE_SWAP (metadata[i]))
2370 / (2 * sizeof (offset_type)));
1fd400ff 2371 ++i;
9291a0cd 2372
1fd400ff
TT
2373 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2374
0fefef59
DE
2375 /* Don't use the index if it's empty. */
2376 if (map->symbol_table_slots == 0)
2377 return 0;
2378
1fd400ff
TT
2379 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2380 return 0;
2381
8b70b953
TT
2382 if (types_list_elements)
2383 {
2384 struct dwarf2_section_info *section;
2385
2386 /* We can only handle a single .debug_types when we have an
2387 index. */
2388 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2389 return 0;
2390
2391 section = VEC_index (dwarf2_section_info_def,
2392 dwarf2_per_objfile->types, 0);
2393
2394 if (!create_signatured_type_table_from_index (objfile, section,
2395 types_list,
2396 types_list_elements))
2397 return 0;
2398 }
9291a0cd
TT
2399
2400 create_addrmap_from_index (objfile, map);
2401
2402 dwarf2_per_objfile->index_table = map;
2403 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2404 dwarf2_per_objfile->quick_file_names_table =
2405 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2406
2407 return 1;
2408}
2409
2410/* A helper for the "quick" functions which sets the global
2411 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2412
9291a0cd
TT
2413static void
2414dw2_setup (struct objfile *objfile)
2415{
2416 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2417 gdb_assert (dwarf2_per_objfile);
2418}
2419
dee91e82 2420/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2421
dee91e82
DE
2422static void
2423dw2_get_file_names_reader (const struct die_reader_specs *reader,
2424 gdb_byte *info_ptr,
2425 struct die_info *comp_unit_die,
2426 int has_children,
2427 void *data)
9291a0cd 2428{
dee91e82
DE
2429 struct dwarf2_cu *cu = reader->cu;
2430 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2431 struct objfile *objfile = dwarf2_per_objfile->objfile;
7b9f3c50 2432 struct line_header *lh;
9291a0cd 2433 struct attribute *attr;
dee91e82 2434 int i;
9ff913ba 2435 unsigned int bytes_read;
9291a0cd 2436 char *name, *comp_dir;
7b9f3c50
DE
2437 void **slot;
2438 struct quick_file_names *qfn;
2439 unsigned int line_offset;
9291a0cd 2440
7b9f3c50
DE
2441 lh = NULL;
2442 slot = NULL;
2443 line_offset = 0;
dee91e82
DE
2444
2445 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2446 if (attr)
2447 {
7b9f3c50
DE
2448 struct quick_file_names find_entry;
2449
2450 line_offset = DW_UNSND (attr);
2451
2452 /* We may have already read in this line header (TU line header sharing).
2453 If we have we're done. */
2454 find_entry.offset = line_offset;
2455 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2456 &find_entry, INSERT);
2457 if (*slot != NULL)
2458 {
7b9f3c50 2459 this_cu->v.quick->file_names = *slot;
dee91e82 2460 return;
7b9f3c50
DE
2461 }
2462
3019eac3 2463 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2464 }
2465 if (lh == NULL)
2466 {
7b9f3c50 2467 this_cu->v.quick->no_file_data = 1;
dee91e82 2468 return;
9291a0cd
TT
2469 }
2470
7b9f3c50
DE
2471 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2472 qfn->offset = line_offset;
2473 gdb_assert (slot != NULL);
2474 *slot = qfn;
9291a0cd 2475
dee91e82 2476 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2477
7b9f3c50
DE
2478 qfn->num_file_names = lh->num_file_names;
2479 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2480 lh->num_file_names * sizeof (char *));
9291a0cd 2481 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2482 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2483 qfn->real_names = NULL;
9291a0cd 2484
7b9f3c50 2485 free_line_header (lh);
7b9f3c50
DE
2486
2487 this_cu->v.quick->file_names = qfn;
dee91e82
DE
2488}
2489
2490/* A helper for the "quick" functions which attempts to read the line
2491 table for THIS_CU. */
2492
2493static struct quick_file_names *
2494dw2_get_file_names (struct objfile *objfile,
2495 struct dwarf2_per_cu_data *this_cu)
2496{
2497 if (this_cu->v.quick->file_names != NULL)
2498 return this_cu->v.quick->file_names;
2499 /* If we know there is no line data, no point in looking again. */
2500 if (this_cu->v.quick->no_file_data)
2501 return NULL;
2502
3019eac3
DE
2503 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2504 in the stub for CUs, there's is no need to lookup the DWO file.
2505 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2506 DWO file. */
2507 if (this_cu->is_debug_types)
fd820528 2508 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
3019eac3
DE
2509 else
2510 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2511
2512 if (this_cu->v.quick->no_file_data)
2513 return NULL;
2514 return this_cu->v.quick->file_names;
9291a0cd
TT
2515}
2516
2517/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2518 real path for a given file name from the line table. */
2fdf6df6 2519
9291a0cd 2520static const char *
7b9f3c50
DE
2521dw2_get_real_path (struct objfile *objfile,
2522 struct quick_file_names *qfn, int index)
9291a0cd 2523{
7b9f3c50
DE
2524 if (qfn->real_names == NULL)
2525 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2526 qfn->num_file_names, sizeof (char *));
9291a0cd 2527
7b9f3c50
DE
2528 if (qfn->real_names[index] == NULL)
2529 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2530
7b9f3c50 2531 return qfn->real_names[index];
9291a0cd
TT
2532}
2533
2534static struct symtab *
2535dw2_find_last_source_symtab (struct objfile *objfile)
2536{
2537 int index;
ae2de4f8 2538
9291a0cd
TT
2539 dw2_setup (objfile);
2540 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2541 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2542}
2543
7b9f3c50
DE
2544/* Traversal function for dw2_forget_cached_source_info. */
2545
2546static int
2547dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2548{
7b9f3c50 2549 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2550
7b9f3c50 2551 if (file_data->real_names)
9291a0cd 2552 {
7b9f3c50 2553 int i;
9291a0cd 2554
7b9f3c50 2555 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2556 {
7b9f3c50
DE
2557 xfree ((void*) file_data->real_names[i]);
2558 file_data->real_names[i] = NULL;
9291a0cd
TT
2559 }
2560 }
7b9f3c50
DE
2561
2562 return 1;
2563}
2564
2565static void
2566dw2_forget_cached_source_info (struct objfile *objfile)
2567{
2568 dw2_setup (objfile);
2569
2570 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2571 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2572}
2573
f8eba3c6
TT
2574/* Helper function for dw2_map_symtabs_matching_filename that expands
2575 the symtabs and calls the iterator. */
2576
2577static int
2578dw2_map_expand_apply (struct objfile *objfile,
2579 struct dwarf2_per_cu_data *per_cu,
2580 const char *name,
2581 const char *full_path, const char *real_path,
2582 int (*callback) (struct symtab *, void *),
2583 void *data)
2584{
2585 struct symtab *last_made = objfile->symtabs;
2586
2587 /* Don't visit already-expanded CUs. */
2588 if (per_cu->v.quick->symtab)
2589 return 0;
2590
2591 /* This may expand more than one symtab, and we want to iterate over
2592 all of them. */
a0f42c21 2593 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2594
2595 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2596 objfile->symtabs, last_made);
2597}
2598
2599/* Implementation of the map_symtabs_matching_filename method. */
2600
9291a0cd 2601static int
f8eba3c6
TT
2602dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2603 const char *full_path, const char *real_path,
2604 int (*callback) (struct symtab *, void *),
2605 void *data)
9291a0cd
TT
2606{
2607 int i;
c011a4f4 2608 const char *name_basename = lbasename (name);
4aac40c8
TT
2609 int name_len = strlen (name);
2610 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
2611
2612 dw2_setup (objfile);
ae2de4f8 2613
1fd400ff 2614 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2615 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2616 {
2617 int j;
e254ef6a 2618 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2619 struct quick_file_names *file_data;
9291a0cd 2620
3d7bb9d9 2621 /* We only need to look at symtabs not already expanded. */
e254ef6a 2622 if (per_cu->v.quick->symtab)
9291a0cd
TT
2623 continue;
2624
7b9f3c50
DE
2625 file_data = dw2_get_file_names (objfile, per_cu);
2626 if (file_data == NULL)
9291a0cd
TT
2627 continue;
2628
7b9f3c50 2629 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2630 {
7b9f3c50 2631 const char *this_name = file_data->file_names[j];
9291a0cd 2632
4aac40c8
TT
2633 if (FILENAME_CMP (name, this_name) == 0
2634 || (!is_abs && compare_filenames_for_search (this_name,
2635 name, name_len)))
9291a0cd 2636 {
f8eba3c6
TT
2637 if (dw2_map_expand_apply (objfile, per_cu,
2638 name, full_path, real_path,
2639 callback, data))
2640 return 1;
4aac40c8 2641 }
9291a0cd 2642
c011a4f4
DE
2643 /* Before we invoke realpath, which can get expensive when many
2644 files are involved, do a quick comparison of the basenames. */
2645 if (! basenames_may_differ
2646 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2647 continue;
2648
9291a0cd
TT
2649 if (full_path != NULL)
2650 {
7b9f3c50
DE
2651 const char *this_real_name = dw2_get_real_path (objfile,
2652 file_data, j);
9291a0cd 2653
7b9f3c50 2654 if (this_real_name != NULL
4aac40c8
TT
2655 && (FILENAME_CMP (full_path, this_real_name) == 0
2656 || (!is_abs
2657 && compare_filenames_for_search (this_real_name,
2658 name, name_len))))
9291a0cd 2659 {
f8eba3c6
TT
2660 if (dw2_map_expand_apply (objfile, per_cu,
2661 name, full_path, real_path,
2662 callback, data))
2663 return 1;
9291a0cd
TT
2664 }
2665 }
2666
2667 if (real_path != NULL)
2668 {
7b9f3c50
DE
2669 const char *this_real_name = dw2_get_real_path (objfile,
2670 file_data, j);
9291a0cd 2671
7b9f3c50 2672 if (this_real_name != NULL
4aac40c8
TT
2673 && (FILENAME_CMP (real_path, this_real_name) == 0
2674 || (!is_abs
2675 && compare_filenames_for_search (this_real_name,
2676 name, name_len))))
9291a0cd 2677 {
f8eba3c6
TT
2678 if (dw2_map_expand_apply (objfile, per_cu,
2679 name, full_path, real_path,
2680 callback, data))
2681 return 1;
9291a0cd
TT
2682 }
2683 }
2684 }
2685 }
2686
9291a0cd
TT
2687 return 0;
2688}
2689
2690static struct symtab *
2691dw2_lookup_symbol (struct objfile *objfile, int block_index,
2692 const char *name, domain_enum domain)
2693{
774b6a14 2694 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2695 instead. */
2696 return NULL;
2697}
2698
2699/* A helper function that expands all symtabs that hold an object
2700 named NAME. */
2fdf6df6 2701
9291a0cd
TT
2702static void
2703dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2704{
2705 dw2_setup (objfile);
2706
ae2de4f8 2707 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2708 if (dwarf2_per_objfile->index_table)
2709 {
2710 offset_type *vec;
2711
2712 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2713 name, &vec))
2714 {
2715 offset_type i, len = MAYBE_SWAP (*vec);
2716 for (i = 0; i < len; ++i)
2717 {
2718 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2719 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2720
a0f42c21 2721 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2722 }
2723 }
2724 }
2725}
2726
774b6a14
TT
2727static void
2728dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2729 enum block_enum block_kind, const char *name,
774b6a14 2730 domain_enum domain)
9291a0cd 2731{
774b6a14 2732 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2733}
2734
2735static void
2736dw2_print_stats (struct objfile *objfile)
2737{
2738 int i, count;
2739
2740 dw2_setup (objfile);
2741 count = 0;
1fd400ff 2742 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2743 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2744 {
e254ef6a 2745 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2746
e254ef6a 2747 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2748 ++count;
2749 }
2750 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2751}
2752
2753static void
2754dw2_dump (struct objfile *objfile)
2755{
2756 /* Nothing worth printing. */
2757}
2758
2759static void
2760dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2761 struct section_offsets *delta)
2762{
2763 /* There's nothing to relocate here. */
2764}
2765
2766static void
2767dw2_expand_symtabs_for_function (struct objfile *objfile,
2768 const char *func_name)
2769{
2770 dw2_do_expand_symtabs_matching (objfile, func_name);
2771}
2772
2773static void
2774dw2_expand_all_symtabs (struct objfile *objfile)
2775{
2776 int i;
2777
2778 dw2_setup (objfile);
1fd400ff
TT
2779
2780 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2781 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2782 {
e254ef6a 2783 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2784
a0f42c21 2785 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2786 }
2787}
2788
2789static void
2790dw2_expand_symtabs_with_filename (struct objfile *objfile,
2791 const char *filename)
2792{
2793 int i;
2794
2795 dw2_setup (objfile);
d4637a04
DE
2796
2797 /* We don't need to consider type units here.
2798 This is only called for examining code, e.g. expand_line_sal.
2799 There can be an order of magnitude (or more) more type units
2800 than comp units, and we avoid them if we can. */
2801
2802 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2803 {
2804 int j;
e254ef6a 2805 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2806 struct quick_file_names *file_data;
9291a0cd 2807
3d7bb9d9 2808 /* We only need to look at symtabs not already expanded. */
e254ef6a 2809 if (per_cu->v.quick->symtab)
9291a0cd
TT
2810 continue;
2811
7b9f3c50
DE
2812 file_data = dw2_get_file_names (objfile, per_cu);
2813 if (file_data == NULL)
9291a0cd
TT
2814 continue;
2815
7b9f3c50 2816 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2817 {
7b9f3c50 2818 const char *this_name = file_data->file_names[j];
1ef75ecc 2819 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2820 {
a0f42c21 2821 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2822 break;
2823 }
2824 }
2825 }
2826}
2827
dd786858 2828static const char *
9291a0cd
TT
2829dw2_find_symbol_file (struct objfile *objfile, const char *name)
2830{
e254ef6a 2831 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2832 offset_type *vec;
7b9f3c50 2833 struct quick_file_names *file_data;
9291a0cd
TT
2834
2835 dw2_setup (objfile);
2836
ae2de4f8 2837 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2838 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2839 {
2840 struct symtab *s;
2841
2842 ALL_OBJFILE_SYMTABS (objfile, s)
2843 if (s->primary)
2844 {
2845 struct blockvector *bv = BLOCKVECTOR (s);
2846 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2847 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2848
2849 if (sym)
2850 return sym->symtab->filename;
2851 }
2852 return NULL;
2853 }
9291a0cd
TT
2854
2855 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2856 name, &vec))
2857 return NULL;
2858
2859 /* Note that this just looks at the very first one named NAME -- but
2860 actually we are looking for a function. find_main_filename
2861 should be rewritten so that it doesn't require a custom hook. It
2862 could just use the ordinary symbol tables. */
2863 /* vec[0] is the length, which must always be >0. */
e254ef6a 2864 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2865
7b9f3c50 2866 file_data = dw2_get_file_names (objfile, per_cu);
943cb756
DE
2867 if (file_data == NULL
2868 || file_data->num_file_names == 0)
9291a0cd
TT
2869 return NULL;
2870
7b9f3c50 2871 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2872}
2873
2874static void
40658b94
PH
2875dw2_map_matching_symbols (const char * name, domain_enum namespace,
2876 struct objfile *objfile, int global,
2877 int (*callback) (struct block *,
2878 struct symbol *, void *),
2edb89d3
JK
2879 void *data, symbol_compare_ftype *match,
2880 symbol_compare_ftype *ordered_compare)
9291a0cd 2881{
40658b94 2882 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2883 current language is Ada for a non-Ada objfile using GNU index. As Ada
2884 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2885}
2886
2887static void
f8eba3c6
TT
2888dw2_expand_symtabs_matching
2889 (struct objfile *objfile,
2890 int (*file_matcher) (const char *, void *),
e078317b 2891 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
2892 enum search_domain kind,
2893 void *data)
9291a0cd
TT
2894{
2895 int i;
2896 offset_type iter;
4b5246aa 2897 struct mapped_index *index;
9291a0cd
TT
2898
2899 dw2_setup (objfile);
ae2de4f8
DE
2900
2901 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2902 if (!dwarf2_per_objfile->index_table)
2903 return;
4b5246aa 2904 index = dwarf2_per_objfile->index_table;
9291a0cd 2905
7b08b9eb 2906 if (file_matcher != NULL)
24c79950
TT
2907 {
2908 struct cleanup *cleanup;
2909 htab_t visited_found, visited_not_found;
2910
2911 visited_found = htab_create_alloc (10,
2912 htab_hash_pointer, htab_eq_pointer,
2913 NULL, xcalloc, xfree);
2914 cleanup = make_cleanup_htab_delete (visited_found);
2915 visited_not_found = htab_create_alloc (10,
2916 htab_hash_pointer, htab_eq_pointer,
2917 NULL, xcalloc, xfree);
2918 make_cleanup_htab_delete (visited_not_found);
2919
2920 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2921 + dwarf2_per_objfile->n_type_units); ++i)
2922 {
2923 int j;
2924 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2925 struct quick_file_names *file_data;
2926 void **slot;
7b08b9eb 2927
24c79950 2928 per_cu->v.quick->mark = 0;
3d7bb9d9 2929
24c79950
TT
2930 /* We only need to look at symtabs not already expanded. */
2931 if (per_cu->v.quick->symtab)
2932 continue;
7b08b9eb 2933
24c79950
TT
2934 file_data = dw2_get_file_names (objfile, per_cu);
2935 if (file_data == NULL)
2936 continue;
7b08b9eb 2937
24c79950
TT
2938 if (htab_find (visited_not_found, file_data) != NULL)
2939 continue;
2940 else if (htab_find (visited_found, file_data) != NULL)
2941 {
2942 per_cu->v.quick->mark = 1;
2943 continue;
2944 }
2945
2946 for (j = 0; j < file_data->num_file_names; ++j)
2947 {
2948 if (file_matcher (file_data->file_names[j], data))
2949 {
2950 per_cu->v.quick->mark = 1;
2951 break;
2952 }
2953 }
2954
2955 slot = htab_find_slot (per_cu->v.quick->mark
2956 ? visited_found
2957 : visited_not_found,
2958 file_data, INSERT);
2959 *slot = file_data;
2960 }
2961
2962 do_cleanups (cleanup);
2963 }
9291a0cd 2964
3876f04e 2965 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2966 {
2967 offset_type idx = 2 * iter;
2968 const char *name;
2969 offset_type *vec, vec_len, vec_idx;
2970
3876f04e 2971 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2972 continue;
2973
3876f04e 2974 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 2975
e078317b 2976 if (! (*name_matcher) (name, data))
9291a0cd
TT
2977 continue;
2978
2979 /* The name was matched, now expand corresponding CUs that were
2980 marked. */
4b5246aa 2981 vec = (offset_type *) (index->constant_pool
3876f04e 2982 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2983 vec_len = MAYBE_SWAP (vec[0]);
2984 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2985 {
e254ef6a 2986 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2987
e254ef6a 2988 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2989 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 2990 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2991 }
2992 }
2993}
2994
9703b513
TT
2995/* A helper for dw2_find_pc_sect_symtab which finds the most specific
2996 symtab. */
2997
2998static struct symtab *
2999recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3000{
3001 int i;
3002
3003 if (BLOCKVECTOR (symtab) != NULL
3004 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3005 return symtab;
3006
3007 for (i = 0; symtab->includes[i]; ++i)
3008 {
3009 struct symtab *s;
3010
3011 s = recursively_find_pc_sect_symtab (s, pc);
3012 if (s != NULL)
3013 return s;
3014 }
3015
3016 return NULL;
3017}
3018
9291a0cd
TT
3019static struct symtab *
3020dw2_find_pc_sect_symtab (struct objfile *objfile,
3021 struct minimal_symbol *msymbol,
3022 CORE_ADDR pc,
3023 struct obj_section *section,
3024 int warn_if_readin)
3025{
3026 struct dwarf2_per_cu_data *data;
9703b513 3027 struct symtab *result;
9291a0cd
TT
3028
3029 dw2_setup (objfile);
3030
3031 if (!objfile->psymtabs_addrmap)
3032 return NULL;
3033
3034 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3035 if (!data)
3036 return NULL;
3037
3038 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3039 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3040 paddress (get_objfile_arch (objfile), pc));
3041
9703b513
TT
3042 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3043 gdb_assert (result != NULL);
3044 return result;
9291a0cd
TT
3045}
3046
9291a0cd 3047static void
44b13c5a 3048dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3049 void *data, int need_fullname)
9291a0cd
TT
3050{
3051 int i;
24c79950
TT
3052 struct cleanup *cleanup;
3053 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3054 NULL, xcalloc, xfree);
9291a0cd 3055
24c79950 3056 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3057 dw2_setup (objfile);
ae2de4f8 3058
24c79950
TT
3059 /* We can ignore file names coming from already-expanded CUs. */
3060 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3061 + dwarf2_per_objfile->n_type_units); ++i)
3062 {
3063 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3064
3065 if (per_cu->v.quick->symtab)
3066 {
3067 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3068 INSERT);
3069
3070 *slot = per_cu->v.quick->file_names;
3071 }
3072 }
3073
1fd400ff 3074 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3075 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
3076 {
3077 int j;
e254ef6a 3078 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3079 struct quick_file_names *file_data;
24c79950 3080 void **slot;
9291a0cd 3081
3d7bb9d9 3082 /* We only need to look at symtabs not already expanded. */
e254ef6a 3083 if (per_cu->v.quick->symtab)
9291a0cd
TT
3084 continue;
3085
7b9f3c50
DE
3086 file_data = dw2_get_file_names (objfile, per_cu);
3087 if (file_data == NULL)
9291a0cd
TT
3088 continue;
3089
24c79950
TT
3090 slot = htab_find_slot (visited, file_data, INSERT);
3091 if (*slot)
3092 {
3093 /* Already visited. */
3094 continue;
3095 }
3096 *slot = file_data;
3097
7b9f3c50 3098 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3099 {
74e2f255
DE
3100 const char *this_real_name;
3101
3102 if (need_fullname)
3103 this_real_name = dw2_get_real_path (objfile, file_data, j);
3104 else
3105 this_real_name = NULL;
7b9f3c50 3106 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3107 }
3108 }
24c79950
TT
3109
3110 do_cleanups (cleanup);
9291a0cd
TT
3111}
3112
3113static int
3114dw2_has_symbols (struct objfile *objfile)
3115{
3116 return 1;
3117}
3118
3119const struct quick_symbol_functions dwarf2_gdb_index_functions =
3120{
3121 dw2_has_symbols,
3122 dw2_find_last_source_symtab,
3123 dw2_forget_cached_source_info,
f8eba3c6 3124 dw2_map_symtabs_matching_filename,
9291a0cd 3125 dw2_lookup_symbol,
774b6a14 3126 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3127 dw2_print_stats,
3128 dw2_dump,
3129 dw2_relocate,
3130 dw2_expand_symtabs_for_function,
3131 dw2_expand_all_symtabs,
3132 dw2_expand_symtabs_with_filename,
3133 dw2_find_symbol_file,
40658b94 3134 dw2_map_matching_symbols,
9291a0cd
TT
3135 dw2_expand_symtabs_matching,
3136 dw2_find_pc_sect_symtab,
9291a0cd
TT
3137 dw2_map_symbol_filenames
3138};
3139
3140/* Initialize for reading DWARF for this objfile. Return 0 if this
3141 file will use psymtabs, or 1 if using the GNU index. */
3142
3143int
3144dwarf2_initialize_objfile (struct objfile *objfile)
3145{
3146 /* If we're about to read full symbols, don't bother with the
3147 indices. In this case we also don't care if some other debug
3148 format is making psymtabs, because they are all about to be
3149 expanded anyway. */
3150 if ((objfile->flags & OBJF_READNOW))
3151 {
3152 int i;
3153
3154 dwarf2_per_objfile->using_index = 1;
3155 create_all_comp_units (objfile);
0e50663e 3156 create_all_type_units (objfile);
7b9f3c50
DE
3157 dwarf2_per_objfile->quick_file_names_table =
3158 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3159
1fd400ff 3160 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3161 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3162 {
e254ef6a 3163 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3164
e254ef6a
DE
3165 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3166 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3167 }
3168
3169 /* Return 1 so that gdb sees the "quick" functions. However,
3170 these functions will be no-ops because we will have expanded
3171 all symtabs. */
3172 return 1;
3173 }
3174
3175 if (dwarf2_read_index (objfile))
3176 return 1;
3177
9291a0cd
TT
3178 return 0;
3179}
3180
3181\f
3182
dce234bc
PP
3183/* Build a partial symbol table. */
3184
3185void
f29dff0a 3186dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3187{
f29dff0a 3188 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3189 {
3190 init_psymbol_list (objfile, 1024);
3191 }
3192
d146bf1e 3193 dwarf2_build_psymtabs_hard (objfile);
c906108c 3194}
c906108c 3195
45452591
DE
3196/* Return TRUE if OFFSET is within CU_HEADER. */
3197
3198static inline int
b64f50a1 3199offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3200{
b64f50a1
JK
3201 sect_offset bottom = { cu_header->offset.sect_off };
3202 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3203 + cu_header->initial_length_size) };
9a619af0 3204
b64f50a1 3205 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3206}
3207
93311388
DE
3208/* Read in the comp unit header information from the debug_info at info_ptr.
3209 NOTE: This leaves members offset, first_die_offset to be filled in
3210 by the caller. */
107d2387 3211
fe1b8b76 3212static gdb_byte *
107d2387 3213read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3214 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3215{
3216 int signed_addr;
891d2f0b 3217 unsigned int bytes_read;
c764a876
DE
3218
3219 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3220 cu_header->initial_length_size = bytes_read;
3221 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3222 info_ptr += bytes_read;
107d2387
AC
3223 cu_header->version = read_2_bytes (abfd, info_ptr);
3224 info_ptr += 2;
b64f50a1
JK
3225 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3226 &bytes_read);
613e1657 3227 info_ptr += bytes_read;
107d2387
AC
3228 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3229 info_ptr += 1;
3230 signed_addr = bfd_get_sign_extend_vma (abfd);
3231 if (signed_addr < 0)
8e65ff28 3232 internal_error (__FILE__, __LINE__,
e2e0b3e5 3233 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3234 cu_header->signed_addr_p = signed_addr;
c764a876 3235
107d2387
AC
3236 return info_ptr;
3237}
3238
9ff913ba
DE
3239/* Subroutine of read_and_check_comp_unit_head and
3240 read_and_check_type_unit_head to simplify them.
3241 Perform various error checking on the header. */
3242
3243static void
3244error_check_comp_unit_head (struct comp_unit_head *header,
3245 struct dwarf2_section_info *section)
3246{
3247 bfd *abfd = section->asection->owner;
3248 const char *filename = bfd_get_filename (abfd);
3249
3250 if (header->version != 2 && header->version != 3 && header->version != 4)
3251 error (_("Dwarf Error: wrong version in compilation unit header "
3252 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3253 filename);
3254
b64f50a1 3255 if (header->abbrev_offset.sect_off
9ff913ba
DE
3256 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3257 &dwarf2_per_objfile->abbrev))
3258 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3259 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3260 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3261 filename);
3262
3263 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3264 avoid potential 32-bit overflow. */
b64f50a1 3265 if (((unsigned long) header->offset.sect_off
9ff913ba
DE
3266 + header->length + header->initial_length_size)
3267 > section->size)
3268 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3269 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3270 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3271 filename);
3272}
3273
3274/* Read in a CU/TU header and perform some basic error checking.
3275 The contents of the header are stored in HEADER.
3276 The result is a pointer to the start of the first DIE. */
adabb602 3277
fe1b8b76 3278static gdb_byte *
9ff913ba
DE
3279read_and_check_comp_unit_head (struct comp_unit_head *header,
3280 struct dwarf2_section_info *section,
3281 gdb_byte *info_ptr,
3282 int is_debug_types_section)
72bf9492 3283{
fe1b8b76 3284 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3285 bfd *abfd = section->asection->owner;
72bf9492 3286
b64f50a1 3287 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3288
72bf9492
DJ
3289 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3290
460c1c54
CC
3291 /* If we're reading a type unit, skip over the signature and
3292 type_offset fields. */
b0df02fd 3293 if (is_debug_types_section)
460c1c54
CC
3294 info_ptr += 8 /*signature*/ + header->offset_size;
3295
b64f50a1 3296 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3297
9ff913ba 3298 error_check_comp_unit_head (header, section);
72bf9492
DJ
3299
3300 return info_ptr;
3301}
3302
348e048f
DE
3303/* Read in the types comp unit header information from .debug_types entry at
3304 types_ptr. The result is a pointer to one past the end of the header. */
3305
3306static gdb_byte *
9ff913ba
DE
3307read_and_check_type_unit_head (struct comp_unit_head *header,
3308 struct dwarf2_section_info *section,
3309 gdb_byte *info_ptr,
dee91e82
DE
3310 ULONGEST *signature,
3311 cu_offset *type_offset_in_tu)
348e048f 3312{
9ff913ba
DE
3313 gdb_byte *beg_of_comp_unit = info_ptr;
3314 bfd *abfd = section->asection->owner;
348e048f 3315
b64f50a1 3316 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3317
9ff913ba 3318 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3319
9ff913ba
DE
3320 /* If we're reading a type unit, skip over the signature and
3321 type_offset fields. */
3322 if (signature != NULL)
3323 *signature = read_8_bytes (abfd, info_ptr);
3324 info_ptr += 8;
dee91e82
DE
3325 if (type_offset_in_tu != NULL)
3326 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3327 header->offset_size);
9ff913ba
DE
3328 info_ptr += header->offset_size;
3329
b64f50a1 3330 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3331
9ff913ba
DE
3332 error_check_comp_unit_head (header, section);
3333
3334 return info_ptr;
348e048f
DE
3335}
3336
aaa75496
JB
3337/* Allocate a new partial symtab for file named NAME and mark this new
3338 partial symtab as being an include of PST. */
3339
3340static void
3341dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3342 struct objfile *objfile)
3343{
3344 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3345
3346 subpst->section_offsets = pst->section_offsets;
3347 subpst->textlow = 0;
3348 subpst->texthigh = 0;
3349
3350 subpst->dependencies = (struct partial_symtab **)
3351 obstack_alloc (&objfile->objfile_obstack,
3352 sizeof (struct partial_symtab *));
3353 subpst->dependencies[0] = pst;
3354 subpst->number_of_dependencies = 1;
3355
3356 subpst->globals_offset = 0;
3357 subpst->n_global_syms = 0;
3358 subpst->statics_offset = 0;
3359 subpst->n_static_syms = 0;
3360 subpst->symtab = NULL;
3361 subpst->read_symtab = pst->read_symtab;
3362 subpst->readin = 0;
3363
3364 /* No private part is necessary for include psymtabs. This property
3365 can be used to differentiate between such include psymtabs and
10b3939b 3366 the regular ones. */
58a9656e 3367 subpst->read_symtab_private = NULL;
aaa75496
JB
3368}
3369
3370/* Read the Line Number Program data and extract the list of files
3371 included by the source file represented by PST. Build an include
d85a05f0 3372 partial symtab for each of these included files. */
aaa75496
JB
3373
3374static void
3375dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
3376 struct die_info *die,
3377 struct partial_symtab *pst)
aaa75496 3378{
d85a05f0
DJ
3379 struct line_header *lh = NULL;
3380 struct attribute *attr;
aaa75496 3381
d85a05f0
DJ
3382 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3383 if (attr)
3019eac3 3384 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
3385 if (lh == NULL)
3386 return; /* No linetable, so no includes. */
3387
c6da4cef 3388 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3389 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3390
3391 free_line_header (lh);
3392}
3393
348e048f 3394static hashval_t
52dc124a 3395hash_signatured_type (const void *item)
348e048f 3396{
52dc124a 3397 const struct signatured_type *sig_type = item;
9a619af0 3398
348e048f 3399 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 3400 return sig_type->signature;
348e048f
DE
3401}
3402
3403static int
52dc124a 3404eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
3405{
3406 const struct signatured_type *lhs = item_lhs;
3407 const struct signatured_type *rhs = item_rhs;
9a619af0 3408
348e048f
DE
3409 return lhs->signature == rhs->signature;
3410}
3411
1fd400ff
TT
3412/* Allocate a hash table for signatured types. */
3413
3414static htab_t
673bfd45 3415allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3416{
3417 return htab_create_alloc_ex (41,
52dc124a
DE
3418 hash_signatured_type,
3419 eq_signatured_type,
1fd400ff
TT
3420 NULL,
3421 &objfile->objfile_obstack,
3422 hashtab_obstack_allocate,
3423 dummy_obstack_deallocate);
3424}
3425
d467dd73 3426/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3427
3428static int
d467dd73 3429add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3430{
3431 struct signatured_type *sigt = *slot;
3432 struct dwarf2_per_cu_data ***datap = datum;
3433
3434 **datap = &sigt->per_cu;
3435 ++*datap;
3436
3437 return 1;
3438}
3439
3019eac3
DE
3440/* Create the hash table of all entries in the .debug_types section.
3441 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3442 The result is a pointer to the hash table or NULL if there are
3443 no types. */
348e048f 3444
3019eac3
DE
3445static htab_t
3446create_debug_types_hash_table (struct dwo_file *dwo_file,
3447 VEC (dwarf2_section_info_def) *types)
348e048f 3448{
3019eac3 3449 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 3450 htab_t types_htab = NULL;
8b70b953
TT
3451 int ix;
3452 struct dwarf2_section_info *section;
348e048f 3453
3019eac3
DE
3454 if (VEC_empty (dwarf2_section_info_def, types))
3455 return NULL;
348e048f 3456
8b70b953 3457 for (ix = 0;
3019eac3 3458 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
3459 ++ix)
3460 {
3019eac3 3461 bfd *abfd;
8b70b953 3462 gdb_byte *info_ptr, *end_ptr;
348e048f 3463
8b70b953
TT
3464 dwarf2_read_section (objfile, section);
3465 info_ptr = section->buffer;
348e048f 3466
8b70b953
TT
3467 if (info_ptr == NULL)
3468 continue;
348e048f 3469
3019eac3
DE
3470 /* We can't set abfd until now because the section may be empty or
3471 not present, in which case section->asection will be NULL. */
3472 abfd = section->asection->owner;
3473
8b70b953 3474 if (types_htab == NULL)
3019eac3
DE
3475 {
3476 if (dwo_file)
3477 types_htab = allocate_dwo_unit_table (objfile);
3478 else
3479 types_htab = allocate_signatured_type_table (objfile);
3480 }
348e048f 3481
8b70b953 3482 if (dwarf2_die_debug)
dee91e82
DE
3483 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3484 bfd_get_filename (abfd));
3485
3486 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3487 because we don't need to read any dies: the signature is in the
3488 header. */
8b70b953
TT
3489
3490 end_ptr = info_ptr + section->size;
3491 while (info_ptr < end_ptr)
3492 {
b64f50a1 3493 sect_offset offset;
3019eac3 3494 cu_offset type_offset_in_tu;
8b70b953 3495 ULONGEST signature;
52dc124a 3496 struct signatured_type *sig_type;
3019eac3 3497 struct dwo_unit *dwo_tu;
8b70b953
TT
3498 void **slot;
3499 gdb_byte *ptr = info_ptr;
9ff913ba 3500 struct comp_unit_head header;
dee91e82 3501 unsigned int length;
348e048f 3502
b64f50a1 3503 offset.sect_off = ptr - section->buffer;
348e048f 3504
8b70b953 3505 /* We need to read the type's signature in order to build the hash
9ff913ba 3506 table, but we don't need anything else just yet. */
348e048f 3507
9ff913ba 3508 ptr = read_and_check_type_unit_head (&header, section, ptr,
3019eac3 3509 &signature, &type_offset_in_tu);
6caca83c 3510
dee91e82
DE
3511 length = header.initial_length_size + header.length;
3512
6caca83c 3513 /* Skip dummy type units. */
dee91e82
DE
3514 if (ptr >= info_ptr + length
3515 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 3516 {
dee91e82 3517 info_ptr += header.initial_length_size + header.length;
6caca83c
CC
3518 continue;
3519 }
8b70b953 3520
3019eac3
DE
3521 if (dwo_file)
3522 {
3523 sig_type = NULL;
3524 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3525 struct dwo_unit);
3526 dwo_tu->dwo_file = dwo_file;
3527 dwo_tu->signature = signature;
3528 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3529 dwo_tu->info_or_types_section = section;
3530 dwo_tu->offset = offset;
3531 dwo_tu->length = length;
3532 }
3533 else
3534 {
3535 /* N.B.: type_offset is not usable if this type uses a DWO file.
3536 The real type_offset is in the DWO file. */
3537 dwo_tu = NULL;
3538 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3539 struct signatured_type);
3540 sig_type->signature = signature;
3541 sig_type->type_offset_in_tu = type_offset_in_tu;
3542 sig_type->per_cu.objfile = objfile;
3543 sig_type->per_cu.is_debug_types = 1;
3544 sig_type->per_cu.info_or_types_section = section;
3545 sig_type->per_cu.offset = offset;
3546 sig_type->per_cu.length = length;
3547 }
8b70b953 3548
3019eac3
DE
3549 slot = htab_find_slot (types_htab,
3550 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3551 INSERT);
8b70b953
TT
3552 gdb_assert (slot != NULL);
3553 if (*slot != NULL)
3554 {
3019eac3
DE
3555 sect_offset dup_offset;
3556
3557 if (dwo_file)
3558 {
3559 const struct dwo_unit *dup_tu = *slot;
3560
3561 dup_offset = dup_tu->offset;
3562 }
3563 else
3564 {
3565 const struct signatured_type *dup_tu = *slot;
3566
3567 dup_offset = dup_tu->per_cu.offset;
3568 }
b3c8eb43 3569
8b70b953
TT
3570 complaint (&symfile_complaints,
3571 _("debug type entry at offset 0x%x is duplicate to the "
3572 "entry at offset 0x%x, signature 0x%s"),
3019eac3 3573 offset.sect_off, dup_offset.sect_off,
8b70b953 3574 phex (signature, sizeof (signature)));
8b70b953 3575 }
3019eac3 3576 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 3577
8b70b953
TT
3578 if (dwarf2_die_debug)
3579 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
3580 offset.sect_off,
3581 phex (signature, sizeof (signature)));
348e048f 3582
dee91e82 3583 info_ptr += length;
8b70b953 3584 }
348e048f
DE
3585 }
3586
3019eac3
DE
3587 return types_htab;
3588}
3589
3590/* Create the hash table of all entries in the .debug_types section,
3591 and initialize all_type_units.
3592 The result is zero if there is an error (e.g. missing .debug_types section),
3593 otherwise non-zero. */
3594
3595static int
3596create_all_type_units (struct objfile *objfile)
3597{
3598 htab_t types_htab;
3599 struct dwarf2_per_cu_data **iter;
3600
3601 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3602 if (types_htab == NULL)
3603 {
3604 dwarf2_per_objfile->signatured_types = NULL;
3605 return 0;
3606 }
3607
348e048f
DE
3608 dwarf2_per_objfile->signatured_types = types_htab;
3609
d467dd73
DE
3610 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3611 dwarf2_per_objfile->all_type_units
1fd400ff 3612 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3613 dwarf2_per_objfile->n_type_units
1fd400ff 3614 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3615 iter = &dwarf2_per_objfile->all_type_units[0];
3616 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3617 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3618 == dwarf2_per_objfile->n_type_units);
1fd400ff 3619
348e048f
DE
3620 return 1;
3621}
3622
380bca97 3623/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 3624 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
3625
3626static struct signatured_type *
e319fa28 3627lookup_signatured_type (ULONGEST sig)
348e048f
DE
3628{
3629 struct signatured_type find_entry, *entry;
3630
3631 if (dwarf2_per_objfile->signatured_types == NULL)
3632 {
3633 complaint (&symfile_complaints,
55f1336d 3634 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 3635 return NULL;
348e048f
DE
3636 }
3637
3638 find_entry.signature = sig;
3639 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3640 return entry;
3641}
3642
d85a05f0
DJ
3643/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3644
3645static void
3646init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 3647 struct dwarf2_cu *cu,
3019eac3
DE
3648 struct dwarf2_section_info *section,
3649 struct dwo_file *dwo_file)
d85a05f0 3650{
fceca515 3651 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 3652 reader->abfd = section->asection->owner;
d85a05f0 3653 reader->cu = cu;
3019eac3 3654 reader->dwo_file = dwo_file;
dee91e82
DE
3655 reader->die_section = section;
3656 reader->buffer = section->buffer;
d85a05f0
DJ
3657}
3658
3659/* Find the base address of the compilation unit for range lists and
3660 location lists. It will normally be specified by DW_AT_low_pc.
3661 In DWARF-3 draft 4, the base address could be overridden by
3662 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3663 compilation units with discontinuous ranges. */
3664
3665static void
3666dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3667{
3668 struct attribute *attr;
3669
3670 cu->base_known = 0;
3671 cu->base_address = 0;
3672
3673 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3674 if (attr)
3675 {
3676 cu->base_address = DW_ADDR (attr);
3677 cu->base_known = 1;
3678 }
3679 else
3680 {
3681 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3682 if (attr)
3683 {
3684 cu->base_address = DW_ADDR (attr);
3685 cu->base_known = 1;
3686 }
3687 }
3688}
3689
fd820528 3690/* Initialize a CU (or TU) and read its DIEs.
3019eac3 3691 If the CU defers to a DWO file, read the DWO file as well.
dee91e82
DE
3692
3693 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3694 Otherwise, a new CU is allocated with xmalloc.
3695
3696 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3697 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3698
3699 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 3700 linker) then DIE_READER_FUNC will not get called. */
aaa75496 3701
70221824 3702static void
fd820528
DE
3703init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3704 int use_existing_cu, int keep,
3705 die_reader_func_ftype *die_reader_func,
3706 void *data)
c906108c 3707{
dee91e82 3708 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
3709 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3710 bfd *abfd = section->asection->owner;
dee91e82
DE
3711 struct dwarf2_cu *cu;
3712 gdb_byte *begin_info_ptr, *info_ptr;
3713 struct die_reader_specs reader;
d85a05f0 3714 struct die_info *comp_unit_die;
dee91e82 3715 int has_children;
d85a05f0 3716 struct attribute *attr;
dee91e82
DE
3717 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3718 struct signatured_type *sig_type = NULL;
c906108c 3719
dee91e82
DE
3720 if (use_existing_cu)
3721 gdb_assert (keep);
23745b47 3722
dee91e82
DE
3723 cleanups = make_cleanup (null_cleanup, NULL);
3724
3725 /* This is cheap if the section is already read in. */
3726 dwarf2_read_section (objfile, section);
3727
3728 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3729
3730 if (use_existing_cu && this_cu->cu != NULL)
3731 {
3732 cu = this_cu->cu;
3733 info_ptr += cu->header.first_die_offset.cu_off;
3734 }
3735 else
3736 {
3737 /* If !use_existing_cu, this_cu->cu must be NULL. */
3738 gdb_assert (this_cu->cu == NULL);
3739
3740 cu = xmalloc (sizeof (*cu));
3741 init_one_comp_unit (cu, this_cu);
3742
3743 /* If an error occurs while loading, release our storage. */
3744 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3745
3019eac3 3746 if (this_cu->is_debug_types)
dee91e82
DE
3747 {
3748 ULONGEST signature;
3749
3750 info_ptr = read_and_check_type_unit_head (&cu->header,
3751 section, info_ptr,
3752 &signature, NULL);
3753
3754 /* There's no way to get from PER_CU to its containing
3755 struct signatured_type.
3756 But we have the signature so we can use that. */
3757 sig_type = lookup_signatured_type (signature);
3758 /* We've already scanned all the signatured types,
3759 this must succeed. */
3760 gdb_assert (sig_type != NULL);
3761 gdb_assert (&sig_type->per_cu == this_cu);
3762 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3763
3764 /* LENGTH has not been set yet for type units. */
3765 this_cu->length = cu->header.length + cu->header.initial_length_size;
3019eac3
DE
3766
3767 /* Establish the type offset that can be used to lookup the type. */
3768 sig_type->type_offset_in_section.sect_off =
3769 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
3770 }
3771 else
3772 {
3773 info_ptr = read_and_check_comp_unit_head (&cu->header,
3774 section, info_ptr, 0);
3775
3776 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3777 gdb_assert (this_cu->length
3778 == cu->header.length + cu->header.initial_length_size);
3779 }
3780 }
10b3939b 3781
6caca83c 3782 /* Skip dummy compilation units. */
dee91e82 3783 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
3784 || peek_abbrev_code (abfd, info_ptr) == 0)
3785 {
dee91e82 3786 do_cleanups (cleanups);
21b2bd31 3787 return;
6caca83c
CC
3788 }
3789
93311388 3790 /* Read the abbrevs for this compilation unit into a table. */
dee91e82
DE
3791 if (cu->dwarf2_abbrevs == NULL)
3792 {
3793 dwarf2_read_abbrevs (cu, &dwarf2_per_objfile->abbrev);
3794 make_cleanup (dwarf2_free_abbrev_table, cu);
3795 }
af703f96 3796
dee91e82 3797 /* Read the top level CU/TU die. */
3019eac3 3798 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 3799 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 3800
3019eac3
DE
3801 /* If we have a DWO stub, process it and then read in the DWO file.
3802 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3803 a DWO CU, that this test will fail. */
3804 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3805 if (attr)
3806 {
3807 char *dwo_name = DW_STRING (attr);
3808 const char *comp_dir;
3809 struct dwo_unit *dwo_unit;
3810 ULONGEST signature; /* Or dwo_id. */
3811 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3812 int i,num_extra_attrs;
3813
3814 if (has_children)
3815 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3816 " has children (offset 0x%x) [in module %s]"),
3817 this_cu->offset.sect_off, bfd_get_filename (abfd));
3818
3819 /* These attributes aren't processed until later:
3820 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3821 However, the attribute is found in the stub which we won't have later.
3822 In order to not impose this complication on the rest of the code,
3823 we read them here and copy them to the DWO CU/TU die. */
3824 stmt_list = low_pc = high_pc = ranges = NULL;
3825
3826 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3827 DWO file. */
3828 if (! this_cu->is_debug_types)
3829 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3830 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3831 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3832 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3833
3834 /* There should be a DW_AT_addr_base attribute here (if needed).
3835 We need the value before we can process DW_FORM_GNU_addr_index. */
3836 cu->addr_base = 0;
3837 cu->have_addr_base = 0;
3838 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
3839 if (attr)
3840 {
3841 cu->addr_base = DW_UNSND (attr);
3842 cu->have_addr_base = 1;
3843 }
3844
3845 if (this_cu->is_debug_types)
3846 {
3847 gdb_assert (sig_type != NULL);
3848 signature = sig_type->signature;
3849 }
3850 else
3851 {
3852 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3853 if (! attr)
3854 error (_("Dwarf Error: missing dwo_id [in module %s]"),
3855 dwo_name);
3856 signature = DW_UNSND (attr);
3857 }
3858
3859 /* We may need the comp_dir in order to find the DWO file. */
3860 comp_dir = NULL;
3861 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3862 if (attr)
3863 comp_dir = DW_STRING (attr);
3864
3865 if (this_cu->is_debug_types)
3866 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
3867 else
3868 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
3869 signature);
3870
3871 if (dwo_unit == NULL)
3872 {
3873 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
3874 " with ID %s [in module %s]"),
3875 this_cu->offset.sect_off,
3876 phex (signature, sizeof (signature)),
3877 objfile->name);
3878 }
3879
3880 /* Set up for reading the DWO CU/TU. */
3881 cu->dwo_unit = dwo_unit;
3882 section = dwo_unit->info_or_types_section;
3883 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
3884 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
3885
3886 if (this_cu->is_debug_types)
3887 {
3888 ULONGEST signature;
3889
3890 info_ptr = read_and_check_type_unit_head (&cu->header,
3891 section, info_ptr,
3892 &signature, NULL);
3893 gdb_assert (sig_type->signature == signature);
3894 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3895 gdb_assert (dwo_unit->length
3896 == cu->header.length + cu->header.initial_length_size);
3897
3898 /* Establish the type offset that can be used to lookup the type.
3899 For DWO files, we don't know it until now. */
3900 sig_type->type_offset_in_section.sect_off =
3901 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
3902 }
3903 else
3904 {
3905 info_ptr = read_and_check_comp_unit_head (&cu->header,
3906 section, info_ptr, 0);
3907 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3908 gdb_assert (dwo_unit->length
3909 == cu->header.length + cu->header.initial_length_size);
3910 }
3911
3912 /* Discard the original CU's abbrev table, and read the DWO's. */
3913 dwarf2_free_abbrev_table (cu);
3914 dwarf2_read_abbrevs (cu, &dwo_unit->dwo_file->sections.abbrev);
3915
3916 /* Read in the die, but leave space to copy over the attributes
3917 from the stub. This has the benefit of simplifying the rest of
3918 the code - all the real work is done here. */
3919 num_extra_attrs = ((stmt_list != NULL)
3920 + (low_pc != NULL)
3921 + (high_pc != NULL)
3922 + (ranges != NULL));
3923 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
3924 &has_children, num_extra_attrs);
3925
3926 /* Copy over the attributes from the stub to the DWO die. */
3927 i = comp_unit_die->num_attrs;
3928 if (stmt_list != NULL)
3929 comp_unit_die->attrs[i++] = *stmt_list;
3930 if (low_pc != NULL)
3931 comp_unit_die->attrs[i++] = *low_pc;
3932 if (high_pc != NULL)
3933 comp_unit_die->attrs[i++] = *high_pc;
3934 if (ranges != NULL)
3935 comp_unit_die->attrs[i++] = *ranges;
3936 comp_unit_die->num_attrs += num_extra_attrs;
3937
3938 /* Skip dummy compilation units. */
3939 if (info_ptr >= begin_info_ptr + dwo_unit->length
3940 || peek_abbrev_code (abfd, info_ptr) == 0)
3941 {
3942 do_cleanups (cleanups);
3943 return;
3944 }
3945 }
3946
dee91e82
DE
3947 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3948
3949 if (free_cu_cleanup != NULL)
348e048f 3950 {
dee91e82
DE
3951 if (keep)
3952 {
3953 /* We've successfully allocated this compilation unit. Let our
3954 caller clean it up when finished with it. */
3955 discard_cleanups (free_cu_cleanup);
3956
3957 /* We can only discard free_cu_cleanup and all subsequent cleanups.
3958 So we have to manually free the abbrev table. */
3959 dwarf2_free_abbrev_table (cu);
3960
3961 /* Link this CU into read_in_chain. */
3962 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3963 dwarf2_per_objfile->read_in_chain = this_cu;
3964 }
3965 else
3966 do_cleanups (free_cu_cleanup);
348e048f 3967 }
dee91e82
DE
3968
3969 do_cleanups (cleanups);
3970}
3971
3019eac3
DE
3972/* Read CU/TU THIS_CU in section SECTION,
3973 but do not follow DW_AT_GNU_dwo_name if present.
3974 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
3975 have already done the lookup to find the DWO file).
dee91e82
DE
3976
3977 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 3978 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
3979
3980 We fill in THIS_CU->length.
3981
3982 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3983 linker) then DIE_READER_FUNC will not get called.
3984
3985 THIS_CU->cu is always freed when done.
3019eac3
DE
3986 This is done in order to not leave THIS_CU->cu in a state where we have
3987 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
3988
3989static void
3990init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3991 struct dwarf2_section_info *abbrev_section,
3019eac3 3992 struct dwo_file *dwo_file,
dee91e82
DE
3993 die_reader_func_ftype *die_reader_func,
3994 void *data)
3995{
3996 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
3997 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3998 bfd *abfd = section->asection->owner;
dee91e82
DE
3999 struct dwarf2_cu cu;
4000 gdb_byte *begin_info_ptr, *info_ptr;
4001 struct die_reader_specs reader;
4002 struct cleanup *cleanups;
4003 struct die_info *comp_unit_die;
4004 int has_children;
4005
4006 gdb_assert (this_cu->cu == NULL);
4007
dee91e82
DE
4008 /* This is cheap if the section is already read in. */
4009 dwarf2_read_section (objfile, section);
4010
4011 init_one_comp_unit (&cu, this_cu);
4012
4013 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4014
4015 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4016 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3019eac3 4017 this_cu->is_debug_types);
dee91e82
DE
4018
4019 this_cu->length = cu.header.length + cu.header.initial_length_size;
4020
4021 /* Skip dummy compilation units. */
4022 if (info_ptr >= begin_info_ptr + this_cu->length
4023 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4024 {
dee91e82 4025 do_cleanups (cleanups);
21b2bd31 4026 return;
93311388 4027 }
72bf9492 4028
dee91e82
DE
4029 dwarf2_read_abbrevs (&cu, abbrev_section);
4030 make_cleanup (dwarf2_free_abbrev_table, &cu);
4031
3019eac3 4032 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4033 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4034
4035 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4036
4037 do_cleanups (cleanups);
4038}
4039
3019eac3
DE
4040/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4041 does not lookup the specified DWO file.
4042 This cannot be used to read DWO files.
dee91e82
DE
4043
4044 THIS_CU->cu is always freed when done.
3019eac3
DE
4045 This is done in order to not leave THIS_CU->cu in a state where we have
4046 to care whether it refers to the "main" CU or the DWO CU.
4047 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4048
4049static void
4050init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4051 die_reader_func_ftype *die_reader_func,
4052 void *data)
4053{
4054 init_cutu_and_read_dies_no_follow (this_cu,
4055 &dwarf2_per_objfile->abbrev,
3019eac3 4056 NULL,
dee91e82
DE
4057 die_reader_func, data);
4058}
4059
4060/* die_reader_func for process_psymtab_comp_unit. */
4061
4062static void
4063process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4064 gdb_byte *info_ptr,
4065 struct die_info *comp_unit_die,
4066 int has_children,
4067 void *data)
4068{
4069 struct dwarf2_cu *cu = reader->cu;
4070 struct objfile *objfile = cu->objfile;
4071 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
4072 bfd *abfd = objfile->obfd;
4073 struct attribute *attr;
4074 CORE_ADDR baseaddr;
4075 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4076 struct partial_symtab *pst;
4077 int has_pc_info;
4078 const char *filename;
95554aad 4079 int *want_partial_unit_ptr = data;
dee91e82 4080
95554aad
TT
4081 if (comp_unit_die->tag == DW_TAG_partial_unit
4082 && (want_partial_unit_ptr == NULL
4083 || !*want_partial_unit_ptr))
dee91e82
DE
4084 return;
4085
95554aad 4086 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4087
4088 cu->list_in_scope = &file_symbols;
c906108c 4089
93311388 4090 /* Allocate a new partial symbol table structure. */
dee91e82 4091 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4092 if (attr == NULL || !DW_STRING (attr))
4093 filename = "";
4094 else
4095 filename = DW_STRING (attr);
93311388 4096 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 4097 filename,
93311388
DE
4098 /* TEXTLOW and TEXTHIGH are set below. */
4099 0,
4100 objfile->global_psymbols.next,
4101 objfile->static_psymbols.next);
9750bca9 4102 pst->psymtabs_addrmap_supported = 1;
72bf9492 4103
dee91e82 4104 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4105 if (attr != NULL)
4106 pst->dirname = DW_STRING (attr);
72bf9492 4107
dee91e82 4108 pst->read_symtab_private = per_cu;
72bf9492 4109
93311388 4110 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4111
0963b4bd 4112 /* Store the function that reads in the rest of the symbol table. */
93311388 4113 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 4114
dee91e82 4115 per_cu->v.psymtab = pst;
c906108c 4116
dee91e82 4117 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4118
93311388
DE
4119 /* Possibly set the default values of LOWPC and HIGHPC from
4120 `DW_AT_ranges'. */
d85a05f0 4121 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4122 &best_highpc, cu, pst);
d85a05f0 4123 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4124 /* Store the contiguous range if it is not empty; it can be empty for
4125 CUs with no code. */
4126 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4127 best_lowpc + baseaddr,
4128 best_highpc + baseaddr - 1, pst);
93311388
DE
4129
4130 /* Check if comp unit has_children.
4131 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4132 If not, there's no more debug_info for this comp unit. */
d85a05f0 4133 if (has_children)
93311388
DE
4134 {
4135 struct partial_die_info *first_die;
4136 CORE_ADDR lowpc, highpc;
31ffec48 4137
93311388
DE
4138 lowpc = ((CORE_ADDR) -1);
4139 highpc = ((CORE_ADDR) 0);
c906108c 4140
dee91e82 4141 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4142
93311388 4143 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4144 ! has_pc_info, cu);
57c22c6c 4145
93311388
DE
4146 /* If we didn't find a lowpc, set it to highpc to avoid
4147 complaints from `maint check'. */
4148 if (lowpc == ((CORE_ADDR) -1))
4149 lowpc = highpc;
10b3939b 4150
93311388
DE
4151 /* If the compilation unit didn't have an explicit address range,
4152 then use the information extracted from its child dies. */
d85a05f0 4153 if (! has_pc_info)
93311388 4154 {
d85a05f0
DJ
4155 best_lowpc = lowpc;
4156 best_highpc = highpc;
93311388
DE
4157 }
4158 }
d85a05f0
DJ
4159 pst->textlow = best_lowpc + baseaddr;
4160 pst->texthigh = best_highpc + baseaddr;
c906108c 4161
93311388
DE
4162 pst->n_global_syms = objfile->global_psymbols.next -
4163 (objfile->global_psymbols.list + pst->globals_offset);
4164 pst->n_static_syms = objfile->static_psymbols.next -
4165 (objfile->static_psymbols.list + pst->statics_offset);
4166 sort_pst_symbols (pst);
c906108c 4167
95554aad
TT
4168 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4169 {
4170 int i;
4171 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4172 struct dwarf2_per_cu_data *iter;
4173
4174 /* Fill in 'dependencies' here; we fill in 'users' in a
4175 post-pass. */
4176 pst->number_of_dependencies = len;
4177 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4178 len * sizeof (struct symtab *));
4179 for (i = 0;
4180 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4181 i, iter);
4182 ++i)
4183 pst->dependencies[i] = iter->v.psymtab;
4184
4185 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4186 }
4187
3019eac3 4188 if (per_cu->is_debug_types)
348e048f
DE
4189 {
4190 /* It's not clear we want to do anything with stmt lists here.
4191 Waiting to see what gcc ultimately does. */
4192 }
d85a05f0 4193 else
93311388
DE
4194 {
4195 /* Get the list of files included in the current compilation unit,
4196 and build a psymtab for each of them. */
dee91e82 4197 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
93311388 4198 }
dee91e82 4199}
ae038cb0 4200
dee91e82
DE
4201/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4202 Process compilation unit THIS_CU for a psymtab. */
4203
4204static void
95554aad
TT
4205process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4206 int want_partial_unit)
dee91e82
DE
4207{
4208 /* If this compilation unit was already read in, free the
4209 cached copy in order to read it in again. This is
4210 necessary because we skipped some symbols when we first
4211 read in the compilation unit (see load_partial_dies).
4212 This problem could be avoided, but the benefit is unclear. */
4213 if (this_cu->cu != NULL)
4214 free_one_cached_comp_unit (this_cu);
4215
3019eac3 4216 gdb_assert (! this_cu->is_debug_types);
fd820528 4217 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
95554aad 4218 &want_partial_unit);
dee91e82
DE
4219
4220 /* Age out any secondary CUs. */
4221 age_cached_comp_units ();
93311388 4222}
ff013f42 4223
348e048f
DE
4224/* Traversal function for htab_traverse_noresize.
4225 Process one .debug_types comp-unit. */
4226
4227static int
dee91e82 4228process_psymtab_type_unit (void **slot, void *info)
348e048f 4229{
dee91e82
DE
4230 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4231 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 4232
fd820528 4233 gdb_assert (per_cu->is_debug_types);
a0f42c21 4234 gdb_assert (info == NULL);
348e048f 4235
dee91e82
DE
4236 /* If this compilation unit was already read in, free the
4237 cached copy in order to read it in again. This is
4238 necessary because we skipped some symbols when we first
4239 read in the compilation unit (see load_partial_dies).
4240 This problem could be avoided, but the benefit is unclear. */
4241 if (per_cu->cu != NULL)
4242 free_one_cached_comp_unit (per_cu);
4243
fd820528
DE
4244 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4245 NULL);
dee91e82
DE
4246
4247 /* Age out any secondary CUs. */
4248 age_cached_comp_units ();
348e048f
DE
4249
4250 return 1;
4251}
4252
4253/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4254 Build partial symbol tables for the .debug_types comp-units. */
4255
4256static void
4257build_type_psymtabs (struct objfile *objfile)
4258{
0e50663e 4259 if (! create_all_type_units (objfile))
348e048f
DE
4260 return;
4261
4262 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
dee91e82 4263 process_psymtab_type_unit, NULL);
348e048f
DE
4264}
4265
60606b2c
TT
4266/* A cleanup function that clears objfile's psymtabs_addrmap field. */
4267
4268static void
4269psymtabs_addrmap_cleanup (void *o)
4270{
4271 struct objfile *objfile = o;
ec61707d 4272
60606b2c
TT
4273 objfile->psymtabs_addrmap = NULL;
4274}
4275
95554aad
TT
4276/* Compute the 'user' field for each psymtab in OBJFILE. */
4277
4278static void
4279set_partial_user (struct objfile *objfile)
4280{
4281 int i;
4282
4283 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4284 {
4285 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4286 struct partial_symtab *pst = per_cu->v.psymtab;
4287 int j;
4288
4289 for (j = 0; j < pst->number_of_dependencies; ++j)
4290 {
4291 /* Set the 'user' field only if it is not already set. */
4292 if (pst->dependencies[j]->user == NULL)
4293 pst->dependencies[j]->user = pst;
4294 }
4295 }
4296}
4297
93311388
DE
4298/* Build the partial symbol table by doing a quick pass through the
4299 .debug_info and .debug_abbrev sections. */
72bf9492 4300
93311388 4301static void
c67a9c90 4302dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 4303{
60606b2c
TT
4304 struct cleanup *back_to, *addrmap_cleanup;
4305 struct obstack temp_obstack;
21b2bd31 4306 int i;
93311388 4307
98bfdba5
PA
4308 dwarf2_per_objfile->reading_partial_symbols = 1;
4309
be391dca 4310 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 4311
93311388
DE
4312 /* Any cached compilation units will be linked by the per-objfile
4313 read_in_chain. Make sure to free them when we're done. */
4314 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 4315
348e048f
DE
4316 build_type_psymtabs (objfile);
4317
93311388 4318 create_all_comp_units (objfile);
c906108c 4319
60606b2c
TT
4320 /* Create a temporary address map on a temporary obstack. We later
4321 copy this to the final obstack. */
4322 obstack_init (&temp_obstack);
4323 make_cleanup_obstack_free (&temp_obstack);
4324 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4325 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 4326
21b2bd31 4327 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 4328 {
21b2bd31 4329 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 4330
95554aad 4331 process_psymtab_comp_unit (per_cu, 0);
c906108c 4332 }
ff013f42 4333
95554aad
TT
4334 set_partial_user (objfile);
4335
ff013f42
JK
4336 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4337 &objfile->objfile_obstack);
60606b2c 4338 discard_cleanups (addrmap_cleanup);
ff013f42 4339
ae038cb0
DJ
4340 do_cleanups (back_to);
4341}
4342
3019eac3 4343/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
4344
4345static void
dee91e82
DE
4346load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4347 gdb_byte *info_ptr,
4348 struct die_info *comp_unit_die,
4349 int has_children,
4350 void *data)
ae038cb0 4351{
dee91e82 4352 struct dwarf2_cu *cu = reader->cu;
ae038cb0 4353
95554aad 4354 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 4355
ae038cb0
DJ
4356 /* Check if comp unit has_children.
4357 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4358 If not, there's no more debug_info for this comp unit. */
d85a05f0 4359 if (has_children)
dee91e82
DE
4360 load_partial_dies (reader, info_ptr, 0);
4361}
98bfdba5 4362
dee91e82
DE
4363/* Load the partial DIEs for a secondary CU into memory.
4364 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 4365
dee91e82
DE
4366static void
4367load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4368{
fd820528 4369 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
4370}
4371
9cdd5dbd
DE
4372/* Create a list of all compilation units in OBJFILE.
4373 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
4374
4375static void
4376create_all_comp_units (struct objfile *objfile)
4377{
4378 int n_allocated;
4379 int n_comp_units;
4380 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
4381 gdb_byte *info_ptr;
4382
4383 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4384 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4385
4386 n_comp_units = 0;
4387 n_allocated = 10;
4388 all_comp_units = xmalloc (n_allocated
4389 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 4390
3e43a32a
MS
4391 while (info_ptr < dwarf2_per_objfile->info.buffer
4392 + dwarf2_per_objfile->info.size)
ae038cb0 4393 {
c764a876 4394 unsigned int length, initial_length_size;
ae038cb0 4395 struct dwarf2_per_cu_data *this_cu;
b64f50a1 4396 sect_offset offset;
ae038cb0 4397
b64f50a1 4398 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4399
4400 /* Read just enough information to find out where the next
4401 compilation unit is. */
c764a876
DE
4402 length = read_initial_length (objfile->obfd, info_ptr,
4403 &initial_length_size);
ae038cb0
DJ
4404
4405 /* Save the compilation unit for later lookup. */
4406 this_cu = obstack_alloc (&objfile->objfile_obstack,
4407 sizeof (struct dwarf2_per_cu_data));
4408 memset (this_cu, 0, sizeof (*this_cu));
4409 this_cu->offset = offset;
c764a876 4410 this_cu->length = length + initial_length_size;
9291a0cd 4411 this_cu->objfile = objfile;
3019eac3 4412 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
ae038cb0
DJ
4413
4414 if (n_comp_units == n_allocated)
4415 {
4416 n_allocated *= 2;
4417 all_comp_units = xrealloc (all_comp_units,
4418 n_allocated
4419 * sizeof (struct dwarf2_per_cu_data *));
4420 }
4421 all_comp_units[n_comp_units++] = this_cu;
4422
4423 info_ptr = info_ptr + this_cu->length;
4424 }
4425
4426 dwarf2_per_objfile->all_comp_units
4427 = obstack_alloc (&objfile->objfile_obstack,
4428 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4429 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4430 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4431 xfree (all_comp_units);
4432 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
4433}
4434
5734ee8b
DJ
4435/* Process all loaded DIEs for compilation unit CU, starting at
4436 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4437 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4438 DW_AT_ranges). If NEED_PC is set, then this function will set
4439 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4440 and record the covered ranges in the addrmap. */
c906108c 4441
72bf9492
DJ
4442static void
4443scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 4444 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 4445{
72bf9492 4446 struct partial_die_info *pdi;
c906108c 4447
91c24f0a
DC
4448 /* Now, march along the PDI's, descending into ones which have
4449 interesting children but skipping the children of the other ones,
4450 until we reach the end of the compilation unit. */
c906108c 4451
72bf9492 4452 pdi = first_die;
91c24f0a 4453
72bf9492
DJ
4454 while (pdi != NULL)
4455 {
4456 fixup_partial_die (pdi, cu);
c906108c 4457
f55ee35c 4458 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
4459 children, so we need to look at them. Ditto for anonymous
4460 enums. */
933c6fe4 4461
72bf9492 4462 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
4463 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4464 || pdi->tag == DW_TAG_imported_unit)
c906108c 4465 {
72bf9492 4466 switch (pdi->tag)
c906108c
SS
4467 {
4468 case DW_TAG_subprogram:
5734ee8b 4469 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 4470 break;
72929c62 4471 case DW_TAG_constant:
c906108c
SS
4472 case DW_TAG_variable:
4473 case DW_TAG_typedef:
91c24f0a 4474 case DW_TAG_union_type:
72bf9492 4475 if (!pdi->is_declaration)
63d06c5c 4476 {
72bf9492 4477 add_partial_symbol (pdi, cu);
63d06c5c
DC
4478 }
4479 break;
c906108c 4480 case DW_TAG_class_type:
680b30c7 4481 case DW_TAG_interface_type:
c906108c 4482 case DW_TAG_structure_type:
72bf9492 4483 if (!pdi->is_declaration)
c906108c 4484 {
72bf9492 4485 add_partial_symbol (pdi, cu);
c906108c
SS
4486 }
4487 break;
91c24f0a 4488 case DW_TAG_enumeration_type:
72bf9492
DJ
4489 if (!pdi->is_declaration)
4490 add_partial_enumeration (pdi, cu);
c906108c
SS
4491 break;
4492 case DW_TAG_base_type:
a02abb62 4493 case DW_TAG_subrange_type:
c906108c 4494 /* File scope base type definitions are added to the partial
c5aa993b 4495 symbol table. */
72bf9492 4496 add_partial_symbol (pdi, cu);
c906108c 4497 break;
d9fa45fe 4498 case DW_TAG_namespace:
5734ee8b 4499 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 4500 break;
5d7cb8df
JK
4501 case DW_TAG_module:
4502 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4503 break;
95554aad
TT
4504 case DW_TAG_imported_unit:
4505 {
4506 struct dwarf2_per_cu_data *per_cu;
4507
4508 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4509 cu->objfile);
4510
4511 /* Go read the partial unit, if needed. */
4512 if (per_cu->v.psymtab == NULL)
4513 process_psymtab_comp_unit (per_cu, 1);
4514
4515 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4516 per_cu);
4517 }
4518 break;
c906108c
SS
4519 default:
4520 break;
4521 }
4522 }
4523
72bf9492
DJ
4524 /* If the die has a sibling, skip to the sibling. */
4525
4526 pdi = pdi->die_sibling;
4527 }
4528}
4529
4530/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 4531
72bf9492 4532 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
4533 name is concatenated with "::" and the partial DIE's name. For
4534 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
4535 Enumerators are an exception; they use the scope of their parent
4536 enumeration type, i.e. the name of the enumeration type is not
4537 prepended to the enumerator.
91c24f0a 4538
72bf9492
DJ
4539 There are two complexities. One is DW_AT_specification; in this
4540 case "parent" means the parent of the target of the specification,
4541 instead of the direct parent of the DIE. The other is compilers
4542 which do not emit DW_TAG_namespace; in this case we try to guess
4543 the fully qualified name of structure types from their members'
4544 linkage names. This must be done using the DIE's children rather
4545 than the children of any DW_AT_specification target. We only need
4546 to do this for structures at the top level, i.e. if the target of
4547 any DW_AT_specification (if any; otherwise the DIE itself) does not
4548 have a parent. */
4549
4550/* Compute the scope prefix associated with PDI's parent, in
4551 compilation unit CU. The result will be allocated on CU's
4552 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4553 field. NULL is returned if no prefix is necessary. */
4554static char *
4555partial_die_parent_scope (struct partial_die_info *pdi,
4556 struct dwarf2_cu *cu)
4557{
4558 char *grandparent_scope;
4559 struct partial_die_info *parent, *real_pdi;
91c24f0a 4560
72bf9492
DJ
4561 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4562 then this means the parent of the specification DIE. */
4563
4564 real_pdi = pdi;
72bf9492 4565 while (real_pdi->has_specification)
10b3939b 4566 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
4567
4568 parent = real_pdi->die_parent;
4569 if (parent == NULL)
4570 return NULL;
4571
4572 if (parent->scope_set)
4573 return parent->scope;
4574
4575 fixup_partial_die (parent, cu);
4576
10b3939b 4577 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 4578
acebe513
UW
4579 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4580 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4581 Work around this problem here. */
4582 if (cu->language == language_cplus
6e70227d 4583 && parent->tag == DW_TAG_namespace
acebe513
UW
4584 && strcmp (parent->name, "::") == 0
4585 && grandparent_scope == NULL)
4586 {
4587 parent->scope = NULL;
4588 parent->scope_set = 1;
4589 return NULL;
4590 }
4591
9c6c53f7
SA
4592 if (pdi->tag == DW_TAG_enumerator)
4593 /* Enumerators should not get the name of the enumeration as a prefix. */
4594 parent->scope = grandparent_scope;
4595 else if (parent->tag == DW_TAG_namespace
f55ee35c 4596 || parent->tag == DW_TAG_module
72bf9492
DJ
4597 || parent->tag == DW_TAG_structure_type
4598 || parent->tag == DW_TAG_class_type
680b30c7 4599 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
4600 || parent->tag == DW_TAG_union_type
4601 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
4602 {
4603 if (grandparent_scope == NULL)
4604 parent->scope = parent->name;
4605 else
3e43a32a
MS
4606 parent->scope = typename_concat (&cu->comp_unit_obstack,
4607 grandparent_scope,
f55ee35c 4608 parent->name, 0, cu);
72bf9492 4609 }
72bf9492
DJ
4610 else
4611 {
4612 /* FIXME drow/2004-04-01: What should we be doing with
4613 function-local names? For partial symbols, we should probably be
4614 ignoring them. */
4615 complaint (&symfile_complaints,
e2e0b3e5 4616 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 4617 parent->tag, pdi->offset.sect_off);
72bf9492 4618 parent->scope = grandparent_scope;
c906108c
SS
4619 }
4620
72bf9492
DJ
4621 parent->scope_set = 1;
4622 return parent->scope;
4623}
4624
4625/* Return the fully scoped name associated with PDI, from compilation unit
4626 CU. The result will be allocated with malloc. */
4568ecf9 4627
72bf9492
DJ
4628static char *
4629partial_die_full_name (struct partial_die_info *pdi,
4630 struct dwarf2_cu *cu)
4631{
4632 char *parent_scope;
4633
98bfdba5
PA
4634 /* If this is a template instantiation, we can not work out the
4635 template arguments from partial DIEs. So, unfortunately, we have
4636 to go through the full DIEs. At least any work we do building
4637 types here will be reused if full symbols are loaded later. */
4638 if (pdi->has_template_arguments)
4639 {
4640 fixup_partial_die (pdi, cu);
4641
4642 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4643 {
4644 struct die_info *die;
4645 struct attribute attr;
4646 struct dwarf2_cu *ref_cu = cu;
4647
b64f50a1 4648 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
4649 attr.name = 0;
4650 attr.form = DW_FORM_ref_addr;
4568ecf9 4651 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
4652 die = follow_die_ref (NULL, &attr, &ref_cu);
4653
4654 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4655 }
4656 }
4657
72bf9492
DJ
4658 parent_scope = partial_die_parent_scope (pdi, cu);
4659 if (parent_scope == NULL)
4660 return NULL;
4661 else
f55ee35c 4662 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4663}
4664
4665static void
72bf9492 4666add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4667{
e7c27a73 4668 struct objfile *objfile = cu->objfile;
c906108c 4669 CORE_ADDR addr = 0;
decbce07 4670 char *actual_name = NULL;
e142c38c 4671 CORE_ADDR baseaddr;
72bf9492 4672 int built_actual_name = 0;
e142c38c
DJ
4673
4674 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4675
94af9270
KS
4676 actual_name = partial_die_full_name (pdi, cu);
4677 if (actual_name)
4678 built_actual_name = 1;
63d06c5c 4679
72bf9492
DJ
4680 if (actual_name == NULL)
4681 actual_name = pdi->name;
4682
c906108c
SS
4683 switch (pdi->tag)
4684 {
4685 case DW_TAG_subprogram:
2cfa0c8d 4686 if (pdi->is_external || cu->language == language_ada)
c906108c 4687 {
2cfa0c8d
JB
4688 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4689 of the global scope. But in Ada, we want to be able to access
4690 nested procedures globally. So all Ada subprograms are stored
4691 in the global scope. */
f47fb265 4692 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4693 mst_text, objfile); */
f47fb265
MS
4694 add_psymbol_to_list (actual_name, strlen (actual_name),
4695 built_actual_name,
4696 VAR_DOMAIN, LOC_BLOCK,
4697 &objfile->global_psymbols,
4698 0, pdi->lowpc + baseaddr,
4699 cu->language, objfile);
c906108c
SS
4700 }
4701 else
4702 {
f47fb265 4703 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4704 mst_file_text, objfile); */
f47fb265
MS
4705 add_psymbol_to_list (actual_name, strlen (actual_name),
4706 built_actual_name,
4707 VAR_DOMAIN, LOC_BLOCK,
4708 &objfile->static_psymbols,
4709 0, pdi->lowpc + baseaddr,
4710 cu->language, objfile);
c906108c
SS
4711 }
4712 break;
72929c62
JB
4713 case DW_TAG_constant:
4714 {
4715 struct psymbol_allocation_list *list;
4716
4717 if (pdi->is_external)
4718 list = &objfile->global_psymbols;
4719 else
4720 list = &objfile->static_psymbols;
f47fb265
MS
4721 add_psymbol_to_list (actual_name, strlen (actual_name),
4722 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4723 list, 0, 0, cu->language, objfile);
72929c62
JB
4724 }
4725 break;
c906108c 4726 case DW_TAG_variable:
95554aad
TT
4727 if (pdi->d.locdesc)
4728 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 4729
95554aad 4730 if (pdi->d.locdesc
caac4577
JG
4731 && addr == 0
4732 && !dwarf2_per_objfile->has_section_at_zero)
4733 {
4734 /* A global or static variable may also have been stripped
4735 out by the linker if unused, in which case its address
4736 will be nullified; do not add such variables into partial
4737 symbol table then. */
4738 }
4739 else if (pdi->is_external)
c906108c
SS
4740 {
4741 /* Global Variable.
4742 Don't enter into the minimal symbol tables as there is
4743 a minimal symbol table entry from the ELF symbols already.
4744 Enter into partial symbol table if it has a location
4745 descriptor or a type.
4746 If the location descriptor is missing, new_symbol will create
4747 a LOC_UNRESOLVED symbol, the address of the variable will then
4748 be determined from the minimal symbol table whenever the variable
4749 is referenced.
4750 The address for the partial symbol table entry is not
4751 used by GDB, but it comes in handy for debugging partial symbol
4752 table building. */
4753
95554aad 4754 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
4755 add_psymbol_to_list (actual_name, strlen (actual_name),
4756 built_actual_name,
4757 VAR_DOMAIN, LOC_STATIC,
4758 &objfile->global_psymbols,
4759 0, addr + baseaddr,
4760 cu->language, objfile);
c906108c
SS
4761 }
4762 else
4763 {
0963b4bd 4764 /* Static Variable. Skip symbols without location descriptors. */
95554aad 4765 if (pdi->d.locdesc == NULL)
decbce07
MS
4766 {
4767 if (built_actual_name)
4768 xfree (actual_name);
4769 return;
4770 }
f47fb265 4771 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4772 mst_file_data, objfile); */
f47fb265
MS
4773 add_psymbol_to_list (actual_name, strlen (actual_name),
4774 built_actual_name,
4775 VAR_DOMAIN, LOC_STATIC,
4776 &objfile->static_psymbols,
4777 0, addr + baseaddr,
4778 cu->language, objfile);
c906108c
SS
4779 }
4780 break;
4781 case DW_TAG_typedef:
4782 case DW_TAG_base_type:
a02abb62 4783 case DW_TAG_subrange_type:
38d518c9 4784 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4785 built_actual_name,
176620f1 4786 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4787 &objfile->static_psymbols,
e142c38c 4788 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4789 break;
72bf9492
DJ
4790 case DW_TAG_namespace:
4791 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4792 built_actual_name,
72bf9492
DJ
4793 VAR_DOMAIN, LOC_TYPEDEF,
4794 &objfile->global_psymbols,
4795 0, (CORE_ADDR) 0, cu->language, objfile);
4796 break;
c906108c 4797 case DW_TAG_class_type:
680b30c7 4798 case DW_TAG_interface_type:
c906108c
SS
4799 case DW_TAG_structure_type:
4800 case DW_TAG_union_type:
4801 case DW_TAG_enumeration_type:
fa4028e9
JB
4802 /* Skip external references. The DWARF standard says in the section
4803 about "Structure, Union, and Class Type Entries": "An incomplete
4804 structure, union or class type is represented by a structure,
4805 union or class entry that does not have a byte size attribute
4806 and that has a DW_AT_declaration attribute." */
4807 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4808 {
4809 if (built_actual_name)
4810 xfree (actual_name);
4811 return;
4812 }
fa4028e9 4813
63d06c5c
DC
4814 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4815 static vs. global. */
38d518c9 4816 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4817 built_actual_name,
176620f1 4818 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4819 (cu->language == language_cplus
4820 || cu->language == language_java)
63d06c5c
DC
4821 ? &objfile->global_psymbols
4822 : &objfile->static_psymbols,
e142c38c 4823 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4824
c906108c
SS
4825 break;
4826 case DW_TAG_enumerator:
38d518c9 4827 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4828 built_actual_name,
176620f1 4829 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4830 (cu->language == language_cplus
4831 || cu->language == language_java)
f6fe98ef
DJ
4832 ? &objfile->global_psymbols
4833 : &objfile->static_psymbols,
e142c38c 4834 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4835 break;
4836 default:
4837 break;
4838 }
5c4e30ca 4839
72bf9492
DJ
4840 if (built_actual_name)
4841 xfree (actual_name);
c906108c
SS
4842}
4843
5c4e30ca
DC
4844/* Read a partial die corresponding to a namespace; also, add a symbol
4845 corresponding to that namespace to the symbol table. NAMESPACE is
4846 the name of the enclosing namespace. */
91c24f0a 4847
72bf9492
DJ
4848static void
4849add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4850 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4851 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4852{
72bf9492 4853 /* Add a symbol for the namespace. */
e7c27a73 4854
72bf9492 4855 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4856
4857 /* Now scan partial symbols in that namespace. */
4858
91c24f0a 4859 if (pdi->has_children)
5734ee8b 4860 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4861}
4862
5d7cb8df
JK
4863/* Read a partial die corresponding to a Fortran module. */
4864
4865static void
4866add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4867 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4868{
f55ee35c 4869 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4870
4871 if (pdi->has_children)
4872 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4873}
4874
bc30ff58
JB
4875/* Read a partial die corresponding to a subprogram and create a partial
4876 symbol for that subprogram. When the CU language allows it, this
4877 routine also defines a partial symbol for each nested subprogram
4878 that this subprogram contains.
6e70227d 4879
bc30ff58
JB
4880 DIE my also be a lexical block, in which case we simply search
4881 recursively for suprograms defined inside that lexical block.
4882 Again, this is only performed when the CU language allows this
4883 type of definitions. */
4884
4885static void
4886add_partial_subprogram (struct partial_die_info *pdi,
4887 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4888 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4889{
4890 if (pdi->tag == DW_TAG_subprogram)
4891 {
4892 if (pdi->has_pc_info)
4893 {
4894 if (pdi->lowpc < *lowpc)
4895 *lowpc = pdi->lowpc;
4896 if (pdi->highpc > *highpc)
4897 *highpc = pdi->highpc;
5734ee8b
DJ
4898 if (need_pc)
4899 {
4900 CORE_ADDR baseaddr;
4901 struct objfile *objfile = cu->objfile;
4902
4903 baseaddr = ANOFFSET (objfile->section_offsets,
4904 SECT_OFF_TEXT (objfile));
4905 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4906 pdi->lowpc + baseaddr,
4907 pdi->highpc - 1 + baseaddr,
9291a0cd 4908 cu->per_cu->v.psymtab);
5734ee8b 4909 }
481860b3
GB
4910 }
4911
4912 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4913 {
bc30ff58 4914 if (!pdi->is_declaration)
e8d05480
JB
4915 /* Ignore subprogram DIEs that do not have a name, they are
4916 illegal. Do not emit a complaint at this point, we will
4917 do so when we convert this psymtab into a symtab. */
4918 if (pdi->name)
4919 add_partial_symbol (pdi, cu);
bc30ff58
JB
4920 }
4921 }
6e70227d 4922
bc30ff58
JB
4923 if (! pdi->has_children)
4924 return;
4925
4926 if (cu->language == language_ada)
4927 {
4928 pdi = pdi->die_child;
4929 while (pdi != NULL)
4930 {
4931 fixup_partial_die (pdi, cu);
4932 if (pdi->tag == DW_TAG_subprogram
4933 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4934 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4935 pdi = pdi->die_sibling;
4936 }
4937 }
4938}
4939
91c24f0a
DC
4940/* Read a partial die corresponding to an enumeration type. */
4941
72bf9492
DJ
4942static void
4943add_partial_enumeration (struct partial_die_info *enum_pdi,
4944 struct dwarf2_cu *cu)
91c24f0a 4945{
72bf9492 4946 struct partial_die_info *pdi;
91c24f0a
DC
4947
4948 if (enum_pdi->name != NULL)
72bf9492
DJ
4949 add_partial_symbol (enum_pdi, cu);
4950
4951 pdi = enum_pdi->die_child;
4952 while (pdi)
91c24f0a 4953 {
72bf9492 4954 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4955 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4956 else
72bf9492
DJ
4957 add_partial_symbol (pdi, cu);
4958 pdi = pdi->die_sibling;
91c24f0a 4959 }
91c24f0a
DC
4960}
4961
6caca83c
CC
4962/* Return the initial uleb128 in the die at INFO_PTR. */
4963
4964static unsigned int
4965peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4966{
4967 unsigned int bytes_read;
4968
4969 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4970}
4971
4bb7a0a7
DJ
4972/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4973 Return the corresponding abbrev, or NULL if the number is zero (indicating
4974 an empty DIE). In either case *BYTES_READ will be set to the length of
4975 the initial number. */
4976
4977static struct abbrev_info *
fe1b8b76 4978peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4979 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4980{
4981 bfd *abfd = cu->objfile->obfd;
4982 unsigned int abbrev_number;
4983 struct abbrev_info *abbrev;
4984
4985 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4986
4987 if (abbrev_number == 0)
4988 return NULL;
4989
4990 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4991 if (!abbrev)
4992 {
3e43a32a
MS
4993 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4994 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
4995 }
4996
4997 return abbrev;
4998}
4999
93311388
DE
5000/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5001 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
5002 DIE. Any children of the skipped DIEs will also be skipped. */
5003
fe1b8b76 5004static gdb_byte *
dee91e82 5005skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 5006{
dee91e82 5007 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
5008 struct abbrev_info *abbrev;
5009 unsigned int bytes_read;
5010
5011 while (1)
5012 {
5013 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5014 if (abbrev == NULL)
5015 return info_ptr + bytes_read;
5016 else
dee91e82 5017 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
5018 }
5019}
5020
93311388
DE
5021/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5022 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
5023 abbrev corresponding to that skipped uleb128 should be passed in
5024 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5025 children. */
5026
fe1b8b76 5027static gdb_byte *
dee91e82
DE
5028skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5029 struct abbrev_info *abbrev)
4bb7a0a7
DJ
5030{
5031 unsigned int bytes_read;
5032 struct attribute attr;
dee91e82
DE
5033 bfd *abfd = reader->abfd;
5034 struct dwarf2_cu *cu = reader->cu;
5035 gdb_byte *buffer = reader->buffer;
4bb7a0a7
DJ
5036 unsigned int form, i;
5037
5038 for (i = 0; i < abbrev->num_attrs; i++)
5039 {
5040 /* The only abbrev we care about is DW_AT_sibling. */
5041 if (abbrev->attrs[i].name == DW_AT_sibling)
5042 {
dee91e82 5043 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 5044 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
5045 complaint (&symfile_complaints,
5046 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 5047 else
b64f50a1 5048 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
5049 }
5050
5051 /* If it isn't DW_AT_sibling, skip this attribute. */
5052 form = abbrev->attrs[i].form;
5053 skip_attribute:
5054 switch (form)
5055 {
4bb7a0a7 5056 case DW_FORM_ref_addr:
ae411497
TT
5057 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5058 and later it is offset sized. */
5059 if (cu->header.version == 2)
5060 info_ptr += cu->header.addr_size;
5061 else
5062 info_ptr += cu->header.offset_size;
5063 break;
5064 case DW_FORM_addr:
4bb7a0a7
DJ
5065 info_ptr += cu->header.addr_size;
5066 break;
5067 case DW_FORM_data1:
5068 case DW_FORM_ref1:
5069 case DW_FORM_flag:
5070 info_ptr += 1;
5071 break;
2dc7f7b3
TT
5072 case DW_FORM_flag_present:
5073 break;
4bb7a0a7
DJ
5074 case DW_FORM_data2:
5075 case DW_FORM_ref2:
5076 info_ptr += 2;
5077 break;
5078 case DW_FORM_data4:
5079 case DW_FORM_ref4:
5080 info_ptr += 4;
5081 break;
5082 case DW_FORM_data8:
5083 case DW_FORM_ref8:
55f1336d 5084 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
5085 info_ptr += 8;
5086 break;
5087 case DW_FORM_string:
9b1c24c8 5088 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
5089 info_ptr += bytes_read;
5090 break;
2dc7f7b3 5091 case DW_FORM_sec_offset:
4bb7a0a7
DJ
5092 case DW_FORM_strp:
5093 info_ptr += cu->header.offset_size;
5094 break;
2dc7f7b3 5095 case DW_FORM_exprloc:
4bb7a0a7
DJ
5096 case DW_FORM_block:
5097 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5098 info_ptr += bytes_read;
5099 break;
5100 case DW_FORM_block1:
5101 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5102 break;
5103 case DW_FORM_block2:
5104 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5105 break;
5106 case DW_FORM_block4:
5107 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5108 break;
5109 case DW_FORM_sdata:
5110 case DW_FORM_udata:
5111 case DW_FORM_ref_udata:
3019eac3
DE
5112 case DW_FORM_GNU_addr_index:
5113 case DW_FORM_GNU_str_index:
4bb7a0a7
DJ
5114 info_ptr = skip_leb128 (abfd, info_ptr);
5115 break;
5116 case DW_FORM_indirect:
5117 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5118 info_ptr += bytes_read;
5119 /* We need to continue parsing from here, so just go back to
5120 the top. */
5121 goto skip_attribute;
5122
5123 default:
3e43a32a
MS
5124 error (_("Dwarf Error: Cannot handle %s "
5125 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
5126 dwarf_form_name (form),
5127 bfd_get_filename (abfd));
5128 }
5129 }
5130
5131 if (abbrev->has_children)
dee91e82 5132 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
5133 else
5134 return info_ptr;
5135}
5136
93311388 5137/* Locate ORIG_PDI's sibling.
dee91e82 5138 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 5139
fe1b8b76 5140static gdb_byte *
dee91e82
DE
5141locate_pdi_sibling (const struct die_reader_specs *reader,
5142 struct partial_die_info *orig_pdi,
5143 gdb_byte *info_ptr)
91c24f0a
DC
5144{
5145 /* Do we know the sibling already? */
72bf9492 5146
91c24f0a
DC
5147 if (orig_pdi->sibling)
5148 return orig_pdi->sibling;
5149
5150 /* Are there any children to deal with? */
5151
5152 if (!orig_pdi->has_children)
5153 return info_ptr;
5154
4bb7a0a7 5155 /* Skip the children the long way. */
91c24f0a 5156
dee91e82 5157 return skip_children (reader, info_ptr);
91c24f0a
DC
5158}
5159
c906108c
SS
5160/* Expand this partial symbol table into a full symbol table. */
5161
5162static void
fba45db2 5163dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 5164{
c906108c
SS
5165 if (pst != NULL)
5166 {
5167 if (pst->readin)
5168 {
3e43a32a
MS
5169 warning (_("bug: psymtab for %s is already read in."),
5170 pst->filename);
c906108c
SS
5171 }
5172 else
5173 {
5174 if (info_verbose)
5175 {
3e43a32a
MS
5176 printf_filtered (_("Reading in symbols for %s..."),
5177 pst->filename);
c906108c
SS
5178 gdb_flush (gdb_stdout);
5179 }
5180
10b3939b
DJ
5181 /* Restore our global data. */
5182 dwarf2_per_objfile = objfile_data (pst->objfile,
5183 dwarf2_objfile_data_key);
5184
b2ab525c
KB
5185 /* If this psymtab is constructed from a debug-only objfile, the
5186 has_section_at_zero flag will not necessarily be correct. We
5187 can get the correct value for this flag by looking at the data
5188 associated with the (presumably stripped) associated objfile. */
5189 if (pst->objfile->separate_debug_objfile_backlink)
5190 {
5191 struct dwarf2_per_objfile *dpo_backlink
5192 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5193 dwarf2_objfile_data_key);
9a619af0 5194
b2ab525c
KB
5195 dwarf2_per_objfile->has_section_at_zero
5196 = dpo_backlink->has_section_at_zero;
5197 }
5198
98bfdba5
PA
5199 dwarf2_per_objfile->reading_partial_symbols = 0;
5200
c906108c
SS
5201 psymtab_to_symtab_1 (pst);
5202
5203 /* Finish up the debug error message. */
5204 if (info_verbose)
a3f17187 5205 printf_filtered (_("done.\n"));
c906108c
SS
5206 }
5207 }
95554aad
TT
5208
5209 process_cu_includes ();
c906108c 5210}
9cdd5dbd
DE
5211\f
5212/* Reading in full CUs. */
c906108c 5213
10b3939b
DJ
5214/* Add PER_CU to the queue. */
5215
5216static void
95554aad
TT
5217queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5218 enum language pretend_language)
10b3939b
DJ
5219{
5220 struct dwarf2_queue_item *item;
5221
5222 per_cu->queued = 1;
5223 item = xmalloc (sizeof (*item));
5224 item->per_cu = per_cu;
95554aad 5225 item->pretend_language = pretend_language;
10b3939b
DJ
5226 item->next = NULL;
5227
5228 if (dwarf2_queue == NULL)
5229 dwarf2_queue = item;
5230 else
5231 dwarf2_queue_tail->next = item;
5232
5233 dwarf2_queue_tail = item;
5234}
5235
5236/* Process the queue. */
5237
5238static void
a0f42c21 5239process_queue (void)
10b3939b
DJ
5240{
5241 struct dwarf2_queue_item *item, *next_item;
5242
03dd20cc
DJ
5243 /* The queue starts out with one item, but following a DIE reference
5244 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
5245 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5246 {
9291a0cd
TT
5247 if (dwarf2_per_objfile->using_index
5248 ? !item->per_cu->v.quick->symtab
5249 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
95554aad 5250 process_full_comp_unit (item->per_cu, item->pretend_language);
10b3939b
DJ
5251
5252 item->per_cu->queued = 0;
5253 next_item = item->next;
5254 xfree (item);
5255 }
5256
5257 dwarf2_queue_tail = NULL;
5258}
5259
5260/* Free all allocated queue entries. This function only releases anything if
5261 an error was thrown; if the queue was processed then it would have been
5262 freed as we went along. */
5263
5264static void
5265dwarf2_release_queue (void *dummy)
5266{
5267 struct dwarf2_queue_item *item, *last;
5268
5269 item = dwarf2_queue;
5270 while (item)
5271 {
5272 /* Anything still marked queued is likely to be in an
5273 inconsistent state, so discard it. */
5274 if (item->per_cu->queued)
5275 {
5276 if (item->per_cu->cu != NULL)
dee91e82 5277 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
5278 item->per_cu->queued = 0;
5279 }
5280
5281 last = item;
5282 item = item->next;
5283 xfree (last);
5284 }
5285
5286 dwarf2_queue = dwarf2_queue_tail = NULL;
5287}
5288
5289/* Read in full symbols for PST, and anything it depends on. */
5290
c906108c 5291static void
fba45db2 5292psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 5293{
10b3939b 5294 struct dwarf2_per_cu_data *per_cu;
c906108c 5295 struct cleanup *back_to;
aaa75496
JB
5296 int i;
5297
95554aad
TT
5298 if (pst->readin)
5299 return;
5300
aaa75496 5301 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
5302 if (!pst->dependencies[i]->readin
5303 && pst->dependencies[i]->user == NULL)
aaa75496
JB
5304 {
5305 /* Inform about additional files that need to be read in. */
5306 if (info_verbose)
5307 {
a3f17187 5308 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
5309 fputs_filtered (" ", gdb_stdout);
5310 wrap_here ("");
5311 fputs_filtered ("and ", gdb_stdout);
5312 wrap_here ("");
5313 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 5314 wrap_here (""); /* Flush output. */
aaa75496
JB
5315 gdb_flush (gdb_stdout);
5316 }
5317 psymtab_to_symtab_1 (pst->dependencies[i]);
5318 }
5319
e38df1d0 5320 per_cu = pst->read_symtab_private;
10b3939b
DJ
5321
5322 if (per_cu == NULL)
aaa75496
JB
5323 {
5324 /* It's an include file, no symbols to read for it.
5325 Everything is in the parent symtab. */
5326 pst->readin = 1;
5327 return;
5328 }
c906108c 5329
a0f42c21 5330 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
5331}
5332
dee91e82
DE
5333/* Trivial hash function for die_info: the hash value of a DIE
5334 is its offset in .debug_info for this objfile. */
10b3939b 5335
dee91e82
DE
5336static hashval_t
5337die_hash (const void *item)
10b3939b 5338{
dee91e82 5339 const struct die_info *die = item;
6502dd73 5340
dee91e82
DE
5341 return die->offset.sect_off;
5342}
63d06c5c 5343
dee91e82
DE
5344/* Trivial comparison function for die_info structures: two DIEs
5345 are equal if they have the same offset. */
98bfdba5 5346
dee91e82
DE
5347static int
5348die_eq (const void *item_lhs, const void *item_rhs)
5349{
5350 const struct die_info *die_lhs = item_lhs;
5351 const struct die_info *die_rhs = item_rhs;
c906108c 5352
dee91e82
DE
5353 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5354}
c906108c 5355
dee91e82
DE
5356/* die_reader_func for load_full_comp_unit.
5357 This is identical to read_signatured_type_reader,
5358 but is kept separate for now. */
c906108c 5359
dee91e82
DE
5360static void
5361load_full_comp_unit_reader (const struct die_reader_specs *reader,
5362 gdb_byte *info_ptr,
5363 struct die_info *comp_unit_die,
5364 int has_children,
5365 void *data)
5366{
5367 struct dwarf2_cu *cu = reader->cu;
5368 struct attribute *attr;
95554aad 5369 enum language *language_ptr = data;
6caca83c 5370
dee91e82
DE
5371 gdb_assert (cu->die_hash == NULL);
5372 cu->die_hash =
5373 htab_create_alloc_ex (cu->header.length / 12,
5374 die_hash,
5375 die_eq,
5376 NULL,
5377 &cu->comp_unit_obstack,
5378 hashtab_obstack_allocate,
5379 dummy_obstack_deallocate);
e142c38c 5380
dee91e82
DE
5381 if (has_children)
5382 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5383 &info_ptr, comp_unit_die);
5384 cu->dies = comp_unit_die;
5385 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
5386
5387 /* We try not to read any attributes in this function, because not
9cdd5dbd 5388 all CUs needed for references have been loaded yet, and symbol
10b3939b 5389 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
5390 or we won't be able to build types correctly.
5391 Similarly, if we do not read the producer, we can not apply
5392 producer-specific interpretation. */
95554aad 5393 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 5394}
10b3939b 5395
dee91e82 5396/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 5397
dee91e82 5398static void
95554aad
TT
5399load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5400 enum language pretend_language)
dee91e82 5401{
3019eac3 5402 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 5403
95554aad
TT
5404 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5405 &pretend_language);
10b3939b
DJ
5406}
5407
3da10d80
KS
5408/* Add a DIE to the delayed physname list. */
5409
5410static void
5411add_to_method_list (struct type *type, int fnfield_index, int index,
5412 const char *name, struct die_info *die,
5413 struct dwarf2_cu *cu)
5414{
5415 struct delayed_method_info mi;
5416 mi.type = type;
5417 mi.fnfield_index = fnfield_index;
5418 mi.index = index;
5419 mi.name = name;
5420 mi.die = die;
5421 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5422}
5423
5424/* A cleanup for freeing the delayed method list. */
5425
5426static void
5427free_delayed_list (void *ptr)
5428{
5429 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5430 if (cu->method_list != NULL)
5431 {
5432 VEC_free (delayed_method_info, cu->method_list);
5433 cu->method_list = NULL;
5434 }
5435}
5436
5437/* Compute the physnames of any methods on the CU's method list.
5438
5439 The computation of method physnames is delayed in order to avoid the
5440 (bad) condition that one of the method's formal parameters is of an as yet
5441 incomplete type. */
5442
5443static void
5444compute_delayed_physnames (struct dwarf2_cu *cu)
5445{
5446 int i;
5447 struct delayed_method_info *mi;
5448 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5449 {
1d06ead6 5450 const char *physname;
3da10d80
KS
5451 struct fn_fieldlist *fn_flp
5452 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 5453 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
5454 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5455 }
5456}
5457
a766d390
DE
5458/* Go objects should be embedded in a DW_TAG_module DIE,
5459 and it's not clear if/how imported objects will appear.
5460 To keep Go support simple until that's worked out,
5461 go back through what we've read and create something usable.
5462 We could do this while processing each DIE, and feels kinda cleaner,
5463 but that way is more invasive.
5464 This is to, for example, allow the user to type "p var" or "b main"
5465 without having to specify the package name, and allow lookups
5466 of module.object to work in contexts that use the expression
5467 parser. */
5468
5469static void
5470fixup_go_packaging (struct dwarf2_cu *cu)
5471{
5472 char *package_name = NULL;
5473 struct pending *list;
5474 int i;
5475
5476 for (list = global_symbols; list != NULL; list = list->next)
5477 {
5478 for (i = 0; i < list->nsyms; ++i)
5479 {
5480 struct symbol *sym = list->symbol[i];
5481
5482 if (SYMBOL_LANGUAGE (sym) == language_go
5483 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5484 {
5485 char *this_package_name = go_symbol_package_name (sym);
5486
5487 if (this_package_name == NULL)
5488 continue;
5489 if (package_name == NULL)
5490 package_name = this_package_name;
5491 else
5492 {
5493 if (strcmp (package_name, this_package_name) != 0)
5494 complaint (&symfile_complaints,
5495 _("Symtab %s has objects from two different Go packages: %s and %s"),
5496 (sym->symtab && sym->symtab->filename
5497 ? sym->symtab->filename
5498 : cu->objfile->name),
5499 this_package_name, package_name);
5500 xfree (this_package_name);
5501 }
5502 }
5503 }
5504 }
5505
5506 if (package_name != NULL)
5507 {
5508 struct objfile *objfile = cu->objfile;
5509 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5510 package_name, objfile);
5511 struct symbol *sym;
5512
5513 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5514
5515 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5516 SYMBOL_SET_LANGUAGE (sym, language_go);
5517 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5518 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5519 e.g., "main" finds the "main" module and not C's main(). */
5520 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5521 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5522 SYMBOL_TYPE (sym) = type;
5523
5524 add_symbol_to_list (sym, &global_symbols);
5525
5526 xfree (package_name);
5527 }
5528}
5529
95554aad
TT
5530static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5531
5532/* Return the symtab for PER_CU. This works properly regardless of
5533 whether we're using the index or psymtabs. */
5534
5535static struct symtab *
5536get_symtab (struct dwarf2_per_cu_data *per_cu)
5537{
5538 return (dwarf2_per_objfile->using_index
5539 ? per_cu->v.quick->symtab
5540 : per_cu->v.psymtab->symtab);
5541}
5542
5543/* A helper function for computing the list of all symbol tables
5544 included by PER_CU. */
5545
5546static void
5547recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5548 htab_t all_children,
5549 struct dwarf2_per_cu_data *per_cu)
5550{
5551 void **slot;
5552 int ix;
5553 struct dwarf2_per_cu_data *iter;
5554
5555 slot = htab_find_slot (all_children, per_cu, INSERT);
5556 if (*slot != NULL)
5557 {
5558 /* This inclusion and its children have been processed. */
5559 return;
5560 }
5561
5562 *slot = per_cu;
5563 /* Only add a CU if it has a symbol table. */
5564 if (get_symtab (per_cu) != NULL)
5565 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5566
5567 for (ix = 0;
5568 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5569 ++ix)
5570 recursively_compute_inclusions (result, all_children, iter);
5571}
5572
5573/* Compute the symtab 'includes' fields for the symtab related to
5574 PER_CU. */
5575
5576static void
5577compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5578{
5579 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5580 {
5581 int ix, len;
5582 struct dwarf2_per_cu_data *iter;
5583 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5584 htab_t all_children;
5585 struct symtab *symtab = get_symtab (per_cu);
5586
5587 /* If we don't have a symtab, we can just skip this case. */
5588 if (symtab == NULL)
5589 return;
5590
5591 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5592 NULL, xcalloc, xfree);
5593
5594 for (ix = 0;
5595 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5596 ix, iter);
5597 ++ix)
5598 recursively_compute_inclusions (&result_children, all_children, iter);
5599
5600 /* Now we have a transitive closure of all the included CUs, so
5601 we can convert it to a list of symtabs. */
5602 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5603 symtab->includes
5604 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5605 (len + 1) * sizeof (struct symtab *));
5606 for (ix = 0;
5607 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5608 ++ix)
5609 symtab->includes[ix] = get_symtab (iter);
5610 symtab->includes[len] = NULL;
5611
5612 VEC_free (dwarf2_per_cu_ptr, result_children);
5613 htab_delete (all_children);
5614 }
5615}
5616
5617/* Compute the 'includes' field for the symtabs of all the CUs we just
5618 read. */
5619
5620static void
5621process_cu_includes (void)
5622{
5623 int ix;
5624 struct dwarf2_per_cu_data *iter;
5625
5626 for (ix = 0;
5627 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5628 ix, iter);
5629 ++ix)
5630 compute_symtab_includes (iter);
5631
5632 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5633}
5634
9cdd5dbd 5635/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
5636 already been loaded into memory. */
5637
5638static void
95554aad
TT
5639process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5640 enum language pretend_language)
10b3939b 5641{
10b3939b 5642 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 5643 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
5644 CORE_ADDR lowpc, highpc;
5645 struct symtab *symtab;
3da10d80 5646 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
5647 CORE_ADDR baseaddr;
5648
5649 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5650
10b3939b
DJ
5651 buildsym_init ();
5652 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 5653 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
5654
5655 cu->list_in_scope = &file_symbols;
c906108c 5656
95554aad
TT
5657 cu->language = pretend_language;
5658 cu->language_defn = language_def (cu->language);
5659
c906108c 5660 /* Do line number decoding in read_file_scope () */
10b3939b 5661 process_die (cu->dies, cu);
c906108c 5662
a766d390
DE
5663 /* For now fudge the Go package. */
5664 if (cu->language == language_go)
5665 fixup_go_packaging (cu);
5666
3da10d80
KS
5667 /* Now that we have processed all the DIEs in the CU, all the types
5668 should be complete, and it should now be safe to compute all of the
5669 physnames. */
5670 compute_delayed_physnames (cu);
5671 do_cleanups (delayed_list_cleanup);
5672
fae299cd
DC
5673 /* Some compilers don't define a DW_AT_high_pc attribute for the
5674 compilation unit. If the DW_AT_high_pc is missing, synthesize
5675 it, by scanning the DIE's below the compilation unit. */
10b3939b 5676 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 5677
613e1657 5678 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 5679
8be455d7 5680 if (symtab != NULL)
c906108c 5681 {
df15bd07 5682 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 5683
8be455d7
JK
5684 /* Set symtab language to language from DW_AT_language. If the
5685 compilation is from a C file generated by language preprocessors, do
5686 not set the language if it was already deduced by start_subfile. */
5687 if (!(cu->language == language_c && symtab->language != language_c))
5688 symtab->language = cu->language;
5689
5690 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5691 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
5692 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5693 there were bugs in prologue debug info, fixed later in GCC-4.5
5694 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
5695
5696 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5697 needed, it would be wrong due to missing DW_AT_producer there.
5698
5699 Still one can confuse GDB by using non-standard GCC compilation
5700 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5701 */
ab260dad 5702 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 5703 symtab->locations_valid = 1;
e0d00bc7
JK
5704
5705 if (gcc_4_minor >= 5)
5706 symtab->epilogue_unwind_valid = 1;
96408a79
SA
5707
5708 symtab->call_site_htab = cu->call_site_htab;
c906108c 5709 }
9291a0cd
TT
5710
5711 if (dwarf2_per_objfile->using_index)
5712 per_cu->v.quick->symtab = symtab;
5713 else
5714 {
5715 struct partial_symtab *pst = per_cu->v.psymtab;
5716 pst->symtab = symtab;
5717 pst->readin = 1;
5718 }
c906108c 5719
95554aad
TT
5720 /* Push it for inclusion processing later. */
5721 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5722
c906108c
SS
5723 do_cleanups (back_to);
5724}
5725
95554aad
TT
5726/* Process an imported unit DIE. */
5727
5728static void
5729process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5730{
5731 struct attribute *attr;
5732
5733 attr = dwarf2_attr (die, DW_AT_import, cu);
5734 if (attr != NULL)
5735 {
5736 struct dwarf2_per_cu_data *per_cu;
5737 struct symtab *imported_symtab;
5738 sect_offset offset;
5739
5740 offset = dwarf2_get_ref_die_offset (attr);
5741 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5742
5743 /* Queue the unit, if needed. */
5744 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
5745 load_full_comp_unit (per_cu, cu->language);
5746
5747 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5748 per_cu);
5749 }
5750}
5751
c906108c
SS
5752/* Process a die and its children. */
5753
5754static void
e7c27a73 5755process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
5756{
5757 switch (die->tag)
5758 {
5759 case DW_TAG_padding:
5760 break;
5761 case DW_TAG_compile_unit:
95554aad 5762 case DW_TAG_partial_unit:
e7c27a73 5763 read_file_scope (die, cu);
c906108c 5764 break;
348e048f
DE
5765 case DW_TAG_type_unit:
5766 read_type_unit_scope (die, cu);
5767 break;
c906108c 5768 case DW_TAG_subprogram:
c906108c 5769 case DW_TAG_inlined_subroutine:
edb3359d 5770 read_func_scope (die, cu);
c906108c
SS
5771 break;
5772 case DW_TAG_lexical_block:
14898363
L
5773 case DW_TAG_try_block:
5774 case DW_TAG_catch_block:
e7c27a73 5775 read_lexical_block_scope (die, cu);
c906108c 5776 break;
96408a79
SA
5777 case DW_TAG_GNU_call_site:
5778 read_call_site_scope (die, cu);
5779 break;
c906108c 5780 case DW_TAG_class_type:
680b30c7 5781 case DW_TAG_interface_type:
c906108c
SS
5782 case DW_TAG_structure_type:
5783 case DW_TAG_union_type:
134d01f1 5784 process_structure_scope (die, cu);
c906108c
SS
5785 break;
5786 case DW_TAG_enumeration_type:
134d01f1 5787 process_enumeration_scope (die, cu);
c906108c 5788 break;
134d01f1 5789
f792889a
DJ
5790 /* These dies have a type, but processing them does not create
5791 a symbol or recurse to process the children. Therefore we can
5792 read them on-demand through read_type_die. */
c906108c 5793 case DW_TAG_subroutine_type:
72019c9c 5794 case DW_TAG_set_type:
c906108c 5795 case DW_TAG_array_type:
c906108c 5796 case DW_TAG_pointer_type:
c906108c 5797 case DW_TAG_ptr_to_member_type:
c906108c 5798 case DW_TAG_reference_type:
c906108c 5799 case DW_TAG_string_type:
c906108c 5800 break;
134d01f1 5801
c906108c 5802 case DW_TAG_base_type:
a02abb62 5803 case DW_TAG_subrange_type:
cb249c71 5804 case DW_TAG_typedef:
134d01f1
DJ
5805 /* Add a typedef symbol for the type definition, if it has a
5806 DW_AT_name. */
f792889a 5807 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 5808 break;
c906108c 5809 case DW_TAG_common_block:
e7c27a73 5810 read_common_block (die, cu);
c906108c
SS
5811 break;
5812 case DW_TAG_common_inclusion:
5813 break;
d9fa45fe 5814 case DW_TAG_namespace:
63d06c5c 5815 processing_has_namespace_info = 1;
e7c27a73 5816 read_namespace (die, cu);
d9fa45fe 5817 break;
5d7cb8df 5818 case DW_TAG_module:
f55ee35c 5819 processing_has_namespace_info = 1;
5d7cb8df
JK
5820 read_module (die, cu);
5821 break;
d9fa45fe
DC
5822 case DW_TAG_imported_declaration:
5823 case DW_TAG_imported_module:
63d06c5c 5824 processing_has_namespace_info = 1;
27aa8d6a
SW
5825 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5826 || cu->language != language_fortran))
5827 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5828 dwarf_tag_name (die->tag));
5829 read_import_statement (die, cu);
d9fa45fe 5830 break;
95554aad
TT
5831
5832 case DW_TAG_imported_unit:
5833 process_imported_unit_die (die, cu);
5834 break;
5835
c906108c 5836 default:
e7c27a73 5837 new_symbol (die, NULL, cu);
c906108c
SS
5838 break;
5839 }
5840}
5841
94af9270
KS
5842/* A helper function for dwarf2_compute_name which determines whether DIE
5843 needs to have the name of the scope prepended to the name listed in the
5844 die. */
5845
5846static int
5847die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5848{
1c809c68
TT
5849 struct attribute *attr;
5850
94af9270
KS
5851 switch (die->tag)
5852 {
5853 case DW_TAG_namespace:
5854 case DW_TAG_typedef:
5855 case DW_TAG_class_type:
5856 case DW_TAG_interface_type:
5857 case DW_TAG_structure_type:
5858 case DW_TAG_union_type:
5859 case DW_TAG_enumeration_type:
5860 case DW_TAG_enumerator:
5861 case DW_TAG_subprogram:
5862 case DW_TAG_member:
5863 return 1;
5864
5865 case DW_TAG_variable:
c2b0a229 5866 case DW_TAG_constant:
94af9270
KS
5867 /* We only need to prefix "globally" visible variables. These include
5868 any variable marked with DW_AT_external or any variable that
5869 lives in a namespace. [Variables in anonymous namespaces
5870 require prefixing, but they are not DW_AT_external.] */
5871
5872 if (dwarf2_attr (die, DW_AT_specification, cu))
5873 {
5874 struct dwarf2_cu *spec_cu = cu;
9a619af0 5875
94af9270
KS
5876 return die_needs_namespace (die_specification (die, &spec_cu),
5877 spec_cu);
5878 }
5879
1c809c68 5880 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
5881 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5882 && die->parent->tag != DW_TAG_module)
1c809c68
TT
5883 return 0;
5884 /* A variable in a lexical block of some kind does not need a
5885 namespace, even though in C++ such variables may be external
5886 and have a mangled name. */
5887 if (die->parent->tag == DW_TAG_lexical_block
5888 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5889 || die->parent->tag == DW_TAG_catch_block
5890 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5891 return 0;
5892 return 1;
94af9270
KS
5893
5894 default:
5895 return 0;
5896 }
5897}
5898
98bfdba5
PA
5899/* Retrieve the last character from a mem_file. */
5900
5901static void
5902do_ui_file_peek_last (void *object, const char *buffer, long length)
5903{
5904 char *last_char_p = (char *) object;
5905
5906 if (length > 0)
5907 *last_char_p = buffer[length - 1];
5908}
5909
94af9270 5910/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
5911 compute the physname for the object, which include a method's:
5912 - formal parameters (C++/Java),
5913 - receiver type (Go),
5914 - return type (Java).
5915
5916 The term "physname" is a bit confusing.
5917 For C++, for example, it is the demangled name.
5918 For Go, for example, it's the mangled name.
94af9270 5919
af6b7be1
JB
5920 For Ada, return the DIE's linkage name rather than the fully qualified
5921 name. PHYSNAME is ignored..
5922
94af9270
KS
5923 The result is allocated on the objfile_obstack and canonicalized. */
5924
5925static const char *
5926dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5927 int physname)
5928{
bb5ed363
DE
5929 struct objfile *objfile = cu->objfile;
5930
94af9270
KS
5931 if (name == NULL)
5932 name = dwarf2_name (die, cu);
5933
f55ee35c
JK
5934 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5935 compute it by typename_concat inside GDB. */
5936 if (cu->language == language_ada
5937 || (cu->language == language_fortran && physname))
5938 {
5939 /* For Ada unit, we prefer the linkage name over the name, as
5940 the former contains the exported name, which the user expects
5941 to be able to reference. Ideally, we want the user to be able
5942 to reference this entity using either natural or linkage name,
5943 but we haven't started looking at this enhancement yet. */
5944 struct attribute *attr;
5945
5946 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5947 if (attr == NULL)
5948 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5949 if (attr && DW_STRING (attr))
5950 return DW_STRING (attr);
5951 }
5952
94af9270
KS
5953 /* These are the only languages we know how to qualify names in. */
5954 if (name != NULL
f55ee35c
JK
5955 && (cu->language == language_cplus || cu->language == language_java
5956 || cu->language == language_fortran))
94af9270
KS
5957 {
5958 if (die_needs_namespace (die, cu))
5959 {
5960 long length;
0d5cff50 5961 const char *prefix;
94af9270
KS
5962 struct ui_file *buf;
5963
5964 prefix = determine_prefix (die, cu);
5965 buf = mem_fileopen ();
5966 if (*prefix != '\0')
5967 {
f55ee35c
JK
5968 char *prefixed_name = typename_concat (NULL, prefix, name,
5969 physname, cu);
9a619af0 5970
94af9270
KS
5971 fputs_unfiltered (prefixed_name, buf);
5972 xfree (prefixed_name);
5973 }
5974 else
62d5b8da 5975 fputs_unfiltered (name, buf);
94af9270 5976
98bfdba5
PA
5977 /* Template parameters may be specified in the DIE's DW_AT_name, or
5978 as children with DW_TAG_template_type_param or
5979 DW_TAG_value_type_param. If the latter, add them to the name
5980 here. If the name already has template parameters, then
5981 skip this step; some versions of GCC emit both, and
5982 it is more efficient to use the pre-computed name.
5983
5984 Something to keep in mind about this process: it is very
5985 unlikely, or in some cases downright impossible, to produce
5986 something that will match the mangled name of a function.
5987 If the definition of the function has the same debug info,
5988 we should be able to match up with it anyway. But fallbacks
5989 using the minimal symbol, for instance to find a method
5990 implemented in a stripped copy of libstdc++, will not work.
5991 If we do not have debug info for the definition, we will have to
5992 match them up some other way.
5993
5994 When we do name matching there is a related problem with function
5995 templates; two instantiated function templates are allowed to
5996 differ only by their return types, which we do not add here. */
5997
5998 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5999 {
6000 struct attribute *attr;
6001 struct die_info *child;
6002 int first = 1;
6003
6004 die->building_fullname = 1;
6005
6006 for (child = die->child; child != NULL; child = child->sibling)
6007 {
6008 struct type *type;
12df843f 6009 LONGEST value;
98bfdba5
PA
6010 gdb_byte *bytes;
6011 struct dwarf2_locexpr_baton *baton;
6012 struct value *v;
6013
6014 if (child->tag != DW_TAG_template_type_param
6015 && child->tag != DW_TAG_template_value_param)
6016 continue;
6017
6018 if (first)
6019 {
6020 fputs_unfiltered ("<", buf);
6021 first = 0;
6022 }
6023 else
6024 fputs_unfiltered (", ", buf);
6025
6026 attr = dwarf2_attr (child, DW_AT_type, cu);
6027 if (attr == NULL)
6028 {
6029 complaint (&symfile_complaints,
6030 _("template parameter missing DW_AT_type"));
6031 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6032 continue;
6033 }
6034 type = die_type (child, cu);
6035
6036 if (child->tag == DW_TAG_template_type_param)
6037 {
6038 c_print_type (type, "", buf, -1, 0);
6039 continue;
6040 }
6041
6042 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6043 if (attr == NULL)
6044 {
6045 complaint (&symfile_complaints,
3e43a32a
MS
6046 _("template parameter missing "
6047 "DW_AT_const_value"));
98bfdba5
PA
6048 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6049 continue;
6050 }
6051
6052 dwarf2_const_value_attr (attr, type, name,
6053 &cu->comp_unit_obstack, cu,
6054 &value, &bytes, &baton);
6055
6056 if (TYPE_NOSIGN (type))
6057 /* GDB prints characters as NUMBER 'CHAR'. If that's
6058 changed, this can use value_print instead. */
6059 c_printchar (value, type, buf);
6060 else
6061 {
6062 struct value_print_options opts;
6063
6064 if (baton != NULL)
6065 v = dwarf2_evaluate_loc_desc (type, NULL,
6066 baton->data,
6067 baton->size,
6068 baton->per_cu);
6069 else if (bytes != NULL)
6070 {
6071 v = allocate_value (type);
6072 memcpy (value_contents_writeable (v), bytes,
6073 TYPE_LENGTH (type));
6074 }
6075 else
6076 v = value_from_longest (type, value);
6077
3e43a32a
MS
6078 /* Specify decimal so that we do not depend on
6079 the radix. */
98bfdba5
PA
6080 get_formatted_print_options (&opts, 'd');
6081 opts.raw = 1;
6082 value_print (v, buf, &opts);
6083 release_value (v);
6084 value_free (v);
6085 }
6086 }
6087
6088 die->building_fullname = 0;
6089
6090 if (!first)
6091 {
6092 /* Close the argument list, with a space if necessary
6093 (nested templates). */
6094 char last_char = '\0';
6095 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6096 if (last_char == '>')
6097 fputs_unfiltered (" >", buf);
6098 else
6099 fputs_unfiltered (">", buf);
6100 }
6101 }
6102
94af9270
KS
6103 /* For Java and C++ methods, append formal parameter type
6104 information, if PHYSNAME. */
6e70227d 6105
94af9270
KS
6106 if (physname && die->tag == DW_TAG_subprogram
6107 && (cu->language == language_cplus
6108 || cu->language == language_java))
6109 {
6110 struct type *type = read_type_die (die, cu);
6111
3167638f 6112 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
6113
6114 if (cu->language == language_java)
6115 {
6116 /* For java, we must append the return type to method
0963b4bd 6117 names. */
94af9270
KS
6118 if (die->tag == DW_TAG_subprogram)
6119 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6120 0, 0);
6121 }
6122 else if (cu->language == language_cplus)
6123 {
60430eff
DJ
6124 /* Assume that an artificial first parameter is
6125 "this", but do not crash if it is not. RealView
6126 marks unnamed (and thus unused) parameters as
6127 artificial; there is no way to differentiate
6128 the two cases. */
94af9270
KS
6129 if (TYPE_NFIELDS (type) > 0
6130 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 6131 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
6132 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6133 0))))
94af9270
KS
6134 fputs_unfiltered (" const", buf);
6135 }
6136 }
6137
bb5ed363 6138 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
6139 &length);
6140 ui_file_delete (buf);
6141
6142 if (cu->language == language_cplus)
6143 {
6144 char *cname
6145 = dwarf2_canonicalize_name (name, cu,
bb5ed363 6146 &objfile->objfile_obstack);
9a619af0 6147
94af9270
KS
6148 if (cname != NULL)
6149 name = cname;
6150 }
6151 }
6152 }
6153
6154 return name;
6155}
6156
0114d602
DJ
6157/* Return the fully qualified name of DIE, based on its DW_AT_name.
6158 If scope qualifiers are appropriate they will be added. The result
6159 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
6160 not have a name. NAME may either be from a previous call to
6161 dwarf2_name or NULL.
6162
0963b4bd 6163 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
6164
6165static const char *
94af9270 6166dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 6167{
94af9270
KS
6168 return dwarf2_compute_name (name, die, cu, 0);
6169}
0114d602 6170
94af9270
KS
6171/* Construct a physname for the given DIE in CU. NAME may either be
6172 from a previous call to dwarf2_name or NULL. The result will be
6173 allocated on the objfile_objstack or NULL if the DIE does not have a
6174 name.
0114d602 6175
94af9270 6176 The output string will be canonicalized (if C++/Java). */
0114d602 6177
94af9270
KS
6178static const char *
6179dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6180{
bb5ed363 6181 struct objfile *objfile = cu->objfile;
900e11f9
JK
6182 struct attribute *attr;
6183 const char *retval, *mangled = NULL, *canon = NULL;
6184 struct cleanup *back_to;
6185 int need_copy = 1;
6186
6187 /* In this case dwarf2_compute_name is just a shortcut not building anything
6188 on its own. */
6189 if (!die_needs_namespace (die, cu))
6190 return dwarf2_compute_name (name, die, cu, 1);
6191
6192 back_to = make_cleanup (null_cleanup, NULL);
6193
6194 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6195 if (!attr)
6196 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6197
6198 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6199 has computed. */
6200 if (attr && DW_STRING (attr))
6201 {
6202 char *demangled;
6203
6204 mangled = DW_STRING (attr);
6205
6206 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6207 type. It is easier for GDB users to search for such functions as
6208 `name(params)' than `long name(params)'. In such case the minimal
6209 symbol names do not match the full symbol names but for template
6210 functions there is never a need to look up their definition from their
6211 declaration so the only disadvantage remains the minimal symbol
6212 variant `long name(params)' does not have the proper inferior type.
6213 */
6214
a766d390
DE
6215 if (cu->language == language_go)
6216 {
6217 /* This is a lie, but we already lie to the caller new_symbol_full.
6218 new_symbol_full assumes we return the mangled name.
6219 This just undoes that lie until things are cleaned up. */
6220 demangled = NULL;
6221 }
6222 else
6223 {
6224 demangled = cplus_demangle (mangled,
6225 (DMGL_PARAMS | DMGL_ANSI
6226 | (cu->language == language_java
6227 ? DMGL_JAVA | DMGL_RET_POSTFIX
6228 : DMGL_RET_DROP)));
6229 }
900e11f9
JK
6230 if (demangled)
6231 {
6232 make_cleanup (xfree, demangled);
6233 canon = demangled;
6234 }
6235 else
6236 {
6237 canon = mangled;
6238 need_copy = 0;
6239 }
6240 }
6241
6242 if (canon == NULL || check_physname)
6243 {
6244 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6245
6246 if (canon != NULL && strcmp (physname, canon) != 0)
6247 {
6248 /* It may not mean a bug in GDB. The compiler could also
6249 compute DW_AT_linkage_name incorrectly. But in such case
6250 GDB would need to be bug-to-bug compatible. */
6251
6252 complaint (&symfile_complaints,
6253 _("Computed physname <%s> does not match demangled <%s> "
6254 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 6255 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
6256
6257 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6258 is available here - over computed PHYSNAME. It is safer
6259 against both buggy GDB and buggy compilers. */
6260
6261 retval = canon;
6262 }
6263 else
6264 {
6265 retval = physname;
6266 need_copy = 0;
6267 }
6268 }
6269 else
6270 retval = canon;
6271
6272 if (need_copy)
6273 retval = obsavestring (retval, strlen (retval),
bb5ed363 6274 &objfile->objfile_obstack);
900e11f9
JK
6275
6276 do_cleanups (back_to);
6277 return retval;
0114d602
DJ
6278}
6279
27aa8d6a
SW
6280/* Read the import statement specified by the given die and record it. */
6281
6282static void
6283read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6284{
bb5ed363 6285 struct objfile *objfile = cu->objfile;
27aa8d6a 6286 struct attribute *import_attr;
32019081 6287 struct die_info *imported_die, *child_die;
de4affc9 6288 struct dwarf2_cu *imported_cu;
27aa8d6a 6289 const char *imported_name;
794684b6 6290 const char *imported_name_prefix;
13387711
SW
6291 const char *canonical_name;
6292 const char *import_alias;
6293 const char *imported_declaration = NULL;
794684b6 6294 const char *import_prefix;
32019081
JK
6295 VEC (const_char_ptr) *excludes = NULL;
6296 struct cleanup *cleanups;
13387711
SW
6297
6298 char *temp;
27aa8d6a
SW
6299
6300 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6301 if (import_attr == NULL)
6302 {
6303 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6304 dwarf_tag_name (die->tag));
6305 return;
6306 }
6307
de4affc9
CC
6308 imported_cu = cu;
6309 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6310 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
6311 if (imported_name == NULL)
6312 {
6313 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6314
6315 The import in the following code:
6316 namespace A
6317 {
6318 typedef int B;
6319 }
6320
6321 int main ()
6322 {
6323 using A::B;
6324 B b;
6325 return b;
6326 }
6327
6328 ...
6329 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6330 <52> DW_AT_decl_file : 1
6331 <53> DW_AT_decl_line : 6
6332 <54> DW_AT_import : <0x75>
6333 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6334 <59> DW_AT_name : B
6335 <5b> DW_AT_decl_file : 1
6336 <5c> DW_AT_decl_line : 2
6337 <5d> DW_AT_type : <0x6e>
6338 ...
6339 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6340 <76> DW_AT_byte_size : 4
6341 <77> DW_AT_encoding : 5 (signed)
6342
6343 imports the wrong die ( 0x75 instead of 0x58 ).
6344 This case will be ignored until the gcc bug is fixed. */
6345 return;
6346 }
6347
82856980
SW
6348 /* Figure out the local name after import. */
6349 import_alias = dwarf2_name (die, cu);
27aa8d6a 6350
794684b6
SW
6351 /* Figure out where the statement is being imported to. */
6352 import_prefix = determine_prefix (die, cu);
6353
6354 /* Figure out what the scope of the imported die is and prepend it
6355 to the name of the imported die. */
de4affc9 6356 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 6357
f55ee35c
JK
6358 if (imported_die->tag != DW_TAG_namespace
6359 && imported_die->tag != DW_TAG_module)
794684b6 6360 {
13387711
SW
6361 imported_declaration = imported_name;
6362 canonical_name = imported_name_prefix;
794684b6 6363 }
13387711 6364 else if (strlen (imported_name_prefix) > 0)
794684b6 6365 {
13387711
SW
6366 temp = alloca (strlen (imported_name_prefix)
6367 + 2 + strlen (imported_name) + 1);
6368 strcpy (temp, imported_name_prefix);
6369 strcat (temp, "::");
6370 strcat (temp, imported_name);
6371 canonical_name = temp;
794684b6 6372 }
13387711
SW
6373 else
6374 canonical_name = imported_name;
794684b6 6375
32019081
JK
6376 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6377
6378 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6379 for (child_die = die->child; child_die && child_die->tag;
6380 child_die = sibling_die (child_die))
6381 {
6382 /* DWARF-4: A Fortran use statement with a “rename list” may be
6383 represented by an imported module entry with an import attribute
6384 referring to the module and owned entries corresponding to those
6385 entities that are renamed as part of being imported. */
6386
6387 if (child_die->tag != DW_TAG_imported_declaration)
6388 {
6389 complaint (&symfile_complaints,
6390 _("child DW_TAG_imported_declaration expected "
6391 "- DIE at 0x%x [in module %s]"),
b64f50a1 6392 child_die->offset.sect_off, objfile->name);
32019081
JK
6393 continue;
6394 }
6395
6396 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6397 if (import_attr == NULL)
6398 {
6399 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6400 dwarf_tag_name (child_die->tag));
6401 continue;
6402 }
6403
6404 imported_cu = cu;
6405 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6406 &imported_cu);
6407 imported_name = dwarf2_name (imported_die, imported_cu);
6408 if (imported_name == NULL)
6409 {
6410 complaint (&symfile_complaints,
6411 _("child DW_TAG_imported_declaration has unknown "
6412 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 6413 child_die->offset.sect_off, objfile->name);
32019081
JK
6414 continue;
6415 }
6416
6417 VEC_safe_push (const_char_ptr, excludes, imported_name);
6418
6419 process_die (child_die, cu);
6420 }
6421
c0cc3a76
SW
6422 cp_add_using_directive (import_prefix,
6423 canonical_name,
6424 import_alias,
13387711 6425 imported_declaration,
32019081 6426 excludes,
bb5ed363 6427 &objfile->objfile_obstack);
32019081
JK
6428
6429 do_cleanups (cleanups);
27aa8d6a
SW
6430}
6431
ae2de4f8
DE
6432/* Cleanup function for read_file_scope. */
6433
cb1df416
DJ
6434static void
6435free_cu_line_header (void *arg)
6436{
6437 struct dwarf2_cu *cu = arg;
6438
6439 free_line_header (cu->line_header);
6440 cu->line_header = NULL;
6441}
6442
9291a0cd
TT
6443static void
6444find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6445 char **name, char **comp_dir)
6446{
6447 struct attribute *attr;
6448
6449 *name = NULL;
6450 *comp_dir = NULL;
6451
6452 /* Find the filename. Do not use dwarf2_name here, since the filename
6453 is not a source language identifier. */
6454 attr = dwarf2_attr (die, DW_AT_name, cu);
6455 if (attr)
6456 {
6457 *name = DW_STRING (attr);
6458 }
6459
6460 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6461 if (attr)
6462 *comp_dir = DW_STRING (attr);
6463 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6464 {
6465 *comp_dir = ldirname (*name);
6466 if (*comp_dir != NULL)
6467 make_cleanup (xfree, *comp_dir);
6468 }
6469 if (*comp_dir != NULL)
6470 {
6471 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6472 directory, get rid of it. */
6473 char *cp = strchr (*comp_dir, ':');
6474
6475 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6476 *comp_dir = cp + 1;
6477 }
6478
6479 if (*name == NULL)
6480 *name = "<unknown>";
6481}
6482
f3f5162e
DE
6483/* Handle DW_AT_stmt_list for a compilation unit or type unit.
6484 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6485 COMP_DIR is the compilation directory.
6486 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
6487
6488static void
6489handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 6490 const char *comp_dir, int want_line_info)
2ab95328
TT
6491{
6492 struct attribute *attr;
2ab95328 6493
2ab95328
TT
6494 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6495 if (attr)
6496 {
6497 unsigned int line_offset = DW_UNSND (attr);
6498 struct line_header *line_header
3019eac3 6499 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
6500
6501 if (line_header)
dee91e82
DE
6502 {
6503 cu->line_header = line_header;
6504 make_cleanup (free_cu_line_header, cu);
f3f5162e 6505 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
dee91e82 6506 }
2ab95328
TT
6507 }
6508}
6509
95554aad 6510/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 6511
c906108c 6512static void
e7c27a73 6513read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6514{
dee91e82 6515 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 6516 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 6517 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
6518 CORE_ADDR highpc = ((CORE_ADDR) 0);
6519 struct attribute *attr;
e1024ff1 6520 char *name = NULL;
c906108c
SS
6521 char *comp_dir = NULL;
6522 struct die_info *child_die;
6523 bfd *abfd = objfile->obfd;
e142c38c 6524 CORE_ADDR baseaddr;
6e70227d 6525
e142c38c 6526 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6527
fae299cd 6528 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
6529
6530 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6531 from finish_block. */
2acceee2 6532 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
6533 lowpc = highpc;
6534 lowpc += baseaddr;
6535 highpc += baseaddr;
6536
9291a0cd 6537 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 6538
95554aad 6539 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 6540
f4b8a18d
KW
6541 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6542 standardised yet. As a workaround for the language detection we fall
6543 back to the DW_AT_producer string. */
6544 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6545 cu->language = language_opencl;
6546
3019eac3
DE
6547 /* Similar hack for Go. */
6548 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6549 set_cu_language (DW_LANG_Go, cu);
6550
6551 /* We assume that we're processing GCC output. */
6552 processing_gcc_compilation = 2;
6553
6554 processing_has_namespace_info = 0;
6555
6556 start_symtab (name, comp_dir, lowpc);
6557 record_debugformat ("DWARF 2");
6558 record_producer (cu->producer);
6559
6560 /* Decode line number information if present. We do this before
6561 processing child DIEs, so that the line header table is available
6562 for DW_AT_decl_file. */
6563 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6564
6565 /* Process all dies in compilation unit. */
6566 if (die->child != NULL)
6567 {
6568 child_die = die->child;
6569 while (child_die && child_die->tag)
6570 {
6571 process_die (child_die, cu);
6572 child_die = sibling_die (child_die);
6573 }
6574 }
6575
6576 /* Decode macro information, if present. Dwarf 2 macro information
6577 refers to information in the line number info statement program
6578 header, so we can only read it if we've read the header
6579 successfully. */
6580 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6581 if (attr && cu->line_header)
6582 {
6583 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6584 complaint (&symfile_complaints,
6585 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6586
6587 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
6588 comp_dir, abfd, cu,
fceca515
DE
6589 &dwarf2_per_objfile->macro, 1,
6590 ".debug_macro");
3019eac3
DE
6591 }
6592 else
6593 {
6594 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6595 if (attr && cu->line_header)
6596 {
6597 unsigned int macro_offset = DW_UNSND (attr);
6598
6599 dwarf_decode_macros (cu->line_header, macro_offset,
6600 comp_dir, abfd, cu,
fceca515
DE
6601 &dwarf2_per_objfile->macinfo, 0,
6602 ".debug_macinfo");
3019eac3
DE
6603 }
6604 }
6605
6606 do_cleanups (back_to);
6607}
6608
6609/* Process DW_TAG_type_unit.
6610 For TUs we want to skip the first top level sibling if it's not the
6611 actual type being defined by this TU. In this case the first top
6612 level sibling is there to provide context only. */
6613
6614static void
6615read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6616{
6617 struct objfile *objfile = cu->objfile;
6618 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6619 CORE_ADDR lowpc;
6620 struct attribute *attr;
6621 char *name = NULL;
6622 char *comp_dir = NULL;
6623 struct die_info *child_die;
6624 bfd *abfd = objfile->obfd;
6625
6626 /* start_symtab needs a low pc, but we don't really have one.
6627 Do what read_file_scope would do in the absence of such info. */
6628 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6629
6630 /* Find the filename. Do not use dwarf2_name here, since the filename
6631 is not a source language identifier. */
6632 attr = dwarf2_attr (die, DW_AT_name, cu);
6633 if (attr)
6634 name = DW_STRING (attr);
6635
6636 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6637 if (attr)
6638 comp_dir = DW_STRING (attr);
6639 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6640 {
6641 comp_dir = ldirname (name);
6642 if (comp_dir != NULL)
6643 make_cleanup (xfree, comp_dir);
6644 }
6645
6646 if (name == NULL)
6647 name = "<unknown>";
6648
95554aad 6649 prepare_one_comp_unit (cu, die, language_minimal);
3019eac3
DE
6650
6651 /* We assume that we're processing GCC output. */
6652 processing_gcc_compilation = 2;
6653
6654 processing_has_namespace_info = 0;
6655
6656 start_symtab (name, comp_dir, lowpc);
6657 record_debugformat ("DWARF 2");
6658 record_producer (cu->producer);
6659
6660 /* Decode line number information if present. We do this before
6661 processing child DIEs, so that the line header table is available
6662 for DW_AT_decl_file.
6663 We don't need the pc/line-number mapping for type units. */
6664 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6665
6666 /* Process the dies in the type unit. */
6667 if (die->child == NULL)
6668 {
6669 dump_die_for_error (die);
6670 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6671 bfd_get_filename (abfd));
6672 }
6673
6674 child_die = die->child;
6675
6676 while (child_die && child_die->tag)
6677 {
6678 process_die (child_die, cu);
6679
6680 child_die = sibling_die (child_die);
6681 }
6682
6683 do_cleanups (back_to);
6684}
6685\f
6686/* DWO files. */
6687
6688static hashval_t
6689hash_dwo_file (const void *item)
6690{
6691 const struct dwo_file *dwo_file = item;
6692
6693 return htab_hash_string (dwo_file->dwo_name);
6694}
6695
6696static int
6697eq_dwo_file (const void *item_lhs, const void *item_rhs)
6698{
6699 const struct dwo_file *lhs = item_lhs;
6700 const struct dwo_file *rhs = item_rhs;
6701
6702 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6703}
6704
6705/* Allocate a hash table for DWO files. */
6706
6707static htab_t
6708allocate_dwo_file_hash_table (void)
6709{
6710 struct objfile *objfile = dwarf2_per_objfile->objfile;
6711
6712 return htab_create_alloc_ex (41,
6713 hash_dwo_file,
6714 eq_dwo_file,
6715 NULL,
6716 &objfile->objfile_obstack,
6717 hashtab_obstack_allocate,
6718 dummy_obstack_deallocate);
6719}
6720
6721static hashval_t
6722hash_dwo_unit (const void *item)
6723{
6724 const struct dwo_unit *dwo_unit = item;
6725
6726 /* This drops the top 32 bits of the id, but is ok for a hash. */
6727 return dwo_unit->signature;
6728}
6729
6730static int
6731eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6732{
6733 const struct dwo_unit *lhs = item_lhs;
6734 const struct dwo_unit *rhs = item_rhs;
6735
6736 /* The signature is assumed to be unique within the DWO file.
6737 So while object file CU dwo_id's always have the value zero,
6738 that's OK, assuming each object file DWO file has only one CU,
6739 and that's the rule for now. */
6740 return lhs->signature == rhs->signature;
6741}
6742
6743/* Allocate a hash table for DWO CUs,TUs.
6744 There is one of these tables for each of CUs,TUs for each DWO file. */
6745
6746static htab_t
6747allocate_dwo_unit_table (struct objfile *objfile)
6748{
6749 /* Start out with a pretty small number.
6750 Generally DWO files contain only one CU and maybe some TUs. */
6751 return htab_create_alloc_ex (3,
6752 hash_dwo_unit,
6753 eq_dwo_unit,
6754 NULL,
6755 &objfile->objfile_obstack,
6756 hashtab_obstack_allocate,
6757 dummy_obstack_deallocate);
6758}
6759
6760/* This function is mapped across the sections and remembers the offset and
6761 size of each of the DWO debugging sections we are interested in. */
6762
6763static void
6764dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6765{
6766 struct dwo_file *dwo_file = dwo_file_ptr;
6767 const struct dwo_section_names *names = &dwo_section_names;
6768
6769 if (section_is_p (sectp->name, &names->abbrev_dwo))
6770 {
6771 dwo_file->sections.abbrev.asection = sectp;
6772 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6773 }
6774 else if (section_is_p (sectp->name, &names->info_dwo))
6775 {
6776 dwo_file->sections.info.asection = sectp;
6777 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6778 }
6779 else if (section_is_p (sectp->name, &names->line_dwo))
6780 {
6781 dwo_file->sections.line.asection = sectp;
6782 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6783 }
6784 else if (section_is_p (sectp->name, &names->loc_dwo))
6785 {
6786 dwo_file->sections.loc.asection = sectp;
6787 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6788 }
6789 else if (section_is_p (sectp->name, &names->str_dwo))
6790 {
6791 dwo_file->sections.str.asection = sectp;
6792 dwo_file->sections.str.size = bfd_get_section_size (sectp);
6793 }
6794 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
6795 {
6796 dwo_file->sections.str_offsets.asection = sectp;
6797 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
6798 }
6799 else if (section_is_p (sectp->name, &names->types_dwo))
6800 {
6801 struct dwarf2_section_info type_section;
6802
6803 memset (&type_section, 0, sizeof (type_section));
6804 type_section.asection = sectp;
6805 type_section.size = bfd_get_section_size (sectp);
6806 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
6807 &type_section);
6808 }
6809}
6810
6811/* Structure used to pass data to create_debug_info_hash_table_reader. */
6812
6813struct create_dwo_info_table_data
6814{
6815 struct dwo_file *dwo_file;
6816 htab_t cu_htab;
6817};
6818
6819/* die_reader_func for create_debug_info_hash_table. */
6820
6821static void
6822create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
6823 gdb_byte *info_ptr,
6824 struct die_info *comp_unit_die,
6825 int has_children,
6826 void *datap)
6827{
6828 struct dwarf2_cu *cu = reader->cu;
6829 struct objfile *objfile = dwarf2_per_objfile->objfile;
6830 sect_offset offset = cu->per_cu->offset;
6831 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
6832 struct create_dwo_info_table_data *data = datap;
6833 struct dwo_file *dwo_file = data->dwo_file;
6834 htab_t cu_htab = data->cu_htab;
6835 void **slot;
6836 struct attribute *attr;
6837 struct dwo_unit *dwo_unit;
6838
6839 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6840 if (attr == NULL)
6841 {
6842 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
6843 " its dwo_id [in module %s]"),
6844 offset.sect_off, dwo_file->dwo_name);
6845 return;
6846 }
6847
6848 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
6849 dwo_unit->dwo_file = dwo_file;
6850 dwo_unit->signature = DW_UNSND (attr);
6851 dwo_unit->info_or_types_section = section;
6852 dwo_unit->offset = offset;
6853 dwo_unit->length = cu->per_cu->length;
6854
6855 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
6856 gdb_assert (slot != NULL);
6857 if (*slot != NULL)
6858 {
6859 const struct dwo_unit *dup_dwo_unit = *slot;
6860
6861 complaint (&symfile_complaints,
6862 _("debug entry at offset 0x%x is duplicate to the entry at"
6863 " offset 0x%x, dwo_id 0x%s [in module %s]"),
6864 offset.sect_off, dup_dwo_unit->offset.sect_off,
6865 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
6866 dwo_file->dwo_name);
6867 }
6868 else
6869 *slot = dwo_unit;
6870
6871 if (dwarf2_die_debug)
6872 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
6873 offset.sect_off,
6874 phex (dwo_unit->signature,
6875 sizeof (dwo_unit->signature)));
6876}
6877
6878/* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
6879
6880static htab_t
6881create_debug_info_hash_table (struct dwo_file *dwo_file)
6882{
6883 struct objfile *objfile = dwarf2_per_objfile->objfile;
6884 struct dwarf2_section_info *section = &dwo_file->sections.info;
6885 bfd *abfd;
6886 htab_t cu_htab;
6887 gdb_byte *info_ptr, *end_ptr;
6888 struct create_dwo_info_table_data create_dwo_info_table_data;
6889
6890 dwarf2_read_section (objfile, section);
6891 info_ptr = section->buffer;
6892
6893 if (info_ptr == NULL)
6894 return NULL;
6895
6896 /* We can't set abfd until now because the section may be empty or
6897 not present, in which case section->asection will be NULL. */
6898 abfd = section->asection->owner;
6899
6900 if (dwarf2_die_debug)
6901 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
6902 bfd_get_filename (abfd));
6903
6904 cu_htab = allocate_dwo_unit_table (objfile);
6905
6906 create_dwo_info_table_data.dwo_file = dwo_file;
6907 create_dwo_info_table_data.cu_htab = cu_htab;
6908
6909 end_ptr = info_ptr + section->size;
6910 while (info_ptr < end_ptr)
6911 {
6912 struct dwarf2_per_cu_data per_cu;
6913
6914 memset (&per_cu, 0, sizeof (per_cu));
6915 per_cu.objfile = objfile;
6916 per_cu.is_debug_types = 0;
6917 per_cu.offset.sect_off = info_ptr - section->buffer;
6918 per_cu.info_or_types_section = section;
6919
6920 init_cutu_and_read_dies_no_follow (&per_cu,
6921 &dwo_file->sections.abbrev,
6922 dwo_file,
6923 create_debug_info_hash_table_reader,
6924 &create_dwo_info_table_data);
6925
6926 info_ptr += per_cu.length;
6927 }
6928
6929 return cu_htab;
6930}
6931
6932/* Subroutine of open_dwo_file to simplify it.
6933 Open the file specified by FILE_NAME and hand it off to BFD for
6934 preliminary analysis. Return a newly initialized bfd *, which
6935 includes a canonicalized copy of FILE_NAME.
6936 In case of trouble, return NULL.
6937 NOTE: This function is derived from symfile_bfd_open. */
6938
6939static bfd *
6940try_open_dwo_file (const char *file_name)
6941{
6942 bfd *sym_bfd;
6943 int desc;
6944 char *absolute_name;
6945 char *name;
6946
6947 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
6948 O_RDONLY | O_BINARY, &absolute_name);
6949 if (desc < 0)
6950 return NULL;
6951
6952 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
6953 if (!sym_bfd)
6954 {
6955 close (desc);
6956 xfree (absolute_name);
6957 return NULL;
6958 }
6959 bfd_set_cacheable (sym_bfd, 1);
6960
6961 if (!bfd_check_format (sym_bfd, bfd_object))
6962 {
6963 bfd_close (sym_bfd); /* This also closes desc. */
6964 xfree (absolute_name);
6965 return NULL;
6966 }
6967
6968 /* bfd_usrdata exists for applications and libbfd must not touch it. */
6969 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
6970
6971 return sym_bfd;
6972}
6973
6974/* Try to open DWO file DWO_NAME.
6975 COMP_DIR is the DW_AT_comp_dir attribute.
6976 The result is the bfd handle of the file.
6977 If there is a problem finding or opening the file, return NULL.
6978 Upon success, the canonicalized path of the file is stored in the bfd,
6979 same as symfile_bfd_open. */
6980
6981static bfd *
6982open_dwo_file (const char *dwo_name, const char *comp_dir)
6983{
6984 bfd *abfd;
6985 char *path_to_try, *debug_dir;
6986
6987 if (IS_ABSOLUTE_PATH (dwo_name))
6988 return try_open_dwo_file (dwo_name);
6989
6990 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
6991
6992 if (comp_dir != NULL)
6993 {
6994 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
6995
6996 /* NOTE: If comp_dir is a relative path, this will also try the
6997 search path, which seems useful. */
6998 abfd = try_open_dwo_file (path_to_try);
6999 xfree (path_to_try);
7000 if (abfd != NULL)
7001 return abfd;
7002 }
7003
7004 /* That didn't work, try debug-file-directory, which, despite its name,
7005 is a list of paths. */
7006
7007 if (*debug_file_directory == '\0')
7008 return NULL;
7009
7010 return try_open_dwo_file (dwo_name);
7011}
7012
7013/* Initialize the use of the DWO file specified by DWO_NAME. */
7014
7015static struct dwo_file *
7016init_dwo_file (const char *dwo_name, const char *comp_dir)
7017{
7018 struct objfile *objfile = dwarf2_per_objfile->objfile;
7019 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7020 struct dwo_file);
7021 bfd *abfd;
7022 struct cleanup *cleanups;
7023
7024 if (dwarf2_die_debug)
7025 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7026
7027 abfd = open_dwo_file (dwo_name, comp_dir);
7028 if (abfd == NULL)
7029 return NULL;
7030 dwo_file->dwo_name = dwo_name;
7031 dwo_file->dwo_bfd = abfd;
7032
7033 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7034
7035 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7036
7037 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7038
7039 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7040 dwo_file->sections.types);
7041
7042 discard_cleanups (cleanups);
7043
7044 return dwo_file;
7045}
7046
7047/* Lookup DWO file DWO_NAME. */
7048
7049static struct dwo_file *
7050lookup_dwo_file (char *dwo_name, const char *comp_dir)
7051{
7052 struct dwo_file *dwo_file;
7053 struct dwo_file find_entry;
7054 void **slot;
7055
7056 if (dwarf2_per_objfile->dwo_files == NULL)
7057 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7058
7059 /* Have we already seen this DWO file? */
7060 find_entry.dwo_name = dwo_name;
7061 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7062
7063 /* If not, read it in and build a table of the DWOs it contains. */
7064 if (*slot == NULL)
7065 *slot = init_dwo_file (dwo_name, comp_dir);
7066
7067 /* NOTE: This will be NULL if unable to open the file. */
7068 dwo_file = *slot;
7069
7070 return dwo_file;
7071}
7072
7073/* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7074 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7075 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7076 nomenclature as TUs).
7077 The result is the DWO CU or NULL if we didn't find it
7078 (dwo_id mismatch or couldn't find the DWO file). */
7079
7080static struct dwo_unit *
7081lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7082 char *dwo_name, const char *comp_dir,
7083 ULONGEST signature)
7084{
7085 struct objfile *objfile = dwarf2_per_objfile->objfile;
7086 struct dwo_file *dwo_file;
7087
7088 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7089 if (dwo_file == NULL)
7090 return NULL;
7091
7092 /* Look up the DWO using its signature(dwo_id). */
7093
7094 if (dwo_file->cus != NULL)
7095 {
7096 struct dwo_unit find_dwo_cu, *dwo_cu;
7097
7098 find_dwo_cu.signature = signature;
7099 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
a766d390 7100
3019eac3
DE
7101 if (dwo_cu != NULL)
7102 return dwo_cu;
7103 }
c906108c 7104
3019eac3 7105 /* We didn't find it. This must mean a dwo_id mismatch. */
df8a16a1 7106
3019eac3
DE
7107 complaint (&symfile_complaints,
7108 _("Could not find DWO CU referenced by CU at offset 0x%x"
7109 " [in module %s]"),
7110 this_cu->offset.sect_off, objfile->name);
7111 return NULL;
7112}
c906108c 7113
3019eac3
DE
7114/* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7115 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7116 The result is the DWO CU or NULL if we didn't find it
7117 (dwo_id mismatch or couldn't find the DWO file). */
debd256d 7118
3019eac3
DE
7119static struct dwo_unit *
7120lookup_dwo_type_unit (struct signatured_type *this_tu,
7121 char *dwo_name, const char *comp_dir)
7122{
7123 struct objfile *objfile = dwarf2_per_objfile->objfile;
7124 struct dwo_file *dwo_file;
7125 struct dwo_unit find_dwo_tu, *dwo_tu;
cb1df416 7126
3019eac3
DE
7127 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7128 if (dwo_file == NULL)
7129 return NULL;
cf2c3c16 7130
3019eac3
DE
7131 /* Look up the DWO using its signature(dwo_id). */
7132
7133 if (dwo_file->tus != NULL)
cf2c3c16 7134 {
3019eac3 7135 struct dwo_unit find_dwo_tu, *dwo_tu;
9a619af0 7136
3019eac3
DE
7137 find_dwo_tu.signature = this_tu->signature;
7138 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7139
7140 if (dwo_tu != NULL)
7141 return dwo_tu;
2e276125 7142 }
9cdd5dbd 7143
3019eac3
DE
7144 /* We didn't find it. This must mean a dwo_id mismatch. */
7145
7146 complaint (&symfile_complaints,
7147 _("Could not find DWO TU referenced by TU at offset 0x%x"
7148 " [in module %s]"),
7149 this_tu->per_cu.offset.sect_off, objfile->name);
7150 return NULL;
5fb290d7
DJ
7151}
7152
3019eac3
DE
7153/* Free all resources associated with DWO_FILE.
7154 Close the DWO file and munmap the sections.
7155 All memory should be on the objfile obstack. */
348e048f
DE
7156
7157static void
3019eac3 7158free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 7159{
3019eac3
DE
7160 int ix;
7161 struct dwarf2_section_info *section;
348e048f 7162
3019eac3
DE
7163 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7164 bfd_close (dwo_file->dwo_bfd);
348e048f 7165
3019eac3
DE
7166 munmap_section_buffer (&dwo_file->sections.abbrev);
7167 munmap_section_buffer (&dwo_file->sections.info);
7168 munmap_section_buffer (&dwo_file->sections.line);
7169 munmap_section_buffer (&dwo_file->sections.loc);
7170 munmap_section_buffer (&dwo_file->sections.str);
7171 munmap_section_buffer (&dwo_file->sections.str_offsets);
348e048f 7172
3019eac3
DE
7173 for (ix = 0;
7174 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7175 ix, section);
7176 ++ix)
7177 munmap_section_buffer (section);
348e048f 7178
3019eac3
DE
7179 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7180}
348e048f 7181
3019eac3 7182/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 7183
3019eac3
DE
7184static void
7185free_dwo_file_cleanup (void *arg)
7186{
7187 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7188 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 7189
3019eac3
DE
7190 free_dwo_file (dwo_file, objfile);
7191}
348e048f 7192
3019eac3 7193/* Traversal function for free_dwo_files. */
2ab95328 7194
3019eac3
DE
7195static int
7196free_dwo_file_from_slot (void **slot, void *info)
7197{
7198 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7199 struct objfile *objfile = (struct objfile *) info;
348e048f 7200
3019eac3 7201 free_dwo_file (dwo_file, objfile);
348e048f 7202
3019eac3
DE
7203 return 1;
7204}
348e048f 7205
3019eac3 7206/* Free all resources associated with DWO_FILES. */
348e048f 7207
3019eac3
DE
7208static void
7209free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7210{
7211 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 7212}
3019eac3
DE
7213\f
7214/* Read in various DIEs. */
348e048f 7215
d389af10
JK
7216/* qsort helper for inherit_abstract_dies. */
7217
7218static int
7219unsigned_int_compar (const void *ap, const void *bp)
7220{
7221 unsigned int a = *(unsigned int *) ap;
7222 unsigned int b = *(unsigned int *) bp;
7223
7224 return (a > b) - (b > a);
7225}
7226
7227/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
7228 Inherit only the children of the DW_AT_abstract_origin DIE not being
7229 already referenced by DW_AT_abstract_origin from the children of the
7230 current DIE. */
d389af10
JK
7231
7232static void
7233inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7234{
7235 struct die_info *child_die;
7236 unsigned die_children_count;
7237 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
7238 sect_offset *offsets;
7239 sect_offset *offsets_end, *offsetp;
d389af10
JK
7240 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7241 struct die_info *origin_die;
7242 /* Iterator of the ORIGIN_DIE children. */
7243 struct die_info *origin_child_die;
7244 struct cleanup *cleanups;
7245 struct attribute *attr;
cd02d79d
PA
7246 struct dwarf2_cu *origin_cu;
7247 struct pending **origin_previous_list_in_scope;
d389af10
JK
7248
7249 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7250 if (!attr)
7251 return;
7252
cd02d79d
PA
7253 /* Note that following die references may follow to a die in a
7254 different cu. */
7255
7256 origin_cu = cu;
7257 origin_die = follow_die_ref (die, attr, &origin_cu);
7258
7259 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7260 symbols in. */
7261 origin_previous_list_in_scope = origin_cu->list_in_scope;
7262 origin_cu->list_in_scope = cu->list_in_scope;
7263
edb3359d
DJ
7264 if (die->tag != origin_die->tag
7265 && !(die->tag == DW_TAG_inlined_subroutine
7266 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7267 complaint (&symfile_complaints,
7268 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 7269 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
7270
7271 child_die = die->child;
7272 die_children_count = 0;
7273 while (child_die && child_die->tag)
7274 {
7275 child_die = sibling_die (child_die);
7276 die_children_count++;
7277 }
7278 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7279 cleanups = make_cleanup (xfree, offsets);
7280
7281 offsets_end = offsets;
7282 child_die = die->child;
7283 while (child_die && child_die->tag)
7284 {
c38f313d
DJ
7285 /* For each CHILD_DIE, find the corresponding child of
7286 ORIGIN_DIE. If there is more than one layer of
7287 DW_AT_abstract_origin, follow them all; there shouldn't be,
7288 but GCC versions at least through 4.4 generate this (GCC PR
7289 40573). */
7290 struct die_info *child_origin_die = child_die;
cd02d79d 7291 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 7292
c38f313d
DJ
7293 while (1)
7294 {
cd02d79d
PA
7295 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7296 child_origin_cu);
c38f313d
DJ
7297 if (attr == NULL)
7298 break;
cd02d79d
PA
7299 child_origin_die = follow_die_ref (child_origin_die, attr,
7300 &child_origin_cu);
c38f313d
DJ
7301 }
7302
d389af10
JK
7303 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7304 counterpart may exist. */
c38f313d 7305 if (child_origin_die != child_die)
d389af10 7306 {
edb3359d
DJ
7307 if (child_die->tag != child_origin_die->tag
7308 && !(child_die->tag == DW_TAG_inlined_subroutine
7309 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7310 complaint (&symfile_complaints,
7311 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7312 "different tags"), child_die->offset.sect_off,
7313 child_origin_die->offset.sect_off);
c38f313d
DJ
7314 if (child_origin_die->parent != origin_die)
7315 complaint (&symfile_complaints,
7316 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7317 "different parents"), child_die->offset.sect_off,
7318 child_origin_die->offset.sect_off);
c38f313d
DJ
7319 else
7320 *offsets_end++ = child_origin_die->offset;
d389af10
JK
7321 }
7322 child_die = sibling_die (child_die);
7323 }
7324 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7325 unsigned_int_compar);
7326 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 7327 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
7328 complaint (&symfile_complaints,
7329 _("Multiple children of DIE 0x%x refer "
7330 "to DIE 0x%x as their abstract origin"),
b64f50a1 7331 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
7332
7333 offsetp = offsets;
7334 origin_child_die = origin_die->child;
7335 while (origin_child_die && origin_child_die->tag)
7336 {
7337 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
7338 while (offsetp < offsets_end
7339 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 7340 offsetp++;
b64f50a1
JK
7341 if (offsetp >= offsets_end
7342 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
7343 {
7344 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 7345 process_die (origin_child_die, origin_cu);
d389af10
JK
7346 }
7347 origin_child_die = sibling_die (origin_child_die);
7348 }
cd02d79d 7349 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
7350
7351 do_cleanups (cleanups);
7352}
7353
c906108c 7354static void
e7c27a73 7355read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7356{
e7c27a73 7357 struct objfile *objfile = cu->objfile;
52f0bd74 7358 struct context_stack *new;
c906108c
SS
7359 CORE_ADDR lowpc;
7360 CORE_ADDR highpc;
7361 struct die_info *child_die;
edb3359d 7362 struct attribute *attr, *call_line, *call_file;
c906108c 7363 char *name;
e142c38c 7364 CORE_ADDR baseaddr;
801e3a5b 7365 struct block *block;
edb3359d 7366 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
7367 VEC (symbolp) *template_args = NULL;
7368 struct template_symbol *templ_func = NULL;
edb3359d
DJ
7369
7370 if (inlined_func)
7371 {
7372 /* If we do not have call site information, we can't show the
7373 caller of this inlined function. That's too confusing, so
7374 only use the scope for local variables. */
7375 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7376 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7377 if (call_line == NULL || call_file == NULL)
7378 {
7379 read_lexical_block_scope (die, cu);
7380 return;
7381 }
7382 }
c906108c 7383
e142c38c
DJ
7384 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7385
94af9270 7386 name = dwarf2_name (die, cu);
c906108c 7387
e8d05480
JB
7388 /* Ignore functions with missing or empty names. These are actually
7389 illegal according to the DWARF standard. */
7390 if (name == NULL)
7391 {
7392 complaint (&symfile_complaints,
b64f50a1
JK
7393 _("missing name for subprogram DIE at %d"),
7394 die->offset.sect_off);
e8d05480
JB
7395 return;
7396 }
7397
7398 /* Ignore functions with missing or invalid low and high pc attributes. */
7399 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7400 {
ae4d0c03
PM
7401 attr = dwarf2_attr (die, DW_AT_external, cu);
7402 if (!attr || !DW_UNSND (attr))
7403 complaint (&symfile_complaints,
3e43a32a
MS
7404 _("cannot get low and high bounds "
7405 "for subprogram DIE at %d"),
b64f50a1 7406 die->offset.sect_off);
e8d05480
JB
7407 return;
7408 }
c906108c
SS
7409
7410 lowpc += baseaddr;
7411 highpc += baseaddr;
7412
34eaf542
TT
7413 /* If we have any template arguments, then we must allocate a
7414 different sort of symbol. */
7415 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7416 {
7417 if (child_die->tag == DW_TAG_template_type_param
7418 || child_die->tag == DW_TAG_template_value_param)
7419 {
7420 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7421 struct template_symbol);
7422 templ_func->base.is_cplus_template_function = 1;
7423 break;
7424 }
7425 }
7426
c906108c 7427 new = push_context (0, lowpc);
34eaf542
TT
7428 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7429 (struct symbol *) templ_func);
4c2df51b 7430
4cecd739
DJ
7431 /* If there is a location expression for DW_AT_frame_base, record
7432 it. */
e142c38c 7433 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 7434 if (attr)
c034e007
AC
7435 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7436 expression is being recorded directly in the function's symbol
7437 and not in a separate frame-base object. I guess this hack is
7438 to avoid adding some sort of frame-base adjunct/annex to the
7439 function's symbol :-(. The problem with doing this is that it
7440 results in a function symbol with a location expression that
7441 has nothing to do with the location of the function, ouch! The
7442 relationship should be: a function's symbol has-a frame base; a
7443 frame-base has-a location expression. */
e7c27a73 7444 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 7445
e142c38c 7446 cu->list_in_scope = &local_symbols;
c906108c 7447
639d11d3 7448 if (die->child != NULL)
c906108c 7449 {
639d11d3 7450 child_die = die->child;
c906108c
SS
7451 while (child_die && child_die->tag)
7452 {
34eaf542
TT
7453 if (child_die->tag == DW_TAG_template_type_param
7454 || child_die->tag == DW_TAG_template_value_param)
7455 {
7456 struct symbol *arg = new_symbol (child_die, NULL, cu);
7457
f1078f66
DJ
7458 if (arg != NULL)
7459 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7460 }
7461 else
7462 process_die (child_die, cu);
c906108c
SS
7463 child_die = sibling_die (child_die);
7464 }
7465 }
7466
d389af10
JK
7467 inherit_abstract_dies (die, cu);
7468
4a811a97
UW
7469 /* If we have a DW_AT_specification, we might need to import using
7470 directives from the context of the specification DIE. See the
7471 comment in determine_prefix. */
7472 if (cu->language == language_cplus
7473 && dwarf2_attr (die, DW_AT_specification, cu))
7474 {
7475 struct dwarf2_cu *spec_cu = cu;
7476 struct die_info *spec_die = die_specification (die, &spec_cu);
7477
7478 while (spec_die)
7479 {
7480 child_die = spec_die->child;
7481 while (child_die && child_die->tag)
7482 {
7483 if (child_die->tag == DW_TAG_imported_module)
7484 process_die (child_die, spec_cu);
7485 child_die = sibling_die (child_die);
7486 }
7487
7488 /* In some cases, GCC generates specification DIEs that
7489 themselves contain DW_AT_specification attributes. */
7490 spec_die = die_specification (spec_die, &spec_cu);
7491 }
7492 }
7493
c906108c
SS
7494 new = pop_context ();
7495 /* Make a block for the local symbols within. */
801e3a5b
JB
7496 block = finish_block (new->name, &local_symbols, new->old_blocks,
7497 lowpc, highpc, objfile);
7498
df8a16a1 7499 /* For C++, set the block's scope. */
f55ee35c 7500 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 7501 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 7502 determine_prefix (die, cu),
df8a16a1
DJ
7503 processing_has_namespace_info);
7504
801e3a5b
JB
7505 /* If we have address ranges, record them. */
7506 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 7507
34eaf542
TT
7508 /* Attach template arguments to function. */
7509 if (! VEC_empty (symbolp, template_args))
7510 {
7511 gdb_assert (templ_func != NULL);
7512
7513 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7514 templ_func->template_arguments
7515 = obstack_alloc (&objfile->objfile_obstack,
7516 (templ_func->n_template_arguments
7517 * sizeof (struct symbol *)));
7518 memcpy (templ_func->template_arguments,
7519 VEC_address (symbolp, template_args),
7520 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7521 VEC_free (symbolp, template_args);
7522 }
7523
208d8187
JB
7524 /* In C++, we can have functions nested inside functions (e.g., when
7525 a function declares a class that has methods). This means that
7526 when we finish processing a function scope, we may need to go
7527 back to building a containing block's symbol lists. */
7528 local_symbols = new->locals;
7529 param_symbols = new->params;
27aa8d6a 7530 using_directives = new->using_directives;
208d8187 7531
921e78cf
JB
7532 /* If we've finished processing a top-level function, subsequent
7533 symbols go in the file symbol list. */
7534 if (outermost_context_p ())
e142c38c 7535 cu->list_in_scope = &file_symbols;
c906108c
SS
7536}
7537
7538/* Process all the DIES contained within a lexical block scope. Start
7539 a new scope, process the dies, and then close the scope. */
7540
7541static void
e7c27a73 7542read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7543{
e7c27a73 7544 struct objfile *objfile = cu->objfile;
52f0bd74 7545 struct context_stack *new;
c906108c
SS
7546 CORE_ADDR lowpc, highpc;
7547 struct die_info *child_die;
e142c38c
DJ
7548 CORE_ADDR baseaddr;
7549
7550 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
7551
7552 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
7553 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7554 as multiple lexical blocks? Handling children in a sane way would
6e70227d 7555 be nasty. Might be easier to properly extend generic blocks to
af34e669 7556 describe ranges. */
d85a05f0 7557 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
7558 return;
7559 lowpc += baseaddr;
7560 highpc += baseaddr;
7561
7562 push_context (0, lowpc);
639d11d3 7563 if (die->child != NULL)
c906108c 7564 {
639d11d3 7565 child_die = die->child;
c906108c
SS
7566 while (child_die && child_die->tag)
7567 {
e7c27a73 7568 process_die (child_die, cu);
c906108c
SS
7569 child_die = sibling_die (child_die);
7570 }
7571 }
7572 new = pop_context ();
7573
8540c487 7574 if (local_symbols != NULL || using_directives != NULL)
c906108c 7575 {
801e3a5b
JB
7576 struct block *block
7577 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7578 highpc, objfile);
7579
7580 /* Note that recording ranges after traversing children, as we
7581 do here, means that recording a parent's ranges entails
7582 walking across all its children's ranges as they appear in
7583 the address map, which is quadratic behavior.
7584
7585 It would be nicer to record the parent's ranges before
7586 traversing its children, simply overriding whatever you find
7587 there. But since we don't even decide whether to create a
7588 block until after we've traversed its children, that's hard
7589 to do. */
7590 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
7591 }
7592 local_symbols = new->locals;
27aa8d6a 7593 using_directives = new->using_directives;
c906108c
SS
7594}
7595
96408a79
SA
7596/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7597
7598static void
7599read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7600{
7601 struct objfile *objfile = cu->objfile;
7602 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7603 CORE_ADDR pc, baseaddr;
7604 struct attribute *attr;
7605 struct call_site *call_site, call_site_local;
7606 void **slot;
7607 int nparams;
7608 struct die_info *child_die;
7609
7610 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7611
7612 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7613 if (!attr)
7614 {
7615 complaint (&symfile_complaints,
7616 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7617 "DIE 0x%x [in module %s]"),
b64f50a1 7618 die->offset.sect_off, objfile->name);
96408a79
SA
7619 return;
7620 }
7621 pc = DW_ADDR (attr) + baseaddr;
7622
7623 if (cu->call_site_htab == NULL)
7624 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7625 NULL, &objfile->objfile_obstack,
7626 hashtab_obstack_allocate, NULL);
7627 call_site_local.pc = pc;
7628 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7629 if (*slot != NULL)
7630 {
7631 complaint (&symfile_complaints,
7632 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7633 "DIE 0x%x [in module %s]"),
b64f50a1 7634 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
7635 return;
7636 }
7637
7638 /* Count parameters at the caller. */
7639
7640 nparams = 0;
7641 for (child_die = die->child; child_die && child_die->tag;
7642 child_die = sibling_die (child_die))
7643 {
7644 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7645 {
7646 complaint (&symfile_complaints,
7647 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7648 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7649 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
7650 continue;
7651 }
7652
7653 nparams++;
7654 }
7655
7656 call_site = obstack_alloc (&objfile->objfile_obstack,
7657 (sizeof (*call_site)
7658 + (sizeof (*call_site->parameter)
7659 * (nparams - 1))));
7660 *slot = call_site;
7661 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7662 call_site->pc = pc;
7663
7664 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7665 {
7666 struct die_info *func_die;
7667
7668 /* Skip also over DW_TAG_inlined_subroutine. */
7669 for (func_die = die->parent;
7670 func_die && func_die->tag != DW_TAG_subprogram
7671 && func_die->tag != DW_TAG_subroutine_type;
7672 func_die = func_die->parent);
7673
7674 /* DW_AT_GNU_all_call_sites is a superset
7675 of DW_AT_GNU_all_tail_call_sites. */
7676 if (func_die
7677 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7678 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7679 {
7680 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7681 not complete. But keep CALL_SITE for look ups via call_site_htab,
7682 both the initial caller containing the real return address PC and
7683 the final callee containing the current PC of a chain of tail
7684 calls do not need to have the tail call list complete. But any
7685 function candidate for a virtual tail call frame searched via
7686 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7687 determined unambiguously. */
7688 }
7689 else
7690 {
7691 struct type *func_type = NULL;
7692
7693 if (func_die)
7694 func_type = get_die_type (func_die, cu);
7695 if (func_type != NULL)
7696 {
7697 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7698
7699 /* Enlist this call site to the function. */
7700 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7701 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7702 }
7703 else
7704 complaint (&symfile_complaints,
7705 _("Cannot find function owning DW_TAG_GNU_call_site "
7706 "DIE 0x%x [in module %s]"),
b64f50a1 7707 die->offset.sect_off, objfile->name);
96408a79
SA
7708 }
7709 }
7710
7711 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7712 if (attr == NULL)
7713 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7714 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7715 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7716 /* Keep NULL DWARF_BLOCK. */;
7717 else if (attr_form_is_block (attr))
7718 {
7719 struct dwarf2_locexpr_baton *dlbaton;
7720
7721 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7722 dlbaton->data = DW_BLOCK (attr)->data;
7723 dlbaton->size = DW_BLOCK (attr)->size;
7724 dlbaton->per_cu = cu->per_cu;
7725
7726 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7727 }
7728 else if (is_ref_attr (attr))
7729 {
96408a79
SA
7730 struct dwarf2_cu *target_cu = cu;
7731 struct die_info *target_die;
7732
7733 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7734 gdb_assert (target_cu->objfile == objfile);
7735 if (die_is_declaration (target_die, target_cu))
7736 {
7737 const char *target_physname;
7738
7739 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7740 if (target_physname == NULL)
7741 complaint (&symfile_complaints,
7742 _("DW_AT_GNU_call_site_target target DIE has invalid "
7743 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 7744 die->offset.sect_off, objfile->name);
96408a79
SA
7745 else
7746 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7747 }
7748 else
7749 {
7750 CORE_ADDR lowpc;
7751
7752 /* DW_AT_entry_pc should be preferred. */
7753 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7754 complaint (&symfile_complaints,
7755 _("DW_AT_GNU_call_site_target target DIE has invalid "
7756 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 7757 die->offset.sect_off, objfile->name);
96408a79
SA
7758 else
7759 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7760 }
7761 }
7762 else
7763 complaint (&symfile_complaints,
7764 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7765 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 7766 die->offset.sect_off, objfile->name);
96408a79
SA
7767
7768 call_site->per_cu = cu->per_cu;
7769
7770 for (child_die = die->child;
7771 child_die && child_die->tag;
7772 child_die = sibling_die (child_die))
7773 {
7774 struct dwarf2_locexpr_baton *dlbaton;
7775 struct call_site_parameter *parameter;
7776
7777 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7778 {
7779 /* Already printed the complaint above. */
7780 continue;
7781 }
7782
7783 gdb_assert (call_site->parameter_count < nparams);
7784 parameter = &call_site->parameter[call_site->parameter_count];
7785
7786 /* DW_AT_location specifies the register number. Value of the data
7787 assumed for the register is contained in DW_AT_GNU_call_site_value. */
7788
7789 attr = dwarf2_attr (child_die, DW_AT_location, cu);
7790 if (!attr || !attr_form_is_block (attr))
7791 {
7792 complaint (&symfile_complaints,
7793 _("No DW_FORM_block* DW_AT_location for "
7794 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7795 child_die->offset.sect_off, objfile->name);
96408a79
SA
7796 continue;
7797 }
7798 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
7799 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
7800 if (parameter->dwarf_reg == -1
7801 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
7802 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
7803 &parameter->fb_offset))
7804 {
7805 complaint (&symfile_complaints,
7806 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
7807 "for DW_FORM_block* DW_AT_location for "
7808 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7809 child_die->offset.sect_off, objfile->name);
96408a79
SA
7810 continue;
7811 }
7812
7813 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
7814 if (!attr_form_is_block (attr))
7815 {
7816 complaint (&symfile_complaints,
7817 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
7818 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7819 child_die->offset.sect_off, objfile->name);
96408a79
SA
7820 continue;
7821 }
7822 parameter->value = DW_BLOCK (attr)->data;
7823 parameter->value_size = DW_BLOCK (attr)->size;
7824
7825 /* Parameters are not pre-cleared by memset above. */
7826 parameter->data_value = NULL;
7827 parameter->data_value_size = 0;
7828 call_site->parameter_count++;
7829
7830 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
7831 if (attr)
7832 {
7833 if (!attr_form_is_block (attr))
7834 complaint (&symfile_complaints,
7835 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
7836 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7837 child_die->offset.sect_off, objfile->name);
96408a79
SA
7838 else
7839 {
7840 parameter->data_value = DW_BLOCK (attr)->data;
7841 parameter->data_value_size = DW_BLOCK (attr)->size;
7842 }
7843 }
7844 }
7845}
7846
43039443 7847/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
7848 Return 1 if the attributes are present and valid, otherwise, return 0.
7849 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
7850
7851static int
7852dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
7853 CORE_ADDR *high_return, struct dwarf2_cu *cu,
7854 struct partial_symtab *ranges_pst)
43039443
JK
7855{
7856 struct objfile *objfile = cu->objfile;
7857 struct comp_unit_head *cu_header = &cu->header;
7858 bfd *obfd = objfile->obfd;
7859 unsigned int addr_size = cu_header->addr_size;
7860 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7861 /* Base address selection entry. */
7862 CORE_ADDR base;
7863 int found_base;
7864 unsigned int dummy;
7865 gdb_byte *buffer;
7866 CORE_ADDR marker;
7867 int low_set;
7868 CORE_ADDR low = 0;
7869 CORE_ADDR high = 0;
ff013f42 7870 CORE_ADDR baseaddr;
43039443 7871
d00adf39
DE
7872 found_base = cu->base_known;
7873 base = cu->base_address;
43039443 7874
be391dca 7875 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 7876 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
7877 {
7878 complaint (&symfile_complaints,
7879 _("Offset %d out of bounds for DW_AT_ranges attribute"),
7880 offset);
7881 return 0;
7882 }
dce234bc 7883 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
7884
7885 /* Read in the largest possible address. */
7886 marker = read_address (obfd, buffer, cu, &dummy);
7887 if ((marker & mask) == mask)
7888 {
7889 /* If we found the largest possible address, then
7890 read the base address. */
7891 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7892 buffer += 2 * addr_size;
7893 offset += 2 * addr_size;
7894 found_base = 1;
7895 }
7896
7897 low_set = 0;
7898
e7030f15 7899 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 7900
43039443
JK
7901 while (1)
7902 {
7903 CORE_ADDR range_beginning, range_end;
7904
7905 range_beginning = read_address (obfd, buffer, cu, &dummy);
7906 buffer += addr_size;
7907 range_end = read_address (obfd, buffer, cu, &dummy);
7908 buffer += addr_size;
7909 offset += 2 * addr_size;
7910
7911 /* An end of list marker is a pair of zero addresses. */
7912 if (range_beginning == 0 && range_end == 0)
7913 /* Found the end of list entry. */
7914 break;
7915
7916 /* Each base address selection entry is a pair of 2 values.
7917 The first is the largest possible address, the second is
7918 the base address. Check for a base address here. */
7919 if ((range_beginning & mask) == mask)
7920 {
7921 /* If we found the largest possible address, then
7922 read the base address. */
7923 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7924 found_base = 1;
7925 continue;
7926 }
7927
7928 if (!found_base)
7929 {
7930 /* We have no valid base address for the ranges
7931 data. */
7932 complaint (&symfile_complaints,
7933 _("Invalid .debug_ranges data (no base address)"));
7934 return 0;
7935 }
7936
9277c30c
UW
7937 if (range_beginning > range_end)
7938 {
7939 /* Inverted range entries are invalid. */
7940 complaint (&symfile_complaints,
7941 _("Invalid .debug_ranges data (inverted range)"));
7942 return 0;
7943 }
7944
7945 /* Empty range entries have no effect. */
7946 if (range_beginning == range_end)
7947 continue;
7948
43039443
JK
7949 range_beginning += base;
7950 range_end += base;
7951
9277c30c 7952 if (ranges_pst != NULL)
ff013f42 7953 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
7954 range_beginning + baseaddr,
7955 range_end - 1 + baseaddr,
ff013f42
JK
7956 ranges_pst);
7957
43039443
JK
7958 /* FIXME: This is recording everything as a low-high
7959 segment of consecutive addresses. We should have a
7960 data structure for discontiguous block ranges
7961 instead. */
7962 if (! low_set)
7963 {
7964 low = range_beginning;
7965 high = range_end;
7966 low_set = 1;
7967 }
7968 else
7969 {
7970 if (range_beginning < low)
7971 low = range_beginning;
7972 if (range_end > high)
7973 high = range_end;
7974 }
7975 }
7976
7977 if (! low_set)
7978 /* If the first entry is an end-of-list marker, the range
7979 describes an empty scope, i.e. no instructions. */
7980 return 0;
7981
7982 if (low_return)
7983 *low_return = low;
7984 if (high_return)
7985 *high_return = high;
7986 return 1;
7987}
7988
af34e669
DJ
7989/* Get low and high pc attributes from a die. Return 1 if the attributes
7990 are present and valid, otherwise, return 0. Return -1 if the range is
7991 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 7992
c906108c 7993static int
af34e669 7994dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
7995 CORE_ADDR *highpc, struct dwarf2_cu *cu,
7996 struct partial_symtab *pst)
c906108c
SS
7997{
7998 struct attribute *attr;
91da1414 7999 struct attribute *attr_high;
af34e669
DJ
8000 CORE_ADDR low = 0;
8001 CORE_ADDR high = 0;
8002 int ret = 0;
c906108c 8003
91da1414
MW
8004 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8005 if (attr_high)
af34e669 8006 {
e142c38c 8007 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 8008 if (attr)
91da1414
MW
8009 {
8010 low = DW_ADDR (attr);
3019eac3
DE
8011 if (attr_high->form == DW_FORM_addr
8012 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8013 high = DW_ADDR (attr_high);
8014 else
8015 high = low + DW_UNSND (attr_high);
8016 }
af34e669
DJ
8017 else
8018 /* Found high w/o low attribute. */
8019 return 0;
8020
8021 /* Found consecutive range of addresses. */
8022 ret = 1;
8023 }
c906108c 8024 else
af34e669 8025 {
e142c38c 8026 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
8027 if (attr != NULL)
8028 {
af34e669 8029 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 8030 .debug_ranges section. */
d85a05f0 8031 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 8032 return 0;
43039443 8033 /* Found discontinuous range of addresses. */
af34e669
DJ
8034 ret = -1;
8035 }
8036 }
c906108c 8037
9373cf26
JK
8038 /* read_partial_die has also the strict LOW < HIGH requirement. */
8039 if (high <= low)
c906108c
SS
8040 return 0;
8041
8042 /* When using the GNU linker, .gnu.linkonce. sections are used to
8043 eliminate duplicate copies of functions and vtables and such.
8044 The linker will arbitrarily choose one and discard the others.
8045 The AT_*_pc values for such functions refer to local labels in
8046 these sections. If the section from that file was discarded, the
8047 labels are not in the output, so the relocs get a value of 0.
8048 If this is a discarded function, mark the pc bounds as invalid,
8049 so that GDB will ignore it. */
72dca2f5 8050 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
8051 return 0;
8052
8053 *lowpc = low;
96408a79
SA
8054 if (highpc)
8055 *highpc = high;
af34e669 8056 return ret;
c906108c
SS
8057}
8058
b084d499
JB
8059/* Assuming that DIE represents a subprogram DIE or a lexical block, get
8060 its low and high PC addresses. Do nothing if these addresses could not
8061 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8062 and HIGHPC to the high address if greater than HIGHPC. */
8063
8064static void
8065dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8066 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8067 struct dwarf2_cu *cu)
8068{
8069 CORE_ADDR low, high;
8070 struct die_info *child = die->child;
8071
d85a05f0 8072 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
8073 {
8074 *lowpc = min (*lowpc, low);
8075 *highpc = max (*highpc, high);
8076 }
8077
8078 /* If the language does not allow nested subprograms (either inside
8079 subprograms or lexical blocks), we're done. */
8080 if (cu->language != language_ada)
8081 return;
6e70227d 8082
b084d499
JB
8083 /* Check all the children of the given DIE. If it contains nested
8084 subprograms, then check their pc bounds. Likewise, we need to
8085 check lexical blocks as well, as they may also contain subprogram
8086 definitions. */
8087 while (child && child->tag)
8088 {
8089 if (child->tag == DW_TAG_subprogram
8090 || child->tag == DW_TAG_lexical_block)
8091 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8092 child = sibling_die (child);
8093 }
8094}
8095
fae299cd
DC
8096/* Get the low and high pc's represented by the scope DIE, and store
8097 them in *LOWPC and *HIGHPC. If the correct values can't be
8098 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8099
8100static void
8101get_scope_pc_bounds (struct die_info *die,
8102 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8103 struct dwarf2_cu *cu)
8104{
8105 CORE_ADDR best_low = (CORE_ADDR) -1;
8106 CORE_ADDR best_high = (CORE_ADDR) 0;
8107 CORE_ADDR current_low, current_high;
8108
d85a05f0 8109 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
8110 {
8111 best_low = current_low;
8112 best_high = current_high;
8113 }
8114 else
8115 {
8116 struct die_info *child = die->child;
8117
8118 while (child && child->tag)
8119 {
8120 switch (child->tag) {
8121 case DW_TAG_subprogram:
b084d499 8122 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
8123 break;
8124 case DW_TAG_namespace:
f55ee35c 8125 case DW_TAG_module:
fae299cd
DC
8126 /* FIXME: carlton/2004-01-16: Should we do this for
8127 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8128 that current GCC's always emit the DIEs corresponding
8129 to definitions of methods of classes as children of a
8130 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8131 the DIEs giving the declarations, which could be
8132 anywhere). But I don't see any reason why the
8133 standards says that they have to be there. */
8134 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8135
8136 if (current_low != ((CORE_ADDR) -1))
8137 {
8138 best_low = min (best_low, current_low);
8139 best_high = max (best_high, current_high);
8140 }
8141 break;
8142 default:
0963b4bd 8143 /* Ignore. */
fae299cd
DC
8144 break;
8145 }
8146
8147 child = sibling_die (child);
8148 }
8149 }
8150
8151 *lowpc = best_low;
8152 *highpc = best_high;
8153}
8154
801e3a5b
JB
8155/* Record the address ranges for BLOCK, offset by BASEADDR, as given
8156 in DIE. */
380bca97 8157
801e3a5b
JB
8158static void
8159dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8160 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8161{
bb5ed363 8162 struct objfile *objfile = cu->objfile;
801e3a5b 8163 struct attribute *attr;
91da1414 8164 struct attribute *attr_high;
801e3a5b 8165
91da1414
MW
8166 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8167 if (attr_high)
801e3a5b 8168 {
801e3a5b
JB
8169 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8170 if (attr)
8171 {
8172 CORE_ADDR low = DW_ADDR (attr);
91da1414 8173 CORE_ADDR high;
3019eac3
DE
8174 if (attr_high->form == DW_FORM_addr
8175 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8176 high = DW_ADDR (attr_high);
8177 else
8178 high = low + DW_UNSND (attr_high);
9a619af0 8179
801e3a5b
JB
8180 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8181 }
8182 }
8183
8184 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8185 if (attr)
8186 {
bb5ed363 8187 bfd *obfd = objfile->obfd;
801e3a5b
JB
8188
8189 /* The value of the DW_AT_ranges attribute is the offset of the
8190 address range list in the .debug_ranges section. */
8191 unsigned long offset = DW_UNSND (attr);
dce234bc 8192 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
8193
8194 /* For some target architectures, but not others, the
8195 read_address function sign-extends the addresses it returns.
8196 To recognize base address selection entries, we need a
8197 mask. */
8198 unsigned int addr_size = cu->header.addr_size;
8199 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8200
8201 /* The base address, to which the next pair is relative. Note
8202 that this 'base' is a DWARF concept: most entries in a range
8203 list are relative, to reduce the number of relocs against the
8204 debugging information. This is separate from this function's
8205 'baseaddr' argument, which GDB uses to relocate debugging
8206 information from a shared library based on the address at
8207 which the library was loaded. */
d00adf39
DE
8208 CORE_ADDR base = cu->base_address;
8209 int base_known = cu->base_known;
801e3a5b 8210
be391dca 8211 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 8212 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
8213 {
8214 complaint (&symfile_complaints,
8215 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8216 offset);
8217 return;
8218 }
8219
8220 for (;;)
8221 {
8222 unsigned int bytes_read;
8223 CORE_ADDR start, end;
8224
8225 start = read_address (obfd, buffer, cu, &bytes_read);
8226 buffer += bytes_read;
8227 end = read_address (obfd, buffer, cu, &bytes_read);
8228 buffer += bytes_read;
8229
8230 /* Did we find the end of the range list? */
8231 if (start == 0 && end == 0)
8232 break;
8233
8234 /* Did we find a base address selection entry? */
8235 else if ((start & base_select_mask) == base_select_mask)
8236 {
8237 base = end;
8238 base_known = 1;
8239 }
8240
8241 /* We found an ordinary address range. */
8242 else
8243 {
8244 if (!base_known)
8245 {
8246 complaint (&symfile_complaints,
3e43a32a
MS
8247 _("Invalid .debug_ranges data "
8248 "(no base address)"));
801e3a5b
JB
8249 return;
8250 }
8251
9277c30c
UW
8252 if (start > end)
8253 {
8254 /* Inverted range entries are invalid. */
8255 complaint (&symfile_complaints,
8256 _("Invalid .debug_ranges data "
8257 "(inverted range)"));
8258 return;
8259 }
8260
8261 /* Empty range entries have no effect. */
8262 if (start == end)
8263 continue;
8264
6e70227d
DE
8265 record_block_range (block,
8266 baseaddr + base + start,
801e3a5b
JB
8267 baseaddr + base + end - 1);
8268 }
8269 }
8270 }
8271}
8272
60d5a603
JK
8273/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8274 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8275 during 4.6.0 experimental. */
8276
8277static int
8278producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8279{
8280 const char *cs;
8281 int major, minor, release;
ba919b58 8282 int result = 0;
60d5a603
JK
8283
8284 if (cu->producer == NULL)
8285 {
8286 /* For unknown compilers expect their behavior is DWARF version
8287 compliant.
8288
8289 GCC started to support .debug_types sections by -gdwarf-4 since
8290 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8291 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8292 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8293 interpreted incorrectly by GDB now - GCC PR debug/48229. */
8294
8295 return 0;
8296 }
8297
ba919b58
TT
8298 if (cu->checked_producer)
8299 return cu->producer_is_gxx_lt_4_6;
8300
60d5a603
JK
8301 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8302
8303 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
8304 {
8305 /* For non-GCC compilers expect their behavior is DWARF version
8306 compliant. */
60d5a603 8307 }
ba919b58 8308 else
60d5a603 8309 {
ba919b58
TT
8310 cs = &cu->producer[strlen ("GNU ")];
8311 while (*cs && !isdigit (*cs))
8312 cs++;
8313 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8314 {
8315 /* Not recognized as GCC. */
8316 }
8317 else
8318 result = major < 4 || (major == 4 && minor < 6);
60d5a603
JK
8319 }
8320
ba919b58
TT
8321 cu->checked_producer = 1;
8322 cu->producer_is_gxx_lt_4_6 = result;
8323
8324 return result;
60d5a603
JK
8325}
8326
8327/* Return the default accessibility type if it is not overriden by
8328 DW_AT_accessibility. */
8329
8330static enum dwarf_access_attribute
8331dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8332{
8333 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8334 {
8335 /* The default DWARF 2 accessibility for members is public, the default
8336 accessibility for inheritance is private. */
8337
8338 if (die->tag != DW_TAG_inheritance)
8339 return DW_ACCESS_public;
8340 else
8341 return DW_ACCESS_private;
8342 }
8343 else
8344 {
8345 /* DWARF 3+ defines the default accessibility a different way. The same
8346 rules apply now for DW_TAG_inheritance as for the members and it only
8347 depends on the container kind. */
8348
8349 if (die->parent->tag == DW_TAG_class_type)
8350 return DW_ACCESS_private;
8351 else
8352 return DW_ACCESS_public;
8353 }
8354}
8355
74ac6d43
TT
8356/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8357 offset. If the attribute was not found return 0, otherwise return
8358 1. If it was found but could not properly be handled, set *OFFSET
8359 to 0. */
8360
8361static int
8362handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8363 LONGEST *offset)
8364{
8365 struct attribute *attr;
8366
8367 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8368 if (attr != NULL)
8369 {
8370 *offset = 0;
8371
8372 /* Note that we do not check for a section offset first here.
8373 This is because DW_AT_data_member_location is new in DWARF 4,
8374 so if we see it, we can assume that a constant form is really
8375 a constant and not a section offset. */
8376 if (attr_form_is_constant (attr))
8377 *offset = dwarf2_get_attr_constant_value (attr, 0);
8378 else if (attr_form_is_section_offset (attr))
8379 dwarf2_complex_location_expr_complaint ();
8380 else if (attr_form_is_block (attr))
8381 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8382 else
8383 dwarf2_complex_location_expr_complaint ();
8384
8385 return 1;
8386 }
8387
8388 return 0;
8389}
8390
c906108c
SS
8391/* Add an aggregate field to the field list. */
8392
8393static void
107d2387 8394dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 8395 struct dwarf2_cu *cu)
6e70227d 8396{
e7c27a73 8397 struct objfile *objfile = cu->objfile;
5e2b427d 8398 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8399 struct nextfield *new_field;
8400 struct attribute *attr;
8401 struct field *fp;
8402 char *fieldname = "";
8403
8404 /* Allocate a new field list entry and link it in. */
8405 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 8406 make_cleanup (xfree, new_field);
c906108c 8407 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
8408
8409 if (die->tag == DW_TAG_inheritance)
8410 {
8411 new_field->next = fip->baseclasses;
8412 fip->baseclasses = new_field;
8413 }
8414 else
8415 {
8416 new_field->next = fip->fields;
8417 fip->fields = new_field;
8418 }
c906108c
SS
8419 fip->nfields++;
8420
e142c38c 8421 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
8422 if (attr)
8423 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
8424 else
8425 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
8426 if (new_field->accessibility != DW_ACCESS_public)
8427 fip->non_public_fields = 1;
60d5a603 8428
e142c38c 8429 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
8430 if (attr)
8431 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
8432 else
8433 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
8434
8435 fp = &new_field->field;
a9a9bd0f 8436
e142c38c 8437 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 8438 {
74ac6d43
TT
8439 LONGEST offset;
8440
a9a9bd0f 8441 /* Data member other than a C++ static data member. */
6e70227d 8442
c906108c 8443 /* Get type of field. */
e7c27a73 8444 fp->type = die_type (die, cu);
c906108c 8445
d6a843b5 8446 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 8447
c906108c 8448 /* Get bit size of field (zero if none). */
e142c38c 8449 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
8450 if (attr)
8451 {
8452 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8453 }
8454 else
8455 {
8456 FIELD_BITSIZE (*fp) = 0;
8457 }
8458
8459 /* Get bit offset of field. */
74ac6d43
TT
8460 if (handle_data_member_location (die, cu, &offset))
8461 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 8462 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
8463 if (attr)
8464 {
5e2b427d 8465 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
8466 {
8467 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
8468 additional bit offset from the MSB of the containing
8469 anonymous object to the MSB of the field. We don't
8470 have to do anything special since we don't need to
8471 know the size of the anonymous object. */
f41f5e61 8472 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
8473 }
8474 else
8475 {
8476 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
8477 MSB of the anonymous object, subtract off the number of
8478 bits from the MSB of the field to the MSB of the
8479 object, and then subtract off the number of bits of
8480 the field itself. The result is the bit offset of
8481 the LSB of the field. */
c906108c
SS
8482 int anonymous_size;
8483 int bit_offset = DW_UNSND (attr);
8484
e142c38c 8485 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8486 if (attr)
8487 {
8488 /* The size of the anonymous object containing
8489 the bit field is explicit, so use the
8490 indicated size (in bytes). */
8491 anonymous_size = DW_UNSND (attr);
8492 }
8493 else
8494 {
8495 /* The size of the anonymous object containing
8496 the bit field must be inferred from the type
8497 attribute of the data member containing the
8498 bit field. */
8499 anonymous_size = TYPE_LENGTH (fp->type);
8500 }
f41f5e61
PA
8501 SET_FIELD_BITPOS (*fp,
8502 (FIELD_BITPOS (*fp)
8503 + anonymous_size * bits_per_byte
8504 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
8505 }
8506 }
8507
8508 /* Get name of field. */
39cbfefa
DJ
8509 fieldname = dwarf2_name (die, cu);
8510 if (fieldname == NULL)
8511 fieldname = "";
d8151005
DJ
8512
8513 /* The name is already allocated along with this objfile, so we don't
8514 need to duplicate it for the type. */
8515 fp->name = fieldname;
c906108c
SS
8516
8517 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 8518 pointer or virtual base class pointer) to private. */
e142c38c 8519 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 8520 {
d48cc9dd 8521 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
8522 new_field->accessibility = DW_ACCESS_private;
8523 fip->non_public_fields = 1;
8524 }
8525 }
a9a9bd0f 8526 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 8527 {
a9a9bd0f
DC
8528 /* C++ static member. */
8529
8530 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8531 is a declaration, but all versions of G++ as of this writing
8532 (so through at least 3.2.1) incorrectly generate
8533 DW_TAG_variable tags. */
6e70227d 8534
ff355380 8535 const char *physname;
c906108c 8536
a9a9bd0f 8537 /* Get name of field. */
39cbfefa
DJ
8538 fieldname = dwarf2_name (die, cu);
8539 if (fieldname == NULL)
c906108c
SS
8540 return;
8541
254e6b9e 8542 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
8543 if (attr
8544 /* Only create a symbol if this is an external value.
8545 new_symbol checks this and puts the value in the global symbol
8546 table, which we want. If it is not external, new_symbol
8547 will try to put the value in cu->list_in_scope which is wrong. */
8548 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
8549 {
8550 /* A static const member, not much different than an enum as far as
8551 we're concerned, except that we can support more types. */
8552 new_symbol (die, NULL, cu);
8553 }
8554
2df3850c 8555 /* Get physical name. */
ff355380 8556 physname = dwarf2_physname (fieldname, die, cu);
c906108c 8557
d8151005
DJ
8558 /* The name is already allocated along with this objfile, so we don't
8559 need to duplicate it for the type. */
8560 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 8561 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 8562 FIELD_NAME (*fp) = fieldname;
c906108c
SS
8563 }
8564 else if (die->tag == DW_TAG_inheritance)
8565 {
74ac6d43 8566 LONGEST offset;
d4b96c9a 8567
74ac6d43
TT
8568 /* C++ base class field. */
8569 if (handle_data_member_location (die, cu, &offset))
8570 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 8571 FIELD_BITSIZE (*fp) = 0;
e7c27a73 8572 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
8573 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8574 fip->nbaseclasses++;
8575 }
8576}
8577
98751a41
JK
8578/* Add a typedef defined in the scope of the FIP's class. */
8579
8580static void
8581dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8582 struct dwarf2_cu *cu)
6e70227d 8583{
98751a41 8584 struct objfile *objfile = cu->objfile;
98751a41
JK
8585 struct typedef_field_list *new_field;
8586 struct attribute *attr;
8587 struct typedef_field *fp;
8588 char *fieldname = "";
8589
8590 /* Allocate a new field list entry and link it in. */
8591 new_field = xzalloc (sizeof (*new_field));
8592 make_cleanup (xfree, new_field);
8593
8594 gdb_assert (die->tag == DW_TAG_typedef);
8595
8596 fp = &new_field->field;
8597
8598 /* Get name of field. */
8599 fp->name = dwarf2_name (die, cu);
8600 if (fp->name == NULL)
8601 return;
8602
8603 fp->type = read_type_die (die, cu);
8604
8605 new_field->next = fip->typedef_field_list;
8606 fip->typedef_field_list = new_field;
8607 fip->typedef_field_list_count++;
8608}
8609
c906108c
SS
8610/* Create the vector of fields, and attach it to the type. */
8611
8612static void
fba45db2 8613dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8614 struct dwarf2_cu *cu)
c906108c
SS
8615{
8616 int nfields = fip->nfields;
8617
8618 /* Record the field count, allocate space for the array of fields,
8619 and create blank accessibility bitfields if necessary. */
8620 TYPE_NFIELDS (type) = nfields;
8621 TYPE_FIELDS (type) = (struct field *)
8622 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8623 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8624
b4ba55a1 8625 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
8626 {
8627 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8628
8629 TYPE_FIELD_PRIVATE_BITS (type) =
8630 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8631 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8632
8633 TYPE_FIELD_PROTECTED_BITS (type) =
8634 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8635 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8636
774b6a14
TT
8637 TYPE_FIELD_IGNORE_BITS (type) =
8638 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8639 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
8640 }
8641
8642 /* If the type has baseclasses, allocate and clear a bit vector for
8643 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 8644 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
8645 {
8646 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 8647 unsigned char *pointer;
c906108c
SS
8648
8649 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
8650 pointer = TYPE_ALLOC (type, num_bytes);
8651 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
8652 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8653 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8654 }
8655
3e43a32a
MS
8656 /* Copy the saved-up fields into the field vector. Start from the head of
8657 the list, adding to the tail of the field array, so that they end up in
8658 the same order in the array in which they were added to the list. */
c906108c
SS
8659 while (nfields-- > 0)
8660 {
7d0ccb61
DJ
8661 struct nextfield *fieldp;
8662
8663 if (fip->fields)
8664 {
8665 fieldp = fip->fields;
8666 fip->fields = fieldp->next;
8667 }
8668 else
8669 {
8670 fieldp = fip->baseclasses;
8671 fip->baseclasses = fieldp->next;
8672 }
8673
8674 TYPE_FIELD (type, nfields) = fieldp->field;
8675 switch (fieldp->accessibility)
c906108c 8676 {
c5aa993b 8677 case DW_ACCESS_private:
b4ba55a1
JB
8678 if (cu->language != language_ada)
8679 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 8680 break;
c906108c 8681
c5aa993b 8682 case DW_ACCESS_protected:
b4ba55a1
JB
8683 if (cu->language != language_ada)
8684 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 8685 break;
c906108c 8686
c5aa993b
JM
8687 case DW_ACCESS_public:
8688 break;
c906108c 8689
c5aa993b
JM
8690 default:
8691 /* Unknown accessibility. Complain and treat it as public. */
8692 {
e2e0b3e5 8693 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 8694 fieldp->accessibility);
c5aa993b
JM
8695 }
8696 break;
c906108c
SS
8697 }
8698 if (nfields < fip->nbaseclasses)
8699 {
7d0ccb61 8700 switch (fieldp->virtuality)
c906108c 8701 {
c5aa993b
JM
8702 case DW_VIRTUALITY_virtual:
8703 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 8704 if (cu->language == language_ada)
a73c6dcd 8705 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
8706 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8707 break;
c906108c
SS
8708 }
8709 }
c906108c
SS
8710 }
8711}
8712
c906108c
SS
8713/* Add a member function to the proper fieldlist. */
8714
8715static void
107d2387 8716dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 8717 struct type *type, struct dwarf2_cu *cu)
c906108c 8718{
e7c27a73 8719 struct objfile *objfile = cu->objfile;
c906108c
SS
8720 struct attribute *attr;
8721 struct fnfieldlist *flp;
8722 int i;
8723 struct fn_field *fnp;
8724 char *fieldname;
c906108c 8725 struct nextfnfield *new_fnfield;
f792889a 8726 struct type *this_type;
60d5a603 8727 enum dwarf_access_attribute accessibility;
c906108c 8728
b4ba55a1 8729 if (cu->language == language_ada)
a73c6dcd 8730 error (_("unexpected member function in Ada type"));
b4ba55a1 8731
2df3850c 8732 /* Get name of member function. */
39cbfefa
DJ
8733 fieldname = dwarf2_name (die, cu);
8734 if (fieldname == NULL)
2df3850c 8735 return;
c906108c 8736
c906108c
SS
8737 /* Look up member function name in fieldlist. */
8738 for (i = 0; i < fip->nfnfields; i++)
8739 {
27bfe10e 8740 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
8741 break;
8742 }
8743
8744 /* Create new list element if necessary. */
8745 if (i < fip->nfnfields)
8746 flp = &fip->fnfieldlists[i];
8747 else
8748 {
8749 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8750 {
8751 fip->fnfieldlists = (struct fnfieldlist *)
8752 xrealloc (fip->fnfieldlists,
8753 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8754 * sizeof (struct fnfieldlist));
c906108c 8755 if (fip->nfnfields == 0)
c13c43fd 8756 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
8757 }
8758 flp = &fip->fnfieldlists[fip->nfnfields];
8759 flp->name = fieldname;
8760 flp->length = 0;
8761 flp->head = NULL;
3da10d80 8762 i = fip->nfnfields++;
c906108c
SS
8763 }
8764
8765 /* Create a new member function field and chain it to the field list
0963b4bd 8766 entry. */
c906108c 8767 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 8768 make_cleanup (xfree, new_fnfield);
c906108c
SS
8769 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8770 new_fnfield->next = flp->head;
8771 flp->head = new_fnfield;
8772 flp->length++;
8773
8774 /* Fill in the member function field info. */
8775 fnp = &new_fnfield->fnfield;
3da10d80
KS
8776
8777 /* Delay processing of the physname until later. */
8778 if (cu->language == language_cplus || cu->language == language_java)
8779 {
8780 add_to_method_list (type, i, flp->length - 1, fieldname,
8781 die, cu);
8782 }
8783 else
8784 {
1d06ead6 8785 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
8786 fnp->physname = physname ? physname : "";
8787 }
8788
c906108c 8789 fnp->type = alloc_type (objfile);
f792889a
DJ
8790 this_type = read_type_die (die, cu);
8791 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 8792 {
f792889a 8793 int nparams = TYPE_NFIELDS (this_type);
c906108c 8794
f792889a 8795 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
8796 of the method itself (TYPE_CODE_METHOD). */
8797 smash_to_method_type (fnp->type, type,
f792889a
DJ
8798 TYPE_TARGET_TYPE (this_type),
8799 TYPE_FIELDS (this_type),
8800 TYPE_NFIELDS (this_type),
8801 TYPE_VARARGS (this_type));
c906108c
SS
8802
8803 /* Handle static member functions.
c5aa993b 8804 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
8805 member functions. G++ helps GDB by marking the first
8806 parameter for non-static member functions (which is the this
8807 pointer) as artificial. We obtain this information from
8808 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 8809 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
8810 fnp->voffset = VOFFSET_STATIC;
8811 }
8812 else
e2e0b3e5 8813 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 8814 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
8815
8816 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 8817 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 8818 fnp->fcontext = die_containing_type (die, cu);
c906108c 8819
3e43a32a
MS
8820 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
8821 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
8822
8823 /* Get accessibility. */
e142c38c 8824 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 8825 if (attr)
60d5a603
JK
8826 accessibility = DW_UNSND (attr);
8827 else
8828 accessibility = dwarf2_default_access_attribute (die, cu);
8829 switch (accessibility)
c906108c 8830 {
60d5a603
JK
8831 case DW_ACCESS_private:
8832 fnp->is_private = 1;
8833 break;
8834 case DW_ACCESS_protected:
8835 fnp->is_protected = 1;
8836 break;
c906108c
SS
8837 }
8838
b02dede2 8839 /* Check for artificial methods. */
e142c38c 8840 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
8841 if (attr && DW_UNSND (attr) != 0)
8842 fnp->is_artificial = 1;
8843
0d564a31 8844 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
8845 function. For older versions of GCC, this is an offset in the
8846 appropriate virtual table, as specified by DW_AT_containing_type.
8847 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
8848 to the object address. */
8849
e142c38c 8850 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 8851 if (attr)
8e19ed76 8852 {
aec5aa8b 8853 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 8854 {
aec5aa8b
TT
8855 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
8856 {
8857 /* Old-style GCC. */
8858 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8859 }
8860 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
8861 || (DW_BLOCK (attr)->size > 1
8862 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
8863 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
8864 {
8865 struct dwarf_block blk;
8866 int offset;
8867
8868 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
8869 ? 1 : 2);
8870 blk.size = DW_BLOCK (attr)->size - offset;
8871 blk.data = DW_BLOCK (attr)->data + offset;
8872 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
8873 if ((fnp->voffset % cu->header.addr_size) != 0)
8874 dwarf2_complex_location_expr_complaint ();
8875 else
8876 fnp->voffset /= cu->header.addr_size;
8877 fnp->voffset += 2;
8878 }
8879 else
8880 dwarf2_complex_location_expr_complaint ();
8881
8882 if (!fnp->fcontext)
8883 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
8884 }
3690dd37 8885 else if (attr_form_is_section_offset (attr))
8e19ed76 8886 {
4d3c2250 8887 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8888 }
8889 else
8890 {
4d3c2250
KB
8891 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
8892 fieldname);
8e19ed76 8893 }
0d564a31 8894 }
d48cc9dd
DJ
8895 else
8896 {
8897 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8898 if (attr && DW_UNSND (attr))
8899 {
8900 /* GCC does this, as of 2008-08-25; PR debug/37237. */
8901 complaint (&symfile_complaints,
3e43a32a
MS
8902 _("Member function \"%s\" (offset %d) is virtual "
8903 "but the vtable offset is not specified"),
b64f50a1 8904 fieldname, die->offset.sect_off);
9655fd1a 8905 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
8906 TYPE_CPLUS_DYNAMIC (type) = 1;
8907 }
8908 }
c906108c
SS
8909}
8910
8911/* Create the vector of member function fields, and attach it to the type. */
8912
8913static void
fba45db2 8914dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8915 struct dwarf2_cu *cu)
c906108c
SS
8916{
8917 struct fnfieldlist *flp;
c906108c
SS
8918 int i;
8919
b4ba55a1 8920 if (cu->language == language_ada)
a73c6dcd 8921 error (_("unexpected member functions in Ada type"));
b4ba55a1 8922
c906108c
SS
8923 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8924 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
8925 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
8926
8927 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
8928 {
8929 struct nextfnfield *nfp = flp->head;
8930 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
8931 int k;
8932
8933 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
8934 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
8935 fn_flp->fn_fields = (struct fn_field *)
8936 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
8937 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 8938 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
8939 }
8940
8941 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
8942}
8943
1168df01
JB
8944/* Returns non-zero if NAME is the name of a vtable member in CU's
8945 language, zero otherwise. */
8946static int
8947is_vtable_name (const char *name, struct dwarf2_cu *cu)
8948{
8949 static const char vptr[] = "_vptr";
987504bb 8950 static const char vtable[] = "vtable";
1168df01 8951
987504bb
JJ
8952 /* Look for the C++ and Java forms of the vtable. */
8953 if ((cu->language == language_java
8954 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
8955 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
8956 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
8957 return 1;
8958
8959 return 0;
8960}
8961
c0dd20ea 8962/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
8963 functions, with the ABI-specified layout. If TYPE describes
8964 such a structure, smash it into a member function type.
61049d3b
DJ
8965
8966 GCC shouldn't do this; it should just output pointer to member DIEs.
8967 This is GCC PR debug/28767. */
c0dd20ea 8968
0b92b5bb
TT
8969static void
8970quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 8971{
0b92b5bb 8972 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
8973
8974 /* Check for a structure with no name and two children. */
0b92b5bb
TT
8975 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
8976 return;
c0dd20ea
DJ
8977
8978 /* Check for __pfn and __delta members. */
0b92b5bb
TT
8979 if (TYPE_FIELD_NAME (type, 0) == NULL
8980 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
8981 || TYPE_FIELD_NAME (type, 1) == NULL
8982 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
8983 return;
c0dd20ea
DJ
8984
8985 /* Find the type of the method. */
0b92b5bb 8986 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
8987 if (pfn_type == NULL
8988 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
8989 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 8990 return;
c0dd20ea
DJ
8991
8992 /* Look for the "this" argument. */
8993 pfn_type = TYPE_TARGET_TYPE (pfn_type);
8994 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 8995 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 8996 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 8997 return;
c0dd20ea
DJ
8998
8999 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
9000 new_type = alloc_type (objfile);
9001 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
9002 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
9003 TYPE_VARARGS (pfn_type));
0b92b5bb 9004 smash_to_methodptr_type (type, new_type);
c0dd20ea 9005}
1168df01 9006
c906108c 9007/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
9008 (definition) to create a type for the structure or union. Fill in
9009 the type's name and general properties; the members will not be
9010 processed until process_structure_type.
c906108c 9011
c767944b
DJ
9012 NOTE: we need to call these functions regardless of whether or not the
9013 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
9014 structure or union. This gets the type entered into our set of
9015 user defined types.
9016
9017 However, if the structure is incomplete (an opaque struct/union)
9018 then suppress creating a symbol table entry for it since gdb only
9019 wants to find the one with the complete definition. Note that if
9020 it is complete, we just call new_symbol, which does it's own
9021 checking about whether the struct/union is anonymous or not (and
9022 suppresses creating a symbol table entry itself). */
9023
f792889a 9024static struct type *
134d01f1 9025read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9026{
e7c27a73 9027 struct objfile *objfile = cu->objfile;
c906108c
SS
9028 struct type *type;
9029 struct attribute *attr;
39cbfefa 9030 char *name;
c906108c 9031
348e048f
DE
9032 /* If the definition of this type lives in .debug_types, read that type.
9033 Don't follow DW_AT_specification though, that will take us back up
9034 the chain and we want to go down. */
9035 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9036 if (attr)
9037 {
9038 struct dwarf2_cu *type_cu = cu;
9039 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9040
348e048f
DE
9041 /* We could just recurse on read_structure_type, but we need to call
9042 get_die_type to ensure only one type for this DIE is created.
9043 This is important, for example, because for c++ classes we need
9044 TYPE_NAME set which is only done by new_symbol. Blech. */
9045 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9046
9047 /* TYPE_CU may not be the same as CU.
9048 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9049 return set_die_type (die, type, cu);
9050 }
9051
c0dd20ea 9052 type = alloc_type (objfile);
c906108c 9053 INIT_CPLUS_SPECIFIC (type);
93311388 9054
39cbfefa
DJ
9055 name = dwarf2_name (die, cu);
9056 if (name != NULL)
c906108c 9057 {
987504bb
JJ
9058 if (cu->language == language_cplus
9059 || cu->language == language_java)
63d06c5c 9060 {
3da10d80
KS
9061 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9062
9063 /* dwarf2_full_name might have already finished building the DIE's
9064 type. If so, there is no need to continue. */
9065 if (get_die_type (die, cu) != NULL)
9066 return get_die_type (die, cu);
9067
9068 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
9069 if (die->tag == DW_TAG_structure_type
9070 || die->tag == DW_TAG_class_type)
9071 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
9072 }
9073 else
9074 {
d8151005
DJ
9075 /* The name is already allocated along with this objfile, so
9076 we don't need to duplicate it for the type. */
94af9270
KS
9077 TYPE_TAG_NAME (type) = (char *) name;
9078 if (die->tag == DW_TAG_class_type)
9079 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 9080 }
c906108c
SS
9081 }
9082
9083 if (die->tag == DW_TAG_structure_type)
9084 {
9085 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9086 }
9087 else if (die->tag == DW_TAG_union_type)
9088 {
9089 TYPE_CODE (type) = TYPE_CODE_UNION;
9090 }
9091 else
9092 {
c906108c
SS
9093 TYPE_CODE (type) = TYPE_CODE_CLASS;
9094 }
9095
0cc2414c
TT
9096 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9097 TYPE_DECLARED_CLASS (type) = 1;
9098
e142c38c 9099 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9100 if (attr)
9101 {
9102 TYPE_LENGTH (type) = DW_UNSND (attr);
9103 }
9104 else
9105 {
9106 TYPE_LENGTH (type) = 0;
9107 }
9108
876cecd0 9109 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 9110 if (die_is_declaration (die, cu))
876cecd0 9111 TYPE_STUB (type) = 1;
a6c727b2
DJ
9112 else if (attr == NULL && die->child == NULL
9113 && producer_is_realview (cu->producer))
9114 /* RealView does not output the required DW_AT_declaration
9115 on incomplete types. */
9116 TYPE_STUB (type) = 1;
dc718098 9117
c906108c
SS
9118 /* We need to add the type field to the die immediately so we don't
9119 infinitely recurse when dealing with pointers to the structure
0963b4bd 9120 type within the structure itself. */
1c379e20 9121 set_die_type (die, type, cu);
c906108c 9122
7e314c57
JK
9123 /* set_die_type should be already done. */
9124 set_descriptive_type (type, die, cu);
9125
c767944b
DJ
9126 return type;
9127}
9128
9129/* Finish creating a structure or union type, including filling in
9130 its members and creating a symbol for it. */
9131
9132static void
9133process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9134{
9135 struct objfile *objfile = cu->objfile;
9136 struct die_info *child_die = die->child;
9137 struct type *type;
9138
9139 type = get_die_type (die, cu);
9140 if (type == NULL)
9141 type = read_structure_type (die, cu);
9142
e142c38c 9143 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
9144 {
9145 struct field_info fi;
9146 struct die_info *child_die;
34eaf542 9147 VEC (symbolp) *template_args = NULL;
c767944b 9148 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
9149
9150 memset (&fi, 0, sizeof (struct field_info));
9151
639d11d3 9152 child_die = die->child;
c906108c
SS
9153
9154 while (child_die && child_die->tag)
9155 {
a9a9bd0f
DC
9156 if (child_die->tag == DW_TAG_member
9157 || child_die->tag == DW_TAG_variable)
c906108c 9158 {
a9a9bd0f
DC
9159 /* NOTE: carlton/2002-11-05: A C++ static data member
9160 should be a DW_TAG_member that is a declaration, but
9161 all versions of G++ as of this writing (so through at
9162 least 3.2.1) incorrectly generate DW_TAG_variable
9163 tags for them instead. */
e7c27a73 9164 dwarf2_add_field (&fi, child_die, cu);
c906108c 9165 }
8713b1b1 9166 else if (child_die->tag == DW_TAG_subprogram)
c906108c 9167 {
0963b4bd 9168 /* C++ member function. */
e7c27a73 9169 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
9170 }
9171 else if (child_die->tag == DW_TAG_inheritance)
9172 {
9173 /* C++ base class field. */
e7c27a73 9174 dwarf2_add_field (&fi, child_die, cu);
c906108c 9175 }
98751a41
JK
9176 else if (child_die->tag == DW_TAG_typedef)
9177 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
9178 else if (child_die->tag == DW_TAG_template_type_param
9179 || child_die->tag == DW_TAG_template_value_param)
9180 {
9181 struct symbol *arg = new_symbol (child_die, NULL, cu);
9182
f1078f66
DJ
9183 if (arg != NULL)
9184 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9185 }
9186
c906108c
SS
9187 child_die = sibling_die (child_die);
9188 }
9189
34eaf542
TT
9190 /* Attach template arguments to type. */
9191 if (! VEC_empty (symbolp, template_args))
9192 {
9193 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9194 TYPE_N_TEMPLATE_ARGUMENTS (type)
9195 = VEC_length (symbolp, template_args);
9196 TYPE_TEMPLATE_ARGUMENTS (type)
9197 = obstack_alloc (&objfile->objfile_obstack,
9198 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9199 * sizeof (struct symbol *)));
9200 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9201 VEC_address (symbolp, template_args),
9202 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9203 * sizeof (struct symbol *)));
9204 VEC_free (symbolp, template_args);
9205 }
9206
c906108c
SS
9207 /* Attach fields and member functions to the type. */
9208 if (fi.nfields)
e7c27a73 9209 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
9210 if (fi.nfnfields)
9211 {
e7c27a73 9212 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 9213
c5aa993b 9214 /* Get the type which refers to the base class (possibly this
c906108c 9215 class itself) which contains the vtable pointer for the current
0d564a31
DJ
9216 class from the DW_AT_containing_type attribute. This use of
9217 DW_AT_containing_type is a GNU extension. */
c906108c 9218
e142c38c 9219 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 9220 {
e7c27a73 9221 struct type *t = die_containing_type (die, cu);
c906108c
SS
9222
9223 TYPE_VPTR_BASETYPE (type) = t;
9224 if (type == t)
9225 {
c906108c
SS
9226 int i;
9227
9228 /* Our own class provides vtbl ptr. */
9229 for (i = TYPE_NFIELDS (t) - 1;
9230 i >= TYPE_N_BASECLASSES (t);
9231 --i)
9232 {
0d5cff50 9233 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 9234
1168df01 9235 if (is_vtable_name (fieldname, cu))
c906108c
SS
9236 {
9237 TYPE_VPTR_FIELDNO (type) = i;
9238 break;
9239 }
9240 }
9241
9242 /* Complain if virtual function table field not found. */
9243 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 9244 complaint (&symfile_complaints,
3e43a32a
MS
9245 _("virtual function table pointer "
9246 "not found when defining class '%s'"),
4d3c2250
KB
9247 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9248 "");
c906108c
SS
9249 }
9250 else
9251 {
9252 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9253 }
9254 }
f6235d4c
EZ
9255 else if (cu->producer
9256 && strncmp (cu->producer,
9257 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9258 {
9259 /* The IBM XLC compiler does not provide direct indication
9260 of the containing type, but the vtable pointer is
9261 always named __vfp. */
9262
9263 int i;
9264
9265 for (i = TYPE_NFIELDS (type) - 1;
9266 i >= TYPE_N_BASECLASSES (type);
9267 --i)
9268 {
9269 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9270 {
9271 TYPE_VPTR_FIELDNO (type) = i;
9272 TYPE_VPTR_BASETYPE (type) = type;
9273 break;
9274 }
9275 }
9276 }
c906108c 9277 }
98751a41
JK
9278
9279 /* Copy fi.typedef_field_list linked list elements content into the
9280 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9281 if (fi.typedef_field_list)
9282 {
9283 int i = fi.typedef_field_list_count;
9284
a0d7a4ff 9285 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
9286 TYPE_TYPEDEF_FIELD_ARRAY (type)
9287 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9288 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9289
9290 /* Reverse the list order to keep the debug info elements order. */
9291 while (--i >= 0)
9292 {
9293 struct typedef_field *dest, *src;
6e70227d 9294
98751a41
JK
9295 dest = &TYPE_TYPEDEF_FIELD (type, i);
9296 src = &fi.typedef_field_list->field;
9297 fi.typedef_field_list = fi.typedef_field_list->next;
9298 *dest = *src;
9299 }
9300 }
c767944b
DJ
9301
9302 do_cleanups (back_to);
eb2a6f42
TT
9303
9304 if (HAVE_CPLUS_STRUCT (type))
9305 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 9306 }
63d06c5c 9307
bb5ed363 9308 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 9309
90aeadfc
DC
9310 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9311 snapshots) has been known to create a die giving a declaration
9312 for a class that has, as a child, a die giving a definition for a
9313 nested class. So we have to process our children even if the
9314 current die is a declaration. Normally, of course, a declaration
9315 won't have any children at all. */
134d01f1 9316
90aeadfc
DC
9317 while (child_die != NULL && child_die->tag)
9318 {
9319 if (child_die->tag == DW_TAG_member
9320 || child_die->tag == DW_TAG_variable
34eaf542
TT
9321 || child_die->tag == DW_TAG_inheritance
9322 || child_die->tag == DW_TAG_template_value_param
9323 || child_die->tag == DW_TAG_template_type_param)
134d01f1 9324 {
90aeadfc 9325 /* Do nothing. */
134d01f1 9326 }
90aeadfc
DC
9327 else
9328 process_die (child_die, cu);
134d01f1 9329
90aeadfc 9330 child_die = sibling_die (child_die);
134d01f1
DJ
9331 }
9332
fa4028e9
JB
9333 /* Do not consider external references. According to the DWARF standard,
9334 these DIEs are identified by the fact that they have no byte_size
9335 attribute, and a declaration attribute. */
9336 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9337 || !die_is_declaration (die, cu))
c767944b 9338 new_symbol (die, type, cu);
134d01f1
DJ
9339}
9340
9341/* Given a DW_AT_enumeration_type die, set its type. We do not
9342 complete the type's fields yet, or create any symbols. */
c906108c 9343
f792889a 9344static struct type *
134d01f1 9345read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9346{
e7c27a73 9347 struct objfile *objfile = cu->objfile;
c906108c 9348 struct type *type;
c906108c 9349 struct attribute *attr;
0114d602 9350 const char *name;
134d01f1 9351
348e048f
DE
9352 /* If the definition of this type lives in .debug_types, read that type.
9353 Don't follow DW_AT_specification though, that will take us back up
9354 the chain and we want to go down. */
9355 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9356 if (attr)
9357 {
9358 struct dwarf2_cu *type_cu = cu;
9359 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9360
348e048f 9361 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9362
9363 /* TYPE_CU may not be the same as CU.
9364 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9365 return set_die_type (die, type, cu);
9366 }
9367
c906108c
SS
9368 type = alloc_type (objfile);
9369
9370 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 9371 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 9372 if (name != NULL)
0114d602 9373 TYPE_TAG_NAME (type) = (char *) name;
c906108c 9374
e142c38c 9375 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9376 if (attr)
9377 {
9378 TYPE_LENGTH (type) = DW_UNSND (attr);
9379 }
9380 else
9381 {
9382 TYPE_LENGTH (type) = 0;
9383 }
9384
137033e9
JB
9385 /* The enumeration DIE can be incomplete. In Ada, any type can be
9386 declared as private in the package spec, and then defined only
9387 inside the package body. Such types are known as Taft Amendment
9388 Types. When another package uses such a type, an incomplete DIE
9389 may be generated by the compiler. */
02eb380e 9390 if (die_is_declaration (die, cu))
876cecd0 9391 TYPE_STUB (type) = 1;
02eb380e 9392
f792889a 9393 return set_die_type (die, type, cu);
134d01f1
DJ
9394}
9395
9396/* Given a pointer to a die which begins an enumeration, process all
9397 the dies that define the members of the enumeration, and create the
9398 symbol for the enumeration type.
9399
9400 NOTE: We reverse the order of the element list. */
9401
9402static void
9403process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9404{
f792889a 9405 struct type *this_type;
134d01f1 9406
f792889a
DJ
9407 this_type = get_die_type (die, cu);
9408 if (this_type == NULL)
9409 this_type = read_enumeration_type (die, cu);
9dc481d3 9410
639d11d3 9411 if (die->child != NULL)
c906108c 9412 {
9dc481d3
DE
9413 struct die_info *child_die;
9414 struct symbol *sym;
9415 struct field *fields = NULL;
9416 int num_fields = 0;
9417 int unsigned_enum = 1;
9418 char *name;
cafec441
TT
9419 int flag_enum = 1;
9420 ULONGEST mask = 0;
9dc481d3 9421
639d11d3 9422 child_die = die->child;
c906108c
SS
9423 while (child_die && child_die->tag)
9424 {
9425 if (child_die->tag != DW_TAG_enumerator)
9426 {
e7c27a73 9427 process_die (child_die, cu);
c906108c
SS
9428 }
9429 else
9430 {
39cbfefa
DJ
9431 name = dwarf2_name (child_die, cu);
9432 if (name)
c906108c 9433 {
f792889a 9434 sym = new_symbol (child_die, this_type, cu);
c906108c 9435 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
9436 {
9437 unsigned_enum = 0;
9438 flag_enum = 0;
9439 }
9440 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9441 flag_enum = 0;
9442 else
9443 mask |= SYMBOL_VALUE (sym);
c906108c
SS
9444
9445 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9446 {
9447 fields = (struct field *)
9448 xrealloc (fields,
9449 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 9450 * sizeof (struct field));
c906108c
SS
9451 }
9452
3567439c 9453 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 9454 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 9455 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
9456 FIELD_BITSIZE (fields[num_fields]) = 0;
9457
9458 num_fields++;
9459 }
9460 }
9461
9462 child_die = sibling_die (child_die);
9463 }
9464
9465 if (num_fields)
9466 {
f792889a
DJ
9467 TYPE_NFIELDS (this_type) = num_fields;
9468 TYPE_FIELDS (this_type) = (struct field *)
9469 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9470 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 9471 sizeof (struct field) * num_fields);
b8c9b27d 9472 xfree (fields);
c906108c
SS
9473 }
9474 if (unsigned_enum)
876cecd0 9475 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
9476 if (flag_enum)
9477 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 9478 }
134d01f1 9479
6c83ed52
TT
9480 /* If we are reading an enum from a .debug_types unit, and the enum
9481 is a declaration, and the enum is not the signatured type in the
9482 unit, then we do not want to add a symbol for it. Adding a
9483 symbol would in some cases obscure the true definition of the
9484 enum, giving users an incomplete type when the definition is
9485 actually available. Note that we do not want to do this for all
9486 enums which are just declarations, because C++0x allows forward
9487 enum declarations. */
3019eac3 9488 if (cu->per_cu->is_debug_types
6c83ed52
TT
9489 && die_is_declaration (die, cu))
9490 {
52dc124a 9491 struct signatured_type *sig_type;
6c83ed52 9492
52dc124a 9493 sig_type
6c83ed52 9494 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 9495 cu->per_cu->info_or_types_section,
6c83ed52 9496 cu->per_cu->offset);
3019eac3
DE
9497 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9498 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
9499 return;
9500 }
9501
f792889a 9502 new_symbol (die, this_type, cu);
c906108c
SS
9503}
9504
9505/* Extract all information from a DW_TAG_array_type DIE and put it in
9506 the DIE's type field. For now, this only handles one dimensional
9507 arrays. */
9508
f792889a 9509static struct type *
e7c27a73 9510read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9511{
e7c27a73 9512 struct objfile *objfile = cu->objfile;
c906108c 9513 struct die_info *child_die;
7e314c57 9514 struct type *type;
c906108c
SS
9515 struct type *element_type, *range_type, *index_type;
9516 struct type **range_types = NULL;
9517 struct attribute *attr;
9518 int ndim = 0;
9519 struct cleanup *back_to;
39cbfefa 9520 char *name;
c906108c 9521
e7c27a73 9522 element_type = die_type (die, cu);
c906108c 9523
7e314c57
JK
9524 /* The die_type call above may have already set the type for this DIE. */
9525 type = get_die_type (die, cu);
9526 if (type)
9527 return type;
9528
c906108c
SS
9529 /* Irix 6.2 native cc creates array types without children for
9530 arrays with unspecified length. */
639d11d3 9531 if (die->child == NULL)
c906108c 9532 {
46bf5051 9533 index_type = objfile_type (objfile)->builtin_int;
c906108c 9534 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
9535 type = create_array_type (NULL, element_type, range_type);
9536 return set_die_type (die, type, cu);
c906108c
SS
9537 }
9538
9539 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 9540 child_die = die->child;
c906108c
SS
9541 while (child_die && child_die->tag)
9542 {
9543 if (child_die->tag == DW_TAG_subrange_type)
9544 {
f792889a 9545 struct type *child_type = read_type_die (child_die, cu);
9a619af0 9546
f792889a 9547 if (child_type != NULL)
a02abb62 9548 {
0963b4bd
MS
9549 /* The range type was succesfully read. Save it for the
9550 array type creation. */
a02abb62
JB
9551 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9552 {
9553 range_types = (struct type **)
9554 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9555 * sizeof (struct type *));
9556 if (ndim == 0)
9557 make_cleanup (free_current_contents, &range_types);
9558 }
f792889a 9559 range_types[ndim++] = child_type;
a02abb62 9560 }
c906108c
SS
9561 }
9562 child_die = sibling_die (child_die);
9563 }
9564
9565 /* Dwarf2 dimensions are output from left to right, create the
9566 necessary array types in backwards order. */
7ca2d3a3 9567
c906108c 9568 type = element_type;
7ca2d3a3
DL
9569
9570 if (read_array_order (die, cu) == DW_ORD_col_major)
9571 {
9572 int i = 0;
9a619af0 9573
7ca2d3a3
DL
9574 while (i < ndim)
9575 type = create_array_type (NULL, type, range_types[i++]);
9576 }
9577 else
9578 {
9579 while (ndim-- > 0)
9580 type = create_array_type (NULL, type, range_types[ndim]);
9581 }
c906108c 9582
f5f8a009
EZ
9583 /* Understand Dwarf2 support for vector types (like they occur on
9584 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9585 array type. This is not part of the Dwarf2/3 standard yet, but a
9586 custom vendor extension. The main difference between a regular
9587 array and the vector variant is that vectors are passed by value
9588 to functions. */
e142c38c 9589 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 9590 if (attr)
ea37ba09 9591 make_vector_type (type);
f5f8a009 9592
dbc98a8b
KW
9593 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9594 implementation may choose to implement triple vectors using this
9595 attribute. */
9596 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9597 if (attr)
9598 {
9599 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9600 TYPE_LENGTH (type) = DW_UNSND (attr);
9601 else
3e43a32a
MS
9602 complaint (&symfile_complaints,
9603 _("DW_AT_byte_size for array type smaller "
9604 "than the total size of elements"));
dbc98a8b
KW
9605 }
9606
39cbfefa
DJ
9607 name = dwarf2_name (die, cu);
9608 if (name)
9609 TYPE_NAME (type) = name;
6e70227d 9610
0963b4bd 9611 /* Install the type in the die. */
7e314c57
JK
9612 set_die_type (die, type, cu);
9613
9614 /* set_die_type should be already done. */
b4ba55a1
JB
9615 set_descriptive_type (type, die, cu);
9616
c906108c
SS
9617 do_cleanups (back_to);
9618
7e314c57 9619 return type;
c906108c
SS
9620}
9621
7ca2d3a3 9622static enum dwarf_array_dim_ordering
6e70227d 9623read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
9624{
9625 struct attribute *attr;
9626
9627 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9628
9629 if (attr) return DW_SND (attr);
9630
0963b4bd
MS
9631 /* GNU F77 is a special case, as at 08/2004 array type info is the
9632 opposite order to the dwarf2 specification, but data is still
9633 laid out as per normal fortran.
7ca2d3a3 9634
0963b4bd
MS
9635 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9636 version checking. */
7ca2d3a3 9637
905e0470
PM
9638 if (cu->language == language_fortran
9639 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
9640 {
9641 return DW_ORD_row_major;
9642 }
9643
6e70227d 9644 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
9645 {
9646 case array_column_major:
9647 return DW_ORD_col_major;
9648 case array_row_major:
9649 default:
9650 return DW_ORD_row_major;
9651 };
9652}
9653
72019c9c 9654/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 9655 the DIE's type field. */
72019c9c 9656
f792889a 9657static struct type *
72019c9c
GM
9658read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9659{
7e314c57
JK
9660 struct type *domain_type, *set_type;
9661 struct attribute *attr;
f792889a 9662
7e314c57
JK
9663 domain_type = die_type (die, cu);
9664
9665 /* The die_type call above may have already set the type for this DIE. */
9666 set_type = get_die_type (die, cu);
9667 if (set_type)
9668 return set_type;
9669
9670 set_type = create_set_type (NULL, domain_type);
9671
9672 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
9673 if (attr)
9674 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 9675
f792889a 9676 return set_die_type (die, set_type, cu);
72019c9c 9677}
7ca2d3a3 9678
c906108c
SS
9679/* First cut: install each common block member as a global variable. */
9680
9681static void
e7c27a73 9682read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
9683{
9684 struct die_info *child_die;
9685 struct attribute *attr;
9686 struct symbol *sym;
9687 CORE_ADDR base = (CORE_ADDR) 0;
9688
e142c38c 9689 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
9690 if (attr)
9691 {
0963b4bd 9692 /* Support the .debug_loc offsets. */
8e19ed76
PS
9693 if (attr_form_is_block (attr))
9694 {
e7c27a73 9695 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 9696 }
3690dd37 9697 else if (attr_form_is_section_offset (attr))
8e19ed76 9698 {
4d3c2250 9699 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9700 }
9701 else
9702 {
4d3c2250
KB
9703 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9704 "common block member");
8e19ed76 9705 }
c906108c 9706 }
639d11d3 9707 if (die->child != NULL)
c906108c 9708 {
639d11d3 9709 child_die = die->child;
c906108c
SS
9710 while (child_die && child_die->tag)
9711 {
74ac6d43
TT
9712 LONGEST offset;
9713
e7c27a73 9714 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
9715 if (sym != NULL
9716 && handle_data_member_location (child_die, cu, &offset))
c906108c 9717 {
74ac6d43 9718 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
9719 add_symbol_to_list (sym, &global_symbols);
9720 }
9721 child_die = sibling_die (child_die);
9722 }
9723 }
9724}
9725
0114d602 9726/* Create a type for a C++ namespace. */
d9fa45fe 9727
0114d602
DJ
9728static struct type *
9729read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 9730{
e7c27a73 9731 struct objfile *objfile = cu->objfile;
0114d602 9732 const char *previous_prefix, *name;
9219021c 9733 int is_anonymous;
0114d602
DJ
9734 struct type *type;
9735
9736 /* For extensions, reuse the type of the original namespace. */
9737 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9738 {
9739 struct die_info *ext_die;
9740 struct dwarf2_cu *ext_cu = cu;
9a619af0 9741
0114d602
DJ
9742 ext_die = dwarf2_extension (die, &ext_cu);
9743 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
9744
9745 /* EXT_CU may not be the same as CU.
9746 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
9747 return set_die_type (die, type, cu);
9748 }
9219021c 9749
e142c38c 9750 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
9751
9752 /* Now build the name of the current namespace. */
9753
0114d602
DJ
9754 previous_prefix = determine_prefix (die, cu);
9755 if (previous_prefix[0] != '\0')
9756 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 9757 previous_prefix, name, 0, cu);
0114d602
DJ
9758
9759 /* Create the type. */
9760 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
9761 objfile);
9762 TYPE_NAME (type) = (char *) name;
9763 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9764
60531b24 9765 return set_die_type (die, type, cu);
0114d602
DJ
9766}
9767
9768/* Read a C++ namespace. */
9769
9770static void
9771read_namespace (struct die_info *die, struct dwarf2_cu *cu)
9772{
9773 struct objfile *objfile = cu->objfile;
0114d602 9774 int is_anonymous;
9219021c 9775
5c4e30ca
DC
9776 /* Add a symbol associated to this if we haven't seen the namespace
9777 before. Also, add a using directive if it's an anonymous
9778 namespace. */
9219021c 9779
f2f0e013 9780 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
9781 {
9782 struct type *type;
9783
0114d602 9784 type = read_type_die (die, cu);
e7c27a73 9785 new_symbol (die, type, cu);
5c4e30ca 9786
e8e80198 9787 namespace_name (die, &is_anonymous, cu);
5c4e30ca 9788 if (is_anonymous)
0114d602
DJ
9789 {
9790 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 9791
c0cc3a76 9792 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 9793 NULL, NULL, &objfile->objfile_obstack);
0114d602 9794 }
5c4e30ca 9795 }
9219021c 9796
639d11d3 9797 if (die->child != NULL)
d9fa45fe 9798 {
639d11d3 9799 struct die_info *child_die = die->child;
6e70227d 9800
d9fa45fe
DC
9801 while (child_die && child_die->tag)
9802 {
e7c27a73 9803 process_die (child_die, cu);
d9fa45fe
DC
9804 child_die = sibling_die (child_die);
9805 }
9806 }
38d518c9
EZ
9807}
9808
f55ee35c
JK
9809/* Read a Fortran module as type. This DIE can be only a declaration used for
9810 imported module. Still we need that type as local Fortran "use ... only"
9811 declaration imports depend on the created type in determine_prefix. */
9812
9813static struct type *
9814read_module_type (struct die_info *die, struct dwarf2_cu *cu)
9815{
9816 struct objfile *objfile = cu->objfile;
9817 char *module_name;
9818 struct type *type;
9819
9820 module_name = dwarf2_name (die, cu);
9821 if (!module_name)
3e43a32a
MS
9822 complaint (&symfile_complaints,
9823 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 9824 die->offset.sect_off);
f55ee35c
JK
9825 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
9826
9827 /* determine_prefix uses TYPE_TAG_NAME. */
9828 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9829
9830 return set_die_type (die, type, cu);
9831}
9832
5d7cb8df
JK
9833/* Read a Fortran module. */
9834
9835static void
9836read_module (struct die_info *die, struct dwarf2_cu *cu)
9837{
9838 struct die_info *child_die = die->child;
9839
5d7cb8df
JK
9840 while (child_die && child_die->tag)
9841 {
9842 process_die (child_die, cu);
9843 child_die = sibling_die (child_die);
9844 }
9845}
9846
38d518c9
EZ
9847/* Return the name of the namespace represented by DIE. Set
9848 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
9849 namespace. */
9850
9851static const char *
e142c38c 9852namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
9853{
9854 struct die_info *current_die;
9855 const char *name = NULL;
9856
9857 /* Loop through the extensions until we find a name. */
9858
9859 for (current_die = die;
9860 current_die != NULL;
f2f0e013 9861 current_die = dwarf2_extension (die, &cu))
38d518c9 9862 {
e142c38c 9863 name = dwarf2_name (current_die, cu);
38d518c9
EZ
9864 if (name != NULL)
9865 break;
9866 }
9867
9868 /* Is it an anonymous namespace? */
9869
9870 *is_anonymous = (name == NULL);
9871 if (*is_anonymous)
2b1dbab0 9872 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
9873
9874 return name;
d9fa45fe
DC
9875}
9876
c906108c
SS
9877/* Extract all information from a DW_TAG_pointer_type DIE and add to
9878 the user defined type vector. */
9879
f792889a 9880static struct type *
e7c27a73 9881read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9882{
5e2b427d 9883 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 9884 struct comp_unit_head *cu_header = &cu->header;
c906108c 9885 struct type *type;
8b2dbe47
KB
9886 struct attribute *attr_byte_size;
9887 struct attribute *attr_address_class;
9888 int byte_size, addr_class;
7e314c57
JK
9889 struct type *target_type;
9890
9891 target_type = die_type (die, cu);
c906108c 9892
7e314c57
JK
9893 /* The die_type call above may have already set the type for this DIE. */
9894 type = get_die_type (die, cu);
9895 if (type)
9896 return type;
9897
9898 type = lookup_pointer_type (target_type);
8b2dbe47 9899
e142c38c 9900 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
9901 if (attr_byte_size)
9902 byte_size = DW_UNSND (attr_byte_size);
c906108c 9903 else
8b2dbe47
KB
9904 byte_size = cu_header->addr_size;
9905
e142c38c 9906 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
9907 if (attr_address_class)
9908 addr_class = DW_UNSND (attr_address_class);
9909 else
9910 addr_class = DW_ADDR_none;
9911
9912 /* If the pointer size or address class is different than the
9913 default, create a type variant marked as such and set the
9914 length accordingly. */
9915 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 9916 {
5e2b427d 9917 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
9918 {
9919 int type_flags;
9920
849957d9 9921 type_flags = gdbarch_address_class_type_flags
5e2b427d 9922 (gdbarch, byte_size, addr_class);
876cecd0
TT
9923 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
9924 == 0);
8b2dbe47
KB
9925 type = make_type_with_address_space (type, type_flags);
9926 }
9927 else if (TYPE_LENGTH (type) != byte_size)
9928 {
3e43a32a
MS
9929 complaint (&symfile_complaints,
9930 _("invalid pointer size %d"), byte_size);
8b2dbe47 9931 }
6e70227d 9932 else
9a619af0
MS
9933 {
9934 /* Should we also complain about unhandled address classes? */
9935 }
c906108c 9936 }
8b2dbe47
KB
9937
9938 TYPE_LENGTH (type) = byte_size;
f792889a 9939 return set_die_type (die, type, cu);
c906108c
SS
9940}
9941
9942/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
9943 the user defined type vector. */
9944
f792889a 9945static struct type *
e7c27a73 9946read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
9947{
9948 struct type *type;
9949 struct type *to_type;
9950 struct type *domain;
9951
e7c27a73
DJ
9952 to_type = die_type (die, cu);
9953 domain = die_containing_type (die, cu);
0d5de010 9954
7e314c57
JK
9955 /* The calls above may have already set the type for this DIE. */
9956 type = get_die_type (die, cu);
9957 if (type)
9958 return type;
9959
0d5de010
DJ
9960 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
9961 type = lookup_methodptr_type (to_type);
9962 else
9963 type = lookup_memberptr_type (to_type, domain);
c906108c 9964
f792889a 9965 return set_die_type (die, type, cu);
c906108c
SS
9966}
9967
9968/* Extract all information from a DW_TAG_reference_type DIE and add to
9969 the user defined type vector. */
9970
f792889a 9971static struct type *
e7c27a73 9972read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9973{
e7c27a73 9974 struct comp_unit_head *cu_header = &cu->header;
7e314c57 9975 struct type *type, *target_type;
c906108c
SS
9976 struct attribute *attr;
9977
7e314c57
JK
9978 target_type = die_type (die, cu);
9979
9980 /* The die_type call above may have already set the type for this DIE. */
9981 type = get_die_type (die, cu);
9982 if (type)
9983 return type;
9984
9985 type = lookup_reference_type (target_type);
e142c38c 9986 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9987 if (attr)
9988 {
9989 TYPE_LENGTH (type) = DW_UNSND (attr);
9990 }
9991 else
9992 {
107d2387 9993 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 9994 }
f792889a 9995 return set_die_type (die, type, cu);
c906108c
SS
9996}
9997
f792889a 9998static struct type *
e7c27a73 9999read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10000{
f792889a 10001 struct type *base_type, *cv_type;
c906108c 10002
e7c27a73 10003 base_type = die_type (die, cu);
7e314c57
JK
10004
10005 /* The die_type call above may have already set the type for this DIE. */
10006 cv_type = get_die_type (die, cu);
10007 if (cv_type)
10008 return cv_type;
10009
2f608a3a
KW
10010 /* In case the const qualifier is applied to an array type, the element type
10011 is so qualified, not the array type (section 6.7.3 of C99). */
10012 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
10013 {
10014 struct type *el_type, *inner_array;
10015
10016 base_type = copy_type (base_type);
10017 inner_array = base_type;
10018
10019 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10020 {
10021 TYPE_TARGET_TYPE (inner_array) =
10022 copy_type (TYPE_TARGET_TYPE (inner_array));
10023 inner_array = TYPE_TARGET_TYPE (inner_array);
10024 }
10025
10026 el_type = TYPE_TARGET_TYPE (inner_array);
10027 TYPE_TARGET_TYPE (inner_array) =
10028 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10029
10030 return set_die_type (die, base_type, cu);
10031 }
10032
f792889a
DJ
10033 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10034 return set_die_type (die, cv_type, cu);
c906108c
SS
10035}
10036
f792889a 10037static struct type *
e7c27a73 10038read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10039{
f792889a 10040 struct type *base_type, *cv_type;
c906108c 10041
e7c27a73 10042 base_type = die_type (die, cu);
7e314c57
JK
10043
10044 /* The die_type call above may have already set the type for this DIE. */
10045 cv_type = get_die_type (die, cu);
10046 if (cv_type)
10047 return cv_type;
10048
f792889a
DJ
10049 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10050 return set_die_type (die, cv_type, cu);
c906108c
SS
10051}
10052
10053/* Extract all information from a DW_TAG_string_type DIE and add to
10054 the user defined type vector. It isn't really a user defined type,
10055 but it behaves like one, with other DIE's using an AT_user_def_type
10056 attribute to reference it. */
10057
f792889a 10058static struct type *
e7c27a73 10059read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10060{
e7c27a73 10061 struct objfile *objfile = cu->objfile;
3b7538c0 10062 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10063 struct type *type, *range_type, *index_type, *char_type;
10064 struct attribute *attr;
10065 unsigned int length;
10066
e142c38c 10067 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
10068 if (attr)
10069 {
10070 length = DW_UNSND (attr);
10071 }
10072 else
10073 {
0963b4bd 10074 /* Check for the DW_AT_byte_size attribute. */
e142c38c 10075 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
10076 if (attr)
10077 {
10078 length = DW_UNSND (attr);
10079 }
10080 else
10081 {
10082 length = 1;
10083 }
c906108c 10084 }
6ccb9162 10085
46bf5051 10086 index_type = objfile_type (objfile)->builtin_int;
c906108c 10087 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
10088 char_type = language_string_char_type (cu->language_defn, gdbarch);
10089 type = create_string_type (NULL, char_type, range_type);
6ccb9162 10090
f792889a 10091 return set_die_type (die, type, cu);
c906108c
SS
10092}
10093
10094/* Handle DIES due to C code like:
10095
10096 struct foo
c5aa993b
JM
10097 {
10098 int (*funcp)(int a, long l);
10099 int b;
10100 };
c906108c 10101
0963b4bd 10102 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 10103
f792889a 10104static struct type *
e7c27a73 10105read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10106{
bb5ed363 10107 struct objfile *objfile = cu->objfile;
0963b4bd
MS
10108 struct type *type; /* Type that this function returns. */
10109 struct type *ftype; /* Function that returns above type. */
c906108c
SS
10110 struct attribute *attr;
10111
e7c27a73 10112 type = die_type (die, cu);
7e314c57
JK
10113
10114 /* The die_type call above may have already set the type for this DIE. */
10115 ftype = get_die_type (die, cu);
10116 if (ftype)
10117 return ftype;
10118
0c8b41f1 10119 ftype = lookup_function_type (type);
c906108c 10120
5b8101ae 10121 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 10122 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 10123 if ((attr && (DW_UNSND (attr) != 0))
987504bb 10124 || cu->language == language_cplus
5b8101ae
PM
10125 || cu->language == language_java
10126 || cu->language == language_pascal)
876cecd0 10127 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
10128 else if (producer_is_realview (cu->producer))
10129 /* RealView does not emit DW_AT_prototyped. We can not
10130 distinguish prototyped and unprototyped functions; default to
10131 prototyped, since that is more common in modern code (and
10132 RealView warns about unprototyped functions). */
10133 TYPE_PROTOTYPED (ftype) = 1;
c906108c 10134
c055b101
CV
10135 /* Store the calling convention in the type if it's available in
10136 the subroutine die. Otherwise set the calling convention to
10137 the default value DW_CC_normal. */
10138 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
10139 if (attr)
10140 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10141 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10142 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10143 else
10144 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
10145
10146 /* We need to add the subroutine type to the die immediately so
10147 we don't infinitely recurse when dealing with parameters
0963b4bd 10148 declared as the same subroutine type. */
76c10ea2 10149 set_die_type (die, ftype, cu);
6e70227d 10150
639d11d3 10151 if (die->child != NULL)
c906108c 10152 {
bb5ed363 10153 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 10154 struct die_info *child_die;
8072405b 10155 int nparams, iparams;
c906108c
SS
10156
10157 /* Count the number of parameters.
10158 FIXME: GDB currently ignores vararg functions, but knows about
10159 vararg member functions. */
8072405b 10160 nparams = 0;
639d11d3 10161 child_die = die->child;
c906108c
SS
10162 while (child_die && child_die->tag)
10163 {
10164 if (child_die->tag == DW_TAG_formal_parameter)
10165 nparams++;
10166 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 10167 TYPE_VARARGS (ftype) = 1;
c906108c
SS
10168 child_die = sibling_die (child_die);
10169 }
10170
10171 /* Allocate storage for parameters and fill them in. */
10172 TYPE_NFIELDS (ftype) = nparams;
10173 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 10174 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 10175
8072405b
JK
10176 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10177 even if we error out during the parameters reading below. */
10178 for (iparams = 0; iparams < nparams; iparams++)
10179 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10180
10181 iparams = 0;
639d11d3 10182 child_die = die->child;
c906108c
SS
10183 while (child_die && child_die->tag)
10184 {
10185 if (child_die->tag == DW_TAG_formal_parameter)
10186 {
3ce3b1ba
PA
10187 struct type *arg_type;
10188
10189 /* DWARF version 2 has no clean way to discern C++
10190 static and non-static member functions. G++ helps
10191 GDB by marking the first parameter for non-static
10192 member functions (which is the this pointer) as
10193 artificial. We pass this information to
10194 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10195
10196 DWARF version 3 added DW_AT_object_pointer, which GCC
10197 4.5 does not yet generate. */
e142c38c 10198 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
10199 if (attr)
10200 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10201 else
418835cc
KS
10202 {
10203 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10204
10205 /* GCC/43521: In java, the formal parameter
10206 "this" is sometimes not marked with DW_AT_artificial. */
10207 if (cu->language == language_java)
10208 {
10209 const char *name = dwarf2_name (child_die, cu);
9a619af0 10210
418835cc
KS
10211 if (name && !strcmp (name, "this"))
10212 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10213 }
10214 }
3ce3b1ba
PA
10215 arg_type = die_type (child_die, cu);
10216
10217 /* RealView does not mark THIS as const, which the testsuite
10218 expects. GCC marks THIS as const in method definitions,
10219 but not in the class specifications (GCC PR 43053). */
10220 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10221 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10222 {
10223 int is_this = 0;
10224 struct dwarf2_cu *arg_cu = cu;
10225 const char *name = dwarf2_name (child_die, cu);
10226
10227 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10228 if (attr)
10229 {
10230 /* If the compiler emits this, use it. */
10231 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10232 is_this = 1;
10233 }
10234 else if (name && strcmp (name, "this") == 0)
10235 /* Function definitions will have the argument names. */
10236 is_this = 1;
10237 else if (name == NULL && iparams == 0)
10238 /* Declarations may not have the names, so like
10239 elsewhere in GDB, assume an artificial first
10240 argument is "this". */
10241 is_this = 1;
10242
10243 if (is_this)
10244 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10245 arg_type, 0);
10246 }
10247
10248 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
10249 iparams++;
10250 }
10251 child_die = sibling_die (child_die);
10252 }
10253 }
10254
76c10ea2 10255 return ftype;
c906108c
SS
10256}
10257
f792889a 10258static struct type *
e7c27a73 10259read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10260{
e7c27a73 10261 struct objfile *objfile = cu->objfile;
0114d602 10262 const char *name = NULL;
3c8e0968 10263 struct type *this_type, *target_type;
c906108c 10264
94af9270 10265 name = dwarf2_full_name (NULL, die, cu);
f792889a 10266 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
10267 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10268 TYPE_NAME (this_type) = (char *) name;
f792889a 10269 set_die_type (die, this_type, cu);
3c8e0968
DE
10270 target_type = die_type (die, cu);
10271 if (target_type != this_type)
10272 TYPE_TARGET_TYPE (this_type) = target_type;
10273 else
10274 {
10275 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10276 spec and cause infinite loops in GDB. */
10277 complaint (&symfile_complaints,
10278 _("Self-referential DW_TAG_typedef "
10279 "- DIE at 0x%x [in module %s]"),
b64f50a1 10280 die->offset.sect_off, objfile->name);
3c8e0968
DE
10281 TYPE_TARGET_TYPE (this_type) = NULL;
10282 }
f792889a 10283 return this_type;
c906108c
SS
10284}
10285
10286/* Find a representation of a given base type and install
10287 it in the TYPE field of the die. */
10288
f792889a 10289static struct type *
e7c27a73 10290read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10291{
e7c27a73 10292 struct objfile *objfile = cu->objfile;
c906108c
SS
10293 struct type *type;
10294 struct attribute *attr;
10295 int encoding = 0, size = 0;
39cbfefa 10296 char *name;
6ccb9162
UW
10297 enum type_code code = TYPE_CODE_INT;
10298 int type_flags = 0;
10299 struct type *target_type = NULL;
c906108c 10300
e142c38c 10301 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
10302 if (attr)
10303 {
10304 encoding = DW_UNSND (attr);
10305 }
e142c38c 10306 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10307 if (attr)
10308 {
10309 size = DW_UNSND (attr);
10310 }
39cbfefa 10311 name = dwarf2_name (die, cu);
6ccb9162 10312 if (!name)
c906108c 10313 {
6ccb9162
UW
10314 complaint (&symfile_complaints,
10315 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 10316 }
6ccb9162
UW
10317
10318 switch (encoding)
c906108c 10319 {
6ccb9162
UW
10320 case DW_ATE_address:
10321 /* Turn DW_ATE_address into a void * pointer. */
10322 code = TYPE_CODE_PTR;
10323 type_flags |= TYPE_FLAG_UNSIGNED;
10324 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10325 break;
10326 case DW_ATE_boolean:
10327 code = TYPE_CODE_BOOL;
10328 type_flags |= TYPE_FLAG_UNSIGNED;
10329 break;
10330 case DW_ATE_complex_float:
10331 code = TYPE_CODE_COMPLEX;
10332 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10333 break;
10334 case DW_ATE_decimal_float:
10335 code = TYPE_CODE_DECFLOAT;
10336 break;
10337 case DW_ATE_float:
10338 code = TYPE_CODE_FLT;
10339 break;
10340 case DW_ATE_signed:
10341 break;
10342 case DW_ATE_unsigned:
10343 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
10344 if (cu->language == language_fortran
10345 && name
10346 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10347 code = TYPE_CODE_CHAR;
6ccb9162
UW
10348 break;
10349 case DW_ATE_signed_char:
6e70227d 10350 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10351 || cu->language == language_pascal
10352 || cu->language == language_fortran)
6ccb9162
UW
10353 code = TYPE_CODE_CHAR;
10354 break;
10355 case DW_ATE_unsigned_char:
868a0084 10356 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10357 || cu->language == language_pascal
10358 || cu->language == language_fortran)
6ccb9162
UW
10359 code = TYPE_CODE_CHAR;
10360 type_flags |= TYPE_FLAG_UNSIGNED;
10361 break;
75079b2b
TT
10362 case DW_ATE_UTF:
10363 /* We just treat this as an integer and then recognize the
10364 type by name elsewhere. */
10365 break;
10366
6ccb9162
UW
10367 default:
10368 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10369 dwarf_type_encoding_name (encoding));
10370 break;
c906108c 10371 }
6ccb9162 10372
0114d602
DJ
10373 type = init_type (code, size, type_flags, NULL, objfile);
10374 TYPE_NAME (type) = name;
6ccb9162
UW
10375 TYPE_TARGET_TYPE (type) = target_type;
10376
0114d602 10377 if (name && strcmp (name, "char") == 0)
876cecd0 10378 TYPE_NOSIGN (type) = 1;
0114d602 10379
f792889a 10380 return set_die_type (die, type, cu);
c906108c
SS
10381}
10382
a02abb62
JB
10383/* Read the given DW_AT_subrange DIE. */
10384
f792889a 10385static struct type *
a02abb62
JB
10386read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10387{
10388 struct type *base_type;
10389 struct type *range_type;
10390 struct attribute *attr;
4fae6e18
JK
10391 LONGEST low, high;
10392 int low_default_is_valid;
39cbfefa 10393 char *name;
43bbcdc2 10394 LONGEST negative_mask;
e77813c8 10395
a02abb62 10396 base_type = die_type (die, cu);
953ac07e
JK
10397 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10398 check_typedef (base_type);
a02abb62 10399
7e314c57
JK
10400 /* The die_type call above may have already set the type for this DIE. */
10401 range_type = get_die_type (die, cu);
10402 if (range_type)
10403 return range_type;
10404
4fae6e18
JK
10405 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10406 omitting DW_AT_lower_bound. */
10407 switch (cu->language)
6e70227d 10408 {
4fae6e18
JK
10409 case language_c:
10410 case language_cplus:
10411 low = 0;
10412 low_default_is_valid = 1;
10413 break;
10414 case language_fortran:
10415 low = 1;
10416 low_default_is_valid = 1;
10417 break;
10418 case language_d:
10419 case language_java:
10420 case language_objc:
10421 low = 0;
10422 low_default_is_valid = (cu->header.version >= 4);
10423 break;
10424 case language_ada:
10425 case language_m2:
10426 case language_pascal:
a02abb62 10427 low = 1;
4fae6e18
JK
10428 low_default_is_valid = (cu->header.version >= 4);
10429 break;
10430 default:
10431 low = 0;
10432 low_default_is_valid = 0;
10433 break;
a02abb62
JB
10434 }
10435
dd5e6932
DJ
10436 /* FIXME: For variable sized arrays either of these could be
10437 a variable rather than a constant value. We'll allow it,
10438 but we don't know how to handle it. */
e142c38c 10439 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 10440 if (attr)
4fae6e18
JK
10441 low = dwarf2_get_attr_constant_value (attr, low);
10442 else if (!low_default_is_valid)
10443 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10444 "- DIE at 0x%x [in module %s]"),
10445 die->offset.sect_off, cu->objfile->name);
a02abb62 10446
e142c38c 10447 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 10448 if (attr)
6e70227d 10449 {
d48323d8 10450 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
10451 {
10452 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 10453 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
10454 FIXME: GDB does not yet know how to handle dynamic
10455 arrays properly, treat them as arrays with unspecified
10456 length for now.
10457
10458 FIXME: jimb/2003-09-22: GDB does not really know
10459 how to handle arrays of unspecified length
10460 either; we just represent them as zero-length
10461 arrays. Choose an appropriate upper bound given
10462 the lower bound we've computed above. */
10463 high = low - 1;
10464 }
10465 else
10466 high = dwarf2_get_attr_constant_value (attr, 1);
10467 }
e77813c8
PM
10468 else
10469 {
10470 attr = dwarf2_attr (die, DW_AT_count, cu);
10471 if (attr)
10472 {
10473 int count = dwarf2_get_attr_constant_value (attr, 1);
10474 high = low + count - 1;
10475 }
c2ff108b
JK
10476 else
10477 {
10478 /* Unspecified array length. */
10479 high = low - 1;
10480 }
e77813c8
PM
10481 }
10482
10483 /* Dwarf-2 specifications explicitly allows to create subrange types
10484 without specifying a base type.
10485 In that case, the base type must be set to the type of
10486 the lower bound, upper bound or count, in that order, if any of these
10487 three attributes references an object that has a type.
10488 If no base type is found, the Dwarf-2 specifications say that
10489 a signed integer type of size equal to the size of an address should
10490 be used.
10491 For the following C code: `extern char gdb_int [];'
10492 GCC produces an empty range DIE.
10493 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 10494 high bound or count are not yet handled by this code. */
e77813c8
PM
10495 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10496 {
10497 struct objfile *objfile = cu->objfile;
10498 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10499 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10500 struct type *int_type = objfile_type (objfile)->builtin_int;
10501
10502 /* Test "int", "long int", and "long long int" objfile types,
10503 and select the first one having a size above or equal to the
10504 architecture address size. */
10505 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10506 base_type = int_type;
10507 else
10508 {
10509 int_type = objfile_type (objfile)->builtin_long;
10510 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10511 base_type = int_type;
10512 else
10513 {
10514 int_type = objfile_type (objfile)->builtin_long_long;
10515 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10516 base_type = int_type;
10517 }
10518 }
10519 }
a02abb62 10520
6e70227d 10521 negative_mask =
43bbcdc2
PH
10522 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10523 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10524 low |= negative_mask;
10525 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10526 high |= negative_mask;
10527
a02abb62
JB
10528 range_type = create_range_type (NULL, base_type, low, high);
10529
bbb0eef6
JK
10530 /* Mark arrays with dynamic length at least as an array of unspecified
10531 length. GDB could check the boundary but before it gets implemented at
10532 least allow accessing the array elements. */
d48323d8 10533 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
10534 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10535
c2ff108b
JK
10536 /* Ada expects an empty array on no boundary attributes. */
10537 if (attr == NULL && cu->language != language_ada)
10538 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10539
39cbfefa
DJ
10540 name = dwarf2_name (die, cu);
10541 if (name)
10542 TYPE_NAME (range_type) = name;
6e70227d 10543
e142c38c 10544 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
10545 if (attr)
10546 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10547
7e314c57
JK
10548 set_die_type (die, range_type, cu);
10549
10550 /* set_die_type should be already done. */
b4ba55a1
JB
10551 set_descriptive_type (range_type, die, cu);
10552
7e314c57 10553 return range_type;
a02abb62 10554}
6e70227d 10555
f792889a 10556static struct type *
81a17f79
JB
10557read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10558{
10559 struct type *type;
81a17f79 10560
81a17f79
JB
10561 /* For now, we only support the C meaning of an unspecified type: void. */
10562
0114d602
DJ
10563 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10564 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 10565
f792889a 10566 return set_die_type (die, type, cu);
81a17f79 10567}
a02abb62 10568
639d11d3
DC
10569/* Read a single die and all its descendents. Set the die's sibling
10570 field to NULL; set other fields in the die correctly, and set all
10571 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10572 location of the info_ptr after reading all of those dies. PARENT
10573 is the parent of the die in question. */
10574
10575static struct die_info *
dee91e82
DE
10576read_die_and_children (const struct die_reader_specs *reader,
10577 gdb_byte *info_ptr,
10578 gdb_byte **new_info_ptr,
10579 struct die_info *parent)
639d11d3
DC
10580{
10581 struct die_info *die;
fe1b8b76 10582 gdb_byte *cur_ptr;
639d11d3
DC
10583 int has_children;
10584
93311388 10585 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
10586 if (die == NULL)
10587 {
10588 *new_info_ptr = cur_ptr;
10589 return NULL;
10590 }
93311388 10591 store_in_ref_table (die, reader->cu);
639d11d3
DC
10592
10593 if (has_children)
348e048f 10594 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
10595 else
10596 {
10597 die->child = NULL;
10598 *new_info_ptr = cur_ptr;
10599 }
10600
10601 die->sibling = NULL;
10602 die->parent = parent;
10603 return die;
10604}
10605
10606/* Read a die, all of its descendents, and all of its siblings; set
10607 all of the fields of all of the dies correctly. Arguments are as
10608 in read_die_and_children. */
10609
10610static struct die_info *
93311388
DE
10611read_die_and_siblings (const struct die_reader_specs *reader,
10612 gdb_byte *info_ptr,
fe1b8b76 10613 gdb_byte **new_info_ptr,
639d11d3
DC
10614 struct die_info *parent)
10615{
10616 struct die_info *first_die, *last_sibling;
fe1b8b76 10617 gdb_byte *cur_ptr;
639d11d3 10618
c906108c 10619 cur_ptr = info_ptr;
639d11d3
DC
10620 first_die = last_sibling = NULL;
10621
10622 while (1)
c906108c 10623 {
639d11d3 10624 struct die_info *die
dee91e82 10625 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 10626
1d325ec1 10627 if (die == NULL)
c906108c 10628 {
639d11d3
DC
10629 *new_info_ptr = cur_ptr;
10630 return first_die;
c906108c 10631 }
1d325ec1
DJ
10632
10633 if (!first_die)
10634 first_die = die;
c906108c 10635 else
1d325ec1
DJ
10636 last_sibling->sibling = die;
10637
10638 last_sibling = die;
c906108c 10639 }
c906108c
SS
10640}
10641
3019eac3
DE
10642/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10643 attributes.
10644 The caller is responsible for filling in the extra attributes
10645 and updating (*DIEP)->num_attrs.
10646 Set DIEP to point to a newly allocated die with its information,
10647 except for its child, sibling, and parent fields.
10648 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
10649
10650static gdb_byte *
3019eac3
DE
10651read_full_die_1 (const struct die_reader_specs *reader,
10652 struct die_info **diep, gdb_byte *info_ptr,
10653 int *has_children, int num_extra_attrs)
93311388 10654{
b64f50a1
JK
10655 unsigned int abbrev_number, bytes_read, i;
10656 sect_offset offset;
93311388
DE
10657 struct abbrev_info *abbrev;
10658 struct die_info *die;
10659 struct dwarf2_cu *cu = reader->cu;
10660 bfd *abfd = reader->abfd;
10661
b64f50a1 10662 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
10663 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10664 info_ptr += bytes_read;
10665 if (!abbrev_number)
10666 {
10667 *diep = NULL;
10668 *has_children = 0;
10669 return info_ptr;
10670 }
10671
10672 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10673 if (!abbrev)
348e048f
DE
10674 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10675 abbrev_number,
10676 bfd_get_filename (abfd));
10677
3019eac3 10678 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
10679 die->offset = offset;
10680 die->tag = abbrev->tag;
10681 die->abbrev = abbrev_number;
10682
3019eac3
DE
10683 /* Make the result usable.
10684 The caller needs to update num_attrs after adding the extra
10685 attributes. */
93311388
DE
10686 die->num_attrs = abbrev->num_attrs;
10687
10688 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
10689 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10690 info_ptr);
93311388
DE
10691
10692 *diep = die;
10693 *has_children = abbrev->has_children;
10694 return info_ptr;
10695}
10696
3019eac3
DE
10697/* Read a die and all its attributes.
10698 Set DIEP to point to a newly allocated die with its information,
10699 except for its child, sibling, and parent fields.
10700 Set HAS_CHILDREN to tell whether the die has children or not. */
10701
10702static gdb_byte *
10703read_full_die (const struct die_reader_specs *reader,
10704 struct die_info **diep, gdb_byte *info_ptr,
10705 int *has_children)
10706{
10707 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10708}
10709
c906108c
SS
10710/* In DWARF version 2, the description of the debugging information is
10711 stored in a separate .debug_abbrev section. Before we read any
10712 dies from a section we read in all abbreviations and install them
72bf9492
DJ
10713 in a hash table. This function also sets flags in CU describing
10714 the data found in the abbrev table. */
c906108c
SS
10715
10716static void
dee91e82
DE
10717dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10718 struct dwarf2_section_info *abbrev_section)
10719
c906108c 10720{
dee91e82 10721 bfd *abfd = abbrev_section->asection->owner;
e7c27a73 10722 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 10723 gdb_byte *abbrev_ptr;
c906108c
SS
10724 struct abbrev_info *cur_abbrev;
10725 unsigned int abbrev_number, bytes_read, abbrev_name;
10726 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
10727 struct attr_abbrev *cur_attrs;
10728 unsigned int allocated_attrs;
c906108c 10729
0963b4bd 10730 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
10731 obstack_init (&cu->abbrev_obstack);
10732 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10733 (ABBREV_HASH_SIZE
10734 * sizeof (struct abbrev_info *)));
10735 memset (cu->dwarf2_abbrevs, 0,
10736 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 10737
dee91e82
DE
10738 dwarf2_read_section (cu->objfile, abbrev_section);
10739 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
c906108c
SS
10740 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10741 abbrev_ptr += bytes_read;
10742
f3dd6933
DJ
10743 allocated_attrs = ATTR_ALLOC_CHUNK;
10744 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 10745
0963b4bd 10746 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
10747 while (abbrev_number)
10748 {
f3dd6933 10749 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
10750
10751 /* read in abbrev header */
10752 cur_abbrev->number = abbrev_number;
10753 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10754 abbrev_ptr += bytes_read;
10755 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
10756 abbrev_ptr += 1;
10757
10758 /* now read in declarations */
10759 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10760 abbrev_ptr += bytes_read;
10761 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10762 abbrev_ptr += bytes_read;
10763 while (abbrev_name)
10764 {
f3dd6933 10765 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 10766 {
f3dd6933
DJ
10767 allocated_attrs += ATTR_ALLOC_CHUNK;
10768 cur_attrs
10769 = xrealloc (cur_attrs, (allocated_attrs
10770 * sizeof (struct attr_abbrev)));
c906108c 10771 }
ae038cb0 10772
f3dd6933
DJ
10773 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
10774 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
10775 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10776 abbrev_ptr += bytes_read;
10777 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10778 abbrev_ptr += bytes_read;
10779 }
10780
f3dd6933
DJ
10781 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
10782 (cur_abbrev->num_attrs
10783 * sizeof (struct attr_abbrev)));
10784 memcpy (cur_abbrev->attrs, cur_attrs,
10785 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
10786
c906108c 10787 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
10788 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
10789 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
10790
10791 /* Get next abbreviation.
10792 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
10793 always properly terminated with an abbrev number of 0.
10794 Exit loop if we encounter an abbreviation which we have
10795 already read (which means we are about to read the abbreviations
10796 for the next compile unit) or if the end of the abbreviation
10797 table is reached. */
dee91e82
DE
10798 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
10799 >= abbrev_section->size)
c906108c
SS
10800 break;
10801 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10802 abbrev_ptr += bytes_read;
e7c27a73 10803 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
10804 break;
10805 }
f3dd6933
DJ
10806
10807 xfree (cur_attrs);
c906108c
SS
10808}
10809
f3dd6933 10810/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 10811
c906108c 10812static void
f3dd6933 10813dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 10814{
f3dd6933 10815 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 10816
f3dd6933
DJ
10817 obstack_free (&cu->abbrev_obstack, NULL);
10818 cu->dwarf2_abbrevs = NULL;
c906108c
SS
10819}
10820
10821/* Lookup an abbrev_info structure in the abbrev hash table. */
10822
10823static struct abbrev_info *
e7c27a73 10824dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
10825{
10826 unsigned int hash_number;
10827 struct abbrev_info *abbrev;
10828
10829 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 10830 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
10831
10832 while (abbrev)
10833 {
10834 if (abbrev->number == number)
10835 return abbrev;
10836 else
10837 abbrev = abbrev->next;
10838 }
10839 return NULL;
10840}
10841
72bf9492
DJ
10842/* Returns nonzero if TAG represents a type that we might generate a partial
10843 symbol for. */
10844
10845static int
10846is_type_tag_for_partial (int tag)
10847{
10848 switch (tag)
10849 {
10850#if 0
10851 /* Some types that would be reasonable to generate partial symbols for,
10852 that we don't at present. */
10853 case DW_TAG_array_type:
10854 case DW_TAG_file_type:
10855 case DW_TAG_ptr_to_member_type:
10856 case DW_TAG_set_type:
10857 case DW_TAG_string_type:
10858 case DW_TAG_subroutine_type:
10859#endif
10860 case DW_TAG_base_type:
10861 case DW_TAG_class_type:
680b30c7 10862 case DW_TAG_interface_type:
72bf9492
DJ
10863 case DW_TAG_enumeration_type:
10864 case DW_TAG_structure_type:
10865 case DW_TAG_subrange_type:
10866 case DW_TAG_typedef:
10867 case DW_TAG_union_type:
10868 return 1;
10869 default:
10870 return 0;
10871 }
10872}
10873
10874/* Load all DIEs that are interesting for partial symbols into memory. */
10875
10876static struct partial_die_info *
dee91e82
DE
10877load_partial_dies (const struct die_reader_specs *reader,
10878 gdb_byte *info_ptr, int building_psymtab)
72bf9492 10879{
dee91e82 10880 struct dwarf2_cu *cu = reader->cu;
bb5ed363 10881 struct objfile *objfile = cu->objfile;
72bf9492
DJ
10882 struct partial_die_info *part_die;
10883 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
10884 struct abbrev_info *abbrev;
10885 unsigned int bytes_read;
5afb4e99 10886 unsigned int load_all = 0;
72bf9492
DJ
10887 int nesting_level = 1;
10888
10889 parent_die = NULL;
10890 last_die = NULL;
10891
7adf1e79
DE
10892 gdb_assert (cu->per_cu != NULL);
10893 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
10894 load_all = 1;
10895
72bf9492
DJ
10896 cu->partial_dies
10897 = htab_create_alloc_ex (cu->header.length / 12,
10898 partial_die_hash,
10899 partial_die_eq,
10900 NULL,
10901 &cu->comp_unit_obstack,
10902 hashtab_obstack_allocate,
10903 dummy_obstack_deallocate);
10904
10905 part_die = obstack_alloc (&cu->comp_unit_obstack,
10906 sizeof (struct partial_die_info));
10907
10908 while (1)
10909 {
10910 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
10911
10912 /* A NULL abbrev means the end of a series of children. */
10913 if (abbrev == NULL)
10914 {
10915 if (--nesting_level == 0)
10916 {
10917 /* PART_DIE was probably the last thing allocated on the
10918 comp_unit_obstack, so we could call obstack_free
10919 here. We don't do that because the waste is small,
10920 and will be cleaned up when we're done with this
10921 compilation unit. This way, we're also more robust
10922 against other users of the comp_unit_obstack. */
10923 return first_die;
10924 }
10925 info_ptr += bytes_read;
10926 last_die = parent_die;
10927 parent_die = parent_die->die_parent;
10928 continue;
10929 }
10930
98bfdba5
PA
10931 /* Check for template arguments. We never save these; if
10932 they're seen, we just mark the parent, and go on our way. */
10933 if (parent_die != NULL
10934 && cu->language == language_cplus
10935 && (abbrev->tag == DW_TAG_template_type_param
10936 || abbrev->tag == DW_TAG_template_value_param))
10937 {
10938 parent_die->has_template_arguments = 1;
10939
10940 if (!load_all)
10941 {
10942 /* We don't need a partial DIE for the template argument. */
dee91e82 10943 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
10944 continue;
10945 }
10946 }
10947
0d99eb77 10948 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
10949 Skip their other children. */
10950 if (!load_all
10951 && cu->language == language_cplus
10952 && parent_die != NULL
10953 && parent_die->tag == DW_TAG_subprogram)
10954 {
dee91e82 10955 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
10956 continue;
10957 }
10958
5afb4e99
DJ
10959 /* Check whether this DIE is interesting enough to save. Normally
10960 we would not be interested in members here, but there may be
10961 later variables referencing them via DW_AT_specification (for
10962 static members). */
10963 if (!load_all
10964 && !is_type_tag_for_partial (abbrev->tag)
72929c62 10965 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
10966 && abbrev->tag != DW_TAG_enumerator
10967 && abbrev->tag != DW_TAG_subprogram
bc30ff58 10968 && abbrev->tag != DW_TAG_lexical_block
72bf9492 10969 && abbrev->tag != DW_TAG_variable
5afb4e99 10970 && abbrev->tag != DW_TAG_namespace
f55ee35c 10971 && abbrev->tag != DW_TAG_module
95554aad
TT
10972 && abbrev->tag != DW_TAG_member
10973 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
10974 {
10975 /* Otherwise we skip to the next sibling, if any. */
dee91e82 10976 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
10977 continue;
10978 }
10979
dee91e82
DE
10980 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
10981 info_ptr);
72bf9492
DJ
10982
10983 /* This two-pass algorithm for processing partial symbols has a
10984 high cost in cache pressure. Thus, handle some simple cases
10985 here which cover the majority of C partial symbols. DIEs
10986 which neither have specification tags in them, nor could have
10987 specification tags elsewhere pointing at them, can simply be
10988 processed and discarded.
10989
10990 This segment is also optional; scan_partial_symbols and
10991 add_partial_symbol will handle these DIEs if we chain
10992 them in normally. When compilers which do not emit large
10993 quantities of duplicate debug information are more common,
10994 this code can probably be removed. */
10995
10996 /* Any complete simple types at the top level (pretty much all
10997 of them, for a language without namespaces), can be processed
10998 directly. */
10999 if (parent_die == NULL
11000 && part_die->has_specification == 0
11001 && part_die->is_declaration == 0
d8228535 11002 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
11003 || part_die->tag == DW_TAG_base_type
11004 || part_die->tag == DW_TAG_subrange_type))
11005 {
11006 if (building_psymtab && part_die->name != NULL)
04a679b8 11007 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11008 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
11009 &objfile->static_psymbols,
11010 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 11011 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11012 continue;
11013 }
11014
d8228535
JK
11015 /* The exception for DW_TAG_typedef with has_children above is
11016 a workaround of GCC PR debug/47510. In the case of this complaint
11017 type_name_no_tag_or_error will error on such types later.
11018
11019 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11020 it could not find the child DIEs referenced later, this is checked
11021 above. In correct DWARF DW_TAG_typedef should have no children. */
11022
11023 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11024 complaint (&symfile_complaints,
11025 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11026 "- DIE at 0x%x [in module %s]"),
b64f50a1 11027 part_die->offset.sect_off, objfile->name);
d8228535 11028
72bf9492
DJ
11029 /* If we're at the second level, and we're an enumerator, and
11030 our parent has no specification (meaning possibly lives in a
11031 namespace elsewhere), then we can add the partial symbol now
11032 instead of queueing it. */
11033 if (part_die->tag == DW_TAG_enumerator
11034 && parent_die != NULL
11035 && parent_die->die_parent == NULL
11036 && parent_die->tag == DW_TAG_enumeration_type
11037 && parent_die->has_specification == 0)
11038 {
11039 if (part_die->name == NULL)
3e43a32a
MS
11040 complaint (&symfile_complaints,
11041 _("malformed enumerator DIE ignored"));
72bf9492 11042 else if (building_psymtab)
04a679b8 11043 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11044 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
11045 (cu->language == language_cplus
11046 || cu->language == language_java)
bb5ed363
DE
11047 ? &objfile->global_psymbols
11048 : &objfile->static_psymbols,
11049 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 11050
dee91e82 11051 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11052 continue;
11053 }
11054
11055 /* We'll save this DIE so link it in. */
11056 part_die->die_parent = parent_die;
11057 part_die->die_sibling = NULL;
11058 part_die->die_child = NULL;
11059
11060 if (last_die && last_die == parent_die)
11061 last_die->die_child = part_die;
11062 else if (last_die)
11063 last_die->die_sibling = part_die;
11064
11065 last_die = part_die;
11066
11067 if (first_die == NULL)
11068 first_die = part_die;
11069
11070 /* Maybe add the DIE to the hash table. Not all DIEs that we
11071 find interesting need to be in the hash table, because we
11072 also have the parent/sibling/child chains; only those that we
11073 might refer to by offset later during partial symbol reading.
11074
11075 For now this means things that might have be the target of a
11076 DW_AT_specification, DW_AT_abstract_origin, or
11077 DW_AT_extension. DW_AT_extension will refer only to
11078 namespaces; DW_AT_abstract_origin refers to functions (and
11079 many things under the function DIE, but we do not recurse
11080 into function DIEs during partial symbol reading) and
11081 possibly variables as well; DW_AT_specification refers to
11082 declarations. Declarations ought to have the DW_AT_declaration
11083 flag. It happens that GCC forgets to put it in sometimes, but
11084 only for functions, not for types.
11085
11086 Adding more things than necessary to the hash table is harmless
11087 except for the performance cost. Adding too few will result in
5afb4e99
DJ
11088 wasted time in find_partial_die, when we reread the compilation
11089 unit with load_all_dies set. */
72bf9492 11090
5afb4e99 11091 if (load_all
72929c62 11092 || abbrev->tag == DW_TAG_constant
5afb4e99 11093 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
11094 || abbrev->tag == DW_TAG_variable
11095 || abbrev->tag == DW_TAG_namespace
11096 || part_die->is_declaration)
11097 {
11098 void **slot;
11099
11100 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 11101 part_die->offset.sect_off, INSERT);
72bf9492
DJ
11102 *slot = part_die;
11103 }
11104
11105 part_die = obstack_alloc (&cu->comp_unit_obstack,
11106 sizeof (struct partial_die_info));
11107
11108 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 11109 we have no reason to follow the children of structures; for other
98bfdba5
PA
11110 languages we have to, so that we can get at method physnames
11111 to infer fully qualified class names, for DW_AT_specification,
11112 and for C++ template arguments. For C++, we also look one level
11113 inside functions to find template arguments (if the name of the
11114 function does not already contain the template arguments).
bc30ff58
JB
11115
11116 For Ada, we need to scan the children of subprograms and lexical
11117 blocks as well because Ada allows the definition of nested
11118 entities that could be interesting for the debugger, such as
11119 nested subprograms for instance. */
72bf9492 11120 if (last_die->has_children
5afb4e99
DJ
11121 && (load_all
11122 || last_die->tag == DW_TAG_namespace
f55ee35c 11123 || last_die->tag == DW_TAG_module
72bf9492 11124 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
11125 || (cu->language == language_cplus
11126 && last_die->tag == DW_TAG_subprogram
11127 && (last_die->name == NULL
11128 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
11129 || (cu->language != language_c
11130 && (last_die->tag == DW_TAG_class_type
680b30c7 11131 || last_die->tag == DW_TAG_interface_type
72bf9492 11132 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
11133 || last_die->tag == DW_TAG_union_type))
11134 || (cu->language == language_ada
11135 && (last_die->tag == DW_TAG_subprogram
11136 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
11137 {
11138 nesting_level++;
11139 parent_die = last_die;
11140 continue;
11141 }
11142
11143 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11144 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
11145
11146 /* Back to the top, do it again. */
11147 }
11148}
11149
c906108c
SS
11150/* Read a minimal amount of information into the minimal die structure. */
11151
fe1b8b76 11152static gdb_byte *
dee91e82
DE
11153read_partial_die (const struct die_reader_specs *reader,
11154 struct partial_die_info *part_die,
11155 struct abbrev_info *abbrev, unsigned int abbrev_len,
11156 gdb_byte *info_ptr)
c906108c 11157{
dee91e82 11158 struct dwarf2_cu *cu = reader->cu;
bb5ed363 11159 struct objfile *objfile = cu->objfile;
dee91e82 11160 gdb_byte *buffer = reader->buffer;
fa238c03 11161 unsigned int i;
c906108c 11162 struct attribute attr;
c5aa993b 11163 int has_low_pc_attr = 0;
c906108c 11164 int has_high_pc_attr = 0;
91da1414 11165 int high_pc_relative = 0;
c906108c 11166
72bf9492 11167 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 11168
b64f50a1 11169 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
11170
11171 info_ptr += abbrev_len;
11172
11173 if (abbrev == NULL)
11174 return info_ptr;
11175
c906108c
SS
11176 part_die->tag = abbrev->tag;
11177 part_die->has_children = abbrev->has_children;
c906108c
SS
11178
11179 for (i = 0; i < abbrev->num_attrs; ++i)
11180 {
dee91e82 11181 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
11182
11183 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 11184 partial symbol table. */
c906108c
SS
11185 switch (attr.name)
11186 {
11187 case DW_AT_name:
71c25dea
TT
11188 switch (part_die->tag)
11189 {
11190 case DW_TAG_compile_unit:
95554aad 11191 case DW_TAG_partial_unit:
348e048f 11192 case DW_TAG_type_unit:
71c25dea
TT
11193 /* Compilation units have a DW_AT_name that is a filename, not
11194 a source language identifier. */
11195 case DW_TAG_enumeration_type:
11196 case DW_TAG_enumerator:
11197 /* These tags always have simple identifiers already; no need
11198 to canonicalize them. */
11199 part_die->name = DW_STRING (&attr);
11200 break;
11201 default:
11202 part_die->name
11203 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 11204 &objfile->objfile_obstack);
71c25dea
TT
11205 break;
11206 }
c906108c 11207 break;
31ef98ae 11208 case DW_AT_linkage_name:
c906108c 11209 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
11210 /* Note that both forms of linkage name might appear. We
11211 assume they will be the same, and we only store the last
11212 one we see. */
94af9270
KS
11213 if (cu->language == language_ada)
11214 part_die->name = DW_STRING (&attr);
abc72ce4 11215 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
11216 break;
11217 case DW_AT_low_pc:
11218 has_low_pc_attr = 1;
11219 part_die->lowpc = DW_ADDR (&attr);
11220 break;
11221 case DW_AT_high_pc:
11222 has_high_pc_attr = 1;
3019eac3
DE
11223 if (attr.form == DW_FORM_addr
11224 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
11225 part_die->highpc = DW_ADDR (&attr);
11226 else
11227 {
11228 high_pc_relative = 1;
11229 part_die->highpc = DW_UNSND (&attr);
11230 }
c906108c
SS
11231 break;
11232 case DW_AT_location:
0963b4bd 11233 /* Support the .debug_loc offsets. */
8e19ed76
PS
11234 if (attr_form_is_block (&attr))
11235 {
95554aad 11236 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 11237 }
3690dd37 11238 else if (attr_form_is_section_offset (&attr))
8e19ed76 11239 {
4d3c2250 11240 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11241 }
11242 else
11243 {
4d3c2250
KB
11244 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11245 "partial symbol information");
8e19ed76 11246 }
c906108c 11247 break;
c906108c
SS
11248 case DW_AT_external:
11249 part_die->is_external = DW_UNSND (&attr);
11250 break;
11251 case DW_AT_declaration:
11252 part_die->is_declaration = DW_UNSND (&attr);
11253 break;
11254 case DW_AT_type:
11255 part_die->has_type = 1;
11256 break;
11257 case DW_AT_abstract_origin:
11258 case DW_AT_specification:
72bf9492
DJ
11259 case DW_AT_extension:
11260 part_die->has_specification = 1;
c764a876 11261 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
11262 break;
11263 case DW_AT_sibling:
11264 /* Ignore absolute siblings, they might point outside of
11265 the current compile unit. */
11266 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
11267 complaint (&symfile_complaints,
11268 _("ignoring absolute DW_AT_sibling"));
c906108c 11269 else
b64f50a1 11270 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 11271 break;
fa4028e9
JB
11272 case DW_AT_byte_size:
11273 part_die->has_byte_size = 1;
11274 break;
68511cec
CES
11275 case DW_AT_calling_convention:
11276 /* DWARF doesn't provide a way to identify a program's source-level
11277 entry point. DW_AT_calling_convention attributes are only meant
11278 to describe functions' calling conventions.
11279
11280 However, because it's a necessary piece of information in
11281 Fortran, and because DW_CC_program is the only piece of debugging
11282 information whose definition refers to a 'main program' at all,
11283 several compilers have begun marking Fortran main programs with
11284 DW_CC_program --- even when those functions use the standard
11285 calling conventions.
11286
11287 So until DWARF specifies a way to provide this information and
11288 compilers pick up the new representation, we'll support this
11289 practice. */
11290 if (DW_UNSND (&attr) == DW_CC_program
11291 && cu->language == language_fortran)
01f8c46d
JK
11292 {
11293 set_main_name (part_die->name);
11294
11295 /* As this DIE has a static linkage the name would be difficult
11296 to look up later. */
11297 language_of_main = language_fortran;
11298 }
68511cec 11299 break;
481860b3
GB
11300 case DW_AT_inline:
11301 if (DW_UNSND (&attr) == DW_INL_inlined
11302 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11303 part_die->may_be_inlined = 1;
11304 break;
95554aad
TT
11305
11306 case DW_AT_import:
11307 if (part_die->tag == DW_TAG_imported_unit)
11308 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11309 break;
11310
c906108c
SS
11311 default:
11312 break;
11313 }
11314 }
11315
91da1414
MW
11316 if (high_pc_relative)
11317 part_die->highpc += part_die->lowpc;
11318
9373cf26
JK
11319 if (has_low_pc_attr && has_high_pc_attr)
11320 {
11321 /* When using the GNU linker, .gnu.linkonce. sections are used to
11322 eliminate duplicate copies of functions and vtables and such.
11323 The linker will arbitrarily choose one and discard the others.
11324 The AT_*_pc values for such functions refer to local labels in
11325 these sections. If the section from that file was discarded, the
11326 labels are not in the output, so the relocs get a value of 0.
11327 If this is a discarded function, mark the pc bounds as invalid,
11328 so that GDB will ignore it. */
11329 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11330 {
bb5ed363 11331 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11332
11333 complaint (&symfile_complaints,
11334 _("DW_AT_low_pc %s is zero "
11335 "for DIE at 0x%x [in module %s]"),
11336 paddress (gdbarch, part_die->lowpc),
b64f50a1 11337 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11338 }
11339 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11340 else if (part_die->lowpc >= part_die->highpc)
11341 {
bb5ed363 11342 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11343
11344 complaint (&symfile_complaints,
11345 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11346 "for DIE at 0x%x [in module %s]"),
11347 paddress (gdbarch, part_die->lowpc),
11348 paddress (gdbarch, part_die->highpc),
b64f50a1 11349 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11350 }
11351 else
11352 part_die->has_pc_info = 1;
11353 }
85cbf3d3 11354
c906108c
SS
11355 return info_ptr;
11356}
11357
72bf9492
DJ
11358/* Find a cached partial DIE at OFFSET in CU. */
11359
11360static struct partial_die_info *
b64f50a1 11361find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
11362{
11363 struct partial_die_info *lookup_die = NULL;
11364 struct partial_die_info part_die;
11365
11366 part_die.offset = offset;
b64f50a1
JK
11367 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11368 offset.sect_off);
72bf9492 11369
72bf9492
DJ
11370 return lookup_die;
11371}
11372
348e048f
DE
11373/* Find a partial DIE at OFFSET, which may or may not be in CU,
11374 except in the case of .debug_types DIEs which do not reference
11375 outside their CU (they do however referencing other types via
55f1336d 11376 DW_FORM_ref_sig8). */
72bf9492
DJ
11377
11378static struct partial_die_info *
b64f50a1 11379find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
72bf9492 11380{
bb5ed363 11381 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
11382 struct dwarf2_per_cu_data *per_cu = NULL;
11383 struct partial_die_info *pd = NULL;
72bf9492 11384
45452591 11385 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
11386 {
11387 pd = find_partial_die_in_comp_unit (offset, cu);
11388 if (pd != NULL)
11389 return pd;
0d99eb77
DE
11390 /* We missed recording what we needed.
11391 Load all dies and try again. */
11392 per_cu = cu->per_cu;
5afb4e99 11393 }
0d99eb77
DE
11394 else
11395 {
11396 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 11397 if (cu->per_cu->is_debug_types)
0d99eb77
DE
11398 {
11399 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11400 " external reference to offset 0x%lx [in module %s].\n"),
11401 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11402 bfd_get_filename (objfile->obfd));
11403 }
11404 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
72bf9492 11405
0d99eb77
DE
11406 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11407 load_partial_comp_unit (per_cu);
ae038cb0 11408
0d99eb77
DE
11409 per_cu->cu->last_used = 0;
11410 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11411 }
5afb4e99 11412
dee91e82
DE
11413 /* If we didn't find it, and not all dies have been loaded,
11414 load them all and try again. */
11415
5afb4e99
DJ
11416 if (pd == NULL && per_cu->load_all_dies == 0)
11417 {
5afb4e99 11418 per_cu->load_all_dies = 1;
fd820528
DE
11419
11420 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11421 THIS_CU->cu may already be in use. So we can't just free it and
11422 replace its DIEs with the ones we read in. Instead, we leave those
11423 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11424 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11425 set. */
dee91e82 11426 load_partial_comp_unit (per_cu);
5afb4e99
DJ
11427
11428 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11429 }
11430
11431 if (pd == NULL)
11432 internal_error (__FILE__, __LINE__,
3e43a32a
MS
11433 _("could not find partial DIE 0x%x "
11434 "in cache [from module %s]\n"),
b64f50a1 11435 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 11436 return pd;
72bf9492
DJ
11437}
11438
abc72ce4
DE
11439/* See if we can figure out if the class lives in a namespace. We do
11440 this by looking for a member function; its demangled name will
11441 contain namespace info, if there is any. */
11442
11443static void
11444guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11445 struct dwarf2_cu *cu)
11446{
11447 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11448 what template types look like, because the demangler
11449 frequently doesn't give the same name as the debug info. We
11450 could fix this by only using the demangled name to get the
11451 prefix (but see comment in read_structure_type). */
11452
11453 struct partial_die_info *real_pdi;
11454 struct partial_die_info *child_pdi;
11455
11456 /* If this DIE (this DIE's specification, if any) has a parent, then
11457 we should not do this. We'll prepend the parent's fully qualified
11458 name when we create the partial symbol. */
11459
11460 real_pdi = struct_pdi;
11461 while (real_pdi->has_specification)
11462 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11463
11464 if (real_pdi->die_parent != NULL)
11465 return;
11466
11467 for (child_pdi = struct_pdi->die_child;
11468 child_pdi != NULL;
11469 child_pdi = child_pdi->die_sibling)
11470 {
11471 if (child_pdi->tag == DW_TAG_subprogram
11472 && child_pdi->linkage_name != NULL)
11473 {
11474 char *actual_class_name
11475 = language_class_name_from_physname (cu->language_defn,
11476 child_pdi->linkage_name);
11477 if (actual_class_name != NULL)
11478 {
11479 struct_pdi->name
11480 = obsavestring (actual_class_name,
11481 strlen (actual_class_name),
11482 &cu->objfile->objfile_obstack);
11483 xfree (actual_class_name);
11484 }
11485 break;
11486 }
11487 }
11488}
11489
72bf9492
DJ
11490/* Adjust PART_DIE before generating a symbol for it. This function
11491 may set the is_external flag or change the DIE's name. */
11492
11493static void
11494fixup_partial_die (struct partial_die_info *part_die,
11495 struct dwarf2_cu *cu)
11496{
abc72ce4
DE
11497 /* Once we've fixed up a die, there's no point in doing so again.
11498 This also avoids a memory leak if we were to call
11499 guess_partial_die_structure_name multiple times. */
11500 if (part_die->fixup_called)
11501 return;
11502
72bf9492
DJ
11503 /* If we found a reference attribute and the DIE has no name, try
11504 to find a name in the referred to DIE. */
11505
11506 if (part_die->name == NULL && part_die->has_specification)
11507 {
11508 struct partial_die_info *spec_die;
72bf9492 11509
10b3939b 11510 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 11511
10b3939b 11512 fixup_partial_die (spec_die, cu);
72bf9492
DJ
11513
11514 if (spec_die->name)
11515 {
11516 part_die->name = spec_die->name;
11517
11518 /* Copy DW_AT_external attribute if it is set. */
11519 if (spec_die->is_external)
11520 part_die->is_external = spec_die->is_external;
11521 }
11522 }
11523
11524 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
11525
11526 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 11527 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 11528
abc72ce4
DE
11529 /* If there is no parent die to provide a namespace, and there are
11530 children, see if we can determine the namespace from their linkage
122d1940 11531 name. */
abc72ce4 11532 if (cu->language == language_cplus
8b70b953 11533 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
11534 && part_die->die_parent == NULL
11535 && part_die->has_children
11536 && (part_die->tag == DW_TAG_class_type
11537 || part_die->tag == DW_TAG_structure_type
11538 || part_die->tag == DW_TAG_union_type))
11539 guess_partial_die_structure_name (part_die, cu);
11540
53832f31
TT
11541 /* GCC might emit a nameless struct or union that has a linkage
11542 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11543 if (part_die->name == NULL
96408a79
SA
11544 && (part_die->tag == DW_TAG_class_type
11545 || part_die->tag == DW_TAG_interface_type
11546 || part_die->tag == DW_TAG_structure_type
11547 || part_die->tag == DW_TAG_union_type)
53832f31
TT
11548 && part_die->linkage_name != NULL)
11549 {
11550 char *demangled;
11551
11552 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11553 if (demangled)
11554 {
96408a79
SA
11555 const char *base;
11556
11557 /* Strip any leading namespaces/classes, keep only the base name.
11558 DW_AT_name for named DIEs does not contain the prefixes. */
11559 base = strrchr (demangled, ':');
11560 if (base && base > demangled && base[-1] == ':')
11561 base++;
11562 else
11563 base = demangled;
11564
11565 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
11566 &cu->objfile->objfile_obstack);
11567 xfree (demangled);
11568 }
11569 }
11570
abc72ce4 11571 part_die->fixup_called = 1;
72bf9492
DJ
11572}
11573
a8329558 11574/* Read an attribute value described by an attribute form. */
c906108c 11575
fe1b8b76 11576static gdb_byte *
dee91e82
DE
11577read_attribute_value (const struct die_reader_specs *reader,
11578 struct attribute *attr, unsigned form,
11579 gdb_byte *info_ptr)
c906108c 11580{
dee91e82
DE
11581 struct dwarf2_cu *cu = reader->cu;
11582 bfd *abfd = reader->abfd;
e7c27a73 11583 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
11584 unsigned int bytes_read;
11585 struct dwarf_block *blk;
11586
a8329558
KW
11587 attr->form = form;
11588 switch (form)
c906108c 11589 {
c906108c 11590 case DW_FORM_ref_addr:
ae411497 11591 if (cu->header.version == 2)
4568ecf9 11592 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 11593 else
4568ecf9
DE
11594 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11595 &cu->header, &bytes_read);
ae411497
TT
11596 info_ptr += bytes_read;
11597 break;
11598 case DW_FORM_addr:
e7c27a73 11599 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 11600 info_ptr += bytes_read;
c906108c
SS
11601 break;
11602 case DW_FORM_block2:
7b5a2f43 11603 blk = dwarf_alloc_block (cu);
c906108c
SS
11604 blk->size = read_2_bytes (abfd, info_ptr);
11605 info_ptr += 2;
11606 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11607 info_ptr += blk->size;
11608 DW_BLOCK (attr) = blk;
11609 break;
11610 case DW_FORM_block4:
7b5a2f43 11611 blk = dwarf_alloc_block (cu);
c906108c
SS
11612 blk->size = read_4_bytes (abfd, info_ptr);
11613 info_ptr += 4;
11614 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11615 info_ptr += blk->size;
11616 DW_BLOCK (attr) = blk;
11617 break;
11618 case DW_FORM_data2:
11619 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11620 info_ptr += 2;
11621 break;
11622 case DW_FORM_data4:
11623 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11624 info_ptr += 4;
11625 break;
11626 case DW_FORM_data8:
11627 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11628 info_ptr += 8;
11629 break;
2dc7f7b3
TT
11630 case DW_FORM_sec_offset:
11631 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11632 info_ptr += bytes_read;
11633 break;
c906108c 11634 case DW_FORM_string:
9b1c24c8 11635 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 11636 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
11637 info_ptr += bytes_read;
11638 break;
4bdf3d34
JJ
11639 case DW_FORM_strp:
11640 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11641 &bytes_read);
8285870a 11642 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
11643 info_ptr += bytes_read;
11644 break;
2dc7f7b3 11645 case DW_FORM_exprloc:
c906108c 11646 case DW_FORM_block:
7b5a2f43 11647 blk = dwarf_alloc_block (cu);
c906108c
SS
11648 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11649 info_ptr += bytes_read;
11650 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11651 info_ptr += blk->size;
11652 DW_BLOCK (attr) = blk;
11653 break;
11654 case DW_FORM_block1:
7b5a2f43 11655 blk = dwarf_alloc_block (cu);
c906108c
SS
11656 blk->size = read_1_byte (abfd, info_ptr);
11657 info_ptr += 1;
11658 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11659 info_ptr += blk->size;
11660 DW_BLOCK (attr) = blk;
11661 break;
11662 case DW_FORM_data1:
11663 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11664 info_ptr += 1;
11665 break;
11666 case DW_FORM_flag:
11667 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11668 info_ptr += 1;
11669 break;
2dc7f7b3
TT
11670 case DW_FORM_flag_present:
11671 DW_UNSND (attr) = 1;
11672 break;
c906108c
SS
11673 case DW_FORM_sdata:
11674 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11675 info_ptr += bytes_read;
11676 break;
11677 case DW_FORM_udata:
11678 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11679 info_ptr += bytes_read;
11680 break;
11681 case DW_FORM_ref1:
4568ecf9
DE
11682 DW_UNSND (attr) = (cu->header.offset.sect_off
11683 + read_1_byte (abfd, info_ptr));
c906108c
SS
11684 info_ptr += 1;
11685 break;
11686 case DW_FORM_ref2:
4568ecf9
DE
11687 DW_UNSND (attr) = (cu->header.offset.sect_off
11688 + read_2_bytes (abfd, info_ptr));
c906108c
SS
11689 info_ptr += 2;
11690 break;
11691 case DW_FORM_ref4:
4568ecf9
DE
11692 DW_UNSND (attr) = (cu->header.offset.sect_off
11693 + read_4_bytes (abfd, info_ptr));
c906108c
SS
11694 info_ptr += 4;
11695 break;
613e1657 11696 case DW_FORM_ref8:
4568ecf9
DE
11697 DW_UNSND (attr) = (cu->header.offset.sect_off
11698 + read_8_bytes (abfd, info_ptr));
613e1657
KB
11699 info_ptr += 8;
11700 break;
55f1336d 11701 case DW_FORM_ref_sig8:
348e048f
DE
11702 /* Convert the signature to something we can record in DW_UNSND
11703 for later lookup.
11704 NOTE: This is NULL if the type wasn't found. */
11705 DW_SIGNATURED_TYPE (attr) =
e319fa28 11706 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
11707 info_ptr += 8;
11708 break;
c906108c 11709 case DW_FORM_ref_udata:
4568ecf9
DE
11710 DW_UNSND (attr) = (cu->header.offset.sect_off
11711 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
11712 info_ptr += bytes_read;
11713 break;
c906108c 11714 case DW_FORM_indirect:
a8329558
KW
11715 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11716 info_ptr += bytes_read;
dee91e82 11717 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 11718 break;
3019eac3
DE
11719 case DW_FORM_GNU_addr_index:
11720 if (reader->dwo_file == NULL)
11721 {
11722 /* For now flag a hard error.
11723 Later we can turn this into a complaint. */
11724 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11725 dwarf_form_name (form),
11726 bfd_get_filename (abfd));
11727 }
11728 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11729 info_ptr += bytes_read;
11730 break;
11731 case DW_FORM_GNU_str_index:
11732 if (reader->dwo_file == NULL)
11733 {
11734 /* For now flag a hard error.
11735 Later we can turn this into a complaint if warranted. */
11736 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11737 dwarf_form_name (form),
11738 bfd_get_filename (abfd));
11739 }
11740 {
11741 ULONGEST str_index =
11742 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11743
11744 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11745 DW_STRING_IS_CANONICAL (attr) = 0;
11746 info_ptr += bytes_read;
11747 }
11748 break;
c906108c 11749 default:
8a3fe4f8 11750 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
11751 dwarf_form_name (form),
11752 bfd_get_filename (abfd));
c906108c 11753 }
28e94949
JB
11754
11755 /* We have seen instances where the compiler tried to emit a byte
11756 size attribute of -1 which ended up being encoded as an unsigned
11757 0xffffffff. Although 0xffffffff is technically a valid size value,
11758 an object of this size seems pretty unlikely so we can relatively
11759 safely treat these cases as if the size attribute was invalid and
11760 treat them as zero by default. */
11761 if (attr->name == DW_AT_byte_size
11762 && form == DW_FORM_data4
11763 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
11764 {
11765 complaint
11766 (&symfile_complaints,
43bbcdc2
PH
11767 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
11768 hex_string (DW_UNSND (attr)));
01c66ae6
JB
11769 DW_UNSND (attr) = 0;
11770 }
28e94949 11771
c906108c
SS
11772 return info_ptr;
11773}
11774
a8329558
KW
11775/* Read an attribute described by an abbreviated attribute. */
11776
fe1b8b76 11777static gdb_byte *
dee91e82
DE
11778read_attribute (const struct die_reader_specs *reader,
11779 struct attribute *attr, struct attr_abbrev *abbrev,
11780 gdb_byte *info_ptr)
a8329558
KW
11781{
11782 attr->name = abbrev->name;
dee91e82 11783 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
11784}
11785
0963b4bd 11786/* Read dwarf information from a buffer. */
c906108c
SS
11787
11788static unsigned int
fe1b8b76 11789read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 11790{
fe1b8b76 11791 return bfd_get_8 (abfd, buf);
c906108c
SS
11792}
11793
11794static int
fe1b8b76 11795read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 11796{
fe1b8b76 11797 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
11798}
11799
11800static unsigned int
fe1b8b76 11801read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11802{
fe1b8b76 11803 return bfd_get_16 (abfd, buf);
c906108c
SS
11804}
11805
21ae7a4d
JK
11806static int
11807read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
11808{
11809 return bfd_get_signed_16 (abfd, buf);
11810}
11811
c906108c 11812static unsigned int
fe1b8b76 11813read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11814{
fe1b8b76 11815 return bfd_get_32 (abfd, buf);
c906108c
SS
11816}
11817
21ae7a4d
JK
11818static int
11819read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
11820{
11821 return bfd_get_signed_32 (abfd, buf);
11822}
11823
93311388 11824static ULONGEST
fe1b8b76 11825read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11826{
fe1b8b76 11827 return bfd_get_64 (abfd, buf);
c906108c
SS
11828}
11829
11830static CORE_ADDR
fe1b8b76 11831read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 11832 unsigned int *bytes_read)
c906108c 11833{
e7c27a73 11834 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
11835 CORE_ADDR retval = 0;
11836
107d2387 11837 if (cu_header->signed_addr_p)
c906108c 11838 {
107d2387
AC
11839 switch (cu_header->addr_size)
11840 {
11841 case 2:
fe1b8b76 11842 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
11843 break;
11844 case 4:
fe1b8b76 11845 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
11846 break;
11847 case 8:
fe1b8b76 11848 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
11849 break;
11850 default:
8e65ff28 11851 internal_error (__FILE__, __LINE__,
e2e0b3e5 11852 _("read_address: bad switch, signed [in module %s]"),
659b0389 11853 bfd_get_filename (abfd));
107d2387
AC
11854 }
11855 }
11856 else
11857 {
11858 switch (cu_header->addr_size)
11859 {
11860 case 2:
fe1b8b76 11861 retval = bfd_get_16 (abfd, buf);
107d2387
AC
11862 break;
11863 case 4:
fe1b8b76 11864 retval = bfd_get_32 (abfd, buf);
107d2387
AC
11865 break;
11866 case 8:
fe1b8b76 11867 retval = bfd_get_64 (abfd, buf);
107d2387
AC
11868 break;
11869 default:
8e65ff28 11870 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
11871 _("read_address: bad switch, "
11872 "unsigned [in module %s]"),
659b0389 11873 bfd_get_filename (abfd));
107d2387 11874 }
c906108c 11875 }
64367e0a 11876
107d2387
AC
11877 *bytes_read = cu_header->addr_size;
11878 return retval;
c906108c
SS
11879}
11880
f7ef9339 11881/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
11882 specification allows the initial length to take up either 4 bytes
11883 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
11884 bytes describe the length and all offsets will be 8 bytes in length
11885 instead of 4.
11886
f7ef9339
KB
11887 An older, non-standard 64-bit format is also handled by this
11888 function. The older format in question stores the initial length
11889 as an 8-byte quantity without an escape value. Lengths greater
11890 than 2^32 aren't very common which means that the initial 4 bytes
11891 is almost always zero. Since a length value of zero doesn't make
11892 sense for the 32-bit format, this initial zero can be considered to
11893 be an escape value which indicates the presence of the older 64-bit
11894 format. As written, the code can't detect (old format) lengths
917c78fc
MK
11895 greater than 4GB. If it becomes necessary to handle lengths
11896 somewhat larger than 4GB, we could allow other small values (such
11897 as the non-sensical values of 1, 2, and 3) to also be used as
11898 escape values indicating the presence of the old format.
f7ef9339 11899
917c78fc
MK
11900 The value returned via bytes_read should be used to increment the
11901 relevant pointer after calling read_initial_length().
c764a876 11902
613e1657
KB
11903 [ Note: read_initial_length() and read_offset() are based on the
11904 document entitled "DWARF Debugging Information Format", revision
f7ef9339 11905 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
11906 from:
11907
f7ef9339 11908 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 11909
613e1657
KB
11910 This document is only a draft and is subject to change. (So beware.)
11911
f7ef9339 11912 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
11913 determined empirically by examining 64-bit ELF files produced by
11914 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
11915
11916 - Kevin, July 16, 2002
613e1657
KB
11917 ] */
11918
11919static LONGEST
c764a876 11920read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 11921{
fe1b8b76 11922 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 11923
dd373385 11924 if (length == 0xffffffff)
613e1657 11925 {
fe1b8b76 11926 length = bfd_get_64 (abfd, buf + 4);
613e1657 11927 *bytes_read = 12;
613e1657 11928 }
dd373385 11929 else if (length == 0)
f7ef9339 11930 {
dd373385 11931 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 11932 length = bfd_get_64 (abfd, buf);
f7ef9339 11933 *bytes_read = 8;
f7ef9339 11934 }
613e1657
KB
11935 else
11936 {
11937 *bytes_read = 4;
613e1657
KB
11938 }
11939
c764a876
DE
11940 return length;
11941}
dd373385 11942
c764a876
DE
11943/* Cover function for read_initial_length.
11944 Returns the length of the object at BUF, and stores the size of the
11945 initial length in *BYTES_READ and stores the size that offsets will be in
11946 *OFFSET_SIZE.
11947 If the initial length size is not equivalent to that specified in
11948 CU_HEADER then issue a complaint.
11949 This is useful when reading non-comp-unit headers. */
dd373385 11950
c764a876
DE
11951static LONGEST
11952read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
11953 const struct comp_unit_head *cu_header,
11954 unsigned int *bytes_read,
11955 unsigned int *offset_size)
11956{
11957 LONGEST length = read_initial_length (abfd, buf, bytes_read);
11958
11959 gdb_assert (cu_header->initial_length_size == 4
11960 || cu_header->initial_length_size == 8
11961 || cu_header->initial_length_size == 12);
11962
11963 if (cu_header->initial_length_size != *bytes_read)
11964 complaint (&symfile_complaints,
11965 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 11966
c764a876 11967 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 11968 return length;
613e1657
KB
11969}
11970
11971/* Read an offset from the data stream. The size of the offset is
917c78fc 11972 given by cu_header->offset_size. */
613e1657
KB
11973
11974static LONGEST
fe1b8b76 11975read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 11976 unsigned int *bytes_read)
c764a876
DE
11977{
11978 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 11979
c764a876
DE
11980 *bytes_read = cu_header->offset_size;
11981 return offset;
11982}
11983
11984/* Read an offset from the data stream. */
11985
11986static LONGEST
11987read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
11988{
11989 LONGEST retval = 0;
11990
c764a876 11991 switch (offset_size)
613e1657
KB
11992 {
11993 case 4:
fe1b8b76 11994 retval = bfd_get_32 (abfd, buf);
613e1657
KB
11995 break;
11996 case 8:
fe1b8b76 11997 retval = bfd_get_64 (abfd, buf);
613e1657
KB
11998 break;
11999 default:
8e65ff28 12000 internal_error (__FILE__, __LINE__,
c764a876 12001 _("read_offset_1: bad switch [in module %s]"),
659b0389 12002 bfd_get_filename (abfd));
613e1657
KB
12003 }
12004
917c78fc 12005 return retval;
613e1657
KB
12006}
12007
fe1b8b76
JB
12008static gdb_byte *
12009read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
12010{
12011 /* If the size of a host char is 8 bits, we can return a pointer
12012 to the buffer, otherwise we have to copy the data to a buffer
12013 allocated on the temporary obstack. */
4bdf3d34 12014 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 12015 return buf;
c906108c
SS
12016}
12017
12018static char *
9b1c24c8 12019read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
12020{
12021 /* If the size of a host char is 8 bits, we can return a pointer
12022 to the string, otherwise we have to copy the string to a buffer
12023 allocated on the temporary obstack. */
4bdf3d34 12024 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
12025 if (*buf == '\0')
12026 {
12027 *bytes_read_ptr = 1;
12028 return NULL;
12029 }
fe1b8b76
JB
12030 *bytes_read_ptr = strlen ((char *) buf) + 1;
12031 return (char *) buf;
4bdf3d34
JJ
12032}
12033
12034static char *
cf2c3c16 12035read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 12036{
be391dca 12037 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 12038 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
12039 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12040 bfd_get_filename (abfd));
dce234bc 12041 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
12042 error (_("DW_FORM_strp pointing outside of "
12043 ".debug_str section [in module %s]"),
12044 bfd_get_filename (abfd));
4bdf3d34 12045 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 12046 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 12047 return NULL;
dce234bc 12048 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
12049}
12050
cf2c3c16
TT
12051static char *
12052read_indirect_string (bfd *abfd, gdb_byte *buf,
12053 const struct comp_unit_head *cu_header,
12054 unsigned int *bytes_read_ptr)
12055{
12056 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12057
12058 return read_indirect_string_at_offset (abfd, str_offset);
12059}
12060
12df843f 12061static ULONGEST
fe1b8b76 12062read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12063{
12df843f 12064 ULONGEST result;
ce5d95e1 12065 unsigned int num_read;
c906108c
SS
12066 int i, shift;
12067 unsigned char byte;
12068
12069 result = 0;
12070 shift = 0;
12071 num_read = 0;
12072 i = 0;
12073 while (1)
12074 {
fe1b8b76 12075 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12076 buf++;
12077 num_read++;
12df843f 12078 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
12079 if ((byte & 128) == 0)
12080 {
12081 break;
12082 }
12083 shift += 7;
12084 }
12085 *bytes_read_ptr = num_read;
12086 return result;
12087}
12088
12df843f 12089static LONGEST
fe1b8b76 12090read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12091{
12df843f 12092 LONGEST result;
77e0b926 12093 int i, shift, num_read;
c906108c
SS
12094 unsigned char byte;
12095
12096 result = 0;
12097 shift = 0;
c906108c
SS
12098 num_read = 0;
12099 i = 0;
12100 while (1)
12101 {
fe1b8b76 12102 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12103 buf++;
12104 num_read++;
12df843f 12105 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
12106 shift += 7;
12107 if ((byte & 128) == 0)
12108 {
12109 break;
12110 }
12111 }
77e0b926 12112 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 12113 result |= -(((LONGEST) 1) << shift);
c906108c
SS
12114 *bytes_read_ptr = num_read;
12115 return result;
12116}
12117
3019eac3
DE
12118/* Given index ADDR_INDEX in .debug_addr, fetch the value.
12119 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12120 ADDR_SIZE is the size of addresses from the CU header. */
12121
12122static CORE_ADDR
12123read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12124{
12125 struct objfile *objfile = dwarf2_per_objfile->objfile;
12126 bfd *abfd = objfile->obfd;
12127 const gdb_byte *info_ptr;
12128
12129 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12130 if (dwarf2_per_objfile->addr.buffer == NULL)
12131 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12132 objfile->name);
12133 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12134 error (_("DW_FORM_addr_index pointing outside of "
12135 ".debug_addr section [in module %s]"),
12136 objfile->name);
12137 info_ptr = (dwarf2_per_objfile->addr.buffer
12138 + addr_base + addr_index * addr_size);
12139 if (addr_size == 4)
12140 return bfd_get_32 (abfd, info_ptr);
12141 else
12142 return bfd_get_64 (abfd, info_ptr);
12143}
12144
12145/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12146
12147static CORE_ADDR
12148read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12149{
12150 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12151}
12152
12153/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12154
12155static CORE_ADDR
12156read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12157 unsigned int *bytes_read)
12158{
12159 bfd *abfd = cu->objfile->obfd;
12160 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12161
12162 return read_addr_index (cu, addr_index);
12163}
12164
12165/* Data structure to pass results from dwarf2_read_addr_index_reader
12166 back to dwarf2_read_addr_index. */
12167
12168struct dwarf2_read_addr_index_data
12169{
12170 ULONGEST addr_base;
12171 int addr_size;
12172};
12173
12174/* die_reader_func for dwarf2_read_addr_index. */
12175
12176static void
12177dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12178 gdb_byte *info_ptr,
12179 struct die_info *comp_unit_die,
12180 int has_children,
12181 void *data)
12182{
12183 struct dwarf2_cu *cu = reader->cu;
12184 struct dwarf2_read_addr_index_data *aidata =
12185 (struct dwarf2_read_addr_index_data *) data;
12186
12187 aidata->addr_base = cu->addr_base;
12188 aidata->addr_size = cu->header.addr_size;
12189}
12190
12191/* Given an index in .debug_addr, fetch the value.
12192 NOTE: This can be called during dwarf expression evaluation,
12193 long after the debug information has been read, and thus per_cu->cu
12194 may no longer exist. */
12195
12196CORE_ADDR
12197dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12198 unsigned int addr_index)
12199{
12200 struct objfile *objfile = per_cu->objfile;
12201 struct dwarf2_cu *cu = per_cu->cu;
12202 ULONGEST addr_base;
12203 int addr_size;
12204
12205 /* This is intended to be called from outside this file. */
12206 dw2_setup (objfile);
12207
12208 /* We need addr_base and addr_size.
12209 If we don't have PER_CU->cu, we have to get it.
12210 Nasty, but the alternative is storing the needed info in PER_CU,
12211 which at this point doesn't seem justified: it's not clear how frequently
12212 it would get used and it would increase the size of every PER_CU.
12213 Entry points like dwarf2_per_cu_addr_size do a similar thing
12214 so we're not in uncharted territory here.
12215 Alas we need to be a bit more complicated as addr_base is contained
12216 in the DIE.
12217
12218 We don't need to read the entire CU(/TU).
12219 We just need the header and top level die.
12220 IWBN to use the aging mechanism to let us lazily later discard the CU.
12221 See however init_cutu_and_read_dies_simple. */
12222
12223 if (cu != NULL)
12224 {
12225 addr_base = cu->addr_base;
12226 addr_size = cu->header.addr_size;
12227 }
12228 else
12229 {
12230 struct dwarf2_read_addr_index_data aidata;
12231
12232 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12233 &aidata);
12234 addr_base = aidata.addr_base;
12235 addr_size = aidata.addr_size;
12236 }
12237
12238 return read_addr_index_1 (addr_index, addr_base, addr_size);
12239}
12240
12241/* Given a DW_AT_str_index, fetch the string. */
12242
12243static char *
12244read_str_index (const struct die_reader_specs *reader,
12245 struct dwarf2_cu *cu, ULONGEST str_index)
12246{
12247 struct objfile *objfile = dwarf2_per_objfile->objfile;
12248 const char *dwo_name = objfile->name;
12249 bfd *abfd = objfile->obfd;
12250 struct dwo_sections *sections = &reader->dwo_file->sections;
12251 gdb_byte *info_ptr;
12252 ULONGEST str_offset;
12253
12254 dwarf2_read_section (objfile, &sections->str);
12255 dwarf2_read_section (objfile, &sections->str_offsets);
12256 if (sections->str.buffer == NULL)
12257 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12258 " in CU at offset 0x%lx [in module %s]"),
12259 (long) cu->header.offset.sect_off, dwo_name);
12260 if (sections->str_offsets.buffer == NULL)
12261 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12262 " in CU at offset 0x%lx [in module %s]"),
12263 (long) cu->header.offset.sect_off, dwo_name);
12264 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12265 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12266 " section in CU at offset 0x%lx [in module %s]"),
12267 (long) cu->header.offset.sect_off, dwo_name);
12268 info_ptr = (sections->str_offsets.buffer
12269 + str_index * cu->header.offset_size);
12270 if (cu->header.offset_size == 4)
12271 str_offset = bfd_get_32 (abfd, info_ptr);
12272 else
12273 str_offset = bfd_get_64 (abfd, info_ptr);
12274 if (str_offset >= sections->str.size)
12275 error (_("Offset from DW_FORM_str_index pointing outside of"
12276 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12277 (long) cu->header.offset.sect_off, dwo_name);
12278 return (char *) (sections->str.buffer + str_offset);
12279}
12280
4bb7a0a7
DJ
12281/* Return a pointer to just past the end of an LEB128 number in BUF. */
12282
fe1b8b76
JB
12283static gdb_byte *
12284skip_leb128 (bfd *abfd, gdb_byte *buf)
4bb7a0a7
DJ
12285{
12286 int byte;
12287
12288 while (1)
12289 {
fe1b8b76 12290 byte = bfd_get_8 (abfd, buf);
4bb7a0a7
DJ
12291 buf++;
12292 if ((byte & 128) == 0)
12293 return buf;
12294 }
12295}
12296
3019eac3
DE
12297/* Return the length of an LEB128 number in BUF. */
12298
12299static int
12300leb128_size (const gdb_byte *buf)
12301{
12302 const gdb_byte *begin = buf;
12303 gdb_byte byte;
12304
12305 while (1)
12306 {
12307 byte = *buf++;
12308 if ((byte & 128) == 0)
12309 return buf - begin;
12310 }
12311}
12312
c906108c 12313static void
e142c38c 12314set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
12315{
12316 switch (lang)
12317 {
12318 case DW_LANG_C89:
76bee0cc 12319 case DW_LANG_C99:
c906108c 12320 case DW_LANG_C:
e142c38c 12321 cu->language = language_c;
c906108c
SS
12322 break;
12323 case DW_LANG_C_plus_plus:
e142c38c 12324 cu->language = language_cplus;
c906108c 12325 break;
6aecb9c2
JB
12326 case DW_LANG_D:
12327 cu->language = language_d;
12328 break;
c906108c
SS
12329 case DW_LANG_Fortran77:
12330 case DW_LANG_Fortran90:
b21b22e0 12331 case DW_LANG_Fortran95:
e142c38c 12332 cu->language = language_fortran;
c906108c 12333 break;
a766d390
DE
12334 case DW_LANG_Go:
12335 cu->language = language_go;
12336 break;
c906108c 12337 case DW_LANG_Mips_Assembler:
e142c38c 12338 cu->language = language_asm;
c906108c 12339 break;
bebd888e 12340 case DW_LANG_Java:
e142c38c 12341 cu->language = language_java;
bebd888e 12342 break;
c906108c 12343 case DW_LANG_Ada83:
8aaf0b47 12344 case DW_LANG_Ada95:
bc5f45f8
JB
12345 cu->language = language_ada;
12346 break;
72019c9c
GM
12347 case DW_LANG_Modula2:
12348 cu->language = language_m2;
12349 break;
fe8e67fd
PM
12350 case DW_LANG_Pascal83:
12351 cu->language = language_pascal;
12352 break;
22566fbd
DJ
12353 case DW_LANG_ObjC:
12354 cu->language = language_objc;
12355 break;
c906108c
SS
12356 case DW_LANG_Cobol74:
12357 case DW_LANG_Cobol85:
c906108c 12358 default:
e142c38c 12359 cu->language = language_minimal;
c906108c
SS
12360 break;
12361 }
e142c38c 12362 cu->language_defn = language_def (cu->language);
c906108c
SS
12363}
12364
12365/* Return the named attribute or NULL if not there. */
12366
12367static struct attribute *
e142c38c 12368dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 12369{
a48e046c 12370 for (;;)
c906108c 12371 {
a48e046c
TT
12372 unsigned int i;
12373 struct attribute *spec = NULL;
12374
12375 for (i = 0; i < die->num_attrs; ++i)
12376 {
12377 if (die->attrs[i].name == name)
12378 return &die->attrs[i];
12379 if (die->attrs[i].name == DW_AT_specification
12380 || die->attrs[i].name == DW_AT_abstract_origin)
12381 spec = &die->attrs[i];
12382 }
12383
12384 if (!spec)
12385 break;
c906108c 12386
f2f0e013 12387 die = follow_die_ref (die, spec, &cu);
f2f0e013 12388 }
c5aa993b 12389
c906108c
SS
12390 return NULL;
12391}
12392
348e048f
DE
12393/* Return the named attribute or NULL if not there,
12394 but do not follow DW_AT_specification, etc.
12395 This is for use in contexts where we're reading .debug_types dies.
12396 Following DW_AT_specification, DW_AT_abstract_origin will take us
12397 back up the chain, and we want to go down. */
12398
12399static struct attribute *
12400dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12401 struct dwarf2_cu *cu)
12402{
12403 unsigned int i;
12404
12405 for (i = 0; i < die->num_attrs; ++i)
12406 if (die->attrs[i].name == name)
12407 return &die->attrs[i];
12408
12409 return NULL;
12410}
12411
05cf31d1
JB
12412/* Return non-zero iff the attribute NAME is defined for the given DIE,
12413 and holds a non-zero value. This function should only be used for
2dc7f7b3 12414 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
12415
12416static int
12417dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12418{
12419 struct attribute *attr = dwarf2_attr (die, name, cu);
12420
12421 return (attr && DW_UNSND (attr));
12422}
12423
3ca72b44 12424static int
e142c38c 12425die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 12426{
05cf31d1
JB
12427 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12428 which value is non-zero. However, we have to be careful with
12429 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12430 (via dwarf2_flag_true_p) follows this attribute. So we may
12431 end up accidently finding a declaration attribute that belongs
12432 to a different DIE referenced by the specification attribute,
12433 even though the given DIE does not have a declaration attribute. */
12434 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12435 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
12436}
12437
63d06c5c 12438/* Return the die giving the specification for DIE, if there is
f2f0e013 12439 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
12440 containing the return value on output. If there is no
12441 specification, but there is an abstract origin, that is
12442 returned. */
63d06c5c
DC
12443
12444static struct die_info *
f2f0e013 12445die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 12446{
f2f0e013
DJ
12447 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12448 *spec_cu);
63d06c5c 12449
edb3359d
DJ
12450 if (spec_attr == NULL)
12451 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12452
63d06c5c
DC
12453 if (spec_attr == NULL)
12454 return NULL;
12455 else
f2f0e013 12456 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 12457}
c906108c 12458
debd256d 12459/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
12460 refers to.
12461 NOTE: This is also used as a "cleanup" function. */
12462
debd256d
JB
12463static void
12464free_line_header (struct line_header *lh)
12465{
12466 if (lh->standard_opcode_lengths)
a8bc7b56 12467 xfree (lh->standard_opcode_lengths);
debd256d
JB
12468
12469 /* Remember that all the lh->file_names[i].name pointers are
12470 pointers into debug_line_buffer, and don't need to be freed. */
12471 if (lh->file_names)
a8bc7b56 12472 xfree (lh->file_names);
debd256d
JB
12473
12474 /* Similarly for the include directory names. */
12475 if (lh->include_dirs)
a8bc7b56 12476 xfree (lh->include_dirs);
debd256d 12477
a8bc7b56 12478 xfree (lh);
debd256d
JB
12479}
12480
debd256d 12481/* Add an entry to LH's include directory table. */
ae2de4f8 12482
debd256d
JB
12483static void
12484add_include_dir (struct line_header *lh, char *include_dir)
c906108c 12485{
debd256d
JB
12486 /* Grow the array if necessary. */
12487 if (lh->include_dirs_size == 0)
c5aa993b 12488 {
debd256d
JB
12489 lh->include_dirs_size = 1; /* for testing */
12490 lh->include_dirs = xmalloc (lh->include_dirs_size
12491 * sizeof (*lh->include_dirs));
12492 }
12493 else if (lh->num_include_dirs >= lh->include_dirs_size)
12494 {
12495 lh->include_dirs_size *= 2;
12496 lh->include_dirs = xrealloc (lh->include_dirs,
12497 (lh->include_dirs_size
12498 * sizeof (*lh->include_dirs)));
c5aa993b 12499 }
c906108c 12500
debd256d
JB
12501 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12502}
6e70227d 12503
debd256d 12504/* Add an entry to LH's file name table. */
ae2de4f8 12505
debd256d
JB
12506static void
12507add_file_name (struct line_header *lh,
12508 char *name,
12509 unsigned int dir_index,
12510 unsigned int mod_time,
12511 unsigned int length)
12512{
12513 struct file_entry *fe;
12514
12515 /* Grow the array if necessary. */
12516 if (lh->file_names_size == 0)
12517 {
12518 lh->file_names_size = 1; /* for testing */
12519 lh->file_names = xmalloc (lh->file_names_size
12520 * sizeof (*lh->file_names));
12521 }
12522 else if (lh->num_file_names >= lh->file_names_size)
12523 {
12524 lh->file_names_size *= 2;
12525 lh->file_names = xrealloc (lh->file_names,
12526 (lh->file_names_size
12527 * sizeof (*lh->file_names)));
12528 }
12529
12530 fe = &lh->file_names[lh->num_file_names++];
12531 fe->name = name;
12532 fe->dir_index = dir_index;
12533 fe->mod_time = mod_time;
12534 fe->length = length;
aaa75496 12535 fe->included_p = 0;
cb1df416 12536 fe->symtab = NULL;
debd256d 12537}
6e70227d 12538
debd256d 12539/* Read the statement program header starting at OFFSET in
3019eac3 12540 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 12541 to a struct line_header, allocated using xmalloc.
debd256d
JB
12542
12543 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
12544 the returned object point into the dwarf line section buffer,
12545 and must not be freed. */
ae2de4f8 12546
debd256d 12547static struct line_header *
3019eac3 12548dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
12549{
12550 struct cleanup *back_to;
12551 struct line_header *lh;
fe1b8b76 12552 gdb_byte *line_ptr;
c764a876 12553 unsigned int bytes_read, offset_size;
debd256d
JB
12554 int i;
12555 char *cur_dir, *cur_file;
3019eac3
DE
12556 struct dwarf2_section_info *section;
12557 bfd *abfd;
12558
12559 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12560 DWO file. */
12561 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12562 section = &cu->dwo_unit->dwo_file->sections.line;
12563 else
12564 section = &dwarf2_per_objfile->line;
debd256d 12565
3019eac3
DE
12566 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12567 if (section->buffer == NULL)
debd256d 12568 {
3019eac3
DE
12569 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12570 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12571 else
12572 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
12573 return 0;
12574 }
12575
fceca515
DE
12576 /* We can't do this until we know the section is non-empty.
12577 Only then do we know we have such a section. */
12578 abfd = section->asection->owner;
12579
a738430d
MK
12580 /* Make sure that at least there's room for the total_length field.
12581 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 12582 if (offset + 4 >= section->size)
debd256d 12583 {
4d3c2250 12584 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12585 return 0;
12586 }
12587
12588 lh = xmalloc (sizeof (*lh));
12589 memset (lh, 0, sizeof (*lh));
12590 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12591 (void *) lh);
12592
3019eac3 12593 line_ptr = section->buffer + offset;
debd256d 12594
a738430d 12595 /* Read in the header. */
6e70227d 12596 lh->total_length =
c764a876
DE
12597 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12598 &bytes_read, &offset_size);
debd256d 12599 line_ptr += bytes_read;
3019eac3 12600 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 12601 {
4d3c2250 12602 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12603 return 0;
12604 }
12605 lh->statement_program_end = line_ptr + lh->total_length;
12606 lh->version = read_2_bytes (abfd, line_ptr);
12607 line_ptr += 2;
c764a876
DE
12608 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12609 line_ptr += offset_size;
debd256d
JB
12610 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12611 line_ptr += 1;
2dc7f7b3
TT
12612 if (lh->version >= 4)
12613 {
12614 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12615 line_ptr += 1;
12616 }
12617 else
12618 lh->maximum_ops_per_instruction = 1;
12619
12620 if (lh->maximum_ops_per_instruction == 0)
12621 {
12622 lh->maximum_ops_per_instruction = 1;
12623 complaint (&symfile_complaints,
3e43a32a
MS
12624 _("invalid maximum_ops_per_instruction "
12625 "in `.debug_line' section"));
2dc7f7b3
TT
12626 }
12627
debd256d
JB
12628 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12629 line_ptr += 1;
12630 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12631 line_ptr += 1;
12632 lh->line_range = read_1_byte (abfd, line_ptr);
12633 line_ptr += 1;
12634 lh->opcode_base = read_1_byte (abfd, line_ptr);
12635 line_ptr += 1;
12636 lh->standard_opcode_lengths
fe1b8b76 12637 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
12638
12639 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12640 for (i = 1; i < lh->opcode_base; ++i)
12641 {
12642 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12643 line_ptr += 1;
12644 }
12645
a738430d 12646 /* Read directory table. */
9b1c24c8 12647 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12648 {
12649 line_ptr += bytes_read;
12650 add_include_dir (lh, cur_dir);
12651 }
12652 line_ptr += bytes_read;
12653
a738430d 12654 /* Read file name table. */
9b1c24c8 12655 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12656 {
12657 unsigned int dir_index, mod_time, length;
12658
12659 line_ptr += bytes_read;
12660 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12661 line_ptr += bytes_read;
12662 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12663 line_ptr += bytes_read;
12664 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12665 line_ptr += bytes_read;
12666
12667 add_file_name (lh, cur_file, dir_index, mod_time, length);
12668 }
12669 line_ptr += bytes_read;
6e70227d 12670 lh->statement_program_start = line_ptr;
debd256d 12671
3019eac3 12672 if (line_ptr > (section->buffer + section->size))
4d3c2250 12673 complaint (&symfile_complaints,
3e43a32a
MS
12674 _("line number info header doesn't "
12675 "fit in `.debug_line' section"));
debd256d
JB
12676
12677 discard_cleanups (back_to);
12678 return lh;
12679}
c906108c 12680
c6da4cef
DE
12681/* Subroutine of dwarf_decode_lines to simplify it.
12682 Return the file name of the psymtab for included file FILE_INDEX
12683 in line header LH of PST.
12684 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12685 If space for the result is malloc'd, it will be freed by a cleanup.
12686 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12687
12688static char *
12689psymtab_include_file_name (const struct line_header *lh, int file_index,
12690 const struct partial_symtab *pst,
12691 const char *comp_dir)
12692{
12693 const struct file_entry fe = lh->file_names [file_index];
12694 char *include_name = fe.name;
12695 char *include_name_to_compare = include_name;
12696 char *dir_name = NULL;
72b9f47f
TT
12697 const char *pst_filename;
12698 char *copied_name = NULL;
c6da4cef
DE
12699 int file_is_pst;
12700
12701 if (fe.dir_index)
12702 dir_name = lh->include_dirs[fe.dir_index - 1];
12703
12704 if (!IS_ABSOLUTE_PATH (include_name)
12705 && (dir_name != NULL || comp_dir != NULL))
12706 {
12707 /* Avoid creating a duplicate psymtab for PST.
12708 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12709 Before we do the comparison, however, we need to account
12710 for DIR_NAME and COMP_DIR.
12711 First prepend dir_name (if non-NULL). If we still don't
12712 have an absolute path prepend comp_dir (if non-NULL).
12713 However, the directory we record in the include-file's
12714 psymtab does not contain COMP_DIR (to match the
12715 corresponding symtab(s)).
12716
12717 Example:
12718
12719 bash$ cd /tmp
12720 bash$ gcc -g ./hello.c
12721 include_name = "hello.c"
12722 dir_name = "."
12723 DW_AT_comp_dir = comp_dir = "/tmp"
12724 DW_AT_name = "./hello.c" */
12725
12726 if (dir_name != NULL)
12727 {
12728 include_name = concat (dir_name, SLASH_STRING,
12729 include_name, (char *)NULL);
12730 include_name_to_compare = include_name;
12731 make_cleanup (xfree, include_name);
12732 }
12733 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12734 {
12735 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12736 include_name, (char *)NULL);
12737 }
12738 }
12739
12740 pst_filename = pst->filename;
12741 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12742 {
72b9f47f
TT
12743 copied_name = concat (pst->dirname, SLASH_STRING,
12744 pst_filename, (char *)NULL);
12745 pst_filename = copied_name;
c6da4cef
DE
12746 }
12747
1e3fad37 12748 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
12749
12750 if (include_name_to_compare != include_name)
12751 xfree (include_name_to_compare);
72b9f47f
TT
12752 if (copied_name != NULL)
12753 xfree (copied_name);
c6da4cef
DE
12754
12755 if (file_is_pst)
12756 return NULL;
12757 return include_name;
12758}
12759
c91513d8
PP
12760/* Ignore this record_line request. */
12761
12762static void
12763noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12764{
12765 return;
12766}
12767
f3f5162e
DE
12768/* Subroutine of dwarf_decode_lines to simplify it.
12769 Process the line number information in LH. */
debd256d 12770
c906108c 12771static void
f3f5162e
DE
12772dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
12773 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 12774{
a8c50c1f 12775 gdb_byte *line_ptr, *extended_end;
fe1b8b76 12776 gdb_byte *line_end;
a8c50c1f 12777 unsigned int bytes_read, extended_len;
c906108c 12778 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
12779 CORE_ADDR baseaddr;
12780 struct objfile *objfile = cu->objfile;
f3f5162e 12781 bfd *abfd = objfile->obfd;
fbf65064 12782 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 12783 const int decode_for_pst_p = (pst != NULL);
f3f5162e 12784 struct subfile *last_subfile = NULL;
c91513d8
PP
12785 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
12786 = record_line;
e142c38c
DJ
12787
12788 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 12789
debd256d
JB
12790 line_ptr = lh->statement_program_start;
12791 line_end = lh->statement_program_end;
c906108c
SS
12792
12793 /* Read the statement sequences until there's nothing left. */
12794 while (line_ptr < line_end)
12795 {
12796 /* state machine registers */
12797 CORE_ADDR address = 0;
12798 unsigned int file = 1;
12799 unsigned int line = 1;
12800 unsigned int column = 0;
debd256d 12801 int is_stmt = lh->default_is_stmt;
c906108c
SS
12802 int basic_block = 0;
12803 int end_sequence = 0;
fbf65064 12804 CORE_ADDR addr;
2dc7f7b3 12805 unsigned char op_index = 0;
c906108c 12806
aaa75496 12807 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 12808 {
aaa75496 12809 /* Start a subfile for the current file of the state machine. */
debd256d
JB
12810 /* lh->include_dirs and lh->file_names are 0-based, but the
12811 directory and file name numbers in the statement program
12812 are 1-based. */
12813 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 12814 char *dir = NULL;
a738430d 12815
debd256d
JB
12816 if (fe->dir_index)
12817 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
12818
12819 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
12820 }
12821
a738430d 12822 /* Decode the table. */
c5aa993b 12823 while (!end_sequence)
c906108c
SS
12824 {
12825 op_code = read_1_byte (abfd, line_ptr);
12826 line_ptr += 1;
59205f5a
JB
12827 if (line_ptr > line_end)
12828 {
12829 dwarf2_debug_line_missing_end_sequence_complaint ();
12830 break;
12831 }
9aa1fe7e 12832
debd256d 12833 if (op_code >= lh->opcode_base)
6e70227d 12834 {
a738430d 12835 /* Special operand. */
debd256d 12836 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
12837 address += (((op_index + (adj_opcode / lh->line_range))
12838 / lh->maximum_ops_per_instruction)
12839 * lh->minimum_instruction_length);
12840 op_index = ((op_index + (adj_opcode / lh->line_range))
12841 % lh->maximum_ops_per_instruction);
debd256d 12842 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 12843 if (lh->num_file_names < file || file == 0)
25e43795 12844 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
12845 /* For now we ignore lines not starting on an
12846 instruction boundary. */
12847 else if (op_index == 0)
25e43795
DJ
12848 {
12849 lh->file_names[file - 1].included_p = 1;
ca5f395d 12850 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
12851 {
12852 if (last_subfile != current_subfile)
12853 {
12854 addr = gdbarch_addr_bits_remove (gdbarch, address);
12855 if (last_subfile)
c91513d8 12856 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
12857 last_subfile = current_subfile;
12858 }
25e43795 12859 /* Append row to matrix using current values. */
7019d805 12860 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 12861 (*p_record_line) (current_subfile, line, addr);
366da635 12862 }
25e43795 12863 }
ca5f395d 12864 basic_block = 0;
9aa1fe7e
GK
12865 }
12866 else switch (op_code)
c906108c
SS
12867 {
12868 case DW_LNS_extended_op:
3e43a32a
MS
12869 extended_len = read_unsigned_leb128 (abfd, line_ptr,
12870 &bytes_read);
473b7be6 12871 line_ptr += bytes_read;
a8c50c1f 12872 extended_end = line_ptr + extended_len;
c906108c
SS
12873 extended_op = read_1_byte (abfd, line_ptr);
12874 line_ptr += 1;
12875 switch (extended_op)
12876 {
12877 case DW_LNE_end_sequence:
c91513d8 12878 p_record_line = record_line;
c906108c 12879 end_sequence = 1;
c906108c
SS
12880 break;
12881 case DW_LNE_set_address:
e7c27a73 12882 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
12883
12884 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
12885 {
12886 /* This line table is for a function which has been
12887 GCd by the linker. Ignore it. PR gdb/12528 */
12888
12889 long line_offset
12890 = line_ptr - dwarf2_per_objfile->line.buffer;
12891
12892 complaint (&symfile_complaints,
12893 _(".debug_line address at offset 0x%lx is 0 "
12894 "[in module %s]"),
bb5ed363 12895 line_offset, objfile->name);
c91513d8
PP
12896 p_record_line = noop_record_line;
12897 }
12898
2dc7f7b3 12899 op_index = 0;
107d2387
AC
12900 line_ptr += bytes_read;
12901 address += baseaddr;
c906108c
SS
12902 break;
12903 case DW_LNE_define_file:
debd256d
JB
12904 {
12905 char *cur_file;
12906 unsigned int dir_index, mod_time, length;
6e70227d 12907
3e43a32a
MS
12908 cur_file = read_direct_string (abfd, line_ptr,
12909 &bytes_read);
debd256d
JB
12910 line_ptr += bytes_read;
12911 dir_index =
12912 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12913 line_ptr += bytes_read;
12914 mod_time =
12915 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12916 line_ptr += bytes_read;
12917 length =
12918 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12919 line_ptr += bytes_read;
12920 add_file_name (lh, cur_file, dir_index, mod_time, length);
12921 }
c906108c 12922 break;
d0c6ba3d
CC
12923 case DW_LNE_set_discriminator:
12924 /* The discriminator is not interesting to the debugger;
12925 just ignore it. */
12926 line_ptr = extended_end;
12927 break;
c906108c 12928 default:
4d3c2250 12929 complaint (&symfile_complaints,
e2e0b3e5 12930 _("mangled .debug_line section"));
debd256d 12931 return;
c906108c 12932 }
a8c50c1f
DJ
12933 /* Make sure that we parsed the extended op correctly. If e.g.
12934 we expected a different address size than the producer used,
12935 we may have read the wrong number of bytes. */
12936 if (line_ptr != extended_end)
12937 {
12938 complaint (&symfile_complaints,
12939 _("mangled .debug_line section"));
12940 return;
12941 }
c906108c
SS
12942 break;
12943 case DW_LNS_copy:
59205f5a 12944 if (lh->num_file_names < file || file == 0)
25e43795
DJ
12945 dwarf2_debug_line_missing_file_complaint ();
12946 else
366da635 12947 {
25e43795 12948 lh->file_names[file - 1].included_p = 1;
ca5f395d 12949 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
12950 {
12951 if (last_subfile != current_subfile)
12952 {
12953 addr = gdbarch_addr_bits_remove (gdbarch, address);
12954 if (last_subfile)
c91513d8 12955 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
12956 last_subfile = current_subfile;
12957 }
7019d805 12958 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 12959 (*p_record_line) (current_subfile, line, addr);
fbf65064 12960 }
366da635 12961 }
c906108c
SS
12962 basic_block = 0;
12963 break;
12964 case DW_LNS_advance_pc:
2dc7f7b3
TT
12965 {
12966 CORE_ADDR adjust
12967 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12968
12969 address += (((op_index + adjust)
12970 / lh->maximum_ops_per_instruction)
12971 * lh->minimum_instruction_length);
12972 op_index = ((op_index + adjust)
12973 % lh->maximum_ops_per_instruction);
12974 line_ptr += bytes_read;
12975 }
c906108c
SS
12976 break;
12977 case DW_LNS_advance_line:
12978 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
12979 line_ptr += bytes_read;
12980 break;
12981 case DW_LNS_set_file:
debd256d 12982 {
a738430d
MK
12983 /* The arrays lh->include_dirs and lh->file_names are
12984 0-based, but the directory and file name numbers in
12985 the statement program are 1-based. */
debd256d 12986 struct file_entry *fe;
4f1520fb 12987 char *dir = NULL;
a738430d 12988
debd256d
JB
12989 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12990 line_ptr += bytes_read;
59205f5a 12991 if (lh->num_file_names < file || file == 0)
25e43795
DJ
12992 dwarf2_debug_line_missing_file_complaint ();
12993 else
12994 {
12995 fe = &lh->file_names[file - 1];
12996 if (fe->dir_index)
12997 dir = lh->include_dirs[fe->dir_index - 1];
12998 if (!decode_for_pst_p)
12999 {
13000 last_subfile = current_subfile;
13001 dwarf2_start_subfile (fe->name, dir, comp_dir);
13002 }
13003 }
debd256d 13004 }
c906108c
SS
13005 break;
13006 case DW_LNS_set_column:
13007 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13008 line_ptr += bytes_read;
13009 break;
13010 case DW_LNS_negate_stmt:
13011 is_stmt = (!is_stmt);
13012 break;
13013 case DW_LNS_set_basic_block:
13014 basic_block = 1;
13015 break;
c2c6d25f
JM
13016 /* Add to the address register of the state machine the
13017 address increment value corresponding to special opcode
a738430d
MK
13018 255. I.e., this value is scaled by the minimum
13019 instruction length since special opcode 255 would have
b021a221 13020 scaled the increment. */
c906108c 13021 case DW_LNS_const_add_pc:
2dc7f7b3
TT
13022 {
13023 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13024
13025 address += (((op_index + adjust)
13026 / lh->maximum_ops_per_instruction)
13027 * lh->minimum_instruction_length);
13028 op_index = ((op_index + adjust)
13029 % lh->maximum_ops_per_instruction);
13030 }
c906108c
SS
13031 break;
13032 case DW_LNS_fixed_advance_pc:
13033 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 13034 op_index = 0;
c906108c
SS
13035 line_ptr += 2;
13036 break;
9aa1fe7e 13037 default:
a738430d
MK
13038 {
13039 /* Unknown standard opcode, ignore it. */
9aa1fe7e 13040 int i;
a738430d 13041
debd256d 13042 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
13043 {
13044 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13045 line_ptr += bytes_read;
13046 }
13047 }
c906108c
SS
13048 }
13049 }
59205f5a
JB
13050 if (lh->num_file_names < file || file == 0)
13051 dwarf2_debug_line_missing_file_complaint ();
13052 else
13053 {
13054 lh->file_names[file - 1].included_p = 1;
13055 if (!decode_for_pst_p)
fbf65064
UW
13056 {
13057 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13058 (*p_record_line) (current_subfile, 0, addr);
fbf65064 13059 }
59205f5a 13060 }
c906108c 13061 }
f3f5162e
DE
13062}
13063
13064/* Decode the Line Number Program (LNP) for the given line_header
13065 structure and CU. The actual information extracted and the type
13066 of structures created from the LNP depends on the value of PST.
13067
13068 1. If PST is NULL, then this procedure uses the data from the program
13069 to create all necessary symbol tables, and their linetables.
13070
13071 2. If PST is not NULL, this procedure reads the program to determine
13072 the list of files included by the unit represented by PST, and
13073 builds all the associated partial symbol tables.
13074
13075 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13076 It is used for relative paths in the line table.
13077 NOTE: When processing partial symtabs (pst != NULL),
13078 comp_dir == pst->dirname.
13079
13080 NOTE: It is important that psymtabs have the same file name (via strcmp)
13081 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13082 symtab we don't use it in the name of the psymtabs we create.
13083 E.g. expand_line_sal requires this when finding psymtabs to expand.
13084 A good testcase for this is mb-inline.exp. */
13085
13086static void
13087dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13088 struct dwarf2_cu *cu, struct partial_symtab *pst,
13089 int want_line_info)
13090{
13091 struct objfile *objfile = cu->objfile;
13092 const int decode_for_pst_p = (pst != NULL);
13093 struct subfile *first_subfile = current_subfile;
13094
13095 if (want_line_info)
13096 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
13097
13098 if (decode_for_pst_p)
13099 {
13100 int file_index;
13101
13102 /* Now that we're done scanning the Line Header Program, we can
13103 create the psymtab of each included file. */
13104 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13105 if (lh->file_names[file_index].included_p == 1)
13106 {
c6da4cef
DE
13107 char *include_name =
13108 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13109 if (include_name != NULL)
aaa75496
JB
13110 dwarf2_create_include_psymtab (include_name, pst, objfile);
13111 }
13112 }
cb1df416
DJ
13113 else
13114 {
13115 /* Make sure a symtab is created for every file, even files
13116 which contain only variables (i.e. no code with associated
13117 line numbers). */
cb1df416 13118 int i;
cb1df416
DJ
13119
13120 for (i = 0; i < lh->num_file_names; i++)
13121 {
13122 char *dir = NULL;
f3f5162e 13123 struct file_entry *fe;
9a619af0 13124
cb1df416
DJ
13125 fe = &lh->file_names[i];
13126 if (fe->dir_index)
13127 dir = lh->include_dirs[fe->dir_index - 1];
13128 dwarf2_start_subfile (fe->name, dir, comp_dir);
13129
13130 /* Skip the main file; we don't need it, and it must be
13131 allocated last, so that it will show up before the
13132 non-primary symtabs in the objfile's symtab list. */
13133 if (current_subfile == first_subfile)
13134 continue;
13135
13136 if (current_subfile->symtab == NULL)
13137 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 13138 objfile);
cb1df416
DJ
13139 fe->symtab = current_subfile->symtab;
13140 }
13141 }
c906108c
SS
13142}
13143
13144/* Start a subfile for DWARF. FILENAME is the name of the file and
13145 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
13146 or NULL if not known. COMP_DIR is the compilation directory for the
13147 linetable's compilation unit or NULL if not known.
c906108c
SS
13148 This routine tries to keep line numbers from identical absolute and
13149 relative file names in a common subfile.
13150
13151 Using the `list' example from the GDB testsuite, which resides in
13152 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13153 of /srcdir/list0.c yields the following debugging information for list0.c:
13154
c5aa993b
JM
13155 DW_AT_name: /srcdir/list0.c
13156 DW_AT_comp_dir: /compdir
357e46e7 13157 files.files[0].name: list0.h
c5aa993b 13158 files.files[0].dir: /srcdir
357e46e7 13159 files.files[1].name: list0.c
c5aa993b 13160 files.files[1].dir: /srcdir
c906108c
SS
13161
13162 The line number information for list0.c has to end up in a single
4f1520fb
FR
13163 subfile, so that `break /srcdir/list0.c:1' works as expected.
13164 start_subfile will ensure that this happens provided that we pass the
13165 concatenation of files.files[1].dir and files.files[1].name as the
13166 subfile's name. */
c906108c
SS
13167
13168static void
3e43a32a
MS
13169dwarf2_start_subfile (char *filename, const char *dirname,
13170 const char *comp_dir)
c906108c 13171{
4f1520fb
FR
13172 char *fullname;
13173
13174 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13175 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13176 second argument to start_subfile. To be consistent, we do the
13177 same here. In order not to lose the line information directory,
13178 we concatenate it to the filename when it makes sense.
13179 Note that the Dwarf3 standard says (speaking of filenames in line
13180 information): ``The directory index is ignored for file names
13181 that represent full path names''. Thus ignoring dirname in the
13182 `else' branch below isn't an issue. */
c906108c 13183
d5166ae1 13184 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
13185 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13186 else
13187 fullname = filename;
c906108c 13188
4f1520fb
FR
13189 start_subfile (fullname, comp_dir);
13190
13191 if (fullname != filename)
13192 xfree (fullname);
c906108c
SS
13193}
13194
4c2df51b
DJ
13195static void
13196var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 13197 struct dwarf2_cu *cu)
4c2df51b 13198{
e7c27a73
DJ
13199 struct objfile *objfile = cu->objfile;
13200 struct comp_unit_head *cu_header = &cu->header;
13201
4c2df51b
DJ
13202 /* NOTE drow/2003-01-30: There used to be a comment and some special
13203 code here to turn a symbol with DW_AT_external and a
13204 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13205 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13206 with some versions of binutils) where shared libraries could have
13207 relocations against symbols in their debug information - the
13208 minimal symbol would have the right address, but the debug info
13209 would not. It's no longer necessary, because we will explicitly
13210 apply relocations when we read in the debug information now. */
13211
13212 /* A DW_AT_location attribute with no contents indicates that a
13213 variable has been optimized away. */
13214 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13215 {
13216 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13217 return;
13218 }
13219
13220 /* Handle one degenerate form of location expression specially, to
13221 preserve GDB's previous behavior when section offsets are
3019eac3
DE
13222 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13223 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
13224
13225 if (attr_form_is_block (attr)
3019eac3
DE
13226 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13227 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13228 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13229 && (DW_BLOCK (attr)->size
13230 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 13231 {
891d2f0b 13232 unsigned int dummy;
4c2df51b 13233
3019eac3
DE
13234 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13235 SYMBOL_VALUE_ADDRESS (sym) =
13236 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13237 else
13238 SYMBOL_VALUE_ADDRESS (sym) =
13239 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 13240 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
13241 fixup_symbol_section (sym, objfile);
13242 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13243 SYMBOL_SECTION (sym));
4c2df51b
DJ
13244 return;
13245 }
13246
13247 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13248 expression evaluator, and use LOC_COMPUTED only when necessary
13249 (i.e. when the value of a register or memory location is
13250 referenced, or a thread-local block, etc.). Then again, it might
13251 not be worthwhile. I'm assuming that it isn't unless performance
13252 or memory numbers show me otherwise. */
13253
e7c27a73 13254 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 13255 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
13256
13257 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13258 cu->has_loclist = 1;
4c2df51b
DJ
13259}
13260
c906108c
SS
13261/* Given a pointer to a DWARF information entry, figure out if we need
13262 to make a symbol table entry for it, and if so, create a new entry
13263 and return a pointer to it.
13264 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
13265 used the passed type.
13266 If SPACE is not NULL, use it to hold the new symbol. If it is
13267 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
13268
13269static struct symbol *
34eaf542
TT
13270new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13271 struct symbol *space)
c906108c 13272{
e7c27a73 13273 struct objfile *objfile = cu->objfile;
c906108c
SS
13274 struct symbol *sym = NULL;
13275 char *name;
13276 struct attribute *attr = NULL;
13277 struct attribute *attr2 = NULL;
e142c38c 13278 CORE_ADDR baseaddr;
e37fd15a
SW
13279 struct pending **list_to_add = NULL;
13280
edb3359d 13281 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
13282
13283 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 13284
94af9270 13285 name = dwarf2_name (die, cu);
c906108c
SS
13286 if (name)
13287 {
94af9270 13288 const char *linkagename;
34eaf542 13289 int suppress_add = 0;
94af9270 13290
34eaf542
TT
13291 if (space)
13292 sym = space;
13293 else
13294 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 13295 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
13296
13297 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 13298 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
13299 linkagename = dwarf2_physname (name, die, cu);
13300 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 13301
f55ee35c
JK
13302 /* Fortran does not have mangling standard and the mangling does differ
13303 between gfortran, iFort etc. */
13304 if (cu->language == language_fortran
b250c185 13305 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
13306 symbol_set_demangled_name (&(sym->ginfo),
13307 (char *) dwarf2_full_name (name, die, cu),
13308 NULL);
f55ee35c 13309
c906108c 13310 /* Default assumptions.
c5aa993b 13311 Use the passed type or decode it from the die. */
176620f1 13312 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 13313 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
13314 if (type != NULL)
13315 SYMBOL_TYPE (sym) = type;
13316 else
e7c27a73 13317 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
13318 attr = dwarf2_attr (die,
13319 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13320 cu);
c906108c
SS
13321 if (attr)
13322 {
13323 SYMBOL_LINE (sym) = DW_UNSND (attr);
13324 }
cb1df416 13325
edb3359d
DJ
13326 attr = dwarf2_attr (die,
13327 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13328 cu);
cb1df416
DJ
13329 if (attr)
13330 {
13331 int file_index = DW_UNSND (attr);
9a619af0 13332
cb1df416
DJ
13333 if (cu->line_header == NULL
13334 || file_index > cu->line_header->num_file_names)
13335 complaint (&symfile_complaints,
13336 _("file index out of range"));
1c3d648d 13337 else if (file_index > 0)
cb1df416
DJ
13338 {
13339 struct file_entry *fe;
9a619af0 13340
cb1df416
DJ
13341 fe = &cu->line_header->file_names[file_index - 1];
13342 SYMBOL_SYMTAB (sym) = fe->symtab;
13343 }
13344 }
13345
c906108c
SS
13346 switch (die->tag)
13347 {
13348 case DW_TAG_label:
e142c38c 13349 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
13350 if (attr)
13351 {
13352 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13353 }
0f5238ed
TT
13354 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13355 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 13356 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 13357 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
13358 break;
13359 case DW_TAG_subprogram:
13360 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13361 finish_block. */
13362 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 13363 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
13364 if ((attr2 && (DW_UNSND (attr2) != 0))
13365 || cu->language == language_ada)
c906108c 13366 {
2cfa0c8d
JB
13367 /* Subprograms marked external are stored as a global symbol.
13368 Ada subprograms, whether marked external or not, are always
13369 stored as a global symbol, because we want to be able to
13370 access them globally. For instance, we want to be able
13371 to break on a nested subprogram without having to
13372 specify the context. */
e37fd15a 13373 list_to_add = &global_symbols;
c906108c
SS
13374 }
13375 else
13376 {
e37fd15a 13377 list_to_add = cu->list_in_scope;
c906108c
SS
13378 }
13379 break;
edb3359d
DJ
13380 case DW_TAG_inlined_subroutine:
13381 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13382 finish_block. */
13383 SYMBOL_CLASS (sym) = LOC_BLOCK;
13384 SYMBOL_INLINED (sym) = 1;
481860b3 13385 list_to_add = cu->list_in_scope;
edb3359d 13386 break;
34eaf542
TT
13387 case DW_TAG_template_value_param:
13388 suppress_add = 1;
13389 /* Fall through. */
72929c62 13390 case DW_TAG_constant:
c906108c 13391 case DW_TAG_variable:
254e6b9e 13392 case DW_TAG_member:
0963b4bd
MS
13393 /* Compilation with minimal debug info may result in
13394 variables with missing type entries. Change the
13395 misleading `void' type to something sensible. */
c906108c 13396 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 13397 SYMBOL_TYPE (sym)
46bf5051 13398 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 13399
e142c38c 13400 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
13401 /* In the case of DW_TAG_member, we should only be called for
13402 static const members. */
13403 if (die->tag == DW_TAG_member)
13404 {
3863f96c
DE
13405 /* dwarf2_add_field uses die_is_declaration,
13406 so we do the same. */
254e6b9e
DE
13407 gdb_assert (die_is_declaration (die, cu));
13408 gdb_assert (attr);
13409 }
c906108c
SS
13410 if (attr)
13411 {
e7c27a73 13412 dwarf2_const_value (attr, sym, cu);
e142c38c 13413 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 13414 if (!suppress_add)
34eaf542
TT
13415 {
13416 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 13417 list_to_add = &global_symbols;
34eaf542 13418 else
e37fd15a 13419 list_to_add = cu->list_in_scope;
34eaf542 13420 }
c906108c
SS
13421 break;
13422 }
e142c38c 13423 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13424 if (attr)
13425 {
e7c27a73 13426 var_decode_location (attr, sym, cu);
e142c38c 13427 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
13428 if (SYMBOL_CLASS (sym) == LOC_STATIC
13429 && SYMBOL_VALUE_ADDRESS (sym) == 0
13430 && !dwarf2_per_objfile->has_section_at_zero)
13431 {
13432 /* When a static variable is eliminated by the linker,
13433 the corresponding debug information is not stripped
13434 out, but the variable address is set to null;
13435 do not add such variables into symbol table. */
13436 }
13437 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 13438 {
f55ee35c
JK
13439 /* Workaround gfortran PR debug/40040 - it uses
13440 DW_AT_location for variables in -fPIC libraries which may
13441 get overriden by other libraries/executable and get
13442 a different address. Resolve it by the minimal symbol
13443 which may come from inferior's executable using copy
13444 relocation. Make this workaround only for gfortran as for
13445 other compilers GDB cannot guess the minimal symbol
13446 Fortran mangling kind. */
13447 if (cu->language == language_fortran && die->parent
13448 && die->parent->tag == DW_TAG_module
13449 && cu->producer
13450 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13451 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13452
1c809c68
TT
13453 /* A variable with DW_AT_external is never static,
13454 but it may be block-scoped. */
13455 list_to_add = (cu->list_in_scope == &file_symbols
13456 ? &global_symbols : cu->list_in_scope);
1c809c68 13457 }
c906108c 13458 else
e37fd15a 13459 list_to_add = cu->list_in_scope;
c906108c
SS
13460 }
13461 else
13462 {
13463 /* We do not know the address of this symbol.
c5aa993b
JM
13464 If it is an external symbol and we have type information
13465 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13466 The address of the variable will then be determined from
13467 the minimal symbol table whenever the variable is
13468 referenced. */
e142c38c 13469 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 13470 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 13471 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 13472 {
0fe7935b
DJ
13473 /* A variable with DW_AT_external is never static, but it
13474 may be block-scoped. */
13475 list_to_add = (cu->list_in_scope == &file_symbols
13476 ? &global_symbols : cu->list_in_scope);
13477
c906108c 13478 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 13479 }
442ddf59
JK
13480 else if (!die_is_declaration (die, cu))
13481 {
13482 /* Use the default LOC_OPTIMIZED_OUT class. */
13483 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
13484 if (!suppress_add)
13485 list_to_add = cu->list_in_scope;
442ddf59 13486 }
c906108c
SS
13487 }
13488 break;
13489 case DW_TAG_formal_parameter:
edb3359d
DJ
13490 /* If we are inside a function, mark this as an argument. If
13491 not, we might be looking at an argument to an inlined function
13492 when we do not have enough information to show inlined frames;
13493 pretend it's a local variable in that case so that the user can
13494 still see it. */
13495 if (context_stack_depth > 0
13496 && context_stack[context_stack_depth - 1].name != NULL)
13497 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 13498 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13499 if (attr)
13500 {
e7c27a73 13501 var_decode_location (attr, sym, cu);
c906108c 13502 }
e142c38c 13503 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13504 if (attr)
13505 {
e7c27a73 13506 dwarf2_const_value (attr, sym, cu);
c906108c 13507 }
f346a30d 13508
e37fd15a 13509 list_to_add = cu->list_in_scope;
c906108c
SS
13510 break;
13511 case DW_TAG_unspecified_parameters:
13512 /* From varargs functions; gdb doesn't seem to have any
13513 interest in this information, so just ignore it for now.
13514 (FIXME?) */
13515 break;
34eaf542
TT
13516 case DW_TAG_template_type_param:
13517 suppress_add = 1;
13518 /* Fall through. */
c906108c 13519 case DW_TAG_class_type:
680b30c7 13520 case DW_TAG_interface_type:
c906108c
SS
13521 case DW_TAG_structure_type:
13522 case DW_TAG_union_type:
72019c9c 13523 case DW_TAG_set_type:
c906108c
SS
13524 case DW_TAG_enumeration_type:
13525 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13526 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 13527
63d06c5c 13528 {
987504bb 13529 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
13530 really ever be static objects: otherwise, if you try
13531 to, say, break of a class's method and you're in a file
13532 which doesn't mention that class, it won't work unless
13533 the check for all static symbols in lookup_symbol_aux
13534 saves you. See the OtherFileClass tests in
13535 gdb.c++/namespace.exp. */
13536
e37fd15a 13537 if (!suppress_add)
34eaf542 13538 {
34eaf542
TT
13539 list_to_add = (cu->list_in_scope == &file_symbols
13540 && (cu->language == language_cplus
13541 || cu->language == language_java)
13542 ? &global_symbols : cu->list_in_scope);
63d06c5c 13543
64382290
TT
13544 /* The semantics of C++ state that "struct foo {
13545 ... }" also defines a typedef for "foo". A Java
13546 class declaration also defines a typedef for the
13547 class. */
13548 if (cu->language == language_cplus
13549 || cu->language == language_java
13550 || cu->language == language_ada)
13551 {
13552 /* The symbol's name is already allocated along
13553 with this objfile, so we don't need to
13554 duplicate it for the type. */
13555 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13556 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13557 }
63d06c5c
DC
13558 }
13559 }
c906108c
SS
13560 break;
13561 case DW_TAG_typedef:
63d06c5c
DC
13562 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13563 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13564 list_to_add = cu->list_in_scope;
63d06c5c 13565 break;
c906108c 13566 case DW_TAG_base_type:
a02abb62 13567 case DW_TAG_subrange_type:
c906108c 13568 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13569 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13570 list_to_add = cu->list_in_scope;
c906108c
SS
13571 break;
13572 case DW_TAG_enumerator:
e142c38c 13573 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13574 if (attr)
13575 {
e7c27a73 13576 dwarf2_const_value (attr, sym, cu);
c906108c 13577 }
63d06c5c
DC
13578 {
13579 /* NOTE: carlton/2003-11-10: See comment above in the
13580 DW_TAG_class_type, etc. block. */
13581
e142c38c 13582 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
13583 && (cu->language == language_cplus
13584 || cu->language == language_java)
e142c38c 13585 ? &global_symbols : cu->list_in_scope);
63d06c5c 13586 }
c906108c 13587 break;
5c4e30ca
DC
13588 case DW_TAG_namespace:
13589 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 13590 list_to_add = &global_symbols;
5c4e30ca 13591 break;
c906108c
SS
13592 default:
13593 /* Not a tag we recognize. Hopefully we aren't processing
13594 trash data, but since we must specifically ignore things
13595 we don't recognize, there is nothing else we should do at
0963b4bd 13596 this point. */
e2e0b3e5 13597 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 13598 dwarf_tag_name (die->tag));
c906108c
SS
13599 break;
13600 }
df8a16a1 13601
e37fd15a
SW
13602 if (suppress_add)
13603 {
13604 sym->hash_next = objfile->template_symbols;
13605 objfile->template_symbols = sym;
13606 list_to_add = NULL;
13607 }
13608
13609 if (list_to_add != NULL)
13610 add_symbol_to_list (sym, list_to_add);
13611
df8a16a1
DJ
13612 /* For the benefit of old versions of GCC, check for anonymous
13613 namespaces based on the demangled name. */
13614 if (!processing_has_namespace_info
94af9270 13615 && cu->language == language_cplus)
a10964d1 13616 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
13617 }
13618 return (sym);
13619}
13620
34eaf542
TT
13621/* A wrapper for new_symbol_full that always allocates a new symbol. */
13622
13623static struct symbol *
13624new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13625{
13626 return new_symbol_full (die, type, cu, NULL);
13627}
13628
98bfdba5
PA
13629/* Given an attr with a DW_FORM_dataN value in host byte order,
13630 zero-extend it as appropriate for the symbol's type. The DWARF
13631 standard (v4) is not entirely clear about the meaning of using
13632 DW_FORM_dataN for a constant with a signed type, where the type is
13633 wider than the data. The conclusion of a discussion on the DWARF
13634 list was that this is unspecified. We choose to always zero-extend
13635 because that is the interpretation long in use by GCC. */
c906108c 13636
98bfdba5
PA
13637static gdb_byte *
13638dwarf2_const_value_data (struct attribute *attr, struct type *type,
13639 const char *name, struct obstack *obstack,
12df843f 13640 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 13641{
e7c27a73 13642 struct objfile *objfile = cu->objfile;
e17a4113
UW
13643 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13644 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
13645 LONGEST l = DW_UNSND (attr);
13646
13647 if (bits < sizeof (*value) * 8)
13648 {
13649 l &= ((LONGEST) 1 << bits) - 1;
13650 *value = l;
13651 }
13652 else if (bits == sizeof (*value) * 8)
13653 *value = l;
13654 else
13655 {
13656 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13657 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13658 return bytes;
13659 }
13660
13661 return NULL;
13662}
13663
13664/* Read a constant value from an attribute. Either set *VALUE, or if
13665 the value does not fit in *VALUE, set *BYTES - either already
13666 allocated on the objfile obstack, or newly allocated on OBSTACK,
13667 or, set *BATON, if we translated the constant to a location
13668 expression. */
13669
13670static void
13671dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13672 const char *name, struct obstack *obstack,
13673 struct dwarf2_cu *cu,
12df843f 13674 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
13675 struct dwarf2_locexpr_baton **baton)
13676{
13677 struct objfile *objfile = cu->objfile;
13678 struct comp_unit_head *cu_header = &cu->header;
c906108c 13679 struct dwarf_block *blk;
98bfdba5
PA
13680 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13681 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13682
13683 *value = 0;
13684 *bytes = NULL;
13685 *baton = NULL;
c906108c
SS
13686
13687 switch (attr->form)
13688 {
13689 case DW_FORM_addr:
3019eac3 13690 case DW_FORM_GNU_addr_index:
ac56253d 13691 {
ac56253d
TT
13692 gdb_byte *data;
13693
98bfdba5
PA
13694 if (TYPE_LENGTH (type) != cu_header->addr_size)
13695 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 13696 cu_header->addr_size,
98bfdba5 13697 TYPE_LENGTH (type));
ac56253d
TT
13698 /* Symbols of this form are reasonably rare, so we just
13699 piggyback on the existing location code rather than writing
13700 a new implementation of symbol_computed_ops. */
98bfdba5
PA
13701 *baton = obstack_alloc (&objfile->objfile_obstack,
13702 sizeof (struct dwarf2_locexpr_baton));
13703 (*baton)->per_cu = cu->per_cu;
13704 gdb_assert ((*baton)->per_cu);
ac56253d 13705
98bfdba5
PA
13706 (*baton)->size = 2 + cu_header->addr_size;
13707 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13708 (*baton)->data = data;
ac56253d
TT
13709
13710 data[0] = DW_OP_addr;
13711 store_unsigned_integer (&data[1], cu_header->addr_size,
13712 byte_order, DW_ADDR (attr));
13713 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 13714 }
c906108c 13715 break;
4ac36638 13716 case DW_FORM_string:
93b5768b 13717 case DW_FORM_strp:
3019eac3 13718 case DW_FORM_GNU_str_index:
98bfdba5
PA
13719 /* DW_STRING is already allocated on the objfile obstack, point
13720 directly to it. */
13721 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 13722 break;
c906108c
SS
13723 case DW_FORM_block1:
13724 case DW_FORM_block2:
13725 case DW_FORM_block4:
13726 case DW_FORM_block:
2dc7f7b3 13727 case DW_FORM_exprloc:
c906108c 13728 blk = DW_BLOCK (attr);
98bfdba5
PA
13729 if (TYPE_LENGTH (type) != blk->size)
13730 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13731 TYPE_LENGTH (type));
13732 *bytes = blk->data;
c906108c 13733 break;
2df3850c
JM
13734
13735 /* The DW_AT_const_value attributes are supposed to carry the
13736 symbol's value "represented as it would be on the target
13737 architecture." By the time we get here, it's already been
13738 converted to host endianness, so we just need to sign- or
13739 zero-extend it as appropriate. */
13740 case DW_FORM_data1:
3e43a32a
MS
13741 *bytes = dwarf2_const_value_data (attr, type, name,
13742 obstack, cu, value, 8);
2df3850c 13743 break;
c906108c 13744 case DW_FORM_data2:
3e43a32a
MS
13745 *bytes = dwarf2_const_value_data (attr, type, name,
13746 obstack, cu, value, 16);
2df3850c 13747 break;
c906108c 13748 case DW_FORM_data4:
3e43a32a
MS
13749 *bytes = dwarf2_const_value_data (attr, type, name,
13750 obstack, cu, value, 32);
2df3850c 13751 break;
c906108c 13752 case DW_FORM_data8:
3e43a32a
MS
13753 *bytes = dwarf2_const_value_data (attr, type, name,
13754 obstack, cu, value, 64);
2df3850c
JM
13755 break;
13756
c906108c 13757 case DW_FORM_sdata:
98bfdba5 13758 *value = DW_SND (attr);
2df3850c
JM
13759 break;
13760
c906108c 13761 case DW_FORM_udata:
98bfdba5 13762 *value = DW_UNSND (attr);
c906108c 13763 break;
2df3850c 13764
c906108c 13765 default:
4d3c2250 13766 complaint (&symfile_complaints,
e2e0b3e5 13767 _("unsupported const value attribute form: '%s'"),
4d3c2250 13768 dwarf_form_name (attr->form));
98bfdba5 13769 *value = 0;
c906108c
SS
13770 break;
13771 }
13772}
13773
2df3850c 13774
98bfdba5
PA
13775/* Copy constant value from an attribute to a symbol. */
13776
2df3850c 13777static void
98bfdba5
PA
13778dwarf2_const_value (struct attribute *attr, struct symbol *sym,
13779 struct dwarf2_cu *cu)
2df3850c 13780{
98bfdba5
PA
13781 struct objfile *objfile = cu->objfile;
13782 struct comp_unit_head *cu_header = &cu->header;
12df843f 13783 LONGEST value;
98bfdba5
PA
13784 gdb_byte *bytes;
13785 struct dwarf2_locexpr_baton *baton;
2df3850c 13786
98bfdba5
PA
13787 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
13788 SYMBOL_PRINT_NAME (sym),
13789 &objfile->objfile_obstack, cu,
13790 &value, &bytes, &baton);
2df3850c 13791
98bfdba5
PA
13792 if (baton != NULL)
13793 {
13794 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13795 SYMBOL_LOCATION_BATON (sym) = baton;
13796 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13797 }
13798 else if (bytes != NULL)
13799 {
13800 SYMBOL_VALUE_BYTES (sym) = bytes;
13801 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
13802 }
13803 else
13804 {
13805 SYMBOL_VALUE (sym) = value;
13806 SYMBOL_CLASS (sym) = LOC_CONST;
13807 }
2df3850c
JM
13808}
13809
c906108c
SS
13810/* Return the type of the die in question using its DW_AT_type attribute. */
13811
13812static struct type *
e7c27a73 13813die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13814{
c906108c 13815 struct attribute *type_attr;
c906108c 13816
e142c38c 13817 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
13818 if (!type_attr)
13819 {
13820 /* A missing DW_AT_type represents a void type. */
46bf5051 13821 return objfile_type (cu->objfile)->builtin_void;
c906108c 13822 }
348e048f 13823
673bfd45 13824 return lookup_die_type (die, type_attr, cu);
c906108c
SS
13825}
13826
b4ba55a1
JB
13827/* True iff CU's producer generates GNAT Ada auxiliary information
13828 that allows to find parallel types through that information instead
13829 of having to do expensive parallel lookups by type name. */
13830
13831static int
13832need_gnat_info (struct dwarf2_cu *cu)
13833{
13834 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
13835 of GNAT produces this auxiliary information, without any indication
13836 that it is produced. Part of enhancing the FSF version of GNAT
13837 to produce that information will be to put in place an indicator
13838 that we can use in order to determine whether the descriptive type
13839 info is available or not. One suggestion that has been made is
13840 to use a new attribute, attached to the CU die. For now, assume
13841 that the descriptive type info is not available. */
13842 return 0;
13843}
13844
b4ba55a1
JB
13845/* Return the auxiliary type of the die in question using its
13846 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
13847 attribute is not present. */
13848
13849static struct type *
13850die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
13851{
b4ba55a1 13852 struct attribute *type_attr;
b4ba55a1
JB
13853
13854 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
13855 if (!type_attr)
13856 return NULL;
13857
673bfd45 13858 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
13859}
13860
13861/* If DIE has a descriptive_type attribute, then set the TYPE's
13862 descriptive type accordingly. */
13863
13864static void
13865set_descriptive_type (struct type *type, struct die_info *die,
13866 struct dwarf2_cu *cu)
13867{
13868 struct type *descriptive_type = die_descriptive_type (die, cu);
13869
13870 if (descriptive_type)
13871 {
13872 ALLOCATE_GNAT_AUX_TYPE (type);
13873 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
13874 }
13875}
13876
c906108c
SS
13877/* Return the containing type of the die in question using its
13878 DW_AT_containing_type attribute. */
13879
13880static struct type *
e7c27a73 13881die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13882{
c906108c 13883 struct attribute *type_attr;
c906108c 13884
e142c38c 13885 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
13886 if (!type_attr)
13887 error (_("Dwarf Error: Problem turning containing type into gdb type "
13888 "[in module %s]"), cu->objfile->name);
13889
673bfd45 13890 return lookup_die_type (die, type_attr, cu);
c906108c
SS
13891}
13892
673bfd45
DE
13893/* Look up the type of DIE in CU using its type attribute ATTR.
13894 If there is no type substitute an error marker. */
13895
c906108c 13896static struct type *
673bfd45
DE
13897lookup_die_type (struct die_info *die, struct attribute *attr,
13898 struct dwarf2_cu *cu)
c906108c 13899{
bb5ed363 13900 struct objfile *objfile = cu->objfile;
f792889a
DJ
13901 struct type *this_type;
13902
673bfd45
DE
13903 /* First see if we have it cached. */
13904
13905 if (is_ref_attr (attr))
13906 {
b64f50a1 13907 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
13908
13909 this_type = get_die_type_at_offset (offset, cu->per_cu);
13910 }
55f1336d 13911 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
13912 {
13913 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
13914
13915 /* sig_type will be NULL if the signatured type is missing from
13916 the debug info. */
13917 if (sig_type == NULL)
13918 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13919 "at 0x%x [in module %s]"),
b64f50a1 13920 die->offset.sect_off, objfile->name);
673bfd45 13921
3019eac3
DE
13922 gdb_assert (sig_type->per_cu.is_debug_types);
13923 /* If we haven't filled in type_offset_in_section yet, then we
13924 haven't read the type in yet. */
13925 this_type = NULL;
13926 if (sig_type->type_offset_in_section.sect_off != 0)
13927 {
13928 this_type =
13929 get_die_type_at_offset (sig_type->type_offset_in_section,
13930 &sig_type->per_cu);
13931 }
673bfd45
DE
13932 }
13933 else
13934 {
13935 dump_die_for_error (die);
13936 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 13937 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
13938 }
13939
13940 /* If not cached we need to read it in. */
13941
13942 if (this_type == NULL)
13943 {
13944 struct die_info *type_die;
13945 struct dwarf2_cu *type_cu = cu;
13946
13947 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
13948 /* If we found the type now, it's probably because the type came
13949 from an inter-CU reference and the type's CU got expanded before
13950 ours. */
13951 this_type = get_die_type (type_die, type_cu);
13952 if (this_type == NULL)
13953 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
13954 }
13955
13956 /* If we still don't have a type use an error marker. */
13957
13958 if (this_type == NULL)
c906108c 13959 {
b00fdb78
TT
13960 char *message, *saved;
13961
13962 /* read_type_die already issued a complaint. */
13963 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 13964 objfile->name,
b64f50a1
JK
13965 cu->header.offset.sect_off,
13966 die->offset.sect_off);
bb5ed363 13967 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
13968 message, strlen (message));
13969 xfree (message);
13970
bb5ed363 13971 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 13972 }
673bfd45 13973
f792889a 13974 return this_type;
c906108c
SS
13975}
13976
673bfd45
DE
13977/* Return the type in DIE, CU.
13978 Returns NULL for invalid types.
13979
13980 This first does a lookup in the appropriate type_hash table,
13981 and only reads the die in if necessary.
13982
13983 NOTE: This can be called when reading in partial or full symbols. */
13984
f792889a 13985static struct type *
e7c27a73 13986read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13987{
f792889a
DJ
13988 struct type *this_type;
13989
13990 this_type = get_die_type (die, cu);
13991 if (this_type)
13992 return this_type;
13993
673bfd45
DE
13994 return read_type_die_1 (die, cu);
13995}
13996
13997/* Read the type in DIE, CU.
13998 Returns NULL for invalid types. */
13999
14000static struct type *
14001read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
14002{
14003 struct type *this_type = NULL;
14004
c906108c
SS
14005 switch (die->tag)
14006 {
14007 case DW_TAG_class_type:
680b30c7 14008 case DW_TAG_interface_type:
c906108c
SS
14009 case DW_TAG_structure_type:
14010 case DW_TAG_union_type:
f792889a 14011 this_type = read_structure_type (die, cu);
c906108c
SS
14012 break;
14013 case DW_TAG_enumeration_type:
f792889a 14014 this_type = read_enumeration_type (die, cu);
c906108c
SS
14015 break;
14016 case DW_TAG_subprogram:
14017 case DW_TAG_subroutine_type:
edb3359d 14018 case DW_TAG_inlined_subroutine:
f792889a 14019 this_type = read_subroutine_type (die, cu);
c906108c
SS
14020 break;
14021 case DW_TAG_array_type:
f792889a 14022 this_type = read_array_type (die, cu);
c906108c 14023 break;
72019c9c 14024 case DW_TAG_set_type:
f792889a 14025 this_type = read_set_type (die, cu);
72019c9c 14026 break;
c906108c 14027 case DW_TAG_pointer_type:
f792889a 14028 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
14029 break;
14030 case DW_TAG_ptr_to_member_type:
f792889a 14031 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
14032 break;
14033 case DW_TAG_reference_type:
f792889a 14034 this_type = read_tag_reference_type (die, cu);
c906108c
SS
14035 break;
14036 case DW_TAG_const_type:
f792889a 14037 this_type = read_tag_const_type (die, cu);
c906108c
SS
14038 break;
14039 case DW_TAG_volatile_type:
f792889a 14040 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
14041 break;
14042 case DW_TAG_string_type:
f792889a 14043 this_type = read_tag_string_type (die, cu);
c906108c
SS
14044 break;
14045 case DW_TAG_typedef:
f792889a 14046 this_type = read_typedef (die, cu);
c906108c 14047 break;
a02abb62 14048 case DW_TAG_subrange_type:
f792889a 14049 this_type = read_subrange_type (die, cu);
a02abb62 14050 break;
c906108c 14051 case DW_TAG_base_type:
f792889a 14052 this_type = read_base_type (die, cu);
c906108c 14053 break;
81a17f79 14054 case DW_TAG_unspecified_type:
f792889a 14055 this_type = read_unspecified_type (die, cu);
81a17f79 14056 break;
0114d602
DJ
14057 case DW_TAG_namespace:
14058 this_type = read_namespace_type (die, cu);
14059 break;
f55ee35c
JK
14060 case DW_TAG_module:
14061 this_type = read_module_type (die, cu);
14062 break;
c906108c 14063 default:
3e43a32a
MS
14064 complaint (&symfile_complaints,
14065 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 14066 dwarf_tag_name (die->tag));
c906108c
SS
14067 break;
14068 }
63d06c5c 14069
f792889a 14070 return this_type;
63d06c5c
DC
14071}
14072
abc72ce4
DE
14073/* See if we can figure out if the class lives in a namespace. We do
14074 this by looking for a member function; its demangled name will
14075 contain namespace info, if there is any.
14076 Return the computed name or NULL.
14077 Space for the result is allocated on the objfile's obstack.
14078 This is the full-die version of guess_partial_die_structure_name.
14079 In this case we know DIE has no useful parent. */
14080
14081static char *
14082guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14083{
14084 struct die_info *spec_die;
14085 struct dwarf2_cu *spec_cu;
14086 struct die_info *child;
14087
14088 spec_cu = cu;
14089 spec_die = die_specification (die, &spec_cu);
14090 if (spec_die != NULL)
14091 {
14092 die = spec_die;
14093 cu = spec_cu;
14094 }
14095
14096 for (child = die->child;
14097 child != NULL;
14098 child = child->sibling)
14099 {
14100 if (child->tag == DW_TAG_subprogram)
14101 {
14102 struct attribute *attr;
14103
14104 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14105 if (attr == NULL)
14106 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14107 if (attr != NULL)
14108 {
14109 char *actual_name
14110 = language_class_name_from_physname (cu->language_defn,
14111 DW_STRING (attr));
14112 char *name = NULL;
14113
14114 if (actual_name != NULL)
14115 {
14116 char *die_name = dwarf2_name (die, cu);
14117
14118 if (die_name != NULL
14119 && strcmp (die_name, actual_name) != 0)
14120 {
14121 /* Strip off the class name from the full name.
14122 We want the prefix. */
14123 int die_name_len = strlen (die_name);
14124 int actual_name_len = strlen (actual_name);
14125
14126 /* Test for '::' as a sanity check. */
14127 if (actual_name_len > die_name_len + 2
3e43a32a
MS
14128 && actual_name[actual_name_len
14129 - die_name_len - 1] == ':')
abc72ce4
DE
14130 name =
14131 obsavestring (actual_name,
14132 actual_name_len - die_name_len - 2,
14133 &cu->objfile->objfile_obstack);
14134 }
14135 }
14136 xfree (actual_name);
14137 return name;
14138 }
14139 }
14140 }
14141
14142 return NULL;
14143}
14144
96408a79
SA
14145/* GCC might emit a nameless typedef that has a linkage name. Determine the
14146 prefix part in such case. See
14147 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14148
14149static char *
14150anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14151{
14152 struct attribute *attr;
14153 char *base;
14154
14155 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14156 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14157 return NULL;
14158
14159 attr = dwarf2_attr (die, DW_AT_name, cu);
14160 if (attr != NULL && DW_STRING (attr) != NULL)
14161 return NULL;
14162
14163 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14164 if (attr == NULL)
14165 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14166 if (attr == NULL || DW_STRING (attr) == NULL)
14167 return NULL;
14168
14169 /* dwarf2_name had to be already called. */
14170 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14171
14172 /* Strip the base name, keep any leading namespaces/classes. */
14173 base = strrchr (DW_STRING (attr), ':');
14174 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14175 return "";
14176
14177 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14178 &cu->objfile->objfile_obstack);
14179}
14180
fdde2d81 14181/* Return the name of the namespace/class that DIE is defined within,
0114d602 14182 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 14183
0114d602
DJ
14184 For example, if we're within the method foo() in the following
14185 code:
14186
14187 namespace N {
14188 class C {
14189 void foo () {
14190 }
14191 };
14192 }
14193
14194 then determine_prefix on foo's die will return "N::C". */
fdde2d81 14195
0d5cff50 14196static const char *
e142c38c 14197determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 14198{
0114d602
DJ
14199 struct die_info *parent, *spec_die;
14200 struct dwarf2_cu *spec_cu;
14201 struct type *parent_type;
96408a79 14202 char *retval;
63d06c5c 14203
f55ee35c
JK
14204 if (cu->language != language_cplus && cu->language != language_java
14205 && cu->language != language_fortran)
0114d602
DJ
14206 return "";
14207
96408a79
SA
14208 retval = anonymous_struct_prefix (die, cu);
14209 if (retval)
14210 return retval;
14211
0114d602
DJ
14212 /* We have to be careful in the presence of DW_AT_specification.
14213 For example, with GCC 3.4, given the code
14214
14215 namespace N {
14216 void foo() {
14217 // Definition of N::foo.
14218 }
14219 }
14220
14221 then we'll have a tree of DIEs like this:
14222
14223 1: DW_TAG_compile_unit
14224 2: DW_TAG_namespace // N
14225 3: DW_TAG_subprogram // declaration of N::foo
14226 4: DW_TAG_subprogram // definition of N::foo
14227 DW_AT_specification // refers to die #3
14228
14229 Thus, when processing die #4, we have to pretend that we're in
14230 the context of its DW_AT_specification, namely the contex of die
14231 #3. */
14232 spec_cu = cu;
14233 spec_die = die_specification (die, &spec_cu);
14234 if (spec_die == NULL)
14235 parent = die->parent;
14236 else
63d06c5c 14237 {
0114d602
DJ
14238 parent = spec_die->parent;
14239 cu = spec_cu;
63d06c5c 14240 }
0114d602
DJ
14241
14242 if (parent == NULL)
14243 return "";
98bfdba5
PA
14244 else if (parent->building_fullname)
14245 {
14246 const char *name;
14247 const char *parent_name;
14248
14249 /* It has been seen on RealView 2.2 built binaries,
14250 DW_TAG_template_type_param types actually _defined_ as
14251 children of the parent class:
14252
14253 enum E {};
14254 template class <class Enum> Class{};
14255 Class<enum E> class_e;
14256
14257 1: DW_TAG_class_type (Class)
14258 2: DW_TAG_enumeration_type (E)
14259 3: DW_TAG_enumerator (enum1:0)
14260 3: DW_TAG_enumerator (enum2:1)
14261 ...
14262 2: DW_TAG_template_type_param
14263 DW_AT_type DW_FORM_ref_udata (E)
14264
14265 Besides being broken debug info, it can put GDB into an
14266 infinite loop. Consider:
14267
14268 When we're building the full name for Class<E>, we'll start
14269 at Class, and go look over its template type parameters,
14270 finding E. We'll then try to build the full name of E, and
14271 reach here. We're now trying to build the full name of E,
14272 and look over the parent DIE for containing scope. In the
14273 broken case, if we followed the parent DIE of E, we'd again
14274 find Class, and once again go look at its template type
14275 arguments, etc., etc. Simply don't consider such parent die
14276 as source-level parent of this die (it can't be, the language
14277 doesn't allow it), and break the loop here. */
14278 name = dwarf2_name (die, cu);
14279 parent_name = dwarf2_name (parent, cu);
14280 complaint (&symfile_complaints,
14281 _("template param type '%s' defined within parent '%s'"),
14282 name ? name : "<unknown>",
14283 parent_name ? parent_name : "<unknown>");
14284 return "";
14285 }
63d06c5c 14286 else
0114d602
DJ
14287 switch (parent->tag)
14288 {
63d06c5c 14289 case DW_TAG_namespace:
0114d602 14290 parent_type = read_type_die (parent, cu);
acebe513
UW
14291 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14292 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14293 Work around this problem here. */
14294 if (cu->language == language_cplus
14295 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14296 return "";
0114d602
DJ
14297 /* We give a name to even anonymous namespaces. */
14298 return TYPE_TAG_NAME (parent_type);
63d06c5c 14299 case DW_TAG_class_type:
680b30c7 14300 case DW_TAG_interface_type:
63d06c5c 14301 case DW_TAG_structure_type:
0114d602 14302 case DW_TAG_union_type:
f55ee35c 14303 case DW_TAG_module:
0114d602
DJ
14304 parent_type = read_type_die (parent, cu);
14305 if (TYPE_TAG_NAME (parent_type) != NULL)
14306 return TYPE_TAG_NAME (parent_type);
14307 else
14308 /* An anonymous structure is only allowed non-static data
14309 members; no typedefs, no member functions, et cetera.
14310 So it does not need a prefix. */
14311 return "";
abc72ce4 14312 case DW_TAG_compile_unit:
95554aad 14313 case DW_TAG_partial_unit:
abc72ce4
DE
14314 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14315 if (cu->language == language_cplus
8b70b953 14316 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14317 && die->child != NULL
14318 && (die->tag == DW_TAG_class_type
14319 || die->tag == DW_TAG_structure_type
14320 || die->tag == DW_TAG_union_type))
14321 {
14322 char *name = guess_full_die_structure_name (die, cu);
14323 if (name != NULL)
14324 return name;
14325 }
14326 return "";
63d06c5c 14327 default:
8176b9b8 14328 return determine_prefix (parent, cu);
63d06c5c 14329 }
63d06c5c
DC
14330}
14331
3e43a32a
MS
14332/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14333 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14334 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14335 an obconcat, otherwise allocate storage for the result. The CU argument is
14336 used to determine the language and hence, the appropriate separator. */
987504bb 14337
f55ee35c 14338#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
14339
14340static char *
f55ee35c
JK
14341typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14342 int physname, struct dwarf2_cu *cu)
63d06c5c 14343{
f55ee35c 14344 const char *lead = "";
5c315b68 14345 const char *sep;
63d06c5c 14346
3e43a32a
MS
14347 if (suffix == NULL || suffix[0] == '\0'
14348 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
14349 sep = "";
14350 else if (cu->language == language_java)
14351 sep = ".";
f55ee35c
JK
14352 else if (cu->language == language_fortran && physname)
14353 {
14354 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14355 DW_AT_MIPS_linkage_name is preferred and used instead. */
14356
14357 lead = "__";
14358 sep = "_MOD_";
14359 }
987504bb
JJ
14360 else
14361 sep = "::";
63d06c5c 14362
6dd47d34
DE
14363 if (prefix == NULL)
14364 prefix = "";
14365 if (suffix == NULL)
14366 suffix = "";
14367
987504bb
JJ
14368 if (obs == NULL)
14369 {
3e43a32a
MS
14370 char *retval
14371 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 14372
f55ee35c
JK
14373 strcpy (retval, lead);
14374 strcat (retval, prefix);
6dd47d34
DE
14375 strcat (retval, sep);
14376 strcat (retval, suffix);
63d06c5c
DC
14377 return retval;
14378 }
987504bb
JJ
14379 else
14380 {
14381 /* We have an obstack. */
f55ee35c 14382 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 14383 }
63d06c5c
DC
14384}
14385
c906108c
SS
14386/* Return sibling of die, NULL if no sibling. */
14387
f9aca02d 14388static struct die_info *
fba45db2 14389sibling_die (struct die_info *die)
c906108c 14390{
639d11d3 14391 return die->sibling;
c906108c
SS
14392}
14393
71c25dea
TT
14394/* Get name of a die, return NULL if not found. */
14395
14396static char *
14397dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14398 struct obstack *obstack)
14399{
14400 if (name && cu->language == language_cplus)
14401 {
14402 char *canon_name = cp_canonicalize_string (name);
14403
14404 if (canon_name != NULL)
14405 {
14406 if (strcmp (canon_name, name) != 0)
14407 name = obsavestring (canon_name, strlen (canon_name),
14408 obstack);
14409 xfree (canon_name);
14410 }
14411 }
14412
14413 return name;
c906108c
SS
14414}
14415
9219021c
DC
14416/* Get name of a die, return NULL if not found. */
14417
14418static char *
e142c38c 14419dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
14420{
14421 struct attribute *attr;
14422
e142c38c 14423 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
14424 if ((!attr || !DW_STRING (attr))
14425 && die->tag != DW_TAG_class_type
14426 && die->tag != DW_TAG_interface_type
14427 && die->tag != DW_TAG_structure_type
14428 && die->tag != DW_TAG_union_type)
71c25dea
TT
14429 return NULL;
14430
14431 switch (die->tag)
14432 {
14433 case DW_TAG_compile_unit:
95554aad 14434 case DW_TAG_partial_unit:
71c25dea
TT
14435 /* Compilation units have a DW_AT_name that is a filename, not
14436 a source language identifier. */
14437 case DW_TAG_enumeration_type:
14438 case DW_TAG_enumerator:
14439 /* These tags always have simple identifiers already; no need
14440 to canonicalize them. */
14441 return DW_STRING (attr);
907af001 14442
418835cc
KS
14443 case DW_TAG_subprogram:
14444 /* Java constructors will all be named "<init>", so return
14445 the class name when we see this special case. */
14446 if (cu->language == language_java
14447 && DW_STRING (attr) != NULL
14448 && strcmp (DW_STRING (attr), "<init>") == 0)
14449 {
14450 struct dwarf2_cu *spec_cu = cu;
14451 struct die_info *spec_die;
14452
14453 /* GCJ will output '<init>' for Java constructor names.
14454 For this special case, return the name of the parent class. */
14455
14456 /* GCJ may output suprogram DIEs with AT_specification set.
14457 If so, use the name of the specified DIE. */
14458 spec_die = die_specification (die, &spec_cu);
14459 if (spec_die != NULL)
14460 return dwarf2_name (spec_die, spec_cu);
14461
14462 do
14463 {
14464 die = die->parent;
14465 if (die->tag == DW_TAG_class_type)
14466 return dwarf2_name (die, cu);
14467 }
95554aad
TT
14468 while (die->tag != DW_TAG_compile_unit
14469 && die->tag != DW_TAG_partial_unit);
418835cc 14470 }
907af001
UW
14471 break;
14472
14473 case DW_TAG_class_type:
14474 case DW_TAG_interface_type:
14475 case DW_TAG_structure_type:
14476 case DW_TAG_union_type:
14477 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14478 structures or unions. These were of the form "._%d" in GCC 4.1,
14479 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14480 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
14481 if (attr && DW_STRING (attr)
14482 && (strncmp (DW_STRING (attr), "._", 2) == 0
14483 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 14484 return NULL;
53832f31
TT
14485
14486 /* GCC might emit a nameless typedef that has a linkage name. See
14487 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14488 if (!attr || DW_STRING (attr) == NULL)
14489 {
df5c6c50 14490 char *demangled = NULL;
53832f31
TT
14491
14492 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14493 if (attr == NULL)
14494 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14495
14496 if (attr == NULL || DW_STRING (attr) == NULL)
14497 return NULL;
14498
df5c6c50
JK
14499 /* Avoid demangling DW_STRING (attr) the second time on a second
14500 call for the same DIE. */
14501 if (!DW_STRING_IS_CANONICAL (attr))
14502 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
14503
14504 if (demangled)
14505 {
96408a79
SA
14506 char *base;
14507
53832f31 14508 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
14509 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14510 &cu->objfile->objfile_obstack);
53832f31
TT
14511 DW_STRING_IS_CANONICAL (attr) = 1;
14512 xfree (demangled);
96408a79
SA
14513
14514 /* Strip any leading namespaces/classes, keep only the base name.
14515 DW_AT_name for named DIEs does not contain the prefixes. */
14516 base = strrchr (DW_STRING (attr), ':');
14517 if (base && base > DW_STRING (attr) && base[-1] == ':')
14518 return &base[1];
14519 else
14520 return DW_STRING (attr);
53832f31
TT
14521 }
14522 }
907af001
UW
14523 break;
14524
71c25dea 14525 default:
907af001
UW
14526 break;
14527 }
14528
14529 if (!DW_STRING_IS_CANONICAL (attr))
14530 {
14531 DW_STRING (attr)
14532 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14533 &cu->objfile->objfile_obstack);
14534 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 14535 }
907af001 14536 return DW_STRING (attr);
9219021c
DC
14537}
14538
14539/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
14540 is none. *EXT_CU is the CU containing DIE on input, and the CU
14541 containing the return value on output. */
9219021c
DC
14542
14543static struct die_info *
f2f0e013 14544dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
14545{
14546 struct attribute *attr;
9219021c 14547
f2f0e013 14548 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
14549 if (attr == NULL)
14550 return NULL;
14551
f2f0e013 14552 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
14553}
14554
c906108c
SS
14555/* Convert a DIE tag into its string name. */
14556
f39c6ffd 14557static const char *
aa1ee363 14558dwarf_tag_name (unsigned tag)
c906108c 14559{
f39c6ffd
TT
14560 const char *name = get_DW_TAG_name (tag);
14561
14562 if (name == NULL)
14563 return "DW_TAG_<unknown>";
14564
14565 return name;
c906108c
SS
14566}
14567
14568/* Convert a DWARF attribute code into its string name. */
14569
f39c6ffd 14570static const char *
aa1ee363 14571dwarf_attr_name (unsigned attr)
c906108c 14572{
f39c6ffd
TT
14573 const char *name;
14574
c764a876 14575#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
14576 if (attr == DW_AT_MIPS_fde)
14577 return "DW_AT_MIPS_fde";
14578#else
14579 if (attr == DW_AT_HP_block_index)
14580 return "DW_AT_HP_block_index";
c764a876 14581#endif
f39c6ffd
TT
14582
14583 name = get_DW_AT_name (attr);
14584
14585 if (name == NULL)
14586 return "DW_AT_<unknown>";
14587
14588 return name;
c906108c
SS
14589}
14590
14591/* Convert a DWARF value form code into its string name. */
14592
f39c6ffd 14593static const char *
aa1ee363 14594dwarf_form_name (unsigned form)
c906108c 14595{
f39c6ffd
TT
14596 const char *name = get_DW_FORM_name (form);
14597
14598 if (name == NULL)
14599 return "DW_FORM_<unknown>";
14600
14601 return name;
c906108c
SS
14602}
14603
14604static char *
fba45db2 14605dwarf_bool_name (unsigned mybool)
c906108c
SS
14606{
14607 if (mybool)
14608 return "TRUE";
14609 else
14610 return "FALSE";
14611}
14612
14613/* Convert a DWARF type code into its string name. */
14614
f39c6ffd 14615static const char *
aa1ee363 14616dwarf_type_encoding_name (unsigned enc)
c906108c 14617{
f39c6ffd 14618 const char *name = get_DW_ATE_name (enc);
c906108c 14619
f39c6ffd
TT
14620 if (name == NULL)
14621 return "DW_ATE_<unknown>";
c906108c 14622
f39c6ffd 14623 return name;
c906108c 14624}
c906108c 14625
f9aca02d 14626static void
d97bc12b 14627dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
14628{
14629 unsigned int i;
14630
d97bc12b
DE
14631 print_spaces (indent, f);
14632 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 14633 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
14634
14635 if (die->parent != NULL)
14636 {
14637 print_spaces (indent, f);
14638 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 14639 die->parent->offset.sect_off);
d97bc12b
DE
14640 }
14641
14642 print_spaces (indent, f);
14643 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 14644 dwarf_bool_name (die->child != NULL));
c906108c 14645
d97bc12b
DE
14646 print_spaces (indent, f);
14647 fprintf_unfiltered (f, " attributes:\n");
14648
c906108c
SS
14649 for (i = 0; i < die->num_attrs; ++i)
14650 {
d97bc12b
DE
14651 print_spaces (indent, f);
14652 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
14653 dwarf_attr_name (die->attrs[i].name),
14654 dwarf_form_name (die->attrs[i].form));
d97bc12b 14655
c906108c
SS
14656 switch (die->attrs[i].form)
14657 {
c906108c 14658 case DW_FORM_addr:
3019eac3 14659 case DW_FORM_GNU_addr_index:
d97bc12b 14660 fprintf_unfiltered (f, "address: ");
5af949e3 14661 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
14662 break;
14663 case DW_FORM_block2:
14664 case DW_FORM_block4:
14665 case DW_FORM_block:
14666 case DW_FORM_block1:
3e43a32a
MS
14667 fprintf_unfiltered (f, "block: size %d",
14668 DW_BLOCK (&die->attrs[i])->size);
c906108c 14669 break;
2dc7f7b3
TT
14670 case DW_FORM_exprloc:
14671 fprintf_unfiltered (f, "expression: size %u",
14672 DW_BLOCK (&die->attrs[i])->size);
14673 break;
4568ecf9
DE
14674 case DW_FORM_ref_addr:
14675 fprintf_unfiltered (f, "ref address: ");
14676 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14677 break;
10b3939b
DJ
14678 case DW_FORM_ref1:
14679 case DW_FORM_ref2:
14680 case DW_FORM_ref4:
4568ecf9
DE
14681 case DW_FORM_ref8:
14682 case DW_FORM_ref_udata:
d97bc12b 14683 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 14684 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 14685 break;
c906108c
SS
14686 case DW_FORM_data1:
14687 case DW_FORM_data2:
14688 case DW_FORM_data4:
ce5d95e1 14689 case DW_FORM_data8:
c906108c
SS
14690 case DW_FORM_udata:
14691 case DW_FORM_sdata:
43bbcdc2
PH
14692 fprintf_unfiltered (f, "constant: %s",
14693 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 14694 break;
2dc7f7b3
TT
14695 case DW_FORM_sec_offset:
14696 fprintf_unfiltered (f, "section offset: %s",
14697 pulongest (DW_UNSND (&die->attrs[i])));
14698 break;
55f1336d 14699 case DW_FORM_ref_sig8:
348e048f
DE
14700 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14701 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 14702 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
14703 else
14704 fprintf_unfiltered (f, "signatured type, offset: unknown");
14705 break;
c906108c 14706 case DW_FORM_string:
4bdf3d34 14707 case DW_FORM_strp:
3019eac3 14708 case DW_FORM_GNU_str_index:
8285870a 14709 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 14710 DW_STRING (&die->attrs[i])
8285870a
JK
14711 ? DW_STRING (&die->attrs[i]) : "",
14712 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
14713 break;
14714 case DW_FORM_flag:
14715 if (DW_UNSND (&die->attrs[i]))
d97bc12b 14716 fprintf_unfiltered (f, "flag: TRUE");
c906108c 14717 else
d97bc12b 14718 fprintf_unfiltered (f, "flag: FALSE");
c906108c 14719 break;
2dc7f7b3
TT
14720 case DW_FORM_flag_present:
14721 fprintf_unfiltered (f, "flag: TRUE");
14722 break;
a8329558 14723 case DW_FORM_indirect:
0963b4bd
MS
14724 /* The reader will have reduced the indirect form to
14725 the "base form" so this form should not occur. */
3e43a32a
MS
14726 fprintf_unfiltered (f,
14727 "unexpected attribute form: DW_FORM_indirect");
a8329558 14728 break;
c906108c 14729 default:
d97bc12b 14730 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 14731 die->attrs[i].form);
d97bc12b 14732 break;
c906108c 14733 }
d97bc12b 14734 fprintf_unfiltered (f, "\n");
c906108c
SS
14735 }
14736}
14737
f9aca02d 14738static void
d97bc12b 14739dump_die_for_error (struct die_info *die)
c906108c 14740{
d97bc12b
DE
14741 dump_die_shallow (gdb_stderr, 0, die);
14742}
14743
14744static void
14745dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14746{
14747 int indent = level * 4;
14748
14749 gdb_assert (die != NULL);
14750
14751 if (level >= max_level)
14752 return;
14753
14754 dump_die_shallow (f, indent, die);
14755
14756 if (die->child != NULL)
c906108c 14757 {
d97bc12b
DE
14758 print_spaces (indent, f);
14759 fprintf_unfiltered (f, " Children:");
14760 if (level + 1 < max_level)
14761 {
14762 fprintf_unfiltered (f, "\n");
14763 dump_die_1 (f, level + 1, max_level, die->child);
14764 }
14765 else
14766 {
3e43a32a
MS
14767 fprintf_unfiltered (f,
14768 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14769 }
14770 }
14771
14772 if (die->sibling != NULL && level > 0)
14773 {
14774 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14775 }
14776}
14777
d97bc12b
DE
14778/* This is called from the pdie macro in gdbinit.in.
14779 It's not static so gcc will keep a copy callable from gdb. */
14780
14781void
14782dump_die (struct die_info *die, int max_level)
14783{
14784 dump_die_1 (gdb_stdlog, 0, max_level, die);
14785}
14786
f9aca02d 14787static void
51545339 14788store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14789{
51545339 14790 void **slot;
c906108c 14791
b64f50a1
JK
14792 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14793 INSERT);
51545339
DJ
14794
14795 *slot = die;
c906108c
SS
14796}
14797
b64f50a1
JK
14798/* DW_ADDR is always stored already as sect_offset; despite for the forms
14799 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14800
93311388
DE
14801static int
14802is_ref_attr (struct attribute *attr)
c906108c 14803{
c906108c
SS
14804 switch (attr->form)
14805 {
14806 case DW_FORM_ref_addr:
c906108c
SS
14807 case DW_FORM_ref1:
14808 case DW_FORM_ref2:
14809 case DW_FORM_ref4:
613e1657 14810 case DW_FORM_ref8:
c906108c 14811 case DW_FORM_ref_udata:
93311388 14812 return 1;
c906108c 14813 default:
93311388 14814 return 0;
c906108c 14815 }
93311388
DE
14816}
14817
b64f50a1
JK
14818/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14819 required kind. */
14820
14821static sect_offset
93311388
DE
14822dwarf2_get_ref_die_offset (struct attribute *attr)
14823{
4568ecf9 14824 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 14825
93311388 14826 if (is_ref_attr (attr))
b64f50a1 14827 return retval;
93311388 14828
b64f50a1 14829 retval.sect_off = 0;
93311388
DE
14830 complaint (&symfile_complaints,
14831 _("unsupported die ref attribute form: '%s'"),
14832 dwarf_form_name (attr->form));
b64f50a1 14833 return retval;
c906108c
SS
14834}
14835
43bbcdc2
PH
14836/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14837 * the value held by the attribute is not constant. */
a02abb62 14838
43bbcdc2 14839static LONGEST
a02abb62
JB
14840dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14841{
14842 if (attr->form == DW_FORM_sdata)
14843 return DW_SND (attr);
14844 else if (attr->form == DW_FORM_udata
14845 || attr->form == DW_FORM_data1
14846 || attr->form == DW_FORM_data2
14847 || attr->form == DW_FORM_data4
14848 || attr->form == DW_FORM_data8)
14849 return DW_UNSND (attr);
14850 else
14851 {
3e43a32a
MS
14852 complaint (&symfile_complaints,
14853 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14854 dwarf_form_name (attr->form));
14855 return default_value;
14856 }
14857}
14858
03dd20cc 14859/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14860 unit and add it to our queue.
14861 The result is non-zero if PER_CU was queued, otherwise the result is zero
14862 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14863
348e048f 14864static int
03dd20cc 14865maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
95554aad
TT
14866 struct dwarf2_per_cu_data *per_cu,
14867 enum language pretend_language)
03dd20cc 14868{
98bfdba5
PA
14869 /* We may arrive here during partial symbol reading, if we need full
14870 DIEs to process an unusual case (e.g. template arguments). Do
14871 not queue PER_CU, just tell our caller to load its DIEs. */
14872 if (dwarf2_per_objfile->reading_partial_symbols)
14873 {
14874 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14875 return 1;
14876 return 0;
14877 }
14878
03dd20cc
DJ
14879 /* Mark the dependence relation so that we don't flush PER_CU
14880 too early. */
14881 dwarf2_add_dependence (this_cu, per_cu);
14882
14883 /* If it's already on the queue, we have nothing to do. */
14884 if (per_cu->queued)
348e048f 14885 return 0;
03dd20cc
DJ
14886
14887 /* If the compilation unit is already loaded, just mark it as
14888 used. */
14889 if (per_cu->cu != NULL)
14890 {
14891 per_cu->cu->last_used = 0;
348e048f 14892 return 0;
03dd20cc
DJ
14893 }
14894
14895 /* Add it to the queue. */
95554aad 14896 queue_comp_unit (per_cu, pretend_language);
348e048f
DE
14897
14898 return 1;
14899}
14900
14901/* Follow reference or signature attribute ATTR of SRC_DIE.
14902 On entry *REF_CU is the CU of SRC_DIE.
14903 On exit *REF_CU is the CU of the result. */
14904
14905static struct die_info *
14906follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14907 struct dwarf2_cu **ref_cu)
14908{
14909 struct die_info *die;
14910
14911 if (is_ref_attr (attr))
14912 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14913 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14914 die = follow_die_sig (src_die, attr, ref_cu);
14915 else
14916 {
14917 dump_die_for_error (src_die);
14918 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14919 (*ref_cu)->objfile->name);
14920 }
14921
14922 return die;
03dd20cc
DJ
14923}
14924
5c631832 14925/* Follow reference OFFSET.
673bfd45
DE
14926 On entry *REF_CU is the CU of the source die referencing OFFSET.
14927 On exit *REF_CU is the CU of the result.
14928 Returns NULL if OFFSET is invalid. */
f504f079 14929
f9aca02d 14930static struct die_info *
b64f50a1 14931follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
c906108c 14932{
10b3939b 14933 struct die_info temp_die;
f2f0e013 14934 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14935
348e048f
DE
14936 gdb_assert (cu->per_cu != NULL);
14937
98bfdba5
PA
14938 target_cu = cu;
14939
3019eac3 14940 if (cu->per_cu->is_debug_types)
348e048f
DE
14941 {
14942 /* .debug_types CUs cannot reference anything outside their CU.
14943 If they need to, they have to reference a signatured type via
55f1336d 14944 DW_FORM_ref_sig8. */
348e048f 14945 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14946 return NULL;
348e048f
DE
14947 }
14948 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14949 {
14950 struct dwarf2_per_cu_data *per_cu;
9a619af0 14951
45452591 14952 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14953
14954 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
14955 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
14956 load_full_comp_unit (per_cu, cu->language);
03dd20cc 14957
10b3939b
DJ
14958 target_cu = per_cu->cu;
14959 }
98bfdba5
PA
14960 else if (cu->dies == NULL)
14961 {
14962 /* We're loading full DIEs during partial symbol reading. */
14963 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 14964 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 14965 }
c906108c 14966
f2f0e013 14967 *ref_cu = target_cu;
51545339 14968 temp_die.offset = offset;
b64f50a1 14969 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 14970}
10b3939b 14971
5c631832
JK
14972/* Follow reference attribute ATTR of SRC_DIE.
14973 On entry *REF_CU is the CU of SRC_DIE.
14974 On exit *REF_CU is the CU of the result. */
14975
14976static struct die_info *
14977follow_die_ref (struct die_info *src_die, struct attribute *attr,
14978 struct dwarf2_cu **ref_cu)
14979{
b64f50a1 14980 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
14981 struct dwarf2_cu *cu = *ref_cu;
14982 struct die_info *die;
14983
14984 die = follow_die_offset (offset, ref_cu);
14985 if (!die)
14986 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14987 "at 0x%x [in module %s]"),
b64f50a1 14988 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 14989
5c631832
JK
14990 return die;
14991}
14992
d83e736b
JK
14993/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14994 Returned value is intended for DW_OP_call*. Returned
14995 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14996
14997struct dwarf2_locexpr_baton
b64f50a1 14998dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
14999 struct dwarf2_per_cu_data *per_cu,
15000 CORE_ADDR (*get_frame_pc) (void *baton),
15001 void *baton)
5c631832 15002{
b64f50a1 15003 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 15004 struct dwarf2_cu *cu;
5c631832
JK
15005 struct die_info *die;
15006 struct attribute *attr;
15007 struct dwarf2_locexpr_baton retval;
15008
8cf6f0b1
TT
15009 dw2_setup (per_cu->objfile);
15010
918dd910
JK
15011 if (per_cu->cu == NULL)
15012 load_cu (per_cu);
15013 cu = per_cu->cu;
15014
5c631832
JK
15015 die = follow_die_offset (offset, &cu);
15016 if (!die)
15017 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 15018 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15019
15020 attr = dwarf2_attr (die, DW_AT_location, cu);
15021 if (!attr)
15022 {
e103e986
JK
15023 /* DWARF: "If there is no such attribute, then there is no effect.".
15024 DATA is ignored if SIZE is 0. */
5c631832 15025
e103e986 15026 retval.data = NULL;
5c631832
JK
15027 retval.size = 0;
15028 }
8cf6f0b1
TT
15029 else if (attr_form_is_section_offset (attr))
15030 {
15031 struct dwarf2_loclist_baton loclist_baton;
15032 CORE_ADDR pc = (*get_frame_pc) (baton);
15033 size_t size;
15034
15035 fill_in_loclist_baton (cu, &loclist_baton, attr);
15036
15037 retval.data = dwarf2_find_location_expression (&loclist_baton,
15038 &size, pc);
15039 retval.size = size;
15040 }
5c631832
JK
15041 else
15042 {
15043 if (!attr_form_is_block (attr))
15044 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15045 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 15046 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15047
15048 retval.data = DW_BLOCK (attr)->data;
15049 retval.size = DW_BLOCK (attr)->size;
15050 }
15051 retval.per_cu = cu->per_cu;
918dd910 15052
918dd910
JK
15053 age_cached_comp_units ();
15054
5c631832 15055 return retval;
348e048f
DE
15056}
15057
8a9b8146
TT
15058/* Return the type of the DIE at DIE_OFFSET in the CU named by
15059 PER_CU. */
15060
15061struct type *
b64f50a1 15062dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
15063 struct dwarf2_per_cu_data *per_cu)
15064{
b64f50a1
JK
15065 sect_offset die_offset_sect;
15066
8a9b8146 15067 dw2_setup (per_cu->objfile);
b64f50a1
JK
15068
15069 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15070 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
15071}
15072
348e048f
DE
15073/* Follow the signature attribute ATTR in SRC_DIE.
15074 On entry *REF_CU is the CU of SRC_DIE.
15075 On exit *REF_CU is the CU of the result. */
15076
15077static struct die_info *
15078follow_die_sig (struct die_info *src_die, struct attribute *attr,
15079 struct dwarf2_cu **ref_cu)
15080{
15081 struct objfile *objfile = (*ref_cu)->objfile;
15082 struct die_info temp_die;
15083 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15084 struct dwarf2_cu *sig_cu;
15085 struct die_info *die;
15086
15087 /* sig_type will be NULL if the signatured type is missing from
15088 the debug info. */
15089 if (sig_type == NULL)
15090 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15091 "at 0x%x [in module %s]"),
b64f50a1 15092 src_die->offset.sect_off, objfile->name);
348e048f
DE
15093
15094 /* If necessary, add it to the queue and load its DIEs. */
15095
95554aad 15096 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 15097 read_signatured_type (sig_type);
348e048f
DE
15098
15099 gdb_assert (sig_type->per_cu.cu != NULL);
15100
15101 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
15102 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15103 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
15104 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15105 temp_die.offset.sect_off);
348e048f
DE
15106 if (die)
15107 {
15108 *ref_cu = sig_cu;
15109 return die;
15110 }
15111
3e43a32a
MS
15112 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15113 "from DIE at 0x%x [in module %s]"),
b64f50a1 15114 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
15115}
15116
15117/* Given an offset of a signatured type, return its signatured_type. */
15118
15119static struct signatured_type *
8b70b953
TT
15120lookup_signatured_type_at_offset (struct objfile *objfile,
15121 struct dwarf2_section_info *section,
b64f50a1 15122 sect_offset offset)
348e048f 15123{
b64f50a1 15124 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
15125 unsigned int length, initial_length_size;
15126 unsigned int sig_offset;
52dc124a 15127 struct signatured_type find_entry, *sig_type;
348e048f
DE
15128
15129 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15130 sig_offset = (initial_length_size
15131 + 2 /*version*/
15132 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15133 + 1 /*address_size*/);
15134 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 15135 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
15136
15137 /* This is only used to lookup previously recorded types.
15138 If we didn't find it, it's our bug. */
52dc124a
DE
15139 gdb_assert (sig_type != NULL);
15140 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 15141
52dc124a 15142 return sig_type;
348e048f
DE
15143}
15144
e5fe5e75 15145/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
15146
15147static void
e5fe5e75 15148load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 15149{
e5fe5e75 15150 struct objfile *objfile = per_cu->objfile;
3019eac3 15151 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
b64f50a1 15152 sect_offset offset = per_cu->offset;
52dc124a 15153 struct signatured_type *sig_type;
348e048f 15154
8b70b953 15155 dwarf2_read_section (objfile, sect);
be391dca 15156
348e048f 15157 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
15158 hash table lookup. */
15159 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15160 the signature to assert we found the right one.
15161 Ok, but it's a lot of work. We should simplify things so any needed
15162 assert doesn't require all this clumsiness. */
52dc124a 15163 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f 15164
dee91e82 15165 gdb_assert (&sig_type->per_cu == per_cu);
52dc124a 15166 gdb_assert (sig_type->per_cu.cu == NULL);
348e048f 15167
52dc124a 15168 read_signatured_type (sig_type);
348e048f 15169
52dc124a 15170 gdb_assert (sig_type->per_cu.cu != NULL);
348e048f
DE
15171}
15172
dee91e82
DE
15173/* die_reader_func for read_signatured_type.
15174 This is identical to load_full_comp_unit_reader,
15175 but is kept separate for now. */
348e048f
DE
15176
15177static void
dee91e82
DE
15178read_signatured_type_reader (const struct die_reader_specs *reader,
15179 gdb_byte *info_ptr,
15180 struct die_info *comp_unit_die,
15181 int has_children,
15182 void *data)
348e048f 15183{
dee91e82
DE
15184 struct dwarf2_cu *cu = reader->cu;
15185 struct attribute *attr;
348e048f 15186
dee91e82
DE
15187 gdb_assert (cu->die_hash == NULL);
15188 cu->die_hash =
15189 htab_create_alloc_ex (cu->header.length / 12,
15190 die_hash,
15191 die_eq,
15192 NULL,
15193 &cu->comp_unit_obstack,
15194 hashtab_obstack_allocate,
15195 dummy_obstack_deallocate);
348e048f 15196
dee91e82
DE
15197 if (has_children)
15198 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15199 &info_ptr, comp_unit_die);
15200 cu->dies = comp_unit_die;
15201 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
15202
15203 /* We try not to read any attributes in this function, because not
9cdd5dbd 15204 all CUs needed for references have been loaded yet, and symbol
348e048f 15205 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
15206 or we won't be able to build types correctly.
15207 Similarly, if we do not read the producer, we can not apply
15208 producer-specific interpretation. */
95554aad 15209 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 15210}
348e048f 15211
3019eac3
DE
15212/* Read in a signatured type and build its CU and DIEs.
15213 If the type is a stub for the real type in a DWO file,
15214 read in the real type from the DWO file as well. */
dee91e82
DE
15215
15216static void
15217read_signatured_type (struct signatured_type *sig_type)
15218{
15219 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 15220
3019eac3 15221 gdb_assert (per_cu->is_debug_types);
dee91e82 15222 gdb_assert (per_cu->cu == NULL);
348e048f 15223
fd820528 15224 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
c906108c
SS
15225}
15226
c906108c
SS
15227/* Decode simple location descriptions.
15228 Given a pointer to a dwarf block that defines a location, compute
15229 the location and return the value.
15230
4cecd739
DJ
15231 NOTE drow/2003-11-18: This function is called in two situations
15232 now: for the address of static or global variables (partial symbols
15233 only) and for offsets into structures which are expected to be
15234 (more or less) constant. The partial symbol case should go away,
15235 and only the constant case should remain. That will let this
15236 function complain more accurately. A few special modes are allowed
15237 without complaint for global variables (for instance, global
15238 register values and thread-local values).
c906108c
SS
15239
15240 A location description containing no operations indicates that the
4cecd739 15241 object is optimized out. The return value is 0 for that case.
6b992462
DJ
15242 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15243 callers will only want a very basic result and this can become a
21ae7a4d
JK
15244 complaint.
15245
15246 Note that stack[0] is unused except as a default error return. */
c906108c
SS
15247
15248static CORE_ADDR
e7c27a73 15249decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 15250{
e7c27a73 15251 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
15252 int i;
15253 int size = blk->size;
15254 gdb_byte *data = blk->data;
15255 CORE_ADDR stack[64];
15256 int stacki;
15257 unsigned int bytes_read, unsnd;
15258 gdb_byte op;
c906108c 15259
21ae7a4d
JK
15260 i = 0;
15261 stacki = 0;
15262 stack[stacki] = 0;
15263 stack[++stacki] = 0;
15264
15265 while (i < size)
15266 {
15267 op = data[i++];
15268 switch (op)
15269 {
15270 case DW_OP_lit0:
15271 case DW_OP_lit1:
15272 case DW_OP_lit2:
15273 case DW_OP_lit3:
15274 case DW_OP_lit4:
15275 case DW_OP_lit5:
15276 case DW_OP_lit6:
15277 case DW_OP_lit7:
15278 case DW_OP_lit8:
15279 case DW_OP_lit9:
15280 case DW_OP_lit10:
15281 case DW_OP_lit11:
15282 case DW_OP_lit12:
15283 case DW_OP_lit13:
15284 case DW_OP_lit14:
15285 case DW_OP_lit15:
15286 case DW_OP_lit16:
15287 case DW_OP_lit17:
15288 case DW_OP_lit18:
15289 case DW_OP_lit19:
15290 case DW_OP_lit20:
15291 case DW_OP_lit21:
15292 case DW_OP_lit22:
15293 case DW_OP_lit23:
15294 case DW_OP_lit24:
15295 case DW_OP_lit25:
15296 case DW_OP_lit26:
15297 case DW_OP_lit27:
15298 case DW_OP_lit28:
15299 case DW_OP_lit29:
15300 case DW_OP_lit30:
15301 case DW_OP_lit31:
15302 stack[++stacki] = op - DW_OP_lit0;
15303 break;
f1bea926 15304
21ae7a4d
JK
15305 case DW_OP_reg0:
15306 case DW_OP_reg1:
15307 case DW_OP_reg2:
15308 case DW_OP_reg3:
15309 case DW_OP_reg4:
15310 case DW_OP_reg5:
15311 case DW_OP_reg6:
15312 case DW_OP_reg7:
15313 case DW_OP_reg8:
15314 case DW_OP_reg9:
15315 case DW_OP_reg10:
15316 case DW_OP_reg11:
15317 case DW_OP_reg12:
15318 case DW_OP_reg13:
15319 case DW_OP_reg14:
15320 case DW_OP_reg15:
15321 case DW_OP_reg16:
15322 case DW_OP_reg17:
15323 case DW_OP_reg18:
15324 case DW_OP_reg19:
15325 case DW_OP_reg20:
15326 case DW_OP_reg21:
15327 case DW_OP_reg22:
15328 case DW_OP_reg23:
15329 case DW_OP_reg24:
15330 case DW_OP_reg25:
15331 case DW_OP_reg26:
15332 case DW_OP_reg27:
15333 case DW_OP_reg28:
15334 case DW_OP_reg29:
15335 case DW_OP_reg30:
15336 case DW_OP_reg31:
15337 stack[++stacki] = op - DW_OP_reg0;
15338 if (i < size)
15339 dwarf2_complex_location_expr_complaint ();
15340 break;
c906108c 15341
21ae7a4d
JK
15342 case DW_OP_regx:
15343 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15344 i += bytes_read;
15345 stack[++stacki] = unsnd;
15346 if (i < size)
15347 dwarf2_complex_location_expr_complaint ();
15348 break;
c906108c 15349
21ae7a4d
JK
15350 case DW_OP_addr:
15351 stack[++stacki] = read_address (objfile->obfd, &data[i],
15352 cu, &bytes_read);
15353 i += bytes_read;
15354 break;
d53d4ac5 15355
21ae7a4d
JK
15356 case DW_OP_const1u:
15357 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15358 i += 1;
15359 break;
15360
15361 case DW_OP_const1s:
15362 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15363 i += 1;
15364 break;
15365
15366 case DW_OP_const2u:
15367 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15368 i += 2;
15369 break;
15370
15371 case DW_OP_const2s:
15372 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15373 i += 2;
15374 break;
d53d4ac5 15375
21ae7a4d
JK
15376 case DW_OP_const4u:
15377 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15378 i += 4;
15379 break;
15380
15381 case DW_OP_const4s:
15382 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15383 i += 4;
15384 break;
15385
585861ea
JK
15386 case DW_OP_const8u:
15387 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15388 i += 8;
15389 break;
15390
21ae7a4d
JK
15391 case DW_OP_constu:
15392 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15393 &bytes_read);
15394 i += bytes_read;
15395 break;
15396
15397 case DW_OP_consts:
15398 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15399 i += bytes_read;
15400 break;
15401
15402 case DW_OP_dup:
15403 stack[stacki + 1] = stack[stacki];
15404 stacki++;
15405 break;
15406
15407 case DW_OP_plus:
15408 stack[stacki - 1] += stack[stacki];
15409 stacki--;
15410 break;
15411
15412 case DW_OP_plus_uconst:
15413 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15414 &bytes_read);
15415 i += bytes_read;
15416 break;
15417
15418 case DW_OP_minus:
15419 stack[stacki - 1] -= stack[stacki];
15420 stacki--;
15421 break;
15422
15423 case DW_OP_deref:
15424 /* If we're not the last op, then we definitely can't encode
15425 this using GDB's address_class enum. This is valid for partial
15426 global symbols, although the variable's address will be bogus
15427 in the psymtab. */
15428 if (i < size)
15429 dwarf2_complex_location_expr_complaint ();
15430 break;
15431
15432 case DW_OP_GNU_push_tls_address:
15433 /* The top of the stack has the offset from the beginning
15434 of the thread control block at which the variable is located. */
15435 /* Nothing should follow this operator, so the top of stack would
15436 be returned. */
15437 /* This is valid for partial global symbols, but the variable's
585861ea
JK
15438 address will be bogus in the psymtab. Make it always at least
15439 non-zero to not look as a variable garbage collected by linker
15440 which have DW_OP_addr 0. */
21ae7a4d
JK
15441 if (i < size)
15442 dwarf2_complex_location_expr_complaint ();
585861ea 15443 stack[stacki]++;
21ae7a4d
JK
15444 break;
15445
15446 case DW_OP_GNU_uninit:
15447 break;
15448
3019eac3
DE
15449 case DW_OP_GNU_addr_index:
15450 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15451 &bytes_read);
15452 i += bytes_read;
15453 break;
15454
21ae7a4d
JK
15455 default:
15456 {
f39c6ffd 15457 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
15458
15459 if (name)
15460 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15461 name);
15462 else
15463 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15464 op);
15465 }
15466
15467 return (stack[stacki]);
d53d4ac5 15468 }
3c6e0cb3 15469
21ae7a4d
JK
15470 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15471 outside of the allocated space. Also enforce minimum>0. */
15472 if (stacki >= ARRAY_SIZE (stack) - 1)
15473 {
15474 complaint (&symfile_complaints,
15475 _("location description stack overflow"));
15476 return 0;
15477 }
15478
15479 if (stacki <= 0)
15480 {
15481 complaint (&symfile_complaints,
15482 _("location description stack underflow"));
15483 return 0;
15484 }
15485 }
15486 return (stack[stacki]);
c906108c
SS
15487}
15488
15489/* memory allocation interface */
15490
c906108c 15491static struct dwarf_block *
7b5a2f43 15492dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
15493{
15494 struct dwarf_block *blk;
15495
15496 blk = (struct dwarf_block *)
7b5a2f43 15497 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
15498 return (blk);
15499}
15500
15501static struct abbrev_info *
f3dd6933 15502dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
15503{
15504 struct abbrev_info *abbrev;
15505
f3dd6933
DJ
15506 abbrev = (struct abbrev_info *)
15507 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
15508 memset (abbrev, 0, sizeof (struct abbrev_info));
15509 return (abbrev);
15510}
15511
15512static struct die_info *
b60c80d6 15513dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
15514{
15515 struct die_info *die;
b60c80d6
DJ
15516 size_t size = sizeof (struct die_info);
15517
15518 if (num_attrs > 1)
15519 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 15520
b60c80d6 15521 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
15522 memset (die, 0, sizeof (struct die_info));
15523 return (die);
15524}
2e276125
JB
15525
15526\f
15527/* Macro support. */
15528
2e276125
JB
15529/* Return the full name of file number I in *LH's file name table.
15530 Use COMP_DIR as the name of the current directory of the
15531 compilation. The result is allocated using xmalloc; the caller is
15532 responsible for freeing it. */
15533static char *
15534file_full_name (int file, struct line_header *lh, const char *comp_dir)
15535{
6a83a1e6
EZ
15536 /* Is the file number a valid index into the line header's file name
15537 table? Remember that file numbers start with one, not zero. */
15538 if (1 <= file && file <= lh->num_file_names)
15539 {
15540 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 15541
6a83a1e6
EZ
15542 if (IS_ABSOLUTE_PATH (fe->name))
15543 return xstrdup (fe->name);
15544 else
15545 {
15546 const char *dir;
15547 int dir_len;
15548 char *full_name;
15549
15550 if (fe->dir_index)
15551 dir = lh->include_dirs[fe->dir_index - 1];
15552 else
15553 dir = comp_dir;
15554
15555 if (dir)
15556 {
15557 dir_len = strlen (dir);
15558 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15559 strcpy (full_name, dir);
15560 full_name[dir_len] = '/';
15561 strcpy (full_name + dir_len + 1, fe->name);
15562 return full_name;
15563 }
15564 else
15565 return xstrdup (fe->name);
15566 }
15567 }
2e276125
JB
15568 else
15569 {
6a83a1e6
EZ
15570 /* The compiler produced a bogus file number. We can at least
15571 record the macro definitions made in the file, even if we
15572 won't be able to find the file by name. */
15573 char fake_name[80];
9a619af0 15574
6a83a1e6 15575 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 15576
6e70227d 15577 complaint (&symfile_complaints,
6a83a1e6
EZ
15578 _("bad file number in macro information (%d)"),
15579 file);
2e276125 15580
6a83a1e6 15581 return xstrdup (fake_name);
2e276125
JB
15582 }
15583}
15584
15585
15586static struct macro_source_file *
15587macro_start_file (int file, int line,
15588 struct macro_source_file *current_file,
15589 const char *comp_dir,
15590 struct line_header *lh, struct objfile *objfile)
15591{
15592 /* The full name of this source file. */
15593 char *full_name = file_full_name (file, lh, comp_dir);
15594
15595 /* We don't create a macro table for this compilation unit
15596 at all until we actually get a filename. */
15597 if (! pending_macros)
4a146b47 15598 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 15599 objfile->macro_cache);
2e276125
JB
15600
15601 if (! current_file)
abc9d0dc
TT
15602 {
15603 /* If we have no current file, then this must be the start_file
15604 directive for the compilation unit's main source file. */
15605 current_file = macro_set_main (pending_macros, full_name);
15606 macro_define_special (pending_macros);
15607 }
2e276125
JB
15608 else
15609 current_file = macro_include (current_file, line, full_name);
15610
15611 xfree (full_name);
6e70227d 15612
2e276125
JB
15613 return current_file;
15614}
15615
15616
15617/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15618 followed by a null byte. */
15619static char *
15620copy_string (const char *buf, int len)
15621{
15622 char *s = xmalloc (len + 1);
9a619af0 15623
2e276125
JB
15624 memcpy (s, buf, len);
15625 s[len] = '\0';
2e276125
JB
15626 return s;
15627}
15628
15629
15630static const char *
15631consume_improper_spaces (const char *p, const char *body)
15632{
15633 if (*p == ' ')
15634 {
4d3c2250 15635 complaint (&symfile_complaints,
3e43a32a
MS
15636 _("macro definition contains spaces "
15637 "in formal argument list:\n`%s'"),
4d3c2250 15638 body);
2e276125
JB
15639
15640 while (*p == ' ')
15641 p++;
15642 }
15643
15644 return p;
15645}
15646
15647
15648static void
15649parse_macro_definition (struct macro_source_file *file, int line,
15650 const char *body)
15651{
15652 const char *p;
15653
15654 /* The body string takes one of two forms. For object-like macro
15655 definitions, it should be:
15656
15657 <macro name> " " <definition>
15658
15659 For function-like macro definitions, it should be:
15660
15661 <macro name> "() " <definition>
15662 or
15663 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15664
15665 Spaces may appear only where explicitly indicated, and in the
15666 <definition>.
15667
15668 The Dwarf 2 spec says that an object-like macro's name is always
15669 followed by a space, but versions of GCC around March 2002 omit
6e70227d 15670 the space when the macro's definition is the empty string.
2e276125
JB
15671
15672 The Dwarf 2 spec says that there should be no spaces between the
15673 formal arguments in a function-like macro's formal argument list,
15674 but versions of GCC around March 2002 include spaces after the
15675 commas. */
15676
15677
15678 /* Find the extent of the macro name. The macro name is terminated
15679 by either a space or null character (for an object-like macro) or
15680 an opening paren (for a function-like macro). */
15681 for (p = body; *p; p++)
15682 if (*p == ' ' || *p == '(')
15683 break;
15684
15685 if (*p == ' ' || *p == '\0')
15686 {
15687 /* It's an object-like macro. */
15688 int name_len = p - body;
15689 char *name = copy_string (body, name_len);
15690 const char *replacement;
15691
15692 if (*p == ' ')
15693 replacement = body + name_len + 1;
15694 else
15695 {
4d3c2250 15696 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15697 replacement = body + name_len;
15698 }
6e70227d 15699
2e276125
JB
15700 macro_define_object (file, line, name, replacement);
15701
15702 xfree (name);
15703 }
15704 else if (*p == '(')
15705 {
15706 /* It's a function-like macro. */
15707 char *name = copy_string (body, p - body);
15708 int argc = 0;
15709 int argv_size = 1;
15710 char **argv = xmalloc (argv_size * sizeof (*argv));
15711
15712 p++;
15713
15714 p = consume_improper_spaces (p, body);
15715
15716 /* Parse the formal argument list. */
15717 while (*p && *p != ')')
15718 {
15719 /* Find the extent of the current argument name. */
15720 const char *arg_start = p;
15721
15722 while (*p && *p != ',' && *p != ')' && *p != ' ')
15723 p++;
15724
15725 if (! *p || p == arg_start)
4d3c2250 15726 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15727 else
15728 {
15729 /* Make sure argv has room for the new argument. */
15730 if (argc >= argv_size)
15731 {
15732 argv_size *= 2;
15733 argv = xrealloc (argv, argv_size * sizeof (*argv));
15734 }
15735
15736 argv[argc++] = copy_string (arg_start, p - arg_start);
15737 }
15738
15739 p = consume_improper_spaces (p, body);
15740
15741 /* Consume the comma, if present. */
15742 if (*p == ',')
15743 {
15744 p++;
15745
15746 p = consume_improper_spaces (p, body);
15747 }
15748 }
15749
15750 if (*p == ')')
15751 {
15752 p++;
15753
15754 if (*p == ' ')
15755 /* Perfectly formed definition, no complaints. */
15756 macro_define_function (file, line, name,
6e70227d 15757 argc, (const char **) argv,
2e276125
JB
15758 p + 1);
15759 else if (*p == '\0')
15760 {
15761 /* Complain, but do define it. */
4d3c2250 15762 dwarf2_macro_malformed_definition_complaint (body);
2e276125 15763 macro_define_function (file, line, name,
6e70227d 15764 argc, (const char **) argv,
2e276125
JB
15765 p);
15766 }
15767 else
15768 /* Just complain. */
4d3c2250 15769 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15770 }
15771 else
15772 /* Just complain. */
4d3c2250 15773 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15774
15775 xfree (name);
15776 {
15777 int i;
15778
15779 for (i = 0; i < argc; i++)
15780 xfree (argv[i]);
15781 }
15782 xfree (argv);
15783 }
15784 else
4d3c2250 15785 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15786}
15787
cf2c3c16
TT
15788/* Skip some bytes from BYTES according to the form given in FORM.
15789 Returns the new pointer. */
2e276125 15790
cf2c3c16
TT
15791static gdb_byte *
15792skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15793 enum dwarf_form form,
15794 unsigned int offset_size,
15795 struct dwarf2_section_info *section)
2e276125 15796{
cf2c3c16 15797 unsigned int bytes_read;
2e276125 15798
cf2c3c16 15799 switch (form)
2e276125 15800 {
cf2c3c16
TT
15801 case DW_FORM_data1:
15802 case DW_FORM_flag:
15803 ++bytes;
15804 break;
15805
15806 case DW_FORM_data2:
15807 bytes += 2;
15808 break;
15809
15810 case DW_FORM_data4:
15811 bytes += 4;
15812 break;
15813
15814 case DW_FORM_data8:
15815 bytes += 8;
15816 break;
15817
15818 case DW_FORM_string:
15819 read_direct_string (abfd, bytes, &bytes_read);
15820 bytes += bytes_read;
15821 break;
15822
15823 case DW_FORM_sec_offset:
15824 case DW_FORM_strp:
15825 bytes += offset_size;
15826 break;
15827
15828 case DW_FORM_block:
15829 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15830 bytes += bytes_read;
15831 break;
15832
15833 case DW_FORM_block1:
15834 bytes += 1 + read_1_byte (abfd, bytes);
15835 break;
15836 case DW_FORM_block2:
15837 bytes += 2 + read_2_bytes (abfd, bytes);
15838 break;
15839 case DW_FORM_block4:
15840 bytes += 4 + read_4_bytes (abfd, bytes);
15841 break;
15842
15843 case DW_FORM_sdata:
15844 case DW_FORM_udata:
3019eac3
DE
15845 case DW_FORM_GNU_addr_index:
15846 case DW_FORM_GNU_str_index:
cf2c3c16
TT
15847 bytes = skip_leb128 (abfd, bytes);
15848 break;
15849
15850 default:
15851 {
15852 complain:
15853 complaint (&symfile_complaints,
15854 _("invalid form 0x%x in `%s'"),
15855 form,
15856 section->asection->name);
15857 return NULL;
15858 }
2e276125
JB
15859 }
15860
cf2c3c16
TT
15861 return bytes;
15862}
757a13d0 15863
cf2c3c16
TT
15864/* A helper for dwarf_decode_macros that handles skipping an unknown
15865 opcode. Returns an updated pointer to the macro data buffer; or,
15866 on error, issues a complaint and returns NULL. */
757a13d0 15867
cf2c3c16
TT
15868static gdb_byte *
15869skip_unknown_opcode (unsigned int opcode,
15870 gdb_byte **opcode_definitions,
15871 gdb_byte *mac_ptr,
15872 bfd *abfd,
15873 unsigned int offset_size,
15874 struct dwarf2_section_info *section)
15875{
15876 unsigned int bytes_read, i;
15877 unsigned long arg;
15878 gdb_byte *defn;
2e276125 15879
cf2c3c16 15880 if (opcode_definitions[opcode] == NULL)
2e276125 15881 {
cf2c3c16
TT
15882 complaint (&symfile_complaints,
15883 _("unrecognized DW_MACFINO opcode 0x%x"),
15884 opcode);
15885 return NULL;
15886 }
2e276125 15887
cf2c3c16
TT
15888 defn = opcode_definitions[opcode];
15889 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15890 defn += bytes_read;
2e276125 15891
cf2c3c16
TT
15892 for (i = 0; i < arg; ++i)
15893 {
15894 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15895 if (mac_ptr == NULL)
15896 {
15897 /* skip_form_bytes already issued the complaint. */
15898 return NULL;
15899 }
15900 }
757a13d0 15901
cf2c3c16
TT
15902 return mac_ptr;
15903}
757a13d0 15904
cf2c3c16
TT
15905/* A helper function which parses the header of a macro section.
15906 If the macro section is the extended (for now called "GNU") type,
15907 then this updates *OFFSET_SIZE. Returns a pointer to just after
15908 the header, or issues a complaint and returns NULL on error. */
757a13d0 15909
cf2c3c16
TT
15910static gdb_byte *
15911dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15912 bfd *abfd,
15913 gdb_byte *mac_ptr,
15914 unsigned int *offset_size,
15915 int section_is_gnu)
15916{
15917 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15918
cf2c3c16
TT
15919 if (section_is_gnu)
15920 {
15921 unsigned int version, flags;
757a13d0 15922
cf2c3c16
TT
15923 version = read_2_bytes (abfd, mac_ptr);
15924 if (version != 4)
15925 {
15926 complaint (&symfile_complaints,
15927 _("unrecognized version `%d' in .debug_macro section"),
15928 version);
15929 return NULL;
15930 }
15931 mac_ptr += 2;
757a13d0 15932
cf2c3c16
TT
15933 flags = read_1_byte (abfd, mac_ptr);
15934 ++mac_ptr;
15935 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15936
cf2c3c16
TT
15937 if ((flags & 2) != 0)
15938 /* We don't need the line table offset. */
15939 mac_ptr += *offset_size;
757a13d0 15940
cf2c3c16
TT
15941 /* Vendor opcode descriptions. */
15942 if ((flags & 4) != 0)
15943 {
15944 unsigned int i, count;
757a13d0 15945
cf2c3c16
TT
15946 count = read_1_byte (abfd, mac_ptr);
15947 ++mac_ptr;
15948 for (i = 0; i < count; ++i)
15949 {
15950 unsigned int opcode, bytes_read;
15951 unsigned long arg;
15952
15953 opcode = read_1_byte (abfd, mac_ptr);
15954 ++mac_ptr;
15955 opcode_definitions[opcode] = mac_ptr;
15956 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15957 mac_ptr += bytes_read;
15958 mac_ptr += arg;
15959 }
757a13d0 15960 }
cf2c3c16 15961 }
757a13d0 15962
cf2c3c16
TT
15963 return mac_ptr;
15964}
757a13d0 15965
cf2c3c16 15966/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 15967 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
15968
15969static void
15970dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15971 struct macro_source_file *current_file,
15972 struct line_header *lh, char *comp_dir,
15973 struct dwarf2_section_info *section,
15974 int section_is_gnu,
15975 unsigned int offset_size,
8fc3fc34
TT
15976 struct objfile *objfile,
15977 htab_t include_hash)
cf2c3c16
TT
15978{
15979 enum dwarf_macro_record_type macinfo_type;
15980 int at_commandline;
15981 gdb_byte *opcode_definitions[256];
757a13d0 15982
cf2c3c16
TT
15983 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15984 &offset_size, section_is_gnu);
15985 if (mac_ptr == NULL)
15986 {
15987 /* We already issued a complaint. */
15988 return;
15989 }
757a13d0
JK
15990
15991 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15992 GDB is still reading the definitions from command line. First
15993 DW_MACINFO_start_file will need to be ignored as it was already executed
15994 to create CURRENT_FILE for the main source holding also the command line
15995 definitions. On first met DW_MACINFO_start_file this flag is reset to
15996 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15997
15998 at_commandline = 1;
15999
16000 do
16001 {
16002 /* Do we at least have room for a macinfo type byte? */
16003 if (mac_ptr >= mac_end)
16004 {
cf2c3c16 16005 dwarf2_macros_too_long_complaint (section);
757a13d0
JK
16006 break;
16007 }
16008
16009 macinfo_type = read_1_byte (abfd, mac_ptr);
16010 mac_ptr++;
16011
cf2c3c16
TT
16012 /* Note that we rely on the fact that the corresponding GNU and
16013 DWARF constants are the same. */
757a13d0
JK
16014 switch (macinfo_type)
16015 {
16016 /* A zero macinfo type indicates the end of the macro
16017 information. */
16018 case 0:
16019 break;
2e276125 16020
cf2c3c16
TT
16021 case DW_MACRO_GNU_define:
16022 case DW_MACRO_GNU_undef:
16023 case DW_MACRO_GNU_define_indirect:
16024 case DW_MACRO_GNU_undef_indirect:
2e276125 16025 {
891d2f0b 16026 unsigned int bytes_read;
2e276125
JB
16027 int line;
16028 char *body;
cf2c3c16 16029 int is_define;
2e276125 16030
cf2c3c16
TT
16031 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16032 mac_ptr += bytes_read;
16033
16034 if (macinfo_type == DW_MACRO_GNU_define
16035 || macinfo_type == DW_MACRO_GNU_undef)
16036 {
16037 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16038 mac_ptr += bytes_read;
16039 }
16040 else
16041 {
16042 LONGEST str_offset;
16043
16044 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16045 mac_ptr += offset_size;
2e276125 16046
cf2c3c16
TT
16047 body = read_indirect_string_at_offset (abfd, str_offset);
16048 }
16049
16050 is_define = (macinfo_type == DW_MACRO_GNU_define
16051 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 16052 if (! current_file)
757a13d0
JK
16053 {
16054 /* DWARF violation as no main source is present. */
16055 complaint (&symfile_complaints,
16056 _("debug info with no main source gives macro %s "
16057 "on line %d: %s"),
cf2c3c16
TT
16058 is_define ? _("definition") : _("undefinition"),
16059 line, body);
757a13d0
JK
16060 break;
16061 }
3e43a32a
MS
16062 if ((line == 0 && !at_commandline)
16063 || (line != 0 && at_commandline))
4d3c2250 16064 complaint (&symfile_complaints,
757a13d0
JK
16065 _("debug info gives %s macro %s with %s line %d: %s"),
16066 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 16067 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
16068 line == 0 ? _("zero") : _("non-zero"), line, body);
16069
cf2c3c16 16070 if (is_define)
757a13d0 16071 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
16072 else
16073 {
16074 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16075 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16076 macro_undef (current_file, line, body);
16077 }
2e276125
JB
16078 }
16079 break;
16080
cf2c3c16 16081 case DW_MACRO_GNU_start_file:
2e276125 16082 {
891d2f0b 16083 unsigned int bytes_read;
2e276125
JB
16084 int line, file;
16085
16086 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16087 mac_ptr += bytes_read;
16088 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16089 mac_ptr += bytes_read;
16090
3e43a32a
MS
16091 if ((line == 0 && !at_commandline)
16092 || (line != 0 && at_commandline))
757a13d0
JK
16093 complaint (&symfile_complaints,
16094 _("debug info gives source %d included "
16095 "from %s at %s line %d"),
16096 file, at_commandline ? _("command-line") : _("file"),
16097 line == 0 ? _("zero") : _("non-zero"), line);
16098
16099 if (at_commandline)
16100 {
cf2c3c16
TT
16101 /* This DW_MACRO_GNU_start_file was executed in the
16102 pass one. */
757a13d0
JK
16103 at_commandline = 0;
16104 }
16105 else
16106 current_file = macro_start_file (file, line,
16107 current_file, comp_dir,
cf2c3c16 16108 lh, objfile);
2e276125
JB
16109 }
16110 break;
16111
cf2c3c16 16112 case DW_MACRO_GNU_end_file:
2e276125 16113 if (! current_file)
4d3c2250 16114 complaint (&symfile_complaints,
3e43a32a
MS
16115 _("macro debug info has an unmatched "
16116 "`close_file' directive"));
2e276125
JB
16117 else
16118 {
16119 current_file = current_file->included_by;
16120 if (! current_file)
16121 {
cf2c3c16 16122 enum dwarf_macro_record_type next_type;
2e276125
JB
16123
16124 /* GCC circa March 2002 doesn't produce the zero
16125 type byte marking the end of the compilation
16126 unit. Complain if it's not there, but exit no
16127 matter what. */
16128
16129 /* Do we at least have room for a macinfo type byte? */
16130 if (mac_ptr >= mac_end)
16131 {
cf2c3c16 16132 dwarf2_macros_too_long_complaint (section);
2e276125
JB
16133 return;
16134 }
16135
16136 /* We don't increment mac_ptr here, so this is just
16137 a look-ahead. */
16138 next_type = read_1_byte (abfd, mac_ptr);
16139 if (next_type != 0)
4d3c2250 16140 complaint (&symfile_complaints,
3e43a32a
MS
16141 _("no terminating 0-type entry for "
16142 "macros in `.debug_macinfo' section"));
2e276125
JB
16143
16144 return;
16145 }
16146 }
16147 break;
16148
cf2c3c16
TT
16149 case DW_MACRO_GNU_transparent_include:
16150 {
16151 LONGEST offset;
8fc3fc34 16152 void **slot;
cf2c3c16
TT
16153
16154 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16155 mac_ptr += offset_size;
16156
8fc3fc34
TT
16157 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16158 if (*slot != NULL)
16159 {
16160 /* This has actually happened; see
16161 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16162 complaint (&symfile_complaints,
16163 _("recursive DW_MACRO_GNU_transparent_include in "
16164 ".debug_macro section"));
16165 }
16166 else
16167 {
16168 *slot = mac_ptr;
16169
16170 dwarf_decode_macro_bytes (abfd,
16171 section->buffer + offset,
16172 mac_end, current_file,
16173 lh, comp_dir,
16174 section, section_is_gnu,
16175 offset_size, objfile, include_hash);
16176
16177 htab_remove_elt (include_hash, mac_ptr);
16178 }
cf2c3c16
TT
16179 }
16180 break;
16181
2e276125 16182 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
16183 if (!section_is_gnu)
16184 {
16185 unsigned int bytes_read;
16186 int constant;
2e276125 16187
cf2c3c16
TT
16188 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16189 mac_ptr += bytes_read;
16190 read_direct_string (abfd, mac_ptr, &bytes_read);
16191 mac_ptr += bytes_read;
2e276125 16192
cf2c3c16
TT
16193 /* We don't recognize any vendor extensions. */
16194 break;
16195 }
16196 /* FALLTHROUGH */
16197
16198 default:
16199 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16200 mac_ptr, abfd, offset_size,
16201 section);
16202 if (mac_ptr == NULL)
16203 return;
16204 break;
2e276125 16205 }
757a13d0 16206 } while (macinfo_type != 0);
2e276125 16207}
8e19ed76 16208
cf2c3c16
TT
16209static void
16210dwarf_decode_macros (struct line_header *lh, unsigned int offset,
16211 char *comp_dir, bfd *abfd,
16212 struct dwarf2_cu *cu,
16213 struct dwarf2_section_info *section,
fceca515 16214 int section_is_gnu, const char *section_name)
cf2c3c16 16215{
bb5ed363 16216 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
16217 gdb_byte *mac_ptr, *mac_end;
16218 struct macro_source_file *current_file = 0;
16219 enum dwarf_macro_record_type macinfo_type;
16220 unsigned int offset_size = cu->header.offset_size;
16221 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
16222 struct cleanup *cleanup;
16223 htab_t include_hash;
16224 void **slot;
cf2c3c16 16225
bb5ed363 16226 dwarf2_read_section (objfile, section);
cf2c3c16
TT
16227 if (section->buffer == NULL)
16228 {
fceca515 16229 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
16230 return;
16231 }
16232
16233 /* First pass: Find the name of the base filename.
16234 This filename is needed in order to process all macros whose definition
16235 (or undefinition) comes from the command line. These macros are defined
16236 before the first DW_MACINFO_start_file entry, and yet still need to be
16237 associated to the base file.
16238
16239 To determine the base file name, we scan the macro definitions until we
16240 reach the first DW_MACINFO_start_file entry. We then initialize
16241 CURRENT_FILE accordingly so that any macro definition found before the
16242 first DW_MACINFO_start_file can still be associated to the base file. */
16243
16244 mac_ptr = section->buffer + offset;
16245 mac_end = section->buffer + section->size;
16246
16247 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16248 &offset_size, section_is_gnu);
16249 if (mac_ptr == NULL)
16250 {
16251 /* We already issued a complaint. */
16252 return;
16253 }
16254
16255 do
16256 {
16257 /* Do we at least have room for a macinfo type byte? */
16258 if (mac_ptr >= mac_end)
16259 {
16260 /* Complaint is printed during the second pass as GDB will probably
16261 stop the first pass earlier upon finding
16262 DW_MACINFO_start_file. */
16263 break;
16264 }
16265
16266 macinfo_type = read_1_byte (abfd, mac_ptr);
16267 mac_ptr++;
16268
16269 /* Note that we rely on the fact that the corresponding GNU and
16270 DWARF constants are the same. */
16271 switch (macinfo_type)
16272 {
16273 /* A zero macinfo type indicates the end of the macro
16274 information. */
16275 case 0:
16276 break;
16277
16278 case DW_MACRO_GNU_define:
16279 case DW_MACRO_GNU_undef:
16280 /* Only skip the data by MAC_PTR. */
16281 {
16282 unsigned int bytes_read;
16283
16284 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16285 mac_ptr += bytes_read;
16286 read_direct_string (abfd, mac_ptr, &bytes_read);
16287 mac_ptr += bytes_read;
16288 }
16289 break;
16290
16291 case DW_MACRO_GNU_start_file:
16292 {
16293 unsigned int bytes_read;
16294 int line, file;
16295
16296 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16297 mac_ptr += bytes_read;
16298 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16299 mac_ptr += bytes_read;
16300
16301 current_file = macro_start_file (file, line, current_file,
bb5ed363 16302 comp_dir, lh, objfile);
cf2c3c16
TT
16303 }
16304 break;
16305
16306 case DW_MACRO_GNU_end_file:
16307 /* No data to skip by MAC_PTR. */
16308 break;
16309
16310 case DW_MACRO_GNU_define_indirect:
16311 case DW_MACRO_GNU_undef_indirect:
16312 {
16313 unsigned int bytes_read;
16314
16315 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16316 mac_ptr += bytes_read;
16317 mac_ptr += offset_size;
16318 }
16319 break;
16320
16321 case DW_MACRO_GNU_transparent_include:
16322 /* Note that, according to the spec, a transparent include
16323 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16324 skip this opcode. */
16325 mac_ptr += offset_size;
16326 break;
16327
16328 case DW_MACINFO_vendor_ext:
16329 /* Only skip the data by MAC_PTR. */
16330 if (!section_is_gnu)
16331 {
16332 unsigned int bytes_read;
16333
16334 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16335 mac_ptr += bytes_read;
16336 read_direct_string (abfd, mac_ptr, &bytes_read);
16337 mac_ptr += bytes_read;
16338 }
16339 /* FALLTHROUGH */
16340
16341 default:
16342 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
16343 mac_ptr, abfd, offset_size,
16344 section);
16345 if (mac_ptr == NULL)
16346 return;
16347 break;
16348 }
16349 } while (macinfo_type != 0 && current_file == NULL);
16350
16351 /* Second pass: Process all entries.
16352
16353 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16354 command-line macro definitions/undefinitions. This flag is unset when we
16355 reach the first DW_MACINFO_start_file entry. */
16356
8fc3fc34
TT
16357 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16358 NULL, xcalloc, xfree);
16359 cleanup = make_cleanup_htab_delete (include_hash);
16360 mac_ptr = section->buffer + offset;
16361 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16362 *slot = mac_ptr;
16363 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
cf2c3c16 16364 current_file, lh, comp_dir, section, section_is_gnu,
8fc3fc34
TT
16365 offset_size, objfile, include_hash);
16366 do_cleanups (cleanup);
cf2c3c16
TT
16367}
16368
8e19ed76 16369/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 16370 if so return true else false. */
380bca97 16371
8e19ed76
PS
16372static int
16373attr_form_is_block (struct attribute *attr)
16374{
16375 return (attr == NULL ? 0 :
16376 attr->form == DW_FORM_block1
16377 || attr->form == DW_FORM_block2
16378 || attr->form == DW_FORM_block4
2dc7f7b3
TT
16379 || attr->form == DW_FORM_block
16380 || attr->form == DW_FORM_exprloc);
8e19ed76 16381}
4c2df51b 16382
c6a0999f
JB
16383/* Return non-zero if ATTR's value is a section offset --- classes
16384 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16385 You may use DW_UNSND (attr) to retrieve such offsets.
16386
16387 Section 7.5.4, "Attribute Encodings", explains that no attribute
16388 may have a value that belongs to more than one of these classes; it
16389 would be ambiguous if we did, because we use the same forms for all
16390 of them. */
380bca97 16391
3690dd37
JB
16392static int
16393attr_form_is_section_offset (struct attribute *attr)
16394{
16395 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
16396 || attr->form == DW_FORM_data8
16397 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
16398}
16399
3690dd37
JB
16400/* Return non-zero if ATTR's value falls in the 'constant' class, or
16401 zero otherwise. When this function returns true, you can apply
16402 dwarf2_get_attr_constant_value to it.
16403
16404 However, note that for some attributes you must check
16405 attr_form_is_section_offset before using this test. DW_FORM_data4
16406 and DW_FORM_data8 are members of both the constant class, and of
16407 the classes that contain offsets into other debug sections
16408 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16409 that, if an attribute's can be either a constant or one of the
16410 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16411 taken as section offsets, not constants. */
380bca97 16412
3690dd37
JB
16413static int
16414attr_form_is_constant (struct attribute *attr)
16415{
16416 switch (attr->form)
16417 {
16418 case DW_FORM_sdata:
16419 case DW_FORM_udata:
16420 case DW_FORM_data1:
16421 case DW_FORM_data2:
16422 case DW_FORM_data4:
16423 case DW_FORM_data8:
16424 return 1;
16425 default:
16426 return 0;
16427 }
16428}
16429
3019eac3
DE
16430/* Return the .debug_loc section to use for CU.
16431 For DWO files use .debug_loc.dwo. */
16432
16433static struct dwarf2_section_info *
16434cu_debug_loc_section (struct dwarf2_cu *cu)
16435{
16436 if (cu->dwo_unit)
16437 return &cu->dwo_unit->dwo_file->sections.loc;
16438 return &dwarf2_per_objfile->loc;
16439}
16440
8cf6f0b1
TT
16441/* A helper function that fills in a dwarf2_loclist_baton. */
16442
16443static void
16444fill_in_loclist_baton (struct dwarf2_cu *cu,
16445 struct dwarf2_loclist_baton *baton,
16446 struct attribute *attr)
16447{
3019eac3
DE
16448 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16449
16450 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
16451
16452 baton->per_cu = cu->per_cu;
16453 gdb_assert (baton->per_cu);
16454 /* We don't know how long the location list is, but make sure we
16455 don't run off the edge of the section. */
3019eac3
DE
16456 baton->size = section->size - DW_UNSND (attr);
16457 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1
TT
16458 baton->base_address = cu->base_address;
16459}
16460
4c2df51b
DJ
16461static void
16462dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 16463 struct dwarf2_cu *cu)
4c2df51b 16464{
bb5ed363 16465 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 16466 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 16467
3690dd37 16468 if (attr_form_is_section_offset (attr)
3019eac3 16469 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
16470 the section. If so, fall through to the complaint in the
16471 other branch. */
3019eac3 16472 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 16473 {
0d53c4c4 16474 struct dwarf2_loclist_baton *baton;
4c2df51b 16475
bb5ed363 16476 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16477 sizeof (struct dwarf2_loclist_baton));
4c2df51b 16478
8cf6f0b1 16479 fill_in_loclist_baton (cu, baton, attr);
be391dca 16480
d00adf39 16481 if (cu->base_known == 0)
0d53c4c4 16482 complaint (&symfile_complaints,
3e43a32a
MS
16483 _("Location list used without "
16484 "specifying the CU base address."));
4c2df51b 16485
768a979c 16486 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
16487 SYMBOL_LOCATION_BATON (sym) = baton;
16488 }
16489 else
16490 {
16491 struct dwarf2_locexpr_baton *baton;
16492
bb5ed363 16493 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16494 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
16495 baton->per_cu = cu->per_cu;
16496 gdb_assert (baton->per_cu);
0d53c4c4
DJ
16497
16498 if (attr_form_is_block (attr))
16499 {
16500 /* Note that we're just copying the block's data pointer
16501 here, not the actual data. We're still pointing into the
6502dd73
DJ
16502 info_buffer for SYM's objfile; right now we never release
16503 that buffer, but when we do clean up properly this may
16504 need to change. */
0d53c4c4
DJ
16505 baton->size = DW_BLOCK (attr)->size;
16506 baton->data = DW_BLOCK (attr)->data;
16507 }
16508 else
16509 {
16510 dwarf2_invalid_attrib_class_complaint ("location description",
16511 SYMBOL_NATURAL_NAME (sym));
16512 baton->size = 0;
0d53c4c4 16513 }
6e70227d 16514
768a979c 16515 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
16516 SYMBOL_LOCATION_BATON (sym) = baton;
16517 }
4c2df51b 16518}
6502dd73 16519
9aa1f1e3
TT
16520/* Return the OBJFILE associated with the compilation unit CU. If CU
16521 came from a separate debuginfo file, then the master objfile is
16522 returned. */
ae0d2f24
UW
16523
16524struct objfile *
16525dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16526{
9291a0cd 16527 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
16528
16529 /* Return the master objfile, so that we can report and look up the
16530 correct file containing this variable. */
16531 if (objfile->separate_debug_objfile_backlink)
16532 objfile = objfile->separate_debug_objfile_backlink;
16533
16534 return objfile;
16535}
16536
96408a79
SA
16537/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16538 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16539 CU_HEADERP first. */
16540
16541static const struct comp_unit_head *
16542per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16543 struct dwarf2_per_cu_data *per_cu)
16544{
16545 struct objfile *objfile;
16546 struct dwarf2_per_objfile *per_objfile;
16547 gdb_byte *info_ptr;
16548
16549 if (per_cu->cu)
16550 return &per_cu->cu->header;
16551
16552 objfile = per_cu->objfile;
16553 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
b64f50a1 16554 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
96408a79
SA
16555
16556 memset (cu_headerp, 0, sizeof (*cu_headerp));
16557 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16558
16559 return cu_headerp;
16560}
16561
ae0d2f24
UW
16562/* Return the address size given in the compilation unit header for CU. */
16563
98714339 16564int
ae0d2f24
UW
16565dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16566{
96408a79
SA
16567 struct comp_unit_head cu_header_local;
16568 const struct comp_unit_head *cu_headerp;
c471e790 16569
96408a79
SA
16570 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16571
16572 return cu_headerp->addr_size;
ae0d2f24
UW
16573}
16574
9eae7c52
TT
16575/* Return the offset size given in the compilation unit header for CU. */
16576
16577int
16578dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16579{
96408a79
SA
16580 struct comp_unit_head cu_header_local;
16581 const struct comp_unit_head *cu_headerp;
9c6c53f7 16582
96408a79
SA
16583 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16584
16585 return cu_headerp->offset_size;
16586}
16587
16588/* See its dwarf2loc.h declaration. */
16589
16590int
16591dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16592{
16593 struct comp_unit_head cu_header_local;
16594 const struct comp_unit_head *cu_headerp;
16595
16596 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16597
16598 if (cu_headerp->version == 2)
16599 return cu_headerp->addr_size;
16600 else
16601 return cu_headerp->offset_size;
181cebd4
JK
16602}
16603
9aa1f1e3
TT
16604/* Return the text offset of the CU. The returned offset comes from
16605 this CU's objfile. If this objfile came from a separate debuginfo
16606 file, then the offset may be different from the corresponding
16607 offset in the parent objfile. */
16608
16609CORE_ADDR
16610dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16611{
bb3fa9d0 16612 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
16613
16614 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16615}
16616
348e048f
DE
16617/* Locate the .debug_info compilation unit from CU's objfile which contains
16618 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
16619
16620static struct dwarf2_per_cu_data *
b64f50a1 16621dwarf2_find_containing_comp_unit (sect_offset offset,
ae038cb0
DJ
16622 struct objfile *objfile)
16623{
16624 struct dwarf2_per_cu_data *this_cu;
16625 int low, high;
16626
ae038cb0
DJ
16627 low = 0;
16628 high = dwarf2_per_objfile->n_comp_units - 1;
16629 while (high > low)
16630 {
16631 int mid = low + (high - low) / 2;
9a619af0 16632
b64f50a1
JK
16633 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16634 >= offset.sect_off)
ae038cb0
DJ
16635 high = mid;
16636 else
16637 low = mid + 1;
16638 }
16639 gdb_assert (low == high);
b64f50a1
JK
16640 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16641 > offset.sect_off)
ae038cb0 16642 {
10b3939b 16643 if (low == 0)
8a3fe4f8
AC
16644 error (_("Dwarf Error: could not find partial DIE containing "
16645 "offset 0x%lx [in module %s]"),
b64f50a1 16646 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 16647
b64f50a1
JK
16648 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16649 <= offset.sect_off);
ae038cb0
DJ
16650 return dwarf2_per_objfile->all_comp_units[low-1];
16651 }
16652 else
16653 {
16654 this_cu = dwarf2_per_objfile->all_comp_units[low];
16655 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
16656 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16657 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16658 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
16659 return this_cu;
16660 }
16661}
16662
23745b47 16663/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 16664
9816fde3 16665static void
23745b47 16666init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 16667{
9816fde3 16668 memset (cu, 0, sizeof (*cu));
23745b47
DE
16669 per_cu->cu = cu;
16670 cu->per_cu = per_cu;
16671 cu->objfile = per_cu->objfile;
93311388 16672 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
16673}
16674
16675/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16676
16677static void
95554aad
TT
16678prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
16679 enum language pretend_language)
9816fde3
JK
16680{
16681 struct attribute *attr;
16682
16683 /* Set the language we're debugging. */
16684 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16685 if (attr)
16686 set_cu_language (DW_UNSND (attr), cu);
16687 else
9cded63f 16688 {
95554aad 16689 cu->language = pretend_language;
9cded63f
TT
16690 cu->language_defn = language_def (cu->language);
16691 }
dee91e82
DE
16692
16693 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
16694 if (attr)
16695 cu->producer = DW_STRING (attr);
93311388
DE
16696}
16697
ae038cb0
DJ
16698/* Release one cached compilation unit, CU. We unlink it from the tree
16699 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
16700 the caller is responsible for that.
16701 NOTE: DATA is a void * because this function is also used as a
16702 cleanup routine. */
ae038cb0
DJ
16703
16704static void
68dc6402 16705free_heap_comp_unit (void *data)
ae038cb0
DJ
16706{
16707 struct dwarf2_cu *cu = data;
16708
23745b47
DE
16709 gdb_assert (cu->per_cu != NULL);
16710 cu->per_cu->cu = NULL;
ae038cb0
DJ
16711 cu->per_cu = NULL;
16712
16713 obstack_free (&cu->comp_unit_obstack, NULL);
16714
16715 xfree (cu);
16716}
16717
72bf9492 16718/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 16719 when we're finished with it. We can't free the pointer itself, but be
dee91e82 16720 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
16721
16722static void
16723free_stack_comp_unit (void *data)
16724{
16725 struct dwarf2_cu *cu = data;
16726
23745b47
DE
16727 gdb_assert (cu->per_cu != NULL);
16728 cu->per_cu->cu = NULL;
16729 cu->per_cu = NULL;
16730
72bf9492
DJ
16731 obstack_free (&cu->comp_unit_obstack, NULL);
16732 cu->partial_dies = NULL;
ae038cb0
DJ
16733}
16734
16735/* Free all cached compilation units. */
16736
16737static void
16738free_cached_comp_units (void *data)
16739{
16740 struct dwarf2_per_cu_data *per_cu, **last_chain;
16741
16742 per_cu = dwarf2_per_objfile->read_in_chain;
16743 last_chain = &dwarf2_per_objfile->read_in_chain;
16744 while (per_cu != NULL)
16745 {
16746 struct dwarf2_per_cu_data *next_cu;
16747
16748 next_cu = per_cu->cu->read_in_chain;
16749
68dc6402 16750 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16751 *last_chain = next_cu;
16752
16753 per_cu = next_cu;
16754 }
16755}
16756
16757/* Increase the age counter on each cached compilation unit, and free
16758 any that are too old. */
16759
16760static void
16761age_cached_comp_units (void)
16762{
16763 struct dwarf2_per_cu_data *per_cu, **last_chain;
16764
16765 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16766 per_cu = dwarf2_per_objfile->read_in_chain;
16767 while (per_cu != NULL)
16768 {
16769 per_cu->cu->last_used ++;
16770 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16771 dwarf2_mark (per_cu->cu);
16772 per_cu = per_cu->cu->read_in_chain;
16773 }
16774
16775 per_cu = dwarf2_per_objfile->read_in_chain;
16776 last_chain = &dwarf2_per_objfile->read_in_chain;
16777 while (per_cu != NULL)
16778 {
16779 struct dwarf2_per_cu_data *next_cu;
16780
16781 next_cu = per_cu->cu->read_in_chain;
16782
16783 if (!per_cu->cu->mark)
16784 {
68dc6402 16785 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16786 *last_chain = next_cu;
16787 }
16788 else
16789 last_chain = &per_cu->cu->read_in_chain;
16790
16791 per_cu = next_cu;
16792 }
16793}
16794
16795/* Remove a single compilation unit from the cache. */
16796
16797static void
dee91e82 16798free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
16799{
16800 struct dwarf2_per_cu_data *per_cu, **last_chain;
16801
16802 per_cu = dwarf2_per_objfile->read_in_chain;
16803 last_chain = &dwarf2_per_objfile->read_in_chain;
16804 while (per_cu != NULL)
16805 {
16806 struct dwarf2_per_cu_data *next_cu;
16807
16808 next_cu = per_cu->cu->read_in_chain;
16809
dee91e82 16810 if (per_cu == target_per_cu)
ae038cb0 16811 {
68dc6402 16812 free_heap_comp_unit (per_cu->cu);
dee91e82 16813 per_cu->cu = NULL;
ae038cb0
DJ
16814 *last_chain = next_cu;
16815 break;
16816 }
16817 else
16818 last_chain = &per_cu->cu->read_in_chain;
16819
16820 per_cu = next_cu;
16821 }
16822}
16823
fe3e1990
DJ
16824/* Release all extra memory associated with OBJFILE. */
16825
16826void
16827dwarf2_free_objfile (struct objfile *objfile)
16828{
16829 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16830
16831 if (dwarf2_per_objfile == NULL)
16832 return;
16833
16834 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16835 free_cached_comp_units (NULL);
16836
7b9f3c50
DE
16837 if (dwarf2_per_objfile->quick_file_names_table)
16838 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16839
fe3e1990
DJ
16840 /* Everything else should be on the objfile obstack. */
16841}
16842
dee91e82
DE
16843/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
16844 We store these in a hash table separate from the DIEs, and preserve them
16845 when the DIEs are flushed out of cache.
16846
16847 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
16848 uniquely identify the type. A file may have multiple .debug_types sections,
16849 or the type may come from a DWO file. We have to use something in
16850 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
16851 routine, get_die_type_at_offset, from outside this file, and thus won't
16852 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
16853 of the objfile. */
1c379e20 16854
dee91e82 16855struct dwarf2_per_cu_offset_and_type
1c379e20 16856{
dee91e82 16857 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 16858 sect_offset offset;
1c379e20
DJ
16859 struct type *type;
16860};
16861
dee91e82 16862/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
16863
16864static hashval_t
dee91e82 16865per_cu_offset_and_type_hash (const void *item)
1c379e20 16866{
dee91e82 16867 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 16868
dee91e82 16869 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
16870}
16871
dee91e82 16872/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
16873
16874static int
dee91e82 16875per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 16876{
dee91e82
DE
16877 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
16878 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16879
dee91e82
DE
16880 return (ofs_lhs->per_cu == ofs_rhs->per_cu
16881 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
16882}
16883
16884/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16885 table if necessary. For convenience, return TYPE.
16886
16887 The DIEs reading must have careful ordering to:
16888 * Not cause infite loops trying to read in DIEs as a prerequisite for
16889 reading current DIE.
16890 * Not trying to dereference contents of still incompletely read in types
16891 while reading in other DIEs.
16892 * Enable referencing still incompletely read in types just by a pointer to
16893 the type without accessing its fields.
16894
16895 Therefore caller should follow these rules:
16896 * Try to fetch any prerequisite types we may need to build this DIE type
16897 before building the type and calling set_die_type.
e71ec853 16898 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16899 possible before fetching more types to complete the current type.
16900 * Make the type as complete as possible before fetching more types. */
1c379e20 16901
f792889a 16902static struct type *
1c379e20
DJ
16903set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16904{
dee91e82 16905 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 16906 struct objfile *objfile = cu->objfile;
1c379e20 16907
b4ba55a1
JB
16908 /* For Ada types, make sure that the gnat-specific data is always
16909 initialized (if not already set). There are a few types where
16910 we should not be doing so, because the type-specific area is
16911 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16912 where the type-specific area is used to store the floatformat).
16913 But this is not a problem, because the gnat-specific information
16914 is actually not needed for these types. */
16915 if (need_gnat_info (cu)
16916 && TYPE_CODE (type) != TYPE_CODE_FUNC
16917 && TYPE_CODE (type) != TYPE_CODE_FLT
16918 && !HAVE_GNAT_AUX_INFO (type))
16919 INIT_GNAT_SPECIFIC (type);
16920
dee91e82 16921 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 16922 {
dee91e82
DE
16923 dwarf2_per_objfile->die_type_hash =
16924 htab_create_alloc_ex (127,
16925 per_cu_offset_and_type_hash,
16926 per_cu_offset_and_type_eq,
16927 NULL,
16928 &objfile->objfile_obstack,
16929 hashtab_obstack_allocate,
16930 dummy_obstack_deallocate);
f792889a 16931 }
1c379e20 16932
dee91e82 16933 ofs.per_cu = cu->per_cu;
1c379e20
DJ
16934 ofs.offset = die->offset;
16935 ofs.type = type;
dee91e82
DE
16936 slot = (struct dwarf2_per_cu_offset_and_type **)
16937 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
16938 if (*slot)
16939 complaint (&symfile_complaints,
16940 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 16941 die->offset.sect_off);
673bfd45 16942 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16943 **slot = ofs;
f792889a 16944 return type;
1c379e20
DJ
16945}
16946
380bca97 16947/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 16948 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16949
16950static struct type *
b64f50a1 16951get_die_type_at_offset (sect_offset offset,
673bfd45 16952 struct dwarf2_per_cu_data *per_cu)
1c379e20 16953{
dee91e82 16954 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 16955
dee91e82 16956 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 16957 return NULL;
1c379e20 16958
dee91e82 16959 ofs.per_cu = per_cu;
673bfd45 16960 ofs.offset = offset;
dee91e82 16961 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
16962 if (slot)
16963 return slot->type;
16964 else
16965 return NULL;
16966}
16967
673bfd45
DE
16968/* Look up the type for DIE in the appropriate type_hash table,
16969 or return NULL if DIE does not have a saved type. */
16970
16971static struct type *
16972get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16973{
16974 return get_die_type_at_offset (die->offset, cu->per_cu);
16975}
16976
10b3939b
DJ
16977/* Add a dependence relationship from CU to REF_PER_CU. */
16978
16979static void
16980dwarf2_add_dependence (struct dwarf2_cu *cu,
16981 struct dwarf2_per_cu_data *ref_per_cu)
16982{
16983 void **slot;
16984
16985 if (cu->dependencies == NULL)
16986 cu->dependencies
16987 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16988 NULL, &cu->comp_unit_obstack,
16989 hashtab_obstack_allocate,
16990 dummy_obstack_deallocate);
16991
16992 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16993 if (*slot == NULL)
16994 *slot = ref_per_cu;
16995}
1c379e20 16996
f504f079
DE
16997/* Subroutine of dwarf2_mark to pass to htab_traverse.
16998 Set the mark field in every compilation unit in the
ae038cb0
DJ
16999 cache that we must keep because we are keeping CU. */
17000
10b3939b
DJ
17001static int
17002dwarf2_mark_helper (void **slot, void *data)
17003{
17004 struct dwarf2_per_cu_data *per_cu;
17005
17006 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
17007
17008 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17009 reading of the chain. As such dependencies remain valid it is not much
17010 useful to track and undo them during QUIT cleanups. */
17011 if (per_cu->cu == NULL)
17012 return 1;
17013
10b3939b
DJ
17014 if (per_cu->cu->mark)
17015 return 1;
17016 per_cu->cu->mark = 1;
17017
17018 if (per_cu->cu->dependencies != NULL)
17019 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17020
17021 return 1;
17022}
17023
f504f079
DE
17024/* Set the mark field in CU and in every other compilation unit in the
17025 cache that we must keep because we are keeping CU. */
17026
ae038cb0
DJ
17027static void
17028dwarf2_mark (struct dwarf2_cu *cu)
17029{
17030 if (cu->mark)
17031 return;
17032 cu->mark = 1;
10b3939b
DJ
17033 if (cu->dependencies != NULL)
17034 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
17035}
17036
17037static void
17038dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17039{
17040 while (per_cu)
17041 {
17042 per_cu->cu->mark = 0;
17043 per_cu = per_cu->cu->read_in_chain;
17044 }
72bf9492
DJ
17045}
17046
72bf9492
DJ
17047/* Trivial hash function for partial_die_info: the hash value of a DIE
17048 is its offset in .debug_info for this objfile. */
17049
17050static hashval_t
17051partial_die_hash (const void *item)
17052{
17053 const struct partial_die_info *part_die = item;
9a619af0 17054
b64f50a1 17055 return part_die->offset.sect_off;
72bf9492
DJ
17056}
17057
17058/* Trivial comparison function for partial_die_info structures: two DIEs
17059 are equal if they have the same offset. */
17060
17061static int
17062partial_die_eq (const void *item_lhs, const void *item_rhs)
17063{
17064 const struct partial_die_info *part_die_lhs = item_lhs;
17065 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 17066
b64f50a1 17067 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
17068}
17069
ae038cb0
DJ
17070static struct cmd_list_element *set_dwarf2_cmdlist;
17071static struct cmd_list_element *show_dwarf2_cmdlist;
17072
17073static void
17074set_dwarf2_cmd (char *args, int from_tty)
17075{
17076 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17077}
17078
17079static void
17080show_dwarf2_cmd (char *args, int from_tty)
6e70227d 17081{
ae038cb0
DJ
17082 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17083}
17084
dce234bc
PP
17085/* If section described by INFO was mmapped, munmap it now. */
17086
17087static void
17088munmap_section_buffer (struct dwarf2_section_info *info)
17089{
b315ab21 17090 if (info->map_addr != NULL)
dce234bc
PP
17091 {
17092#ifdef HAVE_MMAP
b315ab21 17093 int res;
9a619af0 17094
b315ab21
TG
17095 res = munmap (info->map_addr, info->map_len);
17096 gdb_assert (res == 0);
dce234bc
PP
17097#else
17098 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 17099 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
17100#endif
17101 }
17102}
17103
17104/* munmap debug sections for OBJFILE, if necessary. */
17105
17106static void
c1bd65d0 17107dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
17108{
17109 struct dwarf2_per_objfile *data = d;
8b70b953
TT
17110 int ix;
17111 struct dwarf2_section_info *section;
9a619af0 17112
16be1145
DE
17113 /* This is sorted according to the order they're defined in to make it easier
17114 to keep in sync. */
dce234bc
PP
17115 munmap_section_buffer (&data->info);
17116 munmap_section_buffer (&data->abbrev);
17117 munmap_section_buffer (&data->line);
16be1145 17118 munmap_section_buffer (&data->loc);
dce234bc 17119 munmap_section_buffer (&data->macinfo);
cf2c3c16 17120 munmap_section_buffer (&data->macro);
16be1145 17121 munmap_section_buffer (&data->str);
dce234bc 17122 munmap_section_buffer (&data->ranges);
3019eac3 17123 munmap_section_buffer (&data->addr);
dce234bc
PP
17124 munmap_section_buffer (&data->frame);
17125 munmap_section_buffer (&data->eh_frame);
9291a0cd 17126 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
17127
17128 for (ix = 0;
17129 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17130 ++ix)
17131 munmap_section_buffer (section);
17132
95554aad
TT
17133 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17134 VEC_free (dwarf2_per_cu_ptr,
17135 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17136
8b70b953 17137 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
17138
17139 if (data->dwo_files)
17140 free_dwo_files (data->dwo_files, objfile);
9291a0cd
TT
17141}
17142
17143\f
ae2de4f8 17144/* The "save gdb-index" command. */
9291a0cd
TT
17145
17146/* The contents of the hash table we create when building the string
17147 table. */
17148struct strtab_entry
17149{
17150 offset_type offset;
17151 const char *str;
17152};
17153
559a7a62
JK
17154/* Hash function for a strtab_entry.
17155
17156 Function is used only during write_hash_table so no index format backward
17157 compatibility is needed. */
b89be57b 17158
9291a0cd
TT
17159static hashval_t
17160hash_strtab_entry (const void *e)
17161{
17162 const struct strtab_entry *entry = e;
559a7a62 17163 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
17164}
17165
17166/* Equality function for a strtab_entry. */
b89be57b 17167
9291a0cd
TT
17168static int
17169eq_strtab_entry (const void *a, const void *b)
17170{
17171 const struct strtab_entry *ea = a;
17172 const struct strtab_entry *eb = b;
17173 return !strcmp (ea->str, eb->str);
17174}
17175
17176/* Create a strtab_entry hash table. */
b89be57b 17177
9291a0cd
TT
17178static htab_t
17179create_strtab (void)
17180{
17181 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17182 xfree, xcalloc, xfree);
17183}
17184
17185/* Add a string to the constant pool. Return the string's offset in
17186 host order. */
b89be57b 17187
9291a0cd
TT
17188static offset_type
17189add_string (htab_t table, struct obstack *cpool, const char *str)
17190{
17191 void **slot;
17192 struct strtab_entry entry;
17193 struct strtab_entry *result;
17194
17195 entry.str = str;
17196 slot = htab_find_slot (table, &entry, INSERT);
17197 if (*slot)
17198 result = *slot;
17199 else
17200 {
17201 result = XNEW (struct strtab_entry);
17202 result->offset = obstack_object_size (cpool);
17203 result->str = str;
17204 obstack_grow_str0 (cpool, str);
17205 *slot = result;
17206 }
17207 return result->offset;
17208}
17209
17210/* An entry in the symbol table. */
17211struct symtab_index_entry
17212{
17213 /* The name of the symbol. */
17214 const char *name;
17215 /* The offset of the name in the constant pool. */
17216 offset_type index_offset;
17217 /* A sorted vector of the indices of all the CUs that hold an object
17218 of this name. */
17219 VEC (offset_type) *cu_indices;
17220};
17221
17222/* The symbol table. This is a power-of-2-sized hash table. */
17223struct mapped_symtab
17224{
17225 offset_type n_elements;
17226 offset_type size;
17227 struct symtab_index_entry **data;
17228};
17229
17230/* Hash function for a symtab_index_entry. */
b89be57b 17231
9291a0cd
TT
17232static hashval_t
17233hash_symtab_entry (const void *e)
17234{
17235 const struct symtab_index_entry *entry = e;
17236 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17237 sizeof (offset_type) * VEC_length (offset_type,
17238 entry->cu_indices),
17239 0);
17240}
17241
17242/* Equality function for a symtab_index_entry. */
b89be57b 17243
9291a0cd
TT
17244static int
17245eq_symtab_entry (const void *a, const void *b)
17246{
17247 const struct symtab_index_entry *ea = a;
17248 const struct symtab_index_entry *eb = b;
17249 int len = VEC_length (offset_type, ea->cu_indices);
17250 if (len != VEC_length (offset_type, eb->cu_indices))
17251 return 0;
17252 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17253 VEC_address (offset_type, eb->cu_indices),
17254 sizeof (offset_type) * len);
17255}
17256
17257/* Destroy a symtab_index_entry. */
b89be57b 17258
9291a0cd
TT
17259static void
17260delete_symtab_entry (void *p)
17261{
17262 struct symtab_index_entry *entry = p;
17263 VEC_free (offset_type, entry->cu_indices);
17264 xfree (entry);
17265}
17266
17267/* Create a hash table holding symtab_index_entry objects. */
b89be57b 17268
9291a0cd 17269static htab_t
3876f04e 17270create_symbol_hash_table (void)
9291a0cd
TT
17271{
17272 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17273 delete_symtab_entry, xcalloc, xfree);
17274}
17275
17276/* Create a new mapped symtab object. */
b89be57b 17277
9291a0cd
TT
17278static struct mapped_symtab *
17279create_mapped_symtab (void)
17280{
17281 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17282 symtab->n_elements = 0;
17283 symtab->size = 1024;
17284 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17285 return symtab;
17286}
17287
17288/* Destroy a mapped_symtab. */
b89be57b 17289
9291a0cd
TT
17290static void
17291cleanup_mapped_symtab (void *p)
17292{
17293 struct mapped_symtab *symtab = p;
17294 /* The contents of the array are freed when the other hash table is
17295 destroyed. */
17296 xfree (symtab->data);
17297 xfree (symtab);
17298}
17299
17300/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
17301 the slot.
17302
17303 Function is used only during write_hash_table so no index format backward
17304 compatibility is needed. */
b89be57b 17305
9291a0cd
TT
17306static struct symtab_index_entry **
17307find_slot (struct mapped_symtab *symtab, const char *name)
17308{
559a7a62 17309 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
17310
17311 index = hash & (symtab->size - 1);
17312 step = ((hash * 17) & (symtab->size - 1)) | 1;
17313
17314 for (;;)
17315 {
17316 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17317 return &symtab->data[index];
17318 index = (index + step) & (symtab->size - 1);
17319 }
17320}
17321
17322/* Expand SYMTAB's hash table. */
b89be57b 17323
9291a0cd
TT
17324static void
17325hash_expand (struct mapped_symtab *symtab)
17326{
17327 offset_type old_size = symtab->size;
17328 offset_type i;
17329 struct symtab_index_entry **old_entries = symtab->data;
17330
17331 symtab->size *= 2;
17332 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17333
17334 for (i = 0; i < old_size; ++i)
17335 {
17336 if (old_entries[i])
17337 {
17338 struct symtab_index_entry **slot = find_slot (symtab,
17339 old_entries[i]->name);
17340 *slot = old_entries[i];
17341 }
17342 }
17343
17344 xfree (old_entries);
17345}
17346
17347/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
17348 is the index of the CU in which the symbol appears. */
b89be57b 17349
9291a0cd
TT
17350static void
17351add_index_entry (struct mapped_symtab *symtab, const char *name,
17352 offset_type cu_index)
17353{
17354 struct symtab_index_entry **slot;
17355
17356 ++symtab->n_elements;
17357 if (4 * symtab->n_elements / 3 >= symtab->size)
17358 hash_expand (symtab);
17359
17360 slot = find_slot (symtab, name);
17361 if (!*slot)
17362 {
17363 *slot = XNEW (struct symtab_index_entry);
17364 (*slot)->name = name;
17365 (*slot)->cu_indices = NULL;
17366 }
17367 /* Don't push an index twice. Due to how we add entries we only
17368 have to check the last one. */
17369 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 17370 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
17371 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
17372}
17373
17374/* Add a vector of indices to the constant pool. */
b89be57b 17375
9291a0cd 17376static offset_type
3876f04e 17377add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
17378 struct symtab_index_entry *entry)
17379{
17380 void **slot;
17381
3876f04e 17382 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
17383 if (!*slot)
17384 {
17385 offset_type len = VEC_length (offset_type, entry->cu_indices);
17386 offset_type val = MAYBE_SWAP (len);
17387 offset_type iter;
17388 int i;
17389
17390 *slot = entry;
17391 entry->index_offset = obstack_object_size (cpool);
17392
17393 obstack_grow (cpool, &val, sizeof (val));
17394 for (i = 0;
17395 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17396 ++i)
17397 {
17398 val = MAYBE_SWAP (iter);
17399 obstack_grow (cpool, &val, sizeof (val));
17400 }
17401 }
17402 else
17403 {
17404 struct symtab_index_entry *old_entry = *slot;
17405 entry->index_offset = old_entry->index_offset;
17406 entry = old_entry;
17407 }
17408 return entry->index_offset;
17409}
17410
17411/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17412 constant pool entries going into the obstack CPOOL. */
b89be57b 17413
9291a0cd
TT
17414static void
17415write_hash_table (struct mapped_symtab *symtab,
17416 struct obstack *output, struct obstack *cpool)
17417{
17418 offset_type i;
3876f04e 17419 htab_t symbol_hash_table;
9291a0cd
TT
17420 htab_t str_table;
17421
3876f04e 17422 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 17423 str_table = create_strtab ();
3876f04e 17424
9291a0cd
TT
17425 /* We add all the index vectors to the constant pool first, to
17426 ensure alignment is ok. */
17427 for (i = 0; i < symtab->size; ++i)
17428 {
17429 if (symtab->data[i])
3876f04e 17430 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
17431 }
17432
17433 /* Now write out the hash table. */
17434 for (i = 0; i < symtab->size; ++i)
17435 {
17436 offset_type str_off, vec_off;
17437
17438 if (symtab->data[i])
17439 {
17440 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17441 vec_off = symtab->data[i]->index_offset;
17442 }
17443 else
17444 {
17445 /* While 0 is a valid constant pool index, it is not valid
17446 to have 0 for both offsets. */
17447 str_off = 0;
17448 vec_off = 0;
17449 }
17450
17451 str_off = MAYBE_SWAP (str_off);
17452 vec_off = MAYBE_SWAP (vec_off);
17453
17454 obstack_grow (output, &str_off, sizeof (str_off));
17455 obstack_grow (output, &vec_off, sizeof (vec_off));
17456 }
17457
17458 htab_delete (str_table);
3876f04e 17459 htab_delete (symbol_hash_table);
9291a0cd
TT
17460}
17461
0a5429f6
DE
17462/* Struct to map psymtab to CU index in the index file. */
17463struct psymtab_cu_index_map
17464{
17465 struct partial_symtab *psymtab;
17466 unsigned int cu_index;
17467};
17468
17469static hashval_t
17470hash_psymtab_cu_index (const void *item)
17471{
17472 const struct psymtab_cu_index_map *map = item;
17473
17474 return htab_hash_pointer (map->psymtab);
17475}
17476
17477static int
17478eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17479{
17480 const struct psymtab_cu_index_map *lhs = item_lhs;
17481 const struct psymtab_cu_index_map *rhs = item_rhs;
17482
17483 return lhs->psymtab == rhs->psymtab;
17484}
17485
17486/* Helper struct for building the address table. */
17487struct addrmap_index_data
17488{
17489 struct objfile *objfile;
17490 struct obstack *addr_obstack;
17491 htab_t cu_index_htab;
17492
17493 /* Non-zero if the previous_* fields are valid.
17494 We can't write an entry until we see the next entry (since it is only then
17495 that we know the end of the entry). */
17496 int previous_valid;
17497 /* Index of the CU in the table of all CUs in the index file. */
17498 unsigned int previous_cu_index;
0963b4bd 17499 /* Start address of the CU. */
0a5429f6
DE
17500 CORE_ADDR previous_cu_start;
17501};
17502
17503/* Write an address entry to OBSTACK. */
b89be57b 17504
9291a0cd 17505static void
0a5429f6
DE
17506add_address_entry (struct objfile *objfile, struct obstack *obstack,
17507 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 17508{
0a5429f6 17509 offset_type cu_index_to_write;
9291a0cd
TT
17510 char addr[8];
17511 CORE_ADDR baseaddr;
17512
17513 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17514
0a5429f6
DE
17515 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17516 obstack_grow (obstack, addr, 8);
17517 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17518 obstack_grow (obstack, addr, 8);
17519 cu_index_to_write = MAYBE_SWAP (cu_index);
17520 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17521}
17522
17523/* Worker function for traversing an addrmap to build the address table. */
17524
17525static int
17526add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17527{
17528 struct addrmap_index_data *data = datap;
17529 struct partial_symtab *pst = obj;
17530 offset_type cu_index;
17531 void **slot;
17532
17533 if (data->previous_valid)
17534 add_address_entry (data->objfile, data->addr_obstack,
17535 data->previous_cu_start, start_addr,
17536 data->previous_cu_index);
17537
17538 data->previous_cu_start = start_addr;
17539 if (pst != NULL)
17540 {
17541 struct psymtab_cu_index_map find_map, *map;
17542 find_map.psymtab = pst;
17543 map = htab_find (data->cu_index_htab, &find_map);
17544 gdb_assert (map != NULL);
17545 data->previous_cu_index = map->cu_index;
17546 data->previous_valid = 1;
17547 }
17548 else
17549 data->previous_valid = 0;
17550
17551 return 0;
17552}
17553
17554/* Write OBJFILE's address map to OBSTACK.
17555 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17556 in the index file. */
17557
17558static void
17559write_address_map (struct objfile *objfile, struct obstack *obstack,
17560 htab_t cu_index_htab)
17561{
17562 struct addrmap_index_data addrmap_index_data;
17563
17564 /* When writing the address table, we have to cope with the fact that
17565 the addrmap iterator only provides the start of a region; we have to
17566 wait until the next invocation to get the start of the next region. */
17567
17568 addrmap_index_data.objfile = objfile;
17569 addrmap_index_data.addr_obstack = obstack;
17570 addrmap_index_data.cu_index_htab = cu_index_htab;
17571 addrmap_index_data.previous_valid = 0;
17572
17573 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17574 &addrmap_index_data);
17575
17576 /* It's highly unlikely the last entry (end address = 0xff...ff)
17577 is valid, but we should still handle it.
17578 The end address is recorded as the start of the next region, but that
17579 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17580 anyway. */
17581 if (addrmap_index_data.previous_valid)
17582 add_address_entry (objfile, obstack,
17583 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17584 addrmap_index_data.previous_cu_index);
9291a0cd
TT
17585}
17586
17587/* Add a list of partial symbols to SYMTAB. */
b89be57b 17588
9291a0cd
TT
17589static void
17590write_psymbols (struct mapped_symtab *symtab,
987d643c 17591 htab_t psyms_seen,
9291a0cd
TT
17592 struct partial_symbol **psymp,
17593 int count,
987d643c
TT
17594 offset_type cu_index,
17595 int is_static)
9291a0cd
TT
17596{
17597 for (; count-- > 0; ++psymp)
17598 {
987d643c
TT
17599 void **slot, *lookup;
17600
9291a0cd
TT
17601 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
17602 error (_("Ada is not currently supported by the index"));
987d643c
TT
17603
17604 /* We only want to add a given psymbol once. However, we also
17605 want to account for whether it is global or static. So, we
17606 may add it twice, using slightly different values. */
17607 if (is_static)
17608 {
17609 uintptr_t val = 1 | (uintptr_t) *psymp;
17610
17611 lookup = (void *) val;
17612 }
17613 else
17614 lookup = *psymp;
17615
17616 /* Only add a given psymbol once. */
17617 slot = htab_find_slot (psyms_seen, lookup, INSERT);
17618 if (!*slot)
17619 {
17620 *slot = lookup;
bb2f58dc 17621 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 17622 }
9291a0cd
TT
17623 }
17624}
17625
17626/* Write the contents of an ("unfinished") obstack to FILE. Throw an
17627 exception if there is an error. */
b89be57b 17628
9291a0cd
TT
17629static void
17630write_obstack (FILE *file, struct obstack *obstack)
17631{
17632 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17633 file)
17634 != obstack_object_size (obstack))
17635 error (_("couldn't data write to file"));
17636}
17637
17638/* Unlink a file if the argument is not NULL. */
b89be57b 17639
9291a0cd
TT
17640static void
17641unlink_if_set (void *p)
17642{
17643 char **filename = p;
17644 if (*filename)
17645 unlink (*filename);
17646}
17647
1fd400ff
TT
17648/* A helper struct used when iterating over debug_types. */
17649struct signatured_type_index_data
17650{
17651 struct objfile *objfile;
17652 struct mapped_symtab *symtab;
17653 struct obstack *types_list;
987d643c 17654 htab_t psyms_seen;
1fd400ff
TT
17655 int cu_index;
17656};
17657
17658/* A helper function that writes a single signatured_type to an
17659 obstack. */
b89be57b 17660
1fd400ff
TT
17661static int
17662write_one_signatured_type (void **slot, void *d)
17663{
17664 struct signatured_type_index_data *info = d;
17665 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
17666 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17667 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
17668 gdb_byte val[8];
17669
17670 write_psymbols (info->symtab,
987d643c 17671 info->psyms_seen,
3e43a32a
MS
17672 info->objfile->global_psymbols.list
17673 + psymtab->globals_offset,
987d643c
TT
17674 psymtab->n_global_syms, info->cu_index,
17675 0);
1fd400ff 17676 write_psymbols (info->symtab,
987d643c 17677 info->psyms_seen,
3e43a32a
MS
17678 info->objfile->static_psymbols.list
17679 + psymtab->statics_offset,
987d643c
TT
17680 psymtab->n_static_syms, info->cu_index,
17681 1);
1fd400ff 17682
b64f50a1
JK
17683 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17684 entry->per_cu.offset.sect_off);
1fd400ff 17685 obstack_grow (info->types_list, val, 8);
3019eac3
DE
17686 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17687 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
17688 obstack_grow (info->types_list, val, 8);
17689 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17690 obstack_grow (info->types_list, val, 8);
17691
17692 ++info->cu_index;
17693
17694 return 1;
17695}
17696
95554aad
TT
17697/* Recurse into all "included" dependencies and write their symbols as
17698 if they appeared in this psymtab. */
17699
17700static void
17701recursively_write_psymbols (struct objfile *objfile,
17702 struct partial_symtab *psymtab,
17703 struct mapped_symtab *symtab,
17704 htab_t psyms_seen,
17705 offset_type cu_index)
17706{
17707 int i;
17708
17709 for (i = 0; i < psymtab->number_of_dependencies; ++i)
17710 if (psymtab->dependencies[i]->user != NULL)
17711 recursively_write_psymbols (objfile, psymtab->dependencies[i],
17712 symtab, psyms_seen, cu_index);
17713
17714 write_psymbols (symtab,
17715 psyms_seen,
17716 objfile->global_psymbols.list + psymtab->globals_offset,
17717 psymtab->n_global_syms, cu_index,
17718 0);
17719 write_psymbols (symtab,
17720 psyms_seen,
17721 objfile->static_psymbols.list + psymtab->statics_offset,
17722 psymtab->n_static_syms, cu_index,
17723 1);
17724}
17725
9291a0cd 17726/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 17727
9291a0cd
TT
17728static void
17729write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17730{
17731 struct cleanup *cleanup;
17732 char *filename, *cleanup_filename;
1fd400ff
TT
17733 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
17734 struct obstack cu_list, types_cu_list;
9291a0cd
TT
17735 int i;
17736 FILE *out_file;
17737 struct mapped_symtab *symtab;
17738 offset_type val, size_of_contents, total_len;
17739 struct stat st;
17740 char buf[8];
987d643c 17741 htab_t psyms_seen;
0a5429f6
DE
17742 htab_t cu_index_htab;
17743 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 17744
b4f2f049 17745 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 17746 return;
b4f2f049 17747
9291a0cd
TT
17748 if (dwarf2_per_objfile->using_index)
17749 error (_("Cannot use an index to create the index"));
17750
8b70b953
TT
17751 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17752 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17753
9291a0cd 17754 if (stat (objfile->name, &st) < 0)
7e17e088 17755 perror_with_name (objfile->name);
9291a0cd
TT
17756
17757 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17758 INDEX_SUFFIX, (char *) NULL);
17759 cleanup = make_cleanup (xfree, filename);
17760
17761 out_file = fopen (filename, "wb");
17762 if (!out_file)
17763 error (_("Can't open `%s' for writing"), filename);
17764
17765 cleanup_filename = filename;
17766 make_cleanup (unlink_if_set, &cleanup_filename);
17767
17768 symtab = create_mapped_symtab ();
17769 make_cleanup (cleanup_mapped_symtab, symtab);
17770
17771 obstack_init (&addr_obstack);
17772 make_cleanup_obstack_free (&addr_obstack);
17773
17774 obstack_init (&cu_list);
17775 make_cleanup_obstack_free (&cu_list);
17776
1fd400ff
TT
17777 obstack_init (&types_cu_list);
17778 make_cleanup_obstack_free (&types_cu_list);
17779
987d643c
TT
17780 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17781 NULL, xcalloc, xfree);
96408a79 17782 make_cleanup_htab_delete (psyms_seen);
987d643c 17783
0a5429f6
DE
17784 /* While we're scanning CU's create a table that maps a psymtab pointer
17785 (which is what addrmap records) to its index (which is what is recorded
17786 in the index file). This will later be needed to write the address
17787 table. */
17788 cu_index_htab = htab_create_alloc (100,
17789 hash_psymtab_cu_index,
17790 eq_psymtab_cu_index,
17791 NULL, xcalloc, xfree);
96408a79 17792 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
17793 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17794 xmalloc (sizeof (struct psymtab_cu_index_map)
17795 * dwarf2_per_objfile->n_comp_units);
17796 make_cleanup (xfree, psymtab_cu_index_map);
17797
17798 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
17799 work here. Also, the debug_types entries do not appear in
17800 all_comp_units, but only in their own hash table. */
9291a0cd
TT
17801 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17802 {
3e43a32a
MS
17803 struct dwarf2_per_cu_data *per_cu
17804 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 17805 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 17806 gdb_byte val[8];
0a5429f6
DE
17807 struct psymtab_cu_index_map *map;
17808 void **slot;
9291a0cd 17809
95554aad
TT
17810 if (psymtab->user == NULL)
17811 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 17812
0a5429f6
DE
17813 map = &psymtab_cu_index_map[i];
17814 map->psymtab = psymtab;
17815 map->cu_index = i;
17816 slot = htab_find_slot (cu_index_htab, map, INSERT);
17817 gdb_assert (slot != NULL);
17818 gdb_assert (*slot == NULL);
17819 *slot = map;
9291a0cd 17820
b64f50a1
JK
17821 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17822 per_cu->offset.sect_off);
9291a0cd 17823 obstack_grow (&cu_list, val, 8);
e254ef6a 17824 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
17825 obstack_grow (&cu_list, val, 8);
17826 }
17827
0a5429f6
DE
17828 /* Dump the address map. */
17829 write_address_map (objfile, &addr_obstack, cu_index_htab);
17830
1fd400ff
TT
17831 /* Write out the .debug_type entries, if any. */
17832 if (dwarf2_per_objfile->signatured_types)
17833 {
17834 struct signatured_type_index_data sig_data;
17835
17836 sig_data.objfile = objfile;
17837 sig_data.symtab = symtab;
17838 sig_data.types_list = &types_cu_list;
987d643c 17839 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17840 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17841 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17842 write_one_signatured_type, &sig_data);
17843 }
17844
9291a0cd
TT
17845 obstack_init (&constant_pool);
17846 make_cleanup_obstack_free (&constant_pool);
17847 obstack_init (&symtab_obstack);
17848 make_cleanup_obstack_free (&symtab_obstack);
17849 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17850
17851 obstack_init (&contents);
17852 make_cleanup_obstack_free (&contents);
1fd400ff 17853 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17854 total_len = size_of_contents;
17855
17856 /* The version number. */
481860b3 17857 val = MAYBE_SWAP (6);
9291a0cd
TT
17858 obstack_grow (&contents, &val, sizeof (val));
17859
17860 /* The offset of the CU list from the start of the file. */
17861 val = MAYBE_SWAP (total_len);
17862 obstack_grow (&contents, &val, sizeof (val));
17863 total_len += obstack_object_size (&cu_list);
17864
1fd400ff
TT
17865 /* The offset of the types CU list from the start of the file. */
17866 val = MAYBE_SWAP (total_len);
17867 obstack_grow (&contents, &val, sizeof (val));
17868 total_len += obstack_object_size (&types_cu_list);
17869
9291a0cd
TT
17870 /* The offset of the address table from the start of the file. */
17871 val = MAYBE_SWAP (total_len);
17872 obstack_grow (&contents, &val, sizeof (val));
17873 total_len += obstack_object_size (&addr_obstack);
17874
17875 /* The offset of the symbol table from the start of the file. */
17876 val = MAYBE_SWAP (total_len);
17877 obstack_grow (&contents, &val, sizeof (val));
17878 total_len += obstack_object_size (&symtab_obstack);
17879
17880 /* The offset of the constant pool from the start of the file. */
17881 val = MAYBE_SWAP (total_len);
17882 obstack_grow (&contents, &val, sizeof (val));
17883 total_len += obstack_object_size (&constant_pool);
17884
17885 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17886
17887 write_obstack (out_file, &contents);
17888 write_obstack (out_file, &cu_list);
1fd400ff 17889 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17890 write_obstack (out_file, &addr_obstack);
17891 write_obstack (out_file, &symtab_obstack);
17892 write_obstack (out_file, &constant_pool);
17893
17894 fclose (out_file);
17895
17896 /* We want to keep the file, so we set cleanup_filename to NULL
17897 here. See unlink_if_set. */
17898 cleanup_filename = NULL;
17899
17900 do_cleanups (cleanup);
17901}
17902
90476074
TT
17903/* Implementation of the `save gdb-index' command.
17904
17905 Note that the file format used by this command is documented in the
17906 GDB manual. Any changes here must be documented there. */
11570e71 17907
9291a0cd
TT
17908static void
17909save_gdb_index_command (char *arg, int from_tty)
17910{
17911 struct objfile *objfile;
17912
17913 if (!arg || !*arg)
96d19272 17914 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17915
17916 ALL_OBJFILES (objfile)
17917 {
17918 struct stat st;
17919
17920 /* If the objfile does not correspond to an actual file, skip it. */
17921 if (stat (objfile->name, &st) < 0)
17922 continue;
17923
17924 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17925 if (dwarf2_per_objfile)
17926 {
17927 volatile struct gdb_exception except;
17928
17929 TRY_CATCH (except, RETURN_MASK_ERROR)
17930 {
17931 write_psymtabs_to_index (objfile, arg);
17932 }
17933 if (except.reason < 0)
17934 exception_fprintf (gdb_stderr, except,
17935 _("Error while writing index for `%s': "),
17936 objfile->name);
17937 }
17938 }
dce234bc
PP
17939}
17940
9291a0cd
TT
17941\f
17942
9eae7c52
TT
17943int dwarf2_always_disassemble;
17944
17945static void
17946show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17947 struct cmd_list_element *c, const char *value)
17948{
3e43a32a
MS
17949 fprintf_filtered (file,
17950 _("Whether to always disassemble "
17951 "DWARF expressions is %s.\n"),
9eae7c52
TT
17952 value);
17953}
17954
900e11f9
JK
17955static void
17956show_check_physname (struct ui_file *file, int from_tty,
17957 struct cmd_list_element *c, const char *value)
17958{
17959 fprintf_filtered (file,
17960 _("Whether to check \"physname\" is %s.\n"),
17961 value);
17962}
17963
6502dd73
DJ
17964void _initialize_dwarf2_read (void);
17965
17966void
17967_initialize_dwarf2_read (void)
17968{
96d19272
JK
17969 struct cmd_list_element *c;
17970
dce234bc 17971 dwarf2_objfile_data_key
c1bd65d0 17972 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17973
1bedd215
AC
17974 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17975Set DWARF 2 specific variables.\n\
17976Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17977 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17978 0/*allow-unknown*/, &maintenance_set_cmdlist);
17979
1bedd215
AC
17980 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17981Show DWARF 2 specific variables\n\
17982Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17983 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17984 0/*allow-unknown*/, &maintenance_show_cmdlist);
17985
17986 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17987 &dwarf2_max_cache_age, _("\
17988Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17989Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17990A higher limit means that cached compilation units will be stored\n\
17991in memory longer, and more total memory will be used. Zero disables\n\
17992caching, which can slow down startup."),
2c5b56ce 17993 NULL,
920d2a44 17994 show_dwarf2_max_cache_age,
2c5b56ce 17995 &set_dwarf2_cmdlist,
ae038cb0 17996 &show_dwarf2_cmdlist);
d97bc12b 17997
9eae7c52
TT
17998 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17999 &dwarf2_always_disassemble, _("\
18000Set whether `info address' always disassembles DWARF expressions."), _("\
18001Show whether `info address' always disassembles DWARF expressions."), _("\
18002When enabled, DWARF expressions are always printed in an assembly-like\n\
18003syntax. When disabled, expressions will be printed in a more\n\
18004conversational style, when possible."),
18005 NULL,
18006 show_dwarf2_always_disassemble,
18007 &set_dwarf2_cmdlist,
18008 &show_dwarf2_cmdlist);
18009
d97bc12b
DE
18010 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18011Set debugging of the dwarf2 DIE reader."), _("\
18012Show debugging of the dwarf2 DIE reader."), _("\
18013When enabled (non-zero), DIEs are dumped after they are read in.\n\
18014The value is the maximum depth to print."),
18015 NULL,
18016 NULL,
18017 &setdebuglist, &showdebuglist);
9291a0cd 18018
900e11f9
JK
18019 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18020Set cross-checking of \"physname\" code against demangler."), _("\
18021Show cross-checking of \"physname\" code against demangler."), _("\
18022When enabled, GDB's internal \"physname\" code is checked against\n\
18023the demangler."),
18024 NULL, show_check_physname,
18025 &setdebuglist, &showdebuglist);
18026
96d19272 18027 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 18028 _("\
fc1a9d6e 18029Save a gdb-index file.\n\
11570e71 18030Usage: save gdb-index DIRECTORY"),
96d19272
JK
18031 &save_cmdlist);
18032 set_cmd_completer (c, filename_completer);
6502dd73 18033}
This page took 2.726396 seconds and 4 git commands to generate.