gdb/testsuite/
[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
034e5797
DE
415 /* Non-NULL if this CU came from a DWO file.
416 There is an invariant here that is important to remember:
417 Except for attributes copied from the top level DIE in the "main"
418 (or "stub") file in preparation for reading the DWO file
419 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
420 Either there isn't a DWO file (in which case this is NULL and the point
421 is moot), or there is and either we're not going to read it (in which
422 case this is NULL) or there is and we are reading it (in which case this
423 is non-NULL). */
3019eac3
DE
424 struct dwo_unit *dwo_unit;
425
426 /* The DW_AT_addr_base attribute if present, zero otherwise
427 (zero is a valid value though).
428 Note this value comes from the stub CU/TU's DIE. */
429 ULONGEST addr_base;
430
ae038cb0
DJ
431 /* Mark used when releasing cached dies. */
432 unsigned int mark : 1;
433
8be455d7
JK
434 /* This CU references .debug_loc. See the symtab->locations_valid field.
435 This test is imperfect as there may exist optimized debug code not using
436 any location list and still facing inlining issues if handled as
437 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 438 unsigned int has_loclist : 1;
ba919b58 439
685b1105
JK
440 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
441 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
442 are valid. This information is cached because profiling CU expansion
443 showed excessive time spent in producer_is_gxx_lt_4_6. */
ba919b58
TT
444 unsigned int checked_producer : 1;
445 unsigned int producer_is_gxx_lt_4_6 : 1;
685b1105 446 unsigned int producer_is_icc : 1;
3019eac3
DE
447
448 /* Non-zero if DW_AT_addr_base was found.
449 Used when processing DWO files. */
450 unsigned int have_addr_base : 1;
e7c27a73
DJ
451};
452
10b3939b
DJ
453/* Persistent data held for a compilation unit, even when not
454 processing it. We put a pointer to this structure in the
28dee7f5 455 read_symtab_private field of the psymtab. */
10b3939b 456
ae038cb0
DJ
457struct dwarf2_per_cu_data
458{
348e048f 459 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 460 bytes should suffice to store the length of any compilation unit
45452591
DE
461 - if it doesn't, GDB will fall over anyway.
462 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
463 initial_length_size.
464 If the DIE refers to a DWO file, this is always of the original die,
465 not the DWO file. */
b64f50a1 466 sect_offset offset;
348e048f 467 unsigned int length : 29;
ae038cb0
DJ
468
469 /* Flag indicating this compilation unit will be read in before
470 any of the current compilation units are processed. */
c764a876 471 unsigned int queued : 1;
ae038cb0 472
0d99eb77
DE
473 /* This flag will be set when reading partial DIEs if we need to load
474 absolutely all DIEs for this compilation unit, instead of just the ones
475 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
476 hash table and don't find it. */
477 unsigned int load_all_dies : 1;
478
3019eac3
DE
479 /* Non-zero if this CU is from .debug_types. */
480 unsigned int is_debug_types : 1;
481
482 /* The section this CU/TU lives in.
483 If the DIE refers to a DWO file, this is always the original die,
484 not the DWO file. */
485 struct dwarf2_section_info *info_or_types_section;
348e048f 486
17ea53c3
JK
487 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
488 of the CU cache it gets reset to NULL again. */
ae038cb0 489 struct dwarf2_cu *cu;
1c379e20 490
9cdd5dbd
DE
491 /* The corresponding objfile.
492 Normally we can get the objfile from dwarf2_per_objfile.
493 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
494 struct objfile *objfile;
495
496 /* When using partial symbol tables, the 'psymtab' field is active.
497 Otherwise the 'quick' field is active. */
498 union
499 {
500 /* The partial symbol table associated with this compilation unit,
95554aad 501 or NULL for unread partial units. */
9291a0cd
TT
502 struct partial_symtab *psymtab;
503
504 /* Data needed by the "quick" functions. */
505 struct dwarf2_per_cu_quick_data *quick;
506 } v;
95554aad
TT
507
508 /* The CUs we import using DW_TAG_imported_unit. This is filled in
509 while reading psymtabs, used to compute the psymtab dependencies,
510 and then cleared. Then it is filled in again while reading full
511 symbols, and only deleted when the objfile is destroyed. */
512 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
513};
514
348e048f
DE
515/* Entry in the signatured_types hash table. */
516
517struct signatured_type
518{
3019eac3 519 /* The type's signature. */
348e048f
DE
520 ULONGEST signature;
521
3019eac3
DE
522 /* Offset in the TU of the type's DIE, as read from the TU header.
523 If the definition lives in a DWO file, this value is unusable. */
524 cu_offset type_offset_in_tu;
525
526 /* Offset in the section of the type's DIE.
527 If the definition lives in a DWO file, this is the offset in the
528 .debug_types.dwo section.
529 The value is zero until the actual value is known.
530 Zero is otherwise not a valid section offset. */
531 sect_offset type_offset_in_section;
348e048f
DE
532
533 /* The CU(/TU) of this type. */
534 struct dwarf2_per_cu_data per_cu;
535};
536
3019eac3
DE
537/* These sections are what may appear in a "dwo" file. */
538
539struct dwo_sections
540{
541 struct dwarf2_section_info abbrev;
542 struct dwarf2_section_info info;
543 struct dwarf2_section_info line;
544 struct dwarf2_section_info loc;
545 struct dwarf2_section_info str;
546 struct dwarf2_section_info str_offsets;
547 VEC (dwarf2_section_info_def) *types;
548};
549
550/* Common bits of DWO CUs/TUs. */
551
552struct dwo_unit
553{
554 /* Backlink to the containing struct dwo_file. */
555 struct dwo_file *dwo_file;
556
557 /* The "id" that distinguishes this CU/TU.
558 .debug_info calls this "dwo_id", .debug_types calls this "signature".
559 Since signatures came first, we stick with it for consistency. */
560 ULONGEST signature;
561
562 /* The section this CU/TU lives in, in the DWO file. */
563 struct dwarf2_section_info *info_or_types_section;
564
565 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
566 sect_offset offset;
567 unsigned int length;
568
569 /* For types, offset in the type's DIE of the type defined by this TU. */
570 cu_offset type_offset_in_tu;
571};
572
573/* Data for one DWO file. */
574
575struct dwo_file
576{
577 /* The DW_AT_GNU_dwo_name attribute.
578 We don't manage space for this, it's an attribute. */
579 const char *dwo_name;
580
581 /* The bfd, when the file is open. Otherwise this is NULL. */
582 bfd *dwo_bfd;
583
584 /* Section info for this file. */
585 struct dwo_sections sections;
586
587 /* Table of CUs in the file.
588 Each element is a struct dwo_unit. */
589 htab_t cus;
590
591 /* Table of TUs in the file.
592 Each element is a struct dwo_unit. */
593 htab_t tus;
594};
595
0963b4bd
MS
596/* Struct used to pass misc. parameters to read_die_and_children, et
597 al. which are used for both .debug_info and .debug_types dies.
598 All parameters here are unchanging for the life of the call. This
dee91e82 599 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
600
601struct die_reader_specs
602{
dee91e82 603 /* die_section->asection->owner. */
93311388
DE
604 bfd* abfd;
605
606 /* The CU of the DIE we are parsing. */
607 struct dwarf2_cu *cu;
608
3019eac3
DE
609 /* Non-NULL if reading a DWO file. */
610 struct dwo_file *dwo_file;
611
dee91e82 612 /* The section the die comes from.
3019eac3 613 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
614 struct dwarf2_section_info *die_section;
615
616 /* die_section->buffer. */
617 gdb_byte *buffer;
f664829e
DE
618
619 /* The end of the buffer. */
620 const gdb_byte *buffer_end;
93311388
DE
621};
622
fd820528 623/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
624typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
625 gdb_byte *info_ptr,
626 struct die_info *comp_unit_die,
627 int has_children,
628 void *data);
629
debd256d
JB
630/* The line number information for a compilation unit (found in the
631 .debug_line section) begins with a "statement program header",
632 which contains the following information. */
633struct line_header
634{
635 unsigned int total_length;
636 unsigned short version;
637 unsigned int header_length;
638 unsigned char minimum_instruction_length;
2dc7f7b3 639 unsigned char maximum_ops_per_instruction;
debd256d
JB
640 unsigned char default_is_stmt;
641 int line_base;
642 unsigned char line_range;
643 unsigned char opcode_base;
644
645 /* standard_opcode_lengths[i] is the number of operands for the
646 standard opcode whose value is i. This means that
647 standard_opcode_lengths[0] is unused, and the last meaningful
648 element is standard_opcode_lengths[opcode_base - 1]. */
649 unsigned char *standard_opcode_lengths;
650
651 /* The include_directories table. NOTE! These strings are not
652 allocated with xmalloc; instead, they are pointers into
653 debug_line_buffer. If you try to free them, `free' will get
654 indigestion. */
655 unsigned int num_include_dirs, include_dirs_size;
656 char **include_dirs;
657
658 /* The file_names table. NOTE! These strings are not allocated
659 with xmalloc; instead, they are pointers into debug_line_buffer.
660 Don't try to free them directly. */
661 unsigned int num_file_names, file_names_size;
662 struct file_entry
c906108c 663 {
debd256d
JB
664 char *name;
665 unsigned int dir_index;
666 unsigned int mod_time;
667 unsigned int length;
aaa75496 668 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 669 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
670 } *file_names;
671
672 /* The start and end of the statement program following this
6502dd73 673 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 674 gdb_byte *statement_program_start, *statement_program_end;
debd256d 675};
c906108c
SS
676
677/* When we construct a partial symbol table entry we only
0963b4bd 678 need this much information. */
c906108c
SS
679struct partial_die_info
680 {
72bf9492 681 /* Offset of this DIE. */
b64f50a1 682 sect_offset offset;
72bf9492
DJ
683
684 /* DWARF-2 tag for this DIE. */
685 ENUM_BITFIELD(dwarf_tag) tag : 16;
686
72bf9492
DJ
687 /* Assorted flags describing the data found in this DIE. */
688 unsigned int has_children : 1;
689 unsigned int is_external : 1;
690 unsigned int is_declaration : 1;
691 unsigned int has_type : 1;
692 unsigned int has_specification : 1;
693 unsigned int has_pc_info : 1;
481860b3 694 unsigned int may_be_inlined : 1;
72bf9492
DJ
695
696 /* Flag set if the SCOPE field of this structure has been
697 computed. */
698 unsigned int scope_set : 1;
699
fa4028e9
JB
700 /* Flag set if the DIE has a byte_size attribute. */
701 unsigned int has_byte_size : 1;
702
98bfdba5
PA
703 /* Flag set if any of the DIE's children are template arguments. */
704 unsigned int has_template_arguments : 1;
705
abc72ce4
DE
706 /* Flag set if fixup_partial_die has been called on this die. */
707 unsigned int fixup_called : 1;
708
72bf9492 709 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 710 sometimes a default name for unnamed DIEs. */
c906108c 711 char *name;
72bf9492 712
abc72ce4
DE
713 /* The linkage name, if present. */
714 const char *linkage_name;
715
72bf9492
DJ
716 /* The scope to prepend to our children. This is generally
717 allocated on the comp_unit_obstack, so will disappear
718 when this compilation unit leaves the cache. */
719 char *scope;
720
95554aad
TT
721 /* Some data associated with the partial DIE. The tag determines
722 which field is live. */
723 union
724 {
725 /* The location description associated with this DIE, if any. */
726 struct dwarf_block *locdesc;
727 /* The offset of an import, for DW_TAG_imported_unit. */
728 sect_offset offset;
729 } d;
72bf9492
DJ
730
731 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
732 CORE_ADDR lowpc;
733 CORE_ADDR highpc;
72bf9492 734
93311388 735 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 736 DW_AT_sibling, if any. */
abc72ce4
DE
737 /* NOTE: This member isn't strictly necessary, read_partial_die could
738 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 739 gdb_byte *sibling;
72bf9492
DJ
740
741 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
742 DW_AT_specification (or DW_AT_abstract_origin or
743 DW_AT_extension). */
b64f50a1 744 sect_offset spec_offset;
72bf9492
DJ
745
746 /* Pointers to this DIE's parent, first child, and next sibling,
747 if any. */
748 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
749 };
750
0963b4bd 751/* This data structure holds the information of an abbrev. */
c906108c
SS
752struct abbrev_info
753 {
754 unsigned int number; /* number identifying abbrev */
755 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
756 unsigned short has_children; /* boolean */
757 unsigned short num_attrs; /* number of attributes */
c906108c
SS
758 struct attr_abbrev *attrs; /* an array of attribute descriptions */
759 struct abbrev_info *next; /* next in chain */
760 };
761
762struct attr_abbrev
763 {
9d25dd43
DE
764 ENUM_BITFIELD(dwarf_attribute) name : 16;
765 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
766 };
767
0963b4bd 768/* Attributes have a name and a value. */
b60c80d6
DJ
769struct attribute
770 {
9d25dd43 771 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
772 ENUM_BITFIELD(dwarf_form) form : 15;
773
774 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
775 field should be in u.str (existing only for DW_STRING) but it is kept
776 here for better struct attribute alignment. */
777 unsigned int string_is_canonical : 1;
778
b60c80d6
DJ
779 union
780 {
781 char *str;
782 struct dwarf_block *blk;
43bbcdc2
PH
783 ULONGEST unsnd;
784 LONGEST snd;
b60c80d6 785 CORE_ADDR addr;
348e048f 786 struct signatured_type *signatured_type;
b60c80d6
DJ
787 }
788 u;
789 };
790
0963b4bd 791/* This data structure holds a complete die structure. */
c906108c
SS
792struct die_info
793 {
76815b17
DE
794 /* DWARF-2 tag for this DIE. */
795 ENUM_BITFIELD(dwarf_tag) tag : 16;
796
797 /* Number of attributes */
98bfdba5
PA
798 unsigned char num_attrs;
799
800 /* True if we're presently building the full type name for the
801 type derived from this DIE. */
802 unsigned char building_fullname : 1;
76815b17
DE
803
804 /* Abbrev number */
805 unsigned int abbrev;
806
93311388 807 /* Offset in .debug_info or .debug_types section. */
b64f50a1 808 sect_offset offset;
78ba4af6
JB
809
810 /* The dies in a compilation unit form an n-ary tree. PARENT
811 points to this die's parent; CHILD points to the first child of
812 this node; and all the children of a given node are chained
4950bc1c 813 together via their SIBLING fields. */
639d11d3
DC
814 struct die_info *child; /* Its first child, if any. */
815 struct die_info *sibling; /* Its next sibling, if any. */
816 struct die_info *parent; /* Its parent, if any. */
c906108c 817
b60c80d6
DJ
818 /* An array of attributes, with NUM_ATTRS elements. There may be
819 zero, but it's not common and zero-sized arrays are not
820 sufficiently portable C. */
821 struct attribute attrs[1];
c906108c
SS
822 };
823
0963b4bd 824/* Get at parts of an attribute structure. */
c906108c
SS
825
826#define DW_STRING(attr) ((attr)->u.str)
8285870a 827#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
828#define DW_UNSND(attr) ((attr)->u.unsnd)
829#define DW_BLOCK(attr) ((attr)->u.blk)
830#define DW_SND(attr) ((attr)->u.snd)
831#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 832#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 833
0963b4bd 834/* Blocks are a bunch of untyped bytes. */
c906108c
SS
835struct dwarf_block
836 {
837 unsigned int size;
1d6edc3c
JK
838
839 /* Valid only if SIZE is not zero. */
fe1b8b76 840 gdb_byte *data;
c906108c
SS
841 };
842
c906108c
SS
843#ifndef ATTR_ALLOC_CHUNK
844#define ATTR_ALLOC_CHUNK 4
845#endif
846
c906108c
SS
847/* Allocate fields for structs, unions and enums in this size. */
848#ifndef DW_FIELD_ALLOC_CHUNK
849#define DW_FIELD_ALLOC_CHUNK 4
850#endif
851
c906108c
SS
852/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
853 but this would require a corresponding change in unpack_field_as_long
854 and friends. */
855static int bits_per_byte = 8;
856
857/* The routines that read and process dies for a C struct or C++ class
858 pass lists of data member fields and lists of member function fields
859 in an instance of a field_info structure, as defined below. */
860struct field_info
c5aa993b 861 {
0963b4bd 862 /* List of data member and baseclasses fields. */
c5aa993b
JM
863 struct nextfield
864 {
865 struct nextfield *next;
866 int accessibility;
867 int virtuality;
868 struct field field;
869 }
7d0ccb61 870 *fields, *baseclasses;
c906108c 871
7d0ccb61 872 /* Number of fields (including baseclasses). */
c5aa993b 873 int nfields;
c906108c 874
c5aa993b
JM
875 /* Number of baseclasses. */
876 int nbaseclasses;
c906108c 877
c5aa993b
JM
878 /* Set if the accesibility of one of the fields is not public. */
879 int non_public_fields;
c906108c 880
c5aa993b
JM
881 /* Member function fields array, entries are allocated in the order they
882 are encountered in the object file. */
883 struct nextfnfield
884 {
885 struct nextfnfield *next;
886 struct fn_field fnfield;
887 }
888 *fnfields;
c906108c 889
c5aa993b
JM
890 /* Member function fieldlist array, contains name of possibly overloaded
891 member function, number of overloaded member functions and a pointer
892 to the head of the member function field chain. */
893 struct fnfieldlist
894 {
895 char *name;
896 int length;
897 struct nextfnfield *head;
898 }
899 *fnfieldlists;
c906108c 900
c5aa993b
JM
901 /* Number of entries in the fnfieldlists array. */
902 int nfnfields;
98751a41
JK
903
904 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
905 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
906 struct typedef_field_list
907 {
908 struct typedef_field field;
909 struct typedef_field_list *next;
910 }
911 *typedef_field_list;
912 unsigned typedef_field_list_count;
c5aa993b 913 };
c906108c 914
10b3939b
DJ
915/* One item on the queue of compilation units to read in full symbols
916 for. */
917struct dwarf2_queue_item
918{
919 struct dwarf2_per_cu_data *per_cu;
95554aad 920 enum language pretend_language;
10b3939b
DJ
921 struct dwarf2_queue_item *next;
922};
923
924/* The current queue. */
925static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
926
ae038cb0
DJ
927/* Loaded secondary compilation units are kept in memory until they
928 have not been referenced for the processing of this many
929 compilation units. Set this to zero to disable caching. Cache
930 sizes of up to at least twenty will improve startup time for
931 typical inter-CU-reference binaries, at an obvious memory cost. */
932static int dwarf2_max_cache_age = 5;
920d2a44
AC
933static void
934show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
935 struct cmd_list_element *c, const char *value)
936{
3e43a32a
MS
937 fprintf_filtered (file, _("The upper bound on the age of cached "
938 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
939 value);
940}
941
ae038cb0 942
0963b4bd 943/* Various complaints about symbol reading that don't abort the process. */
c906108c 944
4d3c2250
KB
945static void
946dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 947{
4d3c2250 948 complaint (&symfile_complaints,
e2e0b3e5 949 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
950}
951
25e43795
DJ
952static void
953dwarf2_debug_line_missing_file_complaint (void)
954{
955 complaint (&symfile_complaints,
956 _(".debug_line section has line data without a file"));
957}
958
59205f5a
JB
959static void
960dwarf2_debug_line_missing_end_sequence_complaint (void)
961{
962 complaint (&symfile_complaints,
3e43a32a
MS
963 _(".debug_line section has line "
964 "program sequence without an end"));
59205f5a
JB
965}
966
4d3c2250
KB
967static void
968dwarf2_complex_location_expr_complaint (void)
2e276125 969{
e2e0b3e5 970 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
971}
972
4d3c2250
KB
973static void
974dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
975 int arg3)
2e276125 976{
4d3c2250 977 complaint (&symfile_complaints,
3e43a32a
MS
978 _("const value length mismatch for '%s', got %d, expected %d"),
979 arg1, arg2, arg3);
4d3c2250
KB
980}
981
982static void
f664829e 983dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 984{
4d3c2250 985 complaint (&symfile_complaints,
f664829e
DE
986 _("debug info runs off end of %s section"
987 " [in module %s]"),
988 section->asection->name,
989 bfd_get_filename (section->asection->owner));
4d3c2250
KB
990}
991
992static void
993dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 994{
4d3c2250 995 complaint (&symfile_complaints,
3e43a32a
MS
996 _("macro debug info contains a "
997 "malformed macro definition:\n`%s'"),
4d3c2250
KB
998 arg1);
999}
1000
1001static void
1002dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1003{
4d3c2250 1004 complaint (&symfile_complaints,
3e43a32a
MS
1005 _("invalid attribute class or form for '%s' in '%s'"),
1006 arg1, arg2);
4d3c2250 1007}
c906108c 1008
c906108c
SS
1009/* local function prototypes */
1010
4efb68b1 1011static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1012
aaa75496
JB
1013static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1014 struct objfile *);
1015
918dd910
JK
1016static void dwarf2_find_base_address (struct die_info *die,
1017 struct dwarf2_cu *cu);
1018
c67a9c90 1019static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1020
72bf9492
DJ
1021static void scan_partial_symbols (struct partial_die_info *,
1022 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1023 int, struct dwarf2_cu *);
c906108c 1024
72bf9492
DJ
1025static void add_partial_symbol (struct partial_die_info *,
1026 struct dwarf2_cu *);
63d06c5c 1027
72bf9492
DJ
1028static void add_partial_namespace (struct partial_die_info *pdi,
1029 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1030 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1031
5d7cb8df
JK
1032static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1033 CORE_ADDR *highpc, int need_pc,
1034 struct dwarf2_cu *cu);
1035
72bf9492
DJ
1036static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1037 struct dwarf2_cu *cu);
91c24f0a 1038
bc30ff58
JB
1039static void add_partial_subprogram (struct partial_die_info *pdi,
1040 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1041 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1042
a14ed312 1043static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 1044
a14ed312 1045static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1046
dee91e82
DE
1047static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1048 struct dwarf2_section_info *);
c906108c 1049
f3dd6933 1050static void dwarf2_free_abbrev_table (void *);
c906108c 1051
6caca83c
CC
1052static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1053
fe1b8b76 1054static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 1055 struct dwarf2_cu *);
72bf9492 1056
57349743 1057static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 1058 struct dwarf2_cu *);
c906108c 1059
dee91e82
DE
1060static struct partial_die_info *load_partial_dies
1061 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1062
dee91e82
DE
1063static gdb_byte *read_partial_die (const struct die_reader_specs *,
1064 struct partial_die_info *,
1065 struct abbrev_info *,
1066 unsigned int,
1067 gdb_byte *);
c906108c 1068
b64f50a1 1069static struct partial_die_info *find_partial_die (sect_offset,
10b3939b 1070 struct dwarf2_cu *);
72bf9492
DJ
1071
1072static void fixup_partial_die (struct partial_die_info *,
1073 struct dwarf2_cu *);
1074
dee91e82
DE
1075static gdb_byte *read_attribute (const struct die_reader_specs *,
1076 struct attribute *, struct attr_abbrev *,
1077 gdb_byte *);
a8329558 1078
fe1b8b76 1079static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 1080
fe1b8b76 1081static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 1082
fe1b8b76 1083static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 1084
fe1b8b76 1085static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 1086
93311388 1087static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 1088
fe1b8b76 1089static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1090 unsigned int *);
c906108c 1091
c764a876
DE
1092static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1093
1094static LONGEST read_checked_initial_length_and_offset
1095 (bfd *, gdb_byte *, const struct comp_unit_head *,
1096 unsigned int *, unsigned int *);
613e1657 1097
fe1b8b76 1098static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1099 unsigned int *);
1100
1101static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1102
fe1b8b76 1103static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1104
9b1c24c8 1105static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1106
fe1b8b76
JB
1107static char *read_indirect_string (bfd *, gdb_byte *,
1108 const struct comp_unit_head *,
1109 unsigned int *);
4bdf3d34 1110
12df843f 1111static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1112
12df843f 1113static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1114
3019eac3
DE
1115static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1116 unsigned int *);
1117
1118static char *read_str_index (const struct die_reader_specs *reader,
1119 struct dwarf2_cu *cu, ULONGEST str_index);
1120
e142c38c 1121static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1122
e142c38c
DJ
1123static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1124 struct dwarf2_cu *);
c906108c 1125
348e048f
DE
1126static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1127 unsigned int,
1128 struct dwarf2_cu *);
1129
05cf31d1
JB
1130static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1131 struct dwarf2_cu *cu);
1132
e142c38c 1133static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1134
e142c38c 1135static struct die_info *die_specification (struct die_info *die,
f2f0e013 1136 struct dwarf2_cu **);
63d06c5c 1137
debd256d
JB
1138static void free_line_header (struct line_header *lh);
1139
aaa75496
JB
1140static void add_file_name (struct line_header *, char *, unsigned int,
1141 unsigned int, unsigned int);
1142
3019eac3
DE
1143static struct line_header *dwarf_decode_line_header (unsigned int offset,
1144 struct dwarf2_cu *cu);
debd256d 1145
f3f5162e
DE
1146static void dwarf_decode_lines (struct line_header *, const char *,
1147 struct dwarf2_cu *, struct partial_symtab *,
1148 int);
c906108c 1149
72b9f47f 1150static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1151
a14ed312 1152static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1153 struct dwarf2_cu *);
c906108c 1154
34eaf542
TT
1155static struct symbol *new_symbol_full (struct die_info *, struct type *,
1156 struct dwarf2_cu *, struct symbol *);
1157
a14ed312 1158static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1159 struct dwarf2_cu *);
c906108c 1160
98bfdba5
PA
1161static void dwarf2_const_value_attr (struct attribute *attr,
1162 struct type *type,
1163 const char *name,
1164 struct obstack *obstack,
12df843f 1165 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1166 gdb_byte **bytes,
1167 struct dwarf2_locexpr_baton **baton);
2df3850c 1168
e7c27a73 1169static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1170
b4ba55a1
JB
1171static int need_gnat_info (struct dwarf2_cu *);
1172
3e43a32a
MS
1173static struct type *die_descriptive_type (struct die_info *,
1174 struct dwarf2_cu *);
b4ba55a1
JB
1175
1176static void set_descriptive_type (struct type *, struct die_info *,
1177 struct dwarf2_cu *);
1178
e7c27a73
DJ
1179static struct type *die_containing_type (struct die_info *,
1180 struct dwarf2_cu *);
c906108c 1181
673bfd45
DE
1182static struct type *lookup_die_type (struct die_info *, struct attribute *,
1183 struct dwarf2_cu *);
c906108c 1184
f792889a 1185static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1186
673bfd45
DE
1187static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1188
0d5cff50 1189static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1190
6e70227d 1191static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1192 const char *suffix, int physname,
1193 struct dwarf2_cu *cu);
63d06c5c 1194
e7c27a73 1195static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1196
348e048f
DE
1197static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1198
e7c27a73 1199static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1200
e7c27a73 1201static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1202
96408a79
SA
1203static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1204
ff013f42
JK
1205static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1206 struct dwarf2_cu *, struct partial_symtab *);
1207
a14ed312 1208static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1209 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1210 struct partial_symtab *);
c906108c 1211
fae299cd
DC
1212static void get_scope_pc_bounds (struct die_info *,
1213 CORE_ADDR *, CORE_ADDR *,
1214 struct dwarf2_cu *);
1215
801e3a5b
JB
1216static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1217 CORE_ADDR, struct dwarf2_cu *);
1218
a14ed312 1219static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1220 struct dwarf2_cu *);
c906108c 1221
a14ed312 1222static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1223 struct type *, struct dwarf2_cu *);
c906108c 1224
a14ed312 1225static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1226 struct die_info *, struct type *,
e7c27a73 1227 struct dwarf2_cu *);
c906108c 1228
a14ed312 1229static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1230 struct type *,
1231 struct dwarf2_cu *);
c906108c 1232
134d01f1 1233static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1234
e7c27a73 1235static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1236
e7c27a73 1237static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1238
5d7cb8df
JK
1239static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1240
27aa8d6a
SW
1241static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1242
f55ee35c
JK
1243static struct type *read_module_type (struct die_info *die,
1244 struct dwarf2_cu *cu);
1245
38d518c9 1246static const char *namespace_name (struct die_info *die,
e142c38c 1247 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1248
134d01f1 1249static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1250
e7c27a73 1251static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1252
6e70227d 1253static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1254 struct dwarf2_cu *);
1255
dee91e82 1256static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1257 gdb_byte *info_ptr,
fe1b8b76 1258 gdb_byte **new_info_ptr,
639d11d3
DC
1259 struct die_info *parent);
1260
dee91e82 1261static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1262 gdb_byte *info_ptr,
fe1b8b76 1263 gdb_byte **new_info_ptr,
639d11d3
DC
1264 struct die_info *parent);
1265
3019eac3
DE
1266static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1267 struct die_info **, gdb_byte *, int *, int);
1268
dee91e82
DE
1269static gdb_byte *read_full_die (const struct die_reader_specs *,
1270 struct die_info **, gdb_byte *, int *);
93311388 1271
e7c27a73 1272static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1273
71c25dea
TT
1274static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1275 struct obstack *);
1276
e142c38c 1277static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1278
98bfdba5
PA
1279static const char *dwarf2_full_name (char *name,
1280 struct die_info *die,
1281 struct dwarf2_cu *cu);
1282
e142c38c 1283static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1284 struct dwarf2_cu **);
9219021c 1285
f39c6ffd 1286static const char *dwarf_tag_name (unsigned int);
c906108c 1287
f39c6ffd 1288static const char *dwarf_attr_name (unsigned int);
c906108c 1289
f39c6ffd 1290static const char *dwarf_form_name (unsigned int);
c906108c 1291
a14ed312 1292static char *dwarf_bool_name (unsigned int);
c906108c 1293
f39c6ffd 1294static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1295
f9aca02d 1296static struct die_info *sibling_die (struct die_info *);
c906108c 1297
d97bc12b
DE
1298static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1299
1300static void dump_die_for_error (struct die_info *);
1301
1302static void dump_die_1 (struct ui_file *, int level, int max_level,
1303 struct die_info *);
c906108c 1304
d97bc12b 1305/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1306
51545339 1307static void store_in_ref_table (struct die_info *,
10b3939b 1308 struct dwarf2_cu *);
c906108c 1309
93311388
DE
1310static int is_ref_attr (struct attribute *);
1311
b64f50a1 1312static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1313
43bbcdc2 1314static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1315
348e048f
DE
1316static struct die_info *follow_die_ref_or_sig (struct die_info *,
1317 struct attribute *,
1318 struct dwarf2_cu **);
1319
10b3939b
DJ
1320static struct die_info *follow_die_ref (struct die_info *,
1321 struct attribute *,
f2f0e013 1322 struct dwarf2_cu **);
c906108c 1323
348e048f
DE
1324static struct die_info *follow_die_sig (struct die_info *,
1325 struct attribute *,
1326 struct dwarf2_cu **);
1327
6c83ed52
TT
1328static struct signatured_type *lookup_signatured_type_at_offset
1329 (struct objfile *objfile,
b64f50a1 1330 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1331
e5fe5e75 1332static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1333
52dc124a 1334static void read_signatured_type (struct signatured_type *);
348e048f 1335
c906108c
SS
1336/* memory allocation interface */
1337
7b5a2f43 1338static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1339
f3dd6933 1340static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1341
b60c80d6 1342static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1343
2e276125 1344static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1345 char *, bfd *, struct dwarf2_cu *,
1346 struct dwarf2_section_info *,
fceca515 1347 int, const char *);
2e276125 1348
8e19ed76
PS
1349static int attr_form_is_block (struct attribute *);
1350
3690dd37
JB
1351static int attr_form_is_section_offset (struct attribute *);
1352
1353static int attr_form_is_constant (struct attribute *);
1354
8cf6f0b1
TT
1355static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1356 struct dwarf2_loclist_baton *baton,
1357 struct attribute *attr);
1358
93e7bd98
DJ
1359static void dwarf2_symbol_mark_computed (struct attribute *attr,
1360 struct symbol *sym,
1361 struct dwarf2_cu *cu);
4c2df51b 1362
dee91e82
DE
1363static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1364 gdb_byte *info_ptr,
1365 struct abbrev_info *abbrev);
4bb7a0a7 1366
72bf9492
DJ
1367static void free_stack_comp_unit (void *);
1368
72bf9492
DJ
1369static hashval_t partial_die_hash (const void *item);
1370
1371static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1372
ae038cb0 1373static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
b64f50a1 1374 (sect_offset offset, struct objfile *objfile);
ae038cb0 1375
9816fde3 1376static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1377 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1378
1379static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1380 struct die_info *comp_unit_die,
1381 enum language pretend_language);
93311388 1382
68dc6402 1383static void free_heap_comp_unit (void *);
ae038cb0
DJ
1384
1385static void free_cached_comp_units (void *);
1386
1387static void age_cached_comp_units (void);
1388
dee91e82 1389static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1390
f792889a
DJ
1391static struct type *set_die_type (struct die_info *, struct type *,
1392 struct dwarf2_cu *);
1c379e20 1393
ae038cb0
DJ
1394static void create_all_comp_units (struct objfile *);
1395
0e50663e 1396static int create_all_type_units (struct objfile *);
1fd400ff 1397
95554aad
TT
1398static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1399 enum language);
10b3939b 1400
95554aad
TT
1401static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1402 enum language);
10b3939b
DJ
1403
1404static void dwarf2_add_dependence (struct dwarf2_cu *,
1405 struct dwarf2_per_cu_data *);
1406
ae038cb0
DJ
1407static void dwarf2_mark (struct dwarf2_cu *);
1408
1409static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1410
b64f50a1 1411static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1412 struct dwarf2_per_cu_data *per_cu);
1413
f792889a 1414static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1415
9291a0cd
TT
1416static void dwarf2_release_queue (void *dummy);
1417
95554aad
TT
1418static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1419 enum language pretend_language);
1420
1421static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1422 struct dwarf2_per_cu_data *per_cu,
1423 enum language pretend_language);
9291a0cd 1424
a0f42c21 1425static void process_queue (void);
9291a0cd
TT
1426
1427static void find_file_and_directory (struct die_info *die,
1428 struct dwarf2_cu *cu,
1429 char **name, char **comp_dir);
1430
1431static char *file_full_name (int file, struct line_header *lh,
1432 const char *comp_dir);
1433
9ff913ba
DE
1434static gdb_byte *read_and_check_comp_unit_head
1435 (struct comp_unit_head *header,
1436 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1437 int is_debug_types_section);
9291a0cd 1438
fd820528
DE
1439static void init_cutu_and_read_dies
1440 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
3019eac3
DE
1441 die_reader_func_ftype *die_reader_func, void *data);
1442
dee91e82
DE
1443static void init_cutu_and_read_dies_simple
1444 (struct dwarf2_per_cu_data *this_cu,
1445 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1446
673bfd45 1447static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1448
95554aad 1449static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *, int);
dee91e82 1450
3019eac3
DE
1451static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1452
1453static struct dwo_unit *lookup_dwo_comp_unit
1454 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1455
1456static struct dwo_unit *lookup_dwo_type_unit
1457 (struct signatured_type *, char *, const char *);
1458
1459static void free_dwo_file_cleanup (void *);
1460
1461static void munmap_section_buffer (struct dwarf2_section_info *);
1462
95554aad
TT
1463static void process_cu_includes (void);
1464
9291a0cd
TT
1465#if WORDS_BIGENDIAN
1466
1467/* Convert VALUE between big- and little-endian. */
1468static offset_type
1469byte_swap (offset_type value)
1470{
1471 offset_type result;
1472
1473 result = (value & 0xff) << 24;
1474 result |= (value & 0xff00) << 8;
1475 result |= (value & 0xff0000) >> 8;
1476 result |= (value & 0xff000000) >> 24;
1477 return result;
1478}
1479
1480#define MAYBE_SWAP(V) byte_swap (V)
1481
1482#else
1483#define MAYBE_SWAP(V) (V)
1484#endif /* WORDS_BIGENDIAN */
1485
1486/* The suffix for an index file. */
1487#define INDEX_SUFFIX ".gdb-index"
1488
3da10d80
KS
1489static const char *dwarf2_physname (char *name, struct die_info *die,
1490 struct dwarf2_cu *cu);
1491
c906108c 1492/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1493 information and return true if we have enough to do something.
1494 NAMES points to the dwarf2 section names, or is NULL if the standard
1495 ELF names are used. */
c906108c
SS
1496
1497int
251d32d9
TG
1498dwarf2_has_info (struct objfile *objfile,
1499 const struct dwarf2_debug_sections *names)
c906108c 1500{
be391dca
TT
1501 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1502 if (!dwarf2_per_objfile)
1503 {
1504 /* Initialize per-objfile state. */
1505 struct dwarf2_per_objfile *data
1506 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1507
be391dca
TT
1508 memset (data, 0, sizeof (*data));
1509 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1510 dwarf2_per_objfile = data;
6502dd73 1511
251d32d9
TG
1512 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1513 (void *) names);
be391dca
TT
1514 dwarf2_per_objfile->objfile = objfile;
1515 }
1516 return (dwarf2_per_objfile->info.asection != NULL
1517 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1518}
1519
251d32d9
TG
1520/* When loading sections, we look either for uncompressed section or for
1521 compressed section names. */
233a11ab
CS
1522
1523static int
251d32d9
TG
1524section_is_p (const char *section_name,
1525 const struct dwarf2_section_names *names)
233a11ab 1526{
251d32d9
TG
1527 if (names->normal != NULL
1528 && strcmp (section_name, names->normal) == 0)
1529 return 1;
1530 if (names->compressed != NULL
1531 && strcmp (section_name, names->compressed) == 0)
1532 return 1;
1533 return 0;
233a11ab
CS
1534}
1535
c906108c
SS
1536/* This function is mapped across the sections and remembers the
1537 offset and size of each of the debugging sections we are interested
1538 in. */
1539
1540static void
251d32d9 1541dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1542{
251d32d9
TG
1543 const struct dwarf2_debug_sections *names;
1544
1545 if (vnames == NULL)
1546 names = &dwarf2_elf_names;
1547 else
1548 names = (const struct dwarf2_debug_sections *) vnames;
1549
1550 if (section_is_p (sectp->name, &names->info))
c906108c 1551 {
dce234bc
PP
1552 dwarf2_per_objfile->info.asection = sectp;
1553 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1554 }
251d32d9 1555 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1556 {
dce234bc
PP
1557 dwarf2_per_objfile->abbrev.asection = sectp;
1558 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1559 }
251d32d9 1560 else if (section_is_p (sectp->name, &names->line))
c906108c 1561 {
dce234bc
PP
1562 dwarf2_per_objfile->line.asection = sectp;
1563 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1564 }
251d32d9 1565 else if (section_is_p (sectp->name, &names->loc))
c906108c 1566 {
dce234bc
PP
1567 dwarf2_per_objfile->loc.asection = sectp;
1568 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1569 }
251d32d9 1570 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1571 {
dce234bc
PP
1572 dwarf2_per_objfile->macinfo.asection = sectp;
1573 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1574 }
cf2c3c16
TT
1575 else if (section_is_p (sectp->name, &names->macro))
1576 {
1577 dwarf2_per_objfile->macro.asection = sectp;
1578 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1579 }
251d32d9 1580 else if (section_is_p (sectp->name, &names->str))
c906108c 1581 {
dce234bc
PP
1582 dwarf2_per_objfile->str.asection = sectp;
1583 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1584 }
3019eac3
DE
1585 else if (section_is_p (sectp->name, &names->addr))
1586 {
1587 dwarf2_per_objfile->addr.asection = sectp;
1588 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1589 }
251d32d9 1590 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1591 {
dce234bc
PP
1592 dwarf2_per_objfile->frame.asection = sectp;
1593 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1594 }
251d32d9 1595 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1596 {
81b9b86e 1597 flagword aflag = bfd_get_section_flags (abfd, sectp);
9a619af0 1598
3799ccc6
EZ
1599 if (aflag & SEC_HAS_CONTENTS)
1600 {
dce234bc
PP
1601 dwarf2_per_objfile->eh_frame.asection = sectp;
1602 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1603 }
b6af0555 1604 }
251d32d9 1605 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1606 {
dce234bc
PP
1607 dwarf2_per_objfile->ranges.asection = sectp;
1608 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1609 }
251d32d9 1610 else if (section_is_p (sectp->name, &names->types))
348e048f 1611 {
8b70b953
TT
1612 struct dwarf2_section_info type_section;
1613
1614 memset (&type_section, 0, sizeof (type_section));
1615 type_section.asection = sectp;
1616 type_section.size = bfd_get_section_size (sectp);
1617
1618 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1619 &type_section);
348e048f 1620 }
251d32d9 1621 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1622 {
1623 dwarf2_per_objfile->gdb_index.asection = sectp;
1624 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1625 }
dce234bc 1626
72dca2f5
FR
1627 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1628 && bfd_section_vma (abfd, sectp) == 0)
1629 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1630}
1631
dce234bc
PP
1632/* Decompress a section that was compressed using zlib. Store the
1633 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1634
1635static void
dce234bc
PP
1636zlib_decompress_section (struct objfile *objfile, asection *sectp,
1637 gdb_byte **outbuf, bfd_size_type *outsize)
1638{
3019eac3 1639 bfd *abfd = sectp->owner;
dce234bc
PP
1640#ifndef HAVE_ZLIB_H
1641 error (_("Support for zlib-compressed DWARF data (from '%s') "
1642 "is disabled in this copy of GDB"),
1643 bfd_get_filename (abfd));
1644#else
1645 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1646 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1647 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1648 bfd_size_type uncompressed_size;
1649 gdb_byte *uncompressed_buffer;
1650 z_stream strm;
1651 int rc;
1652 int header_size = 12;
1653
1654 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1655 || bfd_bread (compressed_buffer,
1656 compressed_size, abfd) != compressed_size)
dce234bc
PP
1657 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1658 bfd_get_filename (abfd));
1659
1660 /* Read the zlib header. In this case, it should be "ZLIB" followed
1661 by the uncompressed section size, 8 bytes in big-endian order. */
1662 if (compressed_size < header_size
1663 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1664 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1665 bfd_get_filename (abfd));
1666 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1667 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1668 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1669 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1670 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1671 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1672 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1673 uncompressed_size += compressed_buffer[11];
1674
1675 /* It is possible the section consists of several compressed
1676 buffers concatenated together, so we uncompress in a loop. */
1677 strm.zalloc = NULL;
1678 strm.zfree = NULL;
1679 strm.opaque = NULL;
1680 strm.avail_in = compressed_size - header_size;
1681 strm.next_in = (Bytef*) compressed_buffer + header_size;
1682 strm.avail_out = uncompressed_size;
1683 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1684 uncompressed_size);
1685 rc = inflateInit (&strm);
1686 while (strm.avail_in > 0)
1687 {
1688 if (rc != Z_OK)
1689 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1690 bfd_get_filename (abfd), rc);
1691 strm.next_out = ((Bytef*) uncompressed_buffer
1692 + (uncompressed_size - strm.avail_out));
1693 rc = inflate (&strm, Z_FINISH);
1694 if (rc != Z_STREAM_END)
1695 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1696 bfd_get_filename (abfd), rc);
1697 rc = inflateReset (&strm);
1698 }
1699 rc = inflateEnd (&strm);
1700 if (rc != Z_OK
1701 || strm.avail_out != 0)
1702 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1703 bfd_get_filename (abfd), rc);
1704
affddf13 1705 do_cleanups (cleanup);
dce234bc
PP
1706 *outbuf = uncompressed_buffer;
1707 *outsize = uncompressed_size;
1708#endif
233a11ab
CS
1709}
1710
fceca515
DE
1711/* A helper function that decides whether a section is empty,
1712 or not present. */
9e0ac564
TT
1713
1714static int
1715dwarf2_section_empty_p (struct dwarf2_section_info *info)
1716{
1717 return info->asection == NULL || info->size == 0;
1718}
1719
3019eac3
DE
1720/* Read the contents of the section INFO.
1721 OBJFILE is the main object file, but not necessarily the file where
1722 the section comes from. E.g., for DWO files INFO->asection->owner
1723 is the bfd of the DWO file.
dce234bc 1724 If the section is compressed, uncompress it before returning. */
c906108c 1725
dce234bc
PP
1726static void
1727dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1728{
dce234bc 1729 asection *sectp = info->asection;
3019eac3 1730 bfd *abfd;
dce234bc
PP
1731 gdb_byte *buf, *retbuf;
1732 unsigned char header[4];
c906108c 1733
be391dca
TT
1734 if (info->readin)
1735 return;
dce234bc 1736 info->buffer = NULL;
b315ab21 1737 info->map_addr = NULL;
be391dca 1738 info->readin = 1;
188dd5d6 1739
9e0ac564 1740 if (dwarf2_section_empty_p (info))
dce234bc 1741 return;
c906108c 1742
3019eac3
DE
1743 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1744 abfd = sectp->owner;
1745
dce234bc
PP
1746 /* Check if the file has a 4-byte header indicating compression. */
1747 if (info->size > sizeof (header)
1748 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1749 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1750 {
1751 /* Upon decompression, update the buffer and its size. */
1752 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1753 {
1754 zlib_decompress_section (objfile, sectp, &info->buffer,
1755 &info->size);
1756 return;
1757 }
1758 }
4bdf3d34 1759
dce234bc
PP
1760#ifdef HAVE_MMAP
1761 if (pagesize == 0)
1762 pagesize = getpagesize ();
2e276125 1763
dce234bc
PP
1764 /* Only try to mmap sections which are large enough: we don't want to
1765 waste space due to fragmentation. Also, only try mmap for sections
1766 without relocations. */
1767
1768 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1769 {
b315ab21
TG
1770 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1771 MAP_PRIVATE, sectp->filepos,
1772 &info->map_addr, &info->map_len);
dce234bc 1773
b315ab21 1774 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1775 {
be391dca 1776#if HAVE_POSIX_MADVISE
b315ab21 1777 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1778#endif
dce234bc
PP
1779 return;
1780 }
1781 }
1782#endif
1783
1784 /* If we get here, we are a normal, not-compressed section. */
1785 info->buffer = buf
1786 = obstack_alloc (&objfile->objfile_obstack, info->size);
1787
1788 /* When debugging .o files, we may need to apply relocations; see
1789 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1790 We never compress sections in .o files, so we only need to
1791 try this when the section is not compressed. */
ac8035ab 1792 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1793 if (retbuf != NULL)
1794 {
1795 info->buffer = retbuf;
1796 return;
1797 }
1798
1799 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1800 || bfd_bread (buf, info->size, abfd) != info->size)
1801 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1802 bfd_get_filename (abfd));
1803}
1804
9e0ac564
TT
1805/* A helper function that returns the size of a section in a safe way.
1806 If you are positive that the section has been read before using the
1807 size, then it is safe to refer to the dwarf2_section_info object's
1808 "size" field directly. In other cases, you must call this
1809 function, because for compressed sections the size field is not set
1810 correctly until the section has been read. */
1811
1812static bfd_size_type
1813dwarf2_section_size (struct objfile *objfile,
1814 struct dwarf2_section_info *info)
1815{
1816 if (!info->readin)
1817 dwarf2_read_section (objfile, info);
1818 return info->size;
1819}
1820
dce234bc 1821/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1822 SECTION_NAME. */
af34e669 1823
dce234bc 1824void
3017a003
TG
1825dwarf2_get_section_info (struct objfile *objfile,
1826 enum dwarf2_section_enum sect,
dce234bc
PP
1827 asection **sectp, gdb_byte **bufp,
1828 bfd_size_type *sizep)
1829{
1830 struct dwarf2_per_objfile *data
1831 = objfile_data (objfile, dwarf2_objfile_data_key);
1832 struct dwarf2_section_info *info;
a3b2a86b
TT
1833
1834 /* We may see an objfile without any DWARF, in which case we just
1835 return nothing. */
1836 if (data == NULL)
1837 {
1838 *sectp = NULL;
1839 *bufp = NULL;
1840 *sizep = 0;
1841 return;
1842 }
3017a003
TG
1843 switch (sect)
1844 {
1845 case DWARF2_DEBUG_FRAME:
1846 info = &data->frame;
1847 break;
1848 case DWARF2_EH_FRAME:
1849 info = &data->eh_frame;
1850 break;
1851 default:
1852 gdb_assert_not_reached ("unexpected section");
1853 }
dce234bc 1854
9e0ac564 1855 dwarf2_read_section (objfile, info);
dce234bc
PP
1856
1857 *sectp = info->asection;
1858 *bufp = info->buffer;
1859 *sizep = info->size;
1860}
1861
9291a0cd 1862\f
7b9f3c50
DE
1863/* DWARF quick_symbols_functions support. */
1864
1865/* TUs can share .debug_line entries, and there can be a lot more TUs than
1866 unique line tables, so we maintain a separate table of all .debug_line
1867 derived entries to support the sharing.
1868 All the quick functions need is the list of file names. We discard the
1869 line_header when we're done and don't need to record it here. */
1870struct quick_file_names
1871{
1872 /* The offset in .debug_line of the line table. We hash on this. */
1873 unsigned int offset;
1874
1875 /* The number of entries in file_names, real_names. */
1876 unsigned int num_file_names;
1877
1878 /* The file names from the line table, after being run through
1879 file_full_name. */
1880 const char **file_names;
1881
1882 /* The file names from the line table after being run through
1883 gdb_realpath. These are computed lazily. */
1884 const char **real_names;
1885};
1886
1887/* When using the index (and thus not using psymtabs), each CU has an
1888 object of this type. This is used to hold information needed by
1889 the various "quick" methods. */
1890struct dwarf2_per_cu_quick_data
1891{
1892 /* The file table. This can be NULL if there was no file table
1893 or it's currently not read in.
1894 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1895 struct quick_file_names *file_names;
1896
1897 /* The corresponding symbol table. This is NULL if symbols for this
1898 CU have not yet been read. */
1899 struct symtab *symtab;
1900
1901 /* A temporary mark bit used when iterating over all CUs in
1902 expand_symtabs_matching. */
1903 unsigned int mark : 1;
1904
1905 /* True if we've tried to read the file table and found there isn't one.
1906 There will be no point in trying to read it again next time. */
1907 unsigned int no_file_data : 1;
1908};
1909
1910/* Hash function for a quick_file_names. */
1911
1912static hashval_t
1913hash_file_name_entry (const void *e)
1914{
1915 const struct quick_file_names *file_data = e;
1916
1917 return file_data->offset;
1918}
1919
1920/* Equality function for a quick_file_names. */
1921
1922static int
1923eq_file_name_entry (const void *a, const void *b)
1924{
1925 const struct quick_file_names *ea = a;
1926 const struct quick_file_names *eb = b;
1927
1928 return ea->offset == eb->offset;
1929}
1930
1931/* Delete function for a quick_file_names. */
1932
1933static void
1934delete_file_name_entry (void *e)
1935{
1936 struct quick_file_names *file_data = e;
1937 int i;
1938
1939 for (i = 0; i < file_data->num_file_names; ++i)
1940 {
1941 xfree ((void*) file_data->file_names[i]);
1942 if (file_data->real_names)
1943 xfree ((void*) file_data->real_names[i]);
1944 }
1945
1946 /* The space for the struct itself lives on objfile_obstack,
1947 so we don't free it here. */
1948}
1949
1950/* Create a quick_file_names hash table. */
1951
1952static htab_t
1953create_quick_file_names_table (unsigned int nr_initial_entries)
1954{
1955 return htab_create_alloc (nr_initial_entries,
1956 hash_file_name_entry, eq_file_name_entry,
1957 delete_file_name_entry, xcalloc, xfree);
1958}
9291a0cd 1959
918dd910
JK
1960/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1961 have to be created afterwards. You should call age_cached_comp_units after
1962 processing PER_CU->CU. dw2_setup must have been already called. */
1963
1964static void
1965load_cu (struct dwarf2_per_cu_data *per_cu)
1966{
3019eac3 1967 if (per_cu->is_debug_types)
e5fe5e75 1968 load_full_type_unit (per_cu);
918dd910 1969 else
95554aad 1970 load_full_comp_unit (per_cu, language_minimal);
918dd910 1971
918dd910 1972 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
1973
1974 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
1975}
1976
a0f42c21 1977/* Read in the symbols for PER_CU. */
2fdf6df6 1978
9291a0cd 1979static void
a0f42c21 1980dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1981{
1982 struct cleanup *back_to;
1983
1984 back_to = make_cleanup (dwarf2_release_queue, NULL);
1985
95554aad
TT
1986 if (dwarf2_per_objfile->using_index
1987 ? per_cu->v.quick->symtab == NULL
1988 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
1989 {
1990 queue_comp_unit (per_cu, language_minimal);
1991 load_cu (per_cu);
1992 }
9291a0cd 1993
a0f42c21 1994 process_queue ();
9291a0cd
TT
1995
1996 /* Age the cache, releasing compilation units that have not
1997 been used recently. */
1998 age_cached_comp_units ();
1999
2000 do_cleanups (back_to);
2001}
2002
2003/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2004 the objfile from which this CU came. Returns the resulting symbol
2005 table. */
2fdf6df6 2006
9291a0cd 2007static struct symtab *
a0f42c21 2008dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2009{
95554aad 2010 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2011 if (!per_cu->v.quick->symtab)
2012 {
2013 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2014 increment_reading_symtab ();
a0f42c21 2015 dw2_do_instantiate_symtab (per_cu);
95554aad 2016 process_cu_includes ();
9291a0cd
TT
2017 do_cleanups (back_to);
2018 }
2019 return per_cu->v.quick->symtab;
2020}
2021
1fd400ff 2022/* Return the CU given its index. */
2fdf6df6 2023
1fd400ff
TT
2024static struct dwarf2_per_cu_data *
2025dw2_get_cu (int index)
2026{
2027 if (index >= dwarf2_per_objfile->n_comp_units)
2028 {
2029 index -= dwarf2_per_objfile->n_comp_units;
d467dd73 2030 return dwarf2_per_objfile->all_type_units[index];
1fd400ff
TT
2031 }
2032 return dwarf2_per_objfile->all_comp_units[index];
2033}
2034
9291a0cd
TT
2035/* A helper function that knows how to read a 64-bit value in a way
2036 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2037 otherwise. */
2fdf6df6 2038
9291a0cd
TT
2039static int
2040extract_cu_value (const char *bytes, ULONGEST *result)
2041{
2042 if (sizeof (ULONGEST) < 8)
2043 {
2044 int i;
2045
2046 /* Ignore the upper 4 bytes if they are all zero. */
2047 for (i = 0; i < 4; ++i)
2048 if (bytes[i + 4] != 0)
2049 return 0;
2050
2051 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2052 }
2053 else
2054 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2055 return 1;
2056}
2057
2058/* Read the CU list from the mapped index, and use it to create all
2059 the CU objects for this objfile. Return 0 if something went wrong,
2060 1 if everything went ok. */
2fdf6df6 2061
9291a0cd 2062static int
1fd400ff
TT
2063create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2064 offset_type cu_list_elements)
9291a0cd
TT
2065{
2066 offset_type i;
9291a0cd
TT
2067
2068 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2069 dwarf2_per_objfile->all_comp_units
2070 = obstack_alloc (&objfile->objfile_obstack,
2071 dwarf2_per_objfile->n_comp_units
2072 * sizeof (struct dwarf2_per_cu_data *));
2073
2074 for (i = 0; i < cu_list_elements; i += 2)
2075 {
2076 struct dwarf2_per_cu_data *the_cu;
2077 ULONGEST offset, length;
2078
2079 if (!extract_cu_value (cu_list, &offset)
2080 || !extract_cu_value (cu_list + 8, &length))
2081 return 0;
2082 cu_list += 2 * 8;
2083
2084 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2085 struct dwarf2_per_cu_data);
b64f50a1 2086 the_cu->offset.sect_off = offset;
9291a0cd
TT
2087 the_cu->length = length;
2088 the_cu->objfile = objfile;
3019eac3 2089 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
9291a0cd
TT
2090 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2091 struct dwarf2_per_cu_quick_data);
2092 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2093 }
2094
2095 return 1;
2096}
2097
1fd400ff 2098/* Create the signatured type hash table from the index. */
673bfd45 2099
1fd400ff 2100static int
673bfd45 2101create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2102 struct dwarf2_section_info *section,
673bfd45
DE
2103 const gdb_byte *bytes,
2104 offset_type elements)
1fd400ff
TT
2105{
2106 offset_type i;
673bfd45 2107 htab_t sig_types_hash;
1fd400ff 2108
d467dd73
DE
2109 dwarf2_per_objfile->n_type_units = elements / 3;
2110 dwarf2_per_objfile->all_type_units
1fd400ff 2111 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2112 dwarf2_per_objfile->n_type_units
1fd400ff
TT
2113 * sizeof (struct dwarf2_per_cu_data *));
2114
673bfd45 2115 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2116
2117 for (i = 0; i < elements; i += 3)
2118 {
52dc124a
DE
2119 struct signatured_type *sig_type;
2120 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2121 void **slot;
2122
2123 if (!extract_cu_value (bytes, &offset)
52dc124a 2124 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
2125 return 0;
2126 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2127 bytes += 3 * 8;
2128
52dc124a 2129 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2130 struct signatured_type);
52dc124a 2131 sig_type->signature = signature;
3019eac3
DE
2132 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2133 sig_type->per_cu.is_debug_types = 1;
2134 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2135 sig_type->per_cu.offset.sect_off = offset;
2136 sig_type->per_cu.objfile = objfile;
2137 sig_type->per_cu.v.quick
1fd400ff
TT
2138 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2139 struct dwarf2_per_cu_quick_data);
2140
52dc124a
DE
2141 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2142 *slot = sig_type;
1fd400ff 2143
52dc124a 2144 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
1fd400ff
TT
2145 }
2146
673bfd45 2147 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2148
2149 return 1;
2150}
2151
9291a0cd
TT
2152/* Read the address map data from the mapped index, and use it to
2153 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2154
9291a0cd
TT
2155static void
2156create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2157{
2158 const gdb_byte *iter, *end;
2159 struct obstack temp_obstack;
2160 struct addrmap *mutable_map;
2161 struct cleanup *cleanup;
2162 CORE_ADDR baseaddr;
2163
2164 obstack_init (&temp_obstack);
2165 cleanup = make_cleanup_obstack_free (&temp_obstack);
2166 mutable_map = addrmap_create_mutable (&temp_obstack);
2167
2168 iter = index->address_table;
2169 end = iter + index->address_table_size;
2170
2171 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2172
2173 while (iter < end)
2174 {
2175 ULONGEST hi, lo, cu_index;
2176 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2177 iter += 8;
2178 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2179 iter += 8;
2180 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2181 iter += 4;
2182
2183 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2184 dw2_get_cu (cu_index));
9291a0cd
TT
2185 }
2186
2187 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2188 &objfile->objfile_obstack);
2189 do_cleanups (cleanup);
2190}
2191
59d7bcaf
JK
2192/* The hash function for strings in the mapped index. This is the same as
2193 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2194 implementation. This is necessary because the hash function is tied to the
2195 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2196 SYMBOL_HASH_NEXT.
2197
2198 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2199
9291a0cd 2200static hashval_t
559a7a62 2201mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2202{
2203 const unsigned char *str = (const unsigned char *) p;
2204 hashval_t r = 0;
2205 unsigned char c;
2206
2207 while ((c = *str++) != 0)
559a7a62
JK
2208 {
2209 if (index_version >= 5)
2210 c = tolower (c);
2211 r = r * 67 + c - 113;
2212 }
9291a0cd
TT
2213
2214 return r;
2215}
2216
2217/* Find a slot in the mapped index INDEX for the object named NAME.
2218 If NAME is found, set *VEC_OUT to point to the CU vector in the
2219 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2220
9291a0cd
TT
2221static int
2222find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2223 offset_type **vec_out)
2224{
0cf03b49
JK
2225 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2226 offset_type hash;
9291a0cd 2227 offset_type slot, step;
559a7a62 2228 int (*cmp) (const char *, const char *);
9291a0cd 2229
0cf03b49
JK
2230 if (current_language->la_language == language_cplus
2231 || current_language->la_language == language_java
2232 || current_language->la_language == language_fortran)
2233 {
2234 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2235 not contain any. */
2236 const char *paren = strchr (name, '(');
2237
2238 if (paren)
2239 {
2240 char *dup;
2241
2242 dup = xmalloc (paren - name + 1);
2243 memcpy (dup, name, paren - name);
2244 dup[paren - name] = 0;
2245
2246 make_cleanup (xfree, dup);
2247 name = dup;
2248 }
2249 }
2250
559a7a62 2251 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2252 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2253 simulate our NAME being searched is also lowercased. */
2254 hash = mapped_index_string_hash ((index->version == 4
2255 && case_sensitivity == case_sensitive_off
2256 ? 5 : index->version),
2257 name);
2258
3876f04e
DE
2259 slot = hash & (index->symbol_table_slots - 1);
2260 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2261 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2262
2263 for (;;)
2264 {
2265 /* Convert a slot number to an offset into the table. */
2266 offset_type i = 2 * slot;
2267 const char *str;
3876f04e 2268 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2269 {
2270 do_cleanups (back_to);
2271 return 0;
2272 }
9291a0cd 2273
3876f04e 2274 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2275 if (!cmp (name, str))
9291a0cd
TT
2276 {
2277 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2278 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2279 do_cleanups (back_to);
9291a0cd
TT
2280 return 1;
2281 }
2282
3876f04e 2283 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2284 }
2285}
2286
2287/* Read the index file. If everything went ok, initialize the "quick"
2288 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2289
9291a0cd
TT
2290static int
2291dwarf2_read_index (struct objfile *objfile)
2292{
9291a0cd
TT
2293 char *addr;
2294 struct mapped_index *map;
b3b272e1 2295 offset_type *metadata;
ac0b195c
KW
2296 const gdb_byte *cu_list;
2297 const gdb_byte *types_list = NULL;
2298 offset_type version, cu_list_elements;
2299 offset_type types_list_elements = 0;
1fd400ff 2300 int i;
9291a0cd 2301
9e0ac564 2302 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2303 return 0;
82430852
JK
2304
2305 /* Older elfutils strip versions could keep the section in the main
2306 executable while splitting it for the separate debug info file. */
2307 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2308 & SEC_HAS_CONTENTS) == 0)
2309 return 0;
2310
9291a0cd
TT
2311 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2312
2313 addr = dwarf2_per_objfile->gdb_index.buffer;
2314 /* Version check. */
1fd400ff 2315 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2316 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2317 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2318 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2319 indices. */
831adc1f 2320 if (version < 4)
481860b3
GB
2321 {
2322 static int warning_printed = 0;
2323 if (!warning_printed)
2324 {
2325 warning (_("Skipping obsolete .gdb_index section in %s."),
2326 objfile->name);
2327 warning_printed = 1;
2328 }
2329 return 0;
2330 }
2331 /* Index version 4 uses a different hash function than index version
2332 5 and later.
2333
2334 Versions earlier than 6 did not emit psymbols for inlined
2335 functions. Using these files will cause GDB not to be able to
2336 set breakpoints on inlined functions by name, so we ignore these
2337 indices unless the --use-deprecated-index-sections command line
2338 option was supplied. */
2339 if (version < 6 && !use_deprecated_index_sections)
2340 {
2341 static int warning_printed = 0;
2342 if (!warning_printed)
2343 {
2344 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2345 "--use-deprecated-index-sections to use them anyway"),
2346 objfile->name);
2347 warning_printed = 1;
2348 }
2349 return 0;
2350 }
2351 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2352 longer backward compatible. */
481860b3 2353 if (version > 6)
594e8718 2354 return 0;
9291a0cd
TT
2355
2356 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2357 map->version = version;
b3b272e1 2358 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2359
2360 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2361
2362 i = 0;
2363 cu_list = addr + MAYBE_SWAP (metadata[i]);
2364 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2365 / 8);
1fd400ff
TT
2366 ++i;
2367
987d643c
TT
2368 types_list = addr + MAYBE_SWAP (metadata[i]);
2369 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2370 - MAYBE_SWAP (metadata[i]))
2371 / 8);
2372 ++i;
1fd400ff
TT
2373
2374 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2375 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2376 - MAYBE_SWAP (metadata[i]));
2377 ++i;
2378
3876f04e
DE
2379 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2380 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2381 - MAYBE_SWAP (metadata[i]))
2382 / (2 * sizeof (offset_type)));
1fd400ff 2383 ++i;
9291a0cd 2384
1fd400ff
TT
2385 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2386
0fefef59
DE
2387 /* Don't use the index if it's empty. */
2388 if (map->symbol_table_slots == 0)
2389 return 0;
2390
1fd400ff
TT
2391 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2392 return 0;
2393
8b70b953
TT
2394 if (types_list_elements)
2395 {
2396 struct dwarf2_section_info *section;
2397
2398 /* We can only handle a single .debug_types when we have an
2399 index. */
2400 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2401 return 0;
2402
2403 section = VEC_index (dwarf2_section_info_def,
2404 dwarf2_per_objfile->types, 0);
2405
2406 if (!create_signatured_type_table_from_index (objfile, section,
2407 types_list,
2408 types_list_elements))
2409 return 0;
2410 }
9291a0cd
TT
2411
2412 create_addrmap_from_index (objfile, map);
2413
2414 dwarf2_per_objfile->index_table = map;
2415 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2416 dwarf2_per_objfile->quick_file_names_table =
2417 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2418
2419 return 1;
2420}
2421
2422/* A helper for the "quick" functions which sets the global
2423 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2424
9291a0cd
TT
2425static void
2426dw2_setup (struct objfile *objfile)
2427{
2428 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2429 gdb_assert (dwarf2_per_objfile);
2430}
2431
dee91e82 2432/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2433
dee91e82
DE
2434static void
2435dw2_get_file_names_reader (const struct die_reader_specs *reader,
2436 gdb_byte *info_ptr,
2437 struct die_info *comp_unit_die,
2438 int has_children,
2439 void *data)
9291a0cd 2440{
dee91e82
DE
2441 struct dwarf2_cu *cu = reader->cu;
2442 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2443 struct objfile *objfile = dwarf2_per_objfile->objfile;
7b9f3c50 2444 struct line_header *lh;
9291a0cd 2445 struct attribute *attr;
dee91e82 2446 int i;
9291a0cd 2447 char *name, *comp_dir;
7b9f3c50
DE
2448 void **slot;
2449 struct quick_file_names *qfn;
2450 unsigned int line_offset;
9291a0cd 2451
7b9f3c50
DE
2452 lh = NULL;
2453 slot = NULL;
2454 line_offset = 0;
dee91e82
DE
2455
2456 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2457 if (attr)
2458 {
7b9f3c50
DE
2459 struct quick_file_names find_entry;
2460
2461 line_offset = DW_UNSND (attr);
2462
2463 /* We may have already read in this line header (TU line header sharing).
2464 If we have we're done. */
2465 find_entry.offset = line_offset;
2466 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2467 &find_entry, INSERT);
2468 if (*slot != NULL)
2469 {
7b9f3c50 2470 this_cu->v.quick->file_names = *slot;
dee91e82 2471 return;
7b9f3c50
DE
2472 }
2473
3019eac3 2474 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2475 }
2476 if (lh == NULL)
2477 {
7b9f3c50 2478 this_cu->v.quick->no_file_data = 1;
dee91e82 2479 return;
9291a0cd
TT
2480 }
2481
7b9f3c50
DE
2482 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2483 qfn->offset = line_offset;
2484 gdb_assert (slot != NULL);
2485 *slot = qfn;
9291a0cd 2486
dee91e82 2487 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2488
7b9f3c50
DE
2489 qfn->num_file_names = lh->num_file_names;
2490 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2491 lh->num_file_names * sizeof (char *));
9291a0cd 2492 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2493 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2494 qfn->real_names = NULL;
9291a0cd 2495
7b9f3c50 2496 free_line_header (lh);
7b9f3c50
DE
2497
2498 this_cu->v.quick->file_names = qfn;
dee91e82
DE
2499}
2500
2501/* A helper for the "quick" functions which attempts to read the line
2502 table for THIS_CU. */
2503
2504static struct quick_file_names *
2505dw2_get_file_names (struct objfile *objfile,
2506 struct dwarf2_per_cu_data *this_cu)
2507{
2508 if (this_cu->v.quick->file_names != NULL)
2509 return this_cu->v.quick->file_names;
2510 /* If we know there is no line data, no point in looking again. */
2511 if (this_cu->v.quick->no_file_data)
2512 return NULL;
2513
3019eac3
DE
2514 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2515 in the stub for CUs, there's is no need to lookup the DWO file.
2516 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2517 DWO file. */
2518 if (this_cu->is_debug_types)
fd820528 2519 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
3019eac3
DE
2520 else
2521 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2522
2523 if (this_cu->v.quick->no_file_data)
2524 return NULL;
2525 return this_cu->v.quick->file_names;
9291a0cd
TT
2526}
2527
2528/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2529 real path for a given file name from the line table. */
2fdf6df6 2530
9291a0cd 2531static const char *
7b9f3c50
DE
2532dw2_get_real_path (struct objfile *objfile,
2533 struct quick_file_names *qfn, int index)
9291a0cd 2534{
7b9f3c50
DE
2535 if (qfn->real_names == NULL)
2536 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2537 qfn->num_file_names, sizeof (char *));
9291a0cd 2538
7b9f3c50
DE
2539 if (qfn->real_names[index] == NULL)
2540 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2541
7b9f3c50 2542 return qfn->real_names[index];
9291a0cd
TT
2543}
2544
2545static struct symtab *
2546dw2_find_last_source_symtab (struct objfile *objfile)
2547{
2548 int index;
ae2de4f8 2549
9291a0cd
TT
2550 dw2_setup (objfile);
2551 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2552 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2553}
2554
7b9f3c50
DE
2555/* Traversal function for dw2_forget_cached_source_info. */
2556
2557static int
2558dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2559{
7b9f3c50 2560 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2561
7b9f3c50 2562 if (file_data->real_names)
9291a0cd 2563 {
7b9f3c50 2564 int i;
9291a0cd 2565
7b9f3c50 2566 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2567 {
7b9f3c50
DE
2568 xfree ((void*) file_data->real_names[i]);
2569 file_data->real_names[i] = NULL;
9291a0cd
TT
2570 }
2571 }
7b9f3c50
DE
2572
2573 return 1;
2574}
2575
2576static void
2577dw2_forget_cached_source_info (struct objfile *objfile)
2578{
2579 dw2_setup (objfile);
2580
2581 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2582 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2583}
2584
f8eba3c6
TT
2585/* Helper function for dw2_map_symtabs_matching_filename that expands
2586 the symtabs and calls the iterator. */
2587
2588static int
2589dw2_map_expand_apply (struct objfile *objfile,
2590 struct dwarf2_per_cu_data *per_cu,
2591 const char *name,
2592 const char *full_path, const char *real_path,
2593 int (*callback) (struct symtab *, void *),
2594 void *data)
2595{
2596 struct symtab *last_made = objfile->symtabs;
2597
2598 /* Don't visit already-expanded CUs. */
2599 if (per_cu->v.quick->symtab)
2600 return 0;
2601
2602 /* This may expand more than one symtab, and we want to iterate over
2603 all of them. */
a0f42c21 2604 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2605
2606 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2607 objfile->symtabs, last_made);
2608}
2609
2610/* Implementation of the map_symtabs_matching_filename method. */
2611
9291a0cd 2612static int
f8eba3c6
TT
2613dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2614 const char *full_path, const char *real_path,
2615 int (*callback) (struct symtab *, void *),
2616 void *data)
9291a0cd
TT
2617{
2618 int i;
c011a4f4 2619 const char *name_basename = lbasename (name);
4aac40c8
TT
2620 int name_len = strlen (name);
2621 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
2622
2623 dw2_setup (objfile);
ae2de4f8 2624
1fd400ff 2625 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2626 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2627 {
2628 int j;
e254ef6a 2629 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2630 struct quick_file_names *file_data;
9291a0cd 2631
3d7bb9d9 2632 /* We only need to look at symtabs not already expanded. */
e254ef6a 2633 if (per_cu->v.quick->symtab)
9291a0cd
TT
2634 continue;
2635
7b9f3c50
DE
2636 file_data = dw2_get_file_names (objfile, per_cu);
2637 if (file_data == NULL)
9291a0cd
TT
2638 continue;
2639
7b9f3c50 2640 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2641 {
7b9f3c50 2642 const char *this_name = file_data->file_names[j];
9291a0cd 2643
4aac40c8
TT
2644 if (FILENAME_CMP (name, this_name) == 0
2645 || (!is_abs && compare_filenames_for_search (this_name,
2646 name, name_len)))
9291a0cd 2647 {
f8eba3c6
TT
2648 if (dw2_map_expand_apply (objfile, per_cu,
2649 name, full_path, real_path,
2650 callback, data))
2651 return 1;
4aac40c8 2652 }
9291a0cd 2653
c011a4f4
DE
2654 /* Before we invoke realpath, which can get expensive when many
2655 files are involved, do a quick comparison of the basenames. */
2656 if (! basenames_may_differ
2657 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2658 continue;
2659
9291a0cd
TT
2660 if (full_path != NULL)
2661 {
7b9f3c50
DE
2662 const char *this_real_name = dw2_get_real_path (objfile,
2663 file_data, j);
9291a0cd 2664
7b9f3c50 2665 if (this_real_name != NULL
4aac40c8
TT
2666 && (FILENAME_CMP (full_path, this_real_name) == 0
2667 || (!is_abs
2668 && compare_filenames_for_search (this_real_name,
2669 name, name_len))))
9291a0cd 2670 {
f8eba3c6
TT
2671 if (dw2_map_expand_apply (objfile, per_cu,
2672 name, full_path, real_path,
2673 callback, data))
2674 return 1;
9291a0cd
TT
2675 }
2676 }
2677
2678 if (real_path != NULL)
2679 {
7b9f3c50
DE
2680 const char *this_real_name = dw2_get_real_path (objfile,
2681 file_data, j);
9291a0cd 2682
7b9f3c50 2683 if (this_real_name != NULL
4aac40c8
TT
2684 && (FILENAME_CMP (real_path, this_real_name) == 0
2685 || (!is_abs
2686 && compare_filenames_for_search (this_real_name,
2687 name, name_len))))
9291a0cd 2688 {
f8eba3c6
TT
2689 if (dw2_map_expand_apply (objfile, per_cu,
2690 name, full_path, real_path,
2691 callback, data))
2692 return 1;
9291a0cd
TT
2693 }
2694 }
2695 }
2696 }
2697
9291a0cd
TT
2698 return 0;
2699}
2700
2701static struct symtab *
2702dw2_lookup_symbol (struct objfile *objfile, int block_index,
2703 const char *name, domain_enum domain)
2704{
774b6a14 2705 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2706 instead. */
2707 return NULL;
2708}
2709
2710/* A helper function that expands all symtabs that hold an object
2711 named NAME. */
2fdf6df6 2712
9291a0cd
TT
2713static void
2714dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2715{
2716 dw2_setup (objfile);
2717
ae2de4f8 2718 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2719 if (dwarf2_per_objfile->index_table)
2720 {
2721 offset_type *vec;
2722
2723 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2724 name, &vec))
2725 {
2726 offset_type i, len = MAYBE_SWAP (*vec);
2727 for (i = 0; i < len; ++i)
2728 {
2729 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2730 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2731
a0f42c21 2732 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2733 }
2734 }
2735 }
2736}
2737
774b6a14
TT
2738static void
2739dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2740 enum block_enum block_kind, const char *name,
774b6a14 2741 domain_enum domain)
9291a0cd 2742{
774b6a14 2743 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2744}
2745
2746static void
2747dw2_print_stats (struct objfile *objfile)
2748{
2749 int i, count;
2750
2751 dw2_setup (objfile);
2752 count = 0;
1fd400ff 2753 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2754 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2755 {
e254ef6a 2756 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2757
e254ef6a 2758 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2759 ++count;
2760 }
2761 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2762}
2763
2764static void
2765dw2_dump (struct objfile *objfile)
2766{
2767 /* Nothing worth printing. */
2768}
2769
2770static void
2771dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2772 struct section_offsets *delta)
2773{
2774 /* There's nothing to relocate here. */
2775}
2776
2777static void
2778dw2_expand_symtabs_for_function (struct objfile *objfile,
2779 const char *func_name)
2780{
2781 dw2_do_expand_symtabs_matching (objfile, func_name);
2782}
2783
2784static void
2785dw2_expand_all_symtabs (struct objfile *objfile)
2786{
2787 int i;
2788
2789 dw2_setup (objfile);
1fd400ff
TT
2790
2791 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2792 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2793 {
e254ef6a 2794 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2795
a0f42c21 2796 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2797 }
2798}
2799
2800static void
2801dw2_expand_symtabs_with_filename (struct objfile *objfile,
2802 const char *filename)
2803{
2804 int i;
2805
2806 dw2_setup (objfile);
d4637a04
DE
2807
2808 /* We don't need to consider type units here.
2809 This is only called for examining code, e.g. expand_line_sal.
2810 There can be an order of magnitude (or more) more type units
2811 than comp units, and we avoid them if we can. */
2812
2813 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2814 {
2815 int j;
e254ef6a 2816 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2817 struct quick_file_names *file_data;
9291a0cd 2818
3d7bb9d9 2819 /* We only need to look at symtabs not already expanded. */
e254ef6a 2820 if (per_cu->v.quick->symtab)
9291a0cd
TT
2821 continue;
2822
7b9f3c50
DE
2823 file_data = dw2_get_file_names (objfile, per_cu);
2824 if (file_data == NULL)
9291a0cd
TT
2825 continue;
2826
7b9f3c50 2827 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2828 {
7b9f3c50 2829 const char *this_name = file_data->file_names[j];
1ef75ecc 2830 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2831 {
a0f42c21 2832 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2833 break;
2834 }
2835 }
2836 }
2837}
2838
dd786858 2839static const char *
9291a0cd
TT
2840dw2_find_symbol_file (struct objfile *objfile, const char *name)
2841{
e254ef6a 2842 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2843 offset_type *vec;
7b9f3c50 2844 struct quick_file_names *file_data;
9291a0cd
TT
2845
2846 dw2_setup (objfile);
2847
ae2de4f8 2848 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2849 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2850 {
2851 struct symtab *s;
2852
d790cf0a
DE
2853 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
2854 {
2855 struct blockvector *bv = BLOCKVECTOR (s);
2856 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2857 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2858
2859 if (sym)
2860 return sym->symtab->filename;
2861 }
96408a79
SA
2862 return NULL;
2863 }
9291a0cd
TT
2864
2865 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2866 name, &vec))
2867 return NULL;
2868
2869 /* Note that this just looks at the very first one named NAME -- but
2870 actually we are looking for a function. find_main_filename
2871 should be rewritten so that it doesn't require a custom hook. It
2872 could just use the ordinary symbol tables. */
2873 /* vec[0] is the length, which must always be >0. */
e254ef6a 2874 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2875
7b9f3c50 2876 file_data = dw2_get_file_names (objfile, per_cu);
943cb756
DE
2877 if (file_data == NULL
2878 || file_data->num_file_names == 0)
9291a0cd
TT
2879 return NULL;
2880
7b9f3c50 2881 return file_data->file_names[file_data->num_file_names - 1];
9291a0cd
TT
2882}
2883
2884static void
40658b94
PH
2885dw2_map_matching_symbols (const char * name, domain_enum namespace,
2886 struct objfile *objfile, int global,
2887 int (*callback) (struct block *,
2888 struct symbol *, void *),
2edb89d3
JK
2889 void *data, symbol_compare_ftype *match,
2890 symbol_compare_ftype *ordered_compare)
9291a0cd 2891{
40658b94 2892 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2893 current language is Ada for a non-Ada objfile using GNU index. As Ada
2894 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2895}
2896
2897static void
f8eba3c6
TT
2898dw2_expand_symtabs_matching
2899 (struct objfile *objfile,
2900 int (*file_matcher) (const char *, void *),
e078317b 2901 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
2902 enum search_domain kind,
2903 void *data)
9291a0cd
TT
2904{
2905 int i;
2906 offset_type iter;
4b5246aa 2907 struct mapped_index *index;
9291a0cd
TT
2908
2909 dw2_setup (objfile);
ae2de4f8
DE
2910
2911 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2912 if (!dwarf2_per_objfile->index_table)
2913 return;
4b5246aa 2914 index = dwarf2_per_objfile->index_table;
9291a0cd 2915
7b08b9eb 2916 if (file_matcher != NULL)
24c79950
TT
2917 {
2918 struct cleanup *cleanup;
2919 htab_t visited_found, visited_not_found;
2920
2921 visited_found = htab_create_alloc (10,
2922 htab_hash_pointer, htab_eq_pointer,
2923 NULL, xcalloc, xfree);
2924 cleanup = make_cleanup_htab_delete (visited_found);
2925 visited_not_found = htab_create_alloc (10,
2926 htab_hash_pointer, htab_eq_pointer,
2927 NULL, xcalloc, xfree);
2928 make_cleanup_htab_delete (visited_not_found);
2929
2930 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2931 + dwarf2_per_objfile->n_type_units); ++i)
2932 {
2933 int j;
2934 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2935 struct quick_file_names *file_data;
2936 void **slot;
7b08b9eb 2937
24c79950 2938 per_cu->v.quick->mark = 0;
3d7bb9d9 2939
24c79950
TT
2940 /* We only need to look at symtabs not already expanded. */
2941 if (per_cu->v.quick->symtab)
2942 continue;
7b08b9eb 2943
24c79950
TT
2944 file_data = dw2_get_file_names (objfile, per_cu);
2945 if (file_data == NULL)
2946 continue;
7b08b9eb 2947
24c79950
TT
2948 if (htab_find (visited_not_found, file_data) != NULL)
2949 continue;
2950 else if (htab_find (visited_found, file_data) != NULL)
2951 {
2952 per_cu->v.quick->mark = 1;
2953 continue;
2954 }
2955
2956 for (j = 0; j < file_data->num_file_names; ++j)
2957 {
2958 if (file_matcher (file_data->file_names[j], data))
2959 {
2960 per_cu->v.quick->mark = 1;
2961 break;
2962 }
2963 }
2964
2965 slot = htab_find_slot (per_cu->v.quick->mark
2966 ? visited_found
2967 : visited_not_found,
2968 file_data, INSERT);
2969 *slot = file_data;
2970 }
2971
2972 do_cleanups (cleanup);
2973 }
9291a0cd 2974
3876f04e 2975 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
2976 {
2977 offset_type idx = 2 * iter;
2978 const char *name;
2979 offset_type *vec, vec_len, vec_idx;
2980
3876f04e 2981 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
2982 continue;
2983
3876f04e 2984 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 2985
e078317b 2986 if (! (*name_matcher) (name, data))
9291a0cd
TT
2987 continue;
2988
2989 /* The name was matched, now expand corresponding CUs that were
2990 marked. */
4b5246aa 2991 vec = (offset_type *) (index->constant_pool
3876f04e 2992 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
2993 vec_len = MAYBE_SWAP (vec[0]);
2994 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2995 {
e254ef6a 2996 struct dwarf2_per_cu_data *per_cu;
1fd400ff 2997
e254ef6a 2998 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 2999 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3000 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3001 }
3002 }
3003}
3004
9703b513
TT
3005/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3006 symtab. */
3007
3008static struct symtab *
3009recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3010{
3011 int i;
3012
3013 if (BLOCKVECTOR (symtab) != NULL
3014 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3015 return symtab;
3016
a3ec0bb1
DE
3017 if (symtab->includes == NULL)
3018 return NULL;
3019
9703b513
TT
3020 for (i = 0; symtab->includes[i]; ++i)
3021 {
a3ec0bb1 3022 struct symtab *s = symtab->includes[i];
9703b513
TT
3023
3024 s = recursively_find_pc_sect_symtab (s, pc);
3025 if (s != NULL)
3026 return s;
3027 }
3028
3029 return NULL;
3030}
3031
9291a0cd
TT
3032static struct symtab *
3033dw2_find_pc_sect_symtab (struct objfile *objfile,
3034 struct minimal_symbol *msymbol,
3035 CORE_ADDR pc,
3036 struct obj_section *section,
3037 int warn_if_readin)
3038{
3039 struct dwarf2_per_cu_data *data;
9703b513 3040 struct symtab *result;
9291a0cd
TT
3041
3042 dw2_setup (objfile);
3043
3044 if (!objfile->psymtabs_addrmap)
3045 return NULL;
3046
3047 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3048 if (!data)
3049 return NULL;
3050
3051 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3052 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3053 paddress (get_objfile_arch (objfile), pc));
3054
9703b513
TT
3055 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3056 gdb_assert (result != NULL);
3057 return result;
9291a0cd
TT
3058}
3059
9291a0cd 3060static void
44b13c5a 3061dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3062 void *data, int need_fullname)
9291a0cd
TT
3063{
3064 int i;
24c79950
TT
3065 struct cleanup *cleanup;
3066 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3067 NULL, xcalloc, xfree);
9291a0cd 3068
24c79950 3069 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3070 dw2_setup (objfile);
ae2de4f8 3071
24c79950
TT
3072 /* We can ignore file names coming from already-expanded CUs. */
3073 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3074 + dwarf2_per_objfile->n_type_units); ++i)
3075 {
3076 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3077
3078 if (per_cu->v.quick->symtab)
3079 {
3080 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3081 INSERT);
3082
3083 *slot = per_cu->v.quick->file_names;
3084 }
3085 }
3086
1fd400ff 3087 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3088 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
3089 {
3090 int j;
e254ef6a 3091 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3092 struct quick_file_names *file_data;
24c79950 3093 void **slot;
9291a0cd 3094
3d7bb9d9 3095 /* We only need to look at symtabs not already expanded. */
e254ef6a 3096 if (per_cu->v.quick->symtab)
9291a0cd
TT
3097 continue;
3098
7b9f3c50
DE
3099 file_data = dw2_get_file_names (objfile, per_cu);
3100 if (file_data == NULL)
9291a0cd
TT
3101 continue;
3102
24c79950
TT
3103 slot = htab_find_slot (visited, file_data, INSERT);
3104 if (*slot)
3105 {
3106 /* Already visited. */
3107 continue;
3108 }
3109 *slot = file_data;
3110
7b9f3c50 3111 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3112 {
74e2f255
DE
3113 const char *this_real_name;
3114
3115 if (need_fullname)
3116 this_real_name = dw2_get_real_path (objfile, file_data, j);
3117 else
3118 this_real_name = NULL;
7b9f3c50 3119 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3120 }
3121 }
24c79950
TT
3122
3123 do_cleanups (cleanup);
9291a0cd
TT
3124}
3125
3126static int
3127dw2_has_symbols (struct objfile *objfile)
3128{
3129 return 1;
3130}
3131
3132const struct quick_symbol_functions dwarf2_gdb_index_functions =
3133{
3134 dw2_has_symbols,
3135 dw2_find_last_source_symtab,
3136 dw2_forget_cached_source_info,
f8eba3c6 3137 dw2_map_symtabs_matching_filename,
9291a0cd 3138 dw2_lookup_symbol,
774b6a14 3139 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3140 dw2_print_stats,
3141 dw2_dump,
3142 dw2_relocate,
3143 dw2_expand_symtabs_for_function,
3144 dw2_expand_all_symtabs,
3145 dw2_expand_symtabs_with_filename,
3146 dw2_find_symbol_file,
40658b94 3147 dw2_map_matching_symbols,
9291a0cd
TT
3148 dw2_expand_symtabs_matching,
3149 dw2_find_pc_sect_symtab,
9291a0cd
TT
3150 dw2_map_symbol_filenames
3151};
3152
3153/* Initialize for reading DWARF for this objfile. Return 0 if this
3154 file will use psymtabs, or 1 if using the GNU index. */
3155
3156int
3157dwarf2_initialize_objfile (struct objfile *objfile)
3158{
3159 /* If we're about to read full symbols, don't bother with the
3160 indices. In this case we also don't care if some other debug
3161 format is making psymtabs, because they are all about to be
3162 expanded anyway. */
3163 if ((objfile->flags & OBJF_READNOW))
3164 {
3165 int i;
3166
3167 dwarf2_per_objfile->using_index = 1;
3168 create_all_comp_units (objfile);
0e50663e 3169 create_all_type_units (objfile);
7b9f3c50
DE
3170 dwarf2_per_objfile->quick_file_names_table =
3171 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3172
1fd400ff 3173 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3174 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3175 {
e254ef6a 3176 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3177
e254ef6a
DE
3178 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3179 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3180 }
3181
3182 /* Return 1 so that gdb sees the "quick" functions. However,
3183 these functions will be no-ops because we will have expanded
3184 all symtabs. */
3185 return 1;
3186 }
3187
3188 if (dwarf2_read_index (objfile))
3189 return 1;
3190
9291a0cd
TT
3191 return 0;
3192}
3193
3194\f
3195
dce234bc
PP
3196/* Build a partial symbol table. */
3197
3198void
f29dff0a 3199dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3200{
f29dff0a 3201 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3202 {
3203 init_psymbol_list (objfile, 1024);
3204 }
3205
d146bf1e 3206 dwarf2_build_psymtabs_hard (objfile);
c906108c 3207}
c906108c 3208
45452591
DE
3209/* Return TRUE if OFFSET is within CU_HEADER. */
3210
3211static inline int
b64f50a1 3212offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3213{
b64f50a1
JK
3214 sect_offset bottom = { cu_header->offset.sect_off };
3215 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3216 + cu_header->initial_length_size) };
9a619af0 3217
b64f50a1 3218 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3219}
3220
93311388
DE
3221/* Read in the comp unit header information from the debug_info at info_ptr.
3222 NOTE: This leaves members offset, first_die_offset to be filled in
3223 by the caller. */
107d2387 3224
fe1b8b76 3225static gdb_byte *
107d2387 3226read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3227 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3228{
3229 int signed_addr;
891d2f0b 3230 unsigned int bytes_read;
c764a876
DE
3231
3232 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3233 cu_header->initial_length_size = bytes_read;
3234 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3235 info_ptr += bytes_read;
107d2387
AC
3236 cu_header->version = read_2_bytes (abfd, info_ptr);
3237 info_ptr += 2;
b64f50a1
JK
3238 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3239 &bytes_read);
613e1657 3240 info_ptr += bytes_read;
107d2387
AC
3241 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3242 info_ptr += 1;
3243 signed_addr = bfd_get_sign_extend_vma (abfd);
3244 if (signed_addr < 0)
8e65ff28 3245 internal_error (__FILE__, __LINE__,
e2e0b3e5 3246 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3247 cu_header->signed_addr_p = signed_addr;
c764a876 3248
107d2387
AC
3249 return info_ptr;
3250}
3251
9ff913ba
DE
3252/* Subroutine of read_and_check_comp_unit_head and
3253 read_and_check_type_unit_head to simplify them.
3254 Perform various error checking on the header. */
3255
3256static void
3257error_check_comp_unit_head (struct comp_unit_head *header,
3258 struct dwarf2_section_info *section)
3259{
3260 bfd *abfd = section->asection->owner;
3261 const char *filename = bfd_get_filename (abfd);
3262
3263 if (header->version != 2 && header->version != 3 && header->version != 4)
3264 error (_("Dwarf Error: wrong version in compilation unit header "
3265 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3266 filename);
3267
b64f50a1 3268 if (header->abbrev_offset.sect_off
9ff913ba
DE
3269 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3270 &dwarf2_per_objfile->abbrev))
3271 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3272 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3273 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3274 filename);
3275
3276 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3277 avoid potential 32-bit overflow. */
b64f50a1 3278 if (((unsigned long) header->offset.sect_off
9ff913ba
DE
3279 + header->length + header->initial_length_size)
3280 > section->size)
3281 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3282 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3283 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3284 filename);
3285}
3286
3287/* Read in a CU/TU header and perform some basic error checking.
3288 The contents of the header are stored in HEADER.
3289 The result is a pointer to the start of the first DIE. */
adabb602 3290
fe1b8b76 3291static gdb_byte *
9ff913ba
DE
3292read_and_check_comp_unit_head (struct comp_unit_head *header,
3293 struct dwarf2_section_info *section,
3294 gdb_byte *info_ptr,
3295 int is_debug_types_section)
72bf9492 3296{
fe1b8b76 3297 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3298 bfd *abfd = section->asection->owner;
72bf9492 3299
b64f50a1 3300 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3301
72bf9492
DJ
3302 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3303
460c1c54
CC
3304 /* If we're reading a type unit, skip over the signature and
3305 type_offset fields. */
b0df02fd 3306 if (is_debug_types_section)
460c1c54
CC
3307 info_ptr += 8 /*signature*/ + header->offset_size;
3308
b64f50a1 3309 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3310
9ff913ba 3311 error_check_comp_unit_head (header, section);
72bf9492
DJ
3312
3313 return info_ptr;
3314}
3315
348e048f
DE
3316/* Read in the types comp unit header information from .debug_types entry at
3317 types_ptr. The result is a pointer to one past the end of the header. */
3318
3319static gdb_byte *
9ff913ba
DE
3320read_and_check_type_unit_head (struct comp_unit_head *header,
3321 struct dwarf2_section_info *section,
3322 gdb_byte *info_ptr,
dee91e82
DE
3323 ULONGEST *signature,
3324 cu_offset *type_offset_in_tu)
348e048f 3325{
9ff913ba
DE
3326 gdb_byte *beg_of_comp_unit = info_ptr;
3327 bfd *abfd = section->asection->owner;
348e048f 3328
b64f50a1 3329 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3330
9ff913ba 3331 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3332
9ff913ba
DE
3333 /* If we're reading a type unit, skip over the signature and
3334 type_offset fields. */
3335 if (signature != NULL)
3336 *signature = read_8_bytes (abfd, info_ptr);
3337 info_ptr += 8;
dee91e82
DE
3338 if (type_offset_in_tu != NULL)
3339 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3340 header->offset_size);
9ff913ba
DE
3341 info_ptr += header->offset_size;
3342
b64f50a1 3343 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3344
9ff913ba
DE
3345 error_check_comp_unit_head (header, section);
3346
3347 return info_ptr;
348e048f
DE
3348}
3349
aaa75496
JB
3350/* Allocate a new partial symtab for file named NAME and mark this new
3351 partial symtab as being an include of PST. */
3352
3353static void
3354dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3355 struct objfile *objfile)
3356{
3357 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3358
3359 subpst->section_offsets = pst->section_offsets;
3360 subpst->textlow = 0;
3361 subpst->texthigh = 0;
3362
3363 subpst->dependencies = (struct partial_symtab **)
3364 obstack_alloc (&objfile->objfile_obstack,
3365 sizeof (struct partial_symtab *));
3366 subpst->dependencies[0] = pst;
3367 subpst->number_of_dependencies = 1;
3368
3369 subpst->globals_offset = 0;
3370 subpst->n_global_syms = 0;
3371 subpst->statics_offset = 0;
3372 subpst->n_static_syms = 0;
3373 subpst->symtab = NULL;
3374 subpst->read_symtab = pst->read_symtab;
3375 subpst->readin = 0;
3376
3377 /* No private part is necessary for include psymtabs. This property
3378 can be used to differentiate between such include psymtabs and
10b3939b 3379 the regular ones. */
58a9656e 3380 subpst->read_symtab_private = NULL;
aaa75496
JB
3381}
3382
3383/* Read the Line Number Program data and extract the list of files
3384 included by the source file represented by PST. Build an include
d85a05f0 3385 partial symtab for each of these included files. */
aaa75496
JB
3386
3387static void
3388dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
3389 struct die_info *die,
3390 struct partial_symtab *pst)
aaa75496 3391{
d85a05f0
DJ
3392 struct line_header *lh = NULL;
3393 struct attribute *attr;
aaa75496 3394
d85a05f0
DJ
3395 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3396 if (attr)
3019eac3 3397 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
3398 if (lh == NULL)
3399 return; /* No linetable, so no includes. */
3400
c6da4cef 3401 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3402 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3403
3404 free_line_header (lh);
3405}
3406
348e048f 3407static hashval_t
52dc124a 3408hash_signatured_type (const void *item)
348e048f 3409{
52dc124a 3410 const struct signatured_type *sig_type = item;
9a619af0 3411
348e048f 3412 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 3413 return sig_type->signature;
348e048f
DE
3414}
3415
3416static int
52dc124a 3417eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
3418{
3419 const struct signatured_type *lhs = item_lhs;
3420 const struct signatured_type *rhs = item_rhs;
9a619af0 3421
348e048f
DE
3422 return lhs->signature == rhs->signature;
3423}
3424
1fd400ff
TT
3425/* Allocate a hash table for signatured types. */
3426
3427static htab_t
673bfd45 3428allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3429{
3430 return htab_create_alloc_ex (41,
52dc124a
DE
3431 hash_signatured_type,
3432 eq_signatured_type,
1fd400ff
TT
3433 NULL,
3434 &objfile->objfile_obstack,
3435 hashtab_obstack_allocate,
3436 dummy_obstack_deallocate);
3437}
3438
d467dd73 3439/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3440
3441static int
d467dd73 3442add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3443{
3444 struct signatured_type *sigt = *slot;
3445 struct dwarf2_per_cu_data ***datap = datum;
3446
3447 **datap = &sigt->per_cu;
3448 ++*datap;
3449
3450 return 1;
3451}
3452
3019eac3
DE
3453/* Create the hash table of all entries in the .debug_types section.
3454 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3455 The result is a pointer to the hash table or NULL if there are
3456 no types. */
348e048f 3457
3019eac3
DE
3458static htab_t
3459create_debug_types_hash_table (struct dwo_file *dwo_file,
3460 VEC (dwarf2_section_info_def) *types)
348e048f 3461{
3019eac3 3462 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 3463 htab_t types_htab = NULL;
8b70b953
TT
3464 int ix;
3465 struct dwarf2_section_info *section;
348e048f 3466
3019eac3
DE
3467 if (VEC_empty (dwarf2_section_info_def, types))
3468 return NULL;
348e048f 3469
8b70b953 3470 for (ix = 0;
3019eac3 3471 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
3472 ++ix)
3473 {
3019eac3 3474 bfd *abfd;
8b70b953 3475 gdb_byte *info_ptr, *end_ptr;
348e048f 3476
8b70b953
TT
3477 dwarf2_read_section (objfile, section);
3478 info_ptr = section->buffer;
348e048f 3479
8b70b953
TT
3480 if (info_ptr == NULL)
3481 continue;
348e048f 3482
3019eac3
DE
3483 /* We can't set abfd until now because the section may be empty or
3484 not present, in which case section->asection will be NULL. */
3485 abfd = section->asection->owner;
3486
8b70b953 3487 if (types_htab == NULL)
3019eac3
DE
3488 {
3489 if (dwo_file)
3490 types_htab = allocate_dwo_unit_table (objfile);
3491 else
3492 types_htab = allocate_signatured_type_table (objfile);
3493 }
348e048f 3494
8b70b953 3495 if (dwarf2_die_debug)
dee91e82
DE
3496 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3497 bfd_get_filename (abfd));
3498
3499 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3500 because we don't need to read any dies: the signature is in the
3501 header. */
8b70b953
TT
3502
3503 end_ptr = info_ptr + section->size;
3504 while (info_ptr < end_ptr)
3505 {
b64f50a1 3506 sect_offset offset;
3019eac3 3507 cu_offset type_offset_in_tu;
8b70b953 3508 ULONGEST signature;
52dc124a 3509 struct signatured_type *sig_type;
3019eac3 3510 struct dwo_unit *dwo_tu;
8b70b953
TT
3511 void **slot;
3512 gdb_byte *ptr = info_ptr;
9ff913ba 3513 struct comp_unit_head header;
dee91e82 3514 unsigned int length;
348e048f 3515
b64f50a1 3516 offset.sect_off = ptr - section->buffer;
348e048f 3517
8b70b953 3518 /* We need to read the type's signature in order to build the hash
9ff913ba 3519 table, but we don't need anything else just yet. */
348e048f 3520
9ff913ba 3521 ptr = read_and_check_type_unit_head (&header, section, ptr,
3019eac3 3522 &signature, &type_offset_in_tu);
6caca83c 3523
dee91e82
DE
3524 length = header.initial_length_size + header.length;
3525
6caca83c 3526 /* Skip dummy type units. */
dee91e82
DE
3527 if (ptr >= info_ptr + length
3528 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 3529 {
dee91e82 3530 info_ptr += header.initial_length_size + header.length;
6caca83c
CC
3531 continue;
3532 }
8b70b953 3533
3019eac3
DE
3534 if (dwo_file)
3535 {
3536 sig_type = NULL;
3537 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3538 struct dwo_unit);
3539 dwo_tu->dwo_file = dwo_file;
3540 dwo_tu->signature = signature;
3541 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3542 dwo_tu->info_or_types_section = section;
3543 dwo_tu->offset = offset;
3544 dwo_tu->length = length;
3545 }
3546 else
3547 {
3548 /* N.B.: type_offset is not usable if this type uses a DWO file.
3549 The real type_offset is in the DWO file. */
3550 dwo_tu = NULL;
3551 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3552 struct signatured_type);
3553 sig_type->signature = signature;
3554 sig_type->type_offset_in_tu = type_offset_in_tu;
3555 sig_type->per_cu.objfile = objfile;
3556 sig_type->per_cu.is_debug_types = 1;
3557 sig_type->per_cu.info_or_types_section = section;
3558 sig_type->per_cu.offset = offset;
3559 sig_type->per_cu.length = length;
3560 }
8b70b953 3561
3019eac3
DE
3562 slot = htab_find_slot (types_htab,
3563 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3564 INSERT);
8b70b953
TT
3565 gdb_assert (slot != NULL);
3566 if (*slot != NULL)
3567 {
3019eac3
DE
3568 sect_offset dup_offset;
3569
3570 if (dwo_file)
3571 {
3572 const struct dwo_unit *dup_tu = *slot;
3573
3574 dup_offset = dup_tu->offset;
3575 }
3576 else
3577 {
3578 const struct signatured_type *dup_tu = *slot;
3579
3580 dup_offset = dup_tu->per_cu.offset;
3581 }
b3c8eb43 3582
8b70b953
TT
3583 complaint (&symfile_complaints,
3584 _("debug type entry at offset 0x%x is duplicate to the "
3585 "entry at offset 0x%x, signature 0x%s"),
3019eac3 3586 offset.sect_off, dup_offset.sect_off,
8b70b953 3587 phex (signature, sizeof (signature)));
8b70b953 3588 }
3019eac3 3589 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 3590
8b70b953
TT
3591 if (dwarf2_die_debug)
3592 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
3593 offset.sect_off,
3594 phex (signature, sizeof (signature)));
348e048f 3595
dee91e82 3596 info_ptr += length;
8b70b953 3597 }
348e048f
DE
3598 }
3599
3019eac3
DE
3600 return types_htab;
3601}
3602
3603/* Create the hash table of all entries in the .debug_types section,
3604 and initialize all_type_units.
3605 The result is zero if there is an error (e.g. missing .debug_types section),
3606 otherwise non-zero. */
3607
3608static int
3609create_all_type_units (struct objfile *objfile)
3610{
3611 htab_t types_htab;
3612 struct dwarf2_per_cu_data **iter;
3613
3614 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3615 if (types_htab == NULL)
3616 {
3617 dwarf2_per_objfile->signatured_types = NULL;
3618 return 0;
3619 }
3620
348e048f
DE
3621 dwarf2_per_objfile->signatured_types = types_htab;
3622
d467dd73
DE
3623 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3624 dwarf2_per_objfile->all_type_units
1fd400ff 3625 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3626 dwarf2_per_objfile->n_type_units
1fd400ff 3627 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3628 iter = &dwarf2_per_objfile->all_type_units[0];
3629 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3630 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3631 == dwarf2_per_objfile->n_type_units);
1fd400ff 3632
348e048f
DE
3633 return 1;
3634}
3635
380bca97 3636/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 3637 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
3638
3639static struct signatured_type *
e319fa28 3640lookup_signatured_type (ULONGEST sig)
348e048f
DE
3641{
3642 struct signatured_type find_entry, *entry;
3643
3644 if (dwarf2_per_objfile->signatured_types == NULL)
3645 {
3646 complaint (&symfile_complaints,
55f1336d 3647 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 3648 return NULL;
348e048f
DE
3649 }
3650
3651 find_entry.signature = sig;
3652 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3653 return entry;
3654}
3655
d85a05f0
DJ
3656/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3657
3658static void
3659init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 3660 struct dwarf2_cu *cu,
3019eac3
DE
3661 struct dwarf2_section_info *section,
3662 struct dwo_file *dwo_file)
d85a05f0 3663{
fceca515 3664 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 3665 reader->abfd = section->asection->owner;
d85a05f0 3666 reader->cu = cu;
3019eac3 3667 reader->dwo_file = dwo_file;
dee91e82
DE
3668 reader->die_section = section;
3669 reader->buffer = section->buffer;
f664829e 3670 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
3671}
3672
3673/* Find the base address of the compilation unit for range lists and
3674 location lists. It will normally be specified by DW_AT_low_pc.
3675 In DWARF-3 draft 4, the base address could be overridden by
3676 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3677 compilation units with discontinuous ranges. */
3678
3679static void
3680dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3681{
3682 struct attribute *attr;
3683
3684 cu->base_known = 0;
3685 cu->base_address = 0;
3686
3687 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3688 if (attr)
3689 {
3690 cu->base_address = DW_ADDR (attr);
3691 cu->base_known = 1;
3692 }
3693 else
3694 {
3695 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3696 if (attr)
3697 {
3698 cu->base_address = DW_ADDR (attr);
3699 cu->base_known = 1;
3700 }
3701 }
3702}
3703
fd820528 3704/* Initialize a CU (or TU) and read its DIEs.
3019eac3 3705 If the CU defers to a DWO file, read the DWO file as well.
dee91e82
DE
3706
3707 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3708 Otherwise, a new CU is allocated with xmalloc.
3709
3710 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3711 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3712
3713 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 3714 linker) then DIE_READER_FUNC will not get called. */
aaa75496 3715
70221824 3716static void
fd820528
DE
3717init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3718 int use_existing_cu, int keep,
3719 die_reader_func_ftype *die_reader_func,
3720 void *data)
c906108c 3721{
dee91e82 3722 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
3723 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3724 bfd *abfd = section->asection->owner;
dee91e82
DE
3725 struct dwarf2_cu *cu;
3726 gdb_byte *begin_info_ptr, *info_ptr;
3727 struct die_reader_specs reader;
d85a05f0 3728 struct die_info *comp_unit_die;
dee91e82 3729 int has_children;
d85a05f0 3730 struct attribute *attr;
dee91e82
DE
3731 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3732 struct signatured_type *sig_type = NULL;
c906108c 3733
dee91e82
DE
3734 if (use_existing_cu)
3735 gdb_assert (keep);
23745b47 3736
dee91e82
DE
3737 cleanups = make_cleanup (null_cleanup, NULL);
3738
3739 /* This is cheap if the section is already read in. */
3740 dwarf2_read_section (objfile, section);
3741
3742 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3743
3744 if (use_existing_cu && this_cu->cu != NULL)
3745 {
3746 cu = this_cu->cu;
3747 info_ptr += cu->header.first_die_offset.cu_off;
3748 }
3749 else
3750 {
3751 /* If !use_existing_cu, this_cu->cu must be NULL. */
3752 gdb_assert (this_cu->cu == NULL);
3753
3754 cu = xmalloc (sizeof (*cu));
3755 init_one_comp_unit (cu, this_cu);
3756
3757 /* If an error occurs while loading, release our storage. */
3758 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3759
3019eac3 3760 if (this_cu->is_debug_types)
dee91e82
DE
3761 {
3762 ULONGEST signature;
3763
3764 info_ptr = read_and_check_type_unit_head (&cu->header,
3765 section, info_ptr,
3766 &signature, NULL);
3767
3768 /* There's no way to get from PER_CU to its containing
3769 struct signatured_type.
3770 But we have the signature so we can use that. */
3771 sig_type = lookup_signatured_type (signature);
3772 /* We've already scanned all the signatured types,
3773 this must succeed. */
3774 gdb_assert (sig_type != NULL);
3775 gdb_assert (&sig_type->per_cu == this_cu);
3776 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3777
3778 /* LENGTH has not been set yet for type units. */
3779 this_cu->length = cu->header.length + cu->header.initial_length_size;
3019eac3
DE
3780
3781 /* Establish the type offset that can be used to lookup the type. */
3782 sig_type->type_offset_in_section.sect_off =
3783 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
3784 }
3785 else
3786 {
3787 info_ptr = read_and_check_comp_unit_head (&cu->header,
3788 section, info_ptr, 0);
3789
3790 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3791 gdb_assert (this_cu->length
3792 == cu->header.length + cu->header.initial_length_size);
3793 }
3794 }
10b3939b 3795
6caca83c 3796 /* Skip dummy compilation units. */
dee91e82 3797 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
3798 || peek_abbrev_code (abfd, info_ptr) == 0)
3799 {
dee91e82 3800 do_cleanups (cleanups);
21b2bd31 3801 return;
6caca83c
CC
3802 }
3803
93311388 3804 /* Read the abbrevs for this compilation unit into a table. */
dee91e82
DE
3805 if (cu->dwarf2_abbrevs == NULL)
3806 {
3807 dwarf2_read_abbrevs (cu, &dwarf2_per_objfile->abbrev);
3808 make_cleanup (dwarf2_free_abbrev_table, cu);
3809 }
af703f96 3810
dee91e82 3811 /* Read the top level CU/TU die. */
3019eac3 3812 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 3813 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 3814
3019eac3
DE
3815 /* If we have a DWO stub, process it and then read in the DWO file.
3816 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3817 a DWO CU, that this test will fail. */
3818 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3819 if (attr)
3820 {
3821 char *dwo_name = DW_STRING (attr);
3822 const char *comp_dir;
3823 struct dwo_unit *dwo_unit;
3824 ULONGEST signature; /* Or dwo_id. */
3825 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3826 int i,num_extra_attrs;
3827
3828 if (has_children)
3829 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3830 " has children (offset 0x%x) [in module %s]"),
3831 this_cu->offset.sect_off, bfd_get_filename (abfd));
3832
3833 /* These attributes aren't processed until later:
3834 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3835 However, the attribute is found in the stub which we won't have later.
3836 In order to not impose this complication on the rest of the code,
3837 we read them here and copy them to the DWO CU/TU die. */
3838 stmt_list = low_pc = high_pc = ranges = NULL;
3839
3840 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3841 DWO file. */
3842 if (! this_cu->is_debug_types)
3843 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3844 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3845 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3846 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3847
3848 /* There should be a DW_AT_addr_base attribute here (if needed).
3849 We need the value before we can process DW_FORM_GNU_addr_index. */
3850 cu->addr_base = 0;
3851 cu->have_addr_base = 0;
3852 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
3853 if (attr)
3854 {
3855 cu->addr_base = DW_UNSND (attr);
3856 cu->have_addr_base = 1;
3857 }
3858
3859 if (this_cu->is_debug_types)
3860 {
3861 gdb_assert (sig_type != NULL);
3862 signature = sig_type->signature;
3863 }
3864 else
3865 {
3866 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3867 if (! attr)
3868 error (_("Dwarf Error: missing dwo_id [in module %s]"),
3869 dwo_name);
3870 signature = DW_UNSND (attr);
3871 }
3872
3873 /* We may need the comp_dir in order to find the DWO file. */
3874 comp_dir = NULL;
3875 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3876 if (attr)
3877 comp_dir = DW_STRING (attr);
3878
3879 if (this_cu->is_debug_types)
3880 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
3881 else
3882 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
3883 signature);
3884
3885 if (dwo_unit == NULL)
3886 {
3887 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
3888 " with ID %s [in module %s]"),
3889 this_cu->offset.sect_off,
3890 phex (signature, sizeof (signature)),
3891 objfile->name);
3892 }
3893
3894 /* Set up for reading the DWO CU/TU. */
3895 cu->dwo_unit = dwo_unit;
3896 section = dwo_unit->info_or_types_section;
3897 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
3898 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
3899
3900 if (this_cu->is_debug_types)
3901 {
3902 ULONGEST signature;
3903
3904 info_ptr = read_and_check_type_unit_head (&cu->header,
3905 section, info_ptr,
3906 &signature, NULL);
3907 gdb_assert (sig_type->signature == signature);
3908 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3909 gdb_assert (dwo_unit->length
3910 == cu->header.length + cu->header.initial_length_size);
3911
3912 /* Establish the type offset that can be used to lookup the type.
3913 For DWO files, we don't know it until now. */
3914 sig_type->type_offset_in_section.sect_off =
3915 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
3916 }
3917 else
3918 {
3919 info_ptr = read_and_check_comp_unit_head (&cu->header,
3920 section, info_ptr, 0);
3921 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3922 gdb_assert (dwo_unit->length
3923 == cu->header.length + cu->header.initial_length_size);
3924 }
3925
3926 /* Discard the original CU's abbrev table, and read the DWO's. */
3927 dwarf2_free_abbrev_table (cu);
3928 dwarf2_read_abbrevs (cu, &dwo_unit->dwo_file->sections.abbrev);
3929
3930 /* Read in the die, but leave space to copy over the attributes
3931 from the stub. This has the benefit of simplifying the rest of
3932 the code - all the real work is done here. */
3933 num_extra_attrs = ((stmt_list != NULL)
3934 + (low_pc != NULL)
3935 + (high_pc != NULL)
3936 + (ranges != NULL));
3937 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
3938 &has_children, num_extra_attrs);
3939
3940 /* Copy over the attributes from the stub to the DWO die. */
3941 i = comp_unit_die->num_attrs;
3942 if (stmt_list != NULL)
3943 comp_unit_die->attrs[i++] = *stmt_list;
3944 if (low_pc != NULL)
3945 comp_unit_die->attrs[i++] = *low_pc;
3946 if (high_pc != NULL)
3947 comp_unit_die->attrs[i++] = *high_pc;
3948 if (ranges != NULL)
3949 comp_unit_die->attrs[i++] = *ranges;
3950 comp_unit_die->num_attrs += num_extra_attrs;
3951
3952 /* Skip dummy compilation units. */
3953 if (info_ptr >= begin_info_ptr + dwo_unit->length
3954 || peek_abbrev_code (abfd, info_ptr) == 0)
3955 {
3956 do_cleanups (cleanups);
3957 return;
3958 }
3959 }
3960
dee91e82
DE
3961 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3962
3963 if (free_cu_cleanup != NULL)
348e048f 3964 {
dee91e82
DE
3965 if (keep)
3966 {
3967 /* We've successfully allocated this compilation unit. Let our
3968 caller clean it up when finished with it. */
3969 discard_cleanups (free_cu_cleanup);
3970
3971 /* We can only discard free_cu_cleanup and all subsequent cleanups.
3972 So we have to manually free the abbrev table. */
3973 dwarf2_free_abbrev_table (cu);
3974
3975 /* Link this CU into read_in_chain. */
3976 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3977 dwarf2_per_objfile->read_in_chain = this_cu;
3978 }
3979 else
3980 do_cleanups (free_cu_cleanup);
348e048f 3981 }
dee91e82
DE
3982
3983 do_cleanups (cleanups);
3984}
3985
3019eac3
DE
3986/* Read CU/TU THIS_CU in section SECTION,
3987 but do not follow DW_AT_GNU_dwo_name if present.
3988 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
3989 have already done the lookup to find the DWO file).
dee91e82
DE
3990
3991 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 3992 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
3993
3994 We fill in THIS_CU->length.
3995
3996 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
3997 linker) then DIE_READER_FUNC will not get called.
3998
3999 THIS_CU->cu is always freed when done.
3019eac3
DE
4000 This is done in order to not leave THIS_CU->cu in a state where we have
4001 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4002
4003static void
4004init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4005 struct dwarf2_section_info *abbrev_section,
3019eac3 4006 struct dwo_file *dwo_file,
dee91e82
DE
4007 die_reader_func_ftype *die_reader_func,
4008 void *data)
4009{
4010 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4011 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4012 bfd *abfd = section->asection->owner;
dee91e82
DE
4013 struct dwarf2_cu cu;
4014 gdb_byte *begin_info_ptr, *info_ptr;
4015 struct die_reader_specs reader;
4016 struct cleanup *cleanups;
4017 struct die_info *comp_unit_die;
4018 int has_children;
4019
4020 gdb_assert (this_cu->cu == NULL);
4021
dee91e82
DE
4022 /* This is cheap if the section is already read in. */
4023 dwarf2_read_section (objfile, section);
4024
4025 init_one_comp_unit (&cu, this_cu);
4026
4027 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4028
4029 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4030 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3019eac3 4031 this_cu->is_debug_types);
dee91e82
DE
4032
4033 this_cu->length = cu.header.length + cu.header.initial_length_size;
4034
4035 /* Skip dummy compilation units. */
4036 if (info_ptr >= begin_info_ptr + this_cu->length
4037 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4038 {
dee91e82 4039 do_cleanups (cleanups);
21b2bd31 4040 return;
93311388 4041 }
72bf9492 4042
dee91e82
DE
4043 dwarf2_read_abbrevs (&cu, abbrev_section);
4044 make_cleanup (dwarf2_free_abbrev_table, &cu);
4045
3019eac3 4046 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4047 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4048
4049 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4050
4051 do_cleanups (cleanups);
4052}
4053
3019eac3
DE
4054/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4055 does not lookup the specified DWO file.
4056 This cannot be used to read DWO files.
dee91e82
DE
4057
4058 THIS_CU->cu is always freed when done.
3019eac3
DE
4059 This is done in order to not leave THIS_CU->cu in a state where we have
4060 to care whether it refers to the "main" CU or the DWO CU.
4061 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4062
4063static void
4064init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4065 die_reader_func_ftype *die_reader_func,
4066 void *data)
4067{
4068 init_cutu_and_read_dies_no_follow (this_cu,
4069 &dwarf2_per_objfile->abbrev,
3019eac3 4070 NULL,
dee91e82
DE
4071 die_reader_func, data);
4072}
4073
4074/* die_reader_func for process_psymtab_comp_unit. */
4075
4076static void
4077process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4078 gdb_byte *info_ptr,
4079 struct die_info *comp_unit_die,
4080 int has_children,
4081 void *data)
4082{
4083 struct dwarf2_cu *cu = reader->cu;
4084 struct objfile *objfile = cu->objfile;
4085 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4086 struct attribute *attr;
4087 CORE_ADDR baseaddr;
4088 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4089 struct partial_symtab *pst;
4090 int has_pc_info;
4091 const char *filename;
95554aad 4092 int *want_partial_unit_ptr = data;
dee91e82 4093
95554aad
TT
4094 if (comp_unit_die->tag == DW_TAG_partial_unit
4095 && (want_partial_unit_ptr == NULL
4096 || !*want_partial_unit_ptr))
dee91e82
DE
4097 return;
4098
95554aad 4099 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4100
4101 cu->list_in_scope = &file_symbols;
c906108c 4102
93311388 4103 /* Allocate a new partial symbol table structure. */
dee91e82 4104 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4105 if (attr == NULL || !DW_STRING (attr))
4106 filename = "";
4107 else
4108 filename = DW_STRING (attr);
93311388 4109 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 4110 filename,
93311388
DE
4111 /* TEXTLOW and TEXTHIGH are set below. */
4112 0,
4113 objfile->global_psymbols.next,
4114 objfile->static_psymbols.next);
9750bca9 4115 pst->psymtabs_addrmap_supported = 1;
72bf9492 4116
dee91e82 4117 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4118 if (attr != NULL)
4119 pst->dirname = DW_STRING (attr);
72bf9492 4120
dee91e82 4121 pst->read_symtab_private = per_cu;
72bf9492 4122
93311388 4123 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4124
0963b4bd 4125 /* Store the function that reads in the rest of the symbol table. */
93311388 4126 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 4127
dee91e82 4128 per_cu->v.psymtab = pst;
c906108c 4129
dee91e82 4130 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4131
93311388
DE
4132 /* Possibly set the default values of LOWPC and HIGHPC from
4133 `DW_AT_ranges'. */
d85a05f0 4134 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4135 &best_highpc, cu, pst);
d85a05f0 4136 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4137 /* Store the contiguous range if it is not empty; it can be empty for
4138 CUs with no code. */
4139 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4140 best_lowpc + baseaddr,
4141 best_highpc + baseaddr - 1, pst);
93311388
DE
4142
4143 /* Check if comp unit has_children.
4144 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4145 If not, there's no more debug_info for this comp unit. */
d85a05f0 4146 if (has_children)
93311388
DE
4147 {
4148 struct partial_die_info *first_die;
4149 CORE_ADDR lowpc, highpc;
31ffec48 4150
93311388
DE
4151 lowpc = ((CORE_ADDR) -1);
4152 highpc = ((CORE_ADDR) 0);
c906108c 4153
dee91e82 4154 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4155
93311388 4156 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4157 ! has_pc_info, cu);
57c22c6c 4158
93311388
DE
4159 /* If we didn't find a lowpc, set it to highpc to avoid
4160 complaints from `maint check'. */
4161 if (lowpc == ((CORE_ADDR) -1))
4162 lowpc = highpc;
10b3939b 4163
93311388
DE
4164 /* If the compilation unit didn't have an explicit address range,
4165 then use the information extracted from its child dies. */
d85a05f0 4166 if (! has_pc_info)
93311388 4167 {
d85a05f0
DJ
4168 best_lowpc = lowpc;
4169 best_highpc = highpc;
93311388
DE
4170 }
4171 }
d85a05f0
DJ
4172 pst->textlow = best_lowpc + baseaddr;
4173 pst->texthigh = best_highpc + baseaddr;
c906108c 4174
93311388
DE
4175 pst->n_global_syms = objfile->global_psymbols.next -
4176 (objfile->global_psymbols.list + pst->globals_offset);
4177 pst->n_static_syms = objfile->static_psymbols.next -
4178 (objfile->static_psymbols.list + pst->statics_offset);
4179 sort_pst_symbols (pst);
c906108c 4180
95554aad
TT
4181 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4182 {
4183 int i;
4184 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4185 struct dwarf2_per_cu_data *iter;
4186
4187 /* Fill in 'dependencies' here; we fill in 'users' in a
4188 post-pass. */
4189 pst->number_of_dependencies = len;
4190 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4191 len * sizeof (struct symtab *));
4192 for (i = 0;
4193 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4194 i, iter);
4195 ++i)
4196 pst->dependencies[i] = iter->v.psymtab;
4197
4198 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4199 }
4200
3019eac3 4201 if (per_cu->is_debug_types)
348e048f
DE
4202 {
4203 /* It's not clear we want to do anything with stmt lists here.
4204 Waiting to see what gcc ultimately does. */
4205 }
d85a05f0 4206 else
93311388
DE
4207 {
4208 /* Get the list of files included in the current compilation unit,
4209 and build a psymtab for each of them. */
dee91e82 4210 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
93311388 4211 }
dee91e82 4212}
ae038cb0 4213
dee91e82
DE
4214/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4215 Process compilation unit THIS_CU for a psymtab. */
4216
4217static void
95554aad
TT
4218process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4219 int want_partial_unit)
dee91e82
DE
4220{
4221 /* If this compilation unit was already read in, free the
4222 cached copy in order to read it in again. This is
4223 necessary because we skipped some symbols when we first
4224 read in the compilation unit (see load_partial_dies).
4225 This problem could be avoided, but the benefit is unclear. */
4226 if (this_cu->cu != NULL)
4227 free_one_cached_comp_unit (this_cu);
4228
3019eac3 4229 gdb_assert (! this_cu->is_debug_types);
fd820528 4230 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
95554aad 4231 &want_partial_unit);
dee91e82
DE
4232
4233 /* Age out any secondary CUs. */
4234 age_cached_comp_units ();
93311388 4235}
ff013f42 4236
348e048f
DE
4237/* Traversal function for htab_traverse_noresize.
4238 Process one .debug_types comp-unit. */
4239
4240static int
dee91e82 4241process_psymtab_type_unit (void **slot, void *info)
348e048f 4242{
dee91e82
DE
4243 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4244 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 4245
fd820528 4246 gdb_assert (per_cu->is_debug_types);
a0f42c21 4247 gdb_assert (info == NULL);
348e048f 4248
dee91e82
DE
4249 /* If this compilation unit was already read in, free the
4250 cached copy in order to read it in again. This is
4251 necessary because we skipped some symbols when we first
4252 read in the compilation unit (see load_partial_dies).
4253 This problem could be avoided, but the benefit is unclear. */
4254 if (per_cu->cu != NULL)
4255 free_one_cached_comp_unit (per_cu);
4256
fd820528
DE
4257 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4258 NULL);
dee91e82
DE
4259
4260 /* Age out any secondary CUs. */
4261 age_cached_comp_units ();
348e048f
DE
4262
4263 return 1;
4264}
4265
4266/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4267 Build partial symbol tables for the .debug_types comp-units. */
4268
4269static void
4270build_type_psymtabs (struct objfile *objfile)
4271{
0e50663e 4272 if (! create_all_type_units (objfile))
348e048f
DE
4273 return;
4274
4275 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
dee91e82 4276 process_psymtab_type_unit, NULL);
348e048f
DE
4277}
4278
60606b2c
TT
4279/* A cleanup function that clears objfile's psymtabs_addrmap field. */
4280
4281static void
4282psymtabs_addrmap_cleanup (void *o)
4283{
4284 struct objfile *objfile = o;
ec61707d 4285
60606b2c
TT
4286 objfile->psymtabs_addrmap = NULL;
4287}
4288
95554aad
TT
4289/* Compute the 'user' field for each psymtab in OBJFILE. */
4290
4291static void
4292set_partial_user (struct objfile *objfile)
4293{
4294 int i;
4295
4296 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4297 {
4298 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4299 struct partial_symtab *pst = per_cu->v.psymtab;
4300 int j;
4301
4302 for (j = 0; j < pst->number_of_dependencies; ++j)
4303 {
4304 /* Set the 'user' field only if it is not already set. */
4305 if (pst->dependencies[j]->user == NULL)
4306 pst->dependencies[j]->user = pst;
4307 }
4308 }
4309}
4310
93311388
DE
4311/* Build the partial symbol table by doing a quick pass through the
4312 .debug_info and .debug_abbrev sections. */
72bf9492 4313
93311388 4314static void
c67a9c90 4315dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 4316{
60606b2c
TT
4317 struct cleanup *back_to, *addrmap_cleanup;
4318 struct obstack temp_obstack;
21b2bd31 4319 int i;
93311388 4320
98bfdba5
PA
4321 dwarf2_per_objfile->reading_partial_symbols = 1;
4322
be391dca 4323 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 4324
93311388
DE
4325 /* Any cached compilation units will be linked by the per-objfile
4326 read_in_chain. Make sure to free them when we're done. */
4327 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 4328
348e048f
DE
4329 build_type_psymtabs (objfile);
4330
93311388 4331 create_all_comp_units (objfile);
c906108c 4332
60606b2c
TT
4333 /* Create a temporary address map on a temporary obstack. We later
4334 copy this to the final obstack. */
4335 obstack_init (&temp_obstack);
4336 make_cleanup_obstack_free (&temp_obstack);
4337 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4338 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 4339
21b2bd31 4340 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 4341 {
21b2bd31 4342 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 4343
95554aad 4344 process_psymtab_comp_unit (per_cu, 0);
c906108c 4345 }
ff013f42 4346
95554aad
TT
4347 set_partial_user (objfile);
4348
ff013f42
JK
4349 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4350 &objfile->objfile_obstack);
60606b2c 4351 discard_cleanups (addrmap_cleanup);
ff013f42 4352
ae038cb0
DJ
4353 do_cleanups (back_to);
4354}
4355
3019eac3 4356/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
4357
4358static void
dee91e82
DE
4359load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4360 gdb_byte *info_ptr,
4361 struct die_info *comp_unit_die,
4362 int has_children,
4363 void *data)
ae038cb0 4364{
dee91e82 4365 struct dwarf2_cu *cu = reader->cu;
ae038cb0 4366
95554aad 4367 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 4368
ae038cb0
DJ
4369 /* Check if comp unit has_children.
4370 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4371 If not, there's no more debug_info for this comp unit. */
d85a05f0 4372 if (has_children)
dee91e82
DE
4373 load_partial_dies (reader, info_ptr, 0);
4374}
98bfdba5 4375
dee91e82
DE
4376/* Load the partial DIEs for a secondary CU into memory.
4377 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 4378
dee91e82
DE
4379static void
4380load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4381{
fd820528 4382 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
4383}
4384
9cdd5dbd
DE
4385/* Create a list of all compilation units in OBJFILE.
4386 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
4387
4388static void
4389create_all_comp_units (struct objfile *objfile)
4390{
4391 int n_allocated;
4392 int n_comp_units;
4393 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
4394 gdb_byte *info_ptr;
4395
4396 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4397 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4398
4399 n_comp_units = 0;
4400 n_allocated = 10;
4401 all_comp_units = xmalloc (n_allocated
4402 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 4403
3e43a32a
MS
4404 while (info_ptr < dwarf2_per_objfile->info.buffer
4405 + dwarf2_per_objfile->info.size)
ae038cb0 4406 {
c764a876 4407 unsigned int length, initial_length_size;
ae038cb0 4408 struct dwarf2_per_cu_data *this_cu;
b64f50a1 4409 sect_offset offset;
ae038cb0 4410
b64f50a1 4411 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4412
4413 /* Read just enough information to find out where the next
4414 compilation unit is. */
c764a876
DE
4415 length = read_initial_length (objfile->obfd, info_ptr,
4416 &initial_length_size);
ae038cb0
DJ
4417
4418 /* Save the compilation unit for later lookup. */
4419 this_cu = obstack_alloc (&objfile->objfile_obstack,
4420 sizeof (struct dwarf2_per_cu_data));
4421 memset (this_cu, 0, sizeof (*this_cu));
4422 this_cu->offset = offset;
c764a876 4423 this_cu->length = length + initial_length_size;
9291a0cd 4424 this_cu->objfile = objfile;
3019eac3 4425 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
ae038cb0
DJ
4426
4427 if (n_comp_units == n_allocated)
4428 {
4429 n_allocated *= 2;
4430 all_comp_units = xrealloc (all_comp_units,
4431 n_allocated
4432 * sizeof (struct dwarf2_per_cu_data *));
4433 }
4434 all_comp_units[n_comp_units++] = this_cu;
4435
4436 info_ptr = info_ptr + this_cu->length;
4437 }
4438
4439 dwarf2_per_objfile->all_comp_units
4440 = obstack_alloc (&objfile->objfile_obstack,
4441 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4442 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4443 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4444 xfree (all_comp_units);
4445 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
4446}
4447
5734ee8b
DJ
4448/* Process all loaded DIEs for compilation unit CU, starting at
4449 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4450 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4451 DW_AT_ranges). If NEED_PC is set, then this function will set
4452 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4453 and record the covered ranges in the addrmap. */
c906108c 4454
72bf9492
DJ
4455static void
4456scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 4457 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 4458{
72bf9492 4459 struct partial_die_info *pdi;
c906108c 4460
91c24f0a
DC
4461 /* Now, march along the PDI's, descending into ones which have
4462 interesting children but skipping the children of the other ones,
4463 until we reach the end of the compilation unit. */
c906108c 4464
72bf9492 4465 pdi = first_die;
91c24f0a 4466
72bf9492
DJ
4467 while (pdi != NULL)
4468 {
4469 fixup_partial_die (pdi, cu);
c906108c 4470
f55ee35c 4471 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
4472 children, so we need to look at them. Ditto for anonymous
4473 enums. */
933c6fe4 4474
72bf9492 4475 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
4476 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4477 || pdi->tag == DW_TAG_imported_unit)
c906108c 4478 {
72bf9492 4479 switch (pdi->tag)
c906108c
SS
4480 {
4481 case DW_TAG_subprogram:
5734ee8b 4482 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 4483 break;
72929c62 4484 case DW_TAG_constant:
c906108c
SS
4485 case DW_TAG_variable:
4486 case DW_TAG_typedef:
91c24f0a 4487 case DW_TAG_union_type:
72bf9492 4488 if (!pdi->is_declaration)
63d06c5c 4489 {
72bf9492 4490 add_partial_symbol (pdi, cu);
63d06c5c
DC
4491 }
4492 break;
c906108c 4493 case DW_TAG_class_type:
680b30c7 4494 case DW_TAG_interface_type:
c906108c 4495 case DW_TAG_structure_type:
72bf9492 4496 if (!pdi->is_declaration)
c906108c 4497 {
72bf9492 4498 add_partial_symbol (pdi, cu);
c906108c
SS
4499 }
4500 break;
91c24f0a 4501 case DW_TAG_enumeration_type:
72bf9492
DJ
4502 if (!pdi->is_declaration)
4503 add_partial_enumeration (pdi, cu);
c906108c
SS
4504 break;
4505 case DW_TAG_base_type:
a02abb62 4506 case DW_TAG_subrange_type:
c906108c 4507 /* File scope base type definitions are added to the partial
c5aa993b 4508 symbol table. */
72bf9492 4509 add_partial_symbol (pdi, cu);
c906108c 4510 break;
d9fa45fe 4511 case DW_TAG_namespace:
5734ee8b 4512 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 4513 break;
5d7cb8df
JK
4514 case DW_TAG_module:
4515 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4516 break;
95554aad
TT
4517 case DW_TAG_imported_unit:
4518 {
4519 struct dwarf2_per_cu_data *per_cu;
4520
4521 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4522 cu->objfile);
4523
4524 /* Go read the partial unit, if needed. */
4525 if (per_cu->v.psymtab == NULL)
4526 process_psymtab_comp_unit (per_cu, 1);
4527
4528 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4529 per_cu);
4530 }
4531 break;
c906108c
SS
4532 default:
4533 break;
4534 }
4535 }
4536
72bf9492
DJ
4537 /* If the die has a sibling, skip to the sibling. */
4538
4539 pdi = pdi->die_sibling;
4540 }
4541}
4542
4543/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 4544
72bf9492 4545 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
4546 name is concatenated with "::" and the partial DIE's name. For
4547 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
4548 Enumerators are an exception; they use the scope of their parent
4549 enumeration type, i.e. the name of the enumeration type is not
4550 prepended to the enumerator.
91c24f0a 4551
72bf9492
DJ
4552 There are two complexities. One is DW_AT_specification; in this
4553 case "parent" means the parent of the target of the specification,
4554 instead of the direct parent of the DIE. The other is compilers
4555 which do not emit DW_TAG_namespace; in this case we try to guess
4556 the fully qualified name of structure types from their members'
4557 linkage names. This must be done using the DIE's children rather
4558 than the children of any DW_AT_specification target. We only need
4559 to do this for structures at the top level, i.e. if the target of
4560 any DW_AT_specification (if any; otherwise the DIE itself) does not
4561 have a parent. */
4562
4563/* Compute the scope prefix associated with PDI's parent, in
4564 compilation unit CU. The result will be allocated on CU's
4565 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4566 field. NULL is returned if no prefix is necessary. */
4567static char *
4568partial_die_parent_scope (struct partial_die_info *pdi,
4569 struct dwarf2_cu *cu)
4570{
4571 char *grandparent_scope;
4572 struct partial_die_info *parent, *real_pdi;
91c24f0a 4573
72bf9492
DJ
4574 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4575 then this means the parent of the specification DIE. */
4576
4577 real_pdi = pdi;
72bf9492 4578 while (real_pdi->has_specification)
10b3939b 4579 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
4580
4581 parent = real_pdi->die_parent;
4582 if (parent == NULL)
4583 return NULL;
4584
4585 if (parent->scope_set)
4586 return parent->scope;
4587
4588 fixup_partial_die (parent, cu);
4589
10b3939b 4590 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 4591
acebe513
UW
4592 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4593 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4594 Work around this problem here. */
4595 if (cu->language == language_cplus
6e70227d 4596 && parent->tag == DW_TAG_namespace
acebe513
UW
4597 && strcmp (parent->name, "::") == 0
4598 && grandparent_scope == NULL)
4599 {
4600 parent->scope = NULL;
4601 parent->scope_set = 1;
4602 return NULL;
4603 }
4604
9c6c53f7
SA
4605 if (pdi->tag == DW_TAG_enumerator)
4606 /* Enumerators should not get the name of the enumeration as a prefix. */
4607 parent->scope = grandparent_scope;
4608 else if (parent->tag == DW_TAG_namespace
f55ee35c 4609 || parent->tag == DW_TAG_module
72bf9492
DJ
4610 || parent->tag == DW_TAG_structure_type
4611 || parent->tag == DW_TAG_class_type
680b30c7 4612 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
4613 || parent->tag == DW_TAG_union_type
4614 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
4615 {
4616 if (grandparent_scope == NULL)
4617 parent->scope = parent->name;
4618 else
3e43a32a
MS
4619 parent->scope = typename_concat (&cu->comp_unit_obstack,
4620 grandparent_scope,
f55ee35c 4621 parent->name, 0, cu);
72bf9492 4622 }
72bf9492
DJ
4623 else
4624 {
4625 /* FIXME drow/2004-04-01: What should we be doing with
4626 function-local names? For partial symbols, we should probably be
4627 ignoring them. */
4628 complaint (&symfile_complaints,
e2e0b3e5 4629 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 4630 parent->tag, pdi->offset.sect_off);
72bf9492 4631 parent->scope = grandparent_scope;
c906108c
SS
4632 }
4633
72bf9492
DJ
4634 parent->scope_set = 1;
4635 return parent->scope;
4636}
4637
4638/* Return the fully scoped name associated with PDI, from compilation unit
4639 CU. The result will be allocated with malloc. */
4568ecf9 4640
72bf9492
DJ
4641static char *
4642partial_die_full_name (struct partial_die_info *pdi,
4643 struct dwarf2_cu *cu)
4644{
4645 char *parent_scope;
4646
98bfdba5
PA
4647 /* If this is a template instantiation, we can not work out the
4648 template arguments from partial DIEs. So, unfortunately, we have
4649 to go through the full DIEs. At least any work we do building
4650 types here will be reused if full symbols are loaded later. */
4651 if (pdi->has_template_arguments)
4652 {
4653 fixup_partial_die (pdi, cu);
4654
4655 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4656 {
4657 struct die_info *die;
4658 struct attribute attr;
4659 struct dwarf2_cu *ref_cu = cu;
4660
b64f50a1 4661 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
4662 attr.name = 0;
4663 attr.form = DW_FORM_ref_addr;
4568ecf9 4664 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
4665 die = follow_die_ref (NULL, &attr, &ref_cu);
4666
4667 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4668 }
4669 }
4670
72bf9492
DJ
4671 parent_scope = partial_die_parent_scope (pdi, cu);
4672 if (parent_scope == NULL)
4673 return NULL;
4674 else
f55ee35c 4675 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4676}
4677
4678static void
72bf9492 4679add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4680{
e7c27a73 4681 struct objfile *objfile = cu->objfile;
c906108c 4682 CORE_ADDR addr = 0;
decbce07 4683 char *actual_name = NULL;
e142c38c 4684 CORE_ADDR baseaddr;
72bf9492 4685 int built_actual_name = 0;
e142c38c
DJ
4686
4687 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4688
94af9270
KS
4689 actual_name = partial_die_full_name (pdi, cu);
4690 if (actual_name)
4691 built_actual_name = 1;
63d06c5c 4692
72bf9492
DJ
4693 if (actual_name == NULL)
4694 actual_name = pdi->name;
4695
c906108c
SS
4696 switch (pdi->tag)
4697 {
4698 case DW_TAG_subprogram:
2cfa0c8d 4699 if (pdi->is_external || cu->language == language_ada)
c906108c 4700 {
2cfa0c8d
JB
4701 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4702 of the global scope. But in Ada, we want to be able to access
4703 nested procedures globally. So all Ada subprograms are stored
4704 in the global scope. */
f47fb265 4705 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4706 mst_text, objfile); */
f47fb265
MS
4707 add_psymbol_to_list (actual_name, strlen (actual_name),
4708 built_actual_name,
4709 VAR_DOMAIN, LOC_BLOCK,
4710 &objfile->global_psymbols,
4711 0, pdi->lowpc + baseaddr,
4712 cu->language, objfile);
c906108c
SS
4713 }
4714 else
4715 {
f47fb265 4716 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4717 mst_file_text, objfile); */
f47fb265
MS
4718 add_psymbol_to_list (actual_name, strlen (actual_name),
4719 built_actual_name,
4720 VAR_DOMAIN, LOC_BLOCK,
4721 &objfile->static_psymbols,
4722 0, pdi->lowpc + baseaddr,
4723 cu->language, objfile);
c906108c
SS
4724 }
4725 break;
72929c62
JB
4726 case DW_TAG_constant:
4727 {
4728 struct psymbol_allocation_list *list;
4729
4730 if (pdi->is_external)
4731 list = &objfile->global_psymbols;
4732 else
4733 list = &objfile->static_psymbols;
f47fb265
MS
4734 add_psymbol_to_list (actual_name, strlen (actual_name),
4735 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4736 list, 0, 0, cu->language, objfile);
72929c62
JB
4737 }
4738 break;
c906108c 4739 case DW_TAG_variable:
95554aad
TT
4740 if (pdi->d.locdesc)
4741 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 4742
95554aad 4743 if (pdi->d.locdesc
caac4577
JG
4744 && addr == 0
4745 && !dwarf2_per_objfile->has_section_at_zero)
4746 {
4747 /* A global or static variable may also have been stripped
4748 out by the linker if unused, in which case its address
4749 will be nullified; do not add such variables into partial
4750 symbol table then. */
4751 }
4752 else if (pdi->is_external)
c906108c
SS
4753 {
4754 /* Global Variable.
4755 Don't enter into the minimal symbol tables as there is
4756 a minimal symbol table entry from the ELF symbols already.
4757 Enter into partial symbol table if it has a location
4758 descriptor or a type.
4759 If the location descriptor is missing, new_symbol will create
4760 a LOC_UNRESOLVED symbol, the address of the variable will then
4761 be determined from the minimal symbol table whenever the variable
4762 is referenced.
4763 The address for the partial symbol table entry is not
4764 used by GDB, but it comes in handy for debugging partial symbol
4765 table building. */
4766
95554aad 4767 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
4768 add_psymbol_to_list (actual_name, strlen (actual_name),
4769 built_actual_name,
4770 VAR_DOMAIN, LOC_STATIC,
4771 &objfile->global_psymbols,
4772 0, addr + baseaddr,
4773 cu->language, objfile);
c906108c
SS
4774 }
4775 else
4776 {
0963b4bd 4777 /* Static Variable. Skip symbols without location descriptors. */
95554aad 4778 if (pdi->d.locdesc == NULL)
decbce07
MS
4779 {
4780 if (built_actual_name)
4781 xfree (actual_name);
4782 return;
4783 }
f47fb265 4784 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4785 mst_file_data, objfile); */
f47fb265
MS
4786 add_psymbol_to_list (actual_name, strlen (actual_name),
4787 built_actual_name,
4788 VAR_DOMAIN, LOC_STATIC,
4789 &objfile->static_psymbols,
4790 0, addr + baseaddr,
4791 cu->language, objfile);
c906108c
SS
4792 }
4793 break;
4794 case DW_TAG_typedef:
4795 case DW_TAG_base_type:
a02abb62 4796 case DW_TAG_subrange_type:
38d518c9 4797 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4798 built_actual_name,
176620f1 4799 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4800 &objfile->static_psymbols,
e142c38c 4801 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4802 break;
72bf9492
DJ
4803 case DW_TAG_namespace:
4804 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4805 built_actual_name,
72bf9492
DJ
4806 VAR_DOMAIN, LOC_TYPEDEF,
4807 &objfile->global_psymbols,
4808 0, (CORE_ADDR) 0, cu->language, objfile);
4809 break;
c906108c 4810 case DW_TAG_class_type:
680b30c7 4811 case DW_TAG_interface_type:
c906108c
SS
4812 case DW_TAG_structure_type:
4813 case DW_TAG_union_type:
4814 case DW_TAG_enumeration_type:
fa4028e9
JB
4815 /* Skip external references. The DWARF standard says in the section
4816 about "Structure, Union, and Class Type Entries": "An incomplete
4817 structure, union or class type is represented by a structure,
4818 union or class entry that does not have a byte size attribute
4819 and that has a DW_AT_declaration attribute." */
4820 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4821 {
4822 if (built_actual_name)
4823 xfree (actual_name);
4824 return;
4825 }
fa4028e9 4826
63d06c5c
DC
4827 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4828 static vs. global. */
38d518c9 4829 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4830 built_actual_name,
176620f1 4831 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4832 (cu->language == language_cplus
4833 || cu->language == language_java)
63d06c5c
DC
4834 ? &objfile->global_psymbols
4835 : &objfile->static_psymbols,
e142c38c 4836 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4837
c906108c
SS
4838 break;
4839 case DW_TAG_enumerator:
38d518c9 4840 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4841 built_actual_name,
176620f1 4842 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4843 (cu->language == language_cplus
4844 || cu->language == language_java)
f6fe98ef
DJ
4845 ? &objfile->global_psymbols
4846 : &objfile->static_psymbols,
e142c38c 4847 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4848 break;
4849 default:
4850 break;
4851 }
5c4e30ca 4852
72bf9492
DJ
4853 if (built_actual_name)
4854 xfree (actual_name);
c906108c
SS
4855}
4856
5c4e30ca
DC
4857/* Read a partial die corresponding to a namespace; also, add a symbol
4858 corresponding to that namespace to the symbol table. NAMESPACE is
4859 the name of the enclosing namespace. */
91c24f0a 4860
72bf9492
DJ
4861static void
4862add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4863 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4864 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4865{
72bf9492 4866 /* Add a symbol for the namespace. */
e7c27a73 4867
72bf9492 4868 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4869
4870 /* Now scan partial symbols in that namespace. */
4871
91c24f0a 4872 if (pdi->has_children)
5734ee8b 4873 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4874}
4875
5d7cb8df
JK
4876/* Read a partial die corresponding to a Fortran module. */
4877
4878static void
4879add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4880 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4881{
f55ee35c 4882 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4883
4884 if (pdi->has_children)
4885 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4886}
4887
bc30ff58
JB
4888/* Read a partial die corresponding to a subprogram and create a partial
4889 symbol for that subprogram. When the CU language allows it, this
4890 routine also defines a partial symbol for each nested subprogram
4891 that this subprogram contains.
6e70227d 4892
bc30ff58
JB
4893 DIE my also be a lexical block, in which case we simply search
4894 recursively for suprograms defined inside that lexical block.
4895 Again, this is only performed when the CU language allows this
4896 type of definitions. */
4897
4898static void
4899add_partial_subprogram (struct partial_die_info *pdi,
4900 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4901 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4902{
4903 if (pdi->tag == DW_TAG_subprogram)
4904 {
4905 if (pdi->has_pc_info)
4906 {
4907 if (pdi->lowpc < *lowpc)
4908 *lowpc = pdi->lowpc;
4909 if (pdi->highpc > *highpc)
4910 *highpc = pdi->highpc;
5734ee8b
DJ
4911 if (need_pc)
4912 {
4913 CORE_ADDR baseaddr;
4914 struct objfile *objfile = cu->objfile;
4915
4916 baseaddr = ANOFFSET (objfile->section_offsets,
4917 SECT_OFF_TEXT (objfile));
4918 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4919 pdi->lowpc + baseaddr,
4920 pdi->highpc - 1 + baseaddr,
9291a0cd 4921 cu->per_cu->v.psymtab);
5734ee8b 4922 }
481860b3
GB
4923 }
4924
4925 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4926 {
bc30ff58 4927 if (!pdi->is_declaration)
e8d05480
JB
4928 /* Ignore subprogram DIEs that do not have a name, they are
4929 illegal. Do not emit a complaint at this point, we will
4930 do so when we convert this psymtab into a symtab. */
4931 if (pdi->name)
4932 add_partial_symbol (pdi, cu);
bc30ff58
JB
4933 }
4934 }
6e70227d 4935
bc30ff58
JB
4936 if (! pdi->has_children)
4937 return;
4938
4939 if (cu->language == language_ada)
4940 {
4941 pdi = pdi->die_child;
4942 while (pdi != NULL)
4943 {
4944 fixup_partial_die (pdi, cu);
4945 if (pdi->tag == DW_TAG_subprogram
4946 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4947 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4948 pdi = pdi->die_sibling;
4949 }
4950 }
4951}
4952
91c24f0a
DC
4953/* Read a partial die corresponding to an enumeration type. */
4954
72bf9492
DJ
4955static void
4956add_partial_enumeration (struct partial_die_info *enum_pdi,
4957 struct dwarf2_cu *cu)
91c24f0a 4958{
72bf9492 4959 struct partial_die_info *pdi;
91c24f0a
DC
4960
4961 if (enum_pdi->name != NULL)
72bf9492
DJ
4962 add_partial_symbol (enum_pdi, cu);
4963
4964 pdi = enum_pdi->die_child;
4965 while (pdi)
91c24f0a 4966 {
72bf9492 4967 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4968 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 4969 else
72bf9492
DJ
4970 add_partial_symbol (pdi, cu);
4971 pdi = pdi->die_sibling;
91c24f0a 4972 }
91c24f0a
DC
4973}
4974
6caca83c
CC
4975/* Return the initial uleb128 in the die at INFO_PTR. */
4976
4977static unsigned int
4978peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4979{
4980 unsigned int bytes_read;
4981
4982 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4983}
4984
4bb7a0a7
DJ
4985/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4986 Return the corresponding abbrev, or NULL if the number is zero (indicating
4987 an empty DIE). In either case *BYTES_READ will be set to the length of
4988 the initial number. */
4989
4990static struct abbrev_info *
fe1b8b76 4991peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 4992 struct dwarf2_cu *cu)
4bb7a0a7
DJ
4993{
4994 bfd *abfd = cu->objfile->obfd;
4995 unsigned int abbrev_number;
4996 struct abbrev_info *abbrev;
4997
4998 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4999
5000 if (abbrev_number == 0)
5001 return NULL;
5002
5003 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
5004 if (!abbrev)
5005 {
3e43a32a
MS
5006 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
5007 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
5008 }
5009
5010 return abbrev;
5011}
5012
93311388
DE
5013/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5014 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
5015 DIE. Any children of the skipped DIEs will also be skipped. */
5016
fe1b8b76 5017static gdb_byte *
dee91e82 5018skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 5019{
dee91e82 5020 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
5021 struct abbrev_info *abbrev;
5022 unsigned int bytes_read;
5023
5024 while (1)
5025 {
5026 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5027 if (abbrev == NULL)
5028 return info_ptr + bytes_read;
5029 else
dee91e82 5030 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
5031 }
5032}
5033
93311388
DE
5034/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5035 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
5036 abbrev corresponding to that skipped uleb128 should be passed in
5037 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5038 children. */
5039
fe1b8b76 5040static gdb_byte *
dee91e82
DE
5041skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5042 struct abbrev_info *abbrev)
4bb7a0a7
DJ
5043{
5044 unsigned int bytes_read;
5045 struct attribute attr;
dee91e82
DE
5046 bfd *abfd = reader->abfd;
5047 struct dwarf2_cu *cu = reader->cu;
5048 gdb_byte *buffer = reader->buffer;
f664829e
DE
5049 const gdb_byte *buffer_end = reader->buffer_end;
5050 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
5051 unsigned int form, i;
5052
5053 for (i = 0; i < abbrev->num_attrs; i++)
5054 {
5055 /* The only abbrev we care about is DW_AT_sibling. */
5056 if (abbrev->attrs[i].name == DW_AT_sibling)
5057 {
dee91e82 5058 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 5059 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
5060 complaint (&symfile_complaints,
5061 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 5062 else
b64f50a1 5063 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
5064 }
5065
5066 /* If it isn't DW_AT_sibling, skip this attribute. */
5067 form = abbrev->attrs[i].form;
5068 skip_attribute:
5069 switch (form)
5070 {
4bb7a0a7 5071 case DW_FORM_ref_addr:
ae411497
TT
5072 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5073 and later it is offset sized. */
5074 if (cu->header.version == 2)
5075 info_ptr += cu->header.addr_size;
5076 else
5077 info_ptr += cu->header.offset_size;
5078 break;
5079 case DW_FORM_addr:
4bb7a0a7
DJ
5080 info_ptr += cu->header.addr_size;
5081 break;
5082 case DW_FORM_data1:
5083 case DW_FORM_ref1:
5084 case DW_FORM_flag:
5085 info_ptr += 1;
5086 break;
2dc7f7b3
TT
5087 case DW_FORM_flag_present:
5088 break;
4bb7a0a7
DJ
5089 case DW_FORM_data2:
5090 case DW_FORM_ref2:
5091 info_ptr += 2;
5092 break;
5093 case DW_FORM_data4:
5094 case DW_FORM_ref4:
5095 info_ptr += 4;
5096 break;
5097 case DW_FORM_data8:
5098 case DW_FORM_ref8:
55f1336d 5099 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
5100 info_ptr += 8;
5101 break;
5102 case DW_FORM_string:
9b1c24c8 5103 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
5104 info_ptr += bytes_read;
5105 break;
2dc7f7b3 5106 case DW_FORM_sec_offset:
4bb7a0a7
DJ
5107 case DW_FORM_strp:
5108 info_ptr += cu->header.offset_size;
5109 break;
2dc7f7b3 5110 case DW_FORM_exprloc:
4bb7a0a7
DJ
5111 case DW_FORM_block:
5112 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5113 info_ptr += bytes_read;
5114 break;
5115 case DW_FORM_block1:
5116 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5117 break;
5118 case DW_FORM_block2:
5119 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5120 break;
5121 case DW_FORM_block4:
5122 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5123 break;
5124 case DW_FORM_sdata:
5125 case DW_FORM_udata:
5126 case DW_FORM_ref_udata:
3019eac3
DE
5127 case DW_FORM_GNU_addr_index:
5128 case DW_FORM_GNU_str_index:
f664829e 5129 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
5130 break;
5131 case DW_FORM_indirect:
5132 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5133 info_ptr += bytes_read;
5134 /* We need to continue parsing from here, so just go back to
5135 the top. */
5136 goto skip_attribute;
5137
5138 default:
3e43a32a
MS
5139 error (_("Dwarf Error: Cannot handle %s "
5140 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
5141 dwarf_form_name (form),
5142 bfd_get_filename (abfd));
5143 }
5144 }
5145
5146 if (abbrev->has_children)
dee91e82 5147 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
5148 else
5149 return info_ptr;
5150}
5151
93311388 5152/* Locate ORIG_PDI's sibling.
dee91e82 5153 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 5154
fe1b8b76 5155static gdb_byte *
dee91e82
DE
5156locate_pdi_sibling (const struct die_reader_specs *reader,
5157 struct partial_die_info *orig_pdi,
5158 gdb_byte *info_ptr)
91c24f0a
DC
5159{
5160 /* Do we know the sibling already? */
72bf9492 5161
91c24f0a
DC
5162 if (orig_pdi->sibling)
5163 return orig_pdi->sibling;
5164
5165 /* Are there any children to deal with? */
5166
5167 if (!orig_pdi->has_children)
5168 return info_ptr;
5169
4bb7a0a7 5170 /* Skip the children the long way. */
91c24f0a 5171
dee91e82 5172 return skip_children (reader, info_ptr);
91c24f0a
DC
5173}
5174
c906108c
SS
5175/* Expand this partial symbol table into a full symbol table. */
5176
5177static void
fba45db2 5178dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 5179{
c906108c
SS
5180 if (pst != NULL)
5181 {
5182 if (pst->readin)
5183 {
3e43a32a
MS
5184 warning (_("bug: psymtab for %s is already read in."),
5185 pst->filename);
c906108c
SS
5186 }
5187 else
5188 {
5189 if (info_verbose)
5190 {
3e43a32a
MS
5191 printf_filtered (_("Reading in symbols for %s..."),
5192 pst->filename);
c906108c
SS
5193 gdb_flush (gdb_stdout);
5194 }
5195
10b3939b
DJ
5196 /* Restore our global data. */
5197 dwarf2_per_objfile = objfile_data (pst->objfile,
5198 dwarf2_objfile_data_key);
5199
b2ab525c
KB
5200 /* If this psymtab is constructed from a debug-only objfile, the
5201 has_section_at_zero flag will not necessarily be correct. We
5202 can get the correct value for this flag by looking at the data
5203 associated with the (presumably stripped) associated objfile. */
5204 if (pst->objfile->separate_debug_objfile_backlink)
5205 {
5206 struct dwarf2_per_objfile *dpo_backlink
5207 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5208 dwarf2_objfile_data_key);
9a619af0 5209
b2ab525c
KB
5210 dwarf2_per_objfile->has_section_at_zero
5211 = dpo_backlink->has_section_at_zero;
5212 }
5213
98bfdba5
PA
5214 dwarf2_per_objfile->reading_partial_symbols = 0;
5215
c906108c
SS
5216 psymtab_to_symtab_1 (pst);
5217
5218 /* Finish up the debug error message. */
5219 if (info_verbose)
a3f17187 5220 printf_filtered (_("done.\n"));
c906108c
SS
5221 }
5222 }
95554aad
TT
5223
5224 process_cu_includes ();
c906108c 5225}
9cdd5dbd
DE
5226\f
5227/* Reading in full CUs. */
c906108c 5228
10b3939b
DJ
5229/* Add PER_CU to the queue. */
5230
5231static void
95554aad
TT
5232queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5233 enum language pretend_language)
10b3939b
DJ
5234{
5235 struct dwarf2_queue_item *item;
5236
5237 per_cu->queued = 1;
5238 item = xmalloc (sizeof (*item));
5239 item->per_cu = per_cu;
95554aad 5240 item->pretend_language = pretend_language;
10b3939b
DJ
5241 item->next = NULL;
5242
5243 if (dwarf2_queue == NULL)
5244 dwarf2_queue = item;
5245 else
5246 dwarf2_queue_tail->next = item;
5247
5248 dwarf2_queue_tail = item;
5249}
5250
5251/* Process the queue. */
5252
5253static void
a0f42c21 5254process_queue (void)
10b3939b
DJ
5255{
5256 struct dwarf2_queue_item *item, *next_item;
5257
03dd20cc
DJ
5258 /* The queue starts out with one item, but following a DIE reference
5259 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
5260 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5261 {
9291a0cd
TT
5262 if (dwarf2_per_objfile->using_index
5263 ? !item->per_cu->v.quick->symtab
5264 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
95554aad 5265 process_full_comp_unit (item->per_cu, item->pretend_language);
10b3939b
DJ
5266
5267 item->per_cu->queued = 0;
5268 next_item = item->next;
5269 xfree (item);
5270 }
5271
5272 dwarf2_queue_tail = NULL;
5273}
5274
5275/* Free all allocated queue entries. This function only releases anything if
5276 an error was thrown; if the queue was processed then it would have been
5277 freed as we went along. */
5278
5279static void
5280dwarf2_release_queue (void *dummy)
5281{
5282 struct dwarf2_queue_item *item, *last;
5283
5284 item = dwarf2_queue;
5285 while (item)
5286 {
5287 /* Anything still marked queued is likely to be in an
5288 inconsistent state, so discard it. */
5289 if (item->per_cu->queued)
5290 {
5291 if (item->per_cu->cu != NULL)
dee91e82 5292 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
5293 item->per_cu->queued = 0;
5294 }
5295
5296 last = item;
5297 item = item->next;
5298 xfree (last);
5299 }
5300
5301 dwarf2_queue = dwarf2_queue_tail = NULL;
5302}
5303
5304/* Read in full symbols for PST, and anything it depends on. */
5305
c906108c 5306static void
fba45db2 5307psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 5308{
10b3939b 5309 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
5310 int i;
5311
95554aad
TT
5312 if (pst->readin)
5313 return;
5314
aaa75496 5315 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
5316 if (!pst->dependencies[i]->readin
5317 && pst->dependencies[i]->user == NULL)
aaa75496
JB
5318 {
5319 /* Inform about additional files that need to be read in. */
5320 if (info_verbose)
5321 {
a3f17187 5322 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
5323 fputs_filtered (" ", gdb_stdout);
5324 wrap_here ("");
5325 fputs_filtered ("and ", gdb_stdout);
5326 wrap_here ("");
5327 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 5328 wrap_here (""); /* Flush output. */
aaa75496
JB
5329 gdb_flush (gdb_stdout);
5330 }
5331 psymtab_to_symtab_1 (pst->dependencies[i]);
5332 }
5333
e38df1d0 5334 per_cu = pst->read_symtab_private;
10b3939b
DJ
5335
5336 if (per_cu == NULL)
aaa75496
JB
5337 {
5338 /* It's an include file, no symbols to read for it.
5339 Everything is in the parent symtab. */
5340 pst->readin = 1;
5341 return;
5342 }
c906108c 5343
a0f42c21 5344 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
5345}
5346
dee91e82
DE
5347/* Trivial hash function for die_info: the hash value of a DIE
5348 is its offset in .debug_info for this objfile. */
10b3939b 5349
dee91e82
DE
5350static hashval_t
5351die_hash (const void *item)
10b3939b 5352{
dee91e82 5353 const struct die_info *die = item;
6502dd73 5354
dee91e82
DE
5355 return die->offset.sect_off;
5356}
63d06c5c 5357
dee91e82
DE
5358/* Trivial comparison function for die_info structures: two DIEs
5359 are equal if they have the same offset. */
98bfdba5 5360
dee91e82
DE
5361static int
5362die_eq (const void *item_lhs, const void *item_rhs)
5363{
5364 const struct die_info *die_lhs = item_lhs;
5365 const struct die_info *die_rhs = item_rhs;
c906108c 5366
dee91e82
DE
5367 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5368}
c906108c 5369
dee91e82
DE
5370/* die_reader_func for load_full_comp_unit.
5371 This is identical to read_signatured_type_reader,
5372 but is kept separate for now. */
c906108c 5373
dee91e82
DE
5374static void
5375load_full_comp_unit_reader (const struct die_reader_specs *reader,
5376 gdb_byte *info_ptr,
5377 struct die_info *comp_unit_die,
5378 int has_children,
5379 void *data)
5380{
5381 struct dwarf2_cu *cu = reader->cu;
95554aad 5382 enum language *language_ptr = data;
6caca83c 5383
dee91e82
DE
5384 gdb_assert (cu->die_hash == NULL);
5385 cu->die_hash =
5386 htab_create_alloc_ex (cu->header.length / 12,
5387 die_hash,
5388 die_eq,
5389 NULL,
5390 &cu->comp_unit_obstack,
5391 hashtab_obstack_allocate,
5392 dummy_obstack_deallocate);
e142c38c 5393
dee91e82
DE
5394 if (has_children)
5395 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5396 &info_ptr, comp_unit_die);
5397 cu->dies = comp_unit_die;
5398 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
5399
5400 /* We try not to read any attributes in this function, because not
9cdd5dbd 5401 all CUs needed for references have been loaded yet, and symbol
10b3939b 5402 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
5403 or we won't be able to build types correctly.
5404 Similarly, if we do not read the producer, we can not apply
5405 producer-specific interpretation. */
95554aad 5406 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 5407}
10b3939b 5408
dee91e82 5409/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 5410
dee91e82 5411static void
95554aad
TT
5412load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5413 enum language pretend_language)
dee91e82 5414{
3019eac3 5415 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 5416
95554aad
TT
5417 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5418 &pretend_language);
10b3939b
DJ
5419}
5420
3da10d80
KS
5421/* Add a DIE to the delayed physname list. */
5422
5423static void
5424add_to_method_list (struct type *type, int fnfield_index, int index,
5425 const char *name, struct die_info *die,
5426 struct dwarf2_cu *cu)
5427{
5428 struct delayed_method_info mi;
5429 mi.type = type;
5430 mi.fnfield_index = fnfield_index;
5431 mi.index = index;
5432 mi.name = name;
5433 mi.die = die;
5434 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5435}
5436
5437/* A cleanup for freeing the delayed method list. */
5438
5439static void
5440free_delayed_list (void *ptr)
5441{
5442 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5443 if (cu->method_list != NULL)
5444 {
5445 VEC_free (delayed_method_info, cu->method_list);
5446 cu->method_list = NULL;
5447 }
5448}
5449
5450/* Compute the physnames of any methods on the CU's method list.
5451
5452 The computation of method physnames is delayed in order to avoid the
5453 (bad) condition that one of the method's formal parameters is of an as yet
5454 incomplete type. */
5455
5456static void
5457compute_delayed_physnames (struct dwarf2_cu *cu)
5458{
5459 int i;
5460 struct delayed_method_info *mi;
5461 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5462 {
1d06ead6 5463 const char *physname;
3da10d80
KS
5464 struct fn_fieldlist *fn_flp
5465 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 5466 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
5467 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5468 }
5469}
5470
a766d390
DE
5471/* Go objects should be embedded in a DW_TAG_module DIE,
5472 and it's not clear if/how imported objects will appear.
5473 To keep Go support simple until that's worked out,
5474 go back through what we've read and create something usable.
5475 We could do this while processing each DIE, and feels kinda cleaner,
5476 but that way is more invasive.
5477 This is to, for example, allow the user to type "p var" or "b main"
5478 without having to specify the package name, and allow lookups
5479 of module.object to work in contexts that use the expression
5480 parser. */
5481
5482static void
5483fixup_go_packaging (struct dwarf2_cu *cu)
5484{
5485 char *package_name = NULL;
5486 struct pending *list;
5487 int i;
5488
5489 for (list = global_symbols; list != NULL; list = list->next)
5490 {
5491 for (i = 0; i < list->nsyms; ++i)
5492 {
5493 struct symbol *sym = list->symbol[i];
5494
5495 if (SYMBOL_LANGUAGE (sym) == language_go
5496 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5497 {
5498 char *this_package_name = go_symbol_package_name (sym);
5499
5500 if (this_package_name == NULL)
5501 continue;
5502 if (package_name == NULL)
5503 package_name = this_package_name;
5504 else
5505 {
5506 if (strcmp (package_name, this_package_name) != 0)
5507 complaint (&symfile_complaints,
5508 _("Symtab %s has objects from two different Go packages: %s and %s"),
5509 (sym->symtab && sym->symtab->filename
5510 ? sym->symtab->filename
5511 : cu->objfile->name),
5512 this_package_name, package_name);
5513 xfree (this_package_name);
5514 }
5515 }
5516 }
5517 }
5518
5519 if (package_name != NULL)
5520 {
5521 struct objfile *objfile = cu->objfile;
5522 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5523 package_name, objfile);
5524 struct symbol *sym;
5525
5526 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5527
5528 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5529 SYMBOL_SET_LANGUAGE (sym, language_go);
5530 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5531 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5532 e.g., "main" finds the "main" module and not C's main(). */
5533 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5534 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5535 SYMBOL_TYPE (sym) = type;
5536
5537 add_symbol_to_list (sym, &global_symbols);
5538
5539 xfree (package_name);
5540 }
5541}
5542
95554aad
TT
5543static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5544
5545/* Return the symtab for PER_CU. This works properly regardless of
5546 whether we're using the index or psymtabs. */
5547
5548static struct symtab *
5549get_symtab (struct dwarf2_per_cu_data *per_cu)
5550{
5551 return (dwarf2_per_objfile->using_index
5552 ? per_cu->v.quick->symtab
5553 : per_cu->v.psymtab->symtab);
5554}
5555
5556/* A helper function for computing the list of all symbol tables
5557 included by PER_CU. */
5558
5559static void
5560recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5561 htab_t all_children,
5562 struct dwarf2_per_cu_data *per_cu)
5563{
5564 void **slot;
5565 int ix;
5566 struct dwarf2_per_cu_data *iter;
5567
5568 slot = htab_find_slot (all_children, per_cu, INSERT);
5569 if (*slot != NULL)
5570 {
5571 /* This inclusion and its children have been processed. */
5572 return;
5573 }
5574
5575 *slot = per_cu;
5576 /* Only add a CU if it has a symbol table. */
5577 if (get_symtab (per_cu) != NULL)
5578 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5579
5580 for (ix = 0;
5581 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5582 ++ix)
5583 recursively_compute_inclusions (result, all_children, iter);
5584}
5585
5586/* Compute the symtab 'includes' fields for the symtab related to
5587 PER_CU. */
5588
5589static void
5590compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5591{
5592 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5593 {
5594 int ix, len;
5595 struct dwarf2_per_cu_data *iter;
5596 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5597 htab_t all_children;
5598 struct symtab *symtab = get_symtab (per_cu);
5599
5600 /* If we don't have a symtab, we can just skip this case. */
5601 if (symtab == NULL)
5602 return;
5603
5604 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5605 NULL, xcalloc, xfree);
5606
5607 for (ix = 0;
5608 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5609 ix, iter);
5610 ++ix)
5611 recursively_compute_inclusions (&result_children, all_children, iter);
5612
5613 /* Now we have a transitive closure of all the included CUs, so
5614 we can convert it to a list of symtabs. */
5615 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5616 symtab->includes
5617 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5618 (len + 1) * sizeof (struct symtab *));
5619 for (ix = 0;
5620 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5621 ++ix)
5622 symtab->includes[ix] = get_symtab (iter);
5623 symtab->includes[len] = NULL;
5624
5625 VEC_free (dwarf2_per_cu_ptr, result_children);
5626 htab_delete (all_children);
5627 }
5628}
5629
5630/* Compute the 'includes' field for the symtabs of all the CUs we just
5631 read. */
5632
5633static void
5634process_cu_includes (void)
5635{
5636 int ix;
5637 struct dwarf2_per_cu_data *iter;
5638
5639 for (ix = 0;
5640 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5641 ix, iter);
5642 ++ix)
5643 compute_symtab_includes (iter);
5644
5645 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5646}
5647
9cdd5dbd 5648/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
5649 already been loaded into memory. */
5650
5651static void
95554aad
TT
5652process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5653 enum language pretend_language)
10b3939b 5654{
10b3939b 5655 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 5656 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
5657 CORE_ADDR lowpc, highpc;
5658 struct symtab *symtab;
3da10d80 5659 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
5660 CORE_ADDR baseaddr;
5661
5662 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5663
10b3939b
DJ
5664 buildsym_init ();
5665 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 5666 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
5667
5668 cu->list_in_scope = &file_symbols;
c906108c 5669
95554aad
TT
5670 cu->language = pretend_language;
5671 cu->language_defn = language_def (cu->language);
5672
c906108c 5673 /* Do line number decoding in read_file_scope () */
10b3939b 5674 process_die (cu->dies, cu);
c906108c 5675
a766d390
DE
5676 /* For now fudge the Go package. */
5677 if (cu->language == language_go)
5678 fixup_go_packaging (cu);
5679
3da10d80
KS
5680 /* Now that we have processed all the DIEs in the CU, all the types
5681 should be complete, and it should now be safe to compute all of the
5682 physnames. */
5683 compute_delayed_physnames (cu);
5684 do_cleanups (delayed_list_cleanup);
5685
fae299cd
DC
5686 /* Some compilers don't define a DW_AT_high_pc attribute for the
5687 compilation unit. If the DW_AT_high_pc is missing, synthesize
5688 it, by scanning the DIE's below the compilation unit. */
10b3939b 5689 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 5690
613e1657 5691 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 5692
8be455d7 5693 if (symtab != NULL)
c906108c 5694 {
df15bd07 5695 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 5696
8be455d7
JK
5697 /* Set symtab language to language from DW_AT_language. If the
5698 compilation is from a C file generated by language preprocessors, do
5699 not set the language if it was already deduced by start_subfile. */
5700 if (!(cu->language == language_c && symtab->language != language_c))
5701 symtab->language = cu->language;
5702
5703 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5704 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
5705 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5706 there were bugs in prologue debug info, fixed later in GCC-4.5
5707 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
5708
5709 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5710 needed, it would be wrong due to missing DW_AT_producer there.
5711
5712 Still one can confuse GDB by using non-standard GCC compilation
5713 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5714 */
ab260dad 5715 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 5716 symtab->locations_valid = 1;
e0d00bc7
JK
5717
5718 if (gcc_4_minor >= 5)
5719 symtab->epilogue_unwind_valid = 1;
96408a79
SA
5720
5721 symtab->call_site_htab = cu->call_site_htab;
c906108c 5722 }
9291a0cd
TT
5723
5724 if (dwarf2_per_objfile->using_index)
5725 per_cu->v.quick->symtab = symtab;
5726 else
5727 {
5728 struct partial_symtab *pst = per_cu->v.psymtab;
5729 pst->symtab = symtab;
5730 pst->readin = 1;
5731 }
c906108c 5732
95554aad
TT
5733 /* Push it for inclusion processing later. */
5734 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5735
c906108c
SS
5736 do_cleanups (back_to);
5737}
5738
95554aad
TT
5739/* Process an imported unit DIE. */
5740
5741static void
5742process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5743{
5744 struct attribute *attr;
5745
5746 attr = dwarf2_attr (die, DW_AT_import, cu);
5747 if (attr != NULL)
5748 {
5749 struct dwarf2_per_cu_data *per_cu;
5750 struct symtab *imported_symtab;
5751 sect_offset offset;
5752
5753 offset = dwarf2_get_ref_die_offset (attr);
5754 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5755
5756 /* Queue the unit, if needed. */
5757 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
5758 load_full_comp_unit (per_cu, cu->language);
5759
5760 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5761 per_cu);
5762 }
5763}
5764
c906108c
SS
5765/* Process a die and its children. */
5766
5767static void
e7c27a73 5768process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
5769{
5770 switch (die->tag)
5771 {
5772 case DW_TAG_padding:
5773 break;
5774 case DW_TAG_compile_unit:
95554aad 5775 case DW_TAG_partial_unit:
e7c27a73 5776 read_file_scope (die, cu);
c906108c 5777 break;
348e048f
DE
5778 case DW_TAG_type_unit:
5779 read_type_unit_scope (die, cu);
5780 break;
c906108c 5781 case DW_TAG_subprogram:
c906108c 5782 case DW_TAG_inlined_subroutine:
edb3359d 5783 read_func_scope (die, cu);
c906108c
SS
5784 break;
5785 case DW_TAG_lexical_block:
14898363
L
5786 case DW_TAG_try_block:
5787 case DW_TAG_catch_block:
e7c27a73 5788 read_lexical_block_scope (die, cu);
c906108c 5789 break;
96408a79
SA
5790 case DW_TAG_GNU_call_site:
5791 read_call_site_scope (die, cu);
5792 break;
c906108c 5793 case DW_TAG_class_type:
680b30c7 5794 case DW_TAG_interface_type:
c906108c
SS
5795 case DW_TAG_structure_type:
5796 case DW_TAG_union_type:
134d01f1 5797 process_structure_scope (die, cu);
c906108c
SS
5798 break;
5799 case DW_TAG_enumeration_type:
134d01f1 5800 process_enumeration_scope (die, cu);
c906108c 5801 break;
134d01f1 5802
f792889a
DJ
5803 /* These dies have a type, but processing them does not create
5804 a symbol or recurse to process the children. Therefore we can
5805 read them on-demand through read_type_die. */
c906108c 5806 case DW_TAG_subroutine_type:
72019c9c 5807 case DW_TAG_set_type:
c906108c 5808 case DW_TAG_array_type:
c906108c 5809 case DW_TAG_pointer_type:
c906108c 5810 case DW_TAG_ptr_to_member_type:
c906108c 5811 case DW_TAG_reference_type:
c906108c 5812 case DW_TAG_string_type:
c906108c 5813 break;
134d01f1 5814
c906108c 5815 case DW_TAG_base_type:
a02abb62 5816 case DW_TAG_subrange_type:
cb249c71 5817 case DW_TAG_typedef:
134d01f1
DJ
5818 /* Add a typedef symbol for the type definition, if it has a
5819 DW_AT_name. */
f792889a 5820 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 5821 break;
c906108c 5822 case DW_TAG_common_block:
e7c27a73 5823 read_common_block (die, cu);
c906108c
SS
5824 break;
5825 case DW_TAG_common_inclusion:
5826 break;
d9fa45fe 5827 case DW_TAG_namespace:
63d06c5c 5828 processing_has_namespace_info = 1;
e7c27a73 5829 read_namespace (die, cu);
d9fa45fe 5830 break;
5d7cb8df 5831 case DW_TAG_module:
f55ee35c 5832 processing_has_namespace_info = 1;
5d7cb8df
JK
5833 read_module (die, cu);
5834 break;
d9fa45fe
DC
5835 case DW_TAG_imported_declaration:
5836 case DW_TAG_imported_module:
63d06c5c 5837 processing_has_namespace_info = 1;
27aa8d6a
SW
5838 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5839 || cu->language != language_fortran))
5840 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5841 dwarf_tag_name (die->tag));
5842 read_import_statement (die, cu);
d9fa45fe 5843 break;
95554aad
TT
5844
5845 case DW_TAG_imported_unit:
5846 process_imported_unit_die (die, cu);
5847 break;
5848
c906108c 5849 default:
e7c27a73 5850 new_symbol (die, NULL, cu);
c906108c
SS
5851 break;
5852 }
5853}
5854
94af9270
KS
5855/* A helper function for dwarf2_compute_name which determines whether DIE
5856 needs to have the name of the scope prepended to the name listed in the
5857 die. */
5858
5859static int
5860die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5861{
1c809c68
TT
5862 struct attribute *attr;
5863
94af9270
KS
5864 switch (die->tag)
5865 {
5866 case DW_TAG_namespace:
5867 case DW_TAG_typedef:
5868 case DW_TAG_class_type:
5869 case DW_TAG_interface_type:
5870 case DW_TAG_structure_type:
5871 case DW_TAG_union_type:
5872 case DW_TAG_enumeration_type:
5873 case DW_TAG_enumerator:
5874 case DW_TAG_subprogram:
5875 case DW_TAG_member:
5876 return 1;
5877
5878 case DW_TAG_variable:
c2b0a229 5879 case DW_TAG_constant:
94af9270
KS
5880 /* We only need to prefix "globally" visible variables. These include
5881 any variable marked with DW_AT_external or any variable that
5882 lives in a namespace. [Variables in anonymous namespaces
5883 require prefixing, but they are not DW_AT_external.] */
5884
5885 if (dwarf2_attr (die, DW_AT_specification, cu))
5886 {
5887 struct dwarf2_cu *spec_cu = cu;
9a619af0 5888
94af9270
KS
5889 return die_needs_namespace (die_specification (die, &spec_cu),
5890 spec_cu);
5891 }
5892
1c809c68 5893 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
5894 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5895 && die->parent->tag != DW_TAG_module)
1c809c68
TT
5896 return 0;
5897 /* A variable in a lexical block of some kind does not need a
5898 namespace, even though in C++ such variables may be external
5899 and have a mangled name. */
5900 if (die->parent->tag == DW_TAG_lexical_block
5901 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5902 || die->parent->tag == DW_TAG_catch_block
5903 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5904 return 0;
5905 return 1;
94af9270
KS
5906
5907 default:
5908 return 0;
5909 }
5910}
5911
98bfdba5
PA
5912/* Retrieve the last character from a mem_file. */
5913
5914static void
5915do_ui_file_peek_last (void *object, const char *buffer, long length)
5916{
5917 char *last_char_p = (char *) object;
5918
5919 if (length > 0)
5920 *last_char_p = buffer[length - 1];
5921}
5922
94af9270 5923/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
5924 compute the physname for the object, which include a method's:
5925 - formal parameters (C++/Java),
5926 - receiver type (Go),
5927 - return type (Java).
5928
5929 The term "physname" is a bit confusing.
5930 For C++, for example, it is the demangled name.
5931 For Go, for example, it's the mangled name.
94af9270 5932
af6b7be1
JB
5933 For Ada, return the DIE's linkage name rather than the fully qualified
5934 name. PHYSNAME is ignored..
5935
94af9270
KS
5936 The result is allocated on the objfile_obstack and canonicalized. */
5937
5938static const char *
5939dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5940 int physname)
5941{
bb5ed363
DE
5942 struct objfile *objfile = cu->objfile;
5943
94af9270
KS
5944 if (name == NULL)
5945 name = dwarf2_name (die, cu);
5946
f55ee35c
JK
5947 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5948 compute it by typename_concat inside GDB. */
5949 if (cu->language == language_ada
5950 || (cu->language == language_fortran && physname))
5951 {
5952 /* For Ada unit, we prefer the linkage name over the name, as
5953 the former contains the exported name, which the user expects
5954 to be able to reference. Ideally, we want the user to be able
5955 to reference this entity using either natural or linkage name,
5956 but we haven't started looking at this enhancement yet. */
5957 struct attribute *attr;
5958
5959 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5960 if (attr == NULL)
5961 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5962 if (attr && DW_STRING (attr))
5963 return DW_STRING (attr);
5964 }
5965
94af9270
KS
5966 /* These are the only languages we know how to qualify names in. */
5967 if (name != NULL
f55ee35c
JK
5968 && (cu->language == language_cplus || cu->language == language_java
5969 || cu->language == language_fortran))
94af9270
KS
5970 {
5971 if (die_needs_namespace (die, cu))
5972 {
5973 long length;
0d5cff50 5974 const char *prefix;
94af9270
KS
5975 struct ui_file *buf;
5976
5977 prefix = determine_prefix (die, cu);
5978 buf = mem_fileopen ();
5979 if (*prefix != '\0')
5980 {
f55ee35c
JK
5981 char *prefixed_name = typename_concat (NULL, prefix, name,
5982 physname, cu);
9a619af0 5983
94af9270
KS
5984 fputs_unfiltered (prefixed_name, buf);
5985 xfree (prefixed_name);
5986 }
5987 else
62d5b8da 5988 fputs_unfiltered (name, buf);
94af9270 5989
98bfdba5
PA
5990 /* Template parameters may be specified in the DIE's DW_AT_name, or
5991 as children with DW_TAG_template_type_param or
5992 DW_TAG_value_type_param. If the latter, add them to the name
5993 here. If the name already has template parameters, then
5994 skip this step; some versions of GCC emit both, and
5995 it is more efficient to use the pre-computed name.
5996
5997 Something to keep in mind about this process: it is very
5998 unlikely, or in some cases downright impossible, to produce
5999 something that will match the mangled name of a function.
6000 If the definition of the function has the same debug info,
6001 we should be able to match up with it anyway. But fallbacks
6002 using the minimal symbol, for instance to find a method
6003 implemented in a stripped copy of libstdc++, will not work.
6004 If we do not have debug info for the definition, we will have to
6005 match them up some other way.
6006
6007 When we do name matching there is a related problem with function
6008 templates; two instantiated function templates are allowed to
6009 differ only by their return types, which we do not add here. */
6010
6011 if (cu->language == language_cplus && strchr (name, '<') == NULL)
6012 {
6013 struct attribute *attr;
6014 struct die_info *child;
6015 int first = 1;
6016
6017 die->building_fullname = 1;
6018
6019 for (child = die->child; child != NULL; child = child->sibling)
6020 {
6021 struct type *type;
12df843f 6022 LONGEST value;
98bfdba5
PA
6023 gdb_byte *bytes;
6024 struct dwarf2_locexpr_baton *baton;
6025 struct value *v;
6026
6027 if (child->tag != DW_TAG_template_type_param
6028 && child->tag != DW_TAG_template_value_param)
6029 continue;
6030
6031 if (first)
6032 {
6033 fputs_unfiltered ("<", buf);
6034 first = 0;
6035 }
6036 else
6037 fputs_unfiltered (", ", buf);
6038
6039 attr = dwarf2_attr (child, DW_AT_type, cu);
6040 if (attr == NULL)
6041 {
6042 complaint (&symfile_complaints,
6043 _("template parameter missing DW_AT_type"));
6044 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6045 continue;
6046 }
6047 type = die_type (child, cu);
6048
6049 if (child->tag == DW_TAG_template_type_param)
6050 {
6051 c_print_type (type, "", buf, -1, 0);
6052 continue;
6053 }
6054
6055 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6056 if (attr == NULL)
6057 {
6058 complaint (&symfile_complaints,
3e43a32a
MS
6059 _("template parameter missing "
6060 "DW_AT_const_value"));
98bfdba5
PA
6061 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6062 continue;
6063 }
6064
6065 dwarf2_const_value_attr (attr, type, name,
6066 &cu->comp_unit_obstack, cu,
6067 &value, &bytes, &baton);
6068
6069 if (TYPE_NOSIGN (type))
6070 /* GDB prints characters as NUMBER 'CHAR'. If that's
6071 changed, this can use value_print instead. */
6072 c_printchar (value, type, buf);
6073 else
6074 {
6075 struct value_print_options opts;
6076
6077 if (baton != NULL)
6078 v = dwarf2_evaluate_loc_desc (type, NULL,
6079 baton->data,
6080 baton->size,
6081 baton->per_cu);
6082 else if (bytes != NULL)
6083 {
6084 v = allocate_value (type);
6085 memcpy (value_contents_writeable (v), bytes,
6086 TYPE_LENGTH (type));
6087 }
6088 else
6089 v = value_from_longest (type, value);
6090
3e43a32a
MS
6091 /* Specify decimal so that we do not depend on
6092 the radix. */
98bfdba5
PA
6093 get_formatted_print_options (&opts, 'd');
6094 opts.raw = 1;
6095 value_print (v, buf, &opts);
6096 release_value (v);
6097 value_free (v);
6098 }
6099 }
6100
6101 die->building_fullname = 0;
6102
6103 if (!first)
6104 {
6105 /* Close the argument list, with a space if necessary
6106 (nested templates). */
6107 char last_char = '\0';
6108 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6109 if (last_char == '>')
6110 fputs_unfiltered (" >", buf);
6111 else
6112 fputs_unfiltered (">", buf);
6113 }
6114 }
6115
94af9270
KS
6116 /* For Java and C++ methods, append formal parameter type
6117 information, if PHYSNAME. */
6e70227d 6118
94af9270
KS
6119 if (physname && die->tag == DW_TAG_subprogram
6120 && (cu->language == language_cplus
6121 || cu->language == language_java))
6122 {
6123 struct type *type = read_type_die (die, cu);
6124
3167638f 6125 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
6126
6127 if (cu->language == language_java)
6128 {
6129 /* For java, we must append the return type to method
0963b4bd 6130 names. */
94af9270
KS
6131 if (die->tag == DW_TAG_subprogram)
6132 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6133 0, 0);
6134 }
6135 else if (cu->language == language_cplus)
6136 {
60430eff
DJ
6137 /* Assume that an artificial first parameter is
6138 "this", but do not crash if it is not. RealView
6139 marks unnamed (and thus unused) parameters as
6140 artificial; there is no way to differentiate
6141 the two cases. */
94af9270
KS
6142 if (TYPE_NFIELDS (type) > 0
6143 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 6144 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
6145 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6146 0))))
94af9270
KS
6147 fputs_unfiltered (" const", buf);
6148 }
6149 }
6150
bb5ed363 6151 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
6152 &length);
6153 ui_file_delete (buf);
6154
6155 if (cu->language == language_cplus)
6156 {
6157 char *cname
6158 = dwarf2_canonicalize_name (name, cu,
bb5ed363 6159 &objfile->objfile_obstack);
9a619af0 6160
94af9270
KS
6161 if (cname != NULL)
6162 name = cname;
6163 }
6164 }
6165 }
6166
6167 return name;
6168}
6169
0114d602
DJ
6170/* Return the fully qualified name of DIE, based on its DW_AT_name.
6171 If scope qualifiers are appropriate they will be added. The result
6172 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
6173 not have a name. NAME may either be from a previous call to
6174 dwarf2_name or NULL.
6175
0963b4bd 6176 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
6177
6178static const char *
94af9270 6179dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 6180{
94af9270
KS
6181 return dwarf2_compute_name (name, die, cu, 0);
6182}
0114d602 6183
94af9270
KS
6184/* Construct a physname for the given DIE in CU. NAME may either be
6185 from a previous call to dwarf2_name or NULL. The result will be
6186 allocated on the objfile_objstack or NULL if the DIE does not have a
6187 name.
0114d602 6188
94af9270 6189 The output string will be canonicalized (if C++/Java). */
0114d602 6190
94af9270
KS
6191static const char *
6192dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6193{
bb5ed363 6194 struct objfile *objfile = cu->objfile;
900e11f9
JK
6195 struct attribute *attr;
6196 const char *retval, *mangled = NULL, *canon = NULL;
6197 struct cleanup *back_to;
6198 int need_copy = 1;
6199
6200 /* In this case dwarf2_compute_name is just a shortcut not building anything
6201 on its own. */
6202 if (!die_needs_namespace (die, cu))
6203 return dwarf2_compute_name (name, die, cu, 1);
6204
6205 back_to = make_cleanup (null_cleanup, NULL);
6206
6207 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6208 if (!attr)
6209 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6210
6211 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6212 has computed. */
6213 if (attr && DW_STRING (attr))
6214 {
6215 char *demangled;
6216
6217 mangled = DW_STRING (attr);
6218
6219 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6220 type. It is easier for GDB users to search for such functions as
6221 `name(params)' than `long name(params)'. In such case the minimal
6222 symbol names do not match the full symbol names but for template
6223 functions there is never a need to look up their definition from their
6224 declaration so the only disadvantage remains the minimal symbol
6225 variant `long name(params)' does not have the proper inferior type.
6226 */
6227
a766d390
DE
6228 if (cu->language == language_go)
6229 {
6230 /* This is a lie, but we already lie to the caller new_symbol_full.
6231 new_symbol_full assumes we return the mangled name.
6232 This just undoes that lie until things are cleaned up. */
6233 demangled = NULL;
6234 }
6235 else
6236 {
6237 demangled = cplus_demangle (mangled,
6238 (DMGL_PARAMS | DMGL_ANSI
6239 | (cu->language == language_java
6240 ? DMGL_JAVA | DMGL_RET_POSTFIX
6241 : DMGL_RET_DROP)));
6242 }
900e11f9
JK
6243 if (demangled)
6244 {
6245 make_cleanup (xfree, demangled);
6246 canon = demangled;
6247 }
6248 else
6249 {
6250 canon = mangled;
6251 need_copy = 0;
6252 }
6253 }
6254
6255 if (canon == NULL || check_physname)
6256 {
6257 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6258
6259 if (canon != NULL && strcmp (physname, canon) != 0)
6260 {
6261 /* It may not mean a bug in GDB. The compiler could also
6262 compute DW_AT_linkage_name incorrectly. But in such case
6263 GDB would need to be bug-to-bug compatible. */
6264
6265 complaint (&symfile_complaints,
6266 _("Computed physname <%s> does not match demangled <%s> "
6267 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 6268 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
6269
6270 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6271 is available here - over computed PHYSNAME. It is safer
6272 against both buggy GDB and buggy compilers. */
6273
6274 retval = canon;
6275 }
6276 else
6277 {
6278 retval = physname;
6279 need_copy = 0;
6280 }
6281 }
6282 else
6283 retval = canon;
6284
6285 if (need_copy)
6286 retval = obsavestring (retval, strlen (retval),
bb5ed363 6287 &objfile->objfile_obstack);
900e11f9
JK
6288
6289 do_cleanups (back_to);
6290 return retval;
0114d602
DJ
6291}
6292
27aa8d6a
SW
6293/* Read the import statement specified by the given die and record it. */
6294
6295static void
6296read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6297{
bb5ed363 6298 struct objfile *objfile = cu->objfile;
27aa8d6a 6299 struct attribute *import_attr;
32019081 6300 struct die_info *imported_die, *child_die;
de4affc9 6301 struct dwarf2_cu *imported_cu;
27aa8d6a 6302 const char *imported_name;
794684b6 6303 const char *imported_name_prefix;
13387711
SW
6304 const char *canonical_name;
6305 const char *import_alias;
6306 const char *imported_declaration = NULL;
794684b6 6307 const char *import_prefix;
32019081
JK
6308 VEC (const_char_ptr) *excludes = NULL;
6309 struct cleanup *cleanups;
13387711
SW
6310
6311 char *temp;
27aa8d6a
SW
6312
6313 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6314 if (import_attr == NULL)
6315 {
6316 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6317 dwarf_tag_name (die->tag));
6318 return;
6319 }
6320
de4affc9
CC
6321 imported_cu = cu;
6322 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6323 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
6324 if (imported_name == NULL)
6325 {
6326 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6327
6328 The import in the following code:
6329 namespace A
6330 {
6331 typedef int B;
6332 }
6333
6334 int main ()
6335 {
6336 using A::B;
6337 B b;
6338 return b;
6339 }
6340
6341 ...
6342 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6343 <52> DW_AT_decl_file : 1
6344 <53> DW_AT_decl_line : 6
6345 <54> DW_AT_import : <0x75>
6346 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6347 <59> DW_AT_name : B
6348 <5b> DW_AT_decl_file : 1
6349 <5c> DW_AT_decl_line : 2
6350 <5d> DW_AT_type : <0x6e>
6351 ...
6352 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6353 <76> DW_AT_byte_size : 4
6354 <77> DW_AT_encoding : 5 (signed)
6355
6356 imports the wrong die ( 0x75 instead of 0x58 ).
6357 This case will be ignored until the gcc bug is fixed. */
6358 return;
6359 }
6360
82856980
SW
6361 /* Figure out the local name after import. */
6362 import_alias = dwarf2_name (die, cu);
27aa8d6a 6363
794684b6
SW
6364 /* Figure out where the statement is being imported to. */
6365 import_prefix = determine_prefix (die, cu);
6366
6367 /* Figure out what the scope of the imported die is and prepend it
6368 to the name of the imported die. */
de4affc9 6369 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 6370
f55ee35c
JK
6371 if (imported_die->tag != DW_TAG_namespace
6372 && imported_die->tag != DW_TAG_module)
794684b6 6373 {
13387711
SW
6374 imported_declaration = imported_name;
6375 canonical_name = imported_name_prefix;
794684b6 6376 }
13387711 6377 else if (strlen (imported_name_prefix) > 0)
794684b6 6378 {
13387711
SW
6379 temp = alloca (strlen (imported_name_prefix)
6380 + 2 + strlen (imported_name) + 1);
6381 strcpy (temp, imported_name_prefix);
6382 strcat (temp, "::");
6383 strcat (temp, imported_name);
6384 canonical_name = temp;
794684b6 6385 }
13387711
SW
6386 else
6387 canonical_name = imported_name;
794684b6 6388
32019081
JK
6389 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6390
6391 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6392 for (child_die = die->child; child_die && child_die->tag;
6393 child_die = sibling_die (child_die))
6394 {
6395 /* DWARF-4: A Fortran use statement with a “rename list” may be
6396 represented by an imported module entry with an import attribute
6397 referring to the module and owned entries corresponding to those
6398 entities that are renamed as part of being imported. */
6399
6400 if (child_die->tag != DW_TAG_imported_declaration)
6401 {
6402 complaint (&symfile_complaints,
6403 _("child DW_TAG_imported_declaration expected "
6404 "- DIE at 0x%x [in module %s]"),
b64f50a1 6405 child_die->offset.sect_off, objfile->name);
32019081
JK
6406 continue;
6407 }
6408
6409 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6410 if (import_attr == NULL)
6411 {
6412 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6413 dwarf_tag_name (child_die->tag));
6414 continue;
6415 }
6416
6417 imported_cu = cu;
6418 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6419 &imported_cu);
6420 imported_name = dwarf2_name (imported_die, imported_cu);
6421 if (imported_name == NULL)
6422 {
6423 complaint (&symfile_complaints,
6424 _("child DW_TAG_imported_declaration has unknown "
6425 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 6426 child_die->offset.sect_off, objfile->name);
32019081
JK
6427 continue;
6428 }
6429
6430 VEC_safe_push (const_char_ptr, excludes, imported_name);
6431
6432 process_die (child_die, cu);
6433 }
6434
c0cc3a76
SW
6435 cp_add_using_directive (import_prefix,
6436 canonical_name,
6437 import_alias,
13387711 6438 imported_declaration,
32019081 6439 excludes,
bb5ed363 6440 &objfile->objfile_obstack);
32019081
JK
6441
6442 do_cleanups (cleanups);
27aa8d6a
SW
6443}
6444
ae2de4f8
DE
6445/* Cleanup function for read_file_scope. */
6446
cb1df416
DJ
6447static void
6448free_cu_line_header (void *arg)
6449{
6450 struct dwarf2_cu *cu = arg;
6451
6452 free_line_header (cu->line_header);
6453 cu->line_header = NULL;
6454}
6455
9291a0cd
TT
6456static void
6457find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6458 char **name, char **comp_dir)
6459{
6460 struct attribute *attr;
6461
6462 *name = NULL;
6463 *comp_dir = NULL;
6464
6465 /* Find the filename. Do not use dwarf2_name here, since the filename
6466 is not a source language identifier. */
6467 attr = dwarf2_attr (die, DW_AT_name, cu);
6468 if (attr)
6469 {
6470 *name = DW_STRING (attr);
6471 }
6472
6473 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6474 if (attr)
6475 *comp_dir = DW_STRING (attr);
6476 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6477 {
6478 *comp_dir = ldirname (*name);
6479 if (*comp_dir != NULL)
6480 make_cleanup (xfree, *comp_dir);
6481 }
6482 if (*comp_dir != NULL)
6483 {
6484 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6485 directory, get rid of it. */
6486 char *cp = strchr (*comp_dir, ':');
6487
6488 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6489 *comp_dir = cp + 1;
6490 }
6491
6492 if (*name == NULL)
6493 *name = "<unknown>";
6494}
6495
f3f5162e
DE
6496/* Handle DW_AT_stmt_list for a compilation unit or type unit.
6497 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6498 COMP_DIR is the compilation directory.
6499 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
6500
6501static void
6502handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 6503 const char *comp_dir, int want_line_info)
2ab95328
TT
6504{
6505 struct attribute *attr;
2ab95328 6506
2ab95328
TT
6507 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6508 if (attr)
6509 {
6510 unsigned int line_offset = DW_UNSND (attr);
6511 struct line_header *line_header
3019eac3 6512 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
6513
6514 if (line_header)
dee91e82
DE
6515 {
6516 cu->line_header = line_header;
6517 make_cleanup (free_cu_line_header, cu);
f3f5162e 6518 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
dee91e82 6519 }
2ab95328
TT
6520 }
6521}
6522
95554aad 6523/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 6524
c906108c 6525static void
e7c27a73 6526read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6527{
dee91e82 6528 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 6529 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 6530 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
6531 CORE_ADDR highpc = ((CORE_ADDR) 0);
6532 struct attribute *attr;
e1024ff1 6533 char *name = NULL;
c906108c
SS
6534 char *comp_dir = NULL;
6535 struct die_info *child_die;
6536 bfd *abfd = objfile->obfd;
e142c38c 6537 CORE_ADDR baseaddr;
6e70227d 6538
e142c38c 6539 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6540
fae299cd 6541 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
6542
6543 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6544 from finish_block. */
2acceee2 6545 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
6546 lowpc = highpc;
6547 lowpc += baseaddr;
6548 highpc += baseaddr;
6549
9291a0cd 6550 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 6551
95554aad 6552 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 6553
f4b8a18d
KW
6554 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6555 standardised yet. As a workaround for the language detection we fall
6556 back to the DW_AT_producer string. */
6557 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6558 cu->language = language_opencl;
6559
3019eac3
DE
6560 /* Similar hack for Go. */
6561 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6562 set_cu_language (DW_LANG_Go, cu);
6563
6564 /* We assume that we're processing GCC output. */
6565 processing_gcc_compilation = 2;
6566
6567 processing_has_namespace_info = 0;
6568
6569 start_symtab (name, comp_dir, lowpc);
6570 record_debugformat ("DWARF 2");
6571 record_producer (cu->producer);
6572
6573 /* Decode line number information if present. We do this before
6574 processing child DIEs, so that the line header table is available
6575 for DW_AT_decl_file. */
6576 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6577
6578 /* Process all dies in compilation unit. */
6579 if (die->child != NULL)
6580 {
6581 child_die = die->child;
6582 while (child_die && child_die->tag)
6583 {
6584 process_die (child_die, cu);
6585 child_die = sibling_die (child_die);
6586 }
6587 }
6588
6589 /* Decode macro information, if present. Dwarf 2 macro information
6590 refers to information in the line number info statement program
6591 header, so we can only read it if we've read the header
6592 successfully. */
6593 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6594 if (attr && cu->line_header)
6595 {
6596 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6597 complaint (&symfile_complaints,
6598 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6599
6600 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
6601 comp_dir, abfd, cu,
fceca515
DE
6602 &dwarf2_per_objfile->macro, 1,
6603 ".debug_macro");
3019eac3
DE
6604 }
6605 else
6606 {
6607 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6608 if (attr && cu->line_header)
6609 {
6610 unsigned int macro_offset = DW_UNSND (attr);
6611
6612 dwarf_decode_macros (cu->line_header, macro_offset,
6613 comp_dir, abfd, cu,
fceca515
DE
6614 &dwarf2_per_objfile->macinfo, 0,
6615 ".debug_macinfo");
3019eac3
DE
6616 }
6617 }
6618
6619 do_cleanups (back_to);
6620}
6621
6622/* Process DW_TAG_type_unit.
6623 For TUs we want to skip the first top level sibling if it's not the
6624 actual type being defined by this TU. In this case the first top
6625 level sibling is there to provide context only. */
6626
6627static void
6628read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6629{
6630 struct objfile *objfile = cu->objfile;
6631 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6632 CORE_ADDR lowpc;
6633 struct attribute *attr;
6634 char *name = NULL;
6635 char *comp_dir = NULL;
6636 struct die_info *child_die;
6637 bfd *abfd = objfile->obfd;
6638
6639 /* start_symtab needs a low pc, but we don't really have one.
6640 Do what read_file_scope would do in the absence of such info. */
6641 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6642
6643 /* Find the filename. Do not use dwarf2_name here, since the filename
6644 is not a source language identifier. */
6645 attr = dwarf2_attr (die, DW_AT_name, cu);
6646 if (attr)
6647 name = DW_STRING (attr);
6648
6649 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6650 if (attr)
6651 comp_dir = DW_STRING (attr);
6652 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6653 {
6654 comp_dir = ldirname (name);
6655 if (comp_dir != NULL)
6656 make_cleanup (xfree, comp_dir);
6657 }
6658
6659 if (name == NULL)
6660 name = "<unknown>";
6661
95554aad 6662 prepare_one_comp_unit (cu, die, language_minimal);
3019eac3
DE
6663
6664 /* We assume that we're processing GCC output. */
6665 processing_gcc_compilation = 2;
6666
6667 processing_has_namespace_info = 0;
6668
6669 start_symtab (name, comp_dir, lowpc);
6670 record_debugformat ("DWARF 2");
6671 record_producer (cu->producer);
6672
6673 /* Decode line number information if present. We do this before
6674 processing child DIEs, so that the line header table is available
6675 for DW_AT_decl_file.
6676 We don't need the pc/line-number mapping for type units. */
6677 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6678
6679 /* Process the dies in the type unit. */
6680 if (die->child == NULL)
6681 {
6682 dump_die_for_error (die);
6683 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6684 bfd_get_filename (abfd));
6685 }
6686
6687 child_die = die->child;
6688
6689 while (child_die && child_die->tag)
6690 {
6691 process_die (child_die, cu);
6692
6693 child_die = sibling_die (child_die);
6694 }
6695
6696 do_cleanups (back_to);
6697}
6698\f
6699/* DWO files. */
6700
6701static hashval_t
6702hash_dwo_file (const void *item)
6703{
6704 const struct dwo_file *dwo_file = item;
6705
6706 return htab_hash_string (dwo_file->dwo_name);
6707}
6708
6709static int
6710eq_dwo_file (const void *item_lhs, const void *item_rhs)
6711{
6712 const struct dwo_file *lhs = item_lhs;
6713 const struct dwo_file *rhs = item_rhs;
6714
6715 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6716}
6717
6718/* Allocate a hash table for DWO files. */
6719
6720static htab_t
6721allocate_dwo_file_hash_table (void)
6722{
6723 struct objfile *objfile = dwarf2_per_objfile->objfile;
6724
6725 return htab_create_alloc_ex (41,
6726 hash_dwo_file,
6727 eq_dwo_file,
6728 NULL,
6729 &objfile->objfile_obstack,
6730 hashtab_obstack_allocate,
6731 dummy_obstack_deallocate);
6732}
6733
6734static hashval_t
6735hash_dwo_unit (const void *item)
6736{
6737 const struct dwo_unit *dwo_unit = item;
6738
6739 /* This drops the top 32 bits of the id, but is ok for a hash. */
6740 return dwo_unit->signature;
6741}
6742
6743static int
6744eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6745{
6746 const struct dwo_unit *lhs = item_lhs;
6747 const struct dwo_unit *rhs = item_rhs;
6748
6749 /* The signature is assumed to be unique within the DWO file.
6750 So while object file CU dwo_id's always have the value zero,
6751 that's OK, assuming each object file DWO file has only one CU,
6752 and that's the rule for now. */
6753 return lhs->signature == rhs->signature;
6754}
6755
6756/* Allocate a hash table for DWO CUs,TUs.
6757 There is one of these tables for each of CUs,TUs for each DWO file. */
6758
6759static htab_t
6760allocate_dwo_unit_table (struct objfile *objfile)
6761{
6762 /* Start out with a pretty small number.
6763 Generally DWO files contain only one CU and maybe some TUs. */
6764 return htab_create_alloc_ex (3,
6765 hash_dwo_unit,
6766 eq_dwo_unit,
6767 NULL,
6768 &objfile->objfile_obstack,
6769 hashtab_obstack_allocate,
6770 dummy_obstack_deallocate);
6771}
6772
6773/* This function is mapped across the sections and remembers the offset and
6774 size of each of the DWO debugging sections we are interested in. */
6775
6776static void
6777dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6778{
6779 struct dwo_file *dwo_file = dwo_file_ptr;
6780 const struct dwo_section_names *names = &dwo_section_names;
6781
6782 if (section_is_p (sectp->name, &names->abbrev_dwo))
6783 {
6784 dwo_file->sections.abbrev.asection = sectp;
6785 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6786 }
6787 else if (section_is_p (sectp->name, &names->info_dwo))
6788 {
6789 dwo_file->sections.info.asection = sectp;
6790 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6791 }
6792 else if (section_is_p (sectp->name, &names->line_dwo))
6793 {
6794 dwo_file->sections.line.asection = sectp;
6795 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6796 }
6797 else if (section_is_p (sectp->name, &names->loc_dwo))
6798 {
6799 dwo_file->sections.loc.asection = sectp;
6800 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6801 }
6802 else if (section_is_p (sectp->name, &names->str_dwo))
6803 {
6804 dwo_file->sections.str.asection = sectp;
6805 dwo_file->sections.str.size = bfd_get_section_size (sectp);
6806 }
6807 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
6808 {
6809 dwo_file->sections.str_offsets.asection = sectp;
6810 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
6811 }
6812 else if (section_is_p (sectp->name, &names->types_dwo))
6813 {
6814 struct dwarf2_section_info type_section;
6815
6816 memset (&type_section, 0, sizeof (type_section));
6817 type_section.asection = sectp;
6818 type_section.size = bfd_get_section_size (sectp);
6819 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
6820 &type_section);
6821 }
6822}
6823
6824/* Structure used to pass data to create_debug_info_hash_table_reader. */
6825
6826struct create_dwo_info_table_data
6827{
6828 struct dwo_file *dwo_file;
6829 htab_t cu_htab;
6830};
6831
6832/* die_reader_func for create_debug_info_hash_table. */
6833
6834static void
6835create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
6836 gdb_byte *info_ptr,
6837 struct die_info *comp_unit_die,
6838 int has_children,
6839 void *datap)
6840{
6841 struct dwarf2_cu *cu = reader->cu;
6842 struct objfile *objfile = dwarf2_per_objfile->objfile;
6843 sect_offset offset = cu->per_cu->offset;
6844 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
6845 struct create_dwo_info_table_data *data = datap;
6846 struct dwo_file *dwo_file = data->dwo_file;
6847 htab_t cu_htab = data->cu_htab;
6848 void **slot;
6849 struct attribute *attr;
6850 struct dwo_unit *dwo_unit;
6851
6852 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6853 if (attr == NULL)
6854 {
6855 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
6856 " its dwo_id [in module %s]"),
6857 offset.sect_off, dwo_file->dwo_name);
6858 return;
6859 }
6860
6861 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
6862 dwo_unit->dwo_file = dwo_file;
6863 dwo_unit->signature = DW_UNSND (attr);
6864 dwo_unit->info_or_types_section = section;
6865 dwo_unit->offset = offset;
6866 dwo_unit->length = cu->per_cu->length;
6867
6868 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
6869 gdb_assert (slot != NULL);
6870 if (*slot != NULL)
6871 {
6872 const struct dwo_unit *dup_dwo_unit = *slot;
6873
6874 complaint (&symfile_complaints,
6875 _("debug entry at offset 0x%x is duplicate to the entry at"
6876 " offset 0x%x, dwo_id 0x%s [in module %s]"),
6877 offset.sect_off, dup_dwo_unit->offset.sect_off,
6878 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
6879 dwo_file->dwo_name);
6880 }
6881 else
6882 *slot = dwo_unit;
6883
6884 if (dwarf2_die_debug)
6885 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
6886 offset.sect_off,
6887 phex (dwo_unit->signature,
6888 sizeof (dwo_unit->signature)));
6889}
6890
6891/* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
6892
6893static htab_t
6894create_debug_info_hash_table (struct dwo_file *dwo_file)
6895{
6896 struct objfile *objfile = dwarf2_per_objfile->objfile;
6897 struct dwarf2_section_info *section = &dwo_file->sections.info;
6898 bfd *abfd;
6899 htab_t cu_htab;
6900 gdb_byte *info_ptr, *end_ptr;
6901 struct create_dwo_info_table_data create_dwo_info_table_data;
6902
6903 dwarf2_read_section (objfile, section);
6904 info_ptr = section->buffer;
6905
6906 if (info_ptr == NULL)
6907 return NULL;
6908
6909 /* We can't set abfd until now because the section may be empty or
6910 not present, in which case section->asection will be NULL. */
6911 abfd = section->asection->owner;
6912
6913 if (dwarf2_die_debug)
6914 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
6915 bfd_get_filename (abfd));
6916
6917 cu_htab = allocate_dwo_unit_table (objfile);
6918
6919 create_dwo_info_table_data.dwo_file = dwo_file;
6920 create_dwo_info_table_data.cu_htab = cu_htab;
6921
6922 end_ptr = info_ptr + section->size;
6923 while (info_ptr < end_ptr)
6924 {
6925 struct dwarf2_per_cu_data per_cu;
6926
6927 memset (&per_cu, 0, sizeof (per_cu));
6928 per_cu.objfile = objfile;
6929 per_cu.is_debug_types = 0;
6930 per_cu.offset.sect_off = info_ptr - section->buffer;
6931 per_cu.info_or_types_section = section;
6932
6933 init_cutu_and_read_dies_no_follow (&per_cu,
6934 &dwo_file->sections.abbrev,
6935 dwo_file,
6936 create_debug_info_hash_table_reader,
6937 &create_dwo_info_table_data);
6938
6939 info_ptr += per_cu.length;
6940 }
6941
6942 return cu_htab;
6943}
6944
6945/* Subroutine of open_dwo_file to simplify it.
6946 Open the file specified by FILE_NAME and hand it off to BFD for
6947 preliminary analysis. Return a newly initialized bfd *, which
6948 includes a canonicalized copy of FILE_NAME.
6949 In case of trouble, return NULL.
6950 NOTE: This function is derived from symfile_bfd_open. */
6951
6952static bfd *
6953try_open_dwo_file (const char *file_name)
6954{
6955 bfd *sym_bfd;
6956 int desc;
6957 char *absolute_name;
3019eac3
DE
6958
6959 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
6960 O_RDONLY | O_BINARY, &absolute_name);
6961 if (desc < 0)
6962 return NULL;
6963
6964 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
6965 if (!sym_bfd)
6966 {
3019eac3
DE
6967 xfree (absolute_name);
6968 return NULL;
6969 }
6970 bfd_set_cacheable (sym_bfd, 1);
6971
6972 if (!bfd_check_format (sym_bfd, bfd_object))
6973 {
6974 bfd_close (sym_bfd); /* This also closes desc. */
6975 xfree (absolute_name);
6976 return NULL;
6977 }
6978
6979 /* bfd_usrdata exists for applications and libbfd must not touch it. */
6980 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
6981
6982 return sym_bfd;
6983}
6984
6985/* Try to open DWO file DWO_NAME.
6986 COMP_DIR is the DW_AT_comp_dir attribute.
6987 The result is the bfd handle of the file.
6988 If there is a problem finding or opening the file, return NULL.
6989 Upon success, the canonicalized path of the file is stored in the bfd,
6990 same as symfile_bfd_open. */
6991
6992static bfd *
6993open_dwo_file (const char *dwo_name, const char *comp_dir)
6994{
6995 bfd *abfd;
3019eac3
DE
6996
6997 if (IS_ABSOLUTE_PATH (dwo_name))
6998 return try_open_dwo_file (dwo_name);
6999
7000 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
7001
7002 if (comp_dir != NULL)
7003 {
7004 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
7005
7006 /* NOTE: If comp_dir is a relative path, this will also try the
7007 search path, which seems useful. */
7008 abfd = try_open_dwo_file (path_to_try);
7009 xfree (path_to_try);
7010 if (abfd != NULL)
7011 return abfd;
7012 }
7013
7014 /* That didn't work, try debug-file-directory, which, despite its name,
7015 is a list of paths. */
7016
7017 if (*debug_file_directory == '\0')
7018 return NULL;
7019
7020 return try_open_dwo_file (dwo_name);
7021}
7022
7023/* Initialize the use of the DWO file specified by DWO_NAME. */
7024
7025static struct dwo_file *
7026init_dwo_file (const char *dwo_name, const char *comp_dir)
7027{
7028 struct objfile *objfile = dwarf2_per_objfile->objfile;
7029 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7030 struct dwo_file);
7031 bfd *abfd;
7032 struct cleanup *cleanups;
7033
7034 if (dwarf2_die_debug)
7035 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7036
7037 abfd = open_dwo_file (dwo_name, comp_dir);
7038 if (abfd == NULL)
7039 return NULL;
7040 dwo_file->dwo_name = dwo_name;
7041 dwo_file->dwo_bfd = abfd;
7042
7043 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7044
7045 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7046
7047 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7048
7049 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7050 dwo_file->sections.types);
7051
7052 discard_cleanups (cleanups);
7053
7054 return dwo_file;
7055}
7056
7057/* Lookup DWO file DWO_NAME. */
7058
7059static struct dwo_file *
7060lookup_dwo_file (char *dwo_name, const char *comp_dir)
7061{
7062 struct dwo_file *dwo_file;
7063 struct dwo_file find_entry;
7064 void **slot;
7065
7066 if (dwarf2_per_objfile->dwo_files == NULL)
7067 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7068
7069 /* Have we already seen this DWO file? */
7070 find_entry.dwo_name = dwo_name;
7071 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7072
7073 /* If not, read it in and build a table of the DWOs it contains. */
7074 if (*slot == NULL)
7075 *slot = init_dwo_file (dwo_name, comp_dir);
7076
7077 /* NOTE: This will be NULL if unable to open the file. */
7078 dwo_file = *slot;
7079
7080 return dwo_file;
7081}
7082
7083/* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7084 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7085 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7086 nomenclature as TUs).
7087 The result is the DWO CU or NULL if we didn't find it
7088 (dwo_id mismatch or couldn't find the DWO file). */
7089
7090static struct dwo_unit *
7091lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7092 char *dwo_name, const char *comp_dir,
7093 ULONGEST signature)
7094{
7095 struct objfile *objfile = dwarf2_per_objfile->objfile;
7096 struct dwo_file *dwo_file;
7097
7098 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7099 if (dwo_file == NULL)
7100 return NULL;
7101
7102 /* Look up the DWO using its signature(dwo_id). */
7103
7104 if (dwo_file->cus != NULL)
7105 {
7106 struct dwo_unit find_dwo_cu, *dwo_cu;
7107
7108 find_dwo_cu.signature = signature;
7109 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
a766d390 7110
3019eac3
DE
7111 if (dwo_cu != NULL)
7112 return dwo_cu;
7113 }
c906108c 7114
3019eac3 7115 /* We didn't find it. This must mean a dwo_id mismatch. */
df8a16a1 7116
3019eac3
DE
7117 complaint (&symfile_complaints,
7118 _("Could not find DWO CU referenced by CU at offset 0x%x"
7119 " [in module %s]"),
7120 this_cu->offset.sect_off, objfile->name);
7121 return NULL;
7122}
c906108c 7123
3019eac3
DE
7124/* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7125 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7126 The result is the DWO CU or NULL if we didn't find it
7127 (dwo_id mismatch or couldn't find the DWO file). */
debd256d 7128
3019eac3
DE
7129static struct dwo_unit *
7130lookup_dwo_type_unit (struct signatured_type *this_tu,
7131 char *dwo_name, const char *comp_dir)
7132{
7133 struct objfile *objfile = dwarf2_per_objfile->objfile;
7134 struct dwo_file *dwo_file;
cb1df416 7135
3019eac3
DE
7136 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7137 if (dwo_file == NULL)
7138 return NULL;
cf2c3c16 7139
3019eac3
DE
7140 /* Look up the DWO using its signature(dwo_id). */
7141
7142 if (dwo_file->tus != NULL)
cf2c3c16 7143 {
3019eac3 7144 struct dwo_unit find_dwo_tu, *dwo_tu;
9a619af0 7145
3019eac3
DE
7146 find_dwo_tu.signature = this_tu->signature;
7147 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7148
7149 if (dwo_tu != NULL)
7150 return dwo_tu;
2e276125 7151 }
9cdd5dbd 7152
3019eac3
DE
7153 /* We didn't find it. This must mean a dwo_id mismatch. */
7154
7155 complaint (&symfile_complaints,
7156 _("Could not find DWO TU referenced by TU at offset 0x%x"
7157 " [in module %s]"),
7158 this_tu->per_cu.offset.sect_off, objfile->name);
7159 return NULL;
5fb290d7
DJ
7160}
7161
3019eac3
DE
7162/* Free all resources associated with DWO_FILE.
7163 Close the DWO file and munmap the sections.
7164 All memory should be on the objfile obstack. */
348e048f
DE
7165
7166static void
3019eac3 7167free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 7168{
3019eac3
DE
7169 int ix;
7170 struct dwarf2_section_info *section;
348e048f 7171
3019eac3
DE
7172 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7173 bfd_close (dwo_file->dwo_bfd);
348e048f 7174
3019eac3
DE
7175 munmap_section_buffer (&dwo_file->sections.abbrev);
7176 munmap_section_buffer (&dwo_file->sections.info);
7177 munmap_section_buffer (&dwo_file->sections.line);
7178 munmap_section_buffer (&dwo_file->sections.loc);
7179 munmap_section_buffer (&dwo_file->sections.str);
7180 munmap_section_buffer (&dwo_file->sections.str_offsets);
348e048f 7181
3019eac3
DE
7182 for (ix = 0;
7183 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7184 ix, section);
7185 ++ix)
7186 munmap_section_buffer (section);
348e048f 7187
3019eac3
DE
7188 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7189}
348e048f 7190
3019eac3 7191/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 7192
3019eac3
DE
7193static void
7194free_dwo_file_cleanup (void *arg)
7195{
7196 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7197 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 7198
3019eac3
DE
7199 free_dwo_file (dwo_file, objfile);
7200}
348e048f 7201
3019eac3 7202/* Traversal function for free_dwo_files. */
2ab95328 7203
3019eac3
DE
7204static int
7205free_dwo_file_from_slot (void **slot, void *info)
7206{
7207 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7208 struct objfile *objfile = (struct objfile *) info;
348e048f 7209
3019eac3 7210 free_dwo_file (dwo_file, objfile);
348e048f 7211
3019eac3
DE
7212 return 1;
7213}
348e048f 7214
3019eac3 7215/* Free all resources associated with DWO_FILES. */
348e048f 7216
3019eac3
DE
7217static void
7218free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7219{
7220 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 7221}
3019eac3
DE
7222\f
7223/* Read in various DIEs. */
348e048f 7224
d389af10
JK
7225/* qsort helper for inherit_abstract_dies. */
7226
7227static int
7228unsigned_int_compar (const void *ap, const void *bp)
7229{
7230 unsigned int a = *(unsigned int *) ap;
7231 unsigned int b = *(unsigned int *) bp;
7232
7233 return (a > b) - (b > a);
7234}
7235
7236/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
7237 Inherit only the children of the DW_AT_abstract_origin DIE not being
7238 already referenced by DW_AT_abstract_origin from the children of the
7239 current DIE. */
d389af10
JK
7240
7241static void
7242inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7243{
7244 struct die_info *child_die;
7245 unsigned die_children_count;
7246 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
7247 sect_offset *offsets;
7248 sect_offset *offsets_end, *offsetp;
d389af10
JK
7249 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7250 struct die_info *origin_die;
7251 /* Iterator of the ORIGIN_DIE children. */
7252 struct die_info *origin_child_die;
7253 struct cleanup *cleanups;
7254 struct attribute *attr;
cd02d79d
PA
7255 struct dwarf2_cu *origin_cu;
7256 struct pending **origin_previous_list_in_scope;
d389af10
JK
7257
7258 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7259 if (!attr)
7260 return;
7261
cd02d79d
PA
7262 /* Note that following die references may follow to a die in a
7263 different cu. */
7264
7265 origin_cu = cu;
7266 origin_die = follow_die_ref (die, attr, &origin_cu);
7267
7268 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7269 symbols in. */
7270 origin_previous_list_in_scope = origin_cu->list_in_scope;
7271 origin_cu->list_in_scope = cu->list_in_scope;
7272
edb3359d
DJ
7273 if (die->tag != origin_die->tag
7274 && !(die->tag == DW_TAG_inlined_subroutine
7275 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7276 complaint (&symfile_complaints,
7277 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 7278 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
7279
7280 child_die = die->child;
7281 die_children_count = 0;
7282 while (child_die && child_die->tag)
7283 {
7284 child_die = sibling_die (child_die);
7285 die_children_count++;
7286 }
7287 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7288 cleanups = make_cleanup (xfree, offsets);
7289
7290 offsets_end = offsets;
7291 child_die = die->child;
7292 while (child_die && child_die->tag)
7293 {
c38f313d
DJ
7294 /* For each CHILD_DIE, find the corresponding child of
7295 ORIGIN_DIE. If there is more than one layer of
7296 DW_AT_abstract_origin, follow them all; there shouldn't be,
7297 but GCC versions at least through 4.4 generate this (GCC PR
7298 40573). */
7299 struct die_info *child_origin_die = child_die;
cd02d79d 7300 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 7301
c38f313d
DJ
7302 while (1)
7303 {
cd02d79d
PA
7304 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7305 child_origin_cu);
c38f313d
DJ
7306 if (attr == NULL)
7307 break;
cd02d79d
PA
7308 child_origin_die = follow_die_ref (child_origin_die, attr,
7309 &child_origin_cu);
c38f313d
DJ
7310 }
7311
d389af10
JK
7312 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7313 counterpart may exist. */
c38f313d 7314 if (child_origin_die != child_die)
d389af10 7315 {
edb3359d
DJ
7316 if (child_die->tag != child_origin_die->tag
7317 && !(child_die->tag == DW_TAG_inlined_subroutine
7318 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7319 complaint (&symfile_complaints,
7320 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7321 "different tags"), child_die->offset.sect_off,
7322 child_origin_die->offset.sect_off);
c38f313d
DJ
7323 if (child_origin_die->parent != origin_die)
7324 complaint (&symfile_complaints,
7325 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7326 "different parents"), child_die->offset.sect_off,
7327 child_origin_die->offset.sect_off);
c38f313d
DJ
7328 else
7329 *offsets_end++ = child_origin_die->offset;
d389af10
JK
7330 }
7331 child_die = sibling_die (child_die);
7332 }
7333 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7334 unsigned_int_compar);
7335 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 7336 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
7337 complaint (&symfile_complaints,
7338 _("Multiple children of DIE 0x%x refer "
7339 "to DIE 0x%x as their abstract origin"),
b64f50a1 7340 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
7341
7342 offsetp = offsets;
7343 origin_child_die = origin_die->child;
7344 while (origin_child_die && origin_child_die->tag)
7345 {
7346 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
7347 while (offsetp < offsets_end
7348 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 7349 offsetp++;
b64f50a1
JK
7350 if (offsetp >= offsets_end
7351 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
7352 {
7353 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 7354 process_die (origin_child_die, origin_cu);
d389af10
JK
7355 }
7356 origin_child_die = sibling_die (origin_child_die);
7357 }
cd02d79d 7358 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
7359
7360 do_cleanups (cleanups);
7361}
7362
c906108c 7363static void
e7c27a73 7364read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7365{
e7c27a73 7366 struct objfile *objfile = cu->objfile;
52f0bd74 7367 struct context_stack *new;
c906108c
SS
7368 CORE_ADDR lowpc;
7369 CORE_ADDR highpc;
7370 struct die_info *child_die;
edb3359d 7371 struct attribute *attr, *call_line, *call_file;
c906108c 7372 char *name;
e142c38c 7373 CORE_ADDR baseaddr;
801e3a5b 7374 struct block *block;
edb3359d 7375 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
7376 VEC (symbolp) *template_args = NULL;
7377 struct template_symbol *templ_func = NULL;
edb3359d
DJ
7378
7379 if (inlined_func)
7380 {
7381 /* If we do not have call site information, we can't show the
7382 caller of this inlined function. That's too confusing, so
7383 only use the scope for local variables. */
7384 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7385 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7386 if (call_line == NULL || call_file == NULL)
7387 {
7388 read_lexical_block_scope (die, cu);
7389 return;
7390 }
7391 }
c906108c 7392
e142c38c
DJ
7393 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7394
94af9270 7395 name = dwarf2_name (die, cu);
c906108c 7396
e8d05480
JB
7397 /* Ignore functions with missing or empty names. These are actually
7398 illegal according to the DWARF standard. */
7399 if (name == NULL)
7400 {
7401 complaint (&symfile_complaints,
b64f50a1
JK
7402 _("missing name for subprogram DIE at %d"),
7403 die->offset.sect_off);
e8d05480
JB
7404 return;
7405 }
7406
7407 /* Ignore functions with missing or invalid low and high pc attributes. */
7408 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7409 {
ae4d0c03
PM
7410 attr = dwarf2_attr (die, DW_AT_external, cu);
7411 if (!attr || !DW_UNSND (attr))
7412 complaint (&symfile_complaints,
3e43a32a
MS
7413 _("cannot get low and high bounds "
7414 "for subprogram DIE at %d"),
b64f50a1 7415 die->offset.sect_off);
e8d05480
JB
7416 return;
7417 }
c906108c
SS
7418
7419 lowpc += baseaddr;
7420 highpc += baseaddr;
7421
34eaf542
TT
7422 /* If we have any template arguments, then we must allocate a
7423 different sort of symbol. */
7424 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7425 {
7426 if (child_die->tag == DW_TAG_template_type_param
7427 || child_die->tag == DW_TAG_template_value_param)
7428 {
7429 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7430 struct template_symbol);
7431 templ_func->base.is_cplus_template_function = 1;
7432 break;
7433 }
7434 }
7435
c906108c 7436 new = push_context (0, lowpc);
34eaf542
TT
7437 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7438 (struct symbol *) templ_func);
4c2df51b 7439
4cecd739
DJ
7440 /* If there is a location expression for DW_AT_frame_base, record
7441 it. */
e142c38c 7442 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 7443 if (attr)
c034e007
AC
7444 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7445 expression is being recorded directly in the function's symbol
7446 and not in a separate frame-base object. I guess this hack is
7447 to avoid adding some sort of frame-base adjunct/annex to the
7448 function's symbol :-(. The problem with doing this is that it
7449 results in a function symbol with a location expression that
7450 has nothing to do with the location of the function, ouch! The
7451 relationship should be: a function's symbol has-a frame base; a
7452 frame-base has-a location expression. */
e7c27a73 7453 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 7454
e142c38c 7455 cu->list_in_scope = &local_symbols;
c906108c 7456
639d11d3 7457 if (die->child != NULL)
c906108c 7458 {
639d11d3 7459 child_die = die->child;
c906108c
SS
7460 while (child_die && child_die->tag)
7461 {
34eaf542
TT
7462 if (child_die->tag == DW_TAG_template_type_param
7463 || child_die->tag == DW_TAG_template_value_param)
7464 {
7465 struct symbol *arg = new_symbol (child_die, NULL, cu);
7466
f1078f66
DJ
7467 if (arg != NULL)
7468 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7469 }
7470 else
7471 process_die (child_die, cu);
c906108c
SS
7472 child_die = sibling_die (child_die);
7473 }
7474 }
7475
d389af10
JK
7476 inherit_abstract_dies (die, cu);
7477
4a811a97
UW
7478 /* If we have a DW_AT_specification, we might need to import using
7479 directives from the context of the specification DIE. See the
7480 comment in determine_prefix. */
7481 if (cu->language == language_cplus
7482 && dwarf2_attr (die, DW_AT_specification, cu))
7483 {
7484 struct dwarf2_cu *spec_cu = cu;
7485 struct die_info *spec_die = die_specification (die, &spec_cu);
7486
7487 while (spec_die)
7488 {
7489 child_die = spec_die->child;
7490 while (child_die && child_die->tag)
7491 {
7492 if (child_die->tag == DW_TAG_imported_module)
7493 process_die (child_die, spec_cu);
7494 child_die = sibling_die (child_die);
7495 }
7496
7497 /* In some cases, GCC generates specification DIEs that
7498 themselves contain DW_AT_specification attributes. */
7499 spec_die = die_specification (spec_die, &spec_cu);
7500 }
7501 }
7502
c906108c
SS
7503 new = pop_context ();
7504 /* Make a block for the local symbols within. */
801e3a5b
JB
7505 block = finish_block (new->name, &local_symbols, new->old_blocks,
7506 lowpc, highpc, objfile);
7507
df8a16a1 7508 /* For C++, set the block's scope. */
f55ee35c 7509 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 7510 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 7511 determine_prefix (die, cu),
df8a16a1
DJ
7512 processing_has_namespace_info);
7513
801e3a5b
JB
7514 /* If we have address ranges, record them. */
7515 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 7516
34eaf542
TT
7517 /* Attach template arguments to function. */
7518 if (! VEC_empty (symbolp, template_args))
7519 {
7520 gdb_assert (templ_func != NULL);
7521
7522 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7523 templ_func->template_arguments
7524 = obstack_alloc (&objfile->objfile_obstack,
7525 (templ_func->n_template_arguments
7526 * sizeof (struct symbol *)));
7527 memcpy (templ_func->template_arguments,
7528 VEC_address (symbolp, template_args),
7529 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7530 VEC_free (symbolp, template_args);
7531 }
7532
208d8187
JB
7533 /* In C++, we can have functions nested inside functions (e.g., when
7534 a function declares a class that has methods). This means that
7535 when we finish processing a function scope, we may need to go
7536 back to building a containing block's symbol lists. */
7537 local_symbols = new->locals;
7538 param_symbols = new->params;
27aa8d6a 7539 using_directives = new->using_directives;
208d8187 7540
921e78cf
JB
7541 /* If we've finished processing a top-level function, subsequent
7542 symbols go in the file symbol list. */
7543 if (outermost_context_p ())
e142c38c 7544 cu->list_in_scope = &file_symbols;
c906108c
SS
7545}
7546
7547/* Process all the DIES contained within a lexical block scope. Start
7548 a new scope, process the dies, and then close the scope. */
7549
7550static void
e7c27a73 7551read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7552{
e7c27a73 7553 struct objfile *objfile = cu->objfile;
52f0bd74 7554 struct context_stack *new;
c906108c
SS
7555 CORE_ADDR lowpc, highpc;
7556 struct die_info *child_die;
e142c38c
DJ
7557 CORE_ADDR baseaddr;
7558
7559 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
7560
7561 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
7562 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7563 as multiple lexical blocks? Handling children in a sane way would
6e70227d 7564 be nasty. Might be easier to properly extend generic blocks to
af34e669 7565 describe ranges. */
d85a05f0 7566 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
7567 return;
7568 lowpc += baseaddr;
7569 highpc += baseaddr;
7570
7571 push_context (0, lowpc);
639d11d3 7572 if (die->child != NULL)
c906108c 7573 {
639d11d3 7574 child_die = die->child;
c906108c
SS
7575 while (child_die && child_die->tag)
7576 {
e7c27a73 7577 process_die (child_die, cu);
c906108c
SS
7578 child_die = sibling_die (child_die);
7579 }
7580 }
7581 new = pop_context ();
7582
8540c487 7583 if (local_symbols != NULL || using_directives != NULL)
c906108c 7584 {
801e3a5b
JB
7585 struct block *block
7586 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7587 highpc, objfile);
7588
7589 /* Note that recording ranges after traversing children, as we
7590 do here, means that recording a parent's ranges entails
7591 walking across all its children's ranges as they appear in
7592 the address map, which is quadratic behavior.
7593
7594 It would be nicer to record the parent's ranges before
7595 traversing its children, simply overriding whatever you find
7596 there. But since we don't even decide whether to create a
7597 block until after we've traversed its children, that's hard
7598 to do. */
7599 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
7600 }
7601 local_symbols = new->locals;
27aa8d6a 7602 using_directives = new->using_directives;
c906108c
SS
7603}
7604
96408a79
SA
7605/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7606
7607static void
7608read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7609{
7610 struct objfile *objfile = cu->objfile;
7611 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7612 CORE_ADDR pc, baseaddr;
7613 struct attribute *attr;
7614 struct call_site *call_site, call_site_local;
7615 void **slot;
7616 int nparams;
7617 struct die_info *child_die;
7618
7619 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7620
7621 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7622 if (!attr)
7623 {
7624 complaint (&symfile_complaints,
7625 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7626 "DIE 0x%x [in module %s]"),
b64f50a1 7627 die->offset.sect_off, objfile->name);
96408a79
SA
7628 return;
7629 }
7630 pc = DW_ADDR (attr) + baseaddr;
7631
7632 if (cu->call_site_htab == NULL)
7633 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7634 NULL, &objfile->objfile_obstack,
7635 hashtab_obstack_allocate, NULL);
7636 call_site_local.pc = pc;
7637 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7638 if (*slot != NULL)
7639 {
7640 complaint (&symfile_complaints,
7641 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7642 "DIE 0x%x [in module %s]"),
b64f50a1 7643 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
7644 return;
7645 }
7646
7647 /* Count parameters at the caller. */
7648
7649 nparams = 0;
7650 for (child_die = die->child; child_die && child_die->tag;
7651 child_die = sibling_die (child_die))
7652 {
7653 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7654 {
7655 complaint (&symfile_complaints,
7656 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7657 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7658 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
7659 continue;
7660 }
7661
7662 nparams++;
7663 }
7664
7665 call_site = obstack_alloc (&objfile->objfile_obstack,
7666 (sizeof (*call_site)
7667 + (sizeof (*call_site->parameter)
7668 * (nparams - 1))));
7669 *slot = call_site;
7670 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7671 call_site->pc = pc;
7672
7673 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7674 {
7675 struct die_info *func_die;
7676
7677 /* Skip also over DW_TAG_inlined_subroutine. */
7678 for (func_die = die->parent;
7679 func_die && func_die->tag != DW_TAG_subprogram
7680 && func_die->tag != DW_TAG_subroutine_type;
7681 func_die = func_die->parent);
7682
7683 /* DW_AT_GNU_all_call_sites is a superset
7684 of DW_AT_GNU_all_tail_call_sites. */
7685 if (func_die
7686 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7687 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7688 {
7689 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7690 not complete. But keep CALL_SITE for look ups via call_site_htab,
7691 both the initial caller containing the real return address PC and
7692 the final callee containing the current PC of a chain of tail
7693 calls do not need to have the tail call list complete. But any
7694 function candidate for a virtual tail call frame searched via
7695 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7696 determined unambiguously. */
7697 }
7698 else
7699 {
7700 struct type *func_type = NULL;
7701
7702 if (func_die)
7703 func_type = get_die_type (func_die, cu);
7704 if (func_type != NULL)
7705 {
7706 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7707
7708 /* Enlist this call site to the function. */
7709 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7710 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7711 }
7712 else
7713 complaint (&symfile_complaints,
7714 _("Cannot find function owning DW_TAG_GNU_call_site "
7715 "DIE 0x%x [in module %s]"),
b64f50a1 7716 die->offset.sect_off, objfile->name);
96408a79
SA
7717 }
7718 }
7719
7720 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7721 if (attr == NULL)
7722 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7723 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7724 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7725 /* Keep NULL DWARF_BLOCK. */;
7726 else if (attr_form_is_block (attr))
7727 {
7728 struct dwarf2_locexpr_baton *dlbaton;
7729
7730 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7731 dlbaton->data = DW_BLOCK (attr)->data;
7732 dlbaton->size = DW_BLOCK (attr)->size;
7733 dlbaton->per_cu = cu->per_cu;
7734
7735 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7736 }
7737 else if (is_ref_attr (attr))
7738 {
96408a79
SA
7739 struct dwarf2_cu *target_cu = cu;
7740 struct die_info *target_die;
7741
7742 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7743 gdb_assert (target_cu->objfile == objfile);
7744 if (die_is_declaration (target_die, target_cu))
7745 {
7746 const char *target_physname;
7747
7748 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7749 if (target_physname == NULL)
7750 complaint (&symfile_complaints,
7751 _("DW_AT_GNU_call_site_target target DIE has invalid "
7752 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 7753 die->offset.sect_off, objfile->name);
96408a79
SA
7754 else
7755 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7756 }
7757 else
7758 {
7759 CORE_ADDR lowpc;
7760
7761 /* DW_AT_entry_pc should be preferred. */
7762 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7763 complaint (&symfile_complaints,
7764 _("DW_AT_GNU_call_site_target target DIE has invalid "
7765 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 7766 die->offset.sect_off, objfile->name);
96408a79
SA
7767 else
7768 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7769 }
7770 }
7771 else
7772 complaint (&symfile_complaints,
7773 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7774 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 7775 die->offset.sect_off, objfile->name);
96408a79
SA
7776
7777 call_site->per_cu = cu->per_cu;
7778
7779 for (child_die = die->child;
7780 child_die && child_die->tag;
7781 child_die = sibling_die (child_die))
7782 {
96408a79
SA
7783 struct call_site_parameter *parameter;
7784
7785 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7786 {
7787 /* Already printed the complaint above. */
7788 continue;
7789 }
7790
7791 gdb_assert (call_site->parameter_count < nparams);
7792 parameter = &call_site->parameter[call_site->parameter_count];
7793
7794 /* DW_AT_location specifies the register number. Value of the data
7795 assumed for the register is contained in DW_AT_GNU_call_site_value. */
7796
7797 attr = dwarf2_attr (child_die, DW_AT_location, cu);
7798 if (!attr || !attr_form_is_block (attr))
7799 {
7800 complaint (&symfile_complaints,
7801 _("No DW_FORM_block* DW_AT_location for "
7802 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7803 child_die->offset.sect_off, objfile->name);
96408a79
SA
7804 continue;
7805 }
7806 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
7807 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
7808 if (parameter->dwarf_reg == -1
7809 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
7810 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
7811 &parameter->fb_offset))
7812 {
7813 complaint (&symfile_complaints,
7814 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
7815 "for DW_FORM_block* DW_AT_location for "
7816 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7817 child_die->offset.sect_off, objfile->name);
96408a79
SA
7818 continue;
7819 }
7820
7821 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
7822 if (!attr_form_is_block (attr))
7823 {
7824 complaint (&symfile_complaints,
7825 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
7826 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7827 child_die->offset.sect_off, objfile->name);
96408a79
SA
7828 continue;
7829 }
7830 parameter->value = DW_BLOCK (attr)->data;
7831 parameter->value_size = DW_BLOCK (attr)->size;
7832
7833 /* Parameters are not pre-cleared by memset above. */
7834 parameter->data_value = NULL;
7835 parameter->data_value_size = 0;
7836 call_site->parameter_count++;
7837
7838 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
7839 if (attr)
7840 {
7841 if (!attr_form_is_block (attr))
7842 complaint (&symfile_complaints,
7843 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
7844 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7845 child_die->offset.sect_off, objfile->name);
96408a79
SA
7846 else
7847 {
7848 parameter->data_value = DW_BLOCK (attr)->data;
7849 parameter->data_value_size = DW_BLOCK (attr)->size;
7850 }
7851 }
7852 }
7853}
7854
43039443 7855/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
7856 Return 1 if the attributes are present and valid, otherwise, return 0.
7857 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
7858
7859static int
7860dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
7861 CORE_ADDR *high_return, struct dwarf2_cu *cu,
7862 struct partial_symtab *ranges_pst)
43039443
JK
7863{
7864 struct objfile *objfile = cu->objfile;
7865 struct comp_unit_head *cu_header = &cu->header;
7866 bfd *obfd = objfile->obfd;
7867 unsigned int addr_size = cu_header->addr_size;
7868 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7869 /* Base address selection entry. */
7870 CORE_ADDR base;
7871 int found_base;
7872 unsigned int dummy;
7873 gdb_byte *buffer;
7874 CORE_ADDR marker;
7875 int low_set;
7876 CORE_ADDR low = 0;
7877 CORE_ADDR high = 0;
ff013f42 7878 CORE_ADDR baseaddr;
43039443 7879
d00adf39
DE
7880 found_base = cu->base_known;
7881 base = cu->base_address;
43039443 7882
be391dca 7883 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 7884 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
7885 {
7886 complaint (&symfile_complaints,
7887 _("Offset %d out of bounds for DW_AT_ranges attribute"),
7888 offset);
7889 return 0;
7890 }
dce234bc 7891 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
7892
7893 /* Read in the largest possible address. */
7894 marker = read_address (obfd, buffer, cu, &dummy);
7895 if ((marker & mask) == mask)
7896 {
7897 /* If we found the largest possible address, then
7898 read the base address. */
7899 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7900 buffer += 2 * addr_size;
7901 offset += 2 * addr_size;
7902 found_base = 1;
7903 }
7904
7905 low_set = 0;
7906
e7030f15 7907 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 7908
43039443
JK
7909 while (1)
7910 {
7911 CORE_ADDR range_beginning, range_end;
7912
7913 range_beginning = read_address (obfd, buffer, cu, &dummy);
7914 buffer += addr_size;
7915 range_end = read_address (obfd, buffer, cu, &dummy);
7916 buffer += addr_size;
7917 offset += 2 * addr_size;
7918
7919 /* An end of list marker is a pair of zero addresses. */
7920 if (range_beginning == 0 && range_end == 0)
7921 /* Found the end of list entry. */
7922 break;
7923
7924 /* Each base address selection entry is a pair of 2 values.
7925 The first is the largest possible address, the second is
7926 the base address. Check for a base address here. */
7927 if ((range_beginning & mask) == mask)
7928 {
7929 /* If we found the largest possible address, then
7930 read the base address. */
7931 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7932 found_base = 1;
7933 continue;
7934 }
7935
7936 if (!found_base)
7937 {
7938 /* We have no valid base address for the ranges
7939 data. */
7940 complaint (&symfile_complaints,
7941 _("Invalid .debug_ranges data (no base address)"));
7942 return 0;
7943 }
7944
9277c30c
UW
7945 if (range_beginning > range_end)
7946 {
7947 /* Inverted range entries are invalid. */
7948 complaint (&symfile_complaints,
7949 _("Invalid .debug_ranges data (inverted range)"));
7950 return 0;
7951 }
7952
7953 /* Empty range entries have no effect. */
7954 if (range_beginning == range_end)
7955 continue;
7956
43039443
JK
7957 range_beginning += base;
7958 range_end += base;
7959
9277c30c 7960 if (ranges_pst != NULL)
ff013f42 7961 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
7962 range_beginning + baseaddr,
7963 range_end - 1 + baseaddr,
ff013f42
JK
7964 ranges_pst);
7965
43039443
JK
7966 /* FIXME: This is recording everything as a low-high
7967 segment of consecutive addresses. We should have a
7968 data structure for discontiguous block ranges
7969 instead. */
7970 if (! low_set)
7971 {
7972 low = range_beginning;
7973 high = range_end;
7974 low_set = 1;
7975 }
7976 else
7977 {
7978 if (range_beginning < low)
7979 low = range_beginning;
7980 if (range_end > high)
7981 high = range_end;
7982 }
7983 }
7984
7985 if (! low_set)
7986 /* If the first entry is an end-of-list marker, the range
7987 describes an empty scope, i.e. no instructions. */
7988 return 0;
7989
7990 if (low_return)
7991 *low_return = low;
7992 if (high_return)
7993 *high_return = high;
7994 return 1;
7995}
7996
af34e669
DJ
7997/* Get low and high pc attributes from a die. Return 1 if the attributes
7998 are present and valid, otherwise, return 0. Return -1 if the range is
7999 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 8000
c906108c 8001static int
af34e669 8002dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
8003 CORE_ADDR *highpc, struct dwarf2_cu *cu,
8004 struct partial_symtab *pst)
c906108c
SS
8005{
8006 struct attribute *attr;
91da1414 8007 struct attribute *attr_high;
af34e669
DJ
8008 CORE_ADDR low = 0;
8009 CORE_ADDR high = 0;
8010 int ret = 0;
c906108c 8011
91da1414
MW
8012 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8013 if (attr_high)
af34e669 8014 {
e142c38c 8015 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 8016 if (attr)
91da1414
MW
8017 {
8018 low = DW_ADDR (attr);
3019eac3
DE
8019 if (attr_high->form == DW_FORM_addr
8020 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8021 high = DW_ADDR (attr_high);
8022 else
8023 high = low + DW_UNSND (attr_high);
8024 }
af34e669
DJ
8025 else
8026 /* Found high w/o low attribute. */
8027 return 0;
8028
8029 /* Found consecutive range of addresses. */
8030 ret = 1;
8031 }
c906108c 8032 else
af34e669 8033 {
e142c38c 8034 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
8035 if (attr != NULL)
8036 {
af34e669 8037 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 8038 .debug_ranges section. */
d85a05f0 8039 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 8040 return 0;
43039443 8041 /* Found discontinuous range of addresses. */
af34e669
DJ
8042 ret = -1;
8043 }
8044 }
c906108c 8045
9373cf26
JK
8046 /* read_partial_die has also the strict LOW < HIGH requirement. */
8047 if (high <= low)
c906108c
SS
8048 return 0;
8049
8050 /* When using the GNU linker, .gnu.linkonce. sections are used to
8051 eliminate duplicate copies of functions and vtables and such.
8052 The linker will arbitrarily choose one and discard the others.
8053 The AT_*_pc values for such functions refer to local labels in
8054 these sections. If the section from that file was discarded, the
8055 labels are not in the output, so the relocs get a value of 0.
8056 If this is a discarded function, mark the pc bounds as invalid,
8057 so that GDB will ignore it. */
72dca2f5 8058 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
8059 return 0;
8060
8061 *lowpc = low;
96408a79
SA
8062 if (highpc)
8063 *highpc = high;
af34e669 8064 return ret;
c906108c
SS
8065}
8066
b084d499
JB
8067/* Assuming that DIE represents a subprogram DIE or a lexical block, get
8068 its low and high PC addresses. Do nothing if these addresses could not
8069 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8070 and HIGHPC to the high address if greater than HIGHPC. */
8071
8072static void
8073dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8074 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8075 struct dwarf2_cu *cu)
8076{
8077 CORE_ADDR low, high;
8078 struct die_info *child = die->child;
8079
d85a05f0 8080 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
8081 {
8082 *lowpc = min (*lowpc, low);
8083 *highpc = max (*highpc, high);
8084 }
8085
8086 /* If the language does not allow nested subprograms (either inside
8087 subprograms or lexical blocks), we're done. */
8088 if (cu->language != language_ada)
8089 return;
6e70227d 8090
b084d499
JB
8091 /* Check all the children of the given DIE. If it contains nested
8092 subprograms, then check their pc bounds. Likewise, we need to
8093 check lexical blocks as well, as they may also contain subprogram
8094 definitions. */
8095 while (child && child->tag)
8096 {
8097 if (child->tag == DW_TAG_subprogram
8098 || child->tag == DW_TAG_lexical_block)
8099 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8100 child = sibling_die (child);
8101 }
8102}
8103
fae299cd
DC
8104/* Get the low and high pc's represented by the scope DIE, and store
8105 them in *LOWPC and *HIGHPC. If the correct values can't be
8106 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8107
8108static void
8109get_scope_pc_bounds (struct die_info *die,
8110 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8111 struct dwarf2_cu *cu)
8112{
8113 CORE_ADDR best_low = (CORE_ADDR) -1;
8114 CORE_ADDR best_high = (CORE_ADDR) 0;
8115 CORE_ADDR current_low, current_high;
8116
d85a05f0 8117 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
8118 {
8119 best_low = current_low;
8120 best_high = current_high;
8121 }
8122 else
8123 {
8124 struct die_info *child = die->child;
8125
8126 while (child && child->tag)
8127 {
8128 switch (child->tag) {
8129 case DW_TAG_subprogram:
b084d499 8130 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
8131 break;
8132 case DW_TAG_namespace:
f55ee35c 8133 case DW_TAG_module:
fae299cd
DC
8134 /* FIXME: carlton/2004-01-16: Should we do this for
8135 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8136 that current GCC's always emit the DIEs corresponding
8137 to definitions of methods of classes as children of a
8138 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8139 the DIEs giving the declarations, which could be
8140 anywhere). But I don't see any reason why the
8141 standards says that they have to be there. */
8142 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8143
8144 if (current_low != ((CORE_ADDR) -1))
8145 {
8146 best_low = min (best_low, current_low);
8147 best_high = max (best_high, current_high);
8148 }
8149 break;
8150 default:
0963b4bd 8151 /* Ignore. */
fae299cd
DC
8152 break;
8153 }
8154
8155 child = sibling_die (child);
8156 }
8157 }
8158
8159 *lowpc = best_low;
8160 *highpc = best_high;
8161}
8162
801e3a5b
JB
8163/* Record the address ranges for BLOCK, offset by BASEADDR, as given
8164 in DIE. */
380bca97 8165
801e3a5b
JB
8166static void
8167dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8168 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8169{
bb5ed363 8170 struct objfile *objfile = cu->objfile;
801e3a5b 8171 struct attribute *attr;
91da1414 8172 struct attribute *attr_high;
801e3a5b 8173
91da1414
MW
8174 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8175 if (attr_high)
801e3a5b 8176 {
801e3a5b
JB
8177 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8178 if (attr)
8179 {
8180 CORE_ADDR low = DW_ADDR (attr);
91da1414 8181 CORE_ADDR high;
3019eac3
DE
8182 if (attr_high->form == DW_FORM_addr
8183 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8184 high = DW_ADDR (attr_high);
8185 else
8186 high = low + DW_UNSND (attr_high);
9a619af0 8187
801e3a5b
JB
8188 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8189 }
8190 }
8191
8192 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8193 if (attr)
8194 {
bb5ed363 8195 bfd *obfd = objfile->obfd;
801e3a5b
JB
8196
8197 /* The value of the DW_AT_ranges attribute is the offset of the
8198 address range list in the .debug_ranges section. */
8199 unsigned long offset = DW_UNSND (attr);
dce234bc 8200 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
8201
8202 /* For some target architectures, but not others, the
8203 read_address function sign-extends the addresses it returns.
8204 To recognize base address selection entries, we need a
8205 mask. */
8206 unsigned int addr_size = cu->header.addr_size;
8207 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8208
8209 /* The base address, to which the next pair is relative. Note
8210 that this 'base' is a DWARF concept: most entries in a range
8211 list are relative, to reduce the number of relocs against the
8212 debugging information. This is separate from this function's
8213 'baseaddr' argument, which GDB uses to relocate debugging
8214 information from a shared library based on the address at
8215 which the library was loaded. */
d00adf39
DE
8216 CORE_ADDR base = cu->base_address;
8217 int base_known = cu->base_known;
801e3a5b 8218
be391dca 8219 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 8220 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
8221 {
8222 complaint (&symfile_complaints,
8223 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8224 offset);
8225 return;
8226 }
8227
8228 for (;;)
8229 {
8230 unsigned int bytes_read;
8231 CORE_ADDR start, end;
8232
8233 start = read_address (obfd, buffer, cu, &bytes_read);
8234 buffer += bytes_read;
8235 end = read_address (obfd, buffer, cu, &bytes_read);
8236 buffer += bytes_read;
8237
8238 /* Did we find the end of the range list? */
8239 if (start == 0 && end == 0)
8240 break;
8241
8242 /* Did we find a base address selection entry? */
8243 else if ((start & base_select_mask) == base_select_mask)
8244 {
8245 base = end;
8246 base_known = 1;
8247 }
8248
8249 /* We found an ordinary address range. */
8250 else
8251 {
8252 if (!base_known)
8253 {
8254 complaint (&symfile_complaints,
3e43a32a
MS
8255 _("Invalid .debug_ranges data "
8256 "(no base address)"));
801e3a5b
JB
8257 return;
8258 }
8259
9277c30c
UW
8260 if (start > end)
8261 {
8262 /* Inverted range entries are invalid. */
8263 complaint (&symfile_complaints,
8264 _("Invalid .debug_ranges data "
8265 "(inverted range)"));
8266 return;
8267 }
8268
8269 /* Empty range entries have no effect. */
8270 if (start == end)
8271 continue;
8272
6e70227d
DE
8273 record_block_range (block,
8274 baseaddr + base + start,
801e3a5b
JB
8275 baseaddr + base + end - 1);
8276 }
8277 }
8278 }
8279}
8280
685b1105
JK
8281/* Check whether the producer field indicates either of GCC < 4.6, or the
8282 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 8283
685b1105
JK
8284static void
8285check_producer (struct dwarf2_cu *cu)
60d5a603
JK
8286{
8287 const char *cs;
8288 int major, minor, release;
8289
8290 if (cu->producer == NULL)
8291 {
8292 /* For unknown compilers expect their behavior is DWARF version
8293 compliant.
8294
8295 GCC started to support .debug_types sections by -gdwarf-4 since
8296 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8297 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8298 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8299 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 8300 }
685b1105 8301 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 8302 {
685b1105
JK
8303 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8304
ba919b58
TT
8305 cs = &cu->producer[strlen ("GNU ")];
8306 while (*cs && !isdigit (*cs))
8307 cs++;
8308 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8309 {
8310 /* Not recognized as GCC. */
8311 }
8312 else
685b1105
JK
8313 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
8314 }
8315 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
8316 cu->producer_is_icc = 1;
8317 else
8318 {
8319 /* For other non-GCC compilers, expect their behavior is DWARF version
8320 compliant. */
60d5a603
JK
8321 }
8322
ba919b58 8323 cu->checked_producer = 1;
685b1105 8324}
ba919b58 8325
685b1105
JK
8326/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8327 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8328 during 4.6.0 experimental. */
8329
8330static int
8331producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8332{
8333 if (!cu->checked_producer)
8334 check_producer (cu);
8335
8336 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
8337}
8338
8339/* Return the default accessibility type if it is not overriden by
8340 DW_AT_accessibility. */
8341
8342static enum dwarf_access_attribute
8343dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8344{
8345 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8346 {
8347 /* The default DWARF 2 accessibility for members is public, the default
8348 accessibility for inheritance is private. */
8349
8350 if (die->tag != DW_TAG_inheritance)
8351 return DW_ACCESS_public;
8352 else
8353 return DW_ACCESS_private;
8354 }
8355 else
8356 {
8357 /* DWARF 3+ defines the default accessibility a different way. The same
8358 rules apply now for DW_TAG_inheritance as for the members and it only
8359 depends on the container kind. */
8360
8361 if (die->parent->tag == DW_TAG_class_type)
8362 return DW_ACCESS_private;
8363 else
8364 return DW_ACCESS_public;
8365 }
8366}
8367
74ac6d43
TT
8368/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8369 offset. If the attribute was not found return 0, otherwise return
8370 1. If it was found but could not properly be handled, set *OFFSET
8371 to 0. */
8372
8373static int
8374handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8375 LONGEST *offset)
8376{
8377 struct attribute *attr;
8378
8379 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8380 if (attr != NULL)
8381 {
8382 *offset = 0;
8383
8384 /* Note that we do not check for a section offset first here.
8385 This is because DW_AT_data_member_location is new in DWARF 4,
8386 so if we see it, we can assume that a constant form is really
8387 a constant and not a section offset. */
8388 if (attr_form_is_constant (attr))
8389 *offset = dwarf2_get_attr_constant_value (attr, 0);
8390 else if (attr_form_is_section_offset (attr))
8391 dwarf2_complex_location_expr_complaint ();
8392 else if (attr_form_is_block (attr))
8393 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8394 else
8395 dwarf2_complex_location_expr_complaint ();
8396
8397 return 1;
8398 }
8399
8400 return 0;
8401}
8402
c906108c
SS
8403/* Add an aggregate field to the field list. */
8404
8405static void
107d2387 8406dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 8407 struct dwarf2_cu *cu)
6e70227d 8408{
e7c27a73 8409 struct objfile *objfile = cu->objfile;
5e2b427d 8410 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8411 struct nextfield *new_field;
8412 struct attribute *attr;
8413 struct field *fp;
8414 char *fieldname = "";
8415
8416 /* Allocate a new field list entry and link it in. */
8417 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 8418 make_cleanup (xfree, new_field);
c906108c 8419 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
8420
8421 if (die->tag == DW_TAG_inheritance)
8422 {
8423 new_field->next = fip->baseclasses;
8424 fip->baseclasses = new_field;
8425 }
8426 else
8427 {
8428 new_field->next = fip->fields;
8429 fip->fields = new_field;
8430 }
c906108c
SS
8431 fip->nfields++;
8432
e142c38c 8433 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
8434 if (attr)
8435 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
8436 else
8437 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
8438 if (new_field->accessibility != DW_ACCESS_public)
8439 fip->non_public_fields = 1;
60d5a603 8440
e142c38c 8441 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
8442 if (attr)
8443 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
8444 else
8445 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
8446
8447 fp = &new_field->field;
a9a9bd0f 8448
e142c38c 8449 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 8450 {
74ac6d43
TT
8451 LONGEST offset;
8452
a9a9bd0f 8453 /* Data member other than a C++ static data member. */
6e70227d 8454
c906108c 8455 /* Get type of field. */
e7c27a73 8456 fp->type = die_type (die, cu);
c906108c 8457
d6a843b5 8458 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 8459
c906108c 8460 /* Get bit size of field (zero if none). */
e142c38c 8461 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
8462 if (attr)
8463 {
8464 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8465 }
8466 else
8467 {
8468 FIELD_BITSIZE (*fp) = 0;
8469 }
8470
8471 /* Get bit offset of field. */
74ac6d43
TT
8472 if (handle_data_member_location (die, cu, &offset))
8473 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 8474 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
8475 if (attr)
8476 {
5e2b427d 8477 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
8478 {
8479 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
8480 additional bit offset from the MSB of the containing
8481 anonymous object to the MSB of the field. We don't
8482 have to do anything special since we don't need to
8483 know the size of the anonymous object. */
f41f5e61 8484 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
8485 }
8486 else
8487 {
8488 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
8489 MSB of the anonymous object, subtract off the number of
8490 bits from the MSB of the field to the MSB of the
8491 object, and then subtract off the number of bits of
8492 the field itself. The result is the bit offset of
8493 the LSB of the field. */
c906108c
SS
8494 int anonymous_size;
8495 int bit_offset = DW_UNSND (attr);
8496
e142c38c 8497 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8498 if (attr)
8499 {
8500 /* The size of the anonymous object containing
8501 the bit field is explicit, so use the
8502 indicated size (in bytes). */
8503 anonymous_size = DW_UNSND (attr);
8504 }
8505 else
8506 {
8507 /* The size of the anonymous object containing
8508 the bit field must be inferred from the type
8509 attribute of the data member containing the
8510 bit field. */
8511 anonymous_size = TYPE_LENGTH (fp->type);
8512 }
f41f5e61
PA
8513 SET_FIELD_BITPOS (*fp,
8514 (FIELD_BITPOS (*fp)
8515 + anonymous_size * bits_per_byte
8516 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
8517 }
8518 }
8519
8520 /* Get name of field. */
39cbfefa
DJ
8521 fieldname = dwarf2_name (die, cu);
8522 if (fieldname == NULL)
8523 fieldname = "";
d8151005
DJ
8524
8525 /* The name is already allocated along with this objfile, so we don't
8526 need to duplicate it for the type. */
8527 fp->name = fieldname;
c906108c
SS
8528
8529 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 8530 pointer or virtual base class pointer) to private. */
e142c38c 8531 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 8532 {
d48cc9dd 8533 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
8534 new_field->accessibility = DW_ACCESS_private;
8535 fip->non_public_fields = 1;
8536 }
8537 }
a9a9bd0f 8538 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 8539 {
a9a9bd0f
DC
8540 /* C++ static member. */
8541
8542 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8543 is a declaration, but all versions of G++ as of this writing
8544 (so through at least 3.2.1) incorrectly generate
8545 DW_TAG_variable tags. */
6e70227d 8546
ff355380 8547 const char *physname;
c906108c 8548
a9a9bd0f 8549 /* Get name of field. */
39cbfefa
DJ
8550 fieldname = dwarf2_name (die, cu);
8551 if (fieldname == NULL)
c906108c
SS
8552 return;
8553
254e6b9e 8554 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
8555 if (attr
8556 /* Only create a symbol if this is an external value.
8557 new_symbol checks this and puts the value in the global symbol
8558 table, which we want. If it is not external, new_symbol
8559 will try to put the value in cu->list_in_scope which is wrong. */
8560 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
8561 {
8562 /* A static const member, not much different than an enum as far as
8563 we're concerned, except that we can support more types. */
8564 new_symbol (die, NULL, cu);
8565 }
8566
2df3850c 8567 /* Get physical name. */
ff355380 8568 physname = dwarf2_physname (fieldname, die, cu);
c906108c 8569
d8151005
DJ
8570 /* The name is already allocated along with this objfile, so we don't
8571 need to duplicate it for the type. */
8572 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 8573 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 8574 FIELD_NAME (*fp) = fieldname;
c906108c
SS
8575 }
8576 else if (die->tag == DW_TAG_inheritance)
8577 {
74ac6d43 8578 LONGEST offset;
d4b96c9a 8579
74ac6d43
TT
8580 /* C++ base class field. */
8581 if (handle_data_member_location (die, cu, &offset))
8582 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 8583 FIELD_BITSIZE (*fp) = 0;
e7c27a73 8584 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
8585 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8586 fip->nbaseclasses++;
8587 }
8588}
8589
98751a41
JK
8590/* Add a typedef defined in the scope of the FIP's class. */
8591
8592static void
8593dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8594 struct dwarf2_cu *cu)
6e70227d 8595{
98751a41 8596 struct objfile *objfile = cu->objfile;
98751a41
JK
8597 struct typedef_field_list *new_field;
8598 struct attribute *attr;
8599 struct typedef_field *fp;
8600 char *fieldname = "";
8601
8602 /* Allocate a new field list entry and link it in. */
8603 new_field = xzalloc (sizeof (*new_field));
8604 make_cleanup (xfree, new_field);
8605
8606 gdb_assert (die->tag == DW_TAG_typedef);
8607
8608 fp = &new_field->field;
8609
8610 /* Get name of field. */
8611 fp->name = dwarf2_name (die, cu);
8612 if (fp->name == NULL)
8613 return;
8614
8615 fp->type = read_type_die (die, cu);
8616
8617 new_field->next = fip->typedef_field_list;
8618 fip->typedef_field_list = new_field;
8619 fip->typedef_field_list_count++;
8620}
8621
c906108c
SS
8622/* Create the vector of fields, and attach it to the type. */
8623
8624static void
fba45db2 8625dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8626 struct dwarf2_cu *cu)
c906108c
SS
8627{
8628 int nfields = fip->nfields;
8629
8630 /* Record the field count, allocate space for the array of fields,
8631 and create blank accessibility bitfields if necessary. */
8632 TYPE_NFIELDS (type) = nfields;
8633 TYPE_FIELDS (type) = (struct field *)
8634 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8635 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8636
b4ba55a1 8637 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
8638 {
8639 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8640
8641 TYPE_FIELD_PRIVATE_BITS (type) =
8642 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8643 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8644
8645 TYPE_FIELD_PROTECTED_BITS (type) =
8646 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8647 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8648
774b6a14
TT
8649 TYPE_FIELD_IGNORE_BITS (type) =
8650 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8651 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
8652 }
8653
8654 /* If the type has baseclasses, allocate and clear a bit vector for
8655 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 8656 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
8657 {
8658 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 8659 unsigned char *pointer;
c906108c
SS
8660
8661 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
8662 pointer = TYPE_ALLOC (type, num_bytes);
8663 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
8664 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8665 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8666 }
8667
3e43a32a
MS
8668 /* Copy the saved-up fields into the field vector. Start from the head of
8669 the list, adding to the tail of the field array, so that they end up in
8670 the same order in the array in which they were added to the list. */
c906108c
SS
8671 while (nfields-- > 0)
8672 {
7d0ccb61
DJ
8673 struct nextfield *fieldp;
8674
8675 if (fip->fields)
8676 {
8677 fieldp = fip->fields;
8678 fip->fields = fieldp->next;
8679 }
8680 else
8681 {
8682 fieldp = fip->baseclasses;
8683 fip->baseclasses = fieldp->next;
8684 }
8685
8686 TYPE_FIELD (type, nfields) = fieldp->field;
8687 switch (fieldp->accessibility)
c906108c 8688 {
c5aa993b 8689 case DW_ACCESS_private:
b4ba55a1
JB
8690 if (cu->language != language_ada)
8691 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 8692 break;
c906108c 8693
c5aa993b 8694 case DW_ACCESS_protected:
b4ba55a1
JB
8695 if (cu->language != language_ada)
8696 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 8697 break;
c906108c 8698
c5aa993b
JM
8699 case DW_ACCESS_public:
8700 break;
c906108c 8701
c5aa993b
JM
8702 default:
8703 /* Unknown accessibility. Complain and treat it as public. */
8704 {
e2e0b3e5 8705 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 8706 fieldp->accessibility);
c5aa993b
JM
8707 }
8708 break;
c906108c
SS
8709 }
8710 if (nfields < fip->nbaseclasses)
8711 {
7d0ccb61 8712 switch (fieldp->virtuality)
c906108c 8713 {
c5aa993b
JM
8714 case DW_VIRTUALITY_virtual:
8715 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 8716 if (cu->language == language_ada)
a73c6dcd 8717 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
8718 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8719 break;
c906108c
SS
8720 }
8721 }
c906108c
SS
8722 }
8723}
8724
c906108c
SS
8725/* Add a member function to the proper fieldlist. */
8726
8727static void
107d2387 8728dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 8729 struct type *type, struct dwarf2_cu *cu)
c906108c 8730{
e7c27a73 8731 struct objfile *objfile = cu->objfile;
c906108c
SS
8732 struct attribute *attr;
8733 struct fnfieldlist *flp;
8734 int i;
8735 struct fn_field *fnp;
8736 char *fieldname;
c906108c 8737 struct nextfnfield *new_fnfield;
f792889a 8738 struct type *this_type;
60d5a603 8739 enum dwarf_access_attribute accessibility;
c906108c 8740
b4ba55a1 8741 if (cu->language == language_ada)
a73c6dcd 8742 error (_("unexpected member function in Ada type"));
b4ba55a1 8743
2df3850c 8744 /* Get name of member function. */
39cbfefa
DJ
8745 fieldname = dwarf2_name (die, cu);
8746 if (fieldname == NULL)
2df3850c 8747 return;
c906108c 8748
c906108c
SS
8749 /* Look up member function name in fieldlist. */
8750 for (i = 0; i < fip->nfnfields; i++)
8751 {
27bfe10e 8752 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
8753 break;
8754 }
8755
8756 /* Create new list element if necessary. */
8757 if (i < fip->nfnfields)
8758 flp = &fip->fnfieldlists[i];
8759 else
8760 {
8761 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8762 {
8763 fip->fnfieldlists = (struct fnfieldlist *)
8764 xrealloc (fip->fnfieldlists,
8765 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8766 * sizeof (struct fnfieldlist));
c906108c 8767 if (fip->nfnfields == 0)
c13c43fd 8768 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
8769 }
8770 flp = &fip->fnfieldlists[fip->nfnfields];
8771 flp->name = fieldname;
8772 flp->length = 0;
8773 flp->head = NULL;
3da10d80 8774 i = fip->nfnfields++;
c906108c
SS
8775 }
8776
8777 /* Create a new member function field and chain it to the field list
0963b4bd 8778 entry. */
c906108c 8779 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 8780 make_cleanup (xfree, new_fnfield);
c906108c
SS
8781 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8782 new_fnfield->next = flp->head;
8783 flp->head = new_fnfield;
8784 flp->length++;
8785
8786 /* Fill in the member function field info. */
8787 fnp = &new_fnfield->fnfield;
3da10d80
KS
8788
8789 /* Delay processing of the physname until later. */
8790 if (cu->language == language_cplus || cu->language == language_java)
8791 {
8792 add_to_method_list (type, i, flp->length - 1, fieldname,
8793 die, cu);
8794 }
8795 else
8796 {
1d06ead6 8797 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
8798 fnp->physname = physname ? physname : "";
8799 }
8800
c906108c 8801 fnp->type = alloc_type (objfile);
f792889a
DJ
8802 this_type = read_type_die (die, cu);
8803 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 8804 {
f792889a 8805 int nparams = TYPE_NFIELDS (this_type);
c906108c 8806
f792889a 8807 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
8808 of the method itself (TYPE_CODE_METHOD). */
8809 smash_to_method_type (fnp->type, type,
f792889a
DJ
8810 TYPE_TARGET_TYPE (this_type),
8811 TYPE_FIELDS (this_type),
8812 TYPE_NFIELDS (this_type),
8813 TYPE_VARARGS (this_type));
c906108c
SS
8814
8815 /* Handle static member functions.
c5aa993b 8816 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
8817 member functions. G++ helps GDB by marking the first
8818 parameter for non-static member functions (which is the this
8819 pointer) as artificial. We obtain this information from
8820 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 8821 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
8822 fnp->voffset = VOFFSET_STATIC;
8823 }
8824 else
e2e0b3e5 8825 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 8826 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
8827
8828 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 8829 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 8830 fnp->fcontext = die_containing_type (die, cu);
c906108c 8831
3e43a32a
MS
8832 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
8833 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
8834
8835 /* Get accessibility. */
e142c38c 8836 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 8837 if (attr)
60d5a603
JK
8838 accessibility = DW_UNSND (attr);
8839 else
8840 accessibility = dwarf2_default_access_attribute (die, cu);
8841 switch (accessibility)
c906108c 8842 {
60d5a603
JK
8843 case DW_ACCESS_private:
8844 fnp->is_private = 1;
8845 break;
8846 case DW_ACCESS_protected:
8847 fnp->is_protected = 1;
8848 break;
c906108c
SS
8849 }
8850
b02dede2 8851 /* Check for artificial methods. */
e142c38c 8852 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
8853 if (attr && DW_UNSND (attr) != 0)
8854 fnp->is_artificial = 1;
8855
0d564a31 8856 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
8857 function. For older versions of GCC, this is an offset in the
8858 appropriate virtual table, as specified by DW_AT_containing_type.
8859 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
8860 to the object address. */
8861
e142c38c 8862 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 8863 if (attr)
8e19ed76 8864 {
aec5aa8b 8865 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 8866 {
aec5aa8b
TT
8867 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
8868 {
8869 /* Old-style GCC. */
8870 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8871 }
8872 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
8873 || (DW_BLOCK (attr)->size > 1
8874 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
8875 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
8876 {
8877 struct dwarf_block blk;
8878 int offset;
8879
8880 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
8881 ? 1 : 2);
8882 blk.size = DW_BLOCK (attr)->size - offset;
8883 blk.data = DW_BLOCK (attr)->data + offset;
8884 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
8885 if ((fnp->voffset % cu->header.addr_size) != 0)
8886 dwarf2_complex_location_expr_complaint ();
8887 else
8888 fnp->voffset /= cu->header.addr_size;
8889 fnp->voffset += 2;
8890 }
8891 else
8892 dwarf2_complex_location_expr_complaint ();
8893
8894 if (!fnp->fcontext)
8895 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
8896 }
3690dd37 8897 else if (attr_form_is_section_offset (attr))
8e19ed76 8898 {
4d3c2250 8899 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8900 }
8901 else
8902 {
4d3c2250
KB
8903 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
8904 fieldname);
8e19ed76 8905 }
0d564a31 8906 }
d48cc9dd
DJ
8907 else
8908 {
8909 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8910 if (attr && DW_UNSND (attr))
8911 {
8912 /* GCC does this, as of 2008-08-25; PR debug/37237. */
8913 complaint (&symfile_complaints,
3e43a32a
MS
8914 _("Member function \"%s\" (offset %d) is virtual "
8915 "but the vtable offset is not specified"),
b64f50a1 8916 fieldname, die->offset.sect_off);
9655fd1a 8917 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
8918 TYPE_CPLUS_DYNAMIC (type) = 1;
8919 }
8920 }
c906108c
SS
8921}
8922
8923/* Create the vector of member function fields, and attach it to the type. */
8924
8925static void
fba45db2 8926dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8927 struct dwarf2_cu *cu)
c906108c
SS
8928{
8929 struct fnfieldlist *flp;
c906108c
SS
8930 int i;
8931
b4ba55a1 8932 if (cu->language == language_ada)
a73c6dcd 8933 error (_("unexpected member functions in Ada type"));
b4ba55a1 8934
c906108c
SS
8935 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8936 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
8937 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
8938
8939 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
8940 {
8941 struct nextfnfield *nfp = flp->head;
8942 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
8943 int k;
8944
8945 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
8946 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
8947 fn_flp->fn_fields = (struct fn_field *)
8948 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
8949 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 8950 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
8951 }
8952
8953 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
8954}
8955
1168df01
JB
8956/* Returns non-zero if NAME is the name of a vtable member in CU's
8957 language, zero otherwise. */
8958static int
8959is_vtable_name (const char *name, struct dwarf2_cu *cu)
8960{
8961 static const char vptr[] = "_vptr";
987504bb 8962 static const char vtable[] = "vtable";
1168df01 8963
987504bb
JJ
8964 /* Look for the C++ and Java forms of the vtable. */
8965 if ((cu->language == language_java
8966 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
8967 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
8968 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
8969 return 1;
8970
8971 return 0;
8972}
8973
c0dd20ea 8974/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
8975 functions, with the ABI-specified layout. If TYPE describes
8976 such a structure, smash it into a member function type.
61049d3b
DJ
8977
8978 GCC shouldn't do this; it should just output pointer to member DIEs.
8979 This is GCC PR debug/28767. */
c0dd20ea 8980
0b92b5bb
TT
8981static void
8982quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 8983{
0b92b5bb 8984 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
8985
8986 /* Check for a structure with no name and two children. */
0b92b5bb
TT
8987 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
8988 return;
c0dd20ea
DJ
8989
8990 /* Check for __pfn and __delta members. */
0b92b5bb
TT
8991 if (TYPE_FIELD_NAME (type, 0) == NULL
8992 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
8993 || TYPE_FIELD_NAME (type, 1) == NULL
8994 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
8995 return;
c0dd20ea
DJ
8996
8997 /* Find the type of the method. */
0b92b5bb 8998 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
8999 if (pfn_type == NULL
9000 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
9001 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 9002 return;
c0dd20ea
DJ
9003
9004 /* Look for the "this" argument. */
9005 pfn_type = TYPE_TARGET_TYPE (pfn_type);
9006 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 9007 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 9008 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 9009 return;
c0dd20ea
DJ
9010
9011 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
9012 new_type = alloc_type (objfile);
9013 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
9014 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
9015 TYPE_VARARGS (pfn_type));
0b92b5bb 9016 smash_to_methodptr_type (type, new_type);
c0dd20ea 9017}
1168df01 9018
685b1105
JK
9019/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
9020 (icc). */
9021
9022static int
9023producer_is_icc (struct dwarf2_cu *cu)
9024{
9025 if (!cu->checked_producer)
9026 check_producer (cu);
9027
9028 return cu->producer_is_icc;
9029}
9030
c906108c 9031/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
9032 (definition) to create a type for the structure or union. Fill in
9033 the type's name and general properties; the members will not be
9034 processed until process_structure_type.
c906108c 9035
c767944b
DJ
9036 NOTE: we need to call these functions regardless of whether or not the
9037 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
9038 structure or union. This gets the type entered into our set of
9039 user defined types.
9040
9041 However, if the structure is incomplete (an opaque struct/union)
9042 then suppress creating a symbol table entry for it since gdb only
9043 wants to find the one with the complete definition. Note that if
9044 it is complete, we just call new_symbol, which does it's own
9045 checking about whether the struct/union is anonymous or not (and
9046 suppresses creating a symbol table entry itself). */
9047
f792889a 9048static struct type *
134d01f1 9049read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9050{
e7c27a73 9051 struct objfile *objfile = cu->objfile;
c906108c
SS
9052 struct type *type;
9053 struct attribute *attr;
39cbfefa 9054 char *name;
c906108c 9055
348e048f
DE
9056 /* If the definition of this type lives in .debug_types, read that type.
9057 Don't follow DW_AT_specification though, that will take us back up
9058 the chain and we want to go down. */
9059 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9060 if (attr)
9061 {
9062 struct dwarf2_cu *type_cu = cu;
9063 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9064
348e048f
DE
9065 /* We could just recurse on read_structure_type, but we need to call
9066 get_die_type to ensure only one type for this DIE is created.
9067 This is important, for example, because for c++ classes we need
9068 TYPE_NAME set which is only done by new_symbol. Blech. */
9069 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9070
9071 /* TYPE_CU may not be the same as CU.
9072 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9073 return set_die_type (die, type, cu);
9074 }
9075
c0dd20ea 9076 type = alloc_type (objfile);
c906108c 9077 INIT_CPLUS_SPECIFIC (type);
93311388 9078
39cbfefa
DJ
9079 name = dwarf2_name (die, cu);
9080 if (name != NULL)
c906108c 9081 {
987504bb
JJ
9082 if (cu->language == language_cplus
9083 || cu->language == language_java)
63d06c5c 9084 {
3da10d80
KS
9085 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9086
9087 /* dwarf2_full_name might have already finished building the DIE's
9088 type. If so, there is no need to continue. */
9089 if (get_die_type (die, cu) != NULL)
9090 return get_die_type (die, cu);
9091
9092 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
9093 if (die->tag == DW_TAG_structure_type
9094 || die->tag == DW_TAG_class_type)
9095 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
9096 }
9097 else
9098 {
d8151005
DJ
9099 /* The name is already allocated along with this objfile, so
9100 we don't need to duplicate it for the type. */
94af9270
KS
9101 TYPE_TAG_NAME (type) = (char *) name;
9102 if (die->tag == DW_TAG_class_type)
9103 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 9104 }
c906108c
SS
9105 }
9106
9107 if (die->tag == DW_TAG_structure_type)
9108 {
9109 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9110 }
9111 else if (die->tag == DW_TAG_union_type)
9112 {
9113 TYPE_CODE (type) = TYPE_CODE_UNION;
9114 }
9115 else
9116 {
c906108c
SS
9117 TYPE_CODE (type) = TYPE_CODE_CLASS;
9118 }
9119
0cc2414c
TT
9120 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9121 TYPE_DECLARED_CLASS (type) = 1;
9122
e142c38c 9123 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9124 if (attr)
9125 {
9126 TYPE_LENGTH (type) = DW_UNSND (attr);
9127 }
9128 else
9129 {
9130 TYPE_LENGTH (type) = 0;
9131 }
9132
685b1105
JK
9133 if (producer_is_icc (cu))
9134 {
9135 /* ICC does not output the required DW_AT_declaration
9136 on incomplete types, but gives them a size of zero. */
9137 }
9138 else
9139 TYPE_STUB_SUPPORTED (type) = 1;
9140
dc718098 9141 if (die_is_declaration (die, cu))
876cecd0 9142 TYPE_STUB (type) = 1;
a6c727b2
DJ
9143 else if (attr == NULL && die->child == NULL
9144 && producer_is_realview (cu->producer))
9145 /* RealView does not output the required DW_AT_declaration
9146 on incomplete types. */
9147 TYPE_STUB (type) = 1;
dc718098 9148
c906108c
SS
9149 /* We need to add the type field to the die immediately so we don't
9150 infinitely recurse when dealing with pointers to the structure
0963b4bd 9151 type within the structure itself. */
1c379e20 9152 set_die_type (die, type, cu);
c906108c 9153
7e314c57
JK
9154 /* set_die_type should be already done. */
9155 set_descriptive_type (type, die, cu);
9156
c767944b
DJ
9157 return type;
9158}
9159
9160/* Finish creating a structure or union type, including filling in
9161 its members and creating a symbol for it. */
9162
9163static void
9164process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9165{
9166 struct objfile *objfile = cu->objfile;
9167 struct die_info *child_die = die->child;
9168 struct type *type;
9169
9170 type = get_die_type (die, cu);
9171 if (type == NULL)
9172 type = read_structure_type (die, cu);
9173
e142c38c 9174 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
9175 {
9176 struct field_info fi;
9177 struct die_info *child_die;
34eaf542 9178 VEC (symbolp) *template_args = NULL;
c767944b 9179 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
9180
9181 memset (&fi, 0, sizeof (struct field_info));
9182
639d11d3 9183 child_die = die->child;
c906108c
SS
9184
9185 while (child_die && child_die->tag)
9186 {
a9a9bd0f
DC
9187 if (child_die->tag == DW_TAG_member
9188 || child_die->tag == DW_TAG_variable)
c906108c 9189 {
a9a9bd0f
DC
9190 /* NOTE: carlton/2002-11-05: A C++ static data member
9191 should be a DW_TAG_member that is a declaration, but
9192 all versions of G++ as of this writing (so through at
9193 least 3.2.1) incorrectly generate DW_TAG_variable
9194 tags for them instead. */
e7c27a73 9195 dwarf2_add_field (&fi, child_die, cu);
c906108c 9196 }
8713b1b1 9197 else if (child_die->tag == DW_TAG_subprogram)
c906108c 9198 {
0963b4bd 9199 /* C++ member function. */
e7c27a73 9200 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
9201 }
9202 else if (child_die->tag == DW_TAG_inheritance)
9203 {
9204 /* C++ base class field. */
e7c27a73 9205 dwarf2_add_field (&fi, child_die, cu);
c906108c 9206 }
98751a41
JK
9207 else if (child_die->tag == DW_TAG_typedef)
9208 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
9209 else if (child_die->tag == DW_TAG_template_type_param
9210 || child_die->tag == DW_TAG_template_value_param)
9211 {
9212 struct symbol *arg = new_symbol (child_die, NULL, cu);
9213
f1078f66
DJ
9214 if (arg != NULL)
9215 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9216 }
9217
c906108c
SS
9218 child_die = sibling_die (child_die);
9219 }
9220
34eaf542
TT
9221 /* Attach template arguments to type. */
9222 if (! VEC_empty (symbolp, template_args))
9223 {
9224 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9225 TYPE_N_TEMPLATE_ARGUMENTS (type)
9226 = VEC_length (symbolp, template_args);
9227 TYPE_TEMPLATE_ARGUMENTS (type)
9228 = obstack_alloc (&objfile->objfile_obstack,
9229 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9230 * sizeof (struct symbol *)));
9231 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9232 VEC_address (symbolp, template_args),
9233 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9234 * sizeof (struct symbol *)));
9235 VEC_free (symbolp, template_args);
9236 }
9237
c906108c
SS
9238 /* Attach fields and member functions to the type. */
9239 if (fi.nfields)
e7c27a73 9240 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
9241 if (fi.nfnfields)
9242 {
e7c27a73 9243 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 9244
c5aa993b 9245 /* Get the type which refers to the base class (possibly this
c906108c 9246 class itself) which contains the vtable pointer for the current
0d564a31
DJ
9247 class from the DW_AT_containing_type attribute. This use of
9248 DW_AT_containing_type is a GNU extension. */
c906108c 9249
e142c38c 9250 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 9251 {
e7c27a73 9252 struct type *t = die_containing_type (die, cu);
c906108c
SS
9253
9254 TYPE_VPTR_BASETYPE (type) = t;
9255 if (type == t)
9256 {
c906108c
SS
9257 int i;
9258
9259 /* Our own class provides vtbl ptr. */
9260 for (i = TYPE_NFIELDS (t) - 1;
9261 i >= TYPE_N_BASECLASSES (t);
9262 --i)
9263 {
0d5cff50 9264 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 9265
1168df01 9266 if (is_vtable_name (fieldname, cu))
c906108c
SS
9267 {
9268 TYPE_VPTR_FIELDNO (type) = i;
9269 break;
9270 }
9271 }
9272
9273 /* Complain if virtual function table field not found. */
9274 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 9275 complaint (&symfile_complaints,
3e43a32a
MS
9276 _("virtual function table pointer "
9277 "not found when defining class '%s'"),
4d3c2250
KB
9278 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9279 "");
c906108c
SS
9280 }
9281 else
9282 {
9283 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9284 }
9285 }
f6235d4c
EZ
9286 else if (cu->producer
9287 && strncmp (cu->producer,
9288 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9289 {
9290 /* The IBM XLC compiler does not provide direct indication
9291 of the containing type, but the vtable pointer is
9292 always named __vfp. */
9293
9294 int i;
9295
9296 for (i = TYPE_NFIELDS (type) - 1;
9297 i >= TYPE_N_BASECLASSES (type);
9298 --i)
9299 {
9300 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9301 {
9302 TYPE_VPTR_FIELDNO (type) = i;
9303 TYPE_VPTR_BASETYPE (type) = type;
9304 break;
9305 }
9306 }
9307 }
c906108c 9308 }
98751a41
JK
9309
9310 /* Copy fi.typedef_field_list linked list elements content into the
9311 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9312 if (fi.typedef_field_list)
9313 {
9314 int i = fi.typedef_field_list_count;
9315
a0d7a4ff 9316 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
9317 TYPE_TYPEDEF_FIELD_ARRAY (type)
9318 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9319 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9320
9321 /* Reverse the list order to keep the debug info elements order. */
9322 while (--i >= 0)
9323 {
9324 struct typedef_field *dest, *src;
6e70227d 9325
98751a41
JK
9326 dest = &TYPE_TYPEDEF_FIELD (type, i);
9327 src = &fi.typedef_field_list->field;
9328 fi.typedef_field_list = fi.typedef_field_list->next;
9329 *dest = *src;
9330 }
9331 }
c767944b
DJ
9332
9333 do_cleanups (back_to);
eb2a6f42
TT
9334
9335 if (HAVE_CPLUS_STRUCT (type))
9336 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 9337 }
63d06c5c 9338
bb5ed363 9339 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 9340
90aeadfc
DC
9341 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9342 snapshots) has been known to create a die giving a declaration
9343 for a class that has, as a child, a die giving a definition for a
9344 nested class. So we have to process our children even if the
9345 current die is a declaration. Normally, of course, a declaration
9346 won't have any children at all. */
134d01f1 9347
90aeadfc
DC
9348 while (child_die != NULL && child_die->tag)
9349 {
9350 if (child_die->tag == DW_TAG_member
9351 || child_die->tag == DW_TAG_variable
34eaf542
TT
9352 || child_die->tag == DW_TAG_inheritance
9353 || child_die->tag == DW_TAG_template_value_param
9354 || child_die->tag == DW_TAG_template_type_param)
134d01f1 9355 {
90aeadfc 9356 /* Do nothing. */
134d01f1 9357 }
90aeadfc
DC
9358 else
9359 process_die (child_die, cu);
134d01f1 9360
90aeadfc 9361 child_die = sibling_die (child_die);
134d01f1
DJ
9362 }
9363
fa4028e9
JB
9364 /* Do not consider external references. According to the DWARF standard,
9365 these DIEs are identified by the fact that they have no byte_size
9366 attribute, and a declaration attribute. */
9367 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9368 || !die_is_declaration (die, cu))
c767944b 9369 new_symbol (die, type, cu);
134d01f1
DJ
9370}
9371
9372/* Given a DW_AT_enumeration_type die, set its type. We do not
9373 complete the type's fields yet, or create any symbols. */
c906108c 9374
f792889a 9375static struct type *
134d01f1 9376read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9377{
e7c27a73 9378 struct objfile *objfile = cu->objfile;
c906108c 9379 struct type *type;
c906108c 9380 struct attribute *attr;
0114d602 9381 const char *name;
134d01f1 9382
348e048f
DE
9383 /* If the definition of this type lives in .debug_types, read that type.
9384 Don't follow DW_AT_specification though, that will take us back up
9385 the chain and we want to go down. */
9386 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9387 if (attr)
9388 {
9389 struct dwarf2_cu *type_cu = cu;
9390 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9391
348e048f 9392 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9393
9394 /* TYPE_CU may not be the same as CU.
9395 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9396 return set_die_type (die, type, cu);
9397 }
9398
c906108c
SS
9399 type = alloc_type (objfile);
9400
9401 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 9402 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 9403 if (name != NULL)
0114d602 9404 TYPE_TAG_NAME (type) = (char *) name;
c906108c 9405
e142c38c 9406 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9407 if (attr)
9408 {
9409 TYPE_LENGTH (type) = DW_UNSND (attr);
9410 }
9411 else
9412 {
9413 TYPE_LENGTH (type) = 0;
9414 }
9415
137033e9
JB
9416 /* The enumeration DIE can be incomplete. In Ada, any type can be
9417 declared as private in the package spec, and then defined only
9418 inside the package body. Such types are known as Taft Amendment
9419 Types. When another package uses such a type, an incomplete DIE
9420 may be generated by the compiler. */
02eb380e 9421 if (die_is_declaration (die, cu))
876cecd0 9422 TYPE_STUB (type) = 1;
02eb380e 9423
f792889a 9424 return set_die_type (die, type, cu);
134d01f1
DJ
9425}
9426
9427/* Given a pointer to a die which begins an enumeration, process all
9428 the dies that define the members of the enumeration, and create the
9429 symbol for the enumeration type.
9430
9431 NOTE: We reverse the order of the element list. */
9432
9433static void
9434process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9435{
f792889a 9436 struct type *this_type;
134d01f1 9437
f792889a
DJ
9438 this_type = get_die_type (die, cu);
9439 if (this_type == NULL)
9440 this_type = read_enumeration_type (die, cu);
9dc481d3 9441
639d11d3 9442 if (die->child != NULL)
c906108c 9443 {
9dc481d3
DE
9444 struct die_info *child_die;
9445 struct symbol *sym;
9446 struct field *fields = NULL;
9447 int num_fields = 0;
9448 int unsigned_enum = 1;
9449 char *name;
cafec441
TT
9450 int flag_enum = 1;
9451 ULONGEST mask = 0;
9dc481d3 9452
639d11d3 9453 child_die = die->child;
c906108c
SS
9454 while (child_die && child_die->tag)
9455 {
9456 if (child_die->tag != DW_TAG_enumerator)
9457 {
e7c27a73 9458 process_die (child_die, cu);
c906108c
SS
9459 }
9460 else
9461 {
39cbfefa
DJ
9462 name = dwarf2_name (child_die, cu);
9463 if (name)
c906108c 9464 {
f792889a 9465 sym = new_symbol (child_die, this_type, cu);
c906108c 9466 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
9467 {
9468 unsigned_enum = 0;
9469 flag_enum = 0;
9470 }
9471 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9472 flag_enum = 0;
9473 else
9474 mask |= SYMBOL_VALUE (sym);
c906108c
SS
9475
9476 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9477 {
9478 fields = (struct field *)
9479 xrealloc (fields,
9480 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 9481 * sizeof (struct field));
c906108c
SS
9482 }
9483
3567439c 9484 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 9485 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 9486 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
9487 FIELD_BITSIZE (fields[num_fields]) = 0;
9488
9489 num_fields++;
9490 }
9491 }
9492
9493 child_die = sibling_die (child_die);
9494 }
9495
9496 if (num_fields)
9497 {
f792889a
DJ
9498 TYPE_NFIELDS (this_type) = num_fields;
9499 TYPE_FIELDS (this_type) = (struct field *)
9500 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9501 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 9502 sizeof (struct field) * num_fields);
b8c9b27d 9503 xfree (fields);
c906108c
SS
9504 }
9505 if (unsigned_enum)
876cecd0 9506 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
9507 if (flag_enum)
9508 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 9509 }
134d01f1 9510
6c83ed52
TT
9511 /* If we are reading an enum from a .debug_types unit, and the enum
9512 is a declaration, and the enum is not the signatured type in the
9513 unit, then we do not want to add a symbol for it. Adding a
9514 symbol would in some cases obscure the true definition of the
9515 enum, giving users an incomplete type when the definition is
9516 actually available. Note that we do not want to do this for all
9517 enums which are just declarations, because C++0x allows forward
9518 enum declarations. */
3019eac3 9519 if (cu->per_cu->is_debug_types
6c83ed52
TT
9520 && die_is_declaration (die, cu))
9521 {
52dc124a 9522 struct signatured_type *sig_type;
6c83ed52 9523
52dc124a 9524 sig_type
6c83ed52 9525 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 9526 cu->per_cu->info_or_types_section,
6c83ed52 9527 cu->per_cu->offset);
3019eac3
DE
9528 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9529 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
9530 return;
9531 }
9532
f792889a 9533 new_symbol (die, this_type, cu);
c906108c
SS
9534}
9535
9536/* Extract all information from a DW_TAG_array_type DIE and put it in
9537 the DIE's type field. For now, this only handles one dimensional
9538 arrays. */
9539
f792889a 9540static struct type *
e7c27a73 9541read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9542{
e7c27a73 9543 struct objfile *objfile = cu->objfile;
c906108c 9544 struct die_info *child_die;
7e314c57 9545 struct type *type;
c906108c
SS
9546 struct type *element_type, *range_type, *index_type;
9547 struct type **range_types = NULL;
9548 struct attribute *attr;
9549 int ndim = 0;
9550 struct cleanup *back_to;
39cbfefa 9551 char *name;
c906108c 9552
e7c27a73 9553 element_type = die_type (die, cu);
c906108c 9554
7e314c57
JK
9555 /* The die_type call above may have already set the type for this DIE. */
9556 type = get_die_type (die, cu);
9557 if (type)
9558 return type;
9559
c906108c
SS
9560 /* Irix 6.2 native cc creates array types without children for
9561 arrays with unspecified length. */
639d11d3 9562 if (die->child == NULL)
c906108c 9563 {
46bf5051 9564 index_type = objfile_type (objfile)->builtin_int;
c906108c 9565 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
9566 type = create_array_type (NULL, element_type, range_type);
9567 return set_die_type (die, type, cu);
c906108c
SS
9568 }
9569
9570 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 9571 child_die = die->child;
c906108c
SS
9572 while (child_die && child_die->tag)
9573 {
9574 if (child_die->tag == DW_TAG_subrange_type)
9575 {
f792889a 9576 struct type *child_type = read_type_die (child_die, cu);
9a619af0 9577
f792889a 9578 if (child_type != NULL)
a02abb62 9579 {
0963b4bd
MS
9580 /* The range type was succesfully read. Save it for the
9581 array type creation. */
a02abb62
JB
9582 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9583 {
9584 range_types = (struct type **)
9585 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9586 * sizeof (struct type *));
9587 if (ndim == 0)
9588 make_cleanup (free_current_contents, &range_types);
9589 }
f792889a 9590 range_types[ndim++] = child_type;
a02abb62 9591 }
c906108c
SS
9592 }
9593 child_die = sibling_die (child_die);
9594 }
9595
9596 /* Dwarf2 dimensions are output from left to right, create the
9597 necessary array types in backwards order. */
7ca2d3a3 9598
c906108c 9599 type = element_type;
7ca2d3a3
DL
9600
9601 if (read_array_order (die, cu) == DW_ORD_col_major)
9602 {
9603 int i = 0;
9a619af0 9604
7ca2d3a3
DL
9605 while (i < ndim)
9606 type = create_array_type (NULL, type, range_types[i++]);
9607 }
9608 else
9609 {
9610 while (ndim-- > 0)
9611 type = create_array_type (NULL, type, range_types[ndim]);
9612 }
c906108c 9613
f5f8a009
EZ
9614 /* Understand Dwarf2 support for vector types (like they occur on
9615 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9616 array type. This is not part of the Dwarf2/3 standard yet, but a
9617 custom vendor extension. The main difference between a regular
9618 array and the vector variant is that vectors are passed by value
9619 to functions. */
e142c38c 9620 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 9621 if (attr)
ea37ba09 9622 make_vector_type (type);
f5f8a009 9623
dbc98a8b
KW
9624 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9625 implementation may choose to implement triple vectors using this
9626 attribute. */
9627 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9628 if (attr)
9629 {
9630 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9631 TYPE_LENGTH (type) = DW_UNSND (attr);
9632 else
3e43a32a
MS
9633 complaint (&symfile_complaints,
9634 _("DW_AT_byte_size for array type smaller "
9635 "than the total size of elements"));
dbc98a8b
KW
9636 }
9637
39cbfefa
DJ
9638 name = dwarf2_name (die, cu);
9639 if (name)
9640 TYPE_NAME (type) = name;
6e70227d 9641
0963b4bd 9642 /* Install the type in the die. */
7e314c57
JK
9643 set_die_type (die, type, cu);
9644
9645 /* set_die_type should be already done. */
b4ba55a1
JB
9646 set_descriptive_type (type, die, cu);
9647
c906108c
SS
9648 do_cleanups (back_to);
9649
7e314c57 9650 return type;
c906108c
SS
9651}
9652
7ca2d3a3 9653static enum dwarf_array_dim_ordering
6e70227d 9654read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
9655{
9656 struct attribute *attr;
9657
9658 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9659
9660 if (attr) return DW_SND (attr);
9661
0963b4bd
MS
9662 /* GNU F77 is a special case, as at 08/2004 array type info is the
9663 opposite order to the dwarf2 specification, but data is still
9664 laid out as per normal fortran.
7ca2d3a3 9665
0963b4bd
MS
9666 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9667 version checking. */
7ca2d3a3 9668
905e0470
PM
9669 if (cu->language == language_fortran
9670 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
9671 {
9672 return DW_ORD_row_major;
9673 }
9674
6e70227d 9675 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
9676 {
9677 case array_column_major:
9678 return DW_ORD_col_major;
9679 case array_row_major:
9680 default:
9681 return DW_ORD_row_major;
9682 };
9683}
9684
72019c9c 9685/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 9686 the DIE's type field. */
72019c9c 9687
f792889a 9688static struct type *
72019c9c
GM
9689read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9690{
7e314c57
JK
9691 struct type *domain_type, *set_type;
9692 struct attribute *attr;
f792889a 9693
7e314c57
JK
9694 domain_type = die_type (die, cu);
9695
9696 /* The die_type call above may have already set the type for this DIE. */
9697 set_type = get_die_type (die, cu);
9698 if (set_type)
9699 return set_type;
9700
9701 set_type = create_set_type (NULL, domain_type);
9702
9703 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
9704 if (attr)
9705 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 9706
f792889a 9707 return set_die_type (die, set_type, cu);
72019c9c 9708}
7ca2d3a3 9709
c906108c
SS
9710/* First cut: install each common block member as a global variable. */
9711
9712static void
e7c27a73 9713read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
9714{
9715 struct die_info *child_die;
9716 struct attribute *attr;
9717 struct symbol *sym;
9718 CORE_ADDR base = (CORE_ADDR) 0;
9719
e142c38c 9720 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
9721 if (attr)
9722 {
0963b4bd 9723 /* Support the .debug_loc offsets. */
8e19ed76
PS
9724 if (attr_form_is_block (attr))
9725 {
e7c27a73 9726 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 9727 }
3690dd37 9728 else if (attr_form_is_section_offset (attr))
8e19ed76 9729 {
4d3c2250 9730 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9731 }
9732 else
9733 {
4d3c2250
KB
9734 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9735 "common block member");
8e19ed76 9736 }
c906108c 9737 }
639d11d3 9738 if (die->child != NULL)
c906108c 9739 {
639d11d3 9740 child_die = die->child;
c906108c
SS
9741 while (child_die && child_die->tag)
9742 {
74ac6d43
TT
9743 LONGEST offset;
9744
e7c27a73 9745 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
9746 if (sym != NULL
9747 && handle_data_member_location (child_die, cu, &offset))
c906108c 9748 {
74ac6d43 9749 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
9750 add_symbol_to_list (sym, &global_symbols);
9751 }
9752 child_die = sibling_die (child_die);
9753 }
9754 }
9755}
9756
0114d602 9757/* Create a type for a C++ namespace. */
d9fa45fe 9758
0114d602
DJ
9759static struct type *
9760read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 9761{
e7c27a73 9762 struct objfile *objfile = cu->objfile;
0114d602 9763 const char *previous_prefix, *name;
9219021c 9764 int is_anonymous;
0114d602
DJ
9765 struct type *type;
9766
9767 /* For extensions, reuse the type of the original namespace. */
9768 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9769 {
9770 struct die_info *ext_die;
9771 struct dwarf2_cu *ext_cu = cu;
9a619af0 9772
0114d602
DJ
9773 ext_die = dwarf2_extension (die, &ext_cu);
9774 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
9775
9776 /* EXT_CU may not be the same as CU.
9777 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
9778 return set_die_type (die, type, cu);
9779 }
9219021c 9780
e142c38c 9781 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
9782
9783 /* Now build the name of the current namespace. */
9784
0114d602
DJ
9785 previous_prefix = determine_prefix (die, cu);
9786 if (previous_prefix[0] != '\0')
9787 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 9788 previous_prefix, name, 0, cu);
0114d602
DJ
9789
9790 /* Create the type. */
9791 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
9792 objfile);
9793 TYPE_NAME (type) = (char *) name;
9794 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9795
60531b24 9796 return set_die_type (die, type, cu);
0114d602
DJ
9797}
9798
9799/* Read a C++ namespace. */
9800
9801static void
9802read_namespace (struct die_info *die, struct dwarf2_cu *cu)
9803{
9804 struct objfile *objfile = cu->objfile;
0114d602 9805 int is_anonymous;
9219021c 9806
5c4e30ca
DC
9807 /* Add a symbol associated to this if we haven't seen the namespace
9808 before. Also, add a using directive if it's an anonymous
9809 namespace. */
9219021c 9810
f2f0e013 9811 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
9812 {
9813 struct type *type;
9814
0114d602 9815 type = read_type_die (die, cu);
e7c27a73 9816 new_symbol (die, type, cu);
5c4e30ca 9817
e8e80198 9818 namespace_name (die, &is_anonymous, cu);
5c4e30ca 9819 if (is_anonymous)
0114d602
DJ
9820 {
9821 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 9822
c0cc3a76 9823 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 9824 NULL, NULL, &objfile->objfile_obstack);
0114d602 9825 }
5c4e30ca 9826 }
9219021c 9827
639d11d3 9828 if (die->child != NULL)
d9fa45fe 9829 {
639d11d3 9830 struct die_info *child_die = die->child;
6e70227d 9831
d9fa45fe
DC
9832 while (child_die && child_die->tag)
9833 {
e7c27a73 9834 process_die (child_die, cu);
d9fa45fe
DC
9835 child_die = sibling_die (child_die);
9836 }
9837 }
38d518c9
EZ
9838}
9839
f55ee35c
JK
9840/* Read a Fortran module as type. This DIE can be only a declaration used for
9841 imported module. Still we need that type as local Fortran "use ... only"
9842 declaration imports depend on the created type in determine_prefix. */
9843
9844static struct type *
9845read_module_type (struct die_info *die, struct dwarf2_cu *cu)
9846{
9847 struct objfile *objfile = cu->objfile;
9848 char *module_name;
9849 struct type *type;
9850
9851 module_name = dwarf2_name (die, cu);
9852 if (!module_name)
3e43a32a
MS
9853 complaint (&symfile_complaints,
9854 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 9855 die->offset.sect_off);
f55ee35c
JK
9856 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
9857
9858 /* determine_prefix uses TYPE_TAG_NAME. */
9859 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9860
9861 return set_die_type (die, type, cu);
9862}
9863
5d7cb8df
JK
9864/* Read a Fortran module. */
9865
9866static void
9867read_module (struct die_info *die, struct dwarf2_cu *cu)
9868{
9869 struct die_info *child_die = die->child;
9870
5d7cb8df
JK
9871 while (child_die && child_die->tag)
9872 {
9873 process_die (child_die, cu);
9874 child_die = sibling_die (child_die);
9875 }
9876}
9877
38d518c9
EZ
9878/* Return the name of the namespace represented by DIE. Set
9879 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
9880 namespace. */
9881
9882static const char *
e142c38c 9883namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
9884{
9885 struct die_info *current_die;
9886 const char *name = NULL;
9887
9888 /* Loop through the extensions until we find a name. */
9889
9890 for (current_die = die;
9891 current_die != NULL;
f2f0e013 9892 current_die = dwarf2_extension (die, &cu))
38d518c9 9893 {
e142c38c 9894 name = dwarf2_name (current_die, cu);
38d518c9
EZ
9895 if (name != NULL)
9896 break;
9897 }
9898
9899 /* Is it an anonymous namespace? */
9900
9901 *is_anonymous = (name == NULL);
9902 if (*is_anonymous)
2b1dbab0 9903 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
9904
9905 return name;
d9fa45fe
DC
9906}
9907
c906108c
SS
9908/* Extract all information from a DW_TAG_pointer_type DIE and add to
9909 the user defined type vector. */
9910
f792889a 9911static struct type *
e7c27a73 9912read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9913{
5e2b427d 9914 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 9915 struct comp_unit_head *cu_header = &cu->header;
c906108c 9916 struct type *type;
8b2dbe47
KB
9917 struct attribute *attr_byte_size;
9918 struct attribute *attr_address_class;
9919 int byte_size, addr_class;
7e314c57
JK
9920 struct type *target_type;
9921
9922 target_type = die_type (die, cu);
c906108c 9923
7e314c57
JK
9924 /* The die_type call above may have already set the type for this DIE. */
9925 type = get_die_type (die, cu);
9926 if (type)
9927 return type;
9928
9929 type = lookup_pointer_type (target_type);
8b2dbe47 9930
e142c38c 9931 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
9932 if (attr_byte_size)
9933 byte_size = DW_UNSND (attr_byte_size);
c906108c 9934 else
8b2dbe47
KB
9935 byte_size = cu_header->addr_size;
9936
e142c38c 9937 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
9938 if (attr_address_class)
9939 addr_class = DW_UNSND (attr_address_class);
9940 else
9941 addr_class = DW_ADDR_none;
9942
9943 /* If the pointer size or address class is different than the
9944 default, create a type variant marked as such and set the
9945 length accordingly. */
9946 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 9947 {
5e2b427d 9948 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
9949 {
9950 int type_flags;
9951
849957d9 9952 type_flags = gdbarch_address_class_type_flags
5e2b427d 9953 (gdbarch, byte_size, addr_class);
876cecd0
TT
9954 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
9955 == 0);
8b2dbe47
KB
9956 type = make_type_with_address_space (type, type_flags);
9957 }
9958 else if (TYPE_LENGTH (type) != byte_size)
9959 {
3e43a32a
MS
9960 complaint (&symfile_complaints,
9961 _("invalid pointer size %d"), byte_size);
8b2dbe47 9962 }
6e70227d 9963 else
9a619af0
MS
9964 {
9965 /* Should we also complain about unhandled address classes? */
9966 }
c906108c 9967 }
8b2dbe47
KB
9968
9969 TYPE_LENGTH (type) = byte_size;
f792889a 9970 return set_die_type (die, type, cu);
c906108c
SS
9971}
9972
9973/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
9974 the user defined type vector. */
9975
f792889a 9976static struct type *
e7c27a73 9977read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
9978{
9979 struct type *type;
9980 struct type *to_type;
9981 struct type *domain;
9982
e7c27a73
DJ
9983 to_type = die_type (die, cu);
9984 domain = die_containing_type (die, cu);
0d5de010 9985
7e314c57
JK
9986 /* The calls above may have already set the type for this DIE. */
9987 type = get_die_type (die, cu);
9988 if (type)
9989 return type;
9990
0d5de010
DJ
9991 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
9992 type = lookup_methodptr_type (to_type);
9993 else
9994 type = lookup_memberptr_type (to_type, domain);
c906108c 9995
f792889a 9996 return set_die_type (die, type, cu);
c906108c
SS
9997}
9998
9999/* Extract all information from a DW_TAG_reference_type DIE and add to
10000 the user defined type vector. */
10001
f792889a 10002static struct type *
e7c27a73 10003read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10004{
e7c27a73 10005 struct comp_unit_head *cu_header = &cu->header;
7e314c57 10006 struct type *type, *target_type;
c906108c
SS
10007 struct attribute *attr;
10008
7e314c57
JK
10009 target_type = die_type (die, cu);
10010
10011 /* The die_type call above may have already set the type for this DIE. */
10012 type = get_die_type (die, cu);
10013 if (type)
10014 return type;
10015
10016 type = lookup_reference_type (target_type);
e142c38c 10017 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10018 if (attr)
10019 {
10020 TYPE_LENGTH (type) = DW_UNSND (attr);
10021 }
10022 else
10023 {
107d2387 10024 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 10025 }
f792889a 10026 return set_die_type (die, type, cu);
c906108c
SS
10027}
10028
f792889a 10029static struct type *
e7c27a73 10030read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10031{
f792889a 10032 struct type *base_type, *cv_type;
c906108c 10033
e7c27a73 10034 base_type = die_type (die, cu);
7e314c57
JK
10035
10036 /* The die_type call above may have already set the type for this DIE. */
10037 cv_type = get_die_type (die, cu);
10038 if (cv_type)
10039 return cv_type;
10040
2f608a3a
KW
10041 /* In case the const qualifier is applied to an array type, the element type
10042 is so qualified, not the array type (section 6.7.3 of C99). */
10043 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
10044 {
10045 struct type *el_type, *inner_array;
10046
10047 base_type = copy_type (base_type);
10048 inner_array = base_type;
10049
10050 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10051 {
10052 TYPE_TARGET_TYPE (inner_array) =
10053 copy_type (TYPE_TARGET_TYPE (inner_array));
10054 inner_array = TYPE_TARGET_TYPE (inner_array);
10055 }
10056
10057 el_type = TYPE_TARGET_TYPE (inner_array);
10058 TYPE_TARGET_TYPE (inner_array) =
10059 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10060
10061 return set_die_type (die, base_type, cu);
10062 }
10063
f792889a
DJ
10064 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10065 return set_die_type (die, cv_type, cu);
c906108c
SS
10066}
10067
f792889a 10068static struct type *
e7c27a73 10069read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10070{
f792889a 10071 struct type *base_type, *cv_type;
c906108c 10072
e7c27a73 10073 base_type = die_type (die, cu);
7e314c57
JK
10074
10075 /* The die_type call above may have already set the type for this DIE. */
10076 cv_type = get_die_type (die, cu);
10077 if (cv_type)
10078 return cv_type;
10079
f792889a
DJ
10080 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10081 return set_die_type (die, cv_type, cu);
c906108c
SS
10082}
10083
10084/* Extract all information from a DW_TAG_string_type DIE and add to
10085 the user defined type vector. It isn't really a user defined type,
10086 but it behaves like one, with other DIE's using an AT_user_def_type
10087 attribute to reference it. */
10088
f792889a 10089static struct type *
e7c27a73 10090read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10091{
e7c27a73 10092 struct objfile *objfile = cu->objfile;
3b7538c0 10093 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10094 struct type *type, *range_type, *index_type, *char_type;
10095 struct attribute *attr;
10096 unsigned int length;
10097
e142c38c 10098 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
10099 if (attr)
10100 {
10101 length = DW_UNSND (attr);
10102 }
10103 else
10104 {
0963b4bd 10105 /* Check for the DW_AT_byte_size attribute. */
e142c38c 10106 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
10107 if (attr)
10108 {
10109 length = DW_UNSND (attr);
10110 }
10111 else
10112 {
10113 length = 1;
10114 }
c906108c 10115 }
6ccb9162 10116
46bf5051 10117 index_type = objfile_type (objfile)->builtin_int;
c906108c 10118 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
10119 char_type = language_string_char_type (cu->language_defn, gdbarch);
10120 type = create_string_type (NULL, char_type, range_type);
6ccb9162 10121
f792889a 10122 return set_die_type (die, type, cu);
c906108c
SS
10123}
10124
10125/* Handle DIES due to C code like:
10126
10127 struct foo
c5aa993b
JM
10128 {
10129 int (*funcp)(int a, long l);
10130 int b;
10131 };
c906108c 10132
0963b4bd 10133 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 10134
f792889a 10135static struct type *
e7c27a73 10136read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10137{
bb5ed363 10138 struct objfile *objfile = cu->objfile;
0963b4bd
MS
10139 struct type *type; /* Type that this function returns. */
10140 struct type *ftype; /* Function that returns above type. */
c906108c
SS
10141 struct attribute *attr;
10142
e7c27a73 10143 type = die_type (die, cu);
7e314c57
JK
10144
10145 /* The die_type call above may have already set the type for this DIE. */
10146 ftype = get_die_type (die, cu);
10147 if (ftype)
10148 return ftype;
10149
0c8b41f1 10150 ftype = lookup_function_type (type);
c906108c 10151
5b8101ae 10152 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 10153 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 10154 if ((attr && (DW_UNSND (attr) != 0))
987504bb 10155 || cu->language == language_cplus
5b8101ae
PM
10156 || cu->language == language_java
10157 || cu->language == language_pascal)
876cecd0 10158 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
10159 else if (producer_is_realview (cu->producer))
10160 /* RealView does not emit DW_AT_prototyped. We can not
10161 distinguish prototyped and unprototyped functions; default to
10162 prototyped, since that is more common in modern code (and
10163 RealView warns about unprototyped functions). */
10164 TYPE_PROTOTYPED (ftype) = 1;
c906108c 10165
c055b101
CV
10166 /* Store the calling convention in the type if it's available in
10167 the subroutine die. Otherwise set the calling convention to
10168 the default value DW_CC_normal. */
10169 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
10170 if (attr)
10171 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10172 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10173 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10174 else
10175 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
10176
10177 /* We need to add the subroutine type to the die immediately so
10178 we don't infinitely recurse when dealing with parameters
0963b4bd 10179 declared as the same subroutine type. */
76c10ea2 10180 set_die_type (die, ftype, cu);
6e70227d 10181
639d11d3 10182 if (die->child != NULL)
c906108c 10183 {
bb5ed363 10184 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 10185 struct die_info *child_die;
8072405b 10186 int nparams, iparams;
c906108c
SS
10187
10188 /* Count the number of parameters.
10189 FIXME: GDB currently ignores vararg functions, but knows about
10190 vararg member functions. */
8072405b 10191 nparams = 0;
639d11d3 10192 child_die = die->child;
c906108c
SS
10193 while (child_die && child_die->tag)
10194 {
10195 if (child_die->tag == DW_TAG_formal_parameter)
10196 nparams++;
10197 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 10198 TYPE_VARARGS (ftype) = 1;
c906108c
SS
10199 child_die = sibling_die (child_die);
10200 }
10201
10202 /* Allocate storage for parameters and fill them in. */
10203 TYPE_NFIELDS (ftype) = nparams;
10204 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 10205 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 10206
8072405b
JK
10207 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10208 even if we error out during the parameters reading below. */
10209 for (iparams = 0; iparams < nparams; iparams++)
10210 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10211
10212 iparams = 0;
639d11d3 10213 child_die = die->child;
c906108c
SS
10214 while (child_die && child_die->tag)
10215 {
10216 if (child_die->tag == DW_TAG_formal_parameter)
10217 {
3ce3b1ba
PA
10218 struct type *arg_type;
10219
10220 /* DWARF version 2 has no clean way to discern C++
10221 static and non-static member functions. G++ helps
10222 GDB by marking the first parameter for non-static
10223 member functions (which is the this pointer) as
10224 artificial. We pass this information to
10225 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10226
10227 DWARF version 3 added DW_AT_object_pointer, which GCC
10228 4.5 does not yet generate. */
e142c38c 10229 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
10230 if (attr)
10231 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10232 else
418835cc
KS
10233 {
10234 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10235
10236 /* GCC/43521: In java, the formal parameter
10237 "this" is sometimes not marked with DW_AT_artificial. */
10238 if (cu->language == language_java)
10239 {
10240 const char *name = dwarf2_name (child_die, cu);
9a619af0 10241
418835cc
KS
10242 if (name && !strcmp (name, "this"))
10243 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10244 }
10245 }
3ce3b1ba
PA
10246 arg_type = die_type (child_die, cu);
10247
10248 /* RealView does not mark THIS as const, which the testsuite
10249 expects. GCC marks THIS as const in method definitions,
10250 but not in the class specifications (GCC PR 43053). */
10251 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10252 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10253 {
10254 int is_this = 0;
10255 struct dwarf2_cu *arg_cu = cu;
10256 const char *name = dwarf2_name (child_die, cu);
10257
10258 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10259 if (attr)
10260 {
10261 /* If the compiler emits this, use it. */
10262 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10263 is_this = 1;
10264 }
10265 else if (name && strcmp (name, "this") == 0)
10266 /* Function definitions will have the argument names. */
10267 is_this = 1;
10268 else if (name == NULL && iparams == 0)
10269 /* Declarations may not have the names, so like
10270 elsewhere in GDB, assume an artificial first
10271 argument is "this". */
10272 is_this = 1;
10273
10274 if (is_this)
10275 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10276 arg_type, 0);
10277 }
10278
10279 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
10280 iparams++;
10281 }
10282 child_die = sibling_die (child_die);
10283 }
10284 }
10285
76c10ea2 10286 return ftype;
c906108c
SS
10287}
10288
f792889a 10289static struct type *
e7c27a73 10290read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10291{
e7c27a73 10292 struct objfile *objfile = cu->objfile;
0114d602 10293 const char *name = NULL;
3c8e0968 10294 struct type *this_type, *target_type;
c906108c 10295
94af9270 10296 name = dwarf2_full_name (NULL, die, cu);
f792889a 10297 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
10298 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10299 TYPE_NAME (this_type) = (char *) name;
f792889a 10300 set_die_type (die, this_type, cu);
3c8e0968
DE
10301 target_type = die_type (die, cu);
10302 if (target_type != this_type)
10303 TYPE_TARGET_TYPE (this_type) = target_type;
10304 else
10305 {
10306 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10307 spec and cause infinite loops in GDB. */
10308 complaint (&symfile_complaints,
10309 _("Self-referential DW_TAG_typedef "
10310 "- DIE at 0x%x [in module %s]"),
b64f50a1 10311 die->offset.sect_off, objfile->name);
3c8e0968
DE
10312 TYPE_TARGET_TYPE (this_type) = NULL;
10313 }
f792889a 10314 return this_type;
c906108c
SS
10315}
10316
10317/* Find a representation of a given base type and install
10318 it in the TYPE field of the die. */
10319
f792889a 10320static struct type *
e7c27a73 10321read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10322{
e7c27a73 10323 struct objfile *objfile = cu->objfile;
c906108c
SS
10324 struct type *type;
10325 struct attribute *attr;
10326 int encoding = 0, size = 0;
39cbfefa 10327 char *name;
6ccb9162
UW
10328 enum type_code code = TYPE_CODE_INT;
10329 int type_flags = 0;
10330 struct type *target_type = NULL;
c906108c 10331
e142c38c 10332 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
10333 if (attr)
10334 {
10335 encoding = DW_UNSND (attr);
10336 }
e142c38c 10337 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10338 if (attr)
10339 {
10340 size = DW_UNSND (attr);
10341 }
39cbfefa 10342 name = dwarf2_name (die, cu);
6ccb9162 10343 if (!name)
c906108c 10344 {
6ccb9162
UW
10345 complaint (&symfile_complaints,
10346 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 10347 }
6ccb9162
UW
10348
10349 switch (encoding)
c906108c 10350 {
6ccb9162
UW
10351 case DW_ATE_address:
10352 /* Turn DW_ATE_address into a void * pointer. */
10353 code = TYPE_CODE_PTR;
10354 type_flags |= TYPE_FLAG_UNSIGNED;
10355 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10356 break;
10357 case DW_ATE_boolean:
10358 code = TYPE_CODE_BOOL;
10359 type_flags |= TYPE_FLAG_UNSIGNED;
10360 break;
10361 case DW_ATE_complex_float:
10362 code = TYPE_CODE_COMPLEX;
10363 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10364 break;
10365 case DW_ATE_decimal_float:
10366 code = TYPE_CODE_DECFLOAT;
10367 break;
10368 case DW_ATE_float:
10369 code = TYPE_CODE_FLT;
10370 break;
10371 case DW_ATE_signed:
10372 break;
10373 case DW_ATE_unsigned:
10374 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
10375 if (cu->language == language_fortran
10376 && name
10377 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10378 code = TYPE_CODE_CHAR;
6ccb9162
UW
10379 break;
10380 case DW_ATE_signed_char:
6e70227d 10381 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10382 || cu->language == language_pascal
10383 || cu->language == language_fortran)
6ccb9162
UW
10384 code = TYPE_CODE_CHAR;
10385 break;
10386 case DW_ATE_unsigned_char:
868a0084 10387 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10388 || cu->language == language_pascal
10389 || cu->language == language_fortran)
6ccb9162
UW
10390 code = TYPE_CODE_CHAR;
10391 type_flags |= TYPE_FLAG_UNSIGNED;
10392 break;
75079b2b
TT
10393 case DW_ATE_UTF:
10394 /* We just treat this as an integer and then recognize the
10395 type by name elsewhere. */
10396 break;
10397
6ccb9162
UW
10398 default:
10399 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10400 dwarf_type_encoding_name (encoding));
10401 break;
c906108c 10402 }
6ccb9162 10403
0114d602
DJ
10404 type = init_type (code, size, type_flags, NULL, objfile);
10405 TYPE_NAME (type) = name;
6ccb9162
UW
10406 TYPE_TARGET_TYPE (type) = target_type;
10407
0114d602 10408 if (name && strcmp (name, "char") == 0)
876cecd0 10409 TYPE_NOSIGN (type) = 1;
0114d602 10410
f792889a 10411 return set_die_type (die, type, cu);
c906108c
SS
10412}
10413
a02abb62
JB
10414/* Read the given DW_AT_subrange DIE. */
10415
f792889a 10416static struct type *
a02abb62
JB
10417read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10418{
10419 struct type *base_type;
10420 struct type *range_type;
10421 struct attribute *attr;
4fae6e18
JK
10422 LONGEST low, high;
10423 int low_default_is_valid;
39cbfefa 10424 char *name;
43bbcdc2 10425 LONGEST negative_mask;
e77813c8 10426
a02abb62 10427 base_type = die_type (die, cu);
953ac07e
JK
10428 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10429 check_typedef (base_type);
a02abb62 10430
7e314c57
JK
10431 /* The die_type call above may have already set the type for this DIE. */
10432 range_type = get_die_type (die, cu);
10433 if (range_type)
10434 return range_type;
10435
4fae6e18
JK
10436 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10437 omitting DW_AT_lower_bound. */
10438 switch (cu->language)
6e70227d 10439 {
4fae6e18
JK
10440 case language_c:
10441 case language_cplus:
10442 low = 0;
10443 low_default_is_valid = 1;
10444 break;
10445 case language_fortran:
10446 low = 1;
10447 low_default_is_valid = 1;
10448 break;
10449 case language_d:
10450 case language_java:
10451 case language_objc:
10452 low = 0;
10453 low_default_is_valid = (cu->header.version >= 4);
10454 break;
10455 case language_ada:
10456 case language_m2:
10457 case language_pascal:
a02abb62 10458 low = 1;
4fae6e18
JK
10459 low_default_is_valid = (cu->header.version >= 4);
10460 break;
10461 default:
10462 low = 0;
10463 low_default_is_valid = 0;
10464 break;
a02abb62
JB
10465 }
10466
dd5e6932
DJ
10467 /* FIXME: For variable sized arrays either of these could be
10468 a variable rather than a constant value. We'll allow it,
10469 but we don't know how to handle it. */
e142c38c 10470 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 10471 if (attr)
4fae6e18
JK
10472 low = dwarf2_get_attr_constant_value (attr, low);
10473 else if (!low_default_is_valid)
10474 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10475 "- DIE at 0x%x [in module %s]"),
10476 die->offset.sect_off, cu->objfile->name);
a02abb62 10477
e142c38c 10478 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 10479 if (attr)
6e70227d 10480 {
d48323d8 10481 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
10482 {
10483 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 10484 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
10485 FIXME: GDB does not yet know how to handle dynamic
10486 arrays properly, treat them as arrays with unspecified
10487 length for now.
10488
10489 FIXME: jimb/2003-09-22: GDB does not really know
10490 how to handle arrays of unspecified length
10491 either; we just represent them as zero-length
10492 arrays. Choose an appropriate upper bound given
10493 the lower bound we've computed above. */
10494 high = low - 1;
10495 }
10496 else
10497 high = dwarf2_get_attr_constant_value (attr, 1);
10498 }
e77813c8
PM
10499 else
10500 {
10501 attr = dwarf2_attr (die, DW_AT_count, cu);
10502 if (attr)
10503 {
10504 int count = dwarf2_get_attr_constant_value (attr, 1);
10505 high = low + count - 1;
10506 }
c2ff108b
JK
10507 else
10508 {
10509 /* Unspecified array length. */
10510 high = low - 1;
10511 }
e77813c8
PM
10512 }
10513
10514 /* Dwarf-2 specifications explicitly allows to create subrange types
10515 without specifying a base type.
10516 In that case, the base type must be set to the type of
10517 the lower bound, upper bound or count, in that order, if any of these
10518 three attributes references an object that has a type.
10519 If no base type is found, the Dwarf-2 specifications say that
10520 a signed integer type of size equal to the size of an address should
10521 be used.
10522 For the following C code: `extern char gdb_int [];'
10523 GCC produces an empty range DIE.
10524 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 10525 high bound or count are not yet handled by this code. */
e77813c8
PM
10526 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10527 {
10528 struct objfile *objfile = cu->objfile;
10529 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10530 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10531 struct type *int_type = objfile_type (objfile)->builtin_int;
10532
10533 /* Test "int", "long int", and "long long int" objfile types,
10534 and select the first one having a size above or equal to the
10535 architecture address size. */
10536 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10537 base_type = int_type;
10538 else
10539 {
10540 int_type = objfile_type (objfile)->builtin_long;
10541 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10542 base_type = int_type;
10543 else
10544 {
10545 int_type = objfile_type (objfile)->builtin_long_long;
10546 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10547 base_type = int_type;
10548 }
10549 }
10550 }
a02abb62 10551
6e70227d 10552 negative_mask =
43bbcdc2
PH
10553 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10554 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10555 low |= negative_mask;
10556 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10557 high |= negative_mask;
10558
a02abb62
JB
10559 range_type = create_range_type (NULL, base_type, low, high);
10560
bbb0eef6
JK
10561 /* Mark arrays with dynamic length at least as an array of unspecified
10562 length. GDB could check the boundary but before it gets implemented at
10563 least allow accessing the array elements. */
d48323d8 10564 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
10565 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10566
c2ff108b
JK
10567 /* Ada expects an empty array on no boundary attributes. */
10568 if (attr == NULL && cu->language != language_ada)
10569 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10570
39cbfefa
DJ
10571 name = dwarf2_name (die, cu);
10572 if (name)
10573 TYPE_NAME (range_type) = name;
6e70227d 10574
e142c38c 10575 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
10576 if (attr)
10577 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10578
7e314c57
JK
10579 set_die_type (die, range_type, cu);
10580
10581 /* set_die_type should be already done. */
b4ba55a1
JB
10582 set_descriptive_type (range_type, die, cu);
10583
7e314c57 10584 return range_type;
a02abb62 10585}
6e70227d 10586
f792889a 10587static struct type *
81a17f79
JB
10588read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10589{
10590 struct type *type;
81a17f79 10591
81a17f79
JB
10592 /* For now, we only support the C meaning of an unspecified type: void. */
10593
0114d602
DJ
10594 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10595 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 10596
f792889a 10597 return set_die_type (die, type, cu);
81a17f79 10598}
a02abb62 10599
639d11d3
DC
10600/* Read a single die and all its descendents. Set the die's sibling
10601 field to NULL; set other fields in the die correctly, and set all
10602 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10603 location of the info_ptr after reading all of those dies. PARENT
10604 is the parent of the die in question. */
10605
10606static struct die_info *
dee91e82
DE
10607read_die_and_children (const struct die_reader_specs *reader,
10608 gdb_byte *info_ptr,
10609 gdb_byte **new_info_ptr,
10610 struct die_info *parent)
639d11d3
DC
10611{
10612 struct die_info *die;
fe1b8b76 10613 gdb_byte *cur_ptr;
639d11d3
DC
10614 int has_children;
10615
93311388 10616 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
10617 if (die == NULL)
10618 {
10619 *new_info_ptr = cur_ptr;
10620 return NULL;
10621 }
93311388 10622 store_in_ref_table (die, reader->cu);
639d11d3
DC
10623
10624 if (has_children)
348e048f 10625 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
10626 else
10627 {
10628 die->child = NULL;
10629 *new_info_ptr = cur_ptr;
10630 }
10631
10632 die->sibling = NULL;
10633 die->parent = parent;
10634 return die;
10635}
10636
10637/* Read a die, all of its descendents, and all of its siblings; set
10638 all of the fields of all of the dies correctly. Arguments are as
10639 in read_die_and_children. */
10640
10641static struct die_info *
93311388
DE
10642read_die_and_siblings (const struct die_reader_specs *reader,
10643 gdb_byte *info_ptr,
fe1b8b76 10644 gdb_byte **new_info_ptr,
639d11d3
DC
10645 struct die_info *parent)
10646{
10647 struct die_info *first_die, *last_sibling;
fe1b8b76 10648 gdb_byte *cur_ptr;
639d11d3 10649
c906108c 10650 cur_ptr = info_ptr;
639d11d3
DC
10651 first_die = last_sibling = NULL;
10652
10653 while (1)
c906108c 10654 {
639d11d3 10655 struct die_info *die
dee91e82 10656 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 10657
1d325ec1 10658 if (die == NULL)
c906108c 10659 {
639d11d3
DC
10660 *new_info_ptr = cur_ptr;
10661 return first_die;
c906108c 10662 }
1d325ec1
DJ
10663
10664 if (!first_die)
10665 first_die = die;
c906108c 10666 else
1d325ec1
DJ
10667 last_sibling->sibling = die;
10668
10669 last_sibling = die;
c906108c 10670 }
c906108c
SS
10671}
10672
3019eac3
DE
10673/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10674 attributes.
10675 The caller is responsible for filling in the extra attributes
10676 and updating (*DIEP)->num_attrs.
10677 Set DIEP to point to a newly allocated die with its information,
10678 except for its child, sibling, and parent fields.
10679 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
10680
10681static gdb_byte *
3019eac3
DE
10682read_full_die_1 (const struct die_reader_specs *reader,
10683 struct die_info **diep, gdb_byte *info_ptr,
10684 int *has_children, int num_extra_attrs)
93311388 10685{
b64f50a1
JK
10686 unsigned int abbrev_number, bytes_read, i;
10687 sect_offset offset;
93311388
DE
10688 struct abbrev_info *abbrev;
10689 struct die_info *die;
10690 struct dwarf2_cu *cu = reader->cu;
10691 bfd *abfd = reader->abfd;
10692
b64f50a1 10693 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
10694 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10695 info_ptr += bytes_read;
10696 if (!abbrev_number)
10697 {
10698 *diep = NULL;
10699 *has_children = 0;
10700 return info_ptr;
10701 }
10702
10703 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10704 if (!abbrev)
348e048f
DE
10705 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10706 abbrev_number,
10707 bfd_get_filename (abfd));
10708
3019eac3 10709 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
10710 die->offset = offset;
10711 die->tag = abbrev->tag;
10712 die->abbrev = abbrev_number;
10713
3019eac3
DE
10714 /* Make the result usable.
10715 The caller needs to update num_attrs after adding the extra
10716 attributes. */
93311388
DE
10717 die->num_attrs = abbrev->num_attrs;
10718
10719 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
10720 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10721 info_ptr);
93311388
DE
10722
10723 *diep = die;
10724 *has_children = abbrev->has_children;
10725 return info_ptr;
10726}
10727
3019eac3
DE
10728/* Read a die and all its attributes.
10729 Set DIEP to point to a newly allocated die with its information,
10730 except for its child, sibling, and parent fields.
10731 Set HAS_CHILDREN to tell whether the die has children or not. */
10732
10733static gdb_byte *
10734read_full_die (const struct die_reader_specs *reader,
10735 struct die_info **diep, gdb_byte *info_ptr,
10736 int *has_children)
10737{
10738 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10739}
10740
c906108c
SS
10741/* In DWARF version 2, the description of the debugging information is
10742 stored in a separate .debug_abbrev section. Before we read any
10743 dies from a section we read in all abbreviations and install them
72bf9492
DJ
10744 in a hash table. This function also sets flags in CU describing
10745 the data found in the abbrev table. */
c906108c
SS
10746
10747static void
dee91e82
DE
10748dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10749 struct dwarf2_section_info *abbrev_section)
10750
c906108c 10751{
dee91e82 10752 bfd *abfd = abbrev_section->asection->owner;
e7c27a73 10753 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 10754 gdb_byte *abbrev_ptr;
c906108c
SS
10755 struct abbrev_info *cur_abbrev;
10756 unsigned int abbrev_number, bytes_read, abbrev_name;
10757 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
10758 struct attr_abbrev *cur_attrs;
10759 unsigned int allocated_attrs;
c906108c 10760
0963b4bd 10761 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
10762 obstack_init (&cu->abbrev_obstack);
10763 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10764 (ABBREV_HASH_SIZE
10765 * sizeof (struct abbrev_info *)));
10766 memset (cu->dwarf2_abbrevs, 0,
10767 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 10768
dee91e82
DE
10769 dwarf2_read_section (cu->objfile, abbrev_section);
10770 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
c906108c
SS
10771 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10772 abbrev_ptr += bytes_read;
10773
f3dd6933
DJ
10774 allocated_attrs = ATTR_ALLOC_CHUNK;
10775 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 10776
0963b4bd 10777 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
10778 while (abbrev_number)
10779 {
f3dd6933 10780 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
10781
10782 /* read in abbrev header */
10783 cur_abbrev->number = abbrev_number;
10784 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10785 abbrev_ptr += bytes_read;
10786 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
10787 abbrev_ptr += 1;
10788
10789 /* now read in declarations */
10790 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10791 abbrev_ptr += bytes_read;
10792 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10793 abbrev_ptr += bytes_read;
10794 while (abbrev_name)
10795 {
f3dd6933 10796 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 10797 {
f3dd6933
DJ
10798 allocated_attrs += ATTR_ALLOC_CHUNK;
10799 cur_attrs
10800 = xrealloc (cur_attrs, (allocated_attrs
10801 * sizeof (struct attr_abbrev)));
c906108c 10802 }
ae038cb0 10803
f3dd6933
DJ
10804 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
10805 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
10806 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10807 abbrev_ptr += bytes_read;
10808 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10809 abbrev_ptr += bytes_read;
10810 }
10811
f3dd6933
DJ
10812 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
10813 (cur_abbrev->num_attrs
10814 * sizeof (struct attr_abbrev)));
10815 memcpy (cur_abbrev->attrs, cur_attrs,
10816 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
10817
c906108c 10818 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
10819 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
10820 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
10821
10822 /* Get next abbreviation.
10823 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
10824 always properly terminated with an abbrev number of 0.
10825 Exit loop if we encounter an abbreviation which we have
10826 already read (which means we are about to read the abbreviations
10827 for the next compile unit) or if the end of the abbreviation
10828 table is reached. */
dee91e82
DE
10829 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
10830 >= abbrev_section->size)
c906108c
SS
10831 break;
10832 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10833 abbrev_ptr += bytes_read;
e7c27a73 10834 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
10835 break;
10836 }
f3dd6933
DJ
10837
10838 xfree (cur_attrs);
c906108c
SS
10839}
10840
f3dd6933 10841/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 10842
c906108c 10843static void
f3dd6933 10844dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 10845{
f3dd6933 10846 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 10847
f3dd6933
DJ
10848 obstack_free (&cu->abbrev_obstack, NULL);
10849 cu->dwarf2_abbrevs = NULL;
c906108c
SS
10850}
10851
10852/* Lookup an abbrev_info structure in the abbrev hash table. */
10853
10854static struct abbrev_info *
e7c27a73 10855dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
10856{
10857 unsigned int hash_number;
10858 struct abbrev_info *abbrev;
10859
10860 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 10861 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
10862
10863 while (abbrev)
10864 {
10865 if (abbrev->number == number)
10866 return abbrev;
10867 else
10868 abbrev = abbrev->next;
10869 }
10870 return NULL;
10871}
10872
72bf9492
DJ
10873/* Returns nonzero if TAG represents a type that we might generate a partial
10874 symbol for. */
10875
10876static int
10877is_type_tag_for_partial (int tag)
10878{
10879 switch (tag)
10880 {
10881#if 0
10882 /* Some types that would be reasonable to generate partial symbols for,
10883 that we don't at present. */
10884 case DW_TAG_array_type:
10885 case DW_TAG_file_type:
10886 case DW_TAG_ptr_to_member_type:
10887 case DW_TAG_set_type:
10888 case DW_TAG_string_type:
10889 case DW_TAG_subroutine_type:
10890#endif
10891 case DW_TAG_base_type:
10892 case DW_TAG_class_type:
680b30c7 10893 case DW_TAG_interface_type:
72bf9492
DJ
10894 case DW_TAG_enumeration_type:
10895 case DW_TAG_structure_type:
10896 case DW_TAG_subrange_type:
10897 case DW_TAG_typedef:
10898 case DW_TAG_union_type:
10899 return 1;
10900 default:
10901 return 0;
10902 }
10903}
10904
10905/* Load all DIEs that are interesting for partial symbols into memory. */
10906
10907static struct partial_die_info *
dee91e82
DE
10908load_partial_dies (const struct die_reader_specs *reader,
10909 gdb_byte *info_ptr, int building_psymtab)
72bf9492 10910{
dee91e82 10911 struct dwarf2_cu *cu = reader->cu;
bb5ed363 10912 struct objfile *objfile = cu->objfile;
72bf9492
DJ
10913 struct partial_die_info *part_die;
10914 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
10915 struct abbrev_info *abbrev;
10916 unsigned int bytes_read;
5afb4e99 10917 unsigned int load_all = 0;
72bf9492
DJ
10918 int nesting_level = 1;
10919
10920 parent_die = NULL;
10921 last_die = NULL;
10922
7adf1e79
DE
10923 gdb_assert (cu->per_cu != NULL);
10924 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
10925 load_all = 1;
10926
72bf9492
DJ
10927 cu->partial_dies
10928 = htab_create_alloc_ex (cu->header.length / 12,
10929 partial_die_hash,
10930 partial_die_eq,
10931 NULL,
10932 &cu->comp_unit_obstack,
10933 hashtab_obstack_allocate,
10934 dummy_obstack_deallocate);
10935
10936 part_die = obstack_alloc (&cu->comp_unit_obstack,
10937 sizeof (struct partial_die_info));
10938
10939 while (1)
10940 {
10941 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
10942
10943 /* A NULL abbrev means the end of a series of children. */
10944 if (abbrev == NULL)
10945 {
10946 if (--nesting_level == 0)
10947 {
10948 /* PART_DIE was probably the last thing allocated on the
10949 comp_unit_obstack, so we could call obstack_free
10950 here. We don't do that because the waste is small,
10951 and will be cleaned up when we're done with this
10952 compilation unit. This way, we're also more robust
10953 against other users of the comp_unit_obstack. */
10954 return first_die;
10955 }
10956 info_ptr += bytes_read;
10957 last_die = parent_die;
10958 parent_die = parent_die->die_parent;
10959 continue;
10960 }
10961
98bfdba5
PA
10962 /* Check for template arguments. We never save these; if
10963 they're seen, we just mark the parent, and go on our way. */
10964 if (parent_die != NULL
10965 && cu->language == language_cplus
10966 && (abbrev->tag == DW_TAG_template_type_param
10967 || abbrev->tag == DW_TAG_template_value_param))
10968 {
10969 parent_die->has_template_arguments = 1;
10970
10971 if (!load_all)
10972 {
10973 /* We don't need a partial DIE for the template argument. */
dee91e82 10974 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
10975 continue;
10976 }
10977 }
10978
0d99eb77 10979 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
10980 Skip their other children. */
10981 if (!load_all
10982 && cu->language == language_cplus
10983 && parent_die != NULL
10984 && parent_die->tag == DW_TAG_subprogram)
10985 {
dee91e82 10986 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
10987 continue;
10988 }
10989
5afb4e99
DJ
10990 /* Check whether this DIE is interesting enough to save. Normally
10991 we would not be interested in members here, but there may be
10992 later variables referencing them via DW_AT_specification (for
10993 static members). */
10994 if (!load_all
10995 && !is_type_tag_for_partial (abbrev->tag)
72929c62 10996 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
10997 && abbrev->tag != DW_TAG_enumerator
10998 && abbrev->tag != DW_TAG_subprogram
bc30ff58 10999 && abbrev->tag != DW_TAG_lexical_block
72bf9492 11000 && abbrev->tag != DW_TAG_variable
5afb4e99 11001 && abbrev->tag != DW_TAG_namespace
f55ee35c 11002 && abbrev->tag != DW_TAG_module
95554aad
TT
11003 && abbrev->tag != DW_TAG_member
11004 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
11005 {
11006 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11007 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
11008 continue;
11009 }
11010
dee91e82
DE
11011 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
11012 info_ptr);
72bf9492
DJ
11013
11014 /* This two-pass algorithm for processing partial symbols has a
11015 high cost in cache pressure. Thus, handle some simple cases
11016 here which cover the majority of C partial symbols. DIEs
11017 which neither have specification tags in them, nor could have
11018 specification tags elsewhere pointing at them, can simply be
11019 processed and discarded.
11020
11021 This segment is also optional; scan_partial_symbols and
11022 add_partial_symbol will handle these DIEs if we chain
11023 them in normally. When compilers which do not emit large
11024 quantities of duplicate debug information are more common,
11025 this code can probably be removed. */
11026
11027 /* Any complete simple types at the top level (pretty much all
11028 of them, for a language without namespaces), can be processed
11029 directly. */
11030 if (parent_die == NULL
11031 && part_die->has_specification == 0
11032 && part_die->is_declaration == 0
d8228535 11033 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
11034 || part_die->tag == DW_TAG_base_type
11035 || part_die->tag == DW_TAG_subrange_type))
11036 {
11037 if (building_psymtab && part_die->name != NULL)
04a679b8 11038 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11039 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
11040 &objfile->static_psymbols,
11041 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 11042 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11043 continue;
11044 }
11045
d8228535
JK
11046 /* The exception for DW_TAG_typedef with has_children above is
11047 a workaround of GCC PR debug/47510. In the case of this complaint
11048 type_name_no_tag_or_error will error on such types later.
11049
11050 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11051 it could not find the child DIEs referenced later, this is checked
11052 above. In correct DWARF DW_TAG_typedef should have no children. */
11053
11054 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11055 complaint (&symfile_complaints,
11056 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11057 "- DIE at 0x%x [in module %s]"),
b64f50a1 11058 part_die->offset.sect_off, objfile->name);
d8228535 11059
72bf9492
DJ
11060 /* If we're at the second level, and we're an enumerator, and
11061 our parent has no specification (meaning possibly lives in a
11062 namespace elsewhere), then we can add the partial symbol now
11063 instead of queueing it. */
11064 if (part_die->tag == DW_TAG_enumerator
11065 && parent_die != NULL
11066 && parent_die->die_parent == NULL
11067 && parent_die->tag == DW_TAG_enumeration_type
11068 && parent_die->has_specification == 0)
11069 {
11070 if (part_die->name == NULL)
3e43a32a
MS
11071 complaint (&symfile_complaints,
11072 _("malformed enumerator DIE ignored"));
72bf9492 11073 else if (building_psymtab)
04a679b8 11074 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11075 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
11076 (cu->language == language_cplus
11077 || cu->language == language_java)
bb5ed363
DE
11078 ? &objfile->global_psymbols
11079 : &objfile->static_psymbols,
11080 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 11081
dee91e82 11082 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11083 continue;
11084 }
11085
11086 /* We'll save this DIE so link it in. */
11087 part_die->die_parent = parent_die;
11088 part_die->die_sibling = NULL;
11089 part_die->die_child = NULL;
11090
11091 if (last_die && last_die == parent_die)
11092 last_die->die_child = part_die;
11093 else if (last_die)
11094 last_die->die_sibling = part_die;
11095
11096 last_die = part_die;
11097
11098 if (first_die == NULL)
11099 first_die = part_die;
11100
11101 /* Maybe add the DIE to the hash table. Not all DIEs that we
11102 find interesting need to be in the hash table, because we
11103 also have the parent/sibling/child chains; only those that we
11104 might refer to by offset later during partial symbol reading.
11105
11106 For now this means things that might have be the target of a
11107 DW_AT_specification, DW_AT_abstract_origin, or
11108 DW_AT_extension. DW_AT_extension will refer only to
11109 namespaces; DW_AT_abstract_origin refers to functions (and
11110 many things under the function DIE, but we do not recurse
11111 into function DIEs during partial symbol reading) and
11112 possibly variables as well; DW_AT_specification refers to
11113 declarations. Declarations ought to have the DW_AT_declaration
11114 flag. It happens that GCC forgets to put it in sometimes, but
11115 only for functions, not for types.
11116
11117 Adding more things than necessary to the hash table is harmless
11118 except for the performance cost. Adding too few will result in
5afb4e99
DJ
11119 wasted time in find_partial_die, when we reread the compilation
11120 unit with load_all_dies set. */
72bf9492 11121
5afb4e99 11122 if (load_all
72929c62 11123 || abbrev->tag == DW_TAG_constant
5afb4e99 11124 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
11125 || abbrev->tag == DW_TAG_variable
11126 || abbrev->tag == DW_TAG_namespace
11127 || part_die->is_declaration)
11128 {
11129 void **slot;
11130
11131 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 11132 part_die->offset.sect_off, INSERT);
72bf9492
DJ
11133 *slot = part_die;
11134 }
11135
11136 part_die = obstack_alloc (&cu->comp_unit_obstack,
11137 sizeof (struct partial_die_info));
11138
11139 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 11140 we have no reason to follow the children of structures; for other
98bfdba5
PA
11141 languages we have to, so that we can get at method physnames
11142 to infer fully qualified class names, for DW_AT_specification,
11143 and for C++ template arguments. For C++, we also look one level
11144 inside functions to find template arguments (if the name of the
11145 function does not already contain the template arguments).
bc30ff58
JB
11146
11147 For Ada, we need to scan the children of subprograms and lexical
11148 blocks as well because Ada allows the definition of nested
11149 entities that could be interesting for the debugger, such as
11150 nested subprograms for instance. */
72bf9492 11151 if (last_die->has_children
5afb4e99
DJ
11152 && (load_all
11153 || last_die->tag == DW_TAG_namespace
f55ee35c 11154 || last_die->tag == DW_TAG_module
72bf9492 11155 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
11156 || (cu->language == language_cplus
11157 && last_die->tag == DW_TAG_subprogram
11158 && (last_die->name == NULL
11159 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
11160 || (cu->language != language_c
11161 && (last_die->tag == DW_TAG_class_type
680b30c7 11162 || last_die->tag == DW_TAG_interface_type
72bf9492 11163 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
11164 || last_die->tag == DW_TAG_union_type))
11165 || (cu->language == language_ada
11166 && (last_die->tag == DW_TAG_subprogram
11167 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
11168 {
11169 nesting_level++;
11170 parent_die = last_die;
11171 continue;
11172 }
11173
11174 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11175 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
11176
11177 /* Back to the top, do it again. */
11178 }
11179}
11180
c906108c
SS
11181/* Read a minimal amount of information into the minimal die structure. */
11182
fe1b8b76 11183static gdb_byte *
dee91e82
DE
11184read_partial_die (const struct die_reader_specs *reader,
11185 struct partial_die_info *part_die,
11186 struct abbrev_info *abbrev, unsigned int abbrev_len,
11187 gdb_byte *info_ptr)
c906108c 11188{
dee91e82 11189 struct dwarf2_cu *cu = reader->cu;
bb5ed363 11190 struct objfile *objfile = cu->objfile;
dee91e82 11191 gdb_byte *buffer = reader->buffer;
fa238c03 11192 unsigned int i;
c906108c 11193 struct attribute attr;
c5aa993b 11194 int has_low_pc_attr = 0;
c906108c 11195 int has_high_pc_attr = 0;
91da1414 11196 int high_pc_relative = 0;
c906108c 11197
72bf9492 11198 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 11199
b64f50a1 11200 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
11201
11202 info_ptr += abbrev_len;
11203
11204 if (abbrev == NULL)
11205 return info_ptr;
11206
c906108c
SS
11207 part_die->tag = abbrev->tag;
11208 part_die->has_children = abbrev->has_children;
c906108c
SS
11209
11210 for (i = 0; i < abbrev->num_attrs; ++i)
11211 {
dee91e82 11212 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
11213
11214 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 11215 partial symbol table. */
c906108c
SS
11216 switch (attr.name)
11217 {
11218 case DW_AT_name:
71c25dea
TT
11219 switch (part_die->tag)
11220 {
11221 case DW_TAG_compile_unit:
95554aad 11222 case DW_TAG_partial_unit:
348e048f 11223 case DW_TAG_type_unit:
71c25dea
TT
11224 /* Compilation units have a DW_AT_name that is a filename, not
11225 a source language identifier. */
11226 case DW_TAG_enumeration_type:
11227 case DW_TAG_enumerator:
11228 /* These tags always have simple identifiers already; no need
11229 to canonicalize them. */
11230 part_die->name = DW_STRING (&attr);
11231 break;
11232 default:
11233 part_die->name
11234 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 11235 &objfile->objfile_obstack);
71c25dea
TT
11236 break;
11237 }
c906108c 11238 break;
31ef98ae 11239 case DW_AT_linkage_name:
c906108c 11240 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
11241 /* Note that both forms of linkage name might appear. We
11242 assume they will be the same, and we only store the last
11243 one we see. */
94af9270
KS
11244 if (cu->language == language_ada)
11245 part_die->name = DW_STRING (&attr);
abc72ce4 11246 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
11247 break;
11248 case DW_AT_low_pc:
11249 has_low_pc_attr = 1;
11250 part_die->lowpc = DW_ADDR (&attr);
11251 break;
11252 case DW_AT_high_pc:
11253 has_high_pc_attr = 1;
3019eac3
DE
11254 if (attr.form == DW_FORM_addr
11255 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
11256 part_die->highpc = DW_ADDR (&attr);
11257 else
11258 {
11259 high_pc_relative = 1;
11260 part_die->highpc = DW_UNSND (&attr);
11261 }
c906108c
SS
11262 break;
11263 case DW_AT_location:
0963b4bd 11264 /* Support the .debug_loc offsets. */
8e19ed76
PS
11265 if (attr_form_is_block (&attr))
11266 {
95554aad 11267 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 11268 }
3690dd37 11269 else if (attr_form_is_section_offset (&attr))
8e19ed76 11270 {
4d3c2250 11271 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11272 }
11273 else
11274 {
4d3c2250
KB
11275 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11276 "partial symbol information");
8e19ed76 11277 }
c906108c 11278 break;
c906108c
SS
11279 case DW_AT_external:
11280 part_die->is_external = DW_UNSND (&attr);
11281 break;
11282 case DW_AT_declaration:
11283 part_die->is_declaration = DW_UNSND (&attr);
11284 break;
11285 case DW_AT_type:
11286 part_die->has_type = 1;
11287 break;
11288 case DW_AT_abstract_origin:
11289 case DW_AT_specification:
72bf9492
DJ
11290 case DW_AT_extension:
11291 part_die->has_specification = 1;
c764a876 11292 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
11293 break;
11294 case DW_AT_sibling:
11295 /* Ignore absolute siblings, they might point outside of
11296 the current compile unit. */
11297 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
11298 complaint (&symfile_complaints,
11299 _("ignoring absolute DW_AT_sibling"));
c906108c 11300 else
b64f50a1 11301 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 11302 break;
fa4028e9
JB
11303 case DW_AT_byte_size:
11304 part_die->has_byte_size = 1;
11305 break;
68511cec
CES
11306 case DW_AT_calling_convention:
11307 /* DWARF doesn't provide a way to identify a program's source-level
11308 entry point. DW_AT_calling_convention attributes are only meant
11309 to describe functions' calling conventions.
11310
11311 However, because it's a necessary piece of information in
11312 Fortran, and because DW_CC_program is the only piece of debugging
11313 information whose definition refers to a 'main program' at all,
11314 several compilers have begun marking Fortran main programs with
11315 DW_CC_program --- even when those functions use the standard
11316 calling conventions.
11317
11318 So until DWARF specifies a way to provide this information and
11319 compilers pick up the new representation, we'll support this
11320 practice. */
11321 if (DW_UNSND (&attr) == DW_CC_program
11322 && cu->language == language_fortran)
01f8c46d
JK
11323 {
11324 set_main_name (part_die->name);
11325
11326 /* As this DIE has a static linkage the name would be difficult
11327 to look up later. */
11328 language_of_main = language_fortran;
11329 }
68511cec 11330 break;
481860b3
GB
11331 case DW_AT_inline:
11332 if (DW_UNSND (&attr) == DW_INL_inlined
11333 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11334 part_die->may_be_inlined = 1;
11335 break;
95554aad
TT
11336
11337 case DW_AT_import:
11338 if (part_die->tag == DW_TAG_imported_unit)
11339 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11340 break;
11341
c906108c
SS
11342 default:
11343 break;
11344 }
11345 }
11346
91da1414
MW
11347 if (high_pc_relative)
11348 part_die->highpc += part_die->lowpc;
11349
9373cf26
JK
11350 if (has_low_pc_attr && has_high_pc_attr)
11351 {
11352 /* When using the GNU linker, .gnu.linkonce. sections are used to
11353 eliminate duplicate copies of functions and vtables and such.
11354 The linker will arbitrarily choose one and discard the others.
11355 The AT_*_pc values for such functions refer to local labels in
11356 these sections. If the section from that file was discarded, the
11357 labels are not in the output, so the relocs get a value of 0.
11358 If this is a discarded function, mark the pc bounds as invalid,
11359 so that GDB will ignore it. */
11360 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11361 {
bb5ed363 11362 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11363
11364 complaint (&symfile_complaints,
11365 _("DW_AT_low_pc %s is zero "
11366 "for DIE at 0x%x [in module %s]"),
11367 paddress (gdbarch, part_die->lowpc),
b64f50a1 11368 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11369 }
11370 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11371 else if (part_die->lowpc >= part_die->highpc)
11372 {
bb5ed363 11373 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11374
11375 complaint (&symfile_complaints,
11376 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11377 "for DIE at 0x%x [in module %s]"),
11378 paddress (gdbarch, part_die->lowpc),
11379 paddress (gdbarch, part_die->highpc),
b64f50a1 11380 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11381 }
11382 else
11383 part_die->has_pc_info = 1;
11384 }
85cbf3d3 11385
c906108c
SS
11386 return info_ptr;
11387}
11388
72bf9492
DJ
11389/* Find a cached partial DIE at OFFSET in CU. */
11390
11391static struct partial_die_info *
b64f50a1 11392find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
11393{
11394 struct partial_die_info *lookup_die = NULL;
11395 struct partial_die_info part_die;
11396
11397 part_die.offset = offset;
b64f50a1
JK
11398 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11399 offset.sect_off);
72bf9492 11400
72bf9492
DJ
11401 return lookup_die;
11402}
11403
348e048f
DE
11404/* Find a partial DIE at OFFSET, which may or may not be in CU,
11405 except in the case of .debug_types DIEs which do not reference
11406 outside their CU (they do however referencing other types via
55f1336d 11407 DW_FORM_ref_sig8). */
72bf9492
DJ
11408
11409static struct partial_die_info *
b64f50a1 11410find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
72bf9492 11411{
bb5ed363 11412 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
11413 struct dwarf2_per_cu_data *per_cu = NULL;
11414 struct partial_die_info *pd = NULL;
72bf9492 11415
45452591 11416 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
11417 {
11418 pd = find_partial_die_in_comp_unit (offset, cu);
11419 if (pd != NULL)
11420 return pd;
0d99eb77
DE
11421 /* We missed recording what we needed.
11422 Load all dies and try again. */
11423 per_cu = cu->per_cu;
5afb4e99 11424 }
0d99eb77
DE
11425 else
11426 {
11427 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 11428 if (cu->per_cu->is_debug_types)
0d99eb77
DE
11429 {
11430 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11431 " external reference to offset 0x%lx [in module %s].\n"),
11432 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11433 bfd_get_filename (objfile->obfd));
11434 }
11435 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
72bf9492 11436
0d99eb77
DE
11437 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11438 load_partial_comp_unit (per_cu);
ae038cb0 11439
0d99eb77
DE
11440 per_cu->cu->last_used = 0;
11441 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11442 }
5afb4e99 11443
dee91e82
DE
11444 /* If we didn't find it, and not all dies have been loaded,
11445 load them all and try again. */
11446
5afb4e99
DJ
11447 if (pd == NULL && per_cu->load_all_dies == 0)
11448 {
5afb4e99 11449 per_cu->load_all_dies = 1;
fd820528
DE
11450
11451 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11452 THIS_CU->cu may already be in use. So we can't just free it and
11453 replace its DIEs with the ones we read in. Instead, we leave those
11454 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11455 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11456 set. */
dee91e82 11457 load_partial_comp_unit (per_cu);
5afb4e99
DJ
11458
11459 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11460 }
11461
11462 if (pd == NULL)
11463 internal_error (__FILE__, __LINE__,
3e43a32a
MS
11464 _("could not find partial DIE 0x%x "
11465 "in cache [from module %s]\n"),
b64f50a1 11466 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 11467 return pd;
72bf9492
DJ
11468}
11469
abc72ce4
DE
11470/* See if we can figure out if the class lives in a namespace. We do
11471 this by looking for a member function; its demangled name will
11472 contain namespace info, if there is any. */
11473
11474static void
11475guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11476 struct dwarf2_cu *cu)
11477{
11478 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11479 what template types look like, because the demangler
11480 frequently doesn't give the same name as the debug info. We
11481 could fix this by only using the demangled name to get the
11482 prefix (but see comment in read_structure_type). */
11483
11484 struct partial_die_info *real_pdi;
11485 struct partial_die_info *child_pdi;
11486
11487 /* If this DIE (this DIE's specification, if any) has a parent, then
11488 we should not do this. We'll prepend the parent's fully qualified
11489 name when we create the partial symbol. */
11490
11491 real_pdi = struct_pdi;
11492 while (real_pdi->has_specification)
11493 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11494
11495 if (real_pdi->die_parent != NULL)
11496 return;
11497
11498 for (child_pdi = struct_pdi->die_child;
11499 child_pdi != NULL;
11500 child_pdi = child_pdi->die_sibling)
11501 {
11502 if (child_pdi->tag == DW_TAG_subprogram
11503 && child_pdi->linkage_name != NULL)
11504 {
11505 char *actual_class_name
11506 = language_class_name_from_physname (cu->language_defn,
11507 child_pdi->linkage_name);
11508 if (actual_class_name != NULL)
11509 {
11510 struct_pdi->name
11511 = obsavestring (actual_class_name,
11512 strlen (actual_class_name),
11513 &cu->objfile->objfile_obstack);
11514 xfree (actual_class_name);
11515 }
11516 break;
11517 }
11518 }
11519}
11520
72bf9492
DJ
11521/* Adjust PART_DIE before generating a symbol for it. This function
11522 may set the is_external flag or change the DIE's name. */
11523
11524static void
11525fixup_partial_die (struct partial_die_info *part_die,
11526 struct dwarf2_cu *cu)
11527{
abc72ce4
DE
11528 /* Once we've fixed up a die, there's no point in doing so again.
11529 This also avoids a memory leak if we were to call
11530 guess_partial_die_structure_name multiple times. */
11531 if (part_die->fixup_called)
11532 return;
11533
72bf9492
DJ
11534 /* If we found a reference attribute and the DIE has no name, try
11535 to find a name in the referred to DIE. */
11536
11537 if (part_die->name == NULL && part_die->has_specification)
11538 {
11539 struct partial_die_info *spec_die;
72bf9492 11540
10b3939b 11541 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 11542
10b3939b 11543 fixup_partial_die (spec_die, cu);
72bf9492
DJ
11544
11545 if (spec_die->name)
11546 {
11547 part_die->name = spec_die->name;
11548
11549 /* Copy DW_AT_external attribute if it is set. */
11550 if (spec_die->is_external)
11551 part_die->is_external = spec_die->is_external;
11552 }
11553 }
11554
11555 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
11556
11557 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 11558 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 11559
abc72ce4
DE
11560 /* If there is no parent die to provide a namespace, and there are
11561 children, see if we can determine the namespace from their linkage
122d1940 11562 name. */
abc72ce4 11563 if (cu->language == language_cplus
8b70b953 11564 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
11565 && part_die->die_parent == NULL
11566 && part_die->has_children
11567 && (part_die->tag == DW_TAG_class_type
11568 || part_die->tag == DW_TAG_structure_type
11569 || part_die->tag == DW_TAG_union_type))
11570 guess_partial_die_structure_name (part_die, cu);
11571
53832f31
TT
11572 /* GCC might emit a nameless struct or union that has a linkage
11573 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11574 if (part_die->name == NULL
96408a79
SA
11575 && (part_die->tag == DW_TAG_class_type
11576 || part_die->tag == DW_TAG_interface_type
11577 || part_die->tag == DW_TAG_structure_type
11578 || part_die->tag == DW_TAG_union_type)
53832f31
TT
11579 && part_die->linkage_name != NULL)
11580 {
11581 char *demangled;
11582
11583 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11584 if (demangled)
11585 {
96408a79
SA
11586 const char *base;
11587
11588 /* Strip any leading namespaces/classes, keep only the base name.
11589 DW_AT_name for named DIEs does not contain the prefixes. */
11590 base = strrchr (demangled, ':');
11591 if (base && base > demangled && base[-1] == ':')
11592 base++;
11593 else
11594 base = demangled;
11595
11596 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
11597 &cu->objfile->objfile_obstack);
11598 xfree (demangled);
11599 }
11600 }
11601
abc72ce4 11602 part_die->fixup_called = 1;
72bf9492
DJ
11603}
11604
a8329558 11605/* Read an attribute value described by an attribute form. */
c906108c 11606
fe1b8b76 11607static gdb_byte *
dee91e82
DE
11608read_attribute_value (const struct die_reader_specs *reader,
11609 struct attribute *attr, unsigned form,
11610 gdb_byte *info_ptr)
c906108c 11611{
dee91e82
DE
11612 struct dwarf2_cu *cu = reader->cu;
11613 bfd *abfd = reader->abfd;
e7c27a73 11614 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
11615 unsigned int bytes_read;
11616 struct dwarf_block *blk;
11617
a8329558
KW
11618 attr->form = form;
11619 switch (form)
c906108c 11620 {
c906108c 11621 case DW_FORM_ref_addr:
ae411497 11622 if (cu->header.version == 2)
4568ecf9 11623 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 11624 else
4568ecf9
DE
11625 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11626 &cu->header, &bytes_read);
ae411497
TT
11627 info_ptr += bytes_read;
11628 break;
11629 case DW_FORM_addr:
e7c27a73 11630 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 11631 info_ptr += bytes_read;
c906108c
SS
11632 break;
11633 case DW_FORM_block2:
7b5a2f43 11634 blk = dwarf_alloc_block (cu);
c906108c
SS
11635 blk->size = read_2_bytes (abfd, info_ptr);
11636 info_ptr += 2;
11637 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11638 info_ptr += blk->size;
11639 DW_BLOCK (attr) = blk;
11640 break;
11641 case DW_FORM_block4:
7b5a2f43 11642 blk = dwarf_alloc_block (cu);
c906108c
SS
11643 blk->size = read_4_bytes (abfd, info_ptr);
11644 info_ptr += 4;
11645 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11646 info_ptr += blk->size;
11647 DW_BLOCK (attr) = blk;
11648 break;
11649 case DW_FORM_data2:
11650 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11651 info_ptr += 2;
11652 break;
11653 case DW_FORM_data4:
11654 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11655 info_ptr += 4;
11656 break;
11657 case DW_FORM_data8:
11658 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11659 info_ptr += 8;
11660 break;
2dc7f7b3
TT
11661 case DW_FORM_sec_offset:
11662 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11663 info_ptr += bytes_read;
11664 break;
c906108c 11665 case DW_FORM_string:
9b1c24c8 11666 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 11667 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
11668 info_ptr += bytes_read;
11669 break;
4bdf3d34
JJ
11670 case DW_FORM_strp:
11671 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11672 &bytes_read);
8285870a 11673 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
11674 info_ptr += bytes_read;
11675 break;
2dc7f7b3 11676 case DW_FORM_exprloc:
c906108c 11677 case DW_FORM_block:
7b5a2f43 11678 blk = dwarf_alloc_block (cu);
c906108c
SS
11679 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11680 info_ptr += bytes_read;
11681 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11682 info_ptr += blk->size;
11683 DW_BLOCK (attr) = blk;
11684 break;
11685 case DW_FORM_block1:
7b5a2f43 11686 blk = dwarf_alloc_block (cu);
c906108c
SS
11687 blk->size = read_1_byte (abfd, info_ptr);
11688 info_ptr += 1;
11689 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11690 info_ptr += blk->size;
11691 DW_BLOCK (attr) = blk;
11692 break;
11693 case DW_FORM_data1:
11694 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11695 info_ptr += 1;
11696 break;
11697 case DW_FORM_flag:
11698 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11699 info_ptr += 1;
11700 break;
2dc7f7b3
TT
11701 case DW_FORM_flag_present:
11702 DW_UNSND (attr) = 1;
11703 break;
c906108c
SS
11704 case DW_FORM_sdata:
11705 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11706 info_ptr += bytes_read;
11707 break;
11708 case DW_FORM_udata:
11709 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11710 info_ptr += bytes_read;
11711 break;
11712 case DW_FORM_ref1:
4568ecf9
DE
11713 DW_UNSND (attr) = (cu->header.offset.sect_off
11714 + read_1_byte (abfd, info_ptr));
c906108c
SS
11715 info_ptr += 1;
11716 break;
11717 case DW_FORM_ref2:
4568ecf9
DE
11718 DW_UNSND (attr) = (cu->header.offset.sect_off
11719 + read_2_bytes (abfd, info_ptr));
c906108c
SS
11720 info_ptr += 2;
11721 break;
11722 case DW_FORM_ref4:
4568ecf9
DE
11723 DW_UNSND (attr) = (cu->header.offset.sect_off
11724 + read_4_bytes (abfd, info_ptr));
c906108c
SS
11725 info_ptr += 4;
11726 break;
613e1657 11727 case DW_FORM_ref8:
4568ecf9
DE
11728 DW_UNSND (attr) = (cu->header.offset.sect_off
11729 + read_8_bytes (abfd, info_ptr));
613e1657
KB
11730 info_ptr += 8;
11731 break;
55f1336d 11732 case DW_FORM_ref_sig8:
348e048f
DE
11733 /* Convert the signature to something we can record in DW_UNSND
11734 for later lookup.
11735 NOTE: This is NULL if the type wasn't found. */
11736 DW_SIGNATURED_TYPE (attr) =
e319fa28 11737 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
11738 info_ptr += 8;
11739 break;
c906108c 11740 case DW_FORM_ref_udata:
4568ecf9
DE
11741 DW_UNSND (attr) = (cu->header.offset.sect_off
11742 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
11743 info_ptr += bytes_read;
11744 break;
c906108c 11745 case DW_FORM_indirect:
a8329558
KW
11746 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11747 info_ptr += bytes_read;
dee91e82 11748 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 11749 break;
3019eac3
DE
11750 case DW_FORM_GNU_addr_index:
11751 if (reader->dwo_file == NULL)
11752 {
11753 /* For now flag a hard error.
11754 Later we can turn this into a complaint. */
11755 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11756 dwarf_form_name (form),
11757 bfd_get_filename (abfd));
11758 }
11759 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11760 info_ptr += bytes_read;
11761 break;
11762 case DW_FORM_GNU_str_index:
11763 if (reader->dwo_file == NULL)
11764 {
11765 /* For now flag a hard error.
11766 Later we can turn this into a complaint if warranted. */
11767 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11768 dwarf_form_name (form),
11769 bfd_get_filename (abfd));
11770 }
11771 {
11772 ULONGEST str_index =
11773 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11774
11775 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11776 DW_STRING_IS_CANONICAL (attr) = 0;
11777 info_ptr += bytes_read;
11778 }
11779 break;
c906108c 11780 default:
8a3fe4f8 11781 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
11782 dwarf_form_name (form),
11783 bfd_get_filename (abfd));
c906108c 11784 }
28e94949
JB
11785
11786 /* We have seen instances where the compiler tried to emit a byte
11787 size attribute of -1 which ended up being encoded as an unsigned
11788 0xffffffff. Although 0xffffffff is technically a valid size value,
11789 an object of this size seems pretty unlikely so we can relatively
11790 safely treat these cases as if the size attribute was invalid and
11791 treat them as zero by default. */
11792 if (attr->name == DW_AT_byte_size
11793 && form == DW_FORM_data4
11794 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
11795 {
11796 complaint
11797 (&symfile_complaints,
43bbcdc2
PH
11798 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
11799 hex_string (DW_UNSND (attr)));
01c66ae6
JB
11800 DW_UNSND (attr) = 0;
11801 }
28e94949 11802
c906108c
SS
11803 return info_ptr;
11804}
11805
a8329558
KW
11806/* Read an attribute described by an abbreviated attribute. */
11807
fe1b8b76 11808static gdb_byte *
dee91e82
DE
11809read_attribute (const struct die_reader_specs *reader,
11810 struct attribute *attr, struct attr_abbrev *abbrev,
11811 gdb_byte *info_ptr)
a8329558
KW
11812{
11813 attr->name = abbrev->name;
dee91e82 11814 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
11815}
11816
0963b4bd 11817/* Read dwarf information from a buffer. */
c906108c
SS
11818
11819static unsigned int
fe1b8b76 11820read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 11821{
fe1b8b76 11822 return bfd_get_8 (abfd, buf);
c906108c
SS
11823}
11824
11825static int
fe1b8b76 11826read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 11827{
fe1b8b76 11828 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
11829}
11830
11831static unsigned int
fe1b8b76 11832read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11833{
fe1b8b76 11834 return bfd_get_16 (abfd, buf);
c906108c
SS
11835}
11836
21ae7a4d
JK
11837static int
11838read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
11839{
11840 return bfd_get_signed_16 (abfd, buf);
11841}
11842
c906108c 11843static unsigned int
fe1b8b76 11844read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11845{
fe1b8b76 11846 return bfd_get_32 (abfd, buf);
c906108c
SS
11847}
11848
21ae7a4d
JK
11849static int
11850read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
11851{
11852 return bfd_get_signed_32 (abfd, buf);
11853}
11854
93311388 11855static ULONGEST
fe1b8b76 11856read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11857{
fe1b8b76 11858 return bfd_get_64 (abfd, buf);
c906108c
SS
11859}
11860
11861static CORE_ADDR
fe1b8b76 11862read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 11863 unsigned int *bytes_read)
c906108c 11864{
e7c27a73 11865 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
11866 CORE_ADDR retval = 0;
11867
107d2387 11868 if (cu_header->signed_addr_p)
c906108c 11869 {
107d2387
AC
11870 switch (cu_header->addr_size)
11871 {
11872 case 2:
fe1b8b76 11873 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
11874 break;
11875 case 4:
fe1b8b76 11876 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
11877 break;
11878 case 8:
fe1b8b76 11879 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
11880 break;
11881 default:
8e65ff28 11882 internal_error (__FILE__, __LINE__,
e2e0b3e5 11883 _("read_address: bad switch, signed [in module %s]"),
659b0389 11884 bfd_get_filename (abfd));
107d2387
AC
11885 }
11886 }
11887 else
11888 {
11889 switch (cu_header->addr_size)
11890 {
11891 case 2:
fe1b8b76 11892 retval = bfd_get_16 (abfd, buf);
107d2387
AC
11893 break;
11894 case 4:
fe1b8b76 11895 retval = bfd_get_32 (abfd, buf);
107d2387
AC
11896 break;
11897 case 8:
fe1b8b76 11898 retval = bfd_get_64 (abfd, buf);
107d2387
AC
11899 break;
11900 default:
8e65ff28 11901 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
11902 _("read_address: bad switch, "
11903 "unsigned [in module %s]"),
659b0389 11904 bfd_get_filename (abfd));
107d2387 11905 }
c906108c 11906 }
64367e0a 11907
107d2387
AC
11908 *bytes_read = cu_header->addr_size;
11909 return retval;
c906108c
SS
11910}
11911
f7ef9339 11912/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
11913 specification allows the initial length to take up either 4 bytes
11914 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
11915 bytes describe the length and all offsets will be 8 bytes in length
11916 instead of 4.
11917
f7ef9339
KB
11918 An older, non-standard 64-bit format is also handled by this
11919 function. The older format in question stores the initial length
11920 as an 8-byte quantity without an escape value. Lengths greater
11921 than 2^32 aren't very common which means that the initial 4 bytes
11922 is almost always zero. Since a length value of zero doesn't make
11923 sense for the 32-bit format, this initial zero can be considered to
11924 be an escape value which indicates the presence of the older 64-bit
11925 format. As written, the code can't detect (old format) lengths
917c78fc
MK
11926 greater than 4GB. If it becomes necessary to handle lengths
11927 somewhat larger than 4GB, we could allow other small values (such
11928 as the non-sensical values of 1, 2, and 3) to also be used as
11929 escape values indicating the presence of the old format.
f7ef9339 11930
917c78fc
MK
11931 The value returned via bytes_read should be used to increment the
11932 relevant pointer after calling read_initial_length().
c764a876 11933
613e1657
KB
11934 [ Note: read_initial_length() and read_offset() are based on the
11935 document entitled "DWARF Debugging Information Format", revision
f7ef9339 11936 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
11937 from:
11938
f7ef9339 11939 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 11940
613e1657
KB
11941 This document is only a draft and is subject to change. (So beware.)
11942
f7ef9339 11943 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
11944 determined empirically by examining 64-bit ELF files produced by
11945 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
11946
11947 - Kevin, July 16, 2002
613e1657
KB
11948 ] */
11949
11950static LONGEST
c764a876 11951read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 11952{
fe1b8b76 11953 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 11954
dd373385 11955 if (length == 0xffffffff)
613e1657 11956 {
fe1b8b76 11957 length = bfd_get_64 (abfd, buf + 4);
613e1657 11958 *bytes_read = 12;
613e1657 11959 }
dd373385 11960 else if (length == 0)
f7ef9339 11961 {
dd373385 11962 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 11963 length = bfd_get_64 (abfd, buf);
f7ef9339 11964 *bytes_read = 8;
f7ef9339 11965 }
613e1657
KB
11966 else
11967 {
11968 *bytes_read = 4;
613e1657
KB
11969 }
11970
c764a876
DE
11971 return length;
11972}
dd373385 11973
c764a876
DE
11974/* Cover function for read_initial_length.
11975 Returns the length of the object at BUF, and stores the size of the
11976 initial length in *BYTES_READ and stores the size that offsets will be in
11977 *OFFSET_SIZE.
11978 If the initial length size is not equivalent to that specified in
11979 CU_HEADER then issue a complaint.
11980 This is useful when reading non-comp-unit headers. */
dd373385 11981
c764a876
DE
11982static LONGEST
11983read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
11984 const struct comp_unit_head *cu_header,
11985 unsigned int *bytes_read,
11986 unsigned int *offset_size)
11987{
11988 LONGEST length = read_initial_length (abfd, buf, bytes_read);
11989
11990 gdb_assert (cu_header->initial_length_size == 4
11991 || cu_header->initial_length_size == 8
11992 || cu_header->initial_length_size == 12);
11993
11994 if (cu_header->initial_length_size != *bytes_read)
11995 complaint (&symfile_complaints,
11996 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 11997
c764a876 11998 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 11999 return length;
613e1657
KB
12000}
12001
12002/* Read an offset from the data stream. The size of the offset is
917c78fc 12003 given by cu_header->offset_size. */
613e1657
KB
12004
12005static LONGEST
fe1b8b76 12006read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 12007 unsigned int *bytes_read)
c764a876
DE
12008{
12009 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 12010
c764a876
DE
12011 *bytes_read = cu_header->offset_size;
12012 return offset;
12013}
12014
12015/* Read an offset from the data stream. */
12016
12017static LONGEST
12018read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
12019{
12020 LONGEST retval = 0;
12021
c764a876 12022 switch (offset_size)
613e1657
KB
12023 {
12024 case 4:
fe1b8b76 12025 retval = bfd_get_32 (abfd, buf);
613e1657
KB
12026 break;
12027 case 8:
fe1b8b76 12028 retval = bfd_get_64 (abfd, buf);
613e1657
KB
12029 break;
12030 default:
8e65ff28 12031 internal_error (__FILE__, __LINE__,
c764a876 12032 _("read_offset_1: bad switch [in module %s]"),
659b0389 12033 bfd_get_filename (abfd));
613e1657
KB
12034 }
12035
917c78fc 12036 return retval;
613e1657
KB
12037}
12038
fe1b8b76
JB
12039static gdb_byte *
12040read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
12041{
12042 /* If the size of a host char is 8 bits, we can return a pointer
12043 to the buffer, otherwise we have to copy the data to a buffer
12044 allocated on the temporary obstack. */
4bdf3d34 12045 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 12046 return buf;
c906108c
SS
12047}
12048
12049static char *
9b1c24c8 12050read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
12051{
12052 /* If the size of a host char is 8 bits, we can return a pointer
12053 to the string, otherwise we have to copy the string to a buffer
12054 allocated on the temporary obstack. */
4bdf3d34 12055 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
12056 if (*buf == '\0')
12057 {
12058 *bytes_read_ptr = 1;
12059 return NULL;
12060 }
fe1b8b76
JB
12061 *bytes_read_ptr = strlen ((char *) buf) + 1;
12062 return (char *) buf;
4bdf3d34
JJ
12063}
12064
12065static char *
cf2c3c16 12066read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 12067{
be391dca 12068 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 12069 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
12070 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12071 bfd_get_filename (abfd));
dce234bc 12072 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
12073 error (_("DW_FORM_strp pointing outside of "
12074 ".debug_str section [in module %s]"),
12075 bfd_get_filename (abfd));
4bdf3d34 12076 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 12077 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 12078 return NULL;
dce234bc 12079 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
12080}
12081
cf2c3c16
TT
12082static char *
12083read_indirect_string (bfd *abfd, gdb_byte *buf,
12084 const struct comp_unit_head *cu_header,
12085 unsigned int *bytes_read_ptr)
12086{
12087 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12088
12089 return read_indirect_string_at_offset (abfd, str_offset);
12090}
12091
12df843f 12092static ULONGEST
fe1b8b76 12093read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12094{
12df843f 12095 ULONGEST result;
ce5d95e1 12096 unsigned int num_read;
c906108c
SS
12097 int i, shift;
12098 unsigned char byte;
12099
12100 result = 0;
12101 shift = 0;
12102 num_read = 0;
12103 i = 0;
12104 while (1)
12105 {
fe1b8b76 12106 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12107 buf++;
12108 num_read++;
12df843f 12109 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
12110 if ((byte & 128) == 0)
12111 {
12112 break;
12113 }
12114 shift += 7;
12115 }
12116 *bytes_read_ptr = num_read;
12117 return result;
12118}
12119
12df843f 12120static LONGEST
fe1b8b76 12121read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12122{
12df843f 12123 LONGEST result;
77e0b926 12124 int i, shift, num_read;
c906108c
SS
12125 unsigned char byte;
12126
12127 result = 0;
12128 shift = 0;
c906108c
SS
12129 num_read = 0;
12130 i = 0;
12131 while (1)
12132 {
fe1b8b76 12133 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12134 buf++;
12135 num_read++;
12df843f 12136 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
12137 shift += 7;
12138 if ((byte & 128) == 0)
12139 {
12140 break;
12141 }
12142 }
77e0b926 12143 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 12144 result |= -(((LONGEST) 1) << shift);
c906108c
SS
12145 *bytes_read_ptr = num_read;
12146 return result;
12147}
12148
3019eac3
DE
12149/* Given index ADDR_INDEX in .debug_addr, fetch the value.
12150 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12151 ADDR_SIZE is the size of addresses from the CU header. */
12152
12153static CORE_ADDR
12154read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12155{
12156 struct objfile *objfile = dwarf2_per_objfile->objfile;
12157 bfd *abfd = objfile->obfd;
12158 const gdb_byte *info_ptr;
12159
12160 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12161 if (dwarf2_per_objfile->addr.buffer == NULL)
12162 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12163 objfile->name);
12164 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12165 error (_("DW_FORM_addr_index pointing outside of "
12166 ".debug_addr section [in module %s]"),
12167 objfile->name);
12168 info_ptr = (dwarf2_per_objfile->addr.buffer
12169 + addr_base + addr_index * addr_size);
12170 if (addr_size == 4)
12171 return bfd_get_32 (abfd, info_ptr);
12172 else
12173 return bfd_get_64 (abfd, info_ptr);
12174}
12175
12176/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12177
12178static CORE_ADDR
12179read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12180{
12181 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12182}
12183
12184/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12185
12186static CORE_ADDR
12187read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12188 unsigned int *bytes_read)
12189{
12190 bfd *abfd = cu->objfile->obfd;
12191 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12192
12193 return read_addr_index (cu, addr_index);
12194}
12195
12196/* Data structure to pass results from dwarf2_read_addr_index_reader
12197 back to dwarf2_read_addr_index. */
12198
12199struct dwarf2_read_addr_index_data
12200{
12201 ULONGEST addr_base;
12202 int addr_size;
12203};
12204
12205/* die_reader_func for dwarf2_read_addr_index. */
12206
12207static void
12208dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12209 gdb_byte *info_ptr,
12210 struct die_info *comp_unit_die,
12211 int has_children,
12212 void *data)
12213{
12214 struct dwarf2_cu *cu = reader->cu;
12215 struct dwarf2_read_addr_index_data *aidata =
12216 (struct dwarf2_read_addr_index_data *) data;
12217
12218 aidata->addr_base = cu->addr_base;
12219 aidata->addr_size = cu->header.addr_size;
12220}
12221
12222/* Given an index in .debug_addr, fetch the value.
12223 NOTE: This can be called during dwarf expression evaluation,
12224 long after the debug information has been read, and thus per_cu->cu
12225 may no longer exist. */
12226
12227CORE_ADDR
12228dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12229 unsigned int addr_index)
12230{
12231 struct objfile *objfile = per_cu->objfile;
12232 struct dwarf2_cu *cu = per_cu->cu;
12233 ULONGEST addr_base;
12234 int addr_size;
12235
12236 /* This is intended to be called from outside this file. */
12237 dw2_setup (objfile);
12238
12239 /* We need addr_base and addr_size.
12240 If we don't have PER_CU->cu, we have to get it.
12241 Nasty, but the alternative is storing the needed info in PER_CU,
12242 which at this point doesn't seem justified: it's not clear how frequently
12243 it would get used and it would increase the size of every PER_CU.
12244 Entry points like dwarf2_per_cu_addr_size do a similar thing
12245 so we're not in uncharted territory here.
12246 Alas we need to be a bit more complicated as addr_base is contained
12247 in the DIE.
12248
12249 We don't need to read the entire CU(/TU).
12250 We just need the header and top level die.
12251 IWBN to use the aging mechanism to let us lazily later discard the CU.
12252 See however init_cutu_and_read_dies_simple. */
12253
12254 if (cu != NULL)
12255 {
12256 addr_base = cu->addr_base;
12257 addr_size = cu->header.addr_size;
12258 }
12259 else
12260 {
12261 struct dwarf2_read_addr_index_data aidata;
12262
12263 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12264 &aidata);
12265 addr_base = aidata.addr_base;
12266 addr_size = aidata.addr_size;
12267 }
12268
12269 return read_addr_index_1 (addr_index, addr_base, addr_size);
12270}
12271
12272/* Given a DW_AT_str_index, fetch the string. */
12273
12274static char *
12275read_str_index (const struct die_reader_specs *reader,
12276 struct dwarf2_cu *cu, ULONGEST str_index)
12277{
12278 struct objfile *objfile = dwarf2_per_objfile->objfile;
12279 const char *dwo_name = objfile->name;
12280 bfd *abfd = objfile->obfd;
12281 struct dwo_sections *sections = &reader->dwo_file->sections;
12282 gdb_byte *info_ptr;
12283 ULONGEST str_offset;
12284
12285 dwarf2_read_section (objfile, &sections->str);
12286 dwarf2_read_section (objfile, &sections->str_offsets);
12287 if (sections->str.buffer == NULL)
12288 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12289 " in CU at offset 0x%lx [in module %s]"),
12290 (long) cu->header.offset.sect_off, dwo_name);
12291 if (sections->str_offsets.buffer == NULL)
12292 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12293 " in CU at offset 0x%lx [in module %s]"),
12294 (long) cu->header.offset.sect_off, dwo_name);
12295 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12296 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12297 " section in CU at offset 0x%lx [in module %s]"),
12298 (long) cu->header.offset.sect_off, dwo_name);
12299 info_ptr = (sections->str_offsets.buffer
12300 + str_index * cu->header.offset_size);
12301 if (cu->header.offset_size == 4)
12302 str_offset = bfd_get_32 (abfd, info_ptr);
12303 else
12304 str_offset = bfd_get_64 (abfd, info_ptr);
12305 if (str_offset >= sections->str.size)
12306 error (_("Offset from DW_FORM_str_index pointing outside of"
12307 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12308 (long) cu->header.offset.sect_off, dwo_name);
12309 return (char *) (sections->str.buffer + str_offset);
12310}
12311
3019eac3
DE
12312/* Return the length of an LEB128 number in BUF. */
12313
12314static int
12315leb128_size (const gdb_byte *buf)
12316{
12317 const gdb_byte *begin = buf;
12318 gdb_byte byte;
12319
12320 while (1)
12321 {
12322 byte = *buf++;
12323 if ((byte & 128) == 0)
12324 return buf - begin;
12325 }
12326}
12327
c906108c 12328static void
e142c38c 12329set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
12330{
12331 switch (lang)
12332 {
12333 case DW_LANG_C89:
76bee0cc 12334 case DW_LANG_C99:
c906108c 12335 case DW_LANG_C:
e142c38c 12336 cu->language = language_c;
c906108c
SS
12337 break;
12338 case DW_LANG_C_plus_plus:
e142c38c 12339 cu->language = language_cplus;
c906108c 12340 break;
6aecb9c2
JB
12341 case DW_LANG_D:
12342 cu->language = language_d;
12343 break;
c906108c
SS
12344 case DW_LANG_Fortran77:
12345 case DW_LANG_Fortran90:
b21b22e0 12346 case DW_LANG_Fortran95:
e142c38c 12347 cu->language = language_fortran;
c906108c 12348 break;
a766d390
DE
12349 case DW_LANG_Go:
12350 cu->language = language_go;
12351 break;
c906108c 12352 case DW_LANG_Mips_Assembler:
e142c38c 12353 cu->language = language_asm;
c906108c 12354 break;
bebd888e 12355 case DW_LANG_Java:
e142c38c 12356 cu->language = language_java;
bebd888e 12357 break;
c906108c 12358 case DW_LANG_Ada83:
8aaf0b47 12359 case DW_LANG_Ada95:
bc5f45f8
JB
12360 cu->language = language_ada;
12361 break;
72019c9c
GM
12362 case DW_LANG_Modula2:
12363 cu->language = language_m2;
12364 break;
fe8e67fd
PM
12365 case DW_LANG_Pascal83:
12366 cu->language = language_pascal;
12367 break;
22566fbd
DJ
12368 case DW_LANG_ObjC:
12369 cu->language = language_objc;
12370 break;
c906108c
SS
12371 case DW_LANG_Cobol74:
12372 case DW_LANG_Cobol85:
c906108c 12373 default:
e142c38c 12374 cu->language = language_minimal;
c906108c
SS
12375 break;
12376 }
e142c38c 12377 cu->language_defn = language_def (cu->language);
c906108c
SS
12378}
12379
12380/* Return the named attribute or NULL if not there. */
12381
12382static struct attribute *
e142c38c 12383dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 12384{
a48e046c 12385 for (;;)
c906108c 12386 {
a48e046c
TT
12387 unsigned int i;
12388 struct attribute *spec = NULL;
12389
12390 for (i = 0; i < die->num_attrs; ++i)
12391 {
12392 if (die->attrs[i].name == name)
12393 return &die->attrs[i];
12394 if (die->attrs[i].name == DW_AT_specification
12395 || die->attrs[i].name == DW_AT_abstract_origin)
12396 spec = &die->attrs[i];
12397 }
12398
12399 if (!spec)
12400 break;
c906108c 12401
f2f0e013 12402 die = follow_die_ref (die, spec, &cu);
f2f0e013 12403 }
c5aa993b 12404
c906108c
SS
12405 return NULL;
12406}
12407
348e048f
DE
12408/* Return the named attribute or NULL if not there,
12409 but do not follow DW_AT_specification, etc.
12410 This is for use in contexts where we're reading .debug_types dies.
12411 Following DW_AT_specification, DW_AT_abstract_origin will take us
12412 back up the chain, and we want to go down. */
12413
12414static struct attribute *
12415dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12416 struct dwarf2_cu *cu)
12417{
12418 unsigned int i;
12419
12420 for (i = 0; i < die->num_attrs; ++i)
12421 if (die->attrs[i].name == name)
12422 return &die->attrs[i];
12423
12424 return NULL;
12425}
12426
05cf31d1
JB
12427/* Return non-zero iff the attribute NAME is defined for the given DIE,
12428 and holds a non-zero value. This function should only be used for
2dc7f7b3 12429 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
12430
12431static int
12432dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12433{
12434 struct attribute *attr = dwarf2_attr (die, name, cu);
12435
12436 return (attr && DW_UNSND (attr));
12437}
12438
3ca72b44 12439static int
e142c38c 12440die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 12441{
05cf31d1
JB
12442 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12443 which value is non-zero. However, we have to be careful with
12444 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12445 (via dwarf2_flag_true_p) follows this attribute. So we may
12446 end up accidently finding a declaration attribute that belongs
12447 to a different DIE referenced by the specification attribute,
12448 even though the given DIE does not have a declaration attribute. */
12449 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12450 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
12451}
12452
63d06c5c 12453/* Return the die giving the specification for DIE, if there is
f2f0e013 12454 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
12455 containing the return value on output. If there is no
12456 specification, but there is an abstract origin, that is
12457 returned. */
63d06c5c
DC
12458
12459static struct die_info *
f2f0e013 12460die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 12461{
f2f0e013
DJ
12462 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12463 *spec_cu);
63d06c5c 12464
edb3359d
DJ
12465 if (spec_attr == NULL)
12466 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12467
63d06c5c
DC
12468 if (spec_attr == NULL)
12469 return NULL;
12470 else
f2f0e013 12471 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 12472}
c906108c 12473
debd256d 12474/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
12475 refers to.
12476 NOTE: This is also used as a "cleanup" function. */
12477
debd256d
JB
12478static void
12479free_line_header (struct line_header *lh)
12480{
12481 if (lh->standard_opcode_lengths)
a8bc7b56 12482 xfree (lh->standard_opcode_lengths);
debd256d
JB
12483
12484 /* Remember that all the lh->file_names[i].name pointers are
12485 pointers into debug_line_buffer, and don't need to be freed. */
12486 if (lh->file_names)
a8bc7b56 12487 xfree (lh->file_names);
debd256d
JB
12488
12489 /* Similarly for the include directory names. */
12490 if (lh->include_dirs)
a8bc7b56 12491 xfree (lh->include_dirs);
debd256d 12492
a8bc7b56 12493 xfree (lh);
debd256d
JB
12494}
12495
debd256d 12496/* Add an entry to LH's include directory table. */
ae2de4f8 12497
debd256d
JB
12498static void
12499add_include_dir (struct line_header *lh, char *include_dir)
c906108c 12500{
debd256d
JB
12501 /* Grow the array if necessary. */
12502 if (lh->include_dirs_size == 0)
c5aa993b 12503 {
debd256d
JB
12504 lh->include_dirs_size = 1; /* for testing */
12505 lh->include_dirs = xmalloc (lh->include_dirs_size
12506 * sizeof (*lh->include_dirs));
12507 }
12508 else if (lh->num_include_dirs >= lh->include_dirs_size)
12509 {
12510 lh->include_dirs_size *= 2;
12511 lh->include_dirs = xrealloc (lh->include_dirs,
12512 (lh->include_dirs_size
12513 * sizeof (*lh->include_dirs)));
c5aa993b 12514 }
c906108c 12515
debd256d
JB
12516 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12517}
6e70227d 12518
debd256d 12519/* Add an entry to LH's file name table. */
ae2de4f8 12520
debd256d
JB
12521static void
12522add_file_name (struct line_header *lh,
12523 char *name,
12524 unsigned int dir_index,
12525 unsigned int mod_time,
12526 unsigned int length)
12527{
12528 struct file_entry *fe;
12529
12530 /* Grow the array if necessary. */
12531 if (lh->file_names_size == 0)
12532 {
12533 lh->file_names_size = 1; /* for testing */
12534 lh->file_names = xmalloc (lh->file_names_size
12535 * sizeof (*lh->file_names));
12536 }
12537 else if (lh->num_file_names >= lh->file_names_size)
12538 {
12539 lh->file_names_size *= 2;
12540 lh->file_names = xrealloc (lh->file_names,
12541 (lh->file_names_size
12542 * sizeof (*lh->file_names)));
12543 }
12544
12545 fe = &lh->file_names[lh->num_file_names++];
12546 fe->name = name;
12547 fe->dir_index = dir_index;
12548 fe->mod_time = mod_time;
12549 fe->length = length;
aaa75496 12550 fe->included_p = 0;
cb1df416 12551 fe->symtab = NULL;
debd256d 12552}
6e70227d 12553
debd256d 12554/* Read the statement program header starting at OFFSET in
3019eac3 12555 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 12556 to a struct line_header, allocated using xmalloc.
debd256d
JB
12557
12558 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
12559 the returned object point into the dwarf line section buffer,
12560 and must not be freed. */
ae2de4f8 12561
debd256d 12562static struct line_header *
3019eac3 12563dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
12564{
12565 struct cleanup *back_to;
12566 struct line_header *lh;
fe1b8b76 12567 gdb_byte *line_ptr;
c764a876 12568 unsigned int bytes_read, offset_size;
debd256d
JB
12569 int i;
12570 char *cur_dir, *cur_file;
3019eac3
DE
12571 struct dwarf2_section_info *section;
12572 bfd *abfd;
12573
12574 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12575 DWO file. */
12576 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12577 section = &cu->dwo_unit->dwo_file->sections.line;
12578 else
12579 section = &dwarf2_per_objfile->line;
debd256d 12580
3019eac3
DE
12581 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12582 if (section->buffer == NULL)
debd256d 12583 {
3019eac3
DE
12584 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12585 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12586 else
12587 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
12588 return 0;
12589 }
12590
fceca515
DE
12591 /* We can't do this until we know the section is non-empty.
12592 Only then do we know we have such a section. */
12593 abfd = section->asection->owner;
12594
a738430d
MK
12595 /* Make sure that at least there's room for the total_length field.
12596 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 12597 if (offset + 4 >= section->size)
debd256d 12598 {
4d3c2250 12599 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12600 return 0;
12601 }
12602
12603 lh = xmalloc (sizeof (*lh));
12604 memset (lh, 0, sizeof (*lh));
12605 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12606 (void *) lh);
12607
3019eac3 12608 line_ptr = section->buffer + offset;
debd256d 12609
a738430d 12610 /* Read in the header. */
6e70227d 12611 lh->total_length =
c764a876
DE
12612 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12613 &bytes_read, &offset_size);
debd256d 12614 line_ptr += bytes_read;
3019eac3 12615 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 12616 {
4d3c2250 12617 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12618 return 0;
12619 }
12620 lh->statement_program_end = line_ptr + lh->total_length;
12621 lh->version = read_2_bytes (abfd, line_ptr);
12622 line_ptr += 2;
c764a876
DE
12623 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12624 line_ptr += offset_size;
debd256d
JB
12625 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12626 line_ptr += 1;
2dc7f7b3
TT
12627 if (lh->version >= 4)
12628 {
12629 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12630 line_ptr += 1;
12631 }
12632 else
12633 lh->maximum_ops_per_instruction = 1;
12634
12635 if (lh->maximum_ops_per_instruction == 0)
12636 {
12637 lh->maximum_ops_per_instruction = 1;
12638 complaint (&symfile_complaints,
3e43a32a
MS
12639 _("invalid maximum_ops_per_instruction "
12640 "in `.debug_line' section"));
2dc7f7b3
TT
12641 }
12642
debd256d
JB
12643 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12644 line_ptr += 1;
12645 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12646 line_ptr += 1;
12647 lh->line_range = read_1_byte (abfd, line_ptr);
12648 line_ptr += 1;
12649 lh->opcode_base = read_1_byte (abfd, line_ptr);
12650 line_ptr += 1;
12651 lh->standard_opcode_lengths
fe1b8b76 12652 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
12653
12654 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12655 for (i = 1; i < lh->opcode_base; ++i)
12656 {
12657 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12658 line_ptr += 1;
12659 }
12660
a738430d 12661 /* Read directory table. */
9b1c24c8 12662 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12663 {
12664 line_ptr += bytes_read;
12665 add_include_dir (lh, cur_dir);
12666 }
12667 line_ptr += bytes_read;
12668
a738430d 12669 /* Read file name table. */
9b1c24c8 12670 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12671 {
12672 unsigned int dir_index, mod_time, length;
12673
12674 line_ptr += bytes_read;
12675 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12676 line_ptr += bytes_read;
12677 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12678 line_ptr += bytes_read;
12679 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12680 line_ptr += bytes_read;
12681
12682 add_file_name (lh, cur_file, dir_index, mod_time, length);
12683 }
12684 line_ptr += bytes_read;
6e70227d 12685 lh->statement_program_start = line_ptr;
debd256d 12686
3019eac3 12687 if (line_ptr > (section->buffer + section->size))
4d3c2250 12688 complaint (&symfile_complaints,
3e43a32a
MS
12689 _("line number info header doesn't "
12690 "fit in `.debug_line' section"));
debd256d
JB
12691
12692 discard_cleanups (back_to);
12693 return lh;
12694}
c906108c 12695
c6da4cef
DE
12696/* Subroutine of dwarf_decode_lines to simplify it.
12697 Return the file name of the psymtab for included file FILE_INDEX
12698 in line header LH of PST.
12699 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12700 If space for the result is malloc'd, it will be freed by a cleanup.
12701 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12702
12703static char *
12704psymtab_include_file_name (const struct line_header *lh, int file_index,
12705 const struct partial_symtab *pst,
12706 const char *comp_dir)
12707{
12708 const struct file_entry fe = lh->file_names [file_index];
12709 char *include_name = fe.name;
12710 char *include_name_to_compare = include_name;
12711 char *dir_name = NULL;
72b9f47f
TT
12712 const char *pst_filename;
12713 char *copied_name = NULL;
c6da4cef
DE
12714 int file_is_pst;
12715
12716 if (fe.dir_index)
12717 dir_name = lh->include_dirs[fe.dir_index - 1];
12718
12719 if (!IS_ABSOLUTE_PATH (include_name)
12720 && (dir_name != NULL || comp_dir != NULL))
12721 {
12722 /* Avoid creating a duplicate psymtab for PST.
12723 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12724 Before we do the comparison, however, we need to account
12725 for DIR_NAME and COMP_DIR.
12726 First prepend dir_name (if non-NULL). If we still don't
12727 have an absolute path prepend comp_dir (if non-NULL).
12728 However, the directory we record in the include-file's
12729 psymtab does not contain COMP_DIR (to match the
12730 corresponding symtab(s)).
12731
12732 Example:
12733
12734 bash$ cd /tmp
12735 bash$ gcc -g ./hello.c
12736 include_name = "hello.c"
12737 dir_name = "."
12738 DW_AT_comp_dir = comp_dir = "/tmp"
12739 DW_AT_name = "./hello.c" */
12740
12741 if (dir_name != NULL)
12742 {
12743 include_name = concat (dir_name, SLASH_STRING,
12744 include_name, (char *)NULL);
12745 include_name_to_compare = include_name;
12746 make_cleanup (xfree, include_name);
12747 }
12748 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12749 {
12750 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12751 include_name, (char *)NULL);
12752 }
12753 }
12754
12755 pst_filename = pst->filename;
12756 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12757 {
72b9f47f
TT
12758 copied_name = concat (pst->dirname, SLASH_STRING,
12759 pst_filename, (char *)NULL);
12760 pst_filename = copied_name;
c6da4cef
DE
12761 }
12762
1e3fad37 12763 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
12764
12765 if (include_name_to_compare != include_name)
12766 xfree (include_name_to_compare);
72b9f47f
TT
12767 if (copied_name != NULL)
12768 xfree (copied_name);
c6da4cef
DE
12769
12770 if (file_is_pst)
12771 return NULL;
12772 return include_name;
12773}
12774
c91513d8
PP
12775/* Ignore this record_line request. */
12776
12777static void
12778noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12779{
12780 return;
12781}
12782
f3f5162e
DE
12783/* Subroutine of dwarf_decode_lines to simplify it.
12784 Process the line number information in LH. */
debd256d 12785
c906108c 12786static void
f3f5162e
DE
12787dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
12788 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 12789{
a8c50c1f 12790 gdb_byte *line_ptr, *extended_end;
fe1b8b76 12791 gdb_byte *line_end;
a8c50c1f 12792 unsigned int bytes_read, extended_len;
c906108c 12793 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
12794 CORE_ADDR baseaddr;
12795 struct objfile *objfile = cu->objfile;
f3f5162e 12796 bfd *abfd = objfile->obfd;
fbf65064 12797 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 12798 const int decode_for_pst_p = (pst != NULL);
f3f5162e 12799 struct subfile *last_subfile = NULL;
c91513d8
PP
12800 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
12801 = record_line;
e142c38c
DJ
12802
12803 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 12804
debd256d
JB
12805 line_ptr = lh->statement_program_start;
12806 line_end = lh->statement_program_end;
c906108c
SS
12807
12808 /* Read the statement sequences until there's nothing left. */
12809 while (line_ptr < line_end)
12810 {
12811 /* state machine registers */
12812 CORE_ADDR address = 0;
12813 unsigned int file = 1;
12814 unsigned int line = 1;
12815 unsigned int column = 0;
debd256d 12816 int is_stmt = lh->default_is_stmt;
c906108c
SS
12817 int basic_block = 0;
12818 int end_sequence = 0;
fbf65064 12819 CORE_ADDR addr;
2dc7f7b3 12820 unsigned char op_index = 0;
c906108c 12821
aaa75496 12822 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 12823 {
aaa75496 12824 /* Start a subfile for the current file of the state machine. */
debd256d
JB
12825 /* lh->include_dirs and lh->file_names are 0-based, but the
12826 directory and file name numbers in the statement program
12827 are 1-based. */
12828 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 12829 char *dir = NULL;
a738430d 12830
debd256d
JB
12831 if (fe->dir_index)
12832 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
12833
12834 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
12835 }
12836
a738430d 12837 /* Decode the table. */
c5aa993b 12838 while (!end_sequence)
c906108c
SS
12839 {
12840 op_code = read_1_byte (abfd, line_ptr);
12841 line_ptr += 1;
59205f5a
JB
12842 if (line_ptr > line_end)
12843 {
12844 dwarf2_debug_line_missing_end_sequence_complaint ();
12845 break;
12846 }
9aa1fe7e 12847
debd256d 12848 if (op_code >= lh->opcode_base)
6e70227d 12849 {
a738430d 12850 /* Special operand. */
debd256d 12851 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
12852 address += (((op_index + (adj_opcode / lh->line_range))
12853 / lh->maximum_ops_per_instruction)
12854 * lh->minimum_instruction_length);
12855 op_index = ((op_index + (adj_opcode / lh->line_range))
12856 % lh->maximum_ops_per_instruction);
debd256d 12857 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 12858 if (lh->num_file_names < file || file == 0)
25e43795 12859 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
12860 /* For now we ignore lines not starting on an
12861 instruction boundary. */
12862 else if (op_index == 0)
25e43795
DJ
12863 {
12864 lh->file_names[file - 1].included_p = 1;
ca5f395d 12865 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
12866 {
12867 if (last_subfile != current_subfile)
12868 {
12869 addr = gdbarch_addr_bits_remove (gdbarch, address);
12870 if (last_subfile)
c91513d8 12871 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
12872 last_subfile = current_subfile;
12873 }
25e43795 12874 /* Append row to matrix using current values. */
7019d805 12875 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 12876 (*p_record_line) (current_subfile, line, addr);
366da635 12877 }
25e43795 12878 }
ca5f395d 12879 basic_block = 0;
9aa1fe7e
GK
12880 }
12881 else switch (op_code)
c906108c
SS
12882 {
12883 case DW_LNS_extended_op:
3e43a32a
MS
12884 extended_len = read_unsigned_leb128 (abfd, line_ptr,
12885 &bytes_read);
473b7be6 12886 line_ptr += bytes_read;
a8c50c1f 12887 extended_end = line_ptr + extended_len;
c906108c
SS
12888 extended_op = read_1_byte (abfd, line_ptr);
12889 line_ptr += 1;
12890 switch (extended_op)
12891 {
12892 case DW_LNE_end_sequence:
c91513d8 12893 p_record_line = record_line;
c906108c 12894 end_sequence = 1;
c906108c
SS
12895 break;
12896 case DW_LNE_set_address:
e7c27a73 12897 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
12898
12899 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
12900 {
12901 /* This line table is for a function which has been
12902 GCd by the linker. Ignore it. PR gdb/12528 */
12903
12904 long line_offset
12905 = line_ptr - dwarf2_per_objfile->line.buffer;
12906
12907 complaint (&symfile_complaints,
12908 _(".debug_line address at offset 0x%lx is 0 "
12909 "[in module %s]"),
bb5ed363 12910 line_offset, objfile->name);
c91513d8
PP
12911 p_record_line = noop_record_line;
12912 }
12913
2dc7f7b3 12914 op_index = 0;
107d2387
AC
12915 line_ptr += bytes_read;
12916 address += baseaddr;
c906108c
SS
12917 break;
12918 case DW_LNE_define_file:
debd256d
JB
12919 {
12920 char *cur_file;
12921 unsigned int dir_index, mod_time, length;
6e70227d 12922
3e43a32a
MS
12923 cur_file = read_direct_string (abfd, line_ptr,
12924 &bytes_read);
debd256d
JB
12925 line_ptr += bytes_read;
12926 dir_index =
12927 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12928 line_ptr += bytes_read;
12929 mod_time =
12930 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12931 line_ptr += bytes_read;
12932 length =
12933 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12934 line_ptr += bytes_read;
12935 add_file_name (lh, cur_file, dir_index, mod_time, length);
12936 }
c906108c 12937 break;
d0c6ba3d
CC
12938 case DW_LNE_set_discriminator:
12939 /* The discriminator is not interesting to the debugger;
12940 just ignore it. */
12941 line_ptr = extended_end;
12942 break;
c906108c 12943 default:
4d3c2250 12944 complaint (&symfile_complaints,
e2e0b3e5 12945 _("mangled .debug_line section"));
debd256d 12946 return;
c906108c 12947 }
a8c50c1f
DJ
12948 /* Make sure that we parsed the extended op correctly. If e.g.
12949 we expected a different address size than the producer used,
12950 we may have read the wrong number of bytes. */
12951 if (line_ptr != extended_end)
12952 {
12953 complaint (&symfile_complaints,
12954 _("mangled .debug_line section"));
12955 return;
12956 }
c906108c
SS
12957 break;
12958 case DW_LNS_copy:
59205f5a 12959 if (lh->num_file_names < file || file == 0)
25e43795
DJ
12960 dwarf2_debug_line_missing_file_complaint ();
12961 else
366da635 12962 {
25e43795 12963 lh->file_names[file - 1].included_p = 1;
ca5f395d 12964 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
12965 {
12966 if (last_subfile != current_subfile)
12967 {
12968 addr = gdbarch_addr_bits_remove (gdbarch, address);
12969 if (last_subfile)
c91513d8 12970 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
12971 last_subfile = current_subfile;
12972 }
7019d805 12973 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 12974 (*p_record_line) (current_subfile, line, addr);
fbf65064 12975 }
366da635 12976 }
c906108c
SS
12977 basic_block = 0;
12978 break;
12979 case DW_LNS_advance_pc:
2dc7f7b3
TT
12980 {
12981 CORE_ADDR adjust
12982 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12983
12984 address += (((op_index + adjust)
12985 / lh->maximum_ops_per_instruction)
12986 * lh->minimum_instruction_length);
12987 op_index = ((op_index + adjust)
12988 % lh->maximum_ops_per_instruction);
12989 line_ptr += bytes_read;
12990 }
c906108c
SS
12991 break;
12992 case DW_LNS_advance_line:
12993 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
12994 line_ptr += bytes_read;
12995 break;
12996 case DW_LNS_set_file:
debd256d 12997 {
a738430d
MK
12998 /* The arrays lh->include_dirs and lh->file_names are
12999 0-based, but the directory and file name numbers in
13000 the statement program are 1-based. */
debd256d 13001 struct file_entry *fe;
4f1520fb 13002 char *dir = NULL;
a738430d 13003
debd256d
JB
13004 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13005 line_ptr += bytes_read;
59205f5a 13006 if (lh->num_file_names < file || file == 0)
25e43795
DJ
13007 dwarf2_debug_line_missing_file_complaint ();
13008 else
13009 {
13010 fe = &lh->file_names[file - 1];
13011 if (fe->dir_index)
13012 dir = lh->include_dirs[fe->dir_index - 1];
13013 if (!decode_for_pst_p)
13014 {
13015 last_subfile = current_subfile;
13016 dwarf2_start_subfile (fe->name, dir, comp_dir);
13017 }
13018 }
debd256d 13019 }
c906108c
SS
13020 break;
13021 case DW_LNS_set_column:
13022 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13023 line_ptr += bytes_read;
13024 break;
13025 case DW_LNS_negate_stmt:
13026 is_stmt = (!is_stmt);
13027 break;
13028 case DW_LNS_set_basic_block:
13029 basic_block = 1;
13030 break;
c2c6d25f
JM
13031 /* Add to the address register of the state machine the
13032 address increment value corresponding to special opcode
a738430d
MK
13033 255. I.e., this value is scaled by the minimum
13034 instruction length since special opcode 255 would have
b021a221 13035 scaled the increment. */
c906108c 13036 case DW_LNS_const_add_pc:
2dc7f7b3
TT
13037 {
13038 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13039
13040 address += (((op_index + adjust)
13041 / lh->maximum_ops_per_instruction)
13042 * lh->minimum_instruction_length);
13043 op_index = ((op_index + adjust)
13044 % lh->maximum_ops_per_instruction);
13045 }
c906108c
SS
13046 break;
13047 case DW_LNS_fixed_advance_pc:
13048 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 13049 op_index = 0;
c906108c
SS
13050 line_ptr += 2;
13051 break;
9aa1fe7e 13052 default:
a738430d
MK
13053 {
13054 /* Unknown standard opcode, ignore it. */
9aa1fe7e 13055 int i;
a738430d 13056
debd256d 13057 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
13058 {
13059 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13060 line_ptr += bytes_read;
13061 }
13062 }
c906108c
SS
13063 }
13064 }
59205f5a
JB
13065 if (lh->num_file_names < file || file == 0)
13066 dwarf2_debug_line_missing_file_complaint ();
13067 else
13068 {
13069 lh->file_names[file - 1].included_p = 1;
13070 if (!decode_for_pst_p)
fbf65064
UW
13071 {
13072 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13073 (*p_record_line) (current_subfile, 0, addr);
fbf65064 13074 }
59205f5a 13075 }
c906108c 13076 }
f3f5162e
DE
13077}
13078
13079/* Decode the Line Number Program (LNP) for the given line_header
13080 structure and CU. The actual information extracted and the type
13081 of structures created from the LNP depends on the value of PST.
13082
13083 1. If PST is NULL, then this procedure uses the data from the program
13084 to create all necessary symbol tables, and their linetables.
13085
13086 2. If PST is not NULL, this procedure reads the program to determine
13087 the list of files included by the unit represented by PST, and
13088 builds all the associated partial symbol tables.
13089
13090 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13091 It is used for relative paths in the line table.
13092 NOTE: When processing partial symtabs (pst != NULL),
13093 comp_dir == pst->dirname.
13094
13095 NOTE: It is important that psymtabs have the same file name (via strcmp)
13096 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13097 symtab we don't use it in the name of the psymtabs we create.
13098 E.g. expand_line_sal requires this when finding psymtabs to expand.
13099 A good testcase for this is mb-inline.exp. */
13100
13101static void
13102dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13103 struct dwarf2_cu *cu, struct partial_symtab *pst,
13104 int want_line_info)
13105{
13106 struct objfile *objfile = cu->objfile;
13107 const int decode_for_pst_p = (pst != NULL);
13108 struct subfile *first_subfile = current_subfile;
13109
13110 if (want_line_info)
13111 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
13112
13113 if (decode_for_pst_p)
13114 {
13115 int file_index;
13116
13117 /* Now that we're done scanning the Line Header Program, we can
13118 create the psymtab of each included file. */
13119 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13120 if (lh->file_names[file_index].included_p == 1)
13121 {
c6da4cef
DE
13122 char *include_name =
13123 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13124 if (include_name != NULL)
aaa75496
JB
13125 dwarf2_create_include_psymtab (include_name, pst, objfile);
13126 }
13127 }
cb1df416
DJ
13128 else
13129 {
13130 /* Make sure a symtab is created for every file, even files
13131 which contain only variables (i.e. no code with associated
13132 line numbers). */
cb1df416 13133 int i;
cb1df416
DJ
13134
13135 for (i = 0; i < lh->num_file_names; i++)
13136 {
13137 char *dir = NULL;
f3f5162e 13138 struct file_entry *fe;
9a619af0 13139
cb1df416
DJ
13140 fe = &lh->file_names[i];
13141 if (fe->dir_index)
13142 dir = lh->include_dirs[fe->dir_index - 1];
13143 dwarf2_start_subfile (fe->name, dir, comp_dir);
13144
13145 /* Skip the main file; we don't need it, and it must be
13146 allocated last, so that it will show up before the
13147 non-primary symtabs in the objfile's symtab list. */
13148 if (current_subfile == first_subfile)
13149 continue;
13150
13151 if (current_subfile->symtab == NULL)
13152 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 13153 objfile);
cb1df416
DJ
13154 fe->symtab = current_subfile->symtab;
13155 }
13156 }
c906108c
SS
13157}
13158
13159/* Start a subfile for DWARF. FILENAME is the name of the file and
13160 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
13161 or NULL if not known. COMP_DIR is the compilation directory for the
13162 linetable's compilation unit or NULL if not known.
c906108c
SS
13163 This routine tries to keep line numbers from identical absolute and
13164 relative file names in a common subfile.
13165
13166 Using the `list' example from the GDB testsuite, which resides in
13167 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13168 of /srcdir/list0.c yields the following debugging information for list0.c:
13169
c5aa993b
JM
13170 DW_AT_name: /srcdir/list0.c
13171 DW_AT_comp_dir: /compdir
357e46e7 13172 files.files[0].name: list0.h
c5aa993b 13173 files.files[0].dir: /srcdir
357e46e7 13174 files.files[1].name: list0.c
c5aa993b 13175 files.files[1].dir: /srcdir
c906108c
SS
13176
13177 The line number information for list0.c has to end up in a single
4f1520fb
FR
13178 subfile, so that `break /srcdir/list0.c:1' works as expected.
13179 start_subfile will ensure that this happens provided that we pass the
13180 concatenation of files.files[1].dir and files.files[1].name as the
13181 subfile's name. */
c906108c
SS
13182
13183static void
3e43a32a
MS
13184dwarf2_start_subfile (char *filename, const char *dirname,
13185 const char *comp_dir)
c906108c 13186{
4f1520fb
FR
13187 char *fullname;
13188
13189 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13190 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13191 second argument to start_subfile. To be consistent, we do the
13192 same here. In order not to lose the line information directory,
13193 we concatenate it to the filename when it makes sense.
13194 Note that the Dwarf3 standard says (speaking of filenames in line
13195 information): ``The directory index is ignored for file names
13196 that represent full path names''. Thus ignoring dirname in the
13197 `else' branch below isn't an issue. */
c906108c 13198
d5166ae1 13199 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
13200 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13201 else
13202 fullname = filename;
c906108c 13203
4f1520fb
FR
13204 start_subfile (fullname, comp_dir);
13205
13206 if (fullname != filename)
13207 xfree (fullname);
c906108c
SS
13208}
13209
4c2df51b
DJ
13210static void
13211var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 13212 struct dwarf2_cu *cu)
4c2df51b 13213{
e7c27a73
DJ
13214 struct objfile *objfile = cu->objfile;
13215 struct comp_unit_head *cu_header = &cu->header;
13216
4c2df51b
DJ
13217 /* NOTE drow/2003-01-30: There used to be a comment and some special
13218 code here to turn a symbol with DW_AT_external and a
13219 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13220 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13221 with some versions of binutils) where shared libraries could have
13222 relocations against symbols in their debug information - the
13223 minimal symbol would have the right address, but the debug info
13224 would not. It's no longer necessary, because we will explicitly
13225 apply relocations when we read in the debug information now. */
13226
13227 /* A DW_AT_location attribute with no contents indicates that a
13228 variable has been optimized away. */
13229 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13230 {
13231 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13232 return;
13233 }
13234
13235 /* Handle one degenerate form of location expression specially, to
13236 preserve GDB's previous behavior when section offsets are
3019eac3
DE
13237 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13238 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
13239
13240 if (attr_form_is_block (attr)
3019eac3
DE
13241 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13242 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13243 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13244 && (DW_BLOCK (attr)->size
13245 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 13246 {
891d2f0b 13247 unsigned int dummy;
4c2df51b 13248
3019eac3
DE
13249 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13250 SYMBOL_VALUE_ADDRESS (sym) =
13251 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13252 else
13253 SYMBOL_VALUE_ADDRESS (sym) =
13254 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 13255 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
13256 fixup_symbol_section (sym, objfile);
13257 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13258 SYMBOL_SECTION (sym));
4c2df51b
DJ
13259 return;
13260 }
13261
13262 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13263 expression evaluator, and use LOC_COMPUTED only when necessary
13264 (i.e. when the value of a register or memory location is
13265 referenced, or a thread-local block, etc.). Then again, it might
13266 not be worthwhile. I'm assuming that it isn't unless performance
13267 or memory numbers show me otherwise. */
13268
e7c27a73 13269 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 13270 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
13271
13272 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13273 cu->has_loclist = 1;
4c2df51b
DJ
13274}
13275
c906108c
SS
13276/* Given a pointer to a DWARF information entry, figure out if we need
13277 to make a symbol table entry for it, and if so, create a new entry
13278 and return a pointer to it.
13279 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
13280 used the passed type.
13281 If SPACE is not NULL, use it to hold the new symbol. If it is
13282 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
13283
13284static struct symbol *
34eaf542
TT
13285new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13286 struct symbol *space)
c906108c 13287{
e7c27a73 13288 struct objfile *objfile = cu->objfile;
c906108c
SS
13289 struct symbol *sym = NULL;
13290 char *name;
13291 struct attribute *attr = NULL;
13292 struct attribute *attr2 = NULL;
e142c38c 13293 CORE_ADDR baseaddr;
e37fd15a
SW
13294 struct pending **list_to_add = NULL;
13295
edb3359d 13296 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
13297
13298 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 13299
94af9270 13300 name = dwarf2_name (die, cu);
c906108c
SS
13301 if (name)
13302 {
94af9270 13303 const char *linkagename;
34eaf542 13304 int suppress_add = 0;
94af9270 13305
34eaf542
TT
13306 if (space)
13307 sym = space;
13308 else
13309 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 13310 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
13311
13312 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 13313 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
13314 linkagename = dwarf2_physname (name, die, cu);
13315 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 13316
f55ee35c
JK
13317 /* Fortran does not have mangling standard and the mangling does differ
13318 between gfortran, iFort etc. */
13319 if (cu->language == language_fortran
b250c185 13320 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
13321 symbol_set_demangled_name (&(sym->ginfo),
13322 (char *) dwarf2_full_name (name, die, cu),
13323 NULL);
f55ee35c 13324
c906108c 13325 /* Default assumptions.
c5aa993b 13326 Use the passed type or decode it from the die. */
176620f1 13327 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 13328 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
13329 if (type != NULL)
13330 SYMBOL_TYPE (sym) = type;
13331 else
e7c27a73 13332 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
13333 attr = dwarf2_attr (die,
13334 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13335 cu);
c906108c
SS
13336 if (attr)
13337 {
13338 SYMBOL_LINE (sym) = DW_UNSND (attr);
13339 }
cb1df416 13340
edb3359d
DJ
13341 attr = dwarf2_attr (die,
13342 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13343 cu);
cb1df416
DJ
13344 if (attr)
13345 {
13346 int file_index = DW_UNSND (attr);
9a619af0 13347
cb1df416
DJ
13348 if (cu->line_header == NULL
13349 || file_index > cu->line_header->num_file_names)
13350 complaint (&symfile_complaints,
13351 _("file index out of range"));
1c3d648d 13352 else if (file_index > 0)
cb1df416
DJ
13353 {
13354 struct file_entry *fe;
9a619af0 13355
cb1df416
DJ
13356 fe = &cu->line_header->file_names[file_index - 1];
13357 SYMBOL_SYMTAB (sym) = fe->symtab;
13358 }
13359 }
13360
c906108c
SS
13361 switch (die->tag)
13362 {
13363 case DW_TAG_label:
e142c38c 13364 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
13365 if (attr)
13366 {
13367 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13368 }
0f5238ed
TT
13369 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13370 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 13371 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 13372 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
13373 break;
13374 case DW_TAG_subprogram:
13375 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13376 finish_block. */
13377 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 13378 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
13379 if ((attr2 && (DW_UNSND (attr2) != 0))
13380 || cu->language == language_ada)
c906108c 13381 {
2cfa0c8d
JB
13382 /* Subprograms marked external are stored as a global symbol.
13383 Ada subprograms, whether marked external or not, are always
13384 stored as a global symbol, because we want to be able to
13385 access them globally. For instance, we want to be able
13386 to break on a nested subprogram without having to
13387 specify the context. */
e37fd15a 13388 list_to_add = &global_symbols;
c906108c
SS
13389 }
13390 else
13391 {
e37fd15a 13392 list_to_add = cu->list_in_scope;
c906108c
SS
13393 }
13394 break;
edb3359d
DJ
13395 case DW_TAG_inlined_subroutine:
13396 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13397 finish_block. */
13398 SYMBOL_CLASS (sym) = LOC_BLOCK;
13399 SYMBOL_INLINED (sym) = 1;
481860b3 13400 list_to_add = cu->list_in_scope;
edb3359d 13401 break;
34eaf542
TT
13402 case DW_TAG_template_value_param:
13403 suppress_add = 1;
13404 /* Fall through. */
72929c62 13405 case DW_TAG_constant:
c906108c 13406 case DW_TAG_variable:
254e6b9e 13407 case DW_TAG_member:
0963b4bd
MS
13408 /* Compilation with minimal debug info may result in
13409 variables with missing type entries. Change the
13410 misleading `void' type to something sensible. */
c906108c 13411 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 13412 SYMBOL_TYPE (sym)
46bf5051 13413 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 13414
e142c38c 13415 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
13416 /* In the case of DW_TAG_member, we should only be called for
13417 static const members. */
13418 if (die->tag == DW_TAG_member)
13419 {
3863f96c
DE
13420 /* dwarf2_add_field uses die_is_declaration,
13421 so we do the same. */
254e6b9e
DE
13422 gdb_assert (die_is_declaration (die, cu));
13423 gdb_assert (attr);
13424 }
c906108c
SS
13425 if (attr)
13426 {
e7c27a73 13427 dwarf2_const_value (attr, sym, cu);
e142c38c 13428 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 13429 if (!suppress_add)
34eaf542
TT
13430 {
13431 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 13432 list_to_add = &global_symbols;
34eaf542 13433 else
e37fd15a 13434 list_to_add = cu->list_in_scope;
34eaf542 13435 }
c906108c
SS
13436 break;
13437 }
e142c38c 13438 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13439 if (attr)
13440 {
e7c27a73 13441 var_decode_location (attr, sym, cu);
e142c38c 13442 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
13443 if (SYMBOL_CLASS (sym) == LOC_STATIC
13444 && SYMBOL_VALUE_ADDRESS (sym) == 0
13445 && !dwarf2_per_objfile->has_section_at_zero)
13446 {
13447 /* When a static variable is eliminated by the linker,
13448 the corresponding debug information is not stripped
13449 out, but the variable address is set to null;
13450 do not add such variables into symbol table. */
13451 }
13452 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 13453 {
f55ee35c
JK
13454 /* Workaround gfortran PR debug/40040 - it uses
13455 DW_AT_location for variables in -fPIC libraries which may
13456 get overriden by other libraries/executable and get
13457 a different address. Resolve it by the minimal symbol
13458 which may come from inferior's executable using copy
13459 relocation. Make this workaround only for gfortran as for
13460 other compilers GDB cannot guess the minimal symbol
13461 Fortran mangling kind. */
13462 if (cu->language == language_fortran && die->parent
13463 && die->parent->tag == DW_TAG_module
13464 && cu->producer
13465 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13466 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13467
1c809c68
TT
13468 /* A variable with DW_AT_external is never static,
13469 but it may be block-scoped. */
13470 list_to_add = (cu->list_in_scope == &file_symbols
13471 ? &global_symbols : cu->list_in_scope);
1c809c68 13472 }
c906108c 13473 else
e37fd15a 13474 list_to_add = cu->list_in_scope;
c906108c
SS
13475 }
13476 else
13477 {
13478 /* We do not know the address of this symbol.
c5aa993b
JM
13479 If it is an external symbol and we have type information
13480 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13481 The address of the variable will then be determined from
13482 the minimal symbol table whenever the variable is
13483 referenced. */
e142c38c 13484 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 13485 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 13486 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 13487 {
0fe7935b
DJ
13488 /* A variable with DW_AT_external is never static, but it
13489 may be block-scoped. */
13490 list_to_add = (cu->list_in_scope == &file_symbols
13491 ? &global_symbols : cu->list_in_scope);
13492
c906108c 13493 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 13494 }
442ddf59
JK
13495 else if (!die_is_declaration (die, cu))
13496 {
13497 /* Use the default LOC_OPTIMIZED_OUT class. */
13498 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
13499 if (!suppress_add)
13500 list_to_add = cu->list_in_scope;
442ddf59 13501 }
c906108c
SS
13502 }
13503 break;
13504 case DW_TAG_formal_parameter:
edb3359d
DJ
13505 /* If we are inside a function, mark this as an argument. If
13506 not, we might be looking at an argument to an inlined function
13507 when we do not have enough information to show inlined frames;
13508 pretend it's a local variable in that case so that the user can
13509 still see it. */
13510 if (context_stack_depth > 0
13511 && context_stack[context_stack_depth - 1].name != NULL)
13512 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 13513 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13514 if (attr)
13515 {
e7c27a73 13516 var_decode_location (attr, sym, cu);
c906108c 13517 }
e142c38c 13518 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13519 if (attr)
13520 {
e7c27a73 13521 dwarf2_const_value (attr, sym, cu);
c906108c 13522 }
f346a30d 13523
e37fd15a 13524 list_to_add = cu->list_in_scope;
c906108c
SS
13525 break;
13526 case DW_TAG_unspecified_parameters:
13527 /* From varargs functions; gdb doesn't seem to have any
13528 interest in this information, so just ignore it for now.
13529 (FIXME?) */
13530 break;
34eaf542
TT
13531 case DW_TAG_template_type_param:
13532 suppress_add = 1;
13533 /* Fall through. */
c906108c 13534 case DW_TAG_class_type:
680b30c7 13535 case DW_TAG_interface_type:
c906108c
SS
13536 case DW_TAG_structure_type:
13537 case DW_TAG_union_type:
72019c9c 13538 case DW_TAG_set_type:
c906108c
SS
13539 case DW_TAG_enumeration_type:
13540 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13541 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 13542
63d06c5c 13543 {
987504bb 13544 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
13545 really ever be static objects: otherwise, if you try
13546 to, say, break of a class's method and you're in a file
13547 which doesn't mention that class, it won't work unless
13548 the check for all static symbols in lookup_symbol_aux
13549 saves you. See the OtherFileClass tests in
13550 gdb.c++/namespace.exp. */
13551
e37fd15a 13552 if (!suppress_add)
34eaf542 13553 {
34eaf542
TT
13554 list_to_add = (cu->list_in_scope == &file_symbols
13555 && (cu->language == language_cplus
13556 || cu->language == language_java)
13557 ? &global_symbols : cu->list_in_scope);
63d06c5c 13558
64382290
TT
13559 /* The semantics of C++ state that "struct foo {
13560 ... }" also defines a typedef for "foo". A Java
13561 class declaration also defines a typedef for the
13562 class. */
13563 if (cu->language == language_cplus
13564 || cu->language == language_java
13565 || cu->language == language_ada)
13566 {
13567 /* The symbol's name is already allocated along
13568 with this objfile, so we don't need to
13569 duplicate it for the type. */
13570 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13571 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13572 }
63d06c5c
DC
13573 }
13574 }
c906108c
SS
13575 break;
13576 case DW_TAG_typedef:
63d06c5c
DC
13577 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13578 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13579 list_to_add = cu->list_in_scope;
63d06c5c 13580 break;
c906108c 13581 case DW_TAG_base_type:
a02abb62 13582 case DW_TAG_subrange_type:
c906108c 13583 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13584 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13585 list_to_add = cu->list_in_scope;
c906108c
SS
13586 break;
13587 case DW_TAG_enumerator:
e142c38c 13588 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13589 if (attr)
13590 {
e7c27a73 13591 dwarf2_const_value (attr, sym, cu);
c906108c 13592 }
63d06c5c
DC
13593 {
13594 /* NOTE: carlton/2003-11-10: See comment above in the
13595 DW_TAG_class_type, etc. block. */
13596
e142c38c 13597 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
13598 && (cu->language == language_cplus
13599 || cu->language == language_java)
e142c38c 13600 ? &global_symbols : cu->list_in_scope);
63d06c5c 13601 }
c906108c 13602 break;
5c4e30ca
DC
13603 case DW_TAG_namespace:
13604 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 13605 list_to_add = &global_symbols;
5c4e30ca 13606 break;
c906108c
SS
13607 default:
13608 /* Not a tag we recognize. Hopefully we aren't processing
13609 trash data, but since we must specifically ignore things
13610 we don't recognize, there is nothing else we should do at
0963b4bd 13611 this point. */
e2e0b3e5 13612 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 13613 dwarf_tag_name (die->tag));
c906108c
SS
13614 break;
13615 }
df8a16a1 13616
e37fd15a
SW
13617 if (suppress_add)
13618 {
13619 sym->hash_next = objfile->template_symbols;
13620 objfile->template_symbols = sym;
13621 list_to_add = NULL;
13622 }
13623
13624 if (list_to_add != NULL)
13625 add_symbol_to_list (sym, list_to_add);
13626
df8a16a1
DJ
13627 /* For the benefit of old versions of GCC, check for anonymous
13628 namespaces based on the demangled name. */
13629 if (!processing_has_namespace_info
94af9270 13630 && cu->language == language_cplus)
a10964d1 13631 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
13632 }
13633 return (sym);
13634}
13635
34eaf542
TT
13636/* A wrapper for new_symbol_full that always allocates a new symbol. */
13637
13638static struct symbol *
13639new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13640{
13641 return new_symbol_full (die, type, cu, NULL);
13642}
13643
98bfdba5
PA
13644/* Given an attr with a DW_FORM_dataN value in host byte order,
13645 zero-extend it as appropriate for the symbol's type. The DWARF
13646 standard (v4) is not entirely clear about the meaning of using
13647 DW_FORM_dataN for a constant with a signed type, where the type is
13648 wider than the data. The conclusion of a discussion on the DWARF
13649 list was that this is unspecified. We choose to always zero-extend
13650 because that is the interpretation long in use by GCC. */
c906108c 13651
98bfdba5
PA
13652static gdb_byte *
13653dwarf2_const_value_data (struct attribute *attr, struct type *type,
13654 const char *name, struct obstack *obstack,
12df843f 13655 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 13656{
e7c27a73 13657 struct objfile *objfile = cu->objfile;
e17a4113
UW
13658 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13659 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
13660 LONGEST l = DW_UNSND (attr);
13661
13662 if (bits < sizeof (*value) * 8)
13663 {
13664 l &= ((LONGEST) 1 << bits) - 1;
13665 *value = l;
13666 }
13667 else if (bits == sizeof (*value) * 8)
13668 *value = l;
13669 else
13670 {
13671 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13672 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13673 return bytes;
13674 }
13675
13676 return NULL;
13677}
13678
13679/* Read a constant value from an attribute. Either set *VALUE, or if
13680 the value does not fit in *VALUE, set *BYTES - either already
13681 allocated on the objfile obstack, or newly allocated on OBSTACK,
13682 or, set *BATON, if we translated the constant to a location
13683 expression. */
13684
13685static void
13686dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13687 const char *name, struct obstack *obstack,
13688 struct dwarf2_cu *cu,
12df843f 13689 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
13690 struct dwarf2_locexpr_baton **baton)
13691{
13692 struct objfile *objfile = cu->objfile;
13693 struct comp_unit_head *cu_header = &cu->header;
c906108c 13694 struct dwarf_block *blk;
98bfdba5
PA
13695 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13696 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13697
13698 *value = 0;
13699 *bytes = NULL;
13700 *baton = NULL;
c906108c
SS
13701
13702 switch (attr->form)
13703 {
13704 case DW_FORM_addr:
3019eac3 13705 case DW_FORM_GNU_addr_index:
ac56253d 13706 {
ac56253d
TT
13707 gdb_byte *data;
13708
98bfdba5
PA
13709 if (TYPE_LENGTH (type) != cu_header->addr_size)
13710 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 13711 cu_header->addr_size,
98bfdba5 13712 TYPE_LENGTH (type));
ac56253d
TT
13713 /* Symbols of this form are reasonably rare, so we just
13714 piggyback on the existing location code rather than writing
13715 a new implementation of symbol_computed_ops. */
98bfdba5
PA
13716 *baton = obstack_alloc (&objfile->objfile_obstack,
13717 sizeof (struct dwarf2_locexpr_baton));
13718 (*baton)->per_cu = cu->per_cu;
13719 gdb_assert ((*baton)->per_cu);
ac56253d 13720
98bfdba5
PA
13721 (*baton)->size = 2 + cu_header->addr_size;
13722 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13723 (*baton)->data = data;
ac56253d
TT
13724
13725 data[0] = DW_OP_addr;
13726 store_unsigned_integer (&data[1], cu_header->addr_size,
13727 byte_order, DW_ADDR (attr));
13728 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 13729 }
c906108c 13730 break;
4ac36638 13731 case DW_FORM_string:
93b5768b 13732 case DW_FORM_strp:
3019eac3 13733 case DW_FORM_GNU_str_index:
98bfdba5
PA
13734 /* DW_STRING is already allocated on the objfile obstack, point
13735 directly to it. */
13736 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 13737 break;
c906108c
SS
13738 case DW_FORM_block1:
13739 case DW_FORM_block2:
13740 case DW_FORM_block4:
13741 case DW_FORM_block:
2dc7f7b3 13742 case DW_FORM_exprloc:
c906108c 13743 blk = DW_BLOCK (attr);
98bfdba5
PA
13744 if (TYPE_LENGTH (type) != blk->size)
13745 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13746 TYPE_LENGTH (type));
13747 *bytes = blk->data;
c906108c 13748 break;
2df3850c
JM
13749
13750 /* The DW_AT_const_value attributes are supposed to carry the
13751 symbol's value "represented as it would be on the target
13752 architecture." By the time we get here, it's already been
13753 converted to host endianness, so we just need to sign- or
13754 zero-extend it as appropriate. */
13755 case DW_FORM_data1:
3e43a32a
MS
13756 *bytes = dwarf2_const_value_data (attr, type, name,
13757 obstack, cu, value, 8);
2df3850c 13758 break;
c906108c 13759 case DW_FORM_data2:
3e43a32a
MS
13760 *bytes = dwarf2_const_value_data (attr, type, name,
13761 obstack, cu, value, 16);
2df3850c 13762 break;
c906108c 13763 case DW_FORM_data4:
3e43a32a
MS
13764 *bytes = dwarf2_const_value_data (attr, type, name,
13765 obstack, cu, value, 32);
2df3850c 13766 break;
c906108c 13767 case DW_FORM_data8:
3e43a32a
MS
13768 *bytes = dwarf2_const_value_data (attr, type, name,
13769 obstack, cu, value, 64);
2df3850c
JM
13770 break;
13771
c906108c 13772 case DW_FORM_sdata:
98bfdba5 13773 *value = DW_SND (attr);
2df3850c
JM
13774 break;
13775
c906108c 13776 case DW_FORM_udata:
98bfdba5 13777 *value = DW_UNSND (attr);
c906108c 13778 break;
2df3850c 13779
c906108c 13780 default:
4d3c2250 13781 complaint (&symfile_complaints,
e2e0b3e5 13782 _("unsupported const value attribute form: '%s'"),
4d3c2250 13783 dwarf_form_name (attr->form));
98bfdba5 13784 *value = 0;
c906108c
SS
13785 break;
13786 }
13787}
13788
2df3850c 13789
98bfdba5
PA
13790/* Copy constant value from an attribute to a symbol. */
13791
2df3850c 13792static void
98bfdba5
PA
13793dwarf2_const_value (struct attribute *attr, struct symbol *sym,
13794 struct dwarf2_cu *cu)
2df3850c 13795{
98bfdba5
PA
13796 struct objfile *objfile = cu->objfile;
13797 struct comp_unit_head *cu_header = &cu->header;
12df843f 13798 LONGEST value;
98bfdba5
PA
13799 gdb_byte *bytes;
13800 struct dwarf2_locexpr_baton *baton;
2df3850c 13801
98bfdba5
PA
13802 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
13803 SYMBOL_PRINT_NAME (sym),
13804 &objfile->objfile_obstack, cu,
13805 &value, &bytes, &baton);
2df3850c 13806
98bfdba5
PA
13807 if (baton != NULL)
13808 {
13809 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13810 SYMBOL_LOCATION_BATON (sym) = baton;
13811 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13812 }
13813 else if (bytes != NULL)
13814 {
13815 SYMBOL_VALUE_BYTES (sym) = bytes;
13816 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
13817 }
13818 else
13819 {
13820 SYMBOL_VALUE (sym) = value;
13821 SYMBOL_CLASS (sym) = LOC_CONST;
13822 }
2df3850c
JM
13823}
13824
c906108c
SS
13825/* Return the type of the die in question using its DW_AT_type attribute. */
13826
13827static struct type *
e7c27a73 13828die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13829{
c906108c 13830 struct attribute *type_attr;
c906108c 13831
e142c38c 13832 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
13833 if (!type_attr)
13834 {
13835 /* A missing DW_AT_type represents a void type. */
46bf5051 13836 return objfile_type (cu->objfile)->builtin_void;
c906108c 13837 }
348e048f 13838
673bfd45 13839 return lookup_die_type (die, type_attr, cu);
c906108c
SS
13840}
13841
b4ba55a1
JB
13842/* True iff CU's producer generates GNAT Ada auxiliary information
13843 that allows to find parallel types through that information instead
13844 of having to do expensive parallel lookups by type name. */
13845
13846static int
13847need_gnat_info (struct dwarf2_cu *cu)
13848{
13849 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
13850 of GNAT produces this auxiliary information, without any indication
13851 that it is produced. Part of enhancing the FSF version of GNAT
13852 to produce that information will be to put in place an indicator
13853 that we can use in order to determine whether the descriptive type
13854 info is available or not. One suggestion that has been made is
13855 to use a new attribute, attached to the CU die. For now, assume
13856 that the descriptive type info is not available. */
13857 return 0;
13858}
13859
b4ba55a1
JB
13860/* Return the auxiliary type of the die in question using its
13861 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
13862 attribute is not present. */
13863
13864static struct type *
13865die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
13866{
b4ba55a1 13867 struct attribute *type_attr;
b4ba55a1
JB
13868
13869 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
13870 if (!type_attr)
13871 return NULL;
13872
673bfd45 13873 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
13874}
13875
13876/* If DIE has a descriptive_type attribute, then set the TYPE's
13877 descriptive type accordingly. */
13878
13879static void
13880set_descriptive_type (struct type *type, struct die_info *die,
13881 struct dwarf2_cu *cu)
13882{
13883 struct type *descriptive_type = die_descriptive_type (die, cu);
13884
13885 if (descriptive_type)
13886 {
13887 ALLOCATE_GNAT_AUX_TYPE (type);
13888 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
13889 }
13890}
13891
c906108c
SS
13892/* Return the containing type of the die in question using its
13893 DW_AT_containing_type attribute. */
13894
13895static struct type *
e7c27a73 13896die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13897{
c906108c 13898 struct attribute *type_attr;
c906108c 13899
e142c38c 13900 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
13901 if (!type_attr)
13902 error (_("Dwarf Error: Problem turning containing type into gdb type "
13903 "[in module %s]"), cu->objfile->name);
13904
673bfd45 13905 return lookup_die_type (die, type_attr, cu);
c906108c
SS
13906}
13907
673bfd45
DE
13908/* Look up the type of DIE in CU using its type attribute ATTR.
13909 If there is no type substitute an error marker. */
13910
c906108c 13911static struct type *
673bfd45
DE
13912lookup_die_type (struct die_info *die, struct attribute *attr,
13913 struct dwarf2_cu *cu)
c906108c 13914{
bb5ed363 13915 struct objfile *objfile = cu->objfile;
f792889a
DJ
13916 struct type *this_type;
13917
673bfd45
DE
13918 /* First see if we have it cached. */
13919
13920 if (is_ref_attr (attr))
13921 {
b64f50a1 13922 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
13923
13924 this_type = get_die_type_at_offset (offset, cu->per_cu);
13925 }
55f1336d 13926 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
13927 {
13928 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
13929
13930 /* sig_type will be NULL if the signatured type is missing from
13931 the debug info. */
13932 if (sig_type == NULL)
13933 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13934 "at 0x%x [in module %s]"),
b64f50a1 13935 die->offset.sect_off, objfile->name);
673bfd45 13936
3019eac3
DE
13937 gdb_assert (sig_type->per_cu.is_debug_types);
13938 /* If we haven't filled in type_offset_in_section yet, then we
13939 haven't read the type in yet. */
13940 this_type = NULL;
13941 if (sig_type->type_offset_in_section.sect_off != 0)
13942 {
13943 this_type =
13944 get_die_type_at_offset (sig_type->type_offset_in_section,
13945 &sig_type->per_cu);
13946 }
673bfd45
DE
13947 }
13948 else
13949 {
13950 dump_die_for_error (die);
13951 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 13952 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
13953 }
13954
13955 /* If not cached we need to read it in. */
13956
13957 if (this_type == NULL)
13958 {
13959 struct die_info *type_die;
13960 struct dwarf2_cu *type_cu = cu;
13961
13962 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
13963 /* If we found the type now, it's probably because the type came
13964 from an inter-CU reference and the type's CU got expanded before
13965 ours. */
13966 this_type = get_die_type (type_die, type_cu);
13967 if (this_type == NULL)
13968 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
13969 }
13970
13971 /* If we still don't have a type use an error marker. */
13972
13973 if (this_type == NULL)
c906108c 13974 {
b00fdb78
TT
13975 char *message, *saved;
13976
13977 /* read_type_die already issued a complaint. */
13978 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 13979 objfile->name,
b64f50a1
JK
13980 cu->header.offset.sect_off,
13981 die->offset.sect_off);
bb5ed363 13982 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
13983 message, strlen (message));
13984 xfree (message);
13985
bb5ed363 13986 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 13987 }
673bfd45 13988
f792889a 13989 return this_type;
c906108c
SS
13990}
13991
673bfd45
DE
13992/* Return the type in DIE, CU.
13993 Returns NULL for invalid types.
13994
13995 This first does a lookup in the appropriate type_hash table,
13996 and only reads the die in if necessary.
13997
13998 NOTE: This can be called when reading in partial or full symbols. */
13999
f792889a 14000static struct type *
e7c27a73 14001read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14002{
f792889a
DJ
14003 struct type *this_type;
14004
14005 this_type = get_die_type (die, cu);
14006 if (this_type)
14007 return this_type;
14008
673bfd45
DE
14009 return read_type_die_1 (die, cu);
14010}
14011
14012/* Read the type in DIE, CU.
14013 Returns NULL for invalid types. */
14014
14015static struct type *
14016read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
14017{
14018 struct type *this_type = NULL;
14019
c906108c
SS
14020 switch (die->tag)
14021 {
14022 case DW_TAG_class_type:
680b30c7 14023 case DW_TAG_interface_type:
c906108c
SS
14024 case DW_TAG_structure_type:
14025 case DW_TAG_union_type:
f792889a 14026 this_type = read_structure_type (die, cu);
c906108c
SS
14027 break;
14028 case DW_TAG_enumeration_type:
f792889a 14029 this_type = read_enumeration_type (die, cu);
c906108c
SS
14030 break;
14031 case DW_TAG_subprogram:
14032 case DW_TAG_subroutine_type:
edb3359d 14033 case DW_TAG_inlined_subroutine:
f792889a 14034 this_type = read_subroutine_type (die, cu);
c906108c
SS
14035 break;
14036 case DW_TAG_array_type:
f792889a 14037 this_type = read_array_type (die, cu);
c906108c 14038 break;
72019c9c 14039 case DW_TAG_set_type:
f792889a 14040 this_type = read_set_type (die, cu);
72019c9c 14041 break;
c906108c 14042 case DW_TAG_pointer_type:
f792889a 14043 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
14044 break;
14045 case DW_TAG_ptr_to_member_type:
f792889a 14046 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
14047 break;
14048 case DW_TAG_reference_type:
f792889a 14049 this_type = read_tag_reference_type (die, cu);
c906108c
SS
14050 break;
14051 case DW_TAG_const_type:
f792889a 14052 this_type = read_tag_const_type (die, cu);
c906108c
SS
14053 break;
14054 case DW_TAG_volatile_type:
f792889a 14055 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
14056 break;
14057 case DW_TAG_string_type:
f792889a 14058 this_type = read_tag_string_type (die, cu);
c906108c
SS
14059 break;
14060 case DW_TAG_typedef:
f792889a 14061 this_type = read_typedef (die, cu);
c906108c 14062 break;
a02abb62 14063 case DW_TAG_subrange_type:
f792889a 14064 this_type = read_subrange_type (die, cu);
a02abb62 14065 break;
c906108c 14066 case DW_TAG_base_type:
f792889a 14067 this_type = read_base_type (die, cu);
c906108c 14068 break;
81a17f79 14069 case DW_TAG_unspecified_type:
f792889a 14070 this_type = read_unspecified_type (die, cu);
81a17f79 14071 break;
0114d602
DJ
14072 case DW_TAG_namespace:
14073 this_type = read_namespace_type (die, cu);
14074 break;
f55ee35c
JK
14075 case DW_TAG_module:
14076 this_type = read_module_type (die, cu);
14077 break;
c906108c 14078 default:
3e43a32a
MS
14079 complaint (&symfile_complaints,
14080 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 14081 dwarf_tag_name (die->tag));
c906108c
SS
14082 break;
14083 }
63d06c5c 14084
f792889a 14085 return this_type;
63d06c5c
DC
14086}
14087
abc72ce4
DE
14088/* See if we can figure out if the class lives in a namespace. We do
14089 this by looking for a member function; its demangled name will
14090 contain namespace info, if there is any.
14091 Return the computed name or NULL.
14092 Space for the result is allocated on the objfile's obstack.
14093 This is the full-die version of guess_partial_die_structure_name.
14094 In this case we know DIE has no useful parent. */
14095
14096static char *
14097guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14098{
14099 struct die_info *spec_die;
14100 struct dwarf2_cu *spec_cu;
14101 struct die_info *child;
14102
14103 spec_cu = cu;
14104 spec_die = die_specification (die, &spec_cu);
14105 if (spec_die != NULL)
14106 {
14107 die = spec_die;
14108 cu = spec_cu;
14109 }
14110
14111 for (child = die->child;
14112 child != NULL;
14113 child = child->sibling)
14114 {
14115 if (child->tag == DW_TAG_subprogram)
14116 {
14117 struct attribute *attr;
14118
14119 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14120 if (attr == NULL)
14121 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14122 if (attr != NULL)
14123 {
14124 char *actual_name
14125 = language_class_name_from_physname (cu->language_defn,
14126 DW_STRING (attr));
14127 char *name = NULL;
14128
14129 if (actual_name != NULL)
14130 {
14131 char *die_name = dwarf2_name (die, cu);
14132
14133 if (die_name != NULL
14134 && strcmp (die_name, actual_name) != 0)
14135 {
14136 /* Strip off the class name from the full name.
14137 We want the prefix. */
14138 int die_name_len = strlen (die_name);
14139 int actual_name_len = strlen (actual_name);
14140
14141 /* Test for '::' as a sanity check. */
14142 if (actual_name_len > die_name_len + 2
3e43a32a
MS
14143 && actual_name[actual_name_len
14144 - die_name_len - 1] == ':')
abc72ce4
DE
14145 name =
14146 obsavestring (actual_name,
14147 actual_name_len - die_name_len - 2,
14148 &cu->objfile->objfile_obstack);
14149 }
14150 }
14151 xfree (actual_name);
14152 return name;
14153 }
14154 }
14155 }
14156
14157 return NULL;
14158}
14159
96408a79
SA
14160/* GCC might emit a nameless typedef that has a linkage name. Determine the
14161 prefix part in such case. See
14162 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14163
14164static char *
14165anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14166{
14167 struct attribute *attr;
14168 char *base;
14169
14170 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14171 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14172 return NULL;
14173
14174 attr = dwarf2_attr (die, DW_AT_name, cu);
14175 if (attr != NULL && DW_STRING (attr) != NULL)
14176 return NULL;
14177
14178 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14179 if (attr == NULL)
14180 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14181 if (attr == NULL || DW_STRING (attr) == NULL)
14182 return NULL;
14183
14184 /* dwarf2_name had to be already called. */
14185 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14186
14187 /* Strip the base name, keep any leading namespaces/classes. */
14188 base = strrchr (DW_STRING (attr), ':');
14189 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14190 return "";
14191
14192 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14193 &cu->objfile->objfile_obstack);
14194}
14195
fdde2d81 14196/* Return the name of the namespace/class that DIE is defined within,
0114d602 14197 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 14198
0114d602
DJ
14199 For example, if we're within the method foo() in the following
14200 code:
14201
14202 namespace N {
14203 class C {
14204 void foo () {
14205 }
14206 };
14207 }
14208
14209 then determine_prefix on foo's die will return "N::C". */
fdde2d81 14210
0d5cff50 14211static const char *
e142c38c 14212determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 14213{
0114d602
DJ
14214 struct die_info *parent, *spec_die;
14215 struct dwarf2_cu *spec_cu;
14216 struct type *parent_type;
96408a79 14217 char *retval;
63d06c5c 14218
f55ee35c
JK
14219 if (cu->language != language_cplus && cu->language != language_java
14220 && cu->language != language_fortran)
0114d602
DJ
14221 return "";
14222
96408a79
SA
14223 retval = anonymous_struct_prefix (die, cu);
14224 if (retval)
14225 return retval;
14226
0114d602
DJ
14227 /* We have to be careful in the presence of DW_AT_specification.
14228 For example, with GCC 3.4, given the code
14229
14230 namespace N {
14231 void foo() {
14232 // Definition of N::foo.
14233 }
14234 }
14235
14236 then we'll have a tree of DIEs like this:
14237
14238 1: DW_TAG_compile_unit
14239 2: DW_TAG_namespace // N
14240 3: DW_TAG_subprogram // declaration of N::foo
14241 4: DW_TAG_subprogram // definition of N::foo
14242 DW_AT_specification // refers to die #3
14243
14244 Thus, when processing die #4, we have to pretend that we're in
14245 the context of its DW_AT_specification, namely the contex of die
14246 #3. */
14247 spec_cu = cu;
14248 spec_die = die_specification (die, &spec_cu);
14249 if (spec_die == NULL)
14250 parent = die->parent;
14251 else
63d06c5c 14252 {
0114d602
DJ
14253 parent = spec_die->parent;
14254 cu = spec_cu;
63d06c5c 14255 }
0114d602
DJ
14256
14257 if (parent == NULL)
14258 return "";
98bfdba5
PA
14259 else if (parent->building_fullname)
14260 {
14261 const char *name;
14262 const char *parent_name;
14263
14264 /* It has been seen on RealView 2.2 built binaries,
14265 DW_TAG_template_type_param types actually _defined_ as
14266 children of the parent class:
14267
14268 enum E {};
14269 template class <class Enum> Class{};
14270 Class<enum E> class_e;
14271
14272 1: DW_TAG_class_type (Class)
14273 2: DW_TAG_enumeration_type (E)
14274 3: DW_TAG_enumerator (enum1:0)
14275 3: DW_TAG_enumerator (enum2:1)
14276 ...
14277 2: DW_TAG_template_type_param
14278 DW_AT_type DW_FORM_ref_udata (E)
14279
14280 Besides being broken debug info, it can put GDB into an
14281 infinite loop. Consider:
14282
14283 When we're building the full name for Class<E>, we'll start
14284 at Class, and go look over its template type parameters,
14285 finding E. We'll then try to build the full name of E, and
14286 reach here. We're now trying to build the full name of E,
14287 and look over the parent DIE for containing scope. In the
14288 broken case, if we followed the parent DIE of E, we'd again
14289 find Class, and once again go look at its template type
14290 arguments, etc., etc. Simply don't consider such parent die
14291 as source-level parent of this die (it can't be, the language
14292 doesn't allow it), and break the loop here. */
14293 name = dwarf2_name (die, cu);
14294 parent_name = dwarf2_name (parent, cu);
14295 complaint (&symfile_complaints,
14296 _("template param type '%s' defined within parent '%s'"),
14297 name ? name : "<unknown>",
14298 parent_name ? parent_name : "<unknown>");
14299 return "";
14300 }
63d06c5c 14301 else
0114d602
DJ
14302 switch (parent->tag)
14303 {
63d06c5c 14304 case DW_TAG_namespace:
0114d602 14305 parent_type = read_type_die (parent, cu);
acebe513
UW
14306 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14307 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14308 Work around this problem here. */
14309 if (cu->language == language_cplus
14310 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14311 return "";
0114d602
DJ
14312 /* We give a name to even anonymous namespaces. */
14313 return TYPE_TAG_NAME (parent_type);
63d06c5c 14314 case DW_TAG_class_type:
680b30c7 14315 case DW_TAG_interface_type:
63d06c5c 14316 case DW_TAG_structure_type:
0114d602 14317 case DW_TAG_union_type:
f55ee35c 14318 case DW_TAG_module:
0114d602
DJ
14319 parent_type = read_type_die (parent, cu);
14320 if (TYPE_TAG_NAME (parent_type) != NULL)
14321 return TYPE_TAG_NAME (parent_type);
14322 else
14323 /* An anonymous structure is only allowed non-static data
14324 members; no typedefs, no member functions, et cetera.
14325 So it does not need a prefix. */
14326 return "";
abc72ce4 14327 case DW_TAG_compile_unit:
95554aad 14328 case DW_TAG_partial_unit:
abc72ce4
DE
14329 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14330 if (cu->language == language_cplus
8b70b953 14331 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14332 && die->child != NULL
14333 && (die->tag == DW_TAG_class_type
14334 || die->tag == DW_TAG_structure_type
14335 || die->tag == DW_TAG_union_type))
14336 {
14337 char *name = guess_full_die_structure_name (die, cu);
14338 if (name != NULL)
14339 return name;
14340 }
14341 return "";
63d06c5c 14342 default:
8176b9b8 14343 return determine_prefix (parent, cu);
63d06c5c 14344 }
63d06c5c
DC
14345}
14346
3e43a32a
MS
14347/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14348 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14349 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14350 an obconcat, otherwise allocate storage for the result. The CU argument is
14351 used to determine the language and hence, the appropriate separator. */
987504bb 14352
f55ee35c 14353#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
14354
14355static char *
f55ee35c
JK
14356typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14357 int physname, struct dwarf2_cu *cu)
63d06c5c 14358{
f55ee35c 14359 const char *lead = "";
5c315b68 14360 const char *sep;
63d06c5c 14361
3e43a32a
MS
14362 if (suffix == NULL || suffix[0] == '\0'
14363 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
14364 sep = "";
14365 else if (cu->language == language_java)
14366 sep = ".";
f55ee35c
JK
14367 else if (cu->language == language_fortran && physname)
14368 {
14369 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14370 DW_AT_MIPS_linkage_name is preferred and used instead. */
14371
14372 lead = "__";
14373 sep = "_MOD_";
14374 }
987504bb
JJ
14375 else
14376 sep = "::";
63d06c5c 14377
6dd47d34
DE
14378 if (prefix == NULL)
14379 prefix = "";
14380 if (suffix == NULL)
14381 suffix = "";
14382
987504bb
JJ
14383 if (obs == NULL)
14384 {
3e43a32a
MS
14385 char *retval
14386 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 14387
f55ee35c
JK
14388 strcpy (retval, lead);
14389 strcat (retval, prefix);
6dd47d34
DE
14390 strcat (retval, sep);
14391 strcat (retval, suffix);
63d06c5c
DC
14392 return retval;
14393 }
987504bb
JJ
14394 else
14395 {
14396 /* We have an obstack. */
f55ee35c 14397 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 14398 }
63d06c5c
DC
14399}
14400
c906108c
SS
14401/* Return sibling of die, NULL if no sibling. */
14402
f9aca02d 14403static struct die_info *
fba45db2 14404sibling_die (struct die_info *die)
c906108c 14405{
639d11d3 14406 return die->sibling;
c906108c
SS
14407}
14408
71c25dea
TT
14409/* Get name of a die, return NULL if not found. */
14410
14411static char *
14412dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14413 struct obstack *obstack)
14414{
14415 if (name && cu->language == language_cplus)
14416 {
14417 char *canon_name = cp_canonicalize_string (name);
14418
14419 if (canon_name != NULL)
14420 {
14421 if (strcmp (canon_name, name) != 0)
14422 name = obsavestring (canon_name, strlen (canon_name),
14423 obstack);
14424 xfree (canon_name);
14425 }
14426 }
14427
14428 return name;
c906108c
SS
14429}
14430
9219021c
DC
14431/* Get name of a die, return NULL if not found. */
14432
14433static char *
e142c38c 14434dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
14435{
14436 struct attribute *attr;
14437
e142c38c 14438 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
14439 if ((!attr || !DW_STRING (attr))
14440 && die->tag != DW_TAG_class_type
14441 && die->tag != DW_TAG_interface_type
14442 && die->tag != DW_TAG_structure_type
14443 && die->tag != DW_TAG_union_type)
71c25dea
TT
14444 return NULL;
14445
14446 switch (die->tag)
14447 {
14448 case DW_TAG_compile_unit:
95554aad 14449 case DW_TAG_partial_unit:
71c25dea
TT
14450 /* Compilation units have a DW_AT_name that is a filename, not
14451 a source language identifier. */
14452 case DW_TAG_enumeration_type:
14453 case DW_TAG_enumerator:
14454 /* These tags always have simple identifiers already; no need
14455 to canonicalize them. */
14456 return DW_STRING (attr);
907af001 14457
418835cc
KS
14458 case DW_TAG_subprogram:
14459 /* Java constructors will all be named "<init>", so return
14460 the class name when we see this special case. */
14461 if (cu->language == language_java
14462 && DW_STRING (attr) != NULL
14463 && strcmp (DW_STRING (attr), "<init>") == 0)
14464 {
14465 struct dwarf2_cu *spec_cu = cu;
14466 struct die_info *spec_die;
14467
14468 /* GCJ will output '<init>' for Java constructor names.
14469 For this special case, return the name of the parent class. */
14470
14471 /* GCJ may output suprogram DIEs with AT_specification set.
14472 If so, use the name of the specified DIE. */
14473 spec_die = die_specification (die, &spec_cu);
14474 if (spec_die != NULL)
14475 return dwarf2_name (spec_die, spec_cu);
14476
14477 do
14478 {
14479 die = die->parent;
14480 if (die->tag == DW_TAG_class_type)
14481 return dwarf2_name (die, cu);
14482 }
95554aad
TT
14483 while (die->tag != DW_TAG_compile_unit
14484 && die->tag != DW_TAG_partial_unit);
418835cc 14485 }
907af001
UW
14486 break;
14487
14488 case DW_TAG_class_type:
14489 case DW_TAG_interface_type:
14490 case DW_TAG_structure_type:
14491 case DW_TAG_union_type:
14492 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14493 structures or unions. These were of the form "._%d" in GCC 4.1,
14494 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14495 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
14496 if (attr && DW_STRING (attr)
14497 && (strncmp (DW_STRING (attr), "._", 2) == 0
14498 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 14499 return NULL;
53832f31
TT
14500
14501 /* GCC might emit a nameless typedef that has a linkage name. See
14502 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14503 if (!attr || DW_STRING (attr) == NULL)
14504 {
df5c6c50 14505 char *demangled = NULL;
53832f31
TT
14506
14507 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14508 if (attr == NULL)
14509 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14510
14511 if (attr == NULL || DW_STRING (attr) == NULL)
14512 return NULL;
14513
df5c6c50
JK
14514 /* Avoid demangling DW_STRING (attr) the second time on a second
14515 call for the same DIE. */
14516 if (!DW_STRING_IS_CANONICAL (attr))
14517 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
14518
14519 if (demangled)
14520 {
96408a79
SA
14521 char *base;
14522
53832f31 14523 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
14524 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14525 &cu->objfile->objfile_obstack);
53832f31
TT
14526 DW_STRING_IS_CANONICAL (attr) = 1;
14527 xfree (demangled);
96408a79
SA
14528
14529 /* Strip any leading namespaces/classes, keep only the base name.
14530 DW_AT_name for named DIEs does not contain the prefixes. */
14531 base = strrchr (DW_STRING (attr), ':');
14532 if (base && base > DW_STRING (attr) && base[-1] == ':')
14533 return &base[1];
14534 else
14535 return DW_STRING (attr);
53832f31
TT
14536 }
14537 }
907af001
UW
14538 break;
14539
71c25dea 14540 default:
907af001
UW
14541 break;
14542 }
14543
14544 if (!DW_STRING_IS_CANONICAL (attr))
14545 {
14546 DW_STRING (attr)
14547 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14548 &cu->objfile->objfile_obstack);
14549 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 14550 }
907af001 14551 return DW_STRING (attr);
9219021c
DC
14552}
14553
14554/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
14555 is none. *EXT_CU is the CU containing DIE on input, and the CU
14556 containing the return value on output. */
9219021c
DC
14557
14558static struct die_info *
f2f0e013 14559dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
14560{
14561 struct attribute *attr;
9219021c 14562
f2f0e013 14563 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
14564 if (attr == NULL)
14565 return NULL;
14566
f2f0e013 14567 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
14568}
14569
c906108c
SS
14570/* Convert a DIE tag into its string name. */
14571
f39c6ffd 14572static const char *
aa1ee363 14573dwarf_tag_name (unsigned tag)
c906108c 14574{
f39c6ffd
TT
14575 const char *name = get_DW_TAG_name (tag);
14576
14577 if (name == NULL)
14578 return "DW_TAG_<unknown>";
14579
14580 return name;
c906108c
SS
14581}
14582
14583/* Convert a DWARF attribute code into its string name. */
14584
f39c6ffd 14585static const char *
aa1ee363 14586dwarf_attr_name (unsigned attr)
c906108c 14587{
f39c6ffd
TT
14588 const char *name;
14589
c764a876 14590#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
14591 if (attr == DW_AT_MIPS_fde)
14592 return "DW_AT_MIPS_fde";
14593#else
14594 if (attr == DW_AT_HP_block_index)
14595 return "DW_AT_HP_block_index";
c764a876 14596#endif
f39c6ffd
TT
14597
14598 name = get_DW_AT_name (attr);
14599
14600 if (name == NULL)
14601 return "DW_AT_<unknown>";
14602
14603 return name;
c906108c
SS
14604}
14605
14606/* Convert a DWARF value form code into its string name. */
14607
f39c6ffd 14608static const char *
aa1ee363 14609dwarf_form_name (unsigned form)
c906108c 14610{
f39c6ffd
TT
14611 const char *name = get_DW_FORM_name (form);
14612
14613 if (name == NULL)
14614 return "DW_FORM_<unknown>";
14615
14616 return name;
c906108c
SS
14617}
14618
14619static char *
fba45db2 14620dwarf_bool_name (unsigned mybool)
c906108c
SS
14621{
14622 if (mybool)
14623 return "TRUE";
14624 else
14625 return "FALSE";
14626}
14627
14628/* Convert a DWARF type code into its string name. */
14629
f39c6ffd 14630static const char *
aa1ee363 14631dwarf_type_encoding_name (unsigned enc)
c906108c 14632{
f39c6ffd 14633 const char *name = get_DW_ATE_name (enc);
c906108c 14634
f39c6ffd
TT
14635 if (name == NULL)
14636 return "DW_ATE_<unknown>";
c906108c 14637
f39c6ffd 14638 return name;
c906108c 14639}
c906108c 14640
f9aca02d 14641static void
d97bc12b 14642dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
14643{
14644 unsigned int i;
14645
d97bc12b
DE
14646 print_spaces (indent, f);
14647 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 14648 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
14649
14650 if (die->parent != NULL)
14651 {
14652 print_spaces (indent, f);
14653 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 14654 die->parent->offset.sect_off);
d97bc12b
DE
14655 }
14656
14657 print_spaces (indent, f);
14658 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 14659 dwarf_bool_name (die->child != NULL));
c906108c 14660
d97bc12b
DE
14661 print_spaces (indent, f);
14662 fprintf_unfiltered (f, " attributes:\n");
14663
c906108c
SS
14664 for (i = 0; i < die->num_attrs; ++i)
14665 {
d97bc12b
DE
14666 print_spaces (indent, f);
14667 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
14668 dwarf_attr_name (die->attrs[i].name),
14669 dwarf_form_name (die->attrs[i].form));
d97bc12b 14670
c906108c
SS
14671 switch (die->attrs[i].form)
14672 {
c906108c 14673 case DW_FORM_addr:
3019eac3 14674 case DW_FORM_GNU_addr_index:
d97bc12b 14675 fprintf_unfiltered (f, "address: ");
5af949e3 14676 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
14677 break;
14678 case DW_FORM_block2:
14679 case DW_FORM_block4:
14680 case DW_FORM_block:
14681 case DW_FORM_block1:
3e43a32a
MS
14682 fprintf_unfiltered (f, "block: size %d",
14683 DW_BLOCK (&die->attrs[i])->size);
c906108c 14684 break;
2dc7f7b3
TT
14685 case DW_FORM_exprloc:
14686 fprintf_unfiltered (f, "expression: size %u",
14687 DW_BLOCK (&die->attrs[i])->size);
14688 break;
4568ecf9
DE
14689 case DW_FORM_ref_addr:
14690 fprintf_unfiltered (f, "ref address: ");
14691 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14692 break;
10b3939b
DJ
14693 case DW_FORM_ref1:
14694 case DW_FORM_ref2:
14695 case DW_FORM_ref4:
4568ecf9
DE
14696 case DW_FORM_ref8:
14697 case DW_FORM_ref_udata:
d97bc12b 14698 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 14699 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 14700 break;
c906108c
SS
14701 case DW_FORM_data1:
14702 case DW_FORM_data2:
14703 case DW_FORM_data4:
ce5d95e1 14704 case DW_FORM_data8:
c906108c
SS
14705 case DW_FORM_udata:
14706 case DW_FORM_sdata:
43bbcdc2
PH
14707 fprintf_unfiltered (f, "constant: %s",
14708 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 14709 break;
2dc7f7b3
TT
14710 case DW_FORM_sec_offset:
14711 fprintf_unfiltered (f, "section offset: %s",
14712 pulongest (DW_UNSND (&die->attrs[i])));
14713 break;
55f1336d 14714 case DW_FORM_ref_sig8:
348e048f
DE
14715 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14716 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 14717 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
14718 else
14719 fprintf_unfiltered (f, "signatured type, offset: unknown");
14720 break;
c906108c 14721 case DW_FORM_string:
4bdf3d34 14722 case DW_FORM_strp:
3019eac3 14723 case DW_FORM_GNU_str_index:
8285870a 14724 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 14725 DW_STRING (&die->attrs[i])
8285870a
JK
14726 ? DW_STRING (&die->attrs[i]) : "",
14727 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
14728 break;
14729 case DW_FORM_flag:
14730 if (DW_UNSND (&die->attrs[i]))
d97bc12b 14731 fprintf_unfiltered (f, "flag: TRUE");
c906108c 14732 else
d97bc12b 14733 fprintf_unfiltered (f, "flag: FALSE");
c906108c 14734 break;
2dc7f7b3
TT
14735 case DW_FORM_flag_present:
14736 fprintf_unfiltered (f, "flag: TRUE");
14737 break;
a8329558 14738 case DW_FORM_indirect:
0963b4bd
MS
14739 /* The reader will have reduced the indirect form to
14740 the "base form" so this form should not occur. */
3e43a32a
MS
14741 fprintf_unfiltered (f,
14742 "unexpected attribute form: DW_FORM_indirect");
a8329558 14743 break;
c906108c 14744 default:
d97bc12b 14745 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 14746 die->attrs[i].form);
d97bc12b 14747 break;
c906108c 14748 }
d97bc12b 14749 fprintf_unfiltered (f, "\n");
c906108c
SS
14750 }
14751}
14752
f9aca02d 14753static void
d97bc12b 14754dump_die_for_error (struct die_info *die)
c906108c 14755{
d97bc12b
DE
14756 dump_die_shallow (gdb_stderr, 0, die);
14757}
14758
14759static void
14760dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14761{
14762 int indent = level * 4;
14763
14764 gdb_assert (die != NULL);
14765
14766 if (level >= max_level)
14767 return;
14768
14769 dump_die_shallow (f, indent, die);
14770
14771 if (die->child != NULL)
c906108c 14772 {
d97bc12b
DE
14773 print_spaces (indent, f);
14774 fprintf_unfiltered (f, " Children:");
14775 if (level + 1 < max_level)
14776 {
14777 fprintf_unfiltered (f, "\n");
14778 dump_die_1 (f, level + 1, max_level, die->child);
14779 }
14780 else
14781 {
3e43a32a
MS
14782 fprintf_unfiltered (f,
14783 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14784 }
14785 }
14786
14787 if (die->sibling != NULL && level > 0)
14788 {
14789 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14790 }
14791}
14792
d97bc12b
DE
14793/* This is called from the pdie macro in gdbinit.in.
14794 It's not static so gcc will keep a copy callable from gdb. */
14795
14796void
14797dump_die (struct die_info *die, int max_level)
14798{
14799 dump_die_1 (gdb_stdlog, 0, max_level, die);
14800}
14801
f9aca02d 14802static void
51545339 14803store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14804{
51545339 14805 void **slot;
c906108c 14806
b64f50a1
JK
14807 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14808 INSERT);
51545339
DJ
14809
14810 *slot = die;
c906108c
SS
14811}
14812
b64f50a1
JK
14813/* DW_ADDR is always stored already as sect_offset; despite for the forms
14814 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14815
93311388
DE
14816static int
14817is_ref_attr (struct attribute *attr)
c906108c 14818{
c906108c
SS
14819 switch (attr->form)
14820 {
14821 case DW_FORM_ref_addr:
c906108c
SS
14822 case DW_FORM_ref1:
14823 case DW_FORM_ref2:
14824 case DW_FORM_ref4:
613e1657 14825 case DW_FORM_ref8:
c906108c 14826 case DW_FORM_ref_udata:
93311388 14827 return 1;
c906108c 14828 default:
93311388 14829 return 0;
c906108c 14830 }
93311388
DE
14831}
14832
b64f50a1
JK
14833/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14834 required kind. */
14835
14836static sect_offset
93311388
DE
14837dwarf2_get_ref_die_offset (struct attribute *attr)
14838{
4568ecf9 14839 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 14840
93311388 14841 if (is_ref_attr (attr))
b64f50a1 14842 return retval;
93311388 14843
b64f50a1 14844 retval.sect_off = 0;
93311388
DE
14845 complaint (&symfile_complaints,
14846 _("unsupported die ref attribute form: '%s'"),
14847 dwarf_form_name (attr->form));
b64f50a1 14848 return retval;
c906108c
SS
14849}
14850
43bbcdc2
PH
14851/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14852 * the value held by the attribute is not constant. */
a02abb62 14853
43bbcdc2 14854static LONGEST
a02abb62
JB
14855dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14856{
14857 if (attr->form == DW_FORM_sdata)
14858 return DW_SND (attr);
14859 else if (attr->form == DW_FORM_udata
14860 || attr->form == DW_FORM_data1
14861 || attr->form == DW_FORM_data2
14862 || attr->form == DW_FORM_data4
14863 || attr->form == DW_FORM_data8)
14864 return DW_UNSND (attr);
14865 else
14866 {
3e43a32a
MS
14867 complaint (&symfile_complaints,
14868 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14869 dwarf_form_name (attr->form));
14870 return default_value;
14871 }
14872}
14873
03dd20cc 14874/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14875 unit and add it to our queue.
14876 The result is non-zero if PER_CU was queued, otherwise the result is zero
14877 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14878
348e048f 14879static int
03dd20cc 14880maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
95554aad
TT
14881 struct dwarf2_per_cu_data *per_cu,
14882 enum language pretend_language)
03dd20cc 14883{
98bfdba5
PA
14884 /* We may arrive here during partial symbol reading, if we need full
14885 DIEs to process an unusual case (e.g. template arguments). Do
14886 not queue PER_CU, just tell our caller to load its DIEs. */
14887 if (dwarf2_per_objfile->reading_partial_symbols)
14888 {
14889 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14890 return 1;
14891 return 0;
14892 }
14893
03dd20cc
DJ
14894 /* Mark the dependence relation so that we don't flush PER_CU
14895 too early. */
14896 dwarf2_add_dependence (this_cu, per_cu);
14897
14898 /* If it's already on the queue, we have nothing to do. */
14899 if (per_cu->queued)
348e048f 14900 return 0;
03dd20cc
DJ
14901
14902 /* If the compilation unit is already loaded, just mark it as
14903 used. */
14904 if (per_cu->cu != NULL)
14905 {
14906 per_cu->cu->last_used = 0;
348e048f 14907 return 0;
03dd20cc
DJ
14908 }
14909
14910 /* Add it to the queue. */
95554aad 14911 queue_comp_unit (per_cu, pretend_language);
348e048f
DE
14912
14913 return 1;
14914}
14915
14916/* Follow reference or signature attribute ATTR of SRC_DIE.
14917 On entry *REF_CU is the CU of SRC_DIE.
14918 On exit *REF_CU is the CU of the result. */
14919
14920static struct die_info *
14921follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14922 struct dwarf2_cu **ref_cu)
14923{
14924 struct die_info *die;
14925
14926 if (is_ref_attr (attr))
14927 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14928 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14929 die = follow_die_sig (src_die, attr, ref_cu);
14930 else
14931 {
14932 dump_die_for_error (src_die);
14933 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14934 (*ref_cu)->objfile->name);
14935 }
14936
14937 return die;
03dd20cc
DJ
14938}
14939
5c631832 14940/* Follow reference OFFSET.
673bfd45
DE
14941 On entry *REF_CU is the CU of the source die referencing OFFSET.
14942 On exit *REF_CU is the CU of the result.
14943 Returns NULL if OFFSET is invalid. */
f504f079 14944
f9aca02d 14945static struct die_info *
b64f50a1 14946follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
c906108c 14947{
10b3939b 14948 struct die_info temp_die;
f2f0e013 14949 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14950
348e048f
DE
14951 gdb_assert (cu->per_cu != NULL);
14952
98bfdba5
PA
14953 target_cu = cu;
14954
3019eac3 14955 if (cu->per_cu->is_debug_types)
348e048f
DE
14956 {
14957 /* .debug_types CUs cannot reference anything outside their CU.
14958 If they need to, they have to reference a signatured type via
55f1336d 14959 DW_FORM_ref_sig8. */
348e048f 14960 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14961 return NULL;
348e048f
DE
14962 }
14963 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14964 {
14965 struct dwarf2_per_cu_data *per_cu;
9a619af0 14966
45452591 14967 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
14968
14969 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
14970 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
14971 load_full_comp_unit (per_cu, cu->language);
03dd20cc 14972
10b3939b
DJ
14973 target_cu = per_cu->cu;
14974 }
98bfdba5
PA
14975 else if (cu->dies == NULL)
14976 {
14977 /* We're loading full DIEs during partial symbol reading. */
14978 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 14979 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 14980 }
c906108c 14981
f2f0e013 14982 *ref_cu = target_cu;
51545339 14983 temp_die.offset = offset;
b64f50a1 14984 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 14985}
10b3939b 14986
5c631832
JK
14987/* Follow reference attribute ATTR of SRC_DIE.
14988 On entry *REF_CU is the CU of SRC_DIE.
14989 On exit *REF_CU is the CU of the result. */
14990
14991static struct die_info *
14992follow_die_ref (struct die_info *src_die, struct attribute *attr,
14993 struct dwarf2_cu **ref_cu)
14994{
b64f50a1 14995 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
14996 struct dwarf2_cu *cu = *ref_cu;
14997 struct die_info *die;
14998
14999 die = follow_die_offset (offset, ref_cu);
15000 if (!die)
15001 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
15002 "at 0x%x [in module %s]"),
b64f50a1 15003 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 15004
5c631832
JK
15005 return die;
15006}
15007
d83e736b
JK
15008/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
15009 Returned value is intended for DW_OP_call*. Returned
15010 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
15011
15012struct dwarf2_locexpr_baton
b64f50a1 15013dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
15014 struct dwarf2_per_cu_data *per_cu,
15015 CORE_ADDR (*get_frame_pc) (void *baton),
15016 void *baton)
5c631832 15017{
b64f50a1 15018 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 15019 struct dwarf2_cu *cu;
5c631832
JK
15020 struct die_info *die;
15021 struct attribute *attr;
15022 struct dwarf2_locexpr_baton retval;
15023
8cf6f0b1
TT
15024 dw2_setup (per_cu->objfile);
15025
918dd910
JK
15026 if (per_cu->cu == NULL)
15027 load_cu (per_cu);
15028 cu = per_cu->cu;
15029
5c631832
JK
15030 die = follow_die_offset (offset, &cu);
15031 if (!die)
15032 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 15033 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15034
15035 attr = dwarf2_attr (die, DW_AT_location, cu);
15036 if (!attr)
15037 {
e103e986
JK
15038 /* DWARF: "If there is no such attribute, then there is no effect.".
15039 DATA is ignored if SIZE is 0. */
5c631832 15040
e103e986 15041 retval.data = NULL;
5c631832
JK
15042 retval.size = 0;
15043 }
8cf6f0b1
TT
15044 else if (attr_form_is_section_offset (attr))
15045 {
15046 struct dwarf2_loclist_baton loclist_baton;
15047 CORE_ADDR pc = (*get_frame_pc) (baton);
15048 size_t size;
15049
15050 fill_in_loclist_baton (cu, &loclist_baton, attr);
15051
15052 retval.data = dwarf2_find_location_expression (&loclist_baton,
15053 &size, pc);
15054 retval.size = size;
15055 }
5c631832
JK
15056 else
15057 {
15058 if (!attr_form_is_block (attr))
15059 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15060 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 15061 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15062
15063 retval.data = DW_BLOCK (attr)->data;
15064 retval.size = DW_BLOCK (attr)->size;
15065 }
15066 retval.per_cu = cu->per_cu;
918dd910 15067
918dd910
JK
15068 age_cached_comp_units ();
15069
5c631832 15070 return retval;
348e048f
DE
15071}
15072
8a9b8146
TT
15073/* Return the type of the DIE at DIE_OFFSET in the CU named by
15074 PER_CU. */
15075
15076struct type *
b64f50a1 15077dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
15078 struct dwarf2_per_cu_data *per_cu)
15079{
b64f50a1
JK
15080 sect_offset die_offset_sect;
15081
8a9b8146 15082 dw2_setup (per_cu->objfile);
b64f50a1
JK
15083
15084 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15085 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
15086}
15087
348e048f
DE
15088/* Follow the signature attribute ATTR in SRC_DIE.
15089 On entry *REF_CU is the CU of SRC_DIE.
15090 On exit *REF_CU is the CU of the result. */
15091
15092static struct die_info *
15093follow_die_sig (struct die_info *src_die, struct attribute *attr,
15094 struct dwarf2_cu **ref_cu)
15095{
15096 struct objfile *objfile = (*ref_cu)->objfile;
15097 struct die_info temp_die;
15098 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15099 struct dwarf2_cu *sig_cu;
15100 struct die_info *die;
15101
15102 /* sig_type will be NULL if the signatured type is missing from
15103 the debug info. */
15104 if (sig_type == NULL)
15105 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15106 "at 0x%x [in module %s]"),
b64f50a1 15107 src_die->offset.sect_off, objfile->name);
348e048f
DE
15108
15109 /* If necessary, add it to the queue and load its DIEs. */
15110
95554aad 15111 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 15112 read_signatured_type (sig_type);
348e048f
DE
15113
15114 gdb_assert (sig_type->per_cu.cu != NULL);
15115
15116 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
15117 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15118 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
15119 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15120 temp_die.offset.sect_off);
348e048f
DE
15121 if (die)
15122 {
15123 *ref_cu = sig_cu;
15124 return die;
15125 }
15126
3e43a32a
MS
15127 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15128 "from DIE at 0x%x [in module %s]"),
b64f50a1 15129 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
15130}
15131
15132/* Given an offset of a signatured type, return its signatured_type. */
15133
15134static struct signatured_type *
8b70b953
TT
15135lookup_signatured_type_at_offset (struct objfile *objfile,
15136 struct dwarf2_section_info *section,
b64f50a1 15137 sect_offset offset)
348e048f 15138{
b64f50a1 15139 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
15140 unsigned int length, initial_length_size;
15141 unsigned int sig_offset;
52dc124a 15142 struct signatured_type find_entry, *sig_type;
348e048f
DE
15143
15144 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15145 sig_offset = (initial_length_size
15146 + 2 /*version*/
15147 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15148 + 1 /*address_size*/);
15149 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 15150 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
15151
15152 /* This is only used to lookup previously recorded types.
15153 If we didn't find it, it's our bug. */
52dc124a
DE
15154 gdb_assert (sig_type != NULL);
15155 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 15156
52dc124a 15157 return sig_type;
348e048f
DE
15158}
15159
e5fe5e75 15160/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
15161
15162static void
e5fe5e75 15163load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 15164{
e5fe5e75 15165 struct objfile *objfile = per_cu->objfile;
3019eac3 15166 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
b64f50a1 15167 sect_offset offset = per_cu->offset;
52dc124a 15168 struct signatured_type *sig_type;
348e048f 15169
8b70b953 15170 dwarf2_read_section (objfile, sect);
be391dca 15171
348e048f 15172 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
15173 hash table lookup. */
15174 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15175 the signature to assert we found the right one.
15176 Ok, but it's a lot of work. We should simplify things so any needed
15177 assert doesn't require all this clumsiness. */
52dc124a 15178 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f 15179
dee91e82 15180 gdb_assert (&sig_type->per_cu == per_cu);
52dc124a 15181 gdb_assert (sig_type->per_cu.cu == NULL);
348e048f 15182
52dc124a 15183 read_signatured_type (sig_type);
348e048f 15184
52dc124a 15185 gdb_assert (sig_type->per_cu.cu != NULL);
348e048f
DE
15186}
15187
dee91e82
DE
15188/* die_reader_func for read_signatured_type.
15189 This is identical to load_full_comp_unit_reader,
15190 but is kept separate for now. */
348e048f
DE
15191
15192static void
dee91e82
DE
15193read_signatured_type_reader (const struct die_reader_specs *reader,
15194 gdb_byte *info_ptr,
15195 struct die_info *comp_unit_die,
15196 int has_children,
15197 void *data)
348e048f 15198{
dee91e82 15199 struct dwarf2_cu *cu = reader->cu;
348e048f 15200
dee91e82
DE
15201 gdb_assert (cu->die_hash == NULL);
15202 cu->die_hash =
15203 htab_create_alloc_ex (cu->header.length / 12,
15204 die_hash,
15205 die_eq,
15206 NULL,
15207 &cu->comp_unit_obstack,
15208 hashtab_obstack_allocate,
15209 dummy_obstack_deallocate);
348e048f 15210
dee91e82
DE
15211 if (has_children)
15212 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15213 &info_ptr, comp_unit_die);
15214 cu->dies = comp_unit_die;
15215 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
15216
15217 /* We try not to read any attributes in this function, because not
9cdd5dbd 15218 all CUs needed for references have been loaded yet, and symbol
348e048f 15219 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
15220 or we won't be able to build types correctly.
15221 Similarly, if we do not read the producer, we can not apply
15222 producer-specific interpretation. */
95554aad 15223 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 15224}
348e048f 15225
3019eac3
DE
15226/* Read in a signatured type and build its CU and DIEs.
15227 If the type is a stub for the real type in a DWO file,
15228 read in the real type from the DWO file as well. */
dee91e82
DE
15229
15230static void
15231read_signatured_type (struct signatured_type *sig_type)
15232{
15233 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 15234
3019eac3 15235 gdb_assert (per_cu->is_debug_types);
dee91e82 15236 gdb_assert (per_cu->cu == NULL);
348e048f 15237
fd820528 15238 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
c906108c
SS
15239}
15240
c906108c
SS
15241/* Decode simple location descriptions.
15242 Given a pointer to a dwarf block that defines a location, compute
15243 the location and return the value.
15244
4cecd739
DJ
15245 NOTE drow/2003-11-18: This function is called in two situations
15246 now: for the address of static or global variables (partial symbols
15247 only) and for offsets into structures which are expected to be
15248 (more or less) constant. The partial symbol case should go away,
15249 and only the constant case should remain. That will let this
15250 function complain more accurately. A few special modes are allowed
15251 without complaint for global variables (for instance, global
15252 register values and thread-local values).
c906108c
SS
15253
15254 A location description containing no operations indicates that the
4cecd739 15255 object is optimized out. The return value is 0 for that case.
6b992462
DJ
15256 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15257 callers will only want a very basic result and this can become a
21ae7a4d
JK
15258 complaint.
15259
15260 Note that stack[0] is unused except as a default error return. */
c906108c
SS
15261
15262static CORE_ADDR
e7c27a73 15263decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 15264{
e7c27a73 15265 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
15266 int i;
15267 int size = blk->size;
15268 gdb_byte *data = blk->data;
15269 CORE_ADDR stack[64];
15270 int stacki;
15271 unsigned int bytes_read, unsnd;
15272 gdb_byte op;
c906108c 15273
21ae7a4d
JK
15274 i = 0;
15275 stacki = 0;
15276 stack[stacki] = 0;
15277 stack[++stacki] = 0;
15278
15279 while (i < size)
15280 {
15281 op = data[i++];
15282 switch (op)
15283 {
15284 case DW_OP_lit0:
15285 case DW_OP_lit1:
15286 case DW_OP_lit2:
15287 case DW_OP_lit3:
15288 case DW_OP_lit4:
15289 case DW_OP_lit5:
15290 case DW_OP_lit6:
15291 case DW_OP_lit7:
15292 case DW_OP_lit8:
15293 case DW_OP_lit9:
15294 case DW_OP_lit10:
15295 case DW_OP_lit11:
15296 case DW_OP_lit12:
15297 case DW_OP_lit13:
15298 case DW_OP_lit14:
15299 case DW_OP_lit15:
15300 case DW_OP_lit16:
15301 case DW_OP_lit17:
15302 case DW_OP_lit18:
15303 case DW_OP_lit19:
15304 case DW_OP_lit20:
15305 case DW_OP_lit21:
15306 case DW_OP_lit22:
15307 case DW_OP_lit23:
15308 case DW_OP_lit24:
15309 case DW_OP_lit25:
15310 case DW_OP_lit26:
15311 case DW_OP_lit27:
15312 case DW_OP_lit28:
15313 case DW_OP_lit29:
15314 case DW_OP_lit30:
15315 case DW_OP_lit31:
15316 stack[++stacki] = op - DW_OP_lit0;
15317 break;
f1bea926 15318
21ae7a4d
JK
15319 case DW_OP_reg0:
15320 case DW_OP_reg1:
15321 case DW_OP_reg2:
15322 case DW_OP_reg3:
15323 case DW_OP_reg4:
15324 case DW_OP_reg5:
15325 case DW_OP_reg6:
15326 case DW_OP_reg7:
15327 case DW_OP_reg8:
15328 case DW_OP_reg9:
15329 case DW_OP_reg10:
15330 case DW_OP_reg11:
15331 case DW_OP_reg12:
15332 case DW_OP_reg13:
15333 case DW_OP_reg14:
15334 case DW_OP_reg15:
15335 case DW_OP_reg16:
15336 case DW_OP_reg17:
15337 case DW_OP_reg18:
15338 case DW_OP_reg19:
15339 case DW_OP_reg20:
15340 case DW_OP_reg21:
15341 case DW_OP_reg22:
15342 case DW_OP_reg23:
15343 case DW_OP_reg24:
15344 case DW_OP_reg25:
15345 case DW_OP_reg26:
15346 case DW_OP_reg27:
15347 case DW_OP_reg28:
15348 case DW_OP_reg29:
15349 case DW_OP_reg30:
15350 case DW_OP_reg31:
15351 stack[++stacki] = op - DW_OP_reg0;
15352 if (i < size)
15353 dwarf2_complex_location_expr_complaint ();
15354 break;
c906108c 15355
21ae7a4d
JK
15356 case DW_OP_regx:
15357 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15358 i += bytes_read;
15359 stack[++stacki] = unsnd;
15360 if (i < size)
15361 dwarf2_complex_location_expr_complaint ();
15362 break;
c906108c 15363
21ae7a4d
JK
15364 case DW_OP_addr:
15365 stack[++stacki] = read_address (objfile->obfd, &data[i],
15366 cu, &bytes_read);
15367 i += bytes_read;
15368 break;
d53d4ac5 15369
21ae7a4d
JK
15370 case DW_OP_const1u:
15371 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15372 i += 1;
15373 break;
15374
15375 case DW_OP_const1s:
15376 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15377 i += 1;
15378 break;
15379
15380 case DW_OP_const2u:
15381 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15382 i += 2;
15383 break;
15384
15385 case DW_OP_const2s:
15386 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15387 i += 2;
15388 break;
d53d4ac5 15389
21ae7a4d
JK
15390 case DW_OP_const4u:
15391 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15392 i += 4;
15393 break;
15394
15395 case DW_OP_const4s:
15396 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15397 i += 4;
15398 break;
15399
585861ea
JK
15400 case DW_OP_const8u:
15401 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15402 i += 8;
15403 break;
15404
21ae7a4d
JK
15405 case DW_OP_constu:
15406 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15407 &bytes_read);
15408 i += bytes_read;
15409 break;
15410
15411 case DW_OP_consts:
15412 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15413 i += bytes_read;
15414 break;
15415
15416 case DW_OP_dup:
15417 stack[stacki + 1] = stack[stacki];
15418 stacki++;
15419 break;
15420
15421 case DW_OP_plus:
15422 stack[stacki - 1] += stack[stacki];
15423 stacki--;
15424 break;
15425
15426 case DW_OP_plus_uconst:
15427 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15428 &bytes_read);
15429 i += bytes_read;
15430 break;
15431
15432 case DW_OP_minus:
15433 stack[stacki - 1] -= stack[stacki];
15434 stacki--;
15435 break;
15436
15437 case DW_OP_deref:
15438 /* If we're not the last op, then we definitely can't encode
15439 this using GDB's address_class enum. This is valid for partial
15440 global symbols, although the variable's address will be bogus
15441 in the psymtab. */
15442 if (i < size)
15443 dwarf2_complex_location_expr_complaint ();
15444 break;
15445
15446 case DW_OP_GNU_push_tls_address:
15447 /* The top of the stack has the offset from the beginning
15448 of the thread control block at which the variable is located. */
15449 /* Nothing should follow this operator, so the top of stack would
15450 be returned. */
15451 /* This is valid for partial global symbols, but the variable's
585861ea
JK
15452 address will be bogus in the psymtab. Make it always at least
15453 non-zero to not look as a variable garbage collected by linker
15454 which have DW_OP_addr 0. */
21ae7a4d
JK
15455 if (i < size)
15456 dwarf2_complex_location_expr_complaint ();
585861ea 15457 stack[stacki]++;
21ae7a4d
JK
15458 break;
15459
15460 case DW_OP_GNU_uninit:
15461 break;
15462
3019eac3
DE
15463 case DW_OP_GNU_addr_index:
15464 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15465 &bytes_read);
15466 i += bytes_read;
15467 break;
15468
21ae7a4d
JK
15469 default:
15470 {
f39c6ffd 15471 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
15472
15473 if (name)
15474 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15475 name);
15476 else
15477 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15478 op);
15479 }
15480
15481 return (stack[stacki]);
d53d4ac5 15482 }
3c6e0cb3 15483
21ae7a4d
JK
15484 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15485 outside of the allocated space. Also enforce minimum>0. */
15486 if (stacki >= ARRAY_SIZE (stack) - 1)
15487 {
15488 complaint (&symfile_complaints,
15489 _("location description stack overflow"));
15490 return 0;
15491 }
15492
15493 if (stacki <= 0)
15494 {
15495 complaint (&symfile_complaints,
15496 _("location description stack underflow"));
15497 return 0;
15498 }
15499 }
15500 return (stack[stacki]);
c906108c
SS
15501}
15502
15503/* memory allocation interface */
15504
c906108c 15505static struct dwarf_block *
7b5a2f43 15506dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
15507{
15508 struct dwarf_block *blk;
15509
15510 blk = (struct dwarf_block *)
7b5a2f43 15511 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
15512 return (blk);
15513}
15514
15515static struct abbrev_info *
f3dd6933 15516dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
15517{
15518 struct abbrev_info *abbrev;
15519
f3dd6933
DJ
15520 abbrev = (struct abbrev_info *)
15521 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
15522 memset (abbrev, 0, sizeof (struct abbrev_info));
15523 return (abbrev);
15524}
15525
15526static struct die_info *
b60c80d6 15527dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
15528{
15529 struct die_info *die;
b60c80d6
DJ
15530 size_t size = sizeof (struct die_info);
15531
15532 if (num_attrs > 1)
15533 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 15534
b60c80d6 15535 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
15536 memset (die, 0, sizeof (struct die_info));
15537 return (die);
15538}
2e276125
JB
15539
15540\f
15541/* Macro support. */
15542
2e276125
JB
15543/* Return the full name of file number I in *LH's file name table.
15544 Use COMP_DIR as the name of the current directory of the
15545 compilation. The result is allocated using xmalloc; the caller is
15546 responsible for freeing it. */
15547static char *
15548file_full_name (int file, struct line_header *lh, const char *comp_dir)
15549{
6a83a1e6
EZ
15550 /* Is the file number a valid index into the line header's file name
15551 table? Remember that file numbers start with one, not zero. */
15552 if (1 <= file && file <= lh->num_file_names)
15553 {
15554 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 15555
6a83a1e6
EZ
15556 if (IS_ABSOLUTE_PATH (fe->name))
15557 return xstrdup (fe->name);
15558 else
15559 {
15560 const char *dir;
15561 int dir_len;
15562 char *full_name;
15563
15564 if (fe->dir_index)
15565 dir = lh->include_dirs[fe->dir_index - 1];
15566 else
15567 dir = comp_dir;
15568
15569 if (dir)
15570 {
15571 dir_len = strlen (dir);
15572 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15573 strcpy (full_name, dir);
15574 full_name[dir_len] = '/';
15575 strcpy (full_name + dir_len + 1, fe->name);
15576 return full_name;
15577 }
15578 else
15579 return xstrdup (fe->name);
15580 }
15581 }
2e276125
JB
15582 else
15583 {
6a83a1e6
EZ
15584 /* The compiler produced a bogus file number. We can at least
15585 record the macro definitions made in the file, even if we
15586 won't be able to find the file by name. */
15587 char fake_name[80];
9a619af0 15588
6a83a1e6 15589 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 15590
6e70227d 15591 complaint (&symfile_complaints,
6a83a1e6
EZ
15592 _("bad file number in macro information (%d)"),
15593 file);
2e276125 15594
6a83a1e6 15595 return xstrdup (fake_name);
2e276125
JB
15596 }
15597}
15598
15599
15600static struct macro_source_file *
15601macro_start_file (int file, int line,
15602 struct macro_source_file *current_file,
15603 const char *comp_dir,
15604 struct line_header *lh, struct objfile *objfile)
15605{
15606 /* The full name of this source file. */
15607 char *full_name = file_full_name (file, lh, comp_dir);
15608
15609 /* We don't create a macro table for this compilation unit
15610 at all until we actually get a filename. */
15611 if (! pending_macros)
4a146b47 15612 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 15613 objfile->macro_cache);
2e276125
JB
15614
15615 if (! current_file)
abc9d0dc
TT
15616 {
15617 /* If we have no current file, then this must be the start_file
15618 directive for the compilation unit's main source file. */
15619 current_file = macro_set_main (pending_macros, full_name);
15620 macro_define_special (pending_macros);
15621 }
2e276125
JB
15622 else
15623 current_file = macro_include (current_file, line, full_name);
15624
15625 xfree (full_name);
6e70227d 15626
2e276125
JB
15627 return current_file;
15628}
15629
15630
15631/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15632 followed by a null byte. */
15633static char *
15634copy_string (const char *buf, int len)
15635{
15636 char *s = xmalloc (len + 1);
9a619af0 15637
2e276125
JB
15638 memcpy (s, buf, len);
15639 s[len] = '\0';
2e276125
JB
15640 return s;
15641}
15642
15643
15644static const char *
15645consume_improper_spaces (const char *p, const char *body)
15646{
15647 if (*p == ' ')
15648 {
4d3c2250 15649 complaint (&symfile_complaints,
3e43a32a
MS
15650 _("macro definition contains spaces "
15651 "in formal argument list:\n`%s'"),
4d3c2250 15652 body);
2e276125
JB
15653
15654 while (*p == ' ')
15655 p++;
15656 }
15657
15658 return p;
15659}
15660
15661
15662static void
15663parse_macro_definition (struct macro_source_file *file, int line,
15664 const char *body)
15665{
15666 const char *p;
15667
15668 /* The body string takes one of two forms. For object-like macro
15669 definitions, it should be:
15670
15671 <macro name> " " <definition>
15672
15673 For function-like macro definitions, it should be:
15674
15675 <macro name> "() " <definition>
15676 or
15677 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15678
15679 Spaces may appear only where explicitly indicated, and in the
15680 <definition>.
15681
15682 The Dwarf 2 spec says that an object-like macro's name is always
15683 followed by a space, but versions of GCC around March 2002 omit
6e70227d 15684 the space when the macro's definition is the empty string.
2e276125
JB
15685
15686 The Dwarf 2 spec says that there should be no spaces between the
15687 formal arguments in a function-like macro's formal argument list,
15688 but versions of GCC around March 2002 include spaces after the
15689 commas. */
15690
15691
15692 /* Find the extent of the macro name. The macro name is terminated
15693 by either a space or null character (for an object-like macro) or
15694 an opening paren (for a function-like macro). */
15695 for (p = body; *p; p++)
15696 if (*p == ' ' || *p == '(')
15697 break;
15698
15699 if (*p == ' ' || *p == '\0')
15700 {
15701 /* It's an object-like macro. */
15702 int name_len = p - body;
15703 char *name = copy_string (body, name_len);
15704 const char *replacement;
15705
15706 if (*p == ' ')
15707 replacement = body + name_len + 1;
15708 else
15709 {
4d3c2250 15710 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15711 replacement = body + name_len;
15712 }
6e70227d 15713
2e276125
JB
15714 macro_define_object (file, line, name, replacement);
15715
15716 xfree (name);
15717 }
15718 else if (*p == '(')
15719 {
15720 /* It's a function-like macro. */
15721 char *name = copy_string (body, p - body);
15722 int argc = 0;
15723 int argv_size = 1;
15724 char **argv = xmalloc (argv_size * sizeof (*argv));
15725
15726 p++;
15727
15728 p = consume_improper_spaces (p, body);
15729
15730 /* Parse the formal argument list. */
15731 while (*p && *p != ')')
15732 {
15733 /* Find the extent of the current argument name. */
15734 const char *arg_start = p;
15735
15736 while (*p && *p != ',' && *p != ')' && *p != ' ')
15737 p++;
15738
15739 if (! *p || p == arg_start)
4d3c2250 15740 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15741 else
15742 {
15743 /* Make sure argv has room for the new argument. */
15744 if (argc >= argv_size)
15745 {
15746 argv_size *= 2;
15747 argv = xrealloc (argv, argv_size * sizeof (*argv));
15748 }
15749
15750 argv[argc++] = copy_string (arg_start, p - arg_start);
15751 }
15752
15753 p = consume_improper_spaces (p, body);
15754
15755 /* Consume the comma, if present. */
15756 if (*p == ',')
15757 {
15758 p++;
15759
15760 p = consume_improper_spaces (p, body);
15761 }
15762 }
15763
15764 if (*p == ')')
15765 {
15766 p++;
15767
15768 if (*p == ' ')
15769 /* Perfectly formed definition, no complaints. */
15770 macro_define_function (file, line, name,
6e70227d 15771 argc, (const char **) argv,
2e276125
JB
15772 p + 1);
15773 else if (*p == '\0')
15774 {
15775 /* Complain, but do define it. */
4d3c2250 15776 dwarf2_macro_malformed_definition_complaint (body);
2e276125 15777 macro_define_function (file, line, name,
6e70227d 15778 argc, (const char **) argv,
2e276125
JB
15779 p);
15780 }
15781 else
15782 /* Just complain. */
4d3c2250 15783 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15784 }
15785 else
15786 /* Just complain. */
4d3c2250 15787 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15788
15789 xfree (name);
15790 {
15791 int i;
15792
15793 for (i = 0; i < argc; i++)
15794 xfree (argv[i]);
15795 }
15796 xfree (argv);
15797 }
15798 else
4d3c2250 15799 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15800}
15801
cf2c3c16
TT
15802/* Skip some bytes from BYTES according to the form given in FORM.
15803 Returns the new pointer. */
2e276125 15804
cf2c3c16 15805static gdb_byte *
f664829e 15806skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
15807 enum dwarf_form form,
15808 unsigned int offset_size,
15809 struct dwarf2_section_info *section)
2e276125 15810{
cf2c3c16 15811 unsigned int bytes_read;
2e276125 15812
cf2c3c16 15813 switch (form)
2e276125 15814 {
cf2c3c16
TT
15815 case DW_FORM_data1:
15816 case DW_FORM_flag:
15817 ++bytes;
15818 break;
15819
15820 case DW_FORM_data2:
15821 bytes += 2;
15822 break;
15823
15824 case DW_FORM_data4:
15825 bytes += 4;
15826 break;
15827
15828 case DW_FORM_data8:
15829 bytes += 8;
15830 break;
15831
15832 case DW_FORM_string:
15833 read_direct_string (abfd, bytes, &bytes_read);
15834 bytes += bytes_read;
15835 break;
15836
15837 case DW_FORM_sec_offset:
15838 case DW_FORM_strp:
15839 bytes += offset_size;
15840 break;
15841
15842 case DW_FORM_block:
15843 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15844 bytes += bytes_read;
15845 break;
15846
15847 case DW_FORM_block1:
15848 bytes += 1 + read_1_byte (abfd, bytes);
15849 break;
15850 case DW_FORM_block2:
15851 bytes += 2 + read_2_bytes (abfd, bytes);
15852 break;
15853 case DW_FORM_block4:
15854 bytes += 4 + read_4_bytes (abfd, bytes);
15855 break;
15856
15857 case DW_FORM_sdata:
15858 case DW_FORM_udata:
3019eac3
DE
15859 case DW_FORM_GNU_addr_index:
15860 case DW_FORM_GNU_str_index:
f664829e
DE
15861 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
15862 if (bytes == NULL)
15863 {
15864 dwarf2_section_buffer_overflow_complaint (section);
15865 return NULL;
15866 }
cf2c3c16
TT
15867 break;
15868
15869 default:
15870 {
15871 complain:
15872 complaint (&symfile_complaints,
15873 _("invalid form 0x%x in `%s'"),
15874 form,
15875 section->asection->name);
15876 return NULL;
15877 }
2e276125
JB
15878 }
15879
cf2c3c16
TT
15880 return bytes;
15881}
757a13d0 15882
cf2c3c16
TT
15883/* A helper for dwarf_decode_macros that handles skipping an unknown
15884 opcode. Returns an updated pointer to the macro data buffer; or,
15885 on error, issues a complaint and returns NULL. */
757a13d0 15886
cf2c3c16
TT
15887static gdb_byte *
15888skip_unknown_opcode (unsigned int opcode,
15889 gdb_byte **opcode_definitions,
f664829e 15890 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
15891 bfd *abfd,
15892 unsigned int offset_size,
15893 struct dwarf2_section_info *section)
15894{
15895 unsigned int bytes_read, i;
15896 unsigned long arg;
15897 gdb_byte *defn;
2e276125 15898
cf2c3c16 15899 if (opcode_definitions[opcode] == NULL)
2e276125 15900 {
cf2c3c16
TT
15901 complaint (&symfile_complaints,
15902 _("unrecognized DW_MACFINO opcode 0x%x"),
15903 opcode);
15904 return NULL;
15905 }
2e276125 15906
cf2c3c16
TT
15907 defn = opcode_definitions[opcode];
15908 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15909 defn += bytes_read;
2e276125 15910
cf2c3c16
TT
15911 for (i = 0; i < arg; ++i)
15912 {
f664829e
DE
15913 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
15914 section);
cf2c3c16
TT
15915 if (mac_ptr == NULL)
15916 {
15917 /* skip_form_bytes already issued the complaint. */
15918 return NULL;
15919 }
15920 }
757a13d0 15921
cf2c3c16
TT
15922 return mac_ptr;
15923}
757a13d0 15924
cf2c3c16
TT
15925/* A helper function which parses the header of a macro section.
15926 If the macro section is the extended (for now called "GNU") type,
15927 then this updates *OFFSET_SIZE. Returns a pointer to just after
15928 the header, or issues a complaint and returns NULL on error. */
757a13d0 15929
cf2c3c16
TT
15930static gdb_byte *
15931dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15932 bfd *abfd,
15933 gdb_byte *mac_ptr,
15934 unsigned int *offset_size,
15935 int section_is_gnu)
15936{
15937 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15938
cf2c3c16
TT
15939 if (section_is_gnu)
15940 {
15941 unsigned int version, flags;
757a13d0 15942
cf2c3c16
TT
15943 version = read_2_bytes (abfd, mac_ptr);
15944 if (version != 4)
15945 {
15946 complaint (&symfile_complaints,
15947 _("unrecognized version `%d' in .debug_macro section"),
15948 version);
15949 return NULL;
15950 }
15951 mac_ptr += 2;
757a13d0 15952
cf2c3c16
TT
15953 flags = read_1_byte (abfd, mac_ptr);
15954 ++mac_ptr;
15955 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15956
cf2c3c16
TT
15957 if ((flags & 2) != 0)
15958 /* We don't need the line table offset. */
15959 mac_ptr += *offset_size;
757a13d0 15960
cf2c3c16
TT
15961 /* Vendor opcode descriptions. */
15962 if ((flags & 4) != 0)
15963 {
15964 unsigned int i, count;
757a13d0 15965
cf2c3c16
TT
15966 count = read_1_byte (abfd, mac_ptr);
15967 ++mac_ptr;
15968 for (i = 0; i < count; ++i)
15969 {
15970 unsigned int opcode, bytes_read;
15971 unsigned long arg;
15972
15973 opcode = read_1_byte (abfd, mac_ptr);
15974 ++mac_ptr;
15975 opcode_definitions[opcode] = mac_ptr;
15976 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15977 mac_ptr += bytes_read;
15978 mac_ptr += arg;
15979 }
757a13d0 15980 }
cf2c3c16 15981 }
757a13d0 15982
cf2c3c16
TT
15983 return mac_ptr;
15984}
757a13d0 15985
cf2c3c16 15986/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 15987 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
15988
15989static void
15990dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15991 struct macro_source_file *current_file,
15992 struct line_header *lh, char *comp_dir,
15993 struct dwarf2_section_info *section,
15994 int section_is_gnu,
15995 unsigned int offset_size,
8fc3fc34
TT
15996 struct objfile *objfile,
15997 htab_t include_hash)
cf2c3c16
TT
15998{
15999 enum dwarf_macro_record_type macinfo_type;
16000 int at_commandline;
16001 gdb_byte *opcode_definitions[256];
757a13d0 16002
cf2c3c16
TT
16003 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16004 &offset_size, section_is_gnu);
16005 if (mac_ptr == NULL)
16006 {
16007 /* We already issued a complaint. */
16008 return;
16009 }
757a13d0
JK
16010
16011 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
16012 GDB is still reading the definitions from command line. First
16013 DW_MACINFO_start_file will need to be ignored as it was already executed
16014 to create CURRENT_FILE for the main source holding also the command line
16015 definitions. On first met DW_MACINFO_start_file this flag is reset to
16016 normally execute all the remaining DW_MACINFO_start_file macinfos. */
16017
16018 at_commandline = 1;
16019
16020 do
16021 {
16022 /* Do we at least have room for a macinfo type byte? */
16023 if (mac_ptr >= mac_end)
16024 {
f664829e 16025 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
16026 break;
16027 }
16028
16029 macinfo_type = read_1_byte (abfd, mac_ptr);
16030 mac_ptr++;
16031
cf2c3c16
TT
16032 /* Note that we rely on the fact that the corresponding GNU and
16033 DWARF constants are the same. */
757a13d0
JK
16034 switch (macinfo_type)
16035 {
16036 /* A zero macinfo type indicates the end of the macro
16037 information. */
16038 case 0:
16039 break;
2e276125 16040
cf2c3c16
TT
16041 case DW_MACRO_GNU_define:
16042 case DW_MACRO_GNU_undef:
16043 case DW_MACRO_GNU_define_indirect:
16044 case DW_MACRO_GNU_undef_indirect:
2e276125 16045 {
891d2f0b 16046 unsigned int bytes_read;
2e276125
JB
16047 int line;
16048 char *body;
cf2c3c16 16049 int is_define;
2e276125 16050
cf2c3c16
TT
16051 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16052 mac_ptr += bytes_read;
16053
16054 if (macinfo_type == DW_MACRO_GNU_define
16055 || macinfo_type == DW_MACRO_GNU_undef)
16056 {
16057 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16058 mac_ptr += bytes_read;
16059 }
16060 else
16061 {
16062 LONGEST str_offset;
16063
16064 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16065 mac_ptr += offset_size;
2e276125 16066
cf2c3c16
TT
16067 body = read_indirect_string_at_offset (abfd, str_offset);
16068 }
16069
16070 is_define = (macinfo_type == DW_MACRO_GNU_define
16071 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 16072 if (! current_file)
757a13d0
JK
16073 {
16074 /* DWARF violation as no main source is present. */
16075 complaint (&symfile_complaints,
16076 _("debug info with no main source gives macro %s "
16077 "on line %d: %s"),
cf2c3c16
TT
16078 is_define ? _("definition") : _("undefinition"),
16079 line, body);
757a13d0
JK
16080 break;
16081 }
3e43a32a
MS
16082 if ((line == 0 && !at_commandline)
16083 || (line != 0 && at_commandline))
4d3c2250 16084 complaint (&symfile_complaints,
757a13d0
JK
16085 _("debug info gives %s macro %s with %s line %d: %s"),
16086 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 16087 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
16088 line == 0 ? _("zero") : _("non-zero"), line, body);
16089
cf2c3c16 16090 if (is_define)
757a13d0 16091 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
16092 else
16093 {
16094 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16095 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16096 macro_undef (current_file, line, body);
16097 }
2e276125
JB
16098 }
16099 break;
16100
cf2c3c16 16101 case DW_MACRO_GNU_start_file:
2e276125 16102 {
891d2f0b 16103 unsigned int bytes_read;
2e276125
JB
16104 int line, file;
16105
16106 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16107 mac_ptr += bytes_read;
16108 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16109 mac_ptr += bytes_read;
16110
3e43a32a
MS
16111 if ((line == 0 && !at_commandline)
16112 || (line != 0 && at_commandline))
757a13d0
JK
16113 complaint (&symfile_complaints,
16114 _("debug info gives source %d included "
16115 "from %s at %s line %d"),
16116 file, at_commandline ? _("command-line") : _("file"),
16117 line == 0 ? _("zero") : _("non-zero"), line);
16118
16119 if (at_commandline)
16120 {
cf2c3c16
TT
16121 /* This DW_MACRO_GNU_start_file was executed in the
16122 pass one. */
757a13d0
JK
16123 at_commandline = 0;
16124 }
16125 else
16126 current_file = macro_start_file (file, line,
16127 current_file, comp_dir,
cf2c3c16 16128 lh, objfile);
2e276125
JB
16129 }
16130 break;
16131
cf2c3c16 16132 case DW_MACRO_GNU_end_file:
2e276125 16133 if (! current_file)
4d3c2250 16134 complaint (&symfile_complaints,
3e43a32a
MS
16135 _("macro debug info has an unmatched "
16136 "`close_file' directive"));
2e276125
JB
16137 else
16138 {
16139 current_file = current_file->included_by;
16140 if (! current_file)
16141 {
cf2c3c16 16142 enum dwarf_macro_record_type next_type;
2e276125
JB
16143
16144 /* GCC circa March 2002 doesn't produce the zero
16145 type byte marking the end of the compilation
16146 unit. Complain if it's not there, but exit no
16147 matter what. */
16148
16149 /* Do we at least have room for a macinfo type byte? */
16150 if (mac_ptr >= mac_end)
16151 {
f664829e 16152 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
16153 return;
16154 }
16155
16156 /* We don't increment mac_ptr here, so this is just
16157 a look-ahead. */
16158 next_type = read_1_byte (abfd, mac_ptr);
16159 if (next_type != 0)
4d3c2250 16160 complaint (&symfile_complaints,
3e43a32a
MS
16161 _("no terminating 0-type entry for "
16162 "macros in `.debug_macinfo' section"));
2e276125
JB
16163
16164 return;
16165 }
16166 }
16167 break;
16168
cf2c3c16
TT
16169 case DW_MACRO_GNU_transparent_include:
16170 {
16171 LONGEST offset;
8fc3fc34 16172 void **slot;
cf2c3c16
TT
16173
16174 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16175 mac_ptr += offset_size;
16176
8fc3fc34
TT
16177 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16178 if (*slot != NULL)
16179 {
16180 /* This has actually happened; see
16181 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16182 complaint (&symfile_complaints,
16183 _("recursive DW_MACRO_GNU_transparent_include in "
16184 ".debug_macro section"));
16185 }
16186 else
16187 {
16188 *slot = mac_ptr;
16189
16190 dwarf_decode_macro_bytes (abfd,
16191 section->buffer + offset,
16192 mac_end, current_file,
16193 lh, comp_dir,
16194 section, section_is_gnu,
16195 offset_size, objfile, include_hash);
16196
16197 htab_remove_elt (include_hash, mac_ptr);
16198 }
cf2c3c16
TT
16199 }
16200 break;
16201
2e276125 16202 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
16203 if (!section_is_gnu)
16204 {
16205 unsigned int bytes_read;
16206 int constant;
2e276125 16207
cf2c3c16
TT
16208 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16209 mac_ptr += bytes_read;
16210 read_direct_string (abfd, mac_ptr, &bytes_read);
16211 mac_ptr += bytes_read;
2e276125 16212
cf2c3c16
TT
16213 /* We don't recognize any vendor extensions. */
16214 break;
16215 }
16216 /* FALLTHROUGH */
16217
16218 default:
16219 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 16220 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
16221 section);
16222 if (mac_ptr == NULL)
16223 return;
16224 break;
2e276125 16225 }
757a13d0 16226 } while (macinfo_type != 0);
2e276125 16227}
8e19ed76 16228
cf2c3c16
TT
16229static void
16230dwarf_decode_macros (struct line_header *lh, unsigned int offset,
16231 char *comp_dir, bfd *abfd,
16232 struct dwarf2_cu *cu,
16233 struct dwarf2_section_info *section,
fceca515 16234 int section_is_gnu, const char *section_name)
cf2c3c16 16235{
bb5ed363 16236 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
16237 gdb_byte *mac_ptr, *mac_end;
16238 struct macro_source_file *current_file = 0;
16239 enum dwarf_macro_record_type macinfo_type;
16240 unsigned int offset_size = cu->header.offset_size;
16241 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
16242 struct cleanup *cleanup;
16243 htab_t include_hash;
16244 void **slot;
cf2c3c16 16245
bb5ed363 16246 dwarf2_read_section (objfile, section);
cf2c3c16
TT
16247 if (section->buffer == NULL)
16248 {
fceca515 16249 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
16250 return;
16251 }
16252
16253 /* First pass: Find the name of the base filename.
16254 This filename is needed in order to process all macros whose definition
16255 (or undefinition) comes from the command line. These macros are defined
16256 before the first DW_MACINFO_start_file entry, and yet still need to be
16257 associated to the base file.
16258
16259 To determine the base file name, we scan the macro definitions until we
16260 reach the first DW_MACINFO_start_file entry. We then initialize
16261 CURRENT_FILE accordingly so that any macro definition found before the
16262 first DW_MACINFO_start_file can still be associated to the base file. */
16263
16264 mac_ptr = section->buffer + offset;
16265 mac_end = section->buffer + section->size;
16266
16267 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16268 &offset_size, section_is_gnu);
16269 if (mac_ptr == NULL)
16270 {
16271 /* We already issued a complaint. */
16272 return;
16273 }
16274
16275 do
16276 {
16277 /* Do we at least have room for a macinfo type byte? */
16278 if (mac_ptr >= mac_end)
16279 {
16280 /* Complaint is printed during the second pass as GDB will probably
16281 stop the first pass earlier upon finding
16282 DW_MACINFO_start_file. */
16283 break;
16284 }
16285
16286 macinfo_type = read_1_byte (abfd, mac_ptr);
16287 mac_ptr++;
16288
16289 /* Note that we rely on the fact that the corresponding GNU and
16290 DWARF constants are the same. */
16291 switch (macinfo_type)
16292 {
16293 /* A zero macinfo type indicates the end of the macro
16294 information. */
16295 case 0:
16296 break;
16297
16298 case DW_MACRO_GNU_define:
16299 case DW_MACRO_GNU_undef:
16300 /* Only skip the data by MAC_PTR. */
16301 {
16302 unsigned int bytes_read;
16303
16304 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16305 mac_ptr += bytes_read;
16306 read_direct_string (abfd, mac_ptr, &bytes_read);
16307 mac_ptr += bytes_read;
16308 }
16309 break;
16310
16311 case DW_MACRO_GNU_start_file:
16312 {
16313 unsigned int bytes_read;
16314 int line, file;
16315
16316 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16317 mac_ptr += bytes_read;
16318 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16319 mac_ptr += bytes_read;
16320
16321 current_file = macro_start_file (file, line, current_file,
bb5ed363 16322 comp_dir, lh, objfile);
cf2c3c16
TT
16323 }
16324 break;
16325
16326 case DW_MACRO_GNU_end_file:
16327 /* No data to skip by MAC_PTR. */
16328 break;
16329
16330 case DW_MACRO_GNU_define_indirect:
16331 case DW_MACRO_GNU_undef_indirect:
16332 {
16333 unsigned int bytes_read;
16334
16335 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16336 mac_ptr += bytes_read;
16337 mac_ptr += offset_size;
16338 }
16339 break;
16340
16341 case DW_MACRO_GNU_transparent_include:
16342 /* Note that, according to the spec, a transparent include
16343 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16344 skip this opcode. */
16345 mac_ptr += offset_size;
16346 break;
16347
16348 case DW_MACINFO_vendor_ext:
16349 /* Only skip the data by MAC_PTR. */
16350 if (!section_is_gnu)
16351 {
16352 unsigned int bytes_read;
16353
16354 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16355 mac_ptr += bytes_read;
16356 read_direct_string (abfd, mac_ptr, &bytes_read);
16357 mac_ptr += bytes_read;
16358 }
16359 /* FALLTHROUGH */
16360
16361 default:
16362 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 16363 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
16364 section);
16365 if (mac_ptr == NULL)
16366 return;
16367 break;
16368 }
16369 } while (macinfo_type != 0 && current_file == NULL);
16370
16371 /* Second pass: Process all entries.
16372
16373 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16374 command-line macro definitions/undefinitions. This flag is unset when we
16375 reach the first DW_MACINFO_start_file entry. */
16376
8fc3fc34
TT
16377 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16378 NULL, xcalloc, xfree);
16379 cleanup = make_cleanup_htab_delete (include_hash);
16380 mac_ptr = section->buffer + offset;
16381 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16382 *slot = mac_ptr;
16383 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
cf2c3c16 16384 current_file, lh, comp_dir, section, section_is_gnu,
8fc3fc34
TT
16385 offset_size, objfile, include_hash);
16386 do_cleanups (cleanup);
cf2c3c16
TT
16387}
16388
8e19ed76 16389/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 16390 if so return true else false. */
380bca97 16391
8e19ed76
PS
16392static int
16393attr_form_is_block (struct attribute *attr)
16394{
16395 return (attr == NULL ? 0 :
16396 attr->form == DW_FORM_block1
16397 || attr->form == DW_FORM_block2
16398 || attr->form == DW_FORM_block4
2dc7f7b3
TT
16399 || attr->form == DW_FORM_block
16400 || attr->form == DW_FORM_exprloc);
8e19ed76 16401}
4c2df51b 16402
c6a0999f
JB
16403/* Return non-zero if ATTR's value is a section offset --- classes
16404 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16405 You may use DW_UNSND (attr) to retrieve such offsets.
16406
16407 Section 7.5.4, "Attribute Encodings", explains that no attribute
16408 may have a value that belongs to more than one of these classes; it
16409 would be ambiguous if we did, because we use the same forms for all
16410 of them. */
380bca97 16411
3690dd37
JB
16412static int
16413attr_form_is_section_offset (struct attribute *attr)
16414{
16415 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
16416 || attr->form == DW_FORM_data8
16417 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
16418}
16419
3690dd37
JB
16420/* Return non-zero if ATTR's value falls in the 'constant' class, or
16421 zero otherwise. When this function returns true, you can apply
16422 dwarf2_get_attr_constant_value to it.
16423
16424 However, note that for some attributes you must check
16425 attr_form_is_section_offset before using this test. DW_FORM_data4
16426 and DW_FORM_data8 are members of both the constant class, and of
16427 the classes that contain offsets into other debug sections
16428 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16429 that, if an attribute's can be either a constant or one of the
16430 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16431 taken as section offsets, not constants. */
380bca97 16432
3690dd37
JB
16433static int
16434attr_form_is_constant (struct attribute *attr)
16435{
16436 switch (attr->form)
16437 {
16438 case DW_FORM_sdata:
16439 case DW_FORM_udata:
16440 case DW_FORM_data1:
16441 case DW_FORM_data2:
16442 case DW_FORM_data4:
16443 case DW_FORM_data8:
16444 return 1;
16445 default:
16446 return 0;
16447 }
16448}
16449
3019eac3
DE
16450/* Return the .debug_loc section to use for CU.
16451 For DWO files use .debug_loc.dwo. */
16452
16453static struct dwarf2_section_info *
16454cu_debug_loc_section (struct dwarf2_cu *cu)
16455{
16456 if (cu->dwo_unit)
16457 return &cu->dwo_unit->dwo_file->sections.loc;
16458 return &dwarf2_per_objfile->loc;
16459}
16460
8cf6f0b1
TT
16461/* A helper function that fills in a dwarf2_loclist_baton. */
16462
16463static void
16464fill_in_loclist_baton (struct dwarf2_cu *cu,
16465 struct dwarf2_loclist_baton *baton,
16466 struct attribute *attr)
16467{
3019eac3
DE
16468 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16469
16470 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
16471
16472 baton->per_cu = cu->per_cu;
16473 gdb_assert (baton->per_cu);
16474 /* We don't know how long the location list is, but make sure we
16475 don't run off the edge of the section. */
3019eac3
DE
16476 baton->size = section->size - DW_UNSND (attr);
16477 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 16478 baton->base_address = cu->base_address;
f664829e 16479 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
16480}
16481
4c2df51b
DJ
16482static void
16483dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 16484 struct dwarf2_cu *cu)
4c2df51b 16485{
bb5ed363 16486 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 16487 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 16488
3690dd37 16489 if (attr_form_is_section_offset (attr)
3019eac3 16490 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
16491 the section. If so, fall through to the complaint in the
16492 other branch. */
3019eac3 16493 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 16494 {
0d53c4c4 16495 struct dwarf2_loclist_baton *baton;
4c2df51b 16496
bb5ed363 16497 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16498 sizeof (struct dwarf2_loclist_baton));
4c2df51b 16499
8cf6f0b1 16500 fill_in_loclist_baton (cu, baton, attr);
be391dca 16501
d00adf39 16502 if (cu->base_known == 0)
0d53c4c4 16503 complaint (&symfile_complaints,
3e43a32a
MS
16504 _("Location list used without "
16505 "specifying the CU base address."));
4c2df51b 16506
768a979c 16507 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
16508 SYMBOL_LOCATION_BATON (sym) = baton;
16509 }
16510 else
16511 {
16512 struct dwarf2_locexpr_baton *baton;
16513
bb5ed363 16514 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16515 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
16516 baton->per_cu = cu->per_cu;
16517 gdb_assert (baton->per_cu);
0d53c4c4
DJ
16518
16519 if (attr_form_is_block (attr))
16520 {
16521 /* Note that we're just copying the block's data pointer
16522 here, not the actual data. We're still pointing into the
6502dd73
DJ
16523 info_buffer for SYM's objfile; right now we never release
16524 that buffer, but when we do clean up properly this may
16525 need to change. */
0d53c4c4
DJ
16526 baton->size = DW_BLOCK (attr)->size;
16527 baton->data = DW_BLOCK (attr)->data;
16528 }
16529 else
16530 {
16531 dwarf2_invalid_attrib_class_complaint ("location description",
16532 SYMBOL_NATURAL_NAME (sym));
16533 baton->size = 0;
0d53c4c4 16534 }
6e70227d 16535
768a979c 16536 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
16537 SYMBOL_LOCATION_BATON (sym) = baton;
16538 }
4c2df51b 16539}
6502dd73 16540
9aa1f1e3
TT
16541/* Return the OBJFILE associated with the compilation unit CU. If CU
16542 came from a separate debuginfo file, then the master objfile is
16543 returned. */
ae0d2f24
UW
16544
16545struct objfile *
16546dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16547{
9291a0cd 16548 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
16549
16550 /* Return the master objfile, so that we can report and look up the
16551 correct file containing this variable. */
16552 if (objfile->separate_debug_objfile_backlink)
16553 objfile = objfile->separate_debug_objfile_backlink;
16554
16555 return objfile;
16556}
16557
96408a79
SA
16558/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16559 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16560 CU_HEADERP first. */
16561
16562static const struct comp_unit_head *
16563per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16564 struct dwarf2_per_cu_data *per_cu)
16565{
16566 struct objfile *objfile;
16567 struct dwarf2_per_objfile *per_objfile;
16568 gdb_byte *info_ptr;
16569
16570 if (per_cu->cu)
16571 return &per_cu->cu->header;
16572
16573 objfile = per_cu->objfile;
16574 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
b64f50a1 16575 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
96408a79
SA
16576
16577 memset (cu_headerp, 0, sizeof (*cu_headerp));
16578 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16579
16580 return cu_headerp;
16581}
16582
ae0d2f24
UW
16583/* Return the address size given in the compilation unit header for CU. */
16584
98714339 16585int
ae0d2f24
UW
16586dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16587{
96408a79
SA
16588 struct comp_unit_head cu_header_local;
16589 const struct comp_unit_head *cu_headerp;
c471e790 16590
96408a79
SA
16591 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16592
16593 return cu_headerp->addr_size;
ae0d2f24
UW
16594}
16595
9eae7c52
TT
16596/* Return the offset size given in the compilation unit header for CU. */
16597
16598int
16599dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16600{
96408a79
SA
16601 struct comp_unit_head cu_header_local;
16602 const struct comp_unit_head *cu_headerp;
9c6c53f7 16603
96408a79
SA
16604 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16605
16606 return cu_headerp->offset_size;
16607}
16608
16609/* See its dwarf2loc.h declaration. */
16610
16611int
16612dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16613{
16614 struct comp_unit_head cu_header_local;
16615 const struct comp_unit_head *cu_headerp;
16616
16617 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16618
16619 if (cu_headerp->version == 2)
16620 return cu_headerp->addr_size;
16621 else
16622 return cu_headerp->offset_size;
181cebd4
JK
16623}
16624
9aa1f1e3
TT
16625/* Return the text offset of the CU. The returned offset comes from
16626 this CU's objfile. If this objfile came from a separate debuginfo
16627 file, then the offset may be different from the corresponding
16628 offset in the parent objfile. */
16629
16630CORE_ADDR
16631dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16632{
bb3fa9d0 16633 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
16634
16635 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16636}
16637
348e048f
DE
16638/* Locate the .debug_info compilation unit from CU's objfile which contains
16639 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
16640
16641static struct dwarf2_per_cu_data *
b64f50a1 16642dwarf2_find_containing_comp_unit (sect_offset offset,
ae038cb0
DJ
16643 struct objfile *objfile)
16644{
16645 struct dwarf2_per_cu_data *this_cu;
16646 int low, high;
16647
ae038cb0
DJ
16648 low = 0;
16649 high = dwarf2_per_objfile->n_comp_units - 1;
16650 while (high > low)
16651 {
16652 int mid = low + (high - low) / 2;
9a619af0 16653
b64f50a1
JK
16654 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16655 >= offset.sect_off)
ae038cb0
DJ
16656 high = mid;
16657 else
16658 low = mid + 1;
16659 }
16660 gdb_assert (low == high);
b64f50a1
JK
16661 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16662 > offset.sect_off)
ae038cb0 16663 {
10b3939b 16664 if (low == 0)
8a3fe4f8
AC
16665 error (_("Dwarf Error: could not find partial DIE containing "
16666 "offset 0x%lx [in module %s]"),
b64f50a1 16667 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 16668
b64f50a1
JK
16669 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16670 <= offset.sect_off);
ae038cb0
DJ
16671 return dwarf2_per_objfile->all_comp_units[low-1];
16672 }
16673 else
16674 {
16675 this_cu = dwarf2_per_objfile->all_comp_units[low];
16676 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
16677 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16678 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16679 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
16680 return this_cu;
16681 }
16682}
16683
23745b47 16684/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 16685
9816fde3 16686static void
23745b47 16687init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 16688{
9816fde3 16689 memset (cu, 0, sizeof (*cu));
23745b47
DE
16690 per_cu->cu = cu;
16691 cu->per_cu = per_cu;
16692 cu->objfile = per_cu->objfile;
93311388 16693 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
16694}
16695
16696/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16697
16698static void
95554aad
TT
16699prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
16700 enum language pretend_language)
9816fde3
JK
16701{
16702 struct attribute *attr;
16703
16704 /* Set the language we're debugging. */
16705 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16706 if (attr)
16707 set_cu_language (DW_UNSND (attr), cu);
16708 else
9cded63f 16709 {
95554aad 16710 cu->language = pretend_language;
9cded63f
TT
16711 cu->language_defn = language_def (cu->language);
16712 }
dee91e82
DE
16713
16714 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
16715 if (attr)
16716 cu->producer = DW_STRING (attr);
93311388
DE
16717}
16718
ae038cb0
DJ
16719/* Release one cached compilation unit, CU. We unlink it from the tree
16720 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
16721 the caller is responsible for that.
16722 NOTE: DATA is a void * because this function is also used as a
16723 cleanup routine. */
ae038cb0
DJ
16724
16725static void
68dc6402 16726free_heap_comp_unit (void *data)
ae038cb0
DJ
16727{
16728 struct dwarf2_cu *cu = data;
16729
23745b47
DE
16730 gdb_assert (cu->per_cu != NULL);
16731 cu->per_cu->cu = NULL;
ae038cb0
DJ
16732 cu->per_cu = NULL;
16733
16734 obstack_free (&cu->comp_unit_obstack, NULL);
16735
16736 xfree (cu);
16737}
16738
72bf9492 16739/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 16740 when we're finished with it. We can't free the pointer itself, but be
dee91e82 16741 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
16742
16743static void
16744free_stack_comp_unit (void *data)
16745{
16746 struct dwarf2_cu *cu = data;
16747
23745b47
DE
16748 gdb_assert (cu->per_cu != NULL);
16749 cu->per_cu->cu = NULL;
16750 cu->per_cu = NULL;
16751
72bf9492
DJ
16752 obstack_free (&cu->comp_unit_obstack, NULL);
16753 cu->partial_dies = NULL;
ae038cb0
DJ
16754}
16755
16756/* Free all cached compilation units. */
16757
16758static void
16759free_cached_comp_units (void *data)
16760{
16761 struct dwarf2_per_cu_data *per_cu, **last_chain;
16762
16763 per_cu = dwarf2_per_objfile->read_in_chain;
16764 last_chain = &dwarf2_per_objfile->read_in_chain;
16765 while (per_cu != NULL)
16766 {
16767 struct dwarf2_per_cu_data *next_cu;
16768
16769 next_cu = per_cu->cu->read_in_chain;
16770
68dc6402 16771 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16772 *last_chain = next_cu;
16773
16774 per_cu = next_cu;
16775 }
16776}
16777
16778/* Increase the age counter on each cached compilation unit, and free
16779 any that are too old. */
16780
16781static void
16782age_cached_comp_units (void)
16783{
16784 struct dwarf2_per_cu_data *per_cu, **last_chain;
16785
16786 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16787 per_cu = dwarf2_per_objfile->read_in_chain;
16788 while (per_cu != NULL)
16789 {
16790 per_cu->cu->last_used ++;
16791 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16792 dwarf2_mark (per_cu->cu);
16793 per_cu = per_cu->cu->read_in_chain;
16794 }
16795
16796 per_cu = dwarf2_per_objfile->read_in_chain;
16797 last_chain = &dwarf2_per_objfile->read_in_chain;
16798 while (per_cu != NULL)
16799 {
16800 struct dwarf2_per_cu_data *next_cu;
16801
16802 next_cu = per_cu->cu->read_in_chain;
16803
16804 if (!per_cu->cu->mark)
16805 {
68dc6402 16806 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16807 *last_chain = next_cu;
16808 }
16809 else
16810 last_chain = &per_cu->cu->read_in_chain;
16811
16812 per_cu = next_cu;
16813 }
16814}
16815
16816/* Remove a single compilation unit from the cache. */
16817
16818static void
dee91e82 16819free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
16820{
16821 struct dwarf2_per_cu_data *per_cu, **last_chain;
16822
16823 per_cu = dwarf2_per_objfile->read_in_chain;
16824 last_chain = &dwarf2_per_objfile->read_in_chain;
16825 while (per_cu != NULL)
16826 {
16827 struct dwarf2_per_cu_data *next_cu;
16828
16829 next_cu = per_cu->cu->read_in_chain;
16830
dee91e82 16831 if (per_cu == target_per_cu)
ae038cb0 16832 {
68dc6402 16833 free_heap_comp_unit (per_cu->cu);
dee91e82 16834 per_cu->cu = NULL;
ae038cb0
DJ
16835 *last_chain = next_cu;
16836 break;
16837 }
16838 else
16839 last_chain = &per_cu->cu->read_in_chain;
16840
16841 per_cu = next_cu;
16842 }
16843}
16844
fe3e1990
DJ
16845/* Release all extra memory associated with OBJFILE. */
16846
16847void
16848dwarf2_free_objfile (struct objfile *objfile)
16849{
16850 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16851
16852 if (dwarf2_per_objfile == NULL)
16853 return;
16854
16855 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16856 free_cached_comp_units (NULL);
16857
7b9f3c50
DE
16858 if (dwarf2_per_objfile->quick_file_names_table)
16859 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16860
fe3e1990
DJ
16861 /* Everything else should be on the objfile obstack. */
16862}
16863
dee91e82
DE
16864/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
16865 We store these in a hash table separate from the DIEs, and preserve them
16866 when the DIEs are flushed out of cache.
16867
16868 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
16869 uniquely identify the type. A file may have multiple .debug_types sections,
16870 or the type may come from a DWO file. We have to use something in
16871 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
16872 routine, get_die_type_at_offset, from outside this file, and thus won't
16873 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
16874 of the objfile. */
1c379e20 16875
dee91e82 16876struct dwarf2_per_cu_offset_and_type
1c379e20 16877{
dee91e82 16878 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 16879 sect_offset offset;
1c379e20
DJ
16880 struct type *type;
16881};
16882
dee91e82 16883/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
16884
16885static hashval_t
dee91e82 16886per_cu_offset_and_type_hash (const void *item)
1c379e20 16887{
dee91e82 16888 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 16889
dee91e82 16890 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
16891}
16892
dee91e82 16893/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
16894
16895static int
dee91e82 16896per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 16897{
dee91e82
DE
16898 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
16899 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16900
dee91e82
DE
16901 return (ofs_lhs->per_cu == ofs_rhs->per_cu
16902 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
16903}
16904
16905/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16906 table if necessary. For convenience, return TYPE.
16907
16908 The DIEs reading must have careful ordering to:
16909 * Not cause infite loops trying to read in DIEs as a prerequisite for
16910 reading current DIE.
16911 * Not trying to dereference contents of still incompletely read in types
16912 while reading in other DIEs.
16913 * Enable referencing still incompletely read in types just by a pointer to
16914 the type without accessing its fields.
16915
16916 Therefore caller should follow these rules:
16917 * Try to fetch any prerequisite types we may need to build this DIE type
16918 before building the type and calling set_die_type.
e71ec853 16919 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16920 possible before fetching more types to complete the current type.
16921 * Make the type as complete as possible before fetching more types. */
1c379e20 16922
f792889a 16923static struct type *
1c379e20
DJ
16924set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16925{
dee91e82 16926 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 16927 struct objfile *objfile = cu->objfile;
1c379e20 16928
b4ba55a1
JB
16929 /* For Ada types, make sure that the gnat-specific data is always
16930 initialized (if not already set). There are a few types where
16931 we should not be doing so, because the type-specific area is
16932 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16933 where the type-specific area is used to store the floatformat).
16934 But this is not a problem, because the gnat-specific information
16935 is actually not needed for these types. */
16936 if (need_gnat_info (cu)
16937 && TYPE_CODE (type) != TYPE_CODE_FUNC
16938 && TYPE_CODE (type) != TYPE_CODE_FLT
16939 && !HAVE_GNAT_AUX_INFO (type))
16940 INIT_GNAT_SPECIFIC (type);
16941
dee91e82 16942 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 16943 {
dee91e82
DE
16944 dwarf2_per_objfile->die_type_hash =
16945 htab_create_alloc_ex (127,
16946 per_cu_offset_and_type_hash,
16947 per_cu_offset_and_type_eq,
16948 NULL,
16949 &objfile->objfile_obstack,
16950 hashtab_obstack_allocate,
16951 dummy_obstack_deallocate);
f792889a 16952 }
1c379e20 16953
dee91e82 16954 ofs.per_cu = cu->per_cu;
1c379e20
DJ
16955 ofs.offset = die->offset;
16956 ofs.type = type;
dee91e82
DE
16957 slot = (struct dwarf2_per_cu_offset_and_type **)
16958 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
16959 if (*slot)
16960 complaint (&symfile_complaints,
16961 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 16962 die->offset.sect_off);
673bfd45 16963 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16964 **slot = ofs;
f792889a 16965 return type;
1c379e20
DJ
16966}
16967
380bca97 16968/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 16969 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
16970
16971static struct type *
b64f50a1 16972get_die_type_at_offset (sect_offset offset,
673bfd45 16973 struct dwarf2_per_cu_data *per_cu)
1c379e20 16974{
dee91e82 16975 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 16976
dee91e82 16977 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 16978 return NULL;
1c379e20 16979
dee91e82 16980 ofs.per_cu = per_cu;
673bfd45 16981 ofs.offset = offset;
dee91e82 16982 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
16983 if (slot)
16984 return slot->type;
16985 else
16986 return NULL;
16987}
16988
673bfd45
DE
16989/* Look up the type for DIE in the appropriate type_hash table,
16990 or return NULL if DIE does not have a saved type. */
16991
16992static struct type *
16993get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16994{
16995 return get_die_type_at_offset (die->offset, cu->per_cu);
16996}
16997
10b3939b
DJ
16998/* Add a dependence relationship from CU to REF_PER_CU. */
16999
17000static void
17001dwarf2_add_dependence (struct dwarf2_cu *cu,
17002 struct dwarf2_per_cu_data *ref_per_cu)
17003{
17004 void **slot;
17005
17006 if (cu->dependencies == NULL)
17007 cu->dependencies
17008 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
17009 NULL, &cu->comp_unit_obstack,
17010 hashtab_obstack_allocate,
17011 dummy_obstack_deallocate);
17012
17013 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
17014 if (*slot == NULL)
17015 *slot = ref_per_cu;
17016}
1c379e20 17017
f504f079
DE
17018/* Subroutine of dwarf2_mark to pass to htab_traverse.
17019 Set the mark field in every compilation unit in the
ae038cb0
DJ
17020 cache that we must keep because we are keeping CU. */
17021
10b3939b
DJ
17022static int
17023dwarf2_mark_helper (void **slot, void *data)
17024{
17025 struct dwarf2_per_cu_data *per_cu;
17026
17027 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
17028
17029 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17030 reading of the chain. As such dependencies remain valid it is not much
17031 useful to track and undo them during QUIT cleanups. */
17032 if (per_cu->cu == NULL)
17033 return 1;
17034
10b3939b
DJ
17035 if (per_cu->cu->mark)
17036 return 1;
17037 per_cu->cu->mark = 1;
17038
17039 if (per_cu->cu->dependencies != NULL)
17040 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17041
17042 return 1;
17043}
17044
f504f079
DE
17045/* Set the mark field in CU and in every other compilation unit in the
17046 cache that we must keep because we are keeping CU. */
17047
ae038cb0
DJ
17048static void
17049dwarf2_mark (struct dwarf2_cu *cu)
17050{
17051 if (cu->mark)
17052 return;
17053 cu->mark = 1;
10b3939b
DJ
17054 if (cu->dependencies != NULL)
17055 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
17056}
17057
17058static void
17059dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17060{
17061 while (per_cu)
17062 {
17063 per_cu->cu->mark = 0;
17064 per_cu = per_cu->cu->read_in_chain;
17065 }
72bf9492
DJ
17066}
17067
72bf9492
DJ
17068/* Trivial hash function for partial_die_info: the hash value of a DIE
17069 is its offset in .debug_info for this objfile. */
17070
17071static hashval_t
17072partial_die_hash (const void *item)
17073{
17074 const struct partial_die_info *part_die = item;
9a619af0 17075
b64f50a1 17076 return part_die->offset.sect_off;
72bf9492
DJ
17077}
17078
17079/* Trivial comparison function for partial_die_info structures: two DIEs
17080 are equal if they have the same offset. */
17081
17082static int
17083partial_die_eq (const void *item_lhs, const void *item_rhs)
17084{
17085 const struct partial_die_info *part_die_lhs = item_lhs;
17086 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 17087
b64f50a1 17088 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
17089}
17090
ae038cb0
DJ
17091static struct cmd_list_element *set_dwarf2_cmdlist;
17092static struct cmd_list_element *show_dwarf2_cmdlist;
17093
17094static void
17095set_dwarf2_cmd (char *args, int from_tty)
17096{
17097 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17098}
17099
17100static void
17101show_dwarf2_cmd (char *args, int from_tty)
6e70227d 17102{
ae038cb0
DJ
17103 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17104}
17105
dce234bc
PP
17106/* If section described by INFO was mmapped, munmap it now. */
17107
17108static void
17109munmap_section_buffer (struct dwarf2_section_info *info)
17110{
b315ab21 17111 if (info->map_addr != NULL)
dce234bc
PP
17112 {
17113#ifdef HAVE_MMAP
b315ab21 17114 int res;
9a619af0 17115
b315ab21
TG
17116 res = munmap (info->map_addr, info->map_len);
17117 gdb_assert (res == 0);
dce234bc
PP
17118#else
17119 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 17120 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
17121#endif
17122 }
17123}
17124
17125/* munmap debug sections for OBJFILE, if necessary. */
17126
17127static void
c1bd65d0 17128dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
17129{
17130 struct dwarf2_per_objfile *data = d;
8b70b953
TT
17131 int ix;
17132 struct dwarf2_section_info *section;
9a619af0 17133
16be1145
DE
17134 /* This is sorted according to the order they're defined in to make it easier
17135 to keep in sync. */
dce234bc
PP
17136 munmap_section_buffer (&data->info);
17137 munmap_section_buffer (&data->abbrev);
17138 munmap_section_buffer (&data->line);
16be1145 17139 munmap_section_buffer (&data->loc);
dce234bc 17140 munmap_section_buffer (&data->macinfo);
cf2c3c16 17141 munmap_section_buffer (&data->macro);
16be1145 17142 munmap_section_buffer (&data->str);
dce234bc 17143 munmap_section_buffer (&data->ranges);
3019eac3 17144 munmap_section_buffer (&data->addr);
dce234bc
PP
17145 munmap_section_buffer (&data->frame);
17146 munmap_section_buffer (&data->eh_frame);
9291a0cd 17147 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
17148
17149 for (ix = 0;
17150 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17151 ++ix)
17152 munmap_section_buffer (section);
17153
95554aad
TT
17154 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17155 VEC_free (dwarf2_per_cu_ptr,
17156 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17157
8b70b953 17158 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
17159
17160 if (data->dwo_files)
17161 free_dwo_files (data->dwo_files, objfile);
9291a0cd
TT
17162}
17163
17164\f
ae2de4f8 17165/* The "save gdb-index" command. */
9291a0cd
TT
17166
17167/* The contents of the hash table we create when building the string
17168 table. */
17169struct strtab_entry
17170{
17171 offset_type offset;
17172 const char *str;
17173};
17174
559a7a62
JK
17175/* Hash function for a strtab_entry.
17176
17177 Function is used only during write_hash_table so no index format backward
17178 compatibility is needed. */
b89be57b 17179
9291a0cd
TT
17180static hashval_t
17181hash_strtab_entry (const void *e)
17182{
17183 const struct strtab_entry *entry = e;
559a7a62 17184 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
17185}
17186
17187/* Equality function for a strtab_entry. */
b89be57b 17188
9291a0cd
TT
17189static int
17190eq_strtab_entry (const void *a, const void *b)
17191{
17192 const struct strtab_entry *ea = a;
17193 const struct strtab_entry *eb = b;
17194 return !strcmp (ea->str, eb->str);
17195}
17196
17197/* Create a strtab_entry hash table. */
b89be57b 17198
9291a0cd
TT
17199static htab_t
17200create_strtab (void)
17201{
17202 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17203 xfree, xcalloc, xfree);
17204}
17205
17206/* Add a string to the constant pool. Return the string's offset in
17207 host order. */
b89be57b 17208
9291a0cd
TT
17209static offset_type
17210add_string (htab_t table, struct obstack *cpool, const char *str)
17211{
17212 void **slot;
17213 struct strtab_entry entry;
17214 struct strtab_entry *result;
17215
17216 entry.str = str;
17217 slot = htab_find_slot (table, &entry, INSERT);
17218 if (*slot)
17219 result = *slot;
17220 else
17221 {
17222 result = XNEW (struct strtab_entry);
17223 result->offset = obstack_object_size (cpool);
17224 result->str = str;
17225 obstack_grow_str0 (cpool, str);
17226 *slot = result;
17227 }
17228 return result->offset;
17229}
17230
17231/* An entry in the symbol table. */
17232struct symtab_index_entry
17233{
17234 /* The name of the symbol. */
17235 const char *name;
17236 /* The offset of the name in the constant pool. */
17237 offset_type index_offset;
17238 /* A sorted vector of the indices of all the CUs that hold an object
17239 of this name. */
17240 VEC (offset_type) *cu_indices;
17241};
17242
17243/* The symbol table. This is a power-of-2-sized hash table. */
17244struct mapped_symtab
17245{
17246 offset_type n_elements;
17247 offset_type size;
17248 struct symtab_index_entry **data;
17249};
17250
17251/* Hash function for a symtab_index_entry. */
b89be57b 17252
9291a0cd
TT
17253static hashval_t
17254hash_symtab_entry (const void *e)
17255{
17256 const struct symtab_index_entry *entry = e;
17257 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17258 sizeof (offset_type) * VEC_length (offset_type,
17259 entry->cu_indices),
17260 0);
17261}
17262
17263/* Equality function for a symtab_index_entry. */
b89be57b 17264
9291a0cd
TT
17265static int
17266eq_symtab_entry (const void *a, const void *b)
17267{
17268 const struct symtab_index_entry *ea = a;
17269 const struct symtab_index_entry *eb = b;
17270 int len = VEC_length (offset_type, ea->cu_indices);
17271 if (len != VEC_length (offset_type, eb->cu_indices))
17272 return 0;
17273 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17274 VEC_address (offset_type, eb->cu_indices),
17275 sizeof (offset_type) * len);
17276}
17277
17278/* Destroy a symtab_index_entry. */
b89be57b 17279
9291a0cd
TT
17280static void
17281delete_symtab_entry (void *p)
17282{
17283 struct symtab_index_entry *entry = p;
17284 VEC_free (offset_type, entry->cu_indices);
17285 xfree (entry);
17286}
17287
17288/* Create a hash table holding symtab_index_entry objects. */
b89be57b 17289
9291a0cd 17290static htab_t
3876f04e 17291create_symbol_hash_table (void)
9291a0cd
TT
17292{
17293 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17294 delete_symtab_entry, xcalloc, xfree);
17295}
17296
17297/* Create a new mapped symtab object. */
b89be57b 17298
9291a0cd
TT
17299static struct mapped_symtab *
17300create_mapped_symtab (void)
17301{
17302 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17303 symtab->n_elements = 0;
17304 symtab->size = 1024;
17305 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17306 return symtab;
17307}
17308
17309/* Destroy a mapped_symtab. */
b89be57b 17310
9291a0cd
TT
17311static void
17312cleanup_mapped_symtab (void *p)
17313{
17314 struct mapped_symtab *symtab = p;
17315 /* The contents of the array are freed when the other hash table is
17316 destroyed. */
17317 xfree (symtab->data);
17318 xfree (symtab);
17319}
17320
17321/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
17322 the slot.
17323
17324 Function is used only during write_hash_table so no index format backward
17325 compatibility is needed. */
b89be57b 17326
9291a0cd
TT
17327static struct symtab_index_entry **
17328find_slot (struct mapped_symtab *symtab, const char *name)
17329{
559a7a62 17330 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
17331
17332 index = hash & (symtab->size - 1);
17333 step = ((hash * 17) & (symtab->size - 1)) | 1;
17334
17335 for (;;)
17336 {
17337 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17338 return &symtab->data[index];
17339 index = (index + step) & (symtab->size - 1);
17340 }
17341}
17342
17343/* Expand SYMTAB's hash table. */
b89be57b 17344
9291a0cd
TT
17345static void
17346hash_expand (struct mapped_symtab *symtab)
17347{
17348 offset_type old_size = symtab->size;
17349 offset_type i;
17350 struct symtab_index_entry **old_entries = symtab->data;
17351
17352 symtab->size *= 2;
17353 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17354
17355 for (i = 0; i < old_size; ++i)
17356 {
17357 if (old_entries[i])
17358 {
17359 struct symtab_index_entry **slot = find_slot (symtab,
17360 old_entries[i]->name);
17361 *slot = old_entries[i];
17362 }
17363 }
17364
17365 xfree (old_entries);
17366}
17367
17368/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
17369 is the index of the CU in which the symbol appears. */
b89be57b 17370
9291a0cd
TT
17371static void
17372add_index_entry (struct mapped_symtab *symtab, const char *name,
17373 offset_type cu_index)
17374{
17375 struct symtab_index_entry **slot;
17376
17377 ++symtab->n_elements;
17378 if (4 * symtab->n_elements / 3 >= symtab->size)
17379 hash_expand (symtab);
17380
17381 slot = find_slot (symtab, name);
17382 if (!*slot)
17383 {
17384 *slot = XNEW (struct symtab_index_entry);
17385 (*slot)->name = name;
17386 (*slot)->cu_indices = NULL;
17387 }
17388 /* Don't push an index twice. Due to how we add entries we only
17389 have to check the last one. */
17390 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 17391 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
17392 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
17393}
17394
17395/* Add a vector of indices to the constant pool. */
b89be57b 17396
9291a0cd 17397static offset_type
3876f04e 17398add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
17399 struct symtab_index_entry *entry)
17400{
17401 void **slot;
17402
3876f04e 17403 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
17404 if (!*slot)
17405 {
17406 offset_type len = VEC_length (offset_type, entry->cu_indices);
17407 offset_type val = MAYBE_SWAP (len);
17408 offset_type iter;
17409 int i;
17410
17411 *slot = entry;
17412 entry->index_offset = obstack_object_size (cpool);
17413
17414 obstack_grow (cpool, &val, sizeof (val));
17415 for (i = 0;
17416 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17417 ++i)
17418 {
17419 val = MAYBE_SWAP (iter);
17420 obstack_grow (cpool, &val, sizeof (val));
17421 }
17422 }
17423 else
17424 {
17425 struct symtab_index_entry *old_entry = *slot;
17426 entry->index_offset = old_entry->index_offset;
17427 entry = old_entry;
17428 }
17429 return entry->index_offset;
17430}
17431
17432/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17433 constant pool entries going into the obstack CPOOL. */
b89be57b 17434
9291a0cd
TT
17435static void
17436write_hash_table (struct mapped_symtab *symtab,
17437 struct obstack *output, struct obstack *cpool)
17438{
17439 offset_type i;
3876f04e 17440 htab_t symbol_hash_table;
9291a0cd
TT
17441 htab_t str_table;
17442
3876f04e 17443 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 17444 str_table = create_strtab ();
3876f04e 17445
9291a0cd
TT
17446 /* We add all the index vectors to the constant pool first, to
17447 ensure alignment is ok. */
17448 for (i = 0; i < symtab->size; ++i)
17449 {
17450 if (symtab->data[i])
3876f04e 17451 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
17452 }
17453
17454 /* Now write out the hash table. */
17455 for (i = 0; i < symtab->size; ++i)
17456 {
17457 offset_type str_off, vec_off;
17458
17459 if (symtab->data[i])
17460 {
17461 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17462 vec_off = symtab->data[i]->index_offset;
17463 }
17464 else
17465 {
17466 /* While 0 is a valid constant pool index, it is not valid
17467 to have 0 for both offsets. */
17468 str_off = 0;
17469 vec_off = 0;
17470 }
17471
17472 str_off = MAYBE_SWAP (str_off);
17473 vec_off = MAYBE_SWAP (vec_off);
17474
17475 obstack_grow (output, &str_off, sizeof (str_off));
17476 obstack_grow (output, &vec_off, sizeof (vec_off));
17477 }
17478
17479 htab_delete (str_table);
3876f04e 17480 htab_delete (symbol_hash_table);
9291a0cd
TT
17481}
17482
0a5429f6
DE
17483/* Struct to map psymtab to CU index in the index file. */
17484struct psymtab_cu_index_map
17485{
17486 struct partial_symtab *psymtab;
17487 unsigned int cu_index;
17488};
17489
17490static hashval_t
17491hash_psymtab_cu_index (const void *item)
17492{
17493 const struct psymtab_cu_index_map *map = item;
17494
17495 return htab_hash_pointer (map->psymtab);
17496}
17497
17498static int
17499eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17500{
17501 const struct psymtab_cu_index_map *lhs = item_lhs;
17502 const struct psymtab_cu_index_map *rhs = item_rhs;
17503
17504 return lhs->psymtab == rhs->psymtab;
17505}
17506
17507/* Helper struct for building the address table. */
17508struct addrmap_index_data
17509{
17510 struct objfile *objfile;
17511 struct obstack *addr_obstack;
17512 htab_t cu_index_htab;
17513
17514 /* Non-zero if the previous_* fields are valid.
17515 We can't write an entry until we see the next entry (since it is only then
17516 that we know the end of the entry). */
17517 int previous_valid;
17518 /* Index of the CU in the table of all CUs in the index file. */
17519 unsigned int previous_cu_index;
0963b4bd 17520 /* Start address of the CU. */
0a5429f6
DE
17521 CORE_ADDR previous_cu_start;
17522};
17523
17524/* Write an address entry to OBSTACK. */
b89be57b 17525
9291a0cd 17526static void
0a5429f6
DE
17527add_address_entry (struct objfile *objfile, struct obstack *obstack,
17528 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 17529{
0a5429f6 17530 offset_type cu_index_to_write;
9291a0cd
TT
17531 char addr[8];
17532 CORE_ADDR baseaddr;
17533
17534 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17535
0a5429f6
DE
17536 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17537 obstack_grow (obstack, addr, 8);
17538 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17539 obstack_grow (obstack, addr, 8);
17540 cu_index_to_write = MAYBE_SWAP (cu_index);
17541 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17542}
17543
17544/* Worker function for traversing an addrmap to build the address table. */
17545
17546static int
17547add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17548{
17549 struct addrmap_index_data *data = datap;
17550 struct partial_symtab *pst = obj;
0a5429f6
DE
17551
17552 if (data->previous_valid)
17553 add_address_entry (data->objfile, data->addr_obstack,
17554 data->previous_cu_start, start_addr,
17555 data->previous_cu_index);
17556
17557 data->previous_cu_start = start_addr;
17558 if (pst != NULL)
17559 {
17560 struct psymtab_cu_index_map find_map, *map;
17561 find_map.psymtab = pst;
17562 map = htab_find (data->cu_index_htab, &find_map);
17563 gdb_assert (map != NULL);
17564 data->previous_cu_index = map->cu_index;
17565 data->previous_valid = 1;
17566 }
17567 else
17568 data->previous_valid = 0;
17569
17570 return 0;
17571}
17572
17573/* Write OBJFILE's address map to OBSTACK.
17574 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17575 in the index file. */
17576
17577static void
17578write_address_map (struct objfile *objfile, struct obstack *obstack,
17579 htab_t cu_index_htab)
17580{
17581 struct addrmap_index_data addrmap_index_data;
17582
17583 /* When writing the address table, we have to cope with the fact that
17584 the addrmap iterator only provides the start of a region; we have to
17585 wait until the next invocation to get the start of the next region. */
17586
17587 addrmap_index_data.objfile = objfile;
17588 addrmap_index_data.addr_obstack = obstack;
17589 addrmap_index_data.cu_index_htab = cu_index_htab;
17590 addrmap_index_data.previous_valid = 0;
17591
17592 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17593 &addrmap_index_data);
17594
17595 /* It's highly unlikely the last entry (end address = 0xff...ff)
17596 is valid, but we should still handle it.
17597 The end address is recorded as the start of the next region, but that
17598 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17599 anyway. */
17600 if (addrmap_index_data.previous_valid)
17601 add_address_entry (objfile, obstack,
17602 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17603 addrmap_index_data.previous_cu_index);
9291a0cd
TT
17604}
17605
17606/* Add a list of partial symbols to SYMTAB. */
b89be57b 17607
9291a0cd
TT
17608static void
17609write_psymbols (struct mapped_symtab *symtab,
987d643c 17610 htab_t psyms_seen,
9291a0cd
TT
17611 struct partial_symbol **psymp,
17612 int count,
987d643c
TT
17613 offset_type cu_index,
17614 int is_static)
9291a0cd
TT
17615{
17616 for (; count-- > 0; ++psymp)
17617 {
987d643c
TT
17618 void **slot, *lookup;
17619
9291a0cd
TT
17620 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
17621 error (_("Ada is not currently supported by the index"));
987d643c
TT
17622
17623 /* We only want to add a given psymbol once. However, we also
17624 want to account for whether it is global or static. So, we
17625 may add it twice, using slightly different values. */
17626 if (is_static)
17627 {
17628 uintptr_t val = 1 | (uintptr_t) *psymp;
17629
17630 lookup = (void *) val;
17631 }
17632 else
17633 lookup = *psymp;
17634
17635 /* Only add a given psymbol once. */
17636 slot = htab_find_slot (psyms_seen, lookup, INSERT);
17637 if (!*slot)
17638 {
17639 *slot = lookup;
bb2f58dc 17640 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 17641 }
9291a0cd
TT
17642 }
17643}
17644
17645/* Write the contents of an ("unfinished") obstack to FILE. Throw an
17646 exception if there is an error. */
b89be57b 17647
9291a0cd
TT
17648static void
17649write_obstack (FILE *file, struct obstack *obstack)
17650{
17651 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17652 file)
17653 != obstack_object_size (obstack))
17654 error (_("couldn't data write to file"));
17655}
17656
17657/* Unlink a file if the argument is not NULL. */
b89be57b 17658
9291a0cd
TT
17659static void
17660unlink_if_set (void *p)
17661{
17662 char **filename = p;
17663 if (*filename)
17664 unlink (*filename);
17665}
17666
1fd400ff
TT
17667/* A helper struct used when iterating over debug_types. */
17668struct signatured_type_index_data
17669{
17670 struct objfile *objfile;
17671 struct mapped_symtab *symtab;
17672 struct obstack *types_list;
987d643c 17673 htab_t psyms_seen;
1fd400ff
TT
17674 int cu_index;
17675};
17676
17677/* A helper function that writes a single signatured_type to an
17678 obstack. */
b89be57b 17679
1fd400ff
TT
17680static int
17681write_one_signatured_type (void **slot, void *d)
17682{
17683 struct signatured_type_index_data *info = d;
17684 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
17685 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17686 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
17687 gdb_byte val[8];
17688
17689 write_psymbols (info->symtab,
987d643c 17690 info->psyms_seen,
3e43a32a
MS
17691 info->objfile->global_psymbols.list
17692 + psymtab->globals_offset,
987d643c
TT
17693 psymtab->n_global_syms, info->cu_index,
17694 0);
1fd400ff 17695 write_psymbols (info->symtab,
987d643c 17696 info->psyms_seen,
3e43a32a
MS
17697 info->objfile->static_psymbols.list
17698 + psymtab->statics_offset,
987d643c
TT
17699 psymtab->n_static_syms, info->cu_index,
17700 1);
1fd400ff 17701
b64f50a1
JK
17702 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17703 entry->per_cu.offset.sect_off);
1fd400ff 17704 obstack_grow (info->types_list, val, 8);
3019eac3
DE
17705 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17706 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
17707 obstack_grow (info->types_list, val, 8);
17708 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17709 obstack_grow (info->types_list, val, 8);
17710
17711 ++info->cu_index;
17712
17713 return 1;
17714}
17715
95554aad
TT
17716/* Recurse into all "included" dependencies and write their symbols as
17717 if they appeared in this psymtab. */
17718
17719static void
17720recursively_write_psymbols (struct objfile *objfile,
17721 struct partial_symtab *psymtab,
17722 struct mapped_symtab *symtab,
17723 htab_t psyms_seen,
17724 offset_type cu_index)
17725{
17726 int i;
17727
17728 for (i = 0; i < psymtab->number_of_dependencies; ++i)
17729 if (psymtab->dependencies[i]->user != NULL)
17730 recursively_write_psymbols (objfile, psymtab->dependencies[i],
17731 symtab, psyms_seen, cu_index);
17732
17733 write_psymbols (symtab,
17734 psyms_seen,
17735 objfile->global_psymbols.list + psymtab->globals_offset,
17736 psymtab->n_global_syms, cu_index,
17737 0);
17738 write_psymbols (symtab,
17739 psyms_seen,
17740 objfile->static_psymbols.list + psymtab->statics_offset,
17741 psymtab->n_static_syms, cu_index,
17742 1);
17743}
17744
9291a0cd 17745/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 17746
9291a0cd
TT
17747static void
17748write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17749{
17750 struct cleanup *cleanup;
17751 char *filename, *cleanup_filename;
1fd400ff
TT
17752 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
17753 struct obstack cu_list, types_cu_list;
9291a0cd
TT
17754 int i;
17755 FILE *out_file;
17756 struct mapped_symtab *symtab;
17757 offset_type val, size_of_contents, total_len;
17758 struct stat st;
987d643c 17759 htab_t psyms_seen;
0a5429f6
DE
17760 htab_t cu_index_htab;
17761 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 17762
b4f2f049 17763 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 17764 return;
b4f2f049 17765
9291a0cd
TT
17766 if (dwarf2_per_objfile->using_index)
17767 error (_("Cannot use an index to create the index"));
17768
8b70b953
TT
17769 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17770 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17771
9291a0cd 17772 if (stat (objfile->name, &st) < 0)
7e17e088 17773 perror_with_name (objfile->name);
9291a0cd
TT
17774
17775 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17776 INDEX_SUFFIX, (char *) NULL);
17777 cleanup = make_cleanup (xfree, filename);
17778
17779 out_file = fopen (filename, "wb");
17780 if (!out_file)
17781 error (_("Can't open `%s' for writing"), filename);
17782
17783 cleanup_filename = filename;
17784 make_cleanup (unlink_if_set, &cleanup_filename);
17785
17786 symtab = create_mapped_symtab ();
17787 make_cleanup (cleanup_mapped_symtab, symtab);
17788
17789 obstack_init (&addr_obstack);
17790 make_cleanup_obstack_free (&addr_obstack);
17791
17792 obstack_init (&cu_list);
17793 make_cleanup_obstack_free (&cu_list);
17794
1fd400ff
TT
17795 obstack_init (&types_cu_list);
17796 make_cleanup_obstack_free (&types_cu_list);
17797
987d643c
TT
17798 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17799 NULL, xcalloc, xfree);
96408a79 17800 make_cleanup_htab_delete (psyms_seen);
987d643c 17801
0a5429f6
DE
17802 /* While we're scanning CU's create a table that maps a psymtab pointer
17803 (which is what addrmap records) to its index (which is what is recorded
17804 in the index file). This will later be needed to write the address
17805 table. */
17806 cu_index_htab = htab_create_alloc (100,
17807 hash_psymtab_cu_index,
17808 eq_psymtab_cu_index,
17809 NULL, xcalloc, xfree);
96408a79 17810 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
17811 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17812 xmalloc (sizeof (struct psymtab_cu_index_map)
17813 * dwarf2_per_objfile->n_comp_units);
17814 make_cleanup (xfree, psymtab_cu_index_map);
17815
17816 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
17817 work here. Also, the debug_types entries do not appear in
17818 all_comp_units, but only in their own hash table. */
9291a0cd
TT
17819 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17820 {
3e43a32a
MS
17821 struct dwarf2_per_cu_data *per_cu
17822 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 17823 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 17824 gdb_byte val[8];
0a5429f6
DE
17825 struct psymtab_cu_index_map *map;
17826 void **slot;
9291a0cd 17827
95554aad
TT
17828 if (psymtab->user == NULL)
17829 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 17830
0a5429f6
DE
17831 map = &psymtab_cu_index_map[i];
17832 map->psymtab = psymtab;
17833 map->cu_index = i;
17834 slot = htab_find_slot (cu_index_htab, map, INSERT);
17835 gdb_assert (slot != NULL);
17836 gdb_assert (*slot == NULL);
17837 *slot = map;
9291a0cd 17838
b64f50a1
JK
17839 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17840 per_cu->offset.sect_off);
9291a0cd 17841 obstack_grow (&cu_list, val, 8);
e254ef6a 17842 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
17843 obstack_grow (&cu_list, val, 8);
17844 }
17845
0a5429f6
DE
17846 /* Dump the address map. */
17847 write_address_map (objfile, &addr_obstack, cu_index_htab);
17848
1fd400ff
TT
17849 /* Write out the .debug_type entries, if any. */
17850 if (dwarf2_per_objfile->signatured_types)
17851 {
17852 struct signatured_type_index_data sig_data;
17853
17854 sig_data.objfile = objfile;
17855 sig_data.symtab = symtab;
17856 sig_data.types_list = &types_cu_list;
987d643c 17857 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17858 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17859 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17860 write_one_signatured_type, &sig_data);
17861 }
17862
9291a0cd
TT
17863 obstack_init (&constant_pool);
17864 make_cleanup_obstack_free (&constant_pool);
17865 obstack_init (&symtab_obstack);
17866 make_cleanup_obstack_free (&symtab_obstack);
17867 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17868
17869 obstack_init (&contents);
17870 make_cleanup_obstack_free (&contents);
1fd400ff 17871 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17872 total_len = size_of_contents;
17873
17874 /* The version number. */
481860b3 17875 val = MAYBE_SWAP (6);
9291a0cd
TT
17876 obstack_grow (&contents, &val, sizeof (val));
17877
17878 /* The offset of the CU list from the start of the file. */
17879 val = MAYBE_SWAP (total_len);
17880 obstack_grow (&contents, &val, sizeof (val));
17881 total_len += obstack_object_size (&cu_list);
17882
1fd400ff
TT
17883 /* The offset of the types CU list from the start of the file. */
17884 val = MAYBE_SWAP (total_len);
17885 obstack_grow (&contents, &val, sizeof (val));
17886 total_len += obstack_object_size (&types_cu_list);
17887
9291a0cd
TT
17888 /* The offset of the address table from the start of the file. */
17889 val = MAYBE_SWAP (total_len);
17890 obstack_grow (&contents, &val, sizeof (val));
17891 total_len += obstack_object_size (&addr_obstack);
17892
17893 /* The offset of the symbol table from the start of the file. */
17894 val = MAYBE_SWAP (total_len);
17895 obstack_grow (&contents, &val, sizeof (val));
17896 total_len += obstack_object_size (&symtab_obstack);
17897
17898 /* The offset of the constant pool from the start of the file. */
17899 val = MAYBE_SWAP (total_len);
17900 obstack_grow (&contents, &val, sizeof (val));
17901 total_len += obstack_object_size (&constant_pool);
17902
17903 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17904
17905 write_obstack (out_file, &contents);
17906 write_obstack (out_file, &cu_list);
1fd400ff 17907 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17908 write_obstack (out_file, &addr_obstack);
17909 write_obstack (out_file, &symtab_obstack);
17910 write_obstack (out_file, &constant_pool);
17911
17912 fclose (out_file);
17913
17914 /* We want to keep the file, so we set cleanup_filename to NULL
17915 here. See unlink_if_set. */
17916 cleanup_filename = NULL;
17917
17918 do_cleanups (cleanup);
17919}
17920
90476074
TT
17921/* Implementation of the `save gdb-index' command.
17922
17923 Note that the file format used by this command is documented in the
17924 GDB manual. Any changes here must be documented there. */
11570e71 17925
9291a0cd
TT
17926static void
17927save_gdb_index_command (char *arg, int from_tty)
17928{
17929 struct objfile *objfile;
17930
17931 if (!arg || !*arg)
96d19272 17932 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17933
17934 ALL_OBJFILES (objfile)
17935 {
17936 struct stat st;
17937
17938 /* If the objfile does not correspond to an actual file, skip it. */
17939 if (stat (objfile->name, &st) < 0)
17940 continue;
17941
17942 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17943 if (dwarf2_per_objfile)
17944 {
17945 volatile struct gdb_exception except;
17946
17947 TRY_CATCH (except, RETURN_MASK_ERROR)
17948 {
17949 write_psymtabs_to_index (objfile, arg);
17950 }
17951 if (except.reason < 0)
17952 exception_fprintf (gdb_stderr, except,
17953 _("Error while writing index for `%s': "),
17954 objfile->name);
17955 }
17956 }
dce234bc
PP
17957}
17958
9291a0cd
TT
17959\f
17960
9eae7c52
TT
17961int dwarf2_always_disassemble;
17962
17963static void
17964show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17965 struct cmd_list_element *c, const char *value)
17966{
3e43a32a
MS
17967 fprintf_filtered (file,
17968 _("Whether to always disassemble "
17969 "DWARF expressions is %s.\n"),
9eae7c52
TT
17970 value);
17971}
17972
900e11f9
JK
17973static void
17974show_check_physname (struct ui_file *file, int from_tty,
17975 struct cmd_list_element *c, const char *value)
17976{
17977 fprintf_filtered (file,
17978 _("Whether to check \"physname\" is %s.\n"),
17979 value);
17980}
17981
6502dd73
DJ
17982void _initialize_dwarf2_read (void);
17983
17984void
17985_initialize_dwarf2_read (void)
17986{
96d19272
JK
17987 struct cmd_list_element *c;
17988
dce234bc 17989 dwarf2_objfile_data_key
c1bd65d0 17990 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 17991
1bedd215
AC
17992 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17993Set DWARF 2 specific variables.\n\
17994Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
17995 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17996 0/*allow-unknown*/, &maintenance_set_cmdlist);
17997
1bedd215
AC
17998 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17999Show DWARF 2 specific variables\n\
18000Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
18001 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
18002 0/*allow-unknown*/, &maintenance_show_cmdlist);
18003
18004 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
18005 &dwarf2_max_cache_age, _("\
18006Set the upper bound on the age of cached dwarf2 compilation units."), _("\
18007Show the upper bound on the age of cached dwarf2 compilation units."), _("\
18008A higher limit means that cached compilation units will be stored\n\
18009in memory longer, and more total memory will be used. Zero disables\n\
18010caching, which can slow down startup."),
2c5b56ce 18011 NULL,
920d2a44 18012 show_dwarf2_max_cache_age,
2c5b56ce 18013 &set_dwarf2_cmdlist,
ae038cb0 18014 &show_dwarf2_cmdlist);
d97bc12b 18015
9eae7c52
TT
18016 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
18017 &dwarf2_always_disassemble, _("\
18018Set whether `info address' always disassembles DWARF expressions."), _("\
18019Show whether `info address' always disassembles DWARF expressions."), _("\
18020When enabled, DWARF expressions are always printed in an assembly-like\n\
18021syntax. When disabled, expressions will be printed in a more\n\
18022conversational style, when possible."),
18023 NULL,
18024 show_dwarf2_always_disassemble,
18025 &set_dwarf2_cmdlist,
18026 &show_dwarf2_cmdlist);
18027
d97bc12b
DE
18028 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18029Set debugging of the dwarf2 DIE reader."), _("\
18030Show debugging of the dwarf2 DIE reader."), _("\
18031When enabled (non-zero), DIEs are dumped after they are read in.\n\
18032The value is the maximum depth to print."),
18033 NULL,
18034 NULL,
18035 &setdebuglist, &showdebuglist);
9291a0cd 18036
900e11f9
JK
18037 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18038Set cross-checking of \"physname\" code against demangler."), _("\
18039Show cross-checking of \"physname\" code against demangler."), _("\
18040When enabled, GDB's internal \"physname\" code is checked against\n\
18041the demangler."),
18042 NULL, show_check_physname,
18043 &setdebuglist, &showdebuglist);
18044
96d19272 18045 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 18046 _("\
fc1a9d6e 18047Save a gdb-index file.\n\
11570e71 18048Usage: save gdb-index DIRECTORY"),
96d19272
JK
18049 &save_cmdlist);
18050 set_cmd_completer (c, filename_completer);
6502dd73 18051}
This page took 2.496487 seconds and 4 git commands to generate.