*** empty log message ***
[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
07261596
TT
2452 /* Our callers never want to match partial units -- instead they
2453 will match the enclosing full CU. */
2454 if (comp_unit_die->tag == DW_TAG_partial_unit)
2455 {
2456 this_cu->v.quick->no_file_data = 1;
2457 return;
2458 }
2459
7b9f3c50
DE
2460 lh = NULL;
2461 slot = NULL;
2462 line_offset = 0;
dee91e82
DE
2463
2464 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
2465 if (attr)
2466 {
7b9f3c50
DE
2467 struct quick_file_names find_entry;
2468
2469 line_offset = DW_UNSND (attr);
2470
2471 /* We may have already read in this line header (TU line header sharing).
2472 If we have we're done. */
2473 find_entry.offset = line_offset;
2474 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2475 &find_entry, INSERT);
2476 if (*slot != NULL)
2477 {
7b9f3c50 2478 this_cu->v.quick->file_names = *slot;
dee91e82 2479 return;
7b9f3c50
DE
2480 }
2481
3019eac3 2482 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
2483 }
2484 if (lh == NULL)
2485 {
7b9f3c50 2486 this_cu->v.quick->no_file_data = 1;
dee91e82 2487 return;
9291a0cd
TT
2488 }
2489
7b9f3c50
DE
2490 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2491 qfn->offset = line_offset;
2492 gdb_assert (slot != NULL);
2493 *slot = qfn;
9291a0cd 2494
dee91e82 2495 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
9291a0cd 2496
7b9f3c50
DE
2497 qfn->num_file_names = lh->num_file_names;
2498 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2499 lh->num_file_names * sizeof (char *));
9291a0cd 2500 for (i = 0; i < lh->num_file_names; ++i)
7b9f3c50
DE
2501 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2502 qfn->real_names = NULL;
9291a0cd 2503
7b9f3c50 2504 free_line_header (lh);
7b9f3c50
DE
2505
2506 this_cu->v.quick->file_names = qfn;
dee91e82
DE
2507}
2508
2509/* A helper for the "quick" functions which attempts to read the line
2510 table for THIS_CU. */
2511
2512static struct quick_file_names *
2513dw2_get_file_names (struct objfile *objfile,
2514 struct dwarf2_per_cu_data *this_cu)
2515{
2516 if (this_cu->v.quick->file_names != NULL)
2517 return this_cu->v.quick->file_names;
2518 /* If we know there is no line data, no point in looking again. */
2519 if (this_cu->v.quick->no_file_data)
2520 return NULL;
2521
3019eac3
DE
2522 /* If DWO files are in use, we can still find the DW_AT_stmt_list attribute
2523 in the stub for CUs, there's is no need to lookup the DWO file.
2524 However, that's not the case for TUs where DW_AT_stmt_list lives in the
2525 DWO file. */
2526 if (this_cu->is_debug_types)
fd820528 2527 init_cutu_and_read_dies (this_cu, 0, 0, dw2_get_file_names_reader, NULL);
3019eac3
DE
2528 else
2529 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
2530
2531 if (this_cu->v.quick->no_file_data)
2532 return NULL;
2533 return this_cu->v.quick->file_names;
9291a0cd
TT
2534}
2535
2536/* A helper for the "quick" functions which computes and caches the
7b9f3c50 2537 real path for a given file name from the line table. */
2fdf6df6 2538
9291a0cd 2539static const char *
7b9f3c50
DE
2540dw2_get_real_path (struct objfile *objfile,
2541 struct quick_file_names *qfn, int index)
9291a0cd 2542{
7b9f3c50
DE
2543 if (qfn->real_names == NULL)
2544 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2545 qfn->num_file_names, sizeof (char *));
9291a0cd 2546
7b9f3c50
DE
2547 if (qfn->real_names[index] == NULL)
2548 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
9291a0cd 2549
7b9f3c50 2550 return qfn->real_names[index];
9291a0cd
TT
2551}
2552
2553static struct symtab *
2554dw2_find_last_source_symtab (struct objfile *objfile)
2555{
2556 int index;
ae2de4f8 2557
9291a0cd
TT
2558 dw2_setup (objfile);
2559 index = dwarf2_per_objfile->n_comp_units - 1;
a0f42c21 2560 return dw2_instantiate_symtab (dw2_get_cu (index));
9291a0cd
TT
2561}
2562
7b9f3c50
DE
2563/* Traversal function for dw2_forget_cached_source_info. */
2564
2565static int
2566dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 2567{
7b9f3c50 2568 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 2569
7b9f3c50 2570 if (file_data->real_names)
9291a0cd 2571 {
7b9f3c50 2572 int i;
9291a0cd 2573
7b9f3c50 2574 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 2575 {
7b9f3c50
DE
2576 xfree ((void*) file_data->real_names[i]);
2577 file_data->real_names[i] = NULL;
9291a0cd
TT
2578 }
2579 }
7b9f3c50
DE
2580
2581 return 1;
2582}
2583
2584static void
2585dw2_forget_cached_source_info (struct objfile *objfile)
2586{
2587 dw2_setup (objfile);
2588
2589 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2590 dw2_free_cached_file_names, NULL);
9291a0cd
TT
2591}
2592
f8eba3c6
TT
2593/* Helper function for dw2_map_symtabs_matching_filename that expands
2594 the symtabs and calls the iterator. */
2595
2596static int
2597dw2_map_expand_apply (struct objfile *objfile,
2598 struct dwarf2_per_cu_data *per_cu,
2599 const char *name,
2600 const char *full_path, const char *real_path,
2601 int (*callback) (struct symtab *, void *),
2602 void *data)
2603{
2604 struct symtab *last_made = objfile->symtabs;
2605
2606 /* Don't visit already-expanded CUs. */
2607 if (per_cu->v.quick->symtab)
2608 return 0;
2609
2610 /* This may expand more than one symtab, and we want to iterate over
2611 all of them. */
a0f42c21 2612 dw2_instantiate_symtab (per_cu);
f8eba3c6
TT
2613
2614 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2615 objfile->symtabs, last_made);
2616}
2617
2618/* Implementation of the map_symtabs_matching_filename method. */
2619
9291a0cd 2620static int
f8eba3c6
TT
2621dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2622 const char *full_path, const char *real_path,
2623 int (*callback) (struct symtab *, void *),
2624 void *data)
9291a0cd
TT
2625{
2626 int i;
c011a4f4 2627 const char *name_basename = lbasename (name);
4aac40c8
TT
2628 int name_len = strlen (name);
2629 int is_abs = IS_ABSOLUTE_PATH (name);
9291a0cd
TT
2630
2631 dw2_setup (objfile);
ae2de4f8 2632
1fd400ff 2633 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2634 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
2635 {
2636 int j;
e254ef6a 2637 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2638 struct quick_file_names *file_data;
9291a0cd 2639
3d7bb9d9 2640 /* We only need to look at symtabs not already expanded. */
e254ef6a 2641 if (per_cu->v.quick->symtab)
9291a0cd
TT
2642 continue;
2643
7b9f3c50
DE
2644 file_data = dw2_get_file_names (objfile, per_cu);
2645 if (file_data == NULL)
9291a0cd
TT
2646 continue;
2647
7b9f3c50 2648 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2649 {
7b9f3c50 2650 const char *this_name = file_data->file_names[j];
9291a0cd 2651
4aac40c8
TT
2652 if (FILENAME_CMP (name, this_name) == 0
2653 || (!is_abs && compare_filenames_for_search (this_name,
2654 name, name_len)))
9291a0cd 2655 {
f8eba3c6
TT
2656 if (dw2_map_expand_apply (objfile, per_cu,
2657 name, full_path, real_path,
2658 callback, data))
2659 return 1;
4aac40c8 2660 }
9291a0cd 2661
c011a4f4
DE
2662 /* Before we invoke realpath, which can get expensive when many
2663 files are involved, do a quick comparison of the basenames. */
2664 if (! basenames_may_differ
2665 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2666 continue;
2667
9291a0cd
TT
2668 if (full_path != NULL)
2669 {
7b9f3c50
DE
2670 const char *this_real_name = dw2_get_real_path (objfile,
2671 file_data, j);
9291a0cd 2672
7b9f3c50 2673 if (this_real_name != NULL
4aac40c8
TT
2674 && (FILENAME_CMP (full_path, this_real_name) == 0
2675 || (!is_abs
2676 && compare_filenames_for_search (this_real_name,
2677 name, name_len))))
9291a0cd 2678 {
f8eba3c6
TT
2679 if (dw2_map_expand_apply (objfile, per_cu,
2680 name, full_path, real_path,
2681 callback, data))
2682 return 1;
9291a0cd
TT
2683 }
2684 }
2685
2686 if (real_path != NULL)
2687 {
7b9f3c50
DE
2688 const char *this_real_name = dw2_get_real_path (objfile,
2689 file_data, j);
9291a0cd 2690
7b9f3c50 2691 if (this_real_name != NULL
4aac40c8
TT
2692 && (FILENAME_CMP (real_path, this_real_name) == 0
2693 || (!is_abs
2694 && compare_filenames_for_search (this_real_name,
2695 name, name_len))))
9291a0cd 2696 {
f8eba3c6
TT
2697 if (dw2_map_expand_apply (objfile, per_cu,
2698 name, full_path, real_path,
2699 callback, data))
2700 return 1;
9291a0cd
TT
2701 }
2702 }
2703 }
2704 }
2705
9291a0cd
TT
2706 return 0;
2707}
2708
2709static struct symtab *
2710dw2_lookup_symbol (struct objfile *objfile, int block_index,
2711 const char *name, domain_enum domain)
2712{
774b6a14 2713 /* We do all the work in the pre_expand_symtabs_matching hook
9291a0cd
TT
2714 instead. */
2715 return NULL;
2716}
2717
2718/* A helper function that expands all symtabs that hold an object
2719 named NAME. */
2fdf6df6 2720
9291a0cd
TT
2721static void
2722dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2723{
2724 dw2_setup (objfile);
2725
ae2de4f8 2726 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2727 if (dwarf2_per_objfile->index_table)
2728 {
2729 offset_type *vec;
2730
2731 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2732 name, &vec))
2733 {
2734 offset_type i, len = MAYBE_SWAP (*vec);
2735 for (i = 0; i < len; ++i)
2736 {
2737 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
e254ef6a 2738 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
1fd400ff 2739
a0f42c21 2740 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2741 }
2742 }
2743 }
2744}
2745
774b6a14
TT
2746static void
2747dw2_pre_expand_symtabs_matching (struct objfile *objfile,
8903c50d 2748 enum block_enum block_kind, const char *name,
774b6a14 2749 domain_enum domain)
9291a0cd 2750{
774b6a14 2751 dw2_do_expand_symtabs_matching (objfile, name);
9291a0cd
TT
2752}
2753
2754static void
2755dw2_print_stats (struct objfile *objfile)
2756{
2757 int i, count;
2758
2759 dw2_setup (objfile);
2760 count = 0;
1fd400ff 2761 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2762 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2763 {
e254ef6a 2764 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2765
e254ef6a 2766 if (!per_cu->v.quick->symtab)
9291a0cd
TT
2767 ++count;
2768 }
2769 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2770}
2771
2772static void
2773dw2_dump (struct objfile *objfile)
2774{
2775 /* Nothing worth printing. */
2776}
2777
2778static void
2779dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2780 struct section_offsets *delta)
2781{
2782 /* There's nothing to relocate here. */
2783}
2784
2785static void
2786dw2_expand_symtabs_for_function (struct objfile *objfile,
2787 const char *func_name)
2788{
2789 dw2_do_expand_symtabs_matching (objfile, func_name);
2790}
2791
2792static void
2793dw2_expand_all_symtabs (struct objfile *objfile)
2794{
2795 int i;
2796
2797 dw2_setup (objfile);
1fd400ff
TT
2798
2799 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 2800 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 2801 {
e254ef6a 2802 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 2803
a0f42c21 2804 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2805 }
2806}
2807
2808static void
2809dw2_expand_symtabs_with_filename (struct objfile *objfile,
2810 const char *filename)
2811{
2812 int i;
2813
2814 dw2_setup (objfile);
d4637a04
DE
2815
2816 /* We don't need to consider type units here.
2817 This is only called for examining code, e.g. expand_line_sal.
2818 There can be an order of magnitude (or more) more type units
2819 than comp units, and we avoid them if we can. */
2820
2821 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
9291a0cd
TT
2822 {
2823 int j;
e254ef6a 2824 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 2825 struct quick_file_names *file_data;
9291a0cd 2826
3d7bb9d9 2827 /* We only need to look at symtabs not already expanded. */
e254ef6a 2828 if (per_cu->v.quick->symtab)
9291a0cd
TT
2829 continue;
2830
7b9f3c50
DE
2831 file_data = dw2_get_file_names (objfile, per_cu);
2832 if (file_data == NULL)
9291a0cd
TT
2833 continue;
2834
7b9f3c50 2835 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 2836 {
7b9f3c50 2837 const char *this_name = file_data->file_names[j];
1ef75ecc 2838 if (FILENAME_CMP (this_name, filename) == 0)
9291a0cd 2839 {
a0f42c21 2840 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
2841 break;
2842 }
2843 }
2844 }
2845}
2846
356d9f9d
TT
2847/* A helper function for dw2_find_symbol_file that finds the primary
2848 file name for a given CU. This is a die_reader_func. */
2849
2850static void
2851dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
2852 gdb_byte *info_ptr,
2853 struct die_info *comp_unit_die,
2854 int has_children,
2855 void *data)
2856{
2857 const char **result_ptr = data;
2858 struct dwarf2_cu *cu = reader->cu;
2859 struct attribute *attr;
2860
2861 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
2862 if (attr == NULL)
2863 *result_ptr = NULL;
2864 else
2865 *result_ptr = DW_STRING (attr);
2866}
2867
dd786858 2868static const char *
9291a0cd
TT
2869dw2_find_symbol_file (struct objfile *objfile, const char *name)
2870{
e254ef6a 2871 struct dwarf2_per_cu_data *per_cu;
9291a0cd 2872 offset_type *vec;
7b9f3c50 2873 struct quick_file_names *file_data;
356d9f9d 2874 const char *filename;
9291a0cd
TT
2875
2876 dw2_setup (objfile);
2877
ae2de4f8 2878 /* index_table is NULL if OBJF_READNOW. */
9291a0cd 2879 if (!dwarf2_per_objfile->index_table)
96408a79
SA
2880 {
2881 struct symtab *s;
2882
d790cf0a
DE
2883 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
2884 {
2885 struct blockvector *bv = BLOCKVECTOR (s);
2886 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2887 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2888
2889 if (sym)
2890 return sym->symtab->filename;
2891 }
96408a79
SA
2892 return NULL;
2893 }
9291a0cd
TT
2894
2895 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2896 name, &vec))
2897 return NULL;
2898
2899 /* Note that this just looks at the very first one named NAME -- but
2900 actually we are looking for a function. find_main_filename
2901 should be rewritten so that it doesn't require a custom hook. It
2902 could just use the ordinary symbol tables. */
2903 /* vec[0] is the length, which must always be >0. */
e254ef6a 2904 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
9291a0cd 2905
356d9f9d
TT
2906 if (per_cu->v.quick->symtab != NULL)
2907 return per_cu->v.quick->symtab->filename;
2908
663b969e
TT
2909 init_cutu_and_read_dies (per_cu, 0, 0, dw2_get_primary_filename_reader,
2910 &filename);
9291a0cd 2911
356d9f9d 2912 return filename;
9291a0cd
TT
2913}
2914
2915static void
40658b94
PH
2916dw2_map_matching_symbols (const char * name, domain_enum namespace,
2917 struct objfile *objfile, int global,
2918 int (*callback) (struct block *,
2919 struct symbol *, void *),
2edb89d3
JK
2920 void *data, symbol_compare_ftype *match,
2921 symbol_compare_ftype *ordered_compare)
9291a0cd 2922{
40658b94 2923 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2924 current language is Ada for a non-Ada objfile using GNU index. As Ada
2925 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2926}
2927
2928static void
f8eba3c6
TT
2929dw2_expand_symtabs_matching
2930 (struct objfile *objfile,
2931 int (*file_matcher) (const char *, void *),
e078317b 2932 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
2933 enum search_domain kind,
2934 void *data)
9291a0cd
TT
2935{
2936 int i;
2937 offset_type iter;
4b5246aa 2938 struct mapped_index *index;
9291a0cd
TT
2939
2940 dw2_setup (objfile);
ae2de4f8
DE
2941
2942 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2943 if (!dwarf2_per_objfile->index_table)
2944 return;
4b5246aa 2945 index = dwarf2_per_objfile->index_table;
9291a0cd 2946
7b08b9eb 2947 if (file_matcher != NULL)
24c79950
TT
2948 {
2949 struct cleanup *cleanup;
2950 htab_t visited_found, visited_not_found;
2951
2952 visited_found = htab_create_alloc (10,
2953 htab_hash_pointer, htab_eq_pointer,
2954 NULL, xcalloc, xfree);
2955 cleanup = make_cleanup_htab_delete (visited_found);
2956 visited_not_found = htab_create_alloc (10,
2957 htab_hash_pointer, htab_eq_pointer,
2958 NULL, xcalloc, xfree);
2959 make_cleanup_htab_delete (visited_not_found);
2960
2961 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2962 + dwarf2_per_objfile->n_type_units); ++i)
2963 {
2964 int j;
2965 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2966 struct quick_file_names *file_data;
2967 void **slot;
7b08b9eb 2968
24c79950 2969 per_cu->v.quick->mark = 0;
3d7bb9d9 2970
24c79950
TT
2971 /* We only need to look at symtabs not already expanded. */
2972 if (per_cu->v.quick->symtab)
2973 continue;
7b08b9eb 2974
24c79950
TT
2975 file_data = dw2_get_file_names (objfile, per_cu);
2976 if (file_data == NULL)
2977 continue;
7b08b9eb 2978
24c79950
TT
2979 if (htab_find (visited_not_found, file_data) != NULL)
2980 continue;
2981 else if (htab_find (visited_found, file_data) != NULL)
2982 {
2983 per_cu->v.quick->mark = 1;
2984 continue;
2985 }
2986
2987 for (j = 0; j < file_data->num_file_names; ++j)
2988 {
2989 if (file_matcher (file_data->file_names[j], data))
2990 {
2991 per_cu->v.quick->mark = 1;
2992 break;
2993 }
2994 }
2995
2996 slot = htab_find_slot (per_cu->v.quick->mark
2997 ? visited_found
2998 : visited_not_found,
2999 file_data, INSERT);
3000 *slot = file_data;
3001 }
3002
3003 do_cleanups (cleanup);
3004 }
9291a0cd 3005
3876f04e 3006 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3007 {
3008 offset_type idx = 2 * iter;
3009 const char *name;
3010 offset_type *vec, vec_len, vec_idx;
3011
3876f04e 3012 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3013 continue;
3014
3876f04e 3015 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3016
e078317b 3017 if (! (*name_matcher) (name, data))
9291a0cd
TT
3018 continue;
3019
3020 /* The name was matched, now expand corresponding CUs that were
3021 marked. */
4b5246aa 3022 vec = (offset_type *) (index->constant_pool
3876f04e 3023 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3024 vec_len = MAYBE_SWAP (vec[0]);
3025 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3026 {
e254ef6a 3027 struct dwarf2_per_cu_data *per_cu;
1fd400ff 3028
e254ef6a 3029 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 3030 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3031 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3032 }
3033 }
3034}
3035
9703b513
TT
3036/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3037 symtab. */
3038
3039static struct symtab *
3040recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3041{
3042 int i;
3043
3044 if (BLOCKVECTOR (symtab) != NULL
3045 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3046 return symtab;
3047
a3ec0bb1
DE
3048 if (symtab->includes == NULL)
3049 return NULL;
3050
9703b513
TT
3051 for (i = 0; symtab->includes[i]; ++i)
3052 {
a3ec0bb1 3053 struct symtab *s = symtab->includes[i];
9703b513
TT
3054
3055 s = recursively_find_pc_sect_symtab (s, pc);
3056 if (s != NULL)
3057 return s;
3058 }
3059
3060 return NULL;
3061}
3062
9291a0cd
TT
3063static struct symtab *
3064dw2_find_pc_sect_symtab (struct objfile *objfile,
3065 struct minimal_symbol *msymbol,
3066 CORE_ADDR pc,
3067 struct obj_section *section,
3068 int warn_if_readin)
3069{
3070 struct dwarf2_per_cu_data *data;
9703b513 3071 struct symtab *result;
9291a0cd
TT
3072
3073 dw2_setup (objfile);
3074
3075 if (!objfile->psymtabs_addrmap)
3076 return NULL;
3077
3078 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3079 if (!data)
3080 return NULL;
3081
3082 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3083 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3084 paddress (get_objfile_arch (objfile), pc));
3085
9703b513
TT
3086 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3087 gdb_assert (result != NULL);
3088 return result;
9291a0cd
TT
3089}
3090
9291a0cd 3091static void
44b13c5a 3092dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3093 void *data, int need_fullname)
9291a0cd
TT
3094{
3095 int i;
24c79950
TT
3096 struct cleanup *cleanup;
3097 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3098 NULL, xcalloc, xfree);
9291a0cd 3099
24c79950 3100 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3101 dw2_setup (objfile);
ae2de4f8 3102
24c79950
TT
3103 /* We can ignore file names coming from already-expanded CUs. */
3104 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3105 + dwarf2_per_objfile->n_type_units); ++i)
3106 {
3107 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3108
3109 if (per_cu->v.quick->symtab)
3110 {
3111 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3112 INSERT);
3113
3114 *slot = per_cu->v.quick->file_names;
3115 }
3116 }
3117
1fd400ff 3118 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3119 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
3120 {
3121 int j;
e254ef6a 3122 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3123 struct quick_file_names *file_data;
24c79950 3124 void **slot;
9291a0cd 3125
3d7bb9d9 3126 /* We only need to look at symtabs not already expanded. */
e254ef6a 3127 if (per_cu->v.quick->symtab)
9291a0cd
TT
3128 continue;
3129
7b9f3c50
DE
3130 file_data = dw2_get_file_names (objfile, per_cu);
3131 if (file_data == NULL)
9291a0cd
TT
3132 continue;
3133
24c79950
TT
3134 slot = htab_find_slot (visited, file_data, INSERT);
3135 if (*slot)
3136 {
3137 /* Already visited. */
3138 continue;
3139 }
3140 *slot = file_data;
3141
7b9f3c50 3142 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3143 {
74e2f255
DE
3144 const char *this_real_name;
3145
3146 if (need_fullname)
3147 this_real_name = dw2_get_real_path (objfile, file_data, j);
3148 else
3149 this_real_name = NULL;
7b9f3c50 3150 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3151 }
3152 }
24c79950
TT
3153
3154 do_cleanups (cleanup);
9291a0cd
TT
3155}
3156
3157static int
3158dw2_has_symbols (struct objfile *objfile)
3159{
3160 return 1;
3161}
3162
3163const struct quick_symbol_functions dwarf2_gdb_index_functions =
3164{
3165 dw2_has_symbols,
3166 dw2_find_last_source_symtab,
3167 dw2_forget_cached_source_info,
f8eba3c6 3168 dw2_map_symtabs_matching_filename,
9291a0cd 3169 dw2_lookup_symbol,
774b6a14 3170 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3171 dw2_print_stats,
3172 dw2_dump,
3173 dw2_relocate,
3174 dw2_expand_symtabs_for_function,
3175 dw2_expand_all_symtabs,
3176 dw2_expand_symtabs_with_filename,
3177 dw2_find_symbol_file,
40658b94 3178 dw2_map_matching_symbols,
9291a0cd
TT
3179 dw2_expand_symtabs_matching,
3180 dw2_find_pc_sect_symtab,
9291a0cd
TT
3181 dw2_map_symbol_filenames
3182};
3183
3184/* Initialize for reading DWARF for this objfile. Return 0 if this
3185 file will use psymtabs, or 1 if using the GNU index. */
3186
3187int
3188dwarf2_initialize_objfile (struct objfile *objfile)
3189{
3190 /* If we're about to read full symbols, don't bother with the
3191 indices. In this case we also don't care if some other debug
3192 format is making psymtabs, because they are all about to be
3193 expanded anyway. */
3194 if ((objfile->flags & OBJF_READNOW))
3195 {
3196 int i;
3197
3198 dwarf2_per_objfile->using_index = 1;
3199 create_all_comp_units (objfile);
0e50663e 3200 create_all_type_units (objfile);
7b9f3c50
DE
3201 dwarf2_per_objfile->quick_file_names_table =
3202 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3203
1fd400ff 3204 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3205 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3206 {
e254ef6a 3207 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3208
e254ef6a
DE
3209 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3210 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3211 }
3212
3213 /* Return 1 so that gdb sees the "quick" functions. However,
3214 these functions will be no-ops because we will have expanded
3215 all symtabs. */
3216 return 1;
3217 }
3218
3219 if (dwarf2_read_index (objfile))
3220 return 1;
3221
9291a0cd
TT
3222 return 0;
3223}
3224
3225\f
3226
dce234bc
PP
3227/* Build a partial symbol table. */
3228
3229void
f29dff0a 3230dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3231{
f29dff0a 3232 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3233 {
3234 init_psymbol_list (objfile, 1024);
3235 }
3236
d146bf1e 3237 dwarf2_build_psymtabs_hard (objfile);
c906108c 3238}
c906108c 3239
45452591
DE
3240/* Return TRUE if OFFSET is within CU_HEADER. */
3241
3242static inline int
b64f50a1 3243offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3244{
b64f50a1
JK
3245 sect_offset bottom = { cu_header->offset.sect_off };
3246 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3247 + cu_header->initial_length_size) };
9a619af0 3248
b64f50a1 3249 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3250}
3251
93311388
DE
3252/* Read in the comp unit header information from the debug_info at info_ptr.
3253 NOTE: This leaves members offset, first_die_offset to be filled in
3254 by the caller. */
107d2387 3255
fe1b8b76 3256static gdb_byte *
107d2387 3257read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3258 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3259{
3260 int signed_addr;
891d2f0b 3261 unsigned int bytes_read;
c764a876
DE
3262
3263 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3264 cu_header->initial_length_size = bytes_read;
3265 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3266 info_ptr += bytes_read;
107d2387
AC
3267 cu_header->version = read_2_bytes (abfd, info_ptr);
3268 info_ptr += 2;
b64f50a1
JK
3269 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3270 &bytes_read);
613e1657 3271 info_ptr += bytes_read;
107d2387
AC
3272 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3273 info_ptr += 1;
3274 signed_addr = bfd_get_sign_extend_vma (abfd);
3275 if (signed_addr < 0)
8e65ff28 3276 internal_error (__FILE__, __LINE__,
e2e0b3e5 3277 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3278 cu_header->signed_addr_p = signed_addr;
c764a876 3279
107d2387
AC
3280 return info_ptr;
3281}
3282
9ff913ba
DE
3283/* Subroutine of read_and_check_comp_unit_head and
3284 read_and_check_type_unit_head to simplify them.
3285 Perform various error checking on the header. */
3286
3287static void
3288error_check_comp_unit_head (struct comp_unit_head *header,
3289 struct dwarf2_section_info *section)
3290{
3291 bfd *abfd = section->asection->owner;
3292 const char *filename = bfd_get_filename (abfd);
3293
3294 if (header->version != 2 && header->version != 3 && header->version != 4)
3295 error (_("Dwarf Error: wrong version in compilation unit header "
3296 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3297 filename);
3298
b64f50a1 3299 if (header->abbrev_offset.sect_off
9ff913ba
DE
3300 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3301 &dwarf2_per_objfile->abbrev))
3302 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3303 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3304 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3305 filename);
3306
3307 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3308 avoid potential 32-bit overflow. */
b64f50a1 3309 if (((unsigned long) header->offset.sect_off
9ff913ba
DE
3310 + header->length + header->initial_length_size)
3311 > section->size)
3312 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3313 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3314 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3315 filename);
3316}
3317
3318/* Read in a CU/TU header and perform some basic error checking.
3319 The contents of the header are stored in HEADER.
3320 The result is a pointer to the start of the first DIE. */
adabb602 3321
fe1b8b76 3322static gdb_byte *
9ff913ba
DE
3323read_and_check_comp_unit_head (struct comp_unit_head *header,
3324 struct dwarf2_section_info *section,
3325 gdb_byte *info_ptr,
3326 int is_debug_types_section)
72bf9492 3327{
fe1b8b76 3328 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3329 bfd *abfd = section->asection->owner;
72bf9492 3330
b64f50a1 3331 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3332
72bf9492
DJ
3333 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3334
460c1c54
CC
3335 /* If we're reading a type unit, skip over the signature and
3336 type_offset fields. */
b0df02fd 3337 if (is_debug_types_section)
460c1c54
CC
3338 info_ptr += 8 /*signature*/ + header->offset_size;
3339
b64f50a1 3340 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3341
9ff913ba 3342 error_check_comp_unit_head (header, section);
72bf9492
DJ
3343
3344 return info_ptr;
3345}
3346
348e048f
DE
3347/* Read in the types comp unit header information from .debug_types entry at
3348 types_ptr. The result is a pointer to one past the end of the header. */
3349
3350static gdb_byte *
9ff913ba
DE
3351read_and_check_type_unit_head (struct comp_unit_head *header,
3352 struct dwarf2_section_info *section,
3353 gdb_byte *info_ptr,
dee91e82
DE
3354 ULONGEST *signature,
3355 cu_offset *type_offset_in_tu)
348e048f 3356{
9ff913ba
DE
3357 gdb_byte *beg_of_comp_unit = info_ptr;
3358 bfd *abfd = section->asection->owner;
348e048f 3359
b64f50a1 3360 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3361
9ff913ba 3362 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3363
9ff913ba
DE
3364 /* If we're reading a type unit, skip over the signature and
3365 type_offset fields. */
3366 if (signature != NULL)
3367 *signature = read_8_bytes (abfd, info_ptr);
3368 info_ptr += 8;
dee91e82
DE
3369 if (type_offset_in_tu != NULL)
3370 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3371 header->offset_size);
9ff913ba
DE
3372 info_ptr += header->offset_size;
3373
b64f50a1 3374 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3375
9ff913ba
DE
3376 error_check_comp_unit_head (header, section);
3377
3378 return info_ptr;
348e048f
DE
3379}
3380
aaa75496
JB
3381/* Allocate a new partial symtab for file named NAME and mark this new
3382 partial symtab as being an include of PST. */
3383
3384static void
3385dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3386 struct objfile *objfile)
3387{
3388 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3389
3390 subpst->section_offsets = pst->section_offsets;
3391 subpst->textlow = 0;
3392 subpst->texthigh = 0;
3393
3394 subpst->dependencies = (struct partial_symtab **)
3395 obstack_alloc (&objfile->objfile_obstack,
3396 sizeof (struct partial_symtab *));
3397 subpst->dependencies[0] = pst;
3398 subpst->number_of_dependencies = 1;
3399
3400 subpst->globals_offset = 0;
3401 subpst->n_global_syms = 0;
3402 subpst->statics_offset = 0;
3403 subpst->n_static_syms = 0;
3404 subpst->symtab = NULL;
3405 subpst->read_symtab = pst->read_symtab;
3406 subpst->readin = 0;
3407
3408 /* No private part is necessary for include psymtabs. This property
3409 can be used to differentiate between such include psymtabs and
10b3939b 3410 the regular ones. */
58a9656e 3411 subpst->read_symtab_private = NULL;
aaa75496
JB
3412}
3413
3414/* Read the Line Number Program data and extract the list of files
3415 included by the source file represented by PST. Build an include
d85a05f0 3416 partial symtab for each of these included files. */
aaa75496
JB
3417
3418static void
3419dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
3420 struct die_info *die,
3421 struct partial_symtab *pst)
aaa75496 3422{
d85a05f0
DJ
3423 struct line_header *lh = NULL;
3424 struct attribute *attr;
aaa75496 3425
d85a05f0
DJ
3426 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3427 if (attr)
3019eac3 3428 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
3429 if (lh == NULL)
3430 return; /* No linetable, so no includes. */
3431
c6da4cef 3432 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3433 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3434
3435 free_line_header (lh);
3436}
3437
348e048f 3438static hashval_t
52dc124a 3439hash_signatured_type (const void *item)
348e048f 3440{
52dc124a 3441 const struct signatured_type *sig_type = item;
9a619af0 3442
348e048f 3443 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 3444 return sig_type->signature;
348e048f
DE
3445}
3446
3447static int
52dc124a 3448eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
3449{
3450 const struct signatured_type *lhs = item_lhs;
3451 const struct signatured_type *rhs = item_rhs;
9a619af0 3452
348e048f
DE
3453 return lhs->signature == rhs->signature;
3454}
3455
1fd400ff
TT
3456/* Allocate a hash table for signatured types. */
3457
3458static htab_t
673bfd45 3459allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3460{
3461 return htab_create_alloc_ex (41,
52dc124a
DE
3462 hash_signatured_type,
3463 eq_signatured_type,
1fd400ff
TT
3464 NULL,
3465 &objfile->objfile_obstack,
3466 hashtab_obstack_allocate,
3467 dummy_obstack_deallocate);
3468}
3469
d467dd73 3470/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3471
3472static int
d467dd73 3473add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3474{
3475 struct signatured_type *sigt = *slot;
3476 struct dwarf2_per_cu_data ***datap = datum;
3477
3478 **datap = &sigt->per_cu;
3479 ++*datap;
3480
3481 return 1;
3482}
3483
3019eac3
DE
3484/* Create the hash table of all entries in the .debug_types section.
3485 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3486 The result is a pointer to the hash table or NULL if there are
3487 no types. */
348e048f 3488
3019eac3
DE
3489static htab_t
3490create_debug_types_hash_table (struct dwo_file *dwo_file,
3491 VEC (dwarf2_section_info_def) *types)
348e048f 3492{
3019eac3 3493 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 3494 htab_t types_htab = NULL;
8b70b953
TT
3495 int ix;
3496 struct dwarf2_section_info *section;
348e048f 3497
3019eac3
DE
3498 if (VEC_empty (dwarf2_section_info_def, types))
3499 return NULL;
348e048f 3500
8b70b953 3501 for (ix = 0;
3019eac3 3502 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
3503 ++ix)
3504 {
3019eac3 3505 bfd *abfd;
8b70b953 3506 gdb_byte *info_ptr, *end_ptr;
348e048f 3507
8b70b953
TT
3508 dwarf2_read_section (objfile, section);
3509 info_ptr = section->buffer;
348e048f 3510
8b70b953
TT
3511 if (info_ptr == NULL)
3512 continue;
348e048f 3513
3019eac3
DE
3514 /* We can't set abfd until now because the section may be empty or
3515 not present, in which case section->asection will be NULL. */
3516 abfd = section->asection->owner;
3517
8b70b953 3518 if (types_htab == NULL)
3019eac3
DE
3519 {
3520 if (dwo_file)
3521 types_htab = allocate_dwo_unit_table (objfile);
3522 else
3523 types_htab = allocate_signatured_type_table (objfile);
3524 }
348e048f 3525
8b70b953 3526 if (dwarf2_die_debug)
dee91e82
DE
3527 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3528 bfd_get_filename (abfd));
3529
3530 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3531 because we don't need to read any dies: the signature is in the
3532 header. */
8b70b953
TT
3533
3534 end_ptr = info_ptr + section->size;
3535 while (info_ptr < end_ptr)
3536 {
b64f50a1 3537 sect_offset offset;
3019eac3 3538 cu_offset type_offset_in_tu;
8b70b953 3539 ULONGEST signature;
52dc124a 3540 struct signatured_type *sig_type;
3019eac3 3541 struct dwo_unit *dwo_tu;
8b70b953
TT
3542 void **slot;
3543 gdb_byte *ptr = info_ptr;
9ff913ba 3544 struct comp_unit_head header;
dee91e82 3545 unsigned int length;
348e048f 3546
b64f50a1 3547 offset.sect_off = ptr - section->buffer;
348e048f 3548
8b70b953 3549 /* We need to read the type's signature in order to build the hash
9ff913ba 3550 table, but we don't need anything else just yet. */
348e048f 3551
9ff913ba 3552 ptr = read_and_check_type_unit_head (&header, section, ptr,
3019eac3 3553 &signature, &type_offset_in_tu);
6caca83c 3554
dee91e82
DE
3555 length = header.initial_length_size + header.length;
3556
6caca83c 3557 /* Skip dummy type units. */
dee91e82
DE
3558 if (ptr >= info_ptr + length
3559 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 3560 {
dee91e82 3561 info_ptr += header.initial_length_size + header.length;
6caca83c
CC
3562 continue;
3563 }
8b70b953 3564
3019eac3
DE
3565 if (dwo_file)
3566 {
3567 sig_type = NULL;
3568 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3569 struct dwo_unit);
3570 dwo_tu->dwo_file = dwo_file;
3571 dwo_tu->signature = signature;
3572 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3573 dwo_tu->info_or_types_section = section;
3574 dwo_tu->offset = offset;
3575 dwo_tu->length = length;
3576 }
3577 else
3578 {
3579 /* N.B.: type_offset is not usable if this type uses a DWO file.
3580 The real type_offset is in the DWO file. */
3581 dwo_tu = NULL;
3582 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3583 struct signatured_type);
3584 sig_type->signature = signature;
3585 sig_type->type_offset_in_tu = type_offset_in_tu;
3586 sig_type->per_cu.objfile = objfile;
3587 sig_type->per_cu.is_debug_types = 1;
3588 sig_type->per_cu.info_or_types_section = section;
3589 sig_type->per_cu.offset = offset;
3590 sig_type->per_cu.length = length;
3591 }
8b70b953 3592
3019eac3
DE
3593 slot = htab_find_slot (types_htab,
3594 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3595 INSERT);
8b70b953
TT
3596 gdb_assert (slot != NULL);
3597 if (*slot != NULL)
3598 {
3019eac3
DE
3599 sect_offset dup_offset;
3600
3601 if (dwo_file)
3602 {
3603 const struct dwo_unit *dup_tu = *slot;
3604
3605 dup_offset = dup_tu->offset;
3606 }
3607 else
3608 {
3609 const struct signatured_type *dup_tu = *slot;
3610
3611 dup_offset = dup_tu->per_cu.offset;
3612 }
b3c8eb43 3613
8b70b953
TT
3614 complaint (&symfile_complaints,
3615 _("debug type entry at offset 0x%x is duplicate to the "
3616 "entry at offset 0x%x, signature 0x%s"),
3019eac3 3617 offset.sect_off, dup_offset.sect_off,
8b70b953 3618 phex (signature, sizeof (signature)));
8b70b953 3619 }
3019eac3 3620 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 3621
8b70b953
TT
3622 if (dwarf2_die_debug)
3623 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
3624 offset.sect_off,
3625 phex (signature, sizeof (signature)));
348e048f 3626
dee91e82 3627 info_ptr += length;
8b70b953 3628 }
348e048f
DE
3629 }
3630
3019eac3
DE
3631 return types_htab;
3632}
3633
3634/* Create the hash table of all entries in the .debug_types section,
3635 and initialize all_type_units.
3636 The result is zero if there is an error (e.g. missing .debug_types section),
3637 otherwise non-zero. */
3638
3639static int
3640create_all_type_units (struct objfile *objfile)
3641{
3642 htab_t types_htab;
3643 struct dwarf2_per_cu_data **iter;
3644
3645 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3646 if (types_htab == NULL)
3647 {
3648 dwarf2_per_objfile->signatured_types = NULL;
3649 return 0;
3650 }
3651
348e048f
DE
3652 dwarf2_per_objfile->signatured_types = types_htab;
3653
d467dd73
DE
3654 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3655 dwarf2_per_objfile->all_type_units
1fd400ff 3656 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3657 dwarf2_per_objfile->n_type_units
1fd400ff 3658 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3659 iter = &dwarf2_per_objfile->all_type_units[0];
3660 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3661 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3662 == dwarf2_per_objfile->n_type_units);
1fd400ff 3663
348e048f
DE
3664 return 1;
3665}
3666
380bca97 3667/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 3668 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
3669
3670static struct signatured_type *
e319fa28 3671lookup_signatured_type (ULONGEST sig)
348e048f
DE
3672{
3673 struct signatured_type find_entry, *entry;
3674
3675 if (dwarf2_per_objfile->signatured_types == NULL)
3676 {
3677 complaint (&symfile_complaints,
55f1336d 3678 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 3679 return NULL;
348e048f
DE
3680 }
3681
3682 find_entry.signature = sig;
3683 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3684 return entry;
3685}
3686
d85a05f0
DJ
3687/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3688
3689static void
3690init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 3691 struct dwarf2_cu *cu,
3019eac3
DE
3692 struct dwarf2_section_info *section,
3693 struct dwo_file *dwo_file)
d85a05f0 3694{
fceca515 3695 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 3696 reader->abfd = section->asection->owner;
d85a05f0 3697 reader->cu = cu;
3019eac3 3698 reader->dwo_file = dwo_file;
dee91e82
DE
3699 reader->die_section = section;
3700 reader->buffer = section->buffer;
f664829e 3701 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
3702}
3703
3704/* Find the base address of the compilation unit for range lists and
3705 location lists. It will normally be specified by DW_AT_low_pc.
3706 In DWARF-3 draft 4, the base address could be overridden by
3707 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3708 compilation units with discontinuous ranges. */
3709
3710static void
3711dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3712{
3713 struct attribute *attr;
3714
3715 cu->base_known = 0;
3716 cu->base_address = 0;
3717
3718 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3719 if (attr)
3720 {
3721 cu->base_address = DW_ADDR (attr);
3722 cu->base_known = 1;
3723 }
3724 else
3725 {
3726 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3727 if (attr)
3728 {
3729 cu->base_address = DW_ADDR (attr);
3730 cu->base_known = 1;
3731 }
3732 }
3733}
3734
fd820528 3735/* Initialize a CU (or TU) and read its DIEs.
3019eac3 3736 If the CU defers to a DWO file, read the DWO file as well.
dee91e82
DE
3737
3738 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3739 Otherwise, a new CU is allocated with xmalloc.
3740
3741 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3742 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3743
3744 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 3745 linker) then DIE_READER_FUNC will not get called. */
aaa75496 3746
70221824 3747static void
fd820528
DE
3748init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3749 int use_existing_cu, int keep,
3750 die_reader_func_ftype *die_reader_func,
3751 void *data)
c906108c 3752{
dee91e82 3753 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
3754 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3755 bfd *abfd = section->asection->owner;
dee91e82
DE
3756 struct dwarf2_cu *cu;
3757 gdb_byte *begin_info_ptr, *info_ptr;
3758 struct die_reader_specs reader;
d85a05f0 3759 struct die_info *comp_unit_die;
dee91e82 3760 int has_children;
d85a05f0 3761 struct attribute *attr;
dee91e82
DE
3762 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3763 struct signatured_type *sig_type = NULL;
c906108c 3764
dee91e82
DE
3765 if (use_existing_cu)
3766 gdb_assert (keep);
23745b47 3767
dee91e82
DE
3768 cleanups = make_cleanup (null_cleanup, NULL);
3769
3770 /* This is cheap if the section is already read in. */
3771 dwarf2_read_section (objfile, section);
3772
3773 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3774
3775 if (use_existing_cu && this_cu->cu != NULL)
3776 {
3777 cu = this_cu->cu;
3778 info_ptr += cu->header.first_die_offset.cu_off;
3779 }
3780 else
3781 {
3782 /* If !use_existing_cu, this_cu->cu must be NULL. */
3783 gdb_assert (this_cu->cu == NULL);
3784
3785 cu = xmalloc (sizeof (*cu));
3786 init_one_comp_unit (cu, this_cu);
3787
3788 /* If an error occurs while loading, release our storage. */
3789 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3790
3019eac3 3791 if (this_cu->is_debug_types)
dee91e82
DE
3792 {
3793 ULONGEST signature;
3794
3795 info_ptr = read_and_check_type_unit_head (&cu->header,
3796 section, info_ptr,
3797 &signature, NULL);
3798
3799 /* There's no way to get from PER_CU to its containing
3800 struct signatured_type.
3801 But we have the signature so we can use that. */
3802 sig_type = lookup_signatured_type (signature);
3803 /* We've already scanned all the signatured types,
3804 this must succeed. */
3805 gdb_assert (sig_type != NULL);
3806 gdb_assert (&sig_type->per_cu == this_cu);
3807 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3808
3809 /* LENGTH has not been set yet for type units. */
3810 this_cu->length = cu->header.length + cu->header.initial_length_size;
3019eac3
DE
3811
3812 /* Establish the type offset that can be used to lookup the type. */
3813 sig_type->type_offset_in_section.sect_off =
3814 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
3815 }
3816 else
3817 {
3818 info_ptr = read_and_check_comp_unit_head (&cu->header,
3819 section, info_ptr, 0);
3820
3821 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3822 gdb_assert (this_cu->length
3823 == cu->header.length + cu->header.initial_length_size);
3824 }
3825 }
10b3939b 3826
6caca83c 3827 /* Skip dummy compilation units. */
dee91e82 3828 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
3829 || peek_abbrev_code (abfd, info_ptr) == 0)
3830 {
dee91e82 3831 do_cleanups (cleanups);
21b2bd31 3832 return;
6caca83c
CC
3833 }
3834
93311388 3835 /* Read the abbrevs for this compilation unit into a table. */
dee91e82
DE
3836 if (cu->dwarf2_abbrevs == NULL)
3837 {
3838 dwarf2_read_abbrevs (cu, &dwarf2_per_objfile->abbrev);
3839 make_cleanup (dwarf2_free_abbrev_table, cu);
3840 }
af703f96 3841
dee91e82 3842 /* Read the top level CU/TU die. */
3019eac3 3843 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 3844 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 3845
3019eac3
DE
3846 /* If we have a DWO stub, process it and then read in the DWO file.
3847 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3848 a DWO CU, that this test will fail. */
3849 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3850 if (attr)
3851 {
3852 char *dwo_name = DW_STRING (attr);
3853 const char *comp_dir;
3854 struct dwo_unit *dwo_unit;
3855 ULONGEST signature; /* Or dwo_id. */
3856 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3857 int i,num_extra_attrs;
3858
3859 if (has_children)
3860 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3861 " has children (offset 0x%x) [in module %s]"),
3862 this_cu->offset.sect_off, bfd_get_filename (abfd));
3863
3864 /* These attributes aren't processed until later:
3865 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3866 However, the attribute is found in the stub which we won't have later.
3867 In order to not impose this complication on the rest of the code,
3868 we read them here and copy them to the DWO CU/TU die. */
3869 stmt_list = low_pc = high_pc = ranges = NULL;
3870
3871 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3872 DWO file. */
3873 if (! this_cu->is_debug_types)
3874 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3875 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3876 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3877 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3878
3879 /* There should be a DW_AT_addr_base attribute here (if needed).
3880 We need the value before we can process DW_FORM_GNU_addr_index. */
3881 cu->addr_base = 0;
3882 cu->have_addr_base = 0;
3883 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
3884 if (attr)
3885 {
3886 cu->addr_base = DW_UNSND (attr);
3887 cu->have_addr_base = 1;
3888 }
3889
3890 if (this_cu->is_debug_types)
3891 {
3892 gdb_assert (sig_type != NULL);
3893 signature = sig_type->signature;
3894 }
3895 else
3896 {
3897 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3898 if (! attr)
3899 error (_("Dwarf Error: missing dwo_id [in module %s]"),
3900 dwo_name);
3901 signature = DW_UNSND (attr);
3902 }
3903
3904 /* We may need the comp_dir in order to find the DWO file. */
3905 comp_dir = NULL;
3906 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3907 if (attr)
3908 comp_dir = DW_STRING (attr);
3909
3910 if (this_cu->is_debug_types)
3911 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
3912 else
3913 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
3914 signature);
3915
3916 if (dwo_unit == NULL)
3917 {
3918 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
3919 " with ID %s [in module %s]"),
3920 this_cu->offset.sect_off,
3921 phex (signature, sizeof (signature)),
3922 objfile->name);
3923 }
3924
3925 /* Set up for reading the DWO CU/TU. */
3926 cu->dwo_unit = dwo_unit;
3927 section = dwo_unit->info_or_types_section;
3928 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
3929 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
3930
3931 if (this_cu->is_debug_types)
3932 {
3933 ULONGEST signature;
3934
3935 info_ptr = read_and_check_type_unit_head (&cu->header,
3936 section, info_ptr,
3937 &signature, NULL);
3938 gdb_assert (sig_type->signature == signature);
3939 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3940 gdb_assert (dwo_unit->length
3941 == cu->header.length + cu->header.initial_length_size);
3942
3943 /* Establish the type offset that can be used to lookup the type.
3944 For DWO files, we don't know it until now. */
3945 sig_type->type_offset_in_section.sect_off =
3946 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
3947 }
3948 else
3949 {
3950 info_ptr = read_and_check_comp_unit_head (&cu->header,
3951 section, info_ptr, 0);
3952 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3953 gdb_assert (dwo_unit->length
3954 == cu->header.length + cu->header.initial_length_size);
3955 }
3956
3957 /* Discard the original CU's abbrev table, and read the DWO's. */
3958 dwarf2_free_abbrev_table (cu);
3959 dwarf2_read_abbrevs (cu, &dwo_unit->dwo_file->sections.abbrev);
3960
3961 /* Read in the die, but leave space to copy over the attributes
3962 from the stub. This has the benefit of simplifying the rest of
3963 the code - all the real work is done here. */
3964 num_extra_attrs = ((stmt_list != NULL)
3965 + (low_pc != NULL)
3966 + (high_pc != NULL)
3967 + (ranges != NULL));
3968 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
3969 &has_children, num_extra_attrs);
3970
3971 /* Copy over the attributes from the stub to the DWO die. */
3972 i = comp_unit_die->num_attrs;
3973 if (stmt_list != NULL)
3974 comp_unit_die->attrs[i++] = *stmt_list;
3975 if (low_pc != NULL)
3976 comp_unit_die->attrs[i++] = *low_pc;
3977 if (high_pc != NULL)
3978 comp_unit_die->attrs[i++] = *high_pc;
3979 if (ranges != NULL)
3980 comp_unit_die->attrs[i++] = *ranges;
3981 comp_unit_die->num_attrs += num_extra_attrs;
3982
3983 /* Skip dummy compilation units. */
3984 if (info_ptr >= begin_info_ptr + dwo_unit->length
3985 || peek_abbrev_code (abfd, info_ptr) == 0)
3986 {
3987 do_cleanups (cleanups);
3988 return;
3989 }
3990 }
3991
dee91e82
DE
3992 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3993
3994 if (free_cu_cleanup != NULL)
348e048f 3995 {
dee91e82
DE
3996 if (keep)
3997 {
3998 /* We've successfully allocated this compilation unit. Let our
3999 caller clean it up when finished with it. */
4000 discard_cleanups (free_cu_cleanup);
4001
4002 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4003 So we have to manually free the abbrev table. */
4004 dwarf2_free_abbrev_table (cu);
4005
4006 /* Link this CU into read_in_chain. */
4007 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4008 dwarf2_per_objfile->read_in_chain = this_cu;
4009 }
4010 else
4011 do_cleanups (free_cu_cleanup);
348e048f 4012 }
dee91e82
DE
4013
4014 do_cleanups (cleanups);
4015}
4016
3019eac3
DE
4017/* Read CU/TU THIS_CU in section SECTION,
4018 but do not follow DW_AT_GNU_dwo_name if present.
4019 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4020 have already done the lookup to find the DWO file).
dee91e82
DE
4021
4022 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4023 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4024
4025 We fill in THIS_CU->length.
4026
4027 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4028 linker) then DIE_READER_FUNC will not get called.
4029
4030 THIS_CU->cu is always freed when done.
3019eac3
DE
4031 This is done in order to not leave THIS_CU->cu in a state where we have
4032 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4033
4034static void
4035init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4036 struct dwarf2_section_info *abbrev_section,
3019eac3 4037 struct dwo_file *dwo_file,
dee91e82
DE
4038 die_reader_func_ftype *die_reader_func,
4039 void *data)
4040{
4041 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4042 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4043 bfd *abfd = section->asection->owner;
dee91e82
DE
4044 struct dwarf2_cu cu;
4045 gdb_byte *begin_info_ptr, *info_ptr;
4046 struct die_reader_specs reader;
4047 struct cleanup *cleanups;
4048 struct die_info *comp_unit_die;
4049 int has_children;
4050
4051 gdb_assert (this_cu->cu == NULL);
4052
dee91e82
DE
4053 /* This is cheap if the section is already read in. */
4054 dwarf2_read_section (objfile, section);
4055
4056 init_one_comp_unit (&cu, this_cu);
4057
4058 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4059
4060 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4061 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3019eac3 4062 this_cu->is_debug_types);
dee91e82
DE
4063
4064 this_cu->length = cu.header.length + cu.header.initial_length_size;
4065
4066 /* Skip dummy compilation units. */
4067 if (info_ptr >= begin_info_ptr + this_cu->length
4068 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4069 {
dee91e82 4070 do_cleanups (cleanups);
21b2bd31 4071 return;
93311388 4072 }
72bf9492 4073
dee91e82
DE
4074 dwarf2_read_abbrevs (&cu, abbrev_section);
4075 make_cleanup (dwarf2_free_abbrev_table, &cu);
4076
3019eac3 4077 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4078 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4079
4080 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4081
4082 do_cleanups (cleanups);
4083}
4084
3019eac3
DE
4085/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4086 does not lookup the specified DWO file.
4087 This cannot be used to read DWO files.
dee91e82
DE
4088
4089 THIS_CU->cu is always freed when done.
3019eac3
DE
4090 This is done in order to not leave THIS_CU->cu in a state where we have
4091 to care whether it refers to the "main" CU or the DWO CU.
4092 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4093
4094static void
4095init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4096 die_reader_func_ftype *die_reader_func,
4097 void *data)
4098{
4099 init_cutu_and_read_dies_no_follow (this_cu,
4100 &dwarf2_per_objfile->abbrev,
3019eac3 4101 NULL,
dee91e82
DE
4102 die_reader_func, data);
4103}
4104
4105/* die_reader_func for process_psymtab_comp_unit. */
4106
4107static void
4108process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4109 gdb_byte *info_ptr,
4110 struct die_info *comp_unit_die,
4111 int has_children,
4112 void *data)
4113{
4114 struct dwarf2_cu *cu = reader->cu;
4115 struct objfile *objfile = cu->objfile;
4116 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4117 struct attribute *attr;
4118 CORE_ADDR baseaddr;
4119 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4120 struct partial_symtab *pst;
4121 int has_pc_info;
4122 const char *filename;
95554aad 4123 int *want_partial_unit_ptr = data;
dee91e82 4124
95554aad
TT
4125 if (comp_unit_die->tag == DW_TAG_partial_unit
4126 && (want_partial_unit_ptr == NULL
4127 || !*want_partial_unit_ptr))
dee91e82
DE
4128 return;
4129
95554aad 4130 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4131
4132 cu->list_in_scope = &file_symbols;
c906108c 4133
93311388 4134 /* Allocate a new partial symbol table structure. */
dee91e82 4135 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4136 if (attr == NULL || !DW_STRING (attr))
4137 filename = "";
4138 else
4139 filename = DW_STRING (attr);
93311388 4140 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 4141 filename,
93311388
DE
4142 /* TEXTLOW and TEXTHIGH are set below. */
4143 0,
4144 objfile->global_psymbols.next,
4145 objfile->static_psymbols.next);
9750bca9 4146 pst->psymtabs_addrmap_supported = 1;
72bf9492 4147
dee91e82 4148 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4149 if (attr != NULL)
4150 pst->dirname = DW_STRING (attr);
72bf9492 4151
dee91e82 4152 pst->read_symtab_private = per_cu;
72bf9492 4153
93311388 4154 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4155
0963b4bd 4156 /* Store the function that reads in the rest of the symbol table. */
93311388 4157 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 4158
dee91e82 4159 per_cu->v.psymtab = pst;
c906108c 4160
dee91e82 4161 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4162
93311388
DE
4163 /* Possibly set the default values of LOWPC and HIGHPC from
4164 `DW_AT_ranges'. */
d85a05f0 4165 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4166 &best_highpc, cu, pst);
d85a05f0 4167 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4168 /* Store the contiguous range if it is not empty; it can be empty for
4169 CUs with no code. */
4170 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4171 best_lowpc + baseaddr,
4172 best_highpc + baseaddr - 1, pst);
93311388
DE
4173
4174 /* Check if comp unit has_children.
4175 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4176 If not, there's no more debug_info for this comp unit. */
d85a05f0 4177 if (has_children)
93311388
DE
4178 {
4179 struct partial_die_info *first_die;
4180 CORE_ADDR lowpc, highpc;
31ffec48 4181
93311388
DE
4182 lowpc = ((CORE_ADDR) -1);
4183 highpc = ((CORE_ADDR) 0);
c906108c 4184
dee91e82 4185 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4186
93311388 4187 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4188 ! has_pc_info, cu);
57c22c6c 4189
93311388
DE
4190 /* If we didn't find a lowpc, set it to highpc to avoid
4191 complaints from `maint check'. */
4192 if (lowpc == ((CORE_ADDR) -1))
4193 lowpc = highpc;
10b3939b 4194
93311388
DE
4195 /* If the compilation unit didn't have an explicit address range,
4196 then use the information extracted from its child dies. */
d85a05f0 4197 if (! has_pc_info)
93311388 4198 {
d85a05f0
DJ
4199 best_lowpc = lowpc;
4200 best_highpc = highpc;
93311388
DE
4201 }
4202 }
d85a05f0
DJ
4203 pst->textlow = best_lowpc + baseaddr;
4204 pst->texthigh = best_highpc + baseaddr;
c906108c 4205
93311388
DE
4206 pst->n_global_syms = objfile->global_psymbols.next -
4207 (objfile->global_psymbols.list + pst->globals_offset);
4208 pst->n_static_syms = objfile->static_psymbols.next -
4209 (objfile->static_psymbols.list + pst->statics_offset);
4210 sort_pst_symbols (pst);
c906108c 4211
95554aad
TT
4212 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4213 {
4214 int i;
4215 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4216 struct dwarf2_per_cu_data *iter;
4217
4218 /* Fill in 'dependencies' here; we fill in 'users' in a
4219 post-pass. */
4220 pst->number_of_dependencies = len;
4221 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4222 len * sizeof (struct symtab *));
4223 for (i = 0;
4224 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4225 i, iter);
4226 ++i)
4227 pst->dependencies[i] = iter->v.psymtab;
4228
4229 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4230 }
4231
3019eac3 4232 if (per_cu->is_debug_types)
348e048f
DE
4233 {
4234 /* It's not clear we want to do anything with stmt lists here.
4235 Waiting to see what gcc ultimately does. */
4236 }
d85a05f0 4237 else
93311388
DE
4238 {
4239 /* Get the list of files included in the current compilation unit,
4240 and build a psymtab for each of them. */
dee91e82 4241 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
93311388 4242 }
dee91e82 4243}
ae038cb0 4244
dee91e82
DE
4245/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4246 Process compilation unit THIS_CU for a psymtab. */
4247
4248static void
95554aad
TT
4249process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4250 int want_partial_unit)
dee91e82
DE
4251{
4252 /* If this compilation unit was already read in, free the
4253 cached copy in order to read it in again. This is
4254 necessary because we skipped some symbols when we first
4255 read in the compilation unit (see load_partial_dies).
4256 This problem could be avoided, but the benefit is unclear. */
4257 if (this_cu->cu != NULL)
4258 free_one_cached_comp_unit (this_cu);
4259
3019eac3 4260 gdb_assert (! this_cu->is_debug_types);
fd820528 4261 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
95554aad 4262 &want_partial_unit);
dee91e82
DE
4263
4264 /* Age out any secondary CUs. */
4265 age_cached_comp_units ();
93311388 4266}
ff013f42 4267
348e048f
DE
4268/* Traversal function for htab_traverse_noresize.
4269 Process one .debug_types comp-unit. */
4270
4271static int
dee91e82 4272process_psymtab_type_unit (void **slot, void *info)
348e048f 4273{
dee91e82
DE
4274 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4275 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 4276
fd820528 4277 gdb_assert (per_cu->is_debug_types);
a0f42c21 4278 gdb_assert (info == NULL);
348e048f 4279
dee91e82
DE
4280 /* If this compilation unit was already read in, free the
4281 cached copy in order to read it in again. This is
4282 necessary because we skipped some symbols when we first
4283 read in the compilation unit (see load_partial_dies).
4284 This problem could be avoided, but the benefit is unclear. */
4285 if (per_cu->cu != NULL)
4286 free_one_cached_comp_unit (per_cu);
4287
fd820528
DE
4288 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4289 NULL);
dee91e82
DE
4290
4291 /* Age out any secondary CUs. */
4292 age_cached_comp_units ();
348e048f
DE
4293
4294 return 1;
4295}
4296
4297/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4298 Build partial symbol tables for the .debug_types comp-units. */
4299
4300static void
4301build_type_psymtabs (struct objfile *objfile)
4302{
0e50663e 4303 if (! create_all_type_units (objfile))
348e048f
DE
4304 return;
4305
4306 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
dee91e82 4307 process_psymtab_type_unit, NULL);
348e048f
DE
4308}
4309
60606b2c
TT
4310/* A cleanup function that clears objfile's psymtabs_addrmap field. */
4311
4312static void
4313psymtabs_addrmap_cleanup (void *o)
4314{
4315 struct objfile *objfile = o;
ec61707d 4316
60606b2c
TT
4317 objfile->psymtabs_addrmap = NULL;
4318}
4319
95554aad
TT
4320/* Compute the 'user' field for each psymtab in OBJFILE. */
4321
4322static void
4323set_partial_user (struct objfile *objfile)
4324{
4325 int i;
4326
4327 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4328 {
4329 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4330 struct partial_symtab *pst = per_cu->v.psymtab;
4331 int j;
4332
4333 for (j = 0; j < pst->number_of_dependencies; ++j)
4334 {
4335 /* Set the 'user' field only if it is not already set. */
4336 if (pst->dependencies[j]->user == NULL)
4337 pst->dependencies[j]->user = pst;
4338 }
4339 }
4340}
4341
93311388
DE
4342/* Build the partial symbol table by doing a quick pass through the
4343 .debug_info and .debug_abbrev sections. */
72bf9492 4344
93311388 4345static void
c67a9c90 4346dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 4347{
60606b2c
TT
4348 struct cleanup *back_to, *addrmap_cleanup;
4349 struct obstack temp_obstack;
21b2bd31 4350 int i;
93311388 4351
98bfdba5
PA
4352 dwarf2_per_objfile->reading_partial_symbols = 1;
4353
be391dca 4354 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 4355
93311388
DE
4356 /* Any cached compilation units will be linked by the per-objfile
4357 read_in_chain. Make sure to free them when we're done. */
4358 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 4359
348e048f
DE
4360 build_type_psymtabs (objfile);
4361
93311388 4362 create_all_comp_units (objfile);
c906108c 4363
60606b2c
TT
4364 /* Create a temporary address map on a temporary obstack. We later
4365 copy this to the final obstack. */
4366 obstack_init (&temp_obstack);
4367 make_cleanup_obstack_free (&temp_obstack);
4368 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4369 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 4370
21b2bd31 4371 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 4372 {
21b2bd31 4373 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 4374
95554aad 4375 process_psymtab_comp_unit (per_cu, 0);
c906108c 4376 }
ff013f42 4377
95554aad
TT
4378 set_partial_user (objfile);
4379
ff013f42
JK
4380 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4381 &objfile->objfile_obstack);
60606b2c 4382 discard_cleanups (addrmap_cleanup);
ff013f42 4383
ae038cb0
DJ
4384 do_cleanups (back_to);
4385}
4386
3019eac3 4387/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
4388
4389static void
dee91e82
DE
4390load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4391 gdb_byte *info_ptr,
4392 struct die_info *comp_unit_die,
4393 int has_children,
4394 void *data)
ae038cb0 4395{
dee91e82 4396 struct dwarf2_cu *cu = reader->cu;
ae038cb0 4397
95554aad 4398 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 4399
ae038cb0
DJ
4400 /* Check if comp unit has_children.
4401 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4402 If not, there's no more debug_info for this comp unit. */
d85a05f0 4403 if (has_children)
dee91e82
DE
4404 load_partial_dies (reader, info_ptr, 0);
4405}
98bfdba5 4406
dee91e82
DE
4407/* Load the partial DIEs for a secondary CU into memory.
4408 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 4409
dee91e82
DE
4410static void
4411load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4412{
fd820528 4413 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
4414}
4415
9cdd5dbd
DE
4416/* Create a list of all compilation units in OBJFILE.
4417 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
4418
4419static void
4420create_all_comp_units (struct objfile *objfile)
4421{
4422 int n_allocated;
4423 int n_comp_units;
4424 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
4425 gdb_byte *info_ptr;
4426
4427 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4428 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4429
4430 n_comp_units = 0;
4431 n_allocated = 10;
4432 all_comp_units = xmalloc (n_allocated
4433 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 4434
3e43a32a
MS
4435 while (info_ptr < dwarf2_per_objfile->info.buffer
4436 + dwarf2_per_objfile->info.size)
ae038cb0 4437 {
c764a876 4438 unsigned int length, initial_length_size;
ae038cb0 4439 struct dwarf2_per_cu_data *this_cu;
b64f50a1 4440 sect_offset offset;
ae038cb0 4441
b64f50a1 4442 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4443
4444 /* Read just enough information to find out where the next
4445 compilation unit is. */
c764a876
DE
4446 length = read_initial_length (objfile->obfd, info_ptr,
4447 &initial_length_size);
ae038cb0
DJ
4448
4449 /* Save the compilation unit for later lookup. */
4450 this_cu = obstack_alloc (&objfile->objfile_obstack,
4451 sizeof (struct dwarf2_per_cu_data));
4452 memset (this_cu, 0, sizeof (*this_cu));
4453 this_cu->offset = offset;
c764a876 4454 this_cu->length = length + initial_length_size;
9291a0cd 4455 this_cu->objfile = objfile;
3019eac3 4456 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
ae038cb0
DJ
4457
4458 if (n_comp_units == n_allocated)
4459 {
4460 n_allocated *= 2;
4461 all_comp_units = xrealloc (all_comp_units,
4462 n_allocated
4463 * sizeof (struct dwarf2_per_cu_data *));
4464 }
4465 all_comp_units[n_comp_units++] = this_cu;
4466
4467 info_ptr = info_ptr + this_cu->length;
4468 }
4469
4470 dwarf2_per_objfile->all_comp_units
4471 = obstack_alloc (&objfile->objfile_obstack,
4472 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4473 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4474 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4475 xfree (all_comp_units);
4476 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
4477}
4478
5734ee8b
DJ
4479/* Process all loaded DIEs for compilation unit CU, starting at
4480 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4481 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4482 DW_AT_ranges). If NEED_PC is set, then this function will set
4483 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4484 and record the covered ranges in the addrmap. */
c906108c 4485
72bf9492
DJ
4486static void
4487scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 4488 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 4489{
72bf9492 4490 struct partial_die_info *pdi;
c906108c 4491
91c24f0a
DC
4492 /* Now, march along the PDI's, descending into ones which have
4493 interesting children but skipping the children of the other ones,
4494 until we reach the end of the compilation unit. */
c906108c 4495
72bf9492 4496 pdi = first_die;
91c24f0a 4497
72bf9492
DJ
4498 while (pdi != NULL)
4499 {
4500 fixup_partial_die (pdi, cu);
c906108c 4501
f55ee35c 4502 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
4503 children, so we need to look at them. Ditto for anonymous
4504 enums. */
933c6fe4 4505
72bf9492 4506 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
4507 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4508 || pdi->tag == DW_TAG_imported_unit)
c906108c 4509 {
72bf9492 4510 switch (pdi->tag)
c906108c
SS
4511 {
4512 case DW_TAG_subprogram:
5734ee8b 4513 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 4514 break;
72929c62 4515 case DW_TAG_constant:
c906108c
SS
4516 case DW_TAG_variable:
4517 case DW_TAG_typedef:
91c24f0a 4518 case DW_TAG_union_type:
72bf9492 4519 if (!pdi->is_declaration)
63d06c5c 4520 {
72bf9492 4521 add_partial_symbol (pdi, cu);
63d06c5c
DC
4522 }
4523 break;
c906108c 4524 case DW_TAG_class_type:
680b30c7 4525 case DW_TAG_interface_type:
c906108c 4526 case DW_TAG_structure_type:
72bf9492 4527 if (!pdi->is_declaration)
c906108c 4528 {
72bf9492 4529 add_partial_symbol (pdi, cu);
c906108c
SS
4530 }
4531 break;
91c24f0a 4532 case DW_TAG_enumeration_type:
72bf9492
DJ
4533 if (!pdi->is_declaration)
4534 add_partial_enumeration (pdi, cu);
c906108c
SS
4535 break;
4536 case DW_TAG_base_type:
a02abb62 4537 case DW_TAG_subrange_type:
c906108c 4538 /* File scope base type definitions are added to the partial
c5aa993b 4539 symbol table. */
72bf9492 4540 add_partial_symbol (pdi, cu);
c906108c 4541 break;
d9fa45fe 4542 case DW_TAG_namespace:
5734ee8b 4543 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 4544 break;
5d7cb8df
JK
4545 case DW_TAG_module:
4546 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4547 break;
95554aad
TT
4548 case DW_TAG_imported_unit:
4549 {
4550 struct dwarf2_per_cu_data *per_cu;
4551
4552 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4553 cu->objfile);
4554
4555 /* Go read the partial unit, if needed. */
4556 if (per_cu->v.psymtab == NULL)
4557 process_psymtab_comp_unit (per_cu, 1);
4558
4559 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4560 per_cu);
4561 }
4562 break;
c906108c
SS
4563 default:
4564 break;
4565 }
4566 }
4567
72bf9492
DJ
4568 /* If the die has a sibling, skip to the sibling. */
4569
4570 pdi = pdi->die_sibling;
4571 }
4572}
4573
4574/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 4575
72bf9492 4576 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
4577 name is concatenated with "::" and the partial DIE's name. For
4578 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
4579 Enumerators are an exception; they use the scope of their parent
4580 enumeration type, i.e. the name of the enumeration type is not
4581 prepended to the enumerator.
91c24f0a 4582
72bf9492
DJ
4583 There are two complexities. One is DW_AT_specification; in this
4584 case "parent" means the parent of the target of the specification,
4585 instead of the direct parent of the DIE. The other is compilers
4586 which do not emit DW_TAG_namespace; in this case we try to guess
4587 the fully qualified name of structure types from their members'
4588 linkage names. This must be done using the DIE's children rather
4589 than the children of any DW_AT_specification target. We only need
4590 to do this for structures at the top level, i.e. if the target of
4591 any DW_AT_specification (if any; otherwise the DIE itself) does not
4592 have a parent. */
4593
4594/* Compute the scope prefix associated with PDI's parent, in
4595 compilation unit CU. The result will be allocated on CU's
4596 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4597 field. NULL is returned if no prefix is necessary. */
4598static char *
4599partial_die_parent_scope (struct partial_die_info *pdi,
4600 struct dwarf2_cu *cu)
4601{
4602 char *grandparent_scope;
4603 struct partial_die_info *parent, *real_pdi;
91c24f0a 4604
72bf9492
DJ
4605 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4606 then this means the parent of the specification DIE. */
4607
4608 real_pdi = pdi;
72bf9492 4609 while (real_pdi->has_specification)
10b3939b 4610 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
4611
4612 parent = real_pdi->die_parent;
4613 if (parent == NULL)
4614 return NULL;
4615
4616 if (parent->scope_set)
4617 return parent->scope;
4618
4619 fixup_partial_die (parent, cu);
4620
10b3939b 4621 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 4622
acebe513
UW
4623 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4624 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4625 Work around this problem here. */
4626 if (cu->language == language_cplus
6e70227d 4627 && parent->tag == DW_TAG_namespace
acebe513
UW
4628 && strcmp (parent->name, "::") == 0
4629 && grandparent_scope == NULL)
4630 {
4631 parent->scope = NULL;
4632 parent->scope_set = 1;
4633 return NULL;
4634 }
4635
9c6c53f7
SA
4636 if (pdi->tag == DW_TAG_enumerator)
4637 /* Enumerators should not get the name of the enumeration as a prefix. */
4638 parent->scope = grandparent_scope;
4639 else if (parent->tag == DW_TAG_namespace
f55ee35c 4640 || parent->tag == DW_TAG_module
72bf9492
DJ
4641 || parent->tag == DW_TAG_structure_type
4642 || parent->tag == DW_TAG_class_type
680b30c7 4643 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
4644 || parent->tag == DW_TAG_union_type
4645 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
4646 {
4647 if (grandparent_scope == NULL)
4648 parent->scope = parent->name;
4649 else
3e43a32a
MS
4650 parent->scope = typename_concat (&cu->comp_unit_obstack,
4651 grandparent_scope,
f55ee35c 4652 parent->name, 0, cu);
72bf9492 4653 }
72bf9492
DJ
4654 else
4655 {
4656 /* FIXME drow/2004-04-01: What should we be doing with
4657 function-local names? For partial symbols, we should probably be
4658 ignoring them. */
4659 complaint (&symfile_complaints,
e2e0b3e5 4660 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 4661 parent->tag, pdi->offset.sect_off);
72bf9492 4662 parent->scope = grandparent_scope;
c906108c
SS
4663 }
4664
72bf9492
DJ
4665 parent->scope_set = 1;
4666 return parent->scope;
4667}
4668
4669/* Return the fully scoped name associated with PDI, from compilation unit
4670 CU. The result will be allocated with malloc. */
4568ecf9 4671
72bf9492
DJ
4672static char *
4673partial_die_full_name (struct partial_die_info *pdi,
4674 struct dwarf2_cu *cu)
4675{
4676 char *parent_scope;
4677
98bfdba5
PA
4678 /* If this is a template instantiation, we can not work out the
4679 template arguments from partial DIEs. So, unfortunately, we have
4680 to go through the full DIEs. At least any work we do building
4681 types here will be reused if full symbols are loaded later. */
4682 if (pdi->has_template_arguments)
4683 {
4684 fixup_partial_die (pdi, cu);
4685
4686 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4687 {
4688 struct die_info *die;
4689 struct attribute attr;
4690 struct dwarf2_cu *ref_cu = cu;
4691
b64f50a1 4692 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
4693 attr.name = 0;
4694 attr.form = DW_FORM_ref_addr;
4568ecf9 4695 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
4696 die = follow_die_ref (NULL, &attr, &ref_cu);
4697
4698 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4699 }
4700 }
4701
72bf9492
DJ
4702 parent_scope = partial_die_parent_scope (pdi, cu);
4703 if (parent_scope == NULL)
4704 return NULL;
4705 else
f55ee35c 4706 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4707}
4708
4709static void
72bf9492 4710add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4711{
e7c27a73 4712 struct objfile *objfile = cu->objfile;
c906108c 4713 CORE_ADDR addr = 0;
decbce07 4714 char *actual_name = NULL;
e142c38c 4715 CORE_ADDR baseaddr;
72bf9492 4716 int built_actual_name = 0;
e142c38c
DJ
4717
4718 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4719
94af9270
KS
4720 actual_name = partial_die_full_name (pdi, cu);
4721 if (actual_name)
4722 built_actual_name = 1;
63d06c5c 4723
72bf9492
DJ
4724 if (actual_name == NULL)
4725 actual_name = pdi->name;
4726
c906108c
SS
4727 switch (pdi->tag)
4728 {
4729 case DW_TAG_subprogram:
2cfa0c8d 4730 if (pdi->is_external || cu->language == language_ada)
c906108c 4731 {
2cfa0c8d
JB
4732 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4733 of the global scope. But in Ada, we want to be able to access
4734 nested procedures globally. So all Ada subprograms are stored
4735 in the global scope. */
f47fb265 4736 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4737 mst_text, objfile); */
f47fb265
MS
4738 add_psymbol_to_list (actual_name, strlen (actual_name),
4739 built_actual_name,
4740 VAR_DOMAIN, LOC_BLOCK,
4741 &objfile->global_psymbols,
4742 0, pdi->lowpc + baseaddr,
4743 cu->language, objfile);
c906108c
SS
4744 }
4745 else
4746 {
f47fb265 4747 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4748 mst_file_text, objfile); */
f47fb265
MS
4749 add_psymbol_to_list (actual_name, strlen (actual_name),
4750 built_actual_name,
4751 VAR_DOMAIN, LOC_BLOCK,
4752 &objfile->static_psymbols,
4753 0, pdi->lowpc + baseaddr,
4754 cu->language, objfile);
c906108c
SS
4755 }
4756 break;
72929c62
JB
4757 case DW_TAG_constant:
4758 {
4759 struct psymbol_allocation_list *list;
4760
4761 if (pdi->is_external)
4762 list = &objfile->global_psymbols;
4763 else
4764 list = &objfile->static_psymbols;
f47fb265
MS
4765 add_psymbol_to_list (actual_name, strlen (actual_name),
4766 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4767 list, 0, 0, cu->language, objfile);
72929c62
JB
4768 }
4769 break;
c906108c 4770 case DW_TAG_variable:
95554aad
TT
4771 if (pdi->d.locdesc)
4772 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 4773
95554aad 4774 if (pdi->d.locdesc
caac4577
JG
4775 && addr == 0
4776 && !dwarf2_per_objfile->has_section_at_zero)
4777 {
4778 /* A global or static variable may also have been stripped
4779 out by the linker if unused, in which case its address
4780 will be nullified; do not add such variables into partial
4781 symbol table then. */
4782 }
4783 else if (pdi->is_external)
c906108c
SS
4784 {
4785 /* Global Variable.
4786 Don't enter into the minimal symbol tables as there is
4787 a minimal symbol table entry from the ELF symbols already.
4788 Enter into partial symbol table if it has a location
4789 descriptor or a type.
4790 If the location descriptor is missing, new_symbol will create
4791 a LOC_UNRESOLVED symbol, the address of the variable will then
4792 be determined from the minimal symbol table whenever the variable
4793 is referenced.
4794 The address for the partial symbol table entry is not
4795 used by GDB, but it comes in handy for debugging partial symbol
4796 table building. */
4797
95554aad 4798 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
4799 add_psymbol_to_list (actual_name, strlen (actual_name),
4800 built_actual_name,
4801 VAR_DOMAIN, LOC_STATIC,
4802 &objfile->global_psymbols,
4803 0, addr + baseaddr,
4804 cu->language, objfile);
c906108c
SS
4805 }
4806 else
4807 {
0963b4bd 4808 /* Static Variable. Skip symbols without location descriptors. */
95554aad 4809 if (pdi->d.locdesc == NULL)
decbce07
MS
4810 {
4811 if (built_actual_name)
4812 xfree (actual_name);
4813 return;
4814 }
f47fb265 4815 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4816 mst_file_data, objfile); */
f47fb265
MS
4817 add_psymbol_to_list (actual_name, strlen (actual_name),
4818 built_actual_name,
4819 VAR_DOMAIN, LOC_STATIC,
4820 &objfile->static_psymbols,
4821 0, addr + baseaddr,
4822 cu->language, objfile);
c906108c
SS
4823 }
4824 break;
4825 case DW_TAG_typedef:
4826 case DW_TAG_base_type:
a02abb62 4827 case DW_TAG_subrange_type:
38d518c9 4828 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4829 built_actual_name,
176620f1 4830 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4831 &objfile->static_psymbols,
e142c38c 4832 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4833 break;
72bf9492
DJ
4834 case DW_TAG_namespace:
4835 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4836 built_actual_name,
72bf9492
DJ
4837 VAR_DOMAIN, LOC_TYPEDEF,
4838 &objfile->global_psymbols,
4839 0, (CORE_ADDR) 0, cu->language, objfile);
4840 break;
c906108c 4841 case DW_TAG_class_type:
680b30c7 4842 case DW_TAG_interface_type:
c906108c
SS
4843 case DW_TAG_structure_type:
4844 case DW_TAG_union_type:
4845 case DW_TAG_enumeration_type:
fa4028e9
JB
4846 /* Skip external references. The DWARF standard says in the section
4847 about "Structure, Union, and Class Type Entries": "An incomplete
4848 structure, union or class type is represented by a structure,
4849 union or class entry that does not have a byte size attribute
4850 and that has a DW_AT_declaration attribute." */
4851 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4852 {
4853 if (built_actual_name)
4854 xfree (actual_name);
4855 return;
4856 }
fa4028e9 4857
63d06c5c
DC
4858 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4859 static vs. global. */
38d518c9 4860 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4861 built_actual_name,
176620f1 4862 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4863 (cu->language == language_cplus
4864 || cu->language == language_java)
63d06c5c
DC
4865 ? &objfile->global_psymbols
4866 : &objfile->static_psymbols,
e142c38c 4867 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4868
c906108c
SS
4869 break;
4870 case DW_TAG_enumerator:
38d518c9 4871 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4872 built_actual_name,
176620f1 4873 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4874 (cu->language == language_cplus
4875 || cu->language == language_java)
f6fe98ef
DJ
4876 ? &objfile->global_psymbols
4877 : &objfile->static_psymbols,
e142c38c 4878 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4879 break;
4880 default:
4881 break;
4882 }
5c4e30ca 4883
72bf9492
DJ
4884 if (built_actual_name)
4885 xfree (actual_name);
c906108c
SS
4886}
4887
5c4e30ca
DC
4888/* Read a partial die corresponding to a namespace; also, add a symbol
4889 corresponding to that namespace to the symbol table. NAMESPACE is
4890 the name of the enclosing namespace. */
91c24f0a 4891
72bf9492
DJ
4892static void
4893add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4894 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4895 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4896{
72bf9492 4897 /* Add a symbol for the namespace. */
e7c27a73 4898
72bf9492 4899 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4900
4901 /* Now scan partial symbols in that namespace. */
4902
91c24f0a 4903 if (pdi->has_children)
5734ee8b 4904 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4905}
4906
5d7cb8df
JK
4907/* Read a partial die corresponding to a Fortran module. */
4908
4909static void
4910add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4911 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4912{
f55ee35c 4913 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4914
4915 if (pdi->has_children)
4916 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4917}
4918
bc30ff58
JB
4919/* Read a partial die corresponding to a subprogram and create a partial
4920 symbol for that subprogram. When the CU language allows it, this
4921 routine also defines a partial symbol for each nested subprogram
4922 that this subprogram contains.
6e70227d 4923
bc30ff58
JB
4924 DIE my also be a lexical block, in which case we simply search
4925 recursively for suprograms defined inside that lexical block.
4926 Again, this is only performed when the CU language allows this
4927 type of definitions. */
4928
4929static void
4930add_partial_subprogram (struct partial_die_info *pdi,
4931 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4932 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4933{
4934 if (pdi->tag == DW_TAG_subprogram)
4935 {
4936 if (pdi->has_pc_info)
4937 {
4938 if (pdi->lowpc < *lowpc)
4939 *lowpc = pdi->lowpc;
4940 if (pdi->highpc > *highpc)
4941 *highpc = pdi->highpc;
5734ee8b
DJ
4942 if (need_pc)
4943 {
4944 CORE_ADDR baseaddr;
4945 struct objfile *objfile = cu->objfile;
4946
4947 baseaddr = ANOFFSET (objfile->section_offsets,
4948 SECT_OFF_TEXT (objfile));
4949 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4950 pdi->lowpc + baseaddr,
4951 pdi->highpc - 1 + baseaddr,
9291a0cd 4952 cu->per_cu->v.psymtab);
5734ee8b 4953 }
481860b3
GB
4954 }
4955
4956 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4957 {
bc30ff58 4958 if (!pdi->is_declaration)
e8d05480
JB
4959 /* Ignore subprogram DIEs that do not have a name, they are
4960 illegal. Do not emit a complaint at this point, we will
4961 do so when we convert this psymtab into a symtab. */
4962 if (pdi->name)
4963 add_partial_symbol (pdi, cu);
bc30ff58
JB
4964 }
4965 }
6e70227d 4966
bc30ff58
JB
4967 if (! pdi->has_children)
4968 return;
4969
4970 if (cu->language == language_ada)
4971 {
4972 pdi = pdi->die_child;
4973 while (pdi != NULL)
4974 {
4975 fixup_partial_die (pdi, cu);
4976 if (pdi->tag == DW_TAG_subprogram
4977 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4978 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4979 pdi = pdi->die_sibling;
4980 }
4981 }
4982}
4983
91c24f0a
DC
4984/* Read a partial die corresponding to an enumeration type. */
4985
72bf9492
DJ
4986static void
4987add_partial_enumeration (struct partial_die_info *enum_pdi,
4988 struct dwarf2_cu *cu)
91c24f0a 4989{
72bf9492 4990 struct partial_die_info *pdi;
91c24f0a
DC
4991
4992 if (enum_pdi->name != NULL)
72bf9492
DJ
4993 add_partial_symbol (enum_pdi, cu);
4994
4995 pdi = enum_pdi->die_child;
4996 while (pdi)
91c24f0a 4997 {
72bf9492 4998 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 4999 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 5000 else
72bf9492
DJ
5001 add_partial_symbol (pdi, cu);
5002 pdi = pdi->die_sibling;
91c24f0a 5003 }
91c24f0a
DC
5004}
5005
6caca83c
CC
5006/* Return the initial uleb128 in the die at INFO_PTR. */
5007
5008static unsigned int
5009peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
5010{
5011 unsigned int bytes_read;
5012
5013 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5014}
5015
4bb7a0a7
DJ
5016/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
5017 Return the corresponding abbrev, or NULL if the number is zero (indicating
5018 an empty DIE). In either case *BYTES_READ will be set to the length of
5019 the initial number. */
5020
5021static struct abbrev_info *
fe1b8b76 5022peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 5023 struct dwarf2_cu *cu)
4bb7a0a7
DJ
5024{
5025 bfd *abfd = cu->objfile->obfd;
5026 unsigned int abbrev_number;
5027 struct abbrev_info *abbrev;
5028
5029 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
5030
5031 if (abbrev_number == 0)
5032 return NULL;
5033
5034 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
5035 if (!abbrev)
5036 {
3e43a32a
MS
5037 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
5038 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
5039 }
5040
5041 return abbrev;
5042}
5043
93311388
DE
5044/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5045 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
5046 DIE. Any children of the skipped DIEs will also be skipped. */
5047
fe1b8b76 5048static gdb_byte *
dee91e82 5049skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 5050{
dee91e82 5051 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
5052 struct abbrev_info *abbrev;
5053 unsigned int bytes_read;
5054
5055 while (1)
5056 {
5057 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5058 if (abbrev == NULL)
5059 return info_ptr + bytes_read;
5060 else
dee91e82 5061 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
5062 }
5063}
5064
93311388
DE
5065/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5066 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
5067 abbrev corresponding to that skipped uleb128 should be passed in
5068 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5069 children. */
5070
fe1b8b76 5071static gdb_byte *
dee91e82
DE
5072skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5073 struct abbrev_info *abbrev)
4bb7a0a7
DJ
5074{
5075 unsigned int bytes_read;
5076 struct attribute attr;
dee91e82
DE
5077 bfd *abfd = reader->abfd;
5078 struct dwarf2_cu *cu = reader->cu;
5079 gdb_byte *buffer = reader->buffer;
f664829e
DE
5080 const gdb_byte *buffer_end = reader->buffer_end;
5081 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
5082 unsigned int form, i;
5083
5084 for (i = 0; i < abbrev->num_attrs; i++)
5085 {
5086 /* The only abbrev we care about is DW_AT_sibling. */
5087 if (abbrev->attrs[i].name == DW_AT_sibling)
5088 {
dee91e82 5089 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 5090 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
5091 complaint (&symfile_complaints,
5092 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 5093 else
b64f50a1 5094 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
5095 }
5096
5097 /* If it isn't DW_AT_sibling, skip this attribute. */
5098 form = abbrev->attrs[i].form;
5099 skip_attribute:
5100 switch (form)
5101 {
4bb7a0a7 5102 case DW_FORM_ref_addr:
ae411497
TT
5103 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5104 and later it is offset sized. */
5105 if (cu->header.version == 2)
5106 info_ptr += cu->header.addr_size;
5107 else
5108 info_ptr += cu->header.offset_size;
5109 break;
5110 case DW_FORM_addr:
4bb7a0a7
DJ
5111 info_ptr += cu->header.addr_size;
5112 break;
5113 case DW_FORM_data1:
5114 case DW_FORM_ref1:
5115 case DW_FORM_flag:
5116 info_ptr += 1;
5117 break;
2dc7f7b3
TT
5118 case DW_FORM_flag_present:
5119 break;
4bb7a0a7
DJ
5120 case DW_FORM_data2:
5121 case DW_FORM_ref2:
5122 info_ptr += 2;
5123 break;
5124 case DW_FORM_data4:
5125 case DW_FORM_ref4:
5126 info_ptr += 4;
5127 break;
5128 case DW_FORM_data8:
5129 case DW_FORM_ref8:
55f1336d 5130 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
5131 info_ptr += 8;
5132 break;
5133 case DW_FORM_string:
9b1c24c8 5134 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
5135 info_ptr += bytes_read;
5136 break;
2dc7f7b3 5137 case DW_FORM_sec_offset:
4bb7a0a7
DJ
5138 case DW_FORM_strp:
5139 info_ptr += cu->header.offset_size;
5140 break;
2dc7f7b3 5141 case DW_FORM_exprloc:
4bb7a0a7
DJ
5142 case DW_FORM_block:
5143 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5144 info_ptr += bytes_read;
5145 break;
5146 case DW_FORM_block1:
5147 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5148 break;
5149 case DW_FORM_block2:
5150 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5151 break;
5152 case DW_FORM_block4:
5153 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5154 break;
5155 case DW_FORM_sdata:
5156 case DW_FORM_udata:
5157 case DW_FORM_ref_udata:
3019eac3
DE
5158 case DW_FORM_GNU_addr_index:
5159 case DW_FORM_GNU_str_index:
f664829e 5160 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
5161 break;
5162 case DW_FORM_indirect:
5163 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5164 info_ptr += bytes_read;
5165 /* We need to continue parsing from here, so just go back to
5166 the top. */
5167 goto skip_attribute;
5168
5169 default:
3e43a32a
MS
5170 error (_("Dwarf Error: Cannot handle %s "
5171 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
5172 dwarf_form_name (form),
5173 bfd_get_filename (abfd));
5174 }
5175 }
5176
5177 if (abbrev->has_children)
dee91e82 5178 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
5179 else
5180 return info_ptr;
5181}
5182
93311388 5183/* Locate ORIG_PDI's sibling.
dee91e82 5184 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 5185
fe1b8b76 5186static gdb_byte *
dee91e82
DE
5187locate_pdi_sibling (const struct die_reader_specs *reader,
5188 struct partial_die_info *orig_pdi,
5189 gdb_byte *info_ptr)
91c24f0a
DC
5190{
5191 /* Do we know the sibling already? */
72bf9492 5192
91c24f0a
DC
5193 if (orig_pdi->sibling)
5194 return orig_pdi->sibling;
5195
5196 /* Are there any children to deal with? */
5197
5198 if (!orig_pdi->has_children)
5199 return info_ptr;
5200
4bb7a0a7 5201 /* Skip the children the long way. */
91c24f0a 5202
dee91e82 5203 return skip_children (reader, info_ptr);
91c24f0a
DC
5204}
5205
c906108c
SS
5206/* Expand this partial symbol table into a full symbol table. */
5207
5208static void
fba45db2 5209dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 5210{
c906108c
SS
5211 if (pst != NULL)
5212 {
5213 if (pst->readin)
5214 {
3e43a32a
MS
5215 warning (_("bug: psymtab for %s is already read in."),
5216 pst->filename);
c906108c
SS
5217 }
5218 else
5219 {
5220 if (info_verbose)
5221 {
3e43a32a
MS
5222 printf_filtered (_("Reading in symbols for %s..."),
5223 pst->filename);
c906108c
SS
5224 gdb_flush (gdb_stdout);
5225 }
5226
10b3939b
DJ
5227 /* Restore our global data. */
5228 dwarf2_per_objfile = objfile_data (pst->objfile,
5229 dwarf2_objfile_data_key);
5230
b2ab525c
KB
5231 /* If this psymtab is constructed from a debug-only objfile, the
5232 has_section_at_zero flag will not necessarily be correct. We
5233 can get the correct value for this flag by looking at the data
5234 associated with the (presumably stripped) associated objfile. */
5235 if (pst->objfile->separate_debug_objfile_backlink)
5236 {
5237 struct dwarf2_per_objfile *dpo_backlink
5238 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5239 dwarf2_objfile_data_key);
9a619af0 5240
b2ab525c
KB
5241 dwarf2_per_objfile->has_section_at_zero
5242 = dpo_backlink->has_section_at_zero;
5243 }
5244
98bfdba5
PA
5245 dwarf2_per_objfile->reading_partial_symbols = 0;
5246
c906108c
SS
5247 psymtab_to_symtab_1 (pst);
5248
5249 /* Finish up the debug error message. */
5250 if (info_verbose)
a3f17187 5251 printf_filtered (_("done.\n"));
c906108c
SS
5252 }
5253 }
95554aad
TT
5254
5255 process_cu_includes ();
c906108c 5256}
9cdd5dbd
DE
5257\f
5258/* Reading in full CUs. */
c906108c 5259
10b3939b
DJ
5260/* Add PER_CU to the queue. */
5261
5262static void
95554aad
TT
5263queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5264 enum language pretend_language)
10b3939b
DJ
5265{
5266 struct dwarf2_queue_item *item;
5267
5268 per_cu->queued = 1;
5269 item = xmalloc (sizeof (*item));
5270 item->per_cu = per_cu;
95554aad 5271 item->pretend_language = pretend_language;
10b3939b
DJ
5272 item->next = NULL;
5273
5274 if (dwarf2_queue == NULL)
5275 dwarf2_queue = item;
5276 else
5277 dwarf2_queue_tail->next = item;
5278
5279 dwarf2_queue_tail = item;
5280}
5281
5282/* Process the queue. */
5283
5284static void
a0f42c21 5285process_queue (void)
10b3939b
DJ
5286{
5287 struct dwarf2_queue_item *item, *next_item;
5288
03dd20cc
DJ
5289 /* The queue starts out with one item, but following a DIE reference
5290 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
5291 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5292 {
9291a0cd
TT
5293 if (dwarf2_per_objfile->using_index
5294 ? !item->per_cu->v.quick->symtab
5295 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
95554aad 5296 process_full_comp_unit (item->per_cu, item->pretend_language);
10b3939b
DJ
5297
5298 item->per_cu->queued = 0;
5299 next_item = item->next;
5300 xfree (item);
5301 }
5302
5303 dwarf2_queue_tail = NULL;
5304}
5305
5306/* Free all allocated queue entries. This function only releases anything if
5307 an error was thrown; if the queue was processed then it would have been
5308 freed as we went along. */
5309
5310static void
5311dwarf2_release_queue (void *dummy)
5312{
5313 struct dwarf2_queue_item *item, *last;
5314
5315 item = dwarf2_queue;
5316 while (item)
5317 {
5318 /* Anything still marked queued is likely to be in an
5319 inconsistent state, so discard it. */
5320 if (item->per_cu->queued)
5321 {
5322 if (item->per_cu->cu != NULL)
dee91e82 5323 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
5324 item->per_cu->queued = 0;
5325 }
5326
5327 last = item;
5328 item = item->next;
5329 xfree (last);
5330 }
5331
5332 dwarf2_queue = dwarf2_queue_tail = NULL;
5333}
5334
5335/* Read in full symbols for PST, and anything it depends on. */
5336
c906108c 5337static void
fba45db2 5338psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 5339{
10b3939b 5340 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
5341 int i;
5342
95554aad
TT
5343 if (pst->readin)
5344 return;
5345
aaa75496 5346 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
5347 if (!pst->dependencies[i]->readin
5348 && pst->dependencies[i]->user == NULL)
aaa75496
JB
5349 {
5350 /* Inform about additional files that need to be read in. */
5351 if (info_verbose)
5352 {
a3f17187 5353 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
5354 fputs_filtered (" ", gdb_stdout);
5355 wrap_here ("");
5356 fputs_filtered ("and ", gdb_stdout);
5357 wrap_here ("");
5358 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 5359 wrap_here (""); /* Flush output. */
aaa75496
JB
5360 gdb_flush (gdb_stdout);
5361 }
5362 psymtab_to_symtab_1 (pst->dependencies[i]);
5363 }
5364
e38df1d0 5365 per_cu = pst->read_symtab_private;
10b3939b
DJ
5366
5367 if (per_cu == NULL)
aaa75496
JB
5368 {
5369 /* It's an include file, no symbols to read for it.
5370 Everything is in the parent symtab. */
5371 pst->readin = 1;
5372 return;
5373 }
c906108c 5374
a0f42c21 5375 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
5376}
5377
dee91e82
DE
5378/* Trivial hash function for die_info: the hash value of a DIE
5379 is its offset in .debug_info for this objfile. */
10b3939b 5380
dee91e82
DE
5381static hashval_t
5382die_hash (const void *item)
10b3939b 5383{
dee91e82 5384 const struct die_info *die = item;
6502dd73 5385
dee91e82
DE
5386 return die->offset.sect_off;
5387}
63d06c5c 5388
dee91e82
DE
5389/* Trivial comparison function for die_info structures: two DIEs
5390 are equal if they have the same offset. */
98bfdba5 5391
dee91e82
DE
5392static int
5393die_eq (const void *item_lhs, const void *item_rhs)
5394{
5395 const struct die_info *die_lhs = item_lhs;
5396 const struct die_info *die_rhs = item_rhs;
c906108c 5397
dee91e82
DE
5398 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5399}
c906108c 5400
dee91e82
DE
5401/* die_reader_func for load_full_comp_unit.
5402 This is identical to read_signatured_type_reader,
5403 but is kept separate for now. */
c906108c 5404
dee91e82
DE
5405static void
5406load_full_comp_unit_reader (const struct die_reader_specs *reader,
5407 gdb_byte *info_ptr,
5408 struct die_info *comp_unit_die,
5409 int has_children,
5410 void *data)
5411{
5412 struct dwarf2_cu *cu = reader->cu;
95554aad 5413 enum language *language_ptr = data;
6caca83c 5414
dee91e82
DE
5415 gdb_assert (cu->die_hash == NULL);
5416 cu->die_hash =
5417 htab_create_alloc_ex (cu->header.length / 12,
5418 die_hash,
5419 die_eq,
5420 NULL,
5421 &cu->comp_unit_obstack,
5422 hashtab_obstack_allocate,
5423 dummy_obstack_deallocate);
e142c38c 5424
dee91e82
DE
5425 if (has_children)
5426 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5427 &info_ptr, comp_unit_die);
5428 cu->dies = comp_unit_die;
5429 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
5430
5431 /* We try not to read any attributes in this function, because not
9cdd5dbd 5432 all CUs needed for references have been loaded yet, and symbol
10b3939b 5433 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
5434 or we won't be able to build types correctly.
5435 Similarly, if we do not read the producer, we can not apply
5436 producer-specific interpretation. */
95554aad 5437 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 5438}
10b3939b 5439
dee91e82 5440/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 5441
dee91e82 5442static void
95554aad
TT
5443load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5444 enum language pretend_language)
dee91e82 5445{
3019eac3 5446 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 5447
95554aad
TT
5448 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5449 &pretend_language);
10b3939b
DJ
5450}
5451
3da10d80
KS
5452/* Add a DIE to the delayed physname list. */
5453
5454static void
5455add_to_method_list (struct type *type, int fnfield_index, int index,
5456 const char *name, struct die_info *die,
5457 struct dwarf2_cu *cu)
5458{
5459 struct delayed_method_info mi;
5460 mi.type = type;
5461 mi.fnfield_index = fnfield_index;
5462 mi.index = index;
5463 mi.name = name;
5464 mi.die = die;
5465 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5466}
5467
5468/* A cleanup for freeing the delayed method list. */
5469
5470static void
5471free_delayed_list (void *ptr)
5472{
5473 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5474 if (cu->method_list != NULL)
5475 {
5476 VEC_free (delayed_method_info, cu->method_list);
5477 cu->method_list = NULL;
5478 }
5479}
5480
5481/* Compute the physnames of any methods on the CU's method list.
5482
5483 The computation of method physnames is delayed in order to avoid the
5484 (bad) condition that one of the method's formal parameters is of an as yet
5485 incomplete type. */
5486
5487static void
5488compute_delayed_physnames (struct dwarf2_cu *cu)
5489{
5490 int i;
5491 struct delayed_method_info *mi;
5492 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5493 {
1d06ead6 5494 const char *physname;
3da10d80
KS
5495 struct fn_fieldlist *fn_flp
5496 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 5497 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
5498 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5499 }
5500}
5501
a766d390
DE
5502/* Go objects should be embedded in a DW_TAG_module DIE,
5503 and it's not clear if/how imported objects will appear.
5504 To keep Go support simple until that's worked out,
5505 go back through what we've read and create something usable.
5506 We could do this while processing each DIE, and feels kinda cleaner,
5507 but that way is more invasive.
5508 This is to, for example, allow the user to type "p var" or "b main"
5509 without having to specify the package name, and allow lookups
5510 of module.object to work in contexts that use the expression
5511 parser. */
5512
5513static void
5514fixup_go_packaging (struct dwarf2_cu *cu)
5515{
5516 char *package_name = NULL;
5517 struct pending *list;
5518 int i;
5519
5520 for (list = global_symbols; list != NULL; list = list->next)
5521 {
5522 for (i = 0; i < list->nsyms; ++i)
5523 {
5524 struct symbol *sym = list->symbol[i];
5525
5526 if (SYMBOL_LANGUAGE (sym) == language_go
5527 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5528 {
5529 char *this_package_name = go_symbol_package_name (sym);
5530
5531 if (this_package_name == NULL)
5532 continue;
5533 if (package_name == NULL)
5534 package_name = this_package_name;
5535 else
5536 {
5537 if (strcmp (package_name, this_package_name) != 0)
5538 complaint (&symfile_complaints,
5539 _("Symtab %s has objects from two different Go packages: %s and %s"),
5540 (sym->symtab && sym->symtab->filename
5541 ? sym->symtab->filename
5542 : cu->objfile->name),
5543 this_package_name, package_name);
5544 xfree (this_package_name);
5545 }
5546 }
5547 }
5548 }
5549
5550 if (package_name != NULL)
5551 {
5552 struct objfile *objfile = cu->objfile;
5553 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5554 package_name, objfile);
5555 struct symbol *sym;
5556
5557 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5558
5559 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5560 SYMBOL_SET_LANGUAGE (sym, language_go);
5561 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5562 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5563 e.g., "main" finds the "main" module and not C's main(). */
5564 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5565 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5566 SYMBOL_TYPE (sym) = type;
5567
5568 add_symbol_to_list (sym, &global_symbols);
5569
5570 xfree (package_name);
5571 }
5572}
5573
95554aad
TT
5574static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5575
5576/* Return the symtab for PER_CU. This works properly regardless of
5577 whether we're using the index or psymtabs. */
5578
5579static struct symtab *
5580get_symtab (struct dwarf2_per_cu_data *per_cu)
5581{
5582 return (dwarf2_per_objfile->using_index
5583 ? per_cu->v.quick->symtab
5584 : per_cu->v.psymtab->symtab);
5585}
5586
5587/* A helper function for computing the list of all symbol tables
5588 included by PER_CU. */
5589
5590static void
5591recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5592 htab_t all_children,
5593 struct dwarf2_per_cu_data *per_cu)
5594{
5595 void **slot;
5596 int ix;
5597 struct dwarf2_per_cu_data *iter;
5598
5599 slot = htab_find_slot (all_children, per_cu, INSERT);
5600 if (*slot != NULL)
5601 {
5602 /* This inclusion and its children have been processed. */
5603 return;
5604 }
5605
5606 *slot = per_cu;
5607 /* Only add a CU if it has a symbol table. */
5608 if (get_symtab (per_cu) != NULL)
5609 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5610
5611 for (ix = 0;
5612 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5613 ++ix)
5614 recursively_compute_inclusions (result, all_children, iter);
5615}
5616
5617/* Compute the symtab 'includes' fields for the symtab related to
5618 PER_CU. */
5619
5620static void
5621compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5622{
5623 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5624 {
5625 int ix, len;
5626 struct dwarf2_per_cu_data *iter;
5627 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5628 htab_t all_children;
5629 struct symtab *symtab = get_symtab (per_cu);
5630
5631 /* If we don't have a symtab, we can just skip this case. */
5632 if (symtab == NULL)
5633 return;
5634
5635 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5636 NULL, xcalloc, xfree);
5637
5638 for (ix = 0;
5639 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5640 ix, iter);
5641 ++ix)
5642 recursively_compute_inclusions (&result_children, all_children, iter);
5643
5644 /* Now we have a transitive closure of all the included CUs, so
5645 we can convert it to a list of symtabs. */
5646 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5647 symtab->includes
5648 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5649 (len + 1) * sizeof (struct symtab *));
5650 for (ix = 0;
5651 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5652 ++ix)
5653 symtab->includes[ix] = get_symtab (iter);
5654 symtab->includes[len] = NULL;
5655
5656 VEC_free (dwarf2_per_cu_ptr, result_children);
5657 htab_delete (all_children);
5658 }
5659}
5660
5661/* Compute the 'includes' field for the symtabs of all the CUs we just
5662 read. */
5663
5664static void
5665process_cu_includes (void)
5666{
5667 int ix;
5668 struct dwarf2_per_cu_data *iter;
5669
5670 for (ix = 0;
5671 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5672 ix, iter);
5673 ++ix)
5674 compute_symtab_includes (iter);
5675
5676 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5677}
5678
9cdd5dbd 5679/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
5680 already been loaded into memory. */
5681
5682static void
95554aad
TT
5683process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5684 enum language pretend_language)
10b3939b 5685{
10b3939b 5686 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 5687 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
5688 CORE_ADDR lowpc, highpc;
5689 struct symtab *symtab;
3da10d80 5690 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
5691 CORE_ADDR baseaddr;
5692
5693 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5694
10b3939b
DJ
5695 buildsym_init ();
5696 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 5697 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
5698
5699 cu->list_in_scope = &file_symbols;
c906108c 5700
95554aad
TT
5701 cu->language = pretend_language;
5702 cu->language_defn = language_def (cu->language);
5703
c906108c 5704 /* Do line number decoding in read_file_scope () */
10b3939b 5705 process_die (cu->dies, cu);
c906108c 5706
a766d390
DE
5707 /* For now fudge the Go package. */
5708 if (cu->language == language_go)
5709 fixup_go_packaging (cu);
5710
3da10d80
KS
5711 /* Now that we have processed all the DIEs in the CU, all the types
5712 should be complete, and it should now be safe to compute all of the
5713 physnames. */
5714 compute_delayed_physnames (cu);
5715 do_cleanups (delayed_list_cleanup);
5716
fae299cd
DC
5717 /* Some compilers don't define a DW_AT_high_pc attribute for the
5718 compilation unit. If the DW_AT_high_pc is missing, synthesize
5719 it, by scanning the DIE's below the compilation unit. */
10b3939b 5720 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 5721
613e1657 5722 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 5723
8be455d7 5724 if (symtab != NULL)
c906108c 5725 {
df15bd07 5726 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 5727
8be455d7
JK
5728 /* Set symtab language to language from DW_AT_language. If the
5729 compilation is from a C file generated by language preprocessors, do
5730 not set the language if it was already deduced by start_subfile. */
5731 if (!(cu->language == language_c && symtab->language != language_c))
5732 symtab->language = cu->language;
5733
5734 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5735 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
5736 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5737 there were bugs in prologue debug info, fixed later in GCC-4.5
5738 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
5739
5740 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5741 needed, it would be wrong due to missing DW_AT_producer there.
5742
5743 Still one can confuse GDB by using non-standard GCC compilation
5744 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5745 */
ab260dad 5746 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 5747 symtab->locations_valid = 1;
e0d00bc7
JK
5748
5749 if (gcc_4_minor >= 5)
5750 symtab->epilogue_unwind_valid = 1;
96408a79
SA
5751
5752 symtab->call_site_htab = cu->call_site_htab;
c906108c 5753 }
9291a0cd
TT
5754
5755 if (dwarf2_per_objfile->using_index)
5756 per_cu->v.quick->symtab = symtab;
5757 else
5758 {
5759 struct partial_symtab *pst = per_cu->v.psymtab;
5760 pst->symtab = symtab;
5761 pst->readin = 1;
5762 }
c906108c 5763
95554aad
TT
5764 /* Push it for inclusion processing later. */
5765 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5766
c906108c
SS
5767 do_cleanups (back_to);
5768}
5769
95554aad
TT
5770/* Process an imported unit DIE. */
5771
5772static void
5773process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5774{
5775 struct attribute *attr;
5776
5777 attr = dwarf2_attr (die, DW_AT_import, cu);
5778 if (attr != NULL)
5779 {
5780 struct dwarf2_per_cu_data *per_cu;
5781 struct symtab *imported_symtab;
5782 sect_offset offset;
5783
5784 offset = dwarf2_get_ref_die_offset (attr);
5785 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5786
5787 /* Queue the unit, if needed. */
5788 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
5789 load_full_comp_unit (per_cu, cu->language);
5790
5791 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5792 per_cu);
5793 }
5794}
5795
c906108c
SS
5796/* Process a die and its children. */
5797
5798static void
e7c27a73 5799process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
5800{
5801 switch (die->tag)
5802 {
5803 case DW_TAG_padding:
5804 break;
5805 case DW_TAG_compile_unit:
95554aad 5806 case DW_TAG_partial_unit:
e7c27a73 5807 read_file_scope (die, cu);
c906108c 5808 break;
348e048f
DE
5809 case DW_TAG_type_unit:
5810 read_type_unit_scope (die, cu);
5811 break;
c906108c 5812 case DW_TAG_subprogram:
c906108c 5813 case DW_TAG_inlined_subroutine:
edb3359d 5814 read_func_scope (die, cu);
c906108c
SS
5815 break;
5816 case DW_TAG_lexical_block:
14898363
L
5817 case DW_TAG_try_block:
5818 case DW_TAG_catch_block:
e7c27a73 5819 read_lexical_block_scope (die, cu);
c906108c 5820 break;
96408a79
SA
5821 case DW_TAG_GNU_call_site:
5822 read_call_site_scope (die, cu);
5823 break;
c906108c 5824 case DW_TAG_class_type:
680b30c7 5825 case DW_TAG_interface_type:
c906108c
SS
5826 case DW_TAG_structure_type:
5827 case DW_TAG_union_type:
134d01f1 5828 process_structure_scope (die, cu);
c906108c
SS
5829 break;
5830 case DW_TAG_enumeration_type:
134d01f1 5831 process_enumeration_scope (die, cu);
c906108c 5832 break;
134d01f1 5833
f792889a
DJ
5834 /* These dies have a type, but processing them does not create
5835 a symbol or recurse to process the children. Therefore we can
5836 read them on-demand through read_type_die. */
c906108c 5837 case DW_TAG_subroutine_type:
72019c9c 5838 case DW_TAG_set_type:
c906108c 5839 case DW_TAG_array_type:
c906108c 5840 case DW_TAG_pointer_type:
c906108c 5841 case DW_TAG_ptr_to_member_type:
c906108c 5842 case DW_TAG_reference_type:
c906108c 5843 case DW_TAG_string_type:
c906108c 5844 break;
134d01f1 5845
c906108c 5846 case DW_TAG_base_type:
a02abb62 5847 case DW_TAG_subrange_type:
cb249c71 5848 case DW_TAG_typedef:
134d01f1
DJ
5849 /* Add a typedef symbol for the type definition, if it has a
5850 DW_AT_name. */
f792889a 5851 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 5852 break;
c906108c 5853 case DW_TAG_common_block:
e7c27a73 5854 read_common_block (die, cu);
c906108c
SS
5855 break;
5856 case DW_TAG_common_inclusion:
5857 break;
d9fa45fe 5858 case DW_TAG_namespace:
63d06c5c 5859 processing_has_namespace_info = 1;
e7c27a73 5860 read_namespace (die, cu);
d9fa45fe 5861 break;
5d7cb8df 5862 case DW_TAG_module:
f55ee35c 5863 processing_has_namespace_info = 1;
5d7cb8df
JK
5864 read_module (die, cu);
5865 break;
d9fa45fe
DC
5866 case DW_TAG_imported_declaration:
5867 case DW_TAG_imported_module:
63d06c5c 5868 processing_has_namespace_info = 1;
27aa8d6a
SW
5869 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5870 || cu->language != language_fortran))
5871 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5872 dwarf_tag_name (die->tag));
5873 read_import_statement (die, cu);
d9fa45fe 5874 break;
95554aad
TT
5875
5876 case DW_TAG_imported_unit:
5877 process_imported_unit_die (die, cu);
5878 break;
5879
c906108c 5880 default:
e7c27a73 5881 new_symbol (die, NULL, cu);
c906108c
SS
5882 break;
5883 }
5884}
5885
94af9270
KS
5886/* A helper function for dwarf2_compute_name which determines whether DIE
5887 needs to have the name of the scope prepended to the name listed in the
5888 die. */
5889
5890static int
5891die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5892{
1c809c68
TT
5893 struct attribute *attr;
5894
94af9270
KS
5895 switch (die->tag)
5896 {
5897 case DW_TAG_namespace:
5898 case DW_TAG_typedef:
5899 case DW_TAG_class_type:
5900 case DW_TAG_interface_type:
5901 case DW_TAG_structure_type:
5902 case DW_TAG_union_type:
5903 case DW_TAG_enumeration_type:
5904 case DW_TAG_enumerator:
5905 case DW_TAG_subprogram:
5906 case DW_TAG_member:
5907 return 1;
5908
5909 case DW_TAG_variable:
c2b0a229 5910 case DW_TAG_constant:
94af9270
KS
5911 /* We only need to prefix "globally" visible variables. These include
5912 any variable marked with DW_AT_external or any variable that
5913 lives in a namespace. [Variables in anonymous namespaces
5914 require prefixing, but they are not DW_AT_external.] */
5915
5916 if (dwarf2_attr (die, DW_AT_specification, cu))
5917 {
5918 struct dwarf2_cu *spec_cu = cu;
9a619af0 5919
94af9270
KS
5920 return die_needs_namespace (die_specification (die, &spec_cu),
5921 spec_cu);
5922 }
5923
1c809c68 5924 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
5925 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5926 && die->parent->tag != DW_TAG_module)
1c809c68
TT
5927 return 0;
5928 /* A variable in a lexical block of some kind does not need a
5929 namespace, even though in C++ such variables may be external
5930 and have a mangled name. */
5931 if (die->parent->tag == DW_TAG_lexical_block
5932 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5933 || die->parent->tag == DW_TAG_catch_block
5934 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5935 return 0;
5936 return 1;
94af9270
KS
5937
5938 default:
5939 return 0;
5940 }
5941}
5942
98bfdba5
PA
5943/* Retrieve the last character from a mem_file. */
5944
5945static void
5946do_ui_file_peek_last (void *object, const char *buffer, long length)
5947{
5948 char *last_char_p = (char *) object;
5949
5950 if (length > 0)
5951 *last_char_p = buffer[length - 1];
5952}
5953
94af9270 5954/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
5955 compute the physname for the object, which include a method's:
5956 - formal parameters (C++/Java),
5957 - receiver type (Go),
5958 - return type (Java).
5959
5960 The term "physname" is a bit confusing.
5961 For C++, for example, it is the demangled name.
5962 For Go, for example, it's the mangled name.
94af9270 5963
af6b7be1
JB
5964 For Ada, return the DIE's linkage name rather than the fully qualified
5965 name. PHYSNAME is ignored..
5966
94af9270
KS
5967 The result is allocated on the objfile_obstack and canonicalized. */
5968
5969static const char *
5970dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5971 int physname)
5972{
bb5ed363
DE
5973 struct objfile *objfile = cu->objfile;
5974
94af9270
KS
5975 if (name == NULL)
5976 name = dwarf2_name (die, cu);
5977
f55ee35c
JK
5978 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5979 compute it by typename_concat inside GDB. */
5980 if (cu->language == language_ada
5981 || (cu->language == language_fortran && physname))
5982 {
5983 /* For Ada unit, we prefer the linkage name over the name, as
5984 the former contains the exported name, which the user expects
5985 to be able to reference. Ideally, we want the user to be able
5986 to reference this entity using either natural or linkage name,
5987 but we haven't started looking at this enhancement yet. */
5988 struct attribute *attr;
5989
5990 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5991 if (attr == NULL)
5992 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5993 if (attr && DW_STRING (attr))
5994 return DW_STRING (attr);
5995 }
5996
94af9270
KS
5997 /* These are the only languages we know how to qualify names in. */
5998 if (name != NULL
f55ee35c
JK
5999 && (cu->language == language_cplus || cu->language == language_java
6000 || cu->language == language_fortran))
94af9270
KS
6001 {
6002 if (die_needs_namespace (die, cu))
6003 {
6004 long length;
0d5cff50 6005 const char *prefix;
94af9270
KS
6006 struct ui_file *buf;
6007
6008 prefix = determine_prefix (die, cu);
6009 buf = mem_fileopen ();
6010 if (*prefix != '\0')
6011 {
f55ee35c
JK
6012 char *prefixed_name = typename_concat (NULL, prefix, name,
6013 physname, cu);
9a619af0 6014
94af9270
KS
6015 fputs_unfiltered (prefixed_name, buf);
6016 xfree (prefixed_name);
6017 }
6018 else
62d5b8da 6019 fputs_unfiltered (name, buf);
94af9270 6020
98bfdba5
PA
6021 /* Template parameters may be specified in the DIE's DW_AT_name, or
6022 as children with DW_TAG_template_type_param or
6023 DW_TAG_value_type_param. If the latter, add them to the name
6024 here. If the name already has template parameters, then
6025 skip this step; some versions of GCC emit both, and
6026 it is more efficient to use the pre-computed name.
6027
6028 Something to keep in mind about this process: it is very
6029 unlikely, or in some cases downright impossible, to produce
6030 something that will match the mangled name of a function.
6031 If the definition of the function has the same debug info,
6032 we should be able to match up with it anyway. But fallbacks
6033 using the minimal symbol, for instance to find a method
6034 implemented in a stripped copy of libstdc++, will not work.
6035 If we do not have debug info for the definition, we will have to
6036 match them up some other way.
6037
6038 When we do name matching there is a related problem with function
6039 templates; two instantiated function templates are allowed to
6040 differ only by their return types, which we do not add here. */
6041
6042 if (cu->language == language_cplus && strchr (name, '<') == NULL)
6043 {
6044 struct attribute *attr;
6045 struct die_info *child;
6046 int first = 1;
6047
6048 die->building_fullname = 1;
6049
6050 for (child = die->child; child != NULL; child = child->sibling)
6051 {
6052 struct type *type;
12df843f 6053 LONGEST value;
98bfdba5
PA
6054 gdb_byte *bytes;
6055 struct dwarf2_locexpr_baton *baton;
6056 struct value *v;
6057
6058 if (child->tag != DW_TAG_template_type_param
6059 && child->tag != DW_TAG_template_value_param)
6060 continue;
6061
6062 if (first)
6063 {
6064 fputs_unfiltered ("<", buf);
6065 first = 0;
6066 }
6067 else
6068 fputs_unfiltered (", ", buf);
6069
6070 attr = dwarf2_attr (child, DW_AT_type, cu);
6071 if (attr == NULL)
6072 {
6073 complaint (&symfile_complaints,
6074 _("template parameter missing DW_AT_type"));
6075 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6076 continue;
6077 }
6078 type = die_type (child, cu);
6079
6080 if (child->tag == DW_TAG_template_type_param)
6081 {
6082 c_print_type (type, "", buf, -1, 0);
6083 continue;
6084 }
6085
6086 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6087 if (attr == NULL)
6088 {
6089 complaint (&symfile_complaints,
3e43a32a
MS
6090 _("template parameter missing "
6091 "DW_AT_const_value"));
98bfdba5
PA
6092 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6093 continue;
6094 }
6095
6096 dwarf2_const_value_attr (attr, type, name,
6097 &cu->comp_unit_obstack, cu,
6098 &value, &bytes, &baton);
6099
6100 if (TYPE_NOSIGN (type))
6101 /* GDB prints characters as NUMBER 'CHAR'. If that's
6102 changed, this can use value_print instead. */
6103 c_printchar (value, type, buf);
6104 else
6105 {
6106 struct value_print_options opts;
6107
6108 if (baton != NULL)
6109 v = dwarf2_evaluate_loc_desc (type, NULL,
6110 baton->data,
6111 baton->size,
6112 baton->per_cu);
6113 else if (bytes != NULL)
6114 {
6115 v = allocate_value (type);
6116 memcpy (value_contents_writeable (v), bytes,
6117 TYPE_LENGTH (type));
6118 }
6119 else
6120 v = value_from_longest (type, value);
6121
3e43a32a
MS
6122 /* Specify decimal so that we do not depend on
6123 the radix. */
98bfdba5
PA
6124 get_formatted_print_options (&opts, 'd');
6125 opts.raw = 1;
6126 value_print (v, buf, &opts);
6127 release_value (v);
6128 value_free (v);
6129 }
6130 }
6131
6132 die->building_fullname = 0;
6133
6134 if (!first)
6135 {
6136 /* Close the argument list, with a space if necessary
6137 (nested templates). */
6138 char last_char = '\0';
6139 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6140 if (last_char == '>')
6141 fputs_unfiltered (" >", buf);
6142 else
6143 fputs_unfiltered (">", buf);
6144 }
6145 }
6146
94af9270
KS
6147 /* For Java and C++ methods, append formal parameter type
6148 information, if PHYSNAME. */
6e70227d 6149
94af9270
KS
6150 if (physname && die->tag == DW_TAG_subprogram
6151 && (cu->language == language_cplus
6152 || cu->language == language_java))
6153 {
6154 struct type *type = read_type_die (die, cu);
6155
3167638f 6156 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
6157
6158 if (cu->language == language_java)
6159 {
6160 /* For java, we must append the return type to method
0963b4bd 6161 names. */
94af9270
KS
6162 if (die->tag == DW_TAG_subprogram)
6163 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6164 0, 0);
6165 }
6166 else if (cu->language == language_cplus)
6167 {
60430eff
DJ
6168 /* Assume that an artificial first parameter is
6169 "this", but do not crash if it is not. RealView
6170 marks unnamed (and thus unused) parameters as
6171 artificial; there is no way to differentiate
6172 the two cases. */
94af9270
KS
6173 if (TYPE_NFIELDS (type) > 0
6174 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 6175 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
6176 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6177 0))))
94af9270
KS
6178 fputs_unfiltered (" const", buf);
6179 }
6180 }
6181
bb5ed363 6182 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
6183 &length);
6184 ui_file_delete (buf);
6185
6186 if (cu->language == language_cplus)
6187 {
6188 char *cname
6189 = dwarf2_canonicalize_name (name, cu,
bb5ed363 6190 &objfile->objfile_obstack);
9a619af0 6191
94af9270
KS
6192 if (cname != NULL)
6193 name = cname;
6194 }
6195 }
6196 }
6197
6198 return name;
6199}
6200
0114d602
DJ
6201/* Return the fully qualified name of DIE, based on its DW_AT_name.
6202 If scope qualifiers are appropriate they will be added. The result
6203 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
6204 not have a name. NAME may either be from a previous call to
6205 dwarf2_name or NULL.
6206
0963b4bd 6207 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
6208
6209static const char *
94af9270 6210dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 6211{
94af9270
KS
6212 return dwarf2_compute_name (name, die, cu, 0);
6213}
0114d602 6214
94af9270
KS
6215/* Construct a physname for the given DIE in CU. NAME may either be
6216 from a previous call to dwarf2_name or NULL. The result will be
6217 allocated on the objfile_objstack or NULL if the DIE does not have a
6218 name.
0114d602 6219
94af9270 6220 The output string will be canonicalized (if C++/Java). */
0114d602 6221
94af9270
KS
6222static const char *
6223dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6224{
bb5ed363 6225 struct objfile *objfile = cu->objfile;
900e11f9
JK
6226 struct attribute *attr;
6227 const char *retval, *mangled = NULL, *canon = NULL;
6228 struct cleanup *back_to;
6229 int need_copy = 1;
6230
6231 /* In this case dwarf2_compute_name is just a shortcut not building anything
6232 on its own. */
6233 if (!die_needs_namespace (die, cu))
6234 return dwarf2_compute_name (name, die, cu, 1);
6235
6236 back_to = make_cleanup (null_cleanup, NULL);
6237
6238 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6239 if (!attr)
6240 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6241
6242 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6243 has computed. */
6244 if (attr && DW_STRING (attr))
6245 {
6246 char *demangled;
6247
6248 mangled = DW_STRING (attr);
6249
6250 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6251 type. It is easier for GDB users to search for such functions as
6252 `name(params)' than `long name(params)'. In such case the minimal
6253 symbol names do not match the full symbol names but for template
6254 functions there is never a need to look up their definition from their
6255 declaration so the only disadvantage remains the minimal symbol
6256 variant `long name(params)' does not have the proper inferior type.
6257 */
6258
a766d390
DE
6259 if (cu->language == language_go)
6260 {
6261 /* This is a lie, but we already lie to the caller new_symbol_full.
6262 new_symbol_full assumes we return the mangled name.
6263 This just undoes that lie until things are cleaned up. */
6264 demangled = NULL;
6265 }
6266 else
6267 {
6268 demangled = cplus_demangle (mangled,
6269 (DMGL_PARAMS | DMGL_ANSI
6270 | (cu->language == language_java
6271 ? DMGL_JAVA | DMGL_RET_POSTFIX
6272 : DMGL_RET_DROP)));
6273 }
900e11f9
JK
6274 if (demangled)
6275 {
6276 make_cleanup (xfree, demangled);
6277 canon = demangled;
6278 }
6279 else
6280 {
6281 canon = mangled;
6282 need_copy = 0;
6283 }
6284 }
6285
6286 if (canon == NULL || check_physname)
6287 {
6288 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6289
6290 if (canon != NULL && strcmp (physname, canon) != 0)
6291 {
6292 /* It may not mean a bug in GDB. The compiler could also
6293 compute DW_AT_linkage_name incorrectly. But in such case
6294 GDB would need to be bug-to-bug compatible. */
6295
6296 complaint (&symfile_complaints,
6297 _("Computed physname <%s> does not match demangled <%s> "
6298 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 6299 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
6300
6301 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6302 is available here - over computed PHYSNAME. It is safer
6303 against both buggy GDB and buggy compilers. */
6304
6305 retval = canon;
6306 }
6307 else
6308 {
6309 retval = physname;
6310 need_copy = 0;
6311 }
6312 }
6313 else
6314 retval = canon;
6315
6316 if (need_copy)
6317 retval = obsavestring (retval, strlen (retval),
bb5ed363 6318 &objfile->objfile_obstack);
900e11f9
JK
6319
6320 do_cleanups (back_to);
6321 return retval;
0114d602
DJ
6322}
6323
27aa8d6a
SW
6324/* Read the import statement specified by the given die and record it. */
6325
6326static void
6327read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6328{
bb5ed363 6329 struct objfile *objfile = cu->objfile;
27aa8d6a 6330 struct attribute *import_attr;
32019081 6331 struct die_info *imported_die, *child_die;
de4affc9 6332 struct dwarf2_cu *imported_cu;
27aa8d6a 6333 const char *imported_name;
794684b6 6334 const char *imported_name_prefix;
13387711
SW
6335 const char *canonical_name;
6336 const char *import_alias;
6337 const char *imported_declaration = NULL;
794684b6 6338 const char *import_prefix;
32019081
JK
6339 VEC (const_char_ptr) *excludes = NULL;
6340 struct cleanup *cleanups;
13387711
SW
6341
6342 char *temp;
27aa8d6a
SW
6343
6344 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6345 if (import_attr == NULL)
6346 {
6347 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6348 dwarf_tag_name (die->tag));
6349 return;
6350 }
6351
de4affc9
CC
6352 imported_cu = cu;
6353 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6354 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
6355 if (imported_name == NULL)
6356 {
6357 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6358
6359 The import in the following code:
6360 namespace A
6361 {
6362 typedef int B;
6363 }
6364
6365 int main ()
6366 {
6367 using A::B;
6368 B b;
6369 return b;
6370 }
6371
6372 ...
6373 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6374 <52> DW_AT_decl_file : 1
6375 <53> DW_AT_decl_line : 6
6376 <54> DW_AT_import : <0x75>
6377 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6378 <59> DW_AT_name : B
6379 <5b> DW_AT_decl_file : 1
6380 <5c> DW_AT_decl_line : 2
6381 <5d> DW_AT_type : <0x6e>
6382 ...
6383 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6384 <76> DW_AT_byte_size : 4
6385 <77> DW_AT_encoding : 5 (signed)
6386
6387 imports the wrong die ( 0x75 instead of 0x58 ).
6388 This case will be ignored until the gcc bug is fixed. */
6389 return;
6390 }
6391
82856980
SW
6392 /* Figure out the local name after import. */
6393 import_alias = dwarf2_name (die, cu);
27aa8d6a 6394
794684b6
SW
6395 /* Figure out where the statement is being imported to. */
6396 import_prefix = determine_prefix (die, cu);
6397
6398 /* Figure out what the scope of the imported die is and prepend it
6399 to the name of the imported die. */
de4affc9 6400 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 6401
f55ee35c
JK
6402 if (imported_die->tag != DW_TAG_namespace
6403 && imported_die->tag != DW_TAG_module)
794684b6 6404 {
13387711
SW
6405 imported_declaration = imported_name;
6406 canonical_name = imported_name_prefix;
794684b6 6407 }
13387711 6408 else if (strlen (imported_name_prefix) > 0)
794684b6 6409 {
13387711
SW
6410 temp = alloca (strlen (imported_name_prefix)
6411 + 2 + strlen (imported_name) + 1);
6412 strcpy (temp, imported_name_prefix);
6413 strcat (temp, "::");
6414 strcat (temp, imported_name);
6415 canonical_name = temp;
794684b6 6416 }
13387711
SW
6417 else
6418 canonical_name = imported_name;
794684b6 6419
32019081
JK
6420 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6421
6422 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6423 for (child_die = die->child; child_die && child_die->tag;
6424 child_die = sibling_die (child_die))
6425 {
6426 /* DWARF-4: A Fortran use statement with a “rename list” may be
6427 represented by an imported module entry with an import attribute
6428 referring to the module and owned entries corresponding to those
6429 entities that are renamed as part of being imported. */
6430
6431 if (child_die->tag != DW_TAG_imported_declaration)
6432 {
6433 complaint (&symfile_complaints,
6434 _("child DW_TAG_imported_declaration expected "
6435 "- DIE at 0x%x [in module %s]"),
b64f50a1 6436 child_die->offset.sect_off, objfile->name);
32019081
JK
6437 continue;
6438 }
6439
6440 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6441 if (import_attr == NULL)
6442 {
6443 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6444 dwarf_tag_name (child_die->tag));
6445 continue;
6446 }
6447
6448 imported_cu = cu;
6449 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6450 &imported_cu);
6451 imported_name = dwarf2_name (imported_die, imported_cu);
6452 if (imported_name == NULL)
6453 {
6454 complaint (&symfile_complaints,
6455 _("child DW_TAG_imported_declaration has unknown "
6456 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 6457 child_die->offset.sect_off, objfile->name);
32019081
JK
6458 continue;
6459 }
6460
6461 VEC_safe_push (const_char_ptr, excludes, imported_name);
6462
6463 process_die (child_die, cu);
6464 }
6465
c0cc3a76
SW
6466 cp_add_using_directive (import_prefix,
6467 canonical_name,
6468 import_alias,
13387711 6469 imported_declaration,
32019081 6470 excludes,
bb5ed363 6471 &objfile->objfile_obstack);
32019081
JK
6472
6473 do_cleanups (cleanups);
27aa8d6a
SW
6474}
6475
ae2de4f8
DE
6476/* Cleanup function for read_file_scope. */
6477
cb1df416
DJ
6478static void
6479free_cu_line_header (void *arg)
6480{
6481 struct dwarf2_cu *cu = arg;
6482
6483 free_line_header (cu->line_header);
6484 cu->line_header = NULL;
6485}
6486
9291a0cd
TT
6487static void
6488find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6489 char **name, char **comp_dir)
6490{
6491 struct attribute *attr;
6492
6493 *name = NULL;
6494 *comp_dir = NULL;
6495
6496 /* Find the filename. Do not use dwarf2_name here, since the filename
6497 is not a source language identifier. */
6498 attr = dwarf2_attr (die, DW_AT_name, cu);
6499 if (attr)
6500 {
6501 *name = DW_STRING (attr);
6502 }
6503
6504 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6505 if (attr)
6506 *comp_dir = DW_STRING (attr);
6507 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6508 {
6509 *comp_dir = ldirname (*name);
6510 if (*comp_dir != NULL)
6511 make_cleanup (xfree, *comp_dir);
6512 }
6513 if (*comp_dir != NULL)
6514 {
6515 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6516 directory, get rid of it. */
6517 char *cp = strchr (*comp_dir, ':');
6518
6519 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6520 *comp_dir = cp + 1;
6521 }
6522
6523 if (*name == NULL)
6524 *name = "<unknown>";
6525}
6526
f3f5162e
DE
6527/* Handle DW_AT_stmt_list for a compilation unit or type unit.
6528 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6529 COMP_DIR is the compilation directory.
6530 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
6531
6532static void
6533handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 6534 const char *comp_dir, int want_line_info)
2ab95328
TT
6535{
6536 struct attribute *attr;
2ab95328 6537
2ab95328
TT
6538 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6539 if (attr)
6540 {
6541 unsigned int line_offset = DW_UNSND (attr);
6542 struct line_header *line_header
3019eac3 6543 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
6544
6545 if (line_header)
dee91e82
DE
6546 {
6547 cu->line_header = line_header;
6548 make_cleanup (free_cu_line_header, cu);
f3f5162e 6549 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
dee91e82 6550 }
2ab95328
TT
6551 }
6552}
6553
95554aad 6554/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 6555
c906108c 6556static void
e7c27a73 6557read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6558{
dee91e82 6559 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 6560 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 6561 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
6562 CORE_ADDR highpc = ((CORE_ADDR) 0);
6563 struct attribute *attr;
e1024ff1 6564 char *name = NULL;
c906108c
SS
6565 char *comp_dir = NULL;
6566 struct die_info *child_die;
6567 bfd *abfd = objfile->obfd;
e142c38c 6568 CORE_ADDR baseaddr;
6e70227d 6569
e142c38c 6570 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6571
fae299cd 6572 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
6573
6574 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6575 from finish_block. */
2acceee2 6576 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
6577 lowpc = highpc;
6578 lowpc += baseaddr;
6579 highpc += baseaddr;
6580
9291a0cd 6581 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 6582
95554aad 6583 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 6584
f4b8a18d
KW
6585 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6586 standardised yet. As a workaround for the language detection we fall
6587 back to the DW_AT_producer string. */
6588 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6589 cu->language = language_opencl;
6590
3019eac3
DE
6591 /* Similar hack for Go. */
6592 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6593 set_cu_language (DW_LANG_Go, cu);
6594
6595 /* We assume that we're processing GCC output. */
6596 processing_gcc_compilation = 2;
6597
6598 processing_has_namespace_info = 0;
6599
6600 start_symtab (name, comp_dir, lowpc);
6601 record_debugformat ("DWARF 2");
6602 record_producer (cu->producer);
6603
6604 /* Decode line number information if present. We do this before
6605 processing child DIEs, so that the line header table is available
6606 for DW_AT_decl_file. */
6607 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6608
6609 /* Process all dies in compilation unit. */
6610 if (die->child != NULL)
6611 {
6612 child_die = die->child;
6613 while (child_die && child_die->tag)
6614 {
6615 process_die (child_die, cu);
6616 child_die = sibling_die (child_die);
6617 }
6618 }
6619
6620 /* Decode macro information, if present. Dwarf 2 macro information
6621 refers to information in the line number info statement program
6622 header, so we can only read it if we've read the header
6623 successfully. */
6624 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6625 if (attr && cu->line_header)
6626 {
6627 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6628 complaint (&symfile_complaints,
6629 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6630
6631 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
6632 comp_dir, abfd, cu,
fceca515
DE
6633 &dwarf2_per_objfile->macro, 1,
6634 ".debug_macro");
3019eac3
DE
6635 }
6636 else
6637 {
6638 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6639 if (attr && cu->line_header)
6640 {
6641 unsigned int macro_offset = DW_UNSND (attr);
6642
6643 dwarf_decode_macros (cu->line_header, macro_offset,
6644 comp_dir, abfd, cu,
fceca515
DE
6645 &dwarf2_per_objfile->macinfo, 0,
6646 ".debug_macinfo");
3019eac3
DE
6647 }
6648 }
6649
6650 do_cleanups (back_to);
6651}
6652
6653/* Process DW_TAG_type_unit.
6654 For TUs we want to skip the first top level sibling if it's not the
6655 actual type being defined by this TU. In this case the first top
6656 level sibling is there to provide context only. */
6657
6658static void
6659read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6660{
6661 struct objfile *objfile = cu->objfile;
6662 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6663 CORE_ADDR lowpc;
6664 struct attribute *attr;
6665 char *name = NULL;
6666 char *comp_dir = NULL;
6667 struct die_info *child_die;
6668 bfd *abfd = objfile->obfd;
6669
6670 /* start_symtab needs a low pc, but we don't really have one.
6671 Do what read_file_scope would do in the absence of such info. */
6672 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6673
6674 /* Find the filename. Do not use dwarf2_name here, since the filename
6675 is not a source language identifier. */
6676 attr = dwarf2_attr (die, DW_AT_name, cu);
6677 if (attr)
6678 name = DW_STRING (attr);
6679
6680 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6681 if (attr)
6682 comp_dir = DW_STRING (attr);
6683 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6684 {
6685 comp_dir = ldirname (name);
6686 if (comp_dir != NULL)
6687 make_cleanup (xfree, comp_dir);
6688 }
6689
6690 if (name == NULL)
6691 name = "<unknown>";
6692
95554aad 6693 prepare_one_comp_unit (cu, die, language_minimal);
3019eac3
DE
6694
6695 /* We assume that we're processing GCC output. */
6696 processing_gcc_compilation = 2;
6697
6698 processing_has_namespace_info = 0;
6699
6700 start_symtab (name, comp_dir, lowpc);
6701 record_debugformat ("DWARF 2");
6702 record_producer (cu->producer);
6703
6704 /* Decode line number information if present. We do this before
6705 processing child DIEs, so that the line header table is available
6706 for DW_AT_decl_file.
6707 We don't need the pc/line-number mapping for type units. */
6708 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6709
6710 /* Process the dies in the type unit. */
6711 if (die->child == NULL)
6712 {
6713 dump_die_for_error (die);
6714 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6715 bfd_get_filename (abfd));
6716 }
6717
6718 child_die = die->child;
6719
6720 while (child_die && child_die->tag)
6721 {
6722 process_die (child_die, cu);
6723
6724 child_die = sibling_die (child_die);
6725 }
6726
6727 do_cleanups (back_to);
6728}
6729\f
6730/* DWO files. */
6731
6732static hashval_t
6733hash_dwo_file (const void *item)
6734{
6735 const struct dwo_file *dwo_file = item;
6736
6737 return htab_hash_string (dwo_file->dwo_name);
6738}
6739
6740static int
6741eq_dwo_file (const void *item_lhs, const void *item_rhs)
6742{
6743 const struct dwo_file *lhs = item_lhs;
6744 const struct dwo_file *rhs = item_rhs;
6745
6746 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6747}
6748
6749/* Allocate a hash table for DWO files. */
6750
6751static htab_t
6752allocate_dwo_file_hash_table (void)
6753{
6754 struct objfile *objfile = dwarf2_per_objfile->objfile;
6755
6756 return htab_create_alloc_ex (41,
6757 hash_dwo_file,
6758 eq_dwo_file,
6759 NULL,
6760 &objfile->objfile_obstack,
6761 hashtab_obstack_allocate,
6762 dummy_obstack_deallocate);
6763}
6764
6765static hashval_t
6766hash_dwo_unit (const void *item)
6767{
6768 const struct dwo_unit *dwo_unit = item;
6769
6770 /* This drops the top 32 bits of the id, but is ok for a hash. */
6771 return dwo_unit->signature;
6772}
6773
6774static int
6775eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6776{
6777 const struct dwo_unit *lhs = item_lhs;
6778 const struct dwo_unit *rhs = item_rhs;
6779
6780 /* The signature is assumed to be unique within the DWO file.
6781 So while object file CU dwo_id's always have the value zero,
6782 that's OK, assuming each object file DWO file has only one CU,
6783 and that's the rule for now. */
6784 return lhs->signature == rhs->signature;
6785}
6786
6787/* Allocate a hash table for DWO CUs,TUs.
6788 There is one of these tables for each of CUs,TUs for each DWO file. */
6789
6790static htab_t
6791allocate_dwo_unit_table (struct objfile *objfile)
6792{
6793 /* Start out with a pretty small number.
6794 Generally DWO files contain only one CU and maybe some TUs. */
6795 return htab_create_alloc_ex (3,
6796 hash_dwo_unit,
6797 eq_dwo_unit,
6798 NULL,
6799 &objfile->objfile_obstack,
6800 hashtab_obstack_allocate,
6801 dummy_obstack_deallocate);
6802}
6803
6804/* This function is mapped across the sections and remembers the offset and
6805 size of each of the DWO debugging sections we are interested in. */
6806
6807static void
6808dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6809{
6810 struct dwo_file *dwo_file = dwo_file_ptr;
6811 const struct dwo_section_names *names = &dwo_section_names;
6812
6813 if (section_is_p (sectp->name, &names->abbrev_dwo))
6814 {
6815 dwo_file->sections.abbrev.asection = sectp;
6816 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6817 }
6818 else if (section_is_p (sectp->name, &names->info_dwo))
6819 {
6820 dwo_file->sections.info.asection = sectp;
6821 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6822 }
6823 else if (section_is_p (sectp->name, &names->line_dwo))
6824 {
6825 dwo_file->sections.line.asection = sectp;
6826 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6827 }
6828 else if (section_is_p (sectp->name, &names->loc_dwo))
6829 {
6830 dwo_file->sections.loc.asection = sectp;
6831 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6832 }
6833 else if (section_is_p (sectp->name, &names->str_dwo))
6834 {
6835 dwo_file->sections.str.asection = sectp;
6836 dwo_file->sections.str.size = bfd_get_section_size (sectp);
6837 }
6838 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
6839 {
6840 dwo_file->sections.str_offsets.asection = sectp;
6841 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
6842 }
6843 else if (section_is_p (sectp->name, &names->types_dwo))
6844 {
6845 struct dwarf2_section_info type_section;
6846
6847 memset (&type_section, 0, sizeof (type_section));
6848 type_section.asection = sectp;
6849 type_section.size = bfd_get_section_size (sectp);
6850 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
6851 &type_section);
6852 }
6853}
6854
6855/* Structure used to pass data to create_debug_info_hash_table_reader. */
6856
6857struct create_dwo_info_table_data
6858{
6859 struct dwo_file *dwo_file;
6860 htab_t cu_htab;
6861};
6862
6863/* die_reader_func for create_debug_info_hash_table. */
6864
6865static void
6866create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
6867 gdb_byte *info_ptr,
6868 struct die_info *comp_unit_die,
6869 int has_children,
6870 void *datap)
6871{
6872 struct dwarf2_cu *cu = reader->cu;
6873 struct objfile *objfile = dwarf2_per_objfile->objfile;
6874 sect_offset offset = cu->per_cu->offset;
6875 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
6876 struct create_dwo_info_table_data *data = datap;
6877 struct dwo_file *dwo_file = data->dwo_file;
6878 htab_t cu_htab = data->cu_htab;
6879 void **slot;
6880 struct attribute *attr;
6881 struct dwo_unit *dwo_unit;
6882
6883 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6884 if (attr == NULL)
6885 {
6886 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
6887 " its dwo_id [in module %s]"),
6888 offset.sect_off, dwo_file->dwo_name);
6889 return;
6890 }
6891
6892 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
6893 dwo_unit->dwo_file = dwo_file;
6894 dwo_unit->signature = DW_UNSND (attr);
6895 dwo_unit->info_or_types_section = section;
6896 dwo_unit->offset = offset;
6897 dwo_unit->length = cu->per_cu->length;
6898
6899 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
6900 gdb_assert (slot != NULL);
6901 if (*slot != NULL)
6902 {
6903 const struct dwo_unit *dup_dwo_unit = *slot;
6904
6905 complaint (&symfile_complaints,
6906 _("debug entry at offset 0x%x is duplicate to the entry at"
6907 " offset 0x%x, dwo_id 0x%s [in module %s]"),
6908 offset.sect_off, dup_dwo_unit->offset.sect_off,
6909 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
6910 dwo_file->dwo_name);
6911 }
6912 else
6913 *slot = dwo_unit;
6914
6915 if (dwarf2_die_debug)
6916 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
6917 offset.sect_off,
6918 phex (dwo_unit->signature,
6919 sizeof (dwo_unit->signature)));
6920}
6921
6922/* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
6923
6924static htab_t
6925create_debug_info_hash_table (struct dwo_file *dwo_file)
6926{
6927 struct objfile *objfile = dwarf2_per_objfile->objfile;
6928 struct dwarf2_section_info *section = &dwo_file->sections.info;
6929 bfd *abfd;
6930 htab_t cu_htab;
6931 gdb_byte *info_ptr, *end_ptr;
6932 struct create_dwo_info_table_data create_dwo_info_table_data;
6933
6934 dwarf2_read_section (objfile, section);
6935 info_ptr = section->buffer;
6936
6937 if (info_ptr == NULL)
6938 return NULL;
6939
6940 /* We can't set abfd until now because the section may be empty or
6941 not present, in which case section->asection will be NULL. */
6942 abfd = section->asection->owner;
6943
6944 if (dwarf2_die_debug)
6945 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
6946 bfd_get_filename (abfd));
6947
6948 cu_htab = allocate_dwo_unit_table (objfile);
6949
6950 create_dwo_info_table_data.dwo_file = dwo_file;
6951 create_dwo_info_table_data.cu_htab = cu_htab;
6952
6953 end_ptr = info_ptr + section->size;
6954 while (info_ptr < end_ptr)
6955 {
6956 struct dwarf2_per_cu_data per_cu;
6957
6958 memset (&per_cu, 0, sizeof (per_cu));
6959 per_cu.objfile = objfile;
6960 per_cu.is_debug_types = 0;
6961 per_cu.offset.sect_off = info_ptr - section->buffer;
6962 per_cu.info_or_types_section = section;
6963
6964 init_cutu_and_read_dies_no_follow (&per_cu,
6965 &dwo_file->sections.abbrev,
6966 dwo_file,
6967 create_debug_info_hash_table_reader,
6968 &create_dwo_info_table_data);
6969
6970 info_ptr += per_cu.length;
6971 }
6972
6973 return cu_htab;
6974}
6975
6976/* Subroutine of open_dwo_file to simplify it.
6977 Open the file specified by FILE_NAME and hand it off to BFD for
6978 preliminary analysis. Return a newly initialized bfd *, which
6979 includes a canonicalized copy of FILE_NAME.
6980 In case of trouble, return NULL.
6981 NOTE: This function is derived from symfile_bfd_open. */
6982
6983static bfd *
6984try_open_dwo_file (const char *file_name)
6985{
6986 bfd *sym_bfd;
6987 int desc;
6988 char *absolute_name;
3019eac3
DE
6989
6990 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
6991 O_RDONLY | O_BINARY, &absolute_name);
6992 if (desc < 0)
6993 return NULL;
6994
6995 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
6996 if (!sym_bfd)
6997 {
3019eac3
DE
6998 xfree (absolute_name);
6999 return NULL;
7000 }
7001 bfd_set_cacheable (sym_bfd, 1);
7002
7003 if (!bfd_check_format (sym_bfd, bfd_object))
7004 {
7005 bfd_close (sym_bfd); /* This also closes desc. */
7006 xfree (absolute_name);
7007 return NULL;
7008 }
7009
7010 /* bfd_usrdata exists for applications and libbfd must not touch it. */
7011 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
7012
7013 return sym_bfd;
7014}
7015
7016/* Try to open DWO file DWO_NAME.
7017 COMP_DIR is the DW_AT_comp_dir attribute.
7018 The result is the bfd handle of the file.
7019 If there is a problem finding or opening the file, return NULL.
7020 Upon success, the canonicalized path of the file is stored in the bfd,
7021 same as symfile_bfd_open. */
7022
7023static bfd *
7024open_dwo_file (const char *dwo_name, const char *comp_dir)
7025{
7026 bfd *abfd;
3019eac3
DE
7027
7028 if (IS_ABSOLUTE_PATH (dwo_name))
7029 return try_open_dwo_file (dwo_name);
7030
7031 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
7032
7033 if (comp_dir != NULL)
7034 {
7035 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
7036
7037 /* NOTE: If comp_dir is a relative path, this will also try the
7038 search path, which seems useful. */
7039 abfd = try_open_dwo_file (path_to_try);
7040 xfree (path_to_try);
7041 if (abfd != NULL)
7042 return abfd;
7043 }
7044
7045 /* That didn't work, try debug-file-directory, which, despite its name,
7046 is a list of paths. */
7047
7048 if (*debug_file_directory == '\0')
7049 return NULL;
7050
7051 return try_open_dwo_file (dwo_name);
7052}
7053
7054/* Initialize the use of the DWO file specified by DWO_NAME. */
7055
7056static struct dwo_file *
7057init_dwo_file (const char *dwo_name, const char *comp_dir)
7058{
7059 struct objfile *objfile = dwarf2_per_objfile->objfile;
7060 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7061 struct dwo_file);
7062 bfd *abfd;
7063 struct cleanup *cleanups;
7064
7065 if (dwarf2_die_debug)
7066 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7067
7068 abfd = open_dwo_file (dwo_name, comp_dir);
7069 if (abfd == NULL)
7070 return NULL;
7071 dwo_file->dwo_name = dwo_name;
7072 dwo_file->dwo_bfd = abfd;
7073
7074 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7075
7076 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7077
7078 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7079
7080 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7081 dwo_file->sections.types);
7082
7083 discard_cleanups (cleanups);
7084
7085 return dwo_file;
7086}
7087
7088/* Lookup DWO file DWO_NAME. */
7089
7090static struct dwo_file *
7091lookup_dwo_file (char *dwo_name, const char *comp_dir)
7092{
7093 struct dwo_file *dwo_file;
7094 struct dwo_file find_entry;
7095 void **slot;
7096
7097 if (dwarf2_per_objfile->dwo_files == NULL)
7098 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7099
7100 /* Have we already seen this DWO file? */
7101 find_entry.dwo_name = dwo_name;
7102 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7103
7104 /* If not, read it in and build a table of the DWOs it contains. */
7105 if (*slot == NULL)
7106 *slot = init_dwo_file (dwo_name, comp_dir);
7107
7108 /* NOTE: This will be NULL if unable to open the file. */
7109 dwo_file = *slot;
7110
7111 return dwo_file;
7112}
7113
7114/* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7115 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7116 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7117 nomenclature as TUs).
7118 The result is the DWO CU or NULL if we didn't find it
7119 (dwo_id mismatch or couldn't find the DWO file). */
7120
7121static struct dwo_unit *
7122lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7123 char *dwo_name, const char *comp_dir,
7124 ULONGEST signature)
7125{
7126 struct objfile *objfile = dwarf2_per_objfile->objfile;
7127 struct dwo_file *dwo_file;
7128
7129 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7130 if (dwo_file == NULL)
7131 return NULL;
7132
7133 /* Look up the DWO using its signature(dwo_id). */
7134
7135 if (dwo_file->cus != NULL)
7136 {
7137 struct dwo_unit find_dwo_cu, *dwo_cu;
7138
7139 find_dwo_cu.signature = signature;
7140 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
a766d390 7141
3019eac3
DE
7142 if (dwo_cu != NULL)
7143 return dwo_cu;
7144 }
c906108c 7145
3019eac3 7146 /* We didn't find it. This must mean a dwo_id mismatch. */
df8a16a1 7147
3019eac3
DE
7148 complaint (&symfile_complaints,
7149 _("Could not find DWO CU referenced by CU at offset 0x%x"
7150 " [in module %s]"),
7151 this_cu->offset.sect_off, objfile->name);
7152 return NULL;
7153}
c906108c 7154
3019eac3
DE
7155/* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7156 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7157 The result is the DWO CU or NULL if we didn't find it
7158 (dwo_id mismatch or couldn't find the DWO file). */
debd256d 7159
3019eac3
DE
7160static struct dwo_unit *
7161lookup_dwo_type_unit (struct signatured_type *this_tu,
7162 char *dwo_name, const char *comp_dir)
7163{
7164 struct objfile *objfile = dwarf2_per_objfile->objfile;
7165 struct dwo_file *dwo_file;
cb1df416 7166
3019eac3
DE
7167 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7168 if (dwo_file == NULL)
7169 return NULL;
cf2c3c16 7170
3019eac3
DE
7171 /* Look up the DWO using its signature(dwo_id). */
7172
7173 if (dwo_file->tus != NULL)
cf2c3c16 7174 {
3019eac3 7175 struct dwo_unit find_dwo_tu, *dwo_tu;
9a619af0 7176
3019eac3
DE
7177 find_dwo_tu.signature = this_tu->signature;
7178 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7179
7180 if (dwo_tu != NULL)
7181 return dwo_tu;
2e276125 7182 }
9cdd5dbd 7183
3019eac3
DE
7184 /* We didn't find it. This must mean a dwo_id mismatch. */
7185
7186 complaint (&symfile_complaints,
7187 _("Could not find DWO TU referenced by TU at offset 0x%x"
7188 " [in module %s]"),
7189 this_tu->per_cu.offset.sect_off, objfile->name);
7190 return NULL;
5fb290d7
DJ
7191}
7192
3019eac3
DE
7193/* Free all resources associated with DWO_FILE.
7194 Close the DWO file and munmap the sections.
7195 All memory should be on the objfile obstack. */
348e048f
DE
7196
7197static void
3019eac3 7198free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 7199{
3019eac3
DE
7200 int ix;
7201 struct dwarf2_section_info *section;
348e048f 7202
3019eac3
DE
7203 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7204 bfd_close (dwo_file->dwo_bfd);
348e048f 7205
3019eac3
DE
7206 munmap_section_buffer (&dwo_file->sections.abbrev);
7207 munmap_section_buffer (&dwo_file->sections.info);
7208 munmap_section_buffer (&dwo_file->sections.line);
7209 munmap_section_buffer (&dwo_file->sections.loc);
7210 munmap_section_buffer (&dwo_file->sections.str);
7211 munmap_section_buffer (&dwo_file->sections.str_offsets);
348e048f 7212
3019eac3
DE
7213 for (ix = 0;
7214 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7215 ix, section);
7216 ++ix)
7217 munmap_section_buffer (section);
348e048f 7218
3019eac3
DE
7219 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7220}
348e048f 7221
3019eac3 7222/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 7223
3019eac3
DE
7224static void
7225free_dwo_file_cleanup (void *arg)
7226{
7227 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7228 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 7229
3019eac3
DE
7230 free_dwo_file (dwo_file, objfile);
7231}
348e048f 7232
3019eac3 7233/* Traversal function for free_dwo_files. */
2ab95328 7234
3019eac3
DE
7235static int
7236free_dwo_file_from_slot (void **slot, void *info)
7237{
7238 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7239 struct objfile *objfile = (struct objfile *) info;
348e048f 7240
3019eac3 7241 free_dwo_file (dwo_file, objfile);
348e048f 7242
3019eac3
DE
7243 return 1;
7244}
348e048f 7245
3019eac3 7246/* Free all resources associated with DWO_FILES. */
348e048f 7247
3019eac3
DE
7248static void
7249free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7250{
7251 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 7252}
3019eac3
DE
7253\f
7254/* Read in various DIEs. */
348e048f 7255
d389af10
JK
7256/* qsort helper for inherit_abstract_dies. */
7257
7258static int
7259unsigned_int_compar (const void *ap, const void *bp)
7260{
7261 unsigned int a = *(unsigned int *) ap;
7262 unsigned int b = *(unsigned int *) bp;
7263
7264 return (a > b) - (b > a);
7265}
7266
7267/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
7268 Inherit only the children of the DW_AT_abstract_origin DIE not being
7269 already referenced by DW_AT_abstract_origin from the children of the
7270 current DIE. */
d389af10
JK
7271
7272static void
7273inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7274{
7275 struct die_info *child_die;
7276 unsigned die_children_count;
7277 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
7278 sect_offset *offsets;
7279 sect_offset *offsets_end, *offsetp;
d389af10
JK
7280 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7281 struct die_info *origin_die;
7282 /* Iterator of the ORIGIN_DIE children. */
7283 struct die_info *origin_child_die;
7284 struct cleanup *cleanups;
7285 struct attribute *attr;
cd02d79d
PA
7286 struct dwarf2_cu *origin_cu;
7287 struct pending **origin_previous_list_in_scope;
d389af10
JK
7288
7289 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7290 if (!attr)
7291 return;
7292
cd02d79d
PA
7293 /* Note that following die references may follow to a die in a
7294 different cu. */
7295
7296 origin_cu = cu;
7297 origin_die = follow_die_ref (die, attr, &origin_cu);
7298
7299 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7300 symbols in. */
7301 origin_previous_list_in_scope = origin_cu->list_in_scope;
7302 origin_cu->list_in_scope = cu->list_in_scope;
7303
edb3359d
DJ
7304 if (die->tag != origin_die->tag
7305 && !(die->tag == DW_TAG_inlined_subroutine
7306 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7307 complaint (&symfile_complaints,
7308 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 7309 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
7310
7311 child_die = die->child;
7312 die_children_count = 0;
7313 while (child_die && child_die->tag)
7314 {
7315 child_die = sibling_die (child_die);
7316 die_children_count++;
7317 }
7318 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7319 cleanups = make_cleanup (xfree, offsets);
7320
7321 offsets_end = offsets;
7322 child_die = die->child;
7323 while (child_die && child_die->tag)
7324 {
c38f313d
DJ
7325 /* For each CHILD_DIE, find the corresponding child of
7326 ORIGIN_DIE. If there is more than one layer of
7327 DW_AT_abstract_origin, follow them all; there shouldn't be,
7328 but GCC versions at least through 4.4 generate this (GCC PR
7329 40573). */
7330 struct die_info *child_origin_die = child_die;
cd02d79d 7331 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 7332
c38f313d
DJ
7333 while (1)
7334 {
cd02d79d
PA
7335 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7336 child_origin_cu);
c38f313d
DJ
7337 if (attr == NULL)
7338 break;
cd02d79d
PA
7339 child_origin_die = follow_die_ref (child_origin_die, attr,
7340 &child_origin_cu);
c38f313d
DJ
7341 }
7342
d389af10
JK
7343 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7344 counterpart may exist. */
c38f313d 7345 if (child_origin_die != child_die)
d389af10 7346 {
edb3359d
DJ
7347 if (child_die->tag != child_origin_die->tag
7348 && !(child_die->tag == DW_TAG_inlined_subroutine
7349 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7350 complaint (&symfile_complaints,
7351 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7352 "different tags"), child_die->offset.sect_off,
7353 child_origin_die->offset.sect_off);
c38f313d
DJ
7354 if (child_origin_die->parent != origin_die)
7355 complaint (&symfile_complaints,
7356 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7357 "different parents"), child_die->offset.sect_off,
7358 child_origin_die->offset.sect_off);
c38f313d
DJ
7359 else
7360 *offsets_end++ = child_origin_die->offset;
d389af10
JK
7361 }
7362 child_die = sibling_die (child_die);
7363 }
7364 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7365 unsigned_int_compar);
7366 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 7367 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
7368 complaint (&symfile_complaints,
7369 _("Multiple children of DIE 0x%x refer "
7370 "to DIE 0x%x as their abstract origin"),
b64f50a1 7371 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
7372
7373 offsetp = offsets;
7374 origin_child_die = origin_die->child;
7375 while (origin_child_die && origin_child_die->tag)
7376 {
7377 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
7378 while (offsetp < offsets_end
7379 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 7380 offsetp++;
b64f50a1
JK
7381 if (offsetp >= offsets_end
7382 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
7383 {
7384 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 7385 process_die (origin_child_die, origin_cu);
d389af10
JK
7386 }
7387 origin_child_die = sibling_die (origin_child_die);
7388 }
cd02d79d 7389 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
7390
7391 do_cleanups (cleanups);
7392}
7393
c906108c 7394static void
e7c27a73 7395read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7396{
e7c27a73 7397 struct objfile *objfile = cu->objfile;
52f0bd74 7398 struct context_stack *new;
c906108c
SS
7399 CORE_ADDR lowpc;
7400 CORE_ADDR highpc;
7401 struct die_info *child_die;
edb3359d 7402 struct attribute *attr, *call_line, *call_file;
c906108c 7403 char *name;
e142c38c 7404 CORE_ADDR baseaddr;
801e3a5b 7405 struct block *block;
edb3359d 7406 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
7407 VEC (symbolp) *template_args = NULL;
7408 struct template_symbol *templ_func = NULL;
edb3359d
DJ
7409
7410 if (inlined_func)
7411 {
7412 /* If we do not have call site information, we can't show the
7413 caller of this inlined function. That's too confusing, so
7414 only use the scope for local variables. */
7415 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7416 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7417 if (call_line == NULL || call_file == NULL)
7418 {
7419 read_lexical_block_scope (die, cu);
7420 return;
7421 }
7422 }
c906108c 7423
e142c38c
DJ
7424 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7425
94af9270 7426 name = dwarf2_name (die, cu);
c906108c 7427
e8d05480
JB
7428 /* Ignore functions with missing or empty names. These are actually
7429 illegal according to the DWARF standard. */
7430 if (name == NULL)
7431 {
7432 complaint (&symfile_complaints,
b64f50a1
JK
7433 _("missing name for subprogram DIE at %d"),
7434 die->offset.sect_off);
e8d05480
JB
7435 return;
7436 }
7437
7438 /* Ignore functions with missing or invalid low and high pc attributes. */
7439 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7440 {
ae4d0c03
PM
7441 attr = dwarf2_attr (die, DW_AT_external, cu);
7442 if (!attr || !DW_UNSND (attr))
7443 complaint (&symfile_complaints,
3e43a32a
MS
7444 _("cannot get low and high bounds "
7445 "for subprogram DIE at %d"),
b64f50a1 7446 die->offset.sect_off);
e8d05480
JB
7447 return;
7448 }
c906108c
SS
7449
7450 lowpc += baseaddr;
7451 highpc += baseaddr;
7452
34eaf542
TT
7453 /* If we have any template arguments, then we must allocate a
7454 different sort of symbol. */
7455 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7456 {
7457 if (child_die->tag == DW_TAG_template_type_param
7458 || child_die->tag == DW_TAG_template_value_param)
7459 {
7460 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7461 struct template_symbol);
7462 templ_func->base.is_cplus_template_function = 1;
7463 break;
7464 }
7465 }
7466
c906108c 7467 new = push_context (0, lowpc);
34eaf542
TT
7468 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7469 (struct symbol *) templ_func);
4c2df51b 7470
4cecd739
DJ
7471 /* If there is a location expression for DW_AT_frame_base, record
7472 it. */
e142c38c 7473 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 7474 if (attr)
c034e007
AC
7475 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7476 expression is being recorded directly in the function's symbol
7477 and not in a separate frame-base object. I guess this hack is
7478 to avoid adding some sort of frame-base adjunct/annex to the
7479 function's symbol :-(. The problem with doing this is that it
7480 results in a function symbol with a location expression that
7481 has nothing to do with the location of the function, ouch! The
7482 relationship should be: a function's symbol has-a frame base; a
7483 frame-base has-a location expression. */
e7c27a73 7484 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 7485
e142c38c 7486 cu->list_in_scope = &local_symbols;
c906108c 7487
639d11d3 7488 if (die->child != NULL)
c906108c 7489 {
639d11d3 7490 child_die = die->child;
c906108c
SS
7491 while (child_die && child_die->tag)
7492 {
34eaf542
TT
7493 if (child_die->tag == DW_TAG_template_type_param
7494 || child_die->tag == DW_TAG_template_value_param)
7495 {
7496 struct symbol *arg = new_symbol (child_die, NULL, cu);
7497
f1078f66
DJ
7498 if (arg != NULL)
7499 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7500 }
7501 else
7502 process_die (child_die, cu);
c906108c
SS
7503 child_die = sibling_die (child_die);
7504 }
7505 }
7506
d389af10
JK
7507 inherit_abstract_dies (die, cu);
7508
4a811a97
UW
7509 /* If we have a DW_AT_specification, we might need to import using
7510 directives from the context of the specification DIE. See the
7511 comment in determine_prefix. */
7512 if (cu->language == language_cplus
7513 && dwarf2_attr (die, DW_AT_specification, cu))
7514 {
7515 struct dwarf2_cu *spec_cu = cu;
7516 struct die_info *spec_die = die_specification (die, &spec_cu);
7517
7518 while (spec_die)
7519 {
7520 child_die = spec_die->child;
7521 while (child_die && child_die->tag)
7522 {
7523 if (child_die->tag == DW_TAG_imported_module)
7524 process_die (child_die, spec_cu);
7525 child_die = sibling_die (child_die);
7526 }
7527
7528 /* In some cases, GCC generates specification DIEs that
7529 themselves contain DW_AT_specification attributes. */
7530 spec_die = die_specification (spec_die, &spec_cu);
7531 }
7532 }
7533
c906108c
SS
7534 new = pop_context ();
7535 /* Make a block for the local symbols within. */
801e3a5b
JB
7536 block = finish_block (new->name, &local_symbols, new->old_blocks,
7537 lowpc, highpc, objfile);
7538
df8a16a1 7539 /* For C++, set the block's scope. */
f55ee35c 7540 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 7541 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 7542 determine_prefix (die, cu),
df8a16a1
DJ
7543 processing_has_namespace_info);
7544
801e3a5b
JB
7545 /* If we have address ranges, record them. */
7546 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 7547
34eaf542
TT
7548 /* Attach template arguments to function. */
7549 if (! VEC_empty (symbolp, template_args))
7550 {
7551 gdb_assert (templ_func != NULL);
7552
7553 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7554 templ_func->template_arguments
7555 = obstack_alloc (&objfile->objfile_obstack,
7556 (templ_func->n_template_arguments
7557 * sizeof (struct symbol *)));
7558 memcpy (templ_func->template_arguments,
7559 VEC_address (symbolp, template_args),
7560 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7561 VEC_free (symbolp, template_args);
7562 }
7563
208d8187
JB
7564 /* In C++, we can have functions nested inside functions (e.g., when
7565 a function declares a class that has methods). This means that
7566 when we finish processing a function scope, we may need to go
7567 back to building a containing block's symbol lists. */
7568 local_symbols = new->locals;
7569 param_symbols = new->params;
27aa8d6a 7570 using_directives = new->using_directives;
208d8187 7571
921e78cf
JB
7572 /* If we've finished processing a top-level function, subsequent
7573 symbols go in the file symbol list. */
7574 if (outermost_context_p ())
e142c38c 7575 cu->list_in_scope = &file_symbols;
c906108c
SS
7576}
7577
7578/* Process all the DIES contained within a lexical block scope. Start
7579 a new scope, process the dies, and then close the scope. */
7580
7581static void
e7c27a73 7582read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7583{
e7c27a73 7584 struct objfile *objfile = cu->objfile;
52f0bd74 7585 struct context_stack *new;
c906108c
SS
7586 CORE_ADDR lowpc, highpc;
7587 struct die_info *child_die;
e142c38c
DJ
7588 CORE_ADDR baseaddr;
7589
7590 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
7591
7592 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
7593 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7594 as multiple lexical blocks? Handling children in a sane way would
6e70227d 7595 be nasty. Might be easier to properly extend generic blocks to
af34e669 7596 describe ranges. */
d85a05f0 7597 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
7598 return;
7599 lowpc += baseaddr;
7600 highpc += baseaddr;
7601
7602 push_context (0, lowpc);
639d11d3 7603 if (die->child != NULL)
c906108c 7604 {
639d11d3 7605 child_die = die->child;
c906108c
SS
7606 while (child_die && child_die->tag)
7607 {
e7c27a73 7608 process_die (child_die, cu);
c906108c
SS
7609 child_die = sibling_die (child_die);
7610 }
7611 }
7612 new = pop_context ();
7613
8540c487 7614 if (local_symbols != NULL || using_directives != NULL)
c906108c 7615 {
801e3a5b
JB
7616 struct block *block
7617 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7618 highpc, objfile);
7619
7620 /* Note that recording ranges after traversing children, as we
7621 do here, means that recording a parent's ranges entails
7622 walking across all its children's ranges as they appear in
7623 the address map, which is quadratic behavior.
7624
7625 It would be nicer to record the parent's ranges before
7626 traversing its children, simply overriding whatever you find
7627 there. But since we don't even decide whether to create a
7628 block until after we've traversed its children, that's hard
7629 to do. */
7630 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
7631 }
7632 local_symbols = new->locals;
27aa8d6a 7633 using_directives = new->using_directives;
c906108c
SS
7634}
7635
96408a79
SA
7636/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7637
7638static void
7639read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7640{
7641 struct objfile *objfile = cu->objfile;
7642 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7643 CORE_ADDR pc, baseaddr;
7644 struct attribute *attr;
7645 struct call_site *call_site, call_site_local;
7646 void **slot;
7647 int nparams;
7648 struct die_info *child_die;
7649
7650 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7651
7652 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7653 if (!attr)
7654 {
7655 complaint (&symfile_complaints,
7656 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7657 "DIE 0x%x [in module %s]"),
b64f50a1 7658 die->offset.sect_off, objfile->name);
96408a79
SA
7659 return;
7660 }
7661 pc = DW_ADDR (attr) + baseaddr;
7662
7663 if (cu->call_site_htab == NULL)
7664 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7665 NULL, &objfile->objfile_obstack,
7666 hashtab_obstack_allocate, NULL);
7667 call_site_local.pc = pc;
7668 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7669 if (*slot != NULL)
7670 {
7671 complaint (&symfile_complaints,
7672 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7673 "DIE 0x%x [in module %s]"),
b64f50a1 7674 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
7675 return;
7676 }
7677
7678 /* Count parameters at the caller. */
7679
7680 nparams = 0;
7681 for (child_die = die->child; child_die && child_die->tag;
7682 child_die = sibling_die (child_die))
7683 {
7684 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7685 {
7686 complaint (&symfile_complaints,
7687 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7688 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7689 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
7690 continue;
7691 }
7692
7693 nparams++;
7694 }
7695
7696 call_site = obstack_alloc (&objfile->objfile_obstack,
7697 (sizeof (*call_site)
7698 + (sizeof (*call_site->parameter)
7699 * (nparams - 1))));
7700 *slot = call_site;
7701 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7702 call_site->pc = pc;
7703
7704 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7705 {
7706 struct die_info *func_die;
7707
7708 /* Skip also over DW_TAG_inlined_subroutine. */
7709 for (func_die = die->parent;
7710 func_die && func_die->tag != DW_TAG_subprogram
7711 && func_die->tag != DW_TAG_subroutine_type;
7712 func_die = func_die->parent);
7713
7714 /* DW_AT_GNU_all_call_sites is a superset
7715 of DW_AT_GNU_all_tail_call_sites. */
7716 if (func_die
7717 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7718 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7719 {
7720 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7721 not complete. But keep CALL_SITE for look ups via call_site_htab,
7722 both the initial caller containing the real return address PC and
7723 the final callee containing the current PC of a chain of tail
7724 calls do not need to have the tail call list complete. But any
7725 function candidate for a virtual tail call frame searched via
7726 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7727 determined unambiguously. */
7728 }
7729 else
7730 {
7731 struct type *func_type = NULL;
7732
7733 if (func_die)
7734 func_type = get_die_type (func_die, cu);
7735 if (func_type != NULL)
7736 {
7737 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7738
7739 /* Enlist this call site to the function. */
7740 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7741 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7742 }
7743 else
7744 complaint (&symfile_complaints,
7745 _("Cannot find function owning DW_TAG_GNU_call_site "
7746 "DIE 0x%x [in module %s]"),
b64f50a1 7747 die->offset.sect_off, objfile->name);
96408a79
SA
7748 }
7749 }
7750
7751 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7752 if (attr == NULL)
7753 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7754 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7755 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7756 /* Keep NULL DWARF_BLOCK. */;
7757 else if (attr_form_is_block (attr))
7758 {
7759 struct dwarf2_locexpr_baton *dlbaton;
7760
7761 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7762 dlbaton->data = DW_BLOCK (attr)->data;
7763 dlbaton->size = DW_BLOCK (attr)->size;
7764 dlbaton->per_cu = cu->per_cu;
7765
7766 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7767 }
7768 else if (is_ref_attr (attr))
7769 {
96408a79
SA
7770 struct dwarf2_cu *target_cu = cu;
7771 struct die_info *target_die;
7772
7773 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7774 gdb_assert (target_cu->objfile == objfile);
7775 if (die_is_declaration (target_die, target_cu))
7776 {
7777 const char *target_physname;
7778
7779 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7780 if (target_physname == NULL)
7781 complaint (&symfile_complaints,
7782 _("DW_AT_GNU_call_site_target target DIE has invalid "
7783 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 7784 die->offset.sect_off, objfile->name);
96408a79
SA
7785 else
7786 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7787 }
7788 else
7789 {
7790 CORE_ADDR lowpc;
7791
7792 /* DW_AT_entry_pc should be preferred. */
7793 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7794 complaint (&symfile_complaints,
7795 _("DW_AT_GNU_call_site_target target DIE has invalid "
7796 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 7797 die->offset.sect_off, objfile->name);
96408a79
SA
7798 else
7799 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7800 }
7801 }
7802 else
7803 complaint (&symfile_complaints,
7804 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7805 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 7806 die->offset.sect_off, objfile->name);
96408a79
SA
7807
7808 call_site->per_cu = cu->per_cu;
7809
7810 for (child_die = die->child;
7811 child_die && child_die->tag;
7812 child_die = sibling_die (child_die))
7813 {
96408a79 7814 struct call_site_parameter *parameter;
1788b2d3 7815 struct attribute *loc, *origin;
96408a79
SA
7816
7817 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7818 {
7819 /* Already printed the complaint above. */
7820 continue;
7821 }
7822
7823 gdb_assert (call_site->parameter_count < nparams);
7824 parameter = &call_site->parameter[call_site->parameter_count];
7825
1788b2d3
JK
7826 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
7827 specifies DW_TAG_formal_parameter. Value of the data assumed for the
7828 register is contained in DW_AT_GNU_call_site_value. */
96408a79 7829
24c5c679 7830 loc = dwarf2_attr (child_die, DW_AT_location, cu);
1788b2d3
JK
7831 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
7832 if (loc == NULL && origin != NULL && is_ref_attr (origin))
7833 {
7834 sect_offset offset;
7835
7836 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
7837 offset = dwarf2_get_ref_die_offset (origin);
7838 gdb_assert (offset.sect_off >= cu->header.offset.sect_off);
7839 parameter->u.param_offset.cu_off = (offset.sect_off
7840 - cu->header.offset.sect_off);
7841 }
7842 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79
SA
7843 {
7844 complaint (&symfile_complaints,
7845 _("No DW_FORM_block* DW_AT_location for "
7846 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7847 child_die->offset.sect_off, objfile->name);
96408a79
SA
7848 continue;
7849 }
24c5c679 7850 else
96408a79 7851 {
24c5c679
JK
7852 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
7853 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
7854 if (parameter->u.dwarf_reg != -1)
7855 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
7856 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
7857 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
7858 &parameter->u.fb_offset))
7859 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
7860 else
7861 {
7862 complaint (&symfile_complaints,
7863 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
7864 "for DW_FORM_block* DW_AT_location is supported for "
7865 "DW_TAG_GNU_call_site child DIE 0x%x "
7866 "[in module %s]"),
7867 child_die->offset.sect_off, objfile->name);
7868 continue;
7869 }
96408a79
SA
7870 }
7871
7872 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
7873 if (!attr_form_is_block (attr))
7874 {
7875 complaint (&symfile_complaints,
7876 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
7877 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7878 child_die->offset.sect_off, objfile->name);
96408a79
SA
7879 continue;
7880 }
7881 parameter->value = DW_BLOCK (attr)->data;
7882 parameter->value_size = DW_BLOCK (attr)->size;
7883
7884 /* Parameters are not pre-cleared by memset above. */
7885 parameter->data_value = NULL;
7886 parameter->data_value_size = 0;
7887 call_site->parameter_count++;
7888
7889 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
7890 if (attr)
7891 {
7892 if (!attr_form_is_block (attr))
7893 complaint (&symfile_complaints,
7894 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
7895 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7896 child_die->offset.sect_off, objfile->name);
96408a79
SA
7897 else
7898 {
7899 parameter->data_value = DW_BLOCK (attr)->data;
7900 parameter->data_value_size = DW_BLOCK (attr)->size;
7901 }
7902 }
7903 }
7904}
7905
43039443 7906/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
7907 Return 1 if the attributes are present and valid, otherwise, return 0.
7908 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
7909
7910static int
7911dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
7912 CORE_ADDR *high_return, struct dwarf2_cu *cu,
7913 struct partial_symtab *ranges_pst)
43039443
JK
7914{
7915 struct objfile *objfile = cu->objfile;
7916 struct comp_unit_head *cu_header = &cu->header;
7917 bfd *obfd = objfile->obfd;
7918 unsigned int addr_size = cu_header->addr_size;
7919 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7920 /* Base address selection entry. */
7921 CORE_ADDR base;
7922 int found_base;
7923 unsigned int dummy;
7924 gdb_byte *buffer;
7925 CORE_ADDR marker;
7926 int low_set;
7927 CORE_ADDR low = 0;
7928 CORE_ADDR high = 0;
ff013f42 7929 CORE_ADDR baseaddr;
43039443 7930
d00adf39
DE
7931 found_base = cu->base_known;
7932 base = cu->base_address;
43039443 7933
be391dca 7934 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 7935 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
7936 {
7937 complaint (&symfile_complaints,
7938 _("Offset %d out of bounds for DW_AT_ranges attribute"),
7939 offset);
7940 return 0;
7941 }
dce234bc 7942 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
7943
7944 /* Read in the largest possible address. */
7945 marker = read_address (obfd, buffer, cu, &dummy);
7946 if ((marker & mask) == mask)
7947 {
7948 /* If we found the largest possible address, then
7949 read the base address. */
7950 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7951 buffer += 2 * addr_size;
7952 offset += 2 * addr_size;
7953 found_base = 1;
7954 }
7955
7956 low_set = 0;
7957
e7030f15 7958 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 7959
43039443
JK
7960 while (1)
7961 {
7962 CORE_ADDR range_beginning, range_end;
7963
7964 range_beginning = read_address (obfd, buffer, cu, &dummy);
7965 buffer += addr_size;
7966 range_end = read_address (obfd, buffer, cu, &dummy);
7967 buffer += addr_size;
7968 offset += 2 * addr_size;
7969
7970 /* An end of list marker is a pair of zero addresses. */
7971 if (range_beginning == 0 && range_end == 0)
7972 /* Found the end of list entry. */
7973 break;
7974
7975 /* Each base address selection entry is a pair of 2 values.
7976 The first is the largest possible address, the second is
7977 the base address. Check for a base address here. */
7978 if ((range_beginning & mask) == mask)
7979 {
7980 /* If we found the largest possible address, then
7981 read the base address. */
7982 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7983 found_base = 1;
7984 continue;
7985 }
7986
7987 if (!found_base)
7988 {
7989 /* We have no valid base address for the ranges
7990 data. */
7991 complaint (&symfile_complaints,
7992 _("Invalid .debug_ranges data (no base address)"));
7993 return 0;
7994 }
7995
9277c30c
UW
7996 if (range_beginning > range_end)
7997 {
7998 /* Inverted range entries are invalid. */
7999 complaint (&symfile_complaints,
8000 _("Invalid .debug_ranges data (inverted range)"));
8001 return 0;
8002 }
8003
8004 /* Empty range entries have no effect. */
8005 if (range_beginning == range_end)
8006 continue;
8007
43039443
JK
8008 range_beginning += base;
8009 range_end += base;
8010
9277c30c 8011 if (ranges_pst != NULL)
ff013f42 8012 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
8013 range_beginning + baseaddr,
8014 range_end - 1 + baseaddr,
ff013f42
JK
8015 ranges_pst);
8016
43039443
JK
8017 /* FIXME: This is recording everything as a low-high
8018 segment of consecutive addresses. We should have a
8019 data structure for discontiguous block ranges
8020 instead. */
8021 if (! low_set)
8022 {
8023 low = range_beginning;
8024 high = range_end;
8025 low_set = 1;
8026 }
8027 else
8028 {
8029 if (range_beginning < low)
8030 low = range_beginning;
8031 if (range_end > high)
8032 high = range_end;
8033 }
8034 }
8035
8036 if (! low_set)
8037 /* If the first entry is an end-of-list marker, the range
8038 describes an empty scope, i.e. no instructions. */
8039 return 0;
8040
8041 if (low_return)
8042 *low_return = low;
8043 if (high_return)
8044 *high_return = high;
8045 return 1;
8046}
8047
af34e669
DJ
8048/* Get low and high pc attributes from a die. Return 1 if the attributes
8049 are present and valid, otherwise, return 0. Return -1 if the range is
8050 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 8051
c906108c 8052static int
af34e669 8053dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
8054 CORE_ADDR *highpc, struct dwarf2_cu *cu,
8055 struct partial_symtab *pst)
c906108c
SS
8056{
8057 struct attribute *attr;
91da1414 8058 struct attribute *attr_high;
af34e669
DJ
8059 CORE_ADDR low = 0;
8060 CORE_ADDR high = 0;
8061 int ret = 0;
c906108c 8062
91da1414
MW
8063 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8064 if (attr_high)
af34e669 8065 {
e142c38c 8066 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 8067 if (attr)
91da1414
MW
8068 {
8069 low = DW_ADDR (attr);
3019eac3
DE
8070 if (attr_high->form == DW_FORM_addr
8071 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8072 high = DW_ADDR (attr_high);
8073 else
8074 high = low + DW_UNSND (attr_high);
8075 }
af34e669
DJ
8076 else
8077 /* Found high w/o low attribute. */
8078 return 0;
8079
8080 /* Found consecutive range of addresses. */
8081 ret = 1;
8082 }
c906108c 8083 else
af34e669 8084 {
e142c38c 8085 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
8086 if (attr != NULL)
8087 {
af34e669 8088 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 8089 .debug_ranges section. */
d85a05f0 8090 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 8091 return 0;
43039443 8092 /* Found discontinuous range of addresses. */
af34e669
DJ
8093 ret = -1;
8094 }
8095 }
c906108c 8096
9373cf26
JK
8097 /* read_partial_die has also the strict LOW < HIGH requirement. */
8098 if (high <= low)
c906108c
SS
8099 return 0;
8100
8101 /* When using the GNU linker, .gnu.linkonce. sections are used to
8102 eliminate duplicate copies of functions and vtables and such.
8103 The linker will arbitrarily choose one and discard the others.
8104 The AT_*_pc values for such functions refer to local labels in
8105 these sections. If the section from that file was discarded, the
8106 labels are not in the output, so the relocs get a value of 0.
8107 If this is a discarded function, mark the pc bounds as invalid,
8108 so that GDB will ignore it. */
72dca2f5 8109 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
8110 return 0;
8111
8112 *lowpc = low;
96408a79
SA
8113 if (highpc)
8114 *highpc = high;
af34e669 8115 return ret;
c906108c
SS
8116}
8117
b084d499
JB
8118/* Assuming that DIE represents a subprogram DIE or a lexical block, get
8119 its low and high PC addresses. Do nothing if these addresses could not
8120 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8121 and HIGHPC to the high address if greater than HIGHPC. */
8122
8123static void
8124dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8125 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8126 struct dwarf2_cu *cu)
8127{
8128 CORE_ADDR low, high;
8129 struct die_info *child = die->child;
8130
d85a05f0 8131 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
8132 {
8133 *lowpc = min (*lowpc, low);
8134 *highpc = max (*highpc, high);
8135 }
8136
8137 /* If the language does not allow nested subprograms (either inside
8138 subprograms or lexical blocks), we're done. */
8139 if (cu->language != language_ada)
8140 return;
6e70227d 8141
b084d499
JB
8142 /* Check all the children of the given DIE. If it contains nested
8143 subprograms, then check their pc bounds. Likewise, we need to
8144 check lexical blocks as well, as they may also contain subprogram
8145 definitions. */
8146 while (child && child->tag)
8147 {
8148 if (child->tag == DW_TAG_subprogram
8149 || child->tag == DW_TAG_lexical_block)
8150 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8151 child = sibling_die (child);
8152 }
8153}
8154
fae299cd
DC
8155/* Get the low and high pc's represented by the scope DIE, and store
8156 them in *LOWPC and *HIGHPC. If the correct values can't be
8157 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8158
8159static void
8160get_scope_pc_bounds (struct die_info *die,
8161 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8162 struct dwarf2_cu *cu)
8163{
8164 CORE_ADDR best_low = (CORE_ADDR) -1;
8165 CORE_ADDR best_high = (CORE_ADDR) 0;
8166 CORE_ADDR current_low, current_high;
8167
d85a05f0 8168 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
8169 {
8170 best_low = current_low;
8171 best_high = current_high;
8172 }
8173 else
8174 {
8175 struct die_info *child = die->child;
8176
8177 while (child && child->tag)
8178 {
8179 switch (child->tag) {
8180 case DW_TAG_subprogram:
b084d499 8181 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
8182 break;
8183 case DW_TAG_namespace:
f55ee35c 8184 case DW_TAG_module:
fae299cd
DC
8185 /* FIXME: carlton/2004-01-16: Should we do this for
8186 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8187 that current GCC's always emit the DIEs corresponding
8188 to definitions of methods of classes as children of a
8189 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8190 the DIEs giving the declarations, which could be
8191 anywhere). But I don't see any reason why the
8192 standards says that they have to be there. */
8193 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8194
8195 if (current_low != ((CORE_ADDR) -1))
8196 {
8197 best_low = min (best_low, current_low);
8198 best_high = max (best_high, current_high);
8199 }
8200 break;
8201 default:
0963b4bd 8202 /* Ignore. */
fae299cd
DC
8203 break;
8204 }
8205
8206 child = sibling_die (child);
8207 }
8208 }
8209
8210 *lowpc = best_low;
8211 *highpc = best_high;
8212}
8213
801e3a5b
JB
8214/* Record the address ranges for BLOCK, offset by BASEADDR, as given
8215 in DIE. */
380bca97 8216
801e3a5b
JB
8217static void
8218dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8219 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8220{
bb5ed363 8221 struct objfile *objfile = cu->objfile;
801e3a5b 8222 struct attribute *attr;
91da1414 8223 struct attribute *attr_high;
801e3a5b 8224
91da1414
MW
8225 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8226 if (attr_high)
801e3a5b 8227 {
801e3a5b
JB
8228 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8229 if (attr)
8230 {
8231 CORE_ADDR low = DW_ADDR (attr);
91da1414 8232 CORE_ADDR high;
3019eac3
DE
8233 if (attr_high->form == DW_FORM_addr
8234 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8235 high = DW_ADDR (attr_high);
8236 else
8237 high = low + DW_UNSND (attr_high);
9a619af0 8238
801e3a5b
JB
8239 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8240 }
8241 }
8242
8243 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8244 if (attr)
8245 {
bb5ed363 8246 bfd *obfd = objfile->obfd;
801e3a5b
JB
8247
8248 /* The value of the DW_AT_ranges attribute is the offset of the
8249 address range list in the .debug_ranges section. */
8250 unsigned long offset = DW_UNSND (attr);
dce234bc 8251 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
8252
8253 /* For some target architectures, but not others, the
8254 read_address function sign-extends the addresses it returns.
8255 To recognize base address selection entries, we need a
8256 mask. */
8257 unsigned int addr_size = cu->header.addr_size;
8258 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8259
8260 /* The base address, to which the next pair is relative. Note
8261 that this 'base' is a DWARF concept: most entries in a range
8262 list are relative, to reduce the number of relocs against the
8263 debugging information. This is separate from this function's
8264 'baseaddr' argument, which GDB uses to relocate debugging
8265 information from a shared library based on the address at
8266 which the library was loaded. */
d00adf39
DE
8267 CORE_ADDR base = cu->base_address;
8268 int base_known = cu->base_known;
801e3a5b 8269
be391dca 8270 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 8271 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
8272 {
8273 complaint (&symfile_complaints,
8274 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8275 offset);
8276 return;
8277 }
8278
8279 for (;;)
8280 {
8281 unsigned int bytes_read;
8282 CORE_ADDR start, end;
8283
8284 start = read_address (obfd, buffer, cu, &bytes_read);
8285 buffer += bytes_read;
8286 end = read_address (obfd, buffer, cu, &bytes_read);
8287 buffer += bytes_read;
8288
8289 /* Did we find the end of the range list? */
8290 if (start == 0 && end == 0)
8291 break;
8292
8293 /* Did we find a base address selection entry? */
8294 else if ((start & base_select_mask) == base_select_mask)
8295 {
8296 base = end;
8297 base_known = 1;
8298 }
8299
8300 /* We found an ordinary address range. */
8301 else
8302 {
8303 if (!base_known)
8304 {
8305 complaint (&symfile_complaints,
3e43a32a
MS
8306 _("Invalid .debug_ranges data "
8307 "(no base address)"));
801e3a5b
JB
8308 return;
8309 }
8310
9277c30c
UW
8311 if (start > end)
8312 {
8313 /* Inverted range entries are invalid. */
8314 complaint (&symfile_complaints,
8315 _("Invalid .debug_ranges data "
8316 "(inverted range)"));
8317 return;
8318 }
8319
8320 /* Empty range entries have no effect. */
8321 if (start == end)
8322 continue;
8323
6e70227d
DE
8324 record_block_range (block,
8325 baseaddr + base + start,
801e3a5b
JB
8326 baseaddr + base + end - 1);
8327 }
8328 }
8329 }
8330}
8331
685b1105
JK
8332/* Check whether the producer field indicates either of GCC < 4.6, or the
8333 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 8334
685b1105
JK
8335static void
8336check_producer (struct dwarf2_cu *cu)
60d5a603
JK
8337{
8338 const char *cs;
8339 int major, minor, release;
8340
8341 if (cu->producer == NULL)
8342 {
8343 /* For unknown compilers expect their behavior is DWARF version
8344 compliant.
8345
8346 GCC started to support .debug_types sections by -gdwarf-4 since
8347 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8348 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8349 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8350 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 8351 }
685b1105 8352 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 8353 {
685b1105
JK
8354 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8355
ba919b58
TT
8356 cs = &cu->producer[strlen ("GNU ")];
8357 while (*cs && !isdigit (*cs))
8358 cs++;
8359 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8360 {
8361 /* Not recognized as GCC. */
8362 }
8363 else
685b1105
JK
8364 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
8365 }
8366 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
8367 cu->producer_is_icc = 1;
8368 else
8369 {
8370 /* For other non-GCC compilers, expect their behavior is DWARF version
8371 compliant. */
60d5a603
JK
8372 }
8373
ba919b58 8374 cu->checked_producer = 1;
685b1105 8375}
ba919b58 8376
685b1105
JK
8377/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8378 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8379 during 4.6.0 experimental. */
8380
8381static int
8382producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8383{
8384 if (!cu->checked_producer)
8385 check_producer (cu);
8386
8387 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
8388}
8389
8390/* Return the default accessibility type if it is not overriden by
8391 DW_AT_accessibility. */
8392
8393static enum dwarf_access_attribute
8394dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8395{
8396 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8397 {
8398 /* The default DWARF 2 accessibility for members is public, the default
8399 accessibility for inheritance is private. */
8400
8401 if (die->tag != DW_TAG_inheritance)
8402 return DW_ACCESS_public;
8403 else
8404 return DW_ACCESS_private;
8405 }
8406 else
8407 {
8408 /* DWARF 3+ defines the default accessibility a different way. The same
8409 rules apply now for DW_TAG_inheritance as for the members and it only
8410 depends on the container kind. */
8411
8412 if (die->parent->tag == DW_TAG_class_type)
8413 return DW_ACCESS_private;
8414 else
8415 return DW_ACCESS_public;
8416 }
8417}
8418
74ac6d43
TT
8419/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8420 offset. If the attribute was not found return 0, otherwise return
8421 1. If it was found but could not properly be handled, set *OFFSET
8422 to 0. */
8423
8424static int
8425handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8426 LONGEST *offset)
8427{
8428 struct attribute *attr;
8429
8430 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8431 if (attr != NULL)
8432 {
8433 *offset = 0;
8434
8435 /* Note that we do not check for a section offset first here.
8436 This is because DW_AT_data_member_location is new in DWARF 4,
8437 so if we see it, we can assume that a constant form is really
8438 a constant and not a section offset. */
8439 if (attr_form_is_constant (attr))
8440 *offset = dwarf2_get_attr_constant_value (attr, 0);
8441 else if (attr_form_is_section_offset (attr))
8442 dwarf2_complex_location_expr_complaint ();
8443 else if (attr_form_is_block (attr))
8444 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8445 else
8446 dwarf2_complex_location_expr_complaint ();
8447
8448 return 1;
8449 }
8450
8451 return 0;
8452}
8453
c906108c
SS
8454/* Add an aggregate field to the field list. */
8455
8456static void
107d2387 8457dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 8458 struct dwarf2_cu *cu)
6e70227d 8459{
e7c27a73 8460 struct objfile *objfile = cu->objfile;
5e2b427d 8461 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8462 struct nextfield *new_field;
8463 struct attribute *attr;
8464 struct field *fp;
8465 char *fieldname = "";
8466
8467 /* Allocate a new field list entry and link it in. */
8468 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 8469 make_cleanup (xfree, new_field);
c906108c 8470 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
8471
8472 if (die->tag == DW_TAG_inheritance)
8473 {
8474 new_field->next = fip->baseclasses;
8475 fip->baseclasses = new_field;
8476 }
8477 else
8478 {
8479 new_field->next = fip->fields;
8480 fip->fields = new_field;
8481 }
c906108c
SS
8482 fip->nfields++;
8483
e142c38c 8484 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
8485 if (attr)
8486 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
8487 else
8488 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
8489 if (new_field->accessibility != DW_ACCESS_public)
8490 fip->non_public_fields = 1;
60d5a603 8491
e142c38c 8492 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
8493 if (attr)
8494 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
8495 else
8496 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
8497
8498 fp = &new_field->field;
a9a9bd0f 8499
e142c38c 8500 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 8501 {
74ac6d43
TT
8502 LONGEST offset;
8503
a9a9bd0f 8504 /* Data member other than a C++ static data member. */
6e70227d 8505
c906108c 8506 /* Get type of field. */
e7c27a73 8507 fp->type = die_type (die, cu);
c906108c 8508
d6a843b5 8509 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 8510
c906108c 8511 /* Get bit size of field (zero if none). */
e142c38c 8512 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
8513 if (attr)
8514 {
8515 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8516 }
8517 else
8518 {
8519 FIELD_BITSIZE (*fp) = 0;
8520 }
8521
8522 /* Get bit offset of field. */
74ac6d43
TT
8523 if (handle_data_member_location (die, cu, &offset))
8524 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 8525 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
8526 if (attr)
8527 {
5e2b427d 8528 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
8529 {
8530 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
8531 additional bit offset from the MSB of the containing
8532 anonymous object to the MSB of the field. We don't
8533 have to do anything special since we don't need to
8534 know the size of the anonymous object. */
f41f5e61 8535 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
8536 }
8537 else
8538 {
8539 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
8540 MSB of the anonymous object, subtract off the number of
8541 bits from the MSB of the field to the MSB of the
8542 object, and then subtract off the number of bits of
8543 the field itself. The result is the bit offset of
8544 the LSB of the field. */
c906108c
SS
8545 int anonymous_size;
8546 int bit_offset = DW_UNSND (attr);
8547
e142c38c 8548 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8549 if (attr)
8550 {
8551 /* The size of the anonymous object containing
8552 the bit field is explicit, so use the
8553 indicated size (in bytes). */
8554 anonymous_size = DW_UNSND (attr);
8555 }
8556 else
8557 {
8558 /* The size of the anonymous object containing
8559 the bit field must be inferred from the type
8560 attribute of the data member containing the
8561 bit field. */
8562 anonymous_size = TYPE_LENGTH (fp->type);
8563 }
f41f5e61
PA
8564 SET_FIELD_BITPOS (*fp,
8565 (FIELD_BITPOS (*fp)
8566 + anonymous_size * bits_per_byte
8567 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
8568 }
8569 }
8570
8571 /* Get name of field. */
39cbfefa
DJ
8572 fieldname = dwarf2_name (die, cu);
8573 if (fieldname == NULL)
8574 fieldname = "";
d8151005
DJ
8575
8576 /* The name is already allocated along with this objfile, so we don't
8577 need to duplicate it for the type. */
8578 fp->name = fieldname;
c906108c
SS
8579
8580 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 8581 pointer or virtual base class pointer) to private. */
e142c38c 8582 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 8583 {
d48cc9dd 8584 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
8585 new_field->accessibility = DW_ACCESS_private;
8586 fip->non_public_fields = 1;
8587 }
8588 }
a9a9bd0f 8589 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 8590 {
a9a9bd0f
DC
8591 /* C++ static member. */
8592
8593 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8594 is a declaration, but all versions of G++ as of this writing
8595 (so through at least 3.2.1) incorrectly generate
8596 DW_TAG_variable tags. */
6e70227d 8597
ff355380 8598 const char *physname;
c906108c 8599
a9a9bd0f 8600 /* Get name of field. */
39cbfefa
DJ
8601 fieldname = dwarf2_name (die, cu);
8602 if (fieldname == NULL)
c906108c
SS
8603 return;
8604
254e6b9e 8605 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
8606 if (attr
8607 /* Only create a symbol if this is an external value.
8608 new_symbol checks this and puts the value in the global symbol
8609 table, which we want. If it is not external, new_symbol
8610 will try to put the value in cu->list_in_scope which is wrong. */
8611 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
8612 {
8613 /* A static const member, not much different than an enum as far as
8614 we're concerned, except that we can support more types. */
8615 new_symbol (die, NULL, cu);
8616 }
8617
2df3850c 8618 /* Get physical name. */
ff355380 8619 physname = dwarf2_physname (fieldname, die, cu);
c906108c 8620
d8151005
DJ
8621 /* The name is already allocated along with this objfile, so we don't
8622 need to duplicate it for the type. */
8623 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 8624 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 8625 FIELD_NAME (*fp) = fieldname;
c906108c
SS
8626 }
8627 else if (die->tag == DW_TAG_inheritance)
8628 {
74ac6d43 8629 LONGEST offset;
d4b96c9a 8630
74ac6d43
TT
8631 /* C++ base class field. */
8632 if (handle_data_member_location (die, cu, &offset))
8633 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 8634 FIELD_BITSIZE (*fp) = 0;
e7c27a73 8635 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
8636 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8637 fip->nbaseclasses++;
8638 }
8639}
8640
98751a41
JK
8641/* Add a typedef defined in the scope of the FIP's class. */
8642
8643static void
8644dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8645 struct dwarf2_cu *cu)
6e70227d 8646{
98751a41 8647 struct objfile *objfile = cu->objfile;
98751a41
JK
8648 struct typedef_field_list *new_field;
8649 struct attribute *attr;
8650 struct typedef_field *fp;
8651 char *fieldname = "";
8652
8653 /* Allocate a new field list entry and link it in. */
8654 new_field = xzalloc (sizeof (*new_field));
8655 make_cleanup (xfree, new_field);
8656
8657 gdb_assert (die->tag == DW_TAG_typedef);
8658
8659 fp = &new_field->field;
8660
8661 /* Get name of field. */
8662 fp->name = dwarf2_name (die, cu);
8663 if (fp->name == NULL)
8664 return;
8665
8666 fp->type = read_type_die (die, cu);
8667
8668 new_field->next = fip->typedef_field_list;
8669 fip->typedef_field_list = new_field;
8670 fip->typedef_field_list_count++;
8671}
8672
c906108c
SS
8673/* Create the vector of fields, and attach it to the type. */
8674
8675static void
fba45db2 8676dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8677 struct dwarf2_cu *cu)
c906108c
SS
8678{
8679 int nfields = fip->nfields;
8680
8681 /* Record the field count, allocate space for the array of fields,
8682 and create blank accessibility bitfields if necessary. */
8683 TYPE_NFIELDS (type) = nfields;
8684 TYPE_FIELDS (type) = (struct field *)
8685 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8686 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8687
b4ba55a1 8688 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
8689 {
8690 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8691
8692 TYPE_FIELD_PRIVATE_BITS (type) =
8693 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8694 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8695
8696 TYPE_FIELD_PROTECTED_BITS (type) =
8697 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8698 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8699
774b6a14
TT
8700 TYPE_FIELD_IGNORE_BITS (type) =
8701 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8702 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
8703 }
8704
8705 /* If the type has baseclasses, allocate and clear a bit vector for
8706 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 8707 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
8708 {
8709 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 8710 unsigned char *pointer;
c906108c
SS
8711
8712 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
8713 pointer = TYPE_ALLOC (type, num_bytes);
8714 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
8715 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8716 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8717 }
8718
3e43a32a
MS
8719 /* Copy the saved-up fields into the field vector. Start from the head of
8720 the list, adding to the tail of the field array, so that they end up in
8721 the same order in the array in which they were added to the list. */
c906108c
SS
8722 while (nfields-- > 0)
8723 {
7d0ccb61
DJ
8724 struct nextfield *fieldp;
8725
8726 if (fip->fields)
8727 {
8728 fieldp = fip->fields;
8729 fip->fields = fieldp->next;
8730 }
8731 else
8732 {
8733 fieldp = fip->baseclasses;
8734 fip->baseclasses = fieldp->next;
8735 }
8736
8737 TYPE_FIELD (type, nfields) = fieldp->field;
8738 switch (fieldp->accessibility)
c906108c 8739 {
c5aa993b 8740 case DW_ACCESS_private:
b4ba55a1
JB
8741 if (cu->language != language_ada)
8742 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 8743 break;
c906108c 8744
c5aa993b 8745 case DW_ACCESS_protected:
b4ba55a1
JB
8746 if (cu->language != language_ada)
8747 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 8748 break;
c906108c 8749
c5aa993b
JM
8750 case DW_ACCESS_public:
8751 break;
c906108c 8752
c5aa993b
JM
8753 default:
8754 /* Unknown accessibility. Complain and treat it as public. */
8755 {
e2e0b3e5 8756 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 8757 fieldp->accessibility);
c5aa993b
JM
8758 }
8759 break;
c906108c
SS
8760 }
8761 if (nfields < fip->nbaseclasses)
8762 {
7d0ccb61 8763 switch (fieldp->virtuality)
c906108c 8764 {
c5aa993b
JM
8765 case DW_VIRTUALITY_virtual:
8766 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 8767 if (cu->language == language_ada)
a73c6dcd 8768 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
8769 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8770 break;
c906108c
SS
8771 }
8772 }
c906108c
SS
8773 }
8774}
8775
c906108c
SS
8776/* Add a member function to the proper fieldlist. */
8777
8778static void
107d2387 8779dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 8780 struct type *type, struct dwarf2_cu *cu)
c906108c 8781{
e7c27a73 8782 struct objfile *objfile = cu->objfile;
c906108c
SS
8783 struct attribute *attr;
8784 struct fnfieldlist *flp;
8785 int i;
8786 struct fn_field *fnp;
8787 char *fieldname;
c906108c 8788 struct nextfnfield *new_fnfield;
f792889a 8789 struct type *this_type;
60d5a603 8790 enum dwarf_access_attribute accessibility;
c906108c 8791
b4ba55a1 8792 if (cu->language == language_ada)
a73c6dcd 8793 error (_("unexpected member function in Ada type"));
b4ba55a1 8794
2df3850c 8795 /* Get name of member function. */
39cbfefa
DJ
8796 fieldname = dwarf2_name (die, cu);
8797 if (fieldname == NULL)
2df3850c 8798 return;
c906108c 8799
c906108c
SS
8800 /* Look up member function name in fieldlist. */
8801 for (i = 0; i < fip->nfnfields; i++)
8802 {
27bfe10e 8803 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
8804 break;
8805 }
8806
8807 /* Create new list element if necessary. */
8808 if (i < fip->nfnfields)
8809 flp = &fip->fnfieldlists[i];
8810 else
8811 {
8812 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8813 {
8814 fip->fnfieldlists = (struct fnfieldlist *)
8815 xrealloc (fip->fnfieldlists,
8816 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8817 * sizeof (struct fnfieldlist));
c906108c 8818 if (fip->nfnfields == 0)
c13c43fd 8819 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
8820 }
8821 flp = &fip->fnfieldlists[fip->nfnfields];
8822 flp->name = fieldname;
8823 flp->length = 0;
8824 flp->head = NULL;
3da10d80 8825 i = fip->nfnfields++;
c906108c
SS
8826 }
8827
8828 /* Create a new member function field and chain it to the field list
0963b4bd 8829 entry. */
c906108c 8830 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 8831 make_cleanup (xfree, new_fnfield);
c906108c
SS
8832 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8833 new_fnfield->next = flp->head;
8834 flp->head = new_fnfield;
8835 flp->length++;
8836
8837 /* Fill in the member function field info. */
8838 fnp = &new_fnfield->fnfield;
3da10d80
KS
8839
8840 /* Delay processing of the physname until later. */
8841 if (cu->language == language_cplus || cu->language == language_java)
8842 {
8843 add_to_method_list (type, i, flp->length - 1, fieldname,
8844 die, cu);
8845 }
8846 else
8847 {
1d06ead6 8848 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
8849 fnp->physname = physname ? physname : "";
8850 }
8851
c906108c 8852 fnp->type = alloc_type (objfile);
f792889a
DJ
8853 this_type = read_type_die (die, cu);
8854 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 8855 {
f792889a 8856 int nparams = TYPE_NFIELDS (this_type);
c906108c 8857
f792889a 8858 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
8859 of the method itself (TYPE_CODE_METHOD). */
8860 smash_to_method_type (fnp->type, type,
f792889a
DJ
8861 TYPE_TARGET_TYPE (this_type),
8862 TYPE_FIELDS (this_type),
8863 TYPE_NFIELDS (this_type),
8864 TYPE_VARARGS (this_type));
c906108c
SS
8865
8866 /* Handle static member functions.
c5aa993b 8867 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
8868 member functions. G++ helps GDB by marking the first
8869 parameter for non-static member functions (which is the this
8870 pointer) as artificial. We obtain this information from
8871 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 8872 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
8873 fnp->voffset = VOFFSET_STATIC;
8874 }
8875 else
e2e0b3e5 8876 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 8877 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
8878
8879 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 8880 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 8881 fnp->fcontext = die_containing_type (die, cu);
c906108c 8882
3e43a32a
MS
8883 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
8884 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
8885
8886 /* Get accessibility. */
e142c38c 8887 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 8888 if (attr)
60d5a603
JK
8889 accessibility = DW_UNSND (attr);
8890 else
8891 accessibility = dwarf2_default_access_attribute (die, cu);
8892 switch (accessibility)
c906108c 8893 {
60d5a603
JK
8894 case DW_ACCESS_private:
8895 fnp->is_private = 1;
8896 break;
8897 case DW_ACCESS_protected:
8898 fnp->is_protected = 1;
8899 break;
c906108c
SS
8900 }
8901
b02dede2 8902 /* Check for artificial methods. */
e142c38c 8903 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
8904 if (attr && DW_UNSND (attr) != 0)
8905 fnp->is_artificial = 1;
8906
0d564a31 8907 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
8908 function. For older versions of GCC, this is an offset in the
8909 appropriate virtual table, as specified by DW_AT_containing_type.
8910 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
8911 to the object address. */
8912
e142c38c 8913 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 8914 if (attr)
8e19ed76 8915 {
aec5aa8b 8916 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 8917 {
aec5aa8b
TT
8918 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
8919 {
8920 /* Old-style GCC. */
8921 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8922 }
8923 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
8924 || (DW_BLOCK (attr)->size > 1
8925 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
8926 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
8927 {
8928 struct dwarf_block blk;
8929 int offset;
8930
8931 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
8932 ? 1 : 2);
8933 blk.size = DW_BLOCK (attr)->size - offset;
8934 blk.data = DW_BLOCK (attr)->data + offset;
8935 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
8936 if ((fnp->voffset % cu->header.addr_size) != 0)
8937 dwarf2_complex_location_expr_complaint ();
8938 else
8939 fnp->voffset /= cu->header.addr_size;
8940 fnp->voffset += 2;
8941 }
8942 else
8943 dwarf2_complex_location_expr_complaint ();
8944
8945 if (!fnp->fcontext)
8946 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
8947 }
3690dd37 8948 else if (attr_form_is_section_offset (attr))
8e19ed76 8949 {
4d3c2250 8950 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8951 }
8952 else
8953 {
4d3c2250
KB
8954 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
8955 fieldname);
8e19ed76 8956 }
0d564a31 8957 }
d48cc9dd
DJ
8958 else
8959 {
8960 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8961 if (attr && DW_UNSND (attr))
8962 {
8963 /* GCC does this, as of 2008-08-25; PR debug/37237. */
8964 complaint (&symfile_complaints,
3e43a32a
MS
8965 _("Member function \"%s\" (offset %d) is virtual "
8966 "but the vtable offset is not specified"),
b64f50a1 8967 fieldname, die->offset.sect_off);
9655fd1a 8968 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
8969 TYPE_CPLUS_DYNAMIC (type) = 1;
8970 }
8971 }
c906108c
SS
8972}
8973
8974/* Create the vector of member function fields, and attach it to the type. */
8975
8976static void
fba45db2 8977dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8978 struct dwarf2_cu *cu)
c906108c
SS
8979{
8980 struct fnfieldlist *flp;
c906108c
SS
8981 int i;
8982
b4ba55a1 8983 if (cu->language == language_ada)
a73c6dcd 8984 error (_("unexpected member functions in Ada type"));
b4ba55a1 8985
c906108c
SS
8986 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8987 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
8988 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
8989
8990 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
8991 {
8992 struct nextfnfield *nfp = flp->head;
8993 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
8994 int k;
8995
8996 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
8997 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
8998 fn_flp->fn_fields = (struct fn_field *)
8999 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
9000 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 9001 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
9002 }
9003
9004 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
9005}
9006
1168df01
JB
9007/* Returns non-zero if NAME is the name of a vtable member in CU's
9008 language, zero otherwise. */
9009static int
9010is_vtable_name (const char *name, struct dwarf2_cu *cu)
9011{
9012 static const char vptr[] = "_vptr";
987504bb 9013 static const char vtable[] = "vtable";
1168df01 9014
987504bb
JJ
9015 /* Look for the C++ and Java forms of the vtable. */
9016 if ((cu->language == language_java
9017 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
9018 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
9019 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
9020 return 1;
9021
9022 return 0;
9023}
9024
c0dd20ea 9025/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
9026 functions, with the ABI-specified layout. If TYPE describes
9027 such a structure, smash it into a member function type.
61049d3b
DJ
9028
9029 GCC shouldn't do this; it should just output pointer to member DIEs.
9030 This is GCC PR debug/28767. */
c0dd20ea 9031
0b92b5bb
TT
9032static void
9033quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 9034{
0b92b5bb 9035 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
9036
9037 /* Check for a structure with no name and two children. */
0b92b5bb
TT
9038 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
9039 return;
c0dd20ea
DJ
9040
9041 /* Check for __pfn and __delta members. */
0b92b5bb
TT
9042 if (TYPE_FIELD_NAME (type, 0) == NULL
9043 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
9044 || TYPE_FIELD_NAME (type, 1) == NULL
9045 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
9046 return;
c0dd20ea
DJ
9047
9048 /* Find the type of the method. */
0b92b5bb 9049 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
9050 if (pfn_type == NULL
9051 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
9052 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 9053 return;
c0dd20ea
DJ
9054
9055 /* Look for the "this" argument. */
9056 pfn_type = TYPE_TARGET_TYPE (pfn_type);
9057 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 9058 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 9059 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 9060 return;
c0dd20ea
DJ
9061
9062 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
9063 new_type = alloc_type (objfile);
9064 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
9065 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
9066 TYPE_VARARGS (pfn_type));
0b92b5bb 9067 smash_to_methodptr_type (type, new_type);
c0dd20ea 9068}
1168df01 9069
685b1105
JK
9070/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
9071 (icc). */
9072
9073static int
9074producer_is_icc (struct dwarf2_cu *cu)
9075{
9076 if (!cu->checked_producer)
9077 check_producer (cu);
9078
9079 return cu->producer_is_icc;
9080}
9081
c906108c 9082/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
9083 (definition) to create a type for the structure or union. Fill in
9084 the type's name and general properties; the members will not be
9085 processed until process_structure_type.
c906108c 9086
c767944b
DJ
9087 NOTE: we need to call these functions regardless of whether or not the
9088 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
9089 structure or union. This gets the type entered into our set of
9090 user defined types.
9091
9092 However, if the structure is incomplete (an opaque struct/union)
9093 then suppress creating a symbol table entry for it since gdb only
9094 wants to find the one with the complete definition. Note that if
9095 it is complete, we just call new_symbol, which does it's own
9096 checking about whether the struct/union is anonymous or not (and
9097 suppresses creating a symbol table entry itself). */
9098
f792889a 9099static struct type *
134d01f1 9100read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9101{
e7c27a73 9102 struct objfile *objfile = cu->objfile;
c906108c
SS
9103 struct type *type;
9104 struct attribute *attr;
39cbfefa 9105 char *name;
c906108c 9106
348e048f
DE
9107 /* If the definition of this type lives in .debug_types, read that type.
9108 Don't follow DW_AT_specification though, that will take us back up
9109 the chain and we want to go down. */
9110 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9111 if (attr)
9112 {
9113 struct dwarf2_cu *type_cu = cu;
9114 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9115
348e048f
DE
9116 /* We could just recurse on read_structure_type, but we need to call
9117 get_die_type to ensure only one type for this DIE is created.
9118 This is important, for example, because for c++ classes we need
9119 TYPE_NAME set which is only done by new_symbol. Blech. */
9120 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9121
9122 /* TYPE_CU may not be the same as CU.
9123 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9124 return set_die_type (die, type, cu);
9125 }
9126
c0dd20ea 9127 type = alloc_type (objfile);
c906108c 9128 INIT_CPLUS_SPECIFIC (type);
93311388 9129
39cbfefa
DJ
9130 name = dwarf2_name (die, cu);
9131 if (name != NULL)
c906108c 9132 {
987504bb
JJ
9133 if (cu->language == language_cplus
9134 || cu->language == language_java)
63d06c5c 9135 {
3da10d80
KS
9136 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9137
9138 /* dwarf2_full_name might have already finished building the DIE's
9139 type. If so, there is no need to continue. */
9140 if (get_die_type (die, cu) != NULL)
9141 return get_die_type (die, cu);
9142
9143 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
9144 if (die->tag == DW_TAG_structure_type
9145 || die->tag == DW_TAG_class_type)
9146 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
9147 }
9148 else
9149 {
d8151005
DJ
9150 /* The name is already allocated along with this objfile, so
9151 we don't need to duplicate it for the type. */
94af9270
KS
9152 TYPE_TAG_NAME (type) = (char *) name;
9153 if (die->tag == DW_TAG_class_type)
9154 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 9155 }
c906108c
SS
9156 }
9157
9158 if (die->tag == DW_TAG_structure_type)
9159 {
9160 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9161 }
9162 else if (die->tag == DW_TAG_union_type)
9163 {
9164 TYPE_CODE (type) = TYPE_CODE_UNION;
9165 }
9166 else
9167 {
c906108c
SS
9168 TYPE_CODE (type) = TYPE_CODE_CLASS;
9169 }
9170
0cc2414c
TT
9171 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9172 TYPE_DECLARED_CLASS (type) = 1;
9173
e142c38c 9174 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9175 if (attr)
9176 {
9177 TYPE_LENGTH (type) = DW_UNSND (attr);
9178 }
9179 else
9180 {
9181 TYPE_LENGTH (type) = 0;
9182 }
9183
685b1105
JK
9184 if (producer_is_icc (cu))
9185 {
9186 /* ICC does not output the required DW_AT_declaration
9187 on incomplete types, but gives them a size of zero. */
9188 }
9189 else
9190 TYPE_STUB_SUPPORTED (type) = 1;
9191
dc718098 9192 if (die_is_declaration (die, cu))
876cecd0 9193 TYPE_STUB (type) = 1;
a6c727b2
DJ
9194 else if (attr == NULL && die->child == NULL
9195 && producer_is_realview (cu->producer))
9196 /* RealView does not output the required DW_AT_declaration
9197 on incomplete types. */
9198 TYPE_STUB (type) = 1;
dc718098 9199
c906108c
SS
9200 /* We need to add the type field to the die immediately so we don't
9201 infinitely recurse when dealing with pointers to the structure
0963b4bd 9202 type within the structure itself. */
1c379e20 9203 set_die_type (die, type, cu);
c906108c 9204
7e314c57
JK
9205 /* set_die_type should be already done. */
9206 set_descriptive_type (type, die, cu);
9207
c767944b
DJ
9208 return type;
9209}
9210
9211/* Finish creating a structure or union type, including filling in
9212 its members and creating a symbol for it. */
9213
9214static void
9215process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9216{
9217 struct objfile *objfile = cu->objfile;
9218 struct die_info *child_die = die->child;
9219 struct type *type;
9220
9221 type = get_die_type (die, cu);
9222 if (type == NULL)
9223 type = read_structure_type (die, cu);
9224
e142c38c 9225 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
9226 {
9227 struct field_info fi;
9228 struct die_info *child_die;
34eaf542 9229 VEC (symbolp) *template_args = NULL;
c767944b 9230 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
9231
9232 memset (&fi, 0, sizeof (struct field_info));
9233
639d11d3 9234 child_die = die->child;
c906108c
SS
9235
9236 while (child_die && child_die->tag)
9237 {
a9a9bd0f
DC
9238 if (child_die->tag == DW_TAG_member
9239 || child_die->tag == DW_TAG_variable)
c906108c 9240 {
a9a9bd0f
DC
9241 /* NOTE: carlton/2002-11-05: A C++ static data member
9242 should be a DW_TAG_member that is a declaration, but
9243 all versions of G++ as of this writing (so through at
9244 least 3.2.1) incorrectly generate DW_TAG_variable
9245 tags for them instead. */
e7c27a73 9246 dwarf2_add_field (&fi, child_die, cu);
c906108c 9247 }
8713b1b1 9248 else if (child_die->tag == DW_TAG_subprogram)
c906108c 9249 {
0963b4bd 9250 /* C++ member function. */
e7c27a73 9251 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
9252 }
9253 else if (child_die->tag == DW_TAG_inheritance)
9254 {
9255 /* C++ base class field. */
e7c27a73 9256 dwarf2_add_field (&fi, child_die, cu);
c906108c 9257 }
98751a41
JK
9258 else if (child_die->tag == DW_TAG_typedef)
9259 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
9260 else if (child_die->tag == DW_TAG_template_type_param
9261 || child_die->tag == DW_TAG_template_value_param)
9262 {
9263 struct symbol *arg = new_symbol (child_die, NULL, cu);
9264
f1078f66
DJ
9265 if (arg != NULL)
9266 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9267 }
9268
c906108c
SS
9269 child_die = sibling_die (child_die);
9270 }
9271
34eaf542
TT
9272 /* Attach template arguments to type. */
9273 if (! VEC_empty (symbolp, template_args))
9274 {
9275 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9276 TYPE_N_TEMPLATE_ARGUMENTS (type)
9277 = VEC_length (symbolp, template_args);
9278 TYPE_TEMPLATE_ARGUMENTS (type)
9279 = obstack_alloc (&objfile->objfile_obstack,
9280 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9281 * sizeof (struct symbol *)));
9282 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9283 VEC_address (symbolp, template_args),
9284 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9285 * sizeof (struct symbol *)));
9286 VEC_free (symbolp, template_args);
9287 }
9288
c906108c
SS
9289 /* Attach fields and member functions to the type. */
9290 if (fi.nfields)
e7c27a73 9291 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
9292 if (fi.nfnfields)
9293 {
e7c27a73 9294 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 9295
c5aa993b 9296 /* Get the type which refers to the base class (possibly this
c906108c 9297 class itself) which contains the vtable pointer for the current
0d564a31
DJ
9298 class from the DW_AT_containing_type attribute. This use of
9299 DW_AT_containing_type is a GNU extension. */
c906108c 9300
e142c38c 9301 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 9302 {
e7c27a73 9303 struct type *t = die_containing_type (die, cu);
c906108c
SS
9304
9305 TYPE_VPTR_BASETYPE (type) = t;
9306 if (type == t)
9307 {
c906108c
SS
9308 int i;
9309
9310 /* Our own class provides vtbl ptr. */
9311 for (i = TYPE_NFIELDS (t) - 1;
9312 i >= TYPE_N_BASECLASSES (t);
9313 --i)
9314 {
0d5cff50 9315 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 9316
1168df01 9317 if (is_vtable_name (fieldname, cu))
c906108c
SS
9318 {
9319 TYPE_VPTR_FIELDNO (type) = i;
9320 break;
9321 }
9322 }
9323
9324 /* Complain if virtual function table field not found. */
9325 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 9326 complaint (&symfile_complaints,
3e43a32a
MS
9327 _("virtual function table pointer "
9328 "not found when defining class '%s'"),
4d3c2250
KB
9329 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9330 "");
c906108c
SS
9331 }
9332 else
9333 {
9334 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9335 }
9336 }
f6235d4c
EZ
9337 else if (cu->producer
9338 && strncmp (cu->producer,
9339 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9340 {
9341 /* The IBM XLC compiler does not provide direct indication
9342 of the containing type, but the vtable pointer is
9343 always named __vfp. */
9344
9345 int i;
9346
9347 for (i = TYPE_NFIELDS (type) - 1;
9348 i >= TYPE_N_BASECLASSES (type);
9349 --i)
9350 {
9351 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9352 {
9353 TYPE_VPTR_FIELDNO (type) = i;
9354 TYPE_VPTR_BASETYPE (type) = type;
9355 break;
9356 }
9357 }
9358 }
c906108c 9359 }
98751a41
JK
9360
9361 /* Copy fi.typedef_field_list linked list elements content into the
9362 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9363 if (fi.typedef_field_list)
9364 {
9365 int i = fi.typedef_field_list_count;
9366
a0d7a4ff 9367 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
9368 TYPE_TYPEDEF_FIELD_ARRAY (type)
9369 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9370 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9371
9372 /* Reverse the list order to keep the debug info elements order. */
9373 while (--i >= 0)
9374 {
9375 struct typedef_field *dest, *src;
6e70227d 9376
98751a41
JK
9377 dest = &TYPE_TYPEDEF_FIELD (type, i);
9378 src = &fi.typedef_field_list->field;
9379 fi.typedef_field_list = fi.typedef_field_list->next;
9380 *dest = *src;
9381 }
9382 }
c767944b
DJ
9383
9384 do_cleanups (back_to);
eb2a6f42
TT
9385
9386 if (HAVE_CPLUS_STRUCT (type))
9387 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 9388 }
63d06c5c 9389
bb5ed363 9390 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 9391
90aeadfc
DC
9392 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9393 snapshots) has been known to create a die giving a declaration
9394 for a class that has, as a child, a die giving a definition for a
9395 nested class. So we have to process our children even if the
9396 current die is a declaration. Normally, of course, a declaration
9397 won't have any children at all. */
134d01f1 9398
90aeadfc
DC
9399 while (child_die != NULL && child_die->tag)
9400 {
9401 if (child_die->tag == DW_TAG_member
9402 || child_die->tag == DW_TAG_variable
34eaf542
TT
9403 || child_die->tag == DW_TAG_inheritance
9404 || child_die->tag == DW_TAG_template_value_param
9405 || child_die->tag == DW_TAG_template_type_param)
134d01f1 9406 {
90aeadfc 9407 /* Do nothing. */
134d01f1 9408 }
90aeadfc
DC
9409 else
9410 process_die (child_die, cu);
134d01f1 9411
90aeadfc 9412 child_die = sibling_die (child_die);
134d01f1
DJ
9413 }
9414
fa4028e9
JB
9415 /* Do not consider external references. According to the DWARF standard,
9416 these DIEs are identified by the fact that they have no byte_size
9417 attribute, and a declaration attribute. */
9418 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9419 || !die_is_declaration (die, cu))
c767944b 9420 new_symbol (die, type, cu);
134d01f1
DJ
9421}
9422
9423/* Given a DW_AT_enumeration_type die, set its type. We do not
9424 complete the type's fields yet, or create any symbols. */
c906108c 9425
f792889a 9426static struct type *
134d01f1 9427read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9428{
e7c27a73 9429 struct objfile *objfile = cu->objfile;
c906108c 9430 struct type *type;
c906108c 9431 struct attribute *attr;
0114d602 9432 const char *name;
134d01f1 9433
348e048f
DE
9434 /* If the definition of this type lives in .debug_types, read that type.
9435 Don't follow DW_AT_specification though, that will take us back up
9436 the chain and we want to go down. */
9437 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9438 if (attr)
9439 {
9440 struct dwarf2_cu *type_cu = cu;
9441 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9442
348e048f 9443 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9444
9445 /* TYPE_CU may not be the same as CU.
9446 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9447 return set_die_type (die, type, cu);
9448 }
9449
c906108c
SS
9450 type = alloc_type (objfile);
9451
9452 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 9453 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 9454 if (name != NULL)
0114d602 9455 TYPE_TAG_NAME (type) = (char *) name;
c906108c 9456
e142c38c 9457 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9458 if (attr)
9459 {
9460 TYPE_LENGTH (type) = DW_UNSND (attr);
9461 }
9462 else
9463 {
9464 TYPE_LENGTH (type) = 0;
9465 }
9466
137033e9
JB
9467 /* The enumeration DIE can be incomplete. In Ada, any type can be
9468 declared as private in the package spec, and then defined only
9469 inside the package body. Such types are known as Taft Amendment
9470 Types. When another package uses such a type, an incomplete DIE
9471 may be generated by the compiler. */
02eb380e 9472 if (die_is_declaration (die, cu))
876cecd0 9473 TYPE_STUB (type) = 1;
02eb380e 9474
f792889a 9475 return set_die_type (die, type, cu);
134d01f1
DJ
9476}
9477
9478/* Given a pointer to a die which begins an enumeration, process all
9479 the dies that define the members of the enumeration, and create the
9480 symbol for the enumeration type.
9481
9482 NOTE: We reverse the order of the element list. */
9483
9484static void
9485process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9486{
f792889a 9487 struct type *this_type;
134d01f1 9488
f792889a
DJ
9489 this_type = get_die_type (die, cu);
9490 if (this_type == NULL)
9491 this_type = read_enumeration_type (die, cu);
9dc481d3 9492
639d11d3 9493 if (die->child != NULL)
c906108c 9494 {
9dc481d3
DE
9495 struct die_info *child_die;
9496 struct symbol *sym;
9497 struct field *fields = NULL;
9498 int num_fields = 0;
9499 int unsigned_enum = 1;
9500 char *name;
cafec441
TT
9501 int flag_enum = 1;
9502 ULONGEST mask = 0;
9dc481d3 9503
639d11d3 9504 child_die = die->child;
c906108c
SS
9505 while (child_die && child_die->tag)
9506 {
9507 if (child_die->tag != DW_TAG_enumerator)
9508 {
e7c27a73 9509 process_die (child_die, cu);
c906108c
SS
9510 }
9511 else
9512 {
39cbfefa
DJ
9513 name = dwarf2_name (child_die, cu);
9514 if (name)
c906108c 9515 {
f792889a 9516 sym = new_symbol (child_die, this_type, cu);
c906108c 9517 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
9518 {
9519 unsigned_enum = 0;
9520 flag_enum = 0;
9521 }
9522 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9523 flag_enum = 0;
9524 else
9525 mask |= SYMBOL_VALUE (sym);
c906108c
SS
9526
9527 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9528 {
9529 fields = (struct field *)
9530 xrealloc (fields,
9531 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 9532 * sizeof (struct field));
c906108c
SS
9533 }
9534
3567439c 9535 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 9536 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 9537 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
9538 FIELD_BITSIZE (fields[num_fields]) = 0;
9539
9540 num_fields++;
9541 }
9542 }
9543
9544 child_die = sibling_die (child_die);
9545 }
9546
9547 if (num_fields)
9548 {
f792889a
DJ
9549 TYPE_NFIELDS (this_type) = num_fields;
9550 TYPE_FIELDS (this_type) = (struct field *)
9551 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9552 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 9553 sizeof (struct field) * num_fields);
b8c9b27d 9554 xfree (fields);
c906108c
SS
9555 }
9556 if (unsigned_enum)
876cecd0 9557 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
9558 if (flag_enum)
9559 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 9560 }
134d01f1 9561
6c83ed52
TT
9562 /* If we are reading an enum from a .debug_types unit, and the enum
9563 is a declaration, and the enum is not the signatured type in the
9564 unit, then we do not want to add a symbol for it. Adding a
9565 symbol would in some cases obscure the true definition of the
9566 enum, giving users an incomplete type when the definition is
9567 actually available. Note that we do not want to do this for all
9568 enums which are just declarations, because C++0x allows forward
9569 enum declarations. */
3019eac3 9570 if (cu->per_cu->is_debug_types
6c83ed52
TT
9571 && die_is_declaration (die, cu))
9572 {
52dc124a 9573 struct signatured_type *sig_type;
6c83ed52 9574
52dc124a 9575 sig_type
6c83ed52 9576 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 9577 cu->per_cu->info_or_types_section,
6c83ed52 9578 cu->per_cu->offset);
3019eac3
DE
9579 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9580 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
9581 return;
9582 }
9583
f792889a 9584 new_symbol (die, this_type, cu);
c906108c
SS
9585}
9586
9587/* Extract all information from a DW_TAG_array_type DIE and put it in
9588 the DIE's type field. For now, this only handles one dimensional
9589 arrays. */
9590
f792889a 9591static struct type *
e7c27a73 9592read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9593{
e7c27a73 9594 struct objfile *objfile = cu->objfile;
c906108c 9595 struct die_info *child_die;
7e314c57 9596 struct type *type;
c906108c
SS
9597 struct type *element_type, *range_type, *index_type;
9598 struct type **range_types = NULL;
9599 struct attribute *attr;
9600 int ndim = 0;
9601 struct cleanup *back_to;
39cbfefa 9602 char *name;
c906108c 9603
e7c27a73 9604 element_type = die_type (die, cu);
c906108c 9605
7e314c57
JK
9606 /* The die_type call above may have already set the type for this DIE. */
9607 type = get_die_type (die, cu);
9608 if (type)
9609 return type;
9610
c906108c
SS
9611 /* Irix 6.2 native cc creates array types without children for
9612 arrays with unspecified length. */
639d11d3 9613 if (die->child == NULL)
c906108c 9614 {
46bf5051 9615 index_type = objfile_type (objfile)->builtin_int;
c906108c 9616 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
9617 type = create_array_type (NULL, element_type, range_type);
9618 return set_die_type (die, type, cu);
c906108c
SS
9619 }
9620
9621 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 9622 child_die = die->child;
c906108c
SS
9623 while (child_die && child_die->tag)
9624 {
9625 if (child_die->tag == DW_TAG_subrange_type)
9626 {
f792889a 9627 struct type *child_type = read_type_die (child_die, cu);
9a619af0 9628
f792889a 9629 if (child_type != NULL)
a02abb62 9630 {
0963b4bd
MS
9631 /* The range type was succesfully read. Save it for the
9632 array type creation. */
a02abb62
JB
9633 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9634 {
9635 range_types = (struct type **)
9636 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9637 * sizeof (struct type *));
9638 if (ndim == 0)
9639 make_cleanup (free_current_contents, &range_types);
9640 }
f792889a 9641 range_types[ndim++] = child_type;
a02abb62 9642 }
c906108c
SS
9643 }
9644 child_die = sibling_die (child_die);
9645 }
9646
9647 /* Dwarf2 dimensions are output from left to right, create the
9648 necessary array types in backwards order. */
7ca2d3a3 9649
c906108c 9650 type = element_type;
7ca2d3a3
DL
9651
9652 if (read_array_order (die, cu) == DW_ORD_col_major)
9653 {
9654 int i = 0;
9a619af0 9655
7ca2d3a3
DL
9656 while (i < ndim)
9657 type = create_array_type (NULL, type, range_types[i++]);
9658 }
9659 else
9660 {
9661 while (ndim-- > 0)
9662 type = create_array_type (NULL, type, range_types[ndim]);
9663 }
c906108c 9664
f5f8a009
EZ
9665 /* Understand Dwarf2 support for vector types (like they occur on
9666 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9667 array type. This is not part of the Dwarf2/3 standard yet, but a
9668 custom vendor extension. The main difference between a regular
9669 array and the vector variant is that vectors are passed by value
9670 to functions. */
e142c38c 9671 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 9672 if (attr)
ea37ba09 9673 make_vector_type (type);
f5f8a009 9674
dbc98a8b
KW
9675 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9676 implementation may choose to implement triple vectors using this
9677 attribute. */
9678 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9679 if (attr)
9680 {
9681 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9682 TYPE_LENGTH (type) = DW_UNSND (attr);
9683 else
3e43a32a
MS
9684 complaint (&symfile_complaints,
9685 _("DW_AT_byte_size for array type smaller "
9686 "than the total size of elements"));
dbc98a8b
KW
9687 }
9688
39cbfefa
DJ
9689 name = dwarf2_name (die, cu);
9690 if (name)
9691 TYPE_NAME (type) = name;
6e70227d 9692
0963b4bd 9693 /* Install the type in the die. */
7e314c57
JK
9694 set_die_type (die, type, cu);
9695
9696 /* set_die_type should be already done. */
b4ba55a1
JB
9697 set_descriptive_type (type, die, cu);
9698
c906108c
SS
9699 do_cleanups (back_to);
9700
7e314c57 9701 return type;
c906108c
SS
9702}
9703
7ca2d3a3 9704static enum dwarf_array_dim_ordering
6e70227d 9705read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
9706{
9707 struct attribute *attr;
9708
9709 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9710
9711 if (attr) return DW_SND (attr);
9712
0963b4bd
MS
9713 /* GNU F77 is a special case, as at 08/2004 array type info is the
9714 opposite order to the dwarf2 specification, but data is still
9715 laid out as per normal fortran.
7ca2d3a3 9716
0963b4bd
MS
9717 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9718 version checking. */
7ca2d3a3 9719
905e0470
PM
9720 if (cu->language == language_fortran
9721 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
9722 {
9723 return DW_ORD_row_major;
9724 }
9725
6e70227d 9726 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
9727 {
9728 case array_column_major:
9729 return DW_ORD_col_major;
9730 case array_row_major:
9731 default:
9732 return DW_ORD_row_major;
9733 };
9734}
9735
72019c9c 9736/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 9737 the DIE's type field. */
72019c9c 9738
f792889a 9739static struct type *
72019c9c
GM
9740read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9741{
7e314c57
JK
9742 struct type *domain_type, *set_type;
9743 struct attribute *attr;
f792889a 9744
7e314c57
JK
9745 domain_type = die_type (die, cu);
9746
9747 /* The die_type call above may have already set the type for this DIE. */
9748 set_type = get_die_type (die, cu);
9749 if (set_type)
9750 return set_type;
9751
9752 set_type = create_set_type (NULL, domain_type);
9753
9754 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
9755 if (attr)
9756 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 9757
f792889a 9758 return set_die_type (die, set_type, cu);
72019c9c 9759}
7ca2d3a3 9760
c906108c
SS
9761/* First cut: install each common block member as a global variable. */
9762
9763static void
e7c27a73 9764read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
9765{
9766 struct die_info *child_die;
9767 struct attribute *attr;
9768 struct symbol *sym;
9769 CORE_ADDR base = (CORE_ADDR) 0;
9770
e142c38c 9771 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
9772 if (attr)
9773 {
0963b4bd 9774 /* Support the .debug_loc offsets. */
8e19ed76
PS
9775 if (attr_form_is_block (attr))
9776 {
e7c27a73 9777 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 9778 }
3690dd37 9779 else if (attr_form_is_section_offset (attr))
8e19ed76 9780 {
4d3c2250 9781 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9782 }
9783 else
9784 {
4d3c2250
KB
9785 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9786 "common block member");
8e19ed76 9787 }
c906108c 9788 }
639d11d3 9789 if (die->child != NULL)
c906108c 9790 {
639d11d3 9791 child_die = die->child;
c906108c
SS
9792 while (child_die && child_die->tag)
9793 {
74ac6d43
TT
9794 LONGEST offset;
9795
e7c27a73 9796 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
9797 if (sym != NULL
9798 && handle_data_member_location (child_die, cu, &offset))
c906108c 9799 {
74ac6d43 9800 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
9801 add_symbol_to_list (sym, &global_symbols);
9802 }
9803 child_die = sibling_die (child_die);
9804 }
9805 }
9806}
9807
0114d602 9808/* Create a type for a C++ namespace. */
d9fa45fe 9809
0114d602
DJ
9810static struct type *
9811read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 9812{
e7c27a73 9813 struct objfile *objfile = cu->objfile;
0114d602 9814 const char *previous_prefix, *name;
9219021c 9815 int is_anonymous;
0114d602
DJ
9816 struct type *type;
9817
9818 /* For extensions, reuse the type of the original namespace. */
9819 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9820 {
9821 struct die_info *ext_die;
9822 struct dwarf2_cu *ext_cu = cu;
9a619af0 9823
0114d602
DJ
9824 ext_die = dwarf2_extension (die, &ext_cu);
9825 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
9826
9827 /* EXT_CU may not be the same as CU.
9828 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
9829 return set_die_type (die, type, cu);
9830 }
9219021c 9831
e142c38c 9832 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
9833
9834 /* Now build the name of the current namespace. */
9835
0114d602
DJ
9836 previous_prefix = determine_prefix (die, cu);
9837 if (previous_prefix[0] != '\0')
9838 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 9839 previous_prefix, name, 0, cu);
0114d602
DJ
9840
9841 /* Create the type. */
9842 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
9843 objfile);
9844 TYPE_NAME (type) = (char *) name;
9845 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9846
60531b24 9847 return set_die_type (die, type, cu);
0114d602
DJ
9848}
9849
9850/* Read a C++ namespace. */
9851
9852static void
9853read_namespace (struct die_info *die, struct dwarf2_cu *cu)
9854{
9855 struct objfile *objfile = cu->objfile;
0114d602 9856 int is_anonymous;
9219021c 9857
5c4e30ca
DC
9858 /* Add a symbol associated to this if we haven't seen the namespace
9859 before. Also, add a using directive if it's an anonymous
9860 namespace. */
9219021c 9861
f2f0e013 9862 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
9863 {
9864 struct type *type;
9865
0114d602 9866 type = read_type_die (die, cu);
e7c27a73 9867 new_symbol (die, type, cu);
5c4e30ca 9868
e8e80198 9869 namespace_name (die, &is_anonymous, cu);
5c4e30ca 9870 if (is_anonymous)
0114d602
DJ
9871 {
9872 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 9873
c0cc3a76 9874 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 9875 NULL, NULL, &objfile->objfile_obstack);
0114d602 9876 }
5c4e30ca 9877 }
9219021c 9878
639d11d3 9879 if (die->child != NULL)
d9fa45fe 9880 {
639d11d3 9881 struct die_info *child_die = die->child;
6e70227d 9882
d9fa45fe
DC
9883 while (child_die && child_die->tag)
9884 {
e7c27a73 9885 process_die (child_die, cu);
d9fa45fe
DC
9886 child_die = sibling_die (child_die);
9887 }
9888 }
38d518c9
EZ
9889}
9890
f55ee35c
JK
9891/* Read a Fortran module as type. This DIE can be only a declaration used for
9892 imported module. Still we need that type as local Fortran "use ... only"
9893 declaration imports depend on the created type in determine_prefix. */
9894
9895static struct type *
9896read_module_type (struct die_info *die, struct dwarf2_cu *cu)
9897{
9898 struct objfile *objfile = cu->objfile;
9899 char *module_name;
9900 struct type *type;
9901
9902 module_name = dwarf2_name (die, cu);
9903 if (!module_name)
3e43a32a
MS
9904 complaint (&symfile_complaints,
9905 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 9906 die->offset.sect_off);
f55ee35c
JK
9907 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
9908
9909 /* determine_prefix uses TYPE_TAG_NAME. */
9910 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9911
9912 return set_die_type (die, type, cu);
9913}
9914
5d7cb8df
JK
9915/* Read a Fortran module. */
9916
9917static void
9918read_module (struct die_info *die, struct dwarf2_cu *cu)
9919{
9920 struct die_info *child_die = die->child;
9921
5d7cb8df
JK
9922 while (child_die && child_die->tag)
9923 {
9924 process_die (child_die, cu);
9925 child_die = sibling_die (child_die);
9926 }
9927}
9928
38d518c9
EZ
9929/* Return the name of the namespace represented by DIE. Set
9930 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
9931 namespace. */
9932
9933static const char *
e142c38c 9934namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
9935{
9936 struct die_info *current_die;
9937 const char *name = NULL;
9938
9939 /* Loop through the extensions until we find a name. */
9940
9941 for (current_die = die;
9942 current_die != NULL;
f2f0e013 9943 current_die = dwarf2_extension (die, &cu))
38d518c9 9944 {
e142c38c 9945 name = dwarf2_name (current_die, cu);
38d518c9
EZ
9946 if (name != NULL)
9947 break;
9948 }
9949
9950 /* Is it an anonymous namespace? */
9951
9952 *is_anonymous = (name == NULL);
9953 if (*is_anonymous)
2b1dbab0 9954 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
9955
9956 return name;
d9fa45fe
DC
9957}
9958
c906108c
SS
9959/* Extract all information from a DW_TAG_pointer_type DIE and add to
9960 the user defined type vector. */
9961
f792889a 9962static struct type *
e7c27a73 9963read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9964{
5e2b427d 9965 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 9966 struct comp_unit_head *cu_header = &cu->header;
c906108c 9967 struct type *type;
8b2dbe47
KB
9968 struct attribute *attr_byte_size;
9969 struct attribute *attr_address_class;
9970 int byte_size, addr_class;
7e314c57
JK
9971 struct type *target_type;
9972
9973 target_type = die_type (die, cu);
c906108c 9974
7e314c57
JK
9975 /* The die_type call above may have already set the type for this DIE. */
9976 type = get_die_type (die, cu);
9977 if (type)
9978 return type;
9979
9980 type = lookup_pointer_type (target_type);
8b2dbe47 9981
e142c38c 9982 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
9983 if (attr_byte_size)
9984 byte_size = DW_UNSND (attr_byte_size);
c906108c 9985 else
8b2dbe47
KB
9986 byte_size = cu_header->addr_size;
9987
e142c38c 9988 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
9989 if (attr_address_class)
9990 addr_class = DW_UNSND (attr_address_class);
9991 else
9992 addr_class = DW_ADDR_none;
9993
9994 /* If the pointer size or address class is different than the
9995 default, create a type variant marked as such and set the
9996 length accordingly. */
9997 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 9998 {
5e2b427d 9999 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
10000 {
10001 int type_flags;
10002
849957d9 10003 type_flags = gdbarch_address_class_type_flags
5e2b427d 10004 (gdbarch, byte_size, addr_class);
876cecd0
TT
10005 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
10006 == 0);
8b2dbe47
KB
10007 type = make_type_with_address_space (type, type_flags);
10008 }
10009 else if (TYPE_LENGTH (type) != byte_size)
10010 {
3e43a32a
MS
10011 complaint (&symfile_complaints,
10012 _("invalid pointer size %d"), byte_size);
8b2dbe47 10013 }
6e70227d 10014 else
9a619af0
MS
10015 {
10016 /* Should we also complain about unhandled address classes? */
10017 }
c906108c 10018 }
8b2dbe47
KB
10019
10020 TYPE_LENGTH (type) = byte_size;
f792889a 10021 return set_die_type (die, type, cu);
c906108c
SS
10022}
10023
10024/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
10025 the user defined type vector. */
10026
f792889a 10027static struct type *
e7c27a73 10028read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
10029{
10030 struct type *type;
10031 struct type *to_type;
10032 struct type *domain;
10033
e7c27a73
DJ
10034 to_type = die_type (die, cu);
10035 domain = die_containing_type (die, cu);
0d5de010 10036
7e314c57
JK
10037 /* The calls above may have already set the type for this DIE. */
10038 type = get_die_type (die, cu);
10039 if (type)
10040 return type;
10041
0d5de010
DJ
10042 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
10043 type = lookup_methodptr_type (to_type);
10044 else
10045 type = lookup_memberptr_type (to_type, domain);
c906108c 10046
f792889a 10047 return set_die_type (die, type, cu);
c906108c
SS
10048}
10049
10050/* Extract all information from a DW_TAG_reference_type DIE and add to
10051 the user defined type vector. */
10052
f792889a 10053static struct type *
e7c27a73 10054read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10055{
e7c27a73 10056 struct comp_unit_head *cu_header = &cu->header;
7e314c57 10057 struct type *type, *target_type;
c906108c
SS
10058 struct attribute *attr;
10059
7e314c57
JK
10060 target_type = die_type (die, cu);
10061
10062 /* The die_type call above may have already set the type for this DIE. */
10063 type = get_die_type (die, cu);
10064 if (type)
10065 return type;
10066
10067 type = lookup_reference_type (target_type);
e142c38c 10068 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10069 if (attr)
10070 {
10071 TYPE_LENGTH (type) = DW_UNSND (attr);
10072 }
10073 else
10074 {
107d2387 10075 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 10076 }
f792889a 10077 return set_die_type (die, type, cu);
c906108c
SS
10078}
10079
f792889a 10080static struct type *
e7c27a73 10081read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10082{
f792889a 10083 struct type *base_type, *cv_type;
c906108c 10084
e7c27a73 10085 base_type = die_type (die, cu);
7e314c57
JK
10086
10087 /* The die_type call above may have already set the type for this DIE. */
10088 cv_type = get_die_type (die, cu);
10089 if (cv_type)
10090 return cv_type;
10091
2f608a3a
KW
10092 /* In case the const qualifier is applied to an array type, the element type
10093 is so qualified, not the array type (section 6.7.3 of C99). */
10094 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
10095 {
10096 struct type *el_type, *inner_array;
10097
10098 base_type = copy_type (base_type);
10099 inner_array = base_type;
10100
10101 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10102 {
10103 TYPE_TARGET_TYPE (inner_array) =
10104 copy_type (TYPE_TARGET_TYPE (inner_array));
10105 inner_array = TYPE_TARGET_TYPE (inner_array);
10106 }
10107
10108 el_type = TYPE_TARGET_TYPE (inner_array);
10109 TYPE_TARGET_TYPE (inner_array) =
10110 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10111
10112 return set_die_type (die, base_type, cu);
10113 }
10114
f792889a
DJ
10115 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10116 return set_die_type (die, cv_type, cu);
c906108c
SS
10117}
10118
f792889a 10119static struct type *
e7c27a73 10120read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10121{
f792889a 10122 struct type *base_type, *cv_type;
c906108c 10123
e7c27a73 10124 base_type = die_type (die, cu);
7e314c57
JK
10125
10126 /* The die_type call above may have already set the type for this DIE. */
10127 cv_type = get_die_type (die, cu);
10128 if (cv_type)
10129 return cv_type;
10130
f792889a
DJ
10131 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10132 return set_die_type (die, cv_type, cu);
c906108c
SS
10133}
10134
10135/* Extract all information from a DW_TAG_string_type DIE and add to
10136 the user defined type vector. It isn't really a user defined type,
10137 but it behaves like one, with other DIE's using an AT_user_def_type
10138 attribute to reference it. */
10139
f792889a 10140static struct type *
e7c27a73 10141read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10142{
e7c27a73 10143 struct objfile *objfile = cu->objfile;
3b7538c0 10144 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10145 struct type *type, *range_type, *index_type, *char_type;
10146 struct attribute *attr;
10147 unsigned int length;
10148
e142c38c 10149 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
10150 if (attr)
10151 {
10152 length = DW_UNSND (attr);
10153 }
10154 else
10155 {
0963b4bd 10156 /* Check for the DW_AT_byte_size attribute. */
e142c38c 10157 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
10158 if (attr)
10159 {
10160 length = DW_UNSND (attr);
10161 }
10162 else
10163 {
10164 length = 1;
10165 }
c906108c 10166 }
6ccb9162 10167
46bf5051 10168 index_type = objfile_type (objfile)->builtin_int;
c906108c 10169 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
10170 char_type = language_string_char_type (cu->language_defn, gdbarch);
10171 type = create_string_type (NULL, char_type, range_type);
6ccb9162 10172
f792889a 10173 return set_die_type (die, type, cu);
c906108c
SS
10174}
10175
10176/* Handle DIES due to C code like:
10177
10178 struct foo
c5aa993b
JM
10179 {
10180 int (*funcp)(int a, long l);
10181 int b;
10182 };
c906108c 10183
0963b4bd 10184 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 10185
f792889a 10186static struct type *
e7c27a73 10187read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10188{
bb5ed363 10189 struct objfile *objfile = cu->objfile;
0963b4bd
MS
10190 struct type *type; /* Type that this function returns. */
10191 struct type *ftype; /* Function that returns above type. */
c906108c
SS
10192 struct attribute *attr;
10193
e7c27a73 10194 type = die_type (die, cu);
7e314c57
JK
10195
10196 /* The die_type call above may have already set the type for this DIE. */
10197 ftype = get_die_type (die, cu);
10198 if (ftype)
10199 return ftype;
10200
0c8b41f1 10201 ftype = lookup_function_type (type);
c906108c 10202
5b8101ae 10203 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 10204 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 10205 if ((attr && (DW_UNSND (attr) != 0))
987504bb 10206 || cu->language == language_cplus
5b8101ae
PM
10207 || cu->language == language_java
10208 || cu->language == language_pascal)
876cecd0 10209 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
10210 else if (producer_is_realview (cu->producer))
10211 /* RealView does not emit DW_AT_prototyped. We can not
10212 distinguish prototyped and unprototyped functions; default to
10213 prototyped, since that is more common in modern code (and
10214 RealView warns about unprototyped functions). */
10215 TYPE_PROTOTYPED (ftype) = 1;
c906108c 10216
c055b101
CV
10217 /* Store the calling convention in the type if it's available in
10218 the subroutine die. Otherwise set the calling convention to
10219 the default value DW_CC_normal. */
10220 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
10221 if (attr)
10222 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10223 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10224 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10225 else
10226 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
10227
10228 /* We need to add the subroutine type to the die immediately so
10229 we don't infinitely recurse when dealing with parameters
0963b4bd 10230 declared as the same subroutine type. */
76c10ea2 10231 set_die_type (die, ftype, cu);
6e70227d 10232
639d11d3 10233 if (die->child != NULL)
c906108c 10234 {
bb5ed363 10235 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 10236 struct die_info *child_die;
8072405b 10237 int nparams, iparams;
c906108c
SS
10238
10239 /* Count the number of parameters.
10240 FIXME: GDB currently ignores vararg functions, but knows about
10241 vararg member functions. */
8072405b 10242 nparams = 0;
639d11d3 10243 child_die = die->child;
c906108c
SS
10244 while (child_die && child_die->tag)
10245 {
10246 if (child_die->tag == DW_TAG_formal_parameter)
10247 nparams++;
10248 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 10249 TYPE_VARARGS (ftype) = 1;
c906108c
SS
10250 child_die = sibling_die (child_die);
10251 }
10252
10253 /* Allocate storage for parameters and fill them in. */
10254 TYPE_NFIELDS (ftype) = nparams;
10255 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 10256 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 10257
8072405b
JK
10258 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10259 even if we error out during the parameters reading below. */
10260 for (iparams = 0; iparams < nparams; iparams++)
10261 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10262
10263 iparams = 0;
639d11d3 10264 child_die = die->child;
c906108c
SS
10265 while (child_die && child_die->tag)
10266 {
10267 if (child_die->tag == DW_TAG_formal_parameter)
10268 {
3ce3b1ba
PA
10269 struct type *arg_type;
10270
10271 /* DWARF version 2 has no clean way to discern C++
10272 static and non-static member functions. G++ helps
10273 GDB by marking the first parameter for non-static
10274 member functions (which is the this pointer) as
10275 artificial. We pass this information to
10276 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10277
10278 DWARF version 3 added DW_AT_object_pointer, which GCC
10279 4.5 does not yet generate. */
e142c38c 10280 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
10281 if (attr)
10282 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10283 else
418835cc
KS
10284 {
10285 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10286
10287 /* GCC/43521: In java, the formal parameter
10288 "this" is sometimes not marked with DW_AT_artificial. */
10289 if (cu->language == language_java)
10290 {
10291 const char *name = dwarf2_name (child_die, cu);
9a619af0 10292
418835cc
KS
10293 if (name && !strcmp (name, "this"))
10294 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10295 }
10296 }
3ce3b1ba
PA
10297 arg_type = die_type (child_die, cu);
10298
10299 /* RealView does not mark THIS as const, which the testsuite
10300 expects. GCC marks THIS as const in method definitions,
10301 but not in the class specifications (GCC PR 43053). */
10302 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10303 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10304 {
10305 int is_this = 0;
10306 struct dwarf2_cu *arg_cu = cu;
10307 const char *name = dwarf2_name (child_die, cu);
10308
10309 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10310 if (attr)
10311 {
10312 /* If the compiler emits this, use it. */
10313 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10314 is_this = 1;
10315 }
10316 else if (name && strcmp (name, "this") == 0)
10317 /* Function definitions will have the argument names. */
10318 is_this = 1;
10319 else if (name == NULL && iparams == 0)
10320 /* Declarations may not have the names, so like
10321 elsewhere in GDB, assume an artificial first
10322 argument is "this". */
10323 is_this = 1;
10324
10325 if (is_this)
10326 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10327 arg_type, 0);
10328 }
10329
10330 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
10331 iparams++;
10332 }
10333 child_die = sibling_die (child_die);
10334 }
10335 }
10336
76c10ea2 10337 return ftype;
c906108c
SS
10338}
10339
f792889a 10340static struct type *
e7c27a73 10341read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10342{
e7c27a73 10343 struct objfile *objfile = cu->objfile;
0114d602 10344 const char *name = NULL;
3c8e0968 10345 struct type *this_type, *target_type;
c906108c 10346
94af9270 10347 name = dwarf2_full_name (NULL, die, cu);
f792889a 10348 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
10349 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10350 TYPE_NAME (this_type) = (char *) name;
f792889a 10351 set_die_type (die, this_type, cu);
3c8e0968
DE
10352 target_type = die_type (die, cu);
10353 if (target_type != this_type)
10354 TYPE_TARGET_TYPE (this_type) = target_type;
10355 else
10356 {
10357 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10358 spec and cause infinite loops in GDB. */
10359 complaint (&symfile_complaints,
10360 _("Self-referential DW_TAG_typedef "
10361 "- DIE at 0x%x [in module %s]"),
b64f50a1 10362 die->offset.sect_off, objfile->name);
3c8e0968
DE
10363 TYPE_TARGET_TYPE (this_type) = NULL;
10364 }
f792889a 10365 return this_type;
c906108c
SS
10366}
10367
10368/* Find a representation of a given base type and install
10369 it in the TYPE field of the die. */
10370
f792889a 10371static struct type *
e7c27a73 10372read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10373{
e7c27a73 10374 struct objfile *objfile = cu->objfile;
c906108c
SS
10375 struct type *type;
10376 struct attribute *attr;
10377 int encoding = 0, size = 0;
39cbfefa 10378 char *name;
6ccb9162
UW
10379 enum type_code code = TYPE_CODE_INT;
10380 int type_flags = 0;
10381 struct type *target_type = NULL;
c906108c 10382
e142c38c 10383 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
10384 if (attr)
10385 {
10386 encoding = DW_UNSND (attr);
10387 }
e142c38c 10388 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10389 if (attr)
10390 {
10391 size = DW_UNSND (attr);
10392 }
39cbfefa 10393 name = dwarf2_name (die, cu);
6ccb9162 10394 if (!name)
c906108c 10395 {
6ccb9162
UW
10396 complaint (&symfile_complaints,
10397 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 10398 }
6ccb9162
UW
10399
10400 switch (encoding)
c906108c 10401 {
6ccb9162
UW
10402 case DW_ATE_address:
10403 /* Turn DW_ATE_address into a void * pointer. */
10404 code = TYPE_CODE_PTR;
10405 type_flags |= TYPE_FLAG_UNSIGNED;
10406 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10407 break;
10408 case DW_ATE_boolean:
10409 code = TYPE_CODE_BOOL;
10410 type_flags |= TYPE_FLAG_UNSIGNED;
10411 break;
10412 case DW_ATE_complex_float:
10413 code = TYPE_CODE_COMPLEX;
10414 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10415 break;
10416 case DW_ATE_decimal_float:
10417 code = TYPE_CODE_DECFLOAT;
10418 break;
10419 case DW_ATE_float:
10420 code = TYPE_CODE_FLT;
10421 break;
10422 case DW_ATE_signed:
10423 break;
10424 case DW_ATE_unsigned:
10425 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
10426 if (cu->language == language_fortran
10427 && name
10428 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10429 code = TYPE_CODE_CHAR;
6ccb9162
UW
10430 break;
10431 case DW_ATE_signed_char:
6e70227d 10432 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10433 || cu->language == language_pascal
10434 || cu->language == language_fortran)
6ccb9162
UW
10435 code = TYPE_CODE_CHAR;
10436 break;
10437 case DW_ATE_unsigned_char:
868a0084 10438 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10439 || cu->language == language_pascal
10440 || cu->language == language_fortran)
6ccb9162
UW
10441 code = TYPE_CODE_CHAR;
10442 type_flags |= TYPE_FLAG_UNSIGNED;
10443 break;
75079b2b
TT
10444 case DW_ATE_UTF:
10445 /* We just treat this as an integer and then recognize the
10446 type by name elsewhere. */
10447 break;
10448
6ccb9162
UW
10449 default:
10450 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10451 dwarf_type_encoding_name (encoding));
10452 break;
c906108c 10453 }
6ccb9162 10454
0114d602
DJ
10455 type = init_type (code, size, type_flags, NULL, objfile);
10456 TYPE_NAME (type) = name;
6ccb9162
UW
10457 TYPE_TARGET_TYPE (type) = target_type;
10458
0114d602 10459 if (name && strcmp (name, "char") == 0)
876cecd0 10460 TYPE_NOSIGN (type) = 1;
0114d602 10461
f792889a 10462 return set_die_type (die, type, cu);
c906108c
SS
10463}
10464
a02abb62
JB
10465/* Read the given DW_AT_subrange DIE. */
10466
f792889a 10467static struct type *
a02abb62
JB
10468read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10469{
10470 struct type *base_type;
10471 struct type *range_type;
10472 struct attribute *attr;
4fae6e18
JK
10473 LONGEST low, high;
10474 int low_default_is_valid;
39cbfefa 10475 char *name;
43bbcdc2 10476 LONGEST negative_mask;
e77813c8 10477
a02abb62 10478 base_type = die_type (die, cu);
953ac07e
JK
10479 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10480 check_typedef (base_type);
a02abb62 10481
7e314c57
JK
10482 /* The die_type call above may have already set the type for this DIE. */
10483 range_type = get_die_type (die, cu);
10484 if (range_type)
10485 return range_type;
10486
4fae6e18
JK
10487 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10488 omitting DW_AT_lower_bound. */
10489 switch (cu->language)
6e70227d 10490 {
4fae6e18
JK
10491 case language_c:
10492 case language_cplus:
10493 low = 0;
10494 low_default_is_valid = 1;
10495 break;
10496 case language_fortran:
10497 low = 1;
10498 low_default_is_valid = 1;
10499 break;
10500 case language_d:
10501 case language_java:
10502 case language_objc:
10503 low = 0;
10504 low_default_is_valid = (cu->header.version >= 4);
10505 break;
10506 case language_ada:
10507 case language_m2:
10508 case language_pascal:
a02abb62 10509 low = 1;
4fae6e18
JK
10510 low_default_is_valid = (cu->header.version >= 4);
10511 break;
10512 default:
10513 low = 0;
10514 low_default_is_valid = 0;
10515 break;
a02abb62
JB
10516 }
10517
dd5e6932
DJ
10518 /* FIXME: For variable sized arrays either of these could be
10519 a variable rather than a constant value. We'll allow it,
10520 but we don't know how to handle it. */
e142c38c 10521 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 10522 if (attr)
4fae6e18
JK
10523 low = dwarf2_get_attr_constant_value (attr, low);
10524 else if (!low_default_is_valid)
10525 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10526 "- DIE at 0x%x [in module %s]"),
10527 die->offset.sect_off, cu->objfile->name);
a02abb62 10528
e142c38c 10529 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 10530 if (attr)
6e70227d 10531 {
d48323d8 10532 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
10533 {
10534 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 10535 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
10536 FIXME: GDB does not yet know how to handle dynamic
10537 arrays properly, treat them as arrays with unspecified
10538 length for now.
10539
10540 FIXME: jimb/2003-09-22: GDB does not really know
10541 how to handle arrays of unspecified length
10542 either; we just represent them as zero-length
10543 arrays. Choose an appropriate upper bound given
10544 the lower bound we've computed above. */
10545 high = low - 1;
10546 }
10547 else
10548 high = dwarf2_get_attr_constant_value (attr, 1);
10549 }
e77813c8
PM
10550 else
10551 {
10552 attr = dwarf2_attr (die, DW_AT_count, cu);
10553 if (attr)
10554 {
10555 int count = dwarf2_get_attr_constant_value (attr, 1);
10556 high = low + count - 1;
10557 }
c2ff108b
JK
10558 else
10559 {
10560 /* Unspecified array length. */
10561 high = low - 1;
10562 }
e77813c8
PM
10563 }
10564
10565 /* Dwarf-2 specifications explicitly allows to create subrange types
10566 without specifying a base type.
10567 In that case, the base type must be set to the type of
10568 the lower bound, upper bound or count, in that order, if any of these
10569 three attributes references an object that has a type.
10570 If no base type is found, the Dwarf-2 specifications say that
10571 a signed integer type of size equal to the size of an address should
10572 be used.
10573 For the following C code: `extern char gdb_int [];'
10574 GCC produces an empty range DIE.
10575 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 10576 high bound or count are not yet handled by this code. */
e77813c8
PM
10577 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10578 {
10579 struct objfile *objfile = cu->objfile;
10580 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10581 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10582 struct type *int_type = objfile_type (objfile)->builtin_int;
10583
10584 /* Test "int", "long int", and "long long int" objfile types,
10585 and select the first one having a size above or equal to the
10586 architecture address size. */
10587 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10588 base_type = int_type;
10589 else
10590 {
10591 int_type = objfile_type (objfile)->builtin_long;
10592 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10593 base_type = int_type;
10594 else
10595 {
10596 int_type = objfile_type (objfile)->builtin_long_long;
10597 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10598 base_type = int_type;
10599 }
10600 }
10601 }
a02abb62 10602
6e70227d 10603 negative_mask =
43bbcdc2
PH
10604 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10605 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10606 low |= negative_mask;
10607 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10608 high |= negative_mask;
10609
a02abb62
JB
10610 range_type = create_range_type (NULL, base_type, low, high);
10611
bbb0eef6
JK
10612 /* Mark arrays with dynamic length at least as an array of unspecified
10613 length. GDB could check the boundary but before it gets implemented at
10614 least allow accessing the array elements. */
d48323d8 10615 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
10616 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10617
c2ff108b
JK
10618 /* Ada expects an empty array on no boundary attributes. */
10619 if (attr == NULL && cu->language != language_ada)
10620 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10621
39cbfefa
DJ
10622 name = dwarf2_name (die, cu);
10623 if (name)
10624 TYPE_NAME (range_type) = name;
6e70227d 10625
e142c38c 10626 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
10627 if (attr)
10628 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10629
7e314c57
JK
10630 set_die_type (die, range_type, cu);
10631
10632 /* set_die_type should be already done. */
b4ba55a1
JB
10633 set_descriptive_type (range_type, die, cu);
10634
7e314c57 10635 return range_type;
a02abb62 10636}
6e70227d 10637
f792889a 10638static struct type *
81a17f79
JB
10639read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10640{
10641 struct type *type;
81a17f79 10642
81a17f79
JB
10643 /* For now, we only support the C meaning of an unspecified type: void. */
10644
0114d602
DJ
10645 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10646 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 10647
f792889a 10648 return set_die_type (die, type, cu);
81a17f79 10649}
a02abb62 10650
639d11d3
DC
10651/* Read a single die and all its descendents. Set the die's sibling
10652 field to NULL; set other fields in the die correctly, and set all
10653 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10654 location of the info_ptr after reading all of those dies. PARENT
10655 is the parent of the die in question. */
10656
10657static struct die_info *
dee91e82
DE
10658read_die_and_children (const struct die_reader_specs *reader,
10659 gdb_byte *info_ptr,
10660 gdb_byte **new_info_ptr,
10661 struct die_info *parent)
639d11d3
DC
10662{
10663 struct die_info *die;
fe1b8b76 10664 gdb_byte *cur_ptr;
639d11d3
DC
10665 int has_children;
10666
93311388 10667 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
10668 if (die == NULL)
10669 {
10670 *new_info_ptr = cur_ptr;
10671 return NULL;
10672 }
93311388 10673 store_in_ref_table (die, reader->cu);
639d11d3
DC
10674
10675 if (has_children)
348e048f 10676 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
10677 else
10678 {
10679 die->child = NULL;
10680 *new_info_ptr = cur_ptr;
10681 }
10682
10683 die->sibling = NULL;
10684 die->parent = parent;
10685 return die;
10686}
10687
10688/* Read a die, all of its descendents, and all of its siblings; set
10689 all of the fields of all of the dies correctly. Arguments are as
10690 in read_die_and_children. */
10691
10692static struct die_info *
93311388
DE
10693read_die_and_siblings (const struct die_reader_specs *reader,
10694 gdb_byte *info_ptr,
fe1b8b76 10695 gdb_byte **new_info_ptr,
639d11d3
DC
10696 struct die_info *parent)
10697{
10698 struct die_info *first_die, *last_sibling;
fe1b8b76 10699 gdb_byte *cur_ptr;
639d11d3 10700
c906108c 10701 cur_ptr = info_ptr;
639d11d3
DC
10702 first_die = last_sibling = NULL;
10703
10704 while (1)
c906108c 10705 {
639d11d3 10706 struct die_info *die
dee91e82 10707 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 10708
1d325ec1 10709 if (die == NULL)
c906108c 10710 {
639d11d3
DC
10711 *new_info_ptr = cur_ptr;
10712 return first_die;
c906108c 10713 }
1d325ec1
DJ
10714
10715 if (!first_die)
10716 first_die = die;
c906108c 10717 else
1d325ec1
DJ
10718 last_sibling->sibling = die;
10719
10720 last_sibling = die;
c906108c 10721 }
c906108c
SS
10722}
10723
3019eac3
DE
10724/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10725 attributes.
10726 The caller is responsible for filling in the extra attributes
10727 and updating (*DIEP)->num_attrs.
10728 Set DIEP to point to a newly allocated die with its information,
10729 except for its child, sibling, and parent fields.
10730 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
10731
10732static gdb_byte *
3019eac3
DE
10733read_full_die_1 (const struct die_reader_specs *reader,
10734 struct die_info **diep, gdb_byte *info_ptr,
10735 int *has_children, int num_extra_attrs)
93311388 10736{
b64f50a1
JK
10737 unsigned int abbrev_number, bytes_read, i;
10738 sect_offset offset;
93311388
DE
10739 struct abbrev_info *abbrev;
10740 struct die_info *die;
10741 struct dwarf2_cu *cu = reader->cu;
10742 bfd *abfd = reader->abfd;
10743
b64f50a1 10744 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
10745 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10746 info_ptr += bytes_read;
10747 if (!abbrev_number)
10748 {
10749 *diep = NULL;
10750 *has_children = 0;
10751 return info_ptr;
10752 }
10753
10754 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10755 if (!abbrev)
348e048f
DE
10756 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10757 abbrev_number,
10758 bfd_get_filename (abfd));
10759
3019eac3 10760 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
10761 die->offset = offset;
10762 die->tag = abbrev->tag;
10763 die->abbrev = abbrev_number;
10764
3019eac3
DE
10765 /* Make the result usable.
10766 The caller needs to update num_attrs after adding the extra
10767 attributes. */
93311388
DE
10768 die->num_attrs = abbrev->num_attrs;
10769
10770 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
10771 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10772 info_ptr);
93311388
DE
10773
10774 *diep = die;
10775 *has_children = abbrev->has_children;
10776 return info_ptr;
10777}
10778
3019eac3
DE
10779/* Read a die and all its attributes.
10780 Set DIEP to point to a newly allocated die with its information,
10781 except for its child, sibling, and parent fields.
10782 Set HAS_CHILDREN to tell whether the die has children or not. */
10783
10784static gdb_byte *
10785read_full_die (const struct die_reader_specs *reader,
10786 struct die_info **diep, gdb_byte *info_ptr,
10787 int *has_children)
10788{
10789 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10790}
10791
c906108c
SS
10792/* In DWARF version 2, the description of the debugging information is
10793 stored in a separate .debug_abbrev section. Before we read any
10794 dies from a section we read in all abbreviations and install them
72bf9492
DJ
10795 in a hash table. This function also sets flags in CU describing
10796 the data found in the abbrev table. */
c906108c
SS
10797
10798static void
dee91e82
DE
10799dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10800 struct dwarf2_section_info *abbrev_section)
10801
c906108c 10802{
dee91e82 10803 bfd *abfd = abbrev_section->asection->owner;
e7c27a73 10804 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 10805 gdb_byte *abbrev_ptr;
c906108c
SS
10806 struct abbrev_info *cur_abbrev;
10807 unsigned int abbrev_number, bytes_read, abbrev_name;
10808 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
10809 struct attr_abbrev *cur_attrs;
10810 unsigned int allocated_attrs;
c906108c 10811
0963b4bd 10812 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
10813 obstack_init (&cu->abbrev_obstack);
10814 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10815 (ABBREV_HASH_SIZE
10816 * sizeof (struct abbrev_info *)));
10817 memset (cu->dwarf2_abbrevs, 0,
10818 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 10819
dee91e82
DE
10820 dwarf2_read_section (cu->objfile, abbrev_section);
10821 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
c906108c
SS
10822 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10823 abbrev_ptr += bytes_read;
10824
f3dd6933
DJ
10825 allocated_attrs = ATTR_ALLOC_CHUNK;
10826 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 10827
0963b4bd 10828 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
10829 while (abbrev_number)
10830 {
f3dd6933 10831 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
10832
10833 /* read in abbrev header */
10834 cur_abbrev->number = abbrev_number;
10835 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10836 abbrev_ptr += bytes_read;
10837 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
10838 abbrev_ptr += 1;
10839
10840 /* now read in declarations */
10841 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10842 abbrev_ptr += bytes_read;
10843 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10844 abbrev_ptr += bytes_read;
10845 while (abbrev_name)
10846 {
f3dd6933 10847 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 10848 {
f3dd6933
DJ
10849 allocated_attrs += ATTR_ALLOC_CHUNK;
10850 cur_attrs
10851 = xrealloc (cur_attrs, (allocated_attrs
10852 * sizeof (struct attr_abbrev)));
c906108c 10853 }
ae038cb0 10854
f3dd6933
DJ
10855 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
10856 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
10857 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10858 abbrev_ptr += bytes_read;
10859 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10860 abbrev_ptr += bytes_read;
10861 }
10862
f3dd6933
DJ
10863 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
10864 (cur_abbrev->num_attrs
10865 * sizeof (struct attr_abbrev)));
10866 memcpy (cur_abbrev->attrs, cur_attrs,
10867 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
10868
c906108c 10869 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
10870 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
10871 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
10872
10873 /* Get next abbreviation.
10874 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
10875 always properly terminated with an abbrev number of 0.
10876 Exit loop if we encounter an abbreviation which we have
10877 already read (which means we are about to read the abbreviations
10878 for the next compile unit) or if the end of the abbreviation
10879 table is reached. */
dee91e82
DE
10880 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
10881 >= abbrev_section->size)
c906108c
SS
10882 break;
10883 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10884 abbrev_ptr += bytes_read;
e7c27a73 10885 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
10886 break;
10887 }
f3dd6933
DJ
10888
10889 xfree (cur_attrs);
c906108c
SS
10890}
10891
f3dd6933 10892/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 10893
c906108c 10894static void
f3dd6933 10895dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 10896{
f3dd6933 10897 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 10898
f3dd6933
DJ
10899 obstack_free (&cu->abbrev_obstack, NULL);
10900 cu->dwarf2_abbrevs = NULL;
c906108c
SS
10901}
10902
10903/* Lookup an abbrev_info structure in the abbrev hash table. */
10904
10905static struct abbrev_info *
e7c27a73 10906dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
10907{
10908 unsigned int hash_number;
10909 struct abbrev_info *abbrev;
10910
10911 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 10912 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
10913
10914 while (abbrev)
10915 {
10916 if (abbrev->number == number)
10917 return abbrev;
10918 else
10919 abbrev = abbrev->next;
10920 }
10921 return NULL;
10922}
10923
72bf9492
DJ
10924/* Returns nonzero if TAG represents a type that we might generate a partial
10925 symbol for. */
10926
10927static int
10928is_type_tag_for_partial (int tag)
10929{
10930 switch (tag)
10931 {
10932#if 0
10933 /* Some types that would be reasonable to generate partial symbols for,
10934 that we don't at present. */
10935 case DW_TAG_array_type:
10936 case DW_TAG_file_type:
10937 case DW_TAG_ptr_to_member_type:
10938 case DW_TAG_set_type:
10939 case DW_TAG_string_type:
10940 case DW_TAG_subroutine_type:
10941#endif
10942 case DW_TAG_base_type:
10943 case DW_TAG_class_type:
680b30c7 10944 case DW_TAG_interface_type:
72bf9492
DJ
10945 case DW_TAG_enumeration_type:
10946 case DW_TAG_structure_type:
10947 case DW_TAG_subrange_type:
10948 case DW_TAG_typedef:
10949 case DW_TAG_union_type:
10950 return 1;
10951 default:
10952 return 0;
10953 }
10954}
10955
10956/* Load all DIEs that are interesting for partial symbols into memory. */
10957
10958static struct partial_die_info *
dee91e82
DE
10959load_partial_dies (const struct die_reader_specs *reader,
10960 gdb_byte *info_ptr, int building_psymtab)
72bf9492 10961{
dee91e82 10962 struct dwarf2_cu *cu = reader->cu;
bb5ed363 10963 struct objfile *objfile = cu->objfile;
72bf9492
DJ
10964 struct partial_die_info *part_die;
10965 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
10966 struct abbrev_info *abbrev;
10967 unsigned int bytes_read;
5afb4e99 10968 unsigned int load_all = 0;
72bf9492
DJ
10969 int nesting_level = 1;
10970
10971 parent_die = NULL;
10972 last_die = NULL;
10973
7adf1e79
DE
10974 gdb_assert (cu->per_cu != NULL);
10975 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
10976 load_all = 1;
10977
72bf9492
DJ
10978 cu->partial_dies
10979 = htab_create_alloc_ex (cu->header.length / 12,
10980 partial_die_hash,
10981 partial_die_eq,
10982 NULL,
10983 &cu->comp_unit_obstack,
10984 hashtab_obstack_allocate,
10985 dummy_obstack_deallocate);
10986
10987 part_die = obstack_alloc (&cu->comp_unit_obstack,
10988 sizeof (struct partial_die_info));
10989
10990 while (1)
10991 {
10992 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
10993
10994 /* A NULL abbrev means the end of a series of children. */
10995 if (abbrev == NULL)
10996 {
10997 if (--nesting_level == 0)
10998 {
10999 /* PART_DIE was probably the last thing allocated on the
11000 comp_unit_obstack, so we could call obstack_free
11001 here. We don't do that because the waste is small,
11002 and will be cleaned up when we're done with this
11003 compilation unit. This way, we're also more robust
11004 against other users of the comp_unit_obstack. */
11005 return first_die;
11006 }
11007 info_ptr += bytes_read;
11008 last_die = parent_die;
11009 parent_die = parent_die->die_parent;
11010 continue;
11011 }
11012
98bfdba5
PA
11013 /* Check for template arguments. We never save these; if
11014 they're seen, we just mark the parent, and go on our way. */
11015 if (parent_die != NULL
11016 && cu->language == language_cplus
11017 && (abbrev->tag == DW_TAG_template_type_param
11018 || abbrev->tag == DW_TAG_template_value_param))
11019 {
11020 parent_die->has_template_arguments = 1;
11021
11022 if (!load_all)
11023 {
11024 /* We don't need a partial DIE for the template argument. */
dee91e82 11025 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
11026 continue;
11027 }
11028 }
11029
0d99eb77 11030 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
11031 Skip their other children. */
11032 if (!load_all
11033 && cu->language == language_cplus
11034 && parent_die != NULL
11035 && parent_die->tag == DW_TAG_subprogram)
11036 {
dee91e82 11037 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
11038 continue;
11039 }
11040
5afb4e99
DJ
11041 /* Check whether this DIE is interesting enough to save. Normally
11042 we would not be interested in members here, but there may be
11043 later variables referencing them via DW_AT_specification (for
11044 static members). */
11045 if (!load_all
11046 && !is_type_tag_for_partial (abbrev->tag)
72929c62 11047 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
11048 && abbrev->tag != DW_TAG_enumerator
11049 && abbrev->tag != DW_TAG_subprogram
bc30ff58 11050 && abbrev->tag != DW_TAG_lexical_block
72bf9492 11051 && abbrev->tag != DW_TAG_variable
5afb4e99 11052 && abbrev->tag != DW_TAG_namespace
f55ee35c 11053 && abbrev->tag != DW_TAG_module
95554aad
TT
11054 && abbrev->tag != DW_TAG_member
11055 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
11056 {
11057 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11058 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
11059 continue;
11060 }
11061
dee91e82
DE
11062 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
11063 info_ptr);
72bf9492
DJ
11064
11065 /* This two-pass algorithm for processing partial symbols has a
11066 high cost in cache pressure. Thus, handle some simple cases
11067 here which cover the majority of C partial symbols. DIEs
11068 which neither have specification tags in them, nor could have
11069 specification tags elsewhere pointing at them, can simply be
11070 processed and discarded.
11071
11072 This segment is also optional; scan_partial_symbols and
11073 add_partial_symbol will handle these DIEs if we chain
11074 them in normally. When compilers which do not emit large
11075 quantities of duplicate debug information are more common,
11076 this code can probably be removed. */
11077
11078 /* Any complete simple types at the top level (pretty much all
11079 of them, for a language without namespaces), can be processed
11080 directly. */
11081 if (parent_die == NULL
11082 && part_die->has_specification == 0
11083 && part_die->is_declaration == 0
d8228535 11084 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
11085 || part_die->tag == DW_TAG_base_type
11086 || part_die->tag == DW_TAG_subrange_type))
11087 {
11088 if (building_psymtab && part_die->name != NULL)
04a679b8 11089 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11090 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
11091 &objfile->static_psymbols,
11092 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 11093 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11094 continue;
11095 }
11096
d8228535
JK
11097 /* The exception for DW_TAG_typedef with has_children above is
11098 a workaround of GCC PR debug/47510. In the case of this complaint
11099 type_name_no_tag_or_error will error on such types later.
11100
11101 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11102 it could not find the child DIEs referenced later, this is checked
11103 above. In correct DWARF DW_TAG_typedef should have no children. */
11104
11105 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11106 complaint (&symfile_complaints,
11107 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11108 "- DIE at 0x%x [in module %s]"),
b64f50a1 11109 part_die->offset.sect_off, objfile->name);
d8228535 11110
72bf9492
DJ
11111 /* If we're at the second level, and we're an enumerator, and
11112 our parent has no specification (meaning possibly lives in a
11113 namespace elsewhere), then we can add the partial symbol now
11114 instead of queueing it. */
11115 if (part_die->tag == DW_TAG_enumerator
11116 && parent_die != NULL
11117 && parent_die->die_parent == NULL
11118 && parent_die->tag == DW_TAG_enumeration_type
11119 && parent_die->has_specification == 0)
11120 {
11121 if (part_die->name == NULL)
3e43a32a
MS
11122 complaint (&symfile_complaints,
11123 _("malformed enumerator DIE ignored"));
72bf9492 11124 else if (building_psymtab)
04a679b8 11125 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11126 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
11127 (cu->language == language_cplus
11128 || cu->language == language_java)
bb5ed363
DE
11129 ? &objfile->global_psymbols
11130 : &objfile->static_psymbols,
11131 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 11132
dee91e82 11133 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11134 continue;
11135 }
11136
11137 /* We'll save this DIE so link it in. */
11138 part_die->die_parent = parent_die;
11139 part_die->die_sibling = NULL;
11140 part_die->die_child = NULL;
11141
11142 if (last_die && last_die == parent_die)
11143 last_die->die_child = part_die;
11144 else if (last_die)
11145 last_die->die_sibling = part_die;
11146
11147 last_die = part_die;
11148
11149 if (first_die == NULL)
11150 first_die = part_die;
11151
11152 /* Maybe add the DIE to the hash table. Not all DIEs that we
11153 find interesting need to be in the hash table, because we
11154 also have the parent/sibling/child chains; only those that we
11155 might refer to by offset later during partial symbol reading.
11156
11157 For now this means things that might have be the target of a
11158 DW_AT_specification, DW_AT_abstract_origin, or
11159 DW_AT_extension. DW_AT_extension will refer only to
11160 namespaces; DW_AT_abstract_origin refers to functions (and
11161 many things under the function DIE, but we do not recurse
11162 into function DIEs during partial symbol reading) and
11163 possibly variables as well; DW_AT_specification refers to
11164 declarations. Declarations ought to have the DW_AT_declaration
11165 flag. It happens that GCC forgets to put it in sometimes, but
11166 only for functions, not for types.
11167
11168 Adding more things than necessary to the hash table is harmless
11169 except for the performance cost. Adding too few will result in
5afb4e99
DJ
11170 wasted time in find_partial_die, when we reread the compilation
11171 unit with load_all_dies set. */
72bf9492 11172
5afb4e99 11173 if (load_all
72929c62 11174 || abbrev->tag == DW_TAG_constant
5afb4e99 11175 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
11176 || abbrev->tag == DW_TAG_variable
11177 || abbrev->tag == DW_TAG_namespace
11178 || part_die->is_declaration)
11179 {
11180 void **slot;
11181
11182 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 11183 part_die->offset.sect_off, INSERT);
72bf9492
DJ
11184 *slot = part_die;
11185 }
11186
11187 part_die = obstack_alloc (&cu->comp_unit_obstack,
11188 sizeof (struct partial_die_info));
11189
11190 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 11191 we have no reason to follow the children of structures; for other
98bfdba5
PA
11192 languages we have to, so that we can get at method physnames
11193 to infer fully qualified class names, for DW_AT_specification,
11194 and for C++ template arguments. For C++, we also look one level
11195 inside functions to find template arguments (if the name of the
11196 function does not already contain the template arguments).
bc30ff58
JB
11197
11198 For Ada, we need to scan the children of subprograms and lexical
11199 blocks as well because Ada allows the definition of nested
11200 entities that could be interesting for the debugger, such as
11201 nested subprograms for instance. */
72bf9492 11202 if (last_die->has_children
5afb4e99
DJ
11203 && (load_all
11204 || last_die->tag == DW_TAG_namespace
f55ee35c 11205 || last_die->tag == DW_TAG_module
72bf9492 11206 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
11207 || (cu->language == language_cplus
11208 && last_die->tag == DW_TAG_subprogram
11209 && (last_die->name == NULL
11210 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
11211 || (cu->language != language_c
11212 && (last_die->tag == DW_TAG_class_type
680b30c7 11213 || last_die->tag == DW_TAG_interface_type
72bf9492 11214 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
11215 || last_die->tag == DW_TAG_union_type))
11216 || (cu->language == language_ada
11217 && (last_die->tag == DW_TAG_subprogram
11218 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
11219 {
11220 nesting_level++;
11221 parent_die = last_die;
11222 continue;
11223 }
11224
11225 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11226 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
11227
11228 /* Back to the top, do it again. */
11229 }
11230}
11231
c906108c
SS
11232/* Read a minimal amount of information into the minimal die structure. */
11233
fe1b8b76 11234static gdb_byte *
dee91e82
DE
11235read_partial_die (const struct die_reader_specs *reader,
11236 struct partial_die_info *part_die,
11237 struct abbrev_info *abbrev, unsigned int abbrev_len,
11238 gdb_byte *info_ptr)
c906108c 11239{
dee91e82 11240 struct dwarf2_cu *cu = reader->cu;
bb5ed363 11241 struct objfile *objfile = cu->objfile;
dee91e82 11242 gdb_byte *buffer = reader->buffer;
fa238c03 11243 unsigned int i;
c906108c 11244 struct attribute attr;
c5aa993b 11245 int has_low_pc_attr = 0;
c906108c 11246 int has_high_pc_attr = 0;
91da1414 11247 int high_pc_relative = 0;
c906108c 11248
72bf9492 11249 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 11250
b64f50a1 11251 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
11252
11253 info_ptr += abbrev_len;
11254
11255 if (abbrev == NULL)
11256 return info_ptr;
11257
c906108c
SS
11258 part_die->tag = abbrev->tag;
11259 part_die->has_children = abbrev->has_children;
c906108c
SS
11260
11261 for (i = 0; i < abbrev->num_attrs; ++i)
11262 {
dee91e82 11263 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
11264
11265 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 11266 partial symbol table. */
c906108c
SS
11267 switch (attr.name)
11268 {
11269 case DW_AT_name:
71c25dea
TT
11270 switch (part_die->tag)
11271 {
11272 case DW_TAG_compile_unit:
95554aad 11273 case DW_TAG_partial_unit:
348e048f 11274 case DW_TAG_type_unit:
71c25dea
TT
11275 /* Compilation units have a DW_AT_name that is a filename, not
11276 a source language identifier. */
11277 case DW_TAG_enumeration_type:
11278 case DW_TAG_enumerator:
11279 /* These tags always have simple identifiers already; no need
11280 to canonicalize them. */
11281 part_die->name = DW_STRING (&attr);
11282 break;
11283 default:
11284 part_die->name
11285 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 11286 &objfile->objfile_obstack);
71c25dea
TT
11287 break;
11288 }
c906108c 11289 break;
31ef98ae 11290 case DW_AT_linkage_name:
c906108c 11291 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
11292 /* Note that both forms of linkage name might appear. We
11293 assume they will be the same, and we only store the last
11294 one we see. */
94af9270
KS
11295 if (cu->language == language_ada)
11296 part_die->name = DW_STRING (&attr);
abc72ce4 11297 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
11298 break;
11299 case DW_AT_low_pc:
11300 has_low_pc_attr = 1;
11301 part_die->lowpc = DW_ADDR (&attr);
11302 break;
11303 case DW_AT_high_pc:
11304 has_high_pc_attr = 1;
3019eac3
DE
11305 if (attr.form == DW_FORM_addr
11306 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
11307 part_die->highpc = DW_ADDR (&attr);
11308 else
11309 {
11310 high_pc_relative = 1;
11311 part_die->highpc = DW_UNSND (&attr);
11312 }
c906108c
SS
11313 break;
11314 case DW_AT_location:
0963b4bd 11315 /* Support the .debug_loc offsets. */
8e19ed76
PS
11316 if (attr_form_is_block (&attr))
11317 {
95554aad 11318 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 11319 }
3690dd37 11320 else if (attr_form_is_section_offset (&attr))
8e19ed76 11321 {
4d3c2250 11322 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11323 }
11324 else
11325 {
4d3c2250
KB
11326 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11327 "partial symbol information");
8e19ed76 11328 }
c906108c 11329 break;
c906108c
SS
11330 case DW_AT_external:
11331 part_die->is_external = DW_UNSND (&attr);
11332 break;
11333 case DW_AT_declaration:
11334 part_die->is_declaration = DW_UNSND (&attr);
11335 break;
11336 case DW_AT_type:
11337 part_die->has_type = 1;
11338 break;
11339 case DW_AT_abstract_origin:
11340 case DW_AT_specification:
72bf9492
DJ
11341 case DW_AT_extension:
11342 part_die->has_specification = 1;
c764a876 11343 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
11344 break;
11345 case DW_AT_sibling:
11346 /* Ignore absolute siblings, they might point outside of
11347 the current compile unit. */
11348 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
11349 complaint (&symfile_complaints,
11350 _("ignoring absolute DW_AT_sibling"));
c906108c 11351 else
b64f50a1 11352 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 11353 break;
fa4028e9
JB
11354 case DW_AT_byte_size:
11355 part_die->has_byte_size = 1;
11356 break;
68511cec
CES
11357 case DW_AT_calling_convention:
11358 /* DWARF doesn't provide a way to identify a program's source-level
11359 entry point. DW_AT_calling_convention attributes are only meant
11360 to describe functions' calling conventions.
11361
11362 However, because it's a necessary piece of information in
11363 Fortran, and because DW_CC_program is the only piece of debugging
11364 information whose definition refers to a 'main program' at all,
11365 several compilers have begun marking Fortran main programs with
11366 DW_CC_program --- even when those functions use the standard
11367 calling conventions.
11368
11369 So until DWARF specifies a way to provide this information and
11370 compilers pick up the new representation, we'll support this
11371 practice. */
11372 if (DW_UNSND (&attr) == DW_CC_program
11373 && cu->language == language_fortran)
01f8c46d
JK
11374 {
11375 set_main_name (part_die->name);
11376
11377 /* As this DIE has a static linkage the name would be difficult
11378 to look up later. */
11379 language_of_main = language_fortran;
11380 }
68511cec 11381 break;
481860b3
GB
11382 case DW_AT_inline:
11383 if (DW_UNSND (&attr) == DW_INL_inlined
11384 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11385 part_die->may_be_inlined = 1;
11386 break;
95554aad
TT
11387
11388 case DW_AT_import:
11389 if (part_die->tag == DW_TAG_imported_unit)
11390 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11391 break;
11392
c906108c
SS
11393 default:
11394 break;
11395 }
11396 }
11397
91da1414
MW
11398 if (high_pc_relative)
11399 part_die->highpc += part_die->lowpc;
11400
9373cf26
JK
11401 if (has_low_pc_attr && has_high_pc_attr)
11402 {
11403 /* When using the GNU linker, .gnu.linkonce. sections are used to
11404 eliminate duplicate copies of functions and vtables and such.
11405 The linker will arbitrarily choose one and discard the others.
11406 The AT_*_pc values for such functions refer to local labels in
11407 these sections. If the section from that file was discarded, the
11408 labels are not in the output, so the relocs get a value of 0.
11409 If this is a discarded function, mark the pc bounds as invalid,
11410 so that GDB will ignore it. */
11411 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11412 {
bb5ed363 11413 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11414
11415 complaint (&symfile_complaints,
11416 _("DW_AT_low_pc %s is zero "
11417 "for DIE at 0x%x [in module %s]"),
11418 paddress (gdbarch, part_die->lowpc),
b64f50a1 11419 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11420 }
11421 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11422 else if (part_die->lowpc >= part_die->highpc)
11423 {
bb5ed363 11424 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11425
11426 complaint (&symfile_complaints,
11427 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11428 "for DIE at 0x%x [in module %s]"),
11429 paddress (gdbarch, part_die->lowpc),
11430 paddress (gdbarch, part_die->highpc),
b64f50a1 11431 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11432 }
11433 else
11434 part_die->has_pc_info = 1;
11435 }
85cbf3d3 11436
c906108c
SS
11437 return info_ptr;
11438}
11439
72bf9492
DJ
11440/* Find a cached partial DIE at OFFSET in CU. */
11441
11442static struct partial_die_info *
b64f50a1 11443find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
11444{
11445 struct partial_die_info *lookup_die = NULL;
11446 struct partial_die_info part_die;
11447
11448 part_die.offset = offset;
b64f50a1
JK
11449 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11450 offset.sect_off);
72bf9492 11451
72bf9492
DJ
11452 return lookup_die;
11453}
11454
348e048f
DE
11455/* Find a partial DIE at OFFSET, which may or may not be in CU,
11456 except in the case of .debug_types DIEs which do not reference
11457 outside their CU (they do however referencing other types via
55f1336d 11458 DW_FORM_ref_sig8). */
72bf9492
DJ
11459
11460static struct partial_die_info *
b64f50a1 11461find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
72bf9492 11462{
bb5ed363 11463 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
11464 struct dwarf2_per_cu_data *per_cu = NULL;
11465 struct partial_die_info *pd = NULL;
72bf9492 11466
45452591 11467 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
11468 {
11469 pd = find_partial_die_in_comp_unit (offset, cu);
11470 if (pd != NULL)
11471 return pd;
0d99eb77
DE
11472 /* We missed recording what we needed.
11473 Load all dies and try again. */
11474 per_cu = cu->per_cu;
5afb4e99 11475 }
0d99eb77
DE
11476 else
11477 {
11478 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 11479 if (cu->per_cu->is_debug_types)
0d99eb77
DE
11480 {
11481 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11482 " external reference to offset 0x%lx [in module %s].\n"),
11483 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11484 bfd_get_filename (objfile->obfd));
11485 }
11486 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
72bf9492 11487
0d99eb77
DE
11488 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11489 load_partial_comp_unit (per_cu);
ae038cb0 11490
0d99eb77
DE
11491 per_cu->cu->last_used = 0;
11492 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11493 }
5afb4e99 11494
dee91e82
DE
11495 /* If we didn't find it, and not all dies have been loaded,
11496 load them all and try again. */
11497
5afb4e99
DJ
11498 if (pd == NULL && per_cu->load_all_dies == 0)
11499 {
5afb4e99 11500 per_cu->load_all_dies = 1;
fd820528
DE
11501
11502 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11503 THIS_CU->cu may already be in use. So we can't just free it and
11504 replace its DIEs with the ones we read in. Instead, we leave those
11505 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11506 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11507 set. */
dee91e82 11508 load_partial_comp_unit (per_cu);
5afb4e99
DJ
11509
11510 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11511 }
11512
11513 if (pd == NULL)
11514 internal_error (__FILE__, __LINE__,
3e43a32a
MS
11515 _("could not find partial DIE 0x%x "
11516 "in cache [from module %s]\n"),
b64f50a1 11517 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 11518 return pd;
72bf9492
DJ
11519}
11520
abc72ce4
DE
11521/* See if we can figure out if the class lives in a namespace. We do
11522 this by looking for a member function; its demangled name will
11523 contain namespace info, if there is any. */
11524
11525static void
11526guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11527 struct dwarf2_cu *cu)
11528{
11529 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11530 what template types look like, because the demangler
11531 frequently doesn't give the same name as the debug info. We
11532 could fix this by only using the demangled name to get the
11533 prefix (but see comment in read_structure_type). */
11534
11535 struct partial_die_info *real_pdi;
11536 struct partial_die_info *child_pdi;
11537
11538 /* If this DIE (this DIE's specification, if any) has a parent, then
11539 we should not do this. We'll prepend the parent's fully qualified
11540 name when we create the partial symbol. */
11541
11542 real_pdi = struct_pdi;
11543 while (real_pdi->has_specification)
11544 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11545
11546 if (real_pdi->die_parent != NULL)
11547 return;
11548
11549 for (child_pdi = struct_pdi->die_child;
11550 child_pdi != NULL;
11551 child_pdi = child_pdi->die_sibling)
11552 {
11553 if (child_pdi->tag == DW_TAG_subprogram
11554 && child_pdi->linkage_name != NULL)
11555 {
11556 char *actual_class_name
11557 = language_class_name_from_physname (cu->language_defn,
11558 child_pdi->linkage_name);
11559 if (actual_class_name != NULL)
11560 {
11561 struct_pdi->name
11562 = obsavestring (actual_class_name,
11563 strlen (actual_class_name),
11564 &cu->objfile->objfile_obstack);
11565 xfree (actual_class_name);
11566 }
11567 break;
11568 }
11569 }
11570}
11571
72bf9492
DJ
11572/* Adjust PART_DIE before generating a symbol for it. This function
11573 may set the is_external flag or change the DIE's name. */
11574
11575static void
11576fixup_partial_die (struct partial_die_info *part_die,
11577 struct dwarf2_cu *cu)
11578{
abc72ce4
DE
11579 /* Once we've fixed up a die, there's no point in doing so again.
11580 This also avoids a memory leak if we were to call
11581 guess_partial_die_structure_name multiple times. */
11582 if (part_die->fixup_called)
11583 return;
11584
72bf9492
DJ
11585 /* If we found a reference attribute and the DIE has no name, try
11586 to find a name in the referred to DIE. */
11587
11588 if (part_die->name == NULL && part_die->has_specification)
11589 {
11590 struct partial_die_info *spec_die;
72bf9492 11591
10b3939b 11592 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 11593
10b3939b 11594 fixup_partial_die (spec_die, cu);
72bf9492
DJ
11595
11596 if (spec_die->name)
11597 {
11598 part_die->name = spec_die->name;
11599
11600 /* Copy DW_AT_external attribute if it is set. */
11601 if (spec_die->is_external)
11602 part_die->is_external = spec_die->is_external;
11603 }
11604 }
11605
11606 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
11607
11608 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 11609 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 11610
abc72ce4
DE
11611 /* If there is no parent die to provide a namespace, and there are
11612 children, see if we can determine the namespace from their linkage
122d1940 11613 name. */
abc72ce4 11614 if (cu->language == language_cplus
8b70b953 11615 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
11616 && part_die->die_parent == NULL
11617 && part_die->has_children
11618 && (part_die->tag == DW_TAG_class_type
11619 || part_die->tag == DW_TAG_structure_type
11620 || part_die->tag == DW_TAG_union_type))
11621 guess_partial_die_structure_name (part_die, cu);
11622
53832f31
TT
11623 /* GCC might emit a nameless struct or union that has a linkage
11624 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11625 if (part_die->name == NULL
96408a79
SA
11626 && (part_die->tag == DW_TAG_class_type
11627 || part_die->tag == DW_TAG_interface_type
11628 || part_die->tag == DW_TAG_structure_type
11629 || part_die->tag == DW_TAG_union_type)
53832f31
TT
11630 && part_die->linkage_name != NULL)
11631 {
11632 char *demangled;
11633
11634 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11635 if (demangled)
11636 {
96408a79
SA
11637 const char *base;
11638
11639 /* Strip any leading namespaces/classes, keep only the base name.
11640 DW_AT_name for named DIEs does not contain the prefixes. */
11641 base = strrchr (demangled, ':');
11642 if (base && base > demangled && base[-1] == ':')
11643 base++;
11644 else
11645 base = demangled;
11646
11647 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
11648 &cu->objfile->objfile_obstack);
11649 xfree (demangled);
11650 }
11651 }
11652
abc72ce4 11653 part_die->fixup_called = 1;
72bf9492
DJ
11654}
11655
a8329558 11656/* Read an attribute value described by an attribute form. */
c906108c 11657
fe1b8b76 11658static gdb_byte *
dee91e82
DE
11659read_attribute_value (const struct die_reader_specs *reader,
11660 struct attribute *attr, unsigned form,
11661 gdb_byte *info_ptr)
c906108c 11662{
dee91e82
DE
11663 struct dwarf2_cu *cu = reader->cu;
11664 bfd *abfd = reader->abfd;
e7c27a73 11665 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
11666 unsigned int bytes_read;
11667 struct dwarf_block *blk;
11668
a8329558
KW
11669 attr->form = form;
11670 switch (form)
c906108c 11671 {
c906108c 11672 case DW_FORM_ref_addr:
ae411497 11673 if (cu->header.version == 2)
4568ecf9 11674 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 11675 else
4568ecf9
DE
11676 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11677 &cu->header, &bytes_read);
ae411497
TT
11678 info_ptr += bytes_read;
11679 break;
11680 case DW_FORM_addr:
e7c27a73 11681 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 11682 info_ptr += bytes_read;
c906108c
SS
11683 break;
11684 case DW_FORM_block2:
7b5a2f43 11685 blk = dwarf_alloc_block (cu);
c906108c
SS
11686 blk->size = read_2_bytes (abfd, info_ptr);
11687 info_ptr += 2;
11688 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11689 info_ptr += blk->size;
11690 DW_BLOCK (attr) = blk;
11691 break;
11692 case DW_FORM_block4:
7b5a2f43 11693 blk = dwarf_alloc_block (cu);
c906108c
SS
11694 blk->size = read_4_bytes (abfd, info_ptr);
11695 info_ptr += 4;
11696 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11697 info_ptr += blk->size;
11698 DW_BLOCK (attr) = blk;
11699 break;
11700 case DW_FORM_data2:
11701 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11702 info_ptr += 2;
11703 break;
11704 case DW_FORM_data4:
11705 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11706 info_ptr += 4;
11707 break;
11708 case DW_FORM_data8:
11709 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11710 info_ptr += 8;
11711 break;
2dc7f7b3
TT
11712 case DW_FORM_sec_offset:
11713 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11714 info_ptr += bytes_read;
11715 break;
c906108c 11716 case DW_FORM_string:
9b1c24c8 11717 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 11718 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
11719 info_ptr += bytes_read;
11720 break;
4bdf3d34
JJ
11721 case DW_FORM_strp:
11722 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11723 &bytes_read);
8285870a 11724 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
11725 info_ptr += bytes_read;
11726 break;
2dc7f7b3 11727 case DW_FORM_exprloc:
c906108c 11728 case DW_FORM_block:
7b5a2f43 11729 blk = dwarf_alloc_block (cu);
c906108c
SS
11730 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11731 info_ptr += bytes_read;
11732 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11733 info_ptr += blk->size;
11734 DW_BLOCK (attr) = blk;
11735 break;
11736 case DW_FORM_block1:
7b5a2f43 11737 blk = dwarf_alloc_block (cu);
c906108c
SS
11738 blk->size = read_1_byte (abfd, info_ptr);
11739 info_ptr += 1;
11740 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11741 info_ptr += blk->size;
11742 DW_BLOCK (attr) = blk;
11743 break;
11744 case DW_FORM_data1:
11745 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11746 info_ptr += 1;
11747 break;
11748 case DW_FORM_flag:
11749 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11750 info_ptr += 1;
11751 break;
2dc7f7b3
TT
11752 case DW_FORM_flag_present:
11753 DW_UNSND (attr) = 1;
11754 break;
c906108c
SS
11755 case DW_FORM_sdata:
11756 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11757 info_ptr += bytes_read;
11758 break;
11759 case DW_FORM_udata:
11760 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11761 info_ptr += bytes_read;
11762 break;
11763 case DW_FORM_ref1:
4568ecf9
DE
11764 DW_UNSND (attr) = (cu->header.offset.sect_off
11765 + read_1_byte (abfd, info_ptr));
c906108c
SS
11766 info_ptr += 1;
11767 break;
11768 case DW_FORM_ref2:
4568ecf9
DE
11769 DW_UNSND (attr) = (cu->header.offset.sect_off
11770 + read_2_bytes (abfd, info_ptr));
c906108c
SS
11771 info_ptr += 2;
11772 break;
11773 case DW_FORM_ref4:
4568ecf9
DE
11774 DW_UNSND (attr) = (cu->header.offset.sect_off
11775 + read_4_bytes (abfd, info_ptr));
c906108c
SS
11776 info_ptr += 4;
11777 break;
613e1657 11778 case DW_FORM_ref8:
4568ecf9
DE
11779 DW_UNSND (attr) = (cu->header.offset.sect_off
11780 + read_8_bytes (abfd, info_ptr));
613e1657
KB
11781 info_ptr += 8;
11782 break;
55f1336d 11783 case DW_FORM_ref_sig8:
348e048f
DE
11784 /* Convert the signature to something we can record in DW_UNSND
11785 for later lookup.
11786 NOTE: This is NULL if the type wasn't found. */
11787 DW_SIGNATURED_TYPE (attr) =
e319fa28 11788 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
11789 info_ptr += 8;
11790 break;
c906108c 11791 case DW_FORM_ref_udata:
4568ecf9
DE
11792 DW_UNSND (attr) = (cu->header.offset.sect_off
11793 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
11794 info_ptr += bytes_read;
11795 break;
c906108c 11796 case DW_FORM_indirect:
a8329558
KW
11797 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11798 info_ptr += bytes_read;
dee91e82 11799 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 11800 break;
3019eac3
DE
11801 case DW_FORM_GNU_addr_index:
11802 if (reader->dwo_file == NULL)
11803 {
11804 /* For now flag a hard error.
11805 Later we can turn this into a complaint. */
11806 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11807 dwarf_form_name (form),
11808 bfd_get_filename (abfd));
11809 }
11810 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11811 info_ptr += bytes_read;
11812 break;
11813 case DW_FORM_GNU_str_index:
11814 if (reader->dwo_file == NULL)
11815 {
11816 /* For now flag a hard error.
11817 Later we can turn this into a complaint if warranted. */
11818 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11819 dwarf_form_name (form),
11820 bfd_get_filename (abfd));
11821 }
11822 {
11823 ULONGEST str_index =
11824 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11825
11826 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11827 DW_STRING_IS_CANONICAL (attr) = 0;
11828 info_ptr += bytes_read;
11829 }
11830 break;
c906108c 11831 default:
8a3fe4f8 11832 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
11833 dwarf_form_name (form),
11834 bfd_get_filename (abfd));
c906108c 11835 }
28e94949
JB
11836
11837 /* We have seen instances where the compiler tried to emit a byte
11838 size attribute of -1 which ended up being encoded as an unsigned
11839 0xffffffff. Although 0xffffffff is technically a valid size value,
11840 an object of this size seems pretty unlikely so we can relatively
11841 safely treat these cases as if the size attribute was invalid and
11842 treat them as zero by default. */
11843 if (attr->name == DW_AT_byte_size
11844 && form == DW_FORM_data4
11845 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
11846 {
11847 complaint
11848 (&symfile_complaints,
43bbcdc2
PH
11849 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
11850 hex_string (DW_UNSND (attr)));
01c66ae6
JB
11851 DW_UNSND (attr) = 0;
11852 }
28e94949 11853
c906108c
SS
11854 return info_ptr;
11855}
11856
a8329558
KW
11857/* Read an attribute described by an abbreviated attribute. */
11858
fe1b8b76 11859static gdb_byte *
dee91e82
DE
11860read_attribute (const struct die_reader_specs *reader,
11861 struct attribute *attr, struct attr_abbrev *abbrev,
11862 gdb_byte *info_ptr)
a8329558
KW
11863{
11864 attr->name = abbrev->name;
dee91e82 11865 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
11866}
11867
0963b4bd 11868/* Read dwarf information from a buffer. */
c906108c
SS
11869
11870static unsigned int
fe1b8b76 11871read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 11872{
fe1b8b76 11873 return bfd_get_8 (abfd, buf);
c906108c
SS
11874}
11875
11876static int
fe1b8b76 11877read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 11878{
fe1b8b76 11879 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
11880}
11881
11882static unsigned int
fe1b8b76 11883read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11884{
fe1b8b76 11885 return bfd_get_16 (abfd, buf);
c906108c
SS
11886}
11887
21ae7a4d
JK
11888static int
11889read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
11890{
11891 return bfd_get_signed_16 (abfd, buf);
11892}
11893
c906108c 11894static unsigned int
fe1b8b76 11895read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11896{
fe1b8b76 11897 return bfd_get_32 (abfd, buf);
c906108c
SS
11898}
11899
21ae7a4d
JK
11900static int
11901read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
11902{
11903 return bfd_get_signed_32 (abfd, buf);
11904}
11905
93311388 11906static ULONGEST
fe1b8b76 11907read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11908{
fe1b8b76 11909 return bfd_get_64 (abfd, buf);
c906108c
SS
11910}
11911
11912static CORE_ADDR
fe1b8b76 11913read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 11914 unsigned int *bytes_read)
c906108c 11915{
e7c27a73 11916 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
11917 CORE_ADDR retval = 0;
11918
107d2387 11919 if (cu_header->signed_addr_p)
c906108c 11920 {
107d2387
AC
11921 switch (cu_header->addr_size)
11922 {
11923 case 2:
fe1b8b76 11924 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
11925 break;
11926 case 4:
fe1b8b76 11927 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
11928 break;
11929 case 8:
fe1b8b76 11930 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
11931 break;
11932 default:
8e65ff28 11933 internal_error (__FILE__, __LINE__,
e2e0b3e5 11934 _("read_address: bad switch, signed [in module %s]"),
659b0389 11935 bfd_get_filename (abfd));
107d2387
AC
11936 }
11937 }
11938 else
11939 {
11940 switch (cu_header->addr_size)
11941 {
11942 case 2:
fe1b8b76 11943 retval = bfd_get_16 (abfd, buf);
107d2387
AC
11944 break;
11945 case 4:
fe1b8b76 11946 retval = bfd_get_32 (abfd, buf);
107d2387
AC
11947 break;
11948 case 8:
fe1b8b76 11949 retval = bfd_get_64 (abfd, buf);
107d2387
AC
11950 break;
11951 default:
8e65ff28 11952 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
11953 _("read_address: bad switch, "
11954 "unsigned [in module %s]"),
659b0389 11955 bfd_get_filename (abfd));
107d2387 11956 }
c906108c 11957 }
64367e0a 11958
107d2387
AC
11959 *bytes_read = cu_header->addr_size;
11960 return retval;
c906108c
SS
11961}
11962
f7ef9339 11963/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
11964 specification allows the initial length to take up either 4 bytes
11965 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
11966 bytes describe the length and all offsets will be 8 bytes in length
11967 instead of 4.
11968
f7ef9339
KB
11969 An older, non-standard 64-bit format is also handled by this
11970 function. The older format in question stores the initial length
11971 as an 8-byte quantity without an escape value. Lengths greater
11972 than 2^32 aren't very common which means that the initial 4 bytes
11973 is almost always zero. Since a length value of zero doesn't make
11974 sense for the 32-bit format, this initial zero can be considered to
11975 be an escape value which indicates the presence of the older 64-bit
11976 format. As written, the code can't detect (old format) lengths
917c78fc
MK
11977 greater than 4GB. If it becomes necessary to handle lengths
11978 somewhat larger than 4GB, we could allow other small values (such
11979 as the non-sensical values of 1, 2, and 3) to also be used as
11980 escape values indicating the presence of the old format.
f7ef9339 11981
917c78fc
MK
11982 The value returned via bytes_read should be used to increment the
11983 relevant pointer after calling read_initial_length().
c764a876 11984
613e1657
KB
11985 [ Note: read_initial_length() and read_offset() are based on the
11986 document entitled "DWARF Debugging Information Format", revision
f7ef9339 11987 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
11988 from:
11989
f7ef9339 11990 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 11991
613e1657
KB
11992 This document is only a draft and is subject to change. (So beware.)
11993
f7ef9339 11994 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
11995 determined empirically by examining 64-bit ELF files produced by
11996 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
11997
11998 - Kevin, July 16, 2002
613e1657
KB
11999 ] */
12000
12001static LONGEST
c764a876 12002read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 12003{
fe1b8b76 12004 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 12005
dd373385 12006 if (length == 0xffffffff)
613e1657 12007 {
fe1b8b76 12008 length = bfd_get_64 (abfd, buf + 4);
613e1657 12009 *bytes_read = 12;
613e1657 12010 }
dd373385 12011 else if (length == 0)
f7ef9339 12012 {
dd373385 12013 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 12014 length = bfd_get_64 (abfd, buf);
f7ef9339 12015 *bytes_read = 8;
f7ef9339 12016 }
613e1657
KB
12017 else
12018 {
12019 *bytes_read = 4;
613e1657
KB
12020 }
12021
c764a876
DE
12022 return length;
12023}
dd373385 12024
c764a876
DE
12025/* Cover function for read_initial_length.
12026 Returns the length of the object at BUF, and stores the size of the
12027 initial length in *BYTES_READ and stores the size that offsets will be in
12028 *OFFSET_SIZE.
12029 If the initial length size is not equivalent to that specified in
12030 CU_HEADER then issue a complaint.
12031 This is useful when reading non-comp-unit headers. */
dd373385 12032
c764a876
DE
12033static LONGEST
12034read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
12035 const struct comp_unit_head *cu_header,
12036 unsigned int *bytes_read,
12037 unsigned int *offset_size)
12038{
12039 LONGEST length = read_initial_length (abfd, buf, bytes_read);
12040
12041 gdb_assert (cu_header->initial_length_size == 4
12042 || cu_header->initial_length_size == 8
12043 || cu_header->initial_length_size == 12);
12044
12045 if (cu_header->initial_length_size != *bytes_read)
12046 complaint (&symfile_complaints,
12047 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 12048
c764a876 12049 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 12050 return length;
613e1657
KB
12051}
12052
12053/* Read an offset from the data stream. The size of the offset is
917c78fc 12054 given by cu_header->offset_size. */
613e1657
KB
12055
12056static LONGEST
fe1b8b76 12057read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 12058 unsigned int *bytes_read)
c764a876
DE
12059{
12060 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 12061
c764a876
DE
12062 *bytes_read = cu_header->offset_size;
12063 return offset;
12064}
12065
12066/* Read an offset from the data stream. */
12067
12068static LONGEST
12069read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
12070{
12071 LONGEST retval = 0;
12072
c764a876 12073 switch (offset_size)
613e1657
KB
12074 {
12075 case 4:
fe1b8b76 12076 retval = bfd_get_32 (abfd, buf);
613e1657
KB
12077 break;
12078 case 8:
fe1b8b76 12079 retval = bfd_get_64 (abfd, buf);
613e1657
KB
12080 break;
12081 default:
8e65ff28 12082 internal_error (__FILE__, __LINE__,
c764a876 12083 _("read_offset_1: bad switch [in module %s]"),
659b0389 12084 bfd_get_filename (abfd));
613e1657
KB
12085 }
12086
917c78fc 12087 return retval;
613e1657
KB
12088}
12089
fe1b8b76
JB
12090static gdb_byte *
12091read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
12092{
12093 /* If the size of a host char is 8 bits, we can return a pointer
12094 to the buffer, otherwise we have to copy the data to a buffer
12095 allocated on the temporary obstack. */
4bdf3d34 12096 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 12097 return buf;
c906108c
SS
12098}
12099
12100static char *
9b1c24c8 12101read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
12102{
12103 /* If the size of a host char is 8 bits, we can return a pointer
12104 to the string, otherwise we have to copy the string to a buffer
12105 allocated on the temporary obstack. */
4bdf3d34 12106 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
12107 if (*buf == '\0')
12108 {
12109 *bytes_read_ptr = 1;
12110 return NULL;
12111 }
fe1b8b76
JB
12112 *bytes_read_ptr = strlen ((char *) buf) + 1;
12113 return (char *) buf;
4bdf3d34
JJ
12114}
12115
12116static char *
cf2c3c16 12117read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 12118{
be391dca 12119 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 12120 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
12121 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12122 bfd_get_filename (abfd));
dce234bc 12123 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
12124 error (_("DW_FORM_strp pointing outside of "
12125 ".debug_str section [in module %s]"),
12126 bfd_get_filename (abfd));
4bdf3d34 12127 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 12128 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 12129 return NULL;
dce234bc 12130 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
12131}
12132
cf2c3c16
TT
12133static char *
12134read_indirect_string (bfd *abfd, gdb_byte *buf,
12135 const struct comp_unit_head *cu_header,
12136 unsigned int *bytes_read_ptr)
12137{
12138 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12139
12140 return read_indirect_string_at_offset (abfd, str_offset);
12141}
12142
12df843f 12143static ULONGEST
fe1b8b76 12144read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12145{
12df843f 12146 ULONGEST result;
ce5d95e1 12147 unsigned int num_read;
c906108c
SS
12148 int i, shift;
12149 unsigned char byte;
12150
12151 result = 0;
12152 shift = 0;
12153 num_read = 0;
12154 i = 0;
12155 while (1)
12156 {
fe1b8b76 12157 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12158 buf++;
12159 num_read++;
12df843f 12160 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
12161 if ((byte & 128) == 0)
12162 {
12163 break;
12164 }
12165 shift += 7;
12166 }
12167 *bytes_read_ptr = num_read;
12168 return result;
12169}
12170
12df843f 12171static LONGEST
fe1b8b76 12172read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12173{
12df843f 12174 LONGEST result;
77e0b926 12175 int i, shift, num_read;
c906108c
SS
12176 unsigned char byte;
12177
12178 result = 0;
12179 shift = 0;
c906108c
SS
12180 num_read = 0;
12181 i = 0;
12182 while (1)
12183 {
fe1b8b76 12184 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12185 buf++;
12186 num_read++;
12df843f 12187 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
12188 shift += 7;
12189 if ((byte & 128) == 0)
12190 {
12191 break;
12192 }
12193 }
77e0b926 12194 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 12195 result |= -(((LONGEST) 1) << shift);
c906108c
SS
12196 *bytes_read_ptr = num_read;
12197 return result;
12198}
12199
3019eac3
DE
12200/* Given index ADDR_INDEX in .debug_addr, fetch the value.
12201 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12202 ADDR_SIZE is the size of addresses from the CU header. */
12203
12204static CORE_ADDR
12205read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12206{
12207 struct objfile *objfile = dwarf2_per_objfile->objfile;
12208 bfd *abfd = objfile->obfd;
12209 const gdb_byte *info_ptr;
12210
12211 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12212 if (dwarf2_per_objfile->addr.buffer == NULL)
12213 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12214 objfile->name);
12215 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12216 error (_("DW_FORM_addr_index pointing outside of "
12217 ".debug_addr section [in module %s]"),
12218 objfile->name);
12219 info_ptr = (dwarf2_per_objfile->addr.buffer
12220 + addr_base + addr_index * addr_size);
12221 if (addr_size == 4)
12222 return bfd_get_32 (abfd, info_ptr);
12223 else
12224 return bfd_get_64 (abfd, info_ptr);
12225}
12226
12227/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12228
12229static CORE_ADDR
12230read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12231{
12232 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12233}
12234
12235/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12236
12237static CORE_ADDR
12238read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12239 unsigned int *bytes_read)
12240{
12241 bfd *abfd = cu->objfile->obfd;
12242 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12243
12244 return read_addr_index (cu, addr_index);
12245}
12246
12247/* Data structure to pass results from dwarf2_read_addr_index_reader
12248 back to dwarf2_read_addr_index. */
12249
12250struct dwarf2_read_addr_index_data
12251{
12252 ULONGEST addr_base;
12253 int addr_size;
12254};
12255
12256/* die_reader_func for dwarf2_read_addr_index. */
12257
12258static void
12259dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12260 gdb_byte *info_ptr,
12261 struct die_info *comp_unit_die,
12262 int has_children,
12263 void *data)
12264{
12265 struct dwarf2_cu *cu = reader->cu;
12266 struct dwarf2_read_addr_index_data *aidata =
12267 (struct dwarf2_read_addr_index_data *) data;
12268
12269 aidata->addr_base = cu->addr_base;
12270 aidata->addr_size = cu->header.addr_size;
12271}
12272
12273/* Given an index in .debug_addr, fetch the value.
12274 NOTE: This can be called during dwarf expression evaluation,
12275 long after the debug information has been read, and thus per_cu->cu
12276 may no longer exist. */
12277
12278CORE_ADDR
12279dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12280 unsigned int addr_index)
12281{
12282 struct objfile *objfile = per_cu->objfile;
12283 struct dwarf2_cu *cu = per_cu->cu;
12284 ULONGEST addr_base;
12285 int addr_size;
12286
12287 /* This is intended to be called from outside this file. */
12288 dw2_setup (objfile);
12289
12290 /* We need addr_base and addr_size.
12291 If we don't have PER_CU->cu, we have to get it.
12292 Nasty, but the alternative is storing the needed info in PER_CU,
12293 which at this point doesn't seem justified: it's not clear how frequently
12294 it would get used and it would increase the size of every PER_CU.
12295 Entry points like dwarf2_per_cu_addr_size do a similar thing
12296 so we're not in uncharted territory here.
12297 Alas we need to be a bit more complicated as addr_base is contained
12298 in the DIE.
12299
12300 We don't need to read the entire CU(/TU).
12301 We just need the header and top level die.
12302 IWBN to use the aging mechanism to let us lazily later discard the CU.
12303 See however init_cutu_and_read_dies_simple. */
12304
12305 if (cu != NULL)
12306 {
12307 addr_base = cu->addr_base;
12308 addr_size = cu->header.addr_size;
12309 }
12310 else
12311 {
12312 struct dwarf2_read_addr_index_data aidata;
12313
12314 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12315 &aidata);
12316 addr_base = aidata.addr_base;
12317 addr_size = aidata.addr_size;
12318 }
12319
12320 return read_addr_index_1 (addr_index, addr_base, addr_size);
12321}
12322
12323/* Given a DW_AT_str_index, fetch the string. */
12324
12325static char *
12326read_str_index (const struct die_reader_specs *reader,
12327 struct dwarf2_cu *cu, ULONGEST str_index)
12328{
12329 struct objfile *objfile = dwarf2_per_objfile->objfile;
12330 const char *dwo_name = objfile->name;
12331 bfd *abfd = objfile->obfd;
12332 struct dwo_sections *sections = &reader->dwo_file->sections;
12333 gdb_byte *info_ptr;
12334 ULONGEST str_offset;
12335
12336 dwarf2_read_section (objfile, &sections->str);
12337 dwarf2_read_section (objfile, &sections->str_offsets);
12338 if (sections->str.buffer == NULL)
12339 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12340 " in CU at offset 0x%lx [in module %s]"),
12341 (long) cu->header.offset.sect_off, dwo_name);
12342 if (sections->str_offsets.buffer == NULL)
12343 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12344 " in CU at offset 0x%lx [in module %s]"),
12345 (long) cu->header.offset.sect_off, dwo_name);
12346 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12347 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12348 " section in CU at offset 0x%lx [in module %s]"),
12349 (long) cu->header.offset.sect_off, dwo_name);
12350 info_ptr = (sections->str_offsets.buffer
12351 + str_index * cu->header.offset_size);
12352 if (cu->header.offset_size == 4)
12353 str_offset = bfd_get_32 (abfd, info_ptr);
12354 else
12355 str_offset = bfd_get_64 (abfd, info_ptr);
12356 if (str_offset >= sections->str.size)
12357 error (_("Offset from DW_FORM_str_index pointing outside of"
12358 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12359 (long) cu->header.offset.sect_off, dwo_name);
12360 return (char *) (sections->str.buffer + str_offset);
12361}
12362
3019eac3
DE
12363/* Return the length of an LEB128 number in BUF. */
12364
12365static int
12366leb128_size (const gdb_byte *buf)
12367{
12368 const gdb_byte *begin = buf;
12369 gdb_byte byte;
12370
12371 while (1)
12372 {
12373 byte = *buf++;
12374 if ((byte & 128) == 0)
12375 return buf - begin;
12376 }
12377}
12378
c906108c 12379static void
e142c38c 12380set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
12381{
12382 switch (lang)
12383 {
12384 case DW_LANG_C89:
76bee0cc 12385 case DW_LANG_C99:
c906108c 12386 case DW_LANG_C:
e142c38c 12387 cu->language = language_c;
c906108c
SS
12388 break;
12389 case DW_LANG_C_plus_plus:
e142c38c 12390 cu->language = language_cplus;
c906108c 12391 break;
6aecb9c2
JB
12392 case DW_LANG_D:
12393 cu->language = language_d;
12394 break;
c906108c
SS
12395 case DW_LANG_Fortran77:
12396 case DW_LANG_Fortran90:
b21b22e0 12397 case DW_LANG_Fortran95:
e142c38c 12398 cu->language = language_fortran;
c906108c 12399 break;
a766d390
DE
12400 case DW_LANG_Go:
12401 cu->language = language_go;
12402 break;
c906108c 12403 case DW_LANG_Mips_Assembler:
e142c38c 12404 cu->language = language_asm;
c906108c 12405 break;
bebd888e 12406 case DW_LANG_Java:
e142c38c 12407 cu->language = language_java;
bebd888e 12408 break;
c906108c 12409 case DW_LANG_Ada83:
8aaf0b47 12410 case DW_LANG_Ada95:
bc5f45f8
JB
12411 cu->language = language_ada;
12412 break;
72019c9c
GM
12413 case DW_LANG_Modula2:
12414 cu->language = language_m2;
12415 break;
fe8e67fd
PM
12416 case DW_LANG_Pascal83:
12417 cu->language = language_pascal;
12418 break;
22566fbd
DJ
12419 case DW_LANG_ObjC:
12420 cu->language = language_objc;
12421 break;
c906108c
SS
12422 case DW_LANG_Cobol74:
12423 case DW_LANG_Cobol85:
c906108c 12424 default:
e142c38c 12425 cu->language = language_minimal;
c906108c
SS
12426 break;
12427 }
e142c38c 12428 cu->language_defn = language_def (cu->language);
c906108c
SS
12429}
12430
12431/* Return the named attribute or NULL if not there. */
12432
12433static struct attribute *
e142c38c 12434dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 12435{
a48e046c 12436 for (;;)
c906108c 12437 {
a48e046c
TT
12438 unsigned int i;
12439 struct attribute *spec = NULL;
12440
12441 for (i = 0; i < die->num_attrs; ++i)
12442 {
12443 if (die->attrs[i].name == name)
12444 return &die->attrs[i];
12445 if (die->attrs[i].name == DW_AT_specification
12446 || die->attrs[i].name == DW_AT_abstract_origin)
12447 spec = &die->attrs[i];
12448 }
12449
12450 if (!spec)
12451 break;
c906108c 12452
f2f0e013 12453 die = follow_die_ref (die, spec, &cu);
f2f0e013 12454 }
c5aa993b 12455
c906108c
SS
12456 return NULL;
12457}
12458
348e048f
DE
12459/* Return the named attribute or NULL if not there,
12460 but do not follow DW_AT_specification, etc.
12461 This is for use in contexts where we're reading .debug_types dies.
12462 Following DW_AT_specification, DW_AT_abstract_origin will take us
12463 back up the chain, and we want to go down. */
12464
12465static struct attribute *
12466dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12467 struct dwarf2_cu *cu)
12468{
12469 unsigned int i;
12470
12471 for (i = 0; i < die->num_attrs; ++i)
12472 if (die->attrs[i].name == name)
12473 return &die->attrs[i];
12474
12475 return NULL;
12476}
12477
05cf31d1
JB
12478/* Return non-zero iff the attribute NAME is defined for the given DIE,
12479 and holds a non-zero value. This function should only be used for
2dc7f7b3 12480 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
12481
12482static int
12483dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12484{
12485 struct attribute *attr = dwarf2_attr (die, name, cu);
12486
12487 return (attr && DW_UNSND (attr));
12488}
12489
3ca72b44 12490static int
e142c38c 12491die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 12492{
05cf31d1
JB
12493 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12494 which value is non-zero. However, we have to be careful with
12495 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12496 (via dwarf2_flag_true_p) follows this attribute. So we may
12497 end up accidently finding a declaration attribute that belongs
12498 to a different DIE referenced by the specification attribute,
12499 even though the given DIE does not have a declaration attribute. */
12500 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12501 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
12502}
12503
63d06c5c 12504/* Return the die giving the specification for DIE, if there is
f2f0e013 12505 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
12506 containing the return value on output. If there is no
12507 specification, but there is an abstract origin, that is
12508 returned. */
63d06c5c
DC
12509
12510static struct die_info *
f2f0e013 12511die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 12512{
f2f0e013
DJ
12513 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12514 *spec_cu);
63d06c5c 12515
edb3359d
DJ
12516 if (spec_attr == NULL)
12517 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12518
63d06c5c
DC
12519 if (spec_attr == NULL)
12520 return NULL;
12521 else
f2f0e013 12522 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 12523}
c906108c 12524
debd256d 12525/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
12526 refers to.
12527 NOTE: This is also used as a "cleanup" function. */
12528
debd256d
JB
12529static void
12530free_line_header (struct line_header *lh)
12531{
12532 if (lh->standard_opcode_lengths)
a8bc7b56 12533 xfree (lh->standard_opcode_lengths);
debd256d
JB
12534
12535 /* Remember that all the lh->file_names[i].name pointers are
12536 pointers into debug_line_buffer, and don't need to be freed. */
12537 if (lh->file_names)
a8bc7b56 12538 xfree (lh->file_names);
debd256d
JB
12539
12540 /* Similarly for the include directory names. */
12541 if (lh->include_dirs)
a8bc7b56 12542 xfree (lh->include_dirs);
debd256d 12543
a8bc7b56 12544 xfree (lh);
debd256d
JB
12545}
12546
debd256d 12547/* Add an entry to LH's include directory table. */
ae2de4f8 12548
debd256d
JB
12549static void
12550add_include_dir (struct line_header *lh, char *include_dir)
c906108c 12551{
debd256d
JB
12552 /* Grow the array if necessary. */
12553 if (lh->include_dirs_size == 0)
c5aa993b 12554 {
debd256d
JB
12555 lh->include_dirs_size = 1; /* for testing */
12556 lh->include_dirs = xmalloc (lh->include_dirs_size
12557 * sizeof (*lh->include_dirs));
12558 }
12559 else if (lh->num_include_dirs >= lh->include_dirs_size)
12560 {
12561 lh->include_dirs_size *= 2;
12562 lh->include_dirs = xrealloc (lh->include_dirs,
12563 (lh->include_dirs_size
12564 * sizeof (*lh->include_dirs)));
c5aa993b 12565 }
c906108c 12566
debd256d
JB
12567 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12568}
6e70227d 12569
debd256d 12570/* Add an entry to LH's file name table. */
ae2de4f8 12571
debd256d
JB
12572static void
12573add_file_name (struct line_header *lh,
12574 char *name,
12575 unsigned int dir_index,
12576 unsigned int mod_time,
12577 unsigned int length)
12578{
12579 struct file_entry *fe;
12580
12581 /* Grow the array if necessary. */
12582 if (lh->file_names_size == 0)
12583 {
12584 lh->file_names_size = 1; /* for testing */
12585 lh->file_names = xmalloc (lh->file_names_size
12586 * sizeof (*lh->file_names));
12587 }
12588 else if (lh->num_file_names >= lh->file_names_size)
12589 {
12590 lh->file_names_size *= 2;
12591 lh->file_names = xrealloc (lh->file_names,
12592 (lh->file_names_size
12593 * sizeof (*lh->file_names)));
12594 }
12595
12596 fe = &lh->file_names[lh->num_file_names++];
12597 fe->name = name;
12598 fe->dir_index = dir_index;
12599 fe->mod_time = mod_time;
12600 fe->length = length;
aaa75496 12601 fe->included_p = 0;
cb1df416 12602 fe->symtab = NULL;
debd256d 12603}
6e70227d 12604
debd256d 12605/* Read the statement program header starting at OFFSET in
3019eac3 12606 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 12607 to a struct line_header, allocated using xmalloc.
debd256d
JB
12608
12609 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
12610 the returned object point into the dwarf line section buffer,
12611 and must not be freed. */
ae2de4f8 12612
debd256d 12613static struct line_header *
3019eac3 12614dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
12615{
12616 struct cleanup *back_to;
12617 struct line_header *lh;
fe1b8b76 12618 gdb_byte *line_ptr;
c764a876 12619 unsigned int bytes_read, offset_size;
debd256d
JB
12620 int i;
12621 char *cur_dir, *cur_file;
3019eac3
DE
12622 struct dwarf2_section_info *section;
12623 bfd *abfd;
12624
12625 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12626 DWO file. */
12627 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12628 section = &cu->dwo_unit->dwo_file->sections.line;
12629 else
12630 section = &dwarf2_per_objfile->line;
debd256d 12631
3019eac3
DE
12632 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12633 if (section->buffer == NULL)
debd256d 12634 {
3019eac3
DE
12635 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12636 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12637 else
12638 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
12639 return 0;
12640 }
12641
fceca515
DE
12642 /* We can't do this until we know the section is non-empty.
12643 Only then do we know we have such a section. */
12644 abfd = section->asection->owner;
12645
a738430d
MK
12646 /* Make sure that at least there's room for the total_length field.
12647 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 12648 if (offset + 4 >= section->size)
debd256d 12649 {
4d3c2250 12650 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12651 return 0;
12652 }
12653
12654 lh = xmalloc (sizeof (*lh));
12655 memset (lh, 0, sizeof (*lh));
12656 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12657 (void *) lh);
12658
3019eac3 12659 line_ptr = section->buffer + offset;
debd256d 12660
a738430d 12661 /* Read in the header. */
6e70227d 12662 lh->total_length =
c764a876
DE
12663 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12664 &bytes_read, &offset_size);
debd256d 12665 line_ptr += bytes_read;
3019eac3 12666 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 12667 {
4d3c2250 12668 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12669 return 0;
12670 }
12671 lh->statement_program_end = line_ptr + lh->total_length;
12672 lh->version = read_2_bytes (abfd, line_ptr);
12673 line_ptr += 2;
c764a876
DE
12674 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12675 line_ptr += offset_size;
debd256d
JB
12676 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12677 line_ptr += 1;
2dc7f7b3
TT
12678 if (lh->version >= 4)
12679 {
12680 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12681 line_ptr += 1;
12682 }
12683 else
12684 lh->maximum_ops_per_instruction = 1;
12685
12686 if (lh->maximum_ops_per_instruction == 0)
12687 {
12688 lh->maximum_ops_per_instruction = 1;
12689 complaint (&symfile_complaints,
3e43a32a
MS
12690 _("invalid maximum_ops_per_instruction "
12691 "in `.debug_line' section"));
2dc7f7b3
TT
12692 }
12693
debd256d
JB
12694 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12695 line_ptr += 1;
12696 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12697 line_ptr += 1;
12698 lh->line_range = read_1_byte (abfd, line_ptr);
12699 line_ptr += 1;
12700 lh->opcode_base = read_1_byte (abfd, line_ptr);
12701 line_ptr += 1;
12702 lh->standard_opcode_lengths
fe1b8b76 12703 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
12704
12705 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12706 for (i = 1; i < lh->opcode_base; ++i)
12707 {
12708 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12709 line_ptr += 1;
12710 }
12711
a738430d 12712 /* Read directory table. */
9b1c24c8 12713 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12714 {
12715 line_ptr += bytes_read;
12716 add_include_dir (lh, cur_dir);
12717 }
12718 line_ptr += bytes_read;
12719
a738430d 12720 /* Read file name table. */
9b1c24c8 12721 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12722 {
12723 unsigned int dir_index, mod_time, length;
12724
12725 line_ptr += bytes_read;
12726 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12727 line_ptr += bytes_read;
12728 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12729 line_ptr += bytes_read;
12730 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12731 line_ptr += bytes_read;
12732
12733 add_file_name (lh, cur_file, dir_index, mod_time, length);
12734 }
12735 line_ptr += bytes_read;
6e70227d 12736 lh->statement_program_start = line_ptr;
debd256d 12737
3019eac3 12738 if (line_ptr > (section->buffer + section->size))
4d3c2250 12739 complaint (&symfile_complaints,
3e43a32a
MS
12740 _("line number info header doesn't "
12741 "fit in `.debug_line' section"));
debd256d
JB
12742
12743 discard_cleanups (back_to);
12744 return lh;
12745}
c906108c 12746
c6da4cef
DE
12747/* Subroutine of dwarf_decode_lines to simplify it.
12748 Return the file name of the psymtab for included file FILE_INDEX
12749 in line header LH of PST.
12750 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12751 If space for the result is malloc'd, it will be freed by a cleanup.
12752 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12753
12754static char *
12755psymtab_include_file_name (const struct line_header *lh, int file_index,
12756 const struct partial_symtab *pst,
12757 const char *comp_dir)
12758{
12759 const struct file_entry fe = lh->file_names [file_index];
12760 char *include_name = fe.name;
12761 char *include_name_to_compare = include_name;
12762 char *dir_name = NULL;
72b9f47f
TT
12763 const char *pst_filename;
12764 char *copied_name = NULL;
c6da4cef
DE
12765 int file_is_pst;
12766
12767 if (fe.dir_index)
12768 dir_name = lh->include_dirs[fe.dir_index - 1];
12769
12770 if (!IS_ABSOLUTE_PATH (include_name)
12771 && (dir_name != NULL || comp_dir != NULL))
12772 {
12773 /* Avoid creating a duplicate psymtab for PST.
12774 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12775 Before we do the comparison, however, we need to account
12776 for DIR_NAME and COMP_DIR.
12777 First prepend dir_name (if non-NULL). If we still don't
12778 have an absolute path prepend comp_dir (if non-NULL).
12779 However, the directory we record in the include-file's
12780 psymtab does not contain COMP_DIR (to match the
12781 corresponding symtab(s)).
12782
12783 Example:
12784
12785 bash$ cd /tmp
12786 bash$ gcc -g ./hello.c
12787 include_name = "hello.c"
12788 dir_name = "."
12789 DW_AT_comp_dir = comp_dir = "/tmp"
12790 DW_AT_name = "./hello.c" */
12791
12792 if (dir_name != NULL)
12793 {
12794 include_name = concat (dir_name, SLASH_STRING,
12795 include_name, (char *)NULL);
12796 include_name_to_compare = include_name;
12797 make_cleanup (xfree, include_name);
12798 }
12799 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12800 {
12801 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12802 include_name, (char *)NULL);
12803 }
12804 }
12805
12806 pst_filename = pst->filename;
12807 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12808 {
72b9f47f
TT
12809 copied_name = concat (pst->dirname, SLASH_STRING,
12810 pst_filename, (char *)NULL);
12811 pst_filename = copied_name;
c6da4cef
DE
12812 }
12813
1e3fad37 12814 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
12815
12816 if (include_name_to_compare != include_name)
12817 xfree (include_name_to_compare);
72b9f47f
TT
12818 if (copied_name != NULL)
12819 xfree (copied_name);
c6da4cef
DE
12820
12821 if (file_is_pst)
12822 return NULL;
12823 return include_name;
12824}
12825
c91513d8
PP
12826/* Ignore this record_line request. */
12827
12828static void
12829noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12830{
12831 return;
12832}
12833
f3f5162e
DE
12834/* Subroutine of dwarf_decode_lines to simplify it.
12835 Process the line number information in LH. */
debd256d 12836
c906108c 12837static void
f3f5162e
DE
12838dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
12839 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 12840{
a8c50c1f 12841 gdb_byte *line_ptr, *extended_end;
fe1b8b76 12842 gdb_byte *line_end;
a8c50c1f 12843 unsigned int bytes_read, extended_len;
c906108c 12844 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
12845 CORE_ADDR baseaddr;
12846 struct objfile *objfile = cu->objfile;
f3f5162e 12847 bfd *abfd = objfile->obfd;
fbf65064 12848 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 12849 const int decode_for_pst_p = (pst != NULL);
f3f5162e 12850 struct subfile *last_subfile = NULL;
c91513d8
PP
12851 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
12852 = record_line;
e142c38c
DJ
12853
12854 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 12855
debd256d
JB
12856 line_ptr = lh->statement_program_start;
12857 line_end = lh->statement_program_end;
c906108c
SS
12858
12859 /* Read the statement sequences until there's nothing left. */
12860 while (line_ptr < line_end)
12861 {
12862 /* state machine registers */
12863 CORE_ADDR address = 0;
12864 unsigned int file = 1;
12865 unsigned int line = 1;
12866 unsigned int column = 0;
debd256d 12867 int is_stmt = lh->default_is_stmt;
c906108c
SS
12868 int basic_block = 0;
12869 int end_sequence = 0;
fbf65064 12870 CORE_ADDR addr;
2dc7f7b3 12871 unsigned char op_index = 0;
c906108c 12872
aaa75496 12873 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 12874 {
aaa75496 12875 /* Start a subfile for the current file of the state machine. */
debd256d
JB
12876 /* lh->include_dirs and lh->file_names are 0-based, but the
12877 directory and file name numbers in the statement program
12878 are 1-based. */
12879 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 12880 char *dir = NULL;
a738430d 12881
debd256d
JB
12882 if (fe->dir_index)
12883 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
12884
12885 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
12886 }
12887
a738430d 12888 /* Decode the table. */
c5aa993b 12889 while (!end_sequence)
c906108c
SS
12890 {
12891 op_code = read_1_byte (abfd, line_ptr);
12892 line_ptr += 1;
59205f5a
JB
12893 if (line_ptr > line_end)
12894 {
12895 dwarf2_debug_line_missing_end_sequence_complaint ();
12896 break;
12897 }
9aa1fe7e 12898
debd256d 12899 if (op_code >= lh->opcode_base)
6e70227d 12900 {
a738430d 12901 /* Special operand. */
debd256d 12902 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
12903 address += (((op_index + (adj_opcode / lh->line_range))
12904 / lh->maximum_ops_per_instruction)
12905 * lh->minimum_instruction_length);
12906 op_index = ((op_index + (adj_opcode / lh->line_range))
12907 % lh->maximum_ops_per_instruction);
debd256d 12908 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 12909 if (lh->num_file_names < file || file == 0)
25e43795 12910 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
12911 /* For now we ignore lines not starting on an
12912 instruction boundary. */
12913 else if (op_index == 0)
25e43795
DJ
12914 {
12915 lh->file_names[file - 1].included_p = 1;
ca5f395d 12916 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
12917 {
12918 if (last_subfile != current_subfile)
12919 {
12920 addr = gdbarch_addr_bits_remove (gdbarch, address);
12921 if (last_subfile)
c91513d8 12922 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
12923 last_subfile = current_subfile;
12924 }
25e43795 12925 /* Append row to matrix using current values. */
7019d805 12926 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 12927 (*p_record_line) (current_subfile, line, addr);
366da635 12928 }
25e43795 12929 }
ca5f395d 12930 basic_block = 0;
9aa1fe7e
GK
12931 }
12932 else switch (op_code)
c906108c
SS
12933 {
12934 case DW_LNS_extended_op:
3e43a32a
MS
12935 extended_len = read_unsigned_leb128 (abfd, line_ptr,
12936 &bytes_read);
473b7be6 12937 line_ptr += bytes_read;
a8c50c1f 12938 extended_end = line_ptr + extended_len;
c906108c
SS
12939 extended_op = read_1_byte (abfd, line_ptr);
12940 line_ptr += 1;
12941 switch (extended_op)
12942 {
12943 case DW_LNE_end_sequence:
c91513d8 12944 p_record_line = record_line;
c906108c 12945 end_sequence = 1;
c906108c
SS
12946 break;
12947 case DW_LNE_set_address:
e7c27a73 12948 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
12949
12950 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
12951 {
12952 /* This line table is for a function which has been
12953 GCd by the linker. Ignore it. PR gdb/12528 */
12954
12955 long line_offset
12956 = line_ptr - dwarf2_per_objfile->line.buffer;
12957
12958 complaint (&symfile_complaints,
12959 _(".debug_line address at offset 0x%lx is 0 "
12960 "[in module %s]"),
bb5ed363 12961 line_offset, objfile->name);
c91513d8
PP
12962 p_record_line = noop_record_line;
12963 }
12964
2dc7f7b3 12965 op_index = 0;
107d2387
AC
12966 line_ptr += bytes_read;
12967 address += baseaddr;
c906108c
SS
12968 break;
12969 case DW_LNE_define_file:
debd256d
JB
12970 {
12971 char *cur_file;
12972 unsigned int dir_index, mod_time, length;
6e70227d 12973
3e43a32a
MS
12974 cur_file = read_direct_string (abfd, line_ptr,
12975 &bytes_read);
debd256d
JB
12976 line_ptr += bytes_read;
12977 dir_index =
12978 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12979 line_ptr += bytes_read;
12980 mod_time =
12981 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12982 line_ptr += bytes_read;
12983 length =
12984 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12985 line_ptr += bytes_read;
12986 add_file_name (lh, cur_file, dir_index, mod_time, length);
12987 }
c906108c 12988 break;
d0c6ba3d
CC
12989 case DW_LNE_set_discriminator:
12990 /* The discriminator is not interesting to the debugger;
12991 just ignore it. */
12992 line_ptr = extended_end;
12993 break;
c906108c 12994 default:
4d3c2250 12995 complaint (&symfile_complaints,
e2e0b3e5 12996 _("mangled .debug_line section"));
debd256d 12997 return;
c906108c 12998 }
a8c50c1f
DJ
12999 /* Make sure that we parsed the extended op correctly. If e.g.
13000 we expected a different address size than the producer used,
13001 we may have read the wrong number of bytes. */
13002 if (line_ptr != extended_end)
13003 {
13004 complaint (&symfile_complaints,
13005 _("mangled .debug_line section"));
13006 return;
13007 }
c906108c
SS
13008 break;
13009 case DW_LNS_copy:
59205f5a 13010 if (lh->num_file_names < file || file == 0)
25e43795
DJ
13011 dwarf2_debug_line_missing_file_complaint ();
13012 else
366da635 13013 {
25e43795 13014 lh->file_names[file - 1].included_p = 1;
ca5f395d 13015 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
13016 {
13017 if (last_subfile != current_subfile)
13018 {
13019 addr = gdbarch_addr_bits_remove (gdbarch, address);
13020 if (last_subfile)
c91513d8 13021 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
13022 last_subfile = current_subfile;
13023 }
7019d805 13024 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13025 (*p_record_line) (current_subfile, line, addr);
fbf65064 13026 }
366da635 13027 }
c906108c
SS
13028 basic_block = 0;
13029 break;
13030 case DW_LNS_advance_pc:
2dc7f7b3
TT
13031 {
13032 CORE_ADDR adjust
13033 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13034
13035 address += (((op_index + adjust)
13036 / lh->maximum_ops_per_instruction)
13037 * lh->minimum_instruction_length);
13038 op_index = ((op_index + adjust)
13039 % lh->maximum_ops_per_instruction);
13040 line_ptr += bytes_read;
13041 }
c906108c
SS
13042 break;
13043 case DW_LNS_advance_line:
13044 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
13045 line_ptr += bytes_read;
13046 break;
13047 case DW_LNS_set_file:
debd256d 13048 {
a738430d
MK
13049 /* The arrays lh->include_dirs and lh->file_names are
13050 0-based, but the directory and file name numbers in
13051 the statement program are 1-based. */
debd256d 13052 struct file_entry *fe;
4f1520fb 13053 char *dir = NULL;
a738430d 13054
debd256d
JB
13055 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13056 line_ptr += bytes_read;
59205f5a 13057 if (lh->num_file_names < file || file == 0)
25e43795
DJ
13058 dwarf2_debug_line_missing_file_complaint ();
13059 else
13060 {
13061 fe = &lh->file_names[file - 1];
13062 if (fe->dir_index)
13063 dir = lh->include_dirs[fe->dir_index - 1];
13064 if (!decode_for_pst_p)
13065 {
13066 last_subfile = current_subfile;
13067 dwarf2_start_subfile (fe->name, dir, comp_dir);
13068 }
13069 }
debd256d 13070 }
c906108c
SS
13071 break;
13072 case DW_LNS_set_column:
13073 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13074 line_ptr += bytes_read;
13075 break;
13076 case DW_LNS_negate_stmt:
13077 is_stmt = (!is_stmt);
13078 break;
13079 case DW_LNS_set_basic_block:
13080 basic_block = 1;
13081 break;
c2c6d25f
JM
13082 /* Add to the address register of the state machine the
13083 address increment value corresponding to special opcode
a738430d
MK
13084 255. I.e., this value is scaled by the minimum
13085 instruction length since special opcode 255 would have
b021a221 13086 scaled the increment. */
c906108c 13087 case DW_LNS_const_add_pc:
2dc7f7b3
TT
13088 {
13089 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13090
13091 address += (((op_index + adjust)
13092 / lh->maximum_ops_per_instruction)
13093 * lh->minimum_instruction_length);
13094 op_index = ((op_index + adjust)
13095 % lh->maximum_ops_per_instruction);
13096 }
c906108c
SS
13097 break;
13098 case DW_LNS_fixed_advance_pc:
13099 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 13100 op_index = 0;
c906108c
SS
13101 line_ptr += 2;
13102 break;
9aa1fe7e 13103 default:
a738430d
MK
13104 {
13105 /* Unknown standard opcode, ignore it. */
9aa1fe7e 13106 int i;
a738430d 13107
debd256d 13108 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
13109 {
13110 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13111 line_ptr += bytes_read;
13112 }
13113 }
c906108c
SS
13114 }
13115 }
59205f5a
JB
13116 if (lh->num_file_names < file || file == 0)
13117 dwarf2_debug_line_missing_file_complaint ();
13118 else
13119 {
13120 lh->file_names[file - 1].included_p = 1;
13121 if (!decode_for_pst_p)
fbf65064
UW
13122 {
13123 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13124 (*p_record_line) (current_subfile, 0, addr);
fbf65064 13125 }
59205f5a 13126 }
c906108c 13127 }
f3f5162e
DE
13128}
13129
13130/* Decode the Line Number Program (LNP) for the given line_header
13131 structure and CU. The actual information extracted and the type
13132 of structures created from the LNP depends on the value of PST.
13133
13134 1. If PST is NULL, then this procedure uses the data from the program
13135 to create all necessary symbol tables, and their linetables.
13136
13137 2. If PST is not NULL, this procedure reads the program to determine
13138 the list of files included by the unit represented by PST, and
13139 builds all the associated partial symbol tables.
13140
13141 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13142 It is used for relative paths in the line table.
13143 NOTE: When processing partial symtabs (pst != NULL),
13144 comp_dir == pst->dirname.
13145
13146 NOTE: It is important that psymtabs have the same file name (via strcmp)
13147 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13148 symtab we don't use it in the name of the psymtabs we create.
13149 E.g. expand_line_sal requires this when finding psymtabs to expand.
13150 A good testcase for this is mb-inline.exp. */
13151
13152static void
13153dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13154 struct dwarf2_cu *cu, struct partial_symtab *pst,
13155 int want_line_info)
13156{
13157 struct objfile *objfile = cu->objfile;
13158 const int decode_for_pst_p = (pst != NULL);
13159 struct subfile *first_subfile = current_subfile;
13160
13161 if (want_line_info)
13162 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
13163
13164 if (decode_for_pst_p)
13165 {
13166 int file_index;
13167
13168 /* Now that we're done scanning the Line Header Program, we can
13169 create the psymtab of each included file. */
13170 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13171 if (lh->file_names[file_index].included_p == 1)
13172 {
c6da4cef
DE
13173 char *include_name =
13174 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13175 if (include_name != NULL)
aaa75496
JB
13176 dwarf2_create_include_psymtab (include_name, pst, objfile);
13177 }
13178 }
cb1df416
DJ
13179 else
13180 {
13181 /* Make sure a symtab is created for every file, even files
13182 which contain only variables (i.e. no code with associated
13183 line numbers). */
cb1df416 13184 int i;
cb1df416
DJ
13185
13186 for (i = 0; i < lh->num_file_names; i++)
13187 {
13188 char *dir = NULL;
f3f5162e 13189 struct file_entry *fe;
9a619af0 13190
cb1df416
DJ
13191 fe = &lh->file_names[i];
13192 if (fe->dir_index)
13193 dir = lh->include_dirs[fe->dir_index - 1];
13194 dwarf2_start_subfile (fe->name, dir, comp_dir);
13195
13196 /* Skip the main file; we don't need it, and it must be
13197 allocated last, so that it will show up before the
13198 non-primary symtabs in the objfile's symtab list. */
13199 if (current_subfile == first_subfile)
13200 continue;
13201
13202 if (current_subfile->symtab == NULL)
13203 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 13204 objfile);
cb1df416
DJ
13205 fe->symtab = current_subfile->symtab;
13206 }
13207 }
c906108c
SS
13208}
13209
13210/* Start a subfile for DWARF. FILENAME is the name of the file and
13211 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
13212 or NULL if not known. COMP_DIR is the compilation directory for the
13213 linetable's compilation unit or NULL if not known.
c906108c
SS
13214 This routine tries to keep line numbers from identical absolute and
13215 relative file names in a common subfile.
13216
13217 Using the `list' example from the GDB testsuite, which resides in
13218 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13219 of /srcdir/list0.c yields the following debugging information for list0.c:
13220
c5aa993b
JM
13221 DW_AT_name: /srcdir/list0.c
13222 DW_AT_comp_dir: /compdir
357e46e7 13223 files.files[0].name: list0.h
c5aa993b 13224 files.files[0].dir: /srcdir
357e46e7 13225 files.files[1].name: list0.c
c5aa993b 13226 files.files[1].dir: /srcdir
c906108c
SS
13227
13228 The line number information for list0.c has to end up in a single
4f1520fb
FR
13229 subfile, so that `break /srcdir/list0.c:1' works as expected.
13230 start_subfile will ensure that this happens provided that we pass the
13231 concatenation of files.files[1].dir and files.files[1].name as the
13232 subfile's name. */
c906108c
SS
13233
13234static void
3e43a32a
MS
13235dwarf2_start_subfile (char *filename, const char *dirname,
13236 const char *comp_dir)
c906108c 13237{
4f1520fb
FR
13238 char *fullname;
13239
13240 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13241 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13242 second argument to start_subfile. To be consistent, we do the
13243 same here. In order not to lose the line information directory,
13244 we concatenate it to the filename when it makes sense.
13245 Note that the Dwarf3 standard says (speaking of filenames in line
13246 information): ``The directory index is ignored for file names
13247 that represent full path names''. Thus ignoring dirname in the
13248 `else' branch below isn't an issue. */
c906108c 13249
d5166ae1 13250 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
13251 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13252 else
13253 fullname = filename;
c906108c 13254
4f1520fb
FR
13255 start_subfile (fullname, comp_dir);
13256
13257 if (fullname != filename)
13258 xfree (fullname);
c906108c
SS
13259}
13260
4c2df51b
DJ
13261static void
13262var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 13263 struct dwarf2_cu *cu)
4c2df51b 13264{
e7c27a73
DJ
13265 struct objfile *objfile = cu->objfile;
13266 struct comp_unit_head *cu_header = &cu->header;
13267
4c2df51b
DJ
13268 /* NOTE drow/2003-01-30: There used to be a comment and some special
13269 code here to turn a symbol with DW_AT_external and a
13270 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13271 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13272 with some versions of binutils) where shared libraries could have
13273 relocations against symbols in their debug information - the
13274 minimal symbol would have the right address, but the debug info
13275 would not. It's no longer necessary, because we will explicitly
13276 apply relocations when we read in the debug information now. */
13277
13278 /* A DW_AT_location attribute with no contents indicates that a
13279 variable has been optimized away. */
13280 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13281 {
13282 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13283 return;
13284 }
13285
13286 /* Handle one degenerate form of location expression specially, to
13287 preserve GDB's previous behavior when section offsets are
3019eac3
DE
13288 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13289 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
13290
13291 if (attr_form_is_block (attr)
3019eac3
DE
13292 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13293 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13294 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13295 && (DW_BLOCK (attr)->size
13296 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 13297 {
891d2f0b 13298 unsigned int dummy;
4c2df51b 13299
3019eac3
DE
13300 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13301 SYMBOL_VALUE_ADDRESS (sym) =
13302 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13303 else
13304 SYMBOL_VALUE_ADDRESS (sym) =
13305 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 13306 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
13307 fixup_symbol_section (sym, objfile);
13308 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13309 SYMBOL_SECTION (sym));
4c2df51b
DJ
13310 return;
13311 }
13312
13313 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13314 expression evaluator, and use LOC_COMPUTED only when necessary
13315 (i.e. when the value of a register or memory location is
13316 referenced, or a thread-local block, etc.). Then again, it might
13317 not be worthwhile. I'm assuming that it isn't unless performance
13318 or memory numbers show me otherwise. */
13319
e7c27a73 13320 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 13321 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
13322
13323 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13324 cu->has_loclist = 1;
4c2df51b
DJ
13325}
13326
c906108c
SS
13327/* Given a pointer to a DWARF information entry, figure out if we need
13328 to make a symbol table entry for it, and if so, create a new entry
13329 and return a pointer to it.
13330 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
13331 used the passed type.
13332 If SPACE is not NULL, use it to hold the new symbol. If it is
13333 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
13334
13335static struct symbol *
34eaf542
TT
13336new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13337 struct symbol *space)
c906108c 13338{
e7c27a73 13339 struct objfile *objfile = cu->objfile;
c906108c
SS
13340 struct symbol *sym = NULL;
13341 char *name;
13342 struct attribute *attr = NULL;
13343 struct attribute *attr2 = NULL;
e142c38c 13344 CORE_ADDR baseaddr;
e37fd15a
SW
13345 struct pending **list_to_add = NULL;
13346
edb3359d 13347 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
13348
13349 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 13350
94af9270 13351 name = dwarf2_name (die, cu);
c906108c
SS
13352 if (name)
13353 {
94af9270 13354 const char *linkagename;
34eaf542 13355 int suppress_add = 0;
94af9270 13356
34eaf542
TT
13357 if (space)
13358 sym = space;
13359 else
13360 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 13361 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
13362
13363 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 13364 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
13365 linkagename = dwarf2_physname (name, die, cu);
13366 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 13367
f55ee35c
JK
13368 /* Fortran does not have mangling standard and the mangling does differ
13369 between gfortran, iFort etc. */
13370 if (cu->language == language_fortran
b250c185 13371 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
13372 symbol_set_demangled_name (&(sym->ginfo),
13373 (char *) dwarf2_full_name (name, die, cu),
13374 NULL);
f55ee35c 13375
c906108c 13376 /* Default assumptions.
c5aa993b 13377 Use the passed type or decode it from the die. */
176620f1 13378 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 13379 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
13380 if (type != NULL)
13381 SYMBOL_TYPE (sym) = type;
13382 else
e7c27a73 13383 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
13384 attr = dwarf2_attr (die,
13385 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13386 cu);
c906108c
SS
13387 if (attr)
13388 {
13389 SYMBOL_LINE (sym) = DW_UNSND (attr);
13390 }
cb1df416 13391
edb3359d
DJ
13392 attr = dwarf2_attr (die,
13393 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13394 cu);
cb1df416
DJ
13395 if (attr)
13396 {
13397 int file_index = DW_UNSND (attr);
9a619af0 13398
cb1df416
DJ
13399 if (cu->line_header == NULL
13400 || file_index > cu->line_header->num_file_names)
13401 complaint (&symfile_complaints,
13402 _("file index out of range"));
1c3d648d 13403 else if (file_index > 0)
cb1df416
DJ
13404 {
13405 struct file_entry *fe;
9a619af0 13406
cb1df416
DJ
13407 fe = &cu->line_header->file_names[file_index - 1];
13408 SYMBOL_SYMTAB (sym) = fe->symtab;
13409 }
13410 }
13411
c906108c
SS
13412 switch (die->tag)
13413 {
13414 case DW_TAG_label:
e142c38c 13415 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
13416 if (attr)
13417 {
13418 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13419 }
0f5238ed
TT
13420 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13421 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 13422 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 13423 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
13424 break;
13425 case DW_TAG_subprogram:
13426 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13427 finish_block. */
13428 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 13429 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
13430 if ((attr2 && (DW_UNSND (attr2) != 0))
13431 || cu->language == language_ada)
c906108c 13432 {
2cfa0c8d
JB
13433 /* Subprograms marked external are stored as a global symbol.
13434 Ada subprograms, whether marked external or not, are always
13435 stored as a global symbol, because we want to be able to
13436 access them globally. For instance, we want to be able
13437 to break on a nested subprogram without having to
13438 specify the context. */
e37fd15a 13439 list_to_add = &global_symbols;
c906108c
SS
13440 }
13441 else
13442 {
e37fd15a 13443 list_to_add = cu->list_in_scope;
c906108c
SS
13444 }
13445 break;
edb3359d
DJ
13446 case DW_TAG_inlined_subroutine:
13447 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13448 finish_block. */
13449 SYMBOL_CLASS (sym) = LOC_BLOCK;
13450 SYMBOL_INLINED (sym) = 1;
481860b3 13451 list_to_add = cu->list_in_scope;
edb3359d 13452 break;
34eaf542
TT
13453 case DW_TAG_template_value_param:
13454 suppress_add = 1;
13455 /* Fall through. */
72929c62 13456 case DW_TAG_constant:
c906108c 13457 case DW_TAG_variable:
254e6b9e 13458 case DW_TAG_member:
0963b4bd
MS
13459 /* Compilation with minimal debug info may result in
13460 variables with missing type entries. Change the
13461 misleading `void' type to something sensible. */
c906108c 13462 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 13463 SYMBOL_TYPE (sym)
46bf5051 13464 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 13465
e142c38c 13466 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
13467 /* In the case of DW_TAG_member, we should only be called for
13468 static const members. */
13469 if (die->tag == DW_TAG_member)
13470 {
3863f96c
DE
13471 /* dwarf2_add_field uses die_is_declaration,
13472 so we do the same. */
254e6b9e
DE
13473 gdb_assert (die_is_declaration (die, cu));
13474 gdb_assert (attr);
13475 }
c906108c
SS
13476 if (attr)
13477 {
e7c27a73 13478 dwarf2_const_value (attr, sym, cu);
e142c38c 13479 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 13480 if (!suppress_add)
34eaf542
TT
13481 {
13482 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 13483 list_to_add = &global_symbols;
34eaf542 13484 else
e37fd15a 13485 list_to_add = cu->list_in_scope;
34eaf542 13486 }
c906108c
SS
13487 break;
13488 }
e142c38c 13489 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13490 if (attr)
13491 {
e7c27a73 13492 var_decode_location (attr, sym, cu);
e142c38c 13493 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
13494 if (SYMBOL_CLASS (sym) == LOC_STATIC
13495 && SYMBOL_VALUE_ADDRESS (sym) == 0
13496 && !dwarf2_per_objfile->has_section_at_zero)
13497 {
13498 /* When a static variable is eliminated by the linker,
13499 the corresponding debug information is not stripped
13500 out, but the variable address is set to null;
13501 do not add such variables into symbol table. */
13502 }
13503 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 13504 {
f55ee35c
JK
13505 /* Workaround gfortran PR debug/40040 - it uses
13506 DW_AT_location for variables in -fPIC libraries which may
13507 get overriden by other libraries/executable and get
13508 a different address. Resolve it by the minimal symbol
13509 which may come from inferior's executable using copy
13510 relocation. Make this workaround only for gfortran as for
13511 other compilers GDB cannot guess the minimal symbol
13512 Fortran mangling kind. */
13513 if (cu->language == language_fortran && die->parent
13514 && die->parent->tag == DW_TAG_module
13515 && cu->producer
13516 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13517 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13518
1c809c68
TT
13519 /* A variable with DW_AT_external is never static,
13520 but it may be block-scoped. */
13521 list_to_add = (cu->list_in_scope == &file_symbols
13522 ? &global_symbols : cu->list_in_scope);
1c809c68 13523 }
c906108c 13524 else
e37fd15a 13525 list_to_add = cu->list_in_scope;
c906108c
SS
13526 }
13527 else
13528 {
13529 /* We do not know the address of this symbol.
c5aa993b
JM
13530 If it is an external symbol and we have type information
13531 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13532 The address of the variable will then be determined from
13533 the minimal symbol table whenever the variable is
13534 referenced. */
e142c38c 13535 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 13536 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 13537 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 13538 {
0fe7935b
DJ
13539 /* A variable with DW_AT_external is never static, but it
13540 may be block-scoped. */
13541 list_to_add = (cu->list_in_scope == &file_symbols
13542 ? &global_symbols : cu->list_in_scope);
13543
c906108c 13544 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 13545 }
442ddf59
JK
13546 else if (!die_is_declaration (die, cu))
13547 {
13548 /* Use the default LOC_OPTIMIZED_OUT class. */
13549 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
13550 if (!suppress_add)
13551 list_to_add = cu->list_in_scope;
442ddf59 13552 }
c906108c
SS
13553 }
13554 break;
13555 case DW_TAG_formal_parameter:
edb3359d
DJ
13556 /* If we are inside a function, mark this as an argument. If
13557 not, we might be looking at an argument to an inlined function
13558 when we do not have enough information to show inlined frames;
13559 pretend it's a local variable in that case so that the user can
13560 still see it. */
13561 if (context_stack_depth > 0
13562 && context_stack[context_stack_depth - 1].name != NULL)
13563 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 13564 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13565 if (attr)
13566 {
e7c27a73 13567 var_decode_location (attr, sym, cu);
c906108c 13568 }
e142c38c 13569 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13570 if (attr)
13571 {
e7c27a73 13572 dwarf2_const_value (attr, sym, cu);
c906108c 13573 }
f346a30d 13574
e37fd15a 13575 list_to_add = cu->list_in_scope;
c906108c
SS
13576 break;
13577 case DW_TAG_unspecified_parameters:
13578 /* From varargs functions; gdb doesn't seem to have any
13579 interest in this information, so just ignore it for now.
13580 (FIXME?) */
13581 break;
34eaf542
TT
13582 case DW_TAG_template_type_param:
13583 suppress_add = 1;
13584 /* Fall through. */
c906108c 13585 case DW_TAG_class_type:
680b30c7 13586 case DW_TAG_interface_type:
c906108c
SS
13587 case DW_TAG_structure_type:
13588 case DW_TAG_union_type:
72019c9c 13589 case DW_TAG_set_type:
c906108c
SS
13590 case DW_TAG_enumeration_type:
13591 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13592 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 13593
63d06c5c 13594 {
987504bb 13595 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
13596 really ever be static objects: otherwise, if you try
13597 to, say, break of a class's method and you're in a file
13598 which doesn't mention that class, it won't work unless
13599 the check for all static symbols in lookup_symbol_aux
13600 saves you. See the OtherFileClass tests in
13601 gdb.c++/namespace.exp. */
13602
e37fd15a 13603 if (!suppress_add)
34eaf542 13604 {
34eaf542
TT
13605 list_to_add = (cu->list_in_scope == &file_symbols
13606 && (cu->language == language_cplus
13607 || cu->language == language_java)
13608 ? &global_symbols : cu->list_in_scope);
63d06c5c 13609
64382290
TT
13610 /* The semantics of C++ state that "struct foo {
13611 ... }" also defines a typedef for "foo". A Java
13612 class declaration also defines a typedef for the
13613 class. */
13614 if (cu->language == language_cplus
13615 || cu->language == language_java
13616 || cu->language == language_ada)
13617 {
13618 /* The symbol's name is already allocated along
13619 with this objfile, so we don't need to
13620 duplicate it for the type. */
13621 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13622 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13623 }
63d06c5c
DC
13624 }
13625 }
c906108c
SS
13626 break;
13627 case DW_TAG_typedef:
63d06c5c
DC
13628 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13629 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13630 list_to_add = cu->list_in_scope;
63d06c5c 13631 break;
c906108c 13632 case DW_TAG_base_type:
a02abb62 13633 case DW_TAG_subrange_type:
c906108c 13634 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13635 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13636 list_to_add = cu->list_in_scope;
c906108c
SS
13637 break;
13638 case DW_TAG_enumerator:
e142c38c 13639 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13640 if (attr)
13641 {
e7c27a73 13642 dwarf2_const_value (attr, sym, cu);
c906108c 13643 }
63d06c5c
DC
13644 {
13645 /* NOTE: carlton/2003-11-10: See comment above in the
13646 DW_TAG_class_type, etc. block. */
13647
e142c38c 13648 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
13649 && (cu->language == language_cplus
13650 || cu->language == language_java)
e142c38c 13651 ? &global_symbols : cu->list_in_scope);
63d06c5c 13652 }
c906108c 13653 break;
5c4e30ca
DC
13654 case DW_TAG_namespace:
13655 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 13656 list_to_add = &global_symbols;
5c4e30ca 13657 break;
c906108c
SS
13658 default:
13659 /* Not a tag we recognize. Hopefully we aren't processing
13660 trash data, but since we must specifically ignore things
13661 we don't recognize, there is nothing else we should do at
0963b4bd 13662 this point. */
e2e0b3e5 13663 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 13664 dwarf_tag_name (die->tag));
c906108c
SS
13665 break;
13666 }
df8a16a1 13667
e37fd15a
SW
13668 if (suppress_add)
13669 {
13670 sym->hash_next = objfile->template_symbols;
13671 objfile->template_symbols = sym;
13672 list_to_add = NULL;
13673 }
13674
13675 if (list_to_add != NULL)
13676 add_symbol_to_list (sym, list_to_add);
13677
df8a16a1
DJ
13678 /* For the benefit of old versions of GCC, check for anonymous
13679 namespaces based on the demangled name. */
13680 if (!processing_has_namespace_info
94af9270 13681 && cu->language == language_cplus)
a10964d1 13682 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
13683 }
13684 return (sym);
13685}
13686
34eaf542
TT
13687/* A wrapper for new_symbol_full that always allocates a new symbol. */
13688
13689static struct symbol *
13690new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13691{
13692 return new_symbol_full (die, type, cu, NULL);
13693}
13694
98bfdba5
PA
13695/* Given an attr with a DW_FORM_dataN value in host byte order,
13696 zero-extend it as appropriate for the symbol's type. The DWARF
13697 standard (v4) is not entirely clear about the meaning of using
13698 DW_FORM_dataN for a constant with a signed type, where the type is
13699 wider than the data. The conclusion of a discussion on the DWARF
13700 list was that this is unspecified. We choose to always zero-extend
13701 because that is the interpretation long in use by GCC. */
c906108c 13702
98bfdba5
PA
13703static gdb_byte *
13704dwarf2_const_value_data (struct attribute *attr, struct type *type,
13705 const char *name, struct obstack *obstack,
12df843f 13706 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 13707{
e7c27a73 13708 struct objfile *objfile = cu->objfile;
e17a4113
UW
13709 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13710 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
13711 LONGEST l = DW_UNSND (attr);
13712
13713 if (bits < sizeof (*value) * 8)
13714 {
13715 l &= ((LONGEST) 1 << bits) - 1;
13716 *value = l;
13717 }
13718 else if (bits == sizeof (*value) * 8)
13719 *value = l;
13720 else
13721 {
13722 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13723 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13724 return bytes;
13725 }
13726
13727 return NULL;
13728}
13729
13730/* Read a constant value from an attribute. Either set *VALUE, or if
13731 the value does not fit in *VALUE, set *BYTES - either already
13732 allocated on the objfile obstack, or newly allocated on OBSTACK,
13733 or, set *BATON, if we translated the constant to a location
13734 expression. */
13735
13736static void
13737dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13738 const char *name, struct obstack *obstack,
13739 struct dwarf2_cu *cu,
12df843f 13740 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
13741 struct dwarf2_locexpr_baton **baton)
13742{
13743 struct objfile *objfile = cu->objfile;
13744 struct comp_unit_head *cu_header = &cu->header;
c906108c 13745 struct dwarf_block *blk;
98bfdba5
PA
13746 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13747 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13748
13749 *value = 0;
13750 *bytes = NULL;
13751 *baton = NULL;
c906108c
SS
13752
13753 switch (attr->form)
13754 {
13755 case DW_FORM_addr:
3019eac3 13756 case DW_FORM_GNU_addr_index:
ac56253d 13757 {
ac56253d
TT
13758 gdb_byte *data;
13759
98bfdba5
PA
13760 if (TYPE_LENGTH (type) != cu_header->addr_size)
13761 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 13762 cu_header->addr_size,
98bfdba5 13763 TYPE_LENGTH (type));
ac56253d
TT
13764 /* Symbols of this form are reasonably rare, so we just
13765 piggyback on the existing location code rather than writing
13766 a new implementation of symbol_computed_ops. */
98bfdba5
PA
13767 *baton = obstack_alloc (&objfile->objfile_obstack,
13768 sizeof (struct dwarf2_locexpr_baton));
13769 (*baton)->per_cu = cu->per_cu;
13770 gdb_assert ((*baton)->per_cu);
ac56253d 13771
98bfdba5
PA
13772 (*baton)->size = 2 + cu_header->addr_size;
13773 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13774 (*baton)->data = data;
ac56253d
TT
13775
13776 data[0] = DW_OP_addr;
13777 store_unsigned_integer (&data[1], cu_header->addr_size,
13778 byte_order, DW_ADDR (attr));
13779 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 13780 }
c906108c 13781 break;
4ac36638 13782 case DW_FORM_string:
93b5768b 13783 case DW_FORM_strp:
3019eac3 13784 case DW_FORM_GNU_str_index:
98bfdba5
PA
13785 /* DW_STRING is already allocated on the objfile obstack, point
13786 directly to it. */
13787 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 13788 break;
c906108c
SS
13789 case DW_FORM_block1:
13790 case DW_FORM_block2:
13791 case DW_FORM_block4:
13792 case DW_FORM_block:
2dc7f7b3 13793 case DW_FORM_exprloc:
c906108c 13794 blk = DW_BLOCK (attr);
98bfdba5
PA
13795 if (TYPE_LENGTH (type) != blk->size)
13796 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13797 TYPE_LENGTH (type));
13798 *bytes = blk->data;
c906108c 13799 break;
2df3850c
JM
13800
13801 /* The DW_AT_const_value attributes are supposed to carry the
13802 symbol's value "represented as it would be on the target
13803 architecture." By the time we get here, it's already been
13804 converted to host endianness, so we just need to sign- or
13805 zero-extend it as appropriate. */
13806 case DW_FORM_data1:
3e43a32a
MS
13807 *bytes = dwarf2_const_value_data (attr, type, name,
13808 obstack, cu, value, 8);
2df3850c 13809 break;
c906108c 13810 case DW_FORM_data2:
3e43a32a
MS
13811 *bytes = dwarf2_const_value_data (attr, type, name,
13812 obstack, cu, value, 16);
2df3850c 13813 break;
c906108c 13814 case DW_FORM_data4:
3e43a32a
MS
13815 *bytes = dwarf2_const_value_data (attr, type, name,
13816 obstack, cu, value, 32);
2df3850c 13817 break;
c906108c 13818 case DW_FORM_data8:
3e43a32a
MS
13819 *bytes = dwarf2_const_value_data (attr, type, name,
13820 obstack, cu, value, 64);
2df3850c
JM
13821 break;
13822
c906108c 13823 case DW_FORM_sdata:
98bfdba5 13824 *value = DW_SND (attr);
2df3850c
JM
13825 break;
13826
c906108c 13827 case DW_FORM_udata:
98bfdba5 13828 *value = DW_UNSND (attr);
c906108c 13829 break;
2df3850c 13830
c906108c 13831 default:
4d3c2250 13832 complaint (&symfile_complaints,
e2e0b3e5 13833 _("unsupported const value attribute form: '%s'"),
4d3c2250 13834 dwarf_form_name (attr->form));
98bfdba5 13835 *value = 0;
c906108c
SS
13836 break;
13837 }
13838}
13839
2df3850c 13840
98bfdba5
PA
13841/* Copy constant value from an attribute to a symbol. */
13842
2df3850c 13843static void
98bfdba5
PA
13844dwarf2_const_value (struct attribute *attr, struct symbol *sym,
13845 struct dwarf2_cu *cu)
2df3850c 13846{
98bfdba5
PA
13847 struct objfile *objfile = cu->objfile;
13848 struct comp_unit_head *cu_header = &cu->header;
12df843f 13849 LONGEST value;
98bfdba5
PA
13850 gdb_byte *bytes;
13851 struct dwarf2_locexpr_baton *baton;
2df3850c 13852
98bfdba5
PA
13853 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
13854 SYMBOL_PRINT_NAME (sym),
13855 &objfile->objfile_obstack, cu,
13856 &value, &bytes, &baton);
2df3850c 13857
98bfdba5
PA
13858 if (baton != NULL)
13859 {
13860 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13861 SYMBOL_LOCATION_BATON (sym) = baton;
13862 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13863 }
13864 else if (bytes != NULL)
13865 {
13866 SYMBOL_VALUE_BYTES (sym) = bytes;
13867 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
13868 }
13869 else
13870 {
13871 SYMBOL_VALUE (sym) = value;
13872 SYMBOL_CLASS (sym) = LOC_CONST;
13873 }
2df3850c
JM
13874}
13875
c906108c
SS
13876/* Return the type of the die in question using its DW_AT_type attribute. */
13877
13878static struct type *
e7c27a73 13879die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13880{
c906108c 13881 struct attribute *type_attr;
c906108c 13882
e142c38c 13883 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
13884 if (!type_attr)
13885 {
13886 /* A missing DW_AT_type represents a void type. */
46bf5051 13887 return objfile_type (cu->objfile)->builtin_void;
c906108c 13888 }
348e048f 13889
673bfd45 13890 return lookup_die_type (die, type_attr, cu);
c906108c
SS
13891}
13892
b4ba55a1
JB
13893/* True iff CU's producer generates GNAT Ada auxiliary information
13894 that allows to find parallel types through that information instead
13895 of having to do expensive parallel lookups by type name. */
13896
13897static int
13898need_gnat_info (struct dwarf2_cu *cu)
13899{
13900 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
13901 of GNAT produces this auxiliary information, without any indication
13902 that it is produced. Part of enhancing the FSF version of GNAT
13903 to produce that information will be to put in place an indicator
13904 that we can use in order to determine whether the descriptive type
13905 info is available or not. One suggestion that has been made is
13906 to use a new attribute, attached to the CU die. For now, assume
13907 that the descriptive type info is not available. */
13908 return 0;
13909}
13910
b4ba55a1
JB
13911/* Return the auxiliary type of the die in question using its
13912 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
13913 attribute is not present. */
13914
13915static struct type *
13916die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
13917{
b4ba55a1 13918 struct attribute *type_attr;
b4ba55a1
JB
13919
13920 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
13921 if (!type_attr)
13922 return NULL;
13923
673bfd45 13924 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
13925}
13926
13927/* If DIE has a descriptive_type attribute, then set the TYPE's
13928 descriptive type accordingly. */
13929
13930static void
13931set_descriptive_type (struct type *type, struct die_info *die,
13932 struct dwarf2_cu *cu)
13933{
13934 struct type *descriptive_type = die_descriptive_type (die, cu);
13935
13936 if (descriptive_type)
13937 {
13938 ALLOCATE_GNAT_AUX_TYPE (type);
13939 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
13940 }
13941}
13942
c906108c
SS
13943/* Return the containing type of the die in question using its
13944 DW_AT_containing_type attribute. */
13945
13946static struct type *
e7c27a73 13947die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13948{
c906108c 13949 struct attribute *type_attr;
c906108c 13950
e142c38c 13951 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
13952 if (!type_attr)
13953 error (_("Dwarf Error: Problem turning containing type into gdb type "
13954 "[in module %s]"), cu->objfile->name);
13955
673bfd45 13956 return lookup_die_type (die, type_attr, cu);
c906108c
SS
13957}
13958
673bfd45
DE
13959/* Look up the type of DIE in CU using its type attribute ATTR.
13960 If there is no type substitute an error marker. */
13961
c906108c 13962static struct type *
673bfd45
DE
13963lookup_die_type (struct die_info *die, struct attribute *attr,
13964 struct dwarf2_cu *cu)
c906108c 13965{
bb5ed363 13966 struct objfile *objfile = cu->objfile;
f792889a
DJ
13967 struct type *this_type;
13968
673bfd45
DE
13969 /* First see if we have it cached. */
13970
13971 if (is_ref_attr (attr))
13972 {
b64f50a1 13973 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
13974
13975 this_type = get_die_type_at_offset (offset, cu->per_cu);
13976 }
55f1336d 13977 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
13978 {
13979 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
13980
13981 /* sig_type will be NULL if the signatured type is missing from
13982 the debug info. */
13983 if (sig_type == NULL)
13984 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13985 "at 0x%x [in module %s]"),
b64f50a1 13986 die->offset.sect_off, objfile->name);
673bfd45 13987
3019eac3
DE
13988 gdb_assert (sig_type->per_cu.is_debug_types);
13989 /* If we haven't filled in type_offset_in_section yet, then we
13990 haven't read the type in yet. */
13991 this_type = NULL;
13992 if (sig_type->type_offset_in_section.sect_off != 0)
13993 {
13994 this_type =
13995 get_die_type_at_offset (sig_type->type_offset_in_section,
13996 &sig_type->per_cu);
13997 }
673bfd45
DE
13998 }
13999 else
14000 {
14001 dump_die_for_error (die);
14002 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 14003 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
14004 }
14005
14006 /* If not cached we need to read it in. */
14007
14008 if (this_type == NULL)
14009 {
14010 struct die_info *type_die;
14011 struct dwarf2_cu *type_cu = cu;
14012
14013 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
14014 /* If we found the type now, it's probably because the type came
14015 from an inter-CU reference and the type's CU got expanded before
14016 ours. */
14017 this_type = get_die_type (type_die, type_cu);
14018 if (this_type == NULL)
14019 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
14020 }
14021
14022 /* If we still don't have a type use an error marker. */
14023
14024 if (this_type == NULL)
c906108c 14025 {
b00fdb78
TT
14026 char *message, *saved;
14027
14028 /* read_type_die already issued a complaint. */
14029 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 14030 objfile->name,
b64f50a1
JK
14031 cu->header.offset.sect_off,
14032 die->offset.sect_off);
bb5ed363 14033 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
14034 message, strlen (message));
14035 xfree (message);
14036
bb5ed363 14037 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 14038 }
673bfd45 14039
f792889a 14040 return this_type;
c906108c
SS
14041}
14042
673bfd45
DE
14043/* Return the type in DIE, CU.
14044 Returns NULL for invalid types.
14045
14046 This first does a lookup in the appropriate type_hash table,
14047 and only reads the die in if necessary.
14048
14049 NOTE: This can be called when reading in partial or full symbols. */
14050
f792889a 14051static struct type *
e7c27a73 14052read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14053{
f792889a
DJ
14054 struct type *this_type;
14055
14056 this_type = get_die_type (die, cu);
14057 if (this_type)
14058 return this_type;
14059
673bfd45
DE
14060 return read_type_die_1 (die, cu);
14061}
14062
14063/* Read the type in DIE, CU.
14064 Returns NULL for invalid types. */
14065
14066static struct type *
14067read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
14068{
14069 struct type *this_type = NULL;
14070
c906108c
SS
14071 switch (die->tag)
14072 {
14073 case DW_TAG_class_type:
680b30c7 14074 case DW_TAG_interface_type:
c906108c
SS
14075 case DW_TAG_structure_type:
14076 case DW_TAG_union_type:
f792889a 14077 this_type = read_structure_type (die, cu);
c906108c
SS
14078 break;
14079 case DW_TAG_enumeration_type:
f792889a 14080 this_type = read_enumeration_type (die, cu);
c906108c
SS
14081 break;
14082 case DW_TAG_subprogram:
14083 case DW_TAG_subroutine_type:
edb3359d 14084 case DW_TAG_inlined_subroutine:
f792889a 14085 this_type = read_subroutine_type (die, cu);
c906108c
SS
14086 break;
14087 case DW_TAG_array_type:
f792889a 14088 this_type = read_array_type (die, cu);
c906108c 14089 break;
72019c9c 14090 case DW_TAG_set_type:
f792889a 14091 this_type = read_set_type (die, cu);
72019c9c 14092 break;
c906108c 14093 case DW_TAG_pointer_type:
f792889a 14094 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
14095 break;
14096 case DW_TAG_ptr_to_member_type:
f792889a 14097 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
14098 break;
14099 case DW_TAG_reference_type:
f792889a 14100 this_type = read_tag_reference_type (die, cu);
c906108c
SS
14101 break;
14102 case DW_TAG_const_type:
f792889a 14103 this_type = read_tag_const_type (die, cu);
c906108c
SS
14104 break;
14105 case DW_TAG_volatile_type:
f792889a 14106 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
14107 break;
14108 case DW_TAG_string_type:
f792889a 14109 this_type = read_tag_string_type (die, cu);
c906108c
SS
14110 break;
14111 case DW_TAG_typedef:
f792889a 14112 this_type = read_typedef (die, cu);
c906108c 14113 break;
a02abb62 14114 case DW_TAG_subrange_type:
f792889a 14115 this_type = read_subrange_type (die, cu);
a02abb62 14116 break;
c906108c 14117 case DW_TAG_base_type:
f792889a 14118 this_type = read_base_type (die, cu);
c906108c 14119 break;
81a17f79 14120 case DW_TAG_unspecified_type:
f792889a 14121 this_type = read_unspecified_type (die, cu);
81a17f79 14122 break;
0114d602
DJ
14123 case DW_TAG_namespace:
14124 this_type = read_namespace_type (die, cu);
14125 break;
f55ee35c
JK
14126 case DW_TAG_module:
14127 this_type = read_module_type (die, cu);
14128 break;
c906108c 14129 default:
3e43a32a
MS
14130 complaint (&symfile_complaints,
14131 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 14132 dwarf_tag_name (die->tag));
c906108c
SS
14133 break;
14134 }
63d06c5c 14135
f792889a 14136 return this_type;
63d06c5c
DC
14137}
14138
abc72ce4
DE
14139/* See if we can figure out if the class lives in a namespace. We do
14140 this by looking for a member function; its demangled name will
14141 contain namespace info, if there is any.
14142 Return the computed name or NULL.
14143 Space for the result is allocated on the objfile's obstack.
14144 This is the full-die version of guess_partial_die_structure_name.
14145 In this case we know DIE has no useful parent. */
14146
14147static char *
14148guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14149{
14150 struct die_info *spec_die;
14151 struct dwarf2_cu *spec_cu;
14152 struct die_info *child;
14153
14154 spec_cu = cu;
14155 spec_die = die_specification (die, &spec_cu);
14156 if (spec_die != NULL)
14157 {
14158 die = spec_die;
14159 cu = spec_cu;
14160 }
14161
14162 for (child = die->child;
14163 child != NULL;
14164 child = child->sibling)
14165 {
14166 if (child->tag == DW_TAG_subprogram)
14167 {
14168 struct attribute *attr;
14169
14170 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14171 if (attr == NULL)
14172 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14173 if (attr != NULL)
14174 {
14175 char *actual_name
14176 = language_class_name_from_physname (cu->language_defn,
14177 DW_STRING (attr));
14178 char *name = NULL;
14179
14180 if (actual_name != NULL)
14181 {
14182 char *die_name = dwarf2_name (die, cu);
14183
14184 if (die_name != NULL
14185 && strcmp (die_name, actual_name) != 0)
14186 {
14187 /* Strip off the class name from the full name.
14188 We want the prefix. */
14189 int die_name_len = strlen (die_name);
14190 int actual_name_len = strlen (actual_name);
14191
14192 /* Test for '::' as a sanity check. */
14193 if (actual_name_len > die_name_len + 2
3e43a32a
MS
14194 && actual_name[actual_name_len
14195 - die_name_len - 1] == ':')
abc72ce4
DE
14196 name =
14197 obsavestring (actual_name,
14198 actual_name_len - die_name_len - 2,
14199 &cu->objfile->objfile_obstack);
14200 }
14201 }
14202 xfree (actual_name);
14203 return name;
14204 }
14205 }
14206 }
14207
14208 return NULL;
14209}
14210
96408a79
SA
14211/* GCC might emit a nameless typedef that has a linkage name. Determine the
14212 prefix part in such case. See
14213 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14214
14215static char *
14216anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14217{
14218 struct attribute *attr;
14219 char *base;
14220
14221 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14222 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14223 return NULL;
14224
14225 attr = dwarf2_attr (die, DW_AT_name, cu);
14226 if (attr != NULL && DW_STRING (attr) != NULL)
14227 return NULL;
14228
14229 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14230 if (attr == NULL)
14231 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14232 if (attr == NULL || DW_STRING (attr) == NULL)
14233 return NULL;
14234
14235 /* dwarf2_name had to be already called. */
14236 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14237
14238 /* Strip the base name, keep any leading namespaces/classes. */
14239 base = strrchr (DW_STRING (attr), ':');
14240 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14241 return "";
14242
14243 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14244 &cu->objfile->objfile_obstack);
14245}
14246
fdde2d81 14247/* Return the name of the namespace/class that DIE is defined within,
0114d602 14248 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 14249
0114d602
DJ
14250 For example, if we're within the method foo() in the following
14251 code:
14252
14253 namespace N {
14254 class C {
14255 void foo () {
14256 }
14257 };
14258 }
14259
14260 then determine_prefix on foo's die will return "N::C". */
fdde2d81 14261
0d5cff50 14262static const char *
e142c38c 14263determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 14264{
0114d602
DJ
14265 struct die_info *parent, *spec_die;
14266 struct dwarf2_cu *spec_cu;
14267 struct type *parent_type;
96408a79 14268 char *retval;
63d06c5c 14269
f55ee35c
JK
14270 if (cu->language != language_cplus && cu->language != language_java
14271 && cu->language != language_fortran)
0114d602
DJ
14272 return "";
14273
96408a79
SA
14274 retval = anonymous_struct_prefix (die, cu);
14275 if (retval)
14276 return retval;
14277
0114d602
DJ
14278 /* We have to be careful in the presence of DW_AT_specification.
14279 For example, with GCC 3.4, given the code
14280
14281 namespace N {
14282 void foo() {
14283 // Definition of N::foo.
14284 }
14285 }
14286
14287 then we'll have a tree of DIEs like this:
14288
14289 1: DW_TAG_compile_unit
14290 2: DW_TAG_namespace // N
14291 3: DW_TAG_subprogram // declaration of N::foo
14292 4: DW_TAG_subprogram // definition of N::foo
14293 DW_AT_specification // refers to die #3
14294
14295 Thus, when processing die #4, we have to pretend that we're in
14296 the context of its DW_AT_specification, namely the contex of die
14297 #3. */
14298 spec_cu = cu;
14299 spec_die = die_specification (die, &spec_cu);
14300 if (spec_die == NULL)
14301 parent = die->parent;
14302 else
63d06c5c 14303 {
0114d602
DJ
14304 parent = spec_die->parent;
14305 cu = spec_cu;
63d06c5c 14306 }
0114d602
DJ
14307
14308 if (parent == NULL)
14309 return "";
98bfdba5
PA
14310 else if (parent->building_fullname)
14311 {
14312 const char *name;
14313 const char *parent_name;
14314
14315 /* It has been seen on RealView 2.2 built binaries,
14316 DW_TAG_template_type_param types actually _defined_ as
14317 children of the parent class:
14318
14319 enum E {};
14320 template class <class Enum> Class{};
14321 Class<enum E> class_e;
14322
14323 1: DW_TAG_class_type (Class)
14324 2: DW_TAG_enumeration_type (E)
14325 3: DW_TAG_enumerator (enum1:0)
14326 3: DW_TAG_enumerator (enum2:1)
14327 ...
14328 2: DW_TAG_template_type_param
14329 DW_AT_type DW_FORM_ref_udata (E)
14330
14331 Besides being broken debug info, it can put GDB into an
14332 infinite loop. Consider:
14333
14334 When we're building the full name for Class<E>, we'll start
14335 at Class, and go look over its template type parameters,
14336 finding E. We'll then try to build the full name of E, and
14337 reach here. We're now trying to build the full name of E,
14338 and look over the parent DIE for containing scope. In the
14339 broken case, if we followed the parent DIE of E, we'd again
14340 find Class, and once again go look at its template type
14341 arguments, etc., etc. Simply don't consider such parent die
14342 as source-level parent of this die (it can't be, the language
14343 doesn't allow it), and break the loop here. */
14344 name = dwarf2_name (die, cu);
14345 parent_name = dwarf2_name (parent, cu);
14346 complaint (&symfile_complaints,
14347 _("template param type '%s' defined within parent '%s'"),
14348 name ? name : "<unknown>",
14349 parent_name ? parent_name : "<unknown>");
14350 return "";
14351 }
63d06c5c 14352 else
0114d602
DJ
14353 switch (parent->tag)
14354 {
63d06c5c 14355 case DW_TAG_namespace:
0114d602 14356 parent_type = read_type_die (parent, cu);
acebe513
UW
14357 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14358 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14359 Work around this problem here. */
14360 if (cu->language == language_cplus
14361 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14362 return "";
0114d602
DJ
14363 /* We give a name to even anonymous namespaces. */
14364 return TYPE_TAG_NAME (parent_type);
63d06c5c 14365 case DW_TAG_class_type:
680b30c7 14366 case DW_TAG_interface_type:
63d06c5c 14367 case DW_TAG_structure_type:
0114d602 14368 case DW_TAG_union_type:
f55ee35c 14369 case DW_TAG_module:
0114d602
DJ
14370 parent_type = read_type_die (parent, cu);
14371 if (TYPE_TAG_NAME (parent_type) != NULL)
14372 return TYPE_TAG_NAME (parent_type);
14373 else
14374 /* An anonymous structure is only allowed non-static data
14375 members; no typedefs, no member functions, et cetera.
14376 So it does not need a prefix. */
14377 return "";
abc72ce4 14378 case DW_TAG_compile_unit:
95554aad 14379 case DW_TAG_partial_unit:
abc72ce4
DE
14380 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14381 if (cu->language == language_cplus
8b70b953 14382 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14383 && die->child != NULL
14384 && (die->tag == DW_TAG_class_type
14385 || die->tag == DW_TAG_structure_type
14386 || die->tag == DW_TAG_union_type))
14387 {
14388 char *name = guess_full_die_structure_name (die, cu);
14389 if (name != NULL)
14390 return name;
14391 }
14392 return "";
63d06c5c 14393 default:
8176b9b8 14394 return determine_prefix (parent, cu);
63d06c5c 14395 }
63d06c5c
DC
14396}
14397
3e43a32a
MS
14398/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14399 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14400 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14401 an obconcat, otherwise allocate storage for the result. The CU argument is
14402 used to determine the language and hence, the appropriate separator. */
987504bb 14403
f55ee35c 14404#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
14405
14406static char *
f55ee35c
JK
14407typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14408 int physname, struct dwarf2_cu *cu)
63d06c5c 14409{
f55ee35c 14410 const char *lead = "";
5c315b68 14411 const char *sep;
63d06c5c 14412
3e43a32a
MS
14413 if (suffix == NULL || suffix[0] == '\0'
14414 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
14415 sep = "";
14416 else if (cu->language == language_java)
14417 sep = ".";
f55ee35c
JK
14418 else if (cu->language == language_fortran && physname)
14419 {
14420 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14421 DW_AT_MIPS_linkage_name is preferred and used instead. */
14422
14423 lead = "__";
14424 sep = "_MOD_";
14425 }
987504bb
JJ
14426 else
14427 sep = "::";
63d06c5c 14428
6dd47d34
DE
14429 if (prefix == NULL)
14430 prefix = "";
14431 if (suffix == NULL)
14432 suffix = "";
14433
987504bb
JJ
14434 if (obs == NULL)
14435 {
3e43a32a
MS
14436 char *retval
14437 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 14438
f55ee35c
JK
14439 strcpy (retval, lead);
14440 strcat (retval, prefix);
6dd47d34
DE
14441 strcat (retval, sep);
14442 strcat (retval, suffix);
63d06c5c
DC
14443 return retval;
14444 }
987504bb
JJ
14445 else
14446 {
14447 /* We have an obstack. */
f55ee35c 14448 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 14449 }
63d06c5c
DC
14450}
14451
c906108c
SS
14452/* Return sibling of die, NULL if no sibling. */
14453
f9aca02d 14454static struct die_info *
fba45db2 14455sibling_die (struct die_info *die)
c906108c 14456{
639d11d3 14457 return die->sibling;
c906108c
SS
14458}
14459
71c25dea
TT
14460/* Get name of a die, return NULL if not found. */
14461
14462static char *
14463dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14464 struct obstack *obstack)
14465{
14466 if (name && cu->language == language_cplus)
14467 {
14468 char *canon_name = cp_canonicalize_string (name);
14469
14470 if (canon_name != NULL)
14471 {
14472 if (strcmp (canon_name, name) != 0)
14473 name = obsavestring (canon_name, strlen (canon_name),
14474 obstack);
14475 xfree (canon_name);
14476 }
14477 }
14478
14479 return name;
c906108c
SS
14480}
14481
9219021c
DC
14482/* Get name of a die, return NULL if not found. */
14483
14484static char *
e142c38c 14485dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
14486{
14487 struct attribute *attr;
14488
e142c38c 14489 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
14490 if ((!attr || !DW_STRING (attr))
14491 && die->tag != DW_TAG_class_type
14492 && die->tag != DW_TAG_interface_type
14493 && die->tag != DW_TAG_structure_type
14494 && die->tag != DW_TAG_union_type)
71c25dea
TT
14495 return NULL;
14496
14497 switch (die->tag)
14498 {
14499 case DW_TAG_compile_unit:
95554aad 14500 case DW_TAG_partial_unit:
71c25dea
TT
14501 /* Compilation units have a DW_AT_name that is a filename, not
14502 a source language identifier. */
14503 case DW_TAG_enumeration_type:
14504 case DW_TAG_enumerator:
14505 /* These tags always have simple identifiers already; no need
14506 to canonicalize them. */
14507 return DW_STRING (attr);
907af001 14508
418835cc
KS
14509 case DW_TAG_subprogram:
14510 /* Java constructors will all be named "<init>", so return
14511 the class name when we see this special case. */
14512 if (cu->language == language_java
14513 && DW_STRING (attr) != NULL
14514 && strcmp (DW_STRING (attr), "<init>") == 0)
14515 {
14516 struct dwarf2_cu *spec_cu = cu;
14517 struct die_info *spec_die;
14518
14519 /* GCJ will output '<init>' for Java constructor names.
14520 For this special case, return the name of the parent class. */
14521
14522 /* GCJ may output suprogram DIEs with AT_specification set.
14523 If so, use the name of the specified DIE. */
14524 spec_die = die_specification (die, &spec_cu);
14525 if (spec_die != NULL)
14526 return dwarf2_name (spec_die, spec_cu);
14527
14528 do
14529 {
14530 die = die->parent;
14531 if (die->tag == DW_TAG_class_type)
14532 return dwarf2_name (die, cu);
14533 }
95554aad
TT
14534 while (die->tag != DW_TAG_compile_unit
14535 && die->tag != DW_TAG_partial_unit);
418835cc 14536 }
907af001
UW
14537 break;
14538
14539 case DW_TAG_class_type:
14540 case DW_TAG_interface_type:
14541 case DW_TAG_structure_type:
14542 case DW_TAG_union_type:
14543 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14544 structures or unions. These were of the form "._%d" in GCC 4.1,
14545 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14546 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
14547 if (attr && DW_STRING (attr)
14548 && (strncmp (DW_STRING (attr), "._", 2) == 0
14549 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 14550 return NULL;
53832f31
TT
14551
14552 /* GCC might emit a nameless typedef that has a linkage name. See
14553 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14554 if (!attr || DW_STRING (attr) == NULL)
14555 {
df5c6c50 14556 char *demangled = NULL;
53832f31
TT
14557
14558 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14559 if (attr == NULL)
14560 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14561
14562 if (attr == NULL || DW_STRING (attr) == NULL)
14563 return NULL;
14564
df5c6c50
JK
14565 /* Avoid demangling DW_STRING (attr) the second time on a second
14566 call for the same DIE. */
14567 if (!DW_STRING_IS_CANONICAL (attr))
14568 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
14569
14570 if (demangled)
14571 {
96408a79
SA
14572 char *base;
14573
53832f31 14574 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
14575 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14576 &cu->objfile->objfile_obstack);
53832f31
TT
14577 DW_STRING_IS_CANONICAL (attr) = 1;
14578 xfree (demangled);
96408a79
SA
14579
14580 /* Strip any leading namespaces/classes, keep only the base name.
14581 DW_AT_name for named DIEs does not contain the prefixes. */
14582 base = strrchr (DW_STRING (attr), ':');
14583 if (base && base > DW_STRING (attr) && base[-1] == ':')
14584 return &base[1];
14585 else
14586 return DW_STRING (attr);
53832f31
TT
14587 }
14588 }
907af001
UW
14589 break;
14590
71c25dea 14591 default:
907af001
UW
14592 break;
14593 }
14594
14595 if (!DW_STRING_IS_CANONICAL (attr))
14596 {
14597 DW_STRING (attr)
14598 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14599 &cu->objfile->objfile_obstack);
14600 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 14601 }
907af001 14602 return DW_STRING (attr);
9219021c
DC
14603}
14604
14605/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
14606 is none. *EXT_CU is the CU containing DIE on input, and the CU
14607 containing the return value on output. */
9219021c
DC
14608
14609static struct die_info *
f2f0e013 14610dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
14611{
14612 struct attribute *attr;
9219021c 14613
f2f0e013 14614 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
14615 if (attr == NULL)
14616 return NULL;
14617
f2f0e013 14618 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
14619}
14620
c906108c
SS
14621/* Convert a DIE tag into its string name. */
14622
f39c6ffd 14623static const char *
aa1ee363 14624dwarf_tag_name (unsigned tag)
c906108c 14625{
f39c6ffd
TT
14626 const char *name = get_DW_TAG_name (tag);
14627
14628 if (name == NULL)
14629 return "DW_TAG_<unknown>";
14630
14631 return name;
c906108c
SS
14632}
14633
14634/* Convert a DWARF attribute code into its string name. */
14635
f39c6ffd 14636static const char *
aa1ee363 14637dwarf_attr_name (unsigned attr)
c906108c 14638{
f39c6ffd
TT
14639 const char *name;
14640
c764a876 14641#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
14642 if (attr == DW_AT_MIPS_fde)
14643 return "DW_AT_MIPS_fde";
14644#else
14645 if (attr == DW_AT_HP_block_index)
14646 return "DW_AT_HP_block_index";
c764a876 14647#endif
f39c6ffd
TT
14648
14649 name = get_DW_AT_name (attr);
14650
14651 if (name == NULL)
14652 return "DW_AT_<unknown>";
14653
14654 return name;
c906108c
SS
14655}
14656
14657/* Convert a DWARF value form code into its string name. */
14658
f39c6ffd 14659static const char *
aa1ee363 14660dwarf_form_name (unsigned form)
c906108c 14661{
f39c6ffd
TT
14662 const char *name = get_DW_FORM_name (form);
14663
14664 if (name == NULL)
14665 return "DW_FORM_<unknown>";
14666
14667 return name;
c906108c
SS
14668}
14669
14670static char *
fba45db2 14671dwarf_bool_name (unsigned mybool)
c906108c
SS
14672{
14673 if (mybool)
14674 return "TRUE";
14675 else
14676 return "FALSE";
14677}
14678
14679/* Convert a DWARF type code into its string name. */
14680
f39c6ffd 14681static const char *
aa1ee363 14682dwarf_type_encoding_name (unsigned enc)
c906108c 14683{
f39c6ffd 14684 const char *name = get_DW_ATE_name (enc);
c906108c 14685
f39c6ffd
TT
14686 if (name == NULL)
14687 return "DW_ATE_<unknown>";
c906108c 14688
f39c6ffd 14689 return name;
c906108c 14690}
c906108c 14691
f9aca02d 14692static void
d97bc12b 14693dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
14694{
14695 unsigned int i;
14696
d97bc12b
DE
14697 print_spaces (indent, f);
14698 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 14699 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
14700
14701 if (die->parent != NULL)
14702 {
14703 print_spaces (indent, f);
14704 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 14705 die->parent->offset.sect_off);
d97bc12b
DE
14706 }
14707
14708 print_spaces (indent, f);
14709 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 14710 dwarf_bool_name (die->child != NULL));
c906108c 14711
d97bc12b
DE
14712 print_spaces (indent, f);
14713 fprintf_unfiltered (f, " attributes:\n");
14714
c906108c
SS
14715 for (i = 0; i < die->num_attrs; ++i)
14716 {
d97bc12b
DE
14717 print_spaces (indent, f);
14718 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
14719 dwarf_attr_name (die->attrs[i].name),
14720 dwarf_form_name (die->attrs[i].form));
d97bc12b 14721
c906108c
SS
14722 switch (die->attrs[i].form)
14723 {
c906108c 14724 case DW_FORM_addr:
3019eac3 14725 case DW_FORM_GNU_addr_index:
d97bc12b 14726 fprintf_unfiltered (f, "address: ");
5af949e3 14727 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
14728 break;
14729 case DW_FORM_block2:
14730 case DW_FORM_block4:
14731 case DW_FORM_block:
14732 case DW_FORM_block1:
3e43a32a
MS
14733 fprintf_unfiltered (f, "block: size %d",
14734 DW_BLOCK (&die->attrs[i])->size);
c906108c 14735 break;
2dc7f7b3
TT
14736 case DW_FORM_exprloc:
14737 fprintf_unfiltered (f, "expression: size %u",
14738 DW_BLOCK (&die->attrs[i])->size);
14739 break;
4568ecf9
DE
14740 case DW_FORM_ref_addr:
14741 fprintf_unfiltered (f, "ref address: ");
14742 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14743 break;
10b3939b
DJ
14744 case DW_FORM_ref1:
14745 case DW_FORM_ref2:
14746 case DW_FORM_ref4:
4568ecf9
DE
14747 case DW_FORM_ref8:
14748 case DW_FORM_ref_udata:
d97bc12b 14749 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 14750 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 14751 break;
c906108c
SS
14752 case DW_FORM_data1:
14753 case DW_FORM_data2:
14754 case DW_FORM_data4:
ce5d95e1 14755 case DW_FORM_data8:
c906108c
SS
14756 case DW_FORM_udata:
14757 case DW_FORM_sdata:
43bbcdc2
PH
14758 fprintf_unfiltered (f, "constant: %s",
14759 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 14760 break;
2dc7f7b3
TT
14761 case DW_FORM_sec_offset:
14762 fprintf_unfiltered (f, "section offset: %s",
14763 pulongest (DW_UNSND (&die->attrs[i])));
14764 break;
55f1336d 14765 case DW_FORM_ref_sig8:
348e048f
DE
14766 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14767 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 14768 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
14769 else
14770 fprintf_unfiltered (f, "signatured type, offset: unknown");
14771 break;
c906108c 14772 case DW_FORM_string:
4bdf3d34 14773 case DW_FORM_strp:
3019eac3 14774 case DW_FORM_GNU_str_index:
8285870a 14775 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 14776 DW_STRING (&die->attrs[i])
8285870a
JK
14777 ? DW_STRING (&die->attrs[i]) : "",
14778 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
14779 break;
14780 case DW_FORM_flag:
14781 if (DW_UNSND (&die->attrs[i]))
d97bc12b 14782 fprintf_unfiltered (f, "flag: TRUE");
c906108c 14783 else
d97bc12b 14784 fprintf_unfiltered (f, "flag: FALSE");
c906108c 14785 break;
2dc7f7b3
TT
14786 case DW_FORM_flag_present:
14787 fprintf_unfiltered (f, "flag: TRUE");
14788 break;
a8329558 14789 case DW_FORM_indirect:
0963b4bd
MS
14790 /* The reader will have reduced the indirect form to
14791 the "base form" so this form should not occur. */
3e43a32a
MS
14792 fprintf_unfiltered (f,
14793 "unexpected attribute form: DW_FORM_indirect");
a8329558 14794 break;
c906108c 14795 default:
d97bc12b 14796 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 14797 die->attrs[i].form);
d97bc12b 14798 break;
c906108c 14799 }
d97bc12b 14800 fprintf_unfiltered (f, "\n");
c906108c
SS
14801 }
14802}
14803
f9aca02d 14804static void
d97bc12b 14805dump_die_for_error (struct die_info *die)
c906108c 14806{
d97bc12b
DE
14807 dump_die_shallow (gdb_stderr, 0, die);
14808}
14809
14810static void
14811dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14812{
14813 int indent = level * 4;
14814
14815 gdb_assert (die != NULL);
14816
14817 if (level >= max_level)
14818 return;
14819
14820 dump_die_shallow (f, indent, die);
14821
14822 if (die->child != NULL)
c906108c 14823 {
d97bc12b
DE
14824 print_spaces (indent, f);
14825 fprintf_unfiltered (f, " Children:");
14826 if (level + 1 < max_level)
14827 {
14828 fprintf_unfiltered (f, "\n");
14829 dump_die_1 (f, level + 1, max_level, die->child);
14830 }
14831 else
14832 {
3e43a32a
MS
14833 fprintf_unfiltered (f,
14834 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14835 }
14836 }
14837
14838 if (die->sibling != NULL && level > 0)
14839 {
14840 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14841 }
14842}
14843
d97bc12b
DE
14844/* This is called from the pdie macro in gdbinit.in.
14845 It's not static so gcc will keep a copy callable from gdb. */
14846
14847void
14848dump_die (struct die_info *die, int max_level)
14849{
14850 dump_die_1 (gdb_stdlog, 0, max_level, die);
14851}
14852
f9aca02d 14853static void
51545339 14854store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14855{
51545339 14856 void **slot;
c906108c 14857
b64f50a1
JK
14858 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14859 INSERT);
51545339
DJ
14860
14861 *slot = die;
c906108c
SS
14862}
14863
b64f50a1
JK
14864/* DW_ADDR is always stored already as sect_offset; despite for the forms
14865 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14866
93311388
DE
14867static int
14868is_ref_attr (struct attribute *attr)
c906108c 14869{
c906108c
SS
14870 switch (attr->form)
14871 {
14872 case DW_FORM_ref_addr:
c906108c
SS
14873 case DW_FORM_ref1:
14874 case DW_FORM_ref2:
14875 case DW_FORM_ref4:
613e1657 14876 case DW_FORM_ref8:
c906108c 14877 case DW_FORM_ref_udata:
93311388 14878 return 1;
c906108c 14879 default:
93311388 14880 return 0;
c906108c 14881 }
93311388
DE
14882}
14883
b64f50a1
JK
14884/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14885 required kind. */
14886
14887static sect_offset
93311388
DE
14888dwarf2_get_ref_die_offset (struct attribute *attr)
14889{
4568ecf9 14890 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 14891
93311388 14892 if (is_ref_attr (attr))
b64f50a1 14893 return retval;
93311388 14894
b64f50a1 14895 retval.sect_off = 0;
93311388
DE
14896 complaint (&symfile_complaints,
14897 _("unsupported die ref attribute form: '%s'"),
14898 dwarf_form_name (attr->form));
b64f50a1 14899 return retval;
c906108c
SS
14900}
14901
43bbcdc2
PH
14902/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14903 * the value held by the attribute is not constant. */
a02abb62 14904
43bbcdc2 14905static LONGEST
a02abb62
JB
14906dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14907{
14908 if (attr->form == DW_FORM_sdata)
14909 return DW_SND (attr);
14910 else if (attr->form == DW_FORM_udata
14911 || attr->form == DW_FORM_data1
14912 || attr->form == DW_FORM_data2
14913 || attr->form == DW_FORM_data4
14914 || attr->form == DW_FORM_data8)
14915 return DW_UNSND (attr);
14916 else
14917 {
3e43a32a
MS
14918 complaint (&symfile_complaints,
14919 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14920 dwarf_form_name (attr->form));
14921 return default_value;
14922 }
14923}
14924
03dd20cc 14925/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14926 unit and add it to our queue.
14927 The result is non-zero if PER_CU was queued, otherwise the result is zero
14928 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14929
348e048f 14930static int
03dd20cc 14931maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
95554aad
TT
14932 struct dwarf2_per_cu_data *per_cu,
14933 enum language pretend_language)
03dd20cc 14934{
98bfdba5
PA
14935 /* We may arrive here during partial symbol reading, if we need full
14936 DIEs to process an unusual case (e.g. template arguments). Do
14937 not queue PER_CU, just tell our caller to load its DIEs. */
14938 if (dwarf2_per_objfile->reading_partial_symbols)
14939 {
14940 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14941 return 1;
14942 return 0;
14943 }
14944
03dd20cc
DJ
14945 /* Mark the dependence relation so that we don't flush PER_CU
14946 too early. */
14947 dwarf2_add_dependence (this_cu, per_cu);
14948
14949 /* If it's already on the queue, we have nothing to do. */
14950 if (per_cu->queued)
348e048f 14951 return 0;
03dd20cc
DJ
14952
14953 /* If the compilation unit is already loaded, just mark it as
14954 used. */
14955 if (per_cu->cu != NULL)
14956 {
14957 per_cu->cu->last_used = 0;
348e048f 14958 return 0;
03dd20cc
DJ
14959 }
14960
14961 /* Add it to the queue. */
95554aad 14962 queue_comp_unit (per_cu, pretend_language);
348e048f
DE
14963
14964 return 1;
14965}
14966
14967/* Follow reference or signature attribute ATTR of SRC_DIE.
14968 On entry *REF_CU is the CU of SRC_DIE.
14969 On exit *REF_CU is the CU of the result. */
14970
14971static struct die_info *
14972follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14973 struct dwarf2_cu **ref_cu)
14974{
14975 struct die_info *die;
14976
14977 if (is_ref_attr (attr))
14978 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14979 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14980 die = follow_die_sig (src_die, attr, ref_cu);
14981 else
14982 {
14983 dump_die_for_error (src_die);
14984 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14985 (*ref_cu)->objfile->name);
14986 }
14987
14988 return die;
03dd20cc
DJ
14989}
14990
5c631832 14991/* Follow reference OFFSET.
673bfd45
DE
14992 On entry *REF_CU is the CU of the source die referencing OFFSET.
14993 On exit *REF_CU is the CU of the result.
14994 Returns NULL if OFFSET is invalid. */
f504f079 14995
f9aca02d 14996static struct die_info *
b64f50a1 14997follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
c906108c 14998{
10b3939b 14999 struct die_info temp_die;
f2f0e013 15000 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 15001
348e048f
DE
15002 gdb_assert (cu->per_cu != NULL);
15003
98bfdba5
PA
15004 target_cu = cu;
15005
3019eac3 15006 if (cu->per_cu->is_debug_types)
348e048f
DE
15007 {
15008 /* .debug_types CUs cannot reference anything outside their CU.
15009 If they need to, they have to reference a signatured type via
55f1336d 15010 DW_FORM_ref_sig8. */
348e048f 15011 if (! offset_in_cu_p (&cu->header, offset))
5c631832 15012 return NULL;
348e048f
DE
15013 }
15014 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
15015 {
15016 struct dwarf2_per_cu_data *per_cu;
9a619af0 15017
45452591 15018 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
15019
15020 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
15021 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
15022 load_full_comp_unit (per_cu, cu->language);
03dd20cc 15023
10b3939b
DJ
15024 target_cu = per_cu->cu;
15025 }
98bfdba5
PA
15026 else if (cu->dies == NULL)
15027 {
15028 /* We're loading full DIEs during partial symbol reading. */
15029 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 15030 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 15031 }
c906108c 15032
f2f0e013 15033 *ref_cu = target_cu;
51545339 15034 temp_die.offset = offset;
b64f50a1 15035 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 15036}
10b3939b 15037
5c631832
JK
15038/* Follow reference attribute ATTR of SRC_DIE.
15039 On entry *REF_CU is the CU of SRC_DIE.
15040 On exit *REF_CU is the CU of the result. */
15041
15042static struct die_info *
15043follow_die_ref (struct die_info *src_die, struct attribute *attr,
15044 struct dwarf2_cu **ref_cu)
15045{
b64f50a1 15046 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
15047 struct dwarf2_cu *cu = *ref_cu;
15048 struct die_info *die;
15049
15050 die = follow_die_offset (offset, ref_cu);
15051 if (!die)
15052 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
15053 "at 0x%x [in module %s]"),
b64f50a1 15054 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 15055
5c631832
JK
15056 return die;
15057}
15058
d83e736b
JK
15059/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
15060 Returned value is intended for DW_OP_call*. Returned
15061 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
15062
15063struct dwarf2_locexpr_baton
b64f50a1 15064dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
15065 struct dwarf2_per_cu_data *per_cu,
15066 CORE_ADDR (*get_frame_pc) (void *baton),
15067 void *baton)
5c631832 15068{
b64f50a1 15069 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 15070 struct dwarf2_cu *cu;
5c631832
JK
15071 struct die_info *die;
15072 struct attribute *attr;
15073 struct dwarf2_locexpr_baton retval;
15074
8cf6f0b1
TT
15075 dw2_setup (per_cu->objfile);
15076
918dd910
JK
15077 if (per_cu->cu == NULL)
15078 load_cu (per_cu);
15079 cu = per_cu->cu;
15080
5c631832
JK
15081 die = follow_die_offset (offset, &cu);
15082 if (!die)
15083 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 15084 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15085
15086 attr = dwarf2_attr (die, DW_AT_location, cu);
15087 if (!attr)
15088 {
e103e986
JK
15089 /* DWARF: "If there is no such attribute, then there is no effect.".
15090 DATA is ignored if SIZE is 0. */
5c631832 15091
e103e986 15092 retval.data = NULL;
5c631832
JK
15093 retval.size = 0;
15094 }
8cf6f0b1
TT
15095 else if (attr_form_is_section_offset (attr))
15096 {
15097 struct dwarf2_loclist_baton loclist_baton;
15098 CORE_ADDR pc = (*get_frame_pc) (baton);
15099 size_t size;
15100
15101 fill_in_loclist_baton (cu, &loclist_baton, attr);
15102
15103 retval.data = dwarf2_find_location_expression (&loclist_baton,
15104 &size, pc);
15105 retval.size = size;
15106 }
5c631832
JK
15107 else
15108 {
15109 if (!attr_form_is_block (attr))
15110 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15111 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 15112 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15113
15114 retval.data = DW_BLOCK (attr)->data;
15115 retval.size = DW_BLOCK (attr)->size;
15116 }
15117 retval.per_cu = cu->per_cu;
918dd910 15118
918dd910
JK
15119 age_cached_comp_units ();
15120
5c631832 15121 return retval;
348e048f
DE
15122}
15123
8a9b8146
TT
15124/* Return the type of the DIE at DIE_OFFSET in the CU named by
15125 PER_CU. */
15126
15127struct type *
b64f50a1 15128dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
15129 struct dwarf2_per_cu_data *per_cu)
15130{
b64f50a1
JK
15131 sect_offset die_offset_sect;
15132
8a9b8146 15133 dw2_setup (per_cu->objfile);
b64f50a1
JK
15134
15135 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15136 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
15137}
15138
348e048f
DE
15139/* Follow the signature attribute ATTR in SRC_DIE.
15140 On entry *REF_CU is the CU of SRC_DIE.
15141 On exit *REF_CU is the CU of the result. */
15142
15143static struct die_info *
15144follow_die_sig (struct die_info *src_die, struct attribute *attr,
15145 struct dwarf2_cu **ref_cu)
15146{
15147 struct objfile *objfile = (*ref_cu)->objfile;
15148 struct die_info temp_die;
15149 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15150 struct dwarf2_cu *sig_cu;
15151 struct die_info *die;
15152
15153 /* sig_type will be NULL if the signatured type is missing from
15154 the debug info. */
15155 if (sig_type == NULL)
15156 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15157 "at 0x%x [in module %s]"),
b64f50a1 15158 src_die->offset.sect_off, objfile->name);
348e048f
DE
15159
15160 /* If necessary, add it to the queue and load its DIEs. */
15161
95554aad 15162 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 15163 read_signatured_type (sig_type);
348e048f
DE
15164
15165 gdb_assert (sig_type->per_cu.cu != NULL);
15166
15167 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
15168 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15169 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
15170 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15171 temp_die.offset.sect_off);
348e048f
DE
15172 if (die)
15173 {
15174 *ref_cu = sig_cu;
15175 return die;
15176 }
15177
3e43a32a
MS
15178 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15179 "from DIE at 0x%x [in module %s]"),
b64f50a1 15180 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
15181}
15182
15183/* Given an offset of a signatured type, return its signatured_type. */
15184
15185static struct signatured_type *
8b70b953
TT
15186lookup_signatured_type_at_offset (struct objfile *objfile,
15187 struct dwarf2_section_info *section,
b64f50a1 15188 sect_offset offset)
348e048f 15189{
b64f50a1 15190 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
15191 unsigned int length, initial_length_size;
15192 unsigned int sig_offset;
52dc124a 15193 struct signatured_type find_entry, *sig_type;
348e048f
DE
15194
15195 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15196 sig_offset = (initial_length_size
15197 + 2 /*version*/
15198 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15199 + 1 /*address_size*/);
15200 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 15201 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
15202
15203 /* This is only used to lookup previously recorded types.
15204 If we didn't find it, it's our bug. */
52dc124a
DE
15205 gdb_assert (sig_type != NULL);
15206 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 15207
52dc124a 15208 return sig_type;
348e048f
DE
15209}
15210
e5fe5e75 15211/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
15212
15213static void
e5fe5e75 15214load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 15215{
e5fe5e75 15216 struct objfile *objfile = per_cu->objfile;
3019eac3 15217 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
b64f50a1 15218 sect_offset offset = per_cu->offset;
52dc124a 15219 struct signatured_type *sig_type;
348e048f 15220
8b70b953 15221 dwarf2_read_section (objfile, sect);
be391dca 15222
348e048f 15223 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
15224 hash table lookup. */
15225 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15226 the signature to assert we found the right one.
15227 Ok, but it's a lot of work. We should simplify things so any needed
15228 assert doesn't require all this clumsiness. */
52dc124a 15229 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f 15230
dee91e82 15231 gdb_assert (&sig_type->per_cu == per_cu);
52dc124a 15232 gdb_assert (sig_type->per_cu.cu == NULL);
348e048f 15233
52dc124a 15234 read_signatured_type (sig_type);
348e048f 15235
52dc124a 15236 gdb_assert (sig_type->per_cu.cu != NULL);
348e048f
DE
15237}
15238
dee91e82
DE
15239/* die_reader_func for read_signatured_type.
15240 This is identical to load_full_comp_unit_reader,
15241 but is kept separate for now. */
348e048f
DE
15242
15243static void
dee91e82
DE
15244read_signatured_type_reader (const struct die_reader_specs *reader,
15245 gdb_byte *info_ptr,
15246 struct die_info *comp_unit_die,
15247 int has_children,
15248 void *data)
348e048f 15249{
dee91e82 15250 struct dwarf2_cu *cu = reader->cu;
348e048f 15251
dee91e82
DE
15252 gdb_assert (cu->die_hash == NULL);
15253 cu->die_hash =
15254 htab_create_alloc_ex (cu->header.length / 12,
15255 die_hash,
15256 die_eq,
15257 NULL,
15258 &cu->comp_unit_obstack,
15259 hashtab_obstack_allocate,
15260 dummy_obstack_deallocate);
348e048f 15261
dee91e82
DE
15262 if (has_children)
15263 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15264 &info_ptr, comp_unit_die);
15265 cu->dies = comp_unit_die;
15266 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
15267
15268 /* We try not to read any attributes in this function, because not
9cdd5dbd 15269 all CUs needed for references have been loaded yet, and symbol
348e048f 15270 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
15271 or we won't be able to build types correctly.
15272 Similarly, if we do not read the producer, we can not apply
15273 producer-specific interpretation. */
95554aad 15274 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 15275}
348e048f 15276
3019eac3
DE
15277/* Read in a signatured type and build its CU and DIEs.
15278 If the type is a stub for the real type in a DWO file,
15279 read in the real type from the DWO file as well. */
dee91e82
DE
15280
15281static void
15282read_signatured_type (struct signatured_type *sig_type)
15283{
15284 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 15285
3019eac3 15286 gdb_assert (per_cu->is_debug_types);
dee91e82 15287 gdb_assert (per_cu->cu == NULL);
348e048f 15288
fd820528 15289 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
c906108c
SS
15290}
15291
c906108c
SS
15292/* Decode simple location descriptions.
15293 Given a pointer to a dwarf block that defines a location, compute
15294 the location and return the value.
15295
4cecd739
DJ
15296 NOTE drow/2003-11-18: This function is called in two situations
15297 now: for the address of static or global variables (partial symbols
15298 only) and for offsets into structures which are expected to be
15299 (more or less) constant. The partial symbol case should go away,
15300 and only the constant case should remain. That will let this
15301 function complain more accurately. A few special modes are allowed
15302 without complaint for global variables (for instance, global
15303 register values and thread-local values).
c906108c
SS
15304
15305 A location description containing no operations indicates that the
4cecd739 15306 object is optimized out. The return value is 0 for that case.
6b992462
DJ
15307 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15308 callers will only want a very basic result and this can become a
21ae7a4d
JK
15309 complaint.
15310
15311 Note that stack[0] is unused except as a default error return. */
c906108c
SS
15312
15313static CORE_ADDR
e7c27a73 15314decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 15315{
e7c27a73 15316 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
15317 int i;
15318 int size = blk->size;
15319 gdb_byte *data = blk->data;
15320 CORE_ADDR stack[64];
15321 int stacki;
15322 unsigned int bytes_read, unsnd;
15323 gdb_byte op;
c906108c 15324
21ae7a4d
JK
15325 i = 0;
15326 stacki = 0;
15327 stack[stacki] = 0;
15328 stack[++stacki] = 0;
15329
15330 while (i < size)
15331 {
15332 op = data[i++];
15333 switch (op)
15334 {
15335 case DW_OP_lit0:
15336 case DW_OP_lit1:
15337 case DW_OP_lit2:
15338 case DW_OP_lit3:
15339 case DW_OP_lit4:
15340 case DW_OP_lit5:
15341 case DW_OP_lit6:
15342 case DW_OP_lit7:
15343 case DW_OP_lit8:
15344 case DW_OP_lit9:
15345 case DW_OP_lit10:
15346 case DW_OP_lit11:
15347 case DW_OP_lit12:
15348 case DW_OP_lit13:
15349 case DW_OP_lit14:
15350 case DW_OP_lit15:
15351 case DW_OP_lit16:
15352 case DW_OP_lit17:
15353 case DW_OP_lit18:
15354 case DW_OP_lit19:
15355 case DW_OP_lit20:
15356 case DW_OP_lit21:
15357 case DW_OP_lit22:
15358 case DW_OP_lit23:
15359 case DW_OP_lit24:
15360 case DW_OP_lit25:
15361 case DW_OP_lit26:
15362 case DW_OP_lit27:
15363 case DW_OP_lit28:
15364 case DW_OP_lit29:
15365 case DW_OP_lit30:
15366 case DW_OP_lit31:
15367 stack[++stacki] = op - DW_OP_lit0;
15368 break;
f1bea926 15369
21ae7a4d
JK
15370 case DW_OP_reg0:
15371 case DW_OP_reg1:
15372 case DW_OP_reg2:
15373 case DW_OP_reg3:
15374 case DW_OP_reg4:
15375 case DW_OP_reg5:
15376 case DW_OP_reg6:
15377 case DW_OP_reg7:
15378 case DW_OP_reg8:
15379 case DW_OP_reg9:
15380 case DW_OP_reg10:
15381 case DW_OP_reg11:
15382 case DW_OP_reg12:
15383 case DW_OP_reg13:
15384 case DW_OP_reg14:
15385 case DW_OP_reg15:
15386 case DW_OP_reg16:
15387 case DW_OP_reg17:
15388 case DW_OP_reg18:
15389 case DW_OP_reg19:
15390 case DW_OP_reg20:
15391 case DW_OP_reg21:
15392 case DW_OP_reg22:
15393 case DW_OP_reg23:
15394 case DW_OP_reg24:
15395 case DW_OP_reg25:
15396 case DW_OP_reg26:
15397 case DW_OP_reg27:
15398 case DW_OP_reg28:
15399 case DW_OP_reg29:
15400 case DW_OP_reg30:
15401 case DW_OP_reg31:
15402 stack[++stacki] = op - DW_OP_reg0;
15403 if (i < size)
15404 dwarf2_complex_location_expr_complaint ();
15405 break;
c906108c 15406
21ae7a4d
JK
15407 case DW_OP_regx:
15408 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15409 i += bytes_read;
15410 stack[++stacki] = unsnd;
15411 if (i < size)
15412 dwarf2_complex_location_expr_complaint ();
15413 break;
c906108c 15414
21ae7a4d
JK
15415 case DW_OP_addr:
15416 stack[++stacki] = read_address (objfile->obfd, &data[i],
15417 cu, &bytes_read);
15418 i += bytes_read;
15419 break;
d53d4ac5 15420
21ae7a4d
JK
15421 case DW_OP_const1u:
15422 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15423 i += 1;
15424 break;
15425
15426 case DW_OP_const1s:
15427 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15428 i += 1;
15429 break;
15430
15431 case DW_OP_const2u:
15432 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15433 i += 2;
15434 break;
15435
15436 case DW_OP_const2s:
15437 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15438 i += 2;
15439 break;
d53d4ac5 15440
21ae7a4d
JK
15441 case DW_OP_const4u:
15442 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15443 i += 4;
15444 break;
15445
15446 case DW_OP_const4s:
15447 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15448 i += 4;
15449 break;
15450
585861ea
JK
15451 case DW_OP_const8u:
15452 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15453 i += 8;
15454 break;
15455
21ae7a4d
JK
15456 case DW_OP_constu:
15457 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15458 &bytes_read);
15459 i += bytes_read;
15460 break;
15461
15462 case DW_OP_consts:
15463 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15464 i += bytes_read;
15465 break;
15466
15467 case DW_OP_dup:
15468 stack[stacki + 1] = stack[stacki];
15469 stacki++;
15470 break;
15471
15472 case DW_OP_plus:
15473 stack[stacki - 1] += stack[stacki];
15474 stacki--;
15475 break;
15476
15477 case DW_OP_plus_uconst:
15478 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15479 &bytes_read);
15480 i += bytes_read;
15481 break;
15482
15483 case DW_OP_minus:
15484 stack[stacki - 1] -= stack[stacki];
15485 stacki--;
15486 break;
15487
15488 case DW_OP_deref:
15489 /* If we're not the last op, then we definitely can't encode
15490 this using GDB's address_class enum. This is valid for partial
15491 global symbols, although the variable's address will be bogus
15492 in the psymtab. */
15493 if (i < size)
15494 dwarf2_complex_location_expr_complaint ();
15495 break;
15496
15497 case DW_OP_GNU_push_tls_address:
15498 /* The top of the stack has the offset from the beginning
15499 of the thread control block at which the variable is located. */
15500 /* Nothing should follow this operator, so the top of stack would
15501 be returned. */
15502 /* This is valid for partial global symbols, but the variable's
585861ea
JK
15503 address will be bogus in the psymtab. Make it always at least
15504 non-zero to not look as a variable garbage collected by linker
15505 which have DW_OP_addr 0. */
21ae7a4d
JK
15506 if (i < size)
15507 dwarf2_complex_location_expr_complaint ();
585861ea 15508 stack[stacki]++;
21ae7a4d
JK
15509 break;
15510
15511 case DW_OP_GNU_uninit:
15512 break;
15513
3019eac3 15514 case DW_OP_GNU_addr_index:
49f6c839 15515 case DW_OP_GNU_const_index:
3019eac3
DE
15516 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15517 &bytes_read);
15518 i += bytes_read;
15519 break;
15520
21ae7a4d
JK
15521 default:
15522 {
f39c6ffd 15523 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
15524
15525 if (name)
15526 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15527 name);
15528 else
15529 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15530 op);
15531 }
15532
15533 return (stack[stacki]);
d53d4ac5 15534 }
3c6e0cb3 15535
21ae7a4d
JK
15536 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15537 outside of the allocated space. Also enforce minimum>0. */
15538 if (stacki >= ARRAY_SIZE (stack) - 1)
15539 {
15540 complaint (&symfile_complaints,
15541 _("location description stack overflow"));
15542 return 0;
15543 }
15544
15545 if (stacki <= 0)
15546 {
15547 complaint (&symfile_complaints,
15548 _("location description stack underflow"));
15549 return 0;
15550 }
15551 }
15552 return (stack[stacki]);
c906108c
SS
15553}
15554
15555/* memory allocation interface */
15556
c906108c 15557static struct dwarf_block *
7b5a2f43 15558dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
15559{
15560 struct dwarf_block *blk;
15561
15562 blk = (struct dwarf_block *)
7b5a2f43 15563 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
15564 return (blk);
15565}
15566
15567static struct abbrev_info *
f3dd6933 15568dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
15569{
15570 struct abbrev_info *abbrev;
15571
f3dd6933
DJ
15572 abbrev = (struct abbrev_info *)
15573 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
15574 memset (abbrev, 0, sizeof (struct abbrev_info));
15575 return (abbrev);
15576}
15577
15578static struct die_info *
b60c80d6 15579dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
15580{
15581 struct die_info *die;
b60c80d6
DJ
15582 size_t size = sizeof (struct die_info);
15583
15584 if (num_attrs > 1)
15585 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 15586
b60c80d6 15587 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
15588 memset (die, 0, sizeof (struct die_info));
15589 return (die);
15590}
2e276125
JB
15591
15592\f
15593/* Macro support. */
15594
2e276125
JB
15595/* Return the full name of file number I in *LH's file name table.
15596 Use COMP_DIR as the name of the current directory of the
15597 compilation. The result is allocated using xmalloc; the caller is
15598 responsible for freeing it. */
15599static char *
15600file_full_name (int file, struct line_header *lh, const char *comp_dir)
15601{
6a83a1e6
EZ
15602 /* Is the file number a valid index into the line header's file name
15603 table? Remember that file numbers start with one, not zero. */
15604 if (1 <= file && file <= lh->num_file_names)
15605 {
15606 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 15607
6a83a1e6
EZ
15608 if (IS_ABSOLUTE_PATH (fe->name))
15609 return xstrdup (fe->name);
15610 else
15611 {
15612 const char *dir;
15613 int dir_len;
15614 char *full_name;
15615
15616 if (fe->dir_index)
15617 dir = lh->include_dirs[fe->dir_index - 1];
15618 else
15619 dir = comp_dir;
15620
15621 if (dir)
15622 {
15623 dir_len = strlen (dir);
15624 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15625 strcpy (full_name, dir);
15626 full_name[dir_len] = '/';
15627 strcpy (full_name + dir_len + 1, fe->name);
15628 return full_name;
15629 }
15630 else
15631 return xstrdup (fe->name);
15632 }
15633 }
2e276125
JB
15634 else
15635 {
6a83a1e6
EZ
15636 /* The compiler produced a bogus file number. We can at least
15637 record the macro definitions made in the file, even if we
15638 won't be able to find the file by name. */
15639 char fake_name[80];
9a619af0 15640
6a83a1e6 15641 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 15642
6e70227d 15643 complaint (&symfile_complaints,
6a83a1e6
EZ
15644 _("bad file number in macro information (%d)"),
15645 file);
2e276125 15646
6a83a1e6 15647 return xstrdup (fake_name);
2e276125
JB
15648 }
15649}
15650
15651
15652static struct macro_source_file *
15653macro_start_file (int file, int line,
15654 struct macro_source_file *current_file,
15655 const char *comp_dir,
15656 struct line_header *lh, struct objfile *objfile)
15657{
15658 /* The full name of this source file. */
15659 char *full_name = file_full_name (file, lh, comp_dir);
15660
15661 /* We don't create a macro table for this compilation unit
15662 at all until we actually get a filename. */
15663 if (! pending_macros)
4a146b47 15664 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 15665 objfile->macro_cache);
2e276125
JB
15666
15667 if (! current_file)
abc9d0dc
TT
15668 {
15669 /* If we have no current file, then this must be the start_file
15670 directive for the compilation unit's main source file. */
15671 current_file = macro_set_main (pending_macros, full_name);
15672 macro_define_special (pending_macros);
15673 }
2e276125
JB
15674 else
15675 current_file = macro_include (current_file, line, full_name);
15676
15677 xfree (full_name);
6e70227d 15678
2e276125
JB
15679 return current_file;
15680}
15681
15682
15683/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15684 followed by a null byte. */
15685static char *
15686copy_string (const char *buf, int len)
15687{
15688 char *s = xmalloc (len + 1);
9a619af0 15689
2e276125
JB
15690 memcpy (s, buf, len);
15691 s[len] = '\0';
2e276125
JB
15692 return s;
15693}
15694
15695
15696static const char *
15697consume_improper_spaces (const char *p, const char *body)
15698{
15699 if (*p == ' ')
15700 {
4d3c2250 15701 complaint (&symfile_complaints,
3e43a32a
MS
15702 _("macro definition contains spaces "
15703 "in formal argument list:\n`%s'"),
4d3c2250 15704 body);
2e276125
JB
15705
15706 while (*p == ' ')
15707 p++;
15708 }
15709
15710 return p;
15711}
15712
15713
15714static void
15715parse_macro_definition (struct macro_source_file *file, int line,
15716 const char *body)
15717{
15718 const char *p;
15719
15720 /* The body string takes one of two forms. For object-like macro
15721 definitions, it should be:
15722
15723 <macro name> " " <definition>
15724
15725 For function-like macro definitions, it should be:
15726
15727 <macro name> "() " <definition>
15728 or
15729 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15730
15731 Spaces may appear only where explicitly indicated, and in the
15732 <definition>.
15733
15734 The Dwarf 2 spec says that an object-like macro's name is always
15735 followed by a space, but versions of GCC around March 2002 omit
6e70227d 15736 the space when the macro's definition is the empty string.
2e276125
JB
15737
15738 The Dwarf 2 spec says that there should be no spaces between the
15739 formal arguments in a function-like macro's formal argument list,
15740 but versions of GCC around March 2002 include spaces after the
15741 commas. */
15742
15743
15744 /* Find the extent of the macro name. The macro name is terminated
15745 by either a space or null character (for an object-like macro) or
15746 an opening paren (for a function-like macro). */
15747 for (p = body; *p; p++)
15748 if (*p == ' ' || *p == '(')
15749 break;
15750
15751 if (*p == ' ' || *p == '\0')
15752 {
15753 /* It's an object-like macro. */
15754 int name_len = p - body;
15755 char *name = copy_string (body, name_len);
15756 const char *replacement;
15757
15758 if (*p == ' ')
15759 replacement = body + name_len + 1;
15760 else
15761 {
4d3c2250 15762 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15763 replacement = body + name_len;
15764 }
6e70227d 15765
2e276125
JB
15766 macro_define_object (file, line, name, replacement);
15767
15768 xfree (name);
15769 }
15770 else if (*p == '(')
15771 {
15772 /* It's a function-like macro. */
15773 char *name = copy_string (body, p - body);
15774 int argc = 0;
15775 int argv_size = 1;
15776 char **argv = xmalloc (argv_size * sizeof (*argv));
15777
15778 p++;
15779
15780 p = consume_improper_spaces (p, body);
15781
15782 /* Parse the formal argument list. */
15783 while (*p && *p != ')')
15784 {
15785 /* Find the extent of the current argument name. */
15786 const char *arg_start = p;
15787
15788 while (*p && *p != ',' && *p != ')' && *p != ' ')
15789 p++;
15790
15791 if (! *p || p == arg_start)
4d3c2250 15792 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15793 else
15794 {
15795 /* Make sure argv has room for the new argument. */
15796 if (argc >= argv_size)
15797 {
15798 argv_size *= 2;
15799 argv = xrealloc (argv, argv_size * sizeof (*argv));
15800 }
15801
15802 argv[argc++] = copy_string (arg_start, p - arg_start);
15803 }
15804
15805 p = consume_improper_spaces (p, body);
15806
15807 /* Consume the comma, if present. */
15808 if (*p == ',')
15809 {
15810 p++;
15811
15812 p = consume_improper_spaces (p, body);
15813 }
15814 }
15815
15816 if (*p == ')')
15817 {
15818 p++;
15819
15820 if (*p == ' ')
15821 /* Perfectly formed definition, no complaints. */
15822 macro_define_function (file, line, name,
6e70227d 15823 argc, (const char **) argv,
2e276125
JB
15824 p + 1);
15825 else if (*p == '\0')
15826 {
15827 /* Complain, but do define it. */
4d3c2250 15828 dwarf2_macro_malformed_definition_complaint (body);
2e276125 15829 macro_define_function (file, line, name,
6e70227d 15830 argc, (const char **) argv,
2e276125
JB
15831 p);
15832 }
15833 else
15834 /* Just complain. */
4d3c2250 15835 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15836 }
15837 else
15838 /* Just complain. */
4d3c2250 15839 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15840
15841 xfree (name);
15842 {
15843 int i;
15844
15845 for (i = 0; i < argc; i++)
15846 xfree (argv[i]);
15847 }
15848 xfree (argv);
15849 }
15850 else
4d3c2250 15851 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15852}
15853
cf2c3c16
TT
15854/* Skip some bytes from BYTES according to the form given in FORM.
15855 Returns the new pointer. */
2e276125 15856
cf2c3c16 15857static gdb_byte *
f664829e 15858skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
15859 enum dwarf_form form,
15860 unsigned int offset_size,
15861 struct dwarf2_section_info *section)
2e276125 15862{
cf2c3c16 15863 unsigned int bytes_read;
2e276125 15864
cf2c3c16 15865 switch (form)
2e276125 15866 {
cf2c3c16
TT
15867 case DW_FORM_data1:
15868 case DW_FORM_flag:
15869 ++bytes;
15870 break;
15871
15872 case DW_FORM_data2:
15873 bytes += 2;
15874 break;
15875
15876 case DW_FORM_data4:
15877 bytes += 4;
15878 break;
15879
15880 case DW_FORM_data8:
15881 bytes += 8;
15882 break;
15883
15884 case DW_FORM_string:
15885 read_direct_string (abfd, bytes, &bytes_read);
15886 bytes += bytes_read;
15887 break;
15888
15889 case DW_FORM_sec_offset:
15890 case DW_FORM_strp:
15891 bytes += offset_size;
15892 break;
15893
15894 case DW_FORM_block:
15895 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15896 bytes += bytes_read;
15897 break;
15898
15899 case DW_FORM_block1:
15900 bytes += 1 + read_1_byte (abfd, bytes);
15901 break;
15902 case DW_FORM_block2:
15903 bytes += 2 + read_2_bytes (abfd, bytes);
15904 break;
15905 case DW_FORM_block4:
15906 bytes += 4 + read_4_bytes (abfd, bytes);
15907 break;
15908
15909 case DW_FORM_sdata:
15910 case DW_FORM_udata:
3019eac3
DE
15911 case DW_FORM_GNU_addr_index:
15912 case DW_FORM_GNU_str_index:
f664829e
DE
15913 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
15914 if (bytes == NULL)
15915 {
15916 dwarf2_section_buffer_overflow_complaint (section);
15917 return NULL;
15918 }
cf2c3c16
TT
15919 break;
15920
15921 default:
15922 {
15923 complain:
15924 complaint (&symfile_complaints,
15925 _("invalid form 0x%x in `%s'"),
15926 form,
15927 section->asection->name);
15928 return NULL;
15929 }
2e276125
JB
15930 }
15931
cf2c3c16
TT
15932 return bytes;
15933}
757a13d0 15934
cf2c3c16
TT
15935/* A helper for dwarf_decode_macros that handles skipping an unknown
15936 opcode. Returns an updated pointer to the macro data buffer; or,
15937 on error, issues a complaint and returns NULL. */
757a13d0 15938
cf2c3c16
TT
15939static gdb_byte *
15940skip_unknown_opcode (unsigned int opcode,
15941 gdb_byte **opcode_definitions,
f664829e 15942 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
15943 bfd *abfd,
15944 unsigned int offset_size,
15945 struct dwarf2_section_info *section)
15946{
15947 unsigned int bytes_read, i;
15948 unsigned long arg;
15949 gdb_byte *defn;
2e276125 15950
cf2c3c16 15951 if (opcode_definitions[opcode] == NULL)
2e276125 15952 {
cf2c3c16
TT
15953 complaint (&symfile_complaints,
15954 _("unrecognized DW_MACFINO opcode 0x%x"),
15955 opcode);
15956 return NULL;
15957 }
2e276125 15958
cf2c3c16
TT
15959 defn = opcode_definitions[opcode];
15960 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15961 defn += bytes_read;
2e276125 15962
cf2c3c16
TT
15963 for (i = 0; i < arg; ++i)
15964 {
f664829e
DE
15965 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
15966 section);
cf2c3c16
TT
15967 if (mac_ptr == NULL)
15968 {
15969 /* skip_form_bytes already issued the complaint. */
15970 return NULL;
15971 }
15972 }
757a13d0 15973
cf2c3c16
TT
15974 return mac_ptr;
15975}
757a13d0 15976
cf2c3c16
TT
15977/* A helper function which parses the header of a macro section.
15978 If the macro section is the extended (for now called "GNU") type,
15979 then this updates *OFFSET_SIZE. Returns a pointer to just after
15980 the header, or issues a complaint and returns NULL on error. */
757a13d0 15981
cf2c3c16
TT
15982static gdb_byte *
15983dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15984 bfd *abfd,
15985 gdb_byte *mac_ptr,
15986 unsigned int *offset_size,
15987 int section_is_gnu)
15988{
15989 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15990
cf2c3c16
TT
15991 if (section_is_gnu)
15992 {
15993 unsigned int version, flags;
757a13d0 15994
cf2c3c16
TT
15995 version = read_2_bytes (abfd, mac_ptr);
15996 if (version != 4)
15997 {
15998 complaint (&symfile_complaints,
15999 _("unrecognized version `%d' in .debug_macro section"),
16000 version);
16001 return NULL;
16002 }
16003 mac_ptr += 2;
757a13d0 16004
cf2c3c16
TT
16005 flags = read_1_byte (abfd, mac_ptr);
16006 ++mac_ptr;
16007 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 16008
cf2c3c16
TT
16009 if ((flags & 2) != 0)
16010 /* We don't need the line table offset. */
16011 mac_ptr += *offset_size;
757a13d0 16012
cf2c3c16
TT
16013 /* Vendor opcode descriptions. */
16014 if ((flags & 4) != 0)
16015 {
16016 unsigned int i, count;
757a13d0 16017
cf2c3c16
TT
16018 count = read_1_byte (abfd, mac_ptr);
16019 ++mac_ptr;
16020 for (i = 0; i < count; ++i)
16021 {
16022 unsigned int opcode, bytes_read;
16023 unsigned long arg;
16024
16025 opcode = read_1_byte (abfd, mac_ptr);
16026 ++mac_ptr;
16027 opcode_definitions[opcode] = mac_ptr;
16028 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16029 mac_ptr += bytes_read;
16030 mac_ptr += arg;
16031 }
757a13d0 16032 }
cf2c3c16 16033 }
757a13d0 16034
cf2c3c16
TT
16035 return mac_ptr;
16036}
757a13d0 16037
cf2c3c16 16038/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 16039 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
16040
16041static void
16042dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
16043 struct macro_source_file *current_file,
16044 struct line_header *lh, char *comp_dir,
16045 struct dwarf2_section_info *section,
16046 int section_is_gnu,
16047 unsigned int offset_size,
8fc3fc34
TT
16048 struct objfile *objfile,
16049 htab_t include_hash)
cf2c3c16
TT
16050{
16051 enum dwarf_macro_record_type macinfo_type;
16052 int at_commandline;
16053 gdb_byte *opcode_definitions[256];
757a13d0 16054
cf2c3c16
TT
16055 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16056 &offset_size, section_is_gnu);
16057 if (mac_ptr == NULL)
16058 {
16059 /* We already issued a complaint. */
16060 return;
16061 }
757a13d0
JK
16062
16063 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
16064 GDB is still reading the definitions from command line. First
16065 DW_MACINFO_start_file will need to be ignored as it was already executed
16066 to create CURRENT_FILE for the main source holding also the command line
16067 definitions. On first met DW_MACINFO_start_file this flag is reset to
16068 normally execute all the remaining DW_MACINFO_start_file macinfos. */
16069
16070 at_commandline = 1;
16071
16072 do
16073 {
16074 /* Do we at least have room for a macinfo type byte? */
16075 if (mac_ptr >= mac_end)
16076 {
f664829e 16077 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
16078 break;
16079 }
16080
16081 macinfo_type = read_1_byte (abfd, mac_ptr);
16082 mac_ptr++;
16083
cf2c3c16
TT
16084 /* Note that we rely on the fact that the corresponding GNU and
16085 DWARF constants are the same. */
757a13d0
JK
16086 switch (macinfo_type)
16087 {
16088 /* A zero macinfo type indicates the end of the macro
16089 information. */
16090 case 0:
16091 break;
2e276125 16092
cf2c3c16
TT
16093 case DW_MACRO_GNU_define:
16094 case DW_MACRO_GNU_undef:
16095 case DW_MACRO_GNU_define_indirect:
16096 case DW_MACRO_GNU_undef_indirect:
2e276125 16097 {
891d2f0b 16098 unsigned int bytes_read;
2e276125
JB
16099 int line;
16100 char *body;
cf2c3c16 16101 int is_define;
2e276125 16102
cf2c3c16
TT
16103 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16104 mac_ptr += bytes_read;
16105
16106 if (macinfo_type == DW_MACRO_GNU_define
16107 || macinfo_type == DW_MACRO_GNU_undef)
16108 {
16109 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16110 mac_ptr += bytes_read;
16111 }
16112 else
16113 {
16114 LONGEST str_offset;
16115
16116 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16117 mac_ptr += offset_size;
2e276125 16118
cf2c3c16
TT
16119 body = read_indirect_string_at_offset (abfd, str_offset);
16120 }
16121
16122 is_define = (macinfo_type == DW_MACRO_GNU_define
16123 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 16124 if (! current_file)
757a13d0
JK
16125 {
16126 /* DWARF violation as no main source is present. */
16127 complaint (&symfile_complaints,
16128 _("debug info with no main source gives macro %s "
16129 "on line %d: %s"),
cf2c3c16
TT
16130 is_define ? _("definition") : _("undefinition"),
16131 line, body);
757a13d0
JK
16132 break;
16133 }
3e43a32a
MS
16134 if ((line == 0 && !at_commandline)
16135 || (line != 0 && at_commandline))
4d3c2250 16136 complaint (&symfile_complaints,
757a13d0
JK
16137 _("debug info gives %s macro %s with %s line %d: %s"),
16138 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 16139 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
16140 line == 0 ? _("zero") : _("non-zero"), line, body);
16141
cf2c3c16 16142 if (is_define)
757a13d0 16143 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
16144 else
16145 {
16146 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16147 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16148 macro_undef (current_file, line, body);
16149 }
2e276125
JB
16150 }
16151 break;
16152
cf2c3c16 16153 case DW_MACRO_GNU_start_file:
2e276125 16154 {
891d2f0b 16155 unsigned int bytes_read;
2e276125
JB
16156 int line, file;
16157
16158 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16159 mac_ptr += bytes_read;
16160 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16161 mac_ptr += bytes_read;
16162
3e43a32a
MS
16163 if ((line == 0 && !at_commandline)
16164 || (line != 0 && at_commandline))
757a13d0
JK
16165 complaint (&symfile_complaints,
16166 _("debug info gives source %d included "
16167 "from %s at %s line %d"),
16168 file, at_commandline ? _("command-line") : _("file"),
16169 line == 0 ? _("zero") : _("non-zero"), line);
16170
16171 if (at_commandline)
16172 {
cf2c3c16
TT
16173 /* This DW_MACRO_GNU_start_file was executed in the
16174 pass one. */
757a13d0
JK
16175 at_commandline = 0;
16176 }
16177 else
16178 current_file = macro_start_file (file, line,
16179 current_file, comp_dir,
cf2c3c16 16180 lh, objfile);
2e276125
JB
16181 }
16182 break;
16183
cf2c3c16 16184 case DW_MACRO_GNU_end_file:
2e276125 16185 if (! current_file)
4d3c2250 16186 complaint (&symfile_complaints,
3e43a32a
MS
16187 _("macro debug info has an unmatched "
16188 "`close_file' directive"));
2e276125
JB
16189 else
16190 {
16191 current_file = current_file->included_by;
16192 if (! current_file)
16193 {
cf2c3c16 16194 enum dwarf_macro_record_type next_type;
2e276125
JB
16195
16196 /* GCC circa March 2002 doesn't produce the zero
16197 type byte marking the end of the compilation
16198 unit. Complain if it's not there, but exit no
16199 matter what. */
16200
16201 /* Do we at least have room for a macinfo type byte? */
16202 if (mac_ptr >= mac_end)
16203 {
f664829e 16204 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
16205 return;
16206 }
16207
16208 /* We don't increment mac_ptr here, so this is just
16209 a look-ahead. */
16210 next_type = read_1_byte (abfd, mac_ptr);
16211 if (next_type != 0)
4d3c2250 16212 complaint (&symfile_complaints,
3e43a32a
MS
16213 _("no terminating 0-type entry for "
16214 "macros in `.debug_macinfo' section"));
2e276125
JB
16215
16216 return;
16217 }
16218 }
16219 break;
16220
cf2c3c16
TT
16221 case DW_MACRO_GNU_transparent_include:
16222 {
16223 LONGEST offset;
8fc3fc34 16224 void **slot;
cf2c3c16
TT
16225
16226 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16227 mac_ptr += offset_size;
16228
8fc3fc34
TT
16229 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16230 if (*slot != NULL)
16231 {
16232 /* This has actually happened; see
16233 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16234 complaint (&symfile_complaints,
16235 _("recursive DW_MACRO_GNU_transparent_include in "
16236 ".debug_macro section"));
16237 }
16238 else
16239 {
16240 *slot = mac_ptr;
16241
16242 dwarf_decode_macro_bytes (abfd,
16243 section->buffer + offset,
16244 mac_end, current_file,
16245 lh, comp_dir,
16246 section, section_is_gnu,
16247 offset_size, objfile, include_hash);
16248
16249 htab_remove_elt (include_hash, mac_ptr);
16250 }
cf2c3c16
TT
16251 }
16252 break;
16253
2e276125 16254 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
16255 if (!section_is_gnu)
16256 {
16257 unsigned int bytes_read;
16258 int constant;
2e276125 16259
cf2c3c16
TT
16260 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16261 mac_ptr += bytes_read;
16262 read_direct_string (abfd, mac_ptr, &bytes_read);
16263 mac_ptr += bytes_read;
2e276125 16264
cf2c3c16
TT
16265 /* We don't recognize any vendor extensions. */
16266 break;
16267 }
16268 /* FALLTHROUGH */
16269
16270 default:
16271 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 16272 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
16273 section);
16274 if (mac_ptr == NULL)
16275 return;
16276 break;
2e276125 16277 }
757a13d0 16278 } while (macinfo_type != 0);
2e276125 16279}
8e19ed76 16280
cf2c3c16
TT
16281static void
16282dwarf_decode_macros (struct line_header *lh, unsigned int offset,
16283 char *comp_dir, bfd *abfd,
16284 struct dwarf2_cu *cu,
16285 struct dwarf2_section_info *section,
fceca515 16286 int section_is_gnu, const char *section_name)
cf2c3c16 16287{
bb5ed363 16288 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
16289 gdb_byte *mac_ptr, *mac_end;
16290 struct macro_source_file *current_file = 0;
16291 enum dwarf_macro_record_type macinfo_type;
16292 unsigned int offset_size = cu->header.offset_size;
16293 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
16294 struct cleanup *cleanup;
16295 htab_t include_hash;
16296 void **slot;
cf2c3c16 16297
bb5ed363 16298 dwarf2_read_section (objfile, section);
cf2c3c16
TT
16299 if (section->buffer == NULL)
16300 {
fceca515 16301 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
16302 return;
16303 }
16304
16305 /* First pass: Find the name of the base filename.
16306 This filename is needed in order to process all macros whose definition
16307 (or undefinition) comes from the command line. These macros are defined
16308 before the first DW_MACINFO_start_file entry, and yet still need to be
16309 associated to the base file.
16310
16311 To determine the base file name, we scan the macro definitions until we
16312 reach the first DW_MACINFO_start_file entry. We then initialize
16313 CURRENT_FILE accordingly so that any macro definition found before the
16314 first DW_MACINFO_start_file can still be associated to the base file. */
16315
16316 mac_ptr = section->buffer + offset;
16317 mac_end = section->buffer + section->size;
16318
16319 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16320 &offset_size, section_is_gnu);
16321 if (mac_ptr == NULL)
16322 {
16323 /* We already issued a complaint. */
16324 return;
16325 }
16326
16327 do
16328 {
16329 /* Do we at least have room for a macinfo type byte? */
16330 if (mac_ptr >= mac_end)
16331 {
16332 /* Complaint is printed during the second pass as GDB will probably
16333 stop the first pass earlier upon finding
16334 DW_MACINFO_start_file. */
16335 break;
16336 }
16337
16338 macinfo_type = read_1_byte (abfd, mac_ptr);
16339 mac_ptr++;
16340
16341 /* Note that we rely on the fact that the corresponding GNU and
16342 DWARF constants are the same. */
16343 switch (macinfo_type)
16344 {
16345 /* A zero macinfo type indicates the end of the macro
16346 information. */
16347 case 0:
16348 break;
16349
16350 case DW_MACRO_GNU_define:
16351 case DW_MACRO_GNU_undef:
16352 /* Only skip the data by MAC_PTR. */
16353 {
16354 unsigned int bytes_read;
16355
16356 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16357 mac_ptr += bytes_read;
16358 read_direct_string (abfd, mac_ptr, &bytes_read);
16359 mac_ptr += bytes_read;
16360 }
16361 break;
16362
16363 case DW_MACRO_GNU_start_file:
16364 {
16365 unsigned int bytes_read;
16366 int line, file;
16367
16368 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16369 mac_ptr += bytes_read;
16370 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16371 mac_ptr += bytes_read;
16372
16373 current_file = macro_start_file (file, line, current_file,
bb5ed363 16374 comp_dir, lh, objfile);
cf2c3c16
TT
16375 }
16376 break;
16377
16378 case DW_MACRO_GNU_end_file:
16379 /* No data to skip by MAC_PTR. */
16380 break;
16381
16382 case DW_MACRO_GNU_define_indirect:
16383 case DW_MACRO_GNU_undef_indirect:
16384 {
16385 unsigned int bytes_read;
16386
16387 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16388 mac_ptr += bytes_read;
16389 mac_ptr += offset_size;
16390 }
16391 break;
16392
16393 case DW_MACRO_GNU_transparent_include:
16394 /* Note that, according to the spec, a transparent include
16395 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16396 skip this opcode. */
16397 mac_ptr += offset_size;
16398 break;
16399
16400 case DW_MACINFO_vendor_ext:
16401 /* Only skip the data by MAC_PTR. */
16402 if (!section_is_gnu)
16403 {
16404 unsigned int bytes_read;
16405
16406 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16407 mac_ptr += bytes_read;
16408 read_direct_string (abfd, mac_ptr, &bytes_read);
16409 mac_ptr += bytes_read;
16410 }
16411 /* FALLTHROUGH */
16412
16413 default:
16414 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 16415 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
16416 section);
16417 if (mac_ptr == NULL)
16418 return;
16419 break;
16420 }
16421 } while (macinfo_type != 0 && current_file == NULL);
16422
16423 /* Second pass: Process all entries.
16424
16425 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16426 command-line macro definitions/undefinitions. This flag is unset when we
16427 reach the first DW_MACINFO_start_file entry. */
16428
8fc3fc34
TT
16429 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16430 NULL, xcalloc, xfree);
16431 cleanup = make_cleanup_htab_delete (include_hash);
16432 mac_ptr = section->buffer + offset;
16433 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16434 *slot = mac_ptr;
16435 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
cf2c3c16 16436 current_file, lh, comp_dir, section, section_is_gnu,
8fc3fc34
TT
16437 offset_size, objfile, include_hash);
16438 do_cleanups (cleanup);
cf2c3c16
TT
16439}
16440
8e19ed76 16441/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 16442 if so return true else false. */
380bca97 16443
8e19ed76
PS
16444static int
16445attr_form_is_block (struct attribute *attr)
16446{
16447 return (attr == NULL ? 0 :
16448 attr->form == DW_FORM_block1
16449 || attr->form == DW_FORM_block2
16450 || attr->form == DW_FORM_block4
2dc7f7b3
TT
16451 || attr->form == DW_FORM_block
16452 || attr->form == DW_FORM_exprloc);
8e19ed76 16453}
4c2df51b 16454
c6a0999f
JB
16455/* Return non-zero if ATTR's value is a section offset --- classes
16456 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16457 You may use DW_UNSND (attr) to retrieve such offsets.
16458
16459 Section 7.5.4, "Attribute Encodings", explains that no attribute
16460 may have a value that belongs to more than one of these classes; it
16461 would be ambiguous if we did, because we use the same forms for all
16462 of them. */
380bca97 16463
3690dd37
JB
16464static int
16465attr_form_is_section_offset (struct attribute *attr)
16466{
16467 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
16468 || attr->form == DW_FORM_data8
16469 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
16470}
16471
3690dd37
JB
16472/* Return non-zero if ATTR's value falls in the 'constant' class, or
16473 zero otherwise. When this function returns true, you can apply
16474 dwarf2_get_attr_constant_value to it.
16475
16476 However, note that for some attributes you must check
16477 attr_form_is_section_offset before using this test. DW_FORM_data4
16478 and DW_FORM_data8 are members of both the constant class, and of
16479 the classes that contain offsets into other debug sections
16480 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16481 that, if an attribute's can be either a constant or one of the
16482 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16483 taken as section offsets, not constants. */
380bca97 16484
3690dd37
JB
16485static int
16486attr_form_is_constant (struct attribute *attr)
16487{
16488 switch (attr->form)
16489 {
16490 case DW_FORM_sdata:
16491 case DW_FORM_udata:
16492 case DW_FORM_data1:
16493 case DW_FORM_data2:
16494 case DW_FORM_data4:
16495 case DW_FORM_data8:
16496 return 1;
16497 default:
16498 return 0;
16499 }
16500}
16501
3019eac3
DE
16502/* Return the .debug_loc section to use for CU.
16503 For DWO files use .debug_loc.dwo. */
16504
16505static struct dwarf2_section_info *
16506cu_debug_loc_section (struct dwarf2_cu *cu)
16507{
16508 if (cu->dwo_unit)
16509 return &cu->dwo_unit->dwo_file->sections.loc;
16510 return &dwarf2_per_objfile->loc;
16511}
16512
8cf6f0b1
TT
16513/* A helper function that fills in a dwarf2_loclist_baton. */
16514
16515static void
16516fill_in_loclist_baton (struct dwarf2_cu *cu,
16517 struct dwarf2_loclist_baton *baton,
16518 struct attribute *attr)
16519{
3019eac3
DE
16520 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16521
16522 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
16523
16524 baton->per_cu = cu->per_cu;
16525 gdb_assert (baton->per_cu);
16526 /* We don't know how long the location list is, but make sure we
16527 don't run off the edge of the section. */
3019eac3
DE
16528 baton->size = section->size - DW_UNSND (attr);
16529 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 16530 baton->base_address = cu->base_address;
f664829e 16531 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
16532}
16533
4c2df51b
DJ
16534static void
16535dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 16536 struct dwarf2_cu *cu)
4c2df51b 16537{
bb5ed363 16538 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 16539 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 16540
3690dd37 16541 if (attr_form_is_section_offset (attr)
3019eac3 16542 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
16543 the section. If so, fall through to the complaint in the
16544 other branch. */
3019eac3 16545 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 16546 {
0d53c4c4 16547 struct dwarf2_loclist_baton *baton;
4c2df51b 16548
bb5ed363 16549 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16550 sizeof (struct dwarf2_loclist_baton));
4c2df51b 16551
8cf6f0b1 16552 fill_in_loclist_baton (cu, baton, attr);
be391dca 16553
d00adf39 16554 if (cu->base_known == 0)
0d53c4c4 16555 complaint (&symfile_complaints,
3e43a32a
MS
16556 _("Location list used without "
16557 "specifying the CU base address."));
4c2df51b 16558
768a979c 16559 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
16560 SYMBOL_LOCATION_BATON (sym) = baton;
16561 }
16562 else
16563 {
16564 struct dwarf2_locexpr_baton *baton;
16565
bb5ed363 16566 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16567 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
16568 baton->per_cu = cu->per_cu;
16569 gdb_assert (baton->per_cu);
0d53c4c4
DJ
16570
16571 if (attr_form_is_block (attr))
16572 {
16573 /* Note that we're just copying the block's data pointer
16574 here, not the actual data. We're still pointing into the
6502dd73
DJ
16575 info_buffer for SYM's objfile; right now we never release
16576 that buffer, but when we do clean up properly this may
16577 need to change. */
0d53c4c4
DJ
16578 baton->size = DW_BLOCK (attr)->size;
16579 baton->data = DW_BLOCK (attr)->data;
16580 }
16581 else
16582 {
16583 dwarf2_invalid_attrib_class_complaint ("location description",
16584 SYMBOL_NATURAL_NAME (sym));
16585 baton->size = 0;
0d53c4c4 16586 }
6e70227d 16587
768a979c 16588 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
16589 SYMBOL_LOCATION_BATON (sym) = baton;
16590 }
4c2df51b 16591}
6502dd73 16592
9aa1f1e3
TT
16593/* Return the OBJFILE associated with the compilation unit CU. If CU
16594 came from a separate debuginfo file, then the master objfile is
16595 returned. */
ae0d2f24
UW
16596
16597struct objfile *
16598dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16599{
9291a0cd 16600 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
16601
16602 /* Return the master objfile, so that we can report and look up the
16603 correct file containing this variable. */
16604 if (objfile->separate_debug_objfile_backlink)
16605 objfile = objfile->separate_debug_objfile_backlink;
16606
16607 return objfile;
16608}
16609
96408a79
SA
16610/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16611 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16612 CU_HEADERP first. */
16613
16614static const struct comp_unit_head *
16615per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16616 struct dwarf2_per_cu_data *per_cu)
16617{
16618 struct objfile *objfile;
16619 struct dwarf2_per_objfile *per_objfile;
16620 gdb_byte *info_ptr;
16621
16622 if (per_cu->cu)
16623 return &per_cu->cu->header;
16624
16625 objfile = per_cu->objfile;
16626 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
b64f50a1 16627 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
96408a79
SA
16628
16629 memset (cu_headerp, 0, sizeof (*cu_headerp));
16630 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16631
16632 return cu_headerp;
16633}
16634
ae0d2f24
UW
16635/* Return the address size given in the compilation unit header for CU. */
16636
98714339 16637int
ae0d2f24
UW
16638dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16639{
96408a79
SA
16640 struct comp_unit_head cu_header_local;
16641 const struct comp_unit_head *cu_headerp;
c471e790 16642
96408a79
SA
16643 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16644
16645 return cu_headerp->addr_size;
ae0d2f24
UW
16646}
16647
9eae7c52
TT
16648/* Return the offset size given in the compilation unit header for CU. */
16649
16650int
16651dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16652{
96408a79
SA
16653 struct comp_unit_head cu_header_local;
16654 const struct comp_unit_head *cu_headerp;
9c6c53f7 16655
96408a79
SA
16656 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16657
16658 return cu_headerp->offset_size;
16659}
16660
16661/* See its dwarf2loc.h declaration. */
16662
16663int
16664dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16665{
16666 struct comp_unit_head cu_header_local;
16667 const struct comp_unit_head *cu_headerp;
16668
16669 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16670
16671 if (cu_headerp->version == 2)
16672 return cu_headerp->addr_size;
16673 else
16674 return cu_headerp->offset_size;
181cebd4
JK
16675}
16676
9aa1f1e3
TT
16677/* Return the text offset of the CU. The returned offset comes from
16678 this CU's objfile. If this objfile came from a separate debuginfo
16679 file, then the offset may be different from the corresponding
16680 offset in the parent objfile. */
16681
16682CORE_ADDR
16683dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16684{
bb3fa9d0 16685 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
16686
16687 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16688}
16689
348e048f
DE
16690/* Locate the .debug_info compilation unit from CU's objfile which contains
16691 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
16692
16693static struct dwarf2_per_cu_data *
b64f50a1 16694dwarf2_find_containing_comp_unit (sect_offset offset,
ae038cb0
DJ
16695 struct objfile *objfile)
16696{
16697 struct dwarf2_per_cu_data *this_cu;
16698 int low, high;
16699
ae038cb0
DJ
16700 low = 0;
16701 high = dwarf2_per_objfile->n_comp_units - 1;
16702 while (high > low)
16703 {
16704 int mid = low + (high - low) / 2;
9a619af0 16705
b64f50a1
JK
16706 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16707 >= offset.sect_off)
ae038cb0
DJ
16708 high = mid;
16709 else
16710 low = mid + 1;
16711 }
16712 gdb_assert (low == high);
b64f50a1
JK
16713 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16714 > offset.sect_off)
ae038cb0 16715 {
10b3939b 16716 if (low == 0)
8a3fe4f8
AC
16717 error (_("Dwarf Error: could not find partial DIE containing "
16718 "offset 0x%lx [in module %s]"),
b64f50a1 16719 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 16720
b64f50a1
JK
16721 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16722 <= offset.sect_off);
ae038cb0
DJ
16723 return dwarf2_per_objfile->all_comp_units[low-1];
16724 }
16725 else
16726 {
16727 this_cu = dwarf2_per_objfile->all_comp_units[low];
16728 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
16729 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16730 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16731 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
16732 return this_cu;
16733 }
16734}
16735
23745b47 16736/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 16737
9816fde3 16738static void
23745b47 16739init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 16740{
9816fde3 16741 memset (cu, 0, sizeof (*cu));
23745b47
DE
16742 per_cu->cu = cu;
16743 cu->per_cu = per_cu;
16744 cu->objfile = per_cu->objfile;
93311388 16745 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
16746}
16747
16748/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16749
16750static void
95554aad
TT
16751prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
16752 enum language pretend_language)
9816fde3
JK
16753{
16754 struct attribute *attr;
16755
16756 /* Set the language we're debugging. */
16757 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16758 if (attr)
16759 set_cu_language (DW_UNSND (attr), cu);
16760 else
9cded63f 16761 {
95554aad 16762 cu->language = pretend_language;
9cded63f
TT
16763 cu->language_defn = language_def (cu->language);
16764 }
dee91e82
DE
16765
16766 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
16767 if (attr)
16768 cu->producer = DW_STRING (attr);
93311388
DE
16769}
16770
ae038cb0
DJ
16771/* Release one cached compilation unit, CU. We unlink it from the tree
16772 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
16773 the caller is responsible for that.
16774 NOTE: DATA is a void * because this function is also used as a
16775 cleanup routine. */
ae038cb0
DJ
16776
16777static void
68dc6402 16778free_heap_comp_unit (void *data)
ae038cb0
DJ
16779{
16780 struct dwarf2_cu *cu = data;
16781
23745b47
DE
16782 gdb_assert (cu->per_cu != NULL);
16783 cu->per_cu->cu = NULL;
ae038cb0
DJ
16784 cu->per_cu = NULL;
16785
16786 obstack_free (&cu->comp_unit_obstack, NULL);
16787
16788 xfree (cu);
16789}
16790
72bf9492 16791/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 16792 when we're finished with it. We can't free the pointer itself, but be
dee91e82 16793 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
16794
16795static void
16796free_stack_comp_unit (void *data)
16797{
16798 struct dwarf2_cu *cu = data;
16799
23745b47
DE
16800 gdb_assert (cu->per_cu != NULL);
16801 cu->per_cu->cu = NULL;
16802 cu->per_cu = NULL;
16803
72bf9492
DJ
16804 obstack_free (&cu->comp_unit_obstack, NULL);
16805 cu->partial_dies = NULL;
ae038cb0
DJ
16806}
16807
16808/* Free all cached compilation units. */
16809
16810static void
16811free_cached_comp_units (void *data)
16812{
16813 struct dwarf2_per_cu_data *per_cu, **last_chain;
16814
16815 per_cu = dwarf2_per_objfile->read_in_chain;
16816 last_chain = &dwarf2_per_objfile->read_in_chain;
16817 while (per_cu != NULL)
16818 {
16819 struct dwarf2_per_cu_data *next_cu;
16820
16821 next_cu = per_cu->cu->read_in_chain;
16822
68dc6402 16823 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16824 *last_chain = next_cu;
16825
16826 per_cu = next_cu;
16827 }
16828}
16829
16830/* Increase the age counter on each cached compilation unit, and free
16831 any that are too old. */
16832
16833static void
16834age_cached_comp_units (void)
16835{
16836 struct dwarf2_per_cu_data *per_cu, **last_chain;
16837
16838 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16839 per_cu = dwarf2_per_objfile->read_in_chain;
16840 while (per_cu != NULL)
16841 {
16842 per_cu->cu->last_used ++;
16843 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16844 dwarf2_mark (per_cu->cu);
16845 per_cu = per_cu->cu->read_in_chain;
16846 }
16847
16848 per_cu = dwarf2_per_objfile->read_in_chain;
16849 last_chain = &dwarf2_per_objfile->read_in_chain;
16850 while (per_cu != NULL)
16851 {
16852 struct dwarf2_per_cu_data *next_cu;
16853
16854 next_cu = per_cu->cu->read_in_chain;
16855
16856 if (!per_cu->cu->mark)
16857 {
68dc6402 16858 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16859 *last_chain = next_cu;
16860 }
16861 else
16862 last_chain = &per_cu->cu->read_in_chain;
16863
16864 per_cu = next_cu;
16865 }
16866}
16867
16868/* Remove a single compilation unit from the cache. */
16869
16870static void
dee91e82 16871free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
16872{
16873 struct dwarf2_per_cu_data *per_cu, **last_chain;
16874
16875 per_cu = dwarf2_per_objfile->read_in_chain;
16876 last_chain = &dwarf2_per_objfile->read_in_chain;
16877 while (per_cu != NULL)
16878 {
16879 struct dwarf2_per_cu_data *next_cu;
16880
16881 next_cu = per_cu->cu->read_in_chain;
16882
dee91e82 16883 if (per_cu == target_per_cu)
ae038cb0 16884 {
68dc6402 16885 free_heap_comp_unit (per_cu->cu);
dee91e82 16886 per_cu->cu = NULL;
ae038cb0
DJ
16887 *last_chain = next_cu;
16888 break;
16889 }
16890 else
16891 last_chain = &per_cu->cu->read_in_chain;
16892
16893 per_cu = next_cu;
16894 }
16895}
16896
fe3e1990
DJ
16897/* Release all extra memory associated with OBJFILE. */
16898
16899void
16900dwarf2_free_objfile (struct objfile *objfile)
16901{
16902 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16903
16904 if (dwarf2_per_objfile == NULL)
16905 return;
16906
16907 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16908 free_cached_comp_units (NULL);
16909
7b9f3c50
DE
16910 if (dwarf2_per_objfile->quick_file_names_table)
16911 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16912
fe3e1990
DJ
16913 /* Everything else should be on the objfile obstack. */
16914}
16915
dee91e82
DE
16916/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
16917 We store these in a hash table separate from the DIEs, and preserve them
16918 when the DIEs are flushed out of cache.
16919
16920 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
16921 uniquely identify the type. A file may have multiple .debug_types sections,
16922 or the type may come from a DWO file. We have to use something in
16923 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
16924 routine, get_die_type_at_offset, from outside this file, and thus won't
16925 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
16926 of the objfile. */
1c379e20 16927
dee91e82 16928struct dwarf2_per_cu_offset_and_type
1c379e20 16929{
dee91e82 16930 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 16931 sect_offset offset;
1c379e20
DJ
16932 struct type *type;
16933};
16934
dee91e82 16935/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
16936
16937static hashval_t
dee91e82 16938per_cu_offset_and_type_hash (const void *item)
1c379e20 16939{
dee91e82 16940 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 16941
dee91e82 16942 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
16943}
16944
dee91e82 16945/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
16946
16947static int
dee91e82 16948per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 16949{
dee91e82
DE
16950 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
16951 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16952
dee91e82
DE
16953 return (ofs_lhs->per_cu == ofs_rhs->per_cu
16954 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
16955}
16956
16957/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16958 table if necessary. For convenience, return TYPE.
16959
16960 The DIEs reading must have careful ordering to:
16961 * Not cause infite loops trying to read in DIEs as a prerequisite for
16962 reading current DIE.
16963 * Not trying to dereference contents of still incompletely read in types
16964 while reading in other DIEs.
16965 * Enable referencing still incompletely read in types just by a pointer to
16966 the type without accessing its fields.
16967
16968 Therefore caller should follow these rules:
16969 * Try to fetch any prerequisite types we may need to build this DIE type
16970 before building the type and calling set_die_type.
e71ec853 16971 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16972 possible before fetching more types to complete the current type.
16973 * Make the type as complete as possible before fetching more types. */
1c379e20 16974
f792889a 16975static struct type *
1c379e20
DJ
16976set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16977{
dee91e82 16978 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 16979 struct objfile *objfile = cu->objfile;
1c379e20 16980
b4ba55a1
JB
16981 /* For Ada types, make sure that the gnat-specific data is always
16982 initialized (if not already set). There are a few types where
16983 we should not be doing so, because the type-specific area is
16984 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16985 where the type-specific area is used to store the floatformat).
16986 But this is not a problem, because the gnat-specific information
16987 is actually not needed for these types. */
16988 if (need_gnat_info (cu)
16989 && TYPE_CODE (type) != TYPE_CODE_FUNC
16990 && TYPE_CODE (type) != TYPE_CODE_FLT
16991 && !HAVE_GNAT_AUX_INFO (type))
16992 INIT_GNAT_SPECIFIC (type);
16993
dee91e82 16994 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 16995 {
dee91e82
DE
16996 dwarf2_per_objfile->die_type_hash =
16997 htab_create_alloc_ex (127,
16998 per_cu_offset_and_type_hash,
16999 per_cu_offset_and_type_eq,
17000 NULL,
17001 &objfile->objfile_obstack,
17002 hashtab_obstack_allocate,
17003 dummy_obstack_deallocate);
f792889a 17004 }
1c379e20 17005
dee91e82 17006 ofs.per_cu = cu->per_cu;
1c379e20
DJ
17007 ofs.offset = die->offset;
17008 ofs.type = type;
dee91e82
DE
17009 slot = (struct dwarf2_per_cu_offset_and_type **)
17010 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
17011 if (*slot)
17012 complaint (&symfile_complaints,
17013 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 17014 die->offset.sect_off);
673bfd45 17015 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 17016 **slot = ofs;
f792889a 17017 return type;
1c379e20
DJ
17018}
17019
380bca97 17020/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 17021 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
17022
17023static struct type *
b64f50a1 17024get_die_type_at_offset (sect_offset offset,
673bfd45 17025 struct dwarf2_per_cu_data *per_cu)
1c379e20 17026{
dee91e82 17027 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 17028
dee91e82 17029 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 17030 return NULL;
1c379e20 17031
dee91e82 17032 ofs.per_cu = per_cu;
673bfd45 17033 ofs.offset = offset;
dee91e82 17034 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
17035 if (slot)
17036 return slot->type;
17037 else
17038 return NULL;
17039}
17040
673bfd45
DE
17041/* Look up the type for DIE in the appropriate type_hash table,
17042 or return NULL if DIE does not have a saved type. */
17043
17044static struct type *
17045get_die_type (struct die_info *die, struct dwarf2_cu *cu)
17046{
17047 return get_die_type_at_offset (die->offset, cu->per_cu);
17048}
17049
10b3939b
DJ
17050/* Add a dependence relationship from CU to REF_PER_CU. */
17051
17052static void
17053dwarf2_add_dependence (struct dwarf2_cu *cu,
17054 struct dwarf2_per_cu_data *ref_per_cu)
17055{
17056 void **slot;
17057
17058 if (cu->dependencies == NULL)
17059 cu->dependencies
17060 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
17061 NULL, &cu->comp_unit_obstack,
17062 hashtab_obstack_allocate,
17063 dummy_obstack_deallocate);
17064
17065 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
17066 if (*slot == NULL)
17067 *slot = ref_per_cu;
17068}
1c379e20 17069
f504f079
DE
17070/* Subroutine of dwarf2_mark to pass to htab_traverse.
17071 Set the mark field in every compilation unit in the
ae038cb0
DJ
17072 cache that we must keep because we are keeping CU. */
17073
10b3939b
DJ
17074static int
17075dwarf2_mark_helper (void **slot, void *data)
17076{
17077 struct dwarf2_per_cu_data *per_cu;
17078
17079 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
17080
17081 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17082 reading of the chain. As such dependencies remain valid it is not much
17083 useful to track and undo them during QUIT cleanups. */
17084 if (per_cu->cu == NULL)
17085 return 1;
17086
10b3939b
DJ
17087 if (per_cu->cu->mark)
17088 return 1;
17089 per_cu->cu->mark = 1;
17090
17091 if (per_cu->cu->dependencies != NULL)
17092 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17093
17094 return 1;
17095}
17096
f504f079
DE
17097/* Set the mark field in CU and in every other compilation unit in the
17098 cache that we must keep because we are keeping CU. */
17099
ae038cb0
DJ
17100static void
17101dwarf2_mark (struct dwarf2_cu *cu)
17102{
17103 if (cu->mark)
17104 return;
17105 cu->mark = 1;
10b3939b
DJ
17106 if (cu->dependencies != NULL)
17107 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
17108}
17109
17110static void
17111dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17112{
17113 while (per_cu)
17114 {
17115 per_cu->cu->mark = 0;
17116 per_cu = per_cu->cu->read_in_chain;
17117 }
72bf9492
DJ
17118}
17119
72bf9492
DJ
17120/* Trivial hash function for partial_die_info: the hash value of a DIE
17121 is its offset in .debug_info for this objfile. */
17122
17123static hashval_t
17124partial_die_hash (const void *item)
17125{
17126 const struct partial_die_info *part_die = item;
9a619af0 17127
b64f50a1 17128 return part_die->offset.sect_off;
72bf9492
DJ
17129}
17130
17131/* Trivial comparison function for partial_die_info structures: two DIEs
17132 are equal if they have the same offset. */
17133
17134static int
17135partial_die_eq (const void *item_lhs, const void *item_rhs)
17136{
17137 const struct partial_die_info *part_die_lhs = item_lhs;
17138 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 17139
b64f50a1 17140 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
17141}
17142
ae038cb0
DJ
17143static struct cmd_list_element *set_dwarf2_cmdlist;
17144static struct cmd_list_element *show_dwarf2_cmdlist;
17145
17146static void
17147set_dwarf2_cmd (char *args, int from_tty)
17148{
17149 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17150}
17151
17152static void
17153show_dwarf2_cmd (char *args, int from_tty)
6e70227d 17154{
ae038cb0
DJ
17155 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17156}
17157
dce234bc
PP
17158/* If section described by INFO was mmapped, munmap it now. */
17159
17160static void
17161munmap_section_buffer (struct dwarf2_section_info *info)
17162{
b315ab21 17163 if (info->map_addr != NULL)
dce234bc
PP
17164 {
17165#ifdef HAVE_MMAP
b315ab21 17166 int res;
9a619af0 17167
b315ab21
TG
17168 res = munmap (info->map_addr, info->map_len);
17169 gdb_assert (res == 0);
dce234bc
PP
17170#else
17171 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 17172 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
17173#endif
17174 }
17175}
17176
17177/* munmap debug sections for OBJFILE, if necessary. */
17178
17179static void
c1bd65d0 17180dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
17181{
17182 struct dwarf2_per_objfile *data = d;
8b70b953
TT
17183 int ix;
17184 struct dwarf2_section_info *section;
9a619af0 17185
16be1145
DE
17186 /* This is sorted according to the order they're defined in to make it easier
17187 to keep in sync. */
dce234bc
PP
17188 munmap_section_buffer (&data->info);
17189 munmap_section_buffer (&data->abbrev);
17190 munmap_section_buffer (&data->line);
16be1145 17191 munmap_section_buffer (&data->loc);
dce234bc 17192 munmap_section_buffer (&data->macinfo);
cf2c3c16 17193 munmap_section_buffer (&data->macro);
16be1145 17194 munmap_section_buffer (&data->str);
dce234bc 17195 munmap_section_buffer (&data->ranges);
3019eac3 17196 munmap_section_buffer (&data->addr);
dce234bc
PP
17197 munmap_section_buffer (&data->frame);
17198 munmap_section_buffer (&data->eh_frame);
9291a0cd 17199 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
17200
17201 for (ix = 0;
17202 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17203 ++ix)
17204 munmap_section_buffer (section);
17205
95554aad
TT
17206 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17207 VEC_free (dwarf2_per_cu_ptr,
17208 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17209
8b70b953 17210 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
17211
17212 if (data->dwo_files)
17213 free_dwo_files (data->dwo_files, objfile);
9291a0cd
TT
17214}
17215
17216\f
ae2de4f8 17217/* The "save gdb-index" command. */
9291a0cd
TT
17218
17219/* The contents of the hash table we create when building the string
17220 table. */
17221struct strtab_entry
17222{
17223 offset_type offset;
17224 const char *str;
17225};
17226
559a7a62
JK
17227/* Hash function for a strtab_entry.
17228
17229 Function is used only during write_hash_table so no index format backward
17230 compatibility is needed. */
b89be57b 17231
9291a0cd
TT
17232static hashval_t
17233hash_strtab_entry (const void *e)
17234{
17235 const struct strtab_entry *entry = e;
559a7a62 17236 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
17237}
17238
17239/* Equality function for a strtab_entry. */
b89be57b 17240
9291a0cd
TT
17241static int
17242eq_strtab_entry (const void *a, const void *b)
17243{
17244 const struct strtab_entry *ea = a;
17245 const struct strtab_entry *eb = b;
17246 return !strcmp (ea->str, eb->str);
17247}
17248
17249/* Create a strtab_entry hash table. */
b89be57b 17250
9291a0cd
TT
17251static htab_t
17252create_strtab (void)
17253{
17254 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17255 xfree, xcalloc, xfree);
17256}
17257
17258/* Add a string to the constant pool. Return the string's offset in
17259 host order. */
b89be57b 17260
9291a0cd
TT
17261static offset_type
17262add_string (htab_t table, struct obstack *cpool, const char *str)
17263{
17264 void **slot;
17265 struct strtab_entry entry;
17266 struct strtab_entry *result;
17267
17268 entry.str = str;
17269 slot = htab_find_slot (table, &entry, INSERT);
17270 if (*slot)
17271 result = *slot;
17272 else
17273 {
17274 result = XNEW (struct strtab_entry);
17275 result->offset = obstack_object_size (cpool);
17276 result->str = str;
17277 obstack_grow_str0 (cpool, str);
17278 *slot = result;
17279 }
17280 return result->offset;
17281}
17282
17283/* An entry in the symbol table. */
17284struct symtab_index_entry
17285{
17286 /* The name of the symbol. */
17287 const char *name;
17288 /* The offset of the name in the constant pool. */
17289 offset_type index_offset;
17290 /* A sorted vector of the indices of all the CUs that hold an object
17291 of this name. */
17292 VEC (offset_type) *cu_indices;
17293};
17294
17295/* The symbol table. This is a power-of-2-sized hash table. */
17296struct mapped_symtab
17297{
17298 offset_type n_elements;
17299 offset_type size;
17300 struct symtab_index_entry **data;
17301};
17302
17303/* Hash function for a symtab_index_entry. */
b89be57b 17304
9291a0cd
TT
17305static hashval_t
17306hash_symtab_entry (const void *e)
17307{
17308 const struct symtab_index_entry *entry = e;
17309 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17310 sizeof (offset_type) * VEC_length (offset_type,
17311 entry->cu_indices),
17312 0);
17313}
17314
17315/* Equality function for a symtab_index_entry. */
b89be57b 17316
9291a0cd
TT
17317static int
17318eq_symtab_entry (const void *a, const void *b)
17319{
17320 const struct symtab_index_entry *ea = a;
17321 const struct symtab_index_entry *eb = b;
17322 int len = VEC_length (offset_type, ea->cu_indices);
17323 if (len != VEC_length (offset_type, eb->cu_indices))
17324 return 0;
17325 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17326 VEC_address (offset_type, eb->cu_indices),
17327 sizeof (offset_type) * len);
17328}
17329
17330/* Destroy a symtab_index_entry. */
b89be57b 17331
9291a0cd
TT
17332static void
17333delete_symtab_entry (void *p)
17334{
17335 struct symtab_index_entry *entry = p;
17336 VEC_free (offset_type, entry->cu_indices);
17337 xfree (entry);
17338}
17339
17340/* Create a hash table holding symtab_index_entry objects. */
b89be57b 17341
9291a0cd 17342static htab_t
3876f04e 17343create_symbol_hash_table (void)
9291a0cd
TT
17344{
17345 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17346 delete_symtab_entry, xcalloc, xfree);
17347}
17348
17349/* Create a new mapped symtab object. */
b89be57b 17350
9291a0cd
TT
17351static struct mapped_symtab *
17352create_mapped_symtab (void)
17353{
17354 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17355 symtab->n_elements = 0;
17356 symtab->size = 1024;
17357 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17358 return symtab;
17359}
17360
17361/* Destroy a mapped_symtab. */
b89be57b 17362
9291a0cd
TT
17363static void
17364cleanup_mapped_symtab (void *p)
17365{
17366 struct mapped_symtab *symtab = p;
17367 /* The contents of the array are freed when the other hash table is
17368 destroyed. */
17369 xfree (symtab->data);
17370 xfree (symtab);
17371}
17372
17373/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
17374 the slot.
17375
17376 Function is used only during write_hash_table so no index format backward
17377 compatibility is needed. */
b89be57b 17378
9291a0cd
TT
17379static struct symtab_index_entry **
17380find_slot (struct mapped_symtab *symtab, const char *name)
17381{
559a7a62 17382 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
17383
17384 index = hash & (symtab->size - 1);
17385 step = ((hash * 17) & (symtab->size - 1)) | 1;
17386
17387 for (;;)
17388 {
17389 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17390 return &symtab->data[index];
17391 index = (index + step) & (symtab->size - 1);
17392 }
17393}
17394
17395/* Expand SYMTAB's hash table. */
b89be57b 17396
9291a0cd
TT
17397static void
17398hash_expand (struct mapped_symtab *symtab)
17399{
17400 offset_type old_size = symtab->size;
17401 offset_type i;
17402 struct symtab_index_entry **old_entries = symtab->data;
17403
17404 symtab->size *= 2;
17405 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17406
17407 for (i = 0; i < old_size; ++i)
17408 {
17409 if (old_entries[i])
17410 {
17411 struct symtab_index_entry **slot = find_slot (symtab,
17412 old_entries[i]->name);
17413 *slot = old_entries[i];
17414 }
17415 }
17416
17417 xfree (old_entries);
17418}
17419
17420/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
17421 is the index of the CU in which the symbol appears. */
b89be57b 17422
9291a0cd
TT
17423static void
17424add_index_entry (struct mapped_symtab *symtab, const char *name,
17425 offset_type cu_index)
17426{
17427 struct symtab_index_entry **slot;
17428
17429 ++symtab->n_elements;
17430 if (4 * symtab->n_elements / 3 >= symtab->size)
17431 hash_expand (symtab);
17432
17433 slot = find_slot (symtab, name);
17434 if (!*slot)
17435 {
17436 *slot = XNEW (struct symtab_index_entry);
17437 (*slot)->name = name;
17438 (*slot)->cu_indices = NULL;
17439 }
17440 /* Don't push an index twice. Due to how we add entries we only
17441 have to check the last one. */
17442 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 17443 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
17444 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
17445}
17446
17447/* Add a vector of indices to the constant pool. */
b89be57b 17448
9291a0cd 17449static offset_type
3876f04e 17450add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
17451 struct symtab_index_entry *entry)
17452{
17453 void **slot;
17454
3876f04e 17455 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
17456 if (!*slot)
17457 {
17458 offset_type len = VEC_length (offset_type, entry->cu_indices);
17459 offset_type val = MAYBE_SWAP (len);
17460 offset_type iter;
17461 int i;
17462
17463 *slot = entry;
17464 entry->index_offset = obstack_object_size (cpool);
17465
17466 obstack_grow (cpool, &val, sizeof (val));
17467 for (i = 0;
17468 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17469 ++i)
17470 {
17471 val = MAYBE_SWAP (iter);
17472 obstack_grow (cpool, &val, sizeof (val));
17473 }
17474 }
17475 else
17476 {
17477 struct symtab_index_entry *old_entry = *slot;
17478 entry->index_offset = old_entry->index_offset;
17479 entry = old_entry;
17480 }
17481 return entry->index_offset;
17482}
17483
17484/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17485 constant pool entries going into the obstack CPOOL. */
b89be57b 17486
9291a0cd
TT
17487static void
17488write_hash_table (struct mapped_symtab *symtab,
17489 struct obstack *output, struct obstack *cpool)
17490{
17491 offset_type i;
3876f04e 17492 htab_t symbol_hash_table;
9291a0cd
TT
17493 htab_t str_table;
17494
3876f04e 17495 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 17496 str_table = create_strtab ();
3876f04e 17497
9291a0cd
TT
17498 /* We add all the index vectors to the constant pool first, to
17499 ensure alignment is ok. */
17500 for (i = 0; i < symtab->size; ++i)
17501 {
17502 if (symtab->data[i])
3876f04e 17503 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
17504 }
17505
17506 /* Now write out the hash table. */
17507 for (i = 0; i < symtab->size; ++i)
17508 {
17509 offset_type str_off, vec_off;
17510
17511 if (symtab->data[i])
17512 {
17513 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17514 vec_off = symtab->data[i]->index_offset;
17515 }
17516 else
17517 {
17518 /* While 0 is a valid constant pool index, it is not valid
17519 to have 0 for both offsets. */
17520 str_off = 0;
17521 vec_off = 0;
17522 }
17523
17524 str_off = MAYBE_SWAP (str_off);
17525 vec_off = MAYBE_SWAP (vec_off);
17526
17527 obstack_grow (output, &str_off, sizeof (str_off));
17528 obstack_grow (output, &vec_off, sizeof (vec_off));
17529 }
17530
17531 htab_delete (str_table);
3876f04e 17532 htab_delete (symbol_hash_table);
9291a0cd
TT
17533}
17534
0a5429f6
DE
17535/* Struct to map psymtab to CU index in the index file. */
17536struct psymtab_cu_index_map
17537{
17538 struct partial_symtab *psymtab;
17539 unsigned int cu_index;
17540};
17541
17542static hashval_t
17543hash_psymtab_cu_index (const void *item)
17544{
17545 const struct psymtab_cu_index_map *map = item;
17546
17547 return htab_hash_pointer (map->psymtab);
17548}
17549
17550static int
17551eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17552{
17553 const struct psymtab_cu_index_map *lhs = item_lhs;
17554 const struct psymtab_cu_index_map *rhs = item_rhs;
17555
17556 return lhs->psymtab == rhs->psymtab;
17557}
17558
17559/* Helper struct for building the address table. */
17560struct addrmap_index_data
17561{
17562 struct objfile *objfile;
17563 struct obstack *addr_obstack;
17564 htab_t cu_index_htab;
17565
17566 /* Non-zero if the previous_* fields are valid.
17567 We can't write an entry until we see the next entry (since it is only then
17568 that we know the end of the entry). */
17569 int previous_valid;
17570 /* Index of the CU in the table of all CUs in the index file. */
17571 unsigned int previous_cu_index;
0963b4bd 17572 /* Start address of the CU. */
0a5429f6
DE
17573 CORE_ADDR previous_cu_start;
17574};
17575
17576/* Write an address entry to OBSTACK. */
b89be57b 17577
9291a0cd 17578static void
0a5429f6
DE
17579add_address_entry (struct objfile *objfile, struct obstack *obstack,
17580 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 17581{
0a5429f6 17582 offset_type cu_index_to_write;
9291a0cd
TT
17583 char addr[8];
17584 CORE_ADDR baseaddr;
17585
17586 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17587
0a5429f6
DE
17588 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17589 obstack_grow (obstack, addr, 8);
17590 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17591 obstack_grow (obstack, addr, 8);
17592 cu_index_to_write = MAYBE_SWAP (cu_index);
17593 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17594}
17595
17596/* Worker function for traversing an addrmap to build the address table. */
17597
17598static int
17599add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17600{
17601 struct addrmap_index_data *data = datap;
17602 struct partial_symtab *pst = obj;
0a5429f6
DE
17603
17604 if (data->previous_valid)
17605 add_address_entry (data->objfile, data->addr_obstack,
17606 data->previous_cu_start, start_addr,
17607 data->previous_cu_index);
17608
17609 data->previous_cu_start = start_addr;
17610 if (pst != NULL)
17611 {
17612 struct psymtab_cu_index_map find_map, *map;
17613 find_map.psymtab = pst;
17614 map = htab_find (data->cu_index_htab, &find_map);
17615 gdb_assert (map != NULL);
17616 data->previous_cu_index = map->cu_index;
17617 data->previous_valid = 1;
17618 }
17619 else
17620 data->previous_valid = 0;
17621
17622 return 0;
17623}
17624
17625/* Write OBJFILE's address map to OBSTACK.
17626 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17627 in the index file. */
17628
17629static void
17630write_address_map (struct objfile *objfile, struct obstack *obstack,
17631 htab_t cu_index_htab)
17632{
17633 struct addrmap_index_data addrmap_index_data;
17634
17635 /* When writing the address table, we have to cope with the fact that
17636 the addrmap iterator only provides the start of a region; we have to
17637 wait until the next invocation to get the start of the next region. */
17638
17639 addrmap_index_data.objfile = objfile;
17640 addrmap_index_data.addr_obstack = obstack;
17641 addrmap_index_data.cu_index_htab = cu_index_htab;
17642 addrmap_index_data.previous_valid = 0;
17643
17644 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17645 &addrmap_index_data);
17646
17647 /* It's highly unlikely the last entry (end address = 0xff...ff)
17648 is valid, but we should still handle it.
17649 The end address is recorded as the start of the next region, but that
17650 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17651 anyway. */
17652 if (addrmap_index_data.previous_valid)
17653 add_address_entry (objfile, obstack,
17654 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17655 addrmap_index_data.previous_cu_index);
9291a0cd
TT
17656}
17657
17658/* Add a list of partial symbols to SYMTAB. */
b89be57b 17659
9291a0cd
TT
17660static void
17661write_psymbols (struct mapped_symtab *symtab,
987d643c 17662 htab_t psyms_seen,
9291a0cd
TT
17663 struct partial_symbol **psymp,
17664 int count,
987d643c
TT
17665 offset_type cu_index,
17666 int is_static)
9291a0cd
TT
17667{
17668 for (; count-- > 0; ++psymp)
17669 {
987d643c
TT
17670 void **slot, *lookup;
17671
9291a0cd
TT
17672 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
17673 error (_("Ada is not currently supported by the index"));
987d643c
TT
17674
17675 /* We only want to add a given psymbol once. However, we also
17676 want to account for whether it is global or static. So, we
17677 may add it twice, using slightly different values. */
17678 if (is_static)
17679 {
17680 uintptr_t val = 1 | (uintptr_t) *psymp;
17681
17682 lookup = (void *) val;
17683 }
17684 else
17685 lookup = *psymp;
17686
17687 /* Only add a given psymbol once. */
17688 slot = htab_find_slot (psyms_seen, lookup, INSERT);
17689 if (!*slot)
17690 {
17691 *slot = lookup;
bb2f58dc 17692 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 17693 }
9291a0cd
TT
17694 }
17695}
17696
17697/* Write the contents of an ("unfinished") obstack to FILE. Throw an
17698 exception if there is an error. */
b89be57b 17699
9291a0cd
TT
17700static void
17701write_obstack (FILE *file, struct obstack *obstack)
17702{
17703 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17704 file)
17705 != obstack_object_size (obstack))
17706 error (_("couldn't data write to file"));
17707}
17708
17709/* Unlink a file if the argument is not NULL. */
b89be57b 17710
9291a0cd
TT
17711static void
17712unlink_if_set (void *p)
17713{
17714 char **filename = p;
17715 if (*filename)
17716 unlink (*filename);
17717}
17718
1fd400ff
TT
17719/* A helper struct used when iterating over debug_types. */
17720struct signatured_type_index_data
17721{
17722 struct objfile *objfile;
17723 struct mapped_symtab *symtab;
17724 struct obstack *types_list;
987d643c 17725 htab_t psyms_seen;
1fd400ff
TT
17726 int cu_index;
17727};
17728
17729/* A helper function that writes a single signatured_type to an
17730 obstack. */
b89be57b 17731
1fd400ff
TT
17732static int
17733write_one_signatured_type (void **slot, void *d)
17734{
17735 struct signatured_type_index_data *info = d;
17736 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
17737 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17738 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
17739 gdb_byte val[8];
17740
17741 write_psymbols (info->symtab,
987d643c 17742 info->psyms_seen,
3e43a32a
MS
17743 info->objfile->global_psymbols.list
17744 + psymtab->globals_offset,
987d643c
TT
17745 psymtab->n_global_syms, info->cu_index,
17746 0);
1fd400ff 17747 write_psymbols (info->symtab,
987d643c 17748 info->psyms_seen,
3e43a32a
MS
17749 info->objfile->static_psymbols.list
17750 + psymtab->statics_offset,
987d643c
TT
17751 psymtab->n_static_syms, info->cu_index,
17752 1);
1fd400ff 17753
b64f50a1
JK
17754 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17755 entry->per_cu.offset.sect_off);
1fd400ff 17756 obstack_grow (info->types_list, val, 8);
3019eac3
DE
17757 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17758 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
17759 obstack_grow (info->types_list, val, 8);
17760 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17761 obstack_grow (info->types_list, val, 8);
17762
17763 ++info->cu_index;
17764
17765 return 1;
17766}
17767
95554aad
TT
17768/* Recurse into all "included" dependencies and write their symbols as
17769 if they appeared in this psymtab. */
17770
17771static void
17772recursively_write_psymbols (struct objfile *objfile,
17773 struct partial_symtab *psymtab,
17774 struct mapped_symtab *symtab,
17775 htab_t psyms_seen,
17776 offset_type cu_index)
17777{
17778 int i;
17779
17780 for (i = 0; i < psymtab->number_of_dependencies; ++i)
17781 if (psymtab->dependencies[i]->user != NULL)
17782 recursively_write_psymbols (objfile, psymtab->dependencies[i],
17783 symtab, psyms_seen, cu_index);
17784
17785 write_psymbols (symtab,
17786 psyms_seen,
17787 objfile->global_psymbols.list + psymtab->globals_offset,
17788 psymtab->n_global_syms, cu_index,
17789 0);
17790 write_psymbols (symtab,
17791 psyms_seen,
17792 objfile->static_psymbols.list + psymtab->statics_offset,
17793 psymtab->n_static_syms, cu_index,
17794 1);
17795}
17796
9291a0cd 17797/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 17798
9291a0cd
TT
17799static void
17800write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17801{
17802 struct cleanup *cleanup;
17803 char *filename, *cleanup_filename;
1fd400ff
TT
17804 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
17805 struct obstack cu_list, types_cu_list;
9291a0cd
TT
17806 int i;
17807 FILE *out_file;
17808 struct mapped_symtab *symtab;
17809 offset_type val, size_of_contents, total_len;
17810 struct stat st;
987d643c 17811 htab_t psyms_seen;
0a5429f6
DE
17812 htab_t cu_index_htab;
17813 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 17814
b4f2f049 17815 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 17816 return;
b4f2f049 17817
9291a0cd
TT
17818 if (dwarf2_per_objfile->using_index)
17819 error (_("Cannot use an index to create the index"));
17820
8b70b953
TT
17821 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17822 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17823
9291a0cd 17824 if (stat (objfile->name, &st) < 0)
7e17e088 17825 perror_with_name (objfile->name);
9291a0cd
TT
17826
17827 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17828 INDEX_SUFFIX, (char *) NULL);
17829 cleanup = make_cleanup (xfree, filename);
17830
17831 out_file = fopen (filename, "wb");
17832 if (!out_file)
17833 error (_("Can't open `%s' for writing"), filename);
17834
17835 cleanup_filename = filename;
17836 make_cleanup (unlink_if_set, &cleanup_filename);
17837
17838 symtab = create_mapped_symtab ();
17839 make_cleanup (cleanup_mapped_symtab, symtab);
17840
17841 obstack_init (&addr_obstack);
17842 make_cleanup_obstack_free (&addr_obstack);
17843
17844 obstack_init (&cu_list);
17845 make_cleanup_obstack_free (&cu_list);
17846
1fd400ff
TT
17847 obstack_init (&types_cu_list);
17848 make_cleanup_obstack_free (&types_cu_list);
17849
987d643c
TT
17850 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17851 NULL, xcalloc, xfree);
96408a79 17852 make_cleanup_htab_delete (psyms_seen);
987d643c 17853
0a5429f6
DE
17854 /* While we're scanning CU's create a table that maps a psymtab pointer
17855 (which is what addrmap records) to its index (which is what is recorded
17856 in the index file). This will later be needed to write the address
17857 table. */
17858 cu_index_htab = htab_create_alloc (100,
17859 hash_psymtab_cu_index,
17860 eq_psymtab_cu_index,
17861 NULL, xcalloc, xfree);
96408a79 17862 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
17863 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17864 xmalloc (sizeof (struct psymtab_cu_index_map)
17865 * dwarf2_per_objfile->n_comp_units);
17866 make_cleanup (xfree, psymtab_cu_index_map);
17867
17868 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
17869 work here. Also, the debug_types entries do not appear in
17870 all_comp_units, but only in their own hash table. */
9291a0cd
TT
17871 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17872 {
3e43a32a
MS
17873 struct dwarf2_per_cu_data *per_cu
17874 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 17875 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 17876 gdb_byte val[8];
0a5429f6
DE
17877 struct psymtab_cu_index_map *map;
17878 void **slot;
9291a0cd 17879
95554aad
TT
17880 if (psymtab->user == NULL)
17881 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 17882
0a5429f6
DE
17883 map = &psymtab_cu_index_map[i];
17884 map->psymtab = psymtab;
17885 map->cu_index = i;
17886 slot = htab_find_slot (cu_index_htab, map, INSERT);
17887 gdb_assert (slot != NULL);
17888 gdb_assert (*slot == NULL);
17889 *slot = map;
9291a0cd 17890
b64f50a1
JK
17891 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17892 per_cu->offset.sect_off);
9291a0cd 17893 obstack_grow (&cu_list, val, 8);
e254ef6a 17894 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
17895 obstack_grow (&cu_list, val, 8);
17896 }
17897
0a5429f6
DE
17898 /* Dump the address map. */
17899 write_address_map (objfile, &addr_obstack, cu_index_htab);
17900
1fd400ff
TT
17901 /* Write out the .debug_type entries, if any. */
17902 if (dwarf2_per_objfile->signatured_types)
17903 {
17904 struct signatured_type_index_data sig_data;
17905
17906 sig_data.objfile = objfile;
17907 sig_data.symtab = symtab;
17908 sig_data.types_list = &types_cu_list;
987d643c 17909 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17910 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17911 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17912 write_one_signatured_type, &sig_data);
17913 }
17914
9291a0cd
TT
17915 obstack_init (&constant_pool);
17916 make_cleanup_obstack_free (&constant_pool);
17917 obstack_init (&symtab_obstack);
17918 make_cleanup_obstack_free (&symtab_obstack);
17919 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17920
17921 obstack_init (&contents);
17922 make_cleanup_obstack_free (&contents);
1fd400ff 17923 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17924 total_len = size_of_contents;
17925
17926 /* The version number. */
481860b3 17927 val = MAYBE_SWAP (6);
9291a0cd
TT
17928 obstack_grow (&contents, &val, sizeof (val));
17929
17930 /* The offset of the CU list from the start of the file. */
17931 val = MAYBE_SWAP (total_len);
17932 obstack_grow (&contents, &val, sizeof (val));
17933 total_len += obstack_object_size (&cu_list);
17934
1fd400ff
TT
17935 /* The offset of the types CU list from the start of the file. */
17936 val = MAYBE_SWAP (total_len);
17937 obstack_grow (&contents, &val, sizeof (val));
17938 total_len += obstack_object_size (&types_cu_list);
17939
9291a0cd
TT
17940 /* The offset of the address table from the start of the file. */
17941 val = MAYBE_SWAP (total_len);
17942 obstack_grow (&contents, &val, sizeof (val));
17943 total_len += obstack_object_size (&addr_obstack);
17944
17945 /* The offset of the symbol table from the start of the file. */
17946 val = MAYBE_SWAP (total_len);
17947 obstack_grow (&contents, &val, sizeof (val));
17948 total_len += obstack_object_size (&symtab_obstack);
17949
17950 /* The offset of the constant pool from the start of the file. */
17951 val = MAYBE_SWAP (total_len);
17952 obstack_grow (&contents, &val, sizeof (val));
17953 total_len += obstack_object_size (&constant_pool);
17954
17955 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17956
17957 write_obstack (out_file, &contents);
17958 write_obstack (out_file, &cu_list);
1fd400ff 17959 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17960 write_obstack (out_file, &addr_obstack);
17961 write_obstack (out_file, &symtab_obstack);
17962 write_obstack (out_file, &constant_pool);
17963
17964 fclose (out_file);
17965
17966 /* We want to keep the file, so we set cleanup_filename to NULL
17967 here. See unlink_if_set. */
17968 cleanup_filename = NULL;
17969
17970 do_cleanups (cleanup);
17971}
17972
90476074
TT
17973/* Implementation of the `save gdb-index' command.
17974
17975 Note that the file format used by this command is documented in the
17976 GDB manual. Any changes here must be documented there. */
11570e71 17977
9291a0cd
TT
17978static void
17979save_gdb_index_command (char *arg, int from_tty)
17980{
17981 struct objfile *objfile;
17982
17983 if (!arg || !*arg)
96d19272 17984 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17985
17986 ALL_OBJFILES (objfile)
17987 {
17988 struct stat st;
17989
17990 /* If the objfile does not correspond to an actual file, skip it. */
17991 if (stat (objfile->name, &st) < 0)
17992 continue;
17993
17994 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17995 if (dwarf2_per_objfile)
17996 {
17997 volatile struct gdb_exception except;
17998
17999 TRY_CATCH (except, RETURN_MASK_ERROR)
18000 {
18001 write_psymtabs_to_index (objfile, arg);
18002 }
18003 if (except.reason < 0)
18004 exception_fprintf (gdb_stderr, except,
18005 _("Error while writing index for `%s': "),
18006 objfile->name);
18007 }
18008 }
dce234bc
PP
18009}
18010
9291a0cd
TT
18011\f
18012
9eae7c52
TT
18013int dwarf2_always_disassemble;
18014
18015static void
18016show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
18017 struct cmd_list_element *c, const char *value)
18018{
3e43a32a
MS
18019 fprintf_filtered (file,
18020 _("Whether to always disassemble "
18021 "DWARF expressions is %s.\n"),
9eae7c52
TT
18022 value);
18023}
18024
900e11f9
JK
18025static void
18026show_check_physname (struct ui_file *file, int from_tty,
18027 struct cmd_list_element *c, const char *value)
18028{
18029 fprintf_filtered (file,
18030 _("Whether to check \"physname\" is %s.\n"),
18031 value);
18032}
18033
6502dd73
DJ
18034void _initialize_dwarf2_read (void);
18035
18036void
18037_initialize_dwarf2_read (void)
18038{
96d19272
JK
18039 struct cmd_list_element *c;
18040
dce234bc 18041 dwarf2_objfile_data_key
c1bd65d0 18042 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 18043
1bedd215
AC
18044 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
18045Set DWARF 2 specific variables.\n\
18046Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
18047 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
18048 0/*allow-unknown*/, &maintenance_set_cmdlist);
18049
1bedd215
AC
18050 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
18051Show DWARF 2 specific variables\n\
18052Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
18053 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
18054 0/*allow-unknown*/, &maintenance_show_cmdlist);
18055
18056 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
18057 &dwarf2_max_cache_age, _("\
18058Set the upper bound on the age of cached dwarf2 compilation units."), _("\
18059Show the upper bound on the age of cached dwarf2 compilation units."), _("\
18060A higher limit means that cached compilation units will be stored\n\
18061in memory longer, and more total memory will be used. Zero disables\n\
18062caching, which can slow down startup."),
2c5b56ce 18063 NULL,
920d2a44 18064 show_dwarf2_max_cache_age,
2c5b56ce 18065 &set_dwarf2_cmdlist,
ae038cb0 18066 &show_dwarf2_cmdlist);
d97bc12b 18067
9eae7c52
TT
18068 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
18069 &dwarf2_always_disassemble, _("\
18070Set whether `info address' always disassembles DWARF expressions."), _("\
18071Show whether `info address' always disassembles DWARF expressions."), _("\
18072When enabled, DWARF expressions are always printed in an assembly-like\n\
18073syntax. When disabled, expressions will be printed in a more\n\
18074conversational style, when possible."),
18075 NULL,
18076 show_dwarf2_always_disassemble,
18077 &set_dwarf2_cmdlist,
18078 &show_dwarf2_cmdlist);
18079
d97bc12b
DE
18080 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18081Set debugging of the dwarf2 DIE reader."), _("\
18082Show debugging of the dwarf2 DIE reader."), _("\
18083When enabled (non-zero), DIEs are dumped after they are read in.\n\
18084The value is the maximum depth to print."),
18085 NULL,
18086 NULL,
18087 &setdebuglist, &showdebuglist);
9291a0cd 18088
900e11f9
JK
18089 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18090Set cross-checking of \"physname\" code against demangler."), _("\
18091Show cross-checking of \"physname\" code against demangler."), _("\
18092When enabled, GDB's internal \"physname\" code is checked against\n\
18093the demangler."),
18094 NULL, show_check_physname,
18095 &setdebuglist, &showdebuglist);
18096
96d19272 18097 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 18098 _("\
fc1a9d6e 18099Save a gdb-index file.\n\
11570e71 18100Usage: save gdb-index DIRECTORY"),
96d19272
JK
18101 &save_cmdlist);
18102 set_cmd_completer (c, filename_completer);
6502dd73 18103}
This page took 2.548471 seconds and 4 git commands to generate.