* dwarf2-frame.c (struct dwarf2_cie): Make initial_instructions, end
[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;
f664829e
DE
609
610 /* The end of the buffer. */
611 const gdb_byte *buffer_end;
93311388
DE
612};
613
fd820528 614/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
615typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
616 gdb_byte *info_ptr,
617 struct die_info *comp_unit_die,
618 int has_children,
619 void *data);
620
debd256d
JB
621/* The line number information for a compilation unit (found in the
622 .debug_line section) begins with a "statement program header",
623 which contains the following information. */
624struct line_header
625{
626 unsigned int total_length;
627 unsigned short version;
628 unsigned int header_length;
629 unsigned char minimum_instruction_length;
2dc7f7b3 630 unsigned char maximum_ops_per_instruction;
debd256d
JB
631 unsigned char default_is_stmt;
632 int line_base;
633 unsigned char line_range;
634 unsigned char opcode_base;
635
636 /* standard_opcode_lengths[i] is the number of operands for the
637 standard opcode whose value is i. This means that
638 standard_opcode_lengths[0] is unused, and the last meaningful
639 element is standard_opcode_lengths[opcode_base - 1]. */
640 unsigned char *standard_opcode_lengths;
641
642 /* The include_directories table. NOTE! These strings are not
643 allocated with xmalloc; instead, they are pointers into
644 debug_line_buffer. If you try to free them, `free' will get
645 indigestion. */
646 unsigned int num_include_dirs, include_dirs_size;
647 char **include_dirs;
648
649 /* The file_names table. NOTE! These strings are not allocated
650 with xmalloc; instead, they are pointers into debug_line_buffer.
651 Don't try to free them directly. */
652 unsigned int num_file_names, file_names_size;
653 struct file_entry
c906108c 654 {
debd256d
JB
655 char *name;
656 unsigned int dir_index;
657 unsigned int mod_time;
658 unsigned int length;
aaa75496 659 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 660 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
661 } *file_names;
662
663 /* The start and end of the statement program following this
6502dd73 664 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 665 gdb_byte *statement_program_start, *statement_program_end;
debd256d 666};
c906108c
SS
667
668/* When we construct a partial symbol table entry we only
0963b4bd 669 need this much information. */
c906108c
SS
670struct partial_die_info
671 {
72bf9492 672 /* Offset of this DIE. */
b64f50a1 673 sect_offset offset;
72bf9492
DJ
674
675 /* DWARF-2 tag for this DIE. */
676 ENUM_BITFIELD(dwarf_tag) tag : 16;
677
72bf9492
DJ
678 /* Assorted flags describing the data found in this DIE. */
679 unsigned int has_children : 1;
680 unsigned int is_external : 1;
681 unsigned int is_declaration : 1;
682 unsigned int has_type : 1;
683 unsigned int has_specification : 1;
684 unsigned int has_pc_info : 1;
481860b3 685 unsigned int may_be_inlined : 1;
72bf9492
DJ
686
687 /* Flag set if the SCOPE field of this structure has been
688 computed. */
689 unsigned int scope_set : 1;
690
fa4028e9
JB
691 /* Flag set if the DIE has a byte_size attribute. */
692 unsigned int has_byte_size : 1;
693
98bfdba5
PA
694 /* Flag set if any of the DIE's children are template arguments. */
695 unsigned int has_template_arguments : 1;
696
abc72ce4
DE
697 /* Flag set if fixup_partial_die has been called on this die. */
698 unsigned int fixup_called : 1;
699
72bf9492 700 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 701 sometimes a default name for unnamed DIEs. */
c906108c 702 char *name;
72bf9492 703
abc72ce4
DE
704 /* The linkage name, if present. */
705 const char *linkage_name;
706
72bf9492
DJ
707 /* The scope to prepend to our children. This is generally
708 allocated on the comp_unit_obstack, so will disappear
709 when this compilation unit leaves the cache. */
710 char *scope;
711
95554aad
TT
712 /* Some data associated with the partial DIE. The tag determines
713 which field is live. */
714 union
715 {
716 /* The location description associated with this DIE, if any. */
717 struct dwarf_block *locdesc;
718 /* The offset of an import, for DW_TAG_imported_unit. */
719 sect_offset offset;
720 } d;
72bf9492
DJ
721
722 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
723 CORE_ADDR lowpc;
724 CORE_ADDR highpc;
72bf9492 725
93311388 726 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 727 DW_AT_sibling, if any. */
abc72ce4
DE
728 /* NOTE: This member isn't strictly necessary, read_partial_die could
729 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 730 gdb_byte *sibling;
72bf9492
DJ
731
732 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
733 DW_AT_specification (or DW_AT_abstract_origin or
734 DW_AT_extension). */
b64f50a1 735 sect_offset spec_offset;
72bf9492
DJ
736
737 /* Pointers to this DIE's parent, first child, and next sibling,
738 if any. */
739 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
740 };
741
0963b4bd 742/* This data structure holds the information of an abbrev. */
c906108c
SS
743struct abbrev_info
744 {
745 unsigned int number; /* number identifying abbrev */
746 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
747 unsigned short has_children; /* boolean */
748 unsigned short num_attrs; /* number of attributes */
c906108c
SS
749 struct attr_abbrev *attrs; /* an array of attribute descriptions */
750 struct abbrev_info *next; /* next in chain */
751 };
752
753struct attr_abbrev
754 {
9d25dd43
DE
755 ENUM_BITFIELD(dwarf_attribute) name : 16;
756 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
757 };
758
0963b4bd 759/* Attributes have a name and a value. */
b60c80d6
DJ
760struct attribute
761 {
9d25dd43 762 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
763 ENUM_BITFIELD(dwarf_form) form : 15;
764
765 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
766 field should be in u.str (existing only for DW_STRING) but it is kept
767 here for better struct attribute alignment. */
768 unsigned int string_is_canonical : 1;
769
b60c80d6
DJ
770 union
771 {
772 char *str;
773 struct dwarf_block *blk;
43bbcdc2
PH
774 ULONGEST unsnd;
775 LONGEST snd;
b60c80d6 776 CORE_ADDR addr;
348e048f 777 struct signatured_type *signatured_type;
b60c80d6
DJ
778 }
779 u;
780 };
781
0963b4bd 782/* This data structure holds a complete die structure. */
c906108c
SS
783struct die_info
784 {
76815b17
DE
785 /* DWARF-2 tag for this DIE. */
786 ENUM_BITFIELD(dwarf_tag) tag : 16;
787
788 /* Number of attributes */
98bfdba5
PA
789 unsigned char num_attrs;
790
791 /* True if we're presently building the full type name for the
792 type derived from this DIE. */
793 unsigned char building_fullname : 1;
76815b17
DE
794
795 /* Abbrev number */
796 unsigned int abbrev;
797
93311388 798 /* Offset in .debug_info or .debug_types section. */
b64f50a1 799 sect_offset offset;
78ba4af6
JB
800
801 /* The dies in a compilation unit form an n-ary tree. PARENT
802 points to this die's parent; CHILD points to the first child of
803 this node; and all the children of a given node are chained
4950bc1c 804 together via their SIBLING fields. */
639d11d3
DC
805 struct die_info *child; /* Its first child, if any. */
806 struct die_info *sibling; /* Its next sibling, if any. */
807 struct die_info *parent; /* Its parent, if any. */
c906108c 808
b60c80d6
DJ
809 /* An array of attributes, with NUM_ATTRS elements. There may be
810 zero, but it's not common and zero-sized arrays are not
811 sufficiently portable C. */
812 struct attribute attrs[1];
c906108c
SS
813 };
814
0963b4bd 815/* Get at parts of an attribute structure. */
c906108c
SS
816
817#define DW_STRING(attr) ((attr)->u.str)
8285870a 818#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
819#define DW_UNSND(attr) ((attr)->u.unsnd)
820#define DW_BLOCK(attr) ((attr)->u.blk)
821#define DW_SND(attr) ((attr)->u.snd)
822#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 823#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 824
0963b4bd 825/* Blocks are a bunch of untyped bytes. */
c906108c
SS
826struct dwarf_block
827 {
828 unsigned int size;
1d6edc3c
JK
829
830 /* Valid only if SIZE is not zero. */
fe1b8b76 831 gdb_byte *data;
c906108c
SS
832 };
833
c906108c
SS
834#ifndef ATTR_ALLOC_CHUNK
835#define ATTR_ALLOC_CHUNK 4
836#endif
837
c906108c
SS
838/* Allocate fields for structs, unions and enums in this size. */
839#ifndef DW_FIELD_ALLOC_CHUNK
840#define DW_FIELD_ALLOC_CHUNK 4
841#endif
842
c906108c
SS
843/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
844 but this would require a corresponding change in unpack_field_as_long
845 and friends. */
846static int bits_per_byte = 8;
847
848/* The routines that read and process dies for a C struct or C++ class
849 pass lists of data member fields and lists of member function fields
850 in an instance of a field_info structure, as defined below. */
851struct field_info
c5aa993b 852 {
0963b4bd 853 /* List of data member and baseclasses fields. */
c5aa993b
JM
854 struct nextfield
855 {
856 struct nextfield *next;
857 int accessibility;
858 int virtuality;
859 struct field field;
860 }
7d0ccb61 861 *fields, *baseclasses;
c906108c 862
7d0ccb61 863 /* Number of fields (including baseclasses). */
c5aa993b 864 int nfields;
c906108c 865
c5aa993b
JM
866 /* Number of baseclasses. */
867 int nbaseclasses;
c906108c 868
c5aa993b
JM
869 /* Set if the accesibility of one of the fields is not public. */
870 int non_public_fields;
c906108c 871
c5aa993b
JM
872 /* Member function fields array, entries are allocated in the order they
873 are encountered in the object file. */
874 struct nextfnfield
875 {
876 struct nextfnfield *next;
877 struct fn_field fnfield;
878 }
879 *fnfields;
c906108c 880
c5aa993b
JM
881 /* Member function fieldlist array, contains name of possibly overloaded
882 member function, number of overloaded member functions and a pointer
883 to the head of the member function field chain. */
884 struct fnfieldlist
885 {
886 char *name;
887 int length;
888 struct nextfnfield *head;
889 }
890 *fnfieldlists;
c906108c 891
c5aa993b
JM
892 /* Number of entries in the fnfieldlists array. */
893 int nfnfields;
98751a41
JK
894
895 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
896 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
897 struct typedef_field_list
898 {
899 struct typedef_field field;
900 struct typedef_field_list *next;
901 }
902 *typedef_field_list;
903 unsigned typedef_field_list_count;
c5aa993b 904 };
c906108c 905
10b3939b
DJ
906/* One item on the queue of compilation units to read in full symbols
907 for. */
908struct dwarf2_queue_item
909{
910 struct dwarf2_per_cu_data *per_cu;
95554aad 911 enum language pretend_language;
10b3939b
DJ
912 struct dwarf2_queue_item *next;
913};
914
915/* The current queue. */
916static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
917
ae038cb0
DJ
918/* Loaded secondary compilation units are kept in memory until they
919 have not been referenced for the processing of this many
920 compilation units. Set this to zero to disable caching. Cache
921 sizes of up to at least twenty will improve startup time for
922 typical inter-CU-reference binaries, at an obvious memory cost. */
923static int dwarf2_max_cache_age = 5;
920d2a44
AC
924static void
925show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
926 struct cmd_list_element *c, const char *value)
927{
3e43a32a
MS
928 fprintf_filtered (file, _("The upper bound on the age of cached "
929 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
930 value);
931}
932
ae038cb0 933
0963b4bd 934/* Various complaints about symbol reading that don't abort the process. */
c906108c 935
4d3c2250
KB
936static void
937dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 938{
4d3c2250 939 complaint (&symfile_complaints,
e2e0b3e5 940 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
941}
942
25e43795
DJ
943static void
944dwarf2_debug_line_missing_file_complaint (void)
945{
946 complaint (&symfile_complaints,
947 _(".debug_line section has line data without a file"));
948}
949
59205f5a
JB
950static void
951dwarf2_debug_line_missing_end_sequence_complaint (void)
952{
953 complaint (&symfile_complaints,
3e43a32a
MS
954 _(".debug_line section has line "
955 "program sequence without an end"));
59205f5a
JB
956}
957
4d3c2250
KB
958static void
959dwarf2_complex_location_expr_complaint (void)
2e276125 960{
e2e0b3e5 961 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
962}
963
4d3c2250
KB
964static void
965dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
966 int arg3)
2e276125 967{
4d3c2250 968 complaint (&symfile_complaints,
3e43a32a
MS
969 _("const value length mismatch for '%s', got %d, expected %d"),
970 arg1, arg2, arg3);
4d3c2250
KB
971}
972
973static void
f664829e 974dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 975{
4d3c2250 976 complaint (&symfile_complaints,
f664829e
DE
977 _("debug info runs off end of %s section"
978 " [in module %s]"),
979 section->asection->name,
980 bfd_get_filename (section->asection->owner));
4d3c2250
KB
981}
982
983static void
984dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 985{
4d3c2250 986 complaint (&symfile_complaints,
3e43a32a
MS
987 _("macro debug info contains a "
988 "malformed macro definition:\n`%s'"),
4d3c2250
KB
989 arg1);
990}
991
992static void
993dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 994{
4d3c2250 995 complaint (&symfile_complaints,
3e43a32a
MS
996 _("invalid attribute class or form for '%s' in '%s'"),
997 arg1, arg2);
4d3c2250 998}
c906108c 999
c906108c
SS
1000/* local function prototypes */
1001
4efb68b1 1002static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1003
aaa75496
JB
1004static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1005 struct objfile *);
1006
918dd910
JK
1007static void dwarf2_find_base_address (struct die_info *die,
1008 struct dwarf2_cu *cu);
1009
c67a9c90 1010static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1011
72bf9492
DJ
1012static void scan_partial_symbols (struct partial_die_info *,
1013 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1014 int, struct dwarf2_cu *);
c906108c 1015
72bf9492
DJ
1016static void add_partial_symbol (struct partial_die_info *,
1017 struct dwarf2_cu *);
63d06c5c 1018
72bf9492
DJ
1019static void add_partial_namespace (struct partial_die_info *pdi,
1020 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1021 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1022
5d7cb8df
JK
1023static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1024 CORE_ADDR *highpc, int need_pc,
1025 struct dwarf2_cu *cu);
1026
72bf9492
DJ
1027static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1028 struct dwarf2_cu *cu);
91c24f0a 1029
bc30ff58
JB
1030static void add_partial_subprogram (struct partial_die_info *pdi,
1031 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1032 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1033
a14ed312 1034static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 1035
a14ed312 1036static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1037
dee91e82
DE
1038static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1039 struct dwarf2_section_info *);
c906108c 1040
f3dd6933 1041static void dwarf2_free_abbrev_table (void *);
c906108c 1042
6caca83c
CC
1043static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1044
fe1b8b76 1045static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 1046 struct dwarf2_cu *);
72bf9492 1047
57349743 1048static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 1049 struct dwarf2_cu *);
c906108c 1050
dee91e82
DE
1051static struct partial_die_info *load_partial_dies
1052 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1053
dee91e82
DE
1054static gdb_byte *read_partial_die (const struct die_reader_specs *,
1055 struct partial_die_info *,
1056 struct abbrev_info *,
1057 unsigned int,
1058 gdb_byte *);
c906108c 1059
b64f50a1 1060static struct partial_die_info *find_partial_die (sect_offset,
10b3939b 1061 struct dwarf2_cu *);
72bf9492
DJ
1062
1063static void fixup_partial_die (struct partial_die_info *,
1064 struct dwarf2_cu *);
1065
dee91e82
DE
1066static gdb_byte *read_attribute (const struct die_reader_specs *,
1067 struct attribute *, struct attr_abbrev *,
1068 gdb_byte *);
a8329558 1069
fe1b8b76 1070static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 1071
fe1b8b76 1072static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 1073
fe1b8b76 1074static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 1075
fe1b8b76 1076static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 1077
93311388 1078static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 1079
fe1b8b76 1080static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1081 unsigned int *);
c906108c 1082
c764a876
DE
1083static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1084
1085static LONGEST read_checked_initial_length_and_offset
1086 (bfd *, gdb_byte *, const struct comp_unit_head *,
1087 unsigned int *, unsigned int *);
613e1657 1088
fe1b8b76 1089static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1090 unsigned int *);
1091
1092static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1093
fe1b8b76 1094static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1095
9b1c24c8 1096static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1097
fe1b8b76
JB
1098static char *read_indirect_string (bfd *, gdb_byte *,
1099 const struct comp_unit_head *,
1100 unsigned int *);
4bdf3d34 1101
12df843f 1102static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1103
12df843f 1104static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1105
3019eac3
DE
1106static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1107 unsigned int *);
1108
1109static char *read_str_index (const struct die_reader_specs *reader,
1110 struct dwarf2_cu *cu, ULONGEST str_index);
1111
e142c38c 1112static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1113
e142c38c
DJ
1114static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1115 struct dwarf2_cu *);
c906108c 1116
348e048f
DE
1117static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1118 unsigned int,
1119 struct dwarf2_cu *);
1120
05cf31d1
JB
1121static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1122 struct dwarf2_cu *cu);
1123
e142c38c 1124static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1125
e142c38c 1126static struct die_info *die_specification (struct die_info *die,
f2f0e013 1127 struct dwarf2_cu **);
63d06c5c 1128
debd256d
JB
1129static void free_line_header (struct line_header *lh);
1130
aaa75496
JB
1131static void add_file_name (struct line_header *, char *, unsigned int,
1132 unsigned int, unsigned int);
1133
3019eac3
DE
1134static struct line_header *dwarf_decode_line_header (unsigned int offset,
1135 struct dwarf2_cu *cu);
debd256d 1136
f3f5162e
DE
1137static void dwarf_decode_lines (struct line_header *, const char *,
1138 struct dwarf2_cu *, struct partial_symtab *,
1139 int);
c906108c 1140
72b9f47f 1141static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1142
a14ed312 1143static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1144 struct dwarf2_cu *);
c906108c 1145
34eaf542
TT
1146static struct symbol *new_symbol_full (struct die_info *, struct type *,
1147 struct dwarf2_cu *, struct symbol *);
1148
a14ed312 1149static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1150 struct dwarf2_cu *);
c906108c 1151
98bfdba5
PA
1152static void dwarf2_const_value_attr (struct attribute *attr,
1153 struct type *type,
1154 const char *name,
1155 struct obstack *obstack,
12df843f 1156 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1157 gdb_byte **bytes,
1158 struct dwarf2_locexpr_baton **baton);
2df3850c 1159
e7c27a73 1160static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1161
b4ba55a1
JB
1162static int need_gnat_info (struct dwarf2_cu *);
1163
3e43a32a
MS
1164static struct type *die_descriptive_type (struct die_info *,
1165 struct dwarf2_cu *);
b4ba55a1
JB
1166
1167static void set_descriptive_type (struct type *, struct die_info *,
1168 struct dwarf2_cu *);
1169
e7c27a73
DJ
1170static struct type *die_containing_type (struct die_info *,
1171 struct dwarf2_cu *);
c906108c 1172
673bfd45
DE
1173static struct type *lookup_die_type (struct die_info *, struct attribute *,
1174 struct dwarf2_cu *);
c906108c 1175
f792889a 1176static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1177
673bfd45
DE
1178static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1179
0d5cff50 1180static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1181
6e70227d 1182static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1183 const char *suffix, int physname,
1184 struct dwarf2_cu *cu);
63d06c5c 1185
e7c27a73 1186static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1187
348e048f
DE
1188static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1189
e7c27a73 1190static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1191
e7c27a73 1192static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1193
96408a79
SA
1194static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1195
ff013f42
JK
1196static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1197 struct dwarf2_cu *, struct partial_symtab *);
1198
a14ed312 1199static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1200 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1201 struct partial_symtab *);
c906108c 1202
fae299cd
DC
1203static void get_scope_pc_bounds (struct die_info *,
1204 CORE_ADDR *, CORE_ADDR *,
1205 struct dwarf2_cu *);
1206
801e3a5b
JB
1207static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1208 CORE_ADDR, struct dwarf2_cu *);
1209
a14ed312 1210static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1211 struct dwarf2_cu *);
c906108c 1212
a14ed312 1213static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1214 struct type *, struct dwarf2_cu *);
c906108c 1215
a14ed312 1216static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1217 struct die_info *, struct type *,
e7c27a73 1218 struct dwarf2_cu *);
c906108c 1219
a14ed312 1220static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1221 struct type *,
1222 struct dwarf2_cu *);
c906108c 1223
134d01f1 1224static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1225
e7c27a73 1226static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1227
e7c27a73 1228static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1229
5d7cb8df
JK
1230static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1231
27aa8d6a
SW
1232static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1233
f55ee35c
JK
1234static struct type *read_module_type (struct die_info *die,
1235 struct dwarf2_cu *cu);
1236
38d518c9 1237static const char *namespace_name (struct die_info *die,
e142c38c 1238 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1239
134d01f1 1240static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1241
e7c27a73 1242static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1243
6e70227d 1244static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1245 struct dwarf2_cu *);
1246
dee91e82 1247static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1248 gdb_byte *info_ptr,
fe1b8b76 1249 gdb_byte **new_info_ptr,
639d11d3
DC
1250 struct die_info *parent);
1251
dee91e82 1252static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1253 gdb_byte *info_ptr,
fe1b8b76 1254 gdb_byte **new_info_ptr,
639d11d3
DC
1255 struct die_info *parent);
1256
3019eac3
DE
1257static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1258 struct die_info **, gdb_byte *, int *, int);
1259
dee91e82
DE
1260static gdb_byte *read_full_die (const struct die_reader_specs *,
1261 struct die_info **, gdb_byte *, int *);
93311388 1262
e7c27a73 1263static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1264
71c25dea
TT
1265static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1266 struct obstack *);
1267
e142c38c 1268static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1269
98bfdba5
PA
1270static const char *dwarf2_full_name (char *name,
1271 struct die_info *die,
1272 struct dwarf2_cu *cu);
1273
e142c38c 1274static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1275 struct dwarf2_cu **);
9219021c 1276
f39c6ffd 1277static const char *dwarf_tag_name (unsigned int);
c906108c 1278
f39c6ffd 1279static const char *dwarf_attr_name (unsigned int);
c906108c 1280
f39c6ffd 1281static const char *dwarf_form_name (unsigned int);
c906108c 1282
a14ed312 1283static char *dwarf_bool_name (unsigned int);
c906108c 1284
f39c6ffd 1285static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1286
f9aca02d 1287static struct die_info *sibling_die (struct die_info *);
c906108c 1288
d97bc12b
DE
1289static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1290
1291static void dump_die_for_error (struct die_info *);
1292
1293static void dump_die_1 (struct ui_file *, int level, int max_level,
1294 struct die_info *);
c906108c 1295
d97bc12b 1296/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1297
51545339 1298static void store_in_ref_table (struct die_info *,
10b3939b 1299 struct dwarf2_cu *);
c906108c 1300
93311388
DE
1301static int is_ref_attr (struct attribute *);
1302
b64f50a1 1303static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1304
43bbcdc2 1305static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1306
348e048f
DE
1307static struct die_info *follow_die_ref_or_sig (struct die_info *,
1308 struct attribute *,
1309 struct dwarf2_cu **);
1310
10b3939b
DJ
1311static struct die_info *follow_die_ref (struct die_info *,
1312 struct attribute *,
f2f0e013 1313 struct dwarf2_cu **);
c906108c 1314
348e048f
DE
1315static struct die_info *follow_die_sig (struct die_info *,
1316 struct attribute *,
1317 struct dwarf2_cu **);
1318
6c83ed52
TT
1319static struct signatured_type *lookup_signatured_type_at_offset
1320 (struct objfile *objfile,
b64f50a1 1321 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1322
e5fe5e75 1323static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1324
52dc124a 1325static void read_signatured_type (struct signatured_type *);
348e048f 1326
c906108c
SS
1327/* memory allocation interface */
1328
7b5a2f43 1329static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1330
f3dd6933 1331static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1332
b60c80d6 1333static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1334
2e276125 1335static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1336 char *, bfd *, struct dwarf2_cu *,
1337 struct dwarf2_section_info *,
fceca515 1338 int, const char *);
2e276125 1339
8e19ed76
PS
1340static int attr_form_is_block (struct attribute *);
1341
3690dd37
JB
1342static int attr_form_is_section_offset (struct attribute *);
1343
1344static int attr_form_is_constant (struct attribute *);
1345
8cf6f0b1
TT
1346static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1347 struct dwarf2_loclist_baton *baton,
1348 struct attribute *attr);
1349
93e7bd98
DJ
1350static void dwarf2_symbol_mark_computed (struct attribute *attr,
1351 struct symbol *sym,
1352 struct dwarf2_cu *cu);
4c2df51b 1353
dee91e82
DE
1354static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1355 gdb_byte *info_ptr,
1356 struct abbrev_info *abbrev);
4bb7a0a7 1357
72bf9492
DJ
1358static void free_stack_comp_unit (void *);
1359
72bf9492
DJ
1360static hashval_t partial_die_hash (const void *item);
1361
1362static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1363
ae038cb0 1364static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
b64f50a1 1365 (sect_offset offset, struct objfile *objfile);
ae038cb0 1366
9816fde3 1367static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1368 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1369
1370static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1371 struct die_info *comp_unit_die,
1372 enum language pretend_language);
93311388 1373
68dc6402 1374static void free_heap_comp_unit (void *);
ae038cb0
DJ
1375
1376static void free_cached_comp_units (void *);
1377
1378static void age_cached_comp_units (void);
1379
dee91e82 1380static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1381
f792889a
DJ
1382static struct type *set_die_type (struct die_info *, struct type *,
1383 struct dwarf2_cu *);
1c379e20 1384
ae038cb0
DJ
1385static void create_all_comp_units (struct objfile *);
1386
0e50663e 1387static int create_all_type_units (struct objfile *);
1fd400ff 1388
95554aad
TT
1389static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1390 enum language);
10b3939b 1391
95554aad
TT
1392static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1393 enum language);
10b3939b
DJ
1394
1395static void dwarf2_add_dependence (struct dwarf2_cu *,
1396 struct dwarf2_per_cu_data *);
1397
ae038cb0
DJ
1398static void dwarf2_mark (struct dwarf2_cu *);
1399
1400static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1401
b64f50a1 1402static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1403 struct dwarf2_per_cu_data *per_cu);
1404
f792889a 1405static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1406
9291a0cd
TT
1407static void dwarf2_release_queue (void *dummy);
1408
95554aad
TT
1409static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1410 enum language pretend_language);
1411
1412static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1413 struct dwarf2_per_cu_data *per_cu,
1414 enum language pretend_language);
9291a0cd 1415
a0f42c21 1416static void process_queue (void);
9291a0cd
TT
1417
1418static void find_file_and_directory (struct die_info *die,
1419 struct dwarf2_cu *cu,
1420 char **name, char **comp_dir);
1421
1422static char *file_full_name (int file, struct line_header *lh,
1423 const char *comp_dir);
1424
9ff913ba
DE
1425static gdb_byte *read_and_check_comp_unit_head
1426 (struct comp_unit_head *header,
1427 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1428 int is_debug_types_section);
9291a0cd 1429
fd820528
DE
1430static void init_cutu_and_read_dies
1431 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
3019eac3
DE
1432 die_reader_func_ftype *die_reader_func, void *data);
1433
dee91e82
DE
1434static void init_cutu_and_read_dies_simple
1435 (struct dwarf2_per_cu_data *this_cu,
1436 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1437
673bfd45 1438static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1439
95554aad 1440static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *, int);
dee91e82 1441
3019eac3
DE
1442static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1443
1444static struct dwo_unit *lookup_dwo_comp_unit
1445 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1446
1447static struct dwo_unit *lookup_dwo_type_unit
1448 (struct signatured_type *, char *, const char *);
1449
1450static void free_dwo_file_cleanup (void *);
1451
1452static void munmap_section_buffer (struct dwarf2_section_info *);
1453
95554aad
TT
1454static void process_cu_includes (void);
1455
9291a0cd
TT
1456#if WORDS_BIGENDIAN
1457
1458/* Convert VALUE between big- and little-endian. */
1459static offset_type
1460byte_swap (offset_type value)
1461{
1462 offset_type result;
1463
1464 result = (value & 0xff) << 24;
1465 result |= (value & 0xff00) << 8;
1466 result |= (value & 0xff0000) >> 8;
1467 result |= (value & 0xff000000) >> 24;
1468 return result;
1469}
1470
1471#define MAYBE_SWAP(V) byte_swap (V)
1472
1473#else
1474#define MAYBE_SWAP(V) (V)
1475#endif /* WORDS_BIGENDIAN */
1476
1477/* The suffix for an index file. */
1478#define INDEX_SUFFIX ".gdb-index"
1479
3da10d80
KS
1480static const char *dwarf2_physname (char *name, struct die_info *die,
1481 struct dwarf2_cu *cu);
1482
c906108c 1483/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1484 information and return true if we have enough to do something.
1485 NAMES points to the dwarf2 section names, or is NULL if the standard
1486 ELF names are used. */
c906108c
SS
1487
1488int
251d32d9
TG
1489dwarf2_has_info (struct objfile *objfile,
1490 const struct dwarf2_debug_sections *names)
c906108c 1491{
be391dca
TT
1492 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1493 if (!dwarf2_per_objfile)
1494 {
1495 /* Initialize per-objfile state. */
1496 struct dwarf2_per_objfile *data
1497 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1498
be391dca
TT
1499 memset (data, 0, sizeof (*data));
1500 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1501 dwarf2_per_objfile = data;
6502dd73 1502
251d32d9
TG
1503 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1504 (void *) names);
be391dca
TT
1505 dwarf2_per_objfile->objfile = objfile;
1506 }
1507 return (dwarf2_per_objfile->info.asection != NULL
1508 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1509}
1510
251d32d9
TG
1511/* When loading sections, we look either for uncompressed section or for
1512 compressed section names. */
233a11ab
CS
1513
1514static int
251d32d9
TG
1515section_is_p (const char *section_name,
1516 const struct dwarf2_section_names *names)
233a11ab 1517{
251d32d9
TG
1518 if (names->normal != NULL
1519 && strcmp (section_name, names->normal) == 0)
1520 return 1;
1521 if (names->compressed != NULL
1522 && strcmp (section_name, names->compressed) == 0)
1523 return 1;
1524 return 0;
233a11ab
CS
1525}
1526
c906108c
SS
1527/* This function is mapped across the sections and remembers the
1528 offset and size of each of the debugging sections we are interested
1529 in. */
1530
1531static void
251d32d9 1532dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1533{
251d32d9
TG
1534 const struct dwarf2_debug_sections *names;
1535
1536 if (vnames == NULL)
1537 names = &dwarf2_elf_names;
1538 else
1539 names = (const struct dwarf2_debug_sections *) vnames;
1540
1541 if (section_is_p (sectp->name, &names->info))
c906108c 1542 {
dce234bc
PP
1543 dwarf2_per_objfile->info.asection = sectp;
1544 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1545 }
251d32d9 1546 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1547 {
dce234bc
PP
1548 dwarf2_per_objfile->abbrev.asection = sectp;
1549 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1550 }
251d32d9 1551 else if (section_is_p (sectp->name, &names->line))
c906108c 1552 {
dce234bc
PP
1553 dwarf2_per_objfile->line.asection = sectp;
1554 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1555 }
251d32d9 1556 else if (section_is_p (sectp->name, &names->loc))
c906108c 1557 {
dce234bc
PP
1558 dwarf2_per_objfile->loc.asection = sectp;
1559 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1560 }
251d32d9 1561 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1562 {
dce234bc
PP
1563 dwarf2_per_objfile->macinfo.asection = sectp;
1564 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1565 }
cf2c3c16
TT
1566 else if (section_is_p (sectp->name, &names->macro))
1567 {
1568 dwarf2_per_objfile->macro.asection = sectp;
1569 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1570 }
251d32d9 1571 else if (section_is_p (sectp->name, &names->str))
c906108c 1572 {
dce234bc
PP
1573 dwarf2_per_objfile->str.asection = sectp;
1574 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1575 }
3019eac3
DE
1576 else if (section_is_p (sectp->name, &names->addr))
1577 {
1578 dwarf2_per_objfile->addr.asection = sectp;
1579 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1580 }
251d32d9 1581 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1582 {
dce234bc
PP
1583 dwarf2_per_objfile->frame.asection = sectp;
1584 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1585 }
251d32d9 1586 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1587 {
81b9b86e 1588 flagword aflag = bfd_get_section_flags (abfd, sectp);
9a619af0 1589
3799ccc6
EZ
1590 if (aflag & SEC_HAS_CONTENTS)
1591 {
dce234bc
PP
1592 dwarf2_per_objfile->eh_frame.asection = sectp;
1593 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1594 }
b6af0555 1595 }
251d32d9 1596 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1597 {
dce234bc
PP
1598 dwarf2_per_objfile->ranges.asection = sectp;
1599 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1600 }
251d32d9 1601 else if (section_is_p (sectp->name, &names->types))
348e048f 1602 {
8b70b953
TT
1603 struct dwarf2_section_info type_section;
1604
1605 memset (&type_section, 0, sizeof (type_section));
1606 type_section.asection = sectp;
1607 type_section.size = bfd_get_section_size (sectp);
1608
1609 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1610 &type_section);
348e048f 1611 }
251d32d9 1612 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1613 {
1614 dwarf2_per_objfile->gdb_index.asection = sectp;
1615 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1616 }
dce234bc 1617
72dca2f5
FR
1618 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1619 && bfd_section_vma (abfd, sectp) == 0)
1620 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1621}
1622
dce234bc
PP
1623/* Decompress a section that was compressed using zlib. Store the
1624 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1625
1626static void
dce234bc
PP
1627zlib_decompress_section (struct objfile *objfile, asection *sectp,
1628 gdb_byte **outbuf, bfd_size_type *outsize)
1629{
3019eac3 1630 bfd *abfd = sectp->owner;
dce234bc
PP
1631#ifndef HAVE_ZLIB_H
1632 error (_("Support for zlib-compressed DWARF data (from '%s') "
1633 "is disabled in this copy of GDB"),
1634 bfd_get_filename (abfd));
1635#else
1636 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1637 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1638 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1639 bfd_size_type uncompressed_size;
1640 gdb_byte *uncompressed_buffer;
1641 z_stream strm;
1642 int rc;
1643 int header_size = 12;
1644
1645 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1646 || bfd_bread (compressed_buffer,
1647 compressed_size, abfd) != compressed_size)
dce234bc
PP
1648 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1649 bfd_get_filename (abfd));
1650
1651 /* Read the zlib header. In this case, it should be "ZLIB" followed
1652 by the uncompressed section size, 8 bytes in big-endian order. */
1653 if (compressed_size < header_size
1654 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1655 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1656 bfd_get_filename (abfd));
1657 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1658 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1659 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1660 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1661 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1662 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1663 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1664 uncompressed_size += compressed_buffer[11];
1665
1666 /* It is possible the section consists of several compressed
1667 buffers concatenated together, so we uncompress in a loop. */
1668 strm.zalloc = NULL;
1669 strm.zfree = NULL;
1670 strm.opaque = NULL;
1671 strm.avail_in = compressed_size - header_size;
1672 strm.next_in = (Bytef*) compressed_buffer + header_size;
1673 strm.avail_out = uncompressed_size;
1674 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1675 uncompressed_size);
1676 rc = inflateInit (&strm);
1677 while (strm.avail_in > 0)
1678 {
1679 if (rc != Z_OK)
1680 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1681 bfd_get_filename (abfd), rc);
1682 strm.next_out = ((Bytef*) uncompressed_buffer
1683 + (uncompressed_size - strm.avail_out));
1684 rc = inflate (&strm, Z_FINISH);
1685 if (rc != Z_STREAM_END)
1686 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1687 bfd_get_filename (abfd), rc);
1688 rc = inflateReset (&strm);
1689 }
1690 rc = inflateEnd (&strm);
1691 if (rc != Z_OK
1692 || strm.avail_out != 0)
1693 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1694 bfd_get_filename (abfd), rc);
1695
affddf13 1696 do_cleanups (cleanup);
dce234bc
PP
1697 *outbuf = uncompressed_buffer;
1698 *outsize = uncompressed_size;
1699#endif
233a11ab
CS
1700}
1701
fceca515
DE
1702/* A helper function that decides whether a section is empty,
1703 or not present. */
9e0ac564
TT
1704
1705static int
1706dwarf2_section_empty_p (struct dwarf2_section_info *info)
1707{
1708 return info->asection == NULL || info->size == 0;
1709}
1710
3019eac3
DE
1711/* Read the contents of the section INFO.
1712 OBJFILE is the main object file, but not necessarily the file where
1713 the section comes from. E.g., for DWO files INFO->asection->owner
1714 is the bfd of the DWO file.
dce234bc 1715 If the section is compressed, uncompress it before returning. */
c906108c 1716
dce234bc
PP
1717static void
1718dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1719{
dce234bc 1720 asection *sectp = info->asection;
3019eac3 1721 bfd *abfd;
dce234bc
PP
1722 gdb_byte *buf, *retbuf;
1723 unsigned char header[4];
c906108c 1724
be391dca
TT
1725 if (info->readin)
1726 return;
dce234bc 1727 info->buffer = NULL;
b315ab21 1728 info->map_addr = NULL;
be391dca 1729 info->readin = 1;
188dd5d6 1730
9e0ac564 1731 if (dwarf2_section_empty_p (info))
dce234bc 1732 return;
c906108c 1733
3019eac3
DE
1734 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1735 abfd = sectp->owner;
1736
dce234bc
PP
1737 /* Check if the file has a 4-byte header indicating compression. */
1738 if (info->size > sizeof (header)
1739 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1740 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1741 {
1742 /* Upon decompression, update the buffer and its size. */
1743 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1744 {
1745 zlib_decompress_section (objfile, sectp, &info->buffer,
1746 &info->size);
1747 return;
1748 }
1749 }
4bdf3d34 1750
dce234bc
PP
1751#ifdef HAVE_MMAP
1752 if (pagesize == 0)
1753 pagesize = getpagesize ();
2e276125 1754
dce234bc
PP
1755 /* Only try to mmap sections which are large enough: we don't want to
1756 waste space due to fragmentation. Also, only try mmap for sections
1757 without relocations. */
1758
1759 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1760 {
b315ab21
TG
1761 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1762 MAP_PRIVATE, sectp->filepos,
1763 &info->map_addr, &info->map_len);
dce234bc 1764
b315ab21 1765 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1766 {
be391dca 1767#if HAVE_POSIX_MADVISE
b315ab21 1768 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1769#endif
dce234bc
PP
1770 return;
1771 }
1772 }
1773#endif
1774
1775 /* If we get here, we are a normal, not-compressed section. */
1776 info->buffer = buf
1777 = obstack_alloc (&objfile->objfile_obstack, info->size);
1778
1779 /* When debugging .o files, we may need to apply relocations; see
1780 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1781 We never compress sections in .o files, so we only need to
1782 try this when the section is not compressed. */
ac8035ab 1783 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1784 if (retbuf != NULL)
1785 {
1786 info->buffer = retbuf;
1787 return;
1788 }
1789
1790 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1791 || bfd_bread (buf, info->size, abfd) != info->size)
1792 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1793 bfd_get_filename (abfd));
1794}
1795
9e0ac564
TT
1796/* A helper function that returns the size of a section in a safe way.
1797 If you are positive that the section has been read before using the
1798 size, then it is safe to refer to the dwarf2_section_info object's
1799 "size" field directly. In other cases, you must call this
1800 function, because for compressed sections the size field is not set
1801 correctly until the section has been read. */
1802
1803static bfd_size_type
1804dwarf2_section_size (struct objfile *objfile,
1805 struct dwarf2_section_info *info)
1806{
1807 if (!info->readin)
1808 dwarf2_read_section (objfile, info);
1809 return info->size;
1810}
1811
dce234bc 1812/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1813 SECTION_NAME. */
af34e669 1814
dce234bc 1815void
3017a003
TG
1816dwarf2_get_section_info (struct objfile *objfile,
1817 enum dwarf2_section_enum sect,
dce234bc
PP
1818 asection **sectp, gdb_byte **bufp,
1819 bfd_size_type *sizep)
1820{
1821 struct dwarf2_per_objfile *data
1822 = objfile_data (objfile, dwarf2_objfile_data_key);
1823 struct dwarf2_section_info *info;
a3b2a86b
TT
1824
1825 /* We may see an objfile without any DWARF, in which case we just
1826 return nothing. */
1827 if (data == NULL)
1828 {
1829 *sectp = NULL;
1830 *bufp = NULL;
1831 *sizep = 0;
1832 return;
1833 }
3017a003
TG
1834 switch (sect)
1835 {
1836 case DWARF2_DEBUG_FRAME:
1837 info = &data->frame;
1838 break;
1839 case DWARF2_EH_FRAME:
1840 info = &data->eh_frame;
1841 break;
1842 default:
1843 gdb_assert_not_reached ("unexpected section");
1844 }
dce234bc 1845
9e0ac564 1846 dwarf2_read_section (objfile, info);
dce234bc
PP
1847
1848 *sectp = info->asection;
1849 *bufp = info->buffer;
1850 *sizep = info->size;
1851}
1852
9291a0cd 1853\f
7b9f3c50
DE
1854/* DWARF quick_symbols_functions support. */
1855
1856/* TUs can share .debug_line entries, and there can be a lot more TUs than
1857 unique line tables, so we maintain a separate table of all .debug_line
1858 derived entries to support the sharing.
1859 All the quick functions need is the list of file names. We discard the
1860 line_header when we're done and don't need to record it here. */
1861struct quick_file_names
1862{
1863 /* The offset in .debug_line of the line table. We hash on this. */
1864 unsigned int offset;
1865
1866 /* The number of entries in file_names, real_names. */
1867 unsigned int num_file_names;
1868
1869 /* The file names from the line table, after being run through
1870 file_full_name. */
1871 const char **file_names;
1872
1873 /* The file names from the line table after being run through
1874 gdb_realpath. These are computed lazily. */
1875 const char **real_names;
1876};
1877
1878/* When using the index (and thus not using psymtabs), each CU has an
1879 object of this type. This is used to hold information needed by
1880 the various "quick" methods. */
1881struct dwarf2_per_cu_quick_data
1882{
1883 /* The file table. This can be NULL if there was no file table
1884 or it's currently not read in.
1885 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1886 struct quick_file_names *file_names;
1887
1888 /* The corresponding symbol table. This is NULL if symbols for this
1889 CU have not yet been read. */
1890 struct symtab *symtab;
1891
1892 /* A temporary mark bit used when iterating over all CUs in
1893 expand_symtabs_matching. */
1894 unsigned int mark : 1;
1895
1896 /* True if we've tried to read the file table and found there isn't one.
1897 There will be no point in trying to read it again next time. */
1898 unsigned int no_file_data : 1;
1899};
1900
1901/* Hash function for a quick_file_names. */
1902
1903static hashval_t
1904hash_file_name_entry (const void *e)
1905{
1906 const struct quick_file_names *file_data = e;
1907
1908 return file_data->offset;
1909}
1910
1911/* Equality function for a quick_file_names. */
1912
1913static int
1914eq_file_name_entry (const void *a, const void *b)
1915{
1916 const struct quick_file_names *ea = a;
1917 const struct quick_file_names *eb = b;
1918
1919 return ea->offset == eb->offset;
1920}
1921
1922/* Delete function for a quick_file_names. */
1923
1924static void
1925delete_file_name_entry (void *e)
1926{
1927 struct quick_file_names *file_data = e;
1928 int i;
1929
1930 for (i = 0; i < file_data->num_file_names; ++i)
1931 {
1932 xfree ((void*) file_data->file_names[i]);
1933 if (file_data->real_names)
1934 xfree ((void*) file_data->real_names[i]);
1935 }
1936
1937 /* The space for the struct itself lives on objfile_obstack,
1938 so we don't free it here. */
1939}
1940
1941/* Create a quick_file_names hash table. */
1942
1943static htab_t
1944create_quick_file_names_table (unsigned int nr_initial_entries)
1945{
1946 return htab_create_alloc (nr_initial_entries,
1947 hash_file_name_entry, eq_file_name_entry,
1948 delete_file_name_entry, xcalloc, xfree);
1949}
9291a0cd 1950
918dd910
JK
1951/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1952 have to be created afterwards. You should call age_cached_comp_units after
1953 processing PER_CU->CU. dw2_setup must have been already called. */
1954
1955static void
1956load_cu (struct dwarf2_per_cu_data *per_cu)
1957{
3019eac3 1958 if (per_cu->is_debug_types)
e5fe5e75 1959 load_full_type_unit (per_cu);
918dd910 1960 else
95554aad 1961 load_full_comp_unit (per_cu, language_minimal);
918dd910 1962
918dd910 1963 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
1964
1965 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
1966}
1967
a0f42c21 1968/* Read in the symbols for PER_CU. */
2fdf6df6 1969
9291a0cd 1970static void
a0f42c21 1971dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1972{
1973 struct cleanup *back_to;
1974
1975 back_to = make_cleanup (dwarf2_release_queue, NULL);
1976
95554aad
TT
1977 if (dwarf2_per_objfile->using_index
1978 ? per_cu->v.quick->symtab == NULL
1979 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
1980 {
1981 queue_comp_unit (per_cu, language_minimal);
1982 load_cu (per_cu);
1983 }
9291a0cd 1984
a0f42c21 1985 process_queue ();
9291a0cd
TT
1986
1987 /* Age the cache, releasing compilation units that have not
1988 been used recently. */
1989 age_cached_comp_units ();
1990
1991 do_cleanups (back_to);
1992}
1993
1994/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1995 the objfile from which this CU came. Returns the resulting symbol
1996 table. */
2fdf6df6 1997
9291a0cd 1998static struct symtab *
a0f42c21 1999dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2000{
95554aad 2001 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2002 if (!per_cu->v.quick->symtab)
2003 {
2004 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2005 increment_reading_symtab ();
a0f42c21 2006 dw2_do_instantiate_symtab (per_cu);
95554aad 2007 process_cu_includes ();
9291a0cd
TT
2008 do_cleanups (back_to);
2009 }
2010 return per_cu->v.quick->symtab;
2011}
2012
1fd400ff 2013/* Return the CU given its index. */
2fdf6df6 2014
1fd400ff
TT
2015static struct dwarf2_per_cu_data *
2016dw2_get_cu (int index)
2017{
2018 if (index >= dwarf2_per_objfile->n_comp_units)
2019 {
2020 index -= dwarf2_per_objfile->n_comp_units;
d467dd73 2021 return dwarf2_per_objfile->all_type_units[index];
1fd400ff
TT
2022 }
2023 return dwarf2_per_objfile->all_comp_units[index];
2024}
2025
9291a0cd
TT
2026/* A helper function that knows how to read a 64-bit value in a way
2027 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2028 otherwise. */
2fdf6df6 2029
9291a0cd
TT
2030static int
2031extract_cu_value (const char *bytes, ULONGEST *result)
2032{
2033 if (sizeof (ULONGEST) < 8)
2034 {
2035 int i;
2036
2037 /* Ignore the upper 4 bytes if they are all zero. */
2038 for (i = 0; i < 4; ++i)
2039 if (bytes[i + 4] != 0)
2040 return 0;
2041
2042 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2043 }
2044 else
2045 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2046 return 1;
2047}
2048
2049/* Read the CU list from the mapped index, and use it to create all
2050 the CU objects for this objfile. Return 0 if something went wrong,
2051 1 if everything went ok. */
2fdf6df6 2052
9291a0cd 2053static int
1fd400ff
TT
2054create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2055 offset_type cu_list_elements)
9291a0cd
TT
2056{
2057 offset_type i;
9291a0cd
TT
2058
2059 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2060 dwarf2_per_objfile->all_comp_units
2061 = obstack_alloc (&objfile->objfile_obstack,
2062 dwarf2_per_objfile->n_comp_units
2063 * sizeof (struct dwarf2_per_cu_data *));
2064
2065 for (i = 0; i < cu_list_elements; i += 2)
2066 {
2067 struct dwarf2_per_cu_data *the_cu;
2068 ULONGEST offset, length;
2069
2070 if (!extract_cu_value (cu_list, &offset)
2071 || !extract_cu_value (cu_list + 8, &length))
2072 return 0;
2073 cu_list += 2 * 8;
2074
2075 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2076 struct dwarf2_per_cu_data);
b64f50a1 2077 the_cu->offset.sect_off = offset;
9291a0cd
TT
2078 the_cu->length = length;
2079 the_cu->objfile = objfile;
3019eac3 2080 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
9291a0cd
TT
2081 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2082 struct dwarf2_per_cu_quick_data);
2083 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2084 }
2085
2086 return 1;
2087}
2088
1fd400ff 2089/* Create the signatured type hash table from the index. */
673bfd45 2090
1fd400ff 2091static int
673bfd45 2092create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2093 struct dwarf2_section_info *section,
673bfd45
DE
2094 const gdb_byte *bytes,
2095 offset_type elements)
1fd400ff
TT
2096{
2097 offset_type i;
673bfd45 2098 htab_t sig_types_hash;
1fd400ff 2099
d467dd73
DE
2100 dwarf2_per_objfile->n_type_units = elements / 3;
2101 dwarf2_per_objfile->all_type_units
1fd400ff 2102 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2103 dwarf2_per_objfile->n_type_units
1fd400ff
TT
2104 * sizeof (struct dwarf2_per_cu_data *));
2105
673bfd45 2106 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2107
2108 for (i = 0; i < elements; i += 3)
2109 {
52dc124a
DE
2110 struct signatured_type *sig_type;
2111 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2112 void **slot;
2113
2114 if (!extract_cu_value (bytes, &offset)
52dc124a 2115 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
2116 return 0;
2117 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2118 bytes += 3 * 8;
2119
52dc124a 2120 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2121 struct signatured_type);
52dc124a 2122 sig_type->signature = signature;
3019eac3
DE
2123 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2124 sig_type->per_cu.is_debug_types = 1;
2125 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2126 sig_type->per_cu.offset.sect_off = offset;
2127 sig_type->per_cu.objfile = objfile;
2128 sig_type->per_cu.v.quick
1fd400ff
TT
2129 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2130 struct dwarf2_per_cu_quick_data);
2131
52dc124a
DE
2132 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2133 *slot = sig_type;
1fd400ff 2134
52dc124a 2135 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
1fd400ff
TT
2136 }
2137
673bfd45 2138 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2139
2140 return 1;
2141}
2142
9291a0cd
TT
2143/* Read the address map data from the mapped index, and use it to
2144 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2145
9291a0cd
TT
2146static void
2147create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2148{
2149 const gdb_byte *iter, *end;
2150 struct obstack temp_obstack;
2151 struct addrmap *mutable_map;
2152 struct cleanup *cleanup;
2153 CORE_ADDR baseaddr;
2154
2155 obstack_init (&temp_obstack);
2156 cleanup = make_cleanup_obstack_free (&temp_obstack);
2157 mutable_map = addrmap_create_mutable (&temp_obstack);
2158
2159 iter = index->address_table;
2160 end = iter + index->address_table_size;
2161
2162 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2163
2164 while (iter < end)
2165 {
2166 ULONGEST hi, lo, cu_index;
2167 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2168 iter += 8;
2169 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2170 iter += 8;
2171 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2172 iter += 4;
2173
2174 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2175 dw2_get_cu (cu_index));
9291a0cd
TT
2176 }
2177
2178 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2179 &objfile->objfile_obstack);
2180 do_cleanups (cleanup);
2181}
2182
59d7bcaf
JK
2183/* The hash function for strings in the mapped index. This is the same as
2184 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2185 implementation. This is necessary because the hash function is tied to the
2186 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2187 SYMBOL_HASH_NEXT.
2188
2189 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2190
9291a0cd 2191static hashval_t
559a7a62 2192mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2193{
2194 const unsigned char *str = (const unsigned char *) p;
2195 hashval_t r = 0;
2196 unsigned char c;
2197
2198 while ((c = *str++) != 0)
559a7a62
JK
2199 {
2200 if (index_version >= 5)
2201 c = tolower (c);
2202 r = r * 67 + c - 113;
2203 }
9291a0cd
TT
2204
2205 return r;
2206}
2207
2208/* Find a slot in the mapped index INDEX for the object named NAME.
2209 If NAME is found, set *VEC_OUT to point to the CU vector in the
2210 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2211
9291a0cd
TT
2212static int
2213find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2214 offset_type **vec_out)
2215{
0cf03b49
JK
2216 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2217 offset_type hash;
9291a0cd 2218 offset_type slot, step;
559a7a62 2219 int (*cmp) (const char *, const char *);
9291a0cd 2220
0cf03b49
JK
2221 if (current_language->la_language == language_cplus
2222 || current_language->la_language == language_java
2223 || current_language->la_language == language_fortran)
2224 {
2225 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2226 not contain any. */
2227 const char *paren = strchr (name, '(');
2228
2229 if (paren)
2230 {
2231 char *dup;
2232
2233 dup = xmalloc (paren - name + 1);
2234 memcpy (dup, name, paren - name);
2235 dup[paren - name] = 0;
2236
2237 make_cleanup (xfree, dup);
2238 name = dup;
2239 }
2240 }
2241
559a7a62 2242 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2243 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2244 simulate our NAME being searched is also lowercased. */
2245 hash = mapped_index_string_hash ((index->version == 4
2246 && case_sensitivity == case_sensitive_off
2247 ? 5 : index->version),
2248 name);
2249
3876f04e
DE
2250 slot = hash & (index->symbol_table_slots - 1);
2251 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2252 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2253
2254 for (;;)
2255 {
2256 /* Convert a slot number to an offset into the table. */
2257 offset_type i = 2 * slot;
2258 const char *str;
3876f04e 2259 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2260 {
2261 do_cleanups (back_to);
2262 return 0;
2263 }
9291a0cd 2264
3876f04e 2265 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2266 if (!cmp (name, str))
9291a0cd
TT
2267 {
2268 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2269 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2270 do_cleanups (back_to);
9291a0cd
TT
2271 return 1;
2272 }
2273
3876f04e 2274 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2275 }
2276}
2277
2278/* Read the index file. If everything went ok, initialize the "quick"
2279 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2280
9291a0cd
TT
2281static int
2282dwarf2_read_index (struct objfile *objfile)
2283{
9291a0cd
TT
2284 char *addr;
2285 struct mapped_index *map;
b3b272e1 2286 offset_type *metadata;
ac0b195c
KW
2287 const gdb_byte *cu_list;
2288 const gdb_byte *types_list = NULL;
2289 offset_type version, cu_list_elements;
2290 offset_type types_list_elements = 0;
1fd400ff 2291 int i;
9291a0cd 2292
9e0ac564 2293 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2294 return 0;
82430852
JK
2295
2296 /* Older elfutils strip versions could keep the section in the main
2297 executable while splitting it for the separate debug info file. */
2298 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2299 & SEC_HAS_CONTENTS) == 0)
2300 return 0;
2301
9291a0cd
TT
2302 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2303
2304 addr = dwarf2_per_objfile->gdb_index.buffer;
2305 /* Version check. */
1fd400ff 2306 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2307 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2308 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2309 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2310 indices. */
831adc1f 2311 if (version < 4)
481860b3
GB
2312 {
2313 static int warning_printed = 0;
2314 if (!warning_printed)
2315 {
2316 warning (_("Skipping obsolete .gdb_index section in %s."),
2317 objfile->name);
2318 warning_printed = 1;
2319 }
2320 return 0;
2321 }
2322 /* Index version 4 uses a different hash function than index version
2323 5 and later.
2324
2325 Versions earlier than 6 did not emit psymbols for inlined
2326 functions. Using these files will cause GDB not to be able to
2327 set breakpoints on inlined functions by name, so we ignore these
2328 indices unless the --use-deprecated-index-sections command line
2329 option was supplied. */
2330 if (version < 6 && !use_deprecated_index_sections)
2331 {
2332 static int warning_printed = 0;
2333 if (!warning_printed)
2334 {
2335 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2336 "--use-deprecated-index-sections to use them anyway"),
2337 objfile->name);
2338 warning_printed = 1;
2339 }
2340 return 0;
2341 }
2342 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2343 longer backward compatible. */
481860b3 2344 if (version > 6)
594e8718 2345 return 0;
9291a0cd
TT
2346
2347 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2348 map->version = version;
b3b272e1 2349 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2350
2351 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2352
2353 i = 0;
2354 cu_list = addr + MAYBE_SWAP (metadata[i]);
2355 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2356 / 8);
1fd400ff
TT
2357 ++i;
2358
987d643c
TT
2359 types_list = addr + MAYBE_SWAP (metadata[i]);
2360 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2361 - MAYBE_SWAP (metadata[i]))
2362 / 8);
2363 ++i;
1fd400ff
TT
2364
2365 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2366 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2367 - MAYBE_SWAP (metadata[i]));
2368 ++i;
2369
3876f04e
DE
2370 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2371 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2372 - MAYBE_SWAP (metadata[i]))
2373 / (2 * sizeof (offset_type)));
1fd400ff 2374 ++i;
9291a0cd 2375
1fd400ff
TT
2376 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2377
0fefef59
DE
2378 /* Don't use the index if it's empty. */
2379 if (map->symbol_table_slots == 0)
2380 return 0;
2381
1fd400ff
TT
2382 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2383 return 0;
2384
8b70b953
TT
2385 if (types_list_elements)
2386 {
2387 struct dwarf2_section_info *section;
2388
2389 /* We can only handle a single .debug_types when we have an
2390 index. */
2391 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2392 return 0;
2393
2394 section = VEC_index (dwarf2_section_info_def,
2395 dwarf2_per_objfile->types, 0);
2396
2397 if (!create_signatured_type_table_from_index (objfile, section,
2398 types_list,
2399 types_list_elements))
2400 return 0;
2401 }
9291a0cd
TT
2402
2403 create_addrmap_from_index (objfile, map);
2404
2405 dwarf2_per_objfile->index_table = map;
2406 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2407 dwarf2_per_objfile->quick_file_names_table =
2408 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2409
2410 return 1;
2411}
2412
2413/* A helper for the "quick" functions which sets the global
2414 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2415
9291a0cd
TT
2416static void
2417dw2_setup (struct objfile *objfile)
2418{
2419 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2420 gdb_assert (dwarf2_per_objfile);
2421}
2422
dee91e82 2423/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2424
dee91e82
DE
2425static void
2426dw2_get_file_names_reader (const struct die_reader_specs *reader,
2427 gdb_byte *info_ptr,
2428 struct die_info *comp_unit_die,
2429 int has_children,
2430 void *data)
9291a0cd 2431{
dee91e82
DE
2432 struct dwarf2_cu *cu = reader->cu;
2433 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2434 struct objfile *objfile = dwarf2_per_objfile->objfile;
7b9f3c50 2435 struct line_header *lh;
9291a0cd 2436 struct attribute *attr;
dee91e82 2437 int i;
9291a0cd 2438 char *name, *comp_dir;
7b9f3c50
DE
2439 void **slot;
2440 struct quick_file_names *qfn;
2441 unsigned int line_offset;
9291a0cd 2442
7b9f3c50
DE
2443 lh = NULL;
2444 slot = NULL;
2445 line_offset = 0;
dee91e82
DE
2446
2447 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2448 if (attr)
2449 {
7b9f3c50
DE
2450 struct quick_file_names find_entry;
2451
2452 line_offset = DW_UNSND (attr);
2453
2454 /* We may have already read in this line header (TU line header sharing).
2455 If we have we're done. */
2456 find_entry.offset = line_offset;
2457 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2458 &find_entry, INSERT);
2459 if (*slot != NULL)
2460 {
7b9f3c50 2461 this_cu->v.quick->file_names = *slot;
dee91e82 2462 return;
7b9f3c50
DE
2463 }
2464
3019eac3 2465 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2466 }
2467 if (lh == NULL)
2468 {
7b9f3c50 2469 this_cu->v.quick->no_file_data = 1;
dee91e82 2470 return;
9291a0cd
TT
2471 }
2472
7b9f3c50
DE
2473 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2474 qfn->offset = line_offset;
2475 gdb_assert (slot != NULL);
2476 *slot = qfn;
9291a0cd 2477
dee91e82 2478 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2479
7b9f3c50
DE
2480 qfn->num_file_names = lh->num_file_names;
2481 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2482 lh->num_file_names * sizeof (char *));
9291a0cd 2483 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2484 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2485 qfn->real_names = NULL;
9291a0cd 2486
7b9f3c50 2487 free_line_header (lh);
7b9f3c50
DE
2488
2489 this_cu->v.quick->file_names = qfn;
dee91e82
DE
2490}
2491
2492/* A helper for the "quick" functions which attempts to read the line
2493 table for THIS_CU. */
2494
2495static struct quick_file_names *
2496dw2_get_file_names (struct objfile *objfile,
2497 struct dwarf2_per_cu_data *this_cu)
2498{
2499 if (this_cu->v.quick->file_names != NULL)
2500 return this_cu->v.quick->file_names;
2501 /* If we know there is no line data, no point in looking again. */
2502 if (this_cu->v.quick->no_file_data)
2503 return NULL;
2504
3019eac3
DE
2505 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2506 in the stub for CUs, there's is no need to lookup the DWO file.
2507 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2508 DWO file. */
2509 if (this_cu->is_debug_types)
fd820528 2510 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
3019eac3
DE
2511 else
2512 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2513
2514 if (this_cu->v.quick->no_file_data)
2515 return NULL;
2516 return this_cu->v.quick->file_names;
9291a0cd
TT
2517}
2518
2519/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2520 real path for a given file name from the line table. */
2fdf6df6 2521
9291a0cd 2522static const char *
7b9f3c50
DE
2523dw2_get_real_path (struct objfile *objfile,
2524 struct quick_file_names *qfn, int index)
9291a0cd 2525{
7b9f3c50
DE
2526 if (qfn->real_names == NULL)
2527 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2528 qfn->num_file_names, sizeof (char *));
9291a0cd 2529
7b9f3c50
DE
2530 if (qfn->real_names[index] == NULL)
2531 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2532
7b9f3c50 2533 return qfn->real_names[index];
9291a0cd
TT
2534}
2535
2536static struct symtab *
2537dw2_find_last_source_symtab (struct objfile *objfile)
2538{
2539 int index;
ae2de4f8 2540
9291a0cd
TT
2541 dw2_setup (objfile);
2542 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2543 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2544}
2545
7b9f3c50
DE
2546/* Traversal function for dw2_forget_cached_source_info. */
2547
2548static int
2549dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2550{
7b9f3c50 2551 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2552
7b9f3c50 2553 if (file_data->real_names)
9291a0cd 2554 {
7b9f3c50 2555 int i;
9291a0cd 2556
7b9f3c50 2557 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2558 {
7b9f3c50
DE
2559 xfree ((void*) file_data->real_names[i]);
2560 file_data->real_names[i] = NULL;
9291a0cd
TT
2561 }
2562 }
7b9f3c50
DE
2563
2564 return 1;
2565}
2566
2567static void
2568dw2_forget_cached_source_info (struct objfile *objfile)
2569{
2570 dw2_setup (objfile);
2571
2572 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2573 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2574}
2575
f8eba3c6
TT
2576/* Helper function for dw2_map_symtabs_matching_filename that expands
2577 the symtabs and calls the iterator. */
2578
2579static int
2580dw2_map_expand_apply (struct objfile *objfile,
2581 struct dwarf2_per_cu_data *per_cu,
2582 const char *name,
2583 const char *full_path, const char *real_path,
2584 int (*callback) (struct symtab *, void *),
2585 void *data)
2586{
2587 struct symtab *last_made = objfile->symtabs;
2588
2589 /* Don't visit already-expanded CUs. */
2590 if (per_cu->v.quick->symtab)
2591 return 0;
2592
2593 /* This may expand more than one symtab, and we want to iterate over
2594 all of them. */
a0f42c21 2595 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2596
2597 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2598 objfile->symtabs, last_made);
2599}
2600
2601/* Implementation of the map_symtabs_matching_filename method. */
2602
9291a0cd 2603static int
f8eba3c6
TT
2604dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2605 const char *full_path, const char *real_path,
2606 int (*callback) (struct symtab *, void *),
2607 void *data)
9291a0cd
TT
2608{
2609 int i;
c011a4f4 2610 const char *name_basename = lbasename (name);
4aac40c8
TT
2611 int name_len = strlen (name);
2612 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
2613
2614 dw2_setup (objfile);
ae2de4f8 2615
1fd400ff 2616 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2617 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2618 {
2619 int j;
e254ef6a 2620 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2621 struct quick_file_names *file_data;
9291a0cd 2622
3d7bb9d9 2623 /* We only need to look at symtabs not already expanded. */
e254ef6a 2624 if (per_cu->v.quick->symtab)
9291a0cd
TT
2625 continue;
2626
7b9f3c50
DE
2627 file_data = dw2_get_file_names (objfile, per_cu);
2628 if (file_data == NULL)
9291a0cd
TT
2629 continue;
2630
7b9f3c50 2631 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2632 {
7b9f3c50 2633 const char *this_name = file_data->file_names[j];
9291a0cd 2634
4aac40c8
TT
2635 if (FILENAME_CMP (name, this_name) == 0
2636 || (!is_abs && compare_filenames_for_search (this_name,
2637 name, name_len)))
9291a0cd 2638 {
f8eba3c6
TT
2639 if (dw2_map_expand_apply (objfile, per_cu,
2640 name, full_path, real_path,
2641 callback, data))
2642 return 1;
4aac40c8 2643 }
9291a0cd 2644
c011a4f4
DE
2645 /* Before we invoke realpath, which can get expensive when many
2646 files are involved, do a quick comparison of the basenames. */
2647 if (! basenames_may_differ
2648 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2649 continue;
2650
9291a0cd
TT
2651 if (full_path != NULL)
2652 {
7b9f3c50
DE
2653 const char *this_real_name = dw2_get_real_path (objfile,
2654 file_data, j);
9291a0cd 2655
7b9f3c50 2656 if (this_real_name != NULL
4aac40c8
TT
2657 && (FILENAME_CMP (full_path, this_real_name) == 0
2658 || (!is_abs
2659 && compare_filenames_for_search (this_real_name,
2660 name, name_len))))
9291a0cd 2661 {
f8eba3c6
TT
2662 if (dw2_map_expand_apply (objfile, per_cu,
2663 name, full_path, real_path,
2664 callback, data))
2665 return 1;
9291a0cd
TT
2666 }
2667 }
2668
2669 if (real_path != NULL)
2670 {
7b9f3c50
DE
2671 const char *this_real_name = dw2_get_real_path (objfile,
2672 file_data, j);
9291a0cd 2673
7b9f3c50 2674 if (this_real_name != NULL
4aac40c8
TT
2675 && (FILENAME_CMP (real_path, this_real_name) == 0
2676 || (!is_abs
2677 && compare_filenames_for_search (this_real_name,
2678 name, name_len))))
9291a0cd 2679 {
f8eba3c6
TT
2680 if (dw2_map_expand_apply (objfile, per_cu,
2681 name, full_path, real_path,
2682 callback, data))
2683 return 1;
9291a0cd
TT
2684 }
2685 }
2686 }
2687 }
2688
9291a0cd
TT
2689 return 0;
2690}
2691
2692static struct symtab *
2693dw2_lookup_symbol (struct objfile *objfile, int block_index,
2694 const char *name, domain_enum domain)
2695{
774b6a14 2696 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2697 instead. */
2698 return NULL;
2699}
2700
2701/* A helper function that expands all symtabs that hold an object
2702 named NAME. */
2fdf6df6 2703
9291a0cd
TT
2704static void
2705dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2706{
2707 dw2_setup (objfile);
2708
ae2de4f8 2709 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2710 if (dwarf2_per_objfile->index_table)
2711 {
2712 offset_type *vec;
2713
2714 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2715 name, &vec))
2716 {
2717 offset_type i, len = MAYBE_SWAP (*vec);
2718 for (i = 0; i < len; ++i)
2719 {
2720 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2721 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2722
a0f42c21 2723 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2724 }
2725 }
2726 }
2727}
2728
774b6a14
TT
2729static void
2730dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2731 enum block_enum block_kind, const char *name,
774b6a14 2732 domain_enum domain)
9291a0cd 2733{
774b6a14 2734 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2735}
2736
2737static void
2738dw2_print_stats (struct objfile *objfile)
2739{
2740 int i, count;
2741
2742 dw2_setup (objfile);
2743 count = 0;
1fd400ff 2744 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2745 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2746 {
e254ef6a 2747 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2748
e254ef6a 2749 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2750 ++count;
2751 }
2752 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2753}
2754
2755static void
2756dw2_dump (struct objfile *objfile)
2757{
2758 /* Nothing worth printing. */
2759}
2760
2761static void
2762dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2763 struct section_offsets *delta)
2764{
2765 /* There's nothing to relocate here. */
2766}
2767
2768static void
2769dw2_expand_symtabs_for_function (struct objfile *objfile,
2770 const char *func_name)
2771{
2772 dw2_do_expand_symtabs_matching (objfile, func_name);
2773}
2774
2775static void
2776dw2_expand_all_symtabs (struct objfile *objfile)
2777{
2778 int i;
2779
2780 dw2_setup (objfile);
1fd400ff
TT
2781
2782 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2783 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2784 {
e254ef6a 2785 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2786
a0f42c21 2787 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2788 }
2789}
2790
2791static void
2792dw2_expand_symtabs_with_filename (struct objfile *objfile,
2793 const char *filename)
2794{
2795 int i;
2796
2797 dw2_setup (objfile);
d4637a04
DE
2798
2799 /* We don't need to consider type units here.
2800 This is only called for examining code, e.g. expand_line_sal.
2801 There can be an order of magnitude (or more) more type units
2802 than comp units, and we avoid them if we can. */
2803
2804 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2805 {
2806 int j;
e254ef6a 2807 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2808 struct quick_file_names *file_data;
9291a0cd 2809
3d7bb9d9 2810 /* We only need to look at symtabs not already expanded. */
e254ef6a 2811 if (per_cu->v.quick->symtab)
9291a0cd
TT
2812 continue;
2813
7b9f3c50
DE
2814 file_data = dw2_get_file_names (objfile, per_cu);
2815 if (file_data == NULL)
9291a0cd
TT
2816 continue;
2817
7b9f3c50 2818 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2819 {
7b9f3c50 2820 const char *this_name = file_data->file_names[j];
1ef75ecc 2821 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2822 {
a0f42c21 2823 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2824 break;
2825 }
2826 }
2827 }
2828}
2829
dd786858 2830static const char *
9291a0cd
TT
2831dw2_find_symbol_file (struct objfile *objfile, const char *name)
2832{
e254ef6a 2833 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2834 offset_type *vec;
7b9f3c50 2835 struct quick_file_names *file_data;
9291a0cd
TT
2836
2837 dw2_setup (objfile);
2838
ae2de4f8 2839 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2840 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2841 {
2842 struct symtab *s;
2843
2844 ALL_OBJFILE_SYMTABS (objfile, s)
2845 if (s->primary)
2846 {
2847 struct blockvector *bv = BLOCKVECTOR (s);
2848 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2849 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2850
2851 if (sym)
2852 return sym->symtab->filename;
2853 }
2854 return NULL;
2855 }
9291a0cd
TT
2856
2857 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2858 name, &vec))
2859 return NULL;
2860
2861 /* Note that this just looks at the very first one named NAME -- but
2862 actually we are looking for a function. find_main_filename
2863 should be rewritten so that it doesn't require a custom hook. It
2864 could just use the ordinary symbol tables. */
2865 /* vec[0] is the length, which must always be >0. */
e254ef6a 2866 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2867
7b9f3c50 2868 file_data = dw2_get_file_names (objfile, per_cu);
943cb756
DE
2869 if (file_data == NULL
2870 || file_data->num_file_names == 0)
9291a0cd
TT
2871 return NULL;
2872
7b9f3c50 2873 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2874}
2875
2876static void
40658b94
PH
2877dw2_map_matching_symbols (const char * name, domain_enum namespace,
2878 struct objfile *objfile, int global,
2879 int (*callback) (struct block *,
2880 struct symbol *, void *),
2edb89d3
JK
2881 void *data, symbol_compare_ftype *match,
2882 symbol_compare_ftype *ordered_compare)
9291a0cd 2883{
40658b94 2884 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2885 current language is Ada for a non-Ada objfile using GNU index. As Ada
2886 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2887}
2888
2889static void
f8eba3c6
TT
2890dw2_expand_symtabs_matching
2891 (struct objfile *objfile,
2892 int (*file_matcher) (const char *, void *),
e078317b 2893 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
2894 enum search_domain kind,
2895 void *data)
9291a0cd
TT
2896{
2897 int i;
2898 offset_type iter;
4b5246aa 2899 struct mapped_index *index;
9291a0cd
TT
2900
2901 dw2_setup (objfile);
ae2de4f8
DE
2902
2903 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2904 if (!dwarf2_per_objfile->index_table)
2905 return;
4b5246aa 2906 index = dwarf2_per_objfile->index_table;
9291a0cd 2907
7b08b9eb 2908 if (file_matcher != NULL)
24c79950
TT
2909 {
2910 struct cleanup *cleanup;
2911 htab_t visited_found, visited_not_found;
2912
2913 visited_found = htab_create_alloc (10,
2914 htab_hash_pointer, htab_eq_pointer,
2915 NULL, xcalloc, xfree);
2916 cleanup = make_cleanup_htab_delete (visited_found);
2917 visited_not_found = htab_create_alloc (10,
2918 htab_hash_pointer, htab_eq_pointer,
2919 NULL, xcalloc, xfree);
2920 make_cleanup_htab_delete (visited_not_found);
2921
2922 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2923 + dwarf2_per_objfile->n_type_units); ++i)
2924 {
2925 int j;
2926 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2927 struct quick_file_names *file_data;
2928 void **slot;
7b08b9eb 2929
24c79950 2930 per_cu->v.quick->mark = 0;
3d7bb9d9 2931
24c79950
TT
2932 /* We only need to look at symtabs not already expanded. */
2933 if (per_cu->v.quick->symtab)
2934 continue;
7b08b9eb 2935
24c79950
TT
2936 file_data = dw2_get_file_names (objfile, per_cu);
2937 if (file_data == NULL)
2938 continue;
7b08b9eb 2939
24c79950
TT
2940 if (htab_find (visited_not_found, file_data) != NULL)
2941 continue;
2942 else if (htab_find (visited_found, file_data) != NULL)
2943 {
2944 per_cu->v.quick->mark = 1;
2945 continue;
2946 }
2947
2948 for (j = 0; j < file_data->num_file_names; ++j)
2949 {
2950 if (file_matcher (file_data->file_names[j], data))
2951 {
2952 per_cu->v.quick->mark = 1;
2953 break;
2954 }
2955 }
2956
2957 slot = htab_find_slot (per_cu->v.quick->mark
2958 ? visited_found
2959 : visited_not_found,
2960 file_data, INSERT);
2961 *slot = file_data;
2962 }
2963
2964 do_cleanups (cleanup);
2965 }
9291a0cd 2966
3876f04e 2967 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2968 {
2969 offset_type idx = 2 * iter;
2970 const char *name;
2971 offset_type *vec, vec_len, vec_idx;
2972
3876f04e 2973 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2974 continue;
2975
3876f04e 2976 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 2977
e078317b 2978 if (! (*name_matcher) (name, data))
9291a0cd
TT
2979 continue;
2980
2981 /* The name was matched, now expand corresponding CUs that were
2982 marked. */
4b5246aa 2983 vec = (offset_type *) (index->constant_pool
3876f04e 2984 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2985 vec_len = MAYBE_SWAP (vec[0]);
2986 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2987 {
e254ef6a 2988 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2989
e254ef6a 2990 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2991 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 2992 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2993 }
2994 }
2995}
2996
9703b513
TT
2997/* A helper for dw2_find_pc_sect_symtab which finds the most specific
2998 symtab. */
2999
3000static struct symtab *
3001recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3002{
3003 int i;
3004
3005 if (BLOCKVECTOR (symtab) != NULL
3006 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3007 return symtab;
3008
a3ec0bb1
DE
3009 if (symtab->includes == NULL)
3010 return NULL;
3011
9703b513
TT
3012 for (i = 0; symtab->includes[i]; ++i)
3013 {
a3ec0bb1 3014 struct symtab *s = symtab->includes[i];
9703b513
TT
3015
3016 s = recursively_find_pc_sect_symtab (s, pc);
3017 if (s != NULL)
3018 return s;
3019 }
3020
3021 return NULL;
3022}
3023
9291a0cd
TT
3024static struct symtab *
3025dw2_find_pc_sect_symtab (struct objfile *objfile,
3026 struct minimal_symbol *msymbol,
3027 CORE_ADDR pc,
3028 struct obj_section *section,
3029 int warn_if_readin)
3030{
3031 struct dwarf2_per_cu_data *data;
9703b513 3032 struct symtab *result;
9291a0cd
TT
3033
3034 dw2_setup (objfile);
3035
3036 if (!objfile->psymtabs_addrmap)
3037 return NULL;
3038
3039 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3040 if (!data)
3041 return NULL;
3042
3043 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3044 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3045 paddress (get_objfile_arch (objfile), pc));
3046
9703b513
TT
3047 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3048 gdb_assert (result != NULL);
3049 return result;
9291a0cd
TT
3050}
3051
9291a0cd 3052static void
44b13c5a 3053dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3054 void *data, int need_fullname)
9291a0cd
TT
3055{
3056 int i;
24c79950
TT
3057 struct cleanup *cleanup;
3058 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3059 NULL, xcalloc, xfree);
9291a0cd 3060
24c79950 3061 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3062 dw2_setup (objfile);
ae2de4f8 3063
24c79950
TT
3064 /* We can ignore file names coming from already-expanded CUs. */
3065 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3066 + dwarf2_per_objfile->n_type_units); ++i)
3067 {
3068 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3069
3070 if (per_cu->v.quick->symtab)
3071 {
3072 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3073 INSERT);
3074
3075 *slot = per_cu->v.quick->file_names;
3076 }
3077 }
3078
1fd400ff 3079 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3080 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
3081 {
3082 int j;
e254ef6a 3083 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3084 struct quick_file_names *file_data;
24c79950 3085 void **slot;
9291a0cd 3086
3d7bb9d9 3087 /* We only need to look at symtabs not already expanded. */
e254ef6a 3088 if (per_cu->v.quick->symtab)
9291a0cd
TT
3089 continue;
3090
7b9f3c50
DE
3091 file_data = dw2_get_file_names (objfile, per_cu);
3092 if (file_data == NULL)
9291a0cd
TT
3093 continue;
3094
24c79950
TT
3095 slot = htab_find_slot (visited, file_data, INSERT);
3096 if (*slot)
3097 {
3098 /* Already visited. */
3099 continue;
3100 }
3101 *slot = file_data;
3102
7b9f3c50 3103 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3104 {
74e2f255
DE
3105 const char *this_real_name;
3106
3107 if (need_fullname)
3108 this_real_name = dw2_get_real_path (objfile, file_data, j);
3109 else
3110 this_real_name = NULL;
7b9f3c50 3111 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3112 }
3113 }
24c79950
TT
3114
3115 do_cleanups (cleanup);
9291a0cd
TT
3116}
3117
3118static int
3119dw2_has_symbols (struct objfile *objfile)
3120{
3121 return 1;
3122}
3123
3124const struct quick_symbol_functions dwarf2_gdb_index_functions =
3125{
3126 dw2_has_symbols,
3127 dw2_find_last_source_symtab,
3128 dw2_forget_cached_source_info,
f8eba3c6 3129 dw2_map_symtabs_matching_filename,
9291a0cd 3130 dw2_lookup_symbol,
774b6a14 3131 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3132 dw2_print_stats,
3133 dw2_dump,
3134 dw2_relocate,
3135 dw2_expand_symtabs_for_function,
3136 dw2_expand_all_symtabs,
3137 dw2_expand_symtabs_with_filename,
3138 dw2_find_symbol_file,
40658b94 3139 dw2_map_matching_symbols,
9291a0cd
TT
3140 dw2_expand_symtabs_matching,
3141 dw2_find_pc_sect_symtab,
9291a0cd
TT
3142 dw2_map_symbol_filenames
3143};
3144
3145/* Initialize for reading DWARF for this objfile. Return 0 if this
3146 file will use psymtabs, or 1 if using the GNU index. */
3147
3148int
3149dwarf2_initialize_objfile (struct objfile *objfile)
3150{
3151 /* If we're about to read full symbols, don't bother with the
3152 indices. In this case we also don't care if some other debug
3153 format is making psymtabs, because they are all about to be
3154 expanded anyway. */
3155 if ((objfile->flags & OBJF_READNOW))
3156 {
3157 int i;
3158
3159 dwarf2_per_objfile->using_index = 1;
3160 create_all_comp_units (objfile);
0e50663e 3161 create_all_type_units (objfile);
7b9f3c50
DE
3162 dwarf2_per_objfile->quick_file_names_table =
3163 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3164
1fd400ff 3165 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3166 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3167 {
e254ef6a 3168 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3169
e254ef6a
DE
3170 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3171 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3172 }
3173
3174 /* Return 1 so that gdb sees the "quick" functions. However,
3175 these functions will be no-ops because we will have expanded
3176 all symtabs. */
3177 return 1;
3178 }
3179
3180 if (dwarf2_read_index (objfile))
3181 return 1;
3182
9291a0cd
TT
3183 return 0;
3184}
3185
3186\f
3187
dce234bc
PP
3188/* Build a partial symbol table. */
3189
3190void
f29dff0a 3191dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3192{
f29dff0a 3193 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3194 {
3195 init_psymbol_list (objfile, 1024);
3196 }
3197
d146bf1e 3198 dwarf2_build_psymtabs_hard (objfile);
c906108c 3199}
c906108c 3200
45452591
DE
3201/* Return TRUE if OFFSET is within CU_HEADER. */
3202
3203static inline int
b64f50a1 3204offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3205{
b64f50a1
JK
3206 sect_offset bottom = { cu_header->offset.sect_off };
3207 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3208 + cu_header->initial_length_size) };
9a619af0 3209
b64f50a1 3210 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3211}
3212
93311388
DE
3213/* Read in the comp unit header information from the debug_info at info_ptr.
3214 NOTE: This leaves members offset, first_die_offset to be filled in
3215 by the caller. */
107d2387 3216
fe1b8b76 3217static gdb_byte *
107d2387 3218read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3219 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3220{
3221 int signed_addr;
891d2f0b 3222 unsigned int bytes_read;
c764a876
DE
3223
3224 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3225 cu_header->initial_length_size = bytes_read;
3226 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3227 info_ptr += bytes_read;
107d2387
AC
3228 cu_header->version = read_2_bytes (abfd, info_ptr);
3229 info_ptr += 2;
b64f50a1
JK
3230 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3231 &bytes_read);
613e1657 3232 info_ptr += bytes_read;
107d2387
AC
3233 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3234 info_ptr += 1;
3235 signed_addr = bfd_get_sign_extend_vma (abfd);
3236 if (signed_addr < 0)
8e65ff28 3237 internal_error (__FILE__, __LINE__,
e2e0b3e5 3238 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3239 cu_header->signed_addr_p = signed_addr;
c764a876 3240
107d2387
AC
3241 return info_ptr;
3242}
3243
9ff913ba
DE
3244/* Subroutine of read_and_check_comp_unit_head and
3245 read_and_check_type_unit_head to simplify them.
3246 Perform various error checking on the header. */
3247
3248static void
3249error_check_comp_unit_head (struct comp_unit_head *header,
3250 struct dwarf2_section_info *section)
3251{
3252 bfd *abfd = section->asection->owner;
3253 const char *filename = bfd_get_filename (abfd);
3254
3255 if (header->version != 2 && header->version != 3 && header->version != 4)
3256 error (_("Dwarf Error: wrong version in compilation unit header "
3257 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3258 filename);
3259
b64f50a1 3260 if (header->abbrev_offset.sect_off
9ff913ba
DE
3261 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3262 &dwarf2_per_objfile->abbrev))
3263 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3264 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3265 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3266 filename);
3267
3268 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3269 avoid potential 32-bit overflow. */
b64f50a1 3270 if (((unsigned long) header->offset.sect_off
9ff913ba
DE
3271 + header->length + header->initial_length_size)
3272 > section->size)
3273 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3274 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3275 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3276 filename);
3277}
3278
3279/* Read in a CU/TU header and perform some basic error checking.
3280 The contents of the header are stored in HEADER.
3281 The result is a pointer to the start of the first DIE. */
adabb602 3282
fe1b8b76 3283static gdb_byte *
9ff913ba
DE
3284read_and_check_comp_unit_head (struct comp_unit_head *header,
3285 struct dwarf2_section_info *section,
3286 gdb_byte *info_ptr,
3287 int is_debug_types_section)
72bf9492 3288{
fe1b8b76 3289 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3290 bfd *abfd = section->asection->owner;
72bf9492 3291
b64f50a1 3292 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3293
72bf9492
DJ
3294 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3295
460c1c54
CC
3296 /* If we're reading a type unit, skip over the signature and
3297 type_offset fields. */
b0df02fd 3298 if (is_debug_types_section)
460c1c54
CC
3299 info_ptr += 8 /*signature*/ + header->offset_size;
3300
b64f50a1 3301 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3302
9ff913ba 3303 error_check_comp_unit_head (header, section);
72bf9492
DJ
3304
3305 return info_ptr;
3306}
3307
348e048f
DE
3308/* Read in the types comp unit header information from .debug_types entry at
3309 types_ptr. The result is a pointer to one past the end of the header. */
3310
3311static gdb_byte *
9ff913ba
DE
3312read_and_check_type_unit_head (struct comp_unit_head *header,
3313 struct dwarf2_section_info *section,
3314 gdb_byte *info_ptr,
dee91e82
DE
3315 ULONGEST *signature,
3316 cu_offset *type_offset_in_tu)
348e048f 3317{
9ff913ba
DE
3318 gdb_byte *beg_of_comp_unit = info_ptr;
3319 bfd *abfd = section->asection->owner;
348e048f 3320
b64f50a1 3321 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3322
9ff913ba 3323 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3324
9ff913ba
DE
3325 /* If we're reading a type unit, skip over the signature and
3326 type_offset fields. */
3327 if (signature != NULL)
3328 *signature = read_8_bytes (abfd, info_ptr);
3329 info_ptr += 8;
dee91e82
DE
3330 if (type_offset_in_tu != NULL)
3331 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3332 header->offset_size);
9ff913ba
DE
3333 info_ptr += header->offset_size;
3334
b64f50a1 3335 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3336
9ff913ba
DE
3337 error_check_comp_unit_head (header, section);
3338
3339 return info_ptr;
348e048f
DE
3340}
3341
aaa75496
JB
3342/* Allocate a new partial symtab for file named NAME and mark this new
3343 partial symtab as being an include of PST. */
3344
3345static void
3346dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3347 struct objfile *objfile)
3348{
3349 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3350
3351 subpst->section_offsets = pst->section_offsets;
3352 subpst->textlow = 0;
3353 subpst->texthigh = 0;
3354
3355 subpst->dependencies = (struct partial_symtab **)
3356 obstack_alloc (&objfile->objfile_obstack,
3357 sizeof (struct partial_symtab *));
3358 subpst->dependencies[0] = pst;
3359 subpst->number_of_dependencies = 1;
3360
3361 subpst->globals_offset = 0;
3362 subpst->n_global_syms = 0;
3363 subpst->statics_offset = 0;
3364 subpst->n_static_syms = 0;
3365 subpst->symtab = NULL;
3366 subpst->read_symtab = pst->read_symtab;
3367 subpst->readin = 0;
3368
3369 /* No private part is necessary for include psymtabs. This property
3370 can be used to differentiate between such include psymtabs and
10b3939b 3371 the regular ones. */
58a9656e 3372 subpst->read_symtab_private = NULL;
aaa75496
JB
3373}
3374
3375/* Read the Line Number Program data and extract the list of files
3376 included by the source file represented by PST. Build an include
d85a05f0 3377 partial symtab for each of these included files. */
aaa75496
JB
3378
3379static void
3380dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
3381 struct die_info *die,
3382 struct partial_symtab *pst)
aaa75496 3383{
d85a05f0
DJ
3384 struct line_header *lh = NULL;
3385 struct attribute *attr;
aaa75496 3386
d85a05f0
DJ
3387 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3388 if (attr)
3019eac3 3389 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
3390 if (lh == NULL)
3391 return; /* No linetable, so no includes. */
3392
c6da4cef 3393 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3394 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3395
3396 free_line_header (lh);
3397}
3398
348e048f 3399static hashval_t
52dc124a 3400hash_signatured_type (const void *item)
348e048f 3401{
52dc124a 3402 const struct signatured_type *sig_type = item;
9a619af0 3403
348e048f 3404 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 3405 return sig_type->signature;
348e048f
DE
3406}
3407
3408static int
52dc124a 3409eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
3410{
3411 const struct signatured_type *lhs = item_lhs;
3412 const struct signatured_type *rhs = item_rhs;
9a619af0 3413
348e048f
DE
3414 return lhs->signature == rhs->signature;
3415}
3416
1fd400ff
TT
3417/* Allocate a hash table for signatured types. */
3418
3419static htab_t
673bfd45 3420allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3421{
3422 return htab_create_alloc_ex (41,
52dc124a
DE
3423 hash_signatured_type,
3424 eq_signatured_type,
1fd400ff
TT
3425 NULL,
3426 &objfile->objfile_obstack,
3427 hashtab_obstack_allocate,
3428 dummy_obstack_deallocate);
3429}
3430
d467dd73 3431/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3432
3433static int
d467dd73 3434add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3435{
3436 struct signatured_type *sigt = *slot;
3437 struct dwarf2_per_cu_data ***datap = datum;
3438
3439 **datap = &sigt->per_cu;
3440 ++*datap;
3441
3442 return 1;
3443}
3444
3019eac3
DE
3445/* Create the hash table of all entries in the .debug_types section.
3446 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3447 The result is a pointer to the hash table or NULL if there are
3448 no types. */
348e048f 3449
3019eac3
DE
3450static htab_t
3451create_debug_types_hash_table (struct dwo_file *dwo_file,
3452 VEC (dwarf2_section_info_def) *types)
348e048f 3453{
3019eac3 3454 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 3455 htab_t types_htab = NULL;
8b70b953
TT
3456 int ix;
3457 struct dwarf2_section_info *section;
348e048f 3458
3019eac3
DE
3459 if (VEC_empty (dwarf2_section_info_def, types))
3460 return NULL;
348e048f 3461
8b70b953 3462 for (ix = 0;
3019eac3 3463 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
3464 ++ix)
3465 {
3019eac3 3466 bfd *abfd;
8b70b953 3467 gdb_byte *info_ptr, *end_ptr;
348e048f 3468
8b70b953
TT
3469 dwarf2_read_section (objfile, section);
3470 info_ptr = section->buffer;
348e048f 3471
8b70b953
TT
3472 if (info_ptr == NULL)
3473 continue;
348e048f 3474
3019eac3
DE
3475 /* We can't set abfd until now because the section may be empty or
3476 not present, in which case section->asection will be NULL. */
3477 abfd = section->asection->owner;
3478
8b70b953 3479 if (types_htab == NULL)
3019eac3
DE
3480 {
3481 if (dwo_file)
3482 types_htab = allocate_dwo_unit_table (objfile);
3483 else
3484 types_htab = allocate_signatured_type_table (objfile);
3485 }
348e048f 3486
8b70b953 3487 if (dwarf2_die_debug)
dee91e82
DE
3488 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3489 bfd_get_filename (abfd));
3490
3491 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3492 because we don't need to read any dies: the signature is in the
3493 header. */
8b70b953
TT
3494
3495 end_ptr = info_ptr + section->size;
3496 while (info_ptr < end_ptr)
3497 {
b64f50a1 3498 sect_offset offset;
3019eac3 3499 cu_offset type_offset_in_tu;
8b70b953 3500 ULONGEST signature;
52dc124a 3501 struct signatured_type *sig_type;
3019eac3 3502 struct dwo_unit *dwo_tu;
8b70b953
TT
3503 void **slot;
3504 gdb_byte *ptr = info_ptr;
9ff913ba 3505 struct comp_unit_head header;
dee91e82 3506 unsigned int length;
348e048f 3507
b64f50a1 3508 offset.sect_off = ptr - section->buffer;
348e048f 3509
8b70b953 3510 /* We need to read the type's signature in order to build the hash
9ff913ba 3511 table, but we don't need anything else just yet. */
348e048f 3512
9ff913ba 3513 ptr = read_and_check_type_unit_head (&header, section, ptr,
3019eac3 3514 &signature, &type_offset_in_tu);
6caca83c 3515
dee91e82
DE
3516 length = header.initial_length_size + header.length;
3517
6caca83c 3518 /* Skip dummy type units. */
dee91e82
DE
3519 if (ptr >= info_ptr + length
3520 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 3521 {
dee91e82 3522 info_ptr += header.initial_length_size + header.length;
6caca83c
CC
3523 continue;
3524 }
8b70b953 3525
3019eac3
DE
3526 if (dwo_file)
3527 {
3528 sig_type = NULL;
3529 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3530 struct dwo_unit);
3531 dwo_tu->dwo_file = dwo_file;
3532 dwo_tu->signature = signature;
3533 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3534 dwo_tu->info_or_types_section = section;
3535 dwo_tu->offset = offset;
3536 dwo_tu->length = length;
3537 }
3538 else
3539 {
3540 /* N.B.: type_offset is not usable if this type uses a DWO file.
3541 The real type_offset is in the DWO file. */
3542 dwo_tu = NULL;
3543 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3544 struct signatured_type);
3545 sig_type->signature = signature;
3546 sig_type->type_offset_in_tu = type_offset_in_tu;
3547 sig_type->per_cu.objfile = objfile;
3548 sig_type->per_cu.is_debug_types = 1;
3549 sig_type->per_cu.info_or_types_section = section;
3550 sig_type->per_cu.offset = offset;
3551 sig_type->per_cu.length = length;
3552 }
8b70b953 3553
3019eac3
DE
3554 slot = htab_find_slot (types_htab,
3555 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3556 INSERT);
8b70b953
TT
3557 gdb_assert (slot != NULL);
3558 if (*slot != NULL)
3559 {
3019eac3
DE
3560 sect_offset dup_offset;
3561
3562 if (dwo_file)
3563 {
3564 const struct dwo_unit *dup_tu = *slot;
3565
3566 dup_offset = dup_tu->offset;
3567 }
3568 else
3569 {
3570 const struct signatured_type *dup_tu = *slot;
3571
3572 dup_offset = dup_tu->per_cu.offset;
3573 }
b3c8eb43 3574
8b70b953
TT
3575 complaint (&symfile_complaints,
3576 _("debug type entry at offset 0x%x is duplicate to the "
3577 "entry at offset 0x%x, signature 0x%s"),
3019eac3 3578 offset.sect_off, dup_offset.sect_off,
8b70b953 3579 phex (signature, sizeof (signature)));
8b70b953 3580 }
3019eac3 3581 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 3582
8b70b953
TT
3583 if (dwarf2_die_debug)
3584 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
3585 offset.sect_off,
3586 phex (signature, sizeof (signature)));
348e048f 3587
dee91e82 3588 info_ptr += length;
8b70b953 3589 }
348e048f
DE
3590 }
3591
3019eac3
DE
3592 return types_htab;
3593}
3594
3595/* Create the hash table of all entries in the .debug_types section,
3596 and initialize all_type_units.
3597 The result is zero if there is an error (e.g. missing .debug_types section),
3598 otherwise non-zero. */
3599
3600static int
3601create_all_type_units (struct objfile *objfile)
3602{
3603 htab_t types_htab;
3604 struct dwarf2_per_cu_data **iter;
3605
3606 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3607 if (types_htab == NULL)
3608 {
3609 dwarf2_per_objfile->signatured_types = NULL;
3610 return 0;
3611 }
3612
348e048f
DE
3613 dwarf2_per_objfile->signatured_types = types_htab;
3614
d467dd73
DE
3615 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3616 dwarf2_per_objfile->all_type_units
1fd400ff 3617 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3618 dwarf2_per_objfile->n_type_units
1fd400ff 3619 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3620 iter = &dwarf2_per_objfile->all_type_units[0];
3621 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3622 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3623 == dwarf2_per_objfile->n_type_units);
1fd400ff 3624
348e048f
DE
3625 return 1;
3626}
3627
380bca97 3628/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 3629 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
3630
3631static struct signatured_type *
e319fa28 3632lookup_signatured_type (ULONGEST sig)
348e048f
DE
3633{
3634 struct signatured_type find_entry, *entry;
3635
3636 if (dwarf2_per_objfile->signatured_types == NULL)
3637 {
3638 complaint (&symfile_complaints,
55f1336d 3639 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 3640 return NULL;
348e048f
DE
3641 }
3642
3643 find_entry.signature = sig;
3644 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3645 return entry;
3646}
3647
d85a05f0
DJ
3648/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3649
3650static void
3651init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 3652 struct dwarf2_cu *cu,
3019eac3
DE
3653 struct dwarf2_section_info *section,
3654 struct dwo_file *dwo_file)
d85a05f0 3655{
fceca515 3656 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 3657 reader->abfd = section->asection->owner;
d85a05f0 3658 reader->cu = cu;
3019eac3 3659 reader->dwo_file = dwo_file;
dee91e82
DE
3660 reader->die_section = section;
3661 reader->buffer = section->buffer;
f664829e 3662 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
3663}
3664
3665/* Find the base address of the compilation unit for range lists and
3666 location lists. It will normally be specified by DW_AT_low_pc.
3667 In DWARF-3 draft 4, the base address could be overridden by
3668 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3669 compilation units with discontinuous ranges. */
3670
3671static void
3672dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3673{
3674 struct attribute *attr;
3675
3676 cu->base_known = 0;
3677 cu->base_address = 0;
3678
3679 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3680 if (attr)
3681 {
3682 cu->base_address = DW_ADDR (attr);
3683 cu->base_known = 1;
3684 }
3685 else
3686 {
3687 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3688 if (attr)
3689 {
3690 cu->base_address = DW_ADDR (attr);
3691 cu->base_known = 1;
3692 }
3693 }
3694}
3695
fd820528 3696/* Initialize a CU (or TU) and read its DIEs.
3019eac3 3697 If the CU defers to a DWO file, read the DWO file as well.
dee91e82
DE
3698
3699 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3700 Otherwise, a new CU is allocated with xmalloc.
3701
3702 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3703 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3704
3705 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 3706 linker) then DIE_READER_FUNC will not get called. */
aaa75496 3707
70221824 3708static void
fd820528
DE
3709init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3710 int use_existing_cu, int keep,
3711 die_reader_func_ftype *die_reader_func,
3712 void *data)
c906108c 3713{
dee91e82 3714 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
3715 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3716 bfd *abfd = section->asection->owner;
dee91e82
DE
3717 struct dwarf2_cu *cu;
3718 gdb_byte *begin_info_ptr, *info_ptr;
3719 struct die_reader_specs reader;
d85a05f0 3720 struct die_info *comp_unit_die;
dee91e82 3721 int has_children;
d85a05f0 3722 struct attribute *attr;
dee91e82
DE
3723 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3724 struct signatured_type *sig_type = NULL;
c906108c 3725
dee91e82
DE
3726 if (use_existing_cu)
3727 gdb_assert (keep);
23745b47 3728
dee91e82
DE
3729 cleanups = make_cleanup (null_cleanup, NULL);
3730
3731 /* This is cheap if the section is already read in. */
3732 dwarf2_read_section (objfile, section);
3733
3734 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3735
3736 if (use_existing_cu && this_cu->cu != NULL)
3737 {
3738 cu = this_cu->cu;
3739 info_ptr += cu->header.first_die_offset.cu_off;
3740 }
3741 else
3742 {
3743 /* If !use_existing_cu, this_cu->cu must be NULL. */
3744 gdb_assert (this_cu->cu == NULL);
3745
3746 cu = xmalloc (sizeof (*cu));
3747 init_one_comp_unit (cu, this_cu);
3748
3749 /* If an error occurs while loading, release our storage. */
3750 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3751
3019eac3 3752 if (this_cu->is_debug_types)
dee91e82
DE
3753 {
3754 ULONGEST signature;
3755
3756 info_ptr = read_and_check_type_unit_head (&cu->header,
3757 section, info_ptr,
3758 &signature, NULL);
3759
3760 /* There's no way to get from PER_CU to its containing
3761 struct signatured_type.
3762 But we have the signature so we can use that. */
3763 sig_type = lookup_signatured_type (signature);
3764 /* We've already scanned all the signatured types,
3765 this must succeed. */
3766 gdb_assert (sig_type != NULL);
3767 gdb_assert (&sig_type->per_cu == this_cu);
3768 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3769
3770 /* LENGTH has not been set yet for type units. */
3771 this_cu->length = cu->header.length + cu->header.initial_length_size;
3019eac3
DE
3772
3773 /* Establish the type offset that can be used to lookup the type. */
3774 sig_type->type_offset_in_section.sect_off =
3775 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
3776 }
3777 else
3778 {
3779 info_ptr = read_and_check_comp_unit_head (&cu->header,
3780 section, info_ptr, 0);
3781
3782 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3783 gdb_assert (this_cu->length
3784 == cu->header.length + cu->header.initial_length_size);
3785 }
3786 }
10b3939b 3787
6caca83c 3788 /* Skip dummy compilation units. */
dee91e82 3789 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
3790 || peek_abbrev_code (abfd, info_ptr) == 0)
3791 {
dee91e82 3792 do_cleanups (cleanups);
21b2bd31 3793 return;
6caca83c
CC
3794 }
3795
93311388 3796 /* Read the abbrevs for this compilation unit into a table. */
dee91e82
DE
3797 if (cu->dwarf2_abbrevs == NULL)
3798 {
3799 dwarf2_read_abbrevs (cu, &dwarf2_per_objfile->abbrev);
3800 make_cleanup (dwarf2_free_abbrev_table, cu);
3801 }
af703f96 3802
dee91e82 3803 /* Read the top level CU/TU die. */
3019eac3 3804 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 3805 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 3806
3019eac3
DE
3807 /* If we have a DWO stub, process it and then read in the DWO file.
3808 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3809 a DWO CU, that this test will fail. */
3810 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3811 if (attr)
3812 {
3813 char *dwo_name = DW_STRING (attr);
3814 const char *comp_dir;
3815 struct dwo_unit *dwo_unit;
3816 ULONGEST signature; /* Or dwo_id. */
3817 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3818 int i,num_extra_attrs;
3819
3820 if (has_children)
3821 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3822 " has children (offset 0x%x) [in module %s]"),
3823 this_cu->offset.sect_off, bfd_get_filename (abfd));
3824
3825 /* These attributes aren't processed until later:
3826 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3827 However, the attribute is found in the stub which we won't have later.
3828 In order to not impose this complication on the rest of the code,
3829 we read them here and copy them to the DWO CU/TU die. */
3830 stmt_list = low_pc = high_pc = ranges = NULL;
3831
3832 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3833 DWO file. */
3834 if (! this_cu->is_debug_types)
3835 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3836 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3837 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3838 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3839
3840 /* There should be a DW_AT_addr_base attribute here (if needed).
3841 We need the value before we can process DW_FORM_GNU_addr_index. */
3842 cu->addr_base = 0;
3843 cu->have_addr_base = 0;
3844 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
3845 if (attr)
3846 {
3847 cu->addr_base = DW_UNSND (attr);
3848 cu->have_addr_base = 1;
3849 }
3850
3851 if (this_cu->is_debug_types)
3852 {
3853 gdb_assert (sig_type != NULL);
3854 signature = sig_type->signature;
3855 }
3856 else
3857 {
3858 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3859 if (! attr)
3860 error (_("Dwarf Error: missing dwo_id [in module %s]"),
3861 dwo_name);
3862 signature = DW_UNSND (attr);
3863 }
3864
3865 /* We may need the comp_dir in order to find the DWO file. */
3866 comp_dir = NULL;
3867 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3868 if (attr)
3869 comp_dir = DW_STRING (attr);
3870
3871 if (this_cu->is_debug_types)
3872 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
3873 else
3874 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
3875 signature);
3876
3877 if (dwo_unit == NULL)
3878 {
3879 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
3880 " with ID %s [in module %s]"),
3881 this_cu->offset.sect_off,
3882 phex (signature, sizeof (signature)),
3883 objfile->name);
3884 }
3885
3886 /* Set up for reading the DWO CU/TU. */
3887 cu->dwo_unit = dwo_unit;
3888 section = dwo_unit->info_or_types_section;
3889 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
3890 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
3891
3892 if (this_cu->is_debug_types)
3893 {
3894 ULONGEST signature;
3895
3896 info_ptr = read_and_check_type_unit_head (&cu->header,
3897 section, info_ptr,
3898 &signature, NULL);
3899 gdb_assert (sig_type->signature == signature);
3900 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3901 gdb_assert (dwo_unit->length
3902 == cu->header.length + cu->header.initial_length_size);
3903
3904 /* Establish the type offset that can be used to lookup the type.
3905 For DWO files, we don't know it until now. */
3906 sig_type->type_offset_in_section.sect_off =
3907 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
3908 }
3909 else
3910 {
3911 info_ptr = read_and_check_comp_unit_head (&cu->header,
3912 section, info_ptr, 0);
3913 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3914 gdb_assert (dwo_unit->length
3915 == cu->header.length + cu->header.initial_length_size);
3916 }
3917
3918 /* Discard the original CU's abbrev table, and read the DWO's. */
3919 dwarf2_free_abbrev_table (cu);
3920 dwarf2_read_abbrevs (cu, &dwo_unit->dwo_file->sections.abbrev);
3921
3922 /* Read in the die, but leave space to copy over the attributes
3923 from the stub. This has the benefit of simplifying the rest of
3924 the code - all the real work is done here. */
3925 num_extra_attrs = ((stmt_list != NULL)
3926 + (low_pc != NULL)
3927 + (high_pc != NULL)
3928 + (ranges != NULL));
3929 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
3930 &has_children, num_extra_attrs);
3931
3932 /* Copy over the attributes from the stub to the DWO die. */
3933 i = comp_unit_die->num_attrs;
3934 if (stmt_list != NULL)
3935 comp_unit_die->attrs[i++] = *stmt_list;
3936 if (low_pc != NULL)
3937 comp_unit_die->attrs[i++] = *low_pc;
3938 if (high_pc != NULL)
3939 comp_unit_die->attrs[i++] = *high_pc;
3940 if (ranges != NULL)
3941 comp_unit_die->attrs[i++] = *ranges;
3942 comp_unit_die->num_attrs += num_extra_attrs;
3943
3944 /* Skip dummy compilation units. */
3945 if (info_ptr >= begin_info_ptr + dwo_unit->length
3946 || peek_abbrev_code (abfd, info_ptr) == 0)
3947 {
3948 do_cleanups (cleanups);
3949 return;
3950 }
3951 }
3952
dee91e82
DE
3953 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3954
3955 if (free_cu_cleanup != NULL)
348e048f 3956 {
dee91e82
DE
3957 if (keep)
3958 {
3959 /* We've successfully allocated this compilation unit. Let our
3960 caller clean it up when finished with it. */
3961 discard_cleanups (free_cu_cleanup);
3962
3963 /* We can only discard free_cu_cleanup and all subsequent cleanups.
3964 So we have to manually free the abbrev table. */
3965 dwarf2_free_abbrev_table (cu);
3966
3967 /* Link this CU into read_in_chain. */
3968 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3969 dwarf2_per_objfile->read_in_chain = this_cu;
3970 }
3971 else
3972 do_cleanups (free_cu_cleanup);
348e048f 3973 }
dee91e82
DE
3974
3975 do_cleanups (cleanups);
3976}
3977
3019eac3
DE
3978/* Read CU/TU THIS_CU in section SECTION,
3979 but do not follow DW_AT_GNU_dwo_name if present.
3980 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
3981 have already done the lookup to find the DWO file).
dee91e82
DE
3982
3983 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 3984 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
3985
3986 We fill in THIS_CU->length.
3987
3988 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3989 linker) then DIE_READER_FUNC will not get called.
3990
3991 THIS_CU->cu is always freed when done.
3019eac3
DE
3992 This is done in order to not leave THIS_CU->cu in a state where we have
3993 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
3994
3995static void
3996init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3997 struct dwarf2_section_info *abbrev_section,
3019eac3 3998 struct dwo_file *dwo_file,
dee91e82
DE
3999 die_reader_func_ftype *die_reader_func,
4000 void *data)
4001{
4002 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4003 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4004 bfd *abfd = section->asection->owner;
dee91e82
DE
4005 struct dwarf2_cu cu;
4006 gdb_byte *begin_info_ptr, *info_ptr;
4007 struct die_reader_specs reader;
4008 struct cleanup *cleanups;
4009 struct die_info *comp_unit_die;
4010 int has_children;
4011
4012 gdb_assert (this_cu->cu == NULL);
4013
dee91e82
DE
4014 /* This is cheap if the section is already read in. */
4015 dwarf2_read_section (objfile, section);
4016
4017 init_one_comp_unit (&cu, this_cu);
4018
4019 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4020
4021 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4022 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3019eac3 4023 this_cu->is_debug_types);
dee91e82
DE
4024
4025 this_cu->length = cu.header.length + cu.header.initial_length_size;
4026
4027 /* Skip dummy compilation units. */
4028 if (info_ptr >= begin_info_ptr + this_cu->length
4029 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4030 {
dee91e82 4031 do_cleanups (cleanups);
21b2bd31 4032 return;
93311388 4033 }
72bf9492 4034
dee91e82
DE
4035 dwarf2_read_abbrevs (&cu, abbrev_section);
4036 make_cleanup (dwarf2_free_abbrev_table, &cu);
4037
3019eac3 4038 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4039 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4040
4041 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4042
4043 do_cleanups (cleanups);
4044}
4045
3019eac3
DE
4046/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4047 does not lookup the specified DWO file.
4048 This cannot be used to read DWO files.
dee91e82
DE
4049
4050 THIS_CU->cu is always freed when done.
3019eac3
DE
4051 This is done in order to not leave THIS_CU->cu in a state where we have
4052 to care whether it refers to the "main" CU or the DWO CU.
4053 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4054
4055static void
4056init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4057 die_reader_func_ftype *die_reader_func,
4058 void *data)
4059{
4060 init_cutu_and_read_dies_no_follow (this_cu,
4061 &dwarf2_per_objfile->abbrev,
3019eac3 4062 NULL,
dee91e82
DE
4063 die_reader_func, data);
4064}
4065
4066/* die_reader_func for process_psymtab_comp_unit. */
4067
4068static void
4069process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4070 gdb_byte *info_ptr,
4071 struct die_info *comp_unit_die,
4072 int has_children,
4073 void *data)
4074{
4075 struct dwarf2_cu *cu = reader->cu;
4076 struct objfile *objfile = cu->objfile;
4077 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4078 struct attribute *attr;
4079 CORE_ADDR baseaddr;
4080 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4081 struct partial_symtab *pst;
4082 int has_pc_info;
4083 const char *filename;
95554aad 4084 int *want_partial_unit_ptr = data;
dee91e82 4085
95554aad
TT
4086 if (comp_unit_die->tag == DW_TAG_partial_unit
4087 && (want_partial_unit_ptr == NULL
4088 || !*want_partial_unit_ptr))
dee91e82
DE
4089 return;
4090
95554aad 4091 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4092
4093 cu->list_in_scope = &file_symbols;
c906108c 4094
93311388 4095 /* Allocate a new partial symbol table structure. */
dee91e82 4096 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4097 if (attr == NULL || !DW_STRING (attr))
4098 filename = "";
4099 else
4100 filename = DW_STRING (attr);
93311388 4101 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 4102 filename,
93311388
DE
4103 /* TEXTLOW and TEXTHIGH are set below. */
4104 0,
4105 objfile->global_psymbols.next,
4106 objfile->static_psymbols.next);
9750bca9 4107 pst->psymtabs_addrmap_supported = 1;
72bf9492 4108
dee91e82 4109 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4110 if (attr != NULL)
4111 pst->dirname = DW_STRING (attr);
72bf9492 4112
dee91e82 4113 pst->read_symtab_private = per_cu;
72bf9492 4114
93311388 4115 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4116
0963b4bd 4117 /* Store the function that reads in the rest of the symbol table. */
93311388 4118 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 4119
dee91e82 4120 per_cu->v.psymtab = pst;
c906108c 4121
dee91e82 4122 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4123
93311388
DE
4124 /* Possibly set the default values of LOWPC and HIGHPC from
4125 `DW_AT_ranges'. */
d85a05f0 4126 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4127 &best_highpc, cu, pst);
d85a05f0 4128 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4129 /* Store the contiguous range if it is not empty; it can be empty for
4130 CUs with no code. */
4131 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4132 best_lowpc + baseaddr,
4133 best_highpc + baseaddr - 1, pst);
93311388
DE
4134
4135 /* Check if comp unit has_children.
4136 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4137 If not, there's no more debug_info for this comp unit. */
d85a05f0 4138 if (has_children)
93311388
DE
4139 {
4140 struct partial_die_info *first_die;
4141 CORE_ADDR lowpc, highpc;
31ffec48 4142
93311388
DE
4143 lowpc = ((CORE_ADDR) -1);
4144 highpc = ((CORE_ADDR) 0);
c906108c 4145
dee91e82 4146 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4147
93311388 4148 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4149 ! has_pc_info, cu);
57c22c6c 4150
93311388
DE
4151 /* If we didn't find a lowpc, set it to highpc to avoid
4152 complaints from `maint check'. */
4153 if (lowpc == ((CORE_ADDR) -1))
4154 lowpc = highpc;
10b3939b 4155
93311388
DE
4156 /* If the compilation unit didn't have an explicit address range,
4157 then use the information extracted from its child dies. */
d85a05f0 4158 if (! has_pc_info)
93311388 4159 {
d85a05f0
DJ
4160 best_lowpc = lowpc;
4161 best_highpc = highpc;
93311388
DE
4162 }
4163 }
d85a05f0
DJ
4164 pst->textlow = best_lowpc + baseaddr;
4165 pst->texthigh = best_highpc + baseaddr;
c906108c 4166
93311388
DE
4167 pst->n_global_syms = objfile->global_psymbols.next -
4168 (objfile->global_psymbols.list + pst->globals_offset);
4169 pst->n_static_syms = objfile->static_psymbols.next -
4170 (objfile->static_psymbols.list + pst->statics_offset);
4171 sort_pst_symbols (pst);
c906108c 4172
95554aad
TT
4173 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4174 {
4175 int i;
4176 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4177 struct dwarf2_per_cu_data *iter;
4178
4179 /* Fill in 'dependencies' here; we fill in 'users' in a
4180 post-pass. */
4181 pst->number_of_dependencies = len;
4182 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4183 len * sizeof (struct symtab *));
4184 for (i = 0;
4185 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4186 i, iter);
4187 ++i)
4188 pst->dependencies[i] = iter->v.psymtab;
4189
4190 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4191 }
4192
3019eac3 4193 if (per_cu->is_debug_types)
348e048f
DE
4194 {
4195 /* It's not clear we want to do anything with stmt lists here.
4196 Waiting to see what gcc ultimately does. */
4197 }
d85a05f0 4198 else
93311388
DE
4199 {
4200 /* Get the list of files included in the current compilation unit,
4201 and build a psymtab for each of them. */
dee91e82 4202 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
93311388 4203 }
dee91e82 4204}
ae038cb0 4205
dee91e82
DE
4206/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4207 Process compilation unit THIS_CU for a psymtab. */
4208
4209static void
95554aad
TT
4210process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4211 int want_partial_unit)
dee91e82
DE
4212{
4213 /* If this compilation unit was already read in, free the
4214 cached copy in order to read it in again. This is
4215 necessary because we skipped some symbols when we first
4216 read in the compilation unit (see load_partial_dies).
4217 This problem could be avoided, but the benefit is unclear. */
4218 if (this_cu->cu != NULL)
4219 free_one_cached_comp_unit (this_cu);
4220
3019eac3 4221 gdb_assert (! this_cu->is_debug_types);
fd820528 4222 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
95554aad 4223 &want_partial_unit);
dee91e82
DE
4224
4225 /* Age out any secondary CUs. */
4226 age_cached_comp_units ();
93311388 4227}
ff013f42 4228
348e048f
DE
4229/* Traversal function for htab_traverse_noresize.
4230 Process one .debug_types comp-unit. */
4231
4232static int
dee91e82 4233process_psymtab_type_unit (void **slot, void *info)
348e048f 4234{
dee91e82
DE
4235 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4236 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 4237
fd820528 4238 gdb_assert (per_cu->is_debug_types);
a0f42c21 4239 gdb_assert (info == NULL);
348e048f 4240
dee91e82
DE
4241 /* If this compilation unit was already read in, free the
4242 cached copy in order to read it in again. This is
4243 necessary because we skipped some symbols when we first
4244 read in the compilation unit (see load_partial_dies).
4245 This problem could be avoided, but the benefit is unclear. */
4246 if (per_cu->cu != NULL)
4247 free_one_cached_comp_unit (per_cu);
4248
fd820528
DE
4249 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4250 NULL);
dee91e82
DE
4251
4252 /* Age out any secondary CUs. */
4253 age_cached_comp_units ();
348e048f
DE
4254
4255 return 1;
4256}
4257
4258/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4259 Build partial symbol tables for the .debug_types comp-units. */
4260
4261static void
4262build_type_psymtabs (struct objfile *objfile)
4263{
0e50663e 4264 if (! create_all_type_units (objfile))
348e048f
DE
4265 return;
4266
4267 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
dee91e82 4268 process_psymtab_type_unit, NULL);
348e048f
DE
4269}
4270
60606b2c
TT
4271/* A cleanup function that clears objfile's psymtabs_addrmap field. */
4272
4273static void
4274psymtabs_addrmap_cleanup (void *o)
4275{
4276 struct objfile *objfile = o;
ec61707d 4277
60606b2c
TT
4278 objfile->psymtabs_addrmap = NULL;
4279}
4280
95554aad
TT
4281/* Compute the 'user' field for each psymtab in OBJFILE. */
4282
4283static void
4284set_partial_user (struct objfile *objfile)
4285{
4286 int i;
4287
4288 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4289 {
4290 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4291 struct partial_symtab *pst = per_cu->v.psymtab;
4292 int j;
4293
4294 for (j = 0; j < pst->number_of_dependencies; ++j)
4295 {
4296 /* Set the 'user' field only if it is not already set. */
4297 if (pst->dependencies[j]->user == NULL)
4298 pst->dependencies[j]->user = pst;
4299 }
4300 }
4301}
4302
93311388
DE
4303/* Build the partial symbol table by doing a quick pass through the
4304 .debug_info and .debug_abbrev sections. */
72bf9492 4305
93311388 4306static void
c67a9c90 4307dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 4308{
60606b2c
TT
4309 struct cleanup *back_to, *addrmap_cleanup;
4310 struct obstack temp_obstack;
21b2bd31 4311 int i;
93311388 4312
98bfdba5
PA
4313 dwarf2_per_objfile->reading_partial_symbols = 1;
4314
be391dca 4315 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 4316
93311388
DE
4317 /* Any cached compilation units will be linked by the per-objfile
4318 read_in_chain. Make sure to free them when we're done. */
4319 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 4320
348e048f
DE
4321 build_type_psymtabs (objfile);
4322
93311388 4323 create_all_comp_units (objfile);
c906108c 4324
60606b2c
TT
4325 /* Create a temporary address map on a temporary obstack. We later
4326 copy this to the final obstack. */
4327 obstack_init (&temp_obstack);
4328 make_cleanup_obstack_free (&temp_obstack);
4329 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4330 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 4331
21b2bd31 4332 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 4333 {
21b2bd31 4334 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 4335
95554aad 4336 process_psymtab_comp_unit (per_cu, 0);
c906108c 4337 }
ff013f42 4338
95554aad
TT
4339 set_partial_user (objfile);
4340
ff013f42
JK
4341 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4342 &objfile->objfile_obstack);
60606b2c 4343 discard_cleanups (addrmap_cleanup);
ff013f42 4344
ae038cb0
DJ
4345 do_cleanups (back_to);
4346}
4347
3019eac3 4348/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
4349
4350static void
dee91e82
DE
4351load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4352 gdb_byte *info_ptr,
4353 struct die_info *comp_unit_die,
4354 int has_children,
4355 void *data)
ae038cb0 4356{
dee91e82 4357 struct dwarf2_cu *cu = reader->cu;
ae038cb0 4358
95554aad 4359 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 4360
ae038cb0
DJ
4361 /* Check if comp unit has_children.
4362 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4363 If not, there's no more debug_info for this comp unit. */
d85a05f0 4364 if (has_children)
dee91e82
DE
4365 load_partial_dies (reader, info_ptr, 0);
4366}
98bfdba5 4367
dee91e82
DE
4368/* Load the partial DIEs for a secondary CU into memory.
4369 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 4370
dee91e82
DE
4371static void
4372load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4373{
fd820528 4374 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
4375}
4376
9cdd5dbd
DE
4377/* Create a list of all compilation units in OBJFILE.
4378 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
4379
4380static void
4381create_all_comp_units (struct objfile *objfile)
4382{
4383 int n_allocated;
4384 int n_comp_units;
4385 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
4386 gdb_byte *info_ptr;
4387
4388 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4389 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4390
4391 n_comp_units = 0;
4392 n_allocated = 10;
4393 all_comp_units = xmalloc (n_allocated
4394 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 4395
3e43a32a
MS
4396 while (info_ptr < dwarf2_per_objfile->info.buffer
4397 + dwarf2_per_objfile->info.size)
ae038cb0 4398 {
c764a876 4399 unsigned int length, initial_length_size;
ae038cb0 4400 struct dwarf2_per_cu_data *this_cu;
b64f50a1 4401 sect_offset offset;
ae038cb0 4402
b64f50a1 4403 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4404
4405 /* Read just enough information to find out where the next
4406 compilation unit is. */
c764a876
DE
4407 length = read_initial_length (objfile->obfd, info_ptr,
4408 &initial_length_size);
ae038cb0
DJ
4409
4410 /* Save the compilation unit for later lookup. */
4411 this_cu = obstack_alloc (&objfile->objfile_obstack,
4412 sizeof (struct dwarf2_per_cu_data));
4413 memset (this_cu, 0, sizeof (*this_cu));
4414 this_cu->offset = offset;
c764a876 4415 this_cu->length = length + initial_length_size;
9291a0cd 4416 this_cu->objfile = objfile;
3019eac3 4417 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
ae038cb0
DJ
4418
4419 if (n_comp_units == n_allocated)
4420 {
4421 n_allocated *= 2;
4422 all_comp_units = xrealloc (all_comp_units,
4423 n_allocated
4424 * sizeof (struct dwarf2_per_cu_data *));
4425 }
4426 all_comp_units[n_comp_units++] = this_cu;
4427
4428 info_ptr = info_ptr + this_cu->length;
4429 }
4430
4431 dwarf2_per_objfile->all_comp_units
4432 = obstack_alloc (&objfile->objfile_obstack,
4433 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4434 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4435 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4436 xfree (all_comp_units);
4437 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
4438}
4439
5734ee8b
DJ
4440/* Process all loaded DIEs for compilation unit CU, starting at
4441 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4442 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4443 DW_AT_ranges). If NEED_PC is set, then this function will set
4444 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4445 and record the covered ranges in the addrmap. */
c906108c 4446
72bf9492
DJ
4447static void
4448scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 4449 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 4450{
72bf9492 4451 struct partial_die_info *pdi;
c906108c 4452
91c24f0a
DC
4453 /* Now, march along the PDI's, descending into ones which have
4454 interesting children but skipping the children of the other ones,
4455 until we reach the end of the compilation unit. */
c906108c 4456
72bf9492 4457 pdi = first_die;
91c24f0a 4458
72bf9492
DJ
4459 while (pdi != NULL)
4460 {
4461 fixup_partial_die (pdi, cu);
c906108c 4462
f55ee35c 4463 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
4464 children, so we need to look at them. Ditto for anonymous
4465 enums. */
933c6fe4 4466
72bf9492 4467 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
4468 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4469 || pdi->tag == DW_TAG_imported_unit)
c906108c 4470 {
72bf9492 4471 switch (pdi->tag)
c906108c
SS
4472 {
4473 case DW_TAG_subprogram:
5734ee8b 4474 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 4475 break;
72929c62 4476 case DW_TAG_constant:
c906108c
SS
4477 case DW_TAG_variable:
4478 case DW_TAG_typedef:
91c24f0a 4479 case DW_TAG_union_type:
72bf9492 4480 if (!pdi->is_declaration)
63d06c5c 4481 {
72bf9492 4482 add_partial_symbol (pdi, cu);
63d06c5c
DC
4483 }
4484 break;
c906108c 4485 case DW_TAG_class_type:
680b30c7 4486 case DW_TAG_interface_type:
c906108c 4487 case DW_TAG_structure_type:
72bf9492 4488 if (!pdi->is_declaration)
c906108c 4489 {
72bf9492 4490 add_partial_symbol (pdi, cu);
c906108c
SS
4491 }
4492 break;
91c24f0a 4493 case DW_TAG_enumeration_type:
72bf9492
DJ
4494 if (!pdi->is_declaration)
4495 add_partial_enumeration (pdi, cu);
c906108c
SS
4496 break;
4497 case DW_TAG_base_type:
a02abb62 4498 case DW_TAG_subrange_type:
c906108c 4499 /* File scope base type definitions are added to the partial
c5aa993b 4500 symbol table. */
72bf9492 4501 add_partial_symbol (pdi, cu);
c906108c 4502 break;
d9fa45fe 4503 case DW_TAG_namespace:
5734ee8b 4504 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 4505 break;
5d7cb8df
JK
4506 case DW_TAG_module:
4507 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4508 break;
95554aad
TT
4509 case DW_TAG_imported_unit:
4510 {
4511 struct dwarf2_per_cu_data *per_cu;
4512
4513 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4514 cu->objfile);
4515
4516 /* Go read the partial unit, if needed. */
4517 if (per_cu->v.psymtab == NULL)
4518 process_psymtab_comp_unit (per_cu, 1);
4519
4520 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4521 per_cu);
4522 }
4523 break;
c906108c
SS
4524 default:
4525 break;
4526 }
4527 }
4528
72bf9492
DJ
4529 /* If the die has a sibling, skip to the sibling. */
4530
4531 pdi = pdi->die_sibling;
4532 }
4533}
4534
4535/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 4536
72bf9492 4537 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
4538 name is concatenated with "::" and the partial DIE's name. For
4539 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
4540 Enumerators are an exception; they use the scope of their parent
4541 enumeration type, i.e. the name of the enumeration type is not
4542 prepended to the enumerator.
91c24f0a 4543
72bf9492
DJ
4544 There are two complexities. One is DW_AT_specification; in this
4545 case "parent" means the parent of the target of the specification,
4546 instead of the direct parent of the DIE. The other is compilers
4547 which do not emit DW_TAG_namespace; in this case we try to guess
4548 the fully qualified name of structure types from their members'
4549 linkage names. This must be done using the DIE's children rather
4550 than the children of any DW_AT_specification target. We only need
4551 to do this for structures at the top level, i.e. if the target of
4552 any DW_AT_specification (if any; otherwise the DIE itself) does not
4553 have a parent. */
4554
4555/* Compute the scope prefix associated with PDI's parent, in
4556 compilation unit CU. The result will be allocated on CU's
4557 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4558 field. NULL is returned if no prefix is necessary. */
4559static char *
4560partial_die_parent_scope (struct partial_die_info *pdi,
4561 struct dwarf2_cu *cu)
4562{
4563 char *grandparent_scope;
4564 struct partial_die_info *parent, *real_pdi;
91c24f0a 4565
72bf9492
DJ
4566 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4567 then this means the parent of the specification DIE. */
4568
4569 real_pdi = pdi;
72bf9492 4570 while (real_pdi->has_specification)
10b3939b 4571 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
4572
4573 parent = real_pdi->die_parent;
4574 if (parent == NULL)
4575 return NULL;
4576
4577 if (parent->scope_set)
4578 return parent->scope;
4579
4580 fixup_partial_die (parent, cu);
4581
10b3939b 4582 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 4583
acebe513
UW
4584 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4585 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4586 Work around this problem here. */
4587 if (cu->language == language_cplus
6e70227d 4588 && parent->tag == DW_TAG_namespace
acebe513
UW
4589 && strcmp (parent->name, "::") == 0
4590 && grandparent_scope == NULL)
4591 {
4592 parent->scope = NULL;
4593 parent->scope_set = 1;
4594 return NULL;
4595 }
4596
9c6c53f7
SA
4597 if (pdi->tag == DW_TAG_enumerator)
4598 /* Enumerators should not get the name of the enumeration as a prefix. */
4599 parent->scope = grandparent_scope;
4600 else if (parent->tag == DW_TAG_namespace
f55ee35c 4601 || parent->tag == DW_TAG_module
72bf9492
DJ
4602 || parent->tag == DW_TAG_structure_type
4603 || parent->tag == DW_TAG_class_type
680b30c7 4604 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
4605 || parent->tag == DW_TAG_union_type
4606 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
4607 {
4608 if (grandparent_scope == NULL)
4609 parent->scope = parent->name;
4610 else
3e43a32a
MS
4611 parent->scope = typename_concat (&cu->comp_unit_obstack,
4612 grandparent_scope,
f55ee35c 4613 parent->name, 0, cu);
72bf9492 4614 }
72bf9492
DJ
4615 else
4616 {
4617 /* FIXME drow/2004-04-01: What should we be doing with
4618 function-local names? For partial symbols, we should probably be
4619 ignoring them. */
4620 complaint (&symfile_complaints,
e2e0b3e5 4621 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 4622 parent->tag, pdi->offset.sect_off);
72bf9492 4623 parent->scope = grandparent_scope;
c906108c
SS
4624 }
4625
72bf9492
DJ
4626 parent->scope_set = 1;
4627 return parent->scope;
4628}
4629
4630/* Return the fully scoped name associated with PDI, from compilation unit
4631 CU. The result will be allocated with malloc. */
4568ecf9 4632
72bf9492
DJ
4633static char *
4634partial_die_full_name (struct partial_die_info *pdi,
4635 struct dwarf2_cu *cu)
4636{
4637 char *parent_scope;
4638
98bfdba5
PA
4639 /* If this is a template instantiation, we can not work out the
4640 template arguments from partial DIEs. So, unfortunately, we have
4641 to go through the full DIEs. At least any work we do building
4642 types here will be reused if full symbols are loaded later. */
4643 if (pdi->has_template_arguments)
4644 {
4645 fixup_partial_die (pdi, cu);
4646
4647 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4648 {
4649 struct die_info *die;
4650 struct attribute attr;
4651 struct dwarf2_cu *ref_cu = cu;
4652
b64f50a1 4653 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
4654 attr.name = 0;
4655 attr.form = DW_FORM_ref_addr;
4568ecf9 4656 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
4657 die = follow_die_ref (NULL, &attr, &ref_cu);
4658
4659 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4660 }
4661 }
4662
72bf9492
DJ
4663 parent_scope = partial_die_parent_scope (pdi, cu);
4664 if (parent_scope == NULL)
4665 return NULL;
4666 else
f55ee35c 4667 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4668}
4669
4670static void
72bf9492 4671add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4672{
e7c27a73 4673 struct objfile *objfile = cu->objfile;
c906108c 4674 CORE_ADDR addr = 0;
decbce07 4675 char *actual_name = NULL;
e142c38c 4676 CORE_ADDR baseaddr;
72bf9492 4677 int built_actual_name = 0;
e142c38c
DJ
4678
4679 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4680
94af9270
KS
4681 actual_name = partial_die_full_name (pdi, cu);
4682 if (actual_name)
4683 built_actual_name = 1;
63d06c5c 4684
72bf9492
DJ
4685 if (actual_name == NULL)
4686 actual_name = pdi->name;
4687
c906108c
SS
4688 switch (pdi->tag)
4689 {
4690 case DW_TAG_subprogram:
2cfa0c8d 4691 if (pdi->is_external || cu->language == language_ada)
c906108c 4692 {
2cfa0c8d
JB
4693 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4694 of the global scope. But in Ada, we want to be able to access
4695 nested procedures globally. So all Ada subprograms are stored
4696 in the global scope. */
f47fb265 4697 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4698 mst_text, objfile); */
f47fb265
MS
4699 add_psymbol_to_list (actual_name, strlen (actual_name),
4700 built_actual_name,
4701 VAR_DOMAIN, LOC_BLOCK,
4702 &objfile->global_psymbols,
4703 0, pdi->lowpc + baseaddr,
4704 cu->language, objfile);
c906108c
SS
4705 }
4706 else
4707 {
f47fb265 4708 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4709 mst_file_text, objfile); */
f47fb265
MS
4710 add_psymbol_to_list (actual_name, strlen (actual_name),
4711 built_actual_name,
4712 VAR_DOMAIN, LOC_BLOCK,
4713 &objfile->static_psymbols,
4714 0, pdi->lowpc + baseaddr,
4715 cu->language, objfile);
c906108c
SS
4716 }
4717 break;
72929c62
JB
4718 case DW_TAG_constant:
4719 {
4720 struct psymbol_allocation_list *list;
4721
4722 if (pdi->is_external)
4723 list = &objfile->global_psymbols;
4724 else
4725 list = &objfile->static_psymbols;
f47fb265
MS
4726 add_psymbol_to_list (actual_name, strlen (actual_name),
4727 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4728 list, 0, 0, cu->language, objfile);
72929c62
JB
4729 }
4730 break;
c906108c 4731 case DW_TAG_variable:
95554aad
TT
4732 if (pdi->d.locdesc)
4733 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 4734
95554aad 4735 if (pdi->d.locdesc
caac4577
JG
4736 && addr == 0
4737 && !dwarf2_per_objfile->has_section_at_zero)
4738 {
4739 /* A global or static variable may also have been stripped
4740 out by the linker if unused, in which case its address
4741 will be nullified; do not add such variables into partial
4742 symbol table then. */
4743 }
4744 else if (pdi->is_external)
c906108c
SS
4745 {
4746 /* Global Variable.
4747 Don't enter into the minimal symbol tables as there is
4748 a minimal symbol table entry from the ELF symbols already.
4749 Enter into partial symbol table if it has a location
4750 descriptor or a type.
4751 If the location descriptor is missing, new_symbol will create
4752 a LOC_UNRESOLVED symbol, the address of the variable will then
4753 be determined from the minimal symbol table whenever the variable
4754 is referenced.
4755 The address for the partial symbol table entry is not
4756 used by GDB, but it comes in handy for debugging partial symbol
4757 table building. */
4758
95554aad 4759 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
4760 add_psymbol_to_list (actual_name, strlen (actual_name),
4761 built_actual_name,
4762 VAR_DOMAIN, LOC_STATIC,
4763 &objfile->global_psymbols,
4764 0, addr + baseaddr,
4765 cu->language, objfile);
c906108c
SS
4766 }
4767 else
4768 {
0963b4bd 4769 /* Static Variable. Skip symbols without location descriptors. */
95554aad 4770 if (pdi->d.locdesc == NULL)
decbce07
MS
4771 {
4772 if (built_actual_name)
4773 xfree (actual_name);
4774 return;
4775 }
f47fb265 4776 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4777 mst_file_data, objfile); */
f47fb265
MS
4778 add_psymbol_to_list (actual_name, strlen (actual_name),
4779 built_actual_name,
4780 VAR_DOMAIN, LOC_STATIC,
4781 &objfile->static_psymbols,
4782 0, addr + baseaddr,
4783 cu->language, objfile);
c906108c
SS
4784 }
4785 break;
4786 case DW_TAG_typedef:
4787 case DW_TAG_base_type:
a02abb62 4788 case DW_TAG_subrange_type:
38d518c9 4789 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4790 built_actual_name,
176620f1 4791 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4792 &objfile->static_psymbols,
e142c38c 4793 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4794 break;
72bf9492
DJ
4795 case DW_TAG_namespace:
4796 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4797 built_actual_name,
72bf9492
DJ
4798 VAR_DOMAIN, LOC_TYPEDEF,
4799 &objfile->global_psymbols,
4800 0, (CORE_ADDR) 0, cu->language, objfile);
4801 break;
c906108c 4802 case DW_TAG_class_type:
680b30c7 4803 case DW_TAG_interface_type:
c906108c
SS
4804 case DW_TAG_structure_type:
4805 case DW_TAG_union_type:
4806 case DW_TAG_enumeration_type:
fa4028e9
JB
4807 /* Skip external references. The DWARF standard says in the section
4808 about "Structure, Union, and Class Type Entries": "An incomplete
4809 structure, union or class type is represented by a structure,
4810 union or class entry that does not have a byte size attribute
4811 and that has a DW_AT_declaration attribute." */
4812 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4813 {
4814 if (built_actual_name)
4815 xfree (actual_name);
4816 return;
4817 }
fa4028e9 4818
63d06c5c
DC
4819 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4820 static vs. global. */
38d518c9 4821 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4822 built_actual_name,
176620f1 4823 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4824 (cu->language == language_cplus
4825 || cu->language == language_java)
63d06c5c
DC
4826 ? &objfile->global_psymbols
4827 : &objfile->static_psymbols,
e142c38c 4828 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4829
c906108c
SS
4830 break;
4831 case DW_TAG_enumerator:
38d518c9 4832 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4833 built_actual_name,
176620f1 4834 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4835 (cu->language == language_cplus
4836 || cu->language == language_java)
f6fe98ef
DJ
4837 ? &objfile->global_psymbols
4838 : &objfile->static_psymbols,
e142c38c 4839 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4840 break;
4841 default:
4842 break;
4843 }
5c4e30ca 4844
72bf9492
DJ
4845 if (built_actual_name)
4846 xfree (actual_name);
c906108c
SS
4847}
4848
5c4e30ca
DC
4849/* Read a partial die corresponding to a namespace; also, add a symbol
4850 corresponding to that namespace to the symbol table. NAMESPACE is
4851 the name of the enclosing namespace. */
91c24f0a 4852
72bf9492
DJ
4853static void
4854add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4855 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4856 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4857{
72bf9492 4858 /* Add a symbol for the namespace. */
e7c27a73 4859
72bf9492 4860 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4861
4862 /* Now scan partial symbols in that namespace. */
4863
91c24f0a 4864 if (pdi->has_children)
5734ee8b 4865 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4866}
4867
5d7cb8df
JK
4868/* Read a partial die corresponding to a Fortran module. */
4869
4870static void
4871add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4872 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4873{
f55ee35c 4874 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4875
4876 if (pdi->has_children)
4877 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4878}
4879
bc30ff58
JB
4880/* Read a partial die corresponding to a subprogram and create a partial
4881 symbol for that subprogram. When the CU language allows it, this
4882 routine also defines a partial symbol for each nested subprogram
4883 that this subprogram contains.
6e70227d 4884
bc30ff58
JB
4885 DIE my also be a lexical block, in which case we simply search
4886 recursively for suprograms defined inside that lexical block.
4887 Again, this is only performed when the CU language allows this
4888 type of definitions. */
4889
4890static void
4891add_partial_subprogram (struct partial_die_info *pdi,
4892 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4893 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4894{
4895 if (pdi->tag == DW_TAG_subprogram)
4896 {
4897 if (pdi->has_pc_info)
4898 {
4899 if (pdi->lowpc < *lowpc)
4900 *lowpc = pdi->lowpc;
4901 if (pdi->highpc > *highpc)
4902 *highpc = pdi->highpc;
5734ee8b
DJ
4903 if (need_pc)
4904 {
4905 CORE_ADDR baseaddr;
4906 struct objfile *objfile = cu->objfile;
4907
4908 baseaddr = ANOFFSET (objfile->section_offsets,
4909 SECT_OFF_TEXT (objfile));
4910 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4911 pdi->lowpc + baseaddr,
4912 pdi->highpc - 1 + baseaddr,
9291a0cd 4913 cu->per_cu->v.psymtab);
5734ee8b 4914 }
481860b3
GB
4915 }
4916
4917 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4918 {
bc30ff58 4919 if (!pdi->is_declaration)
e8d05480
JB
4920 /* Ignore subprogram DIEs that do not have a name, they are
4921 illegal. Do not emit a complaint at this point, we will
4922 do so when we convert this psymtab into a symtab. */
4923 if (pdi->name)
4924 add_partial_symbol (pdi, cu);
bc30ff58
JB
4925 }
4926 }
6e70227d 4927
bc30ff58
JB
4928 if (! pdi->has_children)
4929 return;
4930
4931 if (cu->language == language_ada)
4932 {
4933 pdi = pdi->die_child;
4934 while (pdi != NULL)
4935 {
4936 fixup_partial_die (pdi, cu);
4937 if (pdi->tag == DW_TAG_subprogram
4938 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4939 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4940 pdi = pdi->die_sibling;
4941 }
4942 }
4943}
4944
91c24f0a
DC
4945/* Read a partial die corresponding to an enumeration type. */
4946
72bf9492
DJ
4947static void
4948add_partial_enumeration (struct partial_die_info *enum_pdi,
4949 struct dwarf2_cu *cu)
91c24f0a 4950{
72bf9492 4951 struct partial_die_info *pdi;
91c24f0a
DC
4952
4953 if (enum_pdi->name != NULL)
72bf9492
DJ
4954 add_partial_symbol (enum_pdi, cu);
4955
4956 pdi = enum_pdi->die_child;
4957 while (pdi)
91c24f0a 4958 {
72bf9492 4959 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4960 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4961 else
72bf9492
DJ
4962 add_partial_symbol (pdi, cu);
4963 pdi = pdi->die_sibling;
91c24f0a 4964 }
91c24f0a
DC
4965}
4966
6caca83c
CC
4967/* Return the initial uleb128 in the die at INFO_PTR. */
4968
4969static unsigned int
4970peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4971{
4972 unsigned int bytes_read;
4973
4974 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4975}
4976
4bb7a0a7
DJ
4977/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4978 Return the corresponding abbrev, or NULL if the number is zero (indicating
4979 an empty DIE). In either case *BYTES_READ will be set to the length of
4980 the initial number. */
4981
4982static struct abbrev_info *
fe1b8b76 4983peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4984 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4985{
4986 bfd *abfd = cu->objfile->obfd;
4987 unsigned int abbrev_number;
4988 struct abbrev_info *abbrev;
4989
4990 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4991
4992 if (abbrev_number == 0)
4993 return NULL;
4994
4995 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4996 if (!abbrev)
4997 {
3e43a32a
MS
4998 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4999 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
5000 }
5001
5002 return abbrev;
5003}
5004
93311388
DE
5005/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5006 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
5007 DIE. Any children of the skipped DIEs will also be skipped. */
5008
fe1b8b76 5009static gdb_byte *
dee91e82 5010skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 5011{
dee91e82 5012 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
5013 struct abbrev_info *abbrev;
5014 unsigned int bytes_read;
5015
5016 while (1)
5017 {
5018 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5019 if (abbrev == NULL)
5020 return info_ptr + bytes_read;
5021 else
dee91e82 5022 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
5023 }
5024}
5025
93311388
DE
5026/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5027 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
5028 abbrev corresponding to that skipped uleb128 should be passed in
5029 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5030 children. */
5031
fe1b8b76 5032static gdb_byte *
dee91e82
DE
5033skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5034 struct abbrev_info *abbrev)
4bb7a0a7
DJ
5035{
5036 unsigned int bytes_read;
5037 struct attribute attr;
dee91e82
DE
5038 bfd *abfd = reader->abfd;
5039 struct dwarf2_cu *cu = reader->cu;
5040 gdb_byte *buffer = reader->buffer;
f664829e
DE
5041 const gdb_byte *buffer_end = reader->buffer_end;
5042 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
5043 unsigned int form, i;
5044
5045 for (i = 0; i < abbrev->num_attrs; i++)
5046 {
5047 /* The only abbrev we care about is DW_AT_sibling. */
5048 if (abbrev->attrs[i].name == DW_AT_sibling)
5049 {
dee91e82 5050 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 5051 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
5052 complaint (&symfile_complaints,
5053 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 5054 else
b64f50a1 5055 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
5056 }
5057
5058 /* If it isn't DW_AT_sibling, skip this attribute. */
5059 form = abbrev->attrs[i].form;
5060 skip_attribute:
5061 switch (form)
5062 {
4bb7a0a7 5063 case DW_FORM_ref_addr:
ae411497
TT
5064 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5065 and later it is offset sized. */
5066 if (cu->header.version == 2)
5067 info_ptr += cu->header.addr_size;
5068 else
5069 info_ptr += cu->header.offset_size;
5070 break;
5071 case DW_FORM_addr:
4bb7a0a7
DJ
5072 info_ptr += cu->header.addr_size;
5073 break;
5074 case DW_FORM_data1:
5075 case DW_FORM_ref1:
5076 case DW_FORM_flag:
5077 info_ptr += 1;
5078 break;
2dc7f7b3
TT
5079 case DW_FORM_flag_present:
5080 break;
4bb7a0a7
DJ
5081 case DW_FORM_data2:
5082 case DW_FORM_ref2:
5083 info_ptr += 2;
5084 break;
5085 case DW_FORM_data4:
5086 case DW_FORM_ref4:
5087 info_ptr += 4;
5088 break;
5089 case DW_FORM_data8:
5090 case DW_FORM_ref8:
55f1336d 5091 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
5092 info_ptr += 8;
5093 break;
5094 case DW_FORM_string:
9b1c24c8 5095 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
5096 info_ptr += bytes_read;
5097 break;
2dc7f7b3 5098 case DW_FORM_sec_offset:
4bb7a0a7
DJ
5099 case DW_FORM_strp:
5100 info_ptr += cu->header.offset_size;
5101 break;
2dc7f7b3 5102 case DW_FORM_exprloc:
4bb7a0a7
DJ
5103 case DW_FORM_block:
5104 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5105 info_ptr += bytes_read;
5106 break;
5107 case DW_FORM_block1:
5108 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5109 break;
5110 case DW_FORM_block2:
5111 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5112 break;
5113 case DW_FORM_block4:
5114 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5115 break;
5116 case DW_FORM_sdata:
5117 case DW_FORM_udata:
5118 case DW_FORM_ref_udata:
3019eac3
DE
5119 case DW_FORM_GNU_addr_index:
5120 case DW_FORM_GNU_str_index:
f664829e 5121 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
5122 break;
5123 case DW_FORM_indirect:
5124 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5125 info_ptr += bytes_read;
5126 /* We need to continue parsing from here, so just go back to
5127 the top. */
5128 goto skip_attribute;
5129
5130 default:
3e43a32a
MS
5131 error (_("Dwarf Error: Cannot handle %s "
5132 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
5133 dwarf_form_name (form),
5134 bfd_get_filename (abfd));
5135 }
5136 }
5137
5138 if (abbrev->has_children)
dee91e82 5139 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
5140 else
5141 return info_ptr;
5142}
5143
93311388 5144/* Locate ORIG_PDI's sibling.
dee91e82 5145 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 5146
fe1b8b76 5147static gdb_byte *
dee91e82
DE
5148locate_pdi_sibling (const struct die_reader_specs *reader,
5149 struct partial_die_info *orig_pdi,
5150 gdb_byte *info_ptr)
91c24f0a
DC
5151{
5152 /* Do we know the sibling already? */
72bf9492 5153
91c24f0a
DC
5154 if (orig_pdi->sibling)
5155 return orig_pdi->sibling;
5156
5157 /* Are there any children to deal with? */
5158
5159 if (!orig_pdi->has_children)
5160 return info_ptr;
5161
4bb7a0a7 5162 /* Skip the children the long way. */
91c24f0a 5163
dee91e82 5164 return skip_children (reader, info_ptr);
91c24f0a
DC
5165}
5166
c906108c
SS
5167/* Expand this partial symbol table into a full symbol table. */
5168
5169static void
fba45db2 5170dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 5171{
c906108c
SS
5172 if (pst != NULL)
5173 {
5174 if (pst->readin)
5175 {
3e43a32a
MS
5176 warning (_("bug: psymtab for %s is already read in."),
5177 pst->filename);
c906108c
SS
5178 }
5179 else
5180 {
5181 if (info_verbose)
5182 {
3e43a32a
MS
5183 printf_filtered (_("Reading in symbols for %s..."),
5184 pst->filename);
c906108c
SS
5185 gdb_flush (gdb_stdout);
5186 }
5187
10b3939b
DJ
5188 /* Restore our global data. */
5189 dwarf2_per_objfile = objfile_data (pst->objfile,
5190 dwarf2_objfile_data_key);
5191
b2ab525c
KB
5192 /* If this psymtab is constructed from a debug-only objfile, the
5193 has_section_at_zero flag will not necessarily be correct. We
5194 can get the correct value for this flag by looking at the data
5195 associated with the (presumably stripped) associated objfile. */
5196 if (pst->objfile->separate_debug_objfile_backlink)
5197 {
5198 struct dwarf2_per_objfile *dpo_backlink
5199 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5200 dwarf2_objfile_data_key);
9a619af0 5201
b2ab525c
KB
5202 dwarf2_per_objfile->has_section_at_zero
5203 = dpo_backlink->has_section_at_zero;
5204 }
5205
98bfdba5
PA
5206 dwarf2_per_objfile->reading_partial_symbols = 0;
5207
c906108c
SS
5208 psymtab_to_symtab_1 (pst);
5209
5210 /* Finish up the debug error message. */
5211 if (info_verbose)
a3f17187 5212 printf_filtered (_("done.\n"));
c906108c
SS
5213 }
5214 }
95554aad
TT
5215
5216 process_cu_includes ();
c906108c 5217}
9cdd5dbd
DE
5218\f
5219/* Reading in full CUs. */
c906108c 5220
10b3939b
DJ
5221/* Add PER_CU to the queue. */
5222
5223static void
95554aad
TT
5224queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5225 enum language pretend_language)
10b3939b
DJ
5226{
5227 struct dwarf2_queue_item *item;
5228
5229 per_cu->queued = 1;
5230 item = xmalloc (sizeof (*item));
5231 item->per_cu = per_cu;
95554aad 5232 item->pretend_language = pretend_language;
10b3939b
DJ
5233 item->next = NULL;
5234
5235 if (dwarf2_queue == NULL)
5236 dwarf2_queue = item;
5237 else
5238 dwarf2_queue_tail->next = item;
5239
5240 dwarf2_queue_tail = item;
5241}
5242
5243/* Process the queue. */
5244
5245static void
a0f42c21 5246process_queue (void)
10b3939b
DJ
5247{
5248 struct dwarf2_queue_item *item, *next_item;
5249
03dd20cc
DJ
5250 /* The queue starts out with one item, but following a DIE reference
5251 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
5252 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5253 {
9291a0cd
TT
5254 if (dwarf2_per_objfile->using_index
5255 ? !item->per_cu->v.quick->symtab
5256 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
95554aad 5257 process_full_comp_unit (item->per_cu, item->pretend_language);
10b3939b
DJ
5258
5259 item->per_cu->queued = 0;
5260 next_item = item->next;
5261 xfree (item);
5262 }
5263
5264 dwarf2_queue_tail = NULL;
5265}
5266
5267/* Free all allocated queue entries. This function only releases anything if
5268 an error was thrown; if the queue was processed then it would have been
5269 freed as we went along. */
5270
5271static void
5272dwarf2_release_queue (void *dummy)
5273{
5274 struct dwarf2_queue_item *item, *last;
5275
5276 item = dwarf2_queue;
5277 while (item)
5278 {
5279 /* Anything still marked queued is likely to be in an
5280 inconsistent state, so discard it. */
5281 if (item->per_cu->queued)
5282 {
5283 if (item->per_cu->cu != NULL)
dee91e82 5284 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
5285 item->per_cu->queued = 0;
5286 }
5287
5288 last = item;
5289 item = item->next;
5290 xfree (last);
5291 }
5292
5293 dwarf2_queue = dwarf2_queue_tail = NULL;
5294}
5295
5296/* Read in full symbols for PST, and anything it depends on. */
5297
c906108c 5298static void
fba45db2 5299psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 5300{
10b3939b 5301 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
5302 int i;
5303
95554aad
TT
5304 if (pst->readin)
5305 return;
5306
aaa75496 5307 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
5308 if (!pst->dependencies[i]->readin
5309 && pst->dependencies[i]->user == NULL)
aaa75496
JB
5310 {
5311 /* Inform about additional files that need to be read in. */
5312 if (info_verbose)
5313 {
a3f17187 5314 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
5315 fputs_filtered (" ", gdb_stdout);
5316 wrap_here ("");
5317 fputs_filtered ("and ", gdb_stdout);
5318 wrap_here ("");
5319 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 5320 wrap_here (""); /* Flush output. */
aaa75496
JB
5321 gdb_flush (gdb_stdout);
5322 }
5323 psymtab_to_symtab_1 (pst->dependencies[i]);
5324 }
5325
e38df1d0 5326 per_cu = pst->read_symtab_private;
10b3939b
DJ
5327
5328 if (per_cu == NULL)
aaa75496
JB
5329 {
5330 /* It's an include file, no symbols to read for it.
5331 Everything is in the parent symtab. */
5332 pst->readin = 1;
5333 return;
5334 }
c906108c 5335
a0f42c21 5336 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
5337}
5338
dee91e82
DE
5339/* Trivial hash function for die_info: the hash value of a DIE
5340 is its offset in .debug_info for this objfile. */
10b3939b 5341
dee91e82
DE
5342static hashval_t
5343die_hash (const void *item)
10b3939b 5344{
dee91e82 5345 const struct die_info *die = item;
6502dd73 5346
dee91e82
DE
5347 return die->offset.sect_off;
5348}
63d06c5c 5349
dee91e82
DE
5350/* Trivial comparison function for die_info structures: two DIEs
5351 are equal if they have the same offset. */
98bfdba5 5352
dee91e82
DE
5353static int
5354die_eq (const void *item_lhs, const void *item_rhs)
5355{
5356 const struct die_info *die_lhs = item_lhs;
5357 const struct die_info *die_rhs = item_rhs;
c906108c 5358
dee91e82
DE
5359 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5360}
c906108c 5361
dee91e82
DE
5362/* die_reader_func for load_full_comp_unit.
5363 This is identical to read_signatured_type_reader,
5364 but is kept separate for now. */
c906108c 5365
dee91e82
DE
5366static void
5367load_full_comp_unit_reader (const struct die_reader_specs *reader,
5368 gdb_byte *info_ptr,
5369 struct die_info *comp_unit_die,
5370 int has_children,
5371 void *data)
5372{
5373 struct dwarf2_cu *cu = reader->cu;
95554aad 5374 enum language *language_ptr = data;
6caca83c 5375
dee91e82
DE
5376 gdb_assert (cu->die_hash == NULL);
5377 cu->die_hash =
5378 htab_create_alloc_ex (cu->header.length / 12,
5379 die_hash,
5380 die_eq,
5381 NULL,
5382 &cu->comp_unit_obstack,
5383 hashtab_obstack_allocate,
5384 dummy_obstack_deallocate);
e142c38c 5385
dee91e82
DE
5386 if (has_children)
5387 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5388 &info_ptr, comp_unit_die);
5389 cu->dies = comp_unit_die;
5390 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
5391
5392 /* We try not to read any attributes in this function, because not
9cdd5dbd 5393 all CUs needed for references have been loaded yet, and symbol
10b3939b 5394 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
5395 or we won't be able to build types correctly.
5396 Similarly, if we do not read the producer, we can not apply
5397 producer-specific interpretation. */
95554aad 5398 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 5399}
10b3939b 5400
dee91e82 5401/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 5402
dee91e82 5403static void
95554aad
TT
5404load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5405 enum language pretend_language)
dee91e82 5406{
3019eac3 5407 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 5408
95554aad
TT
5409 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5410 &pretend_language);
10b3939b
DJ
5411}
5412
3da10d80
KS
5413/* Add a DIE to the delayed physname list. */
5414
5415static void
5416add_to_method_list (struct type *type, int fnfield_index, int index,
5417 const char *name, struct die_info *die,
5418 struct dwarf2_cu *cu)
5419{
5420 struct delayed_method_info mi;
5421 mi.type = type;
5422 mi.fnfield_index = fnfield_index;
5423 mi.index = index;
5424 mi.name = name;
5425 mi.die = die;
5426 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5427}
5428
5429/* A cleanup for freeing the delayed method list. */
5430
5431static void
5432free_delayed_list (void *ptr)
5433{
5434 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5435 if (cu->method_list != NULL)
5436 {
5437 VEC_free (delayed_method_info, cu->method_list);
5438 cu->method_list = NULL;
5439 }
5440}
5441
5442/* Compute the physnames of any methods on the CU's method list.
5443
5444 The computation of method physnames is delayed in order to avoid the
5445 (bad) condition that one of the method's formal parameters is of an as yet
5446 incomplete type. */
5447
5448static void
5449compute_delayed_physnames (struct dwarf2_cu *cu)
5450{
5451 int i;
5452 struct delayed_method_info *mi;
5453 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5454 {
1d06ead6 5455 const char *physname;
3da10d80
KS
5456 struct fn_fieldlist *fn_flp
5457 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 5458 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
5459 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5460 }
5461}
5462
a766d390
DE
5463/* Go objects should be embedded in a DW_TAG_module DIE,
5464 and it's not clear if/how imported objects will appear.
5465 To keep Go support simple until that's worked out,
5466 go back through what we've read and create something usable.
5467 We could do this while processing each DIE, and feels kinda cleaner,
5468 but that way is more invasive.
5469 This is to, for example, allow the user to type "p var" or "b main"
5470 without having to specify the package name, and allow lookups
5471 of module.object to work in contexts that use the expression
5472 parser. */
5473
5474static void
5475fixup_go_packaging (struct dwarf2_cu *cu)
5476{
5477 char *package_name = NULL;
5478 struct pending *list;
5479 int i;
5480
5481 for (list = global_symbols; list != NULL; list = list->next)
5482 {
5483 for (i = 0; i < list->nsyms; ++i)
5484 {
5485 struct symbol *sym = list->symbol[i];
5486
5487 if (SYMBOL_LANGUAGE (sym) == language_go
5488 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5489 {
5490 char *this_package_name = go_symbol_package_name (sym);
5491
5492 if (this_package_name == NULL)
5493 continue;
5494 if (package_name == NULL)
5495 package_name = this_package_name;
5496 else
5497 {
5498 if (strcmp (package_name, this_package_name) != 0)
5499 complaint (&symfile_complaints,
5500 _("Symtab %s has objects from two different Go packages: %s and %s"),
5501 (sym->symtab && sym->symtab->filename
5502 ? sym->symtab->filename
5503 : cu->objfile->name),
5504 this_package_name, package_name);
5505 xfree (this_package_name);
5506 }
5507 }
5508 }
5509 }
5510
5511 if (package_name != NULL)
5512 {
5513 struct objfile *objfile = cu->objfile;
5514 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5515 package_name, objfile);
5516 struct symbol *sym;
5517
5518 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5519
5520 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5521 SYMBOL_SET_LANGUAGE (sym, language_go);
5522 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5523 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5524 e.g., "main" finds the "main" module and not C's main(). */
5525 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5526 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5527 SYMBOL_TYPE (sym) = type;
5528
5529 add_symbol_to_list (sym, &global_symbols);
5530
5531 xfree (package_name);
5532 }
5533}
5534
95554aad
TT
5535static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5536
5537/* Return the symtab for PER_CU. This works properly regardless of
5538 whether we're using the index or psymtabs. */
5539
5540static struct symtab *
5541get_symtab (struct dwarf2_per_cu_data *per_cu)
5542{
5543 return (dwarf2_per_objfile->using_index
5544 ? per_cu->v.quick->symtab
5545 : per_cu->v.psymtab->symtab);
5546}
5547
5548/* A helper function for computing the list of all symbol tables
5549 included by PER_CU. */
5550
5551static void
5552recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5553 htab_t all_children,
5554 struct dwarf2_per_cu_data *per_cu)
5555{
5556 void **slot;
5557 int ix;
5558 struct dwarf2_per_cu_data *iter;
5559
5560 slot = htab_find_slot (all_children, per_cu, INSERT);
5561 if (*slot != NULL)
5562 {
5563 /* This inclusion and its children have been processed. */
5564 return;
5565 }
5566
5567 *slot = per_cu;
5568 /* Only add a CU if it has a symbol table. */
5569 if (get_symtab (per_cu) != NULL)
5570 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5571
5572 for (ix = 0;
5573 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5574 ++ix)
5575 recursively_compute_inclusions (result, all_children, iter);
5576}
5577
5578/* Compute the symtab 'includes' fields for the symtab related to
5579 PER_CU. */
5580
5581static void
5582compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5583{
5584 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5585 {
5586 int ix, len;
5587 struct dwarf2_per_cu_data *iter;
5588 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5589 htab_t all_children;
5590 struct symtab *symtab = get_symtab (per_cu);
5591
5592 /* If we don't have a symtab, we can just skip this case. */
5593 if (symtab == NULL)
5594 return;
5595
5596 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5597 NULL, xcalloc, xfree);
5598
5599 for (ix = 0;
5600 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5601 ix, iter);
5602 ++ix)
5603 recursively_compute_inclusions (&result_children, all_children, iter);
5604
5605 /* Now we have a transitive closure of all the included CUs, so
5606 we can convert it to a list of symtabs. */
5607 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5608 symtab->includes
5609 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5610 (len + 1) * sizeof (struct symtab *));
5611 for (ix = 0;
5612 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5613 ++ix)
5614 symtab->includes[ix] = get_symtab (iter);
5615 symtab->includes[len] = NULL;
5616
5617 VEC_free (dwarf2_per_cu_ptr, result_children);
5618 htab_delete (all_children);
5619 }
5620}
5621
5622/* Compute the 'includes' field for the symtabs of all the CUs we just
5623 read. */
5624
5625static void
5626process_cu_includes (void)
5627{
5628 int ix;
5629 struct dwarf2_per_cu_data *iter;
5630
5631 for (ix = 0;
5632 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5633 ix, iter);
5634 ++ix)
5635 compute_symtab_includes (iter);
5636
5637 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5638}
5639
9cdd5dbd 5640/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
5641 already been loaded into memory. */
5642
5643static void
95554aad
TT
5644process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5645 enum language pretend_language)
10b3939b 5646{
10b3939b 5647 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 5648 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
5649 CORE_ADDR lowpc, highpc;
5650 struct symtab *symtab;
3da10d80 5651 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
5652 CORE_ADDR baseaddr;
5653
5654 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5655
10b3939b
DJ
5656 buildsym_init ();
5657 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 5658 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
5659
5660 cu->list_in_scope = &file_symbols;
c906108c 5661
95554aad
TT
5662 cu->language = pretend_language;
5663 cu->language_defn = language_def (cu->language);
5664
c906108c 5665 /* Do line number decoding in read_file_scope () */
10b3939b 5666 process_die (cu->dies, cu);
c906108c 5667
a766d390
DE
5668 /* For now fudge the Go package. */
5669 if (cu->language == language_go)
5670 fixup_go_packaging (cu);
5671
3da10d80
KS
5672 /* Now that we have processed all the DIEs in the CU, all the types
5673 should be complete, and it should now be safe to compute all of the
5674 physnames. */
5675 compute_delayed_physnames (cu);
5676 do_cleanups (delayed_list_cleanup);
5677
fae299cd
DC
5678 /* Some compilers don't define a DW_AT_high_pc attribute for the
5679 compilation unit. If the DW_AT_high_pc is missing, synthesize
5680 it, by scanning the DIE's below the compilation unit. */
10b3939b 5681 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 5682
613e1657 5683 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 5684
8be455d7 5685 if (symtab != NULL)
c906108c 5686 {
df15bd07 5687 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 5688
8be455d7
JK
5689 /* Set symtab language to language from DW_AT_language. If the
5690 compilation is from a C file generated by language preprocessors, do
5691 not set the language if it was already deduced by start_subfile. */
5692 if (!(cu->language == language_c && symtab->language != language_c))
5693 symtab->language = cu->language;
5694
5695 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5696 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
5697 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5698 there were bugs in prologue debug info, fixed later in GCC-4.5
5699 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
5700
5701 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5702 needed, it would be wrong due to missing DW_AT_producer there.
5703
5704 Still one can confuse GDB by using non-standard GCC compilation
5705 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5706 */
ab260dad 5707 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 5708 symtab->locations_valid = 1;
e0d00bc7
JK
5709
5710 if (gcc_4_minor >= 5)
5711 symtab->epilogue_unwind_valid = 1;
96408a79
SA
5712
5713 symtab->call_site_htab = cu->call_site_htab;
c906108c 5714 }
9291a0cd
TT
5715
5716 if (dwarf2_per_objfile->using_index)
5717 per_cu->v.quick->symtab = symtab;
5718 else
5719 {
5720 struct partial_symtab *pst = per_cu->v.psymtab;
5721 pst->symtab = symtab;
5722 pst->readin = 1;
5723 }
c906108c 5724
95554aad
TT
5725 /* Push it for inclusion processing later. */
5726 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5727
c906108c
SS
5728 do_cleanups (back_to);
5729}
5730
95554aad
TT
5731/* Process an imported unit DIE. */
5732
5733static void
5734process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5735{
5736 struct attribute *attr;
5737
5738 attr = dwarf2_attr (die, DW_AT_import, cu);
5739 if (attr != NULL)
5740 {
5741 struct dwarf2_per_cu_data *per_cu;
5742 struct symtab *imported_symtab;
5743 sect_offset offset;
5744
5745 offset = dwarf2_get_ref_die_offset (attr);
5746 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5747
5748 /* Queue the unit, if needed. */
5749 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
5750 load_full_comp_unit (per_cu, cu->language);
5751
5752 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5753 per_cu);
5754 }
5755}
5756
c906108c
SS
5757/* Process a die and its children. */
5758
5759static void
e7c27a73 5760process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
5761{
5762 switch (die->tag)
5763 {
5764 case DW_TAG_padding:
5765 break;
5766 case DW_TAG_compile_unit:
95554aad 5767 case DW_TAG_partial_unit:
e7c27a73 5768 read_file_scope (die, cu);
c906108c 5769 break;
348e048f
DE
5770 case DW_TAG_type_unit:
5771 read_type_unit_scope (die, cu);
5772 break;
c906108c 5773 case DW_TAG_subprogram:
c906108c 5774 case DW_TAG_inlined_subroutine:
edb3359d 5775 read_func_scope (die, cu);
c906108c
SS
5776 break;
5777 case DW_TAG_lexical_block:
14898363
L
5778 case DW_TAG_try_block:
5779 case DW_TAG_catch_block:
e7c27a73 5780 read_lexical_block_scope (die, cu);
c906108c 5781 break;
96408a79
SA
5782 case DW_TAG_GNU_call_site:
5783 read_call_site_scope (die, cu);
5784 break;
c906108c 5785 case DW_TAG_class_type:
680b30c7 5786 case DW_TAG_interface_type:
c906108c
SS
5787 case DW_TAG_structure_type:
5788 case DW_TAG_union_type:
134d01f1 5789 process_structure_scope (die, cu);
c906108c
SS
5790 break;
5791 case DW_TAG_enumeration_type:
134d01f1 5792 process_enumeration_scope (die, cu);
c906108c 5793 break;
134d01f1 5794
f792889a
DJ
5795 /* These dies have a type, but processing them does not create
5796 a symbol or recurse to process the children. Therefore we can
5797 read them on-demand through read_type_die. */
c906108c 5798 case DW_TAG_subroutine_type:
72019c9c 5799 case DW_TAG_set_type:
c906108c 5800 case DW_TAG_array_type:
c906108c 5801 case DW_TAG_pointer_type:
c906108c 5802 case DW_TAG_ptr_to_member_type:
c906108c 5803 case DW_TAG_reference_type:
c906108c 5804 case DW_TAG_string_type:
c906108c 5805 break;
134d01f1 5806
c906108c 5807 case DW_TAG_base_type:
a02abb62 5808 case DW_TAG_subrange_type:
cb249c71 5809 case DW_TAG_typedef:
134d01f1
DJ
5810 /* Add a typedef symbol for the type definition, if it has a
5811 DW_AT_name. */
f792889a 5812 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 5813 break;
c906108c 5814 case DW_TAG_common_block:
e7c27a73 5815 read_common_block (die, cu);
c906108c
SS
5816 break;
5817 case DW_TAG_common_inclusion:
5818 break;
d9fa45fe 5819 case DW_TAG_namespace:
63d06c5c 5820 processing_has_namespace_info = 1;
e7c27a73 5821 read_namespace (die, cu);
d9fa45fe 5822 break;
5d7cb8df 5823 case DW_TAG_module:
f55ee35c 5824 processing_has_namespace_info = 1;
5d7cb8df
JK
5825 read_module (die, cu);
5826 break;
d9fa45fe
DC
5827 case DW_TAG_imported_declaration:
5828 case DW_TAG_imported_module:
63d06c5c 5829 processing_has_namespace_info = 1;
27aa8d6a
SW
5830 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5831 || cu->language != language_fortran))
5832 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5833 dwarf_tag_name (die->tag));
5834 read_import_statement (die, cu);
d9fa45fe 5835 break;
95554aad
TT
5836
5837 case DW_TAG_imported_unit:
5838 process_imported_unit_die (die, cu);
5839 break;
5840
c906108c 5841 default:
e7c27a73 5842 new_symbol (die, NULL, cu);
c906108c
SS
5843 break;
5844 }
5845}
5846
94af9270
KS
5847/* A helper function for dwarf2_compute_name which determines whether DIE
5848 needs to have the name of the scope prepended to the name listed in the
5849 die. */
5850
5851static int
5852die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5853{
1c809c68
TT
5854 struct attribute *attr;
5855
94af9270
KS
5856 switch (die->tag)
5857 {
5858 case DW_TAG_namespace:
5859 case DW_TAG_typedef:
5860 case DW_TAG_class_type:
5861 case DW_TAG_interface_type:
5862 case DW_TAG_structure_type:
5863 case DW_TAG_union_type:
5864 case DW_TAG_enumeration_type:
5865 case DW_TAG_enumerator:
5866 case DW_TAG_subprogram:
5867 case DW_TAG_member:
5868 return 1;
5869
5870 case DW_TAG_variable:
c2b0a229 5871 case DW_TAG_constant:
94af9270
KS
5872 /* We only need to prefix "globally" visible variables. These include
5873 any variable marked with DW_AT_external or any variable that
5874 lives in a namespace. [Variables in anonymous namespaces
5875 require prefixing, but they are not DW_AT_external.] */
5876
5877 if (dwarf2_attr (die, DW_AT_specification, cu))
5878 {
5879 struct dwarf2_cu *spec_cu = cu;
9a619af0 5880
94af9270
KS
5881 return die_needs_namespace (die_specification (die, &spec_cu),
5882 spec_cu);
5883 }
5884
1c809c68 5885 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
5886 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5887 && die->parent->tag != DW_TAG_module)
1c809c68
TT
5888 return 0;
5889 /* A variable in a lexical block of some kind does not need a
5890 namespace, even though in C++ such variables may be external
5891 and have a mangled name. */
5892 if (die->parent->tag == DW_TAG_lexical_block
5893 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5894 || die->parent->tag == DW_TAG_catch_block
5895 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5896 return 0;
5897 return 1;
94af9270
KS
5898
5899 default:
5900 return 0;
5901 }
5902}
5903
98bfdba5
PA
5904/* Retrieve the last character from a mem_file. */
5905
5906static void
5907do_ui_file_peek_last (void *object, const char *buffer, long length)
5908{
5909 char *last_char_p = (char *) object;
5910
5911 if (length > 0)
5912 *last_char_p = buffer[length - 1];
5913}
5914
94af9270 5915/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
5916 compute the physname for the object, which include a method's:
5917 - formal parameters (C++/Java),
5918 - receiver type (Go),
5919 - return type (Java).
5920
5921 The term "physname" is a bit confusing.
5922 For C++, for example, it is the demangled name.
5923 For Go, for example, it's the mangled name.
94af9270 5924
af6b7be1
JB
5925 For Ada, return the DIE's linkage name rather than the fully qualified
5926 name. PHYSNAME is ignored..
5927
94af9270
KS
5928 The result is allocated on the objfile_obstack and canonicalized. */
5929
5930static const char *
5931dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5932 int physname)
5933{
bb5ed363
DE
5934 struct objfile *objfile = cu->objfile;
5935
94af9270
KS
5936 if (name == NULL)
5937 name = dwarf2_name (die, cu);
5938
f55ee35c
JK
5939 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5940 compute it by typename_concat inside GDB. */
5941 if (cu->language == language_ada
5942 || (cu->language == language_fortran && physname))
5943 {
5944 /* For Ada unit, we prefer the linkage name over the name, as
5945 the former contains the exported name, which the user expects
5946 to be able to reference. Ideally, we want the user to be able
5947 to reference this entity using either natural or linkage name,
5948 but we haven't started looking at this enhancement yet. */
5949 struct attribute *attr;
5950
5951 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5952 if (attr == NULL)
5953 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5954 if (attr && DW_STRING (attr))
5955 return DW_STRING (attr);
5956 }
5957
94af9270
KS
5958 /* These are the only languages we know how to qualify names in. */
5959 if (name != NULL
f55ee35c
JK
5960 && (cu->language == language_cplus || cu->language == language_java
5961 || cu->language == language_fortran))
94af9270
KS
5962 {
5963 if (die_needs_namespace (die, cu))
5964 {
5965 long length;
0d5cff50 5966 const char *prefix;
94af9270
KS
5967 struct ui_file *buf;
5968
5969 prefix = determine_prefix (die, cu);
5970 buf = mem_fileopen ();
5971 if (*prefix != '\0')
5972 {
f55ee35c
JK
5973 char *prefixed_name = typename_concat (NULL, prefix, name,
5974 physname, cu);
9a619af0 5975
94af9270
KS
5976 fputs_unfiltered (prefixed_name, buf);
5977 xfree (prefixed_name);
5978 }
5979 else
62d5b8da 5980 fputs_unfiltered (name, buf);
94af9270 5981
98bfdba5
PA
5982 /* Template parameters may be specified in the DIE's DW_AT_name, or
5983 as children with DW_TAG_template_type_param or
5984 DW_TAG_value_type_param. If the latter, add them to the name
5985 here. If the name already has template parameters, then
5986 skip this step; some versions of GCC emit both, and
5987 it is more efficient to use the pre-computed name.
5988
5989 Something to keep in mind about this process: it is very
5990 unlikely, or in some cases downright impossible, to produce
5991 something that will match the mangled name of a function.
5992 If the definition of the function has the same debug info,
5993 we should be able to match up with it anyway. But fallbacks
5994 using the minimal symbol, for instance to find a method
5995 implemented in a stripped copy of libstdc++, will not work.
5996 If we do not have debug info for the definition, we will have to
5997 match them up some other way.
5998
5999 When we do name matching there is a related problem with function
6000 templates; two instantiated function templates are allowed to
6001 differ only by their return types, which we do not add here. */
6002
6003 if (cu->language == language_cplus && strchr (name, '<') == NULL)
6004 {
6005 struct attribute *attr;
6006 struct die_info *child;
6007 int first = 1;
6008
6009 die->building_fullname = 1;
6010
6011 for (child = die->child; child != NULL; child = child->sibling)
6012 {
6013 struct type *type;
12df843f 6014 LONGEST value;
98bfdba5
PA
6015 gdb_byte *bytes;
6016 struct dwarf2_locexpr_baton *baton;
6017 struct value *v;
6018
6019 if (child->tag != DW_TAG_template_type_param
6020 && child->tag != DW_TAG_template_value_param)
6021 continue;
6022
6023 if (first)
6024 {
6025 fputs_unfiltered ("<", buf);
6026 first = 0;
6027 }
6028 else
6029 fputs_unfiltered (", ", buf);
6030
6031 attr = dwarf2_attr (child, DW_AT_type, cu);
6032 if (attr == NULL)
6033 {
6034 complaint (&symfile_complaints,
6035 _("template parameter missing DW_AT_type"));
6036 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6037 continue;
6038 }
6039 type = die_type (child, cu);
6040
6041 if (child->tag == DW_TAG_template_type_param)
6042 {
6043 c_print_type (type, "", buf, -1, 0);
6044 continue;
6045 }
6046
6047 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6048 if (attr == NULL)
6049 {
6050 complaint (&symfile_complaints,
3e43a32a
MS
6051 _("template parameter missing "
6052 "DW_AT_const_value"));
98bfdba5
PA
6053 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6054 continue;
6055 }
6056
6057 dwarf2_const_value_attr (attr, type, name,
6058 &cu->comp_unit_obstack, cu,
6059 &value, &bytes, &baton);
6060
6061 if (TYPE_NOSIGN (type))
6062 /* GDB prints characters as NUMBER 'CHAR'. If that's
6063 changed, this can use value_print instead. */
6064 c_printchar (value, type, buf);
6065 else
6066 {
6067 struct value_print_options opts;
6068
6069 if (baton != NULL)
6070 v = dwarf2_evaluate_loc_desc (type, NULL,
6071 baton->data,
6072 baton->size,
6073 baton->per_cu);
6074 else if (bytes != NULL)
6075 {
6076 v = allocate_value (type);
6077 memcpy (value_contents_writeable (v), bytes,
6078 TYPE_LENGTH (type));
6079 }
6080 else
6081 v = value_from_longest (type, value);
6082
3e43a32a
MS
6083 /* Specify decimal so that we do not depend on
6084 the radix. */
98bfdba5
PA
6085 get_formatted_print_options (&opts, 'd');
6086 opts.raw = 1;
6087 value_print (v, buf, &opts);
6088 release_value (v);
6089 value_free (v);
6090 }
6091 }
6092
6093 die->building_fullname = 0;
6094
6095 if (!first)
6096 {
6097 /* Close the argument list, with a space if necessary
6098 (nested templates). */
6099 char last_char = '\0';
6100 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6101 if (last_char == '>')
6102 fputs_unfiltered (" >", buf);
6103 else
6104 fputs_unfiltered (">", buf);
6105 }
6106 }
6107
94af9270
KS
6108 /* For Java and C++ methods, append formal parameter type
6109 information, if PHYSNAME. */
6e70227d 6110
94af9270
KS
6111 if (physname && die->tag == DW_TAG_subprogram
6112 && (cu->language == language_cplus
6113 || cu->language == language_java))
6114 {
6115 struct type *type = read_type_die (die, cu);
6116
3167638f 6117 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
6118
6119 if (cu->language == language_java)
6120 {
6121 /* For java, we must append the return type to method
0963b4bd 6122 names. */
94af9270
KS
6123 if (die->tag == DW_TAG_subprogram)
6124 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6125 0, 0);
6126 }
6127 else if (cu->language == language_cplus)
6128 {
60430eff
DJ
6129 /* Assume that an artificial first parameter is
6130 "this", but do not crash if it is not. RealView
6131 marks unnamed (and thus unused) parameters as
6132 artificial; there is no way to differentiate
6133 the two cases. */
94af9270
KS
6134 if (TYPE_NFIELDS (type) > 0
6135 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 6136 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
6137 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6138 0))))
94af9270
KS
6139 fputs_unfiltered (" const", buf);
6140 }
6141 }
6142
bb5ed363 6143 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
6144 &length);
6145 ui_file_delete (buf);
6146
6147 if (cu->language == language_cplus)
6148 {
6149 char *cname
6150 = dwarf2_canonicalize_name (name, cu,
bb5ed363 6151 &objfile->objfile_obstack);
9a619af0 6152
94af9270
KS
6153 if (cname != NULL)
6154 name = cname;
6155 }
6156 }
6157 }
6158
6159 return name;
6160}
6161
0114d602
DJ
6162/* Return the fully qualified name of DIE, based on its DW_AT_name.
6163 If scope qualifiers are appropriate they will be added. The result
6164 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
6165 not have a name. NAME may either be from a previous call to
6166 dwarf2_name or NULL.
6167
0963b4bd 6168 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
6169
6170static const char *
94af9270 6171dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 6172{
94af9270
KS
6173 return dwarf2_compute_name (name, die, cu, 0);
6174}
0114d602 6175
94af9270
KS
6176/* Construct a physname for the given DIE in CU. NAME may either be
6177 from a previous call to dwarf2_name or NULL. The result will be
6178 allocated on the objfile_objstack or NULL if the DIE does not have a
6179 name.
0114d602 6180
94af9270 6181 The output string will be canonicalized (if C++/Java). */
0114d602 6182
94af9270
KS
6183static const char *
6184dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6185{
bb5ed363 6186 struct objfile *objfile = cu->objfile;
900e11f9
JK
6187 struct attribute *attr;
6188 const char *retval, *mangled = NULL, *canon = NULL;
6189 struct cleanup *back_to;
6190 int need_copy = 1;
6191
6192 /* In this case dwarf2_compute_name is just a shortcut not building anything
6193 on its own. */
6194 if (!die_needs_namespace (die, cu))
6195 return dwarf2_compute_name (name, die, cu, 1);
6196
6197 back_to = make_cleanup (null_cleanup, NULL);
6198
6199 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6200 if (!attr)
6201 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6202
6203 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6204 has computed. */
6205 if (attr && DW_STRING (attr))
6206 {
6207 char *demangled;
6208
6209 mangled = DW_STRING (attr);
6210
6211 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6212 type. It is easier for GDB users to search for such functions as
6213 `name(params)' than `long name(params)'. In such case the minimal
6214 symbol names do not match the full symbol names but for template
6215 functions there is never a need to look up their definition from their
6216 declaration so the only disadvantage remains the minimal symbol
6217 variant `long name(params)' does not have the proper inferior type.
6218 */
6219
a766d390
DE
6220 if (cu->language == language_go)
6221 {
6222 /* This is a lie, but we already lie to the caller new_symbol_full.
6223 new_symbol_full assumes we return the mangled name.
6224 This just undoes that lie until things are cleaned up. */
6225 demangled = NULL;
6226 }
6227 else
6228 {
6229 demangled = cplus_demangle (mangled,
6230 (DMGL_PARAMS | DMGL_ANSI
6231 | (cu->language == language_java
6232 ? DMGL_JAVA | DMGL_RET_POSTFIX
6233 : DMGL_RET_DROP)));
6234 }
900e11f9
JK
6235 if (demangled)
6236 {
6237 make_cleanup (xfree, demangled);
6238 canon = demangled;
6239 }
6240 else
6241 {
6242 canon = mangled;
6243 need_copy = 0;
6244 }
6245 }
6246
6247 if (canon == NULL || check_physname)
6248 {
6249 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6250
6251 if (canon != NULL && strcmp (physname, canon) != 0)
6252 {
6253 /* It may not mean a bug in GDB. The compiler could also
6254 compute DW_AT_linkage_name incorrectly. But in such case
6255 GDB would need to be bug-to-bug compatible. */
6256
6257 complaint (&symfile_complaints,
6258 _("Computed physname <%s> does not match demangled <%s> "
6259 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 6260 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
6261
6262 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6263 is available here - over computed PHYSNAME. It is safer
6264 against both buggy GDB and buggy compilers. */
6265
6266 retval = canon;
6267 }
6268 else
6269 {
6270 retval = physname;
6271 need_copy = 0;
6272 }
6273 }
6274 else
6275 retval = canon;
6276
6277 if (need_copy)
6278 retval = obsavestring (retval, strlen (retval),
bb5ed363 6279 &objfile->objfile_obstack);
900e11f9
JK
6280
6281 do_cleanups (back_to);
6282 return retval;
0114d602
DJ
6283}
6284
27aa8d6a
SW
6285/* Read the import statement specified by the given die and record it. */
6286
6287static void
6288read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6289{
bb5ed363 6290 struct objfile *objfile = cu->objfile;
27aa8d6a 6291 struct attribute *import_attr;
32019081 6292 struct die_info *imported_die, *child_die;
de4affc9 6293 struct dwarf2_cu *imported_cu;
27aa8d6a 6294 const char *imported_name;
794684b6 6295 const char *imported_name_prefix;
13387711
SW
6296 const char *canonical_name;
6297 const char *import_alias;
6298 const char *imported_declaration = NULL;
794684b6 6299 const char *import_prefix;
32019081
JK
6300 VEC (const_char_ptr) *excludes = NULL;
6301 struct cleanup *cleanups;
13387711
SW
6302
6303 char *temp;
27aa8d6a
SW
6304
6305 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6306 if (import_attr == NULL)
6307 {
6308 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6309 dwarf_tag_name (die->tag));
6310 return;
6311 }
6312
de4affc9
CC
6313 imported_cu = cu;
6314 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6315 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
6316 if (imported_name == NULL)
6317 {
6318 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6319
6320 The import in the following code:
6321 namespace A
6322 {
6323 typedef int B;
6324 }
6325
6326 int main ()
6327 {
6328 using A::B;
6329 B b;
6330 return b;
6331 }
6332
6333 ...
6334 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6335 <52> DW_AT_decl_file : 1
6336 <53> DW_AT_decl_line : 6
6337 <54> DW_AT_import : <0x75>
6338 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6339 <59> DW_AT_name : B
6340 <5b> DW_AT_decl_file : 1
6341 <5c> DW_AT_decl_line : 2
6342 <5d> DW_AT_type : <0x6e>
6343 ...
6344 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6345 <76> DW_AT_byte_size : 4
6346 <77> DW_AT_encoding : 5 (signed)
6347
6348 imports the wrong die ( 0x75 instead of 0x58 ).
6349 This case will be ignored until the gcc bug is fixed. */
6350 return;
6351 }
6352
82856980
SW
6353 /* Figure out the local name after import. */
6354 import_alias = dwarf2_name (die, cu);
27aa8d6a 6355
794684b6
SW
6356 /* Figure out where the statement is being imported to. */
6357 import_prefix = determine_prefix (die, cu);
6358
6359 /* Figure out what the scope of the imported die is and prepend it
6360 to the name of the imported die. */
de4affc9 6361 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 6362
f55ee35c
JK
6363 if (imported_die->tag != DW_TAG_namespace
6364 && imported_die->tag != DW_TAG_module)
794684b6 6365 {
13387711
SW
6366 imported_declaration = imported_name;
6367 canonical_name = imported_name_prefix;
794684b6 6368 }
13387711 6369 else if (strlen (imported_name_prefix) > 0)
794684b6 6370 {
13387711
SW
6371 temp = alloca (strlen (imported_name_prefix)
6372 + 2 + strlen (imported_name) + 1);
6373 strcpy (temp, imported_name_prefix);
6374 strcat (temp, "::");
6375 strcat (temp, imported_name);
6376 canonical_name = temp;
794684b6 6377 }
13387711
SW
6378 else
6379 canonical_name = imported_name;
794684b6 6380
32019081
JK
6381 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6382
6383 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6384 for (child_die = die->child; child_die && child_die->tag;
6385 child_die = sibling_die (child_die))
6386 {
6387 /* DWARF-4: A Fortran use statement with a “rename list” may be
6388 represented by an imported module entry with an import attribute
6389 referring to the module and owned entries corresponding to those
6390 entities that are renamed as part of being imported. */
6391
6392 if (child_die->tag != DW_TAG_imported_declaration)
6393 {
6394 complaint (&symfile_complaints,
6395 _("child DW_TAG_imported_declaration expected "
6396 "- DIE at 0x%x [in module %s]"),
b64f50a1 6397 child_die->offset.sect_off, objfile->name);
32019081
JK
6398 continue;
6399 }
6400
6401 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6402 if (import_attr == NULL)
6403 {
6404 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6405 dwarf_tag_name (child_die->tag));
6406 continue;
6407 }
6408
6409 imported_cu = cu;
6410 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6411 &imported_cu);
6412 imported_name = dwarf2_name (imported_die, imported_cu);
6413 if (imported_name == NULL)
6414 {
6415 complaint (&symfile_complaints,
6416 _("child DW_TAG_imported_declaration has unknown "
6417 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 6418 child_die->offset.sect_off, objfile->name);
32019081
JK
6419 continue;
6420 }
6421
6422 VEC_safe_push (const_char_ptr, excludes, imported_name);
6423
6424 process_die (child_die, cu);
6425 }
6426
c0cc3a76
SW
6427 cp_add_using_directive (import_prefix,
6428 canonical_name,
6429 import_alias,
13387711 6430 imported_declaration,
32019081 6431 excludes,
bb5ed363 6432 &objfile->objfile_obstack);
32019081
JK
6433
6434 do_cleanups (cleanups);
27aa8d6a
SW
6435}
6436
ae2de4f8
DE
6437/* Cleanup function for read_file_scope. */
6438
cb1df416
DJ
6439static void
6440free_cu_line_header (void *arg)
6441{
6442 struct dwarf2_cu *cu = arg;
6443
6444 free_line_header (cu->line_header);
6445 cu->line_header = NULL;
6446}
6447
9291a0cd
TT
6448static void
6449find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6450 char **name, char **comp_dir)
6451{
6452 struct attribute *attr;
6453
6454 *name = NULL;
6455 *comp_dir = NULL;
6456
6457 /* Find the filename. Do not use dwarf2_name here, since the filename
6458 is not a source language identifier. */
6459 attr = dwarf2_attr (die, DW_AT_name, cu);
6460 if (attr)
6461 {
6462 *name = DW_STRING (attr);
6463 }
6464
6465 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6466 if (attr)
6467 *comp_dir = DW_STRING (attr);
6468 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6469 {
6470 *comp_dir = ldirname (*name);
6471 if (*comp_dir != NULL)
6472 make_cleanup (xfree, *comp_dir);
6473 }
6474 if (*comp_dir != NULL)
6475 {
6476 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6477 directory, get rid of it. */
6478 char *cp = strchr (*comp_dir, ':');
6479
6480 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6481 *comp_dir = cp + 1;
6482 }
6483
6484 if (*name == NULL)
6485 *name = "<unknown>";
6486}
6487
f3f5162e
DE
6488/* Handle DW_AT_stmt_list for a compilation unit or type unit.
6489 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6490 COMP_DIR is the compilation directory.
6491 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
6492
6493static void
6494handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 6495 const char *comp_dir, int want_line_info)
2ab95328
TT
6496{
6497 struct attribute *attr;
2ab95328 6498
2ab95328
TT
6499 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6500 if (attr)
6501 {
6502 unsigned int line_offset = DW_UNSND (attr);
6503 struct line_header *line_header
3019eac3 6504 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
6505
6506 if (line_header)
dee91e82
DE
6507 {
6508 cu->line_header = line_header;
6509 make_cleanup (free_cu_line_header, cu);
f3f5162e 6510 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
dee91e82 6511 }
2ab95328
TT
6512 }
6513}
6514
95554aad 6515/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 6516
c906108c 6517static void
e7c27a73 6518read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6519{
dee91e82 6520 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 6521 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 6522 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
6523 CORE_ADDR highpc = ((CORE_ADDR) 0);
6524 struct attribute *attr;
e1024ff1 6525 char *name = NULL;
c906108c
SS
6526 char *comp_dir = NULL;
6527 struct die_info *child_die;
6528 bfd *abfd = objfile->obfd;
e142c38c 6529 CORE_ADDR baseaddr;
6e70227d 6530
e142c38c 6531 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6532
fae299cd 6533 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
6534
6535 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6536 from finish_block. */
2acceee2 6537 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
6538 lowpc = highpc;
6539 lowpc += baseaddr;
6540 highpc += baseaddr;
6541
9291a0cd 6542 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 6543
95554aad 6544 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 6545
f4b8a18d
KW
6546 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6547 standardised yet. As a workaround for the language detection we fall
6548 back to the DW_AT_producer string. */
6549 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6550 cu->language = language_opencl;
6551
3019eac3
DE
6552 /* Similar hack for Go. */
6553 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6554 set_cu_language (DW_LANG_Go, cu);
6555
6556 /* We assume that we're processing GCC output. */
6557 processing_gcc_compilation = 2;
6558
6559 processing_has_namespace_info = 0;
6560
6561 start_symtab (name, comp_dir, lowpc);
6562 record_debugformat ("DWARF 2");
6563 record_producer (cu->producer);
6564
6565 /* Decode line number information if present. We do this before
6566 processing child DIEs, so that the line header table is available
6567 for DW_AT_decl_file. */
6568 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6569
6570 /* Process all dies in compilation unit. */
6571 if (die->child != NULL)
6572 {
6573 child_die = die->child;
6574 while (child_die && child_die->tag)
6575 {
6576 process_die (child_die, cu);
6577 child_die = sibling_die (child_die);
6578 }
6579 }
6580
6581 /* Decode macro information, if present. Dwarf 2 macro information
6582 refers to information in the line number info statement program
6583 header, so we can only read it if we've read the header
6584 successfully. */
6585 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6586 if (attr && cu->line_header)
6587 {
6588 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6589 complaint (&symfile_complaints,
6590 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6591
6592 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
6593 comp_dir, abfd, cu,
fceca515
DE
6594 &dwarf2_per_objfile->macro, 1,
6595 ".debug_macro");
3019eac3
DE
6596 }
6597 else
6598 {
6599 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6600 if (attr && cu->line_header)
6601 {
6602 unsigned int macro_offset = DW_UNSND (attr);
6603
6604 dwarf_decode_macros (cu->line_header, macro_offset,
6605 comp_dir, abfd, cu,
fceca515
DE
6606 &dwarf2_per_objfile->macinfo, 0,
6607 ".debug_macinfo");
3019eac3
DE
6608 }
6609 }
6610
6611 do_cleanups (back_to);
6612}
6613
6614/* Process DW_TAG_type_unit.
6615 For TUs we want to skip the first top level sibling if it's not the
6616 actual type being defined by this TU. In this case the first top
6617 level sibling is there to provide context only. */
6618
6619static void
6620read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6621{
6622 struct objfile *objfile = cu->objfile;
6623 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6624 CORE_ADDR lowpc;
6625 struct attribute *attr;
6626 char *name = NULL;
6627 char *comp_dir = NULL;
6628 struct die_info *child_die;
6629 bfd *abfd = objfile->obfd;
6630
6631 /* start_symtab needs a low pc, but we don't really have one.
6632 Do what read_file_scope would do in the absence of such info. */
6633 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6634
6635 /* Find the filename. Do not use dwarf2_name here, since the filename
6636 is not a source language identifier. */
6637 attr = dwarf2_attr (die, DW_AT_name, cu);
6638 if (attr)
6639 name = DW_STRING (attr);
6640
6641 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6642 if (attr)
6643 comp_dir = DW_STRING (attr);
6644 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6645 {
6646 comp_dir = ldirname (name);
6647 if (comp_dir != NULL)
6648 make_cleanup (xfree, comp_dir);
6649 }
6650
6651 if (name == NULL)
6652 name = "<unknown>";
6653
95554aad 6654 prepare_one_comp_unit (cu, die, language_minimal);
3019eac3
DE
6655
6656 /* We assume that we're processing GCC output. */
6657 processing_gcc_compilation = 2;
6658
6659 processing_has_namespace_info = 0;
6660
6661 start_symtab (name, comp_dir, lowpc);
6662 record_debugformat ("DWARF 2");
6663 record_producer (cu->producer);
6664
6665 /* Decode line number information if present. We do this before
6666 processing child DIEs, so that the line header table is available
6667 for DW_AT_decl_file.
6668 We don't need the pc/line-number mapping for type units. */
6669 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6670
6671 /* Process the dies in the type unit. */
6672 if (die->child == NULL)
6673 {
6674 dump_die_for_error (die);
6675 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6676 bfd_get_filename (abfd));
6677 }
6678
6679 child_die = die->child;
6680
6681 while (child_die && child_die->tag)
6682 {
6683 process_die (child_die, cu);
6684
6685 child_die = sibling_die (child_die);
6686 }
6687
6688 do_cleanups (back_to);
6689}
6690\f
6691/* DWO files. */
6692
6693static hashval_t
6694hash_dwo_file (const void *item)
6695{
6696 const struct dwo_file *dwo_file = item;
6697
6698 return htab_hash_string (dwo_file->dwo_name);
6699}
6700
6701static int
6702eq_dwo_file (const void *item_lhs, const void *item_rhs)
6703{
6704 const struct dwo_file *lhs = item_lhs;
6705 const struct dwo_file *rhs = item_rhs;
6706
6707 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6708}
6709
6710/* Allocate a hash table for DWO files. */
6711
6712static htab_t
6713allocate_dwo_file_hash_table (void)
6714{
6715 struct objfile *objfile = dwarf2_per_objfile->objfile;
6716
6717 return htab_create_alloc_ex (41,
6718 hash_dwo_file,
6719 eq_dwo_file,
6720 NULL,
6721 &objfile->objfile_obstack,
6722 hashtab_obstack_allocate,
6723 dummy_obstack_deallocate);
6724}
6725
6726static hashval_t
6727hash_dwo_unit (const void *item)
6728{
6729 const struct dwo_unit *dwo_unit = item;
6730
6731 /* This drops the top 32 bits of the id, but is ok for a hash. */
6732 return dwo_unit->signature;
6733}
6734
6735static int
6736eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6737{
6738 const struct dwo_unit *lhs = item_lhs;
6739 const struct dwo_unit *rhs = item_rhs;
6740
6741 /* The signature is assumed to be unique within the DWO file.
6742 So while object file CU dwo_id's always have the value zero,
6743 that's OK, assuming each object file DWO file has only one CU,
6744 and that's the rule for now. */
6745 return lhs->signature == rhs->signature;
6746}
6747
6748/* Allocate a hash table for DWO CUs,TUs.
6749 There is one of these tables for each of CUs,TUs for each DWO file. */
6750
6751static htab_t
6752allocate_dwo_unit_table (struct objfile *objfile)
6753{
6754 /* Start out with a pretty small number.
6755 Generally DWO files contain only one CU and maybe some TUs. */
6756 return htab_create_alloc_ex (3,
6757 hash_dwo_unit,
6758 eq_dwo_unit,
6759 NULL,
6760 &objfile->objfile_obstack,
6761 hashtab_obstack_allocate,
6762 dummy_obstack_deallocate);
6763}
6764
6765/* This function is mapped across the sections and remembers the offset and
6766 size of each of the DWO debugging sections we are interested in. */
6767
6768static void
6769dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6770{
6771 struct dwo_file *dwo_file = dwo_file_ptr;
6772 const struct dwo_section_names *names = &dwo_section_names;
6773
6774 if (section_is_p (sectp->name, &names->abbrev_dwo))
6775 {
6776 dwo_file->sections.abbrev.asection = sectp;
6777 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6778 }
6779 else if (section_is_p (sectp->name, &names->info_dwo))
6780 {
6781 dwo_file->sections.info.asection = sectp;
6782 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6783 }
6784 else if (section_is_p (sectp->name, &names->line_dwo))
6785 {
6786 dwo_file->sections.line.asection = sectp;
6787 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6788 }
6789 else if (section_is_p (sectp->name, &names->loc_dwo))
6790 {
6791 dwo_file->sections.loc.asection = sectp;
6792 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6793 }
6794 else if (section_is_p (sectp->name, &names->str_dwo))
6795 {
6796 dwo_file->sections.str.asection = sectp;
6797 dwo_file->sections.str.size = bfd_get_section_size (sectp);
6798 }
6799 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
6800 {
6801 dwo_file->sections.str_offsets.asection = sectp;
6802 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
6803 }
6804 else if (section_is_p (sectp->name, &names->types_dwo))
6805 {
6806 struct dwarf2_section_info type_section;
6807
6808 memset (&type_section, 0, sizeof (type_section));
6809 type_section.asection = sectp;
6810 type_section.size = bfd_get_section_size (sectp);
6811 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
6812 &type_section);
6813 }
6814}
6815
6816/* Structure used to pass data to create_debug_info_hash_table_reader. */
6817
6818struct create_dwo_info_table_data
6819{
6820 struct dwo_file *dwo_file;
6821 htab_t cu_htab;
6822};
6823
6824/* die_reader_func for create_debug_info_hash_table. */
6825
6826static void
6827create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
6828 gdb_byte *info_ptr,
6829 struct die_info *comp_unit_die,
6830 int has_children,
6831 void *datap)
6832{
6833 struct dwarf2_cu *cu = reader->cu;
6834 struct objfile *objfile = dwarf2_per_objfile->objfile;
6835 sect_offset offset = cu->per_cu->offset;
6836 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
6837 struct create_dwo_info_table_data *data = datap;
6838 struct dwo_file *dwo_file = data->dwo_file;
6839 htab_t cu_htab = data->cu_htab;
6840 void **slot;
6841 struct attribute *attr;
6842 struct dwo_unit *dwo_unit;
6843
6844 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6845 if (attr == NULL)
6846 {
6847 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
6848 " its dwo_id [in module %s]"),
6849 offset.sect_off, dwo_file->dwo_name);
6850 return;
6851 }
6852
6853 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
6854 dwo_unit->dwo_file = dwo_file;
6855 dwo_unit->signature = DW_UNSND (attr);
6856 dwo_unit->info_or_types_section = section;
6857 dwo_unit->offset = offset;
6858 dwo_unit->length = cu->per_cu->length;
6859
6860 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
6861 gdb_assert (slot != NULL);
6862 if (*slot != NULL)
6863 {
6864 const struct dwo_unit *dup_dwo_unit = *slot;
6865
6866 complaint (&symfile_complaints,
6867 _("debug entry at offset 0x%x is duplicate to the entry at"
6868 " offset 0x%x, dwo_id 0x%s [in module %s]"),
6869 offset.sect_off, dup_dwo_unit->offset.sect_off,
6870 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
6871 dwo_file->dwo_name);
6872 }
6873 else
6874 *slot = dwo_unit;
6875
6876 if (dwarf2_die_debug)
6877 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
6878 offset.sect_off,
6879 phex (dwo_unit->signature,
6880 sizeof (dwo_unit->signature)));
6881}
6882
6883/* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
6884
6885static htab_t
6886create_debug_info_hash_table (struct dwo_file *dwo_file)
6887{
6888 struct objfile *objfile = dwarf2_per_objfile->objfile;
6889 struct dwarf2_section_info *section = &dwo_file->sections.info;
6890 bfd *abfd;
6891 htab_t cu_htab;
6892 gdb_byte *info_ptr, *end_ptr;
6893 struct create_dwo_info_table_data create_dwo_info_table_data;
6894
6895 dwarf2_read_section (objfile, section);
6896 info_ptr = section->buffer;
6897
6898 if (info_ptr == NULL)
6899 return NULL;
6900
6901 /* We can't set abfd until now because the section may be empty or
6902 not present, in which case section->asection will be NULL. */
6903 abfd = section->asection->owner;
6904
6905 if (dwarf2_die_debug)
6906 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
6907 bfd_get_filename (abfd));
6908
6909 cu_htab = allocate_dwo_unit_table (objfile);
6910
6911 create_dwo_info_table_data.dwo_file = dwo_file;
6912 create_dwo_info_table_data.cu_htab = cu_htab;
6913
6914 end_ptr = info_ptr + section->size;
6915 while (info_ptr < end_ptr)
6916 {
6917 struct dwarf2_per_cu_data per_cu;
6918
6919 memset (&per_cu, 0, sizeof (per_cu));
6920 per_cu.objfile = objfile;
6921 per_cu.is_debug_types = 0;
6922 per_cu.offset.sect_off = info_ptr - section->buffer;
6923 per_cu.info_or_types_section = section;
6924
6925 init_cutu_and_read_dies_no_follow (&per_cu,
6926 &dwo_file->sections.abbrev,
6927 dwo_file,
6928 create_debug_info_hash_table_reader,
6929 &create_dwo_info_table_data);
6930
6931 info_ptr += per_cu.length;
6932 }
6933
6934 return cu_htab;
6935}
6936
6937/* Subroutine of open_dwo_file to simplify it.
6938 Open the file specified by FILE_NAME and hand it off to BFD for
6939 preliminary analysis. Return a newly initialized bfd *, which
6940 includes a canonicalized copy of FILE_NAME.
6941 In case of trouble, return NULL.
6942 NOTE: This function is derived from symfile_bfd_open. */
6943
6944static bfd *
6945try_open_dwo_file (const char *file_name)
6946{
6947 bfd *sym_bfd;
6948 int desc;
6949 char *absolute_name;
3019eac3
DE
6950
6951 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
6952 O_RDONLY | O_BINARY, &absolute_name);
6953 if (desc < 0)
6954 return NULL;
6955
6956 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
6957 if (!sym_bfd)
6958 {
6959 close (desc);
6960 xfree (absolute_name);
6961 return NULL;
6962 }
6963 bfd_set_cacheable (sym_bfd, 1);
6964
6965 if (!bfd_check_format (sym_bfd, bfd_object))
6966 {
6967 bfd_close (sym_bfd); /* This also closes desc. */
6968 xfree (absolute_name);
6969 return NULL;
6970 }
6971
6972 /* bfd_usrdata exists for applications and libbfd must not touch it. */
6973 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
6974
6975 return sym_bfd;
6976}
6977
6978/* Try to open DWO file DWO_NAME.
6979 COMP_DIR is the DW_AT_comp_dir attribute.
6980 The result is the bfd handle of the file.
6981 If there is a problem finding or opening the file, return NULL.
6982 Upon success, the canonicalized path of the file is stored in the bfd,
6983 same as symfile_bfd_open. */
6984
6985static bfd *
6986open_dwo_file (const char *dwo_name, const char *comp_dir)
6987{
6988 bfd *abfd;
3019eac3
DE
6989
6990 if (IS_ABSOLUTE_PATH (dwo_name))
6991 return try_open_dwo_file (dwo_name);
6992
6993 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
6994
6995 if (comp_dir != NULL)
6996 {
6997 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
6998
6999 /* NOTE: If comp_dir is a relative path, this will also try the
7000 search path, which seems useful. */
7001 abfd = try_open_dwo_file (path_to_try);
7002 xfree (path_to_try);
7003 if (abfd != NULL)
7004 return abfd;
7005 }
7006
7007 /* That didn't work, try debug-file-directory, which, despite its name,
7008 is a list of paths. */
7009
7010 if (*debug_file_directory == '\0')
7011 return NULL;
7012
7013 return try_open_dwo_file (dwo_name);
7014}
7015
7016/* Initialize the use of the DWO file specified by DWO_NAME. */
7017
7018static struct dwo_file *
7019init_dwo_file (const char *dwo_name, const char *comp_dir)
7020{
7021 struct objfile *objfile = dwarf2_per_objfile->objfile;
7022 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7023 struct dwo_file);
7024 bfd *abfd;
7025 struct cleanup *cleanups;
7026
7027 if (dwarf2_die_debug)
7028 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7029
7030 abfd = open_dwo_file (dwo_name, comp_dir);
7031 if (abfd == NULL)
7032 return NULL;
7033 dwo_file->dwo_name = dwo_name;
7034 dwo_file->dwo_bfd = abfd;
7035
7036 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7037
7038 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7039
7040 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7041
7042 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7043 dwo_file->sections.types);
7044
7045 discard_cleanups (cleanups);
7046
7047 return dwo_file;
7048}
7049
7050/* Lookup DWO file DWO_NAME. */
7051
7052static struct dwo_file *
7053lookup_dwo_file (char *dwo_name, const char *comp_dir)
7054{
7055 struct dwo_file *dwo_file;
7056 struct dwo_file find_entry;
7057 void **slot;
7058
7059 if (dwarf2_per_objfile->dwo_files == NULL)
7060 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7061
7062 /* Have we already seen this DWO file? */
7063 find_entry.dwo_name = dwo_name;
7064 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7065
7066 /* If not, read it in and build a table of the DWOs it contains. */
7067 if (*slot == NULL)
7068 *slot = init_dwo_file (dwo_name, comp_dir);
7069
7070 /* NOTE: This will be NULL if unable to open the file. */
7071 dwo_file = *slot;
7072
7073 return dwo_file;
7074}
7075
7076/* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7077 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7078 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7079 nomenclature as TUs).
7080 The result is the DWO CU or NULL if we didn't find it
7081 (dwo_id mismatch or couldn't find the DWO file). */
7082
7083static struct dwo_unit *
7084lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7085 char *dwo_name, const char *comp_dir,
7086 ULONGEST signature)
7087{
7088 struct objfile *objfile = dwarf2_per_objfile->objfile;
7089 struct dwo_file *dwo_file;
7090
7091 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7092 if (dwo_file == NULL)
7093 return NULL;
7094
7095 /* Look up the DWO using its signature(dwo_id). */
7096
7097 if (dwo_file->cus != NULL)
7098 {
7099 struct dwo_unit find_dwo_cu, *dwo_cu;
7100
7101 find_dwo_cu.signature = signature;
7102 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
a766d390 7103
3019eac3
DE
7104 if (dwo_cu != NULL)
7105 return dwo_cu;
7106 }
c906108c 7107
3019eac3 7108 /* We didn't find it. This must mean a dwo_id mismatch. */
df8a16a1 7109
3019eac3
DE
7110 complaint (&symfile_complaints,
7111 _("Could not find DWO CU referenced by CU at offset 0x%x"
7112 " [in module %s]"),
7113 this_cu->offset.sect_off, objfile->name);
7114 return NULL;
7115}
c906108c 7116
3019eac3
DE
7117/* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7118 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7119 The result is the DWO CU or NULL if we didn't find it
7120 (dwo_id mismatch or couldn't find the DWO file). */
debd256d 7121
3019eac3
DE
7122static struct dwo_unit *
7123lookup_dwo_type_unit (struct signatured_type *this_tu,
7124 char *dwo_name, const char *comp_dir)
7125{
7126 struct objfile *objfile = dwarf2_per_objfile->objfile;
7127 struct dwo_file *dwo_file;
cb1df416 7128
3019eac3
DE
7129 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7130 if (dwo_file == NULL)
7131 return NULL;
cf2c3c16 7132
3019eac3
DE
7133 /* Look up the DWO using its signature(dwo_id). */
7134
7135 if (dwo_file->tus != NULL)
cf2c3c16 7136 {
3019eac3 7137 struct dwo_unit find_dwo_tu, *dwo_tu;
9a619af0 7138
3019eac3
DE
7139 find_dwo_tu.signature = this_tu->signature;
7140 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7141
7142 if (dwo_tu != NULL)
7143 return dwo_tu;
2e276125 7144 }
9cdd5dbd 7145
3019eac3
DE
7146 /* We didn't find it. This must mean a dwo_id mismatch. */
7147
7148 complaint (&symfile_complaints,
7149 _("Could not find DWO TU referenced by TU at offset 0x%x"
7150 " [in module %s]"),
7151 this_tu->per_cu.offset.sect_off, objfile->name);
7152 return NULL;
5fb290d7
DJ
7153}
7154
3019eac3
DE
7155/* Free all resources associated with DWO_FILE.
7156 Close the DWO file and munmap the sections.
7157 All memory should be on the objfile obstack. */
348e048f
DE
7158
7159static void
3019eac3 7160free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 7161{
3019eac3
DE
7162 int ix;
7163 struct dwarf2_section_info *section;
348e048f 7164
3019eac3
DE
7165 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7166 bfd_close (dwo_file->dwo_bfd);
348e048f 7167
3019eac3
DE
7168 munmap_section_buffer (&dwo_file->sections.abbrev);
7169 munmap_section_buffer (&dwo_file->sections.info);
7170 munmap_section_buffer (&dwo_file->sections.line);
7171 munmap_section_buffer (&dwo_file->sections.loc);
7172 munmap_section_buffer (&dwo_file->sections.str);
7173 munmap_section_buffer (&dwo_file->sections.str_offsets);
348e048f 7174
3019eac3
DE
7175 for (ix = 0;
7176 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7177 ix, section);
7178 ++ix)
7179 munmap_section_buffer (section);
348e048f 7180
3019eac3
DE
7181 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7182}
348e048f 7183
3019eac3 7184/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 7185
3019eac3
DE
7186static void
7187free_dwo_file_cleanup (void *arg)
7188{
7189 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7190 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 7191
3019eac3
DE
7192 free_dwo_file (dwo_file, objfile);
7193}
348e048f 7194
3019eac3 7195/* Traversal function for free_dwo_files. */
2ab95328 7196
3019eac3
DE
7197static int
7198free_dwo_file_from_slot (void **slot, void *info)
7199{
7200 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7201 struct objfile *objfile = (struct objfile *) info;
348e048f 7202
3019eac3 7203 free_dwo_file (dwo_file, objfile);
348e048f 7204
3019eac3
DE
7205 return 1;
7206}
348e048f 7207
3019eac3 7208/* Free all resources associated with DWO_FILES. */
348e048f 7209
3019eac3
DE
7210static void
7211free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7212{
7213 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 7214}
3019eac3
DE
7215\f
7216/* Read in various DIEs. */
348e048f 7217
d389af10
JK
7218/* qsort helper for inherit_abstract_dies. */
7219
7220static int
7221unsigned_int_compar (const void *ap, const void *bp)
7222{
7223 unsigned int a = *(unsigned int *) ap;
7224 unsigned int b = *(unsigned int *) bp;
7225
7226 return (a > b) - (b > a);
7227}
7228
7229/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
7230 Inherit only the children of the DW_AT_abstract_origin DIE not being
7231 already referenced by DW_AT_abstract_origin from the children of the
7232 current DIE. */
d389af10
JK
7233
7234static void
7235inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7236{
7237 struct die_info *child_die;
7238 unsigned die_children_count;
7239 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
7240 sect_offset *offsets;
7241 sect_offset *offsets_end, *offsetp;
d389af10
JK
7242 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7243 struct die_info *origin_die;
7244 /* Iterator of the ORIGIN_DIE children. */
7245 struct die_info *origin_child_die;
7246 struct cleanup *cleanups;
7247 struct attribute *attr;
cd02d79d
PA
7248 struct dwarf2_cu *origin_cu;
7249 struct pending **origin_previous_list_in_scope;
d389af10
JK
7250
7251 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7252 if (!attr)
7253 return;
7254
cd02d79d
PA
7255 /* Note that following die references may follow to a die in a
7256 different cu. */
7257
7258 origin_cu = cu;
7259 origin_die = follow_die_ref (die, attr, &origin_cu);
7260
7261 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7262 symbols in. */
7263 origin_previous_list_in_scope = origin_cu->list_in_scope;
7264 origin_cu->list_in_scope = cu->list_in_scope;
7265
edb3359d
DJ
7266 if (die->tag != origin_die->tag
7267 && !(die->tag == DW_TAG_inlined_subroutine
7268 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7269 complaint (&symfile_complaints,
7270 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 7271 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
7272
7273 child_die = die->child;
7274 die_children_count = 0;
7275 while (child_die && child_die->tag)
7276 {
7277 child_die = sibling_die (child_die);
7278 die_children_count++;
7279 }
7280 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7281 cleanups = make_cleanup (xfree, offsets);
7282
7283 offsets_end = offsets;
7284 child_die = die->child;
7285 while (child_die && child_die->tag)
7286 {
c38f313d
DJ
7287 /* For each CHILD_DIE, find the corresponding child of
7288 ORIGIN_DIE. If there is more than one layer of
7289 DW_AT_abstract_origin, follow them all; there shouldn't be,
7290 but GCC versions at least through 4.4 generate this (GCC PR
7291 40573). */
7292 struct die_info *child_origin_die = child_die;
cd02d79d 7293 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 7294
c38f313d
DJ
7295 while (1)
7296 {
cd02d79d
PA
7297 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7298 child_origin_cu);
c38f313d
DJ
7299 if (attr == NULL)
7300 break;
cd02d79d
PA
7301 child_origin_die = follow_die_ref (child_origin_die, attr,
7302 &child_origin_cu);
c38f313d
DJ
7303 }
7304
d389af10
JK
7305 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7306 counterpart may exist. */
c38f313d 7307 if (child_origin_die != child_die)
d389af10 7308 {
edb3359d
DJ
7309 if (child_die->tag != child_origin_die->tag
7310 && !(child_die->tag == DW_TAG_inlined_subroutine
7311 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7312 complaint (&symfile_complaints,
7313 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7314 "different tags"), child_die->offset.sect_off,
7315 child_origin_die->offset.sect_off);
c38f313d
DJ
7316 if (child_origin_die->parent != origin_die)
7317 complaint (&symfile_complaints,
7318 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7319 "different parents"), child_die->offset.sect_off,
7320 child_origin_die->offset.sect_off);
c38f313d
DJ
7321 else
7322 *offsets_end++ = child_origin_die->offset;
d389af10
JK
7323 }
7324 child_die = sibling_die (child_die);
7325 }
7326 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7327 unsigned_int_compar);
7328 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 7329 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
7330 complaint (&symfile_complaints,
7331 _("Multiple children of DIE 0x%x refer "
7332 "to DIE 0x%x as their abstract origin"),
b64f50a1 7333 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
7334
7335 offsetp = offsets;
7336 origin_child_die = origin_die->child;
7337 while (origin_child_die && origin_child_die->tag)
7338 {
7339 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
7340 while (offsetp < offsets_end
7341 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 7342 offsetp++;
b64f50a1
JK
7343 if (offsetp >= offsets_end
7344 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
7345 {
7346 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 7347 process_die (origin_child_die, origin_cu);
d389af10
JK
7348 }
7349 origin_child_die = sibling_die (origin_child_die);
7350 }
cd02d79d 7351 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
7352
7353 do_cleanups (cleanups);
7354}
7355
c906108c 7356static void
e7c27a73 7357read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7358{
e7c27a73 7359 struct objfile *objfile = cu->objfile;
52f0bd74 7360 struct context_stack *new;
c906108c
SS
7361 CORE_ADDR lowpc;
7362 CORE_ADDR highpc;
7363 struct die_info *child_die;
edb3359d 7364 struct attribute *attr, *call_line, *call_file;
c906108c 7365 char *name;
e142c38c 7366 CORE_ADDR baseaddr;
801e3a5b 7367 struct block *block;
edb3359d 7368 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
7369 VEC (symbolp) *template_args = NULL;
7370 struct template_symbol *templ_func = NULL;
edb3359d
DJ
7371
7372 if (inlined_func)
7373 {
7374 /* If we do not have call site information, we can't show the
7375 caller of this inlined function. That's too confusing, so
7376 only use the scope for local variables. */
7377 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7378 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7379 if (call_line == NULL || call_file == NULL)
7380 {
7381 read_lexical_block_scope (die, cu);
7382 return;
7383 }
7384 }
c906108c 7385
e142c38c
DJ
7386 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7387
94af9270 7388 name = dwarf2_name (die, cu);
c906108c 7389
e8d05480
JB
7390 /* Ignore functions with missing or empty names. These are actually
7391 illegal according to the DWARF standard. */
7392 if (name == NULL)
7393 {
7394 complaint (&symfile_complaints,
b64f50a1
JK
7395 _("missing name for subprogram DIE at %d"),
7396 die->offset.sect_off);
e8d05480
JB
7397 return;
7398 }
7399
7400 /* Ignore functions with missing or invalid low and high pc attributes. */
7401 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7402 {
ae4d0c03
PM
7403 attr = dwarf2_attr (die, DW_AT_external, cu);
7404 if (!attr || !DW_UNSND (attr))
7405 complaint (&symfile_complaints,
3e43a32a
MS
7406 _("cannot get low and high bounds "
7407 "for subprogram DIE at %d"),
b64f50a1 7408 die->offset.sect_off);
e8d05480
JB
7409 return;
7410 }
c906108c
SS
7411
7412 lowpc += baseaddr;
7413 highpc += baseaddr;
7414
34eaf542
TT
7415 /* If we have any template arguments, then we must allocate a
7416 different sort of symbol. */
7417 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7418 {
7419 if (child_die->tag == DW_TAG_template_type_param
7420 || child_die->tag == DW_TAG_template_value_param)
7421 {
7422 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7423 struct template_symbol);
7424 templ_func->base.is_cplus_template_function = 1;
7425 break;
7426 }
7427 }
7428
c906108c 7429 new = push_context (0, lowpc);
34eaf542
TT
7430 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7431 (struct symbol *) templ_func);
4c2df51b 7432
4cecd739
DJ
7433 /* If there is a location expression for DW_AT_frame_base, record
7434 it. */
e142c38c 7435 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 7436 if (attr)
c034e007
AC
7437 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7438 expression is being recorded directly in the function's symbol
7439 and not in a separate frame-base object. I guess this hack is
7440 to avoid adding some sort of frame-base adjunct/annex to the
7441 function's symbol :-(. The problem with doing this is that it
7442 results in a function symbol with a location expression that
7443 has nothing to do with the location of the function, ouch! The
7444 relationship should be: a function's symbol has-a frame base; a
7445 frame-base has-a location expression. */
e7c27a73 7446 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 7447
e142c38c 7448 cu->list_in_scope = &local_symbols;
c906108c 7449
639d11d3 7450 if (die->child != NULL)
c906108c 7451 {
639d11d3 7452 child_die = die->child;
c906108c
SS
7453 while (child_die && child_die->tag)
7454 {
34eaf542
TT
7455 if (child_die->tag == DW_TAG_template_type_param
7456 || child_die->tag == DW_TAG_template_value_param)
7457 {
7458 struct symbol *arg = new_symbol (child_die, NULL, cu);
7459
f1078f66
DJ
7460 if (arg != NULL)
7461 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7462 }
7463 else
7464 process_die (child_die, cu);
c906108c
SS
7465 child_die = sibling_die (child_die);
7466 }
7467 }
7468
d389af10
JK
7469 inherit_abstract_dies (die, cu);
7470
4a811a97
UW
7471 /* If we have a DW_AT_specification, we might need to import using
7472 directives from the context of the specification DIE. See the
7473 comment in determine_prefix. */
7474 if (cu->language == language_cplus
7475 && dwarf2_attr (die, DW_AT_specification, cu))
7476 {
7477 struct dwarf2_cu *spec_cu = cu;
7478 struct die_info *spec_die = die_specification (die, &spec_cu);
7479
7480 while (spec_die)
7481 {
7482 child_die = spec_die->child;
7483 while (child_die && child_die->tag)
7484 {
7485 if (child_die->tag == DW_TAG_imported_module)
7486 process_die (child_die, spec_cu);
7487 child_die = sibling_die (child_die);
7488 }
7489
7490 /* In some cases, GCC generates specification DIEs that
7491 themselves contain DW_AT_specification attributes. */
7492 spec_die = die_specification (spec_die, &spec_cu);
7493 }
7494 }
7495
c906108c
SS
7496 new = pop_context ();
7497 /* Make a block for the local symbols within. */
801e3a5b
JB
7498 block = finish_block (new->name, &local_symbols, new->old_blocks,
7499 lowpc, highpc, objfile);
7500
df8a16a1 7501 /* For C++, set the block's scope. */
f55ee35c 7502 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 7503 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 7504 determine_prefix (die, cu),
df8a16a1
DJ
7505 processing_has_namespace_info);
7506
801e3a5b
JB
7507 /* If we have address ranges, record them. */
7508 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 7509
34eaf542
TT
7510 /* Attach template arguments to function. */
7511 if (! VEC_empty (symbolp, template_args))
7512 {
7513 gdb_assert (templ_func != NULL);
7514
7515 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7516 templ_func->template_arguments
7517 = obstack_alloc (&objfile->objfile_obstack,
7518 (templ_func->n_template_arguments
7519 * sizeof (struct symbol *)));
7520 memcpy (templ_func->template_arguments,
7521 VEC_address (symbolp, template_args),
7522 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7523 VEC_free (symbolp, template_args);
7524 }
7525
208d8187
JB
7526 /* In C++, we can have functions nested inside functions (e.g., when
7527 a function declares a class that has methods). This means that
7528 when we finish processing a function scope, we may need to go
7529 back to building a containing block's symbol lists. */
7530 local_symbols = new->locals;
7531 param_symbols = new->params;
27aa8d6a 7532 using_directives = new->using_directives;
208d8187 7533
921e78cf
JB
7534 /* If we've finished processing a top-level function, subsequent
7535 symbols go in the file symbol list. */
7536 if (outermost_context_p ())
e142c38c 7537 cu->list_in_scope = &file_symbols;
c906108c
SS
7538}
7539
7540/* Process all the DIES contained within a lexical block scope. Start
7541 a new scope, process the dies, and then close the scope. */
7542
7543static void
e7c27a73 7544read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7545{
e7c27a73 7546 struct objfile *objfile = cu->objfile;
52f0bd74 7547 struct context_stack *new;
c906108c
SS
7548 CORE_ADDR lowpc, highpc;
7549 struct die_info *child_die;
e142c38c
DJ
7550 CORE_ADDR baseaddr;
7551
7552 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
7553
7554 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
7555 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7556 as multiple lexical blocks? Handling children in a sane way would
6e70227d 7557 be nasty. Might be easier to properly extend generic blocks to
af34e669 7558 describe ranges. */
d85a05f0 7559 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
7560 return;
7561 lowpc += baseaddr;
7562 highpc += baseaddr;
7563
7564 push_context (0, lowpc);
639d11d3 7565 if (die->child != NULL)
c906108c 7566 {
639d11d3 7567 child_die = die->child;
c906108c
SS
7568 while (child_die && child_die->tag)
7569 {
e7c27a73 7570 process_die (child_die, cu);
c906108c
SS
7571 child_die = sibling_die (child_die);
7572 }
7573 }
7574 new = pop_context ();
7575
8540c487 7576 if (local_symbols != NULL || using_directives != NULL)
c906108c 7577 {
801e3a5b
JB
7578 struct block *block
7579 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7580 highpc, objfile);
7581
7582 /* Note that recording ranges after traversing children, as we
7583 do here, means that recording a parent's ranges entails
7584 walking across all its children's ranges as they appear in
7585 the address map, which is quadratic behavior.
7586
7587 It would be nicer to record the parent's ranges before
7588 traversing its children, simply overriding whatever you find
7589 there. But since we don't even decide whether to create a
7590 block until after we've traversed its children, that's hard
7591 to do. */
7592 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
7593 }
7594 local_symbols = new->locals;
27aa8d6a 7595 using_directives = new->using_directives;
c906108c
SS
7596}
7597
96408a79
SA
7598/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7599
7600static void
7601read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7602{
7603 struct objfile *objfile = cu->objfile;
7604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7605 CORE_ADDR pc, baseaddr;
7606 struct attribute *attr;
7607 struct call_site *call_site, call_site_local;
7608 void **slot;
7609 int nparams;
7610 struct die_info *child_die;
7611
7612 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7613
7614 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7615 if (!attr)
7616 {
7617 complaint (&symfile_complaints,
7618 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7619 "DIE 0x%x [in module %s]"),
b64f50a1 7620 die->offset.sect_off, objfile->name);
96408a79
SA
7621 return;
7622 }
7623 pc = DW_ADDR (attr) + baseaddr;
7624
7625 if (cu->call_site_htab == NULL)
7626 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7627 NULL, &objfile->objfile_obstack,
7628 hashtab_obstack_allocate, NULL);
7629 call_site_local.pc = pc;
7630 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7631 if (*slot != NULL)
7632 {
7633 complaint (&symfile_complaints,
7634 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7635 "DIE 0x%x [in module %s]"),
b64f50a1 7636 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
7637 return;
7638 }
7639
7640 /* Count parameters at the caller. */
7641
7642 nparams = 0;
7643 for (child_die = die->child; child_die && child_die->tag;
7644 child_die = sibling_die (child_die))
7645 {
7646 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7647 {
7648 complaint (&symfile_complaints,
7649 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7650 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7651 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
7652 continue;
7653 }
7654
7655 nparams++;
7656 }
7657
7658 call_site = obstack_alloc (&objfile->objfile_obstack,
7659 (sizeof (*call_site)
7660 + (sizeof (*call_site->parameter)
7661 * (nparams - 1))));
7662 *slot = call_site;
7663 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7664 call_site->pc = pc;
7665
7666 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7667 {
7668 struct die_info *func_die;
7669
7670 /* Skip also over DW_TAG_inlined_subroutine. */
7671 for (func_die = die->parent;
7672 func_die && func_die->tag != DW_TAG_subprogram
7673 && func_die->tag != DW_TAG_subroutine_type;
7674 func_die = func_die->parent);
7675
7676 /* DW_AT_GNU_all_call_sites is a superset
7677 of DW_AT_GNU_all_tail_call_sites. */
7678 if (func_die
7679 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7680 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7681 {
7682 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7683 not complete. But keep CALL_SITE for look ups via call_site_htab,
7684 both the initial caller containing the real return address PC and
7685 the final callee containing the current PC of a chain of tail
7686 calls do not need to have the tail call list complete. But any
7687 function candidate for a virtual tail call frame searched via
7688 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7689 determined unambiguously. */
7690 }
7691 else
7692 {
7693 struct type *func_type = NULL;
7694
7695 if (func_die)
7696 func_type = get_die_type (func_die, cu);
7697 if (func_type != NULL)
7698 {
7699 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7700
7701 /* Enlist this call site to the function. */
7702 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7703 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7704 }
7705 else
7706 complaint (&symfile_complaints,
7707 _("Cannot find function owning DW_TAG_GNU_call_site "
7708 "DIE 0x%x [in module %s]"),
b64f50a1 7709 die->offset.sect_off, objfile->name);
96408a79
SA
7710 }
7711 }
7712
7713 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7714 if (attr == NULL)
7715 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7716 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7717 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7718 /* Keep NULL DWARF_BLOCK. */;
7719 else if (attr_form_is_block (attr))
7720 {
7721 struct dwarf2_locexpr_baton *dlbaton;
7722
7723 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7724 dlbaton->data = DW_BLOCK (attr)->data;
7725 dlbaton->size = DW_BLOCK (attr)->size;
7726 dlbaton->per_cu = cu->per_cu;
7727
7728 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7729 }
7730 else if (is_ref_attr (attr))
7731 {
96408a79
SA
7732 struct dwarf2_cu *target_cu = cu;
7733 struct die_info *target_die;
7734
7735 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7736 gdb_assert (target_cu->objfile == objfile);
7737 if (die_is_declaration (target_die, target_cu))
7738 {
7739 const char *target_physname;
7740
7741 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7742 if (target_physname == NULL)
7743 complaint (&symfile_complaints,
7744 _("DW_AT_GNU_call_site_target target DIE has invalid "
7745 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 7746 die->offset.sect_off, objfile->name);
96408a79
SA
7747 else
7748 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7749 }
7750 else
7751 {
7752 CORE_ADDR lowpc;
7753
7754 /* DW_AT_entry_pc should be preferred. */
7755 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7756 complaint (&symfile_complaints,
7757 _("DW_AT_GNU_call_site_target target DIE has invalid "
7758 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 7759 die->offset.sect_off, objfile->name);
96408a79
SA
7760 else
7761 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7762 }
7763 }
7764 else
7765 complaint (&symfile_complaints,
7766 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7767 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 7768 die->offset.sect_off, objfile->name);
96408a79
SA
7769
7770 call_site->per_cu = cu->per_cu;
7771
7772 for (child_die = die->child;
7773 child_die && child_die->tag;
7774 child_die = sibling_die (child_die))
7775 {
96408a79
SA
7776 struct call_site_parameter *parameter;
7777
7778 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7779 {
7780 /* Already printed the complaint above. */
7781 continue;
7782 }
7783
7784 gdb_assert (call_site->parameter_count < nparams);
7785 parameter = &call_site->parameter[call_site->parameter_count];
7786
7787 /* DW_AT_location specifies the register number. Value of the data
7788 assumed for the register is contained in DW_AT_GNU_call_site_value. */
7789
7790 attr = dwarf2_attr (child_die, DW_AT_location, cu);
7791 if (!attr || !attr_form_is_block (attr))
7792 {
7793 complaint (&symfile_complaints,
7794 _("No DW_FORM_block* DW_AT_location for "
7795 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7796 child_die->offset.sect_off, objfile->name);
96408a79
SA
7797 continue;
7798 }
7799 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
7800 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
7801 if (parameter->dwarf_reg == -1
7802 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
7803 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
7804 &parameter->fb_offset))
7805 {
7806 complaint (&symfile_complaints,
7807 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
7808 "for DW_FORM_block* DW_AT_location for "
7809 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7810 child_die->offset.sect_off, objfile->name);
96408a79
SA
7811 continue;
7812 }
7813
7814 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
7815 if (!attr_form_is_block (attr))
7816 {
7817 complaint (&symfile_complaints,
7818 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
7819 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7820 child_die->offset.sect_off, objfile->name);
96408a79
SA
7821 continue;
7822 }
7823 parameter->value = DW_BLOCK (attr)->data;
7824 parameter->value_size = DW_BLOCK (attr)->size;
7825
7826 /* Parameters are not pre-cleared by memset above. */
7827 parameter->data_value = NULL;
7828 parameter->data_value_size = 0;
7829 call_site->parameter_count++;
7830
7831 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
7832 if (attr)
7833 {
7834 if (!attr_form_is_block (attr))
7835 complaint (&symfile_complaints,
7836 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
7837 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7838 child_die->offset.sect_off, objfile->name);
96408a79
SA
7839 else
7840 {
7841 parameter->data_value = DW_BLOCK (attr)->data;
7842 parameter->data_value_size = DW_BLOCK (attr)->size;
7843 }
7844 }
7845 }
7846}
7847
43039443 7848/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
7849 Return 1 if the attributes are present and valid, otherwise, return 0.
7850 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
7851
7852static int
7853dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
7854 CORE_ADDR *high_return, struct dwarf2_cu *cu,
7855 struct partial_symtab *ranges_pst)
43039443
JK
7856{
7857 struct objfile *objfile = cu->objfile;
7858 struct comp_unit_head *cu_header = &cu->header;
7859 bfd *obfd = objfile->obfd;
7860 unsigned int addr_size = cu_header->addr_size;
7861 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7862 /* Base address selection entry. */
7863 CORE_ADDR base;
7864 int found_base;
7865 unsigned int dummy;
7866 gdb_byte *buffer;
7867 CORE_ADDR marker;
7868 int low_set;
7869 CORE_ADDR low = 0;
7870 CORE_ADDR high = 0;
ff013f42 7871 CORE_ADDR baseaddr;
43039443 7872
d00adf39
DE
7873 found_base = cu->base_known;
7874 base = cu->base_address;
43039443 7875
be391dca 7876 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 7877 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
7878 {
7879 complaint (&symfile_complaints,
7880 _("Offset %d out of bounds for DW_AT_ranges attribute"),
7881 offset);
7882 return 0;
7883 }
dce234bc 7884 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
7885
7886 /* Read in the largest possible address. */
7887 marker = read_address (obfd, buffer, cu, &dummy);
7888 if ((marker & mask) == mask)
7889 {
7890 /* If we found the largest possible address, then
7891 read the base address. */
7892 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7893 buffer += 2 * addr_size;
7894 offset += 2 * addr_size;
7895 found_base = 1;
7896 }
7897
7898 low_set = 0;
7899
e7030f15 7900 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 7901
43039443
JK
7902 while (1)
7903 {
7904 CORE_ADDR range_beginning, range_end;
7905
7906 range_beginning = read_address (obfd, buffer, cu, &dummy);
7907 buffer += addr_size;
7908 range_end = read_address (obfd, buffer, cu, &dummy);
7909 buffer += addr_size;
7910 offset += 2 * addr_size;
7911
7912 /* An end of list marker is a pair of zero addresses. */
7913 if (range_beginning == 0 && range_end == 0)
7914 /* Found the end of list entry. */
7915 break;
7916
7917 /* Each base address selection entry is a pair of 2 values.
7918 The first is the largest possible address, the second is
7919 the base address. Check for a base address here. */
7920 if ((range_beginning & mask) == mask)
7921 {
7922 /* If we found the largest possible address, then
7923 read the base address. */
7924 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7925 found_base = 1;
7926 continue;
7927 }
7928
7929 if (!found_base)
7930 {
7931 /* We have no valid base address for the ranges
7932 data. */
7933 complaint (&symfile_complaints,
7934 _("Invalid .debug_ranges data (no base address)"));
7935 return 0;
7936 }
7937
9277c30c
UW
7938 if (range_beginning > range_end)
7939 {
7940 /* Inverted range entries are invalid. */
7941 complaint (&symfile_complaints,
7942 _("Invalid .debug_ranges data (inverted range)"));
7943 return 0;
7944 }
7945
7946 /* Empty range entries have no effect. */
7947 if (range_beginning == range_end)
7948 continue;
7949
43039443
JK
7950 range_beginning += base;
7951 range_end += base;
7952
9277c30c 7953 if (ranges_pst != NULL)
ff013f42 7954 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
7955 range_beginning + baseaddr,
7956 range_end - 1 + baseaddr,
ff013f42
JK
7957 ranges_pst);
7958
43039443
JK
7959 /* FIXME: This is recording everything as a low-high
7960 segment of consecutive addresses. We should have a
7961 data structure for discontiguous block ranges
7962 instead. */
7963 if (! low_set)
7964 {
7965 low = range_beginning;
7966 high = range_end;
7967 low_set = 1;
7968 }
7969 else
7970 {
7971 if (range_beginning < low)
7972 low = range_beginning;
7973 if (range_end > high)
7974 high = range_end;
7975 }
7976 }
7977
7978 if (! low_set)
7979 /* If the first entry is an end-of-list marker, the range
7980 describes an empty scope, i.e. no instructions. */
7981 return 0;
7982
7983 if (low_return)
7984 *low_return = low;
7985 if (high_return)
7986 *high_return = high;
7987 return 1;
7988}
7989
af34e669
DJ
7990/* Get low and high pc attributes from a die. Return 1 if the attributes
7991 are present and valid, otherwise, return 0. Return -1 if the range is
7992 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 7993
c906108c 7994static int
af34e669 7995dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
7996 CORE_ADDR *highpc, struct dwarf2_cu *cu,
7997 struct partial_symtab *pst)
c906108c
SS
7998{
7999 struct attribute *attr;
91da1414 8000 struct attribute *attr_high;
af34e669
DJ
8001 CORE_ADDR low = 0;
8002 CORE_ADDR high = 0;
8003 int ret = 0;
c906108c 8004
91da1414
MW
8005 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8006 if (attr_high)
af34e669 8007 {
e142c38c 8008 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 8009 if (attr)
91da1414
MW
8010 {
8011 low = DW_ADDR (attr);
3019eac3
DE
8012 if (attr_high->form == DW_FORM_addr
8013 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8014 high = DW_ADDR (attr_high);
8015 else
8016 high = low + DW_UNSND (attr_high);
8017 }
af34e669
DJ
8018 else
8019 /* Found high w/o low attribute. */
8020 return 0;
8021
8022 /* Found consecutive range of addresses. */
8023 ret = 1;
8024 }
c906108c 8025 else
af34e669 8026 {
e142c38c 8027 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
8028 if (attr != NULL)
8029 {
af34e669 8030 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 8031 .debug_ranges section. */
d85a05f0 8032 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 8033 return 0;
43039443 8034 /* Found discontinuous range of addresses. */
af34e669
DJ
8035 ret = -1;
8036 }
8037 }
c906108c 8038
9373cf26
JK
8039 /* read_partial_die has also the strict LOW < HIGH requirement. */
8040 if (high <= low)
c906108c
SS
8041 return 0;
8042
8043 /* When using the GNU linker, .gnu.linkonce. sections are used to
8044 eliminate duplicate copies of functions and vtables and such.
8045 The linker will arbitrarily choose one and discard the others.
8046 The AT_*_pc values for such functions refer to local labels in
8047 these sections. If the section from that file was discarded, the
8048 labels are not in the output, so the relocs get a value of 0.
8049 If this is a discarded function, mark the pc bounds as invalid,
8050 so that GDB will ignore it. */
72dca2f5 8051 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
8052 return 0;
8053
8054 *lowpc = low;
96408a79
SA
8055 if (highpc)
8056 *highpc = high;
af34e669 8057 return ret;
c906108c
SS
8058}
8059
b084d499
JB
8060/* Assuming that DIE represents a subprogram DIE or a lexical block, get
8061 its low and high PC addresses. Do nothing if these addresses could not
8062 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8063 and HIGHPC to the high address if greater than HIGHPC. */
8064
8065static void
8066dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8067 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8068 struct dwarf2_cu *cu)
8069{
8070 CORE_ADDR low, high;
8071 struct die_info *child = die->child;
8072
d85a05f0 8073 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
8074 {
8075 *lowpc = min (*lowpc, low);
8076 *highpc = max (*highpc, high);
8077 }
8078
8079 /* If the language does not allow nested subprograms (either inside
8080 subprograms or lexical blocks), we're done. */
8081 if (cu->language != language_ada)
8082 return;
6e70227d 8083
b084d499
JB
8084 /* Check all the children of the given DIE. If it contains nested
8085 subprograms, then check their pc bounds. Likewise, we need to
8086 check lexical blocks as well, as they may also contain subprogram
8087 definitions. */
8088 while (child && child->tag)
8089 {
8090 if (child->tag == DW_TAG_subprogram
8091 || child->tag == DW_TAG_lexical_block)
8092 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8093 child = sibling_die (child);
8094 }
8095}
8096
fae299cd
DC
8097/* Get the low and high pc's represented by the scope DIE, and store
8098 them in *LOWPC and *HIGHPC. If the correct values can't be
8099 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8100
8101static void
8102get_scope_pc_bounds (struct die_info *die,
8103 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8104 struct dwarf2_cu *cu)
8105{
8106 CORE_ADDR best_low = (CORE_ADDR) -1;
8107 CORE_ADDR best_high = (CORE_ADDR) 0;
8108 CORE_ADDR current_low, current_high;
8109
d85a05f0 8110 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
8111 {
8112 best_low = current_low;
8113 best_high = current_high;
8114 }
8115 else
8116 {
8117 struct die_info *child = die->child;
8118
8119 while (child && child->tag)
8120 {
8121 switch (child->tag) {
8122 case DW_TAG_subprogram:
b084d499 8123 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
8124 break;
8125 case DW_TAG_namespace:
f55ee35c 8126 case DW_TAG_module:
fae299cd
DC
8127 /* FIXME: carlton/2004-01-16: Should we do this for
8128 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8129 that current GCC's always emit the DIEs corresponding
8130 to definitions of methods of classes as children of a
8131 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8132 the DIEs giving the declarations, which could be
8133 anywhere). But I don't see any reason why the
8134 standards says that they have to be there. */
8135 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8136
8137 if (current_low != ((CORE_ADDR) -1))
8138 {
8139 best_low = min (best_low, current_low);
8140 best_high = max (best_high, current_high);
8141 }
8142 break;
8143 default:
0963b4bd 8144 /* Ignore. */
fae299cd
DC
8145 break;
8146 }
8147
8148 child = sibling_die (child);
8149 }
8150 }
8151
8152 *lowpc = best_low;
8153 *highpc = best_high;
8154}
8155
801e3a5b
JB
8156/* Record the address ranges for BLOCK, offset by BASEADDR, as given
8157 in DIE. */
380bca97 8158
801e3a5b
JB
8159static void
8160dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8161 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8162{
bb5ed363 8163 struct objfile *objfile = cu->objfile;
801e3a5b 8164 struct attribute *attr;
91da1414 8165 struct attribute *attr_high;
801e3a5b 8166
91da1414
MW
8167 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8168 if (attr_high)
801e3a5b 8169 {
801e3a5b
JB
8170 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8171 if (attr)
8172 {
8173 CORE_ADDR low = DW_ADDR (attr);
91da1414 8174 CORE_ADDR high;
3019eac3
DE
8175 if (attr_high->form == DW_FORM_addr
8176 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8177 high = DW_ADDR (attr_high);
8178 else
8179 high = low + DW_UNSND (attr_high);
9a619af0 8180
801e3a5b
JB
8181 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8182 }
8183 }
8184
8185 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8186 if (attr)
8187 {
bb5ed363 8188 bfd *obfd = objfile->obfd;
801e3a5b
JB
8189
8190 /* The value of the DW_AT_ranges attribute is the offset of the
8191 address range list in the .debug_ranges section. */
8192 unsigned long offset = DW_UNSND (attr);
dce234bc 8193 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
8194
8195 /* For some target architectures, but not others, the
8196 read_address function sign-extends the addresses it returns.
8197 To recognize base address selection entries, we need a
8198 mask. */
8199 unsigned int addr_size = cu->header.addr_size;
8200 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8201
8202 /* The base address, to which the next pair is relative. Note
8203 that this 'base' is a DWARF concept: most entries in a range
8204 list are relative, to reduce the number of relocs against the
8205 debugging information. This is separate from this function's
8206 'baseaddr' argument, which GDB uses to relocate debugging
8207 information from a shared library based on the address at
8208 which the library was loaded. */
d00adf39
DE
8209 CORE_ADDR base = cu->base_address;
8210 int base_known = cu->base_known;
801e3a5b 8211
be391dca 8212 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 8213 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
8214 {
8215 complaint (&symfile_complaints,
8216 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8217 offset);
8218 return;
8219 }
8220
8221 for (;;)
8222 {
8223 unsigned int bytes_read;
8224 CORE_ADDR start, end;
8225
8226 start = read_address (obfd, buffer, cu, &bytes_read);
8227 buffer += bytes_read;
8228 end = read_address (obfd, buffer, cu, &bytes_read);
8229 buffer += bytes_read;
8230
8231 /* Did we find the end of the range list? */
8232 if (start == 0 && end == 0)
8233 break;
8234
8235 /* Did we find a base address selection entry? */
8236 else if ((start & base_select_mask) == base_select_mask)
8237 {
8238 base = end;
8239 base_known = 1;
8240 }
8241
8242 /* We found an ordinary address range. */
8243 else
8244 {
8245 if (!base_known)
8246 {
8247 complaint (&symfile_complaints,
3e43a32a
MS
8248 _("Invalid .debug_ranges data "
8249 "(no base address)"));
801e3a5b
JB
8250 return;
8251 }
8252
9277c30c
UW
8253 if (start > end)
8254 {
8255 /* Inverted range entries are invalid. */
8256 complaint (&symfile_complaints,
8257 _("Invalid .debug_ranges data "
8258 "(inverted range)"));
8259 return;
8260 }
8261
8262 /* Empty range entries have no effect. */
8263 if (start == end)
8264 continue;
8265
6e70227d
DE
8266 record_block_range (block,
8267 baseaddr + base + start,
801e3a5b
JB
8268 baseaddr + base + end - 1);
8269 }
8270 }
8271 }
8272}
8273
60d5a603
JK
8274/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8275 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8276 during 4.6.0 experimental. */
8277
8278static int
8279producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8280{
8281 const char *cs;
8282 int major, minor, release;
ba919b58 8283 int result = 0;
60d5a603
JK
8284
8285 if (cu->producer == NULL)
8286 {
8287 /* For unknown compilers expect their behavior is DWARF version
8288 compliant.
8289
8290 GCC started to support .debug_types sections by -gdwarf-4 since
8291 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8292 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8293 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8294 interpreted incorrectly by GDB now - GCC PR debug/48229. */
8295
8296 return 0;
8297 }
8298
ba919b58
TT
8299 if (cu->checked_producer)
8300 return cu->producer_is_gxx_lt_4_6;
8301
60d5a603
JK
8302 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8303
8304 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
8305 {
8306 /* For non-GCC compilers expect their behavior is DWARF version
8307 compliant. */
60d5a603 8308 }
ba919b58 8309 else
60d5a603 8310 {
ba919b58
TT
8311 cs = &cu->producer[strlen ("GNU ")];
8312 while (*cs && !isdigit (*cs))
8313 cs++;
8314 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8315 {
8316 /* Not recognized as GCC. */
8317 }
8318 else
8319 result = major < 4 || (major == 4 && minor < 6);
60d5a603
JK
8320 }
8321
ba919b58
TT
8322 cu->checked_producer = 1;
8323 cu->producer_is_gxx_lt_4_6 = result;
8324
8325 return result;
60d5a603
JK
8326}
8327
8328/* Return the default accessibility type if it is not overriden by
8329 DW_AT_accessibility. */
8330
8331static enum dwarf_access_attribute
8332dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8333{
8334 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8335 {
8336 /* The default DWARF 2 accessibility for members is public, the default
8337 accessibility for inheritance is private. */
8338
8339 if (die->tag != DW_TAG_inheritance)
8340 return DW_ACCESS_public;
8341 else
8342 return DW_ACCESS_private;
8343 }
8344 else
8345 {
8346 /* DWARF 3+ defines the default accessibility a different way. The same
8347 rules apply now for DW_TAG_inheritance as for the members and it only
8348 depends on the container kind. */
8349
8350 if (die->parent->tag == DW_TAG_class_type)
8351 return DW_ACCESS_private;
8352 else
8353 return DW_ACCESS_public;
8354 }
8355}
8356
74ac6d43
TT
8357/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8358 offset. If the attribute was not found return 0, otherwise return
8359 1. If it was found but could not properly be handled, set *OFFSET
8360 to 0. */
8361
8362static int
8363handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8364 LONGEST *offset)
8365{
8366 struct attribute *attr;
8367
8368 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8369 if (attr != NULL)
8370 {
8371 *offset = 0;
8372
8373 /* Note that we do not check for a section offset first here.
8374 This is because DW_AT_data_member_location is new in DWARF 4,
8375 so if we see it, we can assume that a constant form is really
8376 a constant and not a section offset. */
8377 if (attr_form_is_constant (attr))
8378 *offset = dwarf2_get_attr_constant_value (attr, 0);
8379 else if (attr_form_is_section_offset (attr))
8380 dwarf2_complex_location_expr_complaint ();
8381 else if (attr_form_is_block (attr))
8382 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8383 else
8384 dwarf2_complex_location_expr_complaint ();
8385
8386 return 1;
8387 }
8388
8389 return 0;
8390}
8391
c906108c
SS
8392/* Add an aggregate field to the field list. */
8393
8394static void
107d2387 8395dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 8396 struct dwarf2_cu *cu)
6e70227d 8397{
e7c27a73 8398 struct objfile *objfile = cu->objfile;
5e2b427d 8399 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8400 struct nextfield *new_field;
8401 struct attribute *attr;
8402 struct field *fp;
8403 char *fieldname = "";
8404
8405 /* Allocate a new field list entry and link it in. */
8406 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 8407 make_cleanup (xfree, new_field);
c906108c 8408 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
8409
8410 if (die->tag == DW_TAG_inheritance)
8411 {
8412 new_field->next = fip->baseclasses;
8413 fip->baseclasses = new_field;
8414 }
8415 else
8416 {
8417 new_field->next = fip->fields;
8418 fip->fields = new_field;
8419 }
c906108c
SS
8420 fip->nfields++;
8421
e142c38c 8422 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
8423 if (attr)
8424 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
8425 else
8426 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
8427 if (new_field->accessibility != DW_ACCESS_public)
8428 fip->non_public_fields = 1;
60d5a603 8429
e142c38c 8430 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
8431 if (attr)
8432 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
8433 else
8434 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
8435
8436 fp = &new_field->field;
a9a9bd0f 8437
e142c38c 8438 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 8439 {
74ac6d43
TT
8440 LONGEST offset;
8441
a9a9bd0f 8442 /* Data member other than a C++ static data member. */
6e70227d 8443
c906108c 8444 /* Get type of field. */
e7c27a73 8445 fp->type = die_type (die, cu);
c906108c 8446
d6a843b5 8447 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 8448
c906108c 8449 /* Get bit size of field (zero if none). */
e142c38c 8450 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
8451 if (attr)
8452 {
8453 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8454 }
8455 else
8456 {
8457 FIELD_BITSIZE (*fp) = 0;
8458 }
8459
8460 /* Get bit offset of field. */
74ac6d43
TT
8461 if (handle_data_member_location (die, cu, &offset))
8462 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 8463 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
8464 if (attr)
8465 {
5e2b427d 8466 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
8467 {
8468 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
8469 additional bit offset from the MSB of the containing
8470 anonymous object to the MSB of the field. We don't
8471 have to do anything special since we don't need to
8472 know the size of the anonymous object. */
f41f5e61 8473 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
8474 }
8475 else
8476 {
8477 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
8478 MSB of the anonymous object, subtract off the number of
8479 bits from the MSB of the field to the MSB of the
8480 object, and then subtract off the number of bits of
8481 the field itself. The result is the bit offset of
8482 the LSB of the field. */
c906108c
SS
8483 int anonymous_size;
8484 int bit_offset = DW_UNSND (attr);
8485
e142c38c 8486 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8487 if (attr)
8488 {
8489 /* The size of the anonymous object containing
8490 the bit field is explicit, so use the
8491 indicated size (in bytes). */
8492 anonymous_size = DW_UNSND (attr);
8493 }
8494 else
8495 {
8496 /* The size of the anonymous object containing
8497 the bit field must be inferred from the type
8498 attribute of the data member containing the
8499 bit field. */
8500 anonymous_size = TYPE_LENGTH (fp->type);
8501 }
f41f5e61
PA
8502 SET_FIELD_BITPOS (*fp,
8503 (FIELD_BITPOS (*fp)
8504 + anonymous_size * bits_per_byte
8505 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
8506 }
8507 }
8508
8509 /* Get name of field. */
39cbfefa
DJ
8510 fieldname = dwarf2_name (die, cu);
8511 if (fieldname == NULL)
8512 fieldname = "";
d8151005
DJ
8513
8514 /* The name is already allocated along with this objfile, so we don't
8515 need to duplicate it for the type. */
8516 fp->name = fieldname;
c906108c
SS
8517
8518 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 8519 pointer or virtual base class pointer) to private. */
e142c38c 8520 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 8521 {
d48cc9dd 8522 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
8523 new_field->accessibility = DW_ACCESS_private;
8524 fip->non_public_fields = 1;
8525 }
8526 }
a9a9bd0f 8527 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 8528 {
a9a9bd0f
DC
8529 /* C++ static member. */
8530
8531 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8532 is a declaration, but all versions of G++ as of this writing
8533 (so through at least 3.2.1) incorrectly generate
8534 DW_TAG_variable tags. */
6e70227d 8535
ff355380 8536 const char *physname;
c906108c 8537
a9a9bd0f 8538 /* Get name of field. */
39cbfefa
DJ
8539 fieldname = dwarf2_name (die, cu);
8540 if (fieldname == NULL)
c906108c
SS
8541 return;
8542
254e6b9e 8543 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
8544 if (attr
8545 /* Only create a symbol if this is an external value.
8546 new_symbol checks this and puts the value in the global symbol
8547 table, which we want. If it is not external, new_symbol
8548 will try to put the value in cu->list_in_scope which is wrong. */
8549 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
8550 {
8551 /* A static const member, not much different than an enum as far as
8552 we're concerned, except that we can support more types. */
8553 new_symbol (die, NULL, cu);
8554 }
8555
2df3850c 8556 /* Get physical name. */
ff355380 8557 physname = dwarf2_physname (fieldname, die, cu);
c906108c 8558
d8151005
DJ
8559 /* The name is already allocated along with this objfile, so we don't
8560 need to duplicate it for the type. */
8561 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 8562 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 8563 FIELD_NAME (*fp) = fieldname;
c906108c
SS
8564 }
8565 else if (die->tag == DW_TAG_inheritance)
8566 {
74ac6d43 8567 LONGEST offset;
d4b96c9a 8568
74ac6d43
TT
8569 /* C++ base class field. */
8570 if (handle_data_member_location (die, cu, &offset))
8571 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 8572 FIELD_BITSIZE (*fp) = 0;
e7c27a73 8573 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
8574 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8575 fip->nbaseclasses++;
8576 }
8577}
8578
98751a41
JK
8579/* Add a typedef defined in the scope of the FIP's class. */
8580
8581static void
8582dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8583 struct dwarf2_cu *cu)
6e70227d 8584{
98751a41 8585 struct objfile *objfile = cu->objfile;
98751a41
JK
8586 struct typedef_field_list *new_field;
8587 struct attribute *attr;
8588 struct typedef_field *fp;
8589 char *fieldname = "";
8590
8591 /* Allocate a new field list entry and link it in. */
8592 new_field = xzalloc (sizeof (*new_field));
8593 make_cleanup (xfree, new_field);
8594
8595 gdb_assert (die->tag == DW_TAG_typedef);
8596
8597 fp = &new_field->field;
8598
8599 /* Get name of field. */
8600 fp->name = dwarf2_name (die, cu);
8601 if (fp->name == NULL)
8602 return;
8603
8604 fp->type = read_type_die (die, cu);
8605
8606 new_field->next = fip->typedef_field_list;
8607 fip->typedef_field_list = new_field;
8608 fip->typedef_field_list_count++;
8609}
8610
c906108c
SS
8611/* Create the vector of fields, and attach it to the type. */
8612
8613static void
fba45db2 8614dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8615 struct dwarf2_cu *cu)
c906108c
SS
8616{
8617 int nfields = fip->nfields;
8618
8619 /* Record the field count, allocate space for the array of fields,
8620 and create blank accessibility bitfields if necessary. */
8621 TYPE_NFIELDS (type) = nfields;
8622 TYPE_FIELDS (type) = (struct field *)
8623 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8624 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8625
b4ba55a1 8626 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
8627 {
8628 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8629
8630 TYPE_FIELD_PRIVATE_BITS (type) =
8631 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8632 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8633
8634 TYPE_FIELD_PROTECTED_BITS (type) =
8635 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8636 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8637
774b6a14
TT
8638 TYPE_FIELD_IGNORE_BITS (type) =
8639 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8640 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
8641 }
8642
8643 /* If the type has baseclasses, allocate and clear a bit vector for
8644 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 8645 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
8646 {
8647 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 8648 unsigned char *pointer;
c906108c
SS
8649
8650 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
8651 pointer = TYPE_ALLOC (type, num_bytes);
8652 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
8653 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8654 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8655 }
8656
3e43a32a
MS
8657 /* Copy the saved-up fields into the field vector. Start from the head of
8658 the list, adding to the tail of the field array, so that they end up in
8659 the same order in the array in which they were added to the list. */
c906108c
SS
8660 while (nfields-- > 0)
8661 {
7d0ccb61
DJ
8662 struct nextfield *fieldp;
8663
8664 if (fip->fields)
8665 {
8666 fieldp = fip->fields;
8667 fip->fields = fieldp->next;
8668 }
8669 else
8670 {
8671 fieldp = fip->baseclasses;
8672 fip->baseclasses = fieldp->next;
8673 }
8674
8675 TYPE_FIELD (type, nfields) = fieldp->field;
8676 switch (fieldp->accessibility)
c906108c 8677 {
c5aa993b 8678 case DW_ACCESS_private:
b4ba55a1
JB
8679 if (cu->language != language_ada)
8680 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 8681 break;
c906108c 8682
c5aa993b 8683 case DW_ACCESS_protected:
b4ba55a1
JB
8684 if (cu->language != language_ada)
8685 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 8686 break;
c906108c 8687
c5aa993b
JM
8688 case DW_ACCESS_public:
8689 break;
c906108c 8690
c5aa993b
JM
8691 default:
8692 /* Unknown accessibility. Complain and treat it as public. */
8693 {
e2e0b3e5 8694 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 8695 fieldp->accessibility);
c5aa993b
JM
8696 }
8697 break;
c906108c
SS
8698 }
8699 if (nfields < fip->nbaseclasses)
8700 {
7d0ccb61 8701 switch (fieldp->virtuality)
c906108c 8702 {
c5aa993b
JM
8703 case DW_VIRTUALITY_virtual:
8704 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 8705 if (cu->language == language_ada)
a73c6dcd 8706 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
8707 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8708 break;
c906108c
SS
8709 }
8710 }
c906108c
SS
8711 }
8712}
8713
c906108c
SS
8714/* Add a member function to the proper fieldlist. */
8715
8716static void
107d2387 8717dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 8718 struct type *type, struct dwarf2_cu *cu)
c906108c 8719{
e7c27a73 8720 struct objfile *objfile = cu->objfile;
c906108c
SS
8721 struct attribute *attr;
8722 struct fnfieldlist *flp;
8723 int i;
8724 struct fn_field *fnp;
8725 char *fieldname;
c906108c 8726 struct nextfnfield *new_fnfield;
f792889a 8727 struct type *this_type;
60d5a603 8728 enum dwarf_access_attribute accessibility;
c906108c 8729
b4ba55a1 8730 if (cu->language == language_ada)
a73c6dcd 8731 error (_("unexpected member function in Ada type"));
b4ba55a1 8732
2df3850c 8733 /* Get name of member function. */
39cbfefa
DJ
8734 fieldname = dwarf2_name (die, cu);
8735 if (fieldname == NULL)
2df3850c 8736 return;
c906108c 8737
c906108c
SS
8738 /* Look up member function name in fieldlist. */
8739 for (i = 0; i < fip->nfnfields; i++)
8740 {
27bfe10e 8741 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
8742 break;
8743 }
8744
8745 /* Create new list element if necessary. */
8746 if (i < fip->nfnfields)
8747 flp = &fip->fnfieldlists[i];
8748 else
8749 {
8750 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8751 {
8752 fip->fnfieldlists = (struct fnfieldlist *)
8753 xrealloc (fip->fnfieldlists,
8754 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8755 * sizeof (struct fnfieldlist));
c906108c 8756 if (fip->nfnfields == 0)
c13c43fd 8757 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
8758 }
8759 flp = &fip->fnfieldlists[fip->nfnfields];
8760 flp->name = fieldname;
8761 flp->length = 0;
8762 flp->head = NULL;
3da10d80 8763 i = fip->nfnfields++;
c906108c
SS
8764 }
8765
8766 /* Create a new member function field and chain it to the field list
0963b4bd 8767 entry. */
c906108c 8768 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 8769 make_cleanup (xfree, new_fnfield);
c906108c
SS
8770 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8771 new_fnfield->next = flp->head;
8772 flp->head = new_fnfield;
8773 flp->length++;
8774
8775 /* Fill in the member function field info. */
8776 fnp = &new_fnfield->fnfield;
3da10d80
KS
8777
8778 /* Delay processing of the physname until later. */
8779 if (cu->language == language_cplus || cu->language == language_java)
8780 {
8781 add_to_method_list (type, i, flp->length - 1, fieldname,
8782 die, cu);
8783 }
8784 else
8785 {
1d06ead6 8786 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
8787 fnp->physname = physname ? physname : "";
8788 }
8789
c906108c 8790 fnp->type = alloc_type (objfile);
f792889a
DJ
8791 this_type = read_type_die (die, cu);
8792 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 8793 {
f792889a 8794 int nparams = TYPE_NFIELDS (this_type);
c906108c 8795
f792889a 8796 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
8797 of the method itself (TYPE_CODE_METHOD). */
8798 smash_to_method_type (fnp->type, type,
f792889a
DJ
8799 TYPE_TARGET_TYPE (this_type),
8800 TYPE_FIELDS (this_type),
8801 TYPE_NFIELDS (this_type),
8802 TYPE_VARARGS (this_type));
c906108c
SS
8803
8804 /* Handle static member functions.
c5aa993b 8805 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
8806 member functions. G++ helps GDB by marking the first
8807 parameter for non-static member functions (which is the this
8808 pointer) as artificial. We obtain this information from
8809 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 8810 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
8811 fnp->voffset = VOFFSET_STATIC;
8812 }
8813 else
e2e0b3e5 8814 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 8815 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
8816
8817 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 8818 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 8819 fnp->fcontext = die_containing_type (die, cu);
c906108c 8820
3e43a32a
MS
8821 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
8822 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
8823
8824 /* Get accessibility. */
e142c38c 8825 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 8826 if (attr)
60d5a603
JK
8827 accessibility = DW_UNSND (attr);
8828 else
8829 accessibility = dwarf2_default_access_attribute (die, cu);
8830 switch (accessibility)
c906108c 8831 {
60d5a603
JK
8832 case DW_ACCESS_private:
8833 fnp->is_private = 1;
8834 break;
8835 case DW_ACCESS_protected:
8836 fnp->is_protected = 1;
8837 break;
c906108c
SS
8838 }
8839
b02dede2 8840 /* Check for artificial methods. */
e142c38c 8841 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
8842 if (attr && DW_UNSND (attr) != 0)
8843 fnp->is_artificial = 1;
8844
0d564a31 8845 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
8846 function. For older versions of GCC, this is an offset in the
8847 appropriate virtual table, as specified by DW_AT_containing_type.
8848 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
8849 to the object address. */
8850
e142c38c 8851 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 8852 if (attr)
8e19ed76 8853 {
aec5aa8b 8854 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 8855 {
aec5aa8b
TT
8856 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
8857 {
8858 /* Old-style GCC. */
8859 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8860 }
8861 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
8862 || (DW_BLOCK (attr)->size > 1
8863 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
8864 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
8865 {
8866 struct dwarf_block blk;
8867 int offset;
8868
8869 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
8870 ? 1 : 2);
8871 blk.size = DW_BLOCK (attr)->size - offset;
8872 blk.data = DW_BLOCK (attr)->data + offset;
8873 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
8874 if ((fnp->voffset % cu->header.addr_size) != 0)
8875 dwarf2_complex_location_expr_complaint ();
8876 else
8877 fnp->voffset /= cu->header.addr_size;
8878 fnp->voffset += 2;
8879 }
8880 else
8881 dwarf2_complex_location_expr_complaint ();
8882
8883 if (!fnp->fcontext)
8884 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
8885 }
3690dd37 8886 else if (attr_form_is_section_offset (attr))
8e19ed76 8887 {
4d3c2250 8888 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8889 }
8890 else
8891 {
4d3c2250
KB
8892 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
8893 fieldname);
8e19ed76 8894 }
0d564a31 8895 }
d48cc9dd
DJ
8896 else
8897 {
8898 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8899 if (attr && DW_UNSND (attr))
8900 {
8901 /* GCC does this, as of 2008-08-25; PR debug/37237. */
8902 complaint (&symfile_complaints,
3e43a32a
MS
8903 _("Member function \"%s\" (offset %d) is virtual "
8904 "but the vtable offset is not specified"),
b64f50a1 8905 fieldname, die->offset.sect_off);
9655fd1a 8906 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
8907 TYPE_CPLUS_DYNAMIC (type) = 1;
8908 }
8909 }
c906108c
SS
8910}
8911
8912/* Create the vector of member function fields, and attach it to the type. */
8913
8914static void
fba45db2 8915dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8916 struct dwarf2_cu *cu)
c906108c
SS
8917{
8918 struct fnfieldlist *flp;
c906108c
SS
8919 int i;
8920
b4ba55a1 8921 if (cu->language == language_ada)
a73c6dcd 8922 error (_("unexpected member functions in Ada type"));
b4ba55a1 8923
c906108c
SS
8924 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8925 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
8926 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
8927
8928 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
8929 {
8930 struct nextfnfield *nfp = flp->head;
8931 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
8932 int k;
8933
8934 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
8935 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
8936 fn_flp->fn_fields = (struct fn_field *)
8937 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
8938 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 8939 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
8940 }
8941
8942 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
8943}
8944
1168df01
JB
8945/* Returns non-zero if NAME is the name of a vtable member in CU's
8946 language, zero otherwise. */
8947static int
8948is_vtable_name (const char *name, struct dwarf2_cu *cu)
8949{
8950 static const char vptr[] = "_vptr";
987504bb 8951 static const char vtable[] = "vtable";
1168df01 8952
987504bb
JJ
8953 /* Look for the C++ and Java forms of the vtable. */
8954 if ((cu->language == language_java
8955 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
8956 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
8957 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
8958 return 1;
8959
8960 return 0;
8961}
8962
c0dd20ea 8963/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
8964 functions, with the ABI-specified layout. If TYPE describes
8965 such a structure, smash it into a member function type.
61049d3b
DJ
8966
8967 GCC shouldn't do this; it should just output pointer to member DIEs.
8968 This is GCC PR debug/28767. */
c0dd20ea 8969
0b92b5bb
TT
8970static void
8971quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 8972{
0b92b5bb 8973 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
8974
8975 /* Check for a structure with no name and two children. */
0b92b5bb
TT
8976 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
8977 return;
c0dd20ea
DJ
8978
8979 /* Check for __pfn and __delta members. */
0b92b5bb
TT
8980 if (TYPE_FIELD_NAME (type, 0) == NULL
8981 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
8982 || TYPE_FIELD_NAME (type, 1) == NULL
8983 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
8984 return;
c0dd20ea
DJ
8985
8986 /* Find the type of the method. */
0b92b5bb 8987 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
8988 if (pfn_type == NULL
8989 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
8990 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 8991 return;
c0dd20ea
DJ
8992
8993 /* Look for the "this" argument. */
8994 pfn_type = TYPE_TARGET_TYPE (pfn_type);
8995 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 8996 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 8997 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 8998 return;
c0dd20ea
DJ
8999
9000 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
9001 new_type = alloc_type (objfile);
9002 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
9003 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
9004 TYPE_VARARGS (pfn_type));
0b92b5bb 9005 smash_to_methodptr_type (type, new_type);
c0dd20ea 9006}
1168df01 9007
c906108c 9008/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
9009 (definition) to create a type for the structure or union. Fill in
9010 the type's name and general properties; the members will not be
9011 processed until process_structure_type.
c906108c 9012
c767944b
DJ
9013 NOTE: we need to call these functions regardless of whether or not the
9014 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
9015 structure or union. This gets the type entered into our set of
9016 user defined types.
9017
9018 However, if the structure is incomplete (an opaque struct/union)
9019 then suppress creating a symbol table entry for it since gdb only
9020 wants to find the one with the complete definition. Note that if
9021 it is complete, we just call new_symbol, which does it's own
9022 checking about whether the struct/union is anonymous or not (and
9023 suppresses creating a symbol table entry itself). */
9024
f792889a 9025static struct type *
134d01f1 9026read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9027{
e7c27a73 9028 struct objfile *objfile = cu->objfile;
c906108c
SS
9029 struct type *type;
9030 struct attribute *attr;
39cbfefa 9031 char *name;
c906108c 9032
348e048f
DE
9033 /* If the definition of this type lives in .debug_types, read that type.
9034 Don't follow DW_AT_specification though, that will take us back up
9035 the chain and we want to go down. */
9036 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9037 if (attr)
9038 {
9039 struct dwarf2_cu *type_cu = cu;
9040 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9041
348e048f
DE
9042 /* We could just recurse on read_structure_type, but we need to call
9043 get_die_type to ensure only one type for this DIE is created.
9044 This is important, for example, because for c++ classes we need
9045 TYPE_NAME set which is only done by new_symbol. Blech. */
9046 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9047
9048 /* TYPE_CU may not be the same as CU.
9049 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9050 return set_die_type (die, type, cu);
9051 }
9052
c0dd20ea 9053 type = alloc_type (objfile);
c906108c 9054 INIT_CPLUS_SPECIFIC (type);
93311388 9055
39cbfefa
DJ
9056 name = dwarf2_name (die, cu);
9057 if (name != NULL)
c906108c 9058 {
987504bb
JJ
9059 if (cu->language == language_cplus
9060 || cu->language == language_java)
63d06c5c 9061 {
3da10d80
KS
9062 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9063
9064 /* dwarf2_full_name might have already finished building the DIE's
9065 type. If so, there is no need to continue. */
9066 if (get_die_type (die, cu) != NULL)
9067 return get_die_type (die, cu);
9068
9069 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
9070 if (die->tag == DW_TAG_structure_type
9071 || die->tag == DW_TAG_class_type)
9072 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
9073 }
9074 else
9075 {
d8151005
DJ
9076 /* The name is already allocated along with this objfile, so
9077 we don't need to duplicate it for the type. */
94af9270
KS
9078 TYPE_TAG_NAME (type) = (char *) name;
9079 if (die->tag == DW_TAG_class_type)
9080 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 9081 }
c906108c
SS
9082 }
9083
9084 if (die->tag == DW_TAG_structure_type)
9085 {
9086 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9087 }
9088 else if (die->tag == DW_TAG_union_type)
9089 {
9090 TYPE_CODE (type) = TYPE_CODE_UNION;
9091 }
9092 else
9093 {
c906108c
SS
9094 TYPE_CODE (type) = TYPE_CODE_CLASS;
9095 }
9096
0cc2414c
TT
9097 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9098 TYPE_DECLARED_CLASS (type) = 1;
9099
e142c38c 9100 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9101 if (attr)
9102 {
9103 TYPE_LENGTH (type) = DW_UNSND (attr);
9104 }
9105 else
9106 {
9107 TYPE_LENGTH (type) = 0;
9108 }
9109
876cecd0 9110 TYPE_STUB_SUPPORTED (type) = 1;
dc718098 9111 if (die_is_declaration (die, cu))
876cecd0 9112 TYPE_STUB (type) = 1;
a6c727b2
DJ
9113 else if (attr == NULL && die->child == NULL
9114 && producer_is_realview (cu->producer))
9115 /* RealView does not output the required DW_AT_declaration
9116 on incomplete types. */
9117 TYPE_STUB (type) = 1;
dc718098 9118
c906108c
SS
9119 /* We need to add the type field to the die immediately so we don't
9120 infinitely recurse when dealing with pointers to the structure
0963b4bd 9121 type within the structure itself. */
1c379e20 9122 set_die_type (die, type, cu);
c906108c 9123
7e314c57
JK
9124 /* set_die_type should be already done. */
9125 set_descriptive_type (type, die, cu);
9126
c767944b
DJ
9127 return type;
9128}
9129
9130/* Finish creating a structure or union type, including filling in
9131 its members and creating a symbol for it. */
9132
9133static void
9134process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9135{
9136 struct objfile *objfile = cu->objfile;
9137 struct die_info *child_die = die->child;
9138 struct type *type;
9139
9140 type = get_die_type (die, cu);
9141 if (type == NULL)
9142 type = read_structure_type (die, cu);
9143
e142c38c 9144 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
9145 {
9146 struct field_info fi;
9147 struct die_info *child_die;
34eaf542 9148 VEC (symbolp) *template_args = NULL;
c767944b 9149 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
9150
9151 memset (&fi, 0, sizeof (struct field_info));
9152
639d11d3 9153 child_die = die->child;
c906108c
SS
9154
9155 while (child_die && child_die->tag)
9156 {
a9a9bd0f
DC
9157 if (child_die->tag == DW_TAG_member
9158 || child_die->tag == DW_TAG_variable)
c906108c 9159 {
a9a9bd0f
DC
9160 /* NOTE: carlton/2002-11-05: A C++ static data member
9161 should be a DW_TAG_member that is a declaration, but
9162 all versions of G++ as of this writing (so through at
9163 least 3.2.1) incorrectly generate DW_TAG_variable
9164 tags for them instead. */
e7c27a73 9165 dwarf2_add_field (&fi, child_die, cu);
c906108c 9166 }
8713b1b1 9167 else if (child_die->tag == DW_TAG_subprogram)
c906108c 9168 {
0963b4bd 9169 /* C++ member function. */
e7c27a73 9170 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
9171 }
9172 else if (child_die->tag == DW_TAG_inheritance)
9173 {
9174 /* C++ base class field. */
e7c27a73 9175 dwarf2_add_field (&fi, child_die, cu);
c906108c 9176 }
98751a41
JK
9177 else if (child_die->tag == DW_TAG_typedef)
9178 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
9179 else if (child_die->tag == DW_TAG_template_type_param
9180 || child_die->tag == DW_TAG_template_value_param)
9181 {
9182 struct symbol *arg = new_symbol (child_die, NULL, cu);
9183
f1078f66
DJ
9184 if (arg != NULL)
9185 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9186 }
9187
c906108c
SS
9188 child_die = sibling_die (child_die);
9189 }
9190
34eaf542
TT
9191 /* Attach template arguments to type. */
9192 if (! VEC_empty (symbolp, template_args))
9193 {
9194 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9195 TYPE_N_TEMPLATE_ARGUMENTS (type)
9196 = VEC_length (symbolp, template_args);
9197 TYPE_TEMPLATE_ARGUMENTS (type)
9198 = obstack_alloc (&objfile->objfile_obstack,
9199 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9200 * sizeof (struct symbol *)));
9201 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9202 VEC_address (symbolp, template_args),
9203 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9204 * sizeof (struct symbol *)));
9205 VEC_free (symbolp, template_args);
9206 }
9207
c906108c
SS
9208 /* Attach fields and member functions to the type. */
9209 if (fi.nfields)
e7c27a73 9210 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
9211 if (fi.nfnfields)
9212 {
e7c27a73 9213 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 9214
c5aa993b 9215 /* Get the type which refers to the base class (possibly this
c906108c 9216 class itself) which contains the vtable pointer for the current
0d564a31
DJ
9217 class from the DW_AT_containing_type attribute. This use of
9218 DW_AT_containing_type is a GNU extension. */
c906108c 9219
e142c38c 9220 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 9221 {
e7c27a73 9222 struct type *t = die_containing_type (die, cu);
c906108c
SS
9223
9224 TYPE_VPTR_BASETYPE (type) = t;
9225 if (type == t)
9226 {
c906108c
SS
9227 int i;
9228
9229 /* Our own class provides vtbl ptr. */
9230 for (i = TYPE_NFIELDS (t) - 1;
9231 i >= TYPE_N_BASECLASSES (t);
9232 --i)
9233 {
0d5cff50 9234 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 9235
1168df01 9236 if (is_vtable_name (fieldname, cu))
c906108c
SS
9237 {
9238 TYPE_VPTR_FIELDNO (type) = i;
9239 break;
9240 }
9241 }
9242
9243 /* Complain if virtual function table field not found. */
9244 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 9245 complaint (&symfile_complaints,
3e43a32a
MS
9246 _("virtual function table pointer "
9247 "not found when defining class '%s'"),
4d3c2250
KB
9248 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9249 "");
c906108c
SS
9250 }
9251 else
9252 {
9253 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9254 }
9255 }
f6235d4c
EZ
9256 else if (cu->producer
9257 && strncmp (cu->producer,
9258 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9259 {
9260 /* The IBM XLC compiler does not provide direct indication
9261 of the containing type, but the vtable pointer is
9262 always named __vfp. */
9263
9264 int i;
9265
9266 for (i = TYPE_NFIELDS (type) - 1;
9267 i >= TYPE_N_BASECLASSES (type);
9268 --i)
9269 {
9270 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9271 {
9272 TYPE_VPTR_FIELDNO (type) = i;
9273 TYPE_VPTR_BASETYPE (type) = type;
9274 break;
9275 }
9276 }
9277 }
c906108c 9278 }
98751a41
JK
9279
9280 /* Copy fi.typedef_field_list linked list elements content into the
9281 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9282 if (fi.typedef_field_list)
9283 {
9284 int i = fi.typedef_field_list_count;
9285
a0d7a4ff 9286 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
9287 TYPE_TYPEDEF_FIELD_ARRAY (type)
9288 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9289 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9290
9291 /* Reverse the list order to keep the debug info elements order. */
9292 while (--i >= 0)
9293 {
9294 struct typedef_field *dest, *src;
6e70227d 9295
98751a41
JK
9296 dest = &TYPE_TYPEDEF_FIELD (type, i);
9297 src = &fi.typedef_field_list->field;
9298 fi.typedef_field_list = fi.typedef_field_list->next;
9299 *dest = *src;
9300 }
9301 }
c767944b
DJ
9302
9303 do_cleanups (back_to);
eb2a6f42
TT
9304
9305 if (HAVE_CPLUS_STRUCT (type))
9306 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 9307 }
63d06c5c 9308
bb5ed363 9309 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 9310
90aeadfc
DC
9311 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9312 snapshots) has been known to create a die giving a declaration
9313 for a class that has, as a child, a die giving a definition for a
9314 nested class. So we have to process our children even if the
9315 current die is a declaration. Normally, of course, a declaration
9316 won't have any children at all. */
134d01f1 9317
90aeadfc
DC
9318 while (child_die != NULL && child_die->tag)
9319 {
9320 if (child_die->tag == DW_TAG_member
9321 || child_die->tag == DW_TAG_variable
34eaf542
TT
9322 || child_die->tag == DW_TAG_inheritance
9323 || child_die->tag == DW_TAG_template_value_param
9324 || child_die->tag == DW_TAG_template_type_param)
134d01f1 9325 {
90aeadfc 9326 /* Do nothing. */
134d01f1 9327 }
90aeadfc
DC
9328 else
9329 process_die (child_die, cu);
134d01f1 9330
90aeadfc 9331 child_die = sibling_die (child_die);
134d01f1
DJ
9332 }
9333
fa4028e9
JB
9334 /* Do not consider external references. According to the DWARF standard,
9335 these DIEs are identified by the fact that they have no byte_size
9336 attribute, and a declaration attribute. */
9337 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9338 || !die_is_declaration (die, cu))
c767944b 9339 new_symbol (die, type, cu);
134d01f1
DJ
9340}
9341
9342/* Given a DW_AT_enumeration_type die, set its type. We do not
9343 complete the type's fields yet, or create any symbols. */
c906108c 9344
f792889a 9345static struct type *
134d01f1 9346read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9347{
e7c27a73 9348 struct objfile *objfile = cu->objfile;
c906108c 9349 struct type *type;
c906108c 9350 struct attribute *attr;
0114d602 9351 const char *name;
134d01f1 9352
348e048f
DE
9353 /* If the definition of this type lives in .debug_types, read that type.
9354 Don't follow DW_AT_specification though, that will take us back up
9355 the chain and we want to go down. */
9356 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9357 if (attr)
9358 {
9359 struct dwarf2_cu *type_cu = cu;
9360 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9361
348e048f 9362 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9363
9364 /* TYPE_CU may not be the same as CU.
9365 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9366 return set_die_type (die, type, cu);
9367 }
9368
c906108c
SS
9369 type = alloc_type (objfile);
9370
9371 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 9372 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 9373 if (name != NULL)
0114d602 9374 TYPE_TAG_NAME (type) = (char *) name;
c906108c 9375
e142c38c 9376 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9377 if (attr)
9378 {
9379 TYPE_LENGTH (type) = DW_UNSND (attr);
9380 }
9381 else
9382 {
9383 TYPE_LENGTH (type) = 0;
9384 }
9385
137033e9
JB
9386 /* The enumeration DIE can be incomplete. In Ada, any type can be
9387 declared as private in the package spec, and then defined only
9388 inside the package body. Such types are known as Taft Amendment
9389 Types. When another package uses such a type, an incomplete DIE
9390 may be generated by the compiler. */
02eb380e 9391 if (die_is_declaration (die, cu))
876cecd0 9392 TYPE_STUB (type) = 1;
02eb380e 9393
f792889a 9394 return set_die_type (die, type, cu);
134d01f1
DJ
9395}
9396
9397/* Given a pointer to a die which begins an enumeration, process all
9398 the dies that define the members of the enumeration, and create the
9399 symbol for the enumeration type.
9400
9401 NOTE: We reverse the order of the element list. */
9402
9403static void
9404process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9405{
f792889a 9406 struct type *this_type;
134d01f1 9407
f792889a
DJ
9408 this_type = get_die_type (die, cu);
9409 if (this_type == NULL)
9410 this_type = read_enumeration_type (die, cu);
9dc481d3 9411
639d11d3 9412 if (die->child != NULL)
c906108c 9413 {
9dc481d3
DE
9414 struct die_info *child_die;
9415 struct symbol *sym;
9416 struct field *fields = NULL;
9417 int num_fields = 0;
9418 int unsigned_enum = 1;
9419 char *name;
cafec441
TT
9420 int flag_enum = 1;
9421 ULONGEST mask = 0;
9dc481d3 9422
639d11d3 9423 child_die = die->child;
c906108c
SS
9424 while (child_die && child_die->tag)
9425 {
9426 if (child_die->tag != DW_TAG_enumerator)
9427 {
e7c27a73 9428 process_die (child_die, cu);
c906108c
SS
9429 }
9430 else
9431 {
39cbfefa
DJ
9432 name = dwarf2_name (child_die, cu);
9433 if (name)
c906108c 9434 {
f792889a 9435 sym = new_symbol (child_die, this_type, cu);
c906108c 9436 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
9437 {
9438 unsigned_enum = 0;
9439 flag_enum = 0;
9440 }
9441 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9442 flag_enum = 0;
9443 else
9444 mask |= SYMBOL_VALUE (sym);
c906108c
SS
9445
9446 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9447 {
9448 fields = (struct field *)
9449 xrealloc (fields,
9450 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 9451 * sizeof (struct field));
c906108c
SS
9452 }
9453
3567439c 9454 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 9455 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 9456 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
9457 FIELD_BITSIZE (fields[num_fields]) = 0;
9458
9459 num_fields++;
9460 }
9461 }
9462
9463 child_die = sibling_die (child_die);
9464 }
9465
9466 if (num_fields)
9467 {
f792889a
DJ
9468 TYPE_NFIELDS (this_type) = num_fields;
9469 TYPE_FIELDS (this_type) = (struct field *)
9470 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9471 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 9472 sizeof (struct field) * num_fields);
b8c9b27d 9473 xfree (fields);
c906108c
SS
9474 }
9475 if (unsigned_enum)
876cecd0 9476 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
9477 if (flag_enum)
9478 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 9479 }
134d01f1 9480
6c83ed52
TT
9481 /* If we are reading an enum from a .debug_types unit, and the enum
9482 is a declaration, and the enum is not the signatured type in the
9483 unit, then we do not want to add a symbol for it. Adding a
9484 symbol would in some cases obscure the true definition of the
9485 enum, giving users an incomplete type when the definition is
9486 actually available. Note that we do not want to do this for all
9487 enums which are just declarations, because C++0x allows forward
9488 enum declarations. */
3019eac3 9489 if (cu->per_cu->is_debug_types
6c83ed52
TT
9490 && die_is_declaration (die, cu))
9491 {
52dc124a 9492 struct signatured_type *sig_type;
6c83ed52 9493
52dc124a 9494 sig_type
6c83ed52 9495 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 9496 cu->per_cu->info_or_types_section,
6c83ed52 9497 cu->per_cu->offset);
3019eac3
DE
9498 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9499 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
9500 return;
9501 }
9502
f792889a 9503 new_symbol (die, this_type, cu);
c906108c
SS
9504}
9505
9506/* Extract all information from a DW_TAG_array_type DIE and put it in
9507 the DIE's type field. For now, this only handles one dimensional
9508 arrays. */
9509
f792889a 9510static struct type *
e7c27a73 9511read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9512{
e7c27a73 9513 struct objfile *objfile = cu->objfile;
c906108c 9514 struct die_info *child_die;
7e314c57 9515 struct type *type;
c906108c
SS
9516 struct type *element_type, *range_type, *index_type;
9517 struct type **range_types = NULL;
9518 struct attribute *attr;
9519 int ndim = 0;
9520 struct cleanup *back_to;
39cbfefa 9521 char *name;
c906108c 9522
e7c27a73 9523 element_type = die_type (die, cu);
c906108c 9524
7e314c57
JK
9525 /* The die_type call above may have already set the type for this DIE. */
9526 type = get_die_type (die, cu);
9527 if (type)
9528 return type;
9529
c906108c
SS
9530 /* Irix 6.2 native cc creates array types without children for
9531 arrays with unspecified length. */
639d11d3 9532 if (die->child == NULL)
c906108c 9533 {
46bf5051 9534 index_type = objfile_type (objfile)->builtin_int;
c906108c 9535 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
9536 type = create_array_type (NULL, element_type, range_type);
9537 return set_die_type (die, type, cu);
c906108c
SS
9538 }
9539
9540 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 9541 child_die = die->child;
c906108c
SS
9542 while (child_die && child_die->tag)
9543 {
9544 if (child_die->tag == DW_TAG_subrange_type)
9545 {
f792889a 9546 struct type *child_type = read_type_die (child_die, cu);
9a619af0 9547
f792889a 9548 if (child_type != NULL)
a02abb62 9549 {
0963b4bd
MS
9550 /* The range type was succesfully read. Save it for the
9551 array type creation. */
a02abb62
JB
9552 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9553 {
9554 range_types = (struct type **)
9555 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9556 * sizeof (struct type *));
9557 if (ndim == 0)
9558 make_cleanup (free_current_contents, &range_types);
9559 }
f792889a 9560 range_types[ndim++] = child_type;
a02abb62 9561 }
c906108c
SS
9562 }
9563 child_die = sibling_die (child_die);
9564 }
9565
9566 /* Dwarf2 dimensions are output from left to right, create the
9567 necessary array types in backwards order. */
7ca2d3a3 9568
c906108c 9569 type = element_type;
7ca2d3a3
DL
9570
9571 if (read_array_order (die, cu) == DW_ORD_col_major)
9572 {
9573 int i = 0;
9a619af0 9574
7ca2d3a3
DL
9575 while (i < ndim)
9576 type = create_array_type (NULL, type, range_types[i++]);
9577 }
9578 else
9579 {
9580 while (ndim-- > 0)
9581 type = create_array_type (NULL, type, range_types[ndim]);
9582 }
c906108c 9583
f5f8a009
EZ
9584 /* Understand Dwarf2 support for vector types (like they occur on
9585 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9586 array type. This is not part of the Dwarf2/3 standard yet, but a
9587 custom vendor extension. The main difference between a regular
9588 array and the vector variant is that vectors are passed by value
9589 to functions. */
e142c38c 9590 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 9591 if (attr)
ea37ba09 9592 make_vector_type (type);
f5f8a009 9593
dbc98a8b
KW
9594 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9595 implementation may choose to implement triple vectors using this
9596 attribute. */
9597 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9598 if (attr)
9599 {
9600 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9601 TYPE_LENGTH (type) = DW_UNSND (attr);
9602 else
3e43a32a
MS
9603 complaint (&symfile_complaints,
9604 _("DW_AT_byte_size for array type smaller "
9605 "than the total size of elements"));
dbc98a8b
KW
9606 }
9607
39cbfefa
DJ
9608 name = dwarf2_name (die, cu);
9609 if (name)
9610 TYPE_NAME (type) = name;
6e70227d 9611
0963b4bd 9612 /* Install the type in the die. */
7e314c57
JK
9613 set_die_type (die, type, cu);
9614
9615 /* set_die_type should be already done. */
b4ba55a1
JB
9616 set_descriptive_type (type, die, cu);
9617
c906108c
SS
9618 do_cleanups (back_to);
9619
7e314c57 9620 return type;
c906108c
SS
9621}
9622
7ca2d3a3 9623static enum dwarf_array_dim_ordering
6e70227d 9624read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
9625{
9626 struct attribute *attr;
9627
9628 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9629
9630 if (attr) return DW_SND (attr);
9631
0963b4bd
MS
9632 /* GNU F77 is a special case, as at 08/2004 array type info is the
9633 opposite order to the dwarf2 specification, but data is still
9634 laid out as per normal fortran.
7ca2d3a3 9635
0963b4bd
MS
9636 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9637 version checking. */
7ca2d3a3 9638
905e0470
PM
9639 if (cu->language == language_fortran
9640 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
9641 {
9642 return DW_ORD_row_major;
9643 }
9644
6e70227d 9645 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
9646 {
9647 case array_column_major:
9648 return DW_ORD_col_major;
9649 case array_row_major:
9650 default:
9651 return DW_ORD_row_major;
9652 };
9653}
9654
72019c9c 9655/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 9656 the DIE's type field. */
72019c9c 9657
f792889a 9658static struct type *
72019c9c
GM
9659read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9660{
7e314c57
JK
9661 struct type *domain_type, *set_type;
9662 struct attribute *attr;
f792889a 9663
7e314c57
JK
9664 domain_type = die_type (die, cu);
9665
9666 /* The die_type call above may have already set the type for this DIE. */
9667 set_type = get_die_type (die, cu);
9668 if (set_type)
9669 return set_type;
9670
9671 set_type = create_set_type (NULL, domain_type);
9672
9673 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
9674 if (attr)
9675 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 9676
f792889a 9677 return set_die_type (die, set_type, cu);
72019c9c 9678}
7ca2d3a3 9679
c906108c
SS
9680/* First cut: install each common block member as a global variable. */
9681
9682static void
e7c27a73 9683read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
9684{
9685 struct die_info *child_die;
9686 struct attribute *attr;
9687 struct symbol *sym;
9688 CORE_ADDR base = (CORE_ADDR) 0;
9689
e142c38c 9690 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
9691 if (attr)
9692 {
0963b4bd 9693 /* Support the .debug_loc offsets. */
8e19ed76
PS
9694 if (attr_form_is_block (attr))
9695 {
e7c27a73 9696 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 9697 }
3690dd37 9698 else if (attr_form_is_section_offset (attr))
8e19ed76 9699 {
4d3c2250 9700 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9701 }
9702 else
9703 {
4d3c2250
KB
9704 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9705 "common block member");
8e19ed76 9706 }
c906108c 9707 }
639d11d3 9708 if (die->child != NULL)
c906108c 9709 {
639d11d3 9710 child_die = die->child;
c906108c
SS
9711 while (child_die && child_die->tag)
9712 {
74ac6d43
TT
9713 LONGEST offset;
9714
e7c27a73 9715 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
9716 if (sym != NULL
9717 && handle_data_member_location (child_die, cu, &offset))
c906108c 9718 {
74ac6d43 9719 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
9720 add_symbol_to_list (sym, &global_symbols);
9721 }
9722 child_die = sibling_die (child_die);
9723 }
9724 }
9725}
9726
0114d602 9727/* Create a type for a C++ namespace. */
d9fa45fe 9728
0114d602
DJ
9729static struct type *
9730read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 9731{
e7c27a73 9732 struct objfile *objfile = cu->objfile;
0114d602 9733 const char *previous_prefix, *name;
9219021c 9734 int is_anonymous;
0114d602
DJ
9735 struct type *type;
9736
9737 /* For extensions, reuse the type of the original namespace. */
9738 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9739 {
9740 struct die_info *ext_die;
9741 struct dwarf2_cu *ext_cu = cu;
9a619af0 9742
0114d602
DJ
9743 ext_die = dwarf2_extension (die, &ext_cu);
9744 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
9745
9746 /* EXT_CU may not be the same as CU.
9747 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
9748 return set_die_type (die, type, cu);
9749 }
9219021c 9750
e142c38c 9751 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
9752
9753 /* Now build the name of the current namespace. */
9754
0114d602
DJ
9755 previous_prefix = determine_prefix (die, cu);
9756 if (previous_prefix[0] != '\0')
9757 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 9758 previous_prefix, name, 0, cu);
0114d602
DJ
9759
9760 /* Create the type. */
9761 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
9762 objfile);
9763 TYPE_NAME (type) = (char *) name;
9764 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9765
60531b24 9766 return set_die_type (die, type, cu);
0114d602
DJ
9767}
9768
9769/* Read a C++ namespace. */
9770
9771static void
9772read_namespace (struct die_info *die, struct dwarf2_cu *cu)
9773{
9774 struct objfile *objfile = cu->objfile;
0114d602 9775 int is_anonymous;
9219021c 9776
5c4e30ca
DC
9777 /* Add a symbol associated to this if we haven't seen the namespace
9778 before. Also, add a using directive if it's an anonymous
9779 namespace. */
9219021c 9780
f2f0e013 9781 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
9782 {
9783 struct type *type;
9784
0114d602 9785 type = read_type_die (die, cu);
e7c27a73 9786 new_symbol (die, type, cu);
5c4e30ca 9787
e8e80198 9788 namespace_name (die, &is_anonymous, cu);
5c4e30ca 9789 if (is_anonymous)
0114d602
DJ
9790 {
9791 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 9792
c0cc3a76 9793 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 9794 NULL, NULL, &objfile->objfile_obstack);
0114d602 9795 }
5c4e30ca 9796 }
9219021c 9797
639d11d3 9798 if (die->child != NULL)
d9fa45fe 9799 {
639d11d3 9800 struct die_info *child_die = die->child;
6e70227d 9801
d9fa45fe
DC
9802 while (child_die && child_die->tag)
9803 {
e7c27a73 9804 process_die (child_die, cu);
d9fa45fe
DC
9805 child_die = sibling_die (child_die);
9806 }
9807 }
38d518c9
EZ
9808}
9809
f55ee35c
JK
9810/* Read a Fortran module as type. This DIE can be only a declaration used for
9811 imported module. Still we need that type as local Fortran "use ... only"
9812 declaration imports depend on the created type in determine_prefix. */
9813
9814static struct type *
9815read_module_type (struct die_info *die, struct dwarf2_cu *cu)
9816{
9817 struct objfile *objfile = cu->objfile;
9818 char *module_name;
9819 struct type *type;
9820
9821 module_name = dwarf2_name (die, cu);
9822 if (!module_name)
3e43a32a
MS
9823 complaint (&symfile_complaints,
9824 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 9825 die->offset.sect_off);
f55ee35c
JK
9826 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
9827
9828 /* determine_prefix uses TYPE_TAG_NAME. */
9829 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9830
9831 return set_die_type (die, type, cu);
9832}
9833
5d7cb8df
JK
9834/* Read a Fortran module. */
9835
9836static void
9837read_module (struct die_info *die, struct dwarf2_cu *cu)
9838{
9839 struct die_info *child_die = die->child;
9840
5d7cb8df
JK
9841 while (child_die && child_die->tag)
9842 {
9843 process_die (child_die, cu);
9844 child_die = sibling_die (child_die);
9845 }
9846}
9847
38d518c9
EZ
9848/* Return the name of the namespace represented by DIE. Set
9849 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
9850 namespace. */
9851
9852static const char *
e142c38c 9853namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
9854{
9855 struct die_info *current_die;
9856 const char *name = NULL;
9857
9858 /* Loop through the extensions until we find a name. */
9859
9860 for (current_die = die;
9861 current_die != NULL;
f2f0e013 9862 current_die = dwarf2_extension (die, &cu))
38d518c9 9863 {
e142c38c 9864 name = dwarf2_name (current_die, cu);
38d518c9
EZ
9865 if (name != NULL)
9866 break;
9867 }
9868
9869 /* Is it an anonymous namespace? */
9870
9871 *is_anonymous = (name == NULL);
9872 if (*is_anonymous)
2b1dbab0 9873 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
9874
9875 return name;
d9fa45fe
DC
9876}
9877
c906108c
SS
9878/* Extract all information from a DW_TAG_pointer_type DIE and add to
9879 the user defined type vector. */
9880
f792889a 9881static struct type *
e7c27a73 9882read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9883{
5e2b427d 9884 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 9885 struct comp_unit_head *cu_header = &cu->header;
c906108c 9886 struct type *type;
8b2dbe47
KB
9887 struct attribute *attr_byte_size;
9888 struct attribute *attr_address_class;
9889 int byte_size, addr_class;
7e314c57
JK
9890 struct type *target_type;
9891
9892 target_type = die_type (die, cu);
c906108c 9893
7e314c57
JK
9894 /* The die_type call above may have already set the type for this DIE. */
9895 type = get_die_type (die, cu);
9896 if (type)
9897 return type;
9898
9899 type = lookup_pointer_type (target_type);
8b2dbe47 9900
e142c38c 9901 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
9902 if (attr_byte_size)
9903 byte_size = DW_UNSND (attr_byte_size);
c906108c 9904 else
8b2dbe47
KB
9905 byte_size = cu_header->addr_size;
9906
e142c38c 9907 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
9908 if (attr_address_class)
9909 addr_class = DW_UNSND (attr_address_class);
9910 else
9911 addr_class = DW_ADDR_none;
9912
9913 /* If the pointer size or address class is different than the
9914 default, create a type variant marked as such and set the
9915 length accordingly. */
9916 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 9917 {
5e2b427d 9918 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
9919 {
9920 int type_flags;
9921
849957d9 9922 type_flags = gdbarch_address_class_type_flags
5e2b427d 9923 (gdbarch, byte_size, addr_class);
876cecd0
TT
9924 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
9925 == 0);
8b2dbe47
KB
9926 type = make_type_with_address_space (type, type_flags);
9927 }
9928 else if (TYPE_LENGTH (type) != byte_size)
9929 {
3e43a32a
MS
9930 complaint (&symfile_complaints,
9931 _("invalid pointer size %d"), byte_size);
8b2dbe47 9932 }
6e70227d 9933 else
9a619af0
MS
9934 {
9935 /* Should we also complain about unhandled address classes? */
9936 }
c906108c 9937 }
8b2dbe47
KB
9938
9939 TYPE_LENGTH (type) = byte_size;
f792889a 9940 return set_die_type (die, type, cu);
c906108c
SS
9941}
9942
9943/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
9944 the user defined type vector. */
9945
f792889a 9946static struct type *
e7c27a73 9947read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
9948{
9949 struct type *type;
9950 struct type *to_type;
9951 struct type *domain;
9952
e7c27a73
DJ
9953 to_type = die_type (die, cu);
9954 domain = die_containing_type (die, cu);
0d5de010 9955
7e314c57
JK
9956 /* The calls above may have already set the type for this DIE. */
9957 type = get_die_type (die, cu);
9958 if (type)
9959 return type;
9960
0d5de010
DJ
9961 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
9962 type = lookup_methodptr_type (to_type);
9963 else
9964 type = lookup_memberptr_type (to_type, domain);
c906108c 9965
f792889a 9966 return set_die_type (die, type, cu);
c906108c
SS
9967}
9968
9969/* Extract all information from a DW_TAG_reference_type DIE and add to
9970 the user defined type vector. */
9971
f792889a 9972static struct type *
e7c27a73 9973read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9974{
e7c27a73 9975 struct comp_unit_head *cu_header = &cu->header;
7e314c57 9976 struct type *type, *target_type;
c906108c
SS
9977 struct attribute *attr;
9978
7e314c57
JK
9979 target_type = die_type (die, cu);
9980
9981 /* The die_type call above may have already set the type for this DIE. */
9982 type = get_die_type (die, cu);
9983 if (type)
9984 return type;
9985
9986 type = lookup_reference_type (target_type);
e142c38c 9987 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9988 if (attr)
9989 {
9990 TYPE_LENGTH (type) = DW_UNSND (attr);
9991 }
9992 else
9993 {
107d2387 9994 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 9995 }
f792889a 9996 return set_die_type (die, type, cu);
c906108c
SS
9997}
9998
f792889a 9999static struct type *
e7c27a73 10000read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10001{
f792889a 10002 struct type *base_type, *cv_type;
c906108c 10003
e7c27a73 10004 base_type = die_type (die, cu);
7e314c57
JK
10005
10006 /* The die_type call above may have already set the type for this DIE. */
10007 cv_type = get_die_type (die, cu);
10008 if (cv_type)
10009 return cv_type;
10010
2f608a3a
KW
10011 /* In case the const qualifier is applied to an array type, the element type
10012 is so qualified, not the array type (section 6.7.3 of C99). */
10013 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
10014 {
10015 struct type *el_type, *inner_array;
10016
10017 base_type = copy_type (base_type);
10018 inner_array = base_type;
10019
10020 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10021 {
10022 TYPE_TARGET_TYPE (inner_array) =
10023 copy_type (TYPE_TARGET_TYPE (inner_array));
10024 inner_array = TYPE_TARGET_TYPE (inner_array);
10025 }
10026
10027 el_type = TYPE_TARGET_TYPE (inner_array);
10028 TYPE_TARGET_TYPE (inner_array) =
10029 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10030
10031 return set_die_type (die, base_type, cu);
10032 }
10033
f792889a
DJ
10034 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10035 return set_die_type (die, cv_type, cu);
c906108c
SS
10036}
10037
f792889a 10038static struct type *
e7c27a73 10039read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10040{
f792889a 10041 struct type *base_type, *cv_type;
c906108c 10042
e7c27a73 10043 base_type = die_type (die, cu);
7e314c57
JK
10044
10045 /* The die_type call above may have already set the type for this DIE. */
10046 cv_type = get_die_type (die, cu);
10047 if (cv_type)
10048 return cv_type;
10049
f792889a
DJ
10050 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10051 return set_die_type (die, cv_type, cu);
c906108c
SS
10052}
10053
10054/* Extract all information from a DW_TAG_string_type DIE and add to
10055 the user defined type vector. It isn't really a user defined type,
10056 but it behaves like one, with other DIE's using an AT_user_def_type
10057 attribute to reference it. */
10058
f792889a 10059static struct type *
e7c27a73 10060read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10061{
e7c27a73 10062 struct objfile *objfile = cu->objfile;
3b7538c0 10063 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10064 struct type *type, *range_type, *index_type, *char_type;
10065 struct attribute *attr;
10066 unsigned int length;
10067
e142c38c 10068 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
10069 if (attr)
10070 {
10071 length = DW_UNSND (attr);
10072 }
10073 else
10074 {
0963b4bd 10075 /* Check for the DW_AT_byte_size attribute. */
e142c38c 10076 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
10077 if (attr)
10078 {
10079 length = DW_UNSND (attr);
10080 }
10081 else
10082 {
10083 length = 1;
10084 }
c906108c 10085 }
6ccb9162 10086
46bf5051 10087 index_type = objfile_type (objfile)->builtin_int;
c906108c 10088 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
10089 char_type = language_string_char_type (cu->language_defn, gdbarch);
10090 type = create_string_type (NULL, char_type, range_type);
6ccb9162 10091
f792889a 10092 return set_die_type (die, type, cu);
c906108c
SS
10093}
10094
10095/* Handle DIES due to C code like:
10096
10097 struct foo
c5aa993b
JM
10098 {
10099 int (*funcp)(int a, long l);
10100 int b;
10101 };
c906108c 10102
0963b4bd 10103 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 10104
f792889a 10105static struct type *
e7c27a73 10106read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10107{
bb5ed363 10108 struct objfile *objfile = cu->objfile;
0963b4bd
MS
10109 struct type *type; /* Type that this function returns. */
10110 struct type *ftype; /* Function that returns above type. */
c906108c
SS
10111 struct attribute *attr;
10112
e7c27a73 10113 type = die_type (die, cu);
7e314c57
JK
10114
10115 /* The die_type call above may have already set the type for this DIE. */
10116 ftype = get_die_type (die, cu);
10117 if (ftype)
10118 return ftype;
10119
0c8b41f1 10120 ftype = lookup_function_type (type);
c906108c 10121
5b8101ae 10122 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 10123 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 10124 if ((attr && (DW_UNSND (attr) != 0))
987504bb 10125 || cu->language == language_cplus
5b8101ae
PM
10126 || cu->language == language_java
10127 || cu->language == language_pascal)
876cecd0 10128 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
10129 else if (producer_is_realview (cu->producer))
10130 /* RealView does not emit DW_AT_prototyped. We can not
10131 distinguish prototyped and unprototyped functions; default to
10132 prototyped, since that is more common in modern code (and
10133 RealView warns about unprototyped functions). */
10134 TYPE_PROTOTYPED (ftype) = 1;
c906108c 10135
c055b101
CV
10136 /* Store the calling convention in the type if it's available in
10137 the subroutine die. Otherwise set the calling convention to
10138 the default value DW_CC_normal. */
10139 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
10140 if (attr)
10141 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10142 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10143 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10144 else
10145 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
10146
10147 /* We need to add the subroutine type to the die immediately so
10148 we don't infinitely recurse when dealing with parameters
0963b4bd 10149 declared as the same subroutine type. */
76c10ea2 10150 set_die_type (die, ftype, cu);
6e70227d 10151
639d11d3 10152 if (die->child != NULL)
c906108c 10153 {
bb5ed363 10154 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 10155 struct die_info *child_die;
8072405b 10156 int nparams, iparams;
c906108c
SS
10157
10158 /* Count the number of parameters.
10159 FIXME: GDB currently ignores vararg functions, but knows about
10160 vararg member functions. */
8072405b 10161 nparams = 0;
639d11d3 10162 child_die = die->child;
c906108c
SS
10163 while (child_die && child_die->tag)
10164 {
10165 if (child_die->tag == DW_TAG_formal_parameter)
10166 nparams++;
10167 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 10168 TYPE_VARARGS (ftype) = 1;
c906108c
SS
10169 child_die = sibling_die (child_die);
10170 }
10171
10172 /* Allocate storage for parameters and fill them in. */
10173 TYPE_NFIELDS (ftype) = nparams;
10174 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 10175 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 10176
8072405b
JK
10177 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10178 even if we error out during the parameters reading below. */
10179 for (iparams = 0; iparams < nparams; iparams++)
10180 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10181
10182 iparams = 0;
639d11d3 10183 child_die = die->child;
c906108c
SS
10184 while (child_die && child_die->tag)
10185 {
10186 if (child_die->tag == DW_TAG_formal_parameter)
10187 {
3ce3b1ba
PA
10188 struct type *arg_type;
10189
10190 /* DWARF version 2 has no clean way to discern C++
10191 static and non-static member functions. G++ helps
10192 GDB by marking the first parameter for non-static
10193 member functions (which is the this pointer) as
10194 artificial. We pass this information to
10195 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10196
10197 DWARF version 3 added DW_AT_object_pointer, which GCC
10198 4.5 does not yet generate. */
e142c38c 10199 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
10200 if (attr)
10201 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10202 else
418835cc
KS
10203 {
10204 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10205
10206 /* GCC/43521: In java, the formal parameter
10207 "this" is sometimes not marked with DW_AT_artificial. */
10208 if (cu->language == language_java)
10209 {
10210 const char *name = dwarf2_name (child_die, cu);
9a619af0 10211
418835cc
KS
10212 if (name && !strcmp (name, "this"))
10213 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10214 }
10215 }
3ce3b1ba
PA
10216 arg_type = die_type (child_die, cu);
10217
10218 /* RealView does not mark THIS as const, which the testsuite
10219 expects. GCC marks THIS as const in method definitions,
10220 but not in the class specifications (GCC PR 43053). */
10221 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10222 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10223 {
10224 int is_this = 0;
10225 struct dwarf2_cu *arg_cu = cu;
10226 const char *name = dwarf2_name (child_die, cu);
10227
10228 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10229 if (attr)
10230 {
10231 /* If the compiler emits this, use it. */
10232 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10233 is_this = 1;
10234 }
10235 else if (name && strcmp (name, "this") == 0)
10236 /* Function definitions will have the argument names. */
10237 is_this = 1;
10238 else if (name == NULL && iparams == 0)
10239 /* Declarations may not have the names, so like
10240 elsewhere in GDB, assume an artificial first
10241 argument is "this". */
10242 is_this = 1;
10243
10244 if (is_this)
10245 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10246 arg_type, 0);
10247 }
10248
10249 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
10250 iparams++;
10251 }
10252 child_die = sibling_die (child_die);
10253 }
10254 }
10255
76c10ea2 10256 return ftype;
c906108c
SS
10257}
10258
f792889a 10259static struct type *
e7c27a73 10260read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10261{
e7c27a73 10262 struct objfile *objfile = cu->objfile;
0114d602 10263 const char *name = NULL;
3c8e0968 10264 struct type *this_type, *target_type;
c906108c 10265
94af9270 10266 name = dwarf2_full_name (NULL, die, cu);
f792889a 10267 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
10268 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10269 TYPE_NAME (this_type) = (char *) name;
f792889a 10270 set_die_type (die, this_type, cu);
3c8e0968
DE
10271 target_type = die_type (die, cu);
10272 if (target_type != this_type)
10273 TYPE_TARGET_TYPE (this_type) = target_type;
10274 else
10275 {
10276 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10277 spec and cause infinite loops in GDB. */
10278 complaint (&symfile_complaints,
10279 _("Self-referential DW_TAG_typedef "
10280 "- DIE at 0x%x [in module %s]"),
b64f50a1 10281 die->offset.sect_off, objfile->name);
3c8e0968
DE
10282 TYPE_TARGET_TYPE (this_type) = NULL;
10283 }
f792889a 10284 return this_type;
c906108c
SS
10285}
10286
10287/* Find a representation of a given base type and install
10288 it in the TYPE field of the die. */
10289
f792889a 10290static struct type *
e7c27a73 10291read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10292{
e7c27a73 10293 struct objfile *objfile = cu->objfile;
c906108c
SS
10294 struct type *type;
10295 struct attribute *attr;
10296 int encoding = 0, size = 0;
39cbfefa 10297 char *name;
6ccb9162
UW
10298 enum type_code code = TYPE_CODE_INT;
10299 int type_flags = 0;
10300 struct type *target_type = NULL;
c906108c 10301
e142c38c 10302 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
10303 if (attr)
10304 {
10305 encoding = DW_UNSND (attr);
10306 }
e142c38c 10307 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10308 if (attr)
10309 {
10310 size = DW_UNSND (attr);
10311 }
39cbfefa 10312 name = dwarf2_name (die, cu);
6ccb9162 10313 if (!name)
c906108c 10314 {
6ccb9162
UW
10315 complaint (&symfile_complaints,
10316 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 10317 }
6ccb9162
UW
10318
10319 switch (encoding)
c906108c 10320 {
6ccb9162
UW
10321 case DW_ATE_address:
10322 /* Turn DW_ATE_address into a void * pointer. */
10323 code = TYPE_CODE_PTR;
10324 type_flags |= TYPE_FLAG_UNSIGNED;
10325 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10326 break;
10327 case DW_ATE_boolean:
10328 code = TYPE_CODE_BOOL;
10329 type_flags |= TYPE_FLAG_UNSIGNED;
10330 break;
10331 case DW_ATE_complex_float:
10332 code = TYPE_CODE_COMPLEX;
10333 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10334 break;
10335 case DW_ATE_decimal_float:
10336 code = TYPE_CODE_DECFLOAT;
10337 break;
10338 case DW_ATE_float:
10339 code = TYPE_CODE_FLT;
10340 break;
10341 case DW_ATE_signed:
10342 break;
10343 case DW_ATE_unsigned:
10344 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
10345 if (cu->language == language_fortran
10346 && name
10347 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10348 code = TYPE_CODE_CHAR;
6ccb9162
UW
10349 break;
10350 case DW_ATE_signed_char:
6e70227d 10351 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10352 || cu->language == language_pascal
10353 || cu->language == language_fortran)
6ccb9162
UW
10354 code = TYPE_CODE_CHAR;
10355 break;
10356 case DW_ATE_unsigned_char:
868a0084 10357 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10358 || cu->language == language_pascal
10359 || cu->language == language_fortran)
6ccb9162
UW
10360 code = TYPE_CODE_CHAR;
10361 type_flags |= TYPE_FLAG_UNSIGNED;
10362 break;
75079b2b
TT
10363 case DW_ATE_UTF:
10364 /* We just treat this as an integer and then recognize the
10365 type by name elsewhere. */
10366 break;
10367
6ccb9162
UW
10368 default:
10369 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10370 dwarf_type_encoding_name (encoding));
10371 break;
c906108c 10372 }
6ccb9162 10373
0114d602
DJ
10374 type = init_type (code, size, type_flags, NULL, objfile);
10375 TYPE_NAME (type) = name;
6ccb9162
UW
10376 TYPE_TARGET_TYPE (type) = target_type;
10377
0114d602 10378 if (name && strcmp (name, "char") == 0)
876cecd0 10379 TYPE_NOSIGN (type) = 1;
0114d602 10380
f792889a 10381 return set_die_type (die, type, cu);
c906108c
SS
10382}
10383
a02abb62
JB
10384/* Read the given DW_AT_subrange DIE. */
10385
f792889a 10386static struct type *
a02abb62
JB
10387read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10388{
10389 struct type *base_type;
10390 struct type *range_type;
10391 struct attribute *attr;
4fae6e18
JK
10392 LONGEST low, high;
10393 int low_default_is_valid;
39cbfefa 10394 char *name;
43bbcdc2 10395 LONGEST negative_mask;
e77813c8 10396
a02abb62 10397 base_type = die_type (die, cu);
953ac07e
JK
10398 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10399 check_typedef (base_type);
a02abb62 10400
7e314c57
JK
10401 /* The die_type call above may have already set the type for this DIE. */
10402 range_type = get_die_type (die, cu);
10403 if (range_type)
10404 return range_type;
10405
4fae6e18
JK
10406 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10407 omitting DW_AT_lower_bound. */
10408 switch (cu->language)
6e70227d 10409 {
4fae6e18
JK
10410 case language_c:
10411 case language_cplus:
10412 low = 0;
10413 low_default_is_valid = 1;
10414 break;
10415 case language_fortran:
10416 low = 1;
10417 low_default_is_valid = 1;
10418 break;
10419 case language_d:
10420 case language_java:
10421 case language_objc:
10422 low = 0;
10423 low_default_is_valid = (cu->header.version >= 4);
10424 break;
10425 case language_ada:
10426 case language_m2:
10427 case language_pascal:
a02abb62 10428 low = 1;
4fae6e18
JK
10429 low_default_is_valid = (cu->header.version >= 4);
10430 break;
10431 default:
10432 low = 0;
10433 low_default_is_valid = 0;
10434 break;
a02abb62
JB
10435 }
10436
dd5e6932
DJ
10437 /* FIXME: For variable sized arrays either of these could be
10438 a variable rather than a constant value. We'll allow it,
10439 but we don't know how to handle it. */
e142c38c 10440 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 10441 if (attr)
4fae6e18
JK
10442 low = dwarf2_get_attr_constant_value (attr, low);
10443 else if (!low_default_is_valid)
10444 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10445 "- DIE at 0x%x [in module %s]"),
10446 die->offset.sect_off, cu->objfile->name);
a02abb62 10447
e142c38c 10448 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 10449 if (attr)
6e70227d 10450 {
d48323d8 10451 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
10452 {
10453 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 10454 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
10455 FIXME: GDB does not yet know how to handle dynamic
10456 arrays properly, treat them as arrays with unspecified
10457 length for now.
10458
10459 FIXME: jimb/2003-09-22: GDB does not really know
10460 how to handle arrays of unspecified length
10461 either; we just represent them as zero-length
10462 arrays. Choose an appropriate upper bound given
10463 the lower bound we've computed above. */
10464 high = low - 1;
10465 }
10466 else
10467 high = dwarf2_get_attr_constant_value (attr, 1);
10468 }
e77813c8
PM
10469 else
10470 {
10471 attr = dwarf2_attr (die, DW_AT_count, cu);
10472 if (attr)
10473 {
10474 int count = dwarf2_get_attr_constant_value (attr, 1);
10475 high = low + count - 1;
10476 }
c2ff108b
JK
10477 else
10478 {
10479 /* Unspecified array length. */
10480 high = low - 1;
10481 }
e77813c8
PM
10482 }
10483
10484 /* Dwarf-2 specifications explicitly allows to create subrange types
10485 without specifying a base type.
10486 In that case, the base type must be set to the type of
10487 the lower bound, upper bound or count, in that order, if any of these
10488 three attributes references an object that has a type.
10489 If no base type is found, the Dwarf-2 specifications say that
10490 a signed integer type of size equal to the size of an address should
10491 be used.
10492 For the following C code: `extern char gdb_int [];'
10493 GCC produces an empty range DIE.
10494 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 10495 high bound or count are not yet handled by this code. */
e77813c8
PM
10496 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10497 {
10498 struct objfile *objfile = cu->objfile;
10499 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10500 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10501 struct type *int_type = objfile_type (objfile)->builtin_int;
10502
10503 /* Test "int", "long int", and "long long int" objfile types,
10504 and select the first one having a size above or equal to the
10505 architecture address size. */
10506 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10507 base_type = int_type;
10508 else
10509 {
10510 int_type = objfile_type (objfile)->builtin_long;
10511 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10512 base_type = int_type;
10513 else
10514 {
10515 int_type = objfile_type (objfile)->builtin_long_long;
10516 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10517 base_type = int_type;
10518 }
10519 }
10520 }
a02abb62 10521
6e70227d 10522 negative_mask =
43bbcdc2
PH
10523 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10524 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10525 low |= negative_mask;
10526 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10527 high |= negative_mask;
10528
a02abb62
JB
10529 range_type = create_range_type (NULL, base_type, low, high);
10530
bbb0eef6
JK
10531 /* Mark arrays with dynamic length at least as an array of unspecified
10532 length. GDB could check the boundary but before it gets implemented at
10533 least allow accessing the array elements. */
d48323d8 10534 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
10535 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10536
c2ff108b
JK
10537 /* Ada expects an empty array on no boundary attributes. */
10538 if (attr == NULL && cu->language != language_ada)
10539 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10540
39cbfefa
DJ
10541 name = dwarf2_name (die, cu);
10542 if (name)
10543 TYPE_NAME (range_type) = name;
6e70227d 10544
e142c38c 10545 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
10546 if (attr)
10547 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10548
7e314c57
JK
10549 set_die_type (die, range_type, cu);
10550
10551 /* set_die_type should be already done. */
b4ba55a1
JB
10552 set_descriptive_type (range_type, die, cu);
10553
7e314c57 10554 return range_type;
a02abb62 10555}
6e70227d 10556
f792889a 10557static struct type *
81a17f79
JB
10558read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10559{
10560 struct type *type;
81a17f79 10561
81a17f79
JB
10562 /* For now, we only support the C meaning of an unspecified type: void. */
10563
0114d602
DJ
10564 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10565 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 10566
f792889a 10567 return set_die_type (die, type, cu);
81a17f79 10568}
a02abb62 10569
639d11d3
DC
10570/* Read a single die and all its descendents. Set the die's sibling
10571 field to NULL; set other fields in the die correctly, and set all
10572 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10573 location of the info_ptr after reading all of those dies. PARENT
10574 is the parent of the die in question. */
10575
10576static struct die_info *
dee91e82
DE
10577read_die_and_children (const struct die_reader_specs *reader,
10578 gdb_byte *info_ptr,
10579 gdb_byte **new_info_ptr,
10580 struct die_info *parent)
639d11d3
DC
10581{
10582 struct die_info *die;
fe1b8b76 10583 gdb_byte *cur_ptr;
639d11d3
DC
10584 int has_children;
10585
93311388 10586 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
10587 if (die == NULL)
10588 {
10589 *new_info_ptr = cur_ptr;
10590 return NULL;
10591 }
93311388 10592 store_in_ref_table (die, reader->cu);
639d11d3
DC
10593
10594 if (has_children)
348e048f 10595 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
10596 else
10597 {
10598 die->child = NULL;
10599 *new_info_ptr = cur_ptr;
10600 }
10601
10602 die->sibling = NULL;
10603 die->parent = parent;
10604 return die;
10605}
10606
10607/* Read a die, all of its descendents, and all of its siblings; set
10608 all of the fields of all of the dies correctly. Arguments are as
10609 in read_die_and_children. */
10610
10611static struct die_info *
93311388
DE
10612read_die_and_siblings (const struct die_reader_specs *reader,
10613 gdb_byte *info_ptr,
fe1b8b76 10614 gdb_byte **new_info_ptr,
639d11d3
DC
10615 struct die_info *parent)
10616{
10617 struct die_info *first_die, *last_sibling;
fe1b8b76 10618 gdb_byte *cur_ptr;
639d11d3 10619
c906108c 10620 cur_ptr = info_ptr;
639d11d3
DC
10621 first_die = last_sibling = NULL;
10622
10623 while (1)
c906108c 10624 {
639d11d3 10625 struct die_info *die
dee91e82 10626 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 10627
1d325ec1 10628 if (die == NULL)
c906108c 10629 {
639d11d3
DC
10630 *new_info_ptr = cur_ptr;
10631 return first_die;
c906108c 10632 }
1d325ec1
DJ
10633
10634 if (!first_die)
10635 first_die = die;
c906108c 10636 else
1d325ec1
DJ
10637 last_sibling->sibling = die;
10638
10639 last_sibling = die;
c906108c 10640 }
c906108c
SS
10641}
10642
3019eac3
DE
10643/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10644 attributes.
10645 The caller is responsible for filling in the extra attributes
10646 and updating (*DIEP)->num_attrs.
10647 Set DIEP to point to a newly allocated die with its information,
10648 except for its child, sibling, and parent fields.
10649 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
10650
10651static gdb_byte *
3019eac3
DE
10652read_full_die_1 (const struct die_reader_specs *reader,
10653 struct die_info **diep, gdb_byte *info_ptr,
10654 int *has_children, int num_extra_attrs)
93311388 10655{
b64f50a1
JK
10656 unsigned int abbrev_number, bytes_read, i;
10657 sect_offset offset;
93311388
DE
10658 struct abbrev_info *abbrev;
10659 struct die_info *die;
10660 struct dwarf2_cu *cu = reader->cu;
10661 bfd *abfd = reader->abfd;
10662
b64f50a1 10663 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
10664 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10665 info_ptr += bytes_read;
10666 if (!abbrev_number)
10667 {
10668 *diep = NULL;
10669 *has_children = 0;
10670 return info_ptr;
10671 }
10672
10673 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10674 if (!abbrev)
348e048f
DE
10675 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10676 abbrev_number,
10677 bfd_get_filename (abfd));
10678
3019eac3 10679 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
10680 die->offset = offset;
10681 die->tag = abbrev->tag;
10682 die->abbrev = abbrev_number;
10683
3019eac3
DE
10684 /* Make the result usable.
10685 The caller needs to update num_attrs after adding the extra
10686 attributes. */
93311388
DE
10687 die->num_attrs = abbrev->num_attrs;
10688
10689 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
10690 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10691 info_ptr);
93311388
DE
10692
10693 *diep = die;
10694 *has_children = abbrev->has_children;
10695 return info_ptr;
10696}
10697
3019eac3
DE
10698/* Read a die and all its attributes.
10699 Set DIEP to point to a newly allocated die with its information,
10700 except for its child, sibling, and parent fields.
10701 Set HAS_CHILDREN to tell whether the die has children or not. */
10702
10703static gdb_byte *
10704read_full_die (const struct die_reader_specs *reader,
10705 struct die_info **diep, gdb_byte *info_ptr,
10706 int *has_children)
10707{
10708 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10709}
10710
c906108c
SS
10711/* In DWARF version 2, the description of the debugging information is
10712 stored in a separate .debug_abbrev section. Before we read any
10713 dies from a section we read in all abbreviations and install them
72bf9492
DJ
10714 in a hash table. This function also sets flags in CU describing
10715 the data found in the abbrev table. */
c906108c
SS
10716
10717static void
dee91e82
DE
10718dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10719 struct dwarf2_section_info *abbrev_section)
10720
c906108c 10721{
dee91e82 10722 bfd *abfd = abbrev_section->asection->owner;
e7c27a73 10723 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 10724 gdb_byte *abbrev_ptr;
c906108c
SS
10725 struct abbrev_info *cur_abbrev;
10726 unsigned int abbrev_number, bytes_read, abbrev_name;
10727 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
10728 struct attr_abbrev *cur_attrs;
10729 unsigned int allocated_attrs;
c906108c 10730
0963b4bd 10731 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
10732 obstack_init (&cu->abbrev_obstack);
10733 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10734 (ABBREV_HASH_SIZE
10735 * sizeof (struct abbrev_info *)));
10736 memset (cu->dwarf2_abbrevs, 0,
10737 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 10738
dee91e82
DE
10739 dwarf2_read_section (cu->objfile, abbrev_section);
10740 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
c906108c
SS
10741 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10742 abbrev_ptr += bytes_read;
10743
f3dd6933
DJ
10744 allocated_attrs = ATTR_ALLOC_CHUNK;
10745 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 10746
0963b4bd 10747 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
10748 while (abbrev_number)
10749 {
f3dd6933 10750 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
10751
10752 /* read in abbrev header */
10753 cur_abbrev->number = abbrev_number;
10754 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10755 abbrev_ptr += bytes_read;
10756 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
10757 abbrev_ptr += 1;
10758
10759 /* now read in declarations */
10760 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10761 abbrev_ptr += bytes_read;
10762 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10763 abbrev_ptr += bytes_read;
10764 while (abbrev_name)
10765 {
f3dd6933 10766 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 10767 {
f3dd6933
DJ
10768 allocated_attrs += ATTR_ALLOC_CHUNK;
10769 cur_attrs
10770 = xrealloc (cur_attrs, (allocated_attrs
10771 * sizeof (struct attr_abbrev)));
c906108c 10772 }
ae038cb0 10773
f3dd6933
DJ
10774 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
10775 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
10776 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10777 abbrev_ptr += bytes_read;
10778 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10779 abbrev_ptr += bytes_read;
10780 }
10781
f3dd6933
DJ
10782 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
10783 (cur_abbrev->num_attrs
10784 * sizeof (struct attr_abbrev)));
10785 memcpy (cur_abbrev->attrs, cur_attrs,
10786 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
10787
c906108c 10788 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
10789 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
10790 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
10791
10792 /* Get next abbreviation.
10793 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
10794 always properly terminated with an abbrev number of 0.
10795 Exit loop if we encounter an abbreviation which we have
10796 already read (which means we are about to read the abbreviations
10797 for the next compile unit) or if the end of the abbreviation
10798 table is reached. */
dee91e82
DE
10799 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
10800 >= abbrev_section->size)
c906108c
SS
10801 break;
10802 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10803 abbrev_ptr += bytes_read;
e7c27a73 10804 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
10805 break;
10806 }
f3dd6933
DJ
10807
10808 xfree (cur_attrs);
c906108c
SS
10809}
10810
f3dd6933 10811/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 10812
c906108c 10813static void
f3dd6933 10814dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 10815{
f3dd6933 10816 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 10817
f3dd6933
DJ
10818 obstack_free (&cu->abbrev_obstack, NULL);
10819 cu->dwarf2_abbrevs = NULL;
c906108c
SS
10820}
10821
10822/* Lookup an abbrev_info structure in the abbrev hash table. */
10823
10824static struct abbrev_info *
e7c27a73 10825dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
10826{
10827 unsigned int hash_number;
10828 struct abbrev_info *abbrev;
10829
10830 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 10831 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
10832
10833 while (abbrev)
10834 {
10835 if (abbrev->number == number)
10836 return abbrev;
10837 else
10838 abbrev = abbrev->next;
10839 }
10840 return NULL;
10841}
10842
72bf9492
DJ
10843/* Returns nonzero if TAG represents a type that we might generate a partial
10844 symbol for. */
10845
10846static int
10847is_type_tag_for_partial (int tag)
10848{
10849 switch (tag)
10850 {
10851#if 0
10852 /* Some types that would be reasonable to generate partial symbols for,
10853 that we don't at present. */
10854 case DW_TAG_array_type:
10855 case DW_TAG_file_type:
10856 case DW_TAG_ptr_to_member_type:
10857 case DW_TAG_set_type:
10858 case DW_TAG_string_type:
10859 case DW_TAG_subroutine_type:
10860#endif
10861 case DW_TAG_base_type:
10862 case DW_TAG_class_type:
680b30c7 10863 case DW_TAG_interface_type:
72bf9492
DJ
10864 case DW_TAG_enumeration_type:
10865 case DW_TAG_structure_type:
10866 case DW_TAG_subrange_type:
10867 case DW_TAG_typedef:
10868 case DW_TAG_union_type:
10869 return 1;
10870 default:
10871 return 0;
10872 }
10873}
10874
10875/* Load all DIEs that are interesting for partial symbols into memory. */
10876
10877static struct partial_die_info *
dee91e82
DE
10878load_partial_dies (const struct die_reader_specs *reader,
10879 gdb_byte *info_ptr, int building_psymtab)
72bf9492 10880{
dee91e82 10881 struct dwarf2_cu *cu = reader->cu;
bb5ed363 10882 struct objfile *objfile = cu->objfile;
72bf9492
DJ
10883 struct partial_die_info *part_die;
10884 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
10885 struct abbrev_info *abbrev;
10886 unsigned int bytes_read;
5afb4e99 10887 unsigned int load_all = 0;
72bf9492
DJ
10888 int nesting_level = 1;
10889
10890 parent_die = NULL;
10891 last_die = NULL;
10892
7adf1e79
DE
10893 gdb_assert (cu->per_cu != NULL);
10894 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
10895 load_all = 1;
10896
72bf9492
DJ
10897 cu->partial_dies
10898 = htab_create_alloc_ex (cu->header.length / 12,
10899 partial_die_hash,
10900 partial_die_eq,
10901 NULL,
10902 &cu->comp_unit_obstack,
10903 hashtab_obstack_allocate,
10904 dummy_obstack_deallocate);
10905
10906 part_die = obstack_alloc (&cu->comp_unit_obstack,
10907 sizeof (struct partial_die_info));
10908
10909 while (1)
10910 {
10911 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
10912
10913 /* A NULL abbrev means the end of a series of children. */
10914 if (abbrev == NULL)
10915 {
10916 if (--nesting_level == 0)
10917 {
10918 /* PART_DIE was probably the last thing allocated on the
10919 comp_unit_obstack, so we could call obstack_free
10920 here. We don't do that because the waste is small,
10921 and will be cleaned up when we're done with this
10922 compilation unit. This way, we're also more robust
10923 against other users of the comp_unit_obstack. */
10924 return first_die;
10925 }
10926 info_ptr += bytes_read;
10927 last_die = parent_die;
10928 parent_die = parent_die->die_parent;
10929 continue;
10930 }
10931
98bfdba5
PA
10932 /* Check for template arguments. We never save these; if
10933 they're seen, we just mark the parent, and go on our way. */
10934 if (parent_die != NULL
10935 && cu->language == language_cplus
10936 && (abbrev->tag == DW_TAG_template_type_param
10937 || abbrev->tag == DW_TAG_template_value_param))
10938 {
10939 parent_die->has_template_arguments = 1;
10940
10941 if (!load_all)
10942 {
10943 /* We don't need a partial DIE for the template argument. */
dee91e82 10944 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
10945 continue;
10946 }
10947 }
10948
0d99eb77 10949 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
10950 Skip their other children. */
10951 if (!load_all
10952 && cu->language == language_cplus
10953 && parent_die != NULL
10954 && parent_die->tag == DW_TAG_subprogram)
10955 {
dee91e82 10956 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
10957 continue;
10958 }
10959
5afb4e99
DJ
10960 /* Check whether this DIE is interesting enough to save. Normally
10961 we would not be interested in members here, but there may be
10962 later variables referencing them via DW_AT_specification (for
10963 static members). */
10964 if (!load_all
10965 && !is_type_tag_for_partial (abbrev->tag)
72929c62 10966 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
10967 && abbrev->tag != DW_TAG_enumerator
10968 && abbrev->tag != DW_TAG_subprogram
bc30ff58 10969 && abbrev->tag != DW_TAG_lexical_block
72bf9492 10970 && abbrev->tag != DW_TAG_variable
5afb4e99 10971 && abbrev->tag != DW_TAG_namespace
f55ee35c 10972 && abbrev->tag != DW_TAG_module
95554aad
TT
10973 && abbrev->tag != DW_TAG_member
10974 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
10975 {
10976 /* Otherwise we skip to the next sibling, if any. */
dee91e82 10977 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
10978 continue;
10979 }
10980
dee91e82
DE
10981 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
10982 info_ptr);
72bf9492
DJ
10983
10984 /* This two-pass algorithm for processing partial symbols has a
10985 high cost in cache pressure. Thus, handle some simple cases
10986 here which cover the majority of C partial symbols. DIEs
10987 which neither have specification tags in them, nor could have
10988 specification tags elsewhere pointing at them, can simply be
10989 processed and discarded.
10990
10991 This segment is also optional; scan_partial_symbols and
10992 add_partial_symbol will handle these DIEs if we chain
10993 them in normally. When compilers which do not emit large
10994 quantities of duplicate debug information are more common,
10995 this code can probably be removed. */
10996
10997 /* Any complete simple types at the top level (pretty much all
10998 of them, for a language without namespaces), can be processed
10999 directly. */
11000 if (parent_die == NULL
11001 && part_die->has_specification == 0
11002 && part_die->is_declaration == 0
d8228535 11003 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
11004 || part_die->tag == DW_TAG_base_type
11005 || part_die->tag == DW_TAG_subrange_type))
11006 {
11007 if (building_psymtab && part_die->name != NULL)
04a679b8 11008 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11009 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
11010 &objfile->static_psymbols,
11011 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 11012 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11013 continue;
11014 }
11015
d8228535
JK
11016 /* The exception for DW_TAG_typedef with has_children above is
11017 a workaround of GCC PR debug/47510. In the case of this complaint
11018 type_name_no_tag_or_error will error on such types later.
11019
11020 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11021 it could not find the child DIEs referenced later, this is checked
11022 above. In correct DWARF DW_TAG_typedef should have no children. */
11023
11024 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11025 complaint (&symfile_complaints,
11026 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11027 "- DIE at 0x%x [in module %s]"),
b64f50a1 11028 part_die->offset.sect_off, objfile->name);
d8228535 11029
72bf9492
DJ
11030 /* If we're at the second level, and we're an enumerator, and
11031 our parent has no specification (meaning possibly lives in a
11032 namespace elsewhere), then we can add the partial symbol now
11033 instead of queueing it. */
11034 if (part_die->tag == DW_TAG_enumerator
11035 && parent_die != NULL
11036 && parent_die->die_parent == NULL
11037 && parent_die->tag == DW_TAG_enumeration_type
11038 && parent_die->has_specification == 0)
11039 {
11040 if (part_die->name == NULL)
3e43a32a
MS
11041 complaint (&symfile_complaints,
11042 _("malformed enumerator DIE ignored"));
72bf9492 11043 else if (building_psymtab)
04a679b8 11044 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11045 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
11046 (cu->language == language_cplus
11047 || cu->language == language_java)
bb5ed363
DE
11048 ? &objfile->global_psymbols
11049 : &objfile->static_psymbols,
11050 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 11051
dee91e82 11052 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11053 continue;
11054 }
11055
11056 /* We'll save this DIE so link it in. */
11057 part_die->die_parent = parent_die;
11058 part_die->die_sibling = NULL;
11059 part_die->die_child = NULL;
11060
11061 if (last_die && last_die == parent_die)
11062 last_die->die_child = part_die;
11063 else if (last_die)
11064 last_die->die_sibling = part_die;
11065
11066 last_die = part_die;
11067
11068 if (first_die == NULL)
11069 first_die = part_die;
11070
11071 /* Maybe add the DIE to the hash table. Not all DIEs that we
11072 find interesting need to be in the hash table, because we
11073 also have the parent/sibling/child chains; only those that we
11074 might refer to by offset later during partial symbol reading.
11075
11076 For now this means things that might have be the target of a
11077 DW_AT_specification, DW_AT_abstract_origin, or
11078 DW_AT_extension. DW_AT_extension will refer only to
11079 namespaces; DW_AT_abstract_origin refers to functions (and
11080 many things under the function DIE, but we do not recurse
11081 into function DIEs during partial symbol reading) and
11082 possibly variables as well; DW_AT_specification refers to
11083 declarations. Declarations ought to have the DW_AT_declaration
11084 flag. It happens that GCC forgets to put it in sometimes, but
11085 only for functions, not for types.
11086
11087 Adding more things than necessary to the hash table is harmless
11088 except for the performance cost. Adding too few will result in
5afb4e99
DJ
11089 wasted time in find_partial_die, when we reread the compilation
11090 unit with load_all_dies set. */
72bf9492 11091
5afb4e99 11092 if (load_all
72929c62 11093 || abbrev->tag == DW_TAG_constant
5afb4e99 11094 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
11095 || abbrev->tag == DW_TAG_variable
11096 || abbrev->tag == DW_TAG_namespace
11097 || part_die->is_declaration)
11098 {
11099 void **slot;
11100
11101 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 11102 part_die->offset.sect_off, INSERT);
72bf9492
DJ
11103 *slot = part_die;
11104 }
11105
11106 part_die = obstack_alloc (&cu->comp_unit_obstack,
11107 sizeof (struct partial_die_info));
11108
11109 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 11110 we have no reason to follow the children of structures; for other
98bfdba5
PA
11111 languages we have to, so that we can get at method physnames
11112 to infer fully qualified class names, for DW_AT_specification,
11113 and for C++ template arguments. For C++, we also look one level
11114 inside functions to find template arguments (if the name of the
11115 function does not already contain the template arguments).
bc30ff58
JB
11116
11117 For Ada, we need to scan the children of subprograms and lexical
11118 blocks as well because Ada allows the definition of nested
11119 entities that could be interesting for the debugger, such as
11120 nested subprograms for instance. */
72bf9492 11121 if (last_die->has_children
5afb4e99
DJ
11122 && (load_all
11123 || last_die->tag == DW_TAG_namespace
f55ee35c 11124 || last_die->tag == DW_TAG_module
72bf9492 11125 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
11126 || (cu->language == language_cplus
11127 && last_die->tag == DW_TAG_subprogram
11128 && (last_die->name == NULL
11129 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
11130 || (cu->language != language_c
11131 && (last_die->tag == DW_TAG_class_type
680b30c7 11132 || last_die->tag == DW_TAG_interface_type
72bf9492 11133 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
11134 || last_die->tag == DW_TAG_union_type))
11135 || (cu->language == language_ada
11136 && (last_die->tag == DW_TAG_subprogram
11137 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
11138 {
11139 nesting_level++;
11140 parent_die = last_die;
11141 continue;
11142 }
11143
11144 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11145 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
11146
11147 /* Back to the top, do it again. */
11148 }
11149}
11150
c906108c
SS
11151/* Read a minimal amount of information into the minimal die structure. */
11152
fe1b8b76 11153static gdb_byte *
dee91e82
DE
11154read_partial_die (const struct die_reader_specs *reader,
11155 struct partial_die_info *part_die,
11156 struct abbrev_info *abbrev, unsigned int abbrev_len,
11157 gdb_byte *info_ptr)
c906108c 11158{
dee91e82 11159 struct dwarf2_cu *cu = reader->cu;
bb5ed363 11160 struct objfile *objfile = cu->objfile;
dee91e82 11161 gdb_byte *buffer = reader->buffer;
fa238c03 11162 unsigned int i;
c906108c 11163 struct attribute attr;
c5aa993b 11164 int has_low_pc_attr = 0;
c906108c 11165 int has_high_pc_attr = 0;
91da1414 11166 int high_pc_relative = 0;
c906108c 11167
72bf9492 11168 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 11169
b64f50a1 11170 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
11171
11172 info_ptr += abbrev_len;
11173
11174 if (abbrev == NULL)
11175 return info_ptr;
11176
c906108c
SS
11177 part_die->tag = abbrev->tag;
11178 part_die->has_children = abbrev->has_children;
c906108c
SS
11179
11180 for (i = 0; i < abbrev->num_attrs; ++i)
11181 {
dee91e82 11182 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
11183
11184 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 11185 partial symbol table. */
c906108c
SS
11186 switch (attr.name)
11187 {
11188 case DW_AT_name:
71c25dea
TT
11189 switch (part_die->tag)
11190 {
11191 case DW_TAG_compile_unit:
95554aad 11192 case DW_TAG_partial_unit:
348e048f 11193 case DW_TAG_type_unit:
71c25dea
TT
11194 /* Compilation units have a DW_AT_name that is a filename, not
11195 a source language identifier. */
11196 case DW_TAG_enumeration_type:
11197 case DW_TAG_enumerator:
11198 /* These tags always have simple identifiers already; no need
11199 to canonicalize them. */
11200 part_die->name = DW_STRING (&attr);
11201 break;
11202 default:
11203 part_die->name
11204 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 11205 &objfile->objfile_obstack);
71c25dea
TT
11206 break;
11207 }
c906108c 11208 break;
31ef98ae 11209 case DW_AT_linkage_name:
c906108c 11210 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
11211 /* Note that both forms of linkage name might appear. We
11212 assume they will be the same, and we only store the last
11213 one we see. */
94af9270
KS
11214 if (cu->language == language_ada)
11215 part_die->name = DW_STRING (&attr);
abc72ce4 11216 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
11217 break;
11218 case DW_AT_low_pc:
11219 has_low_pc_attr = 1;
11220 part_die->lowpc = DW_ADDR (&attr);
11221 break;
11222 case DW_AT_high_pc:
11223 has_high_pc_attr = 1;
3019eac3
DE
11224 if (attr.form == DW_FORM_addr
11225 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
11226 part_die->highpc = DW_ADDR (&attr);
11227 else
11228 {
11229 high_pc_relative = 1;
11230 part_die->highpc = DW_UNSND (&attr);
11231 }
c906108c
SS
11232 break;
11233 case DW_AT_location:
0963b4bd 11234 /* Support the .debug_loc offsets. */
8e19ed76
PS
11235 if (attr_form_is_block (&attr))
11236 {
95554aad 11237 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 11238 }
3690dd37 11239 else if (attr_form_is_section_offset (&attr))
8e19ed76 11240 {
4d3c2250 11241 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11242 }
11243 else
11244 {
4d3c2250
KB
11245 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11246 "partial symbol information");
8e19ed76 11247 }
c906108c 11248 break;
c906108c
SS
11249 case DW_AT_external:
11250 part_die->is_external = DW_UNSND (&attr);
11251 break;
11252 case DW_AT_declaration:
11253 part_die->is_declaration = DW_UNSND (&attr);
11254 break;
11255 case DW_AT_type:
11256 part_die->has_type = 1;
11257 break;
11258 case DW_AT_abstract_origin:
11259 case DW_AT_specification:
72bf9492
DJ
11260 case DW_AT_extension:
11261 part_die->has_specification = 1;
c764a876 11262 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
11263 break;
11264 case DW_AT_sibling:
11265 /* Ignore absolute siblings, they might point outside of
11266 the current compile unit. */
11267 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
11268 complaint (&symfile_complaints,
11269 _("ignoring absolute DW_AT_sibling"));
c906108c 11270 else
b64f50a1 11271 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 11272 break;
fa4028e9
JB
11273 case DW_AT_byte_size:
11274 part_die->has_byte_size = 1;
11275 break;
68511cec
CES
11276 case DW_AT_calling_convention:
11277 /* DWARF doesn't provide a way to identify a program's source-level
11278 entry point. DW_AT_calling_convention attributes are only meant
11279 to describe functions' calling conventions.
11280
11281 However, because it's a necessary piece of information in
11282 Fortran, and because DW_CC_program is the only piece of debugging
11283 information whose definition refers to a 'main program' at all,
11284 several compilers have begun marking Fortran main programs with
11285 DW_CC_program --- even when those functions use the standard
11286 calling conventions.
11287
11288 So until DWARF specifies a way to provide this information and
11289 compilers pick up the new representation, we'll support this
11290 practice. */
11291 if (DW_UNSND (&attr) == DW_CC_program
11292 && cu->language == language_fortran)
01f8c46d
JK
11293 {
11294 set_main_name (part_die->name);
11295
11296 /* As this DIE has a static linkage the name would be difficult
11297 to look up later. */
11298 language_of_main = language_fortran;
11299 }
68511cec 11300 break;
481860b3
GB
11301 case DW_AT_inline:
11302 if (DW_UNSND (&attr) == DW_INL_inlined
11303 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11304 part_die->may_be_inlined = 1;
11305 break;
95554aad
TT
11306
11307 case DW_AT_import:
11308 if (part_die->tag == DW_TAG_imported_unit)
11309 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11310 break;
11311
c906108c
SS
11312 default:
11313 break;
11314 }
11315 }
11316
91da1414
MW
11317 if (high_pc_relative)
11318 part_die->highpc += part_die->lowpc;
11319
9373cf26
JK
11320 if (has_low_pc_attr && has_high_pc_attr)
11321 {
11322 /* When using the GNU linker, .gnu.linkonce. sections are used to
11323 eliminate duplicate copies of functions and vtables and such.
11324 The linker will arbitrarily choose one and discard the others.
11325 The AT_*_pc values for such functions refer to local labels in
11326 these sections. If the section from that file was discarded, the
11327 labels are not in the output, so the relocs get a value of 0.
11328 If this is a discarded function, mark the pc bounds as invalid,
11329 so that GDB will ignore it. */
11330 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11331 {
bb5ed363 11332 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11333
11334 complaint (&symfile_complaints,
11335 _("DW_AT_low_pc %s is zero "
11336 "for DIE at 0x%x [in module %s]"),
11337 paddress (gdbarch, part_die->lowpc),
b64f50a1 11338 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11339 }
11340 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11341 else if (part_die->lowpc >= part_die->highpc)
11342 {
bb5ed363 11343 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11344
11345 complaint (&symfile_complaints,
11346 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11347 "for DIE at 0x%x [in module %s]"),
11348 paddress (gdbarch, part_die->lowpc),
11349 paddress (gdbarch, part_die->highpc),
b64f50a1 11350 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11351 }
11352 else
11353 part_die->has_pc_info = 1;
11354 }
85cbf3d3 11355
c906108c
SS
11356 return info_ptr;
11357}
11358
72bf9492
DJ
11359/* Find a cached partial DIE at OFFSET in CU. */
11360
11361static struct partial_die_info *
b64f50a1 11362find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
11363{
11364 struct partial_die_info *lookup_die = NULL;
11365 struct partial_die_info part_die;
11366
11367 part_die.offset = offset;
b64f50a1
JK
11368 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11369 offset.sect_off);
72bf9492 11370
72bf9492
DJ
11371 return lookup_die;
11372}
11373
348e048f
DE
11374/* Find a partial DIE at OFFSET, which may or may not be in CU,
11375 except in the case of .debug_types DIEs which do not reference
11376 outside their CU (they do however referencing other types via
55f1336d 11377 DW_FORM_ref_sig8). */
72bf9492
DJ
11378
11379static struct partial_die_info *
b64f50a1 11380find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
72bf9492 11381{
bb5ed363 11382 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
11383 struct dwarf2_per_cu_data *per_cu = NULL;
11384 struct partial_die_info *pd = NULL;
72bf9492 11385
45452591 11386 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
11387 {
11388 pd = find_partial_die_in_comp_unit (offset, cu);
11389 if (pd != NULL)
11390 return pd;
0d99eb77
DE
11391 /* We missed recording what we needed.
11392 Load all dies and try again. */
11393 per_cu = cu->per_cu;
5afb4e99 11394 }
0d99eb77
DE
11395 else
11396 {
11397 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 11398 if (cu->per_cu->is_debug_types)
0d99eb77
DE
11399 {
11400 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11401 " external reference to offset 0x%lx [in module %s].\n"),
11402 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11403 bfd_get_filename (objfile->obfd));
11404 }
11405 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
72bf9492 11406
0d99eb77
DE
11407 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11408 load_partial_comp_unit (per_cu);
ae038cb0 11409
0d99eb77
DE
11410 per_cu->cu->last_used = 0;
11411 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11412 }
5afb4e99 11413
dee91e82
DE
11414 /* If we didn't find it, and not all dies have been loaded,
11415 load them all and try again. */
11416
5afb4e99
DJ
11417 if (pd == NULL && per_cu->load_all_dies == 0)
11418 {
5afb4e99 11419 per_cu->load_all_dies = 1;
fd820528
DE
11420
11421 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11422 THIS_CU->cu may already be in use. So we can't just free it and
11423 replace its DIEs with the ones we read in. Instead, we leave those
11424 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11425 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11426 set. */
dee91e82 11427 load_partial_comp_unit (per_cu);
5afb4e99
DJ
11428
11429 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11430 }
11431
11432 if (pd == NULL)
11433 internal_error (__FILE__, __LINE__,
3e43a32a
MS
11434 _("could not find partial DIE 0x%x "
11435 "in cache [from module %s]\n"),
b64f50a1 11436 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 11437 return pd;
72bf9492
DJ
11438}
11439
abc72ce4
DE
11440/* See if we can figure out if the class lives in a namespace. We do
11441 this by looking for a member function; its demangled name will
11442 contain namespace info, if there is any. */
11443
11444static void
11445guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11446 struct dwarf2_cu *cu)
11447{
11448 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11449 what template types look like, because the demangler
11450 frequently doesn't give the same name as the debug info. We
11451 could fix this by only using the demangled name to get the
11452 prefix (but see comment in read_structure_type). */
11453
11454 struct partial_die_info *real_pdi;
11455 struct partial_die_info *child_pdi;
11456
11457 /* If this DIE (this DIE's specification, if any) has a parent, then
11458 we should not do this. We'll prepend the parent's fully qualified
11459 name when we create the partial symbol. */
11460
11461 real_pdi = struct_pdi;
11462 while (real_pdi->has_specification)
11463 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11464
11465 if (real_pdi->die_parent != NULL)
11466 return;
11467
11468 for (child_pdi = struct_pdi->die_child;
11469 child_pdi != NULL;
11470 child_pdi = child_pdi->die_sibling)
11471 {
11472 if (child_pdi->tag == DW_TAG_subprogram
11473 && child_pdi->linkage_name != NULL)
11474 {
11475 char *actual_class_name
11476 = language_class_name_from_physname (cu->language_defn,
11477 child_pdi->linkage_name);
11478 if (actual_class_name != NULL)
11479 {
11480 struct_pdi->name
11481 = obsavestring (actual_class_name,
11482 strlen (actual_class_name),
11483 &cu->objfile->objfile_obstack);
11484 xfree (actual_class_name);
11485 }
11486 break;
11487 }
11488 }
11489}
11490
72bf9492
DJ
11491/* Adjust PART_DIE before generating a symbol for it. This function
11492 may set the is_external flag or change the DIE's name. */
11493
11494static void
11495fixup_partial_die (struct partial_die_info *part_die,
11496 struct dwarf2_cu *cu)
11497{
abc72ce4
DE
11498 /* Once we've fixed up a die, there's no point in doing so again.
11499 This also avoids a memory leak if we were to call
11500 guess_partial_die_structure_name multiple times. */
11501 if (part_die->fixup_called)
11502 return;
11503
72bf9492
DJ
11504 /* If we found a reference attribute and the DIE has no name, try
11505 to find a name in the referred to DIE. */
11506
11507 if (part_die->name == NULL && part_die->has_specification)
11508 {
11509 struct partial_die_info *spec_die;
72bf9492 11510
10b3939b 11511 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 11512
10b3939b 11513 fixup_partial_die (spec_die, cu);
72bf9492
DJ
11514
11515 if (spec_die->name)
11516 {
11517 part_die->name = spec_die->name;
11518
11519 /* Copy DW_AT_external attribute if it is set. */
11520 if (spec_die->is_external)
11521 part_die->is_external = spec_die->is_external;
11522 }
11523 }
11524
11525 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
11526
11527 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 11528 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 11529
abc72ce4
DE
11530 /* If there is no parent die to provide a namespace, and there are
11531 children, see if we can determine the namespace from their linkage
122d1940 11532 name. */
abc72ce4 11533 if (cu->language == language_cplus
8b70b953 11534 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
11535 && part_die->die_parent == NULL
11536 && part_die->has_children
11537 && (part_die->tag == DW_TAG_class_type
11538 || part_die->tag == DW_TAG_structure_type
11539 || part_die->tag == DW_TAG_union_type))
11540 guess_partial_die_structure_name (part_die, cu);
11541
53832f31
TT
11542 /* GCC might emit a nameless struct or union that has a linkage
11543 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11544 if (part_die->name == NULL
96408a79
SA
11545 && (part_die->tag == DW_TAG_class_type
11546 || part_die->tag == DW_TAG_interface_type
11547 || part_die->tag == DW_TAG_structure_type
11548 || part_die->tag == DW_TAG_union_type)
53832f31
TT
11549 && part_die->linkage_name != NULL)
11550 {
11551 char *demangled;
11552
11553 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11554 if (demangled)
11555 {
96408a79
SA
11556 const char *base;
11557
11558 /* Strip any leading namespaces/classes, keep only the base name.
11559 DW_AT_name for named DIEs does not contain the prefixes. */
11560 base = strrchr (demangled, ':');
11561 if (base && base > demangled && base[-1] == ':')
11562 base++;
11563 else
11564 base = demangled;
11565
11566 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
11567 &cu->objfile->objfile_obstack);
11568 xfree (demangled);
11569 }
11570 }
11571
abc72ce4 11572 part_die->fixup_called = 1;
72bf9492
DJ
11573}
11574
a8329558 11575/* Read an attribute value described by an attribute form. */
c906108c 11576
fe1b8b76 11577static gdb_byte *
dee91e82
DE
11578read_attribute_value (const struct die_reader_specs *reader,
11579 struct attribute *attr, unsigned form,
11580 gdb_byte *info_ptr)
c906108c 11581{
dee91e82
DE
11582 struct dwarf2_cu *cu = reader->cu;
11583 bfd *abfd = reader->abfd;
e7c27a73 11584 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
11585 unsigned int bytes_read;
11586 struct dwarf_block *blk;
11587
a8329558
KW
11588 attr->form = form;
11589 switch (form)
c906108c 11590 {
c906108c 11591 case DW_FORM_ref_addr:
ae411497 11592 if (cu->header.version == 2)
4568ecf9 11593 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 11594 else
4568ecf9
DE
11595 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11596 &cu->header, &bytes_read);
ae411497
TT
11597 info_ptr += bytes_read;
11598 break;
11599 case DW_FORM_addr:
e7c27a73 11600 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 11601 info_ptr += bytes_read;
c906108c
SS
11602 break;
11603 case DW_FORM_block2:
7b5a2f43 11604 blk = dwarf_alloc_block (cu);
c906108c
SS
11605 blk->size = read_2_bytes (abfd, info_ptr);
11606 info_ptr += 2;
11607 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11608 info_ptr += blk->size;
11609 DW_BLOCK (attr) = blk;
11610 break;
11611 case DW_FORM_block4:
7b5a2f43 11612 blk = dwarf_alloc_block (cu);
c906108c
SS
11613 blk->size = read_4_bytes (abfd, info_ptr);
11614 info_ptr += 4;
11615 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11616 info_ptr += blk->size;
11617 DW_BLOCK (attr) = blk;
11618 break;
11619 case DW_FORM_data2:
11620 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11621 info_ptr += 2;
11622 break;
11623 case DW_FORM_data4:
11624 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11625 info_ptr += 4;
11626 break;
11627 case DW_FORM_data8:
11628 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11629 info_ptr += 8;
11630 break;
2dc7f7b3
TT
11631 case DW_FORM_sec_offset:
11632 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11633 info_ptr += bytes_read;
11634 break;
c906108c 11635 case DW_FORM_string:
9b1c24c8 11636 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 11637 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
11638 info_ptr += bytes_read;
11639 break;
4bdf3d34
JJ
11640 case DW_FORM_strp:
11641 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11642 &bytes_read);
8285870a 11643 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
11644 info_ptr += bytes_read;
11645 break;
2dc7f7b3 11646 case DW_FORM_exprloc:
c906108c 11647 case DW_FORM_block:
7b5a2f43 11648 blk = dwarf_alloc_block (cu);
c906108c
SS
11649 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11650 info_ptr += bytes_read;
11651 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11652 info_ptr += blk->size;
11653 DW_BLOCK (attr) = blk;
11654 break;
11655 case DW_FORM_block1:
7b5a2f43 11656 blk = dwarf_alloc_block (cu);
c906108c
SS
11657 blk->size = read_1_byte (abfd, info_ptr);
11658 info_ptr += 1;
11659 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11660 info_ptr += blk->size;
11661 DW_BLOCK (attr) = blk;
11662 break;
11663 case DW_FORM_data1:
11664 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11665 info_ptr += 1;
11666 break;
11667 case DW_FORM_flag:
11668 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11669 info_ptr += 1;
11670 break;
2dc7f7b3
TT
11671 case DW_FORM_flag_present:
11672 DW_UNSND (attr) = 1;
11673 break;
c906108c
SS
11674 case DW_FORM_sdata:
11675 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11676 info_ptr += bytes_read;
11677 break;
11678 case DW_FORM_udata:
11679 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11680 info_ptr += bytes_read;
11681 break;
11682 case DW_FORM_ref1:
4568ecf9
DE
11683 DW_UNSND (attr) = (cu->header.offset.sect_off
11684 + read_1_byte (abfd, info_ptr));
c906108c
SS
11685 info_ptr += 1;
11686 break;
11687 case DW_FORM_ref2:
4568ecf9
DE
11688 DW_UNSND (attr) = (cu->header.offset.sect_off
11689 + read_2_bytes (abfd, info_ptr));
c906108c
SS
11690 info_ptr += 2;
11691 break;
11692 case DW_FORM_ref4:
4568ecf9
DE
11693 DW_UNSND (attr) = (cu->header.offset.sect_off
11694 + read_4_bytes (abfd, info_ptr));
c906108c
SS
11695 info_ptr += 4;
11696 break;
613e1657 11697 case DW_FORM_ref8:
4568ecf9
DE
11698 DW_UNSND (attr) = (cu->header.offset.sect_off
11699 + read_8_bytes (abfd, info_ptr));
613e1657
KB
11700 info_ptr += 8;
11701 break;
55f1336d 11702 case DW_FORM_ref_sig8:
348e048f
DE
11703 /* Convert the signature to something we can record in DW_UNSND
11704 for later lookup.
11705 NOTE: This is NULL if the type wasn't found. */
11706 DW_SIGNATURED_TYPE (attr) =
e319fa28 11707 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
11708 info_ptr += 8;
11709 break;
c906108c 11710 case DW_FORM_ref_udata:
4568ecf9
DE
11711 DW_UNSND (attr) = (cu->header.offset.sect_off
11712 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
11713 info_ptr += bytes_read;
11714 break;
c906108c 11715 case DW_FORM_indirect:
a8329558
KW
11716 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11717 info_ptr += bytes_read;
dee91e82 11718 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 11719 break;
3019eac3
DE
11720 case DW_FORM_GNU_addr_index:
11721 if (reader->dwo_file == NULL)
11722 {
11723 /* For now flag a hard error.
11724 Later we can turn this into a complaint. */
11725 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11726 dwarf_form_name (form),
11727 bfd_get_filename (abfd));
11728 }
11729 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11730 info_ptr += bytes_read;
11731 break;
11732 case DW_FORM_GNU_str_index:
11733 if (reader->dwo_file == NULL)
11734 {
11735 /* For now flag a hard error.
11736 Later we can turn this into a complaint if warranted. */
11737 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11738 dwarf_form_name (form),
11739 bfd_get_filename (abfd));
11740 }
11741 {
11742 ULONGEST str_index =
11743 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11744
11745 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11746 DW_STRING_IS_CANONICAL (attr) = 0;
11747 info_ptr += bytes_read;
11748 }
11749 break;
c906108c 11750 default:
8a3fe4f8 11751 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
11752 dwarf_form_name (form),
11753 bfd_get_filename (abfd));
c906108c 11754 }
28e94949
JB
11755
11756 /* We have seen instances where the compiler tried to emit a byte
11757 size attribute of -1 which ended up being encoded as an unsigned
11758 0xffffffff. Although 0xffffffff is technically a valid size value,
11759 an object of this size seems pretty unlikely so we can relatively
11760 safely treat these cases as if the size attribute was invalid and
11761 treat them as zero by default. */
11762 if (attr->name == DW_AT_byte_size
11763 && form == DW_FORM_data4
11764 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
11765 {
11766 complaint
11767 (&symfile_complaints,
43bbcdc2
PH
11768 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
11769 hex_string (DW_UNSND (attr)));
01c66ae6
JB
11770 DW_UNSND (attr) = 0;
11771 }
28e94949 11772
c906108c
SS
11773 return info_ptr;
11774}
11775
a8329558
KW
11776/* Read an attribute described by an abbreviated attribute. */
11777
fe1b8b76 11778static gdb_byte *
dee91e82
DE
11779read_attribute (const struct die_reader_specs *reader,
11780 struct attribute *attr, struct attr_abbrev *abbrev,
11781 gdb_byte *info_ptr)
a8329558
KW
11782{
11783 attr->name = abbrev->name;
dee91e82 11784 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
11785}
11786
0963b4bd 11787/* Read dwarf information from a buffer. */
c906108c
SS
11788
11789static unsigned int
fe1b8b76 11790read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 11791{
fe1b8b76 11792 return bfd_get_8 (abfd, buf);
c906108c
SS
11793}
11794
11795static int
fe1b8b76 11796read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 11797{
fe1b8b76 11798 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
11799}
11800
11801static unsigned int
fe1b8b76 11802read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11803{
fe1b8b76 11804 return bfd_get_16 (abfd, buf);
c906108c
SS
11805}
11806
21ae7a4d
JK
11807static int
11808read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
11809{
11810 return bfd_get_signed_16 (abfd, buf);
11811}
11812
c906108c 11813static unsigned int
fe1b8b76 11814read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11815{
fe1b8b76 11816 return bfd_get_32 (abfd, buf);
c906108c
SS
11817}
11818
21ae7a4d
JK
11819static int
11820read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
11821{
11822 return bfd_get_signed_32 (abfd, buf);
11823}
11824
93311388 11825static ULONGEST
fe1b8b76 11826read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11827{
fe1b8b76 11828 return bfd_get_64 (abfd, buf);
c906108c
SS
11829}
11830
11831static CORE_ADDR
fe1b8b76 11832read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 11833 unsigned int *bytes_read)
c906108c 11834{
e7c27a73 11835 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
11836 CORE_ADDR retval = 0;
11837
107d2387 11838 if (cu_header->signed_addr_p)
c906108c 11839 {
107d2387
AC
11840 switch (cu_header->addr_size)
11841 {
11842 case 2:
fe1b8b76 11843 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
11844 break;
11845 case 4:
fe1b8b76 11846 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
11847 break;
11848 case 8:
fe1b8b76 11849 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
11850 break;
11851 default:
8e65ff28 11852 internal_error (__FILE__, __LINE__,
e2e0b3e5 11853 _("read_address: bad switch, signed [in module %s]"),
659b0389 11854 bfd_get_filename (abfd));
107d2387
AC
11855 }
11856 }
11857 else
11858 {
11859 switch (cu_header->addr_size)
11860 {
11861 case 2:
fe1b8b76 11862 retval = bfd_get_16 (abfd, buf);
107d2387
AC
11863 break;
11864 case 4:
fe1b8b76 11865 retval = bfd_get_32 (abfd, buf);
107d2387
AC
11866 break;
11867 case 8:
fe1b8b76 11868 retval = bfd_get_64 (abfd, buf);
107d2387
AC
11869 break;
11870 default:
8e65ff28 11871 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
11872 _("read_address: bad switch, "
11873 "unsigned [in module %s]"),
659b0389 11874 bfd_get_filename (abfd));
107d2387 11875 }
c906108c 11876 }
64367e0a 11877
107d2387
AC
11878 *bytes_read = cu_header->addr_size;
11879 return retval;
c906108c
SS
11880}
11881
f7ef9339 11882/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
11883 specification allows the initial length to take up either 4 bytes
11884 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
11885 bytes describe the length and all offsets will be 8 bytes in length
11886 instead of 4.
11887
f7ef9339
KB
11888 An older, non-standard 64-bit format is also handled by this
11889 function. The older format in question stores the initial length
11890 as an 8-byte quantity without an escape value. Lengths greater
11891 than 2^32 aren't very common which means that the initial 4 bytes
11892 is almost always zero. Since a length value of zero doesn't make
11893 sense for the 32-bit format, this initial zero can be considered to
11894 be an escape value which indicates the presence of the older 64-bit
11895 format. As written, the code can't detect (old format) lengths
917c78fc
MK
11896 greater than 4GB. If it becomes necessary to handle lengths
11897 somewhat larger than 4GB, we could allow other small values (such
11898 as the non-sensical values of 1, 2, and 3) to also be used as
11899 escape values indicating the presence of the old format.
f7ef9339 11900
917c78fc
MK
11901 The value returned via bytes_read should be used to increment the
11902 relevant pointer after calling read_initial_length().
c764a876 11903
613e1657
KB
11904 [ Note: read_initial_length() and read_offset() are based on the
11905 document entitled "DWARF Debugging Information Format", revision
f7ef9339 11906 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
11907 from:
11908
f7ef9339 11909 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 11910
613e1657
KB
11911 This document is only a draft and is subject to change. (So beware.)
11912
f7ef9339 11913 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
11914 determined empirically by examining 64-bit ELF files produced by
11915 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
11916
11917 - Kevin, July 16, 2002
613e1657
KB
11918 ] */
11919
11920static LONGEST
c764a876 11921read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 11922{
fe1b8b76 11923 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 11924
dd373385 11925 if (length == 0xffffffff)
613e1657 11926 {
fe1b8b76 11927 length = bfd_get_64 (abfd, buf + 4);
613e1657 11928 *bytes_read = 12;
613e1657 11929 }
dd373385 11930 else if (length == 0)
f7ef9339 11931 {
dd373385 11932 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 11933 length = bfd_get_64 (abfd, buf);
f7ef9339 11934 *bytes_read = 8;
f7ef9339 11935 }
613e1657
KB
11936 else
11937 {
11938 *bytes_read = 4;
613e1657
KB
11939 }
11940
c764a876
DE
11941 return length;
11942}
dd373385 11943
c764a876
DE
11944/* Cover function for read_initial_length.
11945 Returns the length of the object at BUF, and stores the size of the
11946 initial length in *BYTES_READ and stores the size that offsets will be in
11947 *OFFSET_SIZE.
11948 If the initial length size is not equivalent to that specified in
11949 CU_HEADER then issue a complaint.
11950 This is useful when reading non-comp-unit headers. */
dd373385 11951
c764a876
DE
11952static LONGEST
11953read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
11954 const struct comp_unit_head *cu_header,
11955 unsigned int *bytes_read,
11956 unsigned int *offset_size)
11957{
11958 LONGEST length = read_initial_length (abfd, buf, bytes_read);
11959
11960 gdb_assert (cu_header->initial_length_size == 4
11961 || cu_header->initial_length_size == 8
11962 || cu_header->initial_length_size == 12);
11963
11964 if (cu_header->initial_length_size != *bytes_read)
11965 complaint (&symfile_complaints,
11966 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 11967
c764a876 11968 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 11969 return length;
613e1657
KB
11970}
11971
11972/* Read an offset from the data stream. The size of the offset is
917c78fc 11973 given by cu_header->offset_size. */
613e1657
KB
11974
11975static LONGEST
fe1b8b76 11976read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 11977 unsigned int *bytes_read)
c764a876
DE
11978{
11979 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 11980
c764a876
DE
11981 *bytes_read = cu_header->offset_size;
11982 return offset;
11983}
11984
11985/* Read an offset from the data stream. */
11986
11987static LONGEST
11988read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
11989{
11990 LONGEST retval = 0;
11991
c764a876 11992 switch (offset_size)
613e1657
KB
11993 {
11994 case 4:
fe1b8b76 11995 retval = bfd_get_32 (abfd, buf);
613e1657
KB
11996 break;
11997 case 8:
fe1b8b76 11998 retval = bfd_get_64 (abfd, buf);
613e1657
KB
11999 break;
12000 default:
8e65ff28 12001 internal_error (__FILE__, __LINE__,
c764a876 12002 _("read_offset_1: bad switch [in module %s]"),
659b0389 12003 bfd_get_filename (abfd));
613e1657
KB
12004 }
12005
917c78fc 12006 return retval;
613e1657
KB
12007}
12008
fe1b8b76
JB
12009static gdb_byte *
12010read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
12011{
12012 /* If the size of a host char is 8 bits, we can return a pointer
12013 to the buffer, otherwise we have to copy the data to a buffer
12014 allocated on the temporary obstack. */
4bdf3d34 12015 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 12016 return buf;
c906108c
SS
12017}
12018
12019static char *
9b1c24c8 12020read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
12021{
12022 /* If the size of a host char is 8 bits, we can return a pointer
12023 to the string, otherwise we have to copy the string to a buffer
12024 allocated on the temporary obstack. */
4bdf3d34 12025 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
12026 if (*buf == '\0')
12027 {
12028 *bytes_read_ptr = 1;
12029 return NULL;
12030 }
fe1b8b76
JB
12031 *bytes_read_ptr = strlen ((char *) buf) + 1;
12032 return (char *) buf;
4bdf3d34
JJ
12033}
12034
12035static char *
cf2c3c16 12036read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 12037{
be391dca 12038 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 12039 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
12040 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12041 bfd_get_filename (abfd));
dce234bc 12042 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
12043 error (_("DW_FORM_strp pointing outside of "
12044 ".debug_str section [in module %s]"),
12045 bfd_get_filename (abfd));
4bdf3d34 12046 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 12047 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 12048 return NULL;
dce234bc 12049 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
12050}
12051
cf2c3c16
TT
12052static char *
12053read_indirect_string (bfd *abfd, gdb_byte *buf,
12054 const struct comp_unit_head *cu_header,
12055 unsigned int *bytes_read_ptr)
12056{
12057 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12058
12059 return read_indirect_string_at_offset (abfd, str_offset);
12060}
12061
12df843f 12062static ULONGEST
fe1b8b76 12063read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12064{
12df843f 12065 ULONGEST result;
ce5d95e1 12066 unsigned int num_read;
c906108c
SS
12067 int i, shift;
12068 unsigned char byte;
12069
12070 result = 0;
12071 shift = 0;
12072 num_read = 0;
12073 i = 0;
12074 while (1)
12075 {
fe1b8b76 12076 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12077 buf++;
12078 num_read++;
12df843f 12079 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
12080 if ((byte & 128) == 0)
12081 {
12082 break;
12083 }
12084 shift += 7;
12085 }
12086 *bytes_read_ptr = num_read;
12087 return result;
12088}
12089
12df843f 12090static LONGEST
fe1b8b76 12091read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12092{
12df843f 12093 LONGEST result;
77e0b926 12094 int i, shift, num_read;
c906108c
SS
12095 unsigned char byte;
12096
12097 result = 0;
12098 shift = 0;
c906108c
SS
12099 num_read = 0;
12100 i = 0;
12101 while (1)
12102 {
fe1b8b76 12103 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12104 buf++;
12105 num_read++;
12df843f 12106 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
12107 shift += 7;
12108 if ((byte & 128) == 0)
12109 {
12110 break;
12111 }
12112 }
77e0b926 12113 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 12114 result |= -(((LONGEST) 1) << shift);
c906108c
SS
12115 *bytes_read_ptr = num_read;
12116 return result;
12117}
12118
3019eac3
DE
12119/* Given index ADDR_INDEX in .debug_addr, fetch the value.
12120 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12121 ADDR_SIZE is the size of addresses from the CU header. */
12122
12123static CORE_ADDR
12124read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12125{
12126 struct objfile *objfile = dwarf2_per_objfile->objfile;
12127 bfd *abfd = objfile->obfd;
12128 const gdb_byte *info_ptr;
12129
12130 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12131 if (dwarf2_per_objfile->addr.buffer == NULL)
12132 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12133 objfile->name);
12134 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12135 error (_("DW_FORM_addr_index pointing outside of "
12136 ".debug_addr section [in module %s]"),
12137 objfile->name);
12138 info_ptr = (dwarf2_per_objfile->addr.buffer
12139 + addr_base + addr_index * addr_size);
12140 if (addr_size == 4)
12141 return bfd_get_32 (abfd, info_ptr);
12142 else
12143 return bfd_get_64 (abfd, info_ptr);
12144}
12145
12146/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12147
12148static CORE_ADDR
12149read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12150{
12151 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12152}
12153
12154/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12155
12156static CORE_ADDR
12157read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12158 unsigned int *bytes_read)
12159{
12160 bfd *abfd = cu->objfile->obfd;
12161 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12162
12163 return read_addr_index (cu, addr_index);
12164}
12165
12166/* Data structure to pass results from dwarf2_read_addr_index_reader
12167 back to dwarf2_read_addr_index. */
12168
12169struct dwarf2_read_addr_index_data
12170{
12171 ULONGEST addr_base;
12172 int addr_size;
12173};
12174
12175/* die_reader_func for dwarf2_read_addr_index. */
12176
12177static void
12178dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12179 gdb_byte *info_ptr,
12180 struct die_info *comp_unit_die,
12181 int has_children,
12182 void *data)
12183{
12184 struct dwarf2_cu *cu = reader->cu;
12185 struct dwarf2_read_addr_index_data *aidata =
12186 (struct dwarf2_read_addr_index_data *) data;
12187
12188 aidata->addr_base = cu->addr_base;
12189 aidata->addr_size = cu->header.addr_size;
12190}
12191
12192/* Given an index in .debug_addr, fetch the value.
12193 NOTE: This can be called during dwarf expression evaluation,
12194 long after the debug information has been read, and thus per_cu->cu
12195 may no longer exist. */
12196
12197CORE_ADDR
12198dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12199 unsigned int addr_index)
12200{
12201 struct objfile *objfile = per_cu->objfile;
12202 struct dwarf2_cu *cu = per_cu->cu;
12203 ULONGEST addr_base;
12204 int addr_size;
12205
12206 /* This is intended to be called from outside this file. */
12207 dw2_setup (objfile);
12208
12209 /* We need addr_base and addr_size.
12210 If we don't have PER_CU->cu, we have to get it.
12211 Nasty, but the alternative is storing the needed info in PER_CU,
12212 which at this point doesn't seem justified: it's not clear how frequently
12213 it would get used and it would increase the size of every PER_CU.
12214 Entry points like dwarf2_per_cu_addr_size do a similar thing
12215 so we're not in uncharted territory here.
12216 Alas we need to be a bit more complicated as addr_base is contained
12217 in the DIE.
12218
12219 We don't need to read the entire CU(/TU).
12220 We just need the header and top level die.
12221 IWBN to use the aging mechanism to let us lazily later discard the CU.
12222 See however init_cutu_and_read_dies_simple. */
12223
12224 if (cu != NULL)
12225 {
12226 addr_base = cu->addr_base;
12227 addr_size = cu->header.addr_size;
12228 }
12229 else
12230 {
12231 struct dwarf2_read_addr_index_data aidata;
12232
12233 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12234 &aidata);
12235 addr_base = aidata.addr_base;
12236 addr_size = aidata.addr_size;
12237 }
12238
12239 return read_addr_index_1 (addr_index, addr_base, addr_size);
12240}
12241
12242/* Given a DW_AT_str_index, fetch the string. */
12243
12244static char *
12245read_str_index (const struct die_reader_specs *reader,
12246 struct dwarf2_cu *cu, ULONGEST str_index)
12247{
12248 struct objfile *objfile = dwarf2_per_objfile->objfile;
12249 const char *dwo_name = objfile->name;
12250 bfd *abfd = objfile->obfd;
12251 struct dwo_sections *sections = &reader->dwo_file->sections;
12252 gdb_byte *info_ptr;
12253 ULONGEST str_offset;
12254
12255 dwarf2_read_section (objfile, &sections->str);
12256 dwarf2_read_section (objfile, &sections->str_offsets);
12257 if (sections->str.buffer == NULL)
12258 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12259 " in CU at offset 0x%lx [in module %s]"),
12260 (long) cu->header.offset.sect_off, dwo_name);
12261 if (sections->str_offsets.buffer == NULL)
12262 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12263 " in CU at offset 0x%lx [in module %s]"),
12264 (long) cu->header.offset.sect_off, dwo_name);
12265 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12266 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12267 " section in CU at offset 0x%lx [in module %s]"),
12268 (long) cu->header.offset.sect_off, dwo_name);
12269 info_ptr = (sections->str_offsets.buffer
12270 + str_index * cu->header.offset_size);
12271 if (cu->header.offset_size == 4)
12272 str_offset = bfd_get_32 (abfd, info_ptr);
12273 else
12274 str_offset = bfd_get_64 (abfd, info_ptr);
12275 if (str_offset >= sections->str.size)
12276 error (_("Offset from DW_FORM_str_index pointing outside of"
12277 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12278 (long) cu->header.offset.sect_off, dwo_name);
12279 return (char *) (sections->str.buffer + str_offset);
12280}
12281
3019eac3
DE
12282/* Return the length of an LEB128 number in BUF. */
12283
12284static int
12285leb128_size (const gdb_byte *buf)
12286{
12287 const gdb_byte *begin = buf;
12288 gdb_byte byte;
12289
12290 while (1)
12291 {
12292 byte = *buf++;
12293 if ((byte & 128) == 0)
12294 return buf - begin;
12295 }
12296}
12297
c906108c 12298static void
e142c38c 12299set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
12300{
12301 switch (lang)
12302 {
12303 case DW_LANG_C89:
76bee0cc 12304 case DW_LANG_C99:
c906108c 12305 case DW_LANG_C:
e142c38c 12306 cu->language = language_c;
c906108c
SS
12307 break;
12308 case DW_LANG_C_plus_plus:
e142c38c 12309 cu->language = language_cplus;
c906108c 12310 break;
6aecb9c2
JB
12311 case DW_LANG_D:
12312 cu->language = language_d;
12313 break;
c906108c
SS
12314 case DW_LANG_Fortran77:
12315 case DW_LANG_Fortran90:
b21b22e0 12316 case DW_LANG_Fortran95:
e142c38c 12317 cu->language = language_fortran;
c906108c 12318 break;
a766d390
DE
12319 case DW_LANG_Go:
12320 cu->language = language_go;
12321 break;
c906108c 12322 case DW_LANG_Mips_Assembler:
e142c38c 12323 cu->language = language_asm;
c906108c 12324 break;
bebd888e 12325 case DW_LANG_Java:
e142c38c 12326 cu->language = language_java;
bebd888e 12327 break;
c906108c 12328 case DW_LANG_Ada83:
8aaf0b47 12329 case DW_LANG_Ada95:
bc5f45f8
JB
12330 cu->language = language_ada;
12331 break;
72019c9c
GM
12332 case DW_LANG_Modula2:
12333 cu->language = language_m2;
12334 break;
fe8e67fd
PM
12335 case DW_LANG_Pascal83:
12336 cu->language = language_pascal;
12337 break;
22566fbd
DJ
12338 case DW_LANG_ObjC:
12339 cu->language = language_objc;
12340 break;
c906108c
SS
12341 case DW_LANG_Cobol74:
12342 case DW_LANG_Cobol85:
c906108c 12343 default:
e142c38c 12344 cu->language = language_minimal;
c906108c
SS
12345 break;
12346 }
e142c38c 12347 cu->language_defn = language_def (cu->language);
c906108c
SS
12348}
12349
12350/* Return the named attribute or NULL if not there. */
12351
12352static struct attribute *
e142c38c 12353dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 12354{
a48e046c 12355 for (;;)
c906108c 12356 {
a48e046c
TT
12357 unsigned int i;
12358 struct attribute *spec = NULL;
12359
12360 for (i = 0; i < die->num_attrs; ++i)
12361 {
12362 if (die->attrs[i].name == name)
12363 return &die->attrs[i];
12364 if (die->attrs[i].name == DW_AT_specification
12365 || die->attrs[i].name == DW_AT_abstract_origin)
12366 spec = &die->attrs[i];
12367 }
12368
12369 if (!spec)
12370 break;
c906108c 12371
f2f0e013 12372 die = follow_die_ref (die, spec, &cu);
f2f0e013 12373 }
c5aa993b 12374
c906108c
SS
12375 return NULL;
12376}
12377
348e048f
DE
12378/* Return the named attribute or NULL if not there,
12379 but do not follow DW_AT_specification, etc.
12380 This is for use in contexts where we're reading .debug_types dies.
12381 Following DW_AT_specification, DW_AT_abstract_origin will take us
12382 back up the chain, and we want to go down. */
12383
12384static struct attribute *
12385dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12386 struct dwarf2_cu *cu)
12387{
12388 unsigned int i;
12389
12390 for (i = 0; i < die->num_attrs; ++i)
12391 if (die->attrs[i].name == name)
12392 return &die->attrs[i];
12393
12394 return NULL;
12395}
12396
05cf31d1
JB
12397/* Return non-zero iff the attribute NAME is defined for the given DIE,
12398 and holds a non-zero value. This function should only be used for
2dc7f7b3 12399 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
12400
12401static int
12402dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12403{
12404 struct attribute *attr = dwarf2_attr (die, name, cu);
12405
12406 return (attr && DW_UNSND (attr));
12407}
12408
3ca72b44 12409static int
e142c38c 12410die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 12411{
05cf31d1
JB
12412 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12413 which value is non-zero. However, we have to be careful with
12414 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12415 (via dwarf2_flag_true_p) follows this attribute. So we may
12416 end up accidently finding a declaration attribute that belongs
12417 to a different DIE referenced by the specification attribute,
12418 even though the given DIE does not have a declaration attribute. */
12419 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12420 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
12421}
12422
63d06c5c 12423/* Return the die giving the specification for DIE, if there is
f2f0e013 12424 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
12425 containing the return value on output. If there is no
12426 specification, but there is an abstract origin, that is
12427 returned. */
63d06c5c
DC
12428
12429static struct die_info *
f2f0e013 12430die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 12431{
f2f0e013
DJ
12432 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12433 *spec_cu);
63d06c5c 12434
edb3359d
DJ
12435 if (spec_attr == NULL)
12436 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12437
63d06c5c
DC
12438 if (spec_attr == NULL)
12439 return NULL;
12440 else
f2f0e013 12441 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 12442}
c906108c 12443
debd256d 12444/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
12445 refers to.
12446 NOTE: This is also used as a "cleanup" function. */
12447
debd256d
JB
12448static void
12449free_line_header (struct line_header *lh)
12450{
12451 if (lh->standard_opcode_lengths)
a8bc7b56 12452 xfree (lh->standard_opcode_lengths);
debd256d
JB
12453
12454 /* Remember that all the lh->file_names[i].name pointers are
12455 pointers into debug_line_buffer, and don't need to be freed. */
12456 if (lh->file_names)
a8bc7b56 12457 xfree (lh->file_names);
debd256d
JB
12458
12459 /* Similarly for the include directory names. */
12460 if (lh->include_dirs)
a8bc7b56 12461 xfree (lh->include_dirs);
debd256d 12462
a8bc7b56 12463 xfree (lh);
debd256d
JB
12464}
12465
debd256d 12466/* Add an entry to LH's include directory table. */
ae2de4f8 12467
debd256d
JB
12468static void
12469add_include_dir (struct line_header *lh, char *include_dir)
c906108c 12470{
debd256d
JB
12471 /* Grow the array if necessary. */
12472 if (lh->include_dirs_size == 0)
c5aa993b 12473 {
debd256d
JB
12474 lh->include_dirs_size = 1; /* for testing */
12475 lh->include_dirs = xmalloc (lh->include_dirs_size
12476 * sizeof (*lh->include_dirs));
12477 }
12478 else if (lh->num_include_dirs >= lh->include_dirs_size)
12479 {
12480 lh->include_dirs_size *= 2;
12481 lh->include_dirs = xrealloc (lh->include_dirs,
12482 (lh->include_dirs_size
12483 * sizeof (*lh->include_dirs)));
c5aa993b 12484 }
c906108c 12485
debd256d
JB
12486 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12487}
6e70227d 12488
debd256d 12489/* Add an entry to LH's file name table. */
ae2de4f8 12490
debd256d
JB
12491static void
12492add_file_name (struct line_header *lh,
12493 char *name,
12494 unsigned int dir_index,
12495 unsigned int mod_time,
12496 unsigned int length)
12497{
12498 struct file_entry *fe;
12499
12500 /* Grow the array if necessary. */
12501 if (lh->file_names_size == 0)
12502 {
12503 lh->file_names_size = 1; /* for testing */
12504 lh->file_names = xmalloc (lh->file_names_size
12505 * sizeof (*lh->file_names));
12506 }
12507 else if (lh->num_file_names >= lh->file_names_size)
12508 {
12509 lh->file_names_size *= 2;
12510 lh->file_names = xrealloc (lh->file_names,
12511 (lh->file_names_size
12512 * sizeof (*lh->file_names)));
12513 }
12514
12515 fe = &lh->file_names[lh->num_file_names++];
12516 fe->name = name;
12517 fe->dir_index = dir_index;
12518 fe->mod_time = mod_time;
12519 fe->length = length;
aaa75496 12520 fe->included_p = 0;
cb1df416 12521 fe->symtab = NULL;
debd256d 12522}
6e70227d 12523
debd256d 12524/* Read the statement program header starting at OFFSET in
3019eac3 12525 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 12526 to a struct line_header, allocated using xmalloc.
debd256d
JB
12527
12528 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
12529 the returned object point into the dwarf line section buffer,
12530 and must not be freed. */
ae2de4f8 12531
debd256d 12532static struct line_header *
3019eac3 12533dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
12534{
12535 struct cleanup *back_to;
12536 struct line_header *lh;
fe1b8b76 12537 gdb_byte *line_ptr;
c764a876 12538 unsigned int bytes_read, offset_size;
debd256d
JB
12539 int i;
12540 char *cur_dir, *cur_file;
3019eac3
DE
12541 struct dwarf2_section_info *section;
12542 bfd *abfd;
12543
12544 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12545 DWO file. */
12546 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12547 section = &cu->dwo_unit->dwo_file->sections.line;
12548 else
12549 section = &dwarf2_per_objfile->line;
debd256d 12550
3019eac3
DE
12551 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12552 if (section->buffer == NULL)
debd256d 12553 {
3019eac3
DE
12554 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12555 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12556 else
12557 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
12558 return 0;
12559 }
12560
fceca515
DE
12561 /* We can't do this until we know the section is non-empty.
12562 Only then do we know we have such a section. */
12563 abfd = section->asection->owner;
12564
a738430d
MK
12565 /* Make sure that at least there's room for the total_length field.
12566 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 12567 if (offset + 4 >= section->size)
debd256d 12568 {
4d3c2250 12569 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12570 return 0;
12571 }
12572
12573 lh = xmalloc (sizeof (*lh));
12574 memset (lh, 0, sizeof (*lh));
12575 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12576 (void *) lh);
12577
3019eac3 12578 line_ptr = section->buffer + offset;
debd256d 12579
a738430d 12580 /* Read in the header. */
6e70227d 12581 lh->total_length =
c764a876
DE
12582 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12583 &bytes_read, &offset_size);
debd256d 12584 line_ptr += bytes_read;
3019eac3 12585 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 12586 {
4d3c2250 12587 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12588 return 0;
12589 }
12590 lh->statement_program_end = line_ptr + lh->total_length;
12591 lh->version = read_2_bytes (abfd, line_ptr);
12592 line_ptr += 2;
c764a876
DE
12593 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12594 line_ptr += offset_size;
debd256d
JB
12595 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12596 line_ptr += 1;
2dc7f7b3
TT
12597 if (lh->version >= 4)
12598 {
12599 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12600 line_ptr += 1;
12601 }
12602 else
12603 lh->maximum_ops_per_instruction = 1;
12604
12605 if (lh->maximum_ops_per_instruction == 0)
12606 {
12607 lh->maximum_ops_per_instruction = 1;
12608 complaint (&symfile_complaints,
3e43a32a
MS
12609 _("invalid maximum_ops_per_instruction "
12610 "in `.debug_line' section"));
2dc7f7b3
TT
12611 }
12612
debd256d
JB
12613 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12614 line_ptr += 1;
12615 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12616 line_ptr += 1;
12617 lh->line_range = read_1_byte (abfd, line_ptr);
12618 line_ptr += 1;
12619 lh->opcode_base = read_1_byte (abfd, line_ptr);
12620 line_ptr += 1;
12621 lh->standard_opcode_lengths
fe1b8b76 12622 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
12623
12624 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12625 for (i = 1; i < lh->opcode_base; ++i)
12626 {
12627 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12628 line_ptr += 1;
12629 }
12630
a738430d 12631 /* Read directory table. */
9b1c24c8 12632 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12633 {
12634 line_ptr += bytes_read;
12635 add_include_dir (lh, cur_dir);
12636 }
12637 line_ptr += bytes_read;
12638
a738430d 12639 /* Read file name table. */
9b1c24c8 12640 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12641 {
12642 unsigned int dir_index, mod_time, length;
12643
12644 line_ptr += bytes_read;
12645 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12646 line_ptr += bytes_read;
12647 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12648 line_ptr += bytes_read;
12649 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12650 line_ptr += bytes_read;
12651
12652 add_file_name (lh, cur_file, dir_index, mod_time, length);
12653 }
12654 line_ptr += bytes_read;
6e70227d 12655 lh->statement_program_start = line_ptr;
debd256d 12656
3019eac3 12657 if (line_ptr > (section->buffer + section->size))
4d3c2250 12658 complaint (&symfile_complaints,
3e43a32a
MS
12659 _("line number info header doesn't "
12660 "fit in `.debug_line' section"));
debd256d
JB
12661
12662 discard_cleanups (back_to);
12663 return lh;
12664}
c906108c 12665
c6da4cef
DE
12666/* Subroutine of dwarf_decode_lines to simplify it.
12667 Return the file name of the psymtab for included file FILE_INDEX
12668 in line header LH of PST.
12669 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12670 If space for the result is malloc'd, it will be freed by a cleanup.
12671 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12672
12673static char *
12674psymtab_include_file_name (const struct line_header *lh, int file_index,
12675 const struct partial_symtab *pst,
12676 const char *comp_dir)
12677{
12678 const struct file_entry fe = lh->file_names [file_index];
12679 char *include_name = fe.name;
12680 char *include_name_to_compare = include_name;
12681 char *dir_name = NULL;
72b9f47f
TT
12682 const char *pst_filename;
12683 char *copied_name = NULL;
c6da4cef
DE
12684 int file_is_pst;
12685
12686 if (fe.dir_index)
12687 dir_name = lh->include_dirs[fe.dir_index - 1];
12688
12689 if (!IS_ABSOLUTE_PATH (include_name)
12690 && (dir_name != NULL || comp_dir != NULL))
12691 {
12692 /* Avoid creating a duplicate psymtab for PST.
12693 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12694 Before we do the comparison, however, we need to account
12695 for DIR_NAME and COMP_DIR.
12696 First prepend dir_name (if non-NULL). If we still don't
12697 have an absolute path prepend comp_dir (if non-NULL).
12698 However, the directory we record in the include-file's
12699 psymtab does not contain COMP_DIR (to match the
12700 corresponding symtab(s)).
12701
12702 Example:
12703
12704 bash$ cd /tmp
12705 bash$ gcc -g ./hello.c
12706 include_name = "hello.c"
12707 dir_name = "."
12708 DW_AT_comp_dir = comp_dir = "/tmp"
12709 DW_AT_name = "./hello.c" */
12710
12711 if (dir_name != NULL)
12712 {
12713 include_name = concat (dir_name, SLASH_STRING,
12714 include_name, (char *)NULL);
12715 include_name_to_compare = include_name;
12716 make_cleanup (xfree, include_name);
12717 }
12718 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12719 {
12720 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12721 include_name, (char *)NULL);
12722 }
12723 }
12724
12725 pst_filename = pst->filename;
12726 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12727 {
72b9f47f
TT
12728 copied_name = concat (pst->dirname, SLASH_STRING,
12729 pst_filename, (char *)NULL);
12730 pst_filename = copied_name;
c6da4cef
DE
12731 }
12732
1e3fad37 12733 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
12734
12735 if (include_name_to_compare != include_name)
12736 xfree (include_name_to_compare);
72b9f47f
TT
12737 if (copied_name != NULL)
12738 xfree (copied_name);
c6da4cef
DE
12739
12740 if (file_is_pst)
12741 return NULL;
12742 return include_name;
12743}
12744
c91513d8
PP
12745/* Ignore this record_line request. */
12746
12747static void
12748noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12749{
12750 return;
12751}
12752
f3f5162e
DE
12753/* Subroutine of dwarf_decode_lines to simplify it.
12754 Process the line number information in LH. */
debd256d 12755
c906108c 12756static void
f3f5162e
DE
12757dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
12758 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 12759{
a8c50c1f 12760 gdb_byte *line_ptr, *extended_end;
fe1b8b76 12761 gdb_byte *line_end;
a8c50c1f 12762 unsigned int bytes_read, extended_len;
c906108c 12763 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
12764 CORE_ADDR baseaddr;
12765 struct objfile *objfile = cu->objfile;
f3f5162e 12766 bfd *abfd = objfile->obfd;
fbf65064 12767 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 12768 const int decode_for_pst_p = (pst != NULL);
f3f5162e 12769 struct subfile *last_subfile = NULL;
c91513d8
PP
12770 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
12771 = record_line;
e142c38c
DJ
12772
12773 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 12774
debd256d
JB
12775 line_ptr = lh->statement_program_start;
12776 line_end = lh->statement_program_end;
c906108c
SS
12777
12778 /* Read the statement sequences until there's nothing left. */
12779 while (line_ptr < line_end)
12780 {
12781 /* state machine registers */
12782 CORE_ADDR address = 0;
12783 unsigned int file = 1;
12784 unsigned int line = 1;
12785 unsigned int column = 0;
debd256d 12786 int is_stmt = lh->default_is_stmt;
c906108c
SS
12787 int basic_block = 0;
12788 int end_sequence = 0;
fbf65064 12789 CORE_ADDR addr;
2dc7f7b3 12790 unsigned char op_index = 0;
c906108c 12791
aaa75496 12792 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 12793 {
aaa75496 12794 /* Start a subfile for the current file of the state machine. */
debd256d
JB
12795 /* lh->include_dirs and lh->file_names are 0-based, but the
12796 directory and file name numbers in the statement program
12797 are 1-based. */
12798 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 12799 char *dir = NULL;
a738430d 12800
debd256d
JB
12801 if (fe->dir_index)
12802 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
12803
12804 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
12805 }
12806
a738430d 12807 /* Decode the table. */
c5aa993b 12808 while (!end_sequence)
c906108c
SS
12809 {
12810 op_code = read_1_byte (abfd, line_ptr);
12811 line_ptr += 1;
59205f5a
JB
12812 if (line_ptr > line_end)
12813 {
12814 dwarf2_debug_line_missing_end_sequence_complaint ();
12815 break;
12816 }
9aa1fe7e 12817
debd256d 12818 if (op_code >= lh->opcode_base)
6e70227d 12819 {
a738430d 12820 /* Special operand. */
debd256d 12821 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
12822 address += (((op_index + (adj_opcode / lh->line_range))
12823 / lh->maximum_ops_per_instruction)
12824 * lh->minimum_instruction_length);
12825 op_index = ((op_index + (adj_opcode / lh->line_range))
12826 % lh->maximum_ops_per_instruction);
debd256d 12827 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 12828 if (lh->num_file_names < file || file == 0)
25e43795 12829 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
12830 /* For now we ignore lines not starting on an
12831 instruction boundary. */
12832 else if (op_index == 0)
25e43795
DJ
12833 {
12834 lh->file_names[file - 1].included_p = 1;
ca5f395d 12835 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
12836 {
12837 if (last_subfile != current_subfile)
12838 {
12839 addr = gdbarch_addr_bits_remove (gdbarch, address);
12840 if (last_subfile)
c91513d8 12841 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
12842 last_subfile = current_subfile;
12843 }
25e43795 12844 /* Append row to matrix using current values. */
7019d805 12845 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 12846 (*p_record_line) (current_subfile, line, addr);
366da635 12847 }
25e43795 12848 }
ca5f395d 12849 basic_block = 0;
9aa1fe7e
GK
12850 }
12851 else switch (op_code)
c906108c
SS
12852 {
12853 case DW_LNS_extended_op:
3e43a32a
MS
12854 extended_len = read_unsigned_leb128 (abfd, line_ptr,
12855 &bytes_read);
473b7be6 12856 line_ptr += bytes_read;
a8c50c1f 12857 extended_end = line_ptr + extended_len;
c906108c
SS
12858 extended_op = read_1_byte (abfd, line_ptr);
12859 line_ptr += 1;
12860 switch (extended_op)
12861 {
12862 case DW_LNE_end_sequence:
c91513d8 12863 p_record_line = record_line;
c906108c 12864 end_sequence = 1;
c906108c
SS
12865 break;
12866 case DW_LNE_set_address:
e7c27a73 12867 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
12868
12869 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
12870 {
12871 /* This line table is for a function which has been
12872 GCd by the linker. Ignore it. PR gdb/12528 */
12873
12874 long line_offset
12875 = line_ptr - dwarf2_per_objfile->line.buffer;
12876
12877 complaint (&symfile_complaints,
12878 _(".debug_line address at offset 0x%lx is 0 "
12879 "[in module %s]"),
bb5ed363 12880 line_offset, objfile->name);
c91513d8
PP
12881 p_record_line = noop_record_line;
12882 }
12883
2dc7f7b3 12884 op_index = 0;
107d2387
AC
12885 line_ptr += bytes_read;
12886 address += baseaddr;
c906108c
SS
12887 break;
12888 case DW_LNE_define_file:
debd256d
JB
12889 {
12890 char *cur_file;
12891 unsigned int dir_index, mod_time, length;
6e70227d 12892
3e43a32a
MS
12893 cur_file = read_direct_string (abfd, line_ptr,
12894 &bytes_read);
debd256d
JB
12895 line_ptr += bytes_read;
12896 dir_index =
12897 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12898 line_ptr += bytes_read;
12899 mod_time =
12900 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12901 line_ptr += bytes_read;
12902 length =
12903 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12904 line_ptr += bytes_read;
12905 add_file_name (lh, cur_file, dir_index, mod_time, length);
12906 }
c906108c 12907 break;
d0c6ba3d
CC
12908 case DW_LNE_set_discriminator:
12909 /* The discriminator is not interesting to the debugger;
12910 just ignore it. */
12911 line_ptr = extended_end;
12912 break;
c906108c 12913 default:
4d3c2250 12914 complaint (&symfile_complaints,
e2e0b3e5 12915 _("mangled .debug_line section"));
debd256d 12916 return;
c906108c 12917 }
a8c50c1f
DJ
12918 /* Make sure that we parsed the extended op correctly. If e.g.
12919 we expected a different address size than the producer used,
12920 we may have read the wrong number of bytes. */
12921 if (line_ptr != extended_end)
12922 {
12923 complaint (&symfile_complaints,
12924 _("mangled .debug_line section"));
12925 return;
12926 }
c906108c
SS
12927 break;
12928 case DW_LNS_copy:
59205f5a 12929 if (lh->num_file_names < file || file == 0)
25e43795
DJ
12930 dwarf2_debug_line_missing_file_complaint ();
12931 else
366da635 12932 {
25e43795 12933 lh->file_names[file - 1].included_p = 1;
ca5f395d 12934 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
12935 {
12936 if (last_subfile != current_subfile)
12937 {
12938 addr = gdbarch_addr_bits_remove (gdbarch, address);
12939 if (last_subfile)
c91513d8 12940 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
12941 last_subfile = current_subfile;
12942 }
7019d805 12943 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 12944 (*p_record_line) (current_subfile, line, addr);
fbf65064 12945 }
366da635 12946 }
c906108c
SS
12947 basic_block = 0;
12948 break;
12949 case DW_LNS_advance_pc:
2dc7f7b3
TT
12950 {
12951 CORE_ADDR adjust
12952 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12953
12954 address += (((op_index + adjust)
12955 / lh->maximum_ops_per_instruction)
12956 * lh->minimum_instruction_length);
12957 op_index = ((op_index + adjust)
12958 % lh->maximum_ops_per_instruction);
12959 line_ptr += bytes_read;
12960 }
c906108c
SS
12961 break;
12962 case DW_LNS_advance_line:
12963 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
12964 line_ptr += bytes_read;
12965 break;
12966 case DW_LNS_set_file:
debd256d 12967 {
a738430d
MK
12968 /* The arrays lh->include_dirs and lh->file_names are
12969 0-based, but the directory and file name numbers in
12970 the statement program are 1-based. */
debd256d 12971 struct file_entry *fe;
4f1520fb 12972 char *dir = NULL;
a738430d 12973
debd256d
JB
12974 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12975 line_ptr += bytes_read;
59205f5a 12976 if (lh->num_file_names < file || file == 0)
25e43795
DJ
12977 dwarf2_debug_line_missing_file_complaint ();
12978 else
12979 {
12980 fe = &lh->file_names[file - 1];
12981 if (fe->dir_index)
12982 dir = lh->include_dirs[fe->dir_index - 1];
12983 if (!decode_for_pst_p)
12984 {
12985 last_subfile = current_subfile;
12986 dwarf2_start_subfile (fe->name, dir, comp_dir);
12987 }
12988 }
debd256d 12989 }
c906108c
SS
12990 break;
12991 case DW_LNS_set_column:
12992 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12993 line_ptr += bytes_read;
12994 break;
12995 case DW_LNS_negate_stmt:
12996 is_stmt = (!is_stmt);
12997 break;
12998 case DW_LNS_set_basic_block:
12999 basic_block = 1;
13000 break;
c2c6d25f
JM
13001 /* Add to the address register of the state machine the
13002 address increment value corresponding to special opcode
a738430d
MK
13003 255. I.e., this value is scaled by the minimum
13004 instruction length since special opcode 255 would have
b021a221 13005 scaled the increment. */
c906108c 13006 case DW_LNS_const_add_pc:
2dc7f7b3
TT
13007 {
13008 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13009
13010 address += (((op_index + adjust)
13011 / lh->maximum_ops_per_instruction)
13012 * lh->minimum_instruction_length);
13013 op_index = ((op_index + adjust)
13014 % lh->maximum_ops_per_instruction);
13015 }
c906108c
SS
13016 break;
13017 case DW_LNS_fixed_advance_pc:
13018 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 13019 op_index = 0;
c906108c
SS
13020 line_ptr += 2;
13021 break;
9aa1fe7e 13022 default:
a738430d
MK
13023 {
13024 /* Unknown standard opcode, ignore it. */
9aa1fe7e 13025 int i;
a738430d 13026
debd256d 13027 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
13028 {
13029 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13030 line_ptr += bytes_read;
13031 }
13032 }
c906108c
SS
13033 }
13034 }
59205f5a
JB
13035 if (lh->num_file_names < file || file == 0)
13036 dwarf2_debug_line_missing_file_complaint ();
13037 else
13038 {
13039 lh->file_names[file - 1].included_p = 1;
13040 if (!decode_for_pst_p)
fbf65064
UW
13041 {
13042 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13043 (*p_record_line) (current_subfile, 0, addr);
fbf65064 13044 }
59205f5a 13045 }
c906108c 13046 }
f3f5162e
DE
13047}
13048
13049/* Decode the Line Number Program (LNP) for the given line_header
13050 structure and CU. The actual information extracted and the type
13051 of structures created from the LNP depends on the value of PST.
13052
13053 1. If PST is NULL, then this procedure uses the data from the program
13054 to create all necessary symbol tables, and their linetables.
13055
13056 2. If PST is not NULL, this procedure reads the program to determine
13057 the list of files included by the unit represented by PST, and
13058 builds all the associated partial symbol tables.
13059
13060 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13061 It is used for relative paths in the line table.
13062 NOTE: When processing partial symtabs (pst != NULL),
13063 comp_dir == pst->dirname.
13064
13065 NOTE: It is important that psymtabs have the same file name (via strcmp)
13066 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13067 symtab we don't use it in the name of the psymtabs we create.
13068 E.g. expand_line_sal requires this when finding psymtabs to expand.
13069 A good testcase for this is mb-inline.exp. */
13070
13071static void
13072dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13073 struct dwarf2_cu *cu, struct partial_symtab *pst,
13074 int want_line_info)
13075{
13076 struct objfile *objfile = cu->objfile;
13077 const int decode_for_pst_p = (pst != NULL);
13078 struct subfile *first_subfile = current_subfile;
13079
13080 if (want_line_info)
13081 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
13082
13083 if (decode_for_pst_p)
13084 {
13085 int file_index;
13086
13087 /* Now that we're done scanning the Line Header Program, we can
13088 create the psymtab of each included file. */
13089 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13090 if (lh->file_names[file_index].included_p == 1)
13091 {
c6da4cef
DE
13092 char *include_name =
13093 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13094 if (include_name != NULL)
aaa75496
JB
13095 dwarf2_create_include_psymtab (include_name, pst, objfile);
13096 }
13097 }
cb1df416
DJ
13098 else
13099 {
13100 /* Make sure a symtab is created for every file, even files
13101 which contain only variables (i.e. no code with associated
13102 line numbers). */
cb1df416 13103 int i;
cb1df416
DJ
13104
13105 for (i = 0; i < lh->num_file_names; i++)
13106 {
13107 char *dir = NULL;
f3f5162e 13108 struct file_entry *fe;
9a619af0 13109
cb1df416
DJ
13110 fe = &lh->file_names[i];
13111 if (fe->dir_index)
13112 dir = lh->include_dirs[fe->dir_index - 1];
13113 dwarf2_start_subfile (fe->name, dir, comp_dir);
13114
13115 /* Skip the main file; we don't need it, and it must be
13116 allocated last, so that it will show up before the
13117 non-primary symtabs in the objfile's symtab list. */
13118 if (current_subfile == first_subfile)
13119 continue;
13120
13121 if (current_subfile->symtab == NULL)
13122 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 13123 objfile);
cb1df416
DJ
13124 fe->symtab = current_subfile->symtab;
13125 }
13126 }
c906108c
SS
13127}
13128
13129/* Start a subfile for DWARF. FILENAME is the name of the file and
13130 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
13131 or NULL if not known. COMP_DIR is the compilation directory for the
13132 linetable's compilation unit or NULL if not known.
c906108c
SS
13133 This routine tries to keep line numbers from identical absolute and
13134 relative file names in a common subfile.
13135
13136 Using the `list' example from the GDB testsuite, which resides in
13137 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13138 of /srcdir/list0.c yields the following debugging information for list0.c:
13139
c5aa993b
JM
13140 DW_AT_name: /srcdir/list0.c
13141 DW_AT_comp_dir: /compdir
357e46e7 13142 files.files[0].name: list0.h
c5aa993b 13143 files.files[0].dir: /srcdir
357e46e7 13144 files.files[1].name: list0.c
c5aa993b 13145 files.files[1].dir: /srcdir
c906108c
SS
13146
13147 The line number information for list0.c has to end up in a single
4f1520fb
FR
13148 subfile, so that `break /srcdir/list0.c:1' works as expected.
13149 start_subfile will ensure that this happens provided that we pass the
13150 concatenation of files.files[1].dir and files.files[1].name as the
13151 subfile's name. */
c906108c
SS
13152
13153static void
3e43a32a
MS
13154dwarf2_start_subfile (char *filename, const char *dirname,
13155 const char *comp_dir)
c906108c 13156{
4f1520fb
FR
13157 char *fullname;
13158
13159 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13160 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13161 second argument to start_subfile. To be consistent, we do the
13162 same here. In order not to lose the line information directory,
13163 we concatenate it to the filename when it makes sense.
13164 Note that the Dwarf3 standard says (speaking of filenames in line
13165 information): ``The directory index is ignored for file names
13166 that represent full path names''. Thus ignoring dirname in the
13167 `else' branch below isn't an issue. */
c906108c 13168
d5166ae1 13169 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
13170 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13171 else
13172 fullname = filename;
c906108c 13173
4f1520fb
FR
13174 start_subfile (fullname, comp_dir);
13175
13176 if (fullname != filename)
13177 xfree (fullname);
c906108c
SS
13178}
13179
4c2df51b
DJ
13180static void
13181var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 13182 struct dwarf2_cu *cu)
4c2df51b 13183{
e7c27a73
DJ
13184 struct objfile *objfile = cu->objfile;
13185 struct comp_unit_head *cu_header = &cu->header;
13186
4c2df51b
DJ
13187 /* NOTE drow/2003-01-30: There used to be a comment and some special
13188 code here to turn a symbol with DW_AT_external and a
13189 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13190 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13191 with some versions of binutils) where shared libraries could have
13192 relocations against symbols in their debug information - the
13193 minimal symbol would have the right address, but the debug info
13194 would not. It's no longer necessary, because we will explicitly
13195 apply relocations when we read in the debug information now. */
13196
13197 /* A DW_AT_location attribute with no contents indicates that a
13198 variable has been optimized away. */
13199 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13200 {
13201 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13202 return;
13203 }
13204
13205 /* Handle one degenerate form of location expression specially, to
13206 preserve GDB's previous behavior when section offsets are
3019eac3
DE
13207 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13208 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
13209
13210 if (attr_form_is_block (attr)
3019eac3
DE
13211 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13212 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13213 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13214 && (DW_BLOCK (attr)->size
13215 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 13216 {
891d2f0b 13217 unsigned int dummy;
4c2df51b 13218
3019eac3
DE
13219 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13220 SYMBOL_VALUE_ADDRESS (sym) =
13221 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13222 else
13223 SYMBOL_VALUE_ADDRESS (sym) =
13224 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 13225 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
13226 fixup_symbol_section (sym, objfile);
13227 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13228 SYMBOL_SECTION (sym));
4c2df51b
DJ
13229 return;
13230 }
13231
13232 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13233 expression evaluator, and use LOC_COMPUTED only when necessary
13234 (i.e. when the value of a register or memory location is
13235 referenced, or a thread-local block, etc.). Then again, it might
13236 not be worthwhile. I'm assuming that it isn't unless performance
13237 or memory numbers show me otherwise. */
13238
e7c27a73 13239 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 13240 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
13241
13242 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13243 cu->has_loclist = 1;
4c2df51b
DJ
13244}
13245
c906108c
SS
13246/* Given a pointer to a DWARF information entry, figure out if we need
13247 to make a symbol table entry for it, and if so, create a new entry
13248 and return a pointer to it.
13249 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
13250 used the passed type.
13251 If SPACE is not NULL, use it to hold the new symbol. If it is
13252 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
13253
13254static struct symbol *
34eaf542
TT
13255new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13256 struct symbol *space)
c906108c 13257{
e7c27a73 13258 struct objfile *objfile = cu->objfile;
c906108c
SS
13259 struct symbol *sym = NULL;
13260 char *name;
13261 struct attribute *attr = NULL;
13262 struct attribute *attr2 = NULL;
e142c38c 13263 CORE_ADDR baseaddr;
e37fd15a
SW
13264 struct pending **list_to_add = NULL;
13265
edb3359d 13266 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
13267
13268 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 13269
94af9270 13270 name = dwarf2_name (die, cu);
c906108c
SS
13271 if (name)
13272 {
94af9270 13273 const char *linkagename;
34eaf542 13274 int suppress_add = 0;
94af9270 13275
34eaf542
TT
13276 if (space)
13277 sym = space;
13278 else
13279 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 13280 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
13281
13282 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 13283 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
13284 linkagename = dwarf2_physname (name, die, cu);
13285 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 13286
f55ee35c
JK
13287 /* Fortran does not have mangling standard and the mangling does differ
13288 between gfortran, iFort etc. */
13289 if (cu->language == language_fortran
b250c185 13290 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
13291 symbol_set_demangled_name (&(sym->ginfo),
13292 (char *) dwarf2_full_name (name, die, cu),
13293 NULL);
f55ee35c 13294
c906108c 13295 /* Default assumptions.
c5aa993b 13296 Use the passed type or decode it from the die. */
176620f1 13297 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 13298 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
13299 if (type != NULL)
13300 SYMBOL_TYPE (sym) = type;
13301 else
e7c27a73 13302 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
13303 attr = dwarf2_attr (die,
13304 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13305 cu);
c906108c
SS
13306 if (attr)
13307 {
13308 SYMBOL_LINE (sym) = DW_UNSND (attr);
13309 }
cb1df416 13310
edb3359d
DJ
13311 attr = dwarf2_attr (die,
13312 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13313 cu);
cb1df416
DJ
13314 if (attr)
13315 {
13316 int file_index = DW_UNSND (attr);
9a619af0 13317
cb1df416
DJ
13318 if (cu->line_header == NULL
13319 || file_index > cu->line_header->num_file_names)
13320 complaint (&symfile_complaints,
13321 _("file index out of range"));
1c3d648d 13322 else if (file_index > 0)
cb1df416
DJ
13323 {
13324 struct file_entry *fe;
9a619af0 13325
cb1df416
DJ
13326 fe = &cu->line_header->file_names[file_index - 1];
13327 SYMBOL_SYMTAB (sym) = fe->symtab;
13328 }
13329 }
13330
c906108c
SS
13331 switch (die->tag)
13332 {
13333 case DW_TAG_label:
e142c38c 13334 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
13335 if (attr)
13336 {
13337 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13338 }
0f5238ed
TT
13339 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13340 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 13341 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 13342 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
13343 break;
13344 case DW_TAG_subprogram:
13345 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13346 finish_block. */
13347 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 13348 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
13349 if ((attr2 && (DW_UNSND (attr2) != 0))
13350 || cu->language == language_ada)
c906108c 13351 {
2cfa0c8d
JB
13352 /* Subprograms marked external are stored as a global symbol.
13353 Ada subprograms, whether marked external or not, are always
13354 stored as a global symbol, because we want to be able to
13355 access them globally. For instance, we want to be able
13356 to break on a nested subprogram without having to
13357 specify the context. */
e37fd15a 13358 list_to_add = &global_symbols;
c906108c
SS
13359 }
13360 else
13361 {
e37fd15a 13362 list_to_add = cu->list_in_scope;
c906108c
SS
13363 }
13364 break;
edb3359d
DJ
13365 case DW_TAG_inlined_subroutine:
13366 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13367 finish_block. */
13368 SYMBOL_CLASS (sym) = LOC_BLOCK;
13369 SYMBOL_INLINED (sym) = 1;
481860b3 13370 list_to_add = cu->list_in_scope;
edb3359d 13371 break;
34eaf542
TT
13372 case DW_TAG_template_value_param:
13373 suppress_add = 1;
13374 /* Fall through. */
72929c62 13375 case DW_TAG_constant:
c906108c 13376 case DW_TAG_variable:
254e6b9e 13377 case DW_TAG_member:
0963b4bd
MS
13378 /* Compilation with minimal debug info may result in
13379 variables with missing type entries. Change the
13380 misleading `void' type to something sensible. */
c906108c 13381 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 13382 SYMBOL_TYPE (sym)
46bf5051 13383 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 13384
e142c38c 13385 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
13386 /* In the case of DW_TAG_member, we should only be called for
13387 static const members. */
13388 if (die->tag == DW_TAG_member)
13389 {
3863f96c
DE
13390 /* dwarf2_add_field uses die_is_declaration,
13391 so we do the same. */
254e6b9e
DE
13392 gdb_assert (die_is_declaration (die, cu));
13393 gdb_assert (attr);
13394 }
c906108c
SS
13395 if (attr)
13396 {
e7c27a73 13397 dwarf2_const_value (attr, sym, cu);
e142c38c 13398 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 13399 if (!suppress_add)
34eaf542
TT
13400 {
13401 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 13402 list_to_add = &global_symbols;
34eaf542 13403 else
e37fd15a 13404 list_to_add = cu->list_in_scope;
34eaf542 13405 }
c906108c
SS
13406 break;
13407 }
e142c38c 13408 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13409 if (attr)
13410 {
e7c27a73 13411 var_decode_location (attr, sym, cu);
e142c38c 13412 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
13413 if (SYMBOL_CLASS (sym) == LOC_STATIC
13414 && SYMBOL_VALUE_ADDRESS (sym) == 0
13415 && !dwarf2_per_objfile->has_section_at_zero)
13416 {
13417 /* When a static variable is eliminated by the linker,
13418 the corresponding debug information is not stripped
13419 out, but the variable address is set to null;
13420 do not add such variables into symbol table. */
13421 }
13422 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 13423 {
f55ee35c
JK
13424 /* Workaround gfortran PR debug/40040 - it uses
13425 DW_AT_location for variables in -fPIC libraries which may
13426 get overriden by other libraries/executable and get
13427 a different address. Resolve it by the minimal symbol
13428 which may come from inferior's executable using copy
13429 relocation. Make this workaround only for gfortran as for
13430 other compilers GDB cannot guess the minimal symbol
13431 Fortran mangling kind. */
13432 if (cu->language == language_fortran && die->parent
13433 && die->parent->tag == DW_TAG_module
13434 && cu->producer
13435 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13436 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13437
1c809c68
TT
13438 /* A variable with DW_AT_external is never static,
13439 but it may be block-scoped. */
13440 list_to_add = (cu->list_in_scope == &file_symbols
13441 ? &global_symbols : cu->list_in_scope);
1c809c68 13442 }
c906108c 13443 else
e37fd15a 13444 list_to_add = cu->list_in_scope;
c906108c
SS
13445 }
13446 else
13447 {
13448 /* We do not know the address of this symbol.
c5aa993b
JM
13449 If it is an external symbol and we have type information
13450 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13451 The address of the variable will then be determined from
13452 the minimal symbol table whenever the variable is
13453 referenced. */
e142c38c 13454 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 13455 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 13456 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 13457 {
0fe7935b
DJ
13458 /* A variable with DW_AT_external is never static, but it
13459 may be block-scoped. */
13460 list_to_add = (cu->list_in_scope == &file_symbols
13461 ? &global_symbols : cu->list_in_scope);
13462
c906108c 13463 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 13464 }
442ddf59
JK
13465 else if (!die_is_declaration (die, cu))
13466 {
13467 /* Use the default LOC_OPTIMIZED_OUT class. */
13468 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
13469 if (!suppress_add)
13470 list_to_add = cu->list_in_scope;
442ddf59 13471 }
c906108c
SS
13472 }
13473 break;
13474 case DW_TAG_formal_parameter:
edb3359d
DJ
13475 /* If we are inside a function, mark this as an argument. If
13476 not, we might be looking at an argument to an inlined function
13477 when we do not have enough information to show inlined frames;
13478 pretend it's a local variable in that case so that the user can
13479 still see it. */
13480 if (context_stack_depth > 0
13481 && context_stack[context_stack_depth - 1].name != NULL)
13482 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 13483 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13484 if (attr)
13485 {
e7c27a73 13486 var_decode_location (attr, sym, cu);
c906108c 13487 }
e142c38c 13488 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13489 if (attr)
13490 {
e7c27a73 13491 dwarf2_const_value (attr, sym, cu);
c906108c 13492 }
f346a30d 13493
e37fd15a 13494 list_to_add = cu->list_in_scope;
c906108c
SS
13495 break;
13496 case DW_TAG_unspecified_parameters:
13497 /* From varargs functions; gdb doesn't seem to have any
13498 interest in this information, so just ignore it for now.
13499 (FIXME?) */
13500 break;
34eaf542
TT
13501 case DW_TAG_template_type_param:
13502 suppress_add = 1;
13503 /* Fall through. */
c906108c 13504 case DW_TAG_class_type:
680b30c7 13505 case DW_TAG_interface_type:
c906108c
SS
13506 case DW_TAG_structure_type:
13507 case DW_TAG_union_type:
72019c9c 13508 case DW_TAG_set_type:
c906108c
SS
13509 case DW_TAG_enumeration_type:
13510 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13511 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 13512
63d06c5c 13513 {
987504bb 13514 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
13515 really ever be static objects: otherwise, if you try
13516 to, say, break of a class's method and you're in a file
13517 which doesn't mention that class, it won't work unless
13518 the check for all static symbols in lookup_symbol_aux
13519 saves you. See the OtherFileClass tests in
13520 gdb.c++/namespace.exp. */
13521
e37fd15a 13522 if (!suppress_add)
34eaf542 13523 {
34eaf542
TT
13524 list_to_add = (cu->list_in_scope == &file_symbols
13525 && (cu->language == language_cplus
13526 || cu->language == language_java)
13527 ? &global_symbols : cu->list_in_scope);
63d06c5c 13528
64382290
TT
13529 /* The semantics of C++ state that "struct foo {
13530 ... }" also defines a typedef for "foo". A Java
13531 class declaration also defines a typedef for the
13532 class. */
13533 if (cu->language == language_cplus
13534 || cu->language == language_java
13535 || cu->language == language_ada)
13536 {
13537 /* The symbol's name is already allocated along
13538 with this objfile, so we don't need to
13539 duplicate it for the type. */
13540 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13541 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13542 }
63d06c5c
DC
13543 }
13544 }
c906108c
SS
13545 break;
13546 case DW_TAG_typedef:
63d06c5c
DC
13547 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13548 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13549 list_to_add = cu->list_in_scope;
63d06c5c 13550 break;
c906108c 13551 case DW_TAG_base_type:
a02abb62 13552 case DW_TAG_subrange_type:
c906108c 13553 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13554 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13555 list_to_add = cu->list_in_scope;
c906108c
SS
13556 break;
13557 case DW_TAG_enumerator:
e142c38c 13558 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13559 if (attr)
13560 {
e7c27a73 13561 dwarf2_const_value (attr, sym, cu);
c906108c 13562 }
63d06c5c
DC
13563 {
13564 /* NOTE: carlton/2003-11-10: See comment above in the
13565 DW_TAG_class_type, etc. block. */
13566
e142c38c 13567 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
13568 && (cu->language == language_cplus
13569 || cu->language == language_java)
e142c38c 13570 ? &global_symbols : cu->list_in_scope);
63d06c5c 13571 }
c906108c 13572 break;
5c4e30ca
DC
13573 case DW_TAG_namespace:
13574 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 13575 list_to_add = &global_symbols;
5c4e30ca 13576 break;
c906108c
SS
13577 default:
13578 /* Not a tag we recognize. Hopefully we aren't processing
13579 trash data, but since we must specifically ignore things
13580 we don't recognize, there is nothing else we should do at
0963b4bd 13581 this point. */
e2e0b3e5 13582 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 13583 dwarf_tag_name (die->tag));
c906108c
SS
13584 break;
13585 }
df8a16a1 13586
e37fd15a
SW
13587 if (suppress_add)
13588 {
13589 sym->hash_next = objfile->template_symbols;
13590 objfile->template_symbols = sym;
13591 list_to_add = NULL;
13592 }
13593
13594 if (list_to_add != NULL)
13595 add_symbol_to_list (sym, list_to_add);
13596
df8a16a1
DJ
13597 /* For the benefit of old versions of GCC, check for anonymous
13598 namespaces based on the demangled name. */
13599 if (!processing_has_namespace_info
94af9270 13600 && cu->language == language_cplus)
a10964d1 13601 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
13602 }
13603 return (sym);
13604}
13605
34eaf542
TT
13606/* A wrapper for new_symbol_full that always allocates a new symbol. */
13607
13608static struct symbol *
13609new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13610{
13611 return new_symbol_full (die, type, cu, NULL);
13612}
13613
98bfdba5
PA
13614/* Given an attr with a DW_FORM_dataN value in host byte order,
13615 zero-extend it as appropriate for the symbol's type. The DWARF
13616 standard (v4) is not entirely clear about the meaning of using
13617 DW_FORM_dataN for a constant with a signed type, where the type is
13618 wider than the data. The conclusion of a discussion on the DWARF
13619 list was that this is unspecified. We choose to always zero-extend
13620 because that is the interpretation long in use by GCC. */
c906108c 13621
98bfdba5
PA
13622static gdb_byte *
13623dwarf2_const_value_data (struct attribute *attr, struct type *type,
13624 const char *name, struct obstack *obstack,
12df843f 13625 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 13626{
e7c27a73 13627 struct objfile *objfile = cu->objfile;
e17a4113
UW
13628 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13629 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
13630 LONGEST l = DW_UNSND (attr);
13631
13632 if (bits < sizeof (*value) * 8)
13633 {
13634 l &= ((LONGEST) 1 << bits) - 1;
13635 *value = l;
13636 }
13637 else if (bits == sizeof (*value) * 8)
13638 *value = l;
13639 else
13640 {
13641 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13642 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13643 return bytes;
13644 }
13645
13646 return NULL;
13647}
13648
13649/* Read a constant value from an attribute. Either set *VALUE, or if
13650 the value does not fit in *VALUE, set *BYTES - either already
13651 allocated on the objfile obstack, or newly allocated on OBSTACK,
13652 or, set *BATON, if we translated the constant to a location
13653 expression. */
13654
13655static void
13656dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13657 const char *name, struct obstack *obstack,
13658 struct dwarf2_cu *cu,
12df843f 13659 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
13660 struct dwarf2_locexpr_baton **baton)
13661{
13662 struct objfile *objfile = cu->objfile;
13663 struct comp_unit_head *cu_header = &cu->header;
c906108c 13664 struct dwarf_block *blk;
98bfdba5
PA
13665 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13666 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13667
13668 *value = 0;
13669 *bytes = NULL;
13670 *baton = NULL;
c906108c
SS
13671
13672 switch (attr->form)
13673 {
13674 case DW_FORM_addr:
3019eac3 13675 case DW_FORM_GNU_addr_index:
ac56253d 13676 {
ac56253d
TT
13677 gdb_byte *data;
13678
98bfdba5
PA
13679 if (TYPE_LENGTH (type) != cu_header->addr_size)
13680 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 13681 cu_header->addr_size,
98bfdba5 13682 TYPE_LENGTH (type));
ac56253d
TT
13683 /* Symbols of this form are reasonably rare, so we just
13684 piggyback on the existing location code rather than writing
13685 a new implementation of symbol_computed_ops. */
98bfdba5
PA
13686 *baton = obstack_alloc (&objfile->objfile_obstack,
13687 sizeof (struct dwarf2_locexpr_baton));
13688 (*baton)->per_cu = cu->per_cu;
13689 gdb_assert ((*baton)->per_cu);
ac56253d 13690
98bfdba5
PA
13691 (*baton)->size = 2 + cu_header->addr_size;
13692 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13693 (*baton)->data = data;
ac56253d
TT
13694
13695 data[0] = DW_OP_addr;
13696 store_unsigned_integer (&data[1], cu_header->addr_size,
13697 byte_order, DW_ADDR (attr));
13698 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 13699 }
c906108c 13700 break;
4ac36638 13701 case DW_FORM_string:
93b5768b 13702 case DW_FORM_strp:
3019eac3 13703 case DW_FORM_GNU_str_index:
98bfdba5
PA
13704 /* DW_STRING is already allocated on the objfile obstack, point
13705 directly to it. */
13706 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 13707 break;
c906108c
SS
13708 case DW_FORM_block1:
13709 case DW_FORM_block2:
13710 case DW_FORM_block4:
13711 case DW_FORM_block:
2dc7f7b3 13712 case DW_FORM_exprloc:
c906108c 13713 blk = DW_BLOCK (attr);
98bfdba5
PA
13714 if (TYPE_LENGTH (type) != blk->size)
13715 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13716 TYPE_LENGTH (type));
13717 *bytes = blk->data;
c906108c 13718 break;
2df3850c
JM
13719
13720 /* The DW_AT_const_value attributes are supposed to carry the
13721 symbol's value "represented as it would be on the target
13722 architecture." By the time we get here, it's already been
13723 converted to host endianness, so we just need to sign- or
13724 zero-extend it as appropriate. */
13725 case DW_FORM_data1:
3e43a32a
MS
13726 *bytes = dwarf2_const_value_data (attr, type, name,
13727 obstack, cu, value, 8);
2df3850c 13728 break;
c906108c 13729 case DW_FORM_data2:
3e43a32a
MS
13730 *bytes = dwarf2_const_value_data (attr, type, name,
13731 obstack, cu, value, 16);
2df3850c 13732 break;
c906108c 13733 case DW_FORM_data4:
3e43a32a
MS
13734 *bytes = dwarf2_const_value_data (attr, type, name,
13735 obstack, cu, value, 32);
2df3850c 13736 break;
c906108c 13737 case DW_FORM_data8:
3e43a32a
MS
13738 *bytes = dwarf2_const_value_data (attr, type, name,
13739 obstack, cu, value, 64);
2df3850c
JM
13740 break;
13741
c906108c 13742 case DW_FORM_sdata:
98bfdba5 13743 *value = DW_SND (attr);
2df3850c
JM
13744 break;
13745
c906108c 13746 case DW_FORM_udata:
98bfdba5 13747 *value = DW_UNSND (attr);
c906108c 13748 break;
2df3850c 13749
c906108c 13750 default:
4d3c2250 13751 complaint (&symfile_complaints,
e2e0b3e5 13752 _("unsupported const value attribute form: '%s'"),
4d3c2250 13753 dwarf_form_name (attr->form));
98bfdba5 13754 *value = 0;
c906108c
SS
13755 break;
13756 }
13757}
13758
2df3850c 13759
98bfdba5
PA
13760/* Copy constant value from an attribute to a symbol. */
13761
2df3850c 13762static void
98bfdba5
PA
13763dwarf2_const_value (struct attribute *attr, struct symbol *sym,
13764 struct dwarf2_cu *cu)
2df3850c 13765{
98bfdba5
PA
13766 struct objfile *objfile = cu->objfile;
13767 struct comp_unit_head *cu_header = &cu->header;
12df843f 13768 LONGEST value;
98bfdba5
PA
13769 gdb_byte *bytes;
13770 struct dwarf2_locexpr_baton *baton;
2df3850c 13771
98bfdba5
PA
13772 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
13773 SYMBOL_PRINT_NAME (sym),
13774 &objfile->objfile_obstack, cu,
13775 &value, &bytes, &baton);
2df3850c 13776
98bfdba5
PA
13777 if (baton != NULL)
13778 {
13779 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13780 SYMBOL_LOCATION_BATON (sym) = baton;
13781 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13782 }
13783 else if (bytes != NULL)
13784 {
13785 SYMBOL_VALUE_BYTES (sym) = bytes;
13786 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
13787 }
13788 else
13789 {
13790 SYMBOL_VALUE (sym) = value;
13791 SYMBOL_CLASS (sym) = LOC_CONST;
13792 }
2df3850c
JM
13793}
13794
c906108c
SS
13795/* Return the type of the die in question using its DW_AT_type attribute. */
13796
13797static struct type *
e7c27a73 13798die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13799{
c906108c 13800 struct attribute *type_attr;
c906108c 13801
e142c38c 13802 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
13803 if (!type_attr)
13804 {
13805 /* A missing DW_AT_type represents a void type. */
46bf5051 13806 return objfile_type (cu->objfile)->builtin_void;
c906108c 13807 }
348e048f 13808
673bfd45 13809 return lookup_die_type (die, type_attr, cu);
c906108c
SS
13810}
13811
b4ba55a1
JB
13812/* True iff CU's producer generates GNAT Ada auxiliary information
13813 that allows to find parallel types through that information instead
13814 of having to do expensive parallel lookups by type name. */
13815
13816static int
13817need_gnat_info (struct dwarf2_cu *cu)
13818{
13819 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
13820 of GNAT produces this auxiliary information, without any indication
13821 that it is produced. Part of enhancing the FSF version of GNAT
13822 to produce that information will be to put in place an indicator
13823 that we can use in order to determine whether the descriptive type
13824 info is available or not. One suggestion that has been made is
13825 to use a new attribute, attached to the CU die. For now, assume
13826 that the descriptive type info is not available. */
13827 return 0;
13828}
13829
b4ba55a1
JB
13830/* Return the auxiliary type of the die in question using its
13831 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
13832 attribute is not present. */
13833
13834static struct type *
13835die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
13836{
b4ba55a1 13837 struct attribute *type_attr;
b4ba55a1
JB
13838
13839 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
13840 if (!type_attr)
13841 return NULL;
13842
673bfd45 13843 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
13844}
13845
13846/* If DIE has a descriptive_type attribute, then set the TYPE's
13847 descriptive type accordingly. */
13848
13849static void
13850set_descriptive_type (struct type *type, struct die_info *die,
13851 struct dwarf2_cu *cu)
13852{
13853 struct type *descriptive_type = die_descriptive_type (die, cu);
13854
13855 if (descriptive_type)
13856 {
13857 ALLOCATE_GNAT_AUX_TYPE (type);
13858 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
13859 }
13860}
13861
c906108c
SS
13862/* Return the containing type of the die in question using its
13863 DW_AT_containing_type attribute. */
13864
13865static struct type *
e7c27a73 13866die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13867{
c906108c 13868 struct attribute *type_attr;
c906108c 13869
e142c38c 13870 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
13871 if (!type_attr)
13872 error (_("Dwarf Error: Problem turning containing type into gdb type "
13873 "[in module %s]"), cu->objfile->name);
13874
673bfd45 13875 return lookup_die_type (die, type_attr, cu);
c906108c
SS
13876}
13877
673bfd45
DE
13878/* Look up the type of DIE in CU using its type attribute ATTR.
13879 If there is no type substitute an error marker. */
13880
c906108c 13881static struct type *
673bfd45
DE
13882lookup_die_type (struct die_info *die, struct attribute *attr,
13883 struct dwarf2_cu *cu)
c906108c 13884{
bb5ed363 13885 struct objfile *objfile = cu->objfile;
f792889a
DJ
13886 struct type *this_type;
13887
673bfd45
DE
13888 /* First see if we have it cached. */
13889
13890 if (is_ref_attr (attr))
13891 {
b64f50a1 13892 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
13893
13894 this_type = get_die_type_at_offset (offset, cu->per_cu);
13895 }
55f1336d 13896 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
13897 {
13898 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
13899
13900 /* sig_type will be NULL if the signatured type is missing from
13901 the debug info. */
13902 if (sig_type == NULL)
13903 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13904 "at 0x%x [in module %s]"),
b64f50a1 13905 die->offset.sect_off, objfile->name);
673bfd45 13906
3019eac3
DE
13907 gdb_assert (sig_type->per_cu.is_debug_types);
13908 /* If we haven't filled in type_offset_in_section yet, then we
13909 haven't read the type in yet. */
13910 this_type = NULL;
13911 if (sig_type->type_offset_in_section.sect_off != 0)
13912 {
13913 this_type =
13914 get_die_type_at_offset (sig_type->type_offset_in_section,
13915 &sig_type->per_cu);
13916 }
673bfd45
DE
13917 }
13918 else
13919 {
13920 dump_die_for_error (die);
13921 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 13922 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
13923 }
13924
13925 /* If not cached we need to read it in. */
13926
13927 if (this_type == NULL)
13928 {
13929 struct die_info *type_die;
13930 struct dwarf2_cu *type_cu = cu;
13931
13932 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
13933 /* If we found the type now, it's probably because the type came
13934 from an inter-CU reference and the type's CU got expanded before
13935 ours. */
13936 this_type = get_die_type (type_die, type_cu);
13937 if (this_type == NULL)
13938 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
13939 }
13940
13941 /* If we still don't have a type use an error marker. */
13942
13943 if (this_type == NULL)
c906108c 13944 {
b00fdb78
TT
13945 char *message, *saved;
13946
13947 /* read_type_die already issued a complaint. */
13948 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 13949 objfile->name,
b64f50a1
JK
13950 cu->header.offset.sect_off,
13951 die->offset.sect_off);
bb5ed363 13952 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
13953 message, strlen (message));
13954 xfree (message);
13955
bb5ed363 13956 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 13957 }
673bfd45 13958
f792889a 13959 return this_type;
c906108c
SS
13960}
13961
673bfd45
DE
13962/* Return the type in DIE, CU.
13963 Returns NULL for invalid types.
13964
13965 This first does a lookup in the appropriate type_hash table,
13966 and only reads the die in if necessary.
13967
13968 NOTE: This can be called when reading in partial or full symbols. */
13969
f792889a 13970static struct type *
e7c27a73 13971read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13972{
f792889a
DJ
13973 struct type *this_type;
13974
13975 this_type = get_die_type (die, cu);
13976 if (this_type)
13977 return this_type;
13978
673bfd45
DE
13979 return read_type_die_1 (die, cu);
13980}
13981
13982/* Read the type in DIE, CU.
13983 Returns NULL for invalid types. */
13984
13985static struct type *
13986read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
13987{
13988 struct type *this_type = NULL;
13989
c906108c
SS
13990 switch (die->tag)
13991 {
13992 case DW_TAG_class_type:
680b30c7 13993 case DW_TAG_interface_type:
c906108c
SS
13994 case DW_TAG_structure_type:
13995 case DW_TAG_union_type:
f792889a 13996 this_type = read_structure_type (die, cu);
c906108c
SS
13997 break;
13998 case DW_TAG_enumeration_type:
f792889a 13999 this_type = read_enumeration_type (die, cu);
c906108c
SS
14000 break;
14001 case DW_TAG_subprogram:
14002 case DW_TAG_subroutine_type:
edb3359d 14003 case DW_TAG_inlined_subroutine:
f792889a 14004 this_type = read_subroutine_type (die, cu);
c906108c
SS
14005 break;
14006 case DW_TAG_array_type:
f792889a 14007 this_type = read_array_type (die, cu);
c906108c 14008 break;
72019c9c 14009 case DW_TAG_set_type:
f792889a 14010 this_type = read_set_type (die, cu);
72019c9c 14011 break;
c906108c 14012 case DW_TAG_pointer_type:
f792889a 14013 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
14014 break;
14015 case DW_TAG_ptr_to_member_type:
f792889a 14016 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
14017 break;
14018 case DW_TAG_reference_type:
f792889a 14019 this_type = read_tag_reference_type (die, cu);
c906108c
SS
14020 break;
14021 case DW_TAG_const_type:
f792889a 14022 this_type = read_tag_const_type (die, cu);
c906108c
SS
14023 break;
14024 case DW_TAG_volatile_type:
f792889a 14025 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
14026 break;
14027 case DW_TAG_string_type:
f792889a 14028 this_type = read_tag_string_type (die, cu);
c906108c
SS
14029 break;
14030 case DW_TAG_typedef:
f792889a 14031 this_type = read_typedef (die, cu);
c906108c 14032 break;
a02abb62 14033 case DW_TAG_subrange_type:
f792889a 14034 this_type = read_subrange_type (die, cu);
a02abb62 14035 break;
c906108c 14036 case DW_TAG_base_type:
f792889a 14037 this_type = read_base_type (die, cu);
c906108c 14038 break;
81a17f79 14039 case DW_TAG_unspecified_type:
f792889a 14040 this_type = read_unspecified_type (die, cu);
81a17f79 14041 break;
0114d602
DJ
14042 case DW_TAG_namespace:
14043 this_type = read_namespace_type (die, cu);
14044 break;
f55ee35c
JK
14045 case DW_TAG_module:
14046 this_type = read_module_type (die, cu);
14047 break;
c906108c 14048 default:
3e43a32a
MS
14049 complaint (&symfile_complaints,
14050 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 14051 dwarf_tag_name (die->tag));
c906108c
SS
14052 break;
14053 }
63d06c5c 14054
f792889a 14055 return this_type;
63d06c5c
DC
14056}
14057
abc72ce4
DE
14058/* See if we can figure out if the class lives in a namespace. We do
14059 this by looking for a member function; its demangled name will
14060 contain namespace info, if there is any.
14061 Return the computed name or NULL.
14062 Space for the result is allocated on the objfile's obstack.
14063 This is the full-die version of guess_partial_die_structure_name.
14064 In this case we know DIE has no useful parent. */
14065
14066static char *
14067guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14068{
14069 struct die_info *spec_die;
14070 struct dwarf2_cu *spec_cu;
14071 struct die_info *child;
14072
14073 spec_cu = cu;
14074 spec_die = die_specification (die, &spec_cu);
14075 if (spec_die != NULL)
14076 {
14077 die = spec_die;
14078 cu = spec_cu;
14079 }
14080
14081 for (child = die->child;
14082 child != NULL;
14083 child = child->sibling)
14084 {
14085 if (child->tag == DW_TAG_subprogram)
14086 {
14087 struct attribute *attr;
14088
14089 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14090 if (attr == NULL)
14091 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14092 if (attr != NULL)
14093 {
14094 char *actual_name
14095 = language_class_name_from_physname (cu->language_defn,
14096 DW_STRING (attr));
14097 char *name = NULL;
14098
14099 if (actual_name != NULL)
14100 {
14101 char *die_name = dwarf2_name (die, cu);
14102
14103 if (die_name != NULL
14104 && strcmp (die_name, actual_name) != 0)
14105 {
14106 /* Strip off the class name from the full name.
14107 We want the prefix. */
14108 int die_name_len = strlen (die_name);
14109 int actual_name_len = strlen (actual_name);
14110
14111 /* Test for '::' as a sanity check. */
14112 if (actual_name_len > die_name_len + 2
3e43a32a
MS
14113 && actual_name[actual_name_len
14114 - die_name_len - 1] == ':')
abc72ce4
DE
14115 name =
14116 obsavestring (actual_name,
14117 actual_name_len - die_name_len - 2,
14118 &cu->objfile->objfile_obstack);
14119 }
14120 }
14121 xfree (actual_name);
14122 return name;
14123 }
14124 }
14125 }
14126
14127 return NULL;
14128}
14129
96408a79
SA
14130/* GCC might emit a nameless typedef that has a linkage name. Determine the
14131 prefix part in such case. See
14132 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14133
14134static char *
14135anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14136{
14137 struct attribute *attr;
14138 char *base;
14139
14140 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14141 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14142 return NULL;
14143
14144 attr = dwarf2_attr (die, DW_AT_name, cu);
14145 if (attr != NULL && DW_STRING (attr) != NULL)
14146 return NULL;
14147
14148 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14149 if (attr == NULL)
14150 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14151 if (attr == NULL || DW_STRING (attr) == NULL)
14152 return NULL;
14153
14154 /* dwarf2_name had to be already called. */
14155 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14156
14157 /* Strip the base name, keep any leading namespaces/classes. */
14158 base = strrchr (DW_STRING (attr), ':');
14159 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14160 return "";
14161
14162 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14163 &cu->objfile->objfile_obstack);
14164}
14165
fdde2d81 14166/* Return the name of the namespace/class that DIE is defined within,
0114d602 14167 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 14168
0114d602
DJ
14169 For example, if we're within the method foo() in the following
14170 code:
14171
14172 namespace N {
14173 class C {
14174 void foo () {
14175 }
14176 };
14177 }
14178
14179 then determine_prefix on foo's die will return "N::C". */
fdde2d81 14180
0d5cff50 14181static const char *
e142c38c 14182determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 14183{
0114d602
DJ
14184 struct die_info *parent, *spec_die;
14185 struct dwarf2_cu *spec_cu;
14186 struct type *parent_type;
96408a79 14187 char *retval;
63d06c5c 14188
f55ee35c
JK
14189 if (cu->language != language_cplus && cu->language != language_java
14190 && cu->language != language_fortran)
0114d602
DJ
14191 return "";
14192
96408a79
SA
14193 retval = anonymous_struct_prefix (die, cu);
14194 if (retval)
14195 return retval;
14196
0114d602
DJ
14197 /* We have to be careful in the presence of DW_AT_specification.
14198 For example, with GCC 3.4, given the code
14199
14200 namespace N {
14201 void foo() {
14202 // Definition of N::foo.
14203 }
14204 }
14205
14206 then we'll have a tree of DIEs like this:
14207
14208 1: DW_TAG_compile_unit
14209 2: DW_TAG_namespace // N
14210 3: DW_TAG_subprogram // declaration of N::foo
14211 4: DW_TAG_subprogram // definition of N::foo
14212 DW_AT_specification // refers to die #3
14213
14214 Thus, when processing die #4, we have to pretend that we're in
14215 the context of its DW_AT_specification, namely the contex of die
14216 #3. */
14217 spec_cu = cu;
14218 spec_die = die_specification (die, &spec_cu);
14219 if (spec_die == NULL)
14220 parent = die->parent;
14221 else
63d06c5c 14222 {
0114d602
DJ
14223 parent = spec_die->parent;
14224 cu = spec_cu;
63d06c5c 14225 }
0114d602
DJ
14226
14227 if (parent == NULL)
14228 return "";
98bfdba5
PA
14229 else if (parent->building_fullname)
14230 {
14231 const char *name;
14232 const char *parent_name;
14233
14234 /* It has been seen on RealView 2.2 built binaries,
14235 DW_TAG_template_type_param types actually _defined_ as
14236 children of the parent class:
14237
14238 enum E {};
14239 template class <class Enum> Class{};
14240 Class<enum E> class_e;
14241
14242 1: DW_TAG_class_type (Class)
14243 2: DW_TAG_enumeration_type (E)
14244 3: DW_TAG_enumerator (enum1:0)
14245 3: DW_TAG_enumerator (enum2:1)
14246 ...
14247 2: DW_TAG_template_type_param
14248 DW_AT_type DW_FORM_ref_udata (E)
14249
14250 Besides being broken debug info, it can put GDB into an
14251 infinite loop. Consider:
14252
14253 When we're building the full name for Class<E>, we'll start
14254 at Class, and go look over its template type parameters,
14255 finding E. We'll then try to build the full name of E, and
14256 reach here. We're now trying to build the full name of E,
14257 and look over the parent DIE for containing scope. In the
14258 broken case, if we followed the parent DIE of E, we'd again
14259 find Class, and once again go look at its template type
14260 arguments, etc., etc. Simply don't consider such parent die
14261 as source-level parent of this die (it can't be, the language
14262 doesn't allow it), and break the loop here. */
14263 name = dwarf2_name (die, cu);
14264 parent_name = dwarf2_name (parent, cu);
14265 complaint (&symfile_complaints,
14266 _("template param type '%s' defined within parent '%s'"),
14267 name ? name : "<unknown>",
14268 parent_name ? parent_name : "<unknown>");
14269 return "";
14270 }
63d06c5c 14271 else
0114d602
DJ
14272 switch (parent->tag)
14273 {
63d06c5c 14274 case DW_TAG_namespace:
0114d602 14275 parent_type = read_type_die (parent, cu);
acebe513
UW
14276 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14277 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14278 Work around this problem here. */
14279 if (cu->language == language_cplus
14280 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14281 return "";
0114d602
DJ
14282 /* We give a name to even anonymous namespaces. */
14283 return TYPE_TAG_NAME (parent_type);
63d06c5c 14284 case DW_TAG_class_type:
680b30c7 14285 case DW_TAG_interface_type:
63d06c5c 14286 case DW_TAG_structure_type:
0114d602 14287 case DW_TAG_union_type:
f55ee35c 14288 case DW_TAG_module:
0114d602
DJ
14289 parent_type = read_type_die (parent, cu);
14290 if (TYPE_TAG_NAME (parent_type) != NULL)
14291 return TYPE_TAG_NAME (parent_type);
14292 else
14293 /* An anonymous structure is only allowed non-static data
14294 members; no typedefs, no member functions, et cetera.
14295 So it does not need a prefix. */
14296 return "";
abc72ce4 14297 case DW_TAG_compile_unit:
95554aad 14298 case DW_TAG_partial_unit:
abc72ce4
DE
14299 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14300 if (cu->language == language_cplus
8b70b953 14301 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14302 && die->child != NULL
14303 && (die->tag == DW_TAG_class_type
14304 || die->tag == DW_TAG_structure_type
14305 || die->tag == DW_TAG_union_type))
14306 {
14307 char *name = guess_full_die_structure_name (die, cu);
14308 if (name != NULL)
14309 return name;
14310 }
14311 return "";
63d06c5c 14312 default:
8176b9b8 14313 return determine_prefix (parent, cu);
63d06c5c 14314 }
63d06c5c
DC
14315}
14316
3e43a32a
MS
14317/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14318 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14319 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14320 an obconcat, otherwise allocate storage for the result. The CU argument is
14321 used to determine the language and hence, the appropriate separator. */
987504bb 14322
f55ee35c 14323#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
14324
14325static char *
f55ee35c
JK
14326typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14327 int physname, struct dwarf2_cu *cu)
63d06c5c 14328{
f55ee35c 14329 const char *lead = "";
5c315b68 14330 const char *sep;
63d06c5c 14331
3e43a32a
MS
14332 if (suffix == NULL || suffix[0] == '\0'
14333 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
14334 sep = "";
14335 else if (cu->language == language_java)
14336 sep = ".";
f55ee35c
JK
14337 else if (cu->language == language_fortran && physname)
14338 {
14339 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14340 DW_AT_MIPS_linkage_name is preferred and used instead. */
14341
14342 lead = "__";
14343 sep = "_MOD_";
14344 }
987504bb
JJ
14345 else
14346 sep = "::";
63d06c5c 14347
6dd47d34
DE
14348 if (prefix == NULL)
14349 prefix = "";
14350 if (suffix == NULL)
14351 suffix = "";
14352
987504bb
JJ
14353 if (obs == NULL)
14354 {
3e43a32a
MS
14355 char *retval
14356 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 14357
f55ee35c
JK
14358 strcpy (retval, lead);
14359 strcat (retval, prefix);
6dd47d34
DE
14360 strcat (retval, sep);
14361 strcat (retval, suffix);
63d06c5c
DC
14362 return retval;
14363 }
987504bb
JJ
14364 else
14365 {
14366 /* We have an obstack. */
f55ee35c 14367 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 14368 }
63d06c5c
DC
14369}
14370
c906108c
SS
14371/* Return sibling of die, NULL if no sibling. */
14372
f9aca02d 14373static struct die_info *
fba45db2 14374sibling_die (struct die_info *die)
c906108c 14375{
639d11d3 14376 return die->sibling;
c906108c
SS
14377}
14378
71c25dea
TT
14379/* Get name of a die, return NULL if not found. */
14380
14381static char *
14382dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14383 struct obstack *obstack)
14384{
14385 if (name && cu->language == language_cplus)
14386 {
14387 char *canon_name = cp_canonicalize_string (name);
14388
14389 if (canon_name != NULL)
14390 {
14391 if (strcmp (canon_name, name) != 0)
14392 name = obsavestring (canon_name, strlen (canon_name),
14393 obstack);
14394 xfree (canon_name);
14395 }
14396 }
14397
14398 return name;
c906108c
SS
14399}
14400
9219021c
DC
14401/* Get name of a die, return NULL if not found. */
14402
14403static char *
e142c38c 14404dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
14405{
14406 struct attribute *attr;
14407
e142c38c 14408 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
14409 if ((!attr || !DW_STRING (attr))
14410 && die->tag != DW_TAG_class_type
14411 && die->tag != DW_TAG_interface_type
14412 && die->tag != DW_TAG_structure_type
14413 && die->tag != DW_TAG_union_type)
71c25dea
TT
14414 return NULL;
14415
14416 switch (die->tag)
14417 {
14418 case DW_TAG_compile_unit:
95554aad 14419 case DW_TAG_partial_unit:
71c25dea
TT
14420 /* Compilation units have a DW_AT_name that is a filename, not
14421 a source language identifier. */
14422 case DW_TAG_enumeration_type:
14423 case DW_TAG_enumerator:
14424 /* These tags always have simple identifiers already; no need
14425 to canonicalize them. */
14426 return DW_STRING (attr);
907af001 14427
418835cc
KS
14428 case DW_TAG_subprogram:
14429 /* Java constructors will all be named "<init>", so return
14430 the class name when we see this special case. */
14431 if (cu->language == language_java
14432 && DW_STRING (attr) != NULL
14433 && strcmp (DW_STRING (attr), "<init>") == 0)
14434 {
14435 struct dwarf2_cu *spec_cu = cu;
14436 struct die_info *spec_die;
14437
14438 /* GCJ will output '<init>' for Java constructor names.
14439 For this special case, return the name of the parent class. */
14440
14441 /* GCJ may output suprogram DIEs with AT_specification set.
14442 If so, use the name of the specified DIE. */
14443 spec_die = die_specification (die, &spec_cu);
14444 if (spec_die != NULL)
14445 return dwarf2_name (spec_die, spec_cu);
14446
14447 do
14448 {
14449 die = die->parent;
14450 if (die->tag == DW_TAG_class_type)
14451 return dwarf2_name (die, cu);
14452 }
95554aad
TT
14453 while (die->tag != DW_TAG_compile_unit
14454 && die->tag != DW_TAG_partial_unit);
418835cc 14455 }
907af001
UW
14456 break;
14457
14458 case DW_TAG_class_type:
14459 case DW_TAG_interface_type:
14460 case DW_TAG_structure_type:
14461 case DW_TAG_union_type:
14462 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14463 structures or unions. These were of the form "._%d" in GCC 4.1,
14464 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14465 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
14466 if (attr && DW_STRING (attr)
14467 && (strncmp (DW_STRING (attr), "._", 2) == 0
14468 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 14469 return NULL;
53832f31
TT
14470
14471 /* GCC might emit a nameless typedef that has a linkage name. See
14472 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14473 if (!attr || DW_STRING (attr) == NULL)
14474 {
df5c6c50 14475 char *demangled = NULL;
53832f31
TT
14476
14477 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14478 if (attr == NULL)
14479 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14480
14481 if (attr == NULL || DW_STRING (attr) == NULL)
14482 return NULL;
14483
df5c6c50
JK
14484 /* Avoid demangling DW_STRING (attr) the second time on a second
14485 call for the same DIE. */
14486 if (!DW_STRING_IS_CANONICAL (attr))
14487 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
14488
14489 if (demangled)
14490 {
96408a79
SA
14491 char *base;
14492
53832f31 14493 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
14494 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14495 &cu->objfile->objfile_obstack);
53832f31
TT
14496 DW_STRING_IS_CANONICAL (attr) = 1;
14497 xfree (demangled);
96408a79
SA
14498
14499 /* Strip any leading namespaces/classes, keep only the base name.
14500 DW_AT_name for named DIEs does not contain the prefixes. */
14501 base = strrchr (DW_STRING (attr), ':');
14502 if (base && base > DW_STRING (attr) && base[-1] == ':')
14503 return &base[1];
14504 else
14505 return DW_STRING (attr);
53832f31
TT
14506 }
14507 }
907af001
UW
14508 break;
14509
71c25dea 14510 default:
907af001
UW
14511 break;
14512 }
14513
14514 if (!DW_STRING_IS_CANONICAL (attr))
14515 {
14516 DW_STRING (attr)
14517 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14518 &cu->objfile->objfile_obstack);
14519 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 14520 }
907af001 14521 return DW_STRING (attr);
9219021c
DC
14522}
14523
14524/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
14525 is none. *EXT_CU is the CU containing DIE on input, and the CU
14526 containing the return value on output. */
9219021c
DC
14527
14528static struct die_info *
f2f0e013 14529dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
14530{
14531 struct attribute *attr;
9219021c 14532
f2f0e013 14533 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
14534 if (attr == NULL)
14535 return NULL;
14536
f2f0e013 14537 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
14538}
14539
c906108c
SS
14540/* Convert a DIE tag into its string name. */
14541
f39c6ffd 14542static const char *
aa1ee363 14543dwarf_tag_name (unsigned tag)
c906108c 14544{
f39c6ffd
TT
14545 const char *name = get_DW_TAG_name (tag);
14546
14547 if (name == NULL)
14548 return "DW_TAG_<unknown>";
14549
14550 return name;
c906108c
SS
14551}
14552
14553/* Convert a DWARF attribute code into its string name. */
14554
f39c6ffd 14555static const char *
aa1ee363 14556dwarf_attr_name (unsigned attr)
c906108c 14557{
f39c6ffd
TT
14558 const char *name;
14559
c764a876 14560#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
14561 if (attr == DW_AT_MIPS_fde)
14562 return "DW_AT_MIPS_fde";
14563#else
14564 if (attr == DW_AT_HP_block_index)
14565 return "DW_AT_HP_block_index";
c764a876 14566#endif
f39c6ffd
TT
14567
14568 name = get_DW_AT_name (attr);
14569
14570 if (name == NULL)
14571 return "DW_AT_<unknown>";
14572
14573 return name;
c906108c
SS
14574}
14575
14576/* Convert a DWARF value form code into its string name. */
14577
f39c6ffd 14578static const char *
aa1ee363 14579dwarf_form_name (unsigned form)
c906108c 14580{
f39c6ffd
TT
14581 const char *name = get_DW_FORM_name (form);
14582
14583 if (name == NULL)
14584 return "DW_FORM_<unknown>";
14585
14586 return name;
c906108c
SS
14587}
14588
14589static char *
fba45db2 14590dwarf_bool_name (unsigned mybool)
c906108c
SS
14591{
14592 if (mybool)
14593 return "TRUE";
14594 else
14595 return "FALSE";
14596}
14597
14598/* Convert a DWARF type code into its string name. */
14599
f39c6ffd 14600static const char *
aa1ee363 14601dwarf_type_encoding_name (unsigned enc)
c906108c 14602{
f39c6ffd 14603 const char *name = get_DW_ATE_name (enc);
c906108c 14604
f39c6ffd
TT
14605 if (name == NULL)
14606 return "DW_ATE_<unknown>";
c906108c 14607
f39c6ffd 14608 return name;
c906108c 14609}
c906108c 14610
f9aca02d 14611static void
d97bc12b 14612dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
14613{
14614 unsigned int i;
14615
d97bc12b
DE
14616 print_spaces (indent, f);
14617 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 14618 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
14619
14620 if (die->parent != NULL)
14621 {
14622 print_spaces (indent, f);
14623 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 14624 die->parent->offset.sect_off);
d97bc12b
DE
14625 }
14626
14627 print_spaces (indent, f);
14628 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 14629 dwarf_bool_name (die->child != NULL));
c906108c 14630
d97bc12b
DE
14631 print_spaces (indent, f);
14632 fprintf_unfiltered (f, " attributes:\n");
14633
c906108c
SS
14634 for (i = 0; i < die->num_attrs; ++i)
14635 {
d97bc12b
DE
14636 print_spaces (indent, f);
14637 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
14638 dwarf_attr_name (die->attrs[i].name),
14639 dwarf_form_name (die->attrs[i].form));
d97bc12b 14640
c906108c
SS
14641 switch (die->attrs[i].form)
14642 {
c906108c 14643 case DW_FORM_addr:
3019eac3 14644 case DW_FORM_GNU_addr_index:
d97bc12b 14645 fprintf_unfiltered (f, "address: ");
5af949e3 14646 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
14647 break;
14648 case DW_FORM_block2:
14649 case DW_FORM_block4:
14650 case DW_FORM_block:
14651 case DW_FORM_block1:
3e43a32a
MS
14652 fprintf_unfiltered (f, "block: size %d",
14653 DW_BLOCK (&die->attrs[i])->size);
c906108c 14654 break;
2dc7f7b3
TT
14655 case DW_FORM_exprloc:
14656 fprintf_unfiltered (f, "expression: size %u",
14657 DW_BLOCK (&die->attrs[i])->size);
14658 break;
4568ecf9
DE
14659 case DW_FORM_ref_addr:
14660 fprintf_unfiltered (f, "ref address: ");
14661 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14662 break;
10b3939b
DJ
14663 case DW_FORM_ref1:
14664 case DW_FORM_ref2:
14665 case DW_FORM_ref4:
4568ecf9
DE
14666 case DW_FORM_ref8:
14667 case DW_FORM_ref_udata:
d97bc12b 14668 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 14669 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 14670 break;
c906108c
SS
14671 case DW_FORM_data1:
14672 case DW_FORM_data2:
14673 case DW_FORM_data4:
ce5d95e1 14674 case DW_FORM_data8:
c906108c
SS
14675 case DW_FORM_udata:
14676 case DW_FORM_sdata:
43bbcdc2
PH
14677 fprintf_unfiltered (f, "constant: %s",
14678 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 14679 break;
2dc7f7b3
TT
14680 case DW_FORM_sec_offset:
14681 fprintf_unfiltered (f, "section offset: %s",
14682 pulongest (DW_UNSND (&die->attrs[i])));
14683 break;
55f1336d 14684 case DW_FORM_ref_sig8:
348e048f
DE
14685 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14686 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 14687 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
14688 else
14689 fprintf_unfiltered (f, "signatured type, offset: unknown");
14690 break;
c906108c 14691 case DW_FORM_string:
4bdf3d34 14692 case DW_FORM_strp:
3019eac3 14693 case DW_FORM_GNU_str_index:
8285870a 14694 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 14695 DW_STRING (&die->attrs[i])
8285870a
JK
14696 ? DW_STRING (&die->attrs[i]) : "",
14697 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
14698 break;
14699 case DW_FORM_flag:
14700 if (DW_UNSND (&die->attrs[i]))
d97bc12b 14701 fprintf_unfiltered (f, "flag: TRUE");
c906108c 14702 else
d97bc12b 14703 fprintf_unfiltered (f, "flag: FALSE");
c906108c 14704 break;
2dc7f7b3
TT
14705 case DW_FORM_flag_present:
14706 fprintf_unfiltered (f, "flag: TRUE");
14707 break;
a8329558 14708 case DW_FORM_indirect:
0963b4bd
MS
14709 /* The reader will have reduced the indirect form to
14710 the "base form" so this form should not occur. */
3e43a32a
MS
14711 fprintf_unfiltered (f,
14712 "unexpected attribute form: DW_FORM_indirect");
a8329558 14713 break;
c906108c 14714 default:
d97bc12b 14715 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 14716 die->attrs[i].form);
d97bc12b 14717 break;
c906108c 14718 }
d97bc12b 14719 fprintf_unfiltered (f, "\n");
c906108c
SS
14720 }
14721}
14722
f9aca02d 14723static void
d97bc12b 14724dump_die_for_error (struct die_info *die)
c906108c 14725{
d97bc12b
DE
14726 dump_die_shallow (gdb_stderr, 0, die);
14727}
14728
14729static void
14730dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14731{
14732 int indent = level * 4;
14733
14734 gdb_assert (die != NULL);
14735
14736 if (level >= max_level)
14737 return;
14738
14739 dump_die_shallow (f, indent, die);
14740
14741 if (die->child != NULL)
c906108c 14742 {
d97bc12b
DE
14743 print_spaces (indent, f);
14744 fprintf_unfiltered (f, " Children:");
14745 if (level + 1 < max_level)
14746 {
14747 fprintf_unfiltered (f, "\n");
14748 dump_die_1 (f, level + 1, max_level, die->child);
14749 }
14750 else
14751 {
3e43a32a
MS
14752 fprintf_unfiltered (f,
14753 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14754 }
14755 }
14756
14757 if (die->sibling != NULL && level > 0)
14758 {
14759 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14760 }
14761}
14762
d97bc12b
DE
14763/* This is called from the pdie macro in gdbinit.in.
14764 It's not static so gcc will keep a copy callable from gdb. */
14765
14766void
14767dump_die (struct die_info *die, int max_level)
14768{
14769 dump_die_1 (gdb_stdlog, 0, max_level, die);
14770}
14771
f9aca02d 14772static void
51545339 14773store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14774{
51545339 14775 void **slot;
c906108c 14776
b64f50a1
JK
14777 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14778 INSERT);
51545339
DJ
14779
14780 *slot = die;
c906108c
SS
14781}
14782
b64f50a1
JK
14783/* DW_ADDR is always stored already as sect_offset; despite for the forms
14784 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14785
93311388
DE
14786static int
14787is_ref_attr (struct attribute *attr)
c906108c 14788{
c906108c
SS
14789 switch (attr->form)
14790 {
14791 case DW_FORM_ref_addr:
c906108c
SS
14792 case DW_FORM_ref1:
14793 case DW_FORM_ref2:
14794 case DW_FORM_ref4:
613e1657 14795 case DW_FORM_ref8:
c906108c 14796 case DW_FORM_ref_udata:
93311388 14797 return 1;
c906108c 14798 default:
93311388 14799 return 0;
c906108c 14800 }
93311388
DE
14801}
14802
b64f50a1
JK
14803/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14804 required kind. */
14805
14806static sect_offset
93311388
DE
14807dwarf2_get_ref_die_offset (struct attribute *attr)
14808{
4568ecf9 14809 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 14810
93311388 14811 if (is_ref_attr (attr))
b64f50a1 14812 return retval;
93311388 14813
b64f50a1 14814 retval.sect_off = 0;
93311388
DE
14815 complaint (&symfile_complaints,
14816 _("unsupported die ref attribute form: '%s'"),
14817 dwarf_form_name (attr->form));
b64f50a1 14818 return retval;
c906108c
SS
14819}
14820
43bbcdc2
PH
14821/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14822 * the value held by the attribute is not constant. */
a02abb62 14823
43bbcdc2 14824static LONGEST
a02abb62
JB
14825dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14826{
14827 if (attr->form == DW_FORM_sdata)
14828 return DW_SND (attr);
14829 else if (attr->form == DW_FORM_udata
14830 || attr->form == DW_FORM_data1
14831 || attr->form == DW_FORM_data2
14832 || attr->form == DW_FORM_data4
14833 || attr->form == DW_FORM_data8)
14834 return DW_UNSND (attr);
14835 else
14836 {
3e43a32a
MS
14837 complaint (&symfile_complaints,
14838 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14839 dwarf_form_name (attr->form));
14840 return default_value;
14841 }
14842}
14843
03dd20cc 14844/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14845 unit and add it to our queue.
14846 The result is non-zero if PER_CU was queued, otherwise the result is zero
14847 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14848
348e048f 14849static int
03dd20cc 14850maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
95554aad
TT
14851 struct dwarf2_per_cu_data *per_cu,
14852 enum language pretend_language)
03dd20cc 14853{
98bfdba5
PA
14854 /* We may arrive here during partial symbol reading, if we need full
14855 DIEs to process an unusual case (e.g. template arguments). Do
14856 not queue PER_CU, just tell our caller to load its DIEs. */
14857 if (dwarf2_per_objfile->reading_partial_symbols)
14858 {
14859 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14860 return 1;
14861 return 0;
14862 }
14863
03dd20cc
DJ
14864 /* Mark the dependence relation so that we don't flush PER_CU
14865 too early. */
14866 dwarf2_add_dependence (this_cu, per_cu);
14867
14868 /* If it's already on the queue, we have nothing to do. */
14869 if (per_cu->queued)
348e048f 14870 return 0;
03dd20cc
DJ
14871
14872 /* If the compilation unit is already loaded, just mark it as
14873 used. */
14874 if (per_cu->cu != NULL)
14875 {
14876 per_cu->cu->last_used = 0;
348e048f 14877 return 0;
03dd20cc
DJ
14878 }
14879
14880 /* Add it to the queue. */
95554aad 14881 queue_comp_unit (per_cu, pretend_language);
348e048f
DE
14882
14883 return 1;
14884}
14885
14886/* Follow reference or signature attribute ATTR of SRC_DIE.
14887 On entry *REF_CU is the CU of SRC_DIE.
14888 On exit *REF_CU is the CU of the result. */
14889
14890static struct die_info *
14891follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14892 struct dwarf2_cu **ref_cu)
14893{
14894 struct die_info *die;
14895
14896 if (is_ref_attr (attr))
14897 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14898 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14899 die = follow_die_sig (src_die, attr, ref_cu);
14900 else
14901 {
14902 dump_die_for_error (src_die);
14903 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14904 (*ref_cu)->objfile->name);
14905 }
14906
14907 return die;
03dd20cc
DJ
14908}
14909
5c631832 14910/* Follow reference OFFSET.
673bfd45
DE
14911 On entry *REF_CU is the CU of the source die referencing OFFSET.
14912 On exit *REF_CU is the CU of the result.
14913 Returns NULL if OFFSET is invalid. */
f504f079 14914
f9aca02d 14915static struct die_info *
b64f50a1 14916follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
c906108c 14917{
10b3939b 14918 struct die_info temp_die;
f2f0e013 14919 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14920
348e048f
DE
14921 gdb_assert (cu->per_cu != NULL);
14922
98bfdba5
PA
14923 target_cu = cu;
14924
3019eac3 14925 if (cu->per_cu->is_debug_types)
348e048f
DE
14926 {
14927 /* .debug_types CUs cannot reference anything outside their CU.
14928 If they need to, they have to reference a signatured type via
55f1336d 14929 DW_FORM_ref_sig8. */
348e048f 14930 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14931 return NULL;
348e048f
DE
14932 }
14933 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14934 {
14935 struct dwarf2_per_cu_data *per_cu;
9a619af0 14936
45452591 14937 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14938
14939 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
14940 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
14941 load_full_comp_unit (per_cu, cu->language);
03dd20cc 14942
10b3939b
DJ
14943 target_cu = per_cu->cu;
14944 }
98bfdba5
PA
14945 else if (cu->dies == NULL)
14946 {
14947 /* We're loading full DIEs during partial symbol reading. */
14948 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 14949 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 14950 }
c906108c 14951
f2f0e013 14952 *ref_cu = target_cu;
51545339 14953 temp_die.offset = offset;
b64f50a1 14954 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 14955}
10b3939b 14956
5c631832
JK
14957/* Follow reference attribute ATTR of SRC_DIE.
14958 On entry *REF_CU is the CU of SRC_DIE.
14959 On exit *REF_CU is the CU of the result. */
14960
14961static struct die_info *
14962follow_die_ref (struct die_info *src_die, struct attribute *attr,
14963 struct dwarf2_cu **ref_cu)
14964{
b64f50a1 14965 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
14966 struct dwarf2_cu *cu = *ref_cu;
14967 struct die_info *die;
14968
14969 die = follow_die_offset (offset, ref_cu);
14970 if (!die)
14971 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14972 "at 0x%x [in module %s]"),
b64f50a1 14973 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 14974
5c631832
JK
14975 return die;
14976}
14977
d83e736b
JK
14978/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14979 Returned value is intended for DW_OP_call*. Returned
14980 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
14981
14982struct dwarf2_locexpr_baton
b64f50a1 14983dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
14984 struct dwarf2_per_cu_data *per_cu,
14985 CORE_ADDR (*get_frame_pc) (void *baton),
14986 void *baton)
5c631832 14987{
b64f50a1 14988 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 14989 struct dwarf2_cu *cu;
5c631832
JK
14990 struct die_info *die;
14991 struct attribute *attr;
14992 struct dwarf2_locexpr_baton retval;
14993
8cf6f0b1
TT
14994 dw2_setup (per_cu->objfile);
14995
918dd910
JK
14996 if (per_cu->cu == NULL)
14997 load_cu (per_cu);
14998 cu = per_cu->cu;
14999
5c631832
JK
15000 die = follow_die_offset (offset, &cu);
15001 if (!die)
15002 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 15003 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15004
15005 attr = dwarf2_attr (die, DW_AT_location, cu);
15006 if (!attr)
15007 {
e103e986
JK
15008 /* DWARF: "If there is no such attribute, then there is no effect.".
15009 DATA is ignored if SIZE is 0. */
5c631832 15010
e103e986 15011 retval.data = NULL;
5c631832
JK
15012 retval.size = 0;
15013 }
8cf6f0b1
TT
15014 else if (attr_form_is_section_offset (attr))
15015 {
15016 struct dwarf2_loclist_baton loclist_baton;
15017 CORE_ADDR pc = (*get_frame_pc) (baton);
15018 size_t size;
15019
15020 fill_in_loclist_baton (cu, &loclist_baton, attr);
15021
15022 retval.data = dwarf2_find_location_expression (&loclist_baton,
15023 &size, pc);
15024 retval.size = size;
15025 }
5c631832
JK
15026 else
15027 {
15028 if (!attr_form_is_block (attr))
15029 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15030 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 15031 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15032
15033 retval.data = DW_BLOCK (attr)->data;
15034 retval.size = DW_BLOCK (attr)->size;
15035 }
15036 retval.per_cu = cu->per_cu;
918dd910 15037
918dd910
JK
15038 age_cached_comp_units ();
15039
5c631832 15040 return retval;
348e048f
DE
15041}
15042
8a9b8146
TT
15043/* Return the type of the DIE at DIE_OFFSET in the CU named by
15044 PER_CU. */
15045
15046struct type *
b64f50a1 15047dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
15048 struct dwarf2_per_cu_data *per_cu)
15049{
b64f50a1
JK
15050 sect_offset die_offset_sect;
15051
8a9b8146 15052 dw2_setup (per_cu->objfile);
b64f50a1
JK
15053
15054 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15055 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
15056}
15057
348e048f
DE
15058/* Follow the signature attribute ATTR in SRC_DIE.
15059 On entry *REF_CU is the CU of SRC_DIE.
15060 On exit *REF_CU is the CU of the result. */
15061
15062static struct die_info *
15063follow_die_sig (struct die_info *src_die, struct attribute *attr,
15064 struct dwarf2_cu **ref_cu)
15065{
15066 struct objfile *objfile = (*ref_cu)->objfile;
15067 struct die_info temp_die;
15068 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15069 struct dwarf2_cu *sig_cu;
15070 struct die_info *die;
15071
15072 /* sig_type will be NULL if the signatured type is missing from
15073 the debug info. */
15074 if (sig_type == NULL)
15075 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15076 "at 0x%x [in module %s]"),
b64f50a1 15077 src_die->offset.sect_off, objfile->name);
348e048f
DE
15078
15079 /* If necessary, add it to the queue and load its DIEs. */
15080
95554aad 15081 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 15082 read_signatured_type (sig_type);
348e048f
DE
15083
15084 gdb_assert (sig_type->per_cu.cu != NULL);
15085
15086 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
15087 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15088 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
15089 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15090 temp_die.offset.sect_off);
348e048f
DE
15091 if (die)
15092 {
15093 *ref_cu = sig_cu;
15094 return die;
15095 }
15096
3e43a32a
MS
15097 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15098 "from DIE at 0x%x [in module %s]"),
b64f50a1 15099 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
15100}
15101
15102/* Given an offset of a signatured type, return its signatured_type. */
15103
15104static struct signatured_type *
8b70b953
TT
15105lookup_signatured_type_at_offset (struct objfile *objfile,
15106 struct dwarf2_section_info *section,
b64f50a1 15107 sect_offset offset)
348e048f 15108{
b64f50a1 15109 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
15110 unsigned int length, initial_length_size;
15111 unsigned int sig_offset;
52dc124a 15112 struct signatured_type find_entry, *sig_type;
348e048f
DE
15113
15114 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15115 sig_offset = (initial_length_size
15116 + 2 /*version*/
15117 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15118 + 1 /*address_size*/);
15119 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 15120 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
15121
15122 /* This is only used to lookup previously recorded types.
15123 If we didn't find it, it's our bug. */
52dc124a
DE
15124 gdb_assert (sig_type != NULL);
15125 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 15126
52dc124a 15127 return sig_type;
348e048f
DE
15128}
15129
e5fe5e75 15130/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
15131
15132static void
e5fe5e75 15133load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 15134{
e5fe5e75 15135 struct objfile *objfile = per_cu->objfile;
3019eac3 15136 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
b64f50a1 15137 sect_offset offset = per_cu->offset;
52dc124a 15138 struct signatured_type *sig_type;
348e048f 15139
8b70b953 15140 dwarf2_read_section (objfile, sect);
be391dca 15141
348e048f 15142 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
15143 hash table lookup. */
15144 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15145 the signature to assert we found the right one.
15146 Ok, but it's a lot of work. We should simplify things so any needed
15147 assert doesn't require all this clumsiness. */
52dc124a 15148 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f 15149
dee91e82 15150 gdb_assert (&sig_type->per_cu == per_cu);
52dc124a 15151 gdb_assert (sig_type->per_cu.cu == NULL);
348e048f 15152
52dc124a 15153 read_signatured_type (sig_type);
348e048f 15154
52dc124a 15155 gdb_assert (sig_type->per_cu.cu != NULL);
348e048f
DE
15156}
15157
dee91e82
DE
15158/* die_reader_func for read_signatured_type.
15159 This is identical to load_full_comp_unit_reader,
15160 but is kept separate for now. */
348e048f
DE
15161
15162static void
dee91e82
DE
15163read_signatured_type_reader (const struct die_reader_specs *reader,
15164 gdb_byte *info_ptr,
15165 struct die_info *comp_unit_die,
15166 int has_children,
15167 void *data)
348e048f 15168{
dee91e82 15169 struct dwarf2_cu *cu = reader->cu;
348e048f 15170
dee91e82
DE
15171 gdb_assert (cu->die_hash == NULL);
15172 cu->die_hash =
15173 htab_create_alloc_ex (cu->header.length / 12,
15174 die_hash,
15175 die_eq,
15176 NULL,
15177 &cu->comp_unit_obstack,
15178 hashtab_obstack_allocate,
15179 dummy_obstack_deallocate);
348e048f 15180
dee91e82
DE
15181 if (has_children)
15182 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15183 &info_ptr, comp_unit_die);
15184 cu->dies = comp_unit_die;
15185 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
15186
15187 /* We try not to read any attributes in this function, because not
9cdd5dbd 15188 all CUs needed for references have been loaded yet, and symbol
348e048f 15189 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
15190 or we won't be able to build types correctly.
15191 Similarly, if we do not read the producer, we can not apply
15192 producer-specific interpretation. */
95554aad 15193 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 15194}
348e048f 15195
3019eac3
DE
15196/* Read in a signatured type and build its CU and DIEs.
15197 If the type is a stub for the real type in a DWO file,
15198 read in the real type from the DWO file as well. */
dee91e82
DE
15199
15200static void
15201read_signatured_type (struct signatured_type *sig_type)
15202{
15203 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 15204
3019eac3 15205 gdb_assert (per_cu->is_debug_types);
dee91e82 15206 gdb_assert (per_cu->cu == NULL);
348e048f 15207
fd820528 15208 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
c906108c
SS
15209}
15210
c906108c
SS
15211/* Decode simple location descriptions.
15212 Given a pointer to a dwarf block that defines a location, compute
15213 the location and return the value.
15214
4cecd739
DJ
15215 NOTE drow/2003-11-18: This function is called in two situations
15216 now: for the address of static or global variables (partial symbols
15217 only) and for offsets into structures which are expected to be
15218 (more or less) constant. The partial symbol case should go away,
15219 and only the constant case should remain. That will let this
15220 function complain more accurately. A few special modes are allowed
15221 without complaint for global variables (for instance, global
15222 register values and thread-local values).
c906108c
SS
15223
15224 A location description containing no operations indicates that the
4cecd739 15225 object is optimized out. The return value is 0 for that case.
6b992462
DJ
15226 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15227 callers will only want a very basic result and this can become a
21ae7a4d
JK
15228 complaint.
15229
15230 Note that stack[0] is unused except as a default error return. */
c906108c
SS
15231
15232static CORE_ADDR
e7c27a73 15233decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 15234{
e7c27a73 15235 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
15236 int i;
15237 int size = blk->size;
15238 gdb_byte *data = blk->data;
15239 CORE_ADDR stack[64];
15240 int stacki;
15241 unsigned int bytes_read, unsnd;
15242 gdb_byte op;
c906108c 15243
21ae7a4d
JK
15244 i = 0;
15245 stacki = 0;
15246 stack[stacki] = 0;
15247 stack[++stacki] = 0;
15248
15249 while (i < size)
15250 {
15251 op = data[i++];
15252 switch (op)
15253 {
15254 case DW_OP_lit0:
15255 case DW_OP_lit1:
15256 case DW_OP_lit2:
15257 case DW_OP_lit3:
15258 case DW_OP_lit4:
15259 case DW_OP_lit5:
15260 case DW_OP_lit6:
15261 case DW_OP_lit7:
15262 case DW_OP_lit8:
15263 case DW_OP_lit9:
15264 case DW_OP_lit10:
15265 case DW_OP_lit11:
15266 case DW_OP_lit12:
15267 case DW_OP_lit13:
15268 case DW_OP_lit14:
15269 case DW_OP_lit15:
15270 case DW_OP_lit16:
15271 case DW_OP_lit17:
15272 case DW_OP_lit18:
15273 case DW_OP_lit19:
15274 case DW_OP_lit20:
15275 case DW_OP_lit21:
15276 case DW_OP_lit22:
15277 case DW_OP_lit23:
15278 case DW_OP_lit24:
15279 case DW_OP_lit25:
15280 case DW_OP_lit26:
15281 case DW_OP_lit27:
15282 case DW_OP_lit28:
15283 case DW_OP_lit29:
15284 case DW_OP_lit30:
15285 case DW_OP_lit31:
15286 stack[++stacki] = op - DW_OP_lit0;
15287 break;
f1bea926 15288
21ae7a4d
JK
15289 case DW_OP_reg0:
15290 case DW_OP_reg1:
15291 case DW_OP_reg2:
15292 case DW_OP_reg3:
15293 case DW_OP_reg4:
15294 case DW_OP_reg5:
15295 case DW_OP_reg6:
15296 case DW_OP_reg7:
15297 case DW_OP_reg8:
15298 case DW_OP_reg9:
15299 case DW_OP_reg10:
15300 case DW_OP_reg11:
15301 case DW_OP_reg12:
15302 case DW_OP_reg13:
15303 case DW_OP_reg14:
15304 case DW_OP_reg15:
15305 case DW_OP_reg16:
15306 case DW_OP_reg17:
15307 case DW_OP_reg18:
15308 case DW_OP_reg19:
15309 case DW_OP_reg20:
15310 case DW_OP_reg21:
15311 case DW_OP_reg22:
15312 case DW_OP_reg23:
15313 case DW_OP_reg24:
15314 case DW_OP_reg25:
15315 case DW_OP_reg26:
15316 case DW_OP_reg27:
15317 case DW_OP_reg28:
15318 case DW_OP_reg29:
15319 case DW_OP_reg30:
15320 case DW_OP_reg31:
15321 stack[++stacki] = op - DW_OP_reg0;
15322 if (i < size)
15323 dwarf2_complex_location_expr_complaint ();
15324 break;
c906108c 15325
21ae7a4d
JK
15326 case DW_OP_regx:
15327 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15328 i += bytes_read;
15329 stack[++stacki] = unsnd;
15330 if (i < size)
15331 dwarf2_complex_location_expr_complaint ();
15332 break;
c906108c 15333
21ae7a4d
JK
15334 case DW_OP_addr:
15335 stack[++stacki] = read_address (objfile->obfd, &data[i],
15336 cu, &bytes_read);
15337 i += bytes_read;
15338 break;
d53d4ac5 15339
21ae7a4d
JK
15340 case DW_OP_const1u:
15341 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15342 i += 1;
15343 break;
15344
15345 case DW_OP_const1s:
15346 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15347 i += 1;
15348 break;
15349
15350 case DW_OP_const2u:
15351 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15352 i += 2;
15353 break;
15354
15355 case DW_OP_const2s:
15356 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15357 i += 2;
15358 break;
d53d4ac5 15359
21ae7a4d
JK
15360 case DW_OP_const4u:
15361 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15362 i += 4;
15363 break;
15364
15365 case DW_OP_const4s:
15366 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15367 i += 4;
15368 break;
15369
585861ea
JK
15370 case DW_OP_const8u:
15371 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15372 i += 8;
15373 break;
15374
21ae7a4d
JK
15375 case DW_OP_constu:
15376 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15377 &bytes_read);
15378 i += bytes_read;
15379 break;
15380
15381 case DW_OP_consts:
15382 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15383 i += bytes_read;
15384 break;
15385
15386 case DW_OP_dup:
15387 stack[stacki + 1] = stack[stacki];
15388 stacki++;
15389 break;
15390
15391 case DW_OP_plus:
15392 stack[stacki - 1] += stack[stacki];
15393 stacki--;
15394 break;
15395
15396 case DW_OP_plus_uconst:
15397 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15398 &bytes_read);
15399 i += bytes_read;
15400 break;
15401
15402 case DW_OP_minus:
15403 stack[stacki - 1] -= stack[stacki];
15404 stacki--;
15405 break;
15406
15407 case DW_OP_deref:
15408 /* If we're not the last op, then we definitely can't encode
15409 this using GDB's address_class enum. This is valid for partial
15410 global symbols, although the variable's address will be bogus
15411 in the psymtab. */
15412 if (i < size)
15413 dwarf2_complex_location_expr_complaint ();
15414 break;
15415
15416 case DW_OP_GNU_push_tls_address:
15417 /* The top of the stack has the offset from the beginning
15418 of the thread control block at which the variable is located. */
15419 /* Nothing should follow this operator, so the top of stack would
15420 be returned. */
15421 /* This is valid for partial global symbols, but the variable's
585861ea
JK
15422 address will be bogus in the psymtab. Make it always at least
15423 non-zero to not look as a variable garbage collected by linker
15424 which have DW_OP_addr 0. */
21ae7a4d
JK
15425 if (i < size)
15426 dwarf2_complex_location_expr_complaint ();
585861ea 15427 stack[stacki]++;
21ae7a4d
JK
15428 break;
15429
15430 case DW_OP_GNU_uninit:
15431 break;
15432
3019eac3
DE
15433 case DW_OP_GNU_addr_index:
15434 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15435 &bytes_read);
15436 i += bytes_read;
15437 break;
15438
21ae7a4d
JK
15439 default:
15440 {
f39c6ffd 15441 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
15442
15443 if (name)
15444 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15445 name);
15446 else
15447 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15448 op);
15449 }
15450
15451 return (stack[stacki]);
d53d4ac5 15452 }
3c6e0cb3 15453
21ae7a4d
JK
15454 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15455 outside of the allocated space. Also enforce minimum>0. */
15456 if (stacki >= ARRAY_SIZE (stack) - 1)
15457 {
15458 complaint (&symfile_complaints,
15459 _("location description stack overflow"));
15460 return 0;
15461 }
15462
15463 if (stacki <= 0)
15464 {
15465 complaint (&symfile_complaints,
15466 _("location description stack underflow"));
15467 return 0;
15468 }
15469 }
15470 return (stack[stacki]);
c906108c
SS
15471}
15472
15473/* memory allocation interface */
15474
c906108c 15475static struct dwarf_block *
7b5a2f43 15476dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
15477{
15478 struct dwarf_block *blk;
15479
15480 blk = (struct dwarf_block *)
7b5a2f43 15481 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
15482 return (blk);
15483}
15484
15485static struct abbrev_info *
f3dd6933 15486dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
15487{
15488 struct abbrev_info *abbrev;
15489
f3dd6933
DJ
15490 abbrev = (struct abbrev_info *)
15491 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
15492 memset (abbrev, 0, sizeof (struct abbrev_info));
15493 return (abbrev);
15494}
15495
15496static struct die_info *
b60c80d6 15497dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
15498{
15499 struct die_info *die;
b60c80d6
DJ
15500 size_t size = sizeof (struct die_info);
15501
15502 if (num_attrs > 1)
15503 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 15504
b60c80d6 15505 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
15506 memset (die, 0, sizeof (struct die_info));
15507 return (die);
15508}
2e276125
JB
15509
15510\f
15511/* Macro support. */
15512
2e276125
JB
15513/* Return the full name of file number I in *LH's file name table.
15514 Use COMP_DIR as the name of the current directory of the
15515 compilation. The result is allocated using xmalloc; the caller is
15516 responsible for freeing it. */
15517static char *
15518file_full_name (int file, struct line_header *lh, const char *comp_dir)
15519{
6a83a1e6
EZ
15520 /* Is the file number a valid index into the line header's file name
15521 table? Remember that file numbers start with one, not zero. */
15522 if (1 <= file && file <= lh->num_file_names)
15523 {
15524 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 15525
6a83a1e6
EZ
15526 if (IS_ABSOLUTE_PATH (fe->name))
15527 return xstrdup (fe->name);
15528 else
15529 {
15530 const char *dir;
15531 int dir_len;
15532 char *full_name;
15533
15534 if (fe->dir_index)
15535 dir = lh->include_dirs[fe->dir_index - 1];
15536 else
15537 dir = comp_dir;
15538
15539 if (dir)
15540 {
15541 dir_len = strlen (dir);
15542 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15543 strcpy (full_name, dir);
15544 full_name[dir_len] = '/';
15545 strcpy (full_name + dir_len + 1, fe->name);
15546 return full_name;
15547 }
15548 else
15549 return xstrdup (fe->name);
15550 }
15551 }
2e276125
JB
15552 else
15553 {
6a83a1e6
EZ
15554 /* The compiler produced a bogus file number. We can at least
15555 record the macro definitions made in the file, even if we
15556 won't be able to find the file by name. */
15557 char fake_name[80];
9a619af0 15558
6a83a1e6 15559 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 15560
6e70227d 15561 complaint (&symfile_complaints,
6a83a1e6
EZ
15562 _("bad file number in macro information (%d)"),
15563 file);
2e276125 15564
6a83a1e6 15565 return xstrdup (fake_name);
2e276125
JB
15566 }
15567}
15568
15569
15570static struct macro_source_file *
15571macro_start_file (int file, int line,
15572 struct macro_source_file *current_file,
15573 const char *comp_dir,
15574 struct line_header *lh, struct objfile *objfile)
15575{
15576 /* The full name of this source file. */
15577 char *full_name = file_full_name (file, lh, comp_dir);
15578
15579 /* We don't create a macro table for this compilation unit
15580 at all until we actually get a filename. */
15581 if (! pending_macros)
4a146b47 15582 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 15583 objfile->macro_cache);
2e276125
JB
15584
15585 if (! current_file)
abc9d0dc
TT
15586 {
15587 /* If we have no current file, then this must be the start_file
15588 directive for the compilation unit's main source file. */
15589 current_file = macro_set_main (pending_macros, full_name);
15590 macro_define_special (pending_macros);
15591 }
2e276125
JB
15592 else
15593 current_file = macro_include (current_file, line, full_name);
15594
15595 xfree (full_name);
6e70227d 15596
2e276125
JB
15597 return current_file;
15598}
15599
15600
15601/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15602 followed by a null byte. */
15603static char *
15604copy_string (const char *buf, int len)
15605{
15606 char *s = xmalloc (len + 1);
9a619af0 15607
2e276125
JB
15608 memcpy (s, buf, len);
15609 s[len] = '\0';
2e276125
JB
15610 return s;
15611}
15612
15613
15614static const char *
15615consume_improper_spaces (const char *p, const char *body)
15616{
15617 if (*p == ' ')
15618 {
4d3c2250 15619 complaint (&symfile_complaints,
3e43a32a
MS
15620 _("macro definition contains spaces "
15621 "in formal argument list:\n`%s'"),
4d3c2250 15622 body);
2e276125
JB
15623
15624 while (*p == ' ')
15625 p++;
15626 }
15627
15628 return p;
15629}
15630
15631
15632static void
15633parse_macro_definition (struct macro_source_file *file, int line,
15634 const char *body)
15635{
15636 const char *p;
15637
15638 /* The body string takes one of two forms. For object-like macro
15639 definitions, it should be:
15640
15641 <macro name> " " <definition>
15642
15643 For function-like macro definitions, it should be:
15644
15645 <macro name> "() " <definition>
15646 or
15647 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15648
15649 Spaces may appear only where explicitly indicated, and in the
15650 <definition>.
15651
15652 The Dwarf 2 spec says that an object-like macro's name is always
15653 followed by a space, but versions of GCC around March 2002 omit
6e70227d 15654 the space when the macro's definition is the empty string.
2e276125
JB
15655
15656 The Dwarf 2 spec says that there should be no spaces between the
15657 formal arguments in a function-like macro's formal argument list,
15658 but versions of GCC around March 2002 include spaces after the
15659 commas. */
15660
15661
15662 /* Find the extent of the macro name. The macro name is terminated
15663 by either a space or null character (for an object-like macro) or
15664 an opening paren (for a function-like macro). */
15665 for (p = body; *p; p++)
15666 if (*p == ' ' || *p == '(')
15667 break;
15668
15669 if (*p == ' ' || *p == '\0')
15670 {
15671 /* It's an object-like macro. */
15672 int name_len = p - body;
15673 char *name = copy_string (body, name_len);
15674 const char *replacement;
15675
15676 if (*p == ' ')
15677 replacement = body + name_len + 1;
15678 else
15679 {
4d3c2250 15680 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15681 replacement = body + name_len;
15682 }
6e70227d 15683
2e276125
JB
15684 macro_define_object (file, line, name, replacement);
15685
15686 xfree (name);
15687 }
15688 else if (*p == '(')
15689 {
15690 /* It's a function-like macro. */
15691 char *name = copy_string (body, p - body);
15692 int argc = 0;
15693 int argv_size = 1;
15694 char **argv = xmalloc (argv_size * sizeof (*argv));
15695
15696 p++;
15697
15698 p = consume_improper_spaces (p, body);
15699
15700 /* Parse the formal argument list. */
15701 while (*p && *p != ')')
15702 {
15703 /* Find the extent of the current argument name. */
15704 const char *arg_start = p;
15705
15706 while (*p && *p != ',' && *p != ')' && *p != ' ')
15707 p++;
15708
15709 if (! *p || p == arg_start)
4d3c2250 15710 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15711 else
15712 {
15713 /* Make sure argv has room for the new argument. */
15714 if (argc >= argv_size)
15715 {
15716 argv_size *= 2;
15717 argv = xrealloc (argv, argv_size * sizeof (*argv));
15718 }
15719
15720 argv[argc++] = copy_string (arg_start, p - arg_start);
15721 }
15722
15723 p = consume_improper_spaces (p, body);
15724
15725 /* Consume the comma, if present. */
15726 if (*p == ',')
15727 {
15728 p++;
15729
15730 p = consume_improper_spaces (p, body);
15731 }
15732 }
15733
15734 if (*p == ')')
15735 {
15736 p++;
15737
15738 if (*p == ' ')
15739 /* Perfectly formed definition, no complaints. */
15740 macro_define_function (file, line, name,
6e70227d 15741 argc, (const char **) argv,
2e276125
JB
15742 p + 1);
15743 else if (*p == '\0')
15744 {
15745 /* Complain, but do define it. */
4d3c2250 15746 dwarf2_macro_malformed_definition_complaint (body);
2e276125 15747 macro_define_function (file, line, name,
6e70227d 15748 argc, (const char **) argv,
2e276125
JB
15749 p);
15750 }
15751 else
15752 /* Just complain. */
4d3c2250 15753 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15754 }
15755 else
15756 /* Just complain. */
4d3c2250 15757 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15758
15759 xfree (name);
15760 {
15761 int i;
15762
15763 for (i = 0; i < argc; i++)
15764 xfree (argv[i]);
15765 }
15766 xfree (argv);
15767 }
15768 else
4d3c2250 15769 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15770}
15771
cf2c3c16
TT
15772/* Skip some bytes from BYTES according to the form given in FORM.
15773 Returns the new pointer. */
2e276125 15774
cf2c3c16 15775static gdb_byte *
f664829e 15776skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
15777 enum dwarf_form form,
15778 unsigned int offset_size,
15779 struct dwarf2_section_info *section)
2e276125 15780{
cf2c3c16 15781 unsigned int bytes_read;
2e276125 15782
cf2c3c16 15783 switch (form)
2e276125 15784 {
cf2c3c16
TT
15785 case DW_FORM_data1:
15786 case DW_FORM_flag:
15787 ++bytes;
15788 break;
15789
15790 case DW_FORM_data2:
15791 bytes += 2;
15792 break;
15793
15794 case DW_FORM_data4:
15795 bytes += 4;
15796 break;
15797
15798 case DW_FORM_data8:
15799 bytes += 8;
15800 break;
15801
15802 case DW_FORM_string:
15803 read_direct_string (abfd, bytes, &bytes_read);
15804 bytes += bytes_read;
15805 break;
15806
15807 case DW_FORM_sec_offset:
15808 case DW_FORM_strp:
15809 bytes += offset_size;
15810 break;
15811
15812 case DW_FORM_block:
15813 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15814 bytes += bytes_read;
15815 break;
15816
15817 case DW_FORM_block1:
15818 bytes += 1 + read_1_byte (abfd, bytes);
15819 break;
15820 case DW_FORM_block2:
15821 bytes += 2 + read_2_bytes (abfd, bytes);
15822 break;
15823 case DW_FORM_block4:
15824 bytes += 4 + read_4_bytes (abfd, bytes);
15825 break;
15826
15827 case DW_FORM_sdata:
15828 case DW_FORM_udata:
3019eac3
DE
15829 case DW_FORM_GNU_addr_index:
15830 case DW_FORM_GNU_str_index:
f664829e
DE
15831 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
15832 if (bytes == NULL)
15833 {
15834 dwarf2_section_buffer_overflow_complaint (section);
15835 return NULL;
15836 }
cf2c3c16
TT
15837 break;
15838
15839 default:
15840 {
15841 complain:
15842 complaint (&symfile_complaints,
15843 _("invalid form 0x%x in `%s'"),
15844 form,
15845 section->asection->name);
15846 return NULL;
15847 }
2e276125
JB
15848 }
15849
cf2c3c16
TT
15850 return bytes;
15851}
757a13d0 15852
cf2c3c16
TT
15853/* A helper for dwarf_decode_macros that handles skipping an unknown
15854 opcode. Returns an updated pointer to the macro data buffer; or,
15855 on error, issues a complaint and returns NULL. */
757a13d0 15856
cf2c3c16
TT
15857static gdb_byte *
15858skip_unknown_opcode (unsigned int opcode,
15859 gdb_byte **opcode_definitions,
f664829e 15860 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
15861 bfd *abfd,
15862 unsigned int offset_size,
15863 struct dwarf2_section_info *section)
15864{
15865 unsigned int bytes_read, i;
15866 unsigned long arg;
15867 gdb_byte *defn;
2e276125 15868
cf2c3c16 15869 if (opcode_definitions[opcode] == NULL)
2e276125 15870 {
cf2c3c16
TT
15871 complaint (&symfile_complaints,
15872 _("unrecognized DW_MACFINO opcode 0x%x"),
15873 opcode);
15874 return NULL;
15875 }
2e276125 15876
cf2c3c16
TT
15877 defn = opcode_definitions[opcode];
15878 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15879 defn += bytes_read;
2e276125 15880
cf2c3c16
TT
15881 for (i = 0; i < arg; ++i)
15882 {
f664829e
DE
15883 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
15884 section);
cf2c3c16
TT
15885 if (mac_ptr == NULL)
15886 {
15887 /* skip_form_bytes already issued the complaint. */
15888 return NULL;
15889 }
15890 }
757a13d0 15891
cf2c3c16
TT
15892 return mac_ptr;
15893}
757a13d0 15894
cf2c3c16
TT
15895/* A helper function which parses the header of a macro section.
15896 If the macro section is the extended (for now called "GNU") type,
15897 then this updates *OFFSET_SIZE. Returns a pointer to just after
15898 the header, or issues a complaint and returns NULL on error. */
757a13d0 15899
cf2c3c16
TT
15900static gdb_byte *
15901dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15902 bfd *abfd,
15903 gdb_byte *mac_ptr,
15904 unsigned int *offset_size,
15905 int section_is_gnu)
15906{
15907 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15908
cf2c3c16
TT
15909 if (section_is_gnu)
15910 {
15911 unsigned int version, flags;
757a13d0 15912
cf2c3c16
TT
15913 version = read_2_bytes (abfd, mac_ptr);
15914 if (version != 4)
15915 {
15916 complaint (&symfile_complaints,
15917 _("unrecognized version `%d' in .debug_macro section"),
15918 version);
15919 return NULL;
15920 }
15921 mac_ptr += 2;
757a13d0 15922
cf2c3c16
TT
15923 flags = read_1_byte (abfd, mac_ptr);
15924 ++mac_ptr;
15925 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15926
cf2c3c16
TT
15927 if ((flags & 2) != 0)
15928 /* We don't need the line table offset. */
15929 mac_ptr += *offset_size;
757a13d0 15930
cf2c3c16
TT
15931 /* Vendor opcode descriptions. */
15932 if ((flags & 4) != 0)
15933 {
15934 unsigned int i, count;
757a13d0 15935
cf2c3c16
TT
15936 count = read_1_byte (abfd, mac_ptr);
15937 ++mac_ptr;
15938 for (i = 0; i < count; ++i)
15939 {
15940 unsigned int opcode, bytes_read;
15941 unsigned long arg;
15942
15943 opcode = read_1_byte (abfd, mac_ptr);
15944 ++mac_ptr;
15945 opcode_definitions[opcode] = mac_ptr;
15946 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15947 mac_ptr += bytes_read;
15948 mac_ptr += arg;
15949 }
757a13d0 15950 }
cf2c3c16 15951 }
757a13d0 15952
cf2c3c16
TT
15953 return mac_ptr;
15954}
757a13d0 15955
cf2c3c16 15956/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 15957 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
15958
15959static void
15960dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15961 struct macro_source_file *current_file,
15962 struct line_header *lh, char *comp_dir,
15963 struct dwarf2_section_info *section,
15964 int section_is_gnu,
15965 unsigned int offset_size,
8fc3fc34
TT
15966 struct objfile *objfile,
15967 htab_t include_hash)
cf2c3c16
TT
15968{
15969 enum dwarf_macro_record_type macinfo_type;
15970 int at_commandline;
15971 gdb_byte *opcode_definitions[256];
757a13d0 15972
cf2c3c16
TT
15973 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15974 &offset_size, section_is_gnu);
15975 if (mac_ptr == NULL)
15976 {
15977 /* We already issued a complaint. */
15978 return;
15979 }
757a13d0
JK
15980
15981 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15982 GDB is still reading the definitions from command line. First
15983 DW_MACINFO_start_file will need to be ignored as it was already executed
15984 to create CURRENT_FILE for the main source holding also the command line
15985 definitions. On first met DW_MACINFO_start_file this flag is reset to
15986 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15987
15988 at_commandline = 1;
15989
15990 do
15991 {
15992 /* Do we at least have room for a macinfo type byte? */
15993 if (mac_ptr >= mac_end)
15994 {
f664829e 15995 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
15996 break;
15997 }
15998
15999 macinfo_type = read_1_byte (abfd, mac_ptr);
16000 mac_ptr++;
16001
cf2c3c16
TT
16002 /* Note that we rely on the fact that the corresponding GNU and
16003 DWARF constants are the same. */
757a13d0
JK
16004 switch (macinfo_type)
16005 {
16006 /* A zero macinfo type indicates the end of the macro
16007 information. */
16008 case 0:
16009 break;
2e276125 16010
cf2c3c16
TT
16011 case DW_MACRO_GNU_define:
16012 case DW_MACRO_GNU_undef:
16013 case DW_MACRO_GNU_define_indirect:
16014 case DW_MACRO_GNU_undef_indirect:
2e276125 16015 {
891d2f0b 16016 unsigned int bytes_read;
2e276125
JB
16017 int line;
16018 char *body;
cf2c3c16 16019 int is_define;
2e276125 16020
cf2c3c16
TT
16021 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16022 mac_ptr += bytes_read;
16023
16024 if (macinfo_type == DW_MACRO_GNU_define
16025 || macinfo_type == DW_MACRO_GNU_undef)
16026 {
16027 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16028 mac_ptr += bytes_read;
16029 }
16030 else
16031 {
16032 LONGEST str_offset;
16033
16034 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16035 mac_ptr += offset_size;
2e276125 16036
cf2c3c16
TT
16037 body = read_indirect_string_at_offset (abfd, str_offset);
16038 }
16039
16040 is_define = (macinfo_type == DW_MACRO_GNU_define
16041 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 16042 if (! current_file)
757a13d0
JK
16043 {
16044 /* DWARF violation as no main source is present. */
16045 complaint (&symfile_complaints,
16046 _("debug info with no main source gives macro %s "
16047 "on line %d: %s"),
cf2c3c16
TT
16048 is_define ? _("definition") : _("undefinition"),
16049 line, body);
757a13d0
JK
16050 break;
16051 }
3e43a32a
MS
16052 if ((line == 0 && !at_commandline)
16053 || (line != 0 && at_commandline))
4d3c2250 16054 complaint (&symfile_complaints,
757a13d0
JK
16055 _("debug info gives %s macro %s with %s line %d: %s"),
16056 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 16057 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
16058 line == 0 ? _("zero") : _("non-zero"), line, body);
16059
cf2c3c16 16060 if (is_define)
757a13d0 16061 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
16062 else
16063 {
16064 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16065 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16066 macro_undef (current_file, line, body);
16067 }
2e276125
JB
16068 }
16069 break;
16070
cf2c3c16 16071 case DW_MACRO_GNU_start_file:
2e276125 16072 {
891d2f0b 16073 unsigned int bytes_read;
2e276125
JB
16074 int line, file;
16075
16076 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16077 mac_ptr += bytes_read;
16078 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16079 mac_ptr += bytes_read;
16080
3e43a32a
MS
16081 if ((line == 0 && !at_commandline)
16082 || (line != 0 && at_commandline))
757a13d0
JK
16083 complaint (&symfile_complaints,
16084 _("debug info gives source %d included "
16085 "from %s at %s line %d"),
16086 file, at_commandline ? _("command-line") : _("file"),
16087 line == 0 ? _("zero") : _("non-zero"), line);
16088
16089 if (at_commandline)
16090 {
cf2c3c16
TT
16091 /* This DW_MACRO_GNU_start_file was executed in the
16092 pass one. */
757a13d0
JK
16093 at_commandline = 0;
16094 }
16095 else
16096 current_file = macro_start_file (file, line,
16097 current_file, comp_dir,
cf2c3c16 16098 lh, objfile);
2e276125
JB
16099 }
16100 break;
16101
cf2c3c16 16102 case DW_MACRO_GNU_end_file:
2e276125 16103 if (! current_file)
4d3c2250 16104 complaint (&symfile_complaints,
3e43a32a
MS
16105 _("macro debug info has an unmatched "
16106 "`close_file' directive"));
2e276125
JB
16107 else
16108 {
16109 current_file = current_file->included_by;
16110 if (! current_file)
16111 {
cf2c3c16 16112 enum dwarf_macro_record_type next_type;
2e276125
JB
16113
16114 /* GCC circa March 2002 doesn't produce the zero
16115 type byte marking the end of the compilation
16116 unit. Complain if it's not there, but exit no
16117 matter what. */
16118
16119 /* Do we at least have room for a macinfo type byte? */
16120 if (mac_ptr >= mac_end)
16121 {
f664829e 16122 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
16123 return;
16124 }
16125
16126 /* We don't increment mac_ptr here, so this is just
16127 a look-ahead. */
16128 next_type = read_1_byte (abfd, mac_ptr);
16129 if (next_type != 0)
4d3c2250 16130 complaint (&symfile_complaints,
3e43a32a
MS
16131 _("no terminating 0-type entry for "
16132 "macros in `.debug_macinfo' section"));
2e276125
JB
16133
16134 return;
16135 }
16136 }
16137 break;
16138
cf2c3c16
TT
16139 case DW_MACRO_GNU_transparent_include:
16140 {
16141 LONGEST offset;
8fc3fc34 16142 void **slot;
cf2c3c16
TT
16143
16144 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16145 mac_ptr += offset_size;
16146
8fc3fc34
TT
16147 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16148 if (*slot != NULL)
16149 {
16150 /* This has actually happened; see
16151 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16152 complaint (&symfile_complaints,
16153 _("recursive DW_MACRO_GNU_transparent_include in "
16154 ".debug_macro section"));
16155 }
16156 else
16157 {
16158 *slot = mac_ptr;
16159
16160 dwarf_decode_macro_bytes (abfd,
16161 section->buffer + offset,
16162 mac_end, current_file,
16163 lh, comp_dir,
16164 section, section_is_gnu,
16165 offset_size, objfile, include_hash);
16166
16167 htab_remove_elt (include_hash, mac_ptr);
16168 }
cf2c3c16
TT
16169 }
16170 break;
16171
2e276125 16172 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
16173 if (!section_is_gnu)
16174 {
16175 unsigned int bytes_read;
16176 int constant;
2e276125 16177
cf2c3c16
TT
16178 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16179 mac_ptr += bytes_read;
16180 read_direct_string (abfd, mac_ptr, &bytes_read);
16181 mac_ptr += bytes_read;
2e276125 16182
cf2c3c16
TT
16183 /* We don't recognize any vendor extensions. */
16184 break;
16185 }
16186 /* FALLTHROUGH */
16187
16188 default:
16189 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 16190 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
16191 section);
16192 if (mac_ptr == NULL)
16193 return;
16194 break;
2e276125 16195 }
757a13d0 16196 } while (macinfo_type != 0);
2e276125 16197}
8e19ed76 16198
cf2c3c16
TT
16199static void
16200dwarf_decode_macros (struct line_header *lh, unsigned int offset,
16201 char *comp_dir, bfd *abfd,
16202 struct dwarf2_cu *cu,
16203 struct dwarf2_section_info *section,
fceca515 16204 int section_is_gnu, const char *section_name)
cf2c3c16 16205{
bb5ed363 16206 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
16207 gdb_byte *mac_ptr, *mac_end;
16208 struct macro_source_file *current_file = 0;
16209 enum dwarf_macro_record_type macinfo_type;
16210 unsigned int offset_size = cu->header.offset_size;
16211 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
16212 struct cleanup *cleanup;
16213 htab_t include_hash;
16214 void **slot;
cf2c3c16 16215
bb5ed363 16216 dwarf2_read_section (objfile, section);
cf2c3c16
TT
16217 if (section->buffer == NULL)
16218 {
fceca515 16219 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
16220 return;
16221 }
16222
16223 /* First pass: Find the name of the base filename.
16224 This filename is needed in order to process all macros whose definition
16225 (or undefinition) comes from the command line. These macros are defined
16226 before the first DW_MACINFO_start_file entry, and yet still need to be
16227 associated to the base file.
16228
16229 To determine the base file name, we scan the macro definitions until we
16230 reach the first DW_MACINFO_start_file entry. We then initialize
16231 CURRENT_FILE accordingly so that any macro definition found before the
16232 first DW_MACINFO_start_file can still be associated to the base file. */
16233
16234 mac_ptr = section->buffer + offset;
16235 mac_end = section->buffer + section->size;
16236
16237 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16238 &offset_size, section_is_gnu);
16239 if (mac_ptr == NULL)
16240 {
16241 /* We already issued a complaint. */
16242 return;
16243 }
16244
16245 do
16246 {
16247 /* Do we at least have room for a macinfo type byte? */
16248 if (mac_ptr >= mac_end)
16249 {
16250 /* Complaint is printed during the second pass as GDB will probably
16251 stop the first pass earlier upon finding
16252 DW_MACINFO_start_file. */
16253 break;
16254 }
16255
16256 macinfo_type = read_1_byte (abfd, mac_ptr);
16257 mac_ptr++;
16258
16259 /* Note that we rely on the fact that the corresponding GNU and
16260 DWARF constants are the same. */
16261 switch (macinfo_type)
16262 {
16263 /* A zero macinfo type indicates the end of the macro
16264 information. */
16265 case 0:
16266 break;
16267
16268 case DW_MACRO_GNU_define:
16269 case DW_MACRO_GNU_undef:
16270 /* Only skip the data by MAC_PTR. */
16271 {
16272 unsigned int bytes_read;
16273
16274 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16275 mac_ptr += bytes_read;
16276 read_direct_string (abfd, mac_ptr, &bytes_read);
16277 mac_ptr += bytes_read;
16278 }
16279 break;
16280
16281 case DW_MACRO_GNU_start_file:
16282 {
16283 unsigned int bytes_read;
16284 int line, file;
16285
16286 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16287 mac_ptr += bytes_read;
16288 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16289 mac_ptr += bytes_read;
16290
16291 current_file = macro_start_file (file, line, current_file,
bb5ed363 16292 comp_dir, lh, objfile);
cf2c3c16
TT
16293 }
16294 break;
16295
16296 case DW_MACRO_GNU_end_file:
16297 /* No data to skip by MAC_PTR. */
16298 break;
16299
16300 case DW_MACRO_GNU_define_indirect:
16301 case DW_MACRO_GNU_undef_indirect:
16302 {
16303 unsigned int bytes_read;
16304
16305 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16306 mac_ptr += bytes_read;
16307 mac_ptr += offset_size;
16308 }
16309 break;
16310
16311 case DW_MACRO_GNU_transparent_include:
16312 /* Note that, according to the spec, a transparent include
16313 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16314 skip this opcode. */
16315 mac_ptr += offset_size;
16316 break;
16317
16318 case DW_MACINFO_vendor_ext:
16319 /* Only skip the data by MAC_PTR. */
16320 if (!section_is_gnu)
16321 {
16322 unsigned int bytes_read;
16323
16324 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16325 mac_ptr += bytes_read;
16326 read_direct_string (abfd, mac_ptr, &bytes_read);
16327 mac_ptr += bytes_read;
16328 }
16329 /* FALLTHROUGH */
16330
16331 default:
16332 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 16333 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
16334 section);
16335 if (mac_ptr == NULL)
16336 return;
16337 break;
16338 }
16339 } while (macinfo_type != 0 && current_file == NULL);
16340
16341 /* Second pass: Process all entries.
16342
16343 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16344 command-line macro definitions/undefinitions. This flag is unset when we
16345 reach the first DW_MACINFO_start_file entry. */
16346
8fc3fc34
TT
16347 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16348 NULL, xcalloc, xfree);
16349 cleanup = make_cleanup_htab_delete (include_hash);
16350 mac_ptr = section->buffer + offset;
16351 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16352 *slot = mac_ptr;
16353 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
cf2c3c16 16354 current_file, lh, comp_dir, section, section_is_gnu,
8fc3fc34
TT
16355 offset_size, objfile, include_hash);
16356 do_cleanups (cleanup);
cf2c3c16
TT
16357}
16358
8e19ed76 16359/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 16360 if so return true else false. */
380bca97 16361
8e19ed76
PS
16362static int
16363attr_form_is_block (struct attribute *attr)
16364{
16365 return (attr == NULL ? 0 :
16366 attr->form == DW_FORM_block1
16367 || attr->form == DW_FORM_block2
16368 || attr->form == DW_FORM_block4
2dc7f7b3
TT
16369 || attr->form == DW_FORM_block
16370 || attr->form == DW_FORM_exprloc);
8e19ed76 16371}
4c2df51b 16372
c6a0999f
JB
16373/* Return non-zero if ATTR's value is a section offset --- classes
16374 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16375 You may use DW_UNSND (attr) to retrieve such offsets.
16376
16377 Section 7.5.4, "Attribute Encodings", explains that no attribute
16378 may have a value that belongs to more than one of these classes; it
16379 would be ambiguous if we did, because we use the same forms for all
16380 of them. */
380bca97 16381
3690dd37
JB
16382static int
16383attr_form_is_section_offset (struct attribute *attr)
16384{
16385 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
16386 || attr->form == DW_FORM_data8
16387 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
16388}
16389
3690dd37
JB
16390/* Return non-zero if ATTR's value falls in the 'constant' class, or
16391 zero otherwise. When this function returns true, you can apply
16392 dwarf2_get_attr_constant_value to it.
16393
16394 However, note that for some attributes you must check
16395 attr_form_is_section_offset before using this test. DW_FORM_data4
16396 and DW_FORM_data8 are members of both the constant class, and of
16397 the classes that contain offsets into other debug sections
16398 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16399 that, if an attribute's can be either a constant or one of the
16400 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16401 taken as section offsets, not constants. */
380bca97 16402
3690dd37
JB
16403static int
16404attr_form_is_constant (struct attribute *attr)
16405{
16406 switch (attr->form)
16407 {
16408 case DW_FORM_sdata:
16409 case DW_FORM_udata:
16410 case DW_FORM_data1:
16411 case DW_FORM_data2:
16412 case DW_FORM_data4:
16413 case DW_FORM_data8:
16414 return 1;
16415 default:
16416 return 0;
16417 }
16418}
16419
3019eac3
DE
16420/* Return the .debug_loc section to use for CU.
16421 For DWO files use .debug_loc.dwo. */
16422
16423static struct dwarf2_section_info *
16424cu_debug_loc_section (struct dwarf2_cu *cu)
16425{
16426 if (cu->dwo_unit)
16427 return &cu->dwo_unit->dwo_file->sections.loc;
16428 return &dwarf2_per_objfile->loc;
16429}
16430
8cf6f0b1
TT
16431/* A helper function that fills in a dwarf2_loclist_baton. */
16432
16433static void
16434fill_in_loclist_baton (struct dwarf2_cu *cu,
16435 struct dwarf2_loclist_baton *baton,
16436 struct attribute *attr)
16437{
3019eac3
DE
16438 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16439
16440 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
16441
16442 baton->per_cu = cu->per_cu;
16443 gdb_assert (baton->per_cu);
16444 /* We don't know how long the location list is, but make sure we
16445 don't run off the edge of the section. */
3019eac3
DE
16446 baton->size = section->size - DW_UNSND (attr);
16447 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 16448 baton->base_address = cu->base_address;
f664829e 16449 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
16450}
16451
4c2df51b
DJ
16452static void
16453dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 16454 struct dwarf2_cu *cu)
4c2df51b 16455{
bb5ed363 16456 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 16457 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 16458
3690dd37 16459 if (attr_form_is_section_offset (attr)
3019eac3 16460 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
16461 the section. If so, fall through to the complaint in the
16462 other branch. */
3019eac3 16463 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 16464 {
0d53c4c4 16465 struct dwarf2_loclist_baton *baton;
4c2df51b 16466
bb5ed363 16467 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16468 sizeof (struct dwarf2_loclist_baton));
4c2df51b 16469
8cf6f0b1 16470 fill_in_loclist_baton (cu, baton, attr);
be391dca 16471
d00adf39 16472 if (cu->base_known == 0)
0d53c4c4 16473 complaint (&symfile_complaints,
3e43a32a
MS
16474 _("Location list used without "
16475 "specifying the CU base address."));
4c2df51b 16476
768a979c 16477 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
16478 SYMBOL_LOCATION_BATON (sym) = baton;
16479 }
16480 else
16481 {
16482 struct dwarf2_locexpr_baton *baton;
16483
bb5ed363 16484 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16485 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
16486 baton->per_cu = cu->per_cu;
16487 gdb_assert (baton->per_cu);
0d53c4c4
DJ
16488
16489 if (attr_form_is_block (attr))
16490 {
16491 /* Note that we're just copying the block's data pointer
16492 here, not the actual data. We're still pointing into the
6502dd73
DJ
16493 info_buffer for SYM's objfile; right now we never release
16494 that buffer, but when we do clean up properly this may
16495 need to change. */
0d53c4c4
DJ
16496 baton->size = DW_BLOCK (attr)->size;
16497 baton->data = DW_BLOCK (attr)->data;
16498 }
16499 else
16500 {
16501 dwarf2_invalid_attrib_class_complaint ("location description",
16502 SYMBOL_NATURAL_NAME (sym));
16503 baton->size = 0;
0d53c4c4 16504 }
6e70227d 16505
768a979c 16506 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
16507 SYMBOL_LOCATION_BATON (sym) = baton;
16508 }
4c2df51b 16509}
6502dd73 16510
9aa1f1e3
TT
16511/* Return the OBJFILE associated with the compilation unit CU. If CU
16512 came from a separate debuginfo file, then the master objfile is
16513 returned. */
ae0d2f24
UW
16514
16515struct objfile *
16516dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16517{
9291a0cd 16518 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
16519
16520 /* Return the master objfile, so that we can report and look up the
16521 correct file containing this variable. */
16522 if (objfile->separate_debug_objfile_backlink)
16523 objfile = objfile->separate_debug_objfile_backlink;
16524
16525 return objfile;
16526}
16527
96408a79
SA
16528/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16529 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16530 CU_HEADERP first. */
16531
16532static const struct comp_unit_head *
16533per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16534 struct dwarf2_per_cu_data *per_cu)
16535{
16536 struct objfile *objfile;
16537 struct dwarf2_per_objfile *per_objfile;
16538 gdb_byte *info_ptr;
16539
16540 if (per_cu->cu)
16541 return &per_cu->cu->header;
16542
16543 objfile = per_cu->objfile;
16544 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
b64f50a1 16545 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
96408a79
SA
16546
16547 memset (cu_headerp, 0, sizeof (*cu_headerp));
16548 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16549
16550 return cu_headerp;
16551}
16552
ae0d2f24
UW
16553/* Return the address size given in the compilation unit header for CU. */
16554
98714339 16555int
ae0d2f24
UW
16556dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16557{
96408a79
SA
16558 struct comp_unit_head cu_header_local;
16559 const struct comp_unit_head *cu_headerp;
c471e790 16560
96408a79
SA
16561 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16562
16563 return cu_headerp->addr_size;
ae0d2f24
UW
16564}
16565
9eae7c52
TT
16566/* Return the offset size given in the compilation unit header for CU. */
16567
16568int
16569dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16570{
96408a79
SA
16571 struct comp_unit_head cu_header_local;
16572 const struct comp_unit_head *cu_headerp;
9c6c53f7 16573
96408a79
SA
16574 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16575
16576 return cu_headerp->offset_size;
16577}
16578
16579/* See its dwarf2loc.h declaration. */
16580
16581int
16582dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16583{
16584 struct comp_unit_head cu_header_local;
16585 const struct comp_unit_head *cu_headerp;
16586
16587 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16588
16589 if (cu_headerp->version == 2)
16590 return cu_headerp->addr_size;
16591 else
16592 return cu_headerp->offset_size;
181cebd4
JK
16593}
16594
9aa1f1e3
TT
16595/* Return the text offset of the CU. The returned offset comes from
16596 this CU's objfile. If this objfile came from a separate debuginfo
16597 file, then the offset may be different from the corresponding
16598 offset in the parent objfile. */
16599
16600CORE_ADDR
16601dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16602{
bb3fa9d0 16603 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
16604
16605 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16606}
16607
348e048f
DE
16608/* Locate the .debug_info compilation unit from CU's objfile which contains
16609 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
16610
16611static struct dwarf2_per_cu_data *
b64f50a1 16612dwarf2_find_containing_comp_unit (sect_offset offset,
ae038cb0
DJ
16613 struct objfile *objfile)
16614{
16615 struct dwarf2_per_cu_data *this_cu;
16616 int low, high;
16617
ae038cb0
DJ
16618 low = 0;
16619 high = dwarf2_per_objfile->n_comp_units - 1;
16620 while (high > low)
16621 {
16622 int mid = low + (high - low) / 2;
9a619af0 16623
b64f50a1
JK
16624 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16625 >= offset.sect_off)
ae038cb0
DJ
16626 high = mid;
16627 else
16628 low = mid + 1;
16629 }
16630 gdb_assert (low == high);
b64f50a1
JK
16631 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16632 > offset.sect_off)
ae038cb0 16633 {
10b3939b 16634 if (low == 0)
8a3fe4f8
AC
16635 error (_("Dwarf Error: could not find partial DIE containing "
16636 "offset 0x%lx [in module %s]"),
b64f50a1 16637 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 16638
b64f50a1
JK
16639 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16640 <= offset.sect_off);
ae038cb0
DJ
16641 return dwarf2_per_objfile->all_comp_units[low-1];
16642 }
16643 else
16644 {
16645 this_cu = dwarf2_per_objfile->all_comp_units[low];
16646 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
16647 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16648 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16649 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
16650 return this_cu;
16651 }
16652}
16653
23745b47 16654/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 16655
9816fde3 16656static void
23745b47 16657init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 16658{
9816fde3 16659 memset (cu, 0, sizeof (*cu));
23745b47
DE
16660 per_cu->cu = cu;
16661 cu->per_cu = per_cu;
16662 cu->objfile = per_cu->objfile;
93311388 16663 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
16664}
16665
16666/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16667
16668static void
95554aad
TT
16669prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
16670 enum language pretend_language)
9816fde3
JK
16671{
16672 struct attribute *attr;
16673
16674 /* Set the language we're debugging. */
16675 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16676 if (attr)
16677 set_cu_language (DW_UNSND (attr), cu);
16678 else
9cded63f 16679 {
95554aad 16680 cu->language = pretend_language;
9cded63f
TT
16681 cu->language_defn = language_def (cu->language);
16682 }
dee91e82
DE
16683
16684 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
16685 if (attr)
16686 cu->producer = DW_STRING (attr);
93311388
DE
16687}
16688
ae038cb0
DJ
16689/* Release one cached compilation unit, CU. We unlink it from the tree
16690 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
16691 the caller is responsible for that.
16692 NOTE: DATA is a void * because this function is also used as a
16693 cleanup routine. */
ae038cb0
DJ
16694
16695static void
68dc6402 16696free_heap_comp_unit (void *data)
ae038cb0
DJ
16697{
16698 struct dwarf2_cu *cu = data;
16699
23745b47
DE
16700 gdb_assert (cu->per_cu != NULL);
16701 cu->per_cu->cu = NULL;
ae038cb0
DJ
16702 cu->per_cu = NULL;
16703
16704 obstack_free (&cu->comp_unit_obstack, NULL);
16705
16706 xfree (cu);
16707}
16708
72bf9492 16709/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 16710 when we're finished with it. We can't free the pointer itself, but be
dee91e82 16711 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
16712
16713static void
16714free_stack_comp_unit (void *data)
16715{
16716 struct dwarf2_cu *cu = data;
16717
23745b47
DE
16718 gdb_assert (cu->per_cu != NULL);
16719 cu->per_cu->cu = NULL;
16720 cu->per_cu = NULL;
16721
72bf9492
DJ
16722 obstack_free (&cu->comp_unit_obstack, NULL);
16723 cu->partial_dies = NULL;
ae038cb0
DJ
16724}
16725
16726/* Free all cached compilation units. */
16727
16728static void
16729free_cached_comp_units (void *data)
16730{
16731 struct dwarf2_per_cu_data *per_cu, **last_chain;
16732
16733 per_cu = dwarf2_per_objfile->read_in_chain;
16734 last_chain = &dwarf2_per_objfile->read_in_chain;
16735 while (per_cu != NULL)
16736 {
16737 struct dwarf2_per_cu_data *next_cu;
16738
16739 next_cu = per_cu->cu->read_in_chain;
16740
68dc6402 16741 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16742 *last_chain = next_cu;
16743
16744 per_cu = next_cu;
16745 }
16746}
16747
16748/* Increase the age counter on each cached compilation unit, and free
16749 any that are too old. */
16750
16751static void
16752age_cached_comp_units (void)
16753{
16754 struct dwarf2_per_cu_data *per_cu, **last_chain;
16755
16756 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16757 per_cu = dwarf2_per_objfile->read_in_chain;
16758 while (per_cu != NULL)
16759 {
16760 per_cu->cu->last_used ++;
16761 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16762 dwarf2_mark (per_cu->cu);
16763 per_cu = per_cu->cu->read_in_chain;
16764 }
16765
16766 per_cu = dwarf2_per_objfile->read_in_chain;
16767 last_chain = &dwarf2_per_objfile->read_in_chain;
16768 while (per_cu != NULL)
16769 {
16770 struct dwarf2_per_cu_data *next_cu;
16771
16772 next_cu = per_cu->cu->read_in_chain;
16773
16774 if (!per_cu->cu->mark)
16775 {
68dc6402 16776 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16777 *last_chain = next_cu;
16778 }
16779 else
16780 last_chain = &per_cu->cu->read_in_chain;
16781
16782 per_cu = next_cu;
16783 }
16784}
16785
16786/* Remove a single compilation unit from the cache. */
16787
16788static void
dee91e82 16789free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
16790{
16791 struct dwarf2_per_cu_data *per_cu, **last_chain;
16792
16793 per_cu = dwarf2_per_objfile->read_in_chain;
16794 last_chain = &dwarf2_per_objfile->read_in_chain;
16795 while (per_cu != NULL)
16796 {
16797 struct dwarf2_per_cu_data *next_cu;
16798
16799 next_cu = per_cu->cu->read_in_chain;
16800
dee91e82 16801 if (per_cu == target_per_cu)
ae038cb0 16802 {
68dc6402 16803 free_heap_comp_unit (per_cu->cu);
dee91e82 16804 per_cu->cu = NULL;
ae038cb0
DJ
16805 *last_chain = next_cu;
16806 break;
16807 }
16808 else
16809 last_chain = &per_cu->cu->read_in_chain;
16810
16811 per_cu = next_cu;
16812 }
16813}
16814
fe3e1990
DJ
16815/* Release all extra memory associated with OBJFILE. */
16816
16817void
16818dwarf2_free_objfile (struct objfile *objfile)
16819{
16820 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16821
16822 if (dwarf2_per_objfile == NULL)
16823 return;
16824
16825 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16826 free_cached_comp_units (NULL);
16827
7b9f3c50
DE
16828 if (dwarf2_per_objfile->quick_file_names_table)
16829 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16830
fe3e1990
DJ
16831 /* Everything else should be on the objfile obstack. */
16832}
16833
dee91e82
DE
16834/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
16835 We store these in a hash table separate from the DIEs, and preserve them
16836 when the DIEs are flushed out of cache.
16837
16838 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
16839 uniquely identify the type. A file may have multiple .debug_types sections,
16840 or the type may come from a DWO file. We have to use something in
16841 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
16842 routine, get_die_type_at_offset, from outside this file, and thus won't
16843 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
16844 of the objfile. */
1c379e20 16845
dee91e82 16846struct dwarf2_per_cu_offset_and_type
1c379e20 16847{
dee91e82 16848 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 16849 sect_offset offset;
1c379e20
DJ
16850 struct type *type;
16851};
16852
dee91e82 16853/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
16854
16855static hashval_t
dee91e82 16856per_cu_offset_and_type_hash (const void *item)
1c379e20 16857{
dee91e82 16858 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 16859
dee91e82 16860 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
16861}
16862
dee91e82 16863/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
16864
16865static int
dee91e82 16866per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 16867{
dee91e82
DE
16868 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
16869 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16870
dee91e82
DE
16871 return (ofs_lhs->per_cu == ofs_rhs->per_cu
16872 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
16873}
16874
16875/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16876 table if necessary. For convenience, return TYPE.
16877
16878 The DIEs reading must have careful ordering to:
16879 * Not cause infite loops trying to read in DIEs as a prerequisite for
16880 reading current DIE.
16881 * Not trying to dereference contents of still incompletely read in types
16882 while reading in other DIEs.
16883 * Enable referencing still incompletely read in types just by a pointer to
16884 the type without accessing its fields.
16885
16886 Therefore caller should follow these rules:
16887 * Try to fetch any prerequisite types we may need to build this DIE type
16888 before building the type and calling set_die_type.
e71ec853 16889 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16890 possible before fetching more types to complete the current type.
16891 * Make the type as complete as possible before fetching more types. */
1c379e20 16892
f792889a 16893static struct type *
1c379e20
DJ
16894set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16895{
dee91e82 16896 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 16897 struct objfile *objfile = cu->objfile;
1c379e20 16898
b4ba55a1
JB
16899 /* For Ada types, make sure that the gnat-specific data is always
16900 initialized (if not already set). There are a few types where
16901 we should not be doing so, because the type-specific area is
16902 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16903 where the type-specific area is used to store the floatformat).
16904 But this is not a problem, because the gnat-specific information
16905 is actually not needed for these types. */
16906 if (need_gnat_info (cu)
16907 && TYPE_CODE (type) != TYPE_CODE_FUNC
16908 && TYPE_CODE (type) != TYPE_CODE_FLT
16909 && !HAVE_GNAT_AUX_INFO (type))
16910 INIT_GNAT_SPECIFIC (type);
16911
dee91e82 16912 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 16913 {
dee91e82
DE
16914 dwarf2_per_objfile->die_type_hash =
16915 htab_create_alloc_ex (127,
16916 per_cu_offset_and_type_hash,
16917 per_cu_offset_and_type_eq,
16918 NULL,
16919 &objfile->objfile_obstack,
16920 hashtab_obstack_allocate,
16921 dummy_obstack_deallocate);
f792889a 16922 }
1c379e20 16923
dee91e82 16924 ofs.per_cu = cu->per_cu;
1c379e20
DJ
16925 ofs.offset = die->offset;
16926 ofs.type = type;
dee91e82
DE
16927 slot = (struct dwarf2_per_cu_offset_and_type **)
16928 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
16929 if (*slot)
16930 complaint (&symfile_complaints,
16931 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 16932 die->offset.sect_off);
673bfd45 16933 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16934 **slot = ofs;
f792889a 16935 return type;
1c379e20
DJ
16936}
16937
380bca97 16938/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 16939 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16940
16941static struct type *
b64f50a1 16942get_die_type_at_offset (sect_offset offset,
673bfd45 16943 struct dwarf2_per_cu_data *per_cu)
1c379e20 16944{
dee91e82 16945 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 16946
dee91e82 16947 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 16948 return NULL;
1c379e20 16949
dee91e82 16950 ofs.per_cu = per_cu;
673bfd45 16951 ofs.offset = offset;
dee91e82 16952 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
16953 if (slot)
16954 return slot->type;
16955 else
16956 return NULL;
16957}
16958
673bfd45
DE
16959/* Look up the type for DIE in the appropriate type_hash table,
16960 or return NULL if DIE does not have a saved type. */
16961
16962static struct type *
16963get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16964{
16965 return get_die_type_at_offset (die->offset, cu->per_cu);
16966}
16967
10b3939b
DJ
16968/* Add a dependence relationship from CU to REF_PER_CU. */
16969
16970static void
16971dwarf2_add_dependence (struct dwarf2_cu *cu,
16972 struct dwarf2_per_cu_data *ref_per_cu)
16973{
16974 void **slot;
16975
16976 if (cu->dependencies == NULL)
16977 cu->dependencies
16978 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16979 NULL, &cu->comp_unit_obstack,
16980 hashtab_obstack_allocate,
16981 dummy_obstack_deallocate);
16982
16983 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16984 if (*slot == NULL)
16985 *slot = ref_per_cu;
16986}
1c379e20 16987
f504f079
DE
16988/* Subroutine of dwarf2_mark to pass to htab_traverse.
16989 Set the mark field in every compilation unit in the
ae038cb0
DJ
16990 cache that we must keep because we are keeping CU. */
16991
10b3939b
DJ
16992static int
16993dwarf2_mark_helper (void **slot, void *data)
16994{
16995 struct dwarf2_per_cu_data *per_cu;
16996
16997 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
16998
16999 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17000 reading of the chain. As such dependencies remain valid it is not much
17001 useful to track and undo them during QUIT cleanups. */
17002 if (per_cu->cu == NULL)
17003 return 1;
17004
10b3939b
DJ
17005 if (per_cu->cu->mark)
17006 return 1;
17007 per_cu->cu->mark = 1;
17008
17009 if (per_cu->cu->dependencies != NULL)
17010 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17011
17012 return 1;
17013}
17014
f504f079
DE
17015/* Set the mark field in CU and in every other compilation unit in the
17016 cache that we must keep because we are keeping CU. */
17017
ae038cb0
DJ
17018static void
17019dwarf2_mark (struct dwarf2_cu *cu)
17020{
17021 if (cu->mark)
17022 return;
17023 cu->mark = 1;
10b3939b
DJ
17024 if (cu->dependencies != NULL)
17025 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
17026}
17027
17028static void
17029dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17030{
17031 while (per_cu)
17032 {
17033 per_cu->cu->mark = 0;
17034 per_cu = per_cu->cu->read_in_chain;
17035 }
72bf9492
DJ
17036}
17037
72bf9492
DJ
17038/* Trivial hash function for partial_die_info: the hash value of a DIE
17039 is its offset in .debug_info for this objfile. */
17040
17041static hashval_t
17042partial_die_hash (const void *item)
17043{
17044 const struct partial_die_info *part_die = item;
9a619af0 17045
b64f50a1 17046 return part_die->offset.sect_off;
72bf9492
DJ
17047}
17048
17049/* Trivial comparison function for partial_die_info structures: two DIEs
17050 are equal if they have the same offset. */
17051
17052static int
17053partial_die_eq (const void *item_lhs, const void *item_rhs)
17054{
17055 const struct partial_die_info *part_die_lhs = item_lhs;
17056 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 17057
b64f50a1 17058 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
17059}
17060
ae038cb0
DJ
17061static struct cmd_list_element *set_dwarf2_cmdlist;
17062static struct cmd_list_element *show_dwarf2_cmdlist;
17063
17064static void
17065set_dwarf2_cmd (char *args, int from_tty)
17066{
17067 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17068}
17069
17070static void
17071show_dwarf2_cmd (char *args, int from_tty)
6e70227d 17072{
ae038cb0
DJ
17073 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17074}
17075
dce234bc
PP
17076/* If section described by INFO was mmapped, munmap it now. */
17077
17078static void
17079munmap_section_buffer (struct dwarf2_section_info *info)
17080{
b315ab21 17081 if (info->map_addr != NULL)
dce234bc
PP
17082 {
17083#ifdef HAVE_MMAP
b315ab21 17084 int res;
9a619af0 17085
b315ab21
TG
17086 res = munmap (info->map_addr, info->map_len);
17087 gdb_assert (res == 0);
dce234bc
PP
17088#else
17089 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 17090 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
17091#endif
17092 }
17093}
17094
17095/* munmap debug sections for OBJFILE, if necessary. */
17096
17097static void
c1bd65d0 17098dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
17099{
17100 struct dwarf2_per_objfile *data = d;
8b70b953
TT
17101 int ix;
17102 struct dwarf2_section_info *section;
9a619af0 17103
16be1145
DE
17104 /* This is sorted according to the order they're defined in to make it easier
17105 to keep in sync. */
dce234bc
PP
17106 munmap_section_buffer (&data->info);
17107 munmap_section_buffer (&data->abbrev);
17108 munmap_section_buffer (&data->line);
16be1145 17109 munmap_section_buffer (&data->loc);
dce234bc 17110 munmap_section_buffer (&data->macinfo);
cf2c3c16 17111 munmap_section_buffer (&data->macro);
16be1145 17112 munmap_section_buffer (&data->str);
dce234bc 17113 munmap_section_buffer (&data->ranges);
3019eac3 17114 munmap_section_buffer (&data->addr);
dce234bc
PP
17115 munmap_section_buffer (&data->frame);
17116 munmap_section_buffer (&data->eh_frame);
9291a0cd 17117 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
17118
17119 for (ix = 0;
17120 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17121 ++ix)
17122 munmap_section_buffer (section);
17123
95554aad
TT
17124 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17125 VEC_free (dwarf2_per_cu_ptr,
17126 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17127
8b70b953 17128 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
17129
17130 if (data->dwo_files)
17131 free_dwo_files (data->dwo_files, objfile);
9291a0cd
TT
17132}
17133
17134\f
ae2de4f8 17135/* The "save gdb-index" command. */
9291a0cd
TT
17136
17137/* The contents of the hash table we create when building the string
17138 table. */
17139struct strtab_entry
17140{
17141 offset_type offset;
17142 const char *str;
17143};
17144
559a7a62
JK
17145/* Hash function for a strtab_entry.
17146
17147 Function is used only during write_hash_table so no index format backward
17148 compatibility is needed. */
b89be57b 17149
9291a0cd
TT
17150static hashval_t
17151hash_strtab_entry (const void *e)
17152{
17153 const struct strtab_entry *entry = e;
559a7a62 17154 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
17155}
17156
17157/* Equality function for a strtab_entry. */
b89be57b 17158
9291a0cd
TT
17159static int
17160eq_strtab_entry (const void *a, const void *b)
17161{
17162 const struct strtab_entry *ea = a;
17163 const struct strtab_entry *eb = b;
17164 return !strcmp (ea->str, eb->str);
17165}
17166
17167/* Create a strtab_entry hash table. */
b89be57b 17168
9291a0cd
TT
17169static htab_t
17170create_strtab (void)
17171{
17172 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17173 xfree, xcalloc, xfree);
17174}
17175
17176/* Add a string to the constant pool. Return the string's offset in
17177 host order. */
b89be57b 17178
9291a0cd
TT
17179static offset_type
17180add_string (htab_t table, struct obstack *cpool, const char *str)
17181{
17182 void **slot;
17183 struct strtab_entry entry;
17184 struct strtab_entry *result;
17185
17186 entry.str = str;
17187 slot = htab_find_slot (table, &entry, INSERT);
17188 if (*slot)
17189 result = *slot;
17190 else
17191 {
17192 result = XNEW (struct strtab_entry);
17193 result->offset = obstack_object_size (cpool);
17194 result->str = str;
17195 obstack_grow_str0 (cpool, str);
17196 *slot = result;
17197 }
17198 return result->offset;
17199}
17200
17201/* An entry in the symbol table. */
17202struct symtab_index_entry
17203{
17204 /* The name of the symbol. */
17205 const char *name;
17206 /* The offset of the name in the constant pool. */
17207 offset_type index_offset;
17208 /* A sorted vector of the indices of all the CUs that hold an object
17209 of this name. */
17210 VEC (offset_type) *cu_indices;
17211};
17212
17213/* The symbol table. This is a power-of-2-sized hash table. */
17214struct mapped_symtab
17215{
17216 offset_type n_elements;
17217 offset_type size;
17218 struct symtab_index_entry **data;
17219};
17220
17221/* Hash function for a symtab_index_entry. */
b89be57b 17222
9291a0cd
TT
17223static hashval_t
17224hash_symtab_entry (const void *e)
17225{
17226 const struct symtab_index_entry *entry = e;
17227 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17228 sizeof (offset_type) * VEC_length (offset_type,
17229 entry->cu_indices),
17230 0);
17231}
17232
17233/* Equality function for a symtab_index_entry. */
b89be57b 17234
9291a0cd
TT
17235static int
17236eq_symtab_entry (const void *a, const void *b)
17237{
17238 const struct symtab_index_entry *ea = a;
17239 const struct symtab_index_entry *eb = b;
17240 int len = VEC_length (offset_type, ea->cu_indices);
17241 if (len != VEC_length (offset_type, eb->cu_indices))
17242 return 0;
17243 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17244 VEC_address (offset_type, eb->cu_indices),
17245 sizeof (offset_type) * len);
17246}
17247
17248/* Destroy a symtab_index_entry. */
b89be57b 17249
9291a0cd
TT
17250static void
17251delete_symtab_entry (void *p)
17252{
17253 struct symtab_index_entry *entry = p;
17254 VEC_free (offset_type, entry->cu_indices);
17255 xfree (entry);
17256}
17257
17258/* Create a hash table holding symtab_index_entry objects. */
b89be57b 17259
9291a0cd 17260static htab_t
3876f04e 17261create_symbol_hash_table (void)
9291a0cd
TT
17262{
17263 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17264 delete_symtab_entry, xcalloc, xfree);
17265}
17266
17267/* Create a new mapped symtab object. */
b89be57b 17268
9291a0cd
TT
17269static struct mapped_symtab *
17270create_mapped_symtab (void)
17271{
17272 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17273 symtab->n_elements = 0;
17274 symtab->size = 1024;
17275 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17276 return symtab;
17277}
17278
17279/* Destroy a mapped_symtab. */
b89be57b 17280
9291a0cd
TT
17281static void
17282cleanup_mapped_symtab (void *p)
17283{
17284 struct mapped_symtab *symtab = p;
17285 /* The contents of the array are freed when the other hash table is
17286 destroyed. */
17287 xfree (symtab->data);
17288 xfree (symtab);
17289}
17290
17291/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
17292 the slot.
17293
17294 Function is used only during write_hash_table so no index format backward
17295 compatibility is needed. */
b89be57b 17296
9291a0cd
TT
17297static struct symtab_index_entry **
17298find_slot (struct mapped_symtab *symtab, const char *name)
17299{
559a7a62 17300 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
17301
17302 index = hash & (symtab->size - 1);
17303 step = ((hash * 17) & (symtab->size - 1)) | 1;
17304
17305 for (;;)
17306 {
17307 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17308 return &symtab->data[index];
17309 index = (index + step) & (symtab->size - 1);
17310 }
17311}
17312
17313/* Expand SYMTAB's hash table. */
b89be57b 17314
9291a0cd
TT
17315static void
17316hash_expand (struct mapped_symtab *symtab)
17317{
17318 offset_type old_size = symtab->size;
17319 offset_type i;
17320 struct symtab_index_entry **old_entries = symtab->data;
17321
17322 symtab->size *= 2;
17323 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17324
17325 for (i = 0; i < old_size; ++i)
17326 {
17327 if (old_entries[i])
17328 {
17329 struct symtab_index_entry **slot = find_slot (symtab,
17330 old_entries[i]->name);
17331 *slot = old_entries[i];
17332 }
17333 }
17334
17335 xfree (old_entries);
17336}
17337
17338/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
17339 is the index of the CU in which the symbol appears. */
b89be57b 17340
9291a0cd
TT
17341static void
17342add_index_entry (struct mapped_symtab *symtab, const char *name,
17343 offset_type cu_index)
17344{
17345 struct symtab_index_entry **slot;
17346
17347 ++symtab->n_elements;
17348 if (4 * symtab->n_elements / 3 >= symtab->size)
17349 hash_expand (symtab);
17350
17351 slot = find_slot (symtab, name);
17352 if (!*slot)
17353 {
17354 *slot = XNEW (struct symtab_index_entry);
17355 (*slot)->name = name;
17356 (*slot)->cu_indices = NULL;
17357 }
17358 /* Don't push an index twice. Due to how we add entries we only
17359 have to check the last one. */
17360 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 17361 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
17362 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
17363}
17364
17365/* Add a vector of indices to the constant pool. */
b89be57b 17366
9291a0cd 17367static offset_type
3876f04e 17368add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
17369 struct symtab_index_entry *entry)
17370{
17371 void **slot;
17372
3876f04e 17373 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
17374 if (!*slot)
17375 {
17376 offset_type len = VEC_length (offset_type, entry->cu_indices);
17377 offset_type val = MAYBE_SWAP (len);
17378 offset_type iter;
17379 int i;
17380
17381 *slot = entry;
17382 entry->index_offset = obstack_object_size (cpool);
17383
17384 obstack_grow (cpool, &val, sizeof (val));
17385 for (i = 0;
17386 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17387 ++i)
17388 {
17389 val = MAYBE_SWAP (iter);
17390 obstack_grow (cpool, &val, sizeof (val));
17391 }
17392 }
17393 else
17394 {
17395 struct symtab_index_entry *old_entry = *slot;
17396 entry->index_offset = old_entry->index_offset;
17397 entry = old_entry;
17398 }
17399 return entry->index_offset;
17400}
17401
17402/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17403 constant pool entries going into the obstack CPOOL. */
b89be57b 17404
9291a0cd
TT
17405static void
17406write_hash_table (struct mapped_symtab *symtab,
17407 struct obstack *output, struct obstack *cpool)
17408{
17409 offset_type i;
3876f04e 17410 htab_t symbol_hash_table;
9291a0cd
TT
17411 htab_t str_table;
17412
3876f04e 17413 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 17414 str_table = create_strtab ();
3876f04e 17415
9291a0cd
TT
17416 /* We add all the index vectors to the constant pool first, to
17417 ensure alignment is ok. */
17418 for (i = 0; i < symtab->size; ++i)
17419 {
17420 if (symtab->data[i])
3876f04e 17421 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
17422 }
17423
17424 /* Now write out the hash table. */
17425 for (i = 0; i < symtab->size; ++i)
17426 {
17427 offset_type str_off, vec_off;
17428
17429 if (symtab->data[i])
17430 {
17431 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17432 vec_off = symtab->data[i]->index_offset;
17433 }
17434 else
17435 {
17436 /* While 0 is a valid constant pool index, it is not valid
17437 to have 0 for both offsets. */
17438 str_off = 0;
17439 vec_off = 0;
17440 }
17441
17442 str_off = MAYBE_SWAP (str_off);
17443 vec_off = MAYBE_SWAP (vec_off);
17444
17445 obstack_grow (output, &str_off, sizeof (str_off));
17446 obstack_grow (output, &vec_off, sizeof (vec_off));
17447 }
17448
17449 htab_delete (str_table);
3876f04e 17450 htab_delete (symbol_hash_table);
9291a0cd
TT
17451}
17452
0a5429f6
DE
17453/* Struct to map psymtab to CU index in the index file. */
17454struct psymtab_cu_index_map
17455{
17456 struct partial_symtab *psymtab;
17457 unsigned int cu_index;
17458};
17459
17460static hashval_t
17461hash_psymtab_cu_index (const void *item)
17462{
17463 const struct psymtab_cu_index_map *map = item;
17464
17465 return htab_hash_pointer (map->psymtab);
17466}
17467
17468static int
17469eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17470{
17471 const struct psymtab_cu_index_map *lhs = item_lhs;
17472 const struct psymtab_cu_index_map *rhs = item_rhs;
17473
17474 return lhs->psymtab == rhs->psymtab;
17475}
17476
17477/* Helper struct for building the address table. */
17478struct addrmap_index_data
17479{
17480 struct objfile *objfile;
17481 struct obstack *addr_obstack;
17482 htab_t cu_index_htab;
17483
17484 /* Non-zero if the previous_* fields are valid.
17485 We can't write an entry until we see the next entry (since it is only then
17486 that we know the end of the entry). */
17487 int previous_valid;
17488 /* Index of the CU in the table of all CUs in the index file. */
17489 unsigned int previous_cu_index;
0963b4bd 17490 /* Start address of the CU. */
0a5429f6
DE
17491 CORE_ADDR previous_cu_start;
17492};
17493
17494/* Write an address entry to OBSTACK. */
b89be57b 17495
9291a0cd 17496static void
0a5429f6
DE
17497add_address_entry (struct objfile *objfile, struct obstack *obstack,
17498 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 17499{
0a5429f6 17500 offset_type cu_index_to_write;
9291a0cd
TT
17501 char addr[8];
17502 CORE_ADDR baseaddr;
17503
17504 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17505
0a5429f6
DE
17506 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17507 obstack_grow (obstack, addr, 8);
17508 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17509 obstack_grow (obstack, addr, 8);
17510 cu_index_to_write = MAYBE_SWAP (cu_index);
17511 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17512}
17513
17514/* Worker function for traversing an addrmap to build the address table. */
17515
17516static int
17517add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17518{
17519 struct addrmap_index_data *data = datap;
17520 struct partial_symtab *pst = obj;
0a5429f6
DE
17521
17522 if (data->previous_valid)
17523 add_address_entry (data->objfile, data->addr_obstack,
17524 data->previous_cu_start, start_addr,
17525 data->previous_cu_index);
17526
17527 data->previous_cu_start = start_addr;
17528 if (pst != NULL)
17529 {
17530 struct psymtab_cu_index_map find_map, *map;
17531 find_map.psymtab = pst;
17532 map = htab_find (data->cu_index_htab, &find_map);
17533 gdb_assert (map != NULL);
17534 data->previous_cu_index = map->cu_index;
17535 data->previous_valid = 1;
17536 }
17537 else
17538 data->previous_valid = 0;
17539
17540 return 0;
17541}
17542
17543/* Write OBJFILE's address map to OBSTACK.
17544 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17545 in the index file. */
17546
17547static void
17548write_address_map (struct objfile *objfile, struct obstack *obstack,
17549 htab_t cu_index_htab)
17550{
17551 struct addrmap_index_data addrmap_index_data;
17552
17553 /* When writing the address table, we have to cope with the fact that
17554 the addrmap iterator only provides the start of a region; we have to
17555 wait until the next invocation to get the start of the next region. */
17556
17557 addrmap_index_data.objfile = objfile;
17558 addrmap_index_data.addr_obstack = obstack;
17559 addrmap_index_data.cu_index_htab = cu_index_htab;
17560 addrmap_index_data.previous_valid = 0;
17561
17562 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17563 &addrmap_index_data);
17564
17565 /* It's highly unlikely the last entry (end address = 0xff...ff)
17566 is valid, but we should still handle it.
17567 The end address is recorded as the start of the next region, but that
17568 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17569 anyway. */
17570 if (addrmap_index_data.previous_valid)
17571 add_address_entry (objfile, obstack,
17572 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17573 addrmap_index_data.previous_cu_index);
9291a0cd
TT
17574}
17575
17576/* Add a list of partial symbols to SYMTAB. */
b89be57b 17577
9291a0cd
TT
17578static void
17579write_psymbols (struct mapped_symtab *symtab,
987d643c 17580 htab_t psyms_seen,
9291a0cd
TT
17581 struct partial_symbol **psymp,
17582 int count,
987d643c
TT
17583 offset_type cu_index,
17584 int is_static)
9291a0cd
TT
17585{
17586 for (; count-- > 0; ++psymp)
17587 {
987d643c
TT
17588 void **slot, *lookup;
17589
9291a0cd
TT
17590 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
17591 error (_("Ada is not currently supported by the index"));
987d643c
TT
17592
17593 /* We only want to add a given psymbol once. However, we also
17594 want to account for whether it is global or static. So, we
17595 may add it twice, using slightly different values. */
17596 if (is_static)
17597 {
17598 uintptr_t val = 1 | (uintptr_t) *psymp;
17599
17600 lookup = (void *) val;
17601 }
17602 else
17603 lookup = *psymp;
17604
17605 /* Only add a given psymbol once. */
17606 slot = htab_find_slot (psyms_seen, lookup, INSERT);
17607 if (!*slot)
17608 {
17609 *slot = lookup;
bb2f58dc 17610 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 17611 }
9291a0cd
TT
17612 }
17613}
17614
17615/* Write the contents of an ("unfinished") obstack to FILE. Throw an
17616 exception if there is an error. */
b89be57b 17617
9291a0cd
TT
17618static void
17619write_obstack (FILE *file, struct obstack *obstack)
17620{
17621 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17622 file)
17623 != obstack_object_size (obstack))
17624 error (_("couldn't data write to file"));
17625}
17626
17627/* Unlink a file if the argument is not NULL. */
b89be57b 17628
9291a0cd
TT
17629static void
17630unlink_if_set (void *p)
17631{
17632 char **filename = p;
17633 if (*filename)
17634 unlink (*filename);
17635}
17636
1fd400ff
TT
17637/* A helper struct used when iterating over debug_types. */
17638struct signatured_type_index_data
17639{
17640 struct objfile *objfile;
17641 struct mapped_symtab *symtab;
17642 struct obstack *types_list;
987d643c 17643 htab_t psyms_seen;
1fd400ff
TT
17644 int cu_index;
17645};
17646
17647/* A helper function that writes a single signatured_type to an
17648 obstack. */
b89be57b 17649
1fd400ff
TT
17650static int
17651write_one_signatured_type (void **slot, void *d)
17652{
17653 struct signatured_type_index_data *info = d;
17654 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
17655 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17656 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
17657 gdb_byte val[8];
17658
17659 write_psymbols (info->symtab,
987d643c 17660 info->psyms_seen,
3e43a32a
MS
17661 info->objfile->global_psymbols.list
17662 + psymtab->globals_offset,
987d643c
TT
17663 psymtab->n_global_syms, info->cu_index,
17664 0);
1fd400ff 17665 write_psymbols (info->symtab,
987d643c 17666 info->psyms_seen,
3e43a32a
MS
17667 info->objfile->static_psymbols.list
17668 + psymtab->statics_offset,
987d643c
TT
17669 psymtab->n_static_syms, info->cu_index,
17670 1);
1fd400ff 17671
b64f50a1
JK
17672 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17673 entry->per_cu.offset.sect_off);
1fd400ff 17674 obstack_grow (info->types_list, val, 8);
3019eac3
DE
17675 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17676 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
17677 obstack_grow (info->types_list, val, 8);
17678 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17679 obstack_grow (info->types_list, val, 8);
17680
17681 ++info->cu_index;
17682
17683 return 1;
17684}
17685
95554aad
TT
17686/* Recurse into all "included" dependencies and write their symbols as
17687 if they appeared in this psymtab. */
17688
17689static void
17690recursively_write_psymbols (struct objfile *objfile,
17691 struct partial_symtab *psymtab,
17692 struct mapped_symtab *symtab,
17693 htab_t psyms_seen,
17694 offset_type cu_index)
17695{
17696 int i;
17697
17698 for (i = 0; i < psymtab->number_of_dependencies; ++i)
17699 if (psymtab->dependencies[i]->user != NULL)
17700 recursively_write_psymbols (objfile, psymtab->dependencies[i],
17701 symtab, psyms_seen, cu_index);
17702
17703 write_psymbols (symtab,
17704 psyms_seen,
17705 objfile->global_psymbols.list + psymtab->globals_offset,
17706 psymtab->n_global_syms, cu_index,
17707 0);
17708 write_psymbols (symtab,
17709 psyms_seen,
17710 objfile->static_psymbols.list + psymtab->statics_offset,
17711 psymtab->n_static_syms, cu_index,
17712 1);
17713}
17714
9291a0cd 17715/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 17716
9291a0cd
TT
17717static void
17718write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17719{
17720 struct cleanup *cleanup;
17721 char *filename, *cleanup_filename;
1fd400ff
TT
17722 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
17723 struct obstack cu_list, types_cu_list;
9291a0cd
TT
17724 int i;
17725 FILE *out_file;
17726 struct mapped_symtab *symtab;
17727 offset_type val, size_of_contents, total_len;
17728 struct stat st;
987d643c 17729 htab_t psyms_seen;
0a5429f6
DE
17730 htab_t cu_index_htab;
17731 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 17732
b4f2f049 17733 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 17734 return;
b4f2f049 17735
9291a0cd
TT
17736 if (dwarf2_per_objfile->using_index)
17737 error (_("Cannot use an index to create the index"));
17738
8b70b953
TT
17739 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17740 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17741
9291a0cd 17742 if (stat (objfile->name, &st) < 0)
7e17e088 17743 perror_with_name (objfile->name);
9291a0cd
TT
17744
17745 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17746 INDEX_SUFFIX, (char *) NULL);
17747 cleanup = make_cleanup (xfree, filename);
17748
17749 out_file = fopen (filename, "wb");
17750 if (!out_file)
17751 error (_("Can't open `%s' for writing"), filename);
17752
17753 cleanup_filename = filename;
17754 make_cleanup (unlink_if_set, &cleanup_filename);
17755
17756 symtab = create_mapped_symtab ();
17757 make_cleanup (cleanup_mapped_symtab, symtab);
17758
17759 obstack_init (&addr_obstack);
17760 make_cleanup_obstack_free (&addr_obstack);
17761
17762 obstack_init (&cu_list);
17763 make_cleanup_obstack_free (&cu_list);
17764
1fd400ff
TT
17765 obstack_init (&types_cu_list);
17766 make_cleanup_obstack_free (&types_cu_list);
17767
987d643c
TT
17768 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17769 NULL, xcalloc, xfree);
96408a79 17770 make_cleanup_htab_delete (psyms_seen);
987d643c 17771
0a5429f6
DE
17772 /* While we're scanning CU's create a table that maps a psymtab pointer
17773 (which is what addrmap records) to its index (which is what is recorded
17774 in the index file). This will later be needed to write the address
17775 table. */
17776 cu_index_htab = htab_create_alloc (100,
17777 hash_psymtab_cu_index,
17778 eq_psymtab_cu_index,
17779 NULL, xcalloc, xfree);
96408a79 17780 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
17781 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17782 xmalloc (sizeof (struct psymtab_cu_index_map)
17783 * dwarf2_per_objfile->n_comp_units);
17784 make_cleanup (xfree, psymtab_cu_index_map);
17785
17786 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
17787 work here. Also, the debug_types entries do not appear in
17788 all_comp_units, but only in their own hash table. */
9291a0cd
TT
17789 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17790 {
3e43a32a
MS
17791 struct dwarf2_per_cu_data *per_cu
17792 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 17793 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 17794 gdb_byte val[8];
0a5429f6
DE
17795 struct psymtab_cu_index_map *map;
17796 void **slot;
9291a0cd 17797
95554aad
TT
17798 if (psymtab->user == NULL)
17799 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 17800
0a5429f6
DE
17801 map = &psymtab_cu_index_map[i];
17802 map->psymtab = psymtab;
17803 map->cu_index = i;
17804 slot = htab_find_slot (cu_index_htab, map, INSERT);
17805 gdb_assert (slot != NULL);
17806 gdb_assert (*slot == NULL);
17807 *slot = map;
9291a0cd 17808
b64f50a1
JK
17809 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17810 per_cu->offset.sect_off);
9291a0cd 17811 obstack_grow (&cu_list, val, 8);
e254ef6a 17812 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
17813 obstack_grow (&cu_list, val, 8);
17814 }
17815
0a5429f6
DE
17816 /* Dump the address map. */
17817 write_address_map (objfile, &addr_obstack, cu_index_htab);
17818
1fd400ff
TT
17819 /* Write out the .debug_type entries, if any. */
17820 if (dwarf2_per_objfile->signatured_types)
17821 {
17822 struct signatured_type_index_data sig_data;
17823
17824 sig_data.objfile = objfile;
17825 sig_data.symtab = symtab;
17826 sig_data.types_list = &types_cu_list;
987d643c 17827 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17828 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17829 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17830 write_one_signatured_type, &sig_data);
17831 }
17832
9291a0cd
TT
17833 obstack_init (&constant_pool);
17834 make_cleanup_obstack_free (&constant_pool);
17835 obstack_init (&symtab_obstack);
17836 make_cleanup_obstack_free (&symtab_obstack);
17837 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17838
17839 obstack_init (&contents);
17840 make_cleanup_obstack_free (&contents);
1fd400ff 17841 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17842 total_len = size_of_contents;
17843
17844 /* The version number. */
481860b3 17845 val = MAYBE_SWAP (6);
9291a0cd
TT
17846 obstack_grow (&contents, &val, sizeof (val));
17847
17848 /* The offset of the CU list from the start of the file. */
17849 val = MAYBE_SWAP (total_len);
17850 obstack_grow (&contents, &val, sizeof (val));
17851 total_len += obstack_object_size (&cu_list);
17852
1fd400ff
TT
17853 /* The offset of the types CU list from the start of the file. */
17854 val = MAYBE_SWAP (total_len);
17855 obstack_grow (&contents, &val, sizeof (val));
17856 total_len += obstack_object_size (&types_cu_list);
17857
9291a0cd
TT
17858 /* The offset of the address table from the start of the file. */
17859 val = MAYBE_SWAP (total_len);
17860 obstack_grow (&contents, &val, sizeof (val));
17861 total_len += obstack_object_size (&addr_obstack);
17862
17863 /* The offset of the symbol table from the start of the file. */
17864 val = MAYBE_SWAP (total_len);
17865 obstack_grow (&contents, &val, sizeof (val));
17866 total_len += obstack_object_size (&symtab_obstack);
17867
17868 /* The offset of the constant pool from the start of the file. */
17869 val = MAYBE_SWAP (total_len);
17870 obstack_grow (&contents, &val, sizeof (val));
17871 total_len += obstack_object_size (&constant_pool);
17872
17873 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17874
17875 write_obstack (out_file, &contents);
17876 write_obstack (out_file, &cu_list);
1fd400ff 17877 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17878 write_obstack (out_file, &addr_obstack);
17879 write_obstack (out_file, &symtab_obstack);
17880 write_obstack (out_file, &constant_pool);
17881
17882 fclose (out_file);
17883
17884 /* We want to keep the file, so we set cleanup_filename to NULL
17885 here. See unlink_if_set. */
17886 cleanup_filename = NULL;
17887
17888 do_cleanups (cleanup);
17889}
17890
90476074
TT
17891/* Implementation of the `save gdb-index' command.
17892
17893 Note that the file format used by this command is documented in the
17894 GDB manual. Any changes here must be documented there. */
11570e71 17895
9291a0cd
TT
17896static void
17897save_gdb_index_command (char *arg, int from_tty)
17898{
17899 struct objfile *objfile;
17900
17901 if (!arg || !*arg)
96d19272 17902 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17903
17904 ALL_OBJFILES (objfile)
17905 {
17906 struct stat st;
17907
17908 /* If the objfile does not correspond to an actual file, skip it. */
17909 if (stat (objfile->name, &st) < 0)
17910 continue;
17911
17912 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17913 if (dwarf2_per_objfile)
17914 {
17915 volatile struct gdb_exception except;
17916
17917 TRY_CATCH (except, RETURN_MASK_ERROR)
17918 {
17919 write_psymtabs_to_index (objfile, arg);
17920 }
17921 if (except.reason < 0)
17922 exception_fprintf (gdb_stderr, except,
17923 _("Error while writing index for `%s': "),
17924 objfile->name);
17925 }
17926 }
dce234bc
PP
17927}
17928
9291a0cd
TT
17929\f
17930
9eae7c52
TT
17931int dwarf2_always_disassemble;
17932
17933static void
17934show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17935 struct cmd_list_element *c, const char *value)
17936{
3e43a32a
MS
17937 fprintf_filtered (file,
17938 _("Whether to always disassemble "
17939 "DWARF expressions is %s.\n"),
9eae7c52
TT
17940 value);
17941}
17942
900e11f9
JK
17943static void
17944show_check_physname (struct ui_file *file, int from_tty,
17945 struct cmd_list_element *c, const char *value)
17946{
17947 fprintf_filtered (file,
17948 _("Whether to check \"physname\" is %s.\n"),
17949 value);
17950}
17951
6502dd73
DJ
17952void _initialize_dwarf2_read (void);
17953
17954void
17955_initialize_dwarf2_read (void)
17956{
96d19272
JK
17957 struct cmd_list_element *c;
17958
dce234bc 17959 dwarf2_objfile_data_key
c1bd65d0 17960 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17961
1bedd215
AC
17962 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17963Set DWARF 2 specific variables.\n\
17964Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17965 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17966 0/*allow-unknown*/, &maintenance_set_cmdlist);
17967
1bedd215
AC
17968 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17969Show DWARF 2 specific variables\n\
17970Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17971 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17972 0/*allow-unknown*/, &maintenance_show_cmdlist);
17973
17974 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
17975 &dwarf2_max_cache_age, _("\
17976Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17977Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17978A higher limit means that cached compilation units will be stored\n\
17979in memory longer, and more total memory will be used. Zero disables\n\
17980caching, which can slow down startup."),
2c5b56ce 17981 NULL,
920d2a44 17982 show_dwarf2_max_cache_age,
2c5b56ce 17983 &set_dwarf2_cmdlist,
ae038cb0 17984 &show_dwarf2_cmdlist);
d97bc12b 17985
9eae7c52
TT
17986 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17987 &dwarf2_always_disassemble, _("\
17988Set whether `info address' always disassembles DWARF expressions."), _("\
17989Show whether `info address' always disassembles DWARF expressions."), _("\
17990When enabled, DWARF expressions are always printed in an assembly-like\n\
17991syntax. When disabled, expressions will be printed in a more\n\
17992conversational style, when possible."),
17993 NULL,
17994 show_dwarf2_always_disassemble,
17995 &set_dwarf2_cmdlist,
17996 &show_dwarf2_cmdlist);
17997
d97bc12b
DE
17998 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17999Set debugging of the dwarf2 DIE reader."), _("\
18000Show debugging of the dwarf2 DIE reader."), _("\
18001When enabled (non-zero), DIEs are dumped after they are read in.\n\
18002The value is the maximum depth to print."),
18003 NULL,
18004 NULL,
18005 &setdebuglist, &showdebuglist);
9291a0cd 18006
900e11f9
JK
18007 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18008Set cross-checking of \"physname\" code against demangler."), _("\
18009Show cross-checking of \"physname\" code against demangler."), _("\
18010When enabled, GDB's internal \"physname\" code is checked against\n\
18011the demangler."),
18012 NULL, show_check_physname,
18013 &setdebuglist, &showdebuglist);
18014
96d19272 18015 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 18016 _("\
fc1a9d6e 18017Save a gdb-index file.\n\
11570e71 18018Usage: save gdb-index DIRECTORY"),
96d19272
JK
18019 &save_cmdlist);
18020 set_cmd_completer (c, filename_completer);
6502dd73 18021}
This page took 2.495668 seconds and 4 git commands to generate.