gdb/testsuite/
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
0b302171 3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c
SS
31#include "defs.h"
32#include "bfd.h"
c906108c
SS
33#include "symtab.h"
34#include "gdbtypes.h"
c906108c 35#include "objfiles.h"
fa8f86ff 36#include "dwarf2.h"
c906108c
SS
37#include "buildsym.h"
38#include "demangle.h"
50f182aa 39#include "gdb-demangle.h"
c906108c 40#include "expression.h"
d5166ae1 41#include "filenames.h" /* for DOSish file names */
2e276125 42#include "macrotab.h"
c906108c
SS
43#include "language.h"
44#include "complaints.h"
357e46e7 45#include "bcache.h"
4c2df51b
DJ
46#include "dwarf2expr.h"
47#include "dwarf2loc.h"
9219021c 48#include "cp-support.h"
72bf9492 49#include "hashtab.h"
ae038cb0
DJ
50#include "command.h"
51#include "gdbcmd.h"
edb3359d 52#include "block.h"
ff013f42 53#include "addrmap.h"
94af9270
KS
54#include "typeprint.h"
55#include "jv-lang.h"
ccefe4c4 56#include "psympriv.h"
9291a0cd
TT
57#include "exceptions.h"
58#include "gdb_stat.h"
96d19272 59#include "completer.h"
34eaf542 60#include "vec.h"
98bfdba5 61#include "c-lang.h"
a766d390 62#include "go-lang.h"
98bfdba5 63#include "valprint.h"
3019eac3 64#include "gdbcore.h" /* for gnutarget */
60d5a603 65#include <ctype.h>
4c2df51b 66
c906108c
SS
67#include <fcntl.h>
68#include "gdb_string.h"
4bdf3d34 69#include "gdb_assert.h"
c906108c 70#include <sys/types.h>
233a11ab
CS
71#ifdef HAVE_ZLIB_H
72#include <zlib.h>
73#endif
dce234bc
PP
74#ifdef HAVE_MMAP
75#include <sys/mman.h>
85d9bd0e
TT
76#ifndef MAP_FAILED
77#define MAP_FAILED ((void *) -1)
78#endif
dce234bc 79#endif
d8151005 80
34eaf542
TT
81typedef struct symbol *symbolp;
82DEF_VEC_P (symbolp);
83
d97bc12b
DE
84/* When non-zero, dump DIEs after they are read in. */
85static int dwarf2_die_debug = 0;
86
900e11f9
JK
87/* When non-zero, cross-check physname against demangler. */
88static int check_physname = 0;
89
481860b3
GB
90/* When non-zero, do not reject deprecated .gdb_index sections. */
91int use_deprecated_index_sections = 0;
92
dce234bc
PP
93static int pagesize;
94
df8a16a1
DJ
95/* When set, the file that we're processing is known to have debugging
96 info for C++ namespaces. GCC 3.3.x did not produce this information,
97 but later versions do. */
98
99static int processing_has_namespace_info;
100
6502dd73
DJ
101static const struct objfile_data *dwarf2_objfile_data_key;
102
dce234bc
PP
103struct dwarf2_section_info
104{
105 asection *asection;
106 gdb_byte *buffer;
107 bfd_size_type size;
b315ab21
TG
108 /* Not NULL if the section was actually mmapped. */
109 void *map_addr;
110 /* Page aligned size of mmapped area. */
111 bfd_size_type map_len;
be391dca
TT
112 /* True if we have tried to read this section. */
113 int readin;
dce234bc
PP
114};
115
8b70b953
TT
116typedef struct dwarf2_section_info dwarf2_section_info_def;
117DEF_VEC_O (dwarf2_section_info_def);
118
9291a0cd
TT
119/* All offsets in the index are of this type. It must be
120 architecture-independent. */
121typedef uint32_t offset_type;
122
123DEF_VEC_I (offset_type);
124
125/* A description of the mapped index. The file format is described in
126 a comment by the code that writes the index. */
127struct mapped_index
128{
559a7a62
JK
129 /* Index data format version. */
130 int version;
131
9291a0cd
TT
132 /* The total length of the buffer. */
133 off_t total_size;
b11b1f88 134
9291a0cd
TT
135 /* A pointer to the address table data. */
136 const gdb_byte *address_table;
b11b1f88 137
9291a0cd
TT
138 /* Size of the address table data in bytes. */
139 offset_type address_table_size;
b11b1f88 140
3876f04e
DE
141 /* The symbol table, implemented as a hash table. */
142 const offset_type *symbol_table;
b11b1f88 143
9291a0cd 144 /* Size in slots, each slot is 2 offset_types. */
3876f04e 145 offset_type symbol_table_slots;
b11b1f88 146
9291a0cd
TT
147 /* A pointer to the constant pool. */
148 const char *constant_pool;
149};
150
95554aad
TT
151typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
152DEF_VEC_P (dwarf2_per_cu_ptr);
153
9cdd5dbd
DE
154/* Collection of data recorded per objfile.
155 This hangs off of dwarf2_objfile_data_key. */
156
6502dd73
DJ
157struct dwarf2_per_objfile
158{
dce234bc
PP
159 struct dwarf2_section_info info;
160 struct dwarf2_section_info abbrev;
161 struct dwarf2_section_info line;
dce234bc
PP
162 struct dwarf2_section_info loc;
163 struct dwarf2_section_info macinfo;
cf2c3c16 164 struct dwarf2_section_info macro;
dce234bc
PP
165 struct dwarf2_section_info str;
166 struct dwarf2_section_info ranges;
3019eac3 167 struct dwarf2_section_info addr;
dce234bc
PP
168 struct dwarf2_section_info frame;
169 struct dwarf2_section_info eh_frame;
9291a0cd 170 struct dwarf2_section_info gdb_index;
ae038cb0 171
8b70b953
TT
172 VEC (dwarf2_section_info_def) *types;
173
be391dca
TT
174 /* Back link. */
175 struct objfile *objfile;
176
d467dd73 177 /* Table of all the compilation units. This is used to locate
10b3939b 178 the target compilation unit of a particular reference. */
ae038cb0
DJ
179 struct dwarf2_per_cu_data **all_comp_units;
180
181 /* The number of compilation units in ALL_COMP_UNITS. */
182 int n_comp_units;
183
1fd400ff 184 /* The number of .debug_types-related CUs. */
d467dd73 185 int n_type_units;
1fd400ff 186
d467dd73
DE
187 /* The .debug_types-related CUs (TUs). */
188 struct dwarf2_per_cu_data **all_type_units;
1fd400ff 189
ae038cb0
DJ
190 /* A chain of compilation units that are currently read in, so that
191 they can be freed later. */
192 struct dwarf2_per_cu_data *read_in_chain;
72dca2f5 193
348e048f
DE
194 /* A table mapping .debug_types signatures to its signatured_type entry.
195 This is NULL if the .debug_types section hasn't been read in yet. */
196 htab_t signatured_types;
197
3019eac3
DE
198 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
199 This is NULL if the table hasn't been allocated yet. */
200 htab_t dwo_files;
201
72dca2f5
FR
202 /* A flag indicating wether this objfile has a section loaded at a
203 VMA of 0. */
204 int has_section_at_zero;
9291a0cd 205
ae2de4f8
DE
206 /* True if we are using the mapped index,
207 or we are faking it for OBJF_READNOW's sake. */
9291a0cd
TT
208 unsigned char using_index;
209
ae2de4f8 210 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
9291a0cd 211 struct mapped_index *index_table;
98bfdba5 212
7b9f3c50
DE
213 /* When using index_table, this keeps track of all quick_file_names entries.
214 TUs can share line table entries with CUs or other TUs, and there can be
215 a lot more TUs than unique line tables, so we maintain a separate table
216 of all line table entries to support the sharing. */
217 htab_t quick_file_names_table;
218
98bfdba5
PA
219 /* Set during partial symbol reading, to prevent queueing of full
220 symbols. */
221 int reading_partial_symbols;
673bfd45 222
dee91e82 223 /* Table mapping type DIEs to their struct type *.
673bfd45 224 This is NULL if not allocated yet.
dee91e82
DE
225 The mapping is done via (CU/TU signature + DIE offset) -> type. */
226 htab_t die_type_hash;
95554aad
TT
227
228 /* The CUs we recently read. */
229 VEC (dwarf2_per_cu_ptr) *just_read_cus;
6502dd73
DJ
230};
231
232static struct dwarf2_per_objfile *dwarf2_per_objfile;
c906108c 233
251d32d9 234/* Default names of the debugging sections. */
c906108c 235
233a11ab
CS
236/* Note that if the debugging section has been compressed, it might
237 have a name like .zdebug_info. */
238
9cdd5dbd
DE
239static const struct dwarf2_debug_sections dwarf2_elf_names =
240{
251d32d9
TG
241 { ".debug_info", ".zdebug_info" },
242 { ".debug_abbrev", ".zdebug_abbrev" },
243 { ".debug_line", ".zdebug_line" },
244 { ".debug_loc", ".zdebug_loc" },
245 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 246 { ".debug_macro", ".zdebug_macro" },
251d32d9
TG
247 { ".debug_str", ".zdebug_str" },
248 { ".debug_ranges", ".zdebug_ranges" },
249 { ".debug_types", ".zdebug_types" },
3019eac3 250 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
251 { ".debug_frame", ".zdebug_frame" },
252 { ".eh_frame", NULL },
24d3216f
TT
253 { ".gdb_index", ".zgdb_index" },
254 23
251d32d9 255};
c906108c 256
3019eac3
DE
257/* List of DWO sections. */
258
259static const struct dwo_section_names
260{
261 struct dwarf2_section_names abbrev_dwo;
262 struct dwarf2_section_names info_dwo;
263 struct dwarf2_section_names line_dwo;
264 struct dwarf2_section_names loc_dwo;
265 struct dwarf2_section_names str_dwo;
266 struct dwarf2_section_names str_offsets_dwo;
267 struct dwarf2_section_names types_dwo;
268}
269dwo_section_names =
270{
271 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
272 { ".debug_info.dwo", ".zdebug_info.dwo" },
273 { ".debug_line.dwo", ".zdebug_line.dwo" },
274 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
275 { ".debug_str.dwo", ".zdebug_str.dwo" },
276 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
277 { ".debug_types.dwo", ".zdebug_types.dwo" },
278};
279
c906108c
SS
280/* local data types */
281
0963b4bd 282/* We hold several abbreviation tables in memory at the same time. */
57349743
JB
283#ifndef ABBREV_HASH_SIZE
284#define ABBREV_HASH_SIZE 121
285#endif
286
107d2387
AC
287/* The data in a compilation unit header, after target2host
288 translation, looks like this. */
c906108c 289struct comp_unit_head
a738430d 290{
c764a876 291 unsigned int length;
a738430d 292 short version;
a738430d
MK
293 unsigned char addr_size;
294 unsigned char signed_addr_p;
b64f50a1 295 sect_offset abbrev_offset;
57349743 296
a738430d
MK
297 /* Size of file offsets; either 4 or 8. */
298 unsigned int offset_size;
57349743 299
a738430d
MK
300 /* Size of the length field; either 4 or 12. */
301 unsigned int initial_length_size;
57349743 302
a738430d
MK
303 /* Offset to the first byte of this compilation unit header in the
304 .debug_info section, for resolving relative reference dies. */
b64f50a1 305 sect_offset offset;
57349743 306
d00adf39
DE
307 /* Offset to first die in this cu from the start of the cu.
308 This will be the first byte following the compilation unit header. */
b64f50a1 309 cu_offset first_die_offset;
a738430d 310};
c906108c 311
3da10d80
KS
312/* Type used for delaying computation of method physnames.
313 See comments for compute_delayed_physnames. */
314struct delayed_method_info
315{
316 /* The type to which the method is attached, i.e., its parent class. */
317 struct type *type;
318
319 /* The index of the method in the type's function fieldlists. */
320 int fnfield_index;
321
322 /* The index of the method in the fieldlist. */
323 int index;
324
325 /* The name of the DIE. */
326 const char *name;
327
328 /* The DIE associated with this method. */
329 struct die_info *die;
330};
331
332typedef struct delayed_method_info delayed_method_info;
333DEF_VEC_O (delayed_method_info);
334
e7c27a73
DJ
335/* Internal state when decoding a particular compilation unit. */
336struct dwarf2_cu
337{
338 /* The objfile containing this compilation unit. */
339 struct objfile *objfile;
340
d00adf39 341 /* The header of the compilation unit. */
e7c27a73 342 struct comp_unit_head header;
e142c38c 343
d00adf39
DE
344 /* Base address of this compilation unit. */
345 CORE_ADDR base_address;
346
347 /* Non-zero if base_address has been set. */
348 int base_known;
349
e142c38c
DJ
350 /* The language we are debugging. */
351 enum language language;
352 const struct language_defn *language_defn;
353
b0f35d58
DL
354 const char *producer;
355
e142c38c
DJ
356 /* The generic symbol table building routines have separate lists for
357 file scope symbols and all all other scopes (local scopes). So
358 we need to select the right one to pass to add_symbol_to_list().
359 We do it by keeping a pointer to the correct list in list_in_scope.
360
361 FIXME: The original dwarf code just treated the file scope as the
362 first local scope, and all other local scopes as nested local
363 scopes, and worked fine. Check to see if we really need to
364 distinguish these in buildsym.c. */
365 struct pending **list_in_scope;
366
f3dd6933
DJ
367 /* DWARF abbreviation table associated with this compilation unit. */
368 struct abbrev_info **dwarf2_abbrevs;
369
370 /* Storage for the abbrev table. */
371 struct obstack abbrev_obstack;
72bf9492 372
b64f50a1
JK
373 /* Hash table holding all the loaded partial DIEs
374 with partial_die->offset.SECT_OFF as hash. */
72bf9492
DJ
375 htab_t partial_dies;
376
377 /* Storage for things with the same lifetime as this read-in compilation
378 unit, including partial DIEs. */
379 struct obstack comp_unit_obstack;
380
ae038cb0
DJ
381 /* When multiple dwarf2_cu structures are living in memory, this field
382 chains them all together, so that they can be released efficiently.
383 We will probably also want a generation counter so that most-recently-used
384 compilation units are cached... */
385 struct dwarf2_per_cu_data *read_in_chain;
386
387 /* Backchain to our per_cu entry if the tree has been built. */
388 struct dwarf2_per_cu_data *per_cu;
389
390 /* How many compilation units ago was this CU last referenced? */
391 int last_used;
392
b64f50a1
JK
393 /* A hash table of DIE cu_offset for following references with
394 die_info->offset.sect_off as hash. */
51545339 395 htab_t die_hash;
10b3939b
DJ
396
397 /* Full DIEs if read in. */
398 struct die_info *dies;
399
400 /* A set of pointers to dwarf2_per_cu_data objects for compilation
401 units referenced by this one. Only set during full symbol processing;
402 partial symbol tables do not have dependencies. */
403 htab_t dependencies;
404
cb1df416
DJ
405 /* Header data from the line table, during full symbol processing. */
406 struct line_header *line_header;
407
3da10d80
KS
408 /* A list of methods which need to have physnames computed
409 after all type information has been read. */
410 VEC (delayed_method_info) *method_list;
411
96408a79
SA
412 /* To be copied to symtab->call_site_htab. */
413 htab_t call_site_htab;
414
034e5797
DE
415 /* Non-NULL if this CU came from a DWO file.
416 There is an invariant here that is important to remember:
417 Except for attributes copied from the top level DIE in the "main"
418 (or "stub") file in preparation for reading the DWO file
419 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
420 Either there isn't a DWO file (in which case this is NULL and the point
421 is moot), or there is and either we're not going to read it (in which
422 case this is NULL) or there is and we are reading it (in which case this
423 is non-NULL). */
3019eac3
DE
424 struct dwo_unit *dwo_unit;
425
426 /* The DW_AT_addr_base attribute if present, zero otherwise
427 (zero is a valid value though).
428 Note this value comes from the stub CU/TU's DIE. */
429 ULONGEST addr_base;
430
ae038cb0
DJ
431 /* Mark used when releasing cached dies. */
432 unsigned int mark : 1;
433
8be455d7
JK
434 /* This CU references .debug_loc. See the symtab->locations_valid field.
435 This test is imperfect as there may exist optimized debug code not using
436 any location list and still facing inlining issues if handled as
437 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 438 unsigned int has_loclist : 1;
ba919b58 439
685b1105
JK
440 /* These cache the results for producer_is_gxx_lt_4_6 and producer_is_icc.
441 CHECKED_PRODUCER is set if both PRODUCER_IS_GXX_LT_4_6 and PRODUCER_IS_ICC
442 are valid. This information is cached because profiling CU expansion
443 showed excessive time spent in producer_is_gxx_lt_4_6. */
ba919b58
TT
444 unsigned int checked_producer : 1;
445 unsigned int producer_is_gxx_lt_4_6 : 1;
685b1105 446 unsigned int producer_is_icc : 1;
3019eac3
DE
447
448 /* Non-zero if DW_AT_addr_base was found.
449 Used when processing DWO files. */
450 unsigned int have_addr_base : 1;
e7c27a73
DJ
451};
452
10b3939b
DJ
453/* Persistent data held for a compilation unit, even when not
454 processing it. We put a pointer to this structure in the
28dee7f5 455 read_symtab_private field of the psymtab. */
10b3939b 456
ae038cb0
DJ
457struct dwarf2_per_cu_data
458{
348e048f 459 /* The start offset and length of this compilation unit. 2**29-1
ae038cb0 460 bytes should suffice to store the length of any compilation unit
45452591
DE
461 - if it doesn't, GDB will fall over anyway.
462 NOTE: Unlike comp_unit_head.length, this length includes
3019eac3
DE
463 initial_length_size.
464 If the DIE refers to a DWO file, this is always of the original die,
465 not the DWO file. */
b64f50a1 466 sect_offset offset;
348e048f 467 unsigned int length : 29;
ae038cb0
DJ
468
469 /* Flag indicating this compilation unit will be read in before
470 any of the current compilation units are processed. */
c764a876 471 unsigned int queued : 1;
ae038cb0 472
0d99eb77
DE
473 /* This flag will be set when reading partial DIEs if we need to load
474 absolutely all DIEs for this compilation unit, instead of just the ones
475 we think are interesting. It gets set if we look for a DIE in the
5afb4e99
DJ
476 hash table and don't find it. */
477 unsigned int load_all_dies : 1;
478
3019eac3
DE
479 /* Non-zero if this CU is from .debug_types. */
480 unsigned int is_debug_types : 1;
481
482 /* The section this CU/TU lives in.
483 If the DIE refers to a DWO file, this is always the original die,
484 not the DWO file. */
485 struct dwarf2_section_info *info_or_types_section;
348e048f 486
17ea53c3
JK
487 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
488 of the CU cache it gets reset to NULL again. */
ae038cb0 489 struct dwarf2_cu *cu;
1c379e20 490
9cdd5dbd
DE
491 /* The corresponding objfile.
492 Normally we can get the objfile from dwarf2_per_objfile.
493 However we can enter this file with just a "per_cu" handle. */
9291a0cd
TT
494 struct objfile *objfile;
495
496 /* When using partial symbol tables, the 'psymtab' field is active.
497 Otherwise the 'quick' field is active. */
498 union
499 {
500 /* The partial symbol table associated with this compilation unit,
95554aad 501 or NULL for unread partial units. */
9291a0cd
TT
502 struct partial_symtab *psymtab;
503
504 /* Data needed by the "quick" functions. */
505 struct dwarf2_per_cu_quick_data *quick;
506 } v;
95554aad
TT
507
508 /* The CUs we import using DW_TAG_imported_unit. This is filled in
509 while reading psymtabs, used to compute the psymtab dependencies,
510 and then cleared. Then it is filled in again while reading full
511 symbols, and only deleted when the objfile is destroyed. */
512 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
ae038cb0
DJ
513};
514
348e048f
DE
515/* Entry in the signatured_types hash table. */
516
517struct signatured_type
518{
3019eac3 519 /* The type's signature. */
348e048f
DE
520 ULONGEST signature;
521
3019eac3
DE
522 /* Offset in the TU of the type's DIE, as read from the TU header.
523 If the definition lives in a DWO file, this value is unusable. */
524 cu_offset type_offset_in_tu;
525
526 /* Offset in the section of the type's DIE.
527 If the definition lives in a DWO file, this is the offset in the
528 .debug_types.dwo section.
529 The value is zero until the actual value is known.
530 Zero is otherwise not a valid section offset. */
531 sect_offset type_offset_in_section;
348e048f
DE
532
533 /* The CU(/TU) of this type. */
534 struct dwarf2_per_cu_data per_cu;
535};
536
3019eac3
DE
537/* These sections are what may appear in a "dwo" file. */
538
539struct dwo_sections
540{
541 struct dwarf2_section_info abbrev;
542 struct dwarf2_section_info info;
543 struct dwarf2_section_info line;
544 struct dwarf2_section_info loc;
545 struct dwarf2_section_info str;
546 struct dwarf2_section_info str_offsets;
547 VEC (dwarf2_section_info_def) *types;
548};
549
550/* Common bits of DWO CUs/TUs. */
551
552struct dwo_unit
553{
554 /* Backlink to the containing struct dwo_file. */
555 struct dwo_file *dwo_file;
556
557 /* The "id" that distinguishes this CU/TU.
558 .debug_info calls this "dwo_id", .debug_types calls this "signature".
559 Since signatures came first, we stick with it for consistency. */
560 ULONGEST signature;
561
562 /* The section this CU/TU lives in, in the DWO file. */
563 struct dwarf2_section_info *info_or_types_section;
564
565 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
566 sect_offset offset;
567 unsigned int length;
568
569 /* For types, offset in the type's DIE of the type defined by this TU. */
570 cu_offset type_offset_in_tu;
571};
572
573/* Data for one DWO file. */
574
575struct dwo_file
576{
577 /* The DW_AT_GNU_dwo_name attribute.
578 We don't manage space for this, it's an attribute. */
579 const char *dwo_name;
580
581 /* The bfd, when the file is open. Otherwise this is NULL. */
582 bfd *dwo_bfd;
583
584 /* Section info for this file. */
585 struct dwo_sections sections;
586
587 /* Table of CUs in the file.
588 Each element is a struct dwo_unit. */
589 htab_t cus;
590
591 /* Table of TUs in the file.
592 Each element is a struct dwo_unit. */
593 htab_t tus;
594};
595
0963b4bd
MS
596/* Struct used to pass misc. parameters to read_die_and_children, et
597 al. which are used for both .debug_info and .debug_types dies.
598 All parameters here are unchanging for the life of the call. This
dee91e82 599 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
600
601struct die_reader_specs
602{
dee91e82 603 /* die_section->asection->owner. */
93311388
DE
604 bfd* abfd;
605
606 /* The CU of the DIE we are parsing. */
607 struct dwarf2_cu *cu;
608
3019eac3
DE
609 /* Non-NULL if reading a DWO file. */
610 struct dwo_file *dwo_file;
611
dee91e82 612 /* The section the die comes from.
3019eac3 613 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
614 struct dwarf2_section_info *die_section;
615
616 /* die_section->buffer. */
617 gdb_byte *buffer;
f664829e
DE
618
619 /* The end of the buffer. */
620 const gdb_byte *buffer_end;
93311388
DE
621};
622
fd820528 623/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82
DE
624typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
625 gdb_byte *info_ptr,
626 struct die_info *comp_unit_die,
627 int has_children,
628 void *data);
629
debd256d
JB
630/* The line number information for a compilation unit (found in the
631 .debug_line section) begins with a "statement program header",
632 which contains the following information. */
633struct line_header
634{
635 unsigned int total_length;
636 unsigned short version;
637 unsigned int header_length;
638 unsigned char minimum_instruction_length;
2dc7f7b3 639 unsigned char maximum_ops_per_instruction;
debd256d
JB
640 unsigned char default_is_stmt;
641 int line_base;
642 unsigned char line_range;
643 unsigned char opcode_base;
644
645 /* standard_opcode_lengths[i] is the number of operands for the
646 standard opcode whose value is i. This means that
647 standard_opcode_lengths[0] is unused, and the last meaningful
648 element is standard_opcode_lengths[opcode_base - 1]. */
649 unsigned char *standard_opcode_lengths;
650
651 /* The include_directories table. NOTE! These strings are not
652 allocated with xmalloc; instead, they are pointers into
653 debug_line_buffer. If you try to free them, `free' will get
654 indigestion. */
655 unsigned int num_include_dirs, include_dirs_size;
656 char **include_dirs;
657
658 /* The file_names table. NOTE! These strings are not allocated
659 with xmalloc; instead, they are pointers into debug_line_buffer.
660 Don't try to free them directly. */
661 unsigned int num_file_names, file_names_size;
662 struct file_entry
c906108c 663 {
debd256d
JB
664 char *name;
665 unsigned int dir_index;
666 unsigned int mod_time;
667 unsigned int length;
aaa75496 668 int included_p; /* Non-zero if referenced by the Line Number Program. */
cb1df416 669 struct symtab *symtab; /* The associated symbol table, if any. */
debd256d
JB
670 } *file_names;
671
672 /* The start and end of the statement program following this
6502dd73 673 header. These point into dwarf2_per_objfile->line_buffer. */
fe1b8b76 674 gdb_byte *statement_program_start, *statement_program_end;
debd256d 675};
c906108c
SS
676
677/* When we construct a partial symbol table entry we only
0963b4bd 678 need this much information. */
c906108c
SS
679struct partial_die_info
680 {
72bf9492 681 /* Offset of this DIE. */
b64f50a1 682 sect_offset offset;
72bf9492
DJ
683
684 /* DWARF-2 tag for this DIE. */
685 ENUM_BITFIELD(dwarf_tag) tag : 16;
686
72bf9492
DJ
687 /* Assorted flags describing the data found in this DIE. */
688 unsigned int has_children : 1;
689 unsigned int is_external : 1;
690 unsigned int is_declaration : 1;
691 unsigned int has_type : 1;
692 unsigned int has_specification : 1;
693 unsigned int has_pc_info : 1;
481860b3 694 unsigned int may_be_inlined : 1;
72bf9492
DJ
695
696 /* Flag set if the SCOPE field of this structure has been
697 computed. */
698 unsigned int scope_set : 1;
699
fa4028e9
JB
700 /* Flag set if the DIE has a byte_size attribute. */
701 unsigned int has_byte_size : 1;
702
98bfdba5
PA
703 /* Flag set if any of the DIE's children are template arguments. */
704 unsigned int has_template_arguments : 1;
705
abc72ce4
DE
706 /* Flag set if fixup_partial_die has been called on this die. */
707 unsigned int fixup_called : 1;
708
72bf9492 709 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 710 sometimes a default name for unnamed DIEs. */
c906108c 711 char *name;
72bf9492 712
abc72ce4
DE
713 /* The linkage name, if present. */
714 const char *linkage_name;
715
72bf9492
DJ
716 /* The scope to prepend to our children. This is generally
717 allocated on the comp_unit_obstack, so will disappear
718 when this compilation unit leaves the cache. */
719 char *scope;
720
95554aad
TT
721 /* Some data associated with the partial DIE. The tag determines
722 which field is live. */
723 union
724 {
725 /* The location description associated with this DIE, if any. */
726 struct dwarf_block *locdesc;
727 /* The offset of an import, for DW_TAG_imported_unit. */
728 sect_offset offset;
729 } d;
72bf9492
DJ
730
731 /* If HAS_PC_INFO, the PC range associated with this DIE. */
c906108c
SS
732 CORE_ADDR lowpc;
733 CORE_ADDR highpc;
72bf9492 734
93311388 735 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 736 DW_AT_sibling, if any. */
abc72ce4
DE
737 /* NOTE: This member isn't strictly necessary, read_partial_die could
738 return DW_AT_sibling values to its caller load_partial_dies. */
fe1b8b76 739 gdb_byte *sibling;
72bf9492
DJ
740
741 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
742 DW_AT_specification (or DW_AT_abstract_origin or
743 DW_AT_extension). */
b64f50a1 744 sect_offset spec_offset;
72bf9492
DJ
745
746 /* Pointers to this DIE's parent, first child, and next sibling,
747 if any. */
748 struct partial_die_info *die_parent, *die_child, *die_sibling;
c906108c
SS
749 };
750
0963b4bd 751/* This data structure holds the information of an abbrev. */
c906108c
SS
752struct abbrev_info
753 {
754 unsigned int number; /* number identifying abbrev */
755 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
756 unsigned short has_children; /* boolean */
757 unsigned short num_attrs; /* number of attributes */
c906108c
SS
758 struct attr_abbrev *attrs; /* an array of attribute descriptions */
759 struct abbrev_info *next; /* next in chain */
760 };
761
762struct attr_abbrev
763 {
9d25dd43
DE
764 ENUM_BITFIELD(dwarf_attribute) name : 16;
765 ENUM_BITFIELD(dwarf_form) form : 16;
c906108c
SS
766 };
767
0963b4bd 768/* Attributes have a name and a value. */
b60c80d6
DJ
769struct attribute
770 {
9d25dd43 771 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
772 ENUM_BITFIELD(dwarf_form) form : 15;
773
774 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
775 field should be in u.str (existing only for DW_STRING) but it is kept
776 here for better struct attribute alignment. */
777 unsigned int string_is_canonical : 1;
778
b60c80d6
DJ
779 union
780 {
781 char *str;
782 struct dwarf_block *blk;
43bbcdc2
PH
783 ULONGEST unsnd;
784 LONGEST snd;
b60c80d6 785 CORE_ADDR addr;
348e048f 786 struct signatured_type *signatured_type;
b60c80d6
DJ
787 }
788 u;
789 };
790
0963b4bd 791/* This data structure holds a complete die structure. */
c906108c
SS
792struct die_info
793 {
76815b17
DE
794 /* DWARF-2 tag for this DIE. */
795 ENUM_BITFIELD(dwarf_tag) tag : 16;
796
797 /* Number of attributes */
98bfdba5
PA
798 unsigned char num_attrs;
799
800 /* True if we're presently building the full type name for the
801 type derived from this DIE. */
802 unsigned char building_fullname : 1;
76815b17
DE
803
804 /* Abbrev number */
805 unsigned int abbrev;
806
93311388 807 /* Offset in .debug_info or .debug_types section. */
b64f50a1 808 sect_offset offset;
78ba4af6
JB
809
810 /* The dies in a compilation unit form an n-ary tree. PARENT
811 points to this die's parent; CHILD points to the first child of
812 this node; and all the children of a given node are chained
4950bc1c 813 together via their SIBLING fields. */
639d11d3
DC
814 struct die_info *child; /* Its first child, if any. */
815 struct die_info *sibling; /* Its next sibling, if any. */
816 struct die_info *parent; /* Its parent, if any. */
c906108c 817
b60c80d6
DJ
818 /* An array of attributes, with NUM_ATTRS elements. There may be
819 zero, but it's not common and zero-sized arrays are not
820 sufficiently portable C. */
821 struct attribute attrs[1];
c906108c
SS
822 };
823
0963b4bd 824/* Get at parts of an attribute structure. */
c906108c
SS
825
826#define DW_STRING(attr) ((attr)->u.str)
8285870a 827#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
828#define DW_UNSND(attr) ((attr)->u.unsnd)
829#define DW_BLOCK(attr) ((attr)->u.blk)
830#define DW_SND(attr) ((attr)->u.snd)
831#define DW_ADDR(attr) ((attr)->u.addr)
348e048f 832#define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
c906108c 833
0963b4bd 834/* Blocks are a bunch of untyped bytes. */
c906108c
SS
835struct dwarf_block
836 {
837 unsigned int size;
1d6edc3c
JK
838
839 /* Valid only if SIZE is not zero. */
fe1b8b76 840 gdb_byte *data;
c906108c
SS
841 };
842
c906108c
SS
843#ifndef ATTR_ALLOC_CHUNK
844#define ATTR_ALLOC_CHUNK 4
845#endif
846
c906108c
SS
847/* Allocate fields for structs, unions and enums in this size. */
848#ifndef DW_FIELD_ALLOC_CHUNK
849#define DW_FIELD_ALLOC_CHUNK 4
850#endif
851
c906108c
SS
852/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
853 but this would require a corresponding change in unpack_field_as_long
854 and friends. */
855static int bits_per_byte = 8;
856
857/* The routines that read and process dies for a C struct or C++ class
858 pass lists of data member fields and lists of member function fields
859 in an instance of a field_info structure, as defined below. */
860struct field_info
c5aa993b 861 {
0963b4bd 862 /* List of data member and baseclasses fields. */
c5aa993b
JM
863 struct nextfield
864 {
865 struct nextfield *next;
866 int accessibility;
867 int virtuality;
868 struct field field;
869 }
7d0ccb61 870 *fields, *baseclasses;
c906108c 871
7d0ccb61 872 /* Number of fields (including baseclasses). */
c5aa993b 873 int nfields;
c906108c 874
c5aa993b
JM
875 /* Number of baseclasses. */
876 int nbaseclasses;
c906108c 877
c5aa993b
JM
878 /* Set if the accesibility of one of the fields is not public. */
879 int non_public_fields;
c906108c 880
c5aa993b
JM
881 /* Member function fields array, entries are allocated in the order they
882 are encountered in the object file. */
883 struct nextfnfield
884 {
885 struct nextfnfield *next;
886 struct fn_field fnfield;
887 }
888 *fnfields;
c906108c 889
c5aa993b
JM
890 /* Member function fieldlist array, contains name of possibly overloaded
891 member function, number of overloaded member functions and a pointer
892 to the head of the member function field chain. */
893 struct fnfieldlist
894 {
895 char *name;
896 int length;
897 struct nextfnfield *head;
898 }
899 *fnfieldlists;
c906108c 900
c5aa993b
JM
901 /* Number of entries in the fnfieldlists array. */
902 int nfnfields;
98751a41
JK
903
904 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
905 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
906 struct typedef_field_list
907 {
908 struct typedef_field field;
909 struct typedef_field_list *next;
910 }
911 *typedef_field_list;
912 unsigned typedef_field_list_count;
c5aa993b 913 };
c906108c 914
10b3939b
DJ
915/* One item on the queue of compilation units to read in full symbols
916 for. */
917struct dwarf2_queue_item
918{
919 struct dwarf2_per_cu_data *per_cu;
95554aad 920 enum language pretend_language;
10b3939b
DJ
921 struct dwarf2_queue_item *next;
922};
923
924/* The current queue. */
925static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
926
ae038cb0
DJ
927/* Loaded secondary compilation units are kept in memory until they
928 have not been referenced for the processing of this many
929 compilation units. Set this to zero to disable caching. Cache
930 sizes of up to at least twenty will improve startup time for
931 typical inter-CU-reference binaries, at an obvious memory cost. */
932static int dwarf2_max_cache_age = 5;
920d2a44
AC
933static void
934show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
935 struct cmd_list_element *c, const char *value)
936{
3e43a32a
MS
937 fprintf_filtered (file, _("The upper bound on the age of cached "
938 "dwarf2 compilation units is %s.\n"),
920d2a44
AC
939 value);
940}
941
ae038cb0 942
0963b4bd 943/* Various complaints about symbol reading that don't abort the process. */
c906108c 944
4d3c2250
KB
945static void
946dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2e276125 947{
4d3c2250 948 complaint (&symfile_complaints,
e2e0b3e5 949 _("statement list doesn't fit in .debug_line section"));
4d3c2250
KB
950}
951
25e43795
DJ
952static void
953dwarf2_debug_line_missing_file_complaint (void)
954{
955 complaint (&symfile_complaints,
956 _(".debug_line section has line data without a file"));
957}
958
59205f5a
JB
959static void
960dwarf2_debug_line_missing_end_sequence_complaint (void)
961{
962 complaint (&symfile_complaints,
3e43a32a
MS
963 _(".debug_line section has line "
964 "program sequence without an end"));
59205f5a
JB
965}
966
4d3c2250
KB
967static void
968dwarf2_complex_location_expr_complaint (void)
2e276125 969{
e2e0b3e5 970 complaint (&symfile_complaints, _("location expression too complex"));
4d3c2250
KB
971}
972
4d3c2250
KB
973static void
974dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
975 int arg3)
2e276125 976{
4d3c2250 977 complaint (&symfile_complaints,
3e43a32a
MS
978 _("const value length mismatch for '%s', got %d, expected %d"),
979 arg1, arg2, arg3);
4d3c2250
KB
980}
981
982static void
f664829e 983dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2e276125 984{
4d3c2250 985 complaint (&symfile_complaints,
f664829e
DE
986 _("debug info runs off end of %s section"
987 " [in module %s]"),
988 section->asection->name,
989 bfd_get_filename (section->asection->owner));
4d3c2250
KB
990}
991
992static void
993dwarf2_macro_malformed_definition_complaint (const char *arg1)
8e19ed76 994{
4d3c2250 995 complaint (&symfile_complaints,
3e43a32a
MS
996 _("macro debug info contains a "
997 "malformed macro definition:\n`%s'"),
4d3c2250
KB
998 arg1);
999}
1000
1001static void
1002dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
8b2dbe47 1003{
4d3c2250 1004 complaint (&symfile_complaints,
3e43a32a
MS
1005 _("invalid attribute class or form for '%s' in '%s'"),
1006 arg1, arg2);
4d3c2250 1007}
c906108c 1008
c906108c
SS
1009/* local function prototypes */
1010
4efb68b1 1011static void dwarf2_locate_sections (bfd *, asection *, void *);
c906108c 1012
aaa75496
JB
1013static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
1014 struct objfile *);
1015
918dd910
JK
1016static void dwarf2_find_base_address (struct die_info *die,
1017 struct dwarf2_cu *cu);
1018
c67a9c90 1019static void dwarf2_build_psymtabs_hard (struct objfile *);
c906108c 1020
72bf9492
DJ
1021static void scan_partial_symbols (struct partial_die_info *,
1022 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1023 int, struct dwarf2_cu *);
c906108c 1024
72bf9492
DJ
1025static void add_partial_symbol (struct partial_die_info *,
1026 struct dwarf2_cu *);
63d06c5c 1027
72bf9492
DJ
1028static void add_partial_namespace (struct partial_die_info *pdi,
1029 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1030 int need_pc, struct dwarf2_cu *cu);
63d06c5c 1031
5d7cb8df
JK
1032static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1033 CORE_ADDR *highpc, int need_pc,
1034 struct dwarf2_cu *cu);
1035
72bf9492
DJ
1036static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1037 struct dwarf2_cu *cu);
91c24f0a 1038
bc30ff58
JB
1039static void add_partial_subprogram (struct partial_die_info *pdi,
1040 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1041 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1042
a14ed312 1043static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
c906108c 1044
a14ed312 1045static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1046
dee91e82
DE
1047static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1048 struct dwarf2_section_info *);
c906108c 1049
f3dd6933 1050static void dwarf2_free_abbrev_table (void *);
c906108c 1051
6caca83c
CC
1052static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
1053
fe1b8b76 1054static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
891d2f0b 1055 struct dwarf2_cu *);
72bf9492 1056
57349743 1057static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
e7c27a73 1058 struct dwarf2_cu *);
c906108c 1059
dee91e82
DE
1060static struct partial_die_info *load_partial_dies
1061 (const struct die_reader_specs *, gdb_byte *, int);
72bf9492 1062
dee91e82
DE
1063static gdb_byte *read_partial_die (const struct die_reader_specs *,
1064 struct partial_die_info *,
1065 struct abbrev_info *,
1066 unsigned int,
1067 gdb_byte *);
c906108c 1068
b64f50a1 1069static struct partial_die_info *find_partial_die (sect_offset,
10b3939b 1070 struct dwarf2_cu *);
72bf9492
DJ
1071
1072static void fixup_partial_die (struct partial_die_info *,
1073 struct dwarf2_cu *);
1074
dee91e82
DE
1075static gdb_byte *read_attribute (const struct die_reader_specs *,
1076 struct attribute *, struct attr_abbrev *,
1077 gdb_byte *);
a8329558 1078
fe1b8b76 1079static unsigned int read_1_byte (bfd *, gdb_byte *);
c906108c 1080
fe1b8b76 1081static int read_1_signed_byte (bfd *, gdb_byte *);
c906108c 1082
fe1b8b76 1083static unsigned int read_2_bytes (bfd *, gdb_byte *);
c906108c 1084
fe1b8b76 1085static unsigned int read_4_bytes (bfd *, gdb_byte *);
c906108c 1086
93311388 1087static ULONGEST read_8_bytes (bfd *, gdb_byte *);
c906108c 1088
fe1b8b76 1089static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1090 unsigned int *);
c906108c 1091
c764a876
DE
1092static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
1093
1094static LONGEST read_checked_initial_length_and_offset
1095 (bfd *, gdb_byte *, const struct comp_unit_head *,
1096 unsigned int *, unsigned int *);
613e1657 1097
fe1b8b76 1098static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
c764a876
DE
1099 unsigned int *);
1100
1101static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
613e1657 1102
fe1b8b76 1103static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
c906108c 1104
9b1c24c8 1105static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
c906108c 1106
fe1b8b76
JB
1107static char *read_indirect_string (bfd *, gdb_byte *,
1108 const struct comp_unit_head *,
1109 unsigned int *);
4bdf3d34 1110
12df843f 1111static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1112
12df843f 1113static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
c906108c 1114
3019eac3
DE
1115static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *, gdb_byte *,
1116 unsigned int *);
1117
1118static char *read_str_index (const struct die_reader_specs *reader,
1119 struct dwarf2_cu *cu, ULONGEST str_index);
1120
e142c38c 1121static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1122
e142c38c
DJ
1123static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1124 struct dwarf2_cu *);
c906108c 1125
348e048f
DE
1126static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1127 unsigned int,
1128 struct dwarf2_cu *);
1129
05cf31d1
JB
1130static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1131 struct dwarf2_cu *cu);
1132
e142c38c 1133static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1134
e142c38c 1135static struct die_info *die_specification (struct die_info *die,
f2f0e013 1136 struct dwarf2_cu **);
63d06c5c 1137
debd256d
JB
1138static void free_line_header (struct line_header *lh);
1139
aaa75496
JB
1140static void add_file_name (struct line_header *, char *, unsigned int,
1141 unsigned int, unsigned int);
1142
3019eac3
DE
1143static struct line_header *dwarf_decode_line_header (unsigned int offset,
1144 struct dwarf2_cu *cu);
debd256d 1145
f3f5162e
DE
1146static void dwarf_decode_lines (struct line_header *, const char *,
1147 struct dwarf2_cu *, struct partial_symtab *,
1148 int);
c906108c 1149
72b9f47f 1150static void dwarf2_start_subfile (char *, const char *, const char *);
c906108c 1151
a14ed312 1152static struct symbol *new_symbol (struct die_info *, struct type *,
e7c27a73 1153 struct dwarf2_cu *);
c906108c 1154
34eaf542
TT
1155static struct symbol *new_symbol_full (struct die_info *, struct type *,
1156 struct dwarf2_cu *, struct symbol *);
1157
a14ed312 1158static void dwarf2_const_value (struct attribute *, struct symbol *,
e7c27a73 1159 struct dwarf2_cu *);
c906108c 1160
98bfdba5
PA
1161static void dwarf2_const_value_attr (struct attribute *attr,
1162 struct type *type,
1163 const char *name,
1164 struct obstack *obstack,
12df843f 1165 struct dwarf2_cu *cu, LONGEST *value,
98bfdba5
PA
1166 gdb_byte **bytes,
1167 struct dwarf2_locexpr_baton **baton);
2df3850c 1168
e7c27a73 1169static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1170
b4ba55a1
JB
1171static int need_gnat_info (struct dwarf2_cu *);
1172
3e43a32a
MS
1173static struct type *die_descriptive_type (struct die_info *,
1174 struct dwarf2_cu *);
b4ba55a1
JB
1175
1176static void set_descriptive_type (struct type *, struct die_info *,
1177 struct dwarf2_cu *);
1178
e7c27a73
DJ
1179static struct type *die_containing_type (struct die_info *,
1180 struct dwarf2_cu *);
c906108c 1181
673bfd45
DE
1182static struct type *lookup_die_type (struct die_info *, struct attribute *,
1183 struct dwarf2_cu *);
c906108c 1184
f792889a 1185static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1186
673bfd45
DE
1187static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1188
0d5cff50 1189static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1190
6e70227d 1191static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1192 const char *suffix, int physname,
1193 struct dwarf2_cu *cu);
63d06c5c 1194
e7c27a73 1195static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1196
348e048f
DE
1197static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1198
e7c27a73 1199static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1200
e7c27a73 1201static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1202
96408a79
SA
1203static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1204
ff013f42
JK
1205static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1206 struct dwarf2_cu *, struct partial_symtab *);
1207
a14ed312 1208static int dwarf2_get_pc_bounds (struct die_info *,
d85a05f0
DJ
1209 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1210 struct partial_symtab *);
c906108c 1211
fae299cd
DC
1212static void get_scope_pc_bounds (struct die_info *,
1213 CORE_ADDR *, CORE_ADDR *,
1214 struct dwarf2_cu *);
1215
801e3a5b
JB
1216static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1217 CORE_ADDR, struct dwarf2_cu *);
1218
a14ed312 1219static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1220 struct dwarf2_cu *);
c906108c 1221
a14ed312 1222static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1223 struct type *, struct dwarf2_cu *);
c906108c 1224
a14ed312 1225static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1226 struct die_info *, struct type *,
e7c27a73 1227 struct dwarf2_cu *);
c906108c 1228
a14ed312 1229static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1230 struct type *,
1231 struct dwarf2_cu *);
c906108c 1232
134d01f1 1233static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1234
e7c27a73 1235static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1236
e7c27a73 1237static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1238
5d7cb8df
JK
1239static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1240
27aa8d6a
SW
1241static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1242
f55ee35c
JK
1243static struct type *read_module_type (struct die_info *die,
1244 struct dwarf2_cu *cu);
1245
38d518c9 1246static const char *namespace_name (struct die_info *die,
e142c38c 1247 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1248
134d01f1 1249static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1250
e7c27a73 1251static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1252
6e70227d 1253static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1254 struct dwarf2_cu *);
1255
dee91e82 1256static struct die_info *read_die_and_children (const struct die_reader_specs *,
93311388 1257 gdb_byte *info_ptr,
fe1b8b76 1258 gdb_byte **new_info_ptr,
639d11d3
DC
1259 struct die_info *parent);
1260
dee91e82 1261static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
93311388 1262 gdb_byte *info_ptr,
fe1b8b76 1263 gdb_byte **new_info_ptr,
639d11d3
DC
1264 struct die_info *parent);
1265
3019eac3
DE
1266static gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1267 struct die_info **, gdb_byte *, int *, int);
1268
dee91e82
DE
1269static gdb_byte *read_full_die (const struct die_reader_specs *,
1270 struct die_info **, gdb_byte *, int *);
93311388 1271
e7c27a73 1272static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1273
71c25dea
TT
1274static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1275 struct obstack *);
1276
e142c38c 1277static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1278
98bfdba5
PA
1279static const char *dwarf2_full_name (char *name,
1280 struct die_info *die,
1281 struct dwarf2_cu *cu);
1282
e142c38c 1283static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1284 struct dwarf2_cu **);
9219021c 1285
f39c6ffd 1286static const char *dwarf_tag_name (unsigned int);
c906108c 1287
f39c6ffd 1288static const char *dwarf_attr_name (unsigned int);
c906108c 1289
f39c6ffd 1290static const char *dwarf_form_name (unsigned int);
c906108c 1291
a14ed312 1292static char *dwarf_bool_name (unsigned int);
c906108c 1293
f39c6ffd 1294static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1295
f9aca02d 1296static struct die_info *sibling_die (struct die_info *);
c906108c 1297
d97bc12b
DE
1298static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1299
1300static void dump_die_for_error (struct die_info *);
1301
1302static void dump_die_1 (struct ui_file *, int level, int max_level,
1303 struct die_info *);
c906108c 1304
d97bc12b 1305/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1306
51545339 1307static void store_in_ref_table (struct die_info *,
10b3939b 1308 struct dwarf2_cu *);
c906108c 1309
93311388
DE
1310static int is_ref_attr (struct attribute *);
1311
b64f50a1 1312static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
c906108c 1313
43bbcdc2 1314static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
a02abb62 1315
348e048f
DE
1316static struct die_info *follow_die_ref_or_sig (struct die_info *,
1317 struct attribute *,
1318 struct dwarf2_cu **);
1319
10b3939b
DJ
1320static struct die_info *follow_die_ref (struct die_info *,
1321 struct attribute *,
f2f0e013 1322 struct dwarf2_cu **);
c906108c 1323
348e048f
DE
1324static struct die_info *follow_die_sig (struct die_info *,
1325 struct attribute *,
1326 struct dwarf2_cu **);
1327
6c83ed52
TT
1328static struct signatured_type *lookup_signatured_type_at_offset
1329 (struct objfile *objfile,
b64f50a1 1330 struct dwarf2_section_info *section, sect_offset offset);
6c83ed52 1331
e5fe5e75 1332static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1333
52dc124a 1334static void read_signatured_type (struct signatured_type *);
348e048f 1335
c906108c
SS
1336/* memory allocation interface */
1337
7b5a2f43 1338static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1339
f3dd6933 1340static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
c906108c 1341
b60c80d6 1342static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1343
2e276125 1344static void dwarf_decode_macros (struct line_header *, unsigned int,
cf2c3c16
TT
1345 char *, bfd *, struct dwarf2_cu *,
1346 struct dwarf2_section_info *,
fceca515 1347 int, const char *);
2e276125 1348
8e19ed76
PS
1349static int attr_form_is_block (struct attribute *);
1350
3690dd37
JB
1351static int attr_form_is_section_offset (struct attribute *);
1352
1353static int attr_form_is_constant (struct attribute *);
1354
8cf6f0b1
TT
1355static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1356 struct dwarf2_loclist_baton *baton,
1357 struct attribute *attr);
1358
93e7bd98
DJ
1359static void dwarf2_symbol_mark_computed (struct attribute *attr,
1360 struct symbol *sym,
1361 struct dwarf2_cu *cu);
4c2df51b 1362
dee91e82
DE
1363static gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1364 gdb_byte *info_ptr,
1365 struct abbrev_info *abbrev);
4bb7a0a7 1366
72bf9492
DJ
1367static void free_stack_comp_unit (void *);
1368
72bf9492
DJ
1369static hashval_t partial_die_hash (const void *item);
1370
1371static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1372
ae038cb0 1373static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
b64f50a1 1374 (sect_offset offset, struct objfile *objfile);
ae038cb0 1375
9816fde3 1376static void init_one_comp_unit (struct dwarf2_cu *cu,
23745b47 1377 struct dwarf2_per_cu_data *per_cu);
9816fde3
JK
1378
1379static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1380 struct die_info *comp_unit_die,
1381 enum language pretend_language);
93311388 1382
68dc6402 1383static void free_heap_comp_unit (void *);
ae038cb0
DJ
1384
1385static void free_cached_comp_units (void *);
1386
1387static void age_cached_comp_units (void);
1388
dee91e82 1389static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1390
f792889a
DJ
1391static struct type *set_die_type (struct die_info *, struct type *,
1392 struct dwarf2_cu *);
1c379e20 1393
ae038cb0
DJ
1394static void create_all_comp_units (struct objfile *);
1395
0e50663e 1396static int create_all_type_units (struct objfile *);
1fd400ff 1397
95554aad
TT
1398static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1399 enum language);
10b3939b 1400
95554aad
TT
1401static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1402 enum language);
10b3939b
DJ
1403
1404static void dwarf2_add_dependence (struct dwarf2_cu *,
1405 struct dwarf2_per_cu_data *);
1406
ae038cb0
DJ
1407static void dwarf2_mark (struct dwarf2_cu *);
1408
1409static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1410
b64f50a1 1411static struct type *get_die_type_at_offset (sect_offset,
673bfd45
DE
1412 struct dwarf2_per_cu_data *per_cu);
1413
f792889a 1414static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1415
9291a0cd
TT
1416static void dwarf2_release_queue (void *dummy);
1417
95554aad
TT
1418static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1419 enum language pretend_language);
1420
1421static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1422 struct dwarf2_per_cu_data *per_cu,
1423 enum language pretend_language);
9291a0cd 1424
a0f42c21 1425static void process_queue (void);
9291a0cd
TT
1426
1427static void find_file_and_directory (struct die_info *die,
1428 struct dwarf2_cu *cu,
1429 char **name, char **comp_dir);
1430
1431static char *file_full_name (int file, struct line_header *lh,
1432 const char *comp_dir);
1433
9ff913ba
DE
1434static gdb_byte *read_and_check_comp_unit_head
1435 (struct comp_unit_head *header,
1436 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1437 int is_debug_types_section);
9291a0cd 1438
fd820528
DE
1439static void init_cutu_and_read_dies
1440 (struct dwarf2_per_cu_data *this_cu, int use_existing_cu, int keep,
3019eac3
DE
1441 die_reader_func_ftype *die_reader_func, void *data);
1442
dee91e82
DE
1443static void init_cutu_and_read_dies_simple
1444 (struct dwarf2_per_cu_data *this_cu,
1445 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1446
673bfd45 1447static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1448
95554aad 1449static void process_psymtab_comp_unit (struct dwarf2_per_cu_data *, int);
dee91e82 1450
3019eac3
DE
1451static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1452
1453static struct dwo_unit *lookup_dwo_comp_unit
1454 (struct dwarf2_per_cu_data *, char *, const char *, ULONGEST);
1455
1456static struct dwo_unit *lookup_dwo_type_unit
1457 (struct signatured_type *, char *, const char *);
1458
1459static void free_dwo_file_cleanup (void *);
1460
1461static void munmap_section_buffer (struct dwarf2_section_info *);
1462
95554aad
TT
1463static void process_cu_includes (void);
1464
9291a0cd
TT
1465#if WORDS_BIGENDIAN
1466
1467/* Convert VALUE between big- and little-endian. */
1468static offset_type
1469byte_swap (offset_type value)
1470{
1471 offset_type result;
1472
1473 result = (value & 0xff) << 24;
1474 result |= (value & 0xff00) << 8;
1475 result |= (value & 0xff0000) >> 8;
1476 result |= (value & 0xff000000) >> 24;
1477 return result;
1478}
1479
1480#define MAYBE_SWAP(V) byte_swap (V)
1481
1482#else
1483#define MAYBE_SWAP(V) (V)
1484#endif /* WORDS_BIGENDIAN */
1485
1486/* The suffix for an index file. */
1487#define INDEX_SUFFIX ".gdb-index"
1488
3da10d80
KS
1489static const char *dwarf2_physname (char *name, struct die_info *die,
1490 struct dwarf2_cu *cu);
1491
c906108c 1492/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1493 information and return true if we have enough to do something.
1494 NAMES points to the dwarf2 section names, or is NULL if the standard
1495 ELF names are used. */
c906108c
SS
1496
1497int
251d32d9
TG
1498dwarf2_has_info (struct objfile *objfile,
1499 const struct dwarf2_debug_sections *names)
c906108c 1500{
be391dca
TT
1501 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1502 if (!dwarf2_per_objfile)
1503 {
1504 /* Initialize per-objfile state. */
1505 struct dwarf2_per_objfile *data
1506 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
9a619af0 1507
be391dca
TT
1508 memset (data, 0, sizeof (*data));
1509 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1510 dwarf2_per_objfile = data;
6502dd73 1511
251d32d9
TG
1512 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1513 (void *) names);
be391dca
TT
1514 dwarf2_per_objfile->objfile = objfile;
1515 }
1516 return (dwarf2_per_objfile->info.asection != NULL
1517 && dwarf2_per_objfile->abbrev.asection != NULL);
c906108c
SS
1518}
1519
251d32d9
TG
1520/* When loading sections, we look either for uncompressed section or for
1521 compressed section names. */
233a11ab
CS
1522
1523static int
251d32d9
TG
1524section_is_p (const char *section_name,
1525 const struct dwarf2_section_names *names)
233a11ab 1526{
251d32d9
TG
1527 if (names->normal != NULL
1528 && strcmp (section_name, names->normal) == 0)
1529 return 1;
1530 if (names->compressed != NULL
1531 && strcmp (section_name, names->compressed) == 0)
1532 return 1;
1533 return 0;
233a11ab
CS
1534}
1535
c906108c
SS
1536/* This function is mapped across the sections and remembers the
1537 offset and size of each of the debugging sections we are interested
1538 in. */
1539
1540static void
251d32d9 1541dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
c906108c 1542{
251d32d9
TG
1543 const struct dwarf2_debug_sections *names;
1544
1545 if (vnames == NULL)
1546 names = &dwarf2_elf_names;
1547 else
1548 names = (const struct dwarf2_debug_sections *) vnames;
1549
1550 if (section_is_p (sectp->name, &names->info))
c906108c 1551 {
dce234bc
PP
1552 dwarf2_per_objfile->info.asection = sectp;
1553 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
c906108c 1554 }
251d32d9 1555 else if (section_is_p (sectp->name, &names->abbrev))
c906108c 1556 {
dce234bc
PP
1557 dwarf2_per_objfile->abbrev.asection = sectp;
1558 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
c906108c 1559 }
251d32d9 1560 else if (section_is_p (sectp->name, &names->line))
c906108c 1561 {
dce234bc
PP
1562 dwarf2_per_objfile->line.asection = sectp;
1563 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
c906108c 1564 }
251d32d9 1565 else if (section_is_p (sectp->name, &names->loc))
c906108c 1566 {
dce234bc
PP
1567 dwarf2_per_objfile->loc.asection = sectp;
1568 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
c906108c 1569 }
251d32d9 1570 else if (section_is_p (sectp->name, &names->macinfo))
c906108c 1571 {
dce234bc
PP
1572 dwarf2_per_objfile->macinfo.asection = sectp;
1573 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
c906108c 1574 }
cf2c3c16
TT
1575 else if (section_is_p (sectp->name, &names->macro))
1576 {
1577 dwarf2_per_objfile->macro.asection = sectp;
1578 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1579 }
251d32d9 1580 else if (section_is_p (sectp->name, &names->str))
c906108c 1581 {
dce234bc
PP
1582 dwarf2_per_objfile->str.asection = sectp;
1583 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
c906108c 1584 }
3019eac3
DE
1585 else if (section_is_p (sectp->name, &names->addr))
1586 {
1587 dwarf2_per_objfile->addr.asection = sectp;
1588 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1589 }
251d32d9 1590 else if (section_is_p (sectp->name, &names->frame))
b6af0555 1591 {
dce234bc
PP
1592 dwarf2_per_objfile->frame.asection = sectp;
1593 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
b6af0555 1594 }
251d32d9 1595 else if (section_is_p (sectp->name, &names->eh_frame))
b6af0555 1596 {
81b9b86e 1597 flagword aflag = bfd_get_section_flags (abfd, sectp);
9a619af0 1598
3799ccc6
EZ
1599 if (aflag & SEC_HAS_CONTENTS)
1600 {
dce234bc
PP
1601 dwarf2_per_objfile->eh_frame.asection = sectp;
1602 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
3799ccc6 1603 }
b6af0555 1604 }
251d32d9 1605 else if (section_is_p (sectp->name, &names->ranges))
af34e669 1606 {
dce234bc
PP
1607 dwarf2_per_objfile->ranges.asection = sectp;
1608 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
af34e669 1609 }
251d32d9 1610 else if (section_is_p (sectp->name, &names->types))
348e048f 1611 {
8b70b953
TT
1612 struct dwarf2_section_info type_section;
1613
1614 memset (&type_section, 0, sizeof (type_section));
1615 type_section.asection = sectp;
1616 type_section.size = bfd_get_section_size (sectp);
1617
1618 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1619 &type_section);
348e048f 1620 }
251d32d9 1621 else if (section_is_p (sectp->name, &names->gdb_index))
9291a0cd
TT
1622 {
1623 dwarf2_per_objfile->gdb_index.asection = sectp;
1624 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1625 }
dce234bc 1626
72dca2f5
FR
1627 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1628 && bfd_section_vma (abfd, sectp) == 0)
1629 dwarf2_per_objfile->has_section_at_zero = 1;
c906108c
SS
1630}
1631
dce234bc
PP
1632/* Decompress a section that was compressed using zlib. Store the
1633 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
233a11ab
CS
1634
1635static void
dce234bc
PP
1636zlib_decompress_section (struct objfile *objfile, asection *sectp,
1637 gdb_byte **outbuf, bfd_size_type *outsize)
1638{
3019eac3 1639 bfd *abfd = sectp->owner;
dce234bc
PP
1640#ifndef HAVE_ZLIB_H
1641 error (_("Support for zlib-compressed DWARF data (from '%s') "
1642 "is disabled in this copy of GDB"),
1643 bfd_get_filename (abfd));
1644#else
1645 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1646 gdb_byte *compressed_buffer = xmalloc (compressed_size);
affddf13 1647 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
dce234bc
PP
1648 bfd_size_type uncompressed_size;
1649 gdb_byte *uncompressed_buffer;
1650 z_stream strm;
1651 int rc;
1652 int header_size = 12;
1653
1654 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
3e43a32a
MS
1655 || bfd_bread (compressed_buffer,
1656 compressed_size, abfd) != compressed_size)
dce234bc
PP
1657 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1658 bfd_get_filename (abfd));
1659
1660 /* Read the zlib header. In this case, it should be "ZLIB" followed
1661 by the uncompressed section size, 8 bytes in big-endian order. */
1662 if (compressed_size < header_size
1663 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1664 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1665 bfd_get_filename (abfd));
1666 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1667 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1668 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1669 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1670 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1671 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1672 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1673 uncompressed_size += compressed_buffer[11];
1674
1675 /* It is possible the section consists of several compressed
1676 buffers concatenated together, so we uncompress in a loop. */
1677 strm.zalloc = NULL;
1678 strm.zfree = NULL;
1679 strm.opaque = NULL;
1680 strm.avail_in = compressed_size - header_size;
1681 strm.next_in = (Bytef*) compressed_buffer + header_size;
1682 strm.avail_out = uncompressed_size;
1683 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1684 uncompressed_size);
1685 rc = inflateInit (&strm);
1686 while (strm.avail_in > 0)
1687 {
1688 if (rc != Z_OK)
1689 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1690 bfd_get_filename (abfd), rc);
1691 strm.next_out = ((Bytef*) uncompressed_buffer
1692 + (uncompressed_size - strm.avail_out));
1693 rc = inflate (&strm, Z_FINISH);
1694 if (rc != Z_STREAM_END)
1695 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1696 bfd_get_filename (abfd), rc);
1697 rc = inflateReset (&strm);
1698 }
1699 rc = inflateEnd (&strm);
1700 if (rc != Z_OK
1701 || strm.avail_out != 0)
1702 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1703 bfd_get_filename (abfd), rc);
1704
affddf13 1705 do_cleanups (cleanup);
dce234bc
PP
1706 *outbuf = uncompressed_buffer;
1707 *outsize = uncompressed_size;
1708#endif
233a11ab
CS
1709}
1710
fceca515
DE
1711/* A helper function that decides whether a section is empty,
1712 or not present. */
9e0ac564
TT
1713
1714static int
1715dwarf2_section_empty_p (struct dwarf2_section_info *info)
1716{
1717 return info->asection == NULL || info->size == 0;
1718}
1719
3019eac3
DE
1720/* Read the contents of the section INFO.
1721 OBJFILE is the main object file, but not necessarily the file where
1722 the section comes from. E.g., for DWO files INFO->asection->owner
1723 is the bfd of the DWO file.
dce234bc 1724 If the section is compressed, uncompress it before returning. */
c906108c 1725
dce234bc
PP
1726static void
1727dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
c906108c 1728{
dce234bc 1729 asection *sectp = info->asection;
3019eac3 1730 bfd *abfd;
dce234bc
PP
1731 gdb_byte *buf, *retbuf;
1732 unsigned char header[4];
c906108c 1733
be391dca
TT
1734 if (info->readin)
1735 return;
dce234bc 1736 info->buffer = NULL;
b315ab21 1737 info->map_addr = NULL;
be391dca 1738 info->readin = 1;
188dd5d6 1739
9e0ac564 1740 if (dwarf2_section_empty_p (info))
dce234bc 1741 return;
c906108c 1742
3019eac3
DE
1743 /* Note that ABFD may not be from OBJFILE, e.g. a DWO section. */
1744 abfd = sectp->owner;
1745
dce234bc
PP
1746 /* Check if the file has a 4-byte header indicating compression. */
1747 if (info->size > sizeof (header)
1748 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1749 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1750 {
1751 /* Upon decompression, update the buffer and its size. */
1752 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1753 {
1754 zlib_decompress_section (objfile, sectp, &info->buffer,
1755 &info->size);
1756 return;
1757 }
1758 }
4bdf3d34 1759
dce234bc
PP
1760#ifdef HAVE_MMAP
1761 if (pagesize == 0)
1762 pagesize = getpagesize ();
2e276125 1763
dce234bc
PP
1764 /* Only try to mmap sections which are large enough: we don't want to
1765 waste space due to fragmentation. Also, only try mmap for sections
1766 without relocations. */
1767
1768 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1769 {
b315ab21
TG
1770 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1771 MAP_PRIVATE, sectp->filepos,
1772 &info->map_addr, &info->map_len);
dce234bc 1773
b315ab21 1774 if ((caddr_t)info->buffer != MAP_FAILED)
dce234bc 1775 {
be391dca 1776#if HAVE_POSIX_MADVISE
b315ab21 1777 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
be391dca 1778#endif
dce234bc
PP
1779 return;
1780 }
1781 }
1782#endif
1783
1784 /* If we get here, we are a normal, not-compressed section. */
1785 info->buffer = buf
1786 = obstack_alloc (&objfile->objfile_obstack, info->size);
1787
1788 /* When debugging .o files, we may need to apply relocations; see
1789 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1790 We never compress sections in .o files, so we only need to
1791 try this when the section is not compressed. */
ac8035ab 1792 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
1793 if (retbuf != NULL)
1794 {
1795 info->buffer = retbuf;
1796 return;
1797 }
1798
1799 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1800 || bfd_bread (buf, info->size, abfd) != info->size)
1801 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1802 bfd_get_filename (abfd));
1803}
1804
9e0ac564
TT
1805/* A helper function that returns the size of a section in a safe way.
1806 If you are positive that the section has been read before using the
1807 size, then it is safe to refer to the dwarf2_section_info object's
1808 "size" field directly. In other cases, you must call this
1809 function, because for compressed sections the size field is not set
1810 correctly until the section has been read. */
1811
1812static bfd_size_type
1813dwarf2_section_size (struct objfile *objfile,
1814 struct dwarf2_section_info *info)
1815{
1816 if (!info->readin)
1817 dwarf2_read_section (objfile, info);
1818 return info->size;
1819}
1820
dce234bc 1821/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1822 SECTION_NAME. */
af34e669 1823
dce234bc 1824void
3017a003
TG
1825dwarf2_get_section_info (struct objfile *objfile,
1826 enum dwarf2_section_enum sect,
dce234bc
PP
1827 asection **sectp, gdb_byte **bufp,
1828 bfd_size_type *sizep)
1829{
1830 struct dwarf2_per_objfile *data
1831 = objfile_data (objfile, dwarf2_objfile_data_key);
1832 struct dwarf2_section_info *info;
a3b2a86b
TT
1833
1834 /* We may see an objfile without any DWARF, in which case we just
1835 return nothing. */
1836 if (data == NULL)
1837 {
1838 *sectp = NULL;
1839 *bufp = NULL;
1840 *sizep = 0;
1841 return;
1842 }
3017a003
TG
1843 switch (sect)
1844 {
1845 case DWARF2_DEBUG_FRAME:
1846 info = &data->frame;
1847 break;
1848 case DWARF2_EH_FRAME:
1849 info = &data->eh_frame;
1850 break;
1851 default:
1852 gdb_assert_not_reached ("unexpected section");
1853 }
dce234bc 1854
9e0ac564 1855 dwarf2_read_section (objfile, info);
dce234bc
PP
1856
1857 *sectp = info->asection;
1858 *bufp = info->buffer;
1859 *sizep = info->size;
1860}
1861
9291a0cd 1862\f
7b9f3c50
DE
1863/* DWARF quick_symbols_functions support. */
1864
1865/* TUs can share .debug_line entries, and there can be a lot more TUs than
1866 unique line tables, so we maintain a separate table of all .debug_line
1867 derived entries to support the sharing.
1868 All the quick functions need is the list of file names. We discard the
1869 line_header when we're done and don't need to record it here. */
1870struct quick_file_names
1871{
1872 /* The offset in .debug_line of the line table. We hash on this. */
1873 unsigned int offset;
1874
1875 /* The number of entries in file_names, real_names. */
1876 unsigned int num_file_names;
1877
1878 /* The file names from the line table, after being run through
1879 file_full_name. */
1880 const char **file_names;
1881
1882 /* The file names from the line table after being run through
1883 gdb_realpath. These are computed lazily. */
1884 const char **real_names;
1885};
1886
1887/* When using the index (and thus not using psymtabs), each CU has an
1888 object of this type. This is used to hold information needed by
1889 the various "quick" methods. */
1890struct dwarf2_per_cu_quick_data
1891{
1892 /* The file table. This can be NULL if there was no file table
1893 or it's currently not read in.
1894 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1895 struct quick_file_names *file_names;
1896
1897 /* The corresponding symbol table. This is NULL if symbols for this
1898 CU have not yet been read. */
1899 struct symtab *symtab;
1900
1901 /* A temporary mark bit used when iterating over all CUs in
1902 expand_symtabs_matching. */
1903 unsigned int mark : 1;
1904
1905 /* True if we've tried to read the file table and found there isn't one.
1906 There will be no point in trying to read it again next time. */
1907 unsigned int no_file_data : 1;
1908};
1909
1910/* Hash function for a quick_file_names. */
1911
1912static hashval_t
1913hash_file_name_entry (const void *e)
1914{
1915 const struct quick_file_names *file_data = e;
1916
1917 return file_data->offset;
1918}
1919
1920/* Equality function for a quick_file_names. */
1921
1922static int
1923eq_file_name_entry (const void *a, const void *b)
1924{
1925 const struct quick_file_names *ea = a;
1926 const struct quick_file_names *eb = b;
1927
1928 return ea->offset == eb->offset;
1929}
1930
1931/* Delete function for a quick_file_names. */
1932
1933static void
1934delete_file_name_entry (void *e)
1935{
1936 struct quick_file_names *file_data = e;
1937 int i;
1938
1939 for (i = 0; i < file_data->num_file_names; ++i)
1940 {
1941 xfree ((void*) file_data->file_names[i]);
1942 if (file_data->real_names)
1943 xfree ((void*) file_data->real_names[i]);
1944 }
1945
1946 /* The space for the struct itself lives on objfile_obstack,
1947 so we don't free it here. */
1948}
1949
1950/* Create a quick_file_names hash table. */
1951
1952static htab_t
1953create_quick_file_names_table (unsigned int nr_initial_entries)
1954{
1955 return htab_create_alloc (nr_initial_entries,
1956 hash_file_name_entry, eq_file_name_entry,
1957 delete_file_name_entry, xcalloc, xfree);
1958}
9291a0cd 1959
918dd910
JK
1960/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1961 have to be created afterwards. You should call age_cached_comp_units after
1962 processing PER_CU->CU. dw2_setup must have been already called. */
1963
1964static void
1965load_cu (struct dwarf2_per_cu_data *per_cu)
1966{
3019eac3 1967 if (per_cu->is_debug_types)
e5fe5e75 1968 load_full_type_unit (per_cu);
918dd910 1969 else
95554aad 1970 load_full_comp_unit (per_cu, language_minimal);
918dd910 1971
918dd910 1972 gdb_assert (per_cu->cu != NULL);
2dc860c0
DE
1973
1974 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
1975}
1976
a0f42c21 1977/* Read in the symbols for PER_CU. */
2fdf6df6 1978
9291a0cd 1979static void
a0f42c21 1980dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd
TT
1981{
1982 struct cleanup *back_to;
1983
1984 back_to = make_cleanup (dwarf2_release_queue, NULL);
1985
95554aad
TT
1986 if (dwarf2_per_objfile->using_index
1987 ? per_cu->v.quick->symtab == NULL
1988 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
1989 {
1990 queue_comp_unit (per_cu, language_minimal);
1991 load_cu (per_cu);
1992 }
9291a0cd 1993
a0f42c21 1994 process_queue ();
9291a0cd
TT
1995
1996 /* Age the cache, releasing compilation units that have not
1997 been used recently. */
1998 age_cached_comp_units ();
1999
2000 do_cleanups (back_to);
2001}
2002
2003/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2004 the objfile from which this CU came. Returns the resulting symbol
2005 table. */
2fdf6df6 2006
9291a0cd 2007static struct symtab *
a0f42c21 2008dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
9291a0cd 2009{
95554aad 2010 gdb_assert (dwarf2_per_objfile->using_index);
9291a0cd
TT
2011 if (!per_cu->v.quick->symtab)
2012 {
2013 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2014 increment_reading_symtab ();
a0f42c21 2015 dw2_do_instantiate_symtab (per_cu);
95554aad 2016 process_cu_includes ();
9291a0cd
TT
2017 do_cleanups (back_to);
2018 }
2019 return per_cu->v.quick->symtab;
2020}
2021
1fd400ff 2022/* Return the CU given its index. */
2fdf6df6 2023
1fd400ff
TT
2024static struct dwarf2_per_cu_data *
2025dw2_get_cu (int index)
2026{
2027 if (index >= dwarf2_per_objfile->n_comp_units)
2028 {
2029 index -= dwarf2_per_objfile->n_comp_units;
d467dd73 2030 return dwarf2_per_objfile->all_type_units[index];
1fd400ff
TT
2031 }
2032 return dwarf2_per_objfile->all_comp_units[index];
2033}
2034
9291a0cd
TT
2035/* A helper function that knows how to read a 64-bit value in a way
2036 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
2037 otherwise. */
2fdf6df6 2038
9291a0cd
TT
2039static int
2040extract_cu_value (const char *bytes, ULONGEST *result)
2041{
2042 if (sizeof (ULONGEST) < 8)
2043 {
2044 int i;
2045
2046 /* Ignore the upper 4 bytes if they are all zero. */
2047 for (i = 0; i < 4; ++i)
2048 if (bytes[i + 4] != 0)
2049 return 0;
2050
2051 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
2052 }
2053 else
2054 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2055 return 1;
2056}
2057
2058/* Read the CU list from the mapped index, and use it to create all
2059 the CU objects for this objfile. Return 0 if something went wrong,
2060 1 if everything went ok. */
2fdf6df6 2061
9291a0cd 2062static int
1fd400ff
TT
2063create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
2064 offset_type cu_list_elements)
9291a0cd
TT
2065{
2066 offset_type i;
9291a0cd
TT
2067
2068 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
2069 dwarf2_per_objfile->all_comp_units
2070 = obstack_alloc (&objfile->objfile_obstack,
2071 dwarf2_per_objfile->n_comp_units
2072 * sizeof (struct dwarf2_per_cu_data *));
2073
2074 for (i = 0; i < cu_list_elements; i += 2)
2075 {
2076 struct dwarf2_per_cu_data *the_cu;
2077 ULONGEST offset, length;
2078
2079 if (!extract_cu_value (cu_list, &offset)
2080 || !extract_cu_value (cu_list + 8, &length))
2081 return 0;
2082 cu_list += 2 * 8;
2083
2084 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2085 struct dwarf2_per_cu_data);
b64f50a1 2086 the_cu->offset.sect_off = offset;
9291a0cd
TT
2087 the_cu->length = length;
2088 the_cu->objfile = objfile;
3019eac3 2089 the_cu->info_or_types_section = &dwarf2_per_objfile->info;
9291a0cd
TT
2090 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2091 struct dwarf2_per_cu_quick_data);
2092 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
2093 }
2094
2095 return 1;
2096}
2097
1fd400ff 2098/* Create the signatured type hash table from the index. */
673bfd45 2099
1fd400ff 2100static int
673bfd45 2101create_signatured_type_table_from_index (struct objfile *objfile,
8b70b953 2102 struct dwarf2_section_info *section,
673bfd45
DE
2103 const gdb_byte *bytes,
2104 offset_type elements)
1fd400ff
TT
2105{
2106 offset_type i;
673bfd45 2107 htab_t sig_types_hash;
1fd400ff 2108
d467dd73
DE
2109 dwarf2_per_objfile->n_type_units = elements / 3;
2110 dwarf2_per_objfile->all_type_units
1fd400ff 2111 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 2112 dwarf2_per_objfile->n_type_units
1fd400ff
TT
2113 * sizeof (struct dwarf2_per_cu_data *));
2114
673bfd45 2115 sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff
TT
2116
2117 for (i = 0; i < elements; i += 3)
2118 {
52dc124a
DE
2119 struct signatured_type *sig_type;
2120 ULONGEST offset, type_offset_in_tu, signature;
1fd400ff
TT
2121 void **slot;
2122
2123 if (!extract_cu_value (bytes, &offset)
52dc124a 2124 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1fd400ff
TT
2125 return 0;
2126 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2127 bytes += 3 * 8;
2128
52dc124a 2129 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2130 struct signatured_type);
52dc124a 2131 sig_type->signature = signature;
3019eac3
DE
2132 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2133 sig_type->per_cu.is_debug_types = 1;
2134 sig_type->per_cu.info_or_types_section = section;
52dc124a
DE
2135 sig_type->per_cu.offset.sect_off = offset;
2136 sig_type->per_cu.objfile = objfile;
2137 sig_type->per_cu.v.quick
1fd400ff
TT
2138 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2139 struct dwarf2_per_cu_quick_data);
2140
52dc124a
DE
2141 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2142 *slot = sig_type;
1fd400ff 2143
52dc124a 2144 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
1fd400ff
TT
2145 }
2146
673bfd45 2147 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
2148
2149 return 1;
2150}
2151
9291a0cd
TT
2152/* Read the address map data from the mapped index, and use it to
2153 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2154
9291a0cd
TT
2155static void
2156create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2157{
2158 const gdb_byte *iter, *end;
2159 struct obstack temp_obstack;
2160 struct addrmap *mutable_map;
2161 struct cleanup *cleanup;
2162 CORE_ADDR baseaddr;
2163
2164 obstack_init (&temp_obstack);
2165 cleanup = make_cleanup_obstack_free (&temp_obstack);
2166 mutable_map = addrmap_create_mutable (&temp_obstack);
2167
2168 iter = index->address_table;
2169 end = iter + index->address_table_size;
2170
2171 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2172
2173 while (iter < end)
2174 {
2175 ULONGEST hi, lo, cu_index;
2176 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2177 iter += 8;
2178 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2179 iter += 8;
2180 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2181 iter += 4;
2182
2183 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1fd400ff 2184 dw2_get_cu (cu_index));
9291a0cd
TT
2185 }
2186
2187 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2188 &objfile->objfile_obstack);
2189 do_cleanups (cleanup);
2190}
2191
59d7bcaf
JK
2192/* The hash function for strings in the mapped index. This is the same as
2193 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2194 implementation. This is necessary because the hash function is tied to the
2195 format of the mapped index file. The hash values do not have to match with
559a7a62
JK
2196 SYMBOL_HASH_NEXT.
2197
2198 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2fdf6df6 2199
9291a0cd 2200static hashval_t
559a7a62 2201mapped_index_string_hash (int index_version, const void *p)
9291a0cd
TT
2202{
2203 const unsigned char *str = (const unsigned char *) p;
2204 hashval_t r = 0;
2205 unsigned char c;
2206
2207 while ((c = *str++) != 0)
559a7a62
JK
2208 {
2209 if (index_version >= 5)
2210 c = tolower (c);
2211 r = r * 67 + c - 113;
2212 }
9291a0cd
TT
2213
2214 return r;
2215}
2216
2217/* Find a slot in the mapped index INDEX for the object named NAME.
2218 If NAME is found, set *VEC_OUT to point to the CU vector in the
2219 constant pool and return 1. If NAME cannot be found, return 0. */
2fdf6df6 2220
9291a0cd
TT
2221static int
2222find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2223 offset_type **vec_out)
2224{
0cf03b49
JK
2225 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2226 offset_type hash;
9291a0cd 2227 offset_type slot, step;
559a7a62 2228 int (*cmp) (const char *, const char *);
9291a0cd 2229
0cf03b49
JK
2230 if (current_language->la_language == language_cplus
2231 || current_language->la_language == language_java
2232 || current_language->la_language == language_fortran)
2233 {
2234 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2235 not contain any. */
2236 const char *paren = strchr (name, '(');
2237
2238 if (paren)
2239 {
2240 char *dup;
2241
2242 dup = xmalloc (paren - name + 1);
2243 memcpy (dup, name, paren - name);
2244 dup[paren - name] = 0;
2245
2246 make_cleanup (xfree, dup);
2247 name = dup;
2248 }
2249 }
2250
559a7a62 2251 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2252 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2253 simulate our NAME being searched is also lowercased. */
2254 hash = mapped_index_string_hash ((index->version == 4
2255 && case_sensitivity == case_sensitive_off
2256 ? 5 : index->version),
2257 name);
2258
3876f04e
DE
2259 slot = hash & (index->symbol_table_slots - 1);
2260 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
559a7a62 2261 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2262
2263 for (;;)
2264 {
2265 /* Convert a slot number to an offset into the table. */
2266 offset_type i = 2 * slot;
2267 const char *str;
3876f04e 2268 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
0cf03b49
JK
2269 {
2270 do_cleanups (back_to);
2271 return 0;
2272 }
9291a0cd 2273
3876f04e 2274 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
559a7a62 2275 if (!cmp (name, str))
9291a0cd
TT
2276 {
2277 *vec_out = (offset_type *) (index->constant_pool
3876f04e 2278 + MAYBE_SWAP (index->symbol_table[i + 1]));
0cf03b49 2279 do_cleanups (back_to);
9291a0cd
TT
2280 return 1;
2281 }
2282
3876f04e 2283 slot = (slot + step) & (index->symbol_table_slots - 1);
9291a0cd
TT
2284 }
2285}
2286
2287/* Read the index file. If everything went ok, initialize the "quick"
2288 elements of all the CUs and return 1. Otherwise, return 0. */
2fdf6df6 2289
9291a0cd
TT
2290static int
2291dwarf2_read_index (struct objfile *objfile)
2292{
9291a0cd
TT
2293 char *addr;
2294 struct mapped_index *map;
b3b272e1 2295 offset_type *metadata;
ac0b195c
KW
2296 const gdb_byte *cu_list;
2297 const gdb_byte *types_list = NULL;
2298 offset_type version, cu_list_elements;
2299 offset_type types_list_elements = 0;
1fd400ff 2300 int i;
9291a0cd 2301
9e0ac564 2302 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
9291a0cd 2303 return 0;
82430852
JK
2304
2305 /* Older elfutils strip versions could keep the section in the main
2306 executable while splitting it for the separate debug info file. */
2307 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2308 & SEC_HAS_CONTENTS) == 0)
2309 return 0;
2310
9291a0cd
TT
2311 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2312
2313 addr = dwarf2_per_objfile->gdb_index.buffer;
2314 /* Version check. */
1fd400ff 2315 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2316 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2317 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2318 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2319 indices. */
831adc1f 2320 if (version < 4)
481860b3
GB
2321 {
2322 static int warning_printed = 0;
2323 if (!warning_printed)
2324 {
2325 warning (_("Skipping obsolete .gdb_index section in %s."),
2326 objfile->name);
2327 warning_printed = 1;
2328 }
2329 return 0;
2330 }
2331 /* Index version 4 uses a different hash function than index version
2332 5 and later.
2333
2334 Versions earlier than 6 did not emit psymbols for inlined
2335 functions. Using these files will cause GDB not to be able to
2336 set breakpoints on inlined functions by name, so we ignore these
2337 indices unless the --use-deprecated-index-sections command line
2338 option was supplied. */
2339 if (version < 6 && !use_deprecated_index_sections)
2340 {
2341 static int warning_printed = 0;
2342 if (!warning_printed)
2343 {
2344 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2345 "--use-deprecated-index-sections to use them anyway"),
2346 objfile->name);
2347 warning_printed = 1;
2348 }
2349 return 0;
2350 }
2351 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2352 longer backward compatible. */
481860b3 2353 if (version > 6)
594e8718 2354 return 0;
9291a0cd
TT
2355
2356 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
559a7a62 2357 map->version = version;
b3b272e1 2358 map->total_size = dwarf2_per_objfile->gdb_index.size;
9291a0cd
TT
2359
2360 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
2361
2362 i = 0;
2363 cu_list = addr + MAYBE_SWAP (metadata[i]);
2364 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
9291a0cd 2365 / 8);
1fd400ff
TT
2366 ++i;
2367
987d643c
TT
2368 types_list = addr + MAYBE_SWAP (metadata[i]);
2369 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2370 - MAYBE_SWAP (metadata[i]))
2371 / 8);
2372 ++i;
1fd400ff
TT
2373
2374 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2375 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2376 - MAYBE_SWAP (metadata[i]));
2377 ++i;
2378
3876f04e
DE
2379 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2380 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2381 - MAYBE_SWAP (metadata[i]))
2382 / (2 * sizeof (offset_type)));
1fd400ff 2383 ++i;
9291a0cd 2384
1fd400ff
TT
2385 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2386
0fefef59
DE
2387 /* Don't use the index if it's empty. */
2388 if (map->symbol_table_slots == 0)
2389 return 0;
2390
1fd400ff
TT
2391 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2392 return 0;
2393
8b70b953
TT
2394 if (types_list_elements)
2395 {
2396 struct dwarf2_section_info *section;
2397
2398 /* We can only handle a single .debug_types when we have an
2399 index. */
2400 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2401 return 0;
2402
2403 section = VEC_index (dwarf2_section_info_def,
2404 dwarf2_per_objfile->types, 0);
2405
2406 if (!create_signatured_type_table_from_index (objfile, section,
2407 types_list,
2408 types_list_elements))
2409 return 0;
2410 }
9291a0cd
TT
2411
2412 create_addrmap_from_index (objfile, map);
2413
2414 dwarf2_per_objfile->index_table = map;
2415 dwarf2_per_objfile->using_index = 1;
7b9f3c50
DE
2416 dwarf2_per_objfile->quick_file_names_table =
2417 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd
TT
2418
2419 return 1;
2420}
2421
2422/* A helper for the "quick" functions which sets the global
2423 dwarf2_per_objfile according to OBJFILE. */
2fdf6df6 2424
9291a0cd
TT
2425static void
2426dw2_setup (struct objfile *objfile)
2427{
2428 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2429 gdb_assert (dwarf2_per_objfile);
2430}
2431
dee91e82 2432/* die_reader_func for dw2_get_file_names. */
2fdf6df6 2433
dee91e82
DE
2434static void
2435dw2_get_file_names_reader (const struct die_reader_specs *reader,
2436 gdb_byte *info_ptr,
2437 struct die_info *comp_unit_die,
2438 int has_children,
2439 void *data)
9291a0cd 2440{
dee91e82
DE
2441 struct dwarf2_cu *cu = reader->cu;
2442 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2443 struct objfile *objfile = dwarf2_per_objfile->objfile;
7b9f3c50 2444 struct line_header *lh;
9291a0cd 2445 struct attribute *attr;
dee91e82 2446 int i;
9291a0cd 2447 char *name, *comp_dir;
7b9f3c50
DE
2448 void **slot;
2449 struct quick_file_names *qfn;
2450 unsigned int line_offset;
9291a0cd 2451
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
2909 if (per_cu->is_debug_types)
2910 init_cutu_and_read_dies (per_cu, 0, 0, dw2_get_primary_filename_reader,
2911 &filename);
2912 else
2913 init_cutu_and_read_dies_simple (per_cu, dw2_get_primary_filename_reader,
2914 &filename);
9291a0cd 2915
356d9f9d 2916 return filename;
9291a0cd
TT
2917}
2918
2919static void
40658b94
PH
2920dw2_map_matching_symbols (const char * name, domain_enum namespace,
2921 struct objfile *objfile, int global,
2922 int (*callback) (struct block *,
2923 struct symbol *, void *),
2edb89d3
JK
2924 void *data, symbol_compare_ftype *match,
2925 symbol_compare_ftype *ordered_compare)
9291a0cd 2926{
40658b94 2927 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
2928 current language is Ada for a non-Ada objfile using GNU index. As Ada
2929 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
2930}
2931
2932static void
f8eba3c6
TT
2933dw2_expand_symtabs_matching
2934 (struct objfile *objfile,
2935 int (*file_matcher) (const char *, void *),
e078317b 2936 int (*name_matcher) (const char *, void *),
f8eba3c6
TT
2937 enum search_domain kind,
2938 void *data)
9291a0cd
TT
2939{
2940 int i;
2941 offset_type iter;
4b5246aa 2942 struct mapped_index *index;
9291a0cd
TT
2943
2944 dw2_setup (objfile);
ae2de4f8
DE
2945
2946 /* index_table is NULL if OBJF_READNOW. */
9291a0cd
TT
2947 if (!dwarf2_per_objfile->index_table)
2948 return;
4b5246aa 2949 index = dwarf2_per_objfile->index_table;
9291a0cd 2950
7b08b9eb 2951 if (file_matcher != NULL)
24c79950
TT
2952 {
2953 struct cleanup *cleanup;
2954 htab_t visited_found, visited_not_found;
2955
2956 visited_found = htab_create_alloc (10,
2957 htab_hash_pointer, htab_eq_pointer,
2958 NULL, xcalloc, xfree);
2959 cleanup = make_cleanup_htab_delete (visited_found);
2960 visited_not_found = htab_create_alloc (10,
2961 htab_hash_pointer, htab_eq_pointer,
2962 NULL, xcalloc, xfree);
2963 make_cleanup_htab_delete (visited_not_found);
2964
2965 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2966 + dwarf2_per_objfile->n_type_units); ++i)
2967 {
2968 int j;
2969 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2970 struct quick_file_names *file_data;
2971 void **slot;
7b08b9eb 2972
24c79950 2973 per_cu->v.quick->mark = 0;
3d7bb9d9 2974
24c79950
TT
2975 /* We only need to look at symtabs not already expanded. */
2976 if (per_cu->v.quick->symtab)
2977 continue;
7b08b9eb 2978
24c79950
TT
2979 file_data = dw2_get_file_names (objfile, per_cu);
2980 if (file_data == NULL)
2981 continue;
7b08b9eb 2982
24c79950
TT
2983 if (htab_find (visited_not_found, file_data) != NULL)
2984 continue;
2985 else if (htab_find (visited_found, file_data) != NULL)
2986 {
2987 per_cu->v.quick->mark = 1;
2988 continue;
2989 }
2990
2991 for (j = 0; j < file_data->num_file_names; ++j)
2992 {
2993 if (file_matcher (file_data->file_names[j], data))
2994 {
2995 per_cu->v.quick->mark = 1;
2996 break;
2997 }
2998 }
2999
3000 slot = htab_find_slot (per_cu->v.quick->mark
3001 ? visited_found
3002 : visited_not_found,
3003 file_data, INSERT);
3004 *slot = file_data;
3005 }
3006
3007 do_cleanups (cleanup);
3008 }
9291a0cd 3009
3876f04e 3010 for (iter = 0; iter < index->symbol_table_slots; ++iter)
9291a0cd
TT
3011 {
3012 offset_type idx = 2 * iter;
3013 const char *name;
3014 offset_type *vec, vec_len, vec_idx;
3015
3876f04e 3016 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
9291a0cd
TT
3017 continue;
3018
3876f04e 3019 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
9291a0cd 3020
e078317b 3021 if (! (*name_matcher) (name, data))
9291a0cd
TT
3022 continue;
3023
3024 /* The name was matched, now expand corresponding CUs that were
3025 marked. */
4b5246aa 3026 vec = (offset_type *) (index->constant_pool
3876f04e 3027 + MAYBE_SWAP (index->symbol_table[idx + 1]));
9291a0cd
TT
3028 vec_len = MAYBE_SWAP (vec[0]);
3029 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3030 {
e254ef6a 3031 struct dwarf2_per_cu_data *per_cu;
1fd400ff 3032
e254ef6a 3033 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
7b08b9eb 3034 if (file_matcher == NULL || per_cu->v.quick->mark)
a0f42c21 3035 dw2_instantiate_symtab (per_cu);
9291a0cd
TT
3036 }
3037 }
3038}
3039
9703b513
TT
3040/* A helper for dw2_find_pc_sect_symtab which finds the most specific
3041 symtab. */
3042
3043static struct symtab *
3044recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3045{
3046 int i;
3047
3048 if (BLOCKVECTOR (symtab) != NULL
3049 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3050 return symtab;
3051
a3ec0bb1
DE
3052 if (symtab->includes == NULL)
3053 return NULL;
3054
9703b513
TT
3055 for (i = 0; symtab->includes[i]; ++i)
3056 {
a3ec0bb1 3057 struct symtab *s = symtab->includes[i];
9703b513
TT
3058
3059 s = recursively_find_pc_sect_symtab (s, pc);
3060 if (s != NULL)
3061 return s;
3062 }
3063
3064 return NULL;
3065}
3066
9291a0cd
TT
3067static struct symtab *
3068dw2_find_pc_sect_symtab (struct objfile *objfile,
3069 struct minimal_symbol *msymbol,
3070 CORE_ADDR pc,
3071 struct obj_section *section,
3072 int warn_if_readin)
3073{
3074 struct dwarf2_per_cu_data *data;
9703b513 3075 struct symtab *result;
9291a0cd
TT
3076
3077 dw2_setup (objfile);
3078
3079 if (!objfile->psymtabs_addrmap)
3080 return NULL;
3081
3082 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3083 if (!data)
3084 return NULL;
3085
3086 if (warn_if_readin && data->v.quick->symtab)
abebb8b0 3087 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
9291a0cd
TT
3088 paddress (get_objfile_arch (objfile), pc));
3089
9703b513
TT
3090 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3091 gdb_assert (result != NULL);
3092 return result;
9291a0cd
TT
3093}
3094
9291a0cd 3095static void
44b13c5a 3096dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
74e2f255 3097 void *data, int need_fullname)
9291a0cd
TT
3098{
3099 int i;
24c79950
TT
3100 struct cleanup *cleanup;
3101 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3102 NULL, xcalloc, xfree);
9291a0cd 3103
24c79950 3104 cleanup = make_cleanup_htab_delete (visited);
9291a0cd 3105 dw2_setup (objfile);
ae2de4f8 3106
24c79950
TT
3107 /* We can ignore file names coming from already-expanded CUs. */
3108 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3109 + dwarf2_per_objfile->n_type_units); ++i)
3110 {
3111 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3112
3113 if (per_cu->v.quick->symtab)
3114 {
3115 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3116 INSERT);
3117
3118 *slot = per_cu->v.quick->file_names;
3119 }
3120 }
3121
1fd400ff 3122 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3123 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd
TT
3124 {
3125 int j;
e254ef6a 3126 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
7b9f3c50 3127 struct quick_file_names *file_data;
24c79950 3128 void **slot;
9291a0cd 3129
3d7bb9d9 3130 /* We only need to look at symtabs not already expanded. */
e254ef6a 3131 if (per_cu->v.quick->symtab)
9291a0cd
TT
3132 continue;
3133
7b9f3c50
DE
3134 file_data = dw2_get_file_names (objfile, per_cu);
3135 if (file_data == NULL)
9291a0cd
TT
3136 continue;
3137
24c79950
TT
3138 slot = htab_find_slot (visited, file_data, INSERT);
3139 if (*slot)
3140 {
3141 /* Already visited. */
3142 continue;
3143 }
3144 *slot = file_data;
3145
7b9f3c50 3146 for (j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3147 {
74e2f255
DE
3148 const char *this_real_name;
3149
3150 if (need_fullname)
3151 this_real_name = dw2_get_real_path (objfile, file_data, j);
3152 else
3153 this_real_name = NULL;
7b9f3c50 3154 (*fun) (file_data->file_names[j], this_real_name, data);
9291a0cd
TT
3155 }
3156 }
24c79950
TT
3157
3158 do_cleanups (cleanup);
9291a0cd
TT
3159}
3160
3161static int
3162dw2_has_symbols (struct objfile *objfile)
3163{
3164 return 1;
3165}
3166
3167const struct quick_symbol_functions dwarf2_gdb_index_functions =
3168{
3169 dw2_has_symbols,
3170 dw2_find_last_source_symtab,
3171 dw2_forget_cached_source_info,
f8eba3c6 3172 dw2_map_symtabs_matching_filename,
9291a0cd 3173 dw2_lookup_symbol,
774b6a14 3174 dw2_pre_expand_symtabs_matching,
9291a0cd
TT
3175 dw2_print_stats,
3176 dw2_dump,
3177 dw2_relocate,
3178 dw2_expand_symtabs_for_function,
3179 dw2_expand_all_symtabs,
3180 dw2_expand_symtabs_with_filename,
3181 dw2_find_symbol_file,
40658b94 3182 dw2_map_matching_symbols,
9291a0cd
TT
3183 dw2_expand_symtabs_matching,
3184 dw2_find_pc_sect_symtab,
9291a0cd
TT
3185 dw2_map_symbol_filenames
3186};
3187
3188/* Initialize for reading DWARF for this objfile. Return 0 if this
3189 file will use psymtabs, or 1 if using the GNU index. */
3190
3191int
3192dwarf2_initialize_objfile (struct objfile *objfile)
3193{
3194 /* If we're about to read full symbols, don't bother with the
3195 indices. In this case we also don't care if some other debug
3196 format is making psymtabs, because they are all about to be
3197 expanded anyway. */
3198 if ((objfile->flags & OBJF_READNOW))
3199 {
3200 int i;
3201
3202 dwarf2_per_objfile->using_index = 1;
3203 create_all_comp_units (objfile);
0e50663e 3204 create_all_type_units (objfile);
7b9f3c50
DE
3205 dwarf2_per_objfile->quick_file_names_table =
3206 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
9291a0cd 3207
1fd400ff 3208 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
d467dd73 3209 + dwarf2_per_objfile->n_type_units); ++i)
9291a0cd 3210 {
e254ef6a 3211 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
9291a0cd 3212
e254ef6a
DE
3213 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3214 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
3215 }
3216
3217 /* Return 1 so that gdb sees the "quick" functions. However,
3218 these functions will be no-ops because we will have expanded
3219 all symtabs. */
3220 return 1;
3221 }
3222
3223 if (dwarf2_read_index (objfile))
3224 return 1;
3225
9291a0cd
TT
3226 return 0;
3227}
3228
3229\f
3230
dce234bc
PP
3231/* Build a partial symbol table. */
3232
3233void
f29dff0a 3234dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 3235{
f29dff0a 3236 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
c906108c
SS
3237 {
3238 init_psymbol_list (objfile, 1024);
3239 }
3240
d146bf1e 3241 dwarf2_build_psymtabs_hard (objfile);
c906108c 3242}
c906108c 3243
45452591
DE
3244/* Return TRUE if OFFSET is within CU_HEADER. */
3245
3246static inline int
b64f50a1 3247offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
45452591 3248{
b64f50a1
JK
3249 sect_offset bottom = { cu_header->offset.sect_off };
3250 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3251 + cu_header->initial_length_size) };
9a619af0 3252
b64f50a1 3253 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
45452591
DE
3254}
3255
93311388
DE
3256/* Read in the comp unit header information from the debug_info at info_ptr.
3257 NOTE: This leaves members offset, first_die_offset to be filled in
3258 by the caller. */
107d2387 3259
fe1b8b76 3260static gdb_byte *
107d2387 3261read_comp_unit_head (struct comp_unit_head *cu_header,
fe1b8b76 3262 gdb_byte *info_ptr, bfd *abfd)
107d2387
AC
3263{
3264 int signed_addr;
891d2f0b 3265 unsigned int bytes_read;
c764a876
DE
3266
3267 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3268 cu_header->initial_length_size = bytes_read;
3269 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 3270 info_ptr += bytes_read;
107d2387
AC
3271 cu_header->version = read_2_bytes (abfd, info_ptr);
3272 info_ptr += 2;
b64f50a1
JK
3273 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3274 &bytes_read);
613e1657 3275 info_ptr += bytes_read;
107d2387
AC
3276 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3277 info_ptr += 1;
3278 signed_addr = bfd_get_sign_extend_vma (abfd);
3279 if (signed_addr < 0)
8e65ff28 3280 internal_error (__FILE__, __LINE__,
e2e0b3e5 3281 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 3282 cu_header->signed_addr_p = signed_addr;
c764a876 3283
107d2387
AC
3284 return info_ptr;
3285}
3286
9ff913ba
DE
3287/* Subroutine of read_and_check_comp_unit_head and
3288 read_and_check_type_unit_head to simplify them.
3289 Perform various error checking on the header. */
3290
3291static void
3292error_check_comp_unit_head (struct comp_unit_head *header,
3293 struct dwarf2_section_info *section)
3294{
3295 bfd *abfd = section->asection->owner;
3296 const char *filename = bfd_get_filename (abfd);
3297
3298 if (header->version != 2 && header->version != 3 && header->version != 4)
3299 error (_("Dwarf Error: wrong version in compilation unit header "
3300 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3301 filename);
3302
b64f50a1 3303 if (header->abbrev_offset.sect_off
9ff913ba
DE
3304 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3305 &dwarf2_per_objfile->abbrev))
3306 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3307 "(offset 0x%lx + 6) [in module %s]"),
b64f50a1 3308 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
9ff913ba
DE
3309 filename);
3310
3311 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3312 avoid potential 32-bit overflow. */
b64f50a1 3313 if (((unsigned long) header->offset.sect_off
9ff913ba
DE
3314 + header->length + header->initial_length_size)
3315 > section->size)
3316 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3317 "(offset 0x%lx + 0) [in module %s]"),
b64f50a1 3318 (long) header->length, (long) header->offset.sect_off,
9ff913ba
DE
3319 filename);
3320}
3321
3322/* Read in a CU/TU header and perform some basic error checking.
3323 The contents of the header are stored in HEADER.
3324 The result is a pointer to the start of the first DIE. */
adabb602 3325
fe1b8b76 3326static gdb_byte *
9ff913ba
DE
3327read_and_check_comp_unit_head (struct comp_unit_head *header,
3328 struct dwarf2_section_info *section,
3329 gdb_byte *info_ptr,
3330 int is_debug_types_section)
72bf9492 3331{
fe1b8b76 3332 gdb_byte *beg_of_comp_unit = info_ptr;
9ff913ba 3333 bfd *abfd = section->asection->owner;
72bf9492 3334
b64f50a1 3335 header->offset.sect_off = beg_of_comp_unit - section->buffer;
adabb602 3336
72bf9492
DJ
3337 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3338
460c1c54
CC
3339 /* If we're reading a type unit, skip over the signature and
3340 type_offset fields. */
b0df02fd 3341 if (is_debug_types_section)
460c1c54
CC
3342 info_ptr += 8 /*signature*/ + header->offset_size;
3343
b64f50a1 3344 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
adabb602 3345
9ff913ba 3346 error_check_comp_unit_head (header, section);
72bf9492
DJ
3347
3348 return info_ptr;
3349}
3350
348e048f
DE
3351/* Read in the types comp unit header information from .debug_types entry at
3352 types_ptr. The result is a pointer to one past the end of the header. */
3353
3354static gdb_byte *
9ff913ba
DE
3355read_and_check_type_unit_head (struct comp_unit_head *header,
3356 struct dwarf2_section_info *section,
3357 gdb_byte *info_ptr,
dee91e82
DE
3358 ULONGEST *signature,
3359 cu_offset *type_offset_in_tu)
348e048f 3360{
9ff913ba
DE
3361 gdb_byte *beg_of_comp_unit = info_ptr;
3362 bfd *abfd = section->asection->owner;
348e048f 3363
b64f50a1 3364 header->offset.sect_off = beg_of_comp_unit - section->buffer;
348e048f 3365
9ff913ba 3366 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
348e048f 3367
9ff913ba
DE
3368 /* If we're reading a type unit, skip over the signature and
3369 type_offset fields. */
3370 if (signature != NULL)
3371 *signature = read_8_bytes (abfd, info_ptr);
3372 info_ptr += 8;
dee91e82
DE
3373 if (type_offset_in_tu != NULL)
3374 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
3375 header->offset_size);
9ff913ba
DE
3376 info_ptr += header->offset_size;
3377
b64f50a1 3378 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
348e048f 3379
9ff913ba
DE
3380 error_check_comp_unit_head (header, section);
3381
3382 return info_ptr;
348e048f
DE
3383}
3384
aaa75496
JB
3385/* Allocate a new partial symtab for file named NAME and mark this new
3386 partial symtab as being an include of PST. */
3387
3388static void
3389dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3390 struct objfile *objfile)
3391{
3392 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3393
3394 subpst->section_offsets = pst->section_offsets;
3395 subpst->textlow = 0;
3396 subpst->texthigh = 0;
3397
3398 subpst->dependencies = (struct partial_symtab **)
3399 obstack_alloc (&objfile->objfile_obstack,
3400 sizeof (struct partial_symtab *));
3401 subpst->dependencies[0] = pst;
3402 subpst->number_of_dependencies = 1;
3403
3404 subpst->globals_offset = 0;
3405 subpst->n_global_syms = 0;
3406 subpst->statics_offset = 0;
3407 subpst->n_static_syms = 0;
3408 subpst->symtab = NULL;
3409 subpst->read_symtab = pst->read_symtab;
3410 subpst->readin = 0;
3411
3412 /* No private part is necessary for include psymtabs. This property
3413 can be used to differentiate between such include psymtabs and
10b3939b 3414 the regular ones. */
58a9656e 3415 subpst->read_symtab_private = NULL;
aaa75496
JB
3416}
3417
3418/* Read the Line Number Program data and extract the list of files
3419 included by the source file represented by PST. Build an include
d85a05f0 3420 partial symtab for each of these included files. */
aaa75496
JB
3421
3422static void
3423dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
3424 struct die_info *die,
3425 struct partial_symtab *pst)
aaa75496 3426{
d85a05f0
DJ
3427 struct line_header *lh = NULL;
3428 struct attribute *attr;
aaa75496 3429
d85a05f0
DJ
3430 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3431 if (attr)
3019eac3 3432 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
aaa75496
JB
3433 if (lh == NULL)
3434 return; /* No linetable, so no includes. */
3435
c6da4cef 3436 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
f3f5162e 3437 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
aaa75496
JB
3438
3439 free_line_header (lh);
3440}
3441
348e048f 3442static hashval_t
52dc124a 3443hash_signatured_type (const void *item)
348e048f 3444{
52dc124a 3445 const struct signatured_type *sig_type = item;
9a619af0 3446
348e048f 3447 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 3448 return sig_type->signature;
348e048f
DE
3449}
3450
3451static int
52dc124a 3452eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f
DE
3453{
3454 const struct signatured_type *lhs = item_lhs;
3455 const struct signatured_type *rhs = item_rhs;
9a619af0 3456
348e048f
DE
3457 return lhs->signature == rhs->signature;
3458}
3459
1fd400ff
TT
3460/* Allocate a hash table for signatured types. */
3461
3462static htab_t
673bfd45 3463allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
3464{
3465 return htab_create_alloc_ex (41,
52dc124a
DE
3466 hash_signatured_type,
3467 eq_signatured_type,
1fd400ff
TT
3468 NULL,
3469 &objfile->objfile_obstack,
3470 hashtab_obstack_allocate,
3471 dummy_obstack_deallocate);
3472}
3473
d467dd73 3474/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
3475
3476static int
d467dd73 3477add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff
TT
3478{
3479 struct signatured_type *sigt = *slot;
3480 struct dwarf2_per_cu_data ***datap = datum;
3481
3482 **datap = &sigt->per_cu;
3483 ++*datap;
3484
3485 return 1;
3486}
3487
3019eac3
DE
3488/* Create the hash table of all entries in the .debug_types section.
3489 DWO_FILE is a pointer to the DWO file for .debug_types.dwo, NULL otherwise.
3490 The result is a pointer to the hash table or NULL if there are
3491 no types. */
348e048f 3492
3019eac3
DE
3493static htab_t
3494create_debug_types_hash_table (struct dwo_file *dwo_file,
3495 VEC (dwarf2_section_info_def) *types)
348e048f 3496{
3019eac3 3497 struct objfile *objfile = dwarf2_per_objfile->objfile;
8b70b953 3498 htab_t types_htab = NULL;
8b70b953
TT
3499 int ix;
3500 struct dwarf2_section_info *section;
348e048f 3501
3019eac3
DE
3502 if (VEC_empty (dwarf2_section_info_def, types))
3503 return NULL;
348e048f 3504
8b70b953 3505 for (ix = 0;
3019eac3 3506 VEC_iterate (dwarf2_section_info_def, types, ix, section);
8b70b953
TT
3507 ++ix)
3508 {
3019eac3 3509 bfd *abfd;
8b70b953 3510 gdb_byte *info_ptr, *end_ptr;
348e048f 3511
8b70b953
TT
3512 dwarf2_read_section (objfile, section);
3513 info_ptr = section->buffer;
348e048f 3514
8b70b953
TT
3515 if (info_ptr == NULL)
3516 continue;
348e048f 3517
3019eac3
DE
3518 /* We can't set abfd until now because the section may be empty or
3519 not present, in which case section->asection will be NULL. */
3520 abfd = section->asection->owner;
3521
8b70b953 3522 if (types_htab == NULL)
3019eac3
DE
3523 {
3524 if (dwo_file)
3525 types_htab = allocate_dwo_unit_table (objfile);
3526 else
3527 types_htab = allocate_signatured_type_table (objfile);
3528 }
348e048f 3529
8b70b953 3530 if (dwarf2_die_debug)
dee91e82
DE
3531 fprintf_unfiltered (gdb_stdlog, "Reading signatured types for %s:\n",
3532 bfd_get_filename (abfd));
3533
3534 /* We don't use init_cutu_and_read_dies_simple, or some such, here
3535 because we don't need to read any dies: the signature is in the
3536 header. */
8b70b953
TT
3537
3538 end_ptr = info_ptr + section->size;
3539 while (info_ptr < end_ptr)
3540 {
b64f50a1 3541 sect_offset offset;
3019eac3 3542 cu_offset type_offset_in_tu;
8b70b953 3543 ULONGEST signature;
52dc124a 3544 struct signatured_type *sig_type;
3019eac3 3545 struct dwo_unit *dwo_tu;
8b70b953
TT
3546 void **slot;
3547 gdb_byte *ptr = info_ptr;
9ff913ba 3548 struct comp_unit_head header;
dee91e82 3549 unsigned int length;
348e048f 3550
b64f50a1 3551 offset.sect_off = ptr - section->buffer;
348e048f 3552
8b70b953 3553 /* We need to read the type's signature in order to build the hash
9ff913ba 3554 table, but we don't need anything else just yet. */
348e048f 3555
9ff913ba 3556 ptr = read_and_check_type_unit_head (&header, section, ptr,
3019eac3 3557 &signature, &type_offset_in_tu);
6caca83c 3558
dee91e82
DE
3559 length = header.initial_length_size + header.length;
3560
6caca83c 3561 /* Skip dummy type units. */
dee91e82
DE
3562 if (ptr >= info_ptr + length
3563 || peek_abbrev_code (abfd, ptr) == 0)
6caca83c 3564 {
dee91e82 3565 info_ptr += header.initial_length_size + header.length;
6caca83c
CC
3566 continue;
3567 }
8b70b953 3568
3019eac3
DE
3569 if (dwo_file)
3570 {
3571 sig_type = NULL;
3572 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3573 struct dwo_unit);
3574 dwo_tu->dwo_file = dwo_file;
3575 dwo_tu->signature = signature;
3576 dwo_tu->type_offset_in_tu = type_offset_in_tu;
3577 dwo_tu->info_or_types_section = section;
3578 dwo_tu->offset = offset;
3579 dwo_tu->length = length;
3580 }
3581 else
3582 {
3583 /* N.B.: type_offset is not usable if this type uses a DWO file.
3584 The real type_offset is in the DWO file. */
3585 dwo_tu = NULL;
3586 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3587 struct signatured_type);
3588 sig_type->signature = signature;
3589 sig_type->type_offset_in_tu = type_offset_in_tu;
3590 sig_type->per_cu.objfile = objfile;
3591 sig_type->per_cu.is_debug_types = 1;
3592 sig_type->per_cu.info_or_types_section = section;
3593 sig_type->per_cu.offset = offset;
3594 sig_type->per_cu.length = length;
3595 }
8b70b953 3596
3019eac3
DE
3597 slot = htab_find_slot (types_htab,
3598 dwo_file ? (void*) dwo_tu : (void *) sig_type,
3599 INSERT);
8b70b953
TT
3600 gdb_assert (slot != NULL);
3601 if (*slot != NULL)
3602 {
3019eac3
DE
3603 sect_offset dup_offset;
3604
3605 if (dwo_file)
3606 {
3607 const struct dwo_unit *dup_tu = *slot;
3608
3609 dup_offset = dup_tu->offset;
3610 }
3611 else
3612 {
3613 const struct signatured_type *dup_tu = *slot;
3614
3615 dup_offset = dup_tu->per_cu.offset;
3616 }
b3c8eb43 3617
8b70b953
TT
3618 complaint (&symfile_complaints,
3619 _("debug type entry at offset 0x%x is duplicate to the "
3620 "entry at offset 0x%x, signature 0x%s"),
3019eac3 3621 offset.sect_off, dup_offset.sect_off,
8b70b953 3622 phex (signature, sizeof (signature)));
8b70b953 3623 }
3019eac3 3624 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
348e048f 3625
8b70b953
TT
3626 if (dwarf2_die_debug)
3627 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
b64f50a1
JK
3628 offset.sect_off,
3629 phex (signature, sizeof (signature)));
348e048f 3630
dee91e82 3631 info_ptr += length;
8b70b953 3632 }
348e048f
DE
3633 }
3634
3019eac3
DE
3635 return types_htab;
3636}
3637
3638/* Create the hash table of all entries in the .debug_types section,
3639 and initialize all_type_units.
3640 The result is zero if there is an error (e.g. missing .debug_types section),
3641 otherwise non-zero. */
3642
3643static int
3644create_all_type_units (struct objfile *objfile)
3645{
3646 htab_t types_htab;
3647 struct dwarf2_per_cu_data **iter;
3648
3649 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
3650 if (types_htab == NULL)
3651 {
3652 dwarf2_per_objfile->signatured_types = NULL;
3653 return 0;
3654 }
3655
348e048f
DE
3656 dwarf2_per_objfile->signatured_types = types_htab;
3657
d467dd73
DE
3658 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3659 dwarf2_per_objfile->all_type_units
1fd400ff 3660 = obstack_alloc (&objfile->objfile_obstack,
d467dd73 3661 dwarf2_per_objfile->n_type_units
1fd400ff 3662 * sizeof (struct dwarf2_per_cu_data *));
d467dd73
DE
3663 iter = &dwarf2_per_objfile->all_type_units[0];
3664 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3665 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3666 == dwarf2_per_objfile->n_type_units);
1fd400ff 3667
348e048f
DE
3668 return 1;
3669}
3670
380bca97 3671/* Lookup a signature based type for DW_FORM_ref_sig8.
e319fa28 3672 Returns NULL if signature SIG is not present in the table. */
348e048f
DE
3673
3674static struct signatured_type *
e319fa28 3675lookup_signatured_type (ULONGEST sig)
348e048f
DE
3676{
3677 struct signatured_type find_entry, *entry;
3678
3679 if (dwarf2_per_objfile->signatured_types == NULL)
3680 {
3681 complaint (&symfile_complaints,
55f1336d 3682 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
dcc07052 3683 return NULL;
348e048f
DE
3684 }
3685
3686 find_entry.signature = sig;
3687 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3688 return entry;
3689}
3690
d85a05f0
DJ
3691/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3692
3693static void
3694init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 3695 struct dwarf2_cu *cu,
3019eac3
DE
3696 struct dwarf2_section_info *section,
3697 struct dwo_file *dwo_file)
d85a05f0 3698{
fceca515 3699 gdb_assert (section->readin && section->buffer != NULL);
dee91e82 3700 reader->abfd = section->asection->owner;
d85a05f0 3701 reader->cu = cu;
3019eac3 3702 reader->dwo_file = dwo_file;
dee91e82
DE
3703 reader->die_section = section;
3704 reader->buffer = section->buffer;
f664829e 3705 reader->buffer_end = section->buffer + section->size;
d85a05f0
DJ
3706}
3707
3708/* Find the base address of the compilation unit for range lists and
3709 location lists. It will normally be specified by DW_AT_low_pc.
3710 In DWARF-3 draft 4, the base address could be overridden by
3711 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3712 compilation units with discontinuous ranges. */
3713
3714static void
3715dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3716{
3717 struct attribute *attr;
3718
3719 cu->base_known = 0;
3720 cu->base_address = 0;
3721
3722 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3723 if (attr)
3724 {
3725 cu->base_address = DW_ADDR (attr);
3726 cu->base_known = 1;
3727 }
3728 else
3729 {
3730 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3731 if (attr)
3732 {
3733 cu->base_address = DW_ADDR (attr);
3734 cu->base_known = 1;
3735 }
3736 }
3737}
3738
fd820528 3739/* Initialize a CU (or TU) and read its DIEs.
3019eac3 3740 If the CU defers to a DWO file, read the DWO file as well.
dee91e82
DE
3741
3742 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
3743 Otherwise, a new CU is allocated with xmalloc.
3744
3745 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
3746 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
3747
3748 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 3749 linker) then DIE_READER_FUNC will not get called. */
aaa75496 3750
70221824 3751static void
fd820528
DE
3752init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
3753 int use_existing_cu, int keep,
3754 die_reader_func_ftype *die_reader_func,
3755 void *data)
c906108c 3756{
dee91e82 3757 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
3758 struct dwarf2_section_info *section = this_cu->info_or_types_section;
3759 bfd *abfd = section->asection->owner;
dee91e82
DE
3760 struct dwarf2_cu *cu;
3761 gdb_byte *begin_info_ptr, *info_ptr;
3762 struct die_reader_specs reader;
d85a05f0 3763 struct die_info *comp_unit_die;
dee91e82 3764 int has_children;
d85a05f0 3765 struct attribute *attr;
dee91e82
DE
3766 struct cleanup *cleanups, *free_cu_cleanup = NULL;
3767 struct signatured_type *sig_type = NULL;
c906108c 3768
dee91e82
DE
3769 if (use_existing_cu)
3770 gdb_assert (keep);
23745b47 3771
dee91e82
DE
3772 cleanups = make_cleanup (null_cleanup, NULL);
3773
3774 /* This is cheap if the section is already read in. */
3775 dwarf2_read_section (objfile, section);
3776
3777 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
3778
3779 if (use_existing_cu && this_cu->cu != NULL)
3780 {
3781 cu = this_cu->cu;
3782 info_ptr += cu->header.first_die_offset.cu_off;
3783 }
3784 else
3785 {
3786 /* If !use_existing_cu, this_cu->cu must be NULL. */
3787 gdb_assert (this_cu->cu == NULL);
3788
3789 cu = xmalloc (sizeof (*cu));
3790 init_one_comp_unit (cu, this_cu);
3791
3792 /* If an error occurs while loading, release our storage. */
3793 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3794
3019eac3 3795 if (this_cu->is_debug_types)
dee91e82
DE
3796 {
3797 ULONGEST signature;
3798
3799 info_ptr = read_and_check_type_unit_head (&cu->header,
3800 section, info_ptr,
3801 &signature, NULL);
3802
3803 /* There's no way to get from PER_CU to its containing
3804 struct signatured_type.
3805 But we have the signature so we can use that. */
3806 sig_type = lookup_signatured_type (signature);
3807 /* We've already scanned all the signatured types,
3808 this must succeed. */
3809 gdb_assert (sig_type != NULL);
3810 gdb_assert (&sig_type->per_cu == this_cu);
3811 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3812
3813 /* LENGTH has not been set yet for type units. */
3814 this_cu->length = cu->header.length + cu->header.initial_length_size;
3019eac3
DE
3815
3816 /* Establish the type offset that can be used to lookup the type. */
3817 sig_type->type_offset_in_section.sect_off =
3818 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
dee91e82
DE
3819 }
3820 else
3821 {
3822 info_ptr = read_and_check_comp_unit_head (&cu->header,
3823 section, info_ptr, 0);
3824
3825 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
3826 gdb_assert (this_cu->length
3827 == cu->header.length + cu->header.initial_length_size);
3828 }
3829 }
10b3939b 3830
6caca83c 3831 /* Skip dummy compilation units. */
dee91e82 3832 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c
CC
3833 || peek_abbrev_code (abfd, info_ptr) == 0)
3834 {
dee91e82 3835 do_cleanups (cleanups);
21b2bd31 3836 return;
6caca83c
CC
3837 }
3838
93311388 3839 /* Read the abbrevs for this compilation unit into a table. */
dee91e82
DE
3840 if (cu->dwarf2_abbrevs == NULL)
3841 {
3842 dwarf2_read_abbrevs (cu, &dwarf2_per_objfile->abbrev);
3843 make_cleanup (dwarf2_free_abbrev_table, cu);
3844 }
af703f96 3845
dee91e82 3846 /* Read the top level CU/TU die. */
3019eac3 3847 init_cu_die_reader (&reader, cu, section, NULL);
dee91e82 3848 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 3849
3019eac3
DE
3850 /* If we have a DWO stub, process it and then read in the DWO file.
3851 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains
3852 a DWO CU, that this test will fail. */
3853 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
3854 if (attr)
3855 {
3856 char *dwo_name = DW_STRING (attr);
3857 const char *comp_dir;
3858 struct dwo_unit *dwo_unit;
3859 ULONGEST signature; /* Or dwo_id. */
3860 struct attribute *stmt_list, *low_pc, *high_pc, *ranges;
3861 int i,num_extra_attrs;
3862
3863 if (has_children)
3864 error (_("Dwarf Error: compilation unit with DW_AT_GNU_dwo_name"
3865 " has children (offset 0x%x) [in module %s]"),
3866 this_cu->offset.sect_off, bfd_get_filename (abfd));
3867
3868 /* These attributes aren't processed until later:
3869 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
3870 However, the attribute is found in the stub which we won't have later.
3871 In order to not impose this complication on the rest of the code,
3872 we read them here and copy them to the DWO CU/TU die. */
3873 stmt_list = low_pc = high_pc = ranges = NULL;
3874
3875 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
3876 DWO file. */
3877 if (! this_cu->is_debug_types)
3878 stmt_list = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3879 low_pc = dwarf2_attr (comp_unit_die, DW_AT_low_pc, cu);
3880 high_pc = dwarf2_attr (comp_unit_die, DW_AT_high_pc, cu);
3881 ranges = dwarf2_attr (comp_unit_die, DW_AT_ranges, cu);
3882
3883 /* There should be a DW_AT_addr_base attribute here (if needed).
3884 We need the value before we can process DW_FORM_GNU_addr_index. */
3885 cu->addr_base = 0;
3886 cu->have_addr_base = 0;
3887 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_addr_base, cu);
3888 if (attr)
3889 {
3890 cu->addr_base = DW_UNSND (attr);
3891 cu->have_addr_base = 1;
3892 }
3893
3894 if (this_cu->is_debug_types)
3895 {
3896 gdb_assert (sig_type != NULL);
3897 signature = sig_type->signature;
3898 }
3899 else
3900 {
3901 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
3902 if (! attr)
3903 error (_("Dwarf Error: missing dwo_id [in module %s]"),
3904 dwo_name);
3905 signature = DW_UNSND (attr);
3906 }
3907
3908 /* We may need the comp_dir in order to find the DWO file. */
3909 comp_dir = NULL;
3910 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
3911 if (attr)
3912 comp_dir = DW_STRING (attr);
3913
3914 if (this_cu->is_debug_types)
3915 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
3916 else
3917 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
3918 signature);
3919
3920 if (dwo_unit == NULL)
3921 {
3922 error (_("Dwarf Error: CU at offset 0x%x references unknown DWO"
3923 " with ID %s [in module %s]"),
3924 this_cu->offset.sect_off,
3925 phex (signature, sizeof (signature)),
3926 objfile->name);
3927 }
3928
3929 /* Set up for reading the DWO CU/TU. */
3930 cu->dwo_unit = dwo_unit;
3931 section = dwo_unit->info_or_types_section;
3932 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
3933 init_cu_die_reader (&reader, cu, section, dwo_unit->dwo_file);
3934
3935 if (this_cu->is_debug_types)
3936 {
3937 ULONGEST signature;
3938
3939 info_ptr = read_and_check_type_unit_head (&cu->header,
3940 section, info_ptr,
3941 &signature, NULL);
3942 gdb_assert (sig_type->signature == signature);
3943 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3944 gdb_assert (dwo_unit->length
3945 == cu->header.length + cu->header.initial_length_size);
3946
3947 /* Establish the type offset that can be used to lookup the type.
3948 For DWO files, we don't know it until now. */
3949 sig_type->type_offset_in_section.sect_off =
3950 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
3951 }
3952 else
3953 {
3954 info_ptr = read_and_check_comp_unit_head (&cu->header,
3955 section, info_ptr, 0);
3956 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
3957 gdb_assert (dwo_unit->length
3958 == cu->header.length + cu->header.initial_length_size);
3959 }
3960
3961 /* Discard the original CU's abbrev table, and read the DWO's. */
3962 dwarf2_free_abbrev_table (cu);
3963 dwarf2_read_abbrevs (cu, &dwo_unit->dwo_file->sections.abbrev);
3964
3965 /* Read in the die, but leave space to copy over the attributes
3966 from the stub. This has the benefit of simplifying the rest of
3967 the code - all the real work is done here. */
3968 num_extra_attrs = ((stmt_list != NULL)
3969 + (low_pc != NULL)
3970 + (high_pc != NULL)
3971 + (ranges != NULL));
3972 info_ptr = read_full_die_1 (&reader, &comp_unit_die, info_ptr,
3973 &has_children, num_extra_attrs);
3974
3975 /* Copy over the attributes from the stub to the DWO die. */
3976 i = comp_unit_die->num_attrs;
3977 if (stmt_list != NULL)
3978 comp_unit_die->attrs[i++] = *stmt_list;
3979 if (low_pc != NULL)
3980 comp_unit_die->attrs[i++] = *low_pc;
3981 if (high_pc != NULL)
3982 comp_unit_die->attrs[i++] = *high_pc;
3983 if (ranges != NULL)
3984 comp_unit_die->attrs[i++] = *ranges;
3985 comp_unit_die->num_attrs += num_extra_attrs;
3986
3987 /* Skip dummy compilation units. */
3988 if (info_ptr >= begin_info_ptr + dwo_unit->length
3989 || peek_abbrev_code (abfd, info_ptr) == 0)
3990 {
3991 do_cleanups (cleanups);
3992 return;
3993 }
3994 }
3995
dee91e82
DE
3996 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
3997
3998 if (free_cu_cleanup != NULL)
348e048f 3999 {
dee91e82
DE
4000 if (keep)
4001 {
4002 /* We've successfully allocated this compilation unit. Let our
4003 caller clean it up when finished with it. */
4004 discard_cleanups (free_cu_cleanup);
4005
4006 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4007 So we have to manually free the abbrev table. */
4008 dwarf2_free_abbrev_table (cu);
4009
4010 /* Link this CU into read_in_chain. */
4011 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4012 dwarf2_per_objfile->read_in_chain = this_cu;
4013 }
4014 else
4015 do_cleanups (free_cu_cleanup);
348e048f 4016 }
dee91e82
DE
4017
4018 do_cleanups (cleanups);
4019}
4020
3019eac3
DE
4021/* Read CU/TU THIS_CU in section SECTION,
4022 but do not follow DW_AT_GNU_dwo_name if present.
4023 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed to
4024 have already done the lookup to find the DWO file).
dee91e82
DE
4025
4026 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 4027 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
4028
4029 We fill in THIS_CU->length.
4030
4031 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4032 linker) then DIE_READER_FUNC will not get called.
4033
4034 THIS_CU->cu is always freed when done.
3019eac3
DE
4035 This is done in order to not leave THIS_CU->cu in a state where we have
4036 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
4037
4038static void
4039init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
4040 struct dwarf2_section_info *abbrev_section,
3019eac3 4041 struct dwo_file *dwo_file,
dee91e82
DE
4042 die_reader_func_ftype *die_reader_func,
4043 void *data)
4044{
4045 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3
DE
4046 struct dwarf2_section_info *section = this_cu->info_or_types_section;
4047 bfd *abfd = section->asection->owner;
dee91e82
DE
4048 struct dwarf2_cu cu;
4049 gdb_byte *begin_info_ptr, *info_ptr;
4050 struct die_reader_specs reader;
4051 struct cleanup *cleanups;
4052 struct die_info *comp_unit_die;
4053 int has_children;
4054
4055 gdb_assert (this_cu->cu == NULL);
4056
dee91e82
DE
4057 /* This is cheap if the section is already read in. */
4058 dwarf2_read_section (objfile, section);
4059
4060 init_one_comp_unit (&cu, this_cu);
4061
4062 cleanups = make_cleanup (free_stack_comp_unit, &cu);
4063
4064 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
4065 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3019eac3 4066 this_cu->is_debug_types);
dee91e82
DE
4067
4068 this_cu->length = cu.header.length + cu.header.initial_length_size;
4069
4070 /* Skip dummy compilation units. */
4071 if (info_ptr >= begin_info_ptr + this_cu->length
4072 || peek_abbrev_code (abfd, info_ptr) == 0)
c906108c 4073 {
dee91e82 4074 do_cleanups (cleanups);
21b2bd31 4075 return;
93311388 4076 }
72bf9492 4077
dee91e82
DE
4078 dwarf2_read_abbrevs (&cu, abbrev_section);
4079 make_cleanup (dwarf2_free_abbrev_table, &cu);
4080
3019eac3 4081 init_cu_die_reader (&reader, &cu, section, dwo_file);
dee91e82
DE
4082 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
4083
4084 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4085
4086 do_cleanups (cleanups);
4087}
4088
3019eac3
DE
4089/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
4090 does not lookup the specified DWO file.
4091 This cannot be used to read DWO files.
dee91e82
DE
4092
4093 THIS_CU->cu is always freed when done.
3019eac3
DE
4094 This is done in order to not leave THIS_CU->cu in a state where we have
4095 to care whether it refers to the "main" CU or the DWO CU.
4096 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
4097
4098static void
4099init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
4100 die_reader_func_ftype *die_reader_func,
4101 void *data)
4102{
4103 init_cutu_and_read_dies_no_follow (this_cu,
4104 &dwarf2_per_objfile->abbrev,
3019eac3 4105 NULL,
dee91e82
DE
4106 die_reader_func, data);
4107}
4108
4109/* die_reader_func for process_psymtab_comp_unit. */
4110
4111static void
4112process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
4113 gdb_byte *info_ptr,
4114 struct die_info *comp_unit_die,
4115 int has_children,
4116 void *data)
4117{
4118 struct dwarf2_cu *cu = reader->cu;
4119 struct objfile *objfile = cu->objfile;
4120 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
dee91e82
DE
4121 struct attribute *attr;
4122 CORE_ADDR baseaddr;
4123 CORE_ADDR best_lowpc = 0, best_highpc = 0;
4124 struct partial_symtab *pst;
4125 int has_pc_info;
4126 const char *filename;
95554aad 4127 int *want_partial_unit_ptr = data;
dee91e82 4128
95554aad
TT
4129 if (comp_unit_die->tag == DW_TAG_partial_unit
4130 && (want_partial_unit_ptr == NULL
4131 || !*want_partial_unit_ptr))
dee91e82
DE
4132 return;
4133
95554aad 4134 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
dee91e82
DE
4135
4136 cu->list_in_scope = &file_symbols;
c906108c 4137
93311388 4138 /* Allocate a new partial symbol table structure. */
dee91e82 4139 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3e2a0cee
TT
4140 if (attr == NULL || !DW_STRING (attr))
4141 filename = "";
4142 else
4143 filename = DW_STRING (attr);
93311388 4144 pst = start_psymtab_common (objfile, objfile->section_offsets,
3e2a0cee 4145 filename,
93311388
DE
4146 /* TEXTLOW and TEXTHIGH are set below. */
4147 0,
4148 objfile->global_psymbols.next,
4149 objfile->static_psymbols.next);
9750bca9 4150 pst->psymtabs_addrmap_supported = 1;
72bf9492 4151
dee91e82 4152 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
d85a05f0
DJ
4153 if (attr != NULL)
4154 pst->dirname = DW_STRING (attr);
72bf9492 4155
dee91e82 4156 pst->read_symtab_private = per_cu;
72bf9492 4157
93311388 4158 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
e7c27a73 4159
0963b4bd 4160 /* Store the function that reads in the rest of the symbol table. */
93311388 4161 pst->read_symtab = dwarf2_psymtab_to_symtab;
57349743 4162
dee91e82 4163 per_cu->v.psymtab = pst;
c906108c 4164
dee91e82 4165 dwarf2_find_base_address (comp_unit_die, cu);
d85a05f0 4166
93311388
DE
4167 /* Possibly set the default values of LOWPC and HIGHPC from
4168 `DW_AT_ranges'. */
d85a05f0 4169 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
dee91e82 4170 &best_highpc, cu, pst);
d85a05f0 4171 if (has_pc_info == 1 && best_lowpc < best_highpc)
93311388
DE
4172 /* Store the contiguous range if it is not empty; it can be empty for
4173 CUs with no code. */
4174 addrmap_set_empty (objfile->psymtabs_addrmap,
d85a05f0
DJ
4175 best_lowpc + baseaddr,
4176 best_highpc + baseaddr - 1, pst);
93311388
DE
4177
4178 /* Check if comp unit has_children.
4179 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4180 If not, there's no more debug_info for this comp unit. */
d85a05f0 4181 if (has_children)
93311388
DE
4182 {
4183 struct partial_die_info *first_die;
4184 CORE_ADDR lowpc, highpc;
31ffec48 4185
93311388
DE
4186 lowpc = ((CORE_ADDR) -1);
4187 highpc = ((CORE_ADDR) 0);
c906108c 4188
dee91e82 4189 first_die = load_partial_dies (reader, info_ptr, 1);
c906108c 4190
93311388 4191 scan_partial_symbols (first_die, &lowpc, &highpc,
dee91e82 4192 ! has_pc_info, cu);
57c22c6c 4193
93311388
DE
4194 /* If we didn't find a lowpc, set it to highpc to avoid
4195 complaints from `maint check'. */
4196 if (lowpc == ((CORE_ADDR) -1))
4197 lowpc = highpc;
10b3939b 4198
93311388
DE
4199 /* If the compilation unit didn't have an explicit address range,
4200 then use the information extracted from its child dies. */
d85a05f0 4201 if (! has_pc_info)
93311388 4202 {
d85a05f0
DJ
4203 best_lowpc = lowpc;
4204 best_highpc = highpc;
93311388
DE
4205 }
4206 }
d85a05f0
DJ
4207 pst->textlow = best_lowpc + baseaddr;
4208 pst->texthigh = best_highpc + baseaddr;
c906108c 4209
93311388
DE
4210 pst->n_global_syms = objfile->global_psymbols.next -
4211 (objfile->global_psymbols.list + pst->globals_offset);
4212 pst->n_static_syms = objfile->static_psymbols.next -
4213 (objfile->static_psymbols.list + pst->statics_offset);
4214 sort_pst_symbols (pst);
c906108c 4215
95554aad
TT
4216 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
4217 {
4218 int i;
4219 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4220 struct dwarf2_per_cu_data *iter;
4221
4222 /* Fill in 'dependencies' here; we fill in 'users' in a
4223 post-pass. */
4224 pst->number_of_dependencies = len;
4225 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
4226 len * sizeof (struct symtab *));
4227 for (i = 0;
4228 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4229 i, iter);
4230 ++i)
4231 pst->dependencies[i] = iter->v.psymtab;
4232
4233 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
4234 }
4235
3019eac3 4236 if (per_cu->is_debug_types)
348e048f
DE
4237 {
4238 /* It's not clear we want to do anything with stmt lists here.
4239 Waiting to see what gcc ultimately does. */
4240 }
d85a05f0 4241 else
93311388
DE
4242 {
4243 /* Get the list of files included in the current compilation unit,
4244 and build a psymtab for each of them. */
dee91e82 4245 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
93311388 4246 }
dee91e82 4247}
ae038cb0 4248
dee91e82
DE
4249/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4250 Process compilation unit THIS_CU for a psymtab. */
4251
4252static void
95554aad
TT
4253process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
4254 int want_partial_unit)
dee91e82
DE
4255{
4256 /* If this compilation unit was already read in, free the
4257 cached copy in order to read it in again. This is
4258 necessary because we skipped some symbols when we first
4259 read in the compilation unit (see load_partial_dies).
4260 This problem could be avoided, but the benefit is unclear. */
4261 if (this_cu->cu != NULL)
4262 free_one_cached_comp_unit (this_cu);
4263
3019eac3 4264 gdb_assert (! this_cu->is_debug_types);
fd820528 4265 init_cutu_and_read_dies (this_cu, 0, 0, process_psymtab_comp_unit_reader,
95554aad 4266 &want_partial_unit);
dee91e82
DE
4267
4268 /* Age out any secondary CUs. */
4269 age_cached_comp_units ();
93311388 4270}
ff013f42 4271
348e048f
DE
4272/* Traversal function for htab_traverse_noresize.
4273 Process one .debug_types comp-unit. */
4274
4275static int
dee91e82 4276process_psymtab_type_unit (void **slot, void *info)
348e048f 4277{
dee91e82
DE
4278 struct signatured_type *sig_type = (struct signatured_type *) *slot;
4279 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 4280
fd820528 4281 gdb_assert (per_cu->is_debug_types);
a0f42c21 4282 gdb_assert (info == NULL);
348e048f 4283
dee91e82
DE
4284 /* If this compilation unit was already read in, free the
4285 cached copy in order to read it in again. This is
4286 necessary because we skipped some symbols when we first
4287 read in the compilation unit (see load_partial_dies).
4288 This problem could be avoided, but the benefit is unclear. */
4289 if (per_cu->cu != NULL)
4290 free_one_cached_comp_unit (per_cu);
4291
fd820528
DE
4292 init_cutu_and_read_dies (per_cu, 0, 0, process_psymtab_comp_unit_reader,
4293 NULL);
dee91e82
DE
4294
4295 /* Age out any secondary CUs. */
4296 age_cached_comp_units ();
348e048f
DE
4297
4298 return 1;
4299}
4300
4301/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
4302 Build partial symbol tables for the .debug_types comp-units. */
4303
4304static void
4305build_type_psymtabs (struct objfile *objfile)
4306{
0e50663e 4307 if (! create_all_type_units (objfile))
348e048f
DE
4308 return;
4309
4310 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
dee91e82 4311 process_psymtab_type_unit, NULL);
348e048f
DE
4312}
4313
60606b2c
TT
4314/* A cleanup function that clears objfile's psymtabs_addrmap field. */
4315
4316static void
4317psymtabs_addrmap_cleanup (void *o)
4318{
4319 struct objfile *objfile = o;
ec61707d 4320
60606b2c
TT
4321 objfile->psymtabs_addrmap = NULL;
4322}
4323
95554aad
TT
4324/* Compute the 'user' field for each psymtab in OBJFILE. */
4325
4326static void
4327set_partial_user (struct objfile *objfile)
4328{
4329 int i;
4330
4331 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
4332 {
4333 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
4334 struct partial_symtab *pst = per_cu->v.psymtab;
4335 int j;
4336
4337 for (j = 0; j < pst->number_of_dependencies; ++j)
4338 {
4339 /* Set the 'user' field only if it is not already set. */
4340 if (pst->dependencies[j]->user == NULL)
4341 pst->dependencies[j]->user = pst;
4342 }
4343 }
4344}
4345
93311388
DE
4346/* Build the partial symbol table by doing a quick pass through the
4347 .debug_info and .debug_abbrev sections. */
72bf9492 4348
93311388 4349static void
c67a9c90 4350dwarf2_build_psymtabs_hard (struct objfile *objfile)
93311388 4351{
60606b2c
TT
4352 struct cleanup *back_to, *addrmap_cleanup;
4353 struct obstack temp_obstack;
21b2bd31 4354 int i;
93311388 4355
98bfdba5
PA
4356 dwarf2_per_objfile->reading_partial_symbols = 1;
4357
be391dca 4358 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 4359
93311388
DE
4360 /* Any cached compilation units will be linked by the per-objfile
4361 read_in_chain. Make sure to free them when we're done. */
4362 back_to = make_cleanup (free_cached_comp_units, NULL);
72bf9492 4363
348e048f
DE
4364 build_type_psymtabs (objfile);
4365
93311388 4366 create_all_comp_units (objfile);
c906108c 4367
60606b2c
TT
4368 /* Create a temporary address map on a temporary obstack. We later
4369 copy this to the final obstack. */
4370 obstack_init (&temp_obstack);
4371 make_cleanup_obstack_free (&temp_obstack);
4372 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
4373 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
72bf9492 4374
21b2bd31 4375 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
93311388 4376 {
21b2bd31 4377 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
aaa75496 4378
95554aad 4379 process_psymtab_comp_unit (per_cu, 0);
c906108c 4380 }
ff013f42 4381
95554aad
TT
4382 set_partial_user (objfile);
4383
ff013f42
JK
4384 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
4385 &objfile->objfile_obstack);
60606b2c 4386 discard_cleanups (addrmap_cleanup);
ff013f42 4387
ae038cb0
DJ
4388 do_cleanups (back_to);
4389}
4390
3019eac3 4391/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
4392
4393static void
dee91e82
DE
4394load_partial_comp_unit_reader (const struct die_reader_specs *reader,
4395 gdb_byte *info_ptr,
4396 struct die_info *comp_unit_die,
4397 int has_children,
4398 void *data)
ae038cb0 4399{
dee91e82 4400 struct dwarf2_cu *cu = reader->cu;
ae038cb0 4401
95554aad 4402 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 4403
ae038cb0
DJ
4404 /* Check if comp unit has_children.
4405 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 4406 If not, there's no more debug_info for this comp unit. */
d85a05f0 4407 if (has_children)
dee91e82
DE
4408 load_partial_dies (reader, info_ptr, 0);
4409}
98bfdba5 4410
dee91e82
DE
4411/* Load the partial DIEs for a secondary CU into memory.
4412 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 4413
dee91e82
DE
4414static void
4415load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
4416{
fd820528 4417 init_cutu_and_read_dies (this_cu, 1, 1, load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
4418}
4419
9cdd5dbd
DE
4420/* Create a list of all compilation units in OBJFILE.
4421 This is only done for -readnow and building partial symtabs. */
ae038cb0
DJ
4422
4423static void
4424create_all_comp_units (struct objfile *objfile)
4425{
4426 int n_allocated;
4427 int n_comp_units;
4428 struct dwarf2_per_cu_data **all_comp_units;
be391dca
TT
4429 gdb_byte *info_ptr;
4430
4431 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4432 info_ptr = dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4433
4434 n_comp_units = 0;
4435 n_allocated = 10;
4436 all_comp_units = xmalloc (n_allocated
4437 * sizeof (struct dwarf2_per_cu_data *));
6e70227d 4438
3e43a32a
MS
4439 while (info_ptr < dwarf2_per_objfile->info.buffer
4440 + dwarf2_per_objfile->info.size)
ae038cb0 4441 {
c764a876 4442 unsigned int length, initial_length_size;
ae038cb0 4443 struct dwarf2_per_cu_data *this_cu;
b64f50a1 4444 sect_offset offset;
ae038cb0 4445
b64f50a1 4446 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
ae038cb0
DJ
4447
4448 /* Read just enough information to find out where the next
4449 compilation unit is. */
c764a876
DE
4450 length = read_initial_length (objfile->obfd, info_ptr,
4451 &initial_length_size);
ae038cb0
DJ
4452
4453 /* Save the compilation unit for later lookup. */
4454 this_cu = obstack_alloc (&objfile->objfile_obstack,
4455 sizeof (struct dwarf2_per_cu_data));
4456 memset (this_cu, 0, sizeof (*this_cu));
4457 this_cu->offset = offset;
c764a876 4458 this_cu->length = length + initial_length_size;
9291a0cd 4459 this_cu->objfile = objfile;
3019eac3 4460 this_cu->info_or_types_section = &dwarf2_per_objfile->info;
ae038cb0
DJ
4461
4462 if (n_comp_units == n_allocated)
4463 {
4464 n_allocated *= 2;
4465 all_comp_units = xrealloc (all_comp_units,
4466 n_allocated
4467 * sizeof (struct dwarf2_per_cu_data *));
4468 }
4469 all_comp_units[n_comp_units++] = this_cu;
4470
4471 info_ptr = info_ptr + this_cu->length;
4472 }
4473
4474 dwarf2_per_objfile->all_comp_units
4475 = obstack_alloc (&objfile->objfile_obstack,
4476 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4477 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
4478 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
4479 xfree (all_comp_units);
4480 dwarf2_per_objfile->n_comp_units = n_comp_units;
c906108c
SS
4481}
4482
5734ee8b
DJ
4483/* Process all loaded DIEs for compilation unit CU, starting at
4484 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
4485 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
4486 DW_AT_ranges). If NEED_PC is set, then this function will set
4487 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
4488 and record the covered ranges in the addrmap. */
c906108c 4489
72bf9492
DJ
4490static void
4491scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
5734ee8b 4492 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
c906108c 4493{
72bf9492 4494 struct partial_die_info *pdi;
c906108c 4495
91c24f0a
DC
4496 /* Now, march along the PDI's, descending into ones which have
4497 interesting children but skipping the children of the other ones,
4498 until we reach the end of the compilation unit. */
c906108c 4499
72bf9492 4500 pdi = first_die;
91c24f0a 4501
72bf9492
DJ
4502 while (pdi != NULL)
4503 {
4504 fixup_partial_die (pdi, cu);
c906108c 4505
f55ee35c 4506 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
4507 children, so we need to look at them. Ditto for anonymous
4508 enums. */
933c6fe4 4509
72bf9492 4510 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad
TT
4511 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
4512 || pdi->tag == DW_TAG_imported_unit)
c906108c 4513 {
72bf9492 4514 switch (pdi->tag)
c906108c
SS
4515 {
4516 case DW_TAG_subprogram:
5734ee8b 4517 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
c906108c 4518 break;
72929c62 4519 case DW_TAG_constant:
c906108c
SS
4520 case DW_TAG_variable:
4521 case DW_TAG_typedef:
91c24f0a 4522 case DW_TAG_union_type:
72bf9492 4523 if (!pdi->is_declaration)
63d06c5c 4524 {
72bf9492 4525 add_partial_symbol (pdi, cu);
63d06c5c
DC
4526 }
4527 break;
c906108c 4528 case DW_TAG_class_type:
680b30c7 4529 case DW_TAG_interface_type:
c906108c 4530 case DW_TAG_structure_type:
72bf9492 4531 if (!pdi->is_declaration)
c906108c 4532 {
72bf9492 4533 add_partial_symbol (pdi, cu);
c906108c
SS
4534 }
4535 break;
91c24f0a 4536 case DW_TAG_enumeration_type:
72bf9492
DJ
4537 if (!pdi->is_declaration)
4538 add_partial_enumeration (pdi, cu);
c906108c
SS
4539 break;
4540 case DW_TAG_base_type:
a02abb62 4541 case DW_TAG_subrange_type:
c906108c 4542 /* File scope base type definitions are added to the partial
c5aa993b 4543 symbol table. */
72bf9492 4544 add_partial_symbol (pdi, cu);
c906108c 4545 break;
d9fa45fe 4546 case DW_TAG_namespace:
5734ee8b 4547 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
91c24f0a 4548 break;
5d7cb8df
JK
4549 case DW_TAG_module:
4550 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
4551 break;
95554aad
TT
4552 case DW_TAG_imported_unit:
4553 {
4554 struct dwarf2_per_cu_data *per_cu;
4555
4556 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
4557 cu->objfile);
4558
4559 /* Go read the partial unit, if needed. */
4560 if (per_cu->v.psymtab == NULL)
4561 process_psymtab_comp_unit (per_cu, 1);
4562
4563 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
4564 per_cu);
4565 }
4566 break;
c906108c
SS
4567 default:
4568 break;
4569 }
4570 }
4571
72bf9492
DJ
4572 /* If the die has a sibling, skip to the sibling. */
4573
4574 pdi = pdi->die_sibling;
4575 }
4576}
4577
4578/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 4579
72bf9492 4580 Normally, this is simple. For C++, the parent DIE's fully scoped
987504bb
JJ
4581 name is concatenated with "::" and the partial DIE's name. For
4582 Java, the same thing occurs except that "." is used instead of "::".
72bf9492
DJ
4583 Enumerators are an exception; they use the scope of their parent
4584 enumeration type, i.e. the name of the enumeration type is not
4585 prepended to the enumerator.
91c24f0a 4586
72bf9492
DJ
4587 There are two complexities. One is DW_AT_specification; in this
4588 case "parent" means the parent of the target of the specification,
4589 instead of the direct parent of the DIE. The other is compilers
4590 which do not emit DW_TAG_namespace; in this case we try to guess
4591 the fully qualified name of structure types from their members'
4592 linkage names. This must be done using the DIE's children rather
4593 than the children of any DW_AT_specification target. We only need
4594 to do this for structures at the top level, i.e. if the target of
4595 any DW_AT_specification (if any; otherwise the DIE itself) does not
4596 have a parent. */
4597
4598/* Compute the scope prefix associated with PDI's parent, in
4599 compilation unit CU. The result will be allocated on CU's
4600 comp_unit_obstack, or a copy of the already allocated PDI->NAME
4601 field. NULL is returned if no prefix is necessary. */
4602static char *
4603partial_die_parent_scope (struct partial_die_info *pdi,
4604 struct dwarf2_cu *cu)
4605{
4606 char *grandparent_scope;
4607 struct partial_die_info *parent, *real_pdi;
91c24f0a 4608
72bf9492
DJ
4609 /* We need to look at our parent DIE; if we have a DW_AT_specification,
4610 then this means the parent of the specification DIE. */
4611
4612 real_pdi = pdi;
72bf9492 4613 while (real_pdi->has_specification)
10b3939b 4614 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
72bf9492
DJ
4615
4616 parent = real_pdi->die_parent;
4617 if (parent == NULL)
4618 return NULL;
4619
4620 if (parent->scope_set)
4621 return parent->scope;
4622
4623 fixup_partial_die (parent, cu);
4624
10b3939b 4625 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 4626
acebe513
UW
4627 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
4628 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
4629 Work around this problem here. */
4630 if (cu->language == language_cplus
6e70227d 4631 && parent->tag == DW_TAG_namespace
acebe513
UW
4632 && strcmp (parent->name, "::") == 0
4633 && grandparent_scope == NULL)
4634 {
4635 parent->scope = NULL;
4636 parent->scope_set = 1;
4637 return NULL;
4638 }
4639
9c6c53f7
SA
4640 if (pdi->tag == DW_TAG_enumerator)
4641 /* Enumerators should not get the name of the enumeration as a prefix. */
4642 parent->scope = grandparent_scope;
4643 else if (parent->tag == DW_TAG_namespace
f55ee35c 4644 || parent->tag == DW_TAG_module
72bf9492
DJ
4645 || parent->tag == DW_TAG_structure_type
4646 || parent->tag == DW_TAG_class_type
680b30c7 4647 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
4648 || parent->tag == DW_TAG_union_type
4649 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
4650 {
4651 if (grandparent_scope == NULL)
4652 parent->scope = parent->name;
4653 else
3e43a32a
MS
4654 parent->scope = typename_concat (&cu->comp_unit_obstack,
4655 grandparent_scope,
f55ee35c 4656 parent->name, 0, cu);
72bf9492 4657 }
72bf9492
DJ
4658 else
4659 {
4660 /* FIXME drow/2004-04-01: What should we be doing with
4661 function-local names? For partial symbols, we should probably be
4662 ignoring them. */
4663 complaint (&symfile_complaints,
e2e0b3e5 4664 _("unhandled containing DIE tag %d for DIE at %d"),
b64f50a1 4665 parent->tag, pdi->offset.sect_off);
72bf9492 4666 parent->scope = grandparent_scope;
c906108c
SS
4667 }
4668
72bf9492
DJ
4669 parent->scope_set = 1;
4670 return parent->scope;
4671}
4672
4673/* Return the fully scoped name associated with PDI, from compilation unit
4674 CU. The result will be allocated with malloc. */
4568ecf9 4675
72bf9492
DJ
4676static char *
4677partial_die_full_name (struct partial_die_info *pdi,
4678 struct dwarf2_cu *cu)
4679{
4680 char *parent_scope;
4681
98bfdba5
PA
4682 /* If this is a template instantiation, we can not work out the
4683 template arguments from partial DIEs. So, unfortunately, we have
4684 to go through the full DIEs. At least any work we do building
4685 types here will be reused if full symbols are loaded later. */
4686 if (pdi->has_template_arguments)
4687 {
4688 fixup_partial_die (pdi, cu);
4689
4690 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4691 {
4692 struct die_info *die;
4693 struct attribute attr;
4694 struct dwarf2_cu *ref_cu = cu;
4695
b64f50a1 4696 /* DW_FORM_ref_addr is using section offset. */
98bfdba5
PA
4697 attr.name = 0;
4698 attr.form = DW_FORM_ref_addr;
4568ecf9 4699 attr.u.unsnd = pdi->offset.sect_off;
98bfdba5
PA
4700 die = follow_die_ref (NULL, &attr, &ref_cu);
4701
4702 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4703 }
4704 }
4705
72bf9492
DJ
4706 parent_scope = partial_die_parent_scope (pdi, cu);
4707 if (parent_scope == NULL)
4708 return NULL;
4709 else
f55ee35c 4710 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
4711}
4712
4713static void
72bf9492 4714add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 4715{
e7c27a73 4716 struct objfile *objfile = cu->objfile;
c906108c 4717 CORE_ADDR addr = 0;
decbce07 4718 char *actual_name = NULL;
e142c38c 4719 CORE_ADDR baseaddr;
72bf9492 4720 int built_actual_name = 0;
e142c38c
DJ
4721
4722 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 4723
94af9270
KS
4724 actual_name = partial_die_full_name (pdi, cu);
4725 if (actual_name)
4726 built_actual_name = 1;
63d06c5c 4727
72bf9492
DJ
4728 if (actual_name == NULL)
4729 actual_name = pdi->name;
4730
c906108c
SS
4731 switch (pdi->tag)
4732 {
4733 case DW_TAG_subprogram:
2cfa0c8d 4734 if (pdi->is_external || cu->language == language_ada)
c906108c 4735 {
2cfa0c8d
JB
4736 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4737 of the global scope. But in Ada, we want to be able to access
4738 nested procedures globally. So all Ada subprograms are stored
4739 in the global scope. */
f47fb265 4740 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4741 mst_text, objfile); */
f47fb265
MS
4742 add_psymbol_to_list (actual_name, strlen (actual_name),
4743 built_actual_name,
4744 VAR_DOMAIN, LOC_BLOCK,
4745 &objfile->global_psymbols,
4746 0, pdi->lowpc + baseaddr,
4747 cu->language, objfile);
c906108c
SS
4748 }
4749 else
4750 {
f47fb265 4751 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
c5aa993b 4752 mst_file_text, objfile); */
f47fb265
MS
4753 add_psymbol_to_list (actual_name, strlen (actual_name),
4754 built_actual_name,
4755 VAR_DOMAIN, LOC_BLOCK,
4756 &objfile->static_psymbols,
4757 0, pdi->lowpc + baseaddr,
4758 cu->language, objfile);
c906108c
SS
4759 }
4760 break;
72929c62
JB
4761 case DW_TAG_constant:
4762 {
4763 struct psymbol_allocation_list *list;
4764
4765 if (pdi->is_external)
4766 list = &objfile->global_psymbols;
4767 else
4768 list = &objfile->static_psymbols;
f47fb265
MS
4769 add_psymbol_to_list (actual_name, strlen (actual_name),
4770 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4771 list, 0, 0, cu->language, objfile);
72929c62
JB
4772 }
4773 break;
c906108c 4774 case DW_TAG_variable:
95554aad
TT
4775 if (pdi->d.locdesc)
4776 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 4777
95554aad 4778 if (pdi->d.locdesc
caac4577
JG
4779 && addr == 0
4780 && !dwarf2_per_objfile->has_section_at_zero)
4781 {
4782 /* A global or static variable may also have been stripped
4783 out by the linker if unused, in which case its address
4784 will be nullified; do not add such variables into partial
4785 symbol table then. */
4786 }
4787 else if (pdi->is_external)
c906108c
SS
4788 {
4789 /* Global Variable.
4790 Don't enter into the minimal symbol tables as there is
4791 a minimal symbol table entry from the ELF symbols already.
4792 Enter into partial symbol table if it has a location
4793 descriptor or a type.
4794 If the location descriptor is missing, new_symbol will create
4795 a LOC_UNRESOLVED symbol, the address of the variable will then
4796 be determined from the minimal symbol table whenever the variable
4797 is referenced.
4798 The address for the partial symbol table entry is not
4799 used by GDB, but it comes in handy for debugging partial symbol
4800 table building. */
4801
95554aad 4802 if (pdi->d.locdesc || pdi->has_type)
f47fb265
MS
4803 add_psymbol_to_list (actual_name, strlen (actual_name),
4804 built_actual_name,
4805 VAR_DOMAIN, LOC_STATIC,
4806 &objfile->global_psymbols,
4807 0, addr + baseaddr,
4808 cu->language, objfile);
c906108c
SS
4809 }
4810 else
4811 {
0963b4bd 4812 /* Static Variable. Skip symbols without location descriptors. */
95554aad 4813 if (pdi->d.locdesc == NULL)
decbce07
MS
4814 {
4815 if (built_actual_name)
4816 xfree (actual_name);
4817 return;
4818 }
f47fb265 4819 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
c5aa993b 4820 mst_file_data, objfile); */
f47fb265
MS
4821 add_psymbol_to_list (actual_name, strlen (actual_name),
4822 built_actual_name,
4823 VAR_DOMAIN, LOC_STATIC,
4824 &objfile->static_psymbols,
4825 0, addr + baseaddr,
4826 cu->language, objfile);
c906108c
SS
4827 }
4828 break;
4829 case DW_TAG_typedef:
4830 case DW_TAG_base_type:
a02abb62 4831 case DW_TAG_subrange_type:
38d518c9 4832 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4833 built_actual_name,
176620f1 4834 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 4835 &objfile->static_psymbols,
e142c38c 4836 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4837 break;
72bf9492
DJ
4838 case DW_TAG_namespace:
4839 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4840 built_actual_name,
72bf9492
DJ
4841 VAR_DOMAIN, LOC_TYPEDEF,
4842 &objfile->global_psymbols,
4843 0, (CORE_ADDR) 0, cu->language, objfile);
4844 break;
c906108c 4845 case DW_TAG_class_type:
680b30c7 4846 case DW_TAG_interface_type:
c906108c
SS
4847 case DW_TAG_structure_type:
4848 case DW_TAG_union_type:
4849 case DW_TAG_enumeration_type:
fa4028e9
JB
4850 /* Skip external references. The DWARF standard says in the section
4851 about "Structure, Union, and Class Type Entries": "An incomplete
4852 structure, union or class type is represented by a structure,
4853 union or class entry that does not have a byte size attribute
4854 and that has a DW_AT_declaration attribute." */
4855 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07
MS
4856 {
4857 if (built_actual_name)
4858 xfree (actual_name);
4859 return;
4860 }
fa4028e9 4861
63d06c5c
DC
4862 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4863 static vs. global. */
38d518c9 4864 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4865 built_actual_name,
176620f1 4866 STRUCT_DOMAIN, LOC_TYPEDEF,
987504bb
JJ
4867 (cu->language == language_cplus
4868 || cu->language == language_java)
63d06c5c
DC
4869 ? &objfile->global_psymbols
4870 : &objfile->static_psymbols,
e142c38c 4871 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c 4872
c906108c
SS
4873 break;
4874 case DW_TAG_enumerator:
38d518c9 4875 add_psymbol_to_list (actual_name, strlen (actual_name),
04a679b8 4876 built_actual_name,
176620f1 4877 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
4878 (cu->language == language_cplus
4879 || cu->language == language_java)
f6fe98ef
DJ
4880 ? &objfile->global_psymbols
4881 : &objfile->static_psymbols,
e142c38c 4882 0, (CORE_ADDR) 0, cu->language, objfile);
c906108c
SS
4883 break;
4884 default:
4885 break;
4886 }
5c4e30ca 4887
72bf9492
DJ
4888 if (built_actual_name)
4889 xfree (actual_name);
c906108c
SS
4890}
4891
5c4e30ca
DC
4892/* Read a partial die corresponding to a namespace; also, add a symbol
4893 corresponding to that namespace to the symbol table. NAMESPACE is
4894 the name of the enclosing namespace. */
91c24f0a 4895
72bf9492
DJ
4896static void
4897add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 4898 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4899 int need_pc, struct dwarf2_cu *cu)
91c24f0a 4900{
72bf9492 4901 /* Add a symbol for the namespace. */
e7c27a73 4902
72bf9492 4903 add_partial_symbol (pdi, cu);
5c4e30ca
DC
4904
4905 /* Now scan partial symbols in that namespace. */
4906
91c24f0a 4907 if (pdi->has_children)
5734ee8b 4908 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
91c24f0a
DC
4909}
4910
5d7cb8df
JK
4911/* Read a partial die corresponding to a Fortran module. */
4912
4913static void
4914add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4915 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4916{
f55ee35c 4917 /* Now scan partial symbols in that module. */
5d7cb8df
JK
4918
4919 if (pdi->has_children)
4920 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4921}
4922
bc30ff58
JB
4923/* Read a partial die corresponding to a subprogram and create a partial
4924 symbol for that subprogram. When the CU language allows it, this
4925 routine also defines a partial symbol for each nested subprogram
4926 that this subprogram contains.
6e70227d 4927
bc30ff58
JB
4928 DIE my also be a lexical block, in which case we simply search
4929 recursively for suprograms defined inside that lexical block.
4930 Again, this is only performed when the CU language allows this
4931 type of definitions. */
4932
4933static void
4934add_partial_subprogram (struct partial_die_info *pdi,
4935 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 4936 int need_pc, struct dwarf2_cu *cu)
bc30ff58
JB
4937{
4938 if (pdi->tag == DW_TAG_subprogram)
4939 {
4940 if (pdi->has_pc_info)
4941 {
4942 if (pdi->lowpc < *lowpc)
4943 *lowpc = pdi->lowpc;
4944 if (pdi->highpc > *highpc)
4945 *highpc = pdi->highpc;
5734ee8b
DJ
4946 if (need_pc)
4947 {
4948 CORE_ADDR baseaddr;
4949 struct objfile *objfile = cu->objfile;
4950
4951 baseaddr = ANOFFSET (objfile->section_offsets,
4952 SECT_OFF_TEXT (objfile));
4953 addrmap_set_empty (objfile->psymtabs_addrmap,
01637564
DE
4954 pdi->lowpc + baseaddr,
4955 pdi->highpc - 1 + baseaddr,
9291a0cd 4956 cu->per_cu->v.psymtab);
5734ee8b 4957 }
481860b3
GB
4958 }
4959
4960 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4961 {
bc30ff58 4962 if (!pdi->is_declaration)
e8d05480
JB
4963 /* Ignore subprogram DIEs that do not have a name, they are
4964 illegal. Do not emit a complaint at this point, we will
4965 do so when we convert this psymtab into a symtab. */
4966 if (pdi->name)
4967 add_partial_symbol (pdi, cu);
bc30ff58
JB
4968 }
4969 }
6e70227d 4970
bc30ff58
JB
4971 if (! pdi->has_children)
4972 return;
4973
4974 if (cu->language == language_ada)
4975 {
4976 pdi = pdi->die_child;
4977 while (pdi != NULL)
4978 {
4979 fixup_partial_die (pdi, cu);
4980 if (pdi->tag == DW_TAG_subprogram
4981 || pdi->tag == DW_TAG_lexical_block)
5734ee8b 4982 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
bc30ff58
JB
4983 pdi = pdi->die_sibling;
4984 }
4985 }
4986}
4987
91c24f0a
DC
4988/* Read a partial die corresponding to an enumeration type. */
4989
72bf9492
DJ
4990static void
4991add_partial_enumeration (struct partial_die_info *enum_pdi,
4992 struct dwarf2_cu *cu)
91c24f0a 4993{
72bf9492 4994 struct partial_die_info *pdi;
91c24f0a
DC
4995
4996 if (enum_pdi->name != NULL)
72bf9492
DJ
4997 add_partial_symbol (enum_pdi, cu);
4998
4999 pdi = enum_pdi->die_child;
5000 while (pdi)
91c24f0a 5001 {
72bf9492 5002 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
e2e0b3e5 5003 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
91c24f0a 5004 else
72bf9492
DJ
5005 add_partial_symbol (pdi, cu);
5006 pdi = pdi->die_sibling;
91c24f0a 5007 }
91c24f0a
DC
5008}
5009
6caca83c
CC
5010/* Return the initial uleb128 in the die at INFO_PTR. */
5011
5012static unsigned int
5013peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
5014{
5015 unsigned int bytes_read;
5016
5017 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5018}
5019
4bb7a0a7
DJ
5020/* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
5021 Return the corresponding abbrev, or NULL if the number is zero (indicating
5022 an empty DIE). In either case *BYTES_READ will be set to the length of
5023 the initial number. */
5024
5025static struct abbrev_info *
fe1b8b76 5026peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
891d2f0b 5027 struct dwarf2_cu *cu)
4bb7a0a7
DJ
5028{
5029 bfd *abfd = cu->objfile->obfd;
5030 unsigned int abbrev_number;
5031 struct abbrev_info *abbrev;
5032
5033 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
5034
5035 if (abbrev_number == 0)
5036 return NULL;
5037
5038 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
5039 if (!abbrev)
5040 {
3e43a32a
MS
5041 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
5042 abbrev_number, bfd_get_filename (abfd));
4bb7a0a7
DJ
5043 }
5044
5045 return abbrev;
5046}
5047
93311388
DE
5048/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5049 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
5050 DIE. Any children of the skipped DIEs will also be skipped. */
5051
fe1b8b76 5052static gdb_byte *
dee91e82 5053skip_children (const struct die_reader_specs *reader, gdb_byte *info_ptr)
4bb7a0a7 5054{
dee91e82 5055 struct dwarf2_cu *cu = reader->cu;
4bb7a0a7
DJ
5056 struct abbrev_info *abbrev;
5057 unsigned int bytes_read;
5058
5059 while (1)
5060 {
5061 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
5062 if (abbrev == NULL)
5063 return info_ptr + bytes_read;
5064 else
dee91e82 5065 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
5066 }
5067}
5068
93311388
DE
5069/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
5070 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
5071 abbrev corresponding to that skipped uleb128 should be passed in
5072 ABBREV. Returns a pointer to this DIE's sibling, skipping any
5073 children. */
5074
fe1b8b76 5075static gdb_byte *
dee91e82
DE
5076skip_one_die (const struct die_reader_specs *reader, gdb_byte *info_ptr,
5077 struct abbrev_info *abbrev)
4bb7a0a7
DJ
5078{
5079 unsigned int bytes_read;
5080 struct attribute attr;
dee91e82
DE
5081 bfd *abfd = reader->abfd;
5082 struct dwarf2_cu *cu = reader->cu;
5083 gdb_byte *buffer = reader->buffer;
f664829e
DE
5084 const gdb_byte *buffer_end = reader->buffer_end;
5085 gdb_byte *start_info_ptr = info_ptr;
4bb7a0a7
DJ
5086 unsigned int form, i;
5087
5088 for (i = 0; i < abbrev->num_attrs; i++)
5089 {
5090 /* The only abbrev we care about is DW_AT_sibling. */
5091 if (abbrev->attrs[i].name == DW_AT_sibling)
5092 {
dee91e82 5093 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 5094 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
5095 complaint (&symfile_complaints,
5096 _("ignoring absolute DW_AT_sibling"));
4bb7a0a7 5097 else
b64f50a1 5098 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4bb7a0a7
DJ
5099 }
5100
5101 /* If it isn't DW_AT_sibling, skip this attribute. */
5102 form = abbrev->attrs[i].form;
5103 skip_attribute:
5104 switch (form)
5105 {
4bb7a0a7 5106 case DW_FORM_ref_addr:
ae411497
TT
5107 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
5108 and later it is offset sized. */
5109 if (cu->header.version == 2)
5110 info_ptr += cu->header.addr_size;
5111 else
5112 info_ptr += cu->header.offset_size;
5113 break;
5114 case DW_FORM_addr:
4bb7a0a7
DJ
5115 info_ptr += cu->header.addr_size;
5116 break;
5117 case DW_FORM_data1:
5118 case DW_FORM_ref1:
5119 case DW_FORM_flag:
5120 info_ptr += 1;
5121 break;
2dc7f7b3
TT
5122 case DW_FORM_flag_present:
5123 break;
4bb7a0a7
DJ
5124 case DW_FORM_data2:
5125 case DW_FORM_ref2:
5126 info_ptr += 2;
5127 break;
5128 case DW_FORM_data4:
5129 case DW_FORM_ref4:
5130 info_ptr += 4;
5131 break;
5132 case DW_FORM_data8:
5133 case DW_FORM_ref8:
55f1336d 5134 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
5135 info_ptr += 8;
5136 break;
5137 case DW_FORM_string:
9b1c24c8 5138 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
5139 info_ptr += bytes_read;
5140 break;
2dc7f7b3 5141 case DW_FORM_sec_offset:
4bb7a0a7
DJ
5142 case DW_FORM_strp:
5143 info_ptr += cu->header.offset_size;
5144 break;
2dc7f7b3 5145 case DW_FORM_exprloc:
4bb7a0a7
DJ
5146 case DW_FORM_block:
5147 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5148 info_ptr += bytes_read;
5149 break;
5150 case DW_FORM_block1:
5151 info_ptr += 1 + read_1_byte (abfd, info_ptr);
5152 break;
5153 case DW_FORM_block2:
5154 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
5155 break;
5156 case DW_FORM_block4:
5157 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
5158 break;
5159 case DW_FORM_sdata:
5160 case DW_FORM_udata:
5161 case DW_FORM_ref_udata:
3019eac3
DE
5162 case DW_FORM_GNU_addr_index:
5163 case DW_FORM_GNU_str_index:
f664829e 5164 info_ptr = (gdb_byte *) safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
5165 break;
5166 case DW_FORM_indirect:
5167 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
5168 info_ptr += bytes_read;
5169 /* We need to continue parsing from here, so just go back to
5170 the top. */
5171 goto skip_attribute;
5172
5173 default:
3e43a32a
MS
5174 error (_("Dwarf Error: Cannot handle %s "
5175 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
5176 dwarf_form_name (form),
5177 bfd_get_filename (abfd));
5178 }
5179 }
5180
5181 if (abbrev->has_children)
dee91e82 5182 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
5183 else
5184 return info_ptr;
5185}
5186
93311388 5187/* Locate ORIG_PDI's sibling.
dee91e82 5188 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 5189
fe1b8b76 5190static gdb_byte *
dee91e82
DE
5191locate_pdi_sibling (const struct die_reader_specs *reader,
5192 struct partial_die_info *orig_pdi,
5193 gdb_byte *info_ptr)
91c24f0a
DC
5194{
5195 /* Do we know the sibling already? */
72bf9492 5196
91c24f0a
DC
5197 if (orig_pdi->sibling)
5198 return orig_pdi->sibling;
5199
5200 /* Are there any children to deal with? */
5201
5202 if (!orig_pdi->has_children)
5203 return info_ptr;
5204
4bb7a0a7 5205 /* Skip the children the long way. */
91c24f0a 5206
dee91e82 5207 return skip_children (reader, info_ptr);
91c24f0a
DC
5208}
5209
c906108c
SS
5210/* Expand this partial symbol table into a full symbol table. */
5211
5212static void
fba45db2 5213dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
c906108c 5214{
c906108c
SS
5215 if (pst != NULL)
5216 {
5217 if (pst->readin)
5218 {
3e43a32a
MS
5219 warning (_("bug: psymtab for %s is already read in."),
5220 pst->filename);
c906108c
SS
5221 }
5222 else
5223 {
5224 if (info_verbose)
5225 {
3e43a32a
MS
5226 printf_filtered (_("Reading in symbols for %s..."),
5227 pst->filename);
c906108c
SS
5228 gdb_flush (gdb_stdout);
5229 }
5230
10b3939b
DJ
5231 /* Restore our global data. */
5232 dwarf2_per_objfile = objfile_data (pst->objfile,
5233 dwarf2_objfile_data_key);
5234
b2ab525c
KB
5235 /* If this psymtab is constructed from a debug-only objfile, the
5236 has_section_at_zero flag will not necessarily be correct. We
5237 can get the correct value for this flag by looking at the data
5238 associated with the (presumably stripped) associated objfile. */
5239 if (pst->objfile->separate_debug_objfile_backlink)
5240 {
5241 struct dwarf2_per_objfile *dpo_backlink
5242 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
5243 dwarf2_objfile_data_key);
9a619af0 5244
b2ab525c
KB
5245 dwarf2_per_objfile->has_section_at_zero
5246 = dpo_backlink->has_section_at_zero;
5247 }
5248
98bfdba5
PA
5249 dwarf2_per_objfile->reading_partial_symbols = 0;
5250
c906108c
SS
5251 psymtab_to_symtab_1 (pst);
5252
5253 /* Finish up the debug error message. */
5254 if (info_verbose)
a3f17187 5255 printf_filtered (_("done.\n"));
c906108c
SS
5256 }
5257 }
95554aad
TT
5258
5259 process_cu_includes ();
c906108c 5260}
9cdd5dbd
DE
5261\f
5262/* Reading in full CUs. */
c906108c 5263
10b3939b
DJ
5264/* Add PER_CU to the queue. */
5265
5266static void
95554aad
TT
5267queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
5268 enum language pretend_language)
10b3939b
DJ
5269{
5270 struct dwarf2_queue_item *item;
5271
5272 per_cu->queued = 1;
5273 item = xmalloc (sizeof (*item));
5274 item->per_cu = per_cu;
95554aad 5275 item->pretend_language = pretend_language;
10b3939b
DJ
5276 item->next = NULL;
5277
5278 if (dwarf2_queue == NULL)
5279 dwarf2_queue = item;
5280 else
5281 dwarf2_queue_tail->next = item;
5282
5283 dwarf2_queue_tail = item;
5284}
5285
5286/* Process the queue. */
5287
5288static void
a0f42c21 5289process_queue (void)
10b3939b
DJ
5290{
5291 struct dwarf2_queue_item *item, *next_item;
5292
03dd20cc
DJ
5293 /* The queue starts out with one item, but following a DIE reference
5294 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
5295 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
5296 {
9291a0cd
TT
5297 if (dwarf2_per_objfile->using_index
5298 ? !item->per_cu->v.quick->symtab
5299 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
95554aad 5300 process_full_comp_unit (item->per_cu, item->pretend_language);
10b3939b
DJ
5301
5302 item->per_cu->queued = 0;
5303 next_item = item->next;
5304 xfree (item);
5305 }
5306
5307 dwarf2_queue_tail = NULL;
5308}
5309
5310/* Free all allocated queue entries. This function only releases anything if
5311 an error was thrown; if the queue was processed then it would have been
5312 freed as we went along. */
5313
5314static void
5315dwarf2_release_queue (void *dummy)
5316{
5317 struct dwarf2_queue_item *item, *last;
5318
5319 item = dwarf2_queue;
5320 while (item)
5321 {
5322 /* Anything still marked queued is likely to be in an
5323 inconsistent state, so discard it. */
5324 if (item->per_cu->queued)
5325 {
5326 if (item->per_cu->cu != NULL)
dee91e82 5327 free_one_cached_comp_unit (item->per_cu);
10b3939b
DJ
5328 item->per_cu->queued = 0;
5329 }
5330
5331 last = item;
5332 item = item->next;
5333 xfree (last);
5334 }
5335
5336 dwarf2_queue = dwarf2_queue_tail = NULL;
5337}
5338
5339/* Read in full symbols for PST, and anything it depends on. */
5340
c906108c 5341static void
fba45db2 5342psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 5343{
10b3939b 5344 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
5345 int i;
5346
95554aad
TT
5347 if (pst->readin)
5348 return;
5349
aaa75496 5350 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
5351 if (!pst->dependencies[i]->readin
5352 && pst->dependencies[i]->user == NULL)
aaa75496
JB
5353 {
5354 /* Inform about additional files that need to be read in. */
5355 if (info_verbose)
5356 {
a3f17187 5357 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
5358 fputs_filtered (" ", gdb_stdout);
5359 wrap_here ("");
5360 fputs_filtered ("and ", gdb_stdout);
5361 wrap_here ("");
5362 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 5363 wrap_here (""); /* Flush output. */
aaa75496
JB
5364 gdb_flush (gdb_stdout);
5365 }
5366 psymtab_to_symtab_1 (pst->dependencies[i]);
5367 }
5368
e38df1d0 5369 per_cu = pst->read_symtab_private;
10b3939b
DJ
5370
5371 if (per_cu == NULL)
aaa75496
JB
5372 {
5373 /* It's an include file, no symbols to read for it.
5374 Everything is in the parent symtab. */
5375 pst->readin = 1;
5376 return;
5377 }
c906108c 5378
a0f42c21 5379 dw2_do_instantiate_symtab (per_cu);
10b3939b
DJ
5380}
5381
dee91e82
DE
5382/* Trivial hash function for die_info: the hash value of a DIE
5383 is its offset in .debug_info for this objfile. */
10b3939b 5384
dee91e82
DE
5385static hashval_t
5386die_hash (const void *item)
10b3939b 5387{
dee91e82 5388 const struct die_info *die = item;
6502dd73 5389
dee91e82
DE
5390 return die->offset.sect_off;
5391}
63d06c5c 5392
dee91e82
DE
5393/* Trivial comparison function for die_info structures: two DIEs
5394 are equal if they have the same offset. */
98bfdba5 5395
dee91e82
DE
5396static int
5397die_eq (const void *item_lhs, const void *item_rhs)
5398{
5399 const struct die_info *die_lhs = item_lhs;
5400 const struct die_info *die_rhs = item_rhs;
c906108c 5401
dee91e82
DE
5402 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
5403}
c906108c 5404
dee91e82
DE
5405/* die_reader_func for load_full_comp_unit.
5406 This is identical to read_signatured_type_reader,
5407 but is kept separate for now. */
c906108c 5408
dee91e82
DE
5409static void
5410load_full_comp_unit_reader (const struct die_reader_specs *reader,
5411 gdb_byte *info_ptr,
5412 struct die_info *comp_unit_die,
5413 int has_children,
5414 void *data)
5415{
5416 struct dwarf2_cu *cu = reader->cu;
95554aad 5417 enum language *language_ptr = data;
6caca83c 5418
dee91e82
DE
5419 gdb_assert (cu->die_hash == NULL);
5420 cu->die_hash =
5421 htab_create_alloc_ex (cu->header.length / 12,
5422 die_hash,
5423 die_eq,
5424 NULL,
5425 &cu->comp_unit_obstack,
5426 hashtab_obstack_allocate,
5427 dummy_obstack_deallocate);
e142c38c 5428
dee91e82
DE
5429 if (has_children)
5430 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
5431 &info_ptr, comp_unit_die);
5432 cu->dies = comp_unit_die;
5433 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
5434
5435 /* We try not to read any attributes in this function, because not
9cdd5dbd 5436 all CUs needed for references have been loaded yet, and symbol
10b3939b 5437 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
5438 or we won't be able to build types correctly.
5439 Similarly, if we do not read the producer, we can not apply
5440 producer-specific interpretation. */
95554aad 5441 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 5442}
10b3939b 5443
dee91e82 5444/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 5445
dee91e82 5446static void
95554aad
TT
5447load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
5448 enum language pretend_language)
dee91e82 5449{
3019eac3 5450 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 5451
95554aad
TT
5452 init_cutu_and_read_dies (this_cu, 1, 1, load_full_comp_unit_reader,
5453 &pretend_language);
10b3939b
DJ
5454}
5455
3da10d80
KS
5456/* Add a DIE to the delayed physname list. */
5457
5458static void
5459add_to_method_list (struct type *type, int fnfield_index, int index,
5460 const char *name, struct die_info *die,
5461 struct dwarf2_cu *cu)
5462{
5463 struct delayed_method_info mi;
5464 mi.type = type;
5465 mi.fnfield_index = fnfield_index;
5466 mi.index = index;
5467 mi.name = name;
5468 mi.die = die;
5469 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
5470}
5471
5472/* A cleanup for freeing the delayed method list. */
5473
5474static void
5475free_delayed_list (void *ptr)
5476{
5477 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
5478 if (cu->method_list != NULL)
5479 {
5480 VEC_free (delayed_method_info, cu->method_list);
5481 cu->method_list = NULL;
5482 }
5483}
5484
5485/* Compute the physnames of any methods on the CU's method list.
5486
5487 The computation of method physnames is delayed in order to avoid the
5488 (bad) condition that one of the method's formal parameters is of an as yet
5489 incomplete type. */
5490
5491static void
5492compute_delayed_physnames (struct dwarf2_cu *cu)
5493{
5494 int i;
5495 struct delayed_method_info *mi;
5496 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
5497 {
1d06ead6 5498 const char *physname;
3da10d80
KS
5499 struct fn_fieldlist *fn_flp
5500 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
1d06ead6 5501 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
3da10d80
KS
5502 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
5503 }
5504}
5505
a766d390
DE
5506/* Go objects should be embedded in a DW_TAG_module DIE,
5507 and it's not clear if/how imported objects will appear.
5508 To keep Go support simple until that's worked out,
5509 go back through what we've read and create something usable.
5510 We could do this while processing each DIE, and feels kinda cleaner,
5511 but that way is more invasive.
5512 This is to, for example, allow the user to type "p var" or "b main"
5513 without having to specify the package name, and allow lookups
5514 of module.object to work in contexts that use the expression
5515 parser. */
5516
5517static void
5518fixup_go_packaging (struct dwarf2_cu *cu)
5519{
5520 char *package_name = NULL;
5521 struct pending *list;
5522 int i;
5523
5524 for (list = global_symbols; list != NULL; list = list->next)
5525 {
5526 for (i = 0; i < list->nsyms; ++i)
5527 {
5528 struct symbol *sym = list->symbol[i];
5529
5530 if (SYMBOL_LANGUAGE (sym) == language_go
5531 && SYMBOL_CLASS (sym) == LOC_BLOCK)
5532 {
5533 char *this_package_name = go_symbol_package_name (sym);
5534
5535 if (this_package_name == NULL)
5536 continue;
5537 if (package_name == NULL)
5538 package_name = this_package_name;
5539 else
5540 {
5541 if (strcmp (package_name, this_package_name) != 0)
5542 complaint (&symfile_complaints,
5543 _("Symtab %s has objects from two different Go packages: %s and %s"),
5544 (sym->symtab && sym->symtab->filename
5545 ? sym->symtab->filename
5546 : cu->objfile->name),
5547 this_package_name, package_name);
5548 xfree (this_package_name);
5549 }
5550 }
5551 }
5552 }
5553
5554 if (package_name != NULL)
5555 {
5556 struct objfile *objfile = cu->objfile;
5557 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
5558 package_name, objfile);
5559 struct symbol *sym;
5560
5561 TYPE_TAG_NAME (type) = TYPE_NAME (type);
5562
5563 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
5564 SYMBOL_SET_LANGUAGE (sym, language_go);
5565 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
5566 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
5567 e.g., "main" finds the "main" module and not C's main(). */
5568 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
5569 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
5570 SYMBOL_TYPE (sym) = type;
5571
5572 add_symbol_to_list (sym, &global_symbols);
5573
5574 xfree (package_name);
5575 }
5576}
5577
95554aad
TT
5578static void compute_symtab_includes (struct dwarf2_per_cu_data *per_cu);
5579
5580/* Return the symtab for PER_CU. This works properly regardless of
5581 whether we're using the index or psymtabs. */
5582
5583static struct symtab *
5584get_symtab (struct dwarf2_per_cu_data *per_cu)
5585{
5586 return (dwarf2_per_objfile->using_index
5587 ? per_cu->v.quick->symtab
5588 : per_cu->v.psymtab->symtab);
5589}
5590
5591/* A helper function for computing the list of all symbol tables
5592 included by PER_CU. */
5593
5594static void
5595recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
5596 htab_t all_children,
5597 struct dwarf2_per_cu_data *per_cu)
5598{
5599 void **slot;
5600 int ix;
5601 struct dwarf2_per_cu_data *iter;
5602
5603 slot = htab_find_slot (all_children, per_cu, INSERT);
5604 if (*slot != NULL)
5605 {
5606 /* This inclusion and its children have been processed. */
5607 return;
5608 }
5609
5610 *slot = per_cu;
5611 /* Only add a CU if it has a symbol table. */
5612 if (get_symtab (per_cu) != NULL)
5613 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
5614
5615 for (ix = 0;
5616 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
5617 ++ix)
5618 recursively_compute_inclusions (result, all_children, iter);
5619}
5620
5621/* Compute the symtab 'includes' fields for the symtab related to
5622 PER_CU. */
5623
5624static void
5625compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
5626{
5627 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
5628 {
5629 int ix, len;
5630 struct dwarf2_per_cu_data *iter;
5631 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
5632 htab_t all_children;
5633 struct symtab *symtab = get_symtab (per_cu);
5634
5635 /* If we don't have a symtab, we can just skip this case. */
5636 if (symtab == NULL)
5637 return;
5638
5639 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
5640 NULL, xcalloc, xfree);
5641
5642 for (ix = 0;
5643 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
5644 ix, iter);
5645 ++ix)
5646 recursively_compute_inclusions (&result_children, all_children, iter);
5647
5648 /* Now we have a transitive closure of all the included CUs, so
5649 we can convert it to a list of symtabs. */
5650 len = VEC_length (dwarf2_per_cu_ptr, result_children);
5651 symtab->includes
5652 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
5653 (len + 1) * sizeof (struct symtab *));
5654 for (ix = 0;
5655 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
5656 ++ix)
5657 symtab->includes[ix] = get_symtab (iter);
5658 symtab->includes[len] = NULL;
5659
5660 VEC_free (dwarf2_per_cu_ptr, result_children);
5661 htab_delete (all_children);
5662 }
5663}
5664
5665/* Compute the 'includes' field for the symtabs of all the CUs we just
5666 read. */
5667
5668static void
5669process_cu_includes (void)
5670{
5671 int ix;
5672 struct dwarf2_per_cu_data *iter;
5673
5674 for (ix = 0;
5675 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
5676 ix, iter);
5677 ++ix)
5678 compute_symtab_includes (iter);
5679
5680 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
5681}
5682
9cdd5dbd 5683/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
5684 already been loaded into memory. */
5685
5686static void
95554aad
TT
5687process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
5688 enum language pretend_language)
10b3939b 5689{
10b3939b 5690 struct dwarf2_cu *cu = per_cu->cu;
9291a0cd 5691 struct objfile *objfile = per_cu->objfile;
10b3939b
DJ
5692 CORE_ADDR lowpc, highpc;
5693 struct symtab *symtab;
3da10d80 5694 struct cleanup *back_to, *delayed_list_cleanup;
10b3939b
DJ
5695 CORE_ADDR baseaddr;
5696
5697 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5698
10b3939b
DJ
5699 buildsym_init ();
5700 back_to = make_cleanup (really_free_pendings, NULL);
3da10d80 5701 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
10b3939b
DJ
5702
5703 cu->list_in_scope = &file_symbols;
c906108c 5704
95554aad
TT
5705 cu->language = pretend_language;
5706 cu->language_defn = language_def (cu->language);
5707
c906108c 5708 /* Do line number decoding in read_file_scope () */
10b3939b 5709 process_die (cu->dies, cu);
c906108c 5710
a766d390
DE
5711 /* For now fudge the Go package. */
5712 if (cu->language == language_go)
5713 fixup_go_packaging (cu);
5714
3da10d80
KS
5715 /* Now that we have processed all the DIEs in the CU, all the types
5716 should be complete, and it should now be safe to compute all of the
5717 physnames. */
5718 compute_delayed_physnames (cu);
5719 do_cleanups (delayed_list_cleanup);
5720
fae299cd
DC
5721 /* Some compilers don't define a DW_AT_high_pc attribute for the
5722 compilation unit. If the DW_AT_high_pc is missing, synthesize
5723 it, by scanning the DIE's below the compilation unit. */
10b3939b 5724 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 5725
613e1657 5726 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
c906108c 5727
8be455d7 5728 if (symtab != NULL)
c906108c 5729 {
df15bd07 5730 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 5731
8be455d7
JK
5732 /* Set symtab language to language from DW_AT_language. If the
5733 compilation is from a C file generated by language preprocessors, do
5734 not set the language if it was already deduced by start_subfile. */
5735 if (!(cu->language == language_c && symtab->language != language_c))
5736 symtab->language = cu->language;
5737
5738 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
5739 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
5740 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
5741 there were bugs in prologue debug info, fixed later in GCC-4.5
5742 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
5743
5744 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
5745 needed, it would be wrong due to missing DW_AT_producer there.
5746
5747 Still one can confuse GDB by using non-standard GCC compilation
5748 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5749 */
ab260dad 5750 if (cu->has_loclist && gcc_4_minor >= 5)
8be455d7 5751 symtab->locations_valid = 1;
e0d00bc7
JK
5752
5753 if (gcc_4_minor >= 5)
5754 symtab->epilogue_unwind_valid = 1;
96408a79
SA
5755
5756 symtab->call_site_htab = cu->call_site_htab;
c906108c 5757 }
9291a0cd
TT
5758
5759 if (dwarf2_per_objfile->using_index)
5760 per_cu->v.quick->symtab = symtab;
5761 else
5762 {
5763 struct partial_symtab *pst = per_cu->v.psymtab;
5764 pst->symtab = symtab;
5765 pst->readin = 1;
5766 }
c906108c 5767
95554aad
TT
5768 /* Push it for inclusion processing later. */
5769 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
5770
c906108c
SS
5771 do_cleanups (back_to);
5772}
5773
95554aad
TT
5774/* Process an imported unit DIE. */
5775
5776static void
5777process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
5778{
5779 struct attribute *attr;
5780
5781 attr = dwarf2_attr (die, DW_AT_import, cu);
5782 if (attr != NULL)
5783 {
5784 struct dwarf2_per_cu_data *per_cu;
5785 struct symtab *imported_symtab;
5786 sect_offset offset;
5787
5788 offset = dwarf2_get_ref_die_offset (attr);
5789 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
5790
5791 /* Queue the unit, if needed. */
5792 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
5793 load_full_comp_unit (per_cu, cu->language);
5794
5795 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5796 per_cu);
5797 }
5798}
5799
c906108c
SS
5800/* Process a die and its children. */
5801
5802static void
e7c27a73 5803process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
5804{
5805 switch (die->tag)
5806 {
5807 case DW_TAG_padding:
5808 break;
5809 case DW_TAG_compile_unit:
95554aad 5810 case DW_TAG_partial_unit:
e7c27a73 5811 read_file_scope (die, cu);
c906108c 5812 break;
348e048f
DE
5813 case DW_TAG_type_unit:
5814 read_type_unit_scope (die, cu);
5815 break;
c906108c 5816 case DW_TAG_subprogram:
c906108c 5817 case DW_TAG_inlined_subroutine:
edb3359d 5818 read_func_scope (die, cu);
c906108c
SS
5819 break;
5820 case DW_TAG_lexical_block:
14898363
L
5821 case DW_TAG_try_block:
5822 case DW_TAG_catch_block:
e7c27a73 5823 read_lexical_block_scope (die, cu);
c906108c 5824 break;
96408a79
SA
5825 case DW_TAG_GNU_call_site:
5826 read_call_site_scope (die, cu);
5827 break;
c906108c 5828 case DW_TAG_class_type:
680b30c7 5829 case DW_TAG_interface_type:
c906108c
SS
5830 case DW_TAG_structure_type:
5831 case DW_TAG_union_type:
134d01f1 5832 process_structure_scope (die, cu);
c906108c
SS
5833 break;
5834 case DW_TAG_enumeration_type:
134d01f1 5835 process_enumeration_scope (die, cu);
c906108c 5836 break;
134d01f1 5837
f792889a
DJ
5838 /* These dies have a type, but processing them does not create
5839 a symbol or recurse to process the children. Therefore we can
5840 read them on-demand through read_type_die. */
c906108c 5841 case DW_TAG_subroutine_type:
72019c9c 5842 case DW_TAG_set_type:
c906108c 5843 case DW_TAG_array_type:
c906108c 5844 case DW_TAG_pointer_type:
c906108c 5845 case DW_TAG_ptr_to_member_type:
c906108c 5846 case DW_TAG_reference_type:
c906108c 5847 case DW_TAG_string_type:
c906108c 5848 break;
134d01f1 5849
c906108c 5850 case DW_TAG_base_type:
a02abb62 5851 case DW_TAG_subrange_type:
cb249c71 5852 case DW_TAG_typedef:
134d01f1
DJ
5853 /* Add a typedef symbol for the type definition, if it has a
5854 DW_AT_name. */
f792889a 5855 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 5856 break;
c906108c 5857 case DW_TAG_common_block:
e7c27a73 5858 read_common_block (die, cu);
c906108c
SS
5859 break;
5860 case DW_TAG_common_inclusion:
5861 break;
d9fa45fe 5862 case DW_TAG_namespace:
63d06c5c 5863 processing_has_namespace_info = 1;
e7c27a73 5864 read_namespace (die, cu);
d9fa45fe 5865 break;
5d7cb8df 5866 case DW_TAG_module:
f55ee35c 5867 processing_has_namespace_info = 1;
5d7cb8df
JK
5868 read_module (die, cu);
5869 break;
d9fa45fe
DC
5870 case DW_TAG_imported_declaration:
5871 case DW_TAG_imported_module:
63d06c5c 5872 processing_has_namespace_info = 1;
27aa8d6a
SW
5873 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5874 || cu->language != language_fortran))
5875 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5876 dwarf_tag_name (die->tag));
5877 read_import_statement (die, cu);
d9fa45fe 5878 break;
95554aad
TT
5879
5880 case DW_TAG_imported_unit:
5881 process_imported_unit_die (die, cu);
5882 break;
5883
c906108c 5884 default:
e7c27a73 5885 new_symbol (die, NULL, cu);
c906108c
SS
5886 break;
5887 }
5888}
5889
94af9270
KS
5890/* A helper function for dwarf2_compute_name which determines whether DIE
5891 needs to have the name of the scope prepended to the name listed in the
5892 die. */
5893
5894static int
5895die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5896{
1c809c68
TT
5897 struct attribute *attr;
5898
94af9270
KS
5899 switch (die->tag)
5900 {
5901 case DW_TAG_namespace:
5902 case DW_TAG_typedef:
5903 case DW_TAG_class_type:
5904 case DW_TAG_interface_type:
5905 case DW_TAG_structure_type:
5906 case DW_TAG_union_type:
5907 case DW_TAG_enumeration_type:
5908 case DW_TAG_enumerator:
5909 case DW_TAG_subprogram:
5910 case DW_TAG_member:
5911 return 1;
5912
5913 case DW_TAG_variable:
c2b0a229 5914 case DW_TAG_constant:
94af9270
KS
5915 /* We only need to prefix "globally" visible variables. These include
5916 any variable marked with DW_AT_external or any variable that
5917 lives in a namespace. [Variables in anonymous namespaces
5918 require prefixing, but they are not DW_AT_external.] */
5919
5920 if (dwarf2_attr (die, DW_AT_specification, cu))
5921 {
5922 struct dwarf2_cu *spec_cu = cu;
9a619af0 5923
94af9270
KS
5924 return die_needs_namespace (die_specification (die, &spec_cu),
5925 spec_cu);
5926 }
5927
1c809c68 5928 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
5929 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5930 && die->parent->tag != DW_TAG_module)
1c809c68
TT
5931 return 0;
5932 /* A variable in a lexical block of some kind does not need a
5933 namespace, even though in C++ such variables may be external
5934 and have a mangled name. */
5935 if (die->parent->tag == DW_TAG_lexical_block
5936 || die->parent->tag == DW_TAG_try_block
1054b214
TT
5937 || die->parent->tag == DW_TAG_catch_block
5938 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
5939 return 0;
5940 return 1;
94af9270
KS
5941
5942 default:
5943 return 0;
5944 }
5945}
5946
98bfdba5
PA
5947/* Retrieve the last character from a mem_file. */
5948
5949static void
5950do_ui_file_peek_last (void *object, const char *buffer, long length)
5951{
5952 char *last_char_p = (char *) object;
5953
5954 if (length > 0)
5955 *last_char_p = buffer[length - 1];
5956}
5957
94af9270 5958/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390
DE
5959 compute the physname for the object, which include a method's:
5960 - formal parameters (C++/Java),
5961 - receiver type (Go),
5962 - return type (Java).
5963
5964 The term "physname" is a bit confusing.
5965 For C++, for example, it is the demangled name.
5966 For Go, for example, it's the mangled name.
94af9270 5967
af6b7be1
JB
5968 For Ada, return the DIE's linkage name rather than the fully qualified
5969 name. PHYSNAME is ignored..
5970
94af9270
KS
5971 The result is allocated on the objfile_obstack and canonicalized. */
5972
5973static const char *
5974dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5975 int physname)
5976{
bb5ed363
DE
5977 struct objfile *objfile = cu->objfile;
5978
94af9270
KS
5979 if (name == NULL)
5980 name = dwarf2_name (die, cu);
5981
f55ee35c
JK
5982 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5983 compute it by typename_concat inside GDB. */
5984 if (cu->language == language_ada
5985 || (cu->language == language_fortran && physname))
5986 {
5987 /* For Ada unit, we prefer the linkage name over the name, as
5988 the former contains the exported name, which the user expects
5989 to be able to reference. Ideally, we want the user to be able
5990 to reference this entity using either natural or linkage name,
5991 but we haven't started looking at this enhancement yet. */
5992 struct attribute *attr;
5993
5994 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5995 if (attr == NULL)
5996 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5997 if (attr && DW_STRING (attr))
5998 return DW_STRING (attr);
5999 }
6000
94af9270
KS
6001 /* These are the only languages we know how to qualify names in. */
6002 if (name != NULL
f55ee35c
JK
6003 && (cu->language == language_cplus || cu->language == language_java
6004 || cu->language == language_fortran))
94af9270
KS
6005 {
6006 if (die_needs_namespace (die, cu))
6007 {
6008 long length;
0d5cff50 6009 const char *prefix;
94af9270
KS
6010 struct ui_file *buf;
6011
6012 prefix = determine_prefix (die, cu);
6013 buf = mem_fileopen ();
6014 if (*prefix != '\0')
6015 {
f55ee35c
JK
6016 char *prefixed_name = typename_concat (NULL, prefix, name,
6017 physname, cu);
9a619af0 6018
94af9270
KS
6019 fputs_unfiltered (prefixed_name, buf);
6020 xfree (prefixed_name);
6021 }
6022 else
62d5b8da 6023 fputs_unfiltered (name, buf);
94af9270 6024
98bfdba5
PA
6025 /* Template parameters may be specified in the DIE's DW_AT_name, or
6026 as children with DW_TAG_template_type_param or
6027 DW_TAG_value_type_param. If the latter, add them to the name
6028 here. If the name already has template parameters, then
6029 skip this step; some versions of GCC emit both, and
6030 it is more efficient to use the pre-computed name.
6031
6032 Something to keep in mind about this process: it is very
6033 unlikely, or in some cases downright impossible, to produce
6034 something that will match the mangled name of a function.
6035 If the definition of the function has the same debug info,
6036 we should be able to match up with it anyway. But fallbacks
6037 using the minimal symbol, for instance to find a method
6038 implemented in a stripped copy of libstdc++, will not work.
6039 If we do not have debug info for the definition, we will have to
6040 match them up some other way.
6041
6042 When we do name matching there is a related problem with function
6043 templates; two instantiated function templates are allowed to
6044 differ only by their return types, which we do not add here. */
6045
6046 if (cu->language == language_cplus && strchr (name, '<') == NULL)
6047 {
6048 struct attribute *attr;
6049 struct die_info *child;
6050 int first = 1;
6051
6052 die->building_fullname = 1;
6053
6054 for (child = die->child; child != NULL; child = child->sibling)
6055 {
6056 struct type *type;
12df843f 6057 LONGEST value;
98bfdba5
PA
6058 gdb_byte *bytes;
6059 struct dwarf2_locexpr_baton *baton;
6060 struct value *v;
6061
6062 if (child->tag != DW_TAG_template_type_param
6063 && child->tag != DW_TAG_template_value_param)
6064 continue;
6065
6066 if (first)
6067 {
6068 fputs_unfiltered ("<", buf);
6069 first = 0;
6070 }
6071 else
6072 fputs_unfiltered (", ", buf);
6073
6074 attr = dwarf2_attr (child, DW_AT_type, cu);
6075 if (attr == NULL)
6076 {
6077 complaint (&symfile_complaints,
6078 _("template parameter missing DW_AT_type"));
6079 fputs_unfiltered ("UNKNOWN_TYPE", buf);
6080 continue;
6081 }
6082 type = die_type (child, cu);
6083
6084 if (child->tag == DW_TAG_template_type_param)
6085 {
6086 c_print_type (type, "", buf, -1, 0);
6087 continue;
6088 }
6089
6090 attr = dwarf2_attr (child, DW_AT_const_value, cu);
6091 if (attr == NULL)
6092 {
6093 complaint (&symfile_complaints,
3e43a32a
MS
6094 _("template parameter missing "
6095 "DW_AT_const_value"));
98bfdba5
PA
6096 fputs_unfiltered ("UNKNOWN_VALUE", buf);
6097 continue;
6098 }
6099
6100 dwarf2_const_value_attr (attr, type, name,
6101 &cu->comp_unit_obstack, cu,
6102 &value, &bytes, &baton);
6103
6104 if (TYPE_NOSIGN (type))
6105 /* GDB prints characters as NUMBER 'CHAR'. If that's
6106 changed, this can use value_print instead. */
6107 c_printchar (value, type, buf);
6108 else
6109 {
6110 struct value_print_options opts;
6111
6112 if (baton != NULL)
6113 v = dwarf2_evaluate_loc_desc (type, NULL,
6114 baton->data,
6115 baton->size,
6116 baton->per_cu);
6117 else if (bytes != NULL)
6118 {
6119 v = allocate_value (type);
6120 memcpy (value_contents_writeable (v), bytes,
6121 TYPE_LENGTH (type));
6122 }
6123 else
6124 v = value_from_longest (type, value);
6125
3e43a32a
MS
6126 /* Specify decimal so that we do not depend on
6127 the radix. */
98bfdba5
PA
6128 get_formatted_print_options (&opts, 'd');
6129 opts.raw = 1;
6130 value_print (v, buf, &opts);
6131 release_value (v);
6132 value_free (v);
6133 }
6134 }
6135
6136 die->building_fullname = 0;
6137
6138 if (!first)
6139 {
6140 /* Close the argument list, with a space if necessary
6141 (nested templates). */
6142 char last_char = '\0';
6143 ui_file_put (buf, do_ui_file_peek_last, &last_char);
6144 if (last_char == '>')
6145 fputs_unfiltered (" >", buf);
6146 else
6147 fputs_unfiltered (">", buf);
6148 }
6149 }
6150
94af9270
KS
6151 /* For Java and C++ methods, append formal parameter type
6152 information, if PHYSNAME. */
6e70227d 6153
94af9270
KS
6154 if (physname && die->tag == DW_TAG_subprogram
6155 && (cu->language == language_cplus
6156 || cu->language == language_java))
6157 {
6158 struct type *type = read_type_die (die, cu);
6159
3167638f 6160 c_type_print_args (type, buf, 1, cu->language);
94af9270
KS
6161
6162 if (cu->language == language_java)
6163 {
6164 /* For java, we must append the return type to method
0963b4bd 6165 names. */
94af9270
KS
6166 if (die->tag == DW_TAG_subprogram)
6167 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
6168 0, 0);
6169 }
6170 else if (cu->language == language_cplus)
6171 {
60430eff
DJ
6172 /* Assume that an artificial first parameter is
6173 "this", but do not crash if it is not. RealView
6174 marks unnamed (and thus unused) parameters as
6175 artificial; there is no way to differentiate
6176 the two cases. */
94af9270
KS
6177 if (TYPE_NFIELDS (type) > 0
6178 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 6179 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
6180 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
6181 0))))
94af9270
KS
6182 fputs_unfiltered (" const", buf);
6183 }
6184 }
6185
bb5ed363 6186 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
94af9270
KS
6187 &length);
6188 ui_file_delete (buf);
6189
6190 if (cu->language == language_cplus)
6191 {
6192 char *cname
6193 = dwarf2_canonicalize_name (name, cu,
bb5ed363 6194 &objfile->objfile_obstack);
9a619af0 6195
94af9270
KS
6196 if (cname != NULL)
6197 name = cname;
6198 }
6199 }
6200 }
6201
6202 return name;
6203}
6204
0114d602
DJ
6205/* Return the fully qualified name of DIE, based on its DW_AT_name.
6206 If scope qualifiers are appropriate they will be added. The result
6207 will be allocated on the objfile_obstack, or NULL if the DIE does
94af9270
KS
6208 not have a name. NAME may either be from a previous call to
6209 dwarf2_name or NULL.
6210
0963b4bd 6211 The output string will be canonicalized (if C++/Java). */
0114d602
DJ
6212
6213static const char *
94af9270 6214dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 6215{
94af9270
KS
6216 return dwarf2_compute_name (name, die, cu, 0);
6217}
0114d602 6218
94af9270
KS
6219/* Construct a physname for the given DIE in CU. NAME may either be
6220 from a previous call to dwarf2_name or NULL. The result will be
6221 allocated on the objfile_objstack or NULL if the DIE does not have a
6222 name.
0114d602 6223
94af9270 6224 The output string will be canonicalized (if C++/Java). */
0114d602 6225
94af9270
KS
6226static const char *
6227dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
6228{
bb5ed363 6229 struct objfile *objfile = cu->objfile;
900e11f9
JK
6230 struct attribute *attr;
6231 const char *retval, *mangled = NULL, *canon = NULL;
6232 struct cleanup *back_to;
6233 int need_copy = 1;
6234
6235 /* In this case dwarf2_compute_name is just a shortcut not building anything
6236 on its own. */
6237 if (!die_needs_namespace (die, cu))
6238 return dwarf2_compute_name (name, die, cu, 1);
6239
6240 back_to = make_cleanup (null_cleanup, NULL);
6241
6242 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
6243 if (!attr)
6244 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
6245
6246 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
6247 has computed. */
6248 if (attr && DW_STRING (attr))
6249 {
6250 char *demangled;
6251
6252 mangled = DW_STRING (attr);
6253
6254 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
6255 type. It is easier for GDB users to search for such functions as
6256 `name(params)' than `long name(params)'. In such case the minimal
6257 symbol names do not match the full symbol names but for template
6258 functions there is never a need to look up their definition from their
6259 declaration so the only disadvantage remains the minimal symbol
6260 variant `long name(params)' does not have the proper inferior type.
6261 */
6262
a766d390
DE
6263 if (cu->language == language_go)
6264 {
6265 /* This is a lie, but we already lie to the caller new_symbol_full.
6266 new_symbol_full assumes we return the mangled name.
6267 This just undoes that lie until things are cleaned up. */
6268 demangled = NULL;
6269 }
6270 else
6271 {
6272 demangled = cplus_demangle (mangled,
6273 (DMGL_PARAMS | DMGL_ANSI
6274 | (cu->language == language_java
6275 ? DMGL_JAVA | DMGL_RET_POSTFIX
6276 : DMGL_RET_DROP)));
6277 }
900e11f9
JK
6278 if (demangled)
6279 {
6280 make_cleanup (xfree, demangled);
6281 canon = demangled;
6282 }
6283 else
6284 {
6285 canon = mangled;
6286 need_copy = 0;
6287 }
6288 }
6289
6290 if (canon == NULL || check_physname)
6291 {
6292 const char *physname = dwarf2_compute_name (name, die, cu, 1);
6293
6294 if (canon != NULL && strcmp (physname, canon) != 0)
6295 {
6296 /* It may not mean a bug in GDB. The compiler could also
6297 compute DW_AT_linkage_name incorrectly. But in such case
6298 GDB would need to be bug-to-bug compatible. */
6299
6300 complaint (&symfile_complaints,
6301 _("Computed physname <%s> does not match demangled <%s> "
6302 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
b64f50a1 6303 physname, canon, mangled, die->offset.sect_off, objfile->name);
900e11f9
JK
6304
6305 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
6306 is available here - over computed PHYSNAME. It is safer
6307 against both buggy GDB and buggy compilers. */
6308
6309 retval = canon;
6310 }
6311 else
6312 {
6313 retval = physname;
6314 need_copy = 0;
6315 }
6316 }
6317 else
6318 retval = canon;
6319
6320 if (need_copy)
6321 retval = obsavestring (retval, strlen (retval),
bb5ed363 6322 &objfile->objfile_obstack);
900e11f9
JK
6323
6324 do_cleanups (back_to);
6325 return retval;
0114d602
DJ
6326}
6327
27aa8d6a
SW
6328/* Read the import statement specified by the given die and record it. */
6329
6330static void
6331read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
6332{
bb5ed363 6333 struct objfile *objfile = cu->objfile;
27aa8d6a 6334 struct attribute *import_attr;
32019081 6335 struct die_info *imported_die, *child_die;
de4affc9 6336 struct dwarf2_cu *imported_cu;
27aa8d6a 6337 const char *imported_name;
794684b6 6338 const char *imported_name_prefix;
13387711
SW
6339 const char *canonical_name;
6340 const char *import_alias;
6341 const char *imported_declaration = NULL;
794684b6 6342 const char *import_prefix;
32019081
JK
6343 VEC (const_char_ptr) *excludes = NULL;
6344 struct cleanup *cleanups;
13387711
SW
6345
6346 char *temp;
27aa8d6a
SW
6347
6348 import_attr = dwarf2_attr (die, DW_AT_import, cu);
6349 if (import_attr == NULL)
6350 {
6351 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6352 dwarf_tag_name (die->tag));
6353 return;
6354 }
6355
de4affc9
CC
6356 imported_cu = cu;
6357 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
6358 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
6359 if (imported_name == NULL)
6360 {
6361 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
6362
6363 The import in the following code:
6364 namespace A
6365 {
6366 typedef int B;
6367 }
6368
6369 int main ()
6370 {
6371 using A::B;
6372 B b;
6373 return b;
6374 }
6375
6376 ...
6377 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
6378 <52> DW_AT_decl_file : 1
6379 <53> DW_AT_decl_line : 6
6380 <54> DW_AT_import : <0x75>
6381 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
6382 <59> DW_AT_name : B
6383 <5b> DW_AT_decl_file : 1
6384 <5c> DW_AT_decl_line : 2
6385 <5d> DW_AT_type : <0x6e>
6386 ...
6387 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
6388 <76> DW_AT_byte_size : 4
6389 <77> DW_AT_encoding : 5 (signed)
6390
6391 imports the wrong die ( 0x75 instead of 0x58 ).
6392 This case will be ignored until the gcc bug is fixed. */
6393 return;
6394 }
6395
82856980
SW
6396 /* Figure out the local name after import. */
6397 import_alias = dwarf2_name (die, cu);
27aa8d6a 6398
794684b6
SW
6399 /* Figure out where the statement is being imported to. */
6400 import_prefix = determine_prefix (die, cu);
6401
6402 /* Figure out what the scope of the imported die is and prepend it
6403 to the name of the imported die. */
de4affc9 6404 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 6405
f55ee35c
JK
6406 if (imported_die->tag != DW_TAG_namespace
6407 && imported_die->tag != DW_TAG_module)
794684b6 6408 {
13387711
SW
6409 imported_declaration = imported_name;
6410 canonical_name = imported_name_prefix;
794684b6 6411 }
13387711 6412 else if (strlen (imported_name_prefix) > 0)
794684b6 6413 {
13387711
SW
6414 temp = alloca (strlen (imported_name_prefix)
6415 + 2 + strlen (imported_name) + 1);
6416 strcpy (temp, imported_name_prefix);
6417 strcat (temp, "::");
6418 strcat (temp, imported_name);
6419 canonical_name = temp;
794684b6 6420 }
13387711
SW
6421 else
6422 canonical_name = imported_name;
794684b6 6423
32019081
JK
6424 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
6425
6426 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
6427 for (child_die = die->child; child_die && child_die->tag;
6428 child_die = sibling_die (child_die))
6429 {
6430 /* DWARF-4: A Fortran use statement with a “rename list” may be
6431 represented by an imported module entry with an import attribute
6432 referring to the module and owned entries corresponding to those
6433 entities that are renamed as part of being imported. */
6434
6435 if (child_die->tag != DW_TAG_imported_declaration)
6436 {
6437 complaint (&symfile_complaints,
6438 _("child DW_TAG_imported_declaration expected "
6439 "- DIE at 0x%x [in module %s]"),
b64f50a1 6440 child_die->offset.sect_off, objfile->name);
32019081
JK
6441 continue;
6442 }
6443
6444 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
6445 if (import_attr == NULL)
6446 {
6447 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
6448 dwarf_tag_name (child_die->tag));
6449 continue;
6450 }
6451
6452 imported_cu = cu;
6453 imported_die = follow_die_ref_or_sig (child_die, import_attr,
6454 &imported_cu);
6455 imported_name = dwarf2_name (imported_die, imported_cu);
6456 if (imported_name == NULL)
6457 {
6458 complaint (&symfile_complaints,
6459 _("child DW_TAG_imported_declaration has unknown "
6460 "imported name - DIE at 0x%x [in module %s]"),
b64f50a1 6461 child_die->offset.sect_off, objfile->name);
32019081
JK
6462 continue;
6463 }
6464
6465 VEC_safe_push (const_char_ptr, excludes, imported_name);
6466
6467 process_die (child_die, cu);
6468 }
6469
c0cc3a76
SW
6470 cp_add_using_directive (import_prefix,
6471 canonical_name,
6472 import_alias,
13387711 6473 imported_declaration,
32019081 6474 excludes,
bb5ed363 6475 &objfile->objfile_obstack);
32019081
JK
6476
6477 do_cleanups (cleanups);
27aa8d6a
SW
6478}
6479
ae2de4f8
DE
6480/* Cleanup function for read_file_scope. */
6481
cb1df416
DJ
6482static void
6483free_cu_line_header (void *arg)
6484{
6485 struct dwarf2_cu *cu = arg;
6486
6487 free_line_header (cu->line_header);
6488 cu->line_header = NULL;
6489}
6490
9291a0cd
TT
6491static void
6492find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
6493 char **name, char **comp_dir)
6494{
6495 struct attribute *attr;
6496
6497 *name = NULL;
6498 *comp_dir = NULL;
6499
6500 /* Find the filename. Do not use dwarf2_name here, since the filename
6501 is not a source language identifier. */
6502 attr = dwarf2_attr (die, DW_AT_name, cu);
6503 if (attr)
6504 {
6505 *name = DW_STRING (attr);
6506 }
6507
6508 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6509 if (attr)
6510 *comp_dir = DW_STRING (attr);
6511 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
6512 {
6513 *comp_dir = ldirname (*name);
6514 if (*comp_dir != NULL)
6515 make_cleanup (xfree, *comp_dir);
6516 }
6517 if (*comp_dir != NULL)
6518 {
6519 /* Irix 6.2 native cc prepends <machine>.: to the compilation
6520 directory, get rid of it. */
6521 char *cp = strchr (*comp_dir, ':');
6522
6523 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
6524 *comp_dir = cp + 1;
6525 }
6526
6527 if (*name == NULL)
6528 *name = "<unknown>";
6529}
6530
f3f5162e
DE
6531/* Handle DW_AT_stmt_list for a compilation unit or type unit.
6532 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
6533 COMP_DIR is the compilation directory.
6534 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
2ab95328
TT
6535
6536static void
6537handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
f3f5162e 6538 const char *comp_dir, int want_line_info)
2ab95328
TT
6539{
6540 struct attribute *attr;
2ab95328 6541
2ab95328
TT
6542 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6543 if (attr)
6544 {
6545 unsigned int line_offset = DW_UNSND (attr);
6546 struct line_header *line_header
3019eac3 6547 = dwarf_decode_line_header (line_offset, cu);
2ab95328
TT
6548
6549 if (line_header)
dee91e82
DE
6550 {
6551 cu->line_header = line_header;
6552 make_cleanup (free_cu_line_header, cu);
f3f5162e 6553 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
dee91e82 6554 }
2ab95328
TT
6555 }
6556}
6557
95554aad 6558/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 6559
c906108c 6560static void
e7c27a73 6561read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 6562{
dee91e82 6563 struct objfile *objfile = dwarf2_per_objfile->objfile;
debd256d 6564 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2acceee2 6565 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
6566 CORE_ADDR highpc = ((CORE_ADDR) 0);
6567 struct attribute *attr;
e1024ff1 6568 char *name = NULL;
c906108c
SS
6569 char *comp_dir = NULL;
6570 struct die_info *child_die;
6571 bfd *abfd = objfile->obfd;
e142c38c 6572 CORE_ADDR baseaddr;
6e70227d 6573
e142c38c 6574 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 6575
fae299cd 6576 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
6577
6578 /* If we didn't find a lowpc, set it to highpc to avoid complaints
6579 from finish_block. */
2acceee2 6580 if (lowpc == ((CORE_ADDR) -1))
c906108c
SS
6581 lowpc = highpc;
6582 lowpc += baseaddr;
6583 highpc += baseaddr;
6584
9291a0cd 6585 find_file_and_directory (die, cu, &name, &comp_dir);
e1024ff1 6586
95554aad 6587 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 6588
f4b8a18d
KW
6589 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
6590 standardised yet. As a workaround for the language detection we fall
6591 back to the DW_AT_producer string. */
6592 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
6593 cu->language = language_opencl;
6594
3019eac3
DE
6595 /* Similar hack for Go. */
6596 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
6597 set_cu_language (DW_LANG_Go, cu);
6598
6599 /* We assume that we're processing GCC output. */
6600 processing_gcc_compilation = 2;
6601
6602 processing_has_namespace_info = 0;
6603
6604 start_symtab (name, comp_dir, lowpc);
6605 record_debugformat ("DWARF 2");
6606 record_producer (cu->producer);
6607
6608 /* Decode line number information if present. We do this before
6609 processing child DIEs, so that the line header table is available
6610 for DW_AT_decl_file. */
6611 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
6612
6613 /* Process all dies in compilation unit. */
6614 if (die->child != NULL)
6615 {
6616 child_die = die->child;
6617 while (child_die && child_die->tag)
6618 {
6619 process_die (child_die, cu);
6620 child_die = sibling_die (child_die);
6621 }
6622 }
6623
6624 /* Decode macro information, if present. Dwarf 2 macro information
6625 refers to information in the line number info statement program
6626 header, so we can only read it if we've read the header
6627 successfully. */
6628 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
6629 if (attr && cu->line_header)
6630 {
6631 if (dwarf2_attr (die, DW_AT_macro_info, cu))
6632 complaint (&symfile_complaints,
6633 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
6634
6635 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
6636 comp_dir, abfd, cu,
fceca515
DE
6637 &dwarf2_per_objfile->macro, 1,
6638 ".debug_macro");
3019eac3
DE
6639 }
6640 else
6641 {
6642 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
6643 if (attr && cu->line_header)
6644 {
6645 unsigned int macro_offset = DW_UNSND (attr);
6646
6647 dwarf_decode_macros (cu->line_header, macro_offset,
6648 comp_dir, abfd, cu,
fceca515
DE
6649 &dwarf2_per_objfile->macinfo, 0,
6650 ".debug_macinfo");
3019eac3
DE
6651 }
6652 }
6653
6654 do_cleanups (back_to);
6655}
6656
6657/* Process DW_TAG_type_unit.
6658 For TUs we want to skip the first top level sibling if it's not the
6659 actual type being defined by this TU. In this case the first top
6660 level sibling is there to provide context only. */
6661
6662static void
6663read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
6664{
6665 struct objfile *objfile = cu->objfile;
6666 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6667 CORE_ADDR lowpc;
6668 struct attribute *attr;
6669 char *name = NULL;
6670 char *comp_dir = NULL;
6671 struct die_info *child_die;
6672 bfd *abfd = objfile->obfd;
6673
6674 /* start_symtab needs a low pc, but we don't really have one.
6675 Do what read_file_scope would do in the absence of such info. */
6676 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6677
6678 /* Find the filename. Do not use dwarf2_name here, since the filename
6679 is not a source language identifier. */
6680 attr = dwarf2_attr (die, DW_AT_name, cu);
6681 if (attr)
6682 name = DW_STRING (attr);
6683
6684 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
6685 if (attr)
6686 comp_dir = DW_STRING (attr);
6687 else if (name != NULL && IS_ABSOLUTE_PATH (name))
6688 {
6689 comp_dir = ldirname (name);
6690 if (comp_dir != NULL)
6691 make_cleanup (xfree, comp_dir);
6692 }
6693
6694 if (name == NULL)
6695 name = "<unknown>";
6696
95554aad 6697 prepare_one_comp_unit (cu, die, language_minimal);
3019eac3
DE
6698
6699 /* We assume that we're processing GCC output. */
6700 processing_gcc_compilation = 2;
6701
6702 processing_has_namespace_info = 0;
6703
6704 start_symtab (name, comp_dir, lowpc);
6705 record_debugformat ("DWARF 2");
6706 record_producer (cu->producer);
6707
6708 /* Decode line number information if present. We do this before
6709 processing child DIEs, so that the line header table is available
6710 for DW_AT_decl_file.
6711 We don't need the pc/line-number mapping for type units. */
6712 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
6713
6714 /* Process the dies in the type unit. */
6715 if (die->child == NULL)
6716 {
6717 dump_die_for_error (die);
6718 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
6719 bfd_get_filename (abfd));
6720 }
6721
6722 child_die = die->child;
6723
6724 while (child_die && child_die->tag)
6725 {
6726 process_die (child_die, cu);
6727
6728 child_die = sibling_die (child_die);
6729 }
6730
6731 do_cleanups (back_to);
6732}
6733\f
6734/* DWO files. */
6735
6736static hashval_t
6737hash_dwo_file (const void *item)
6738{
6739 const struct dwo_file *dwo_file = item;
6740
6741 return htab_hash_string (dwo_file->dwo_name);
6742}
6743
6744static int
6745eq_dwo_file (const void *item_lhs, const void *item_rhs)
6746{
6747 const struct dwo_file *lhs = item_lhs;
6748 const struct dwo_file *rhs = item_rhs;
6749
6750 return strcmp (lhs->dwo_name, rhs->dwo_name) == 0;
6751}
6752
6753/* Allocate a hash table for DWO files. */
6754
6755static htab_t
6756allocate_dwo_file_hash_table (void)
6757{
6758 struct objfile *objfile = dwarf2_per_objfile->objfile;
6759
6760 return htab_create_alloc_ex (41,
6761 hash_dwo_file,
6762 eq_dwo_file,
6763 NULL,
6764 &objfile->objfile_obstack,
6765 hashtab_obstack_allocate,
6766 dummy_obstack_deallocate);
6767}
6768
6769static hashval_t
6770hash_dwo_unit (const void *item)
6771{
6772 const struct dwo_unit *dwo_unit = item;
6773
6774 /* This drops the top 32 bits of the id, but is ok for a hash. */
6775 return dwo_unit->signature;
6776}
6777
6778static int
6779eq_dwo_unit (const void *item_lhs, const void *item_rhs)
6780{
6781 const struct dwo_unit *lhs = item_lhs;
6782 const struct dwo_unit *rhs = item_rhs;
6783
6784 /* The signature is assumed to be unique within the DWO file.
6785 So while object file CU dwo_id's always have the value zero,
6786 that's OK, assuming each object file DWO file has only one CU,
6787 and that's the rule for now. */
6788 return lhs->signature == rhs->signature;
6789}
6790
6791/* Allocate a hash table for DWO CUs,TUs.
6792 There is one of these tables for each of CUs,TUs for each DWO file. */
6793
6794static htab_t
6795allocate_dwo_unit_table (struct objfile *objfile)
6796{
6797 /* Start out with a pretty small number.
6798 Generally DWO files contain only one CU and maybe some TUs. */
6799 return htab_create_alloc_ex (3,
6800 hash_dwo_unit,
6801 eq_dwo_unit,
6802 NULL,
6803 &objfile->objfile_obstack,
6804 hashtab_obstack_allocate,
6805 dummy_obstack_deallocate);
6806}
6807
6808/* This function is mapped across the sections and remembers the offset and
6809 size of each of the DWO debugging sections we are interested in. */
6810
6811static void
6812dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_file_ptr)
6813{
6814 struct dwo_file *dwo_file = dwo_file_ptr;
6815 const struct dwo_section_names *names = &dwo_section_names;
6816
6817 if (section_is_p (sectp->name, &names->abbrev_dwo))
6818 {
6819 dwo_file->sections.abbrev.asection = sectp;
6820 dwo_file->sections.abbrev.size = bfd_get_section_size (sectp);
6821 }
6822 else if (section_is_p (sectp->name, &names->info_dwo))
6823 {
6824 dwo_file->sections.info.asection = sectp;
6825 dwo_file->sections.info.size = bfd_get_section_size (sectp);
6826 }
6827 else if (section_is_p (sectp->name, &names->line_dwo))
6828 {
6829 dwo_file->sections.line.asection = sectp;
6830 dwo_file->sections.line.size = bfd_get_section_size (sectp);
6831 }
6832 else if (section_is_p (sectp->name, &names->loc_dwo))
6833 {
6834 dwo_file->sections.loc.asection = sectp;
6835 dwo_file->sections.loc.size = bfd_get_section_size (sectp);
6836 }
6837 else if (section_is_p (sectp->name, &names->str_dwo))
6838 {
6839 dwo_file->sections.str.asection = sectp;
6840 dwo_file->sections.str.size = bfd_get_section_size (sectp);
6841 }
6842 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
6843 {
6844 dwo_file->sections.str_offsets.asection = sectp;
6845 dwo_file->sections.str_offsets.size = bfd_get_section_size (sectp);
6846 }
6847 else if (section_is_p (sectp->name, &names->types_dwo))
6848 {
6849 struct dwarf2_section_info type_section;
6850
6851 memset (&type_section, 0, sizeof (type_section));
6852 type_section.asection = sectp;
6853 type_section.size = bfd_get_section_size (sectp);
6854 VEC_safe_push (dwarf2_section_info_def, dwo_file->sections.types,
6855 &type_section);
6856 }
6857}
6858
6859/* Structure used to pass data to create_debug_info_hash_table_reader. */
6860
6861struct create_dwo_info_table_data
6862{
6863 struct dwo_file *dwo_file;
6864 htab_t cu_htab;
6865};
6866
6867/* die_reader_func for create_debug_info_hash_table. */
6868
6869static void
6870create_debug_info_hash_table_reader (const struct die_reader_specs *reader,
6871 gdb_byte *info_ptr,
6872 struct die_info *comp_unit_die,
6873 int has_children,
6874 void *datap)
6875{
6876 struct dwarf2_cu *cu = reader->cu;
6877 struct objfile *objfile = dwarf2_per_objfile->objfile;
6878 sect_offset offset = cu->per_cu->offset;
6879 struct dwarf2_section_info *section = cu->per_cu->info_or_types_section;
6880 struct create_dwo_info_table_data *data = datap;
6881 struct dwo_file *dwo_file = data->dwo_file;
6882 htab_t cu_htab = data->cu_htab;
6883 void **slot;
6884 struct attribute *attr;
6885 struct dwo_unit *dwo_unit;
6886
6887 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6888 if (attr == NULL)
6889 {
6890 error (_("Dwarf Error: debug entry at offset 0x%x is missing"
6891 " its dwo_id [in module %s]"),
6892 offset.sect_off, dwo_file->dwo_name);
6893 return;
6894 }
6895
6896 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
6897 dwo_unit->dwo_file = dwo_file;
6898 dwo_unit->signature = DW_UNSND (attr);
6899 dwo_unit->info_or_types_section = section;
6900 dwo_unit->offset = offset;
6901 dwo_unit->length = cu->per_cu->length;
6902
6903 slot = htab_find_slot (cu_htab, dwo_unit, INSERT);
6904 gdb_assert (slot != NULL);
6905 if (*slot != NULL)
6906 {
6907 const struct dwo_unit *dup_dwo_unit = *slot;
6908
6909 complaint (&symfile_complaints,
6910 _("debug entry at offset 0x%x is duplicate to the entry at"
6911 " offset 0x%x, dwo_id 0x%s [in module %s]"),
6912 offset.sect_off, dup_dwo_unit->offset.sect_off,
6913 phex (dwo_unit->signature, sizeof (dwo_unit->signature)),
6914 dwo_file->dwo_name);
6915 }
6916 else
6917 *slot = dwo_unit;
6918
6919 if (dwarf2_die_debug)
6920 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id 0x%s\n",
6921 offset.sect_off,
6922 phex (dwo_unit->signature,
6923 sizeof (dwo_unit->signature)));
6924}
6925
6926/* Create a hash table to map DWO IDs to their CU entry in .debug_info.dwo. */
6927
6928static htab_t
6929create_debug_info_hash_table (struct dwo_file *dwo_file)
6930{
6931 struct objfile *objfile = dwarf2_per_objfile->objfile;
6932 struct dwarf2_section_info *section = &dwo_file->sections.info;
6933 bfd *abfd;
6934 htab_t cu_htab;
6935 gdb_byte *info_ptr, *end_ptr;
6936 struct create_dwo_info_table_data create_dwo_info_table_data;
6937
6938 dwarf2_read_section (objfile, section);
6939 info_ptr = section->buffer;
6940
6941 if (info_ptr == NULL)
6942 return NULL;
6943
6944 /* We can't set abfd until now because the section may be empty or
6945 not present, in which case section->asection will be NULL. */
6946 abfd = section->asection->owner;
6947
6948 if (dwarf2_die_debug)
6949 fprintf_unfiltered (gdb_stdlog, "Reading .debug_info.dwo for %s:\n",
6950 bfd_get_filename (abfd));
6951
6952 cu_htab = allocate_dwo_unit_table (objfile);
6953
6954 create_dwo_info_table_data.dwo_file = dwo_file;
6955 create_dwo_info_table_data.cu_htab = cu_htab;
6956
6957 end_ptr = info_ptr + section->size;
6958 while (info_ptr < end_ptr)
6959 {
6960 struct dwarf2_per_cu_data per_cu;
6961
6962 memset (&per_cu, 0, sizeof (per_cu));
6963 per_cu.objfile = objfile;
6964 per_cu.is_debug_types = 0;
6965 per_cu.offset.sect_off = info_ptr - section->buffer;
6966 per_cu.info_or_types_section = section;
6967
6968 init_cutu_and_read_dies_no_follow (&per_cu,
6969 &dwo_file->sections.abbrev,
6970 dwo_file,
6971 create_debug_info_hash_table_reader,
6972 &create_dwo_info_table_data);
6973
6974 info_ptr += per_cu.length;
6975 }
6976
6977 return cu_htab;
6978}
6979
6980/* Subroutine of open_dwo_file to simplify it.
6981 Open the file specified by FILE_NAME and hand it off to BFD for
6982 preliminary analysis. Return a newly initialized bfd *, which
6983 includes a canonicalized copy of FILE_NAME.
6984 In case of trouble, return NULL.
6985 NOTE: This function is derived from symfile_bfd_open. */
6986
6987static bfd *
6988try_open_dwo_file (const char *file_name)
6989{
6990 bfd *sym_bfd;
6991 int desc;
6992 char *absolute_name;
3019eac3
DE
6993
6994 desc = openp (debug_file_directory, OPF_TRY_CWD_FIRST, file_name,
6995 O_RDONLY | O_BINARY, &absolute_name);
6996 if (desc < 0)
6997 return NULL;
6998
6999 sym_bfd = bfd_fopen (absolute_name, gnutarget, FOPEN_RB, desc);
7000 if (!sym_bfd)
7001 {
3019eac3
DE
7002 xfree (absolute_name);
7003 return NULL;
7004 }
7005 bfd_set_cacheable (sym_bfd, 1);
7006
7007 if (!bfd_check_format (sym_bfd, bfd_object))
7008 {
7009 bfd_close (sym_bfd); /* This also closes desc. */
7010 xfree (absolute_name);
7011 return NULL;
7012 }
7013
7014 /* bfd_usrdata exists for applications and libbfd must not touch it. */
7015 gdb_assert (bfd_usrdata (sym_bfd) == NULL);
7016
7017 return sym_bfd;
7018}
7019
7020/* Try to open DWO file DWO_NAME.
7021 COMP_DIR is the DW_AT_comp_dir attribute.
7022 The result is the bfd handle of the file.
7023 If there is a problem finding or opening the file, return NULL.
7024 Upon success, the canonicalized path of the file is stored in the bfd,
7025 same as symfile_bfd_open. */
7026
7027static bfd *
7028open_dwo_file (const char *dwo_name, const char *comp_dir)
7029{
7030 bfd *abfd;
3019eac3
DE
7031
7032 if (IS_ABSOLUTE_PATH (dwo_name))
7033 return try_open_dwo_file (dwo_name);
7034
7035 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
7036
7037 if (comp_dir != NULL)
7038 {
7039 char *path_to_try = concat (comp_dir, SLASH_STRING, dwo_name, NULL);
7040
7041 /* NOTE: If comp_dir is a relative path, this will also try the
7042 search path, which seems useful. */
7043 abfd = try_open_dwo_file (path_to_try);
7044 xfree (path_to_try);
7045 if (abfd != NULL)
7046 return abfd;
7047 }
7048
7049 /* That didn't work, try debug-file-directory, which, despite its name,
7050 is a list of paths. */
7051
7052 if (*debug_file_directory == '\0')
7053 return NULL;
7054
7055 return try_open_dwo_file (dwo_name);
7056}
7057
7058/* Initialize the use of the DWO file specified by DWO_NAME. */
7059
7060static struct dwo_file *
7061init_dwo_file (const char *dwo_name, const char *comp_dir)
7062{
7063 struct objfile *objfile = dwarf2_per_objfile->objfile;
7064 struct dwo_file *dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7065 struct dwo_file);
7066 bfd *abfd;
7067 struct cleanup *cleanups;
7068
7069 if (dwarf2_die_debug)
7070 fprintf_unfiltered (gdb_stdlog, "Reading DWO file %s:\n", dwo_name);
7071
7072 abfd = open_dwo_file (dwo_name, comp_dir);
7073 if (abfd == NULL)
7074 return NULL;
7075 dwo_file->dwo_name = dwo_name;
7076 dwo_file->dwo_bfd = abfd;
7077
7078 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
7079
7080 bfd_map_over_sections (abfd, dwarf2_locate_dwo_sections, dwo_file);
7081
7082 dwo_file->cus = create_debug_info_hash_table (dwo_file);
7083
7084 dwo_file->tus = create_debug_types_hash_table (dwo_file,
7085 dwo_file->sections.types);
7086
7087 discard_cleanups (cleanups);
7088
7089 return dwo_file;
7090}
7091
7092/* Lookup DWO file DWO_NAME. */
7093
7094static struct dwo_file *
7095lookup_dwo_file (char *dwo_name, const char *comp_dir)
7096{
7097 struct dwo_file *dwo_file;
7098 struct dwo_file find_entry;
7099 void **slot;
7100
7101 if (dwarf2_per_objfile->dwo_files == NULL)
7102 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
7103
7104 /* Have we already seen this DWO file? */
7105 find_entry.dwo_name = dwo_name;
7106 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
7107
7108 /* If not, read it in and build a table of the DWOs it contains. */
7109 if (*slot == NULL)
7110 *slot = init_dwo_file (dwo_name, comp_dir);
7111
7112 /* NOTE: This will be NULL if unable to open the file. */
7113 dwo_file = *slot;
7114
7115 return dwo_file;
7116}
7117
7118/* Lookup the DWO CU referenced from THIS_CU in DWO file DWO_NAME.
7119 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7120 SIGNATURE is the "dwo_id" of the CU (for consistency we use the same
7121 nomenclature as TUs).
7122 The result is the DWO CU or NULL if we didn't find it
7123 (dwo_id mismatch or couldn't find the DWO file). */
7124
7125static struct dwo_unit *
7126lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
7127 char *dwo_name, const char *comp_dir,
7128 ULONGEST signature)
7129{
7130 struct objfile *objfile = dwarf2_per_objfile->objfile;
7131 struct dwo_file *dwo_file;
7132
7133 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7134 if (dwo_file == NULL)
7135 return NULL;
7136
7137 /* Look up the DWO using its signature(dwo_id). */
7138
7139 if (dwo_file->cus != NULL)
7140 {
7141 struct dwo_unit find_dwo_cu, *dwo_cu;
7142
7143 find_dwo_cu.signature = signature;
7144 dwo_cu = htab_find (dwo_file->cus, &find_dwo_cu);
a766d390 7145
3019eac3
DE
7146 if (dwo_cu != NULL)
7147 return dwo_cu;
7148 }
c906108c 7149
3019eac3 7150 /* We didn't find it. This must mean a dwo_id mismatch. */
df8a16a1 7151
3019eac3
DE
7152 complaint (&symfile_complaints,
7153 _("Could not find DWO CU referenced by CU at offset 0x%x"
7154 " [in module %s]"),
7155 this_cu->offset.sect_off, objfile->name);
7156 return NULL;
7157}
c906108c 7158
3019eac3
DE
7159/* Lookup the DWO TU referenced from THIS_TU in DWO file DWO_NAME.
7160 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
7161 The result is the DWO CU or NULL if we didn't find it
7162 (dwo_id mismatch or couldn't find the DWO file). */
debd256d 7163
3019eac3
DE
7164static struct dwo_unit *
7165lookup_dwo_type_unit (struct signatured_type *this_tu,
7166 char *dwo_name, const char *comp_dir)
7167{
7168 struct objfile *objfile = dwarf2_per_objfile->objfile;
7169 struct dwo_file *dwo_file;
cb1df416 7170
3019eac3
DE
7171 dwo_file = lookup_dwo_file (dwo_name, comp_dir);
7172 if (dwo_file == NULL)
7173 return NULL;
cf2c3c16 7174
3019eac3
DE
7175 /* Look up the DWO using its signature(dwo_id). */
7176
7177 if (dwo_file->tus != NULL)
cf2c3c16 7178 {
3019eac3 7179 struct dwo_unit find_dwo_tu, *dwo_tu;
9a619af0 7180
3019eac3
DE
7181 find_dwo_tu.signature = this_tu->signature;
7182 dwo_tu = htab_find (dwo_file->tus, &find_dwo_tu);
7183
7184 if (dwo_tu != NULL)
7185 return dwo_tu;
2e276125 7186 }
9cdd5dbd 7187
3019eac3
DE
7188 /* We didn't find it. This must mean a dwo_id mismatch. */
7189
7190 complaint (&symfile_complaints,
7191 _("Could not find DWO TU referenced by TU at offset 0x%x"
7192 " [in module %s]"),
7193 this_tu->per_cu.offset.sect_off, objfile->name);
7194 return NULL;
5fb290d7
DJ
7195}
7196
3019eac3
DE
7197/* Free all resources associated with DWO_FILE.
7198 Close the DWO file and munmap the sections.
7199 All memory should be on the objfile obstack. */
348e048f
DE
7200
7201static void
3019eac3 7202free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
348e048f 7203{
3019eac3
DE
7204 int ix;
7205 struct dwarf2_section_info *section;
348e048f 7206
3019eac3
DE
7207 gdb_assert (dwo_file->dwo_bfd != objfile->obfd);
7208 bfd_close (dwo_file->dwo_bfd);
348e048f 7209
3019eac3
DE
7210 munmap_section_buffer (&dwo_file->sections.abbrev);
7211 munmap_section_buffer (&dwo_file->sections.info);
7212 munmap_section_buffer (&dwo_file->sections.line);
7213 munmap_section_buffer (&dwo_file->sections.loc);
7214 munmap_section_buffer (&dwo_file->sections.str);
7215 munmap_section_buffer (&dwo_file->sections.str_offsets);
348e048f 7216
3019eac3
DE
7217 for (ix = 0;
7218 VEC_iterate (dwarf2_section_info_def, dwo_file->sections.types,
7219 ix, section);
7220 ++ix)
7221 munmap_section_buffer (section);
348e048f 7222
3019eac3
DE
7223 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
7224}
348e048f 7225
3019eac3 7226/* Wrapper for free_dwo_file for use in cleanups. */
348e048f 7227
3019eac3
DE
7228static void
7229free_dwo_file_cleanup (void *arg)
7230{
7231 struct dwo_file *dwo_file = (struct dwo_file *) arg;
7232 struct objfile *objfile = dwarf2_per_objfile->objfile;
348e048f 7233
3019eac3
DE
7234 free_dwo_file (dwo_file, objfile);
7235}
348e048f 7236
3019eac3 7237/* Traversal function for free_dwo_files. */
2ab95328 7238
3019eac3
DE
7239static int
7240free_dwo_file_from_slot (void **slot, void *info)
7241{
7242 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7243 struct objfile *objfile = (struct objfile *) info;
348e048f 7244
3019eac3 7245 free_dwo_file (dwo_file, objfile);
348e048f 7246
3019eac3
DE
7247 return 1;
7248}
348e048f 7249
3019eac3 7250/* Free all resources associated with DWO_FILES. */
348e048f 7251
3019eac3
DE
7252static void
7253free_dwo_files (htab_t dwo_files, struct objfile *objfile)
7254{
7255 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 7256}
3019eac3
DE
7257\f
7258/* Read in various DIEs. */
348e048f 7259
d389af10
JK
7260/* qsort helper for inherit_abstract_dies. */
7261
7262static int
7263unsigned_int_compar (const void *ap, const void *bp)
7264{
7265 unsigned int a = *(unsigned int *) ap;
7266 unsigned int b = *(unsigned int *) bp;
7267
7268 return (a > b) - (b > a);
7269}
7270
7271/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
7272 Inherit only the children of the DW_AT_abstract_origin DIE not being
7273 already referenced by DW_AT_abstract_origin from the children of the
7274 current DIE. */
d389af10
JK
7275
7276static void
7277inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
7278{
7279 struct die_info *child_die;
7280 unsigned die_children_count;
7281 /* CU offsets which were referenced by children of the current DIE. */
b64f50a1
JK
7282 sect_offset *offsets;
7283 sect_offset *offsets_end, *offsetp;
d389af10
JK
7284 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
7285 struct die_info *origin_die;
7286 /* Iterator of the ORIGIN_DIE children. */
7287 struct die_info *origin_child_die;
7288 struct cleanup *cleanups;
7289 struct attribute *attr;
cd02d79d
PA
7290 struct dwarf2_cu *origin_cu;
7291 struct pending **origin_previous_list_in_scope;
d389af10
JK
7292
7293 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7294 if (!attr)
7295 return;
7296
cd02d79d
PA
7297 /* Note that following die references may follow to a die in a
7298 different cu. */
7299
7300 origin_cu = cu;
7301 origin_die = follow_die_ref (die, attr, &origin_cu);
7302
7303 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
7304 symbols in. */
7305 origin_previous_list_in_scope = origin_cu->list_in_scope;
7306 origin_cu->list_in_scope = cu->list_in_scope;
7307
edb3359d
DJ
7308 if (die->tag != origin_die->tag
7309 && !(die->tag == DW_TAG_inlined_subroutine
7310 && origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7311 complaint (&symfile_complaints,
7312 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
b64f50a1 7313 die->offset.sect_off, origin_die->offset.sect_off);
d389af10
JK
7314
7315 child_die = die->child;
7316 die_children_count = 0;
7317 while (child_die && child_die->tag)
7318 {
7319 child_die = sibling_die (child_die);
7320 die_children_count++;
7321 }
7322 offsets = xmalloc (sizeof (*offsets) * die_children_count);
7323 cleanups = make_cleanup (xfree, offsets);
7324
7325 offsets_end = offsets;
7326 child_die = die->child;
7327 while (child_die && child_die->tag)
7328 {
c38f313d
DJ
7329 /* For each CHILD_DIE, find the corresponding child of
7330 ORIGIN_DIE. If there is more than one layer of
7331 DW_AT_abstract_origin, follow them all; there shouldn't be,
7332 but GCC versions at least through 4.4 generate this (GCC PR
7333 40573). */
7334 struct die_info *child_origin_die = child_die;
cd02d79d 7335 struct dwarf2_cu *child_origin_cu = cu;
9a619af0 7336
c38f313d
DJ
7337 while (1)
7338 {
cd02d79d
PA
7339 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
7340 child_origin_cu);
c38f313d
DJ
7341 if (attr == NULL)
7342 break;
cd02d79d
PA
7343 child_origin_die = follow_die_ref (child_origin_die, attr,
7344 &child_origin_cu);
c38f313d
DJ
7345 }
7346
d389af10
JK
7347 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
7348 counterpart may exist. */
c38f313d 7349 if (child_origin_die != child_die)
d389af10 7350 {
edb3359d
DJ
7351 if (child_die->tag != child_origin_die->tag
7352 && !(child_die->tag == DW_TAG_inlined_subroutine
7353 && child_origin_die->tag == DW_TAG_subprogram))
d389af10
JK
7354 complaint (&symfile_complaints,
7355 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7356 "different tags"), child_die->offset.sect_off,
7357 child_origin_die->offset.sect_off);
c38f313d
DJ
7358 if (child_origin_die->parent != origin_die)
7359 complaint (&symfile_complaints,
7360 _("Child DIE 0x%x and its abstract origin 0x%x have "
b64f50a1
JK
7361 "different parents"), child_die->offset.sect_off,
7362 child_origin_die->offset.sect_off);
c38f313d
DJ
7363 else
7364 *offsets_end++ = child_origin_die->offset;
d389af10
JK
7365 }
7366 child_die = sibling_die (child_die);
7367 }
7368 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
7369 unsigned_int_compar);
7370 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
b64f50a1 7371 if (offsetp[-1].sect_off == offsetp->sect_off)
3e43a32a
MS
7372 complaint (&symfile_complaints,
7373 _("Multiple children of DIE 0x%x refer "
7374 "to DIE 0x%x as their abstract origin"),
b64f50a1 7375 die->offset.sect_off, offsetp->sect_off);
d389af10
JK
7376
7377 offsetp = offsets;
7378 origin_child_die = origin_die->child;
7379 while (origin_child_die && origin_child_die->tag)
7380 {
7381 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1
JK
7382 while (offsetp < offsets_end
7383 && offsetp->sect_off < origin_child_die->offset.sect_off)
d389af10 7384 offsetp++;
b64f50a1
JK
7385 if (offsetp >= offsets_end
7386 || offsetp->sect_off > origin_child_die->offset.sect_off)
d389af10
JK
7387 {
7388 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
cd02d79d 7389 process_die (origin_child_die, origin_cu);
d389af10
JK
7390 }
7391 origin_child_die = sibling_die (origin_child_die);
7392 }
cd02d79d 7393 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
7394
7395 do_cleanups (cleanups);
7396}
7397
c906108c 7398static void
e7c27a73 7399read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7400{
e7c27a73 7401 struct objfile *objfile = cu->objfile;
52f0bd74 7402 struct context_stack *new;
c906108c
SS
7403 CORE_ADDR lowpc;
7404 CORE_ADDR highpc;
7405 struct die_info *child_die;
edb3359d 7406 struct attribute *attr, *call_line, *call_file;
c906108c 7407 char *name;
e142c38c 7408 CORE_ADDR baseaddr;
801e3a5b 7409 struct block *block;
edb3359d 7410 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
34eaf542
TT
7411 VEC (symbolp) *template_args = NULL;
7412 struct template_symbol *templ_func = NULL;
edb3359d
DJ
7413
7414 if (inlined_func)
7415 {
7416 /* If we do not have call site information, we can't show the
7417 caller of this inlined function. That's too confusing, so
7418 only use the scope for local variables. */
7419 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
7420 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
7421 if (call_line == NULL || call_file == NULL)
7422 {
7423 read_lexical_block_scope (die, cu);
7424 return;
7425 }
7426 }
c906108c 7427
e142c38c
DJ
7428 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7429
94af9270 7430 name = dwarf2_name (die, cu);
c906108c 7431
e8d05480
JB
7432 /* Ignore functions with missing or empty names. These are actually
7433 illegal according to the DWARF standard. */
7434 if (name == NULL)
7435 {
7436 complaint (&symfile_complaints,
b64f50a1
JK
7437 _("missing name for subprogram DIE at %d"),
7438 die->offset.sect_off);
e8d05480
JB
7439 return;
7440 }
7441
7442 /* Ignore functions with missing or invalid low and high pc attributes. */
7443 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
7444 {
ae4d0c03
PM
7445 attr = dwarf2_attr (die, DW_AT_external, cu);
7446 if (!attr || !DW_UNSND (attr))
7447 complaint (&symfile_complaints,
3e43a32a
MS
7448 _("cannot get low and high bounds "
7449 "for subprogram DIE at %d"),
b64f50a1 7450 die->offset.sect_off);
e8d05480
JB
7451 return;
7452 }
c906108c
SS
7453
7454 lowpc += baseaddr;
7455 highpc += baseaddr;
7456
34eaf542
TT
7457 /* If we have any template arguments, then we must allocate a
7458 different sort of symbol. */
7459 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
7460 {
7461 if (child_die->tag == DW_TAG_template_type_param
7462 || child_die->tag == DW_TAG_template_value_param)
7463 {
7464 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7465 struct template_symbol);
7466 templ_func->base.is_cplus_template_function = 1;
7467 break;
7468 }
7469 }
7470
c906108c 7471 new = push_context (0, lowpc);
34eaf542
TT
7472 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
7473 (struct symbol *) templ_func);
4c2df51b 7474
4cecd739
DJ
7475 /* If there is a location expression for DW_AT_frame_base, record
7476 it. */
e142c38c 7477 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 7478 if (attr)
c034e007
AC
7479 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
7480 expression is being recorded directly in the function's symbol
7481 and not in a separate frame-base object. I guess this hack is
7482 to avoid adding some sort of frame-base adjunct/annex to the
7483 function's symbol :-(. The problem with doing this is that it
7484 results in a function symbol with a location expression that
7485 has nothing to do with the location of the function, ouch! The
7486 relationship should be: a function's symbol has-a frame base; a
7487 frame-base has-a location expression. */
e7c27a73 7488 dwarf2_symbol_mark_computed (attr, new->name, cu);
4c2df51b 7489
e142c38c 7490 cu->list_in_scope = &local_symbols;
c906108c 7491
639d11d3 7492 if (die->child != NULL)
c906108c 7493 {
639d11d3 7494 child_die = die->child;
c906108c
SS
7495 while (child_die && child_die->tag)
7496 {
34eaf542
TT
7497 if (child_die->tag == DW_TAG_template_type_param
7498 || child_die->tag == DW_TAG_template_value_param)
7499 {
7500 struct symbol *arg = new_symbol (child_die, NULL, cu);
7501
f1078f66
DJ
7502 if (arg != NULL)
7503 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
7504 }
7505 else
7506 process_die (child_die, cu);
c906108c
SS
7507 child_die = sibling_die (child_die);
7508 }
7509 }
7510
d389af10
JK
7511 inherit_abstract_dies (die, cu);
7512
4a811a97
UW
7513 /* If we have a DW_AT_specification, we might need to import using
7514 directives from the context of the specification DIE. See the
7515 comment in determine_prefix. */
7516 if (cu->language == language_cplus
7517 && dwarf2_attr (die, DW_AT_specification, cu))
7518 {
7519 struct dwarf2_cu *spec_cu = cu;
7520 struct die_info *spec_die = die_specification (die, &spec_cu);
7521
7522 while (spec_die)
7523 {
7524 child_die = spec_die->child;
7525 while (child_die && child_die->tag)
7526 {
7527 if (child_die->tag == DW_TAG_imported_module)
7528 process_die (child_die, spec_cu);
7529 child_die = sibling_die (child_die);
7530 }
7531
7532 /* In some cases, GCC generates specification DIEs that
7533 themselves contain DW_AT_specification attributes. */
7534 spec_die = die_specification (spec_die, &spec_cu);
7535 }
7536 }
7537
c906108c
SS
7538 new = pop_context ();
7539 /* Make a block for the local symbols within. */
801e3a5b
JB
7540 block = finish_block (new->name, &local_symbols, new->old_blocks,
7541 lowpc, highpc, objfile);
7542
df8a16a1 7543 /* For C++, set the block's scope. */
f55ee35c 7544 if (cu->language == language_cplus || cu->language == language_fortran)
df8a16a1 7545 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
0114d602 7546 determine_prefix (die, cu),
df8a16a1
DJ
7547 processing_has_namespace_info);
7548
801e3a5b
JB
7549 /* If we have address ranges, record them. */
7550 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 7551
34eaf542
TT
7552 /* Attach template arguments to function. */
7553 if (! VEC_empty (symbolp, template_args))
7554 {
7555 gdb_assert (templ_func != NULL);
7556
7557 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
7558 templ_func->template_arguments
7559 = obstack_alloc (&objfile->objfile_obstack,
7560 (templ_func->n_template_arguments
7561 * sizeof (struct symbol *)));
7562 memcpy (templ_func->template_arguments,
7563 VEC_address (symbolp, template_args),
7564 (templ_func->n_template_arguments * sizeof (struct symbol *)));
7565 VEC_free (symbolp, template_args);
7566 }
7567
208d8187
JB
7568 /* In C++, we can have functions nested inside functions (e.g., when
7569 a function declares a class that has methods). This means that
7570 when we finish processing a function scope, we may need to go
7571 back to building a containing block's symbol lists. */
7572 local_symbols = new->locals;
7573 param_symbols = new->params;
27aa8d6a 7574 using_directives = new->using_directives;
208d8187 7575
921e78cf
JB
7576 /* If we've finished processing a top-level function, subsequent
7577 symbols go in the file symbol list. */
7578 if (outermost_context_p ())
e142c38c 7579 cu->list_in_scope = &file_symbols;
c906108c
SS
7580}
7581
7582/* Process all the DIES contained within a lexical block scope. Start
7583 a new scope, process the dies, and then close the scope. */
7584
7585static void
e7c27a73 7586read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 7587{
e7c27a73 7588 struct objfile *objfile = cu->objfile;
52f0bd74 7589 struct context_stack *new;
c906108c
SS
7590 CORE_ADDR lowpc, highpc;
7591 struct die_info *child_die;
e142c38c
DJ
7592 CORE_ADDR baseaddr;
7593
7594 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
7595
7596 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
7597 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
7598 as multiple lexical blocks? Handling children in a sane way would
6e70227d 7599 be nasty. Might be easier to properly extend generic blocks to
af34e669 7600 describe ranges. */
d85a05f0 7601 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
c906108c
SS
7602 return;
7603 lowpc += baseaddr;
7604 highpc += baseaddr;
7605
7606 push_context (0, lowpc);
639d11d3 7607 if (die->child != NULL)
c906108c 7608 {
639d11d3 7609 child_die = die->child;
c906108c
SS
7610 while (child_die && child_die->tag)
7611 {
e7c27a73 7612 process_die (child_die, cu);
c906108c
SS
7613 child_die = sibling_die (child_die);
7614 }
7615 }
7616 new = pop_context ();
7617
8540c487 7618 if (local_symbols != NULL || using_directives != NULL)
c906108c 7619 {
801e3a5b
JB
7620 struct block *block
7621 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
7622 highpc, objfile);
7623
7624 /* Note that recording ranges after traversing children, as we
7625 do here, means that recording a parent's ranges entails
7626 walking across all its children's ranges as they appear in
7627 the address map, which is quadratic behavior.
7628
7629 It would be nicer to record the parent's ranges before
7630 traversing its children, simply overriding whatever you find
7631 there. But since we don't even decide whether to create a
7632 block until after we've traversed its children, that's hard
7633 to do. */
7634 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c
SS
7635 }
7636 local_symbols = new->locals;
27aa8d6a 7637 using_directives = new->using_directives;
c906108c
SS
7638}
7639
96408a79
SA
7640/* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
7641
7642static void
7643read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
7644{
7645 struct objfile *objfile = cu->objfile;
7646 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7647 CORE_ADDR pc, baseaddr;
7648 struct attribute *attr;
7649 struct call_site *call_site, call_site_local;
7650 void **slot;
7651 int nparams;
7652 struct die_info *child_die;
7653
7654 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7655
7656 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
7657 if (!attr)
7658 {
7659 complaint (&symfile_complaints,
7660 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
7661 "DIE 0x%x [in module %s]"),
b64f50a1 7662 die->offset.sect_off, objfile->name);
96408a79
SA
7663 return;
7664 }
7665 pc = DW_ADDR (attr) + baseaddr;
7666
7667 if (cu->call_site_htab == NULL)
7668 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
7669 NULL, &objfile->objfile_obstack,
7670 hashtab_obstack_allocate, NULL);
7671 call_site_local.pc = pc;
7672 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
7673 if (*slot != NULL)
7674 {
7675 complaint (&symfile_complaints,
7676 _("Duplicate PC %s for DW_TAG_GNU_call_site "
7677 "DIE 0x%x [in module %s]"),
b64f50a1 7678 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
96408a79
SA
7679 return;
7680 }
7681
7682 /* Count parameters at the caller. */
7683
7684 nparams = 0;
7685 for (child_die = die->child; child_die && child_die->tag;
7686 child_die = sibling_die (child_die))
7687 {
7688 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7689 {
7690 complaint (&symfile_complaints,
7691 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
7692 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7693 child_die->tag, child_die->offset.sect_off, objfile->name);
96408a79
SA
7694 continue;
7695 }
7696
7697 nparams++;
7698 }
7699
7700 call_site = obstack_alloc (&objfile->objfile_obstack,
7701 (sizeof (*call_site)
7702 + (sizeof (*call_site->parameter)
7703 * (nparams - 1))));
7704 *slot = call_site;
7705 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
7706 call_site->pc = pc;
7707
7708 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
7709 {
7710 struct die_info *func_die;
7711
7712 /* Skip also over DW_TAG_inlined_subroutine. */
7713 for (func_die = die->parent;
7714 func_die && func_die->tag != DW_TAG_subprogram
7715 && func_die->tag != DW_TAG_subroutine_type;
7716 func_die = func_die->parent);
7717
7718 /* DW_AT_GNU_all_call_sites is a superset
7719 of DW_AT_GNU_all_tail_call_sites. */
7720 if (func_die
7721 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
7722 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
7723 {
7724 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
7725 not complete. But keep CALL_SITE for look ups via call_site_htab,
7726 both the initial caller containing the real return address PC and
7727 the final callee containing the current PC of a chain of tail
7728 calls do not need to have the tail call list complete. But any
7729 function candidate for a virtual tail call frame searched via
7730 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
7731 determined unambiguously. */
7732 }
7733 else
7734 {
7735 struct type *func_type = NULL;
7736
7737 if (func_die)
7738 func_type = get_die_type (func_die, cu);
7739 if (func_type != NULL)
7740 {
7741 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
7742
7743 /* Enlist this call site to the function. */
7744 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
7745 TYPE_TAIL_CALL_LIST (func_type) = call_site;
7746 }
7747 else
7748 complaint (&symfile_complaints,
7749 _("Cannot find function owning DW_TAG_GNU_call_site "
7750 "DIE 0x%x [in module %s]"),
b64f50a1 7751 die->offset.sect_off, objfile->name);
96408a79
SA
7752 }
7753 }
7754
7755 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
7756 if (attr == NULL)
7757 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
7758 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
7759 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
7760 /* Keep NULL DWARF_BLOCK. */;
7761 else if (attr_form_is_block (attr))
7762 {
7763 struct dwarf2_locexpr_baton *dlbaton;
7764
7765 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
7766 dlbaton->data = DW_BLOCK (attr)->data;
7767 dlbaton->size = DW_BLOCK (attr)->size;
7768 dlbaton->per_cu = cu->per_cu;
7769
7770 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
7771 }
7772 else if (is_ref_attr (attr))
7773 {
96408a79
SA
7774 struct dwarf2_cu *target_cu = cu;
7775 struct die_info *target_die;
7776
7777 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
7778 gdb_assert (target_cu->objfile == objfile);
7779 if (die_is_declaration (target_die, target_cu))
7780 {
7781 const char *target_physname;
7782
7783 target_physname = dwarf2_physname (NULL, target_die, target_cu);
7784 if (target_physname == NULL)
7785 complaint (&symfile_complaints,
7786 _("DW_AT_GNU_call_site_target target DIE has invalid "
7787 "physname, for referencing DIE 0x%x [in module %s]"),
b64f50a1 7788 die->offset.sect_off, objfile->name);
96408a79
SA
7789 else
7790 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
7791 }
7792 else
7793 {
7794 CORE_ADDR lowpc;
7795
7796 /* DW_AT_entry_pc should be preferred. */
7797 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
7798 complaint (&symfile_complaints,
7799 _("DW_AT_GNU_call_site_target target DIE has invalid "
7800 "low pc, for referencing DIE 0x%x [in module %s]"),
b64f50a1 7801 die->offset.sect_off, objfile->name);
96408a79
SA
7802 else
7803 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
7804 }
7805 }
7806 else
7807 complaint (&symfile_complaints,
7808 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
7809 "block nor reference, for DIE 0x%x [in module %s]"),
b64f50a1 7810 die->offset.sect_off, objfile->name);
96408a79
SA
7811
7812 call_site->per_cu = cu->per_cu;
7813
7814 for (child_die = die->child;
7815 child_die && child_die->tag;
7816 child_die = sibling_die (child_die))
7817 {
96408a79
SA
7818 struct call_site_parameter *parameter;
7819
7820 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
7821 {
7822 /* Already printed the complaint above. */
7823 continue;
7824 }
7825
7826 gdb_assert (call_site->parameter_count < nparams);
7827 parameter = &call_site->parameter[call_site->parameter_count];
7828
7829 /* DW_AT_location specifies the register number. Value of the data
7830 assumed for the register is contained in DW_AT_GNU_call_site_value. */
7831
7832 attr = dwarf2_attr (child_die, DW_AT_location, cu);
7833 if (!attr || !attr_form_is_block (attr))
7834 {
7835 complaint (&symfile_complaints,
7836 _("No DW_FORM_block* DW_AT_location for "
7837 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7838 child_die->offset.sect_off, objfile->name);
96408a79
SA
7839 continue;
7840 }
7841 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
7842 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
7843 if (parameter->dwarf_reg == -1
7844 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
7845 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
7846 &parameter->fb_offset))
7847 {
7848 complaint (&symfile_complaints,
7849 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
7850 "for DW_FORM_block* DW_AT_location for "
7851 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7852 child_die->offset.sect_off, objfile->name);
96408a79
SA
7853 continue;
7854 }
7855
7856 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
7857 if (!attr_form_is_block (attr))
7858 {
7859 complaint (&symfile_complaints,
7860 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
7861 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7862 child_die->offset.sect_off, objfile->name);
96408a79
SA
7863 continue;
7864 }
7865 parameter->value = DW_BLOCK (attr)->data;
7866 parameter->value_size = DW_BLOCK (attr)->size;
7867
7868 /* Parameters are not pre-cleared by memset above. */
7869 parameter->data_value = NULL;
7870 parameter->data_value_size = 0;
7871 call_site->parameter_count++;
7872
7873 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
7874 if (attr)
7875 {
7876 if (!attr_form_is_block (attr))
7877 complaint (&symfile_complaints,
7878 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
7879 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
b64f50a1 7880 child_die->offset.sect_off, objfile->name);
96408a79
SA
7881 else
7882 {
7883 parameter->data_value = DW_BLOCK (attr)->data;
7884 parameter->data_value_size = DW_BLOCK (attr)->size;
7885 }
7886 }
7887 }
7888}
7889
43039443 7890/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
ff013f42
JK
7891 Return 1 if the attributes are present and valid, otherwise, return 0.
7892 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
43039443
JK
7893
7894static int
7895dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
ff013f42
JK
7896 CORE_ADDR *high_return, struct dwarf2_cu *cu,
7897 struct partial_symtab *ranges_pst)
43039443
JK
7898{
7899 struct objfile *objfile = cu->objfile;
7900 struct comp_unit_head *cu_header = &cu->header;
7901 bfd *obfd = objfile->obfd;
7902 unsigned int addr_size = cu_header->addr_size;
7903 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
7904 /* Base address selection entry. */
7905 CORE_ADDR base;
7906 int found_base;
7907 unsigned int dummy;
7908 gdb_byte *buffer;
7909 CORE_ADDR marker;
7910 int low_set;
7911 CORE_ADDR low = 0;
7912 CORE_ADDR high = 0;
ff013f42 7913 CORE_ADDR baseaddr;
43039443 7914
d00adf39
DE
7915 found_base = cu->base_known;
7916 base = cu->base_address;
43039443 7917
be391dca 7918 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 7919 if (offset >= dwarf2_per_objfile->ranges.size)
43039443
JK
7920 {
7921 complaint (&symfile_complaints,
7922 _("Offset %d out of bounds for DW_AT_ranges attribute"),
7923 offset);
7924 return 0;
7925 }
dce234bc 7926 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443
JK
7927
7928 /* Read in the largest possible address. */
7929 marker = read_address (obfd, buffer, cu, &dummy);
7930 if ((marker & mask) == mask)
7931 {
7932 /* If we found the largest possible address, then
7933 read the base address. */
7934 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7935 buffer += 2 * addr_size;
7936 offset += 2 * addr_size;
7937 found_base = 1;
7938 }
7939
7940 low_set = 0;
7941
e7030f15 7942 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 7943
43039443
JK
7944 while (1)
7945 {
7946 CORE_ADDR range_beginning, range_end;
7947
7948 range_beginning = read_address (obfd, buffer, cu, &dummy);
7949 buffer += addr_size;
7950 range_end = read_address (obfd, buffer, cu, &dummy);
7951 buffer += addr_size;
7952 offset += 2 * addr_size;
7953
7954 /* An end of list marker is a pair of zero addresses. */
7955 if (range_beginning == 0 && range_end == 0)
7956 /* Found the end of list entry. */
7957 break;
7958
7959 /* Each base address selection entry is a pair of 2 values.
7960 The first is the largest possible address, the second is
7961 the base address. Check for a base address here. */
7962 if ((range_beginning & mask) == mask)
7963 {
7964 /* If we found the largest possible address, then
7965 read the base address. */
7966 base = read_address (obfd, buffer + addr_size, cu, &dummy);
7967 found_base = 1;
7968 continue;
7969 }
7970
7971 if (!found_base)
7972 {
7973 /* We have no valid base address for the ranges
7974 data. */
7975 complaint (&symfile_complaints,
7976 _("Invalid .debug_ranges data (no base address)"));
7977 return 0;
7978 }
7979
9277c30c
UW
7980 if (range_beginning > range_end)
7981 {
7982 /* Inverted range entries are invalid. */
7983 complaint (&symfile_complaints,
7984 _("Invalid .debug_ranges data (inverted range)"));
7985 return 0;
7986 }
7987
7988 /* Empty range entries have no effect. */
7989 if (range_beginning == range_end)
7990 continue;
7991
43039443
JK
7992 range_beginning += base;
7993 range_end += base;
7994
9277c30c 7995 if (ranges_pst != NULL)
ff013f42 7996 addrmap_set_empty (objfile->psymtabs_addrmap,
3e43a32a
MS
7997 range_beginning + baseaddr,
7998 range_end - 1 + baseaddr,
ff013f42
JK
7999 ranges_pst);
8000
43039443
JK
8001 /* FIXME: This is recording everything as a low-high
8002 segment of consecutive addresses. We should have a
8003 data structure for discontiguous block ranges
8004 instead. */
8005 if (! low_set)
8006 {
8007 low = range_beginning;
8008 high = range_end;
8009 low_set = 1;
8010 }
8011 else
8012 {
8013 if (range_beginning < low)
8014 low = range_beginning;
8015 if (range_end > high)
8016 high = range_end;
8017 }
8018 }
8019
8020 if (! low_set)
8021 /* If the first entry is an end-of-list marker, the range
8022 describes an empty scope, i.e. no instructions. */
8023 return 0;
8024
8025 if (low_return)
8026 *low_return = low;
8027 if (high_return)
8028 *high_return = high;
8029 return 1;
8030}
8031
af34e669
DJ
8032/* Get low and high pc attributes from a die. Return 1 if the attributes
8033 are present and valid, otherwise, return 0. Return -1 if the range is
8034 discontinuous, i.e. derived from DW_AT_ranges information. */
380bca97 8035
c906108c 8036static int
af34e669 8037dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
8038 CORE_ADDR *highpc, struct dwarf2_cu *cu,
8039 struct partial_symtab *pst)
c906108c
SS
8040{
8041 struct attribute *attr;
91da1414 8042 struct attribute *attr_high;
af34e669
DJ
8043 CORE_ADDR low = 0;
8044 CORE_ADDR high = 0;
8045 int ret = 0;
c906108c 8046
91da1414
MW
8047 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8048 if (attr_high)
af34e669 8049 {
e142c38c 8050 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 8051 if (attr)
91da1414
MW
8052 {
8053 low = DW_ADDR (attr);
3019eac3
DE
8054 if (attr_high->form == DW_FORM_addr
8055 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8056 high = DW_ADDR (attr_high);
8057 else
8058 high = low + DW_UNSND (attr_high);
8059 }
af34e669
DJ
8060 else
8061 /* Found high w/o low attribute. */
8062 return 0;
8063
8064 /* Found consecutive range of addresses. */
8065 ret = 1;
8066 }
c906108c 8067 else
af34e669 8068 {
e142c38c 8069 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
8070 if (attr != NULL)
8071 {
af34e669 8072 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 8073 .debug_ranges section. */
d85a05f0 8074 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
af34e669 8075 return 0;
43039443 8076 /* Found discontinuous range of addresses. */
af34e669
DJ
8077 ret = -1;
8078 }
8079 }
c906108c 8080
9373cf26
JK
8081 /* read_partial_die has also the strict LOW < HIGH requirement. */
8082 if (high <= low)
c906108c
SS
8083 return 0;
8084
8085 /* When using the GNU linker, .gnu.linkonce. sections are used to
8086 eliminate duplicate copies of functions and vtables and such.
8087 The linker will arbitrarily choose one and discard the others.
8088 The AT_*_pc values for such functions refer to local labels in
8089 these sections. If the section from that file was discarded, the
8090 labels are not in the output, so the relocs get a value of 0.
8091 If this is a discarded function, mark the pc bounds as invalid,
8092 so that GDB will ignore it. */
72dca2f5 8093 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
c906108c
SS
8094 return 0;
8095
8096 *lowpc = low;
96408a79
SA
8097 if (highpc)
8098 *highpc = high;
af34e669 8099 return ret;
c906108c
SS
8100}
8101
b084d499
JB
8102/* Assuming that DIE represents a subprogram DIE or a lexical block, get
8103 its low and high PC addresses. Do nothing if these addresses could not
8104 be determined. Otherwise, set LOWPC to the low address if it is smaller,
8105 and HIGHPC to the high address if greater than HIGHPC. */
8106
8107static void
8108dwarf2_get_subprogram_pc_bounds (struct die_info *die,
8109 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8110 struct dwarf2_cu *cu)
8111{
8112 CORE_ADDR low, high;
8113 struct die_info *child = die->child;
8114
d85a05f0 8115 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
b084d499
JB
8116 {
8117 *lowpc = min (*lowpc, low);
8118 *highpc = max (*highpc, high);
8119 }
8120
8121 /* If the language does not allow nested subprograms (either inside
8122 subprograms or lexical blocks), we're done. */
8123 if (cu->language != language_ada)
8124 return;
6e70227d 8125
b084d499
JB
8126 /* Check all the children of the given DIE. If it contains nested
8127 subprograms, then check their pc bounds. Likewise, we need to
8128 check lexical blocks as well, as they may also contain subprogram
8129 definitions. */
8130 while (child && child->tag)
8131 {
8132 if (child->tag == DW_TAG_subprogram
8133 || child->tag == DW_TAG_lexical_block)
8134 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
8135 child = sibling_die (child);
8136 }
8137}
8138
fae299cd
DC
8139/* Get the low and high pc's represented by the scope DIE, and store
8140 them in *LOWPC and *HIGHPC. If the correct values can't be
8141 determined, set *LOWPC to -1 and *HIGHPC to 0. */
8142
8143static void
8144get_scope_pc_bounds (struct die_info *die,
8145 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8146 struct dwarf2_cu *cu)
8147{
8148 CORE_ADDR best_low = (CORE_ADDR) -1;
8149 CORE_ADDR best_high = (CORE_ADDR) 0;
8150 CORE_ADDR current_low, current_high;
8151
d85a05f0 8152 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
fae299cd
DC
8153 {
8154 best_low = current_low;
8155 best_high = current_high;
8156 }
8157 else
8158 {
8159 struct die_info *child = die->child;
8160
8161 while (child && child->tag)
8162 {
8163 switch (child->tag) {
8164 case DW_TAG_subprogram:
b084d499 8165 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
8166 break;
8167 case DW_TAG_namespace:
f55ee35c 8168 case DW_TAG_module:
fae299cd
DC
8169 /* FIXME: carlton/2004-01-16: Should we do this for
8170 DW_TAG_class_type/DW_TAG_structure_type, too? I think
8171 that current GCC's always emit the DIEs corresponding
8172 to definitions of methods of classes as children of a
8173 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
8174 the DIEs giving the declarations, which could be
8175 anywhere). But I don't see any reason why the
8176 standards says that they have to be there. */
8177 get_scope_pc_bounds (child, &current_low, &current_high, cu);
8178
8179 if (current_low != ((CORE_ADDR) -1))
8180 {
8181 best_low = min (best_low, current_low);
8182 best_high = max (best_high, current_high);
8183 }
8184 break;
8185 default:
0963b4bd 8186 /* Ignore. */
fae299cd
DC
8187 break;
8188 }
8189
8190 child = sibling_die (child);
8191 }
8192 }
8193
8194 *lowpc = best_low;
8195 *highpc = best_high;
8196}
8197
801e3a5b
JB
8198/* Record the address ranges for BLOCK, offset by BASEADDR, as given
8199 in DIE. */
380bca97 8200
801e3a5b
JB
8201static void
8202dwarf2_record_block_ranges (struct die_info *die, struct block *block,
8203 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
8204{
bb5ed363 8205 struct objfile *objfile = cu->objfile;
801e3a5b 8206 struct attribute *attr;
91da1414 8207 struct attribute *attr_high;
801e3a5b 8208
91da1414
MW
8209 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
8210 if (attr_high)
801e3a5b 8211 {
801e3a5b
JB
8212 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
8213 if (attr)
8214 {
8215 CORE_ADDR low = DW_ADDR (attr);
91da1414 8216 CORE_ADDR high;
3019eac3
DE
8217 if (attr_high->form == DW_FORM_addr
8218 || attr_high->form == DW_FORM_GNU_addr_index)
91da1414
MW
8219 high = DW_ADDR (attr_high);
8220 else
8221 high = low + DW_UNSND (attr_high);
9a619af0 8222
801e3a5b
JB
8223 record_block_range (block, baseaddr + low, baseaddr + high - 1);
8224 }
8225 }
8226
8227 attr = dwarf2_attr (die, DW_AT_ranges, cu);
8228 if (attr)
8229 {
bb5ed363 8230 bfd *obfd = objfile->obfd;
801e3a5b
JB
8231
8232 /* The value of the DW_AT_ranges attribute is the offset of the
8233 address range list in the .debug_ranges section. */
8234 unsigned long offset = DW_UNSND (attr);
dce234bc 8235 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
801e3a5b
JB
8236
8237 /* For some target architectures, but not others, the
8238 read_address function sign-extends the addresses it returns.
8239 To recognize base address selection entries, we need a
8240 mask. */
8241 unsigned int addr_size = cu->header.addr_size;
8242 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
8243
8244 /* The base address, to which the next pair is relative. Note
8245 that this 'base' is a DWARF concept: most entries in a range
8246 list are relative, to reduce the number of relocs against the
8247 debugging information. This is separate from this function's
8248 'baseaddr' argument, which GDB uses to relocate debugging
8249 information from a shared library based on the address at
8250 which the library was loaded. */
d00adf39
DE
8251 CORE_ADDR base = cu->base_address;
8252 int base_known = cu->base_known;
801e3a5b 8253
be391dca 8254 gdb_assert (dwarf2_per_objfile->ranges.readin);
dce234bc 8255 if (offset >= dwarf2_per_objfile->ranges.size)
801e3a5b
JB
8256 {
8257 complaint (&symfile_complaints,
8258 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
8259 offset);
8260 return;
8261 }
8262
8263 for (;;)
8264 {
8265 unsigned int bytes_read;
8266 CORE_ADDR start, end;
8267
8268 start = read_address (obfd, buffer, cu, &bytes_read);
8269 buffer += bytes_read;
8270 end = read_address (obfd, buffer, cu, &bytes_read);
8271 buffer += bytes_read;
8272
8273 /* Did we find the end of the range list? */
8274 if (start == 0 && end == 0)
8275 break;
8276
8277 /* Did we find a base address selection entry? */
8278 else if ((start & base_select_mask) == base_select_mask)
8279 {
8280 base = end;
8281 base_known = 1;
8282 }
8283
8284 /* We found an ordinary address range. */
8285 else
8286 {
8287 if (!base_known)
8288 {
8289 complaint (&symfile_complaints,
3e43a32a
MS
8290 _("Invalid .debug_ranges data "
8291 "(no base address)"));
801e3a5b
JB
8292 return;
8293 }
8294
9277c30c
UW
8295 if (start > end)
8296 {
8297 /* Inverted range entries are invalid. */
8298 complaint (&symfile_complaints,
8299 _("Invalid .debug_ranges data "
8300 "(inverted range)"));
8301 return;
8302 }
8303
8304 /* Empty range entries have no effect. */
8305 if (start == end)
8306 continue;
8307
6e70227d
DE
8308 record_block_range (block,
8309 baseaddr + base + start,
801e3a5b
JB
8310 baseaddr + base + end - 1);
8311 }
8312 }
8313 }
8314}
8315
685b1105
JK
8316/* Check whether the producer field indicates either of GCC < 4.6, or the
8317 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 8318
685b1105
JK
8319static void
8320check_producer (struct dwarf2_cu *cu)
60d5a603
JK
8321{
8322 const char *cs;
8323 int major, minor, release;
8324
8325 if (cu->producer == NULL)
8326 {
8327 /* For unknown compilers expect their behavior is DWARF version
8328 compliant.
8329
8330 GCC started to support .debug_types sections by -gdwarf-4 since
8331 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
8332 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
8333 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
8334 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 8335 }
685b1105 8336 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
60d5a603 8337 {
685b1105
JK
8338 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
8339
ba919b58
TT
8340 cs = &cu->producer[strlen ("GNU ")];
8341 while (*cs && !isdigit (*cs))
8342 cs++;
8343 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
8344 {
8345 /* Not recognized as GCC. */
8346 }
8347 else
685b1105
JK
8348 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
8349 }
8350 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
8351 cu->producer_is_icc = 1;
8352 else
8353 {
8354 /* For other non-GCC compilers, expect their behavior is DWARF version
8355 compliant. */
60d5a603
JK
8356 }
8357
ba919b58 8358 cu->checked_producer = 1;
685b1105 8359}
ba919b58 8360
685b1105
JK
8361/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
8362 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
8363 during 4.6.0 experimental. */
8364
8365static int
8366producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
8367{
8368 if (!cu->checked_producer)
8369 check_producer (cu);
8370
8371 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
8372}
8373
8374/* Return the default accessibility type if it is not overriden by
8375 DW_AT_accessibility. */
8376
8377static enum dwarf_access_attribute
8378dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
8379{
8380 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
8381 {
8382 /* The default DWARF 2 accessibility for members is public, the default
8383 accessibility for inheritance is private. */
8384
8385 if (die->tag != DW_TAG_inheritance)
8386 return DW_ACCESS_public;
8387 else
8388 return DW_ACCESS_private;
8389 }
8390 else
8391 {
8392 /* DWARF 3+ defines the default accessibility a different way. The same
8393 rules apply now for DW_TAG_inheritance as for the members and it only
8394 depends on the container kind. */
8395
8396 if (die->parent->tag == DW_TAG_class_type)
8397 return DW_ACCESS_private;
8398 else
8399 return DW_ACCESS_public;
8400 }
8401}
8402
74ac6d43
TT
8403/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
8404 offset. If the attribute was not found return 0, otherwise return
8405 1. If it was found but could not properly be handled, set *OFFSET
8406 to 0. */
8407
8408static int
8409handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
8410 LONGEST *offset)
8411{
8412 struct attribute *attr;
8413
8414 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
8415 if (attr != NULL)
8416 {
8417 *offset = 0;
8418
8419 /* Note that we do not check for a section offset first here.
8420 This is because DW_AT_data_member_location is new in DWARF 4,
8421 so if we see it, we can assume that a constant form is really
8422 a constant and not a section offset. */
8423 if (attr_form_is_constant (attr))
8424 *offset = dwarf2_get_attr_constant_value (attr, 0);
8425 else if (attr_form_is_section_offset (attr))
8426 dwarf2_complex_location_expr_complaint ();
8427 else if (attr_form_is_block (attr))
8428 *offset = decode_locdesc (DW_BLOCK (attr), cu);
8429 else
8430 dwarf2_complex_location_expr_complaint ();
8431
8432 return 1;
8433 }
8434
8435 return 0;
8436}
8437
c906108c
SS
8438/* Add an aggregate field to the field list. */
8439
8440static void
107d2387 8441dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 8442 struct dwarf2_cu *cu)
6e70227d 8443{
e7c27a73 8444 struct objfile *objfile = cu->objfile;
5e2b427d 8445 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
8446 struct nextfield *new_field;
8447 struct attribute *attr;
8448 struct field *fp;
8449 char *fieldname = "";
8450
8451 /* Allocate a new field list entry and link it in. */
8452 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
b8c9b27d 8453 make_cleanup (xfree, new_field);
c906108c 8454 memset (new_field, 0, sizeof (struct nextfield));
7d0ccb61
DJ
8455
8456 if (die->tag == DW_TAG_inheritance)
8457 {
8458 new_field->next = fip->baseclasses;
8459 fip->baseclasses = new_field;
8460 }
8461 else
8462 {
8463 new_field->next = fip->fields;
8464 fip->fields = new_field;
8465 }
c906108c
SS
8466 fip->nfields++;
8467
e142c38c 8468 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
8469 if (attr)
8470 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
8471 else
8472 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
8473 if (new_field->accessibility != DW_ACCESS_public)
8474 fip->non_public_fields = 1;
60d5a603 8475
e142c38c 8476 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
8477 if (attr)
8478 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
8479 else
8480 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
8481
8482 fp = &new_field->field;
a9a9bd0f 8483
e142c38c 8484 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 8485 {
74ac6d43
TT
8486 LONGEST offset;
8487
a9a9bd0f 8488 /* Data member other than a C++ static data member. */
6e70227d 8489
c906108c 8490 /* Get type of field. */
e7c27a73 8491 fp->type = die_type (die, cu);
c906108c 8492
d6a843b5 8493 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 8494
c906108c 8495 /* Get bit size of field (zero if none). */
e142c38c 8496 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
8497 if (attr)
8498 {
8499 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
8500 }
8501 else
8502 {
8503 FIELD_BITSIZE (*fp) = 0;
8504 }
8505
8506 /* Get bit offset of field. */
74ac6d43
TT
8507 if (handle_data_member_location (die, cu, &offset))
8508 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 8509 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
8510 if (attr)
8511 {
5e2b427d 8512 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
8513 {
8514 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
8515 additional bit offset from the MSB of the containing
8516 anonymous object to the MSB of the field. We don't
8517 have to do anything special since we don't need to
8518 know the size of the anonymous object. */
f41f5e61 8519 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
8520 }
8521 else
8522 {
8523 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
8524 MSB of the anonymous object, subtract off the number of
8525 bits from the MSB of the field to the MSB of the
8526 object, and then subtract off the number of bits of
8527 the field itself. The result is the bit offset of
8528 the LSB of the field. */
c906108c
SS
8529 int anonymous_size;
8530 int bit_offset = DW_UNSND (attr);
8531
e142c38c 8532 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
8533 if (attr)
8534 {
8535 /* The size of the anonymous object containing
8536 the bit field is explicit, so use the
8537 indicated size (in bytes). */
8538 anonymous_size = DW_UNSND (attr);
8539 }
8540 else
8541 {
8542 /* The size of the anonymous object containing
8543 the bit field must be inferred from the type
8544 attribute of the data member containing the
8545 bit field. */
8546 anonymous_size = TYPE_LENGTH (fp->type);
8547 }
f41f5e61
PA
8548 SET_FIELD_BITPOS (*fp,
8549 (FIELD_BITPOS (*fp)
8550 + anonymous_size * bits_per_byte
8551 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
8552 }
8553 }
8554
8555 /* Get name of field. */
39cbfefa
DJ
8556 fieldname = dwarf2_name (die, cu);
8557 if (fieldname == NULL)
8558 fieldname = "";
d8151005
DJ
8559
8560 /* The name is already allocated along with this objfile, so we don't
8561 need to duplicate it for the type. */
8562 fp->name = fieldname;
c906108c
SS
8563
8564 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 8565 pointer or virtual base class pointer) to private. */
e142c38c 8566 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 8567 {
d48cc9dd 8568 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
8569 new_field->accessibility = DW_ACCESS_private;
8570 fip->non_public_fields = 1;
8571 }
8572 }
a9a9bd0f 8573 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 8574 {
a9a9bd0f
DC
8575 /* C++ static member. */
8576
8577 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
8578 is a declaration, but all versions of G++ as of this writing
8579 (so through at least 3.2.1) incorrectly generate
8580 DW_TAG_variable tags. */
6e70227d 8581
ff355380 8582 const char *physname;
c906108c 8583
a9a9bd0f 8584 /* Get name of field. */
39cbfefa
DJ
8585 fieldname = dwarf2_name (die, cu);
8586 if (fieldname == NULL)
c906108c
SS
8587 return;
8588
254e6b9e 8589 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
8590 if (attr
8591 /* Only create a symbol if this is an external value.
8592 new_symbol checks this and puts the value in the global symbol
8593 table, which we want. If it is not external, new_symbol
8594 will try to put the value in cu->list_in_scope which is wrong. */
8595 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
8596 {
8597 /* A static const member, not much different than an enum as far as
8598 we're concerned, except that we can support more types. */
8599 new_symbol (die, NULL, cu);
8600 }
8601
2df3850c 8602 /* Get physical name. */
ff355380 8603 physname = dwarf2_physname (fieldname, die, cu);
c906108c 8604
d8151005
DJ
8605 /* The name is already allocated along with this objfile, so we don't
8606 need to duplicate it for the type. */
8607 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 8608 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 8609 FIELD_NAME (*fp) = fieldname;
c906108c
SS
8610 }
8611 else if (die->tag == DW_TAG_inheritance)
8612 {
74ac6d43 8613 LONGEST offset;
d4b96c9a 8614
74ac6d43
TT
8615 /* C++ base class field. */
8616 if (handle_data_member_location (die, cu, &offset))
8617 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 8618 FIELD_BITSIZE (*fp) = 0;
e7c27a73 8619 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c
SS
8620 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
8621 fip->nbaseclasses++;
8622 }
8623}
8624
98751a41
JK
8625/* Add a typedef defined in the scope of the FIP's class. */
8626
8627static void
8628dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
8629 struct dwarf2_cu *cu)
6e70227d 8630{
98751a41 8631 struct objfile *objfile = cu->objfile;
98751a41
JK
8632 struct typedef_field_list *new_field;
8633 struct attribute *attr;
8634 struct typedef_field *fp;
8635 char *fieldname = "";
8636
8637 /* Allocate a new field list entry and link it in. */
8638 new_field = xzalloc (sizeof (*new_field));
8639 make_cleanup (xfree, new_field);
8640
8641 gdb_assert (die->tag == DW_TAG_typedef);
8642
8643 fp = &new_field->field;
8644
8645 /* Get name of field. */
8646 fp->name = dwarf2_name (die, cu);
8647 if (fp->name == NULL)
8648 return;
8649
8650 fp->type = read_type_die (die, cu);
8651
8652 new_field->next = fip->typedef_field_list;
8653 fip->typedef_field_list = new_field;
8654 fip->typedef_field_list_count++;
8655}
8656
c906108c
SS
8657/* Create the vector of fields, and attach it to the type. */
8658
8659static void
fba45db2 8660dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8661 struct dwarf2_cu *cu)
c906108c
SS
8662{
8663 int nfields = fip->nfields;
8664
8665 /* Record the field count, allocate space for the array of fields,
8666 and create blank accessibility bitfields if necessary. */
8667 TYPE_NFIELDS (type) = nfields;
8668 TYPE_FIELDS (type) = (struct field *)
8669 TYPE_ALLOC (type, sizeof (struct field) * nfields);
8670 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
8671
b4ba55a1 8672 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
8673 {
8674 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8675
8676 TYPE_FIELD_PRIVATE_BITS (type) =
8677 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8678 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
8679
8680 TYPE_FIELD_PROTECTED_BITS (type) =
8681 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8682 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
8683
774b6a14
TT
8684 TYPE_FIELD_IGNORE_BITS (type) =
8685 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
8686 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
8687 }
8688
8689 /* If the type has baseclasses, allocate and clear a bit vector for
8690 TYPE_FIELD_VIRTUAL_BITS. */
b4ba55a1 8691 if (fip->nbaseclasses && cu->language != language_ada)
c906108c
SS
8692 {
8693 int num_bytes = B_BYTES (fip->nbaseclasses);
fe1b8b76 8694 unsigned char *pointer;
c906108c
SS
8695
8696 ALLOCATE_CPLUS_STRUCT_TYPE (type);
fe1b8b76
JB
8697 pointer = TYPE_ALLOC (type, num_bytes);
8698 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
c906108c
SS
8699 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
8700 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
8701 }
8702
3e43a32a
MS
8703 /* Copy the saved-up fields into the field vector. Start from the head of
8704 the list, adding to the tail of the field array, so that they end up in
8705 the same order in the array in which they were added to the list. */
c906108c
SS
8706 while (nfields-- > 0)
8707 {
7d0ccb61
DJ
8708 struct nextfield *fieldp;
8709
8710 if (fip->fields)
8711 {
8712 fieldp = fip->fields;
8713 fip->fields = fieldp->next;
8714 }
8715 else
8716 {
8717 fieldp = fip->baseclasses;
8718 fip->baseclasses = fieldp->next;
8719 }
8720
8721 TYPE_FIELD (type, nfields) = fieldp->field;
8722 switch (fieldp->accessibility)
c906108c 8723 {
c5aa993b 8724 case DW_ACCESS_private:
b4ba55a1
JB
8725 if (cu->language != language_ada)
8726 SET_TYPE_FIELD_PRIVATE (type, nfields);
c5aa993b 8727 break;
c906108c 8728
c5aa993b 8729 case DW_ACCESS_protected:
b4ba55a1
JB
8730 if (cu->language != language_ada)
8731 SET_TYPE_FIELD_PROTECTED (type, nfields);
c5aa993b 8732 break;
c906108c 8733
c5aa993b
JM
8734 case DW_ACCESS_public:
8735 break;
c906108c 8736
c5aa993b
JM
8737 default:
8738 /* Unknown accessibility. Complain and treat it as public. */
8739 {
e2e0b3e5 8740 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7d0ccb61 8741 fieldp->accessibility);
c5aa993b
JM
8742 }
8743 break;
c906108c
SS
8744 }
8745 if (nfields < fip->nbaseclasses)
8746 {
7d0ccb61 8747 switch (fieldp->virtuality)
c906108c 8748 {
c5aa993b
JM
8749 case DW_VIRTUALITY_virtual:
8750 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 8751 if (cu->language == language_ada)
a73c6dcd 8752 error (_("unexpected virtuality in component of Ada type"));
c5aa993b
JM
8753 SET_TYPE_FIELD_VIRTUAL (type, nfields);
8754 break;
c906108c
SS
8755 }
8756 }
c906108c
SS
8757 }
8758}
8759
c906108c
SS
8760/* Add a member function to the proper fieldlist. */
8761
8762static void
107d2387 8763dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 8764 struct type *type, struct dwarf2_cu *cu)
c906108c 8765{
e7c27a73 8766 struct objfile *objfile = cu->objfile;
c906108c
SS
8767 struct attribute *attr;
8768 struct fnfieldlist *flp;
8769 int i;
8770 struct fn_field *fnp;
8771 char *fieldname;
c906108c 8772 struct nextfnfield *new_fnfield;
f792889a 8773 struct type *this_type;
60d5a603 8774 enum dwarf_access_attribute accessibility;
c906108c 8775
b4ba55a1 8776 if (cu->language == language_ada)
a73c6dcd 8777 error (_("unexpected member function in Ada type"));
b4ba55a1 8778
2df3850c 8779 /* Get name of member function. */
39cbfefa
DJ
8780 fieldname = dwarf2_name (die, cu);
8781 if (fieldname == NULL)
2df3850c 8782 return;
c906108c 8783
c906108c
SS
8784 /* Look up member function name in fieldlist. */
8785 for (i = 0; i < fip->nfnfields; i++)
8786 {
27bfe10e 8787 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
c906108c
SS
8788 break;
8789 }
8790
8791 /* Create new list element if necessary. */
8792 if (i < fip->nfnfields)
8793 flp = &fip->fnfieldlists[i];
8794 else
8795 {
8796 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
8797 {
8798 fip->fnfieldlists = (struct fnfieldlist *)
8799 xrealloc (fip->fnfieldlists,
8800 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 8801 * sizeof (struct fnfieldlist));
c906108c 8802 if (fip->nfnfields == 0)
c13c43fd 8803 make_cleanup (free_current_contents, &fip->fnfieldlists);
c906108c
SS
8804 }
8805 flp = &fip->fnfieldlists[fip->nfnfields];
8806 flp->name = fieldname;
8807 flp->length = 0;
8808 flp->head = NULL;
3da10d80 8809 i = fip->nfnfields++;
c906108c
SS
8810 }
8811
8812 /* Create a new member function field and chain it to the field list
0963b4bd 8813 entry. */
c906108c 8814 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
b8c9b27d 8815 make_cleanup (xfree, new_fnfield);
c906108c
SS
8816 memset (new_fnfield, 0, sizeof (struct nextfnfield));
8817 new_fnfield->next = flp->head;
8818 flp->head = new_fnfield;
8819 flp->length++;
8820
8821 /* Fill in the member function field info. */
8822 fnp = &new_fnfield->fnfield;
3da10d80
KS
8823
8824 /* Delay processing of the physname until later. */
8825 if (cu->language == language_cplus || cu->language == language_java)
8826 {
8827 add_to_method_list (type, i, flp->length - 1, fieldname,
8828 die, cu);
8829 }
8830 else
8831 {
1d06ead6 8832 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
8833 fnp->physname = physname ? physname : "";
8834 }
8835
c906108c 8836 fnp->type = alloc_type (objfile);
f792889a
DJ
8837 this_type = read_type_die (die, cu);
8838 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 8839 {
f792889a 8840 int nparams = TYPE_NFIELDS (this_type);
c906108c 8841
f792889a 8842 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
8843 of the method itself (TYPE_CODE_METHOD). */
8844 smash_to_method_type (fnp->type, type,
f792889a
DJ
8845 TYPE_TARGET_TYPE (this_type),
8846 TYPE_FIELDS (this_type),
8847 TYPE_NFIELDS (this_type),
8848 TYPE_VARARGS (this_type));
c906108c
SS
8849
8850 /* Handle static member functions.
c5aa993b 8851 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
8852 member functions. G++ helps GDB by marking the first
8853 parameter for non-static member functions (which is the this
8854 pointer) as artificial. We obtain this information from
8855 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 8856 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
8857 fnp->voffset = VOFFSET_STATIC;
8858 }
8859 else
e2e0b3e5 8860 complaint (&symfile_complaints, _("member function type missing for '%s'"),
3da10d80 8861 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
8862
8863 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 8864 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 8865 fnp->fcontext = die_containing_type (die, cu);
c906108c 8866
3e43a32a
MS
8867 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
8868 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
8869
8870 /* Get accessibility. */
e142c38c 8871 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 8872 if (attr)
60d5a603
JK
8873 accessibility = DW_UNSND (attr);
8874 else
8875 accessibility = dwarf2_default_access_attribute (die, cu);
8876 switch (accessibility)
c906108c 8877 {
60d5a603
JK
8878 case DW_ACCESS_private:
8879 fnp->is_private = 1;
8880 break;
8881 case DW_ACCESS_protected:
8882 fnp->is_protected = 1;
8883 break;
c906108c
SS
8884 }
8885
b02dede2 8886 /* Check for artificial methods. */
e142c38c 8887 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
8888 if (attr && DW_UNSND (attr) != 0)
8889 fnp->is_artificial = 1;
8890
0d564a31 8891 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
8892 function. For older versions of GCC, this is an offset in the
8893 appropriate virtual table, as specified by DW_AT_containing_type.
8894 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
8895 to the object address. */
8896
e142c38c 8897 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 8898 if (attr)
8e19ed76 8899 {
aec5aa8b 8900 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 8901 {
aec5aa8b
TT
8902 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
8903 {
8904 /* Old-style GCC. */
8905 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
8906 }
8907 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
8908 || (DW_BLOCK (attr)->size > 1
8909 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
8910 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
8911 {
8912 struct dwarf_block blk;
8913 int offset;
8914
8915 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
8916 ? 1 : 2);
8917 blk.size = DW_BLOCK (attr)->size - offset;
8918 blk.data = DW_BLOCK (attr)->data + offset;
8919 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
8920 if ((fnp->voffset % cu->header.addr_size) != 0)
8921 dwarf2_complex_location_expr_complaint ();
8922 else
8923 fnp->voffset /= cu->header.addr_size;
8924 fnp->voffset += 2;
8925 }
8926 else
8927 dwarf2_complex_location_expr_complaint ();
8928
8929 if (!fnp->fcontext)
8930 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
8931 }
3690dd37 8932 else if (attr_form_is_section_offset (attr))
8e19ed76 8933 {
4d3c2250 8934 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
8935 }
8936 else
8937 {
4d3c2250
KB
8938 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
8939 fieldname);
8e19ed76 8940 }
0d564a31 8941 }
d48cc9dd
DJ
8942 else
8943 {
8944 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
8945 if (attr && DW_UNSND (attr))
8946 {
8947 /* GCC does this, as of 2008-08-25; PR debug/37237. */
8948 complaint (&symfile_complaints,
3e43a32a
MS
8949 _("Member function \"%s\" (offset %d) is virtual "
8950 "but the vtable offset is not specified"),
b64f50a1 8951 fieldname, die->offset.sect_off);
9655fd1a 8952 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
8953 TYPE_CPLUS_DYNAMIC (type) = 1;
8954 }
8955 }
c906108c
SS
8956}
8957
8958/* Create the vector of member function fields, and attach it to the type. */
8959
8960static void
fba45db2 8961dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 8962 struct dwarf2_cu *cu)
c906108c
SS
8963{
8964 struct fnfieldlist *flp;
c906108c
SS
8965 int i;
8966
b4ba55a1 8967 if (cu->language == language_ada)
a73c6dcd 8968 error (_("unexpected member functions in Ada type"));
b4ba55a1 8969
c906108c
SS
8970 ALLOCATE_CPLUS_STRUCT_TYPE (type);
8971 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
8972 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
8973
8974 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
8975 {
8976 struct nextfnfield *nfp = flp->head;
8977 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
8978 int k;
8979
8980 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
8981 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
8982 fn_flp->fn_fields = (struct fn_field *)
8983 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
8984 for (k = flp->length; (k--, nfp); nfp = nfp->next)
c5aa993b 8985 fn_flp->fn_fields[k] = nfp->fnfield;
c906108c
SS
8986 }
8987
8988 TYPE_NFN_FIELDS (type) = fip->nfnfields;
c906108c
SS
8989}
8990
1168df01
JB
8991/* Returns non-zero if NAME is the name of a vtable member in CU's
8992 language, zero otherwise. */
8993static int
8994is_vtable_name (const char *name, struct dwarf2_cu *cu)
8995{
8996 static const char vptr[] = "_vptr";
987504bb 8997 static const char vtable[] = "vtable";
1168df01 8998
987504bb
JJ
8999 /* Look for the C++ and Java forms of the vtable. */
9000 if ((cu->language == language_java
9001 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
9002 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
9003 && is_cplus_marker (name[sizeof (vptr) - 1])))
1168df01
JB
9004 return 1;
9005
9006 return 0;
9007}
9008
c0dd20ea 9009/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
9010 functions, with the ABI-specified layout. If TYPE describes
9011 such a structure, smash it into a member function type.
61049d3b
DJ
9012
9013 GCC shouldn't do this; it should just output pointer to member DIEs.
9014 This is GCC PR debug/28767. */
c0dd20ea 9015
0b92b5bb
TT
9016static void
9017quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 9018{
0b92b5bb 9019 struct type *pfn_type, *domain_type, *new_type;
c0dd20ea
DJ
9020
9021 /* Check for a structure with no name and two children. */
0b92b5bb
TT
9022 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
9023 return;
c0dd20ea
DJ
9024
9025 /* Check for __pfn and __delta members. */
0b92b5bb
TT
9026 if (TYPE_FIELD_NAME (type, 0) == NULL
9027 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
9028 || TYPE_FIELD_NAME (type, 1) == NULL
9029 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
9030 return;
c0dd20ea
DJ
9031
9032 /* Find the type of the method. */
0b92b5bb 9033 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
9034 if (pfn_type == NULL
9035 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
9036 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 9037 return;
c0dd20ea
DJ
9038
9039 /* Look for the "this" argument. */
9040 pfn_type = TYPE_TARGET_TYPE (pfn_type);
9041 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 9042 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 9043 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 9044 return;
c0dd20ea
DJ
9045
9046 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb
TT
9047 new_type = alloc_type (objfile);
9048 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
9049 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
9050 TYPE_VARARGS (pfn_type));
0b92b5bb 9051 smash_to_methodptr_type (type, new_type);
c0dd20ea 9052}
1168df01 9053
685b1105
JK
9054/* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
9055 (icc). */
9056
9057static int
9058producer_is_icc (struct dwarf2_cu *cu)
9059{
9060 if (!cu->checked_producer)
9061 check_producer (cu);
9062
9063 return cu->producer_is_icc;
9064}
9065
c906108c 9066/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
9067 (definition) to create a type for the structure or union. Fill in
9068 the type's name and general properties; the members will not be
9069 processed until process_structure_type.
c906108c 9070
c767944b
DJ
9071 NOTE: we need to call these functions regardless of whether or not the
9072 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c
SS
9073 structure or union. This gets the type entered into our set of
9074 user defined types.
9075
9076 However, if the structure is incomplete (an opaque struct/union)
9077 then suppress creating a symbol table entry for it since gdb only
9078 wants to find the one with the complete definition. Note that if
9079 it is complete, we just call new_symbol, which does it's own
9080 checking about whether the struct/union is anonymous or not (and
9081 suppresses creating a symbol table entry itself). */
9082
f792889a 9083static struct type *
134d01f1 9084read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9085{
e7c27a73 9086 struct objfile *objfile = cu->objfile;
c906108c
SS
9087 struct type *type;
9088 struct attribute *attr;
39cbfefa 9089 char *name;
c906108c 9090
348e048f
DE
9091 /* If the definition of this type lives in .debug_types, read that type.
9092 Don't follow DW_AT_specification though, that will take us back up
9093 the chain and we want to go down. */
9094 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9095 if (attr)
9096 {
9097 struct dwarf2_cu *type_cu = cu;
9098 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9099
348e048f
DE
9100 /* We could just recurse on read_structure_type, but we need to call
9101 get_die_type to ensure only one type for this DIE is created.
9102 This is important, for example, because for c++ classes we need
9103 TYPE_NAME set which is only done by new_symbol. Blech. */
9104 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9105
9106 /* TYPE_CU may not be the same as CU.
9107 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9108 return set_die_type (die, type, cu);
9109 }
9110
c0dd20ea 9111 type = alloc_type (objfile);
c906108c 9112 INIT_CPLUS_SPECIFIC (type);
93311388 9113
39cbfefa
DJ
9114 name = dwarf2_name (die, cu);
9115 if (name != NULL)
c906108c 9116 {
987504bb
JJ
9117 if (cu->language == language_cplus
9118 || cu->language == language_java)
63d06c5c 9119 {
3da10d80
KS
9120 char *full_name = (char *) dwarf2_full_name (name, die, cu);
9121
9122 /* dwarf2_full_name might have already finished building the DIE's
9123 type. If so, there is no need to continue. */
9124 if (get_die_type (die, cu) != NULL)
9125 return get_die_type (die, cu);
9126
9127 TYPE_TAG_NAME (type) = full_name;
94af9270
KS
9128 if (die->tag == DW_TAG_structure_type
9129 || die->tag == DW_TAG_class_type)
9130 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c
DC
9131 }
9132 else
9133 {
d8151005
DJ
9134 /* The name is already allocated along with this objfile, so
9135 we don't need to duplicate it for the type. */
94af9270
KS
9136 TYPE_TAG_NAME (type) = (char *) name;
9137 if (die->tag == DW_TAG_class_type)
9138 TYPE_NAME (type) = TYPE_TAG_NAME (type);
63d06c5c 9139 }
c906108c
SS
9140 }
9141
9142 if (die->tag == DW_TAG_structure_type)
9143 {
9144 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9145 }
9146 else if (die->tag == DW_TAG_union_type)
9147 {
9148 TYPE_CODE (type) = TYPE_CODE_UNION;
9149 }
9150 else
9151 {
c906108c
SS
9152 TYPE_CODE (type) = TYPE_CODE_CLASS;
9153 }
9154
0cc2414c
TT
9155 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
9156 TYPE_DECLARED_CLASS (type) = 1;
9157
e142c38c 9158 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9159 if (attr)
9160 {
9161 TYPE_LENGTH (type) = DW_UNSND (attr);
9162 }
9163 else
9164 {
9165 TYPE_LENGTH (type) = 0;
9166 }
9167
685b1105
JK
9168 if (producer_is_icc (cu))
9169 {
9170 /* ICC does not output the required DW_AT_declaration
9171 on incomplete types, but gives them a size of zero. */
9172 }
9173 else
9174 TYPE_STUB_SUPPORTED (type) = 1;
9175
dc718098 9176 if (die_is_declaration (die, cu))
876cecd0 9177 TYPE_STUB (type) = 1;
a6c727b2
DJ
9178 else if (attr == NULL && die->child == NULL
9179 && producer_is_realview (cu->producer))
9180 /* RealView does not output the required DW_AT_declaration
9181 on incomplete types. */
9182 TYPE_STUB (type) = 1;
dc718098 9183
c906108c
SS
9184 /* We need to add the type field to the die immediately so we don't
9185 infinitely recurse when dealing with pointers to the structure
0963b4bd 9186 type within the structure itself. */
1c379e20 9187 set_die_type (die, type, cu);
c906108c 9188
7e314c57
JK
9189 /* set_die_type should be already done. */
9190 set_descriptive_type (type, die, cu);
9191
c767944b
DJ
9192 return type;
9193}
9194
9195/* Finish creating a structure or union type, including filling in
9196 its members and creating a symbol for it. */
9197
9198static void
9199process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
9200{
9201 struct objfile *objfile = cu->objfile;
9202 struct die_info *child_die = die->child;
9203 struct type *type;
9204
9205 type = get_die_type (die, cu);
9206 if (type == NULL)
9207 type = read_structure_type (die, cu);
9208
e142c38c 9209 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
9210 {
9211 struct field_info fi;
9212 struct die_info *child_die;
34eaf542 9213 VEC (symbolp) *template_args = NULL;
c767944b 9214 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
c906108c
SS
9215
9216 memset (&fi, 0, sizeof (struct field_info));
9217
639d11d3 9218 child_die = die->child;
c906108c
SS
9219
9220 while (child_die && child_die->tag)
9221 {
a9a9bd0f
DC
9222 if (child_die->tag == DW_TAG_member
9223 || child_die->tag == DW_TAG_variable)
c906108c 9224 {
a9a9bd0f
DC
9225 /* NOTE: carlton/2002-11-05: A C++ static data member
9226 should be a DW_TAG_member that is a declaration, but
9227 all versions of G++ as of this writing (so through at
9228 least 3.2.1) incorrectly generate DW_TAG_variable
9229 tags for them instead. */
e7c27a73 9230 dwarf2_add_field (&fi, child_die, cu);
c906108c 9231 }
8713b1b1 9232 else if (child_die->tag == DW_TAG_subprogram)
c906108c 9233 {
0963b4bd 9234 /* C++ member function. */
e7c27a73 9235 dwarf2_add_member_fn (&fi, child_die, type, cu);
c906108c
SS
9236 }
9237 else if (child_die->tag == DW_TAG_inheritance)
9238 {
9239 /* C++ base class field. */
e7c27a73 9240 dwarf2_add_field (&fi, child_die, cu);
c906108c 9241 }
98751a41
JK
9242 else if (child_die->tag == DW_TAG_typedef)
9243 dwarf2_add_typedef (&fi, child_die, cu);
34eaf542
TT
9244 else if (child_die->tag == DW_TAG_template_type_param
9245 || child_die->tag == DW_TAG_template_value_param)
9246 {
9247 struct symbol *arg = new_symbol (child_die, NULL, cu);
9248
f1078f66
DJ
9249 if (arg != NULL)
9250 VEC_safe_push (symbolp, template_args, arg);
34eaf542
TT
9251 }
9252
c906108c
SS
9253 child_die = sibling_die (child_die);
9254 }
9255
34eaf542
TT
9256 /* Attach template arguments to type. */
9257 if (! VEC_empty (symbolp, template_args))
9258 {
9259 ALLOCATE_CPLUS_STRUCT_TYPE (type);
9260 TYPE_N_TEMPLATE_ARGUMENTS (type)
9261 = VEC_length (symbolp, template_args);
9262 TYPE_TEMPLATE_ARGUMENTS (type)
9263 = obstack_alloc (&objfile->objfile_obstack,
9264 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9265 * sizeof (struct symbol *)));
9266 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
9267 VEC_address (symbolp, template_args),
9268 (TYPE_N_TEMPLATE_ARGUMENTS (type)
9269 * sizeof (struct symbol *)));
9270 VEC_free (symbolp, template_args);
9271 }
9272
c906108c
SS
9273 /* Attach fields and member functions to the type. */
9274 if (fi.nfields)
e7c27a73 9275 dwarf2_attach_fields_to_type (&fi, type, cu);
c906108c
SS
9276 if (fi.nfnfields)
9277 {
e7c27a73 9278 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 9279
c5aa993b 9280 /* Get the type which refers to the base class (possibly this
c906108c 9281 class itself) which contains the vtable pointer for the current
0d564a31
DJ
9282 class from the DW_AT_containing_type attribute. This use of
9283 DW_AT_containing_type is a GNU extension. */
c906108c 9284
e142c38c 9285 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 9286 {
e7c27a73 9287 struct type *t = die_containing_type (die, cu);
c906108c
SS
9288
9289 TYPE_VPTR_BASETYPE (type) = t;
9290 if (type == t)
9291 {
c906108c
SS
9292 int i;
9293
9294 /* Our own class provides vtbl ptr. */
9295 for (i = TYPE_NFIELDS (t) - 1;
9296 i >= TYPE_N_BASECLASSES (t);
9297 --i)
9298 {
0d5cff50 9299 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 9300
1168df01 9301 if (is_vtable_name (fieldname, cu))
c906108c
SS
9302 {
9303 TYPE_VPTR_FIELDNO (type) = i;
9304 break;
9305 }
9306 }
9307
9308 /* Complain if virtual function table field not found. */
9309 if (i < TYPE_N_BASECLASSES (t))
4d3c2250 9310 complaint (&symfile_complaints,
3e43a32a
MS
9311 _("virtual function table pointer "
9312 "not found when defining class '%s'"),
4d3c2250
KB
9313 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
9314 "");
c906108c
SS
9315 }
9316 else
9317 {
9318 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
9319 }
9320 }
f6235d4c
EZ
9321 else if (cu->producer
9322 && strncmp (cu->producer,
9323 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
9324 {
9325 /* The IBM XLC compiler does not provide direct indication
9326 of the containing type, but the vtable pointer is
9327 always named __vfp. */
9328
9329 int i;
9330
9331 for (i = TYPE_NFIELDS (type) - 1;
9332 i >= TYPE_N_BASECLASSES (type);
9333 --i)
9334 {
9335 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
9336 {
9337 TYPE_VPTR_FIELDNO (type) = i;
9338 TYPE_VPTR_BASETYPE (type) = type;
9339 break;
9340 }
9341 }
9342 }
c906108c 9343 }
98751a41
JK
9344
9345 /* Copy fi.typedef_field_list linked list elements content into the
9346 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
9347 if (fi.typedef_field_list)
9348 {
9349 int i = fi.typedef_field_list_count;
9350
a0d7a4ff 9351 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41
JK
9352 TYPE_TYPEDEF_FIELD_ARRAY (type)
9353 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
9354 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
9355
9356 /* Reverse the list order to keep the debug info elements order. */
9357 while (--i >= 0)
9358 {
9359 struct typedef_field *dest, *src;
6e70227d 9360
98751a41
JK
9361 dest = &TYPE_TYPEDEF_FIELD (type, i);
9362 src = &fi.typedef_field_list->field;
9363 fi.typedef_field_list = fi.typedef_field_list->next;
9364 *dest = *src;
9365 }
9366 }
c767944b
DJ
9367
9368 do_cleanups (back_to);
eb2a6f42
TT
9369
9370 if (HAVE_CPLUS_STRUCT (type))
9371 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
c906108c 9372 }
63d06c5c 9373
bb5ed363 9374 quirk_gcc_member_function_pointer (type, objfile);
0b92b5bb 9375
90aeadfc
DC
9376 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
9377 snapshots) has been known to create a die giving a declaration
9378 for a class that has, as a child, a die giving a definition for a
9379 nested class. So we have to process our children even if the
9380 current die is a declaration. Normally, of course, a declaration
9381 won't have any children at all. */
134d01f1 9382
90aeadfc
DC
9383 while (child_die != NULL && child_die->tag)
9384 {
9385 if (child_die->tag == DW_TAG_member
9386 || child_die->tag == DW_TAG_variable
34eaf542
TT
9387 || child_die->tag == DW_TAG_inheritance
9388 || child_die->tag == DW_TAG_template_value_param
9389 || child_die->tag == DW_TAG_template_type_param)
134d01f1 9390 {
90aeadfc 9391 /* Do nothing. */
134d01f1 9392 }
90aeadfc
DC
9393 else
9394 process_die (child_die, cu);
134d01f1 9395
90aeadfc 9396 child_die = sibling_die (child_die);
134d01f1
DJ
9397 }
9398
fa4028e9
JB
9399 /* Do not consider external references. According to the DWARF standard,
9400 these DIEs are identified by the fact that they have no byte_size
9401 attribute, and a declaration attribute. */
9402 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
9403 || !die_is_declaration (die, cu))
c767944b 9404 new_symbol (die, type, cu);
134d01f1
DJ
9405}
9406
9407/* Given a DW_AT_enumeration_type die, set its type. We do not
9408 complete the type's fields yet, or create any symbols. */
c906108c 9409
f792889a 9410static struct type *
134d01f1 9411read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9412{
e7c27a73 9413 struct objfile *objfile = cu->objfile;
c906108c 9414 struct type *type;
c906108c 9415 struct attribute *attr;
0114d602 9416 const char *name;
134d01f1 9417
348e048f
DE
9418 /* If the definition of this type lives in .debug_types, read that type.
9419 Don't follow DW_AT_specification though, that will take us back up
9420 the chain and we want to go down. */
9421 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
9422 if (attr)
9423 {
9424 struct dwarf2_cu *type_cu = cu;
9425 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
9a619af0 9426
348e048f 9427 type = read_type_die (type_die, type_cu);
9dc481d3
DE
9428
9429 /* TYPE_CU may not be the same as CU.
9430 Ensure TYPE is recorded in CU's type_hash table. */
348e048f
DE
9431 return set_die_type (die, type, cu);
9432 }
9433
c906108c
SS
9434 type = alloc_type (objfile);
9435
9436 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 9437 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 9438 if (name != NULL)
0114d602 9439 TYPE_TAG_NAME (type) = (char *) name;
c906108c 9440
e142c38c 9441 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
9442 if (attr)
9443 {
9444 TYPE_LENGTH (type) = DW_UNSND (attr);
9445 }
9446 else
9447 {
9448 TYPE_LENGTH (type) = 0;
9449 }
9450
137033e9
JB
9451 /* The enumeration DIE can be incomplete. In Ada, any type can be
9452 declared as private in the package spec, and then defined only
9453 inside the package body. Such types are known as Taft Amendment
9454 Types. When another package uses such a type, an incomplete DIE
9455 may be generated by the compiler. */
02eb380e 9456 if (die_is_declaration (die, cu))
876cecd0 9457 TYPE_STUB (type) = 1;
02eb380e 9458
f792889a 9459 return set_die_type (die, type, cu);
134d01f1
DJ
9460}
9461
9462/* Given a pointer to a die which begins an enumeration, process all
9463 the dies that define the members of the enumeration, and create the
9464 symbol for the enumeration type.
9465
9466 NOTE: We reverse the order of the element list. */
9467
9468static void
9469process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
9470{
f792889a 9471 struct type *this_type;
134d01f1 9472
f792889a
DJ
9473 this_type = get_die_type (die, cu);
9474 if (this_type == NULL)
9475 this_type = read_enumeration_type (die, cu);
9dc481d3 9476
639d11d3 9477 if (die->child != NULL)
c906108c 9478 {
9dc481d3
DE
9479 struct die_info *child_die;
9480 struct symbol *sym;
9481 struct field *fields = NULL;
9482 int num_fields = 0;
9483 int unsigned_enum = 1;
9484 char *name;
cafec441
TT
9485 int flag_enum = 1;
9486 ULONGEST mask = 0;
9dc481d3 9487
639d11d3 9488 child_die = die->child;
c906108c
SS
9489 while (child_die && child_die->tag)
9490 {
9491 if (child_die->tag != DW_TAG_enumerator)
9492 {
e7c27a73 9493 process_die (child_die, cu);
c906108c
SS
9494 }
9495 else
9496 {
39cbfefa
DJ
9497 name = dwarf2_name (child_die, cu);
9498 if (name)
c906108c 9499 {
f792889a 9500 sym = new_symbol (child_die, this_type, cu);
c906108c 9501 if (SYMBOL_VALUE (sym) < 0)
cafec441
TT
9502 {
9503 unsigned_enum = 0;
9504 flag_enum = 0;
9505 }
9506 else if ((mask & SYMBOL_VALUE (sym)) != 0)
9507 flag_enum = 0;
9508 else
9509 mask |= SYMBOL_VALUE (sym);
c906108c
SS
9510
9511 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
9512 {
9513 fields = (struct field *)
9514 xrealloc (fields,
9515 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 9516 * sizeof (struct field));
c906108c
SS
9517 }
9518
3567439c 9519 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 9520 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 9521 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
9522 FIELD_BITSIZE (fields[num_fields]) = 0;
9523
9524 num_fields++;
9525 }
9526 }
9527
9528 child_die = sibling_die (child_die);
9529 }
9530
9531 if (num_fields)
9532 {
f792889a
DJ
9533 TYPE_NFIELDS (this_type) = num_fields;
9534 TYPE_FIELDS (this_type) = (struct field *)
9535 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
9536 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 9537 sizeof (struct field) * num_fields);
b8c9b27d 9538 xfree (fields);
c906108c
SS
9539 }
9540 if (unsigned_enum)
876cecd0 9541 TYPE_UNSIGNED (this_type) = 1;
cafec441
TT
9542 if (flag_enum)
9543 TYPE_FLAG_ENUM (this_type) = 1;
c906108c 9544 }
134d01f1 9545
6c83ed52
TT
9546 /* If we are reading an enum from a .debug_types unit, and the enum
9547 is a declaration, and the enum is not the signatured type in the
9548 unit, then we do not want to add a symbol for it. Adding a
9549 symbol would in some cases obscure the true definition of the
9550 enum, giving users an incomplete type when the definition is
9551 actually available. Note that we do not want to do this for all
9552 enums which are just declarations, because C++0x allows forward
9553 enum declarations. */
3019eac3 9554 if (cu->per_cu->is_debug_types
6c83ed52
TT
9555 && die_is_declaration (die, cu))
9556 {
52dc124a 9557 struct signatured_type *sig_type;
6c83ed52 9558
52dc124a 9559 sig_type
6c83ed52 9560 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
3019eac3 9561 cu->per_cu->info_or_types_section,
6c83ed52 9562 cu->per_cu->offset);
3019eac3
DE
9563 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
9564 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
6c83ed52
TT
9565 return;
9566 }
9567
f792889a 9568 new_symbol (die, this_type, cu);
c906108c
SS
9569}
9570
9571/* Extract all information from a DW_TAG_array_type DIE and put it in
9572 the DIE's type field. For now, this only handles one dimensional
9573 arrays. */
9574
f792889a 9575static struct type *
e7c27a73 9576read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9577{
e7c27a73 9578 struct objfile *objfile = cu->objfile;
c906108c 9579 struct die_info *child_die;
7e314c57 9580 struct type *type;
c906108c
SS
9581 struct type *element_type, *range_type, *index_type;
9582 struct type **range_types = NULL;
9583 struct attribute *attr;
9584 int ndim = 0;
9585 struct cleanup *back_to;
39cbfefa 9586 char *name;
c906108c 9587
e7c27a73 9588 element_type = die_type (die, cu);
c906108c 9589
7e314c57
JK
9590 /* The die_type call above may have already set the type for this DIE. */
9591 type = get_die_type (die, cu);
9592 if (type)
9593 return type;
9594
c906108c
SS
9595 /* Irix 6.2 native cc creates array types without children for
9596 arrays with unspecified length. */
639d11d3 9597 if (die->child == NULL)
c906108c 9598 {
46bf5051 9599 index_type = objfile_type (objfile)->builtin_int;
c906108c 9600 range_type = create_range_type (NULL, index_type, 0, -1);
f792889a
DJ
9601 type = create_array_type (NULL, element_type, range_type);
9602 return set_die_type (die, type, cu);
c906108c
SS
9603 }
9604
9605 back_to = make_cleanup (null_cleanup, NULL);
639d11d3 9606 child_die = die->child;
c906108c
SS
9607 while (child_die && child_die->tag)
9608 {
9609 if (child_die->tag == DW_TAG_subrange_type)
9610 {
f792889a 9611 struct type *child_type = read_type_die (child_die, cu);
9a619af0 9612
f792889a 9613 if (child_type != NULL)
a02abb62 9614 {
0963b4bd
MS
9615 /* The range type was succesfully read. Save it for the
9616 array type creation. */
a02abb62
JB
9617 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
9618 {
9619 range_types = (struct type **)
9620 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
9621 * sizeof (struct type *));
9622 if (ndim == 0)
9623 make_cleanup (free_current_contents, &range_types);
9624 }
f792889a 9625 range_types[ndim++] = child_type;
a02abb62 9626 }
c906108c
SS
9627 }
9628 child_die = sibling_die (child_die);
9629 }
9630
9631 /* Dwarf2 dimensions are output from left to right, create the
9632 necessary array types in backwards order. */
7ca2d3a3 9633
c906108c 9634 type = element_type;
7ca2d3a3
DL
9635
9636 if (read_array_order (die, cu) == DW_ORD_col_major)
9637 {
9638 int i = 0;
9a619af0 9639
7ca2d3a3
DL
9640 while (i < ndim)
9641 type = create_array_type (NULL, type, range_types[i++]);
9642 }
9643 else
9644 {
9645 while (ndim-- > 0)
9646 type = create_array_type (NULL, type, range_types[ndim]);
9647 }
c906108c 9648
f5f8a009
EZ
9649 /* Understand Dwarf2 support for vector types (like they occur on
9650 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
9651 array type. This is not part of the Dwarf2/3 standard yet, but a
9652 custom vendor extension. The main difference between a regular
9653 array and the vector variant is that vectors are passed by value
9654 to functions. */
e142c38c 9655 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 9656 if (attr)
ea37ba09 9657 make_vector_type (type);
f5f8a009 9658
dbc98a8b
KW
9659 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
9660 implementation may choose to implement triple vectors using this
9661 attribute. */
9662 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9663 if (attr)
9664 {
9665 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
9666 TYPE_LENGTH (type) = DW_UNSND (attr);
9667 else
3e43a32a
MS
9668 complaint (&symfile_complaints,
9669 _("DW_AT_byte_size for array type smaller "
9670 "than the total size of elements"));
dbc98a8b
KW
9671 }
9672
39cbfefa
DJ
9673 name = dwarf2_name (die, cu);
9674 if (name)
9675 TYPE_NAME (type) = name;
6e70227d 9676
0963b4bd 9677 /* Install the type in the die. */
7e314c57
JK
9678 set_die_type (die, type, cu);
9679
9680 /* set_die_type should be already done. */
b4ba55a1
JB
9681 set_descriptive_type (type, die, cu);
9682
c906108c
SS
9683 do_cleanups (back_to);
9684
7e314c57 9685 return type;
c906108c
SS
9686}
9687
7ca2d3a3 9688static enum dwarf_array_dim_ordering
6e70227d 9689read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
9690{
9691 struct attribute *attr;
9692
9693 attr = dwarf2_attr (die, DW_AT_ordering, cu);
9694
9695 if (attr) return DW_SND (attr);
9696
0963b4bd
MS
9697 /* GNU F77 is a special case, as at 08/2004 array type info is the
9698 opposite order to the dwarf2 specification, but data is still
9699 laid out as per normal fortran.
7ca2d3a3 9700
0963b4bd
MS
9701 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
9702 version checking. */
7ca2d3a3 9703
905e0470
PM
9704 if (cu->language == language_fortran
9705 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
9706 {
9707 return DW_ORD_row_major;
9708 }
9709
6e70227d 9710 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
9711 {
9712 case array_column_major:
9713 return DW_ORD_col_major;
9714 case array_row_major:
9715 default:
9716 return DW_ORD_row_major;
9717 };
9718}
9719
72019c9c 9720/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 9721 the DIE's type field. */
72019c9c 9722
f792889a 9723static struct type *
72019c9c
GM
9724read_set_type (struct die_info *die, struct dwarf2_cu *cu)
9725{
7e314c57
JK
9726 struct type *domain_type, *set_type;
9727 struct attribute *attr;
f792889a 9728
7e314c57
JK
9729 domain_type = die_type (die, cu);
9730
9731 /* The die_type call above may have already set the type for this DIE. */
9732 set_type = get_die_type (die, cu);
9733 if (set_type)
9734 return set_type;
9735
9736 set_type = create_set_type (NULL, domain_type);
9737
9738 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
9739 if (attr)
9740 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 9741
f792889a 9742 return set_die_type (die, set_type, cu);
72019c9c 9743}
7ca2d3a3 9744
c906108c
SS
9745/* First cut: install each common block member as a global variable. */
9746
9747static void
e7c27a73 9748read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
9749{
9750 struct die_info *child_die;
9751 struct attribute *attr;
9752 struct symbol *sym;
9753 CORE_ADDR base = (CORE_ADDR) 0;
9754
e142c38c 9755 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
9756 if (attr)
9757 {
0963b4bd 9758 /* Support the .debug_loc offsets. */
8e19ed76
PS
9759 if (attr_form_is_block (attr))
9760 {
e7c27a73 9761 base = decode_locdesc (DW_BLOCK (attr), cu);
8e19ed76 9762 }
3690dd37 9763 else if (attr_form_is_section_offset (attr))
8e19ed76 9764 {
4d3c2250 9765 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
9766 }
9767 else
9768 {
4d3c2250
KB
9769 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9770 "common block member");
8e19ed76 9771 }
c906108c 9772 }
639d11d3 9773 if (die->child != NULL)
c906108c 9774 {
639d11d3 9775 child_die = die->child;
c906108c
SS
9776 while (child_die && child_die->tag)
9777 {
74ac6d43
TT
9778 LONGEST offset;
9779
e7c27a73 9780 sym = new_symbol (child_die, NULL, cu);
e8d28ef4
TT
9781 if (sym != NULL
9782 && handle_data_member_location (child_die, cu, &offset))
c906108c 9783 {
74ac6d43 9784 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
c906108c
SS
9785 add_symbol_to_list (sym, &global_symbols);
9786 }
9787 child_die = sibling_die (child_die);
9788 }
9789 }
9790}
9791
0114d602 9792/* Create a type for a C++ namespace. */
d9fa45fe 9793
0114d602
DJ
9794static struct type *
9795read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 9796{
e7c27a73 9797 struct objfile *objfile = cu->objfile;
0114d602 9798 const char *previous_prefix, *name;
9219021c 9799 int is_anonymous;
0114d602
DJ
9800 struct type *type;
9801
9802 /* For extensions, reuse the type of the original namespace. */
9803 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
9804 {
9805 struct die_info *ext_die;
9806 struct dwarf2_cu *ext_cu = cu;
9a619af0 9807
0114d602
DJ
9808 ext_die = dwarf2_extension (die, &ext_cu);
9809 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
9810
9811 /* EXT_CU may not be the same as CU.
9812 Ensure TYPE is recorded in CU's type_hash table. */
0114d602
DJ
9813 return set_die_type (die, type, cu);
9814 }
9219021c 9815
e142c38c 9816 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
9817
9818 /* Now build the name of the current namespace. */
9819
0114d602
DJ
9820 previous_prefix = determine_prefix (die, cu);
9821 if (previous_prefix[0] != '\0')
9822 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 9823 previous_prefix, name, 0, cu);
0114d602
DJ
9824
9825 /* Create the type. */
9826 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
9827 objfile);
9828 TYPE_NAME (type) = (char *) name;
9829 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9830
60531b24 9831 return set_die_type (die, type, cu);
0114d602
DJ
9832}
9833
9834/* Read a C++ namespace. */
9835
9836static void
9837read_namespace (struct die_info *die, struct dwarf2_cu *cu)
9838{
9839 struct objfile *objfile = cu->objfile;
0114d602 9840 int is_anonymous;
9219021c 9841
5c4e30ca
DC
9842 /* Add a symbol associated to this if we haven't seen the namespace
9843 before. Also, add a using directive if it's an anonymous
9844 namespace. */
9219021c 9845
f2f0e013 9846 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
9847 {
9848 struct type *type;
9849
0114d602 9850 type = read_type_die (die, cu);
e7c27a73 9851 new_symbol (die, type, cu);
5c4e30ca 9852
e8e80198 9853 namespace_name (die, &is_anonymous, cu);
5c4e30ca 9854 if (is_anonymous)
0114d602
DJ
9855 {
9856 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 9857
c0cc3a76 9858 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
32019081 9859 NULL, NULL, &objfile->objfile_obstack);
0114d602 9860 }
5c4e30ca 9861 }
9219021c 9862
639d11d3 9863 if (die->child != NULL)
d9fa45fe 9864 {
639d11d3 9865 struct die_info *child_die = die->child;
6e70227d 9866
d9fa45fe
DC
9867 while (child_die && child_die->tag)
9868 {
e7c27a73 9869 process_die (child_die, cu);
d9fa45fe
DC
9870 child_die = sibling_die (child_die);
9871 }
9872 }
38d518c9
EZ
9873}
9874
f55ee35c
JK
9875/* Read a Fortran module as type. This DIE can be only a declaration used for
9876 imported module. Still we need that type as local Fortran "use ... only"
9877 declaration imports depend on the created type in determine_prefix. */
9878
9879static struct type *
9880read_module_type (struct die_info *die, struct dwarf2_cu *cu)
9881{
9882 struct objfile *objfile = cu->objfile;
9883 char *module_name;
9884 struct type *type;
9885
9886 module_name = dwarf2_name (die, cu);
9887 if (!module_name)
3e43a32a
MS
9888 complaint (&symfile_complaints,
9889 _("DW_TAG_module has no name, offset 0x%x"),
b64f50a1 9890 die->offset.sect_off);
f55ee35c
JK
9891 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
9892
9893 /* determine_prefix uses TYPE_TAG_NAME. */
9894 TYPE_TAG_NAME (type) = TYPE_NAME (type);
9895
9896 return set_die_type (die, type, cu);
9897}
9898
5d7cb8df
JK
9899/* Read a Fortran module. */
9900
9901static void
9902read_module (struct die_info *die, struct dwarf2_cu *cu)
9903{
9904 struct die_info *child_die = die->child;
9905
5d7cb8df
JK
9906 while (child_die && child_die->tag)
9907 {
9908 process_die (child_die, cu);
9909 child_die = sibling_die (child_die);
9910 }
9911}
9912
38d518c9
EZ
9913/* Return the name of the namespace represented by DIE. Set
9914 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
9915 namespace. */
9916
9917static const char *
e142c38c 9918namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
9919{
9920 struct die_info *current_die;
9921 const char *name = NULL;
9922
9923 /* Loop through the extensions until we find a name. */
9924
9925 for (current_die = die;
9926 current_die != NULL;
f2f0e013 9927 current_die = dwarf2_extension (die, &cu))
38d518c9 9928 {
e142c38c 9929 name = dwarf2_name (current_die, cu);
38d518c9
EZ
9930 if (name != NULL)
9931 break;
9932 }
9933
9934 /* Is it an anonymous namespace? */
9935
9936 *is_anonymous = (name == NULL);
9937 if (*is_anonymous)
2b1dbab0 9938 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
9939
9940 return name;
d9fa45fe
DC
9941}
9942
c906108c
SS
9943/* Extract all information from a DW_TAG_pointer_type DIE and add to
9944 the user defined type vector. */
9945
f792889a 9946static struct type *
e7c27a73 9947read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9948{
5e2b427d 9949 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
e7c27a73 9950 struct comp_unit_head *cu_header = &cu->header;
c906108c 9951 struct type *type;
8b2dbe47
KB
9952 struct attribute *attr_byte_size;
9953 struct attribute *attr_address_class;
9954 int byte_size, addr_class;
7e314c57
JK
9955 struct type *target_type;
9956
9957 target_type = die_type (die, cu);
c906108c 9958
7e314c57
JK
9959 /* The die_type call above may have already set the type for this DIE. */
9960 type = get_die_type (die, cu);
9961 if (type)
9962 return type;
9963
9964 type = lookup_pointer_type (target_type);
8b2dbe47 9965
e142c38c 9966 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
9967 if (attr_byte_size)
9968 byte_size = DW_UNSND (attr_byte_size);
c906108c 9969 else
8b2dbe47
KB
9970 byte_size = cu_header->addr_size;
9971
e142c38c 9972 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
9973 if (attr_address_class)
9974 addr_class = DW_UNSND (attr_address_class);
9975 else
9976 addr_class = DW_ADDR_none;
9977
9978 /* If the pointer size or address class is different than the
9979 default, create a type variant marked as such and set the
9980 length accordingly. */
9981 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
c906108c 9982 {
5e2b427d 9983 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
9984 {
9985 int type_flags;
9986
849957d9 9987 type_flags = gdbarch_address_class_type_flags
5e2b427d 9988 (gdbarch, byte_size, addr_class);
876cecd0
TT
9989 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
9990 == 0);
8b2dbe47
KB
9991 type = make_type_with_address_space (type, type_flags);
9992 }
9993 else if (TYPE_LENGTH (type) != byte_size)
9994 {
3e43a32a
MS
9995 complaint (&symfile_complaints,
9996 _("invalid pointer size %d"), byte_size);
8b2dbe47 9997 }
6e70227d 9998 else
9a619af0
MS
9999 {
10000 /* Should we also complain about unhandled address classes? */
10001 }
c906108c 10002 }
8b2dbe47
KB
10003
10004 TYPE_LENGTH (type) = byte_size;
f792889a 10005 return set_die_type (die, type, cu);
c906108c
SS
10006}
10007
10008/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
10009 the user defined type vector. */
10010
f792889a 10011static struct type *
e7c27a73 10012read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
10013{
10014 struct type *type;
10015 struct type *to_type;
10016 struct type *domain;
10017
e7c27a73
DJ
10018 to_type = die_type (die, cu);
10019 domain = die_containing_type (die, cu);
0d5de010 10020
7e314c57
JK
10021 /* The calls above may have already set the type for this DIE. */
10022 type = get_die_type (die, cu);
10023 if (type)
10024 return type;
10025
0d5de010
DJ
10026 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
10027 type = lookup_methodptr_type (to_type);
10028 else
10029 type = lookup_memberptr_type (to_type, domain);
c906108c 10030
f792889a 10031 return set_die_type (die, type, cu);
c906108c
SS
10032}
10033
10034/* Extract all information from a DW_TAG_reference_type DIE and add to
10035 the user defined type vector. */
10036
f792889a 10037static struct type *
e7c27a73 10038read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10039{
e7c27a73 10040 struct comp_unit_head *cu_header = &cu->header;
7e314c57 10041 struct type *type, *target_type;
c906108c
SS
10042 struct attribute *attr;
10043
7e314c57
JK
10044 target_type = die_type (die, cu);
10045
10046 /* The die_type call above may have already set the type for this DIE. */
10047 type = get_die_type (die, cu);
10048 if (type)
10049 return type;
10050
10051 type = lookup_reference_type (target_type);
e142c38c 10052 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10053 if (attr)
10054 {
10055 TYPE_LENGTH (type) = DW_UNSND (attr);
10056 }
10057 else
10058 {
107d2387 10059 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 10060 }
f792889a 10061 return set_die_type (die, type, cu);
c906108c
SS
10062}
10063
f792889a 10064static struct type *
e7c27a73 10065read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10066{
f792889a 10067 struct type *base_type, *cv_type;
c906108c 10068
e7c27a73 10069 base_type = die_type (die, cu);
7e314c57
JK
10070
10071 /* The die_type call above may have already set the type for this DIE. */
10072 cv_type = get_die_type (die, cu);
10073 if (cv_type)
10074 return cv_type;
10075
2f608a3a
KW
10076 /* In case the const qualifier is applied to an array type, the element type
10077 is so qualified, not the array type (section 6.7.3 of C99). */
10078 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
10079 {
10080 struct type *el_type, *inner_array;
10081
10082 base_type = copy_type (base_type);
10083 inner_array = base_type;
10084
10085 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
10086 {
10087 TYPE_TARGET_TYPE (inner_array) =
10088 copy_type (TYPE_TARGET_TYPE (inner_array));
10089 inner_array = TYPE_TARGET_TYPE (inner_array);
10090 }
10091
10092 el_type = TYPE_TARGET_TYPE (inner_array);
10093 TYPE_TARGET_TYPE (inner_array) =
10094 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
10095
10096 return set_die_type (die, base_type, cu);
10097 }
10098
f792889a
DJ
10099 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
10100 return set_die_type (die, cv_type, cu);
c906108c
SS
10101}
10102
f792889a 10103static struct type *
e7c27a73 10104read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10105{
f792889a 10106 struct type *base_type, *cv_type;
c906108c 10107
e7c27a73 10108 base_type = die_type (die, cu);
7e314c57
JK
10109
10110 /* The die_type call above may have already set the type for this DIE. */
10111 cv_type = get_die_type (die, cu);
10112 if (cv_type)
10113 return cv_type;
10114
f792889a
DJ
10115 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
10116 return set_die_type (die, cv_type, cu);
c906108c
SS
10117}
10118
10119/* Extract all information from a DW_TAG_string_type DIE and add to
10120 the user defined type vector. It isn't really a user defined type,
10121 but it behaves like one, with other DIE's using an AT_user_def_type
10122 attribute to reference it. */
10123
f792889a 10124static struct type *
e7c27a73 10125read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10126{
e7c27a73 10127 struct objfile *objfile = cu->objfile;
3b7538c0 10128 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
10129 struct type *type, *range_type, *index_type, *char_type;
10130 struct attribute *attr;
10131 unsigned int length;
10132
e142c38c 10133 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
10134 if (attr)
10135 {
10136 length = DW_UNSND (attr);
10137 }
10138 else
10139 {
0963b4bd 10140 /* Check for the DW_AT_byte_size attribute. */
e142c38c 10141 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
10142 if (attr)
10143 {
10144 length = DW_UNSND (attr);
10145 }
10146 else
10147 {
10148 length = 1;
10149 }
c906108c 10150 }
6ccb9162 10151
46bf5051 10152 index_type = objfile_type (objfile)->builtin_int;
c906108c 10153 range_type = create_range_type (NULL, index_type, 1, length);
3b7538c0
UW
10154 char_type = language_string_char_type (cu->language_defn, gdbarch);
10155 type = create_string_type (NULL, char_type, range_type);
6ccb9162 10156
f792889a 10157 return set_die_type (die, type, cu);
c906108c
SS
10158}
10159
10160/* Handle DIES due to C code like:
10161
10162 struct foo
c5aa993b
JM
10163 {
10164 int (*funcp)(int a, long l);
10165 int b;
10166 };
c906108c 10167
0963b4bd 10168 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 10169
f792889a 10170static struct type *
e7c27a73 10171read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10172{
bb5ed363 10173 struct objfile *objfile = cu->objfile;
0963b4bd
MS
10174 struct type *type; /* Type that this function returns. */
10175 struct type *ftype; /* Function that returns above type. */
c906108c
SS
10176 struct attribute *attr;
10177
e7c27a73 10178 type = die_type (die, cu);
7e314c57
JK
10179
10180 /* The die_type call above may have already set the type for this DIE. */
10181 ftype = get_die_type (die, cu);
10182 if (ftype)
10183 return ftype;
10184
0c8b41f1 10185 ftype = lookup_function_type (type);
c906108c 10186
5b8101ae 10187 /* All functions in C++, Pascal and Java have prototypes. */
e142c38c 10188 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c906108c 10189 if ((attr && (DW_UNSND (attr) != 0))
987504bb 10190 || cu->language == language_cplus
5b8101ae
PM
10191 || cu->language == language_java
10192 || cu->language == language_pascal)
876cecd0 10193 TYPE_PROTOTYPED (ftype) = 1;
a6c727b2
DJ
10194 else if (producer_is_realview (cu->producer))
10195 /* RealView does not emit DW_AT_prototyped. We can not
10196 distinguish prototyped and unprototyped functions; default to
10197 prototyped, since that is more common in modern code (and
10198 RealView warns about unprototyped functions). */
10199 TYPE_PROTOTYPED (ftype) = 1;
c906108c 10200
c055b101
CV
10201 /* Store the calling convention in the type if it's available in
10202 the subroutine die. Otherwise set the calling convention to
10203 the default value DW_CC_normal. */
10204 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
10205 if (attr)
10206 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
10207 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
10208 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
10209 else
10210 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2
GM
10211
10212 /* We need to add the subroutine type to the die immediately so
10213 we don't infinitely recurse when dealing with parameters
0963b4bd 10214 declared as the same subroutine type. */
76c10ea2 10215 set_die_type (die, ftype, cu);
6e70227d 10216
639d11d3 10217 if (die->child != NULL)
c906108c 10218 {
bb5ed363 10219 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 10220 struct die_info *child_die;
8072405b 10221 int nparams, iparams;
c906108c
SS
10222
10223 /* Count the number of parameters.
10224 FIXME: GDB currently ignores vararg functions, but knows about
10225 vararg member functions. */
8072405b 10226 nparams = 0;
639d11d3 10227 child_die = die->child;
c906108c
SS
10228 while (child_die && child_die->tag)
10229 {
10230 if (child_die->tag == DW_TAG_formal_parameter)
10231 nparams++;
10232 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 10233 TYPE_VARARGS (ftype) = 1;
c906108c
SS
10234 child_die = sibling_die (child_die);
10235 }
10236
10237 /* Allocate storage for parameters and fill them in. */
10238 TYPE_NFIELDS (ftype) = nparams;
10239 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 10240 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 10241
8072405b
JK
10242 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
10243 even if we error out during the parameters reading below. */
10244 for (iparams = 0; iparams < nparams; iparams++)
10245 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
10246
10247 iparams = 0;
639d11d3 10248 child_die = die->child;
c906108c
SS
10249 while (child_die && child_die->tag)
10250 {
10251 if (child_die->tag == DW_TAG_formal_parameter)
10252 {
3ce3b1ba
PA
10253 struct type *arg_type;
10254
10255 /* DWARF version 2 has no clean way to discern C++
10256 static and non-static member functions. G++ helps
10257 GDB by marking the first parameter for non-static
10258 member functions (which is the this pointer) as
10259 artificial. We pass this information to
10260 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
10261
10262 DWARF version 3 added DW_AT_object_pointer, which GCC
10263 4.5 does not yet generate. */
e142c38c 10264 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
10265 if (attr)
10266 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
10267 else
418835cc
KS
10268 {
10269 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
10270
10271 /* GCC/43521: In java, the formal parameter
10272 "this" is sometimes not marked with DW_AT_artificial. */
10273 if (cu->language == language_java)
10274 {
10275 const char *name = dwarf2_name (child_die, cu);
9a619af0 10276
418835cc
KS
10277 if (name && !strcmp (name, "this"))
10278 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
10279 }
10280 }
3ce3b1ba
PA
10281 arg_type = die_type (child_die, cu);
10282
10283 /* RealView does not mark THIS as const, which the testsuite
10284 expects. GCC marks THIS as const in method definitions,
10285 but not in the class specifications (GCC PR 43053). */
10286 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
10287 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
10288 {
10289 int is_this = 0;
10290 struct dwarf2_cu *arg_cu = cu;
10291 const char *name = dwarf2_name (child_die, cu);
10292
10293 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
10294 if (attr)
10295 {
10296 /* If the compiler emits this, use it. */
10297 if (follow_die_ref (die, attr, &arg_cu) == child_die)
10298 is_this = 1;
10299 }
10300 else if (name && strcmp (name, "this") == 0)
10301 /* Function definitions will have the argument names. */
10302 is_this = 1;
10303 else if (name == NULL && iparams == 0)
10304 /* Declarations may not have the names, so like
10305 elsewhere in GDB, assume an artificial first
10306 argument is "this". */
10307 is_this = 1;
10308
10309 if (is_this)
10310 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
10311 arg_type, 0);
10312 }
10313
10314 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
10315 iparams++;
10316 }
10317 child_die = sibling_die (child_die);
10318 }
10319 }
10320
76c10ea2 10321 return ftype;
c906108c
SS
10322}
10323
f792889a 10324static struct type *
e7c27a73 10325read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10326{
e7c27a73 10327 struct objfile *objfile = cu->objfile;
0114d602 10328 const char *name = NULL;
3c8e0968 10329 struct type *this_type, *target_type;
c906108c 10330
94af9270 10331 name = dwarf2_full_name (NULL, die, cu);
f792889a 10332 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
0114d602
DJ
10333 TYPE_FLAG_TARGET_STUB, NULL, objfile);
10334 TYPE_NAME (this_type) = (char *) name;
f792889a 10335 set_die_type (die, this_type, cu);
3c8e0968
DE
10336 target_type = die_type (die, cu);
10337 if (target_type != this_type)
10338 TYPE_TARGET_TYPE (this_type) = target_type;
10339 else
10340 {
10341 /* Self-referential typedefs are, it seems, not allowed by the DWARF
10342 spec and cause infinite loops in GDB. */
10343 complaint (&symfile_complaints,
10344 _("Self-referential DW_TAG_typedef "
10345 "- DIE at 0x%x [in module %s]"),
b64f50a1 10346 die->offset.sect_off, objfile->name);
3c8e0968
DE
10347 TYPE_TARGET_TYPE (this_type) = NULL;
10348 }
f792889a 10349 return this_type;
c906108c
SS
10350}
10351
10352/* Find a representation of a given base type and install
10353 it in the TYPE field of the die. */
10354
f792889a 10355static struct type *
e7c27a73 10356read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10357{
e7c27a73 10358 struct objfile *objfile = cu->objfile;
c906108c
SS
10359 struct type *type;
10360 struct attribute *attr;
10361 int encoding = 0, size = 0;
39cbfefa 10362 char *name;
6ccb9162
UW
10363 enum type_code code = TYPE_CODE_INT;
10364 int type_flags = 0;
10365 struct type *target_type = NULL;
c906108c 10366
e142c38c 10367 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
10368 if (attr)
10369 {
10370 encoding = DW_UNSND (attr);
10371 }
e142c38c 10372 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
10373 if (attr)
10374 {
10375 size = DW_UNSND (attr);
10376 }
39cbfefa 10377 name = dwarf2_name (die, cu);
6ccb9162 10378 if (!name)
c906108c 10379 {
6ccb9162
UW
10380 complaint (&symfile_complaints,
10381 _("DW_AT_name missing from DW_TAG_base_type"));
c906108c 10382 }
6ccb9162
UW
10383
10384 switch (encoding)
c906108c 10385 {
6ccb9162
UW
10386 case DW_ATE_address:
10387 /* Turn DW_ATE_address into a void * pointer. */
10388 code = TYPE_CODE_PTR;
10389 type_flags |= TYPE_FLAG_UNSIGNED;
10390 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
10391 break;
10392 case DW_ATE_boolean:
10393 code = TYPE_CODE_BOOL;
10394 type_flags |= TYPE_FLAG_UNSIGNED;
10395 break;
10396 case DW_ATE_complex_float:
10397 code = TYPE_CODE_COMPLEX;
10398 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
10399 break;
10400 case DW_ATE_decimal_float:
10401 code = TYPE_CODE_DECFLOAT;
10402 break;
10403 case DW_ATE_float:
10404 code = TYPE_CODE_FLT;
10405 break;
10406 case DW_ATE_signed:
10407 break;
10408 case DW_ATE_unsigned:
10409 type_flags |= TYPE_FLAG_UNSIGNED;
3b2b8fea
TT
10410 if (cu->language == language_fortran
10411 && name
10412 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
10413 code = TYPE_CODE_CHAR;
6ccb9162
UW
10414 break;
10415 case DW_ATE_signed_char:
6e70227d 10416 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10417 || cu->language == language_pascal
10418 || cu->language == language_fortran)
6ccb9162
UW
10419 code = TYPE_CODE_CHAR;
10420 break;
10421 case DW_ATE_unsigned_char:
868a0084 10422 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
10423 || cu->language == language_pascal
10424 || cu->language == language_fortran)
6ccb9162
UW
10425 code = TYPE_CODE_CHAR;
10426 type_flags |= TYPE_FLAG_UNSIGNED;
10427 break;
75079b2b
TT
10428 case DW_ATE_UTF:
10429 /* We just treat this as an integer and then recognize the
10430 type by name elsewhere. */
10431 break;
10432
6ccb9162
UW
10433 default:
10434 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
10435 dwarf_type_encoding_name (encoding));
10436 break;
c906108c 10437 }
6ccb9162 10438
0114d602
DJ
10439 type = init_type (code, size, type_flags, NULL, objfile);
10440 TYPE_NAME (type) = name;
6ccb9162
UW
10441 TYPE_TARGET_TYPE (type) = target_type;
10442
0114d602 10443 if (name && strcmp (name, "char") == 0)
876cecd0 10444 TYPE_NOSIGN (type) = 1;
0114d602 10445
f792889a 10446 return set_die_type (die, type, cu);
c906108c
SS
10447}
10448
a02abb62
JB
10449/* Read the given DW_AT_subrange DIE. */
10450
f792889a 10451static struct type *
a02abb62
JB
10452read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
10453{
10454 struct type *base_type;
10455 struct type *range_type;
10456 struct attribute *attr;
4fae6e18
JK
10457 LONGEST low, high;
10458 int low_default_is_valid;
39cbfefa 10459 char *name;
43bbcdc2 10460 LONGEST negative_mask;
e77813c8 10461
a02abb62 10462 base_type = die_type (die, cu);
953ac07e
JK
10463 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
10464 check_typedef (base_type);
a02abb62 10465
7e314c57
JK
10466 /* The die_type call above may have already set the type for this DIE. */
10467 range_type = get_die_type (die, cu);
10468 if (range_type)
10469 return range_type;
10470
4fae6e18
JK
10471 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
10472 omitting DW_AT_lower_bound. */
10473 switch (cu->language)
6e70227d 10474 {
4fae6e18
JK
10475 case language_c:
10476 case language_cplus:
10477 low = 0;
10478 low_default_is_valid = 1;
10479 break;
10480 case language_fortran:
10481 low = 1;
10482 low_default_is_valid = 1;
10483 break;
10484 case language_d:
10485 case language_java:
10486 case language_objc:
10487 low = 0;
10488 low_default_is_valid = (cu->header.version >= 4);
10489 break;
10490 case language_ada:
10491 case language_m2:
10492 case language_pascal:
a02abb62 10493 low = 1;
4fae6e18
JK
10494 low_default_is_valid = (cu->header.version >= 4);
10495 break;
10496 default:
10497 low = 0;
10498 low_default_is_valid = 0;
10499 break;
a02abb62
JB
10500 }
10501
dd5e6932
DJ
10502 /* FIXME: For variable sized arrays either of these could be
10503 a variable rather than a constant value. We'll allow it,
10504 but we don't know how to handle it. */
e142c38c 10505 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 10506 if (attr)
4fae6e18
JK
10507 low = dwarf2_get_attr_constant_value (attr, low);
10508 else if (!low_default_is_valid)
10509 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
10510 "- DIE at 0x%x [in module %s]"),
10511 die->offset.sect_off, cu->objfile->name);
a02abb62 10512
e142c38c 10513 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
a02abb62 10514 if (attr)
6e70227d 10515 {
d48323d8 10516 if (attr_form_is_block (attr) || is_ref_attr (attr))
a02abb62
JB
10517 {
10518 /* GCC encodes arrays with unspecified or dynamic length
e77813c8 10519 with a DW_FORM_block1 attribute or a reference attribute.
a02abb62
JB
10520 FIXME: GDB does not yet know how to handle dynamic
10521 arrays properly, treat them as arrays with unspecified
10522 length for now.
10523
10524 FIXME: jimb/2003-09-22: GDB does not really know
10525 how to handle arrays of unspecified length
10526 either; we just represent them as zero-length
10527 arrays. Choose an appropriate upper bound given
10528 the lower bound we've computed above. */
10529 high = low - 1;
10530 }
10531 else
10532 high = dwarf2_get_attr_constant_value (attr, 1);
10533 }
e77813c8
PM
10534 else
10535 {
10536 attr = dwarf2_attr (die, DW_AT_count, cu);
10537 if (attr)
10538 {
10539 int count = dwarf2_get_attr_constant_value (attr, 1);
10540 high = low + count - 1;
10541 }
c2ff108b
JK
10542 else
10543 {
10544 /* Unspecified array length. */
10545 high = low - 1;
10546 }
e77813c8
PM
10547 }
10548
10549 /* Dwarf-2 specifications explicitly allows to create subrange types
10550 without specifying a base type.
10551 In that case, the base type must be set to the type of
10552 the lower bound, upper bound or count, in that order, if any of these
10553 three attributes references an object that has a type.
10554 If no base type is found, the Dwarf-2 specifications say that
10555 a signed integer type of size equal to the size of an address should
10556 be used.
10557 For the following C code: `extern char gdb_int [];'
10558 GCC produces an empty range DIE.
10559 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 10560 high bound or count are not yet handled by this code. */
e77813c8
PM
10561 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
10562 {
10563 struct objfile *objfile = cu->objfile;
10564 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10565 int addr_size = gdbarch_addr_bit (gdbarch) /8;
10566 struct type *int_type = objfile_type (objfile)->builtin_int;
10567
10568 /* Test "int", "long int", and "long long int" objfile types,
10569 and select the first one having a size above or equal to the
10570 architecture address size. */
10571 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10572 base_type = int_type;
10573 else
10574 {
10575 int_type = objfile_type (objfile)->builtin_long;
10576 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10577 base_type = int_type;
10578 else
10579 {
10580 int_type = objfile_type (objfile)->builtin_long_long;
10581 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
10582 base_type = int_type;
10583 }
10584 }
10585 }
a02abb62 10586
6e70227d 10587 negative_mask =
43bbcdc2
PH
10588 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
10589 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
10590 low |= negative_mask;
10591 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
10592 high |= negative_mask;
10593
a02abb62
JB
10594 range_type = create_range_type (NULL, base_type, low, high);
10595
bbb0eef6
JK
10596 /* Mark arrays with dynamic length at least as an array of unspecified
10597 length. GDB could check the boundary but before it gets implemented at
10598 least allow accessing the array elements. */
d48323d8 10599 if (attr && attr_form_is_block (attr))
bbb0eef6
JK
10600 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10601
c2ff108b
JK
10602 /* Ada expects an empty array on no boundary attributes. */
10603 if (attr == NULL && cu->language != language_ada)
10604 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
10605
39cbfefa
DJ
10606 name = dwarf2_name (die, cu);
10607 if (name)
10608 TYPE_NAME (range_type) = name;
6e70227d 10609
e142c38c 10610 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
10611 if (attr)
10612 TYPE_LENGTH (range_type) = DW_UNSND (attr);
10613
7e314c57
JK
10614 set_die_type (die, range_type, cu);
10615
10616 /* set_die_type should be already done. */
b4ba55a1
JB
10617 set_descriptive_type (range_type, die, cu);
10618
7e314c57 10619 return range_type;
a02abb62 10620}
6e70227d 10621
f792889a 10622static struct type *
81a17f79
JB
10623read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
10624{
10625 struct type *type;
81a17f79 10626
81a17f79
JB
10627 /* For now, we only support the C meaning of an unspecified type: void. */
10628
0114d602
DJ
10629 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
10630 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 10631
f792889a 10632 return set_die_type (die, type, cu);
81a17f79 10633}
a02abb62 10634
639d11d3
DC
10635/* Read a single die and all its descendents. Set the die's sibling
10636 field to NULL; set other fields in the die correctly, and set all
10637 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
10638 location of the info_ptr after reading all of those dies. PARENT
10639 is the parent of the die in question. */
10640
10641static struct die_info *
dee91e82
DE
10642read_die_and_children (const struct die_reader_specs *reader,
10643 gdb_byte *info_ptr,
10644 gdb_byte **new_info_ptr,
10645 struct die_info *parent)
639d11d3
DC
10646{
10647 struct die_info *die;
fe1b8b76 10648 gdb_byte *cur_ptr;
639d11d3
DC
10649 int has_children;
10650
93311388 10651 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
1d325ec1
DJ
10652 if (die == NULL)
10653 {
10654 *new_info_ptr = cur_ptr;
10655 return NULL;
10656 }
93311388 10657 store_in_ref_table (die, reader->cu);
639d11d3
DC
10658
10659 if (has_children)
348e048f 10660 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
10661 else
10662 {
10663 die->child = NULL;
10664 *new_info_ptr = cur_ptr;
10665 }
10666
10667 die->sibling = NULL;
10668 die->parent = parent;
10669 return die;
10670}
10671
10672/* Read a die, all of its descendents, and all of its siblings; set
10673 all of the fields of all of the dies correctly. Arguments are as
10674 in read_die_and_children. */
10675
10676static struct die_info *
93311388
DE
10677read_die_and_siblings (const struct die_reader_specs *reader,
10678 gdb_byte *info_ptr,
fe1b8b76 10679 gdb_byte **new_info_ptr,
639d11d3
DC
10680 struct die_info *parent)
10681{
10682 struct die_info *first_die, *last_sibling;
fe1b8b76 10683 gdb_byte *cur_ptr;
639d11d3 10684
c906108c 10685 cur_ptr = info_ptr;
639d11d3
DC
10686 first_die = last_sibling = NULL;
10687
10688 while (1)
c906108c 10689 {
639d11d3 10690 struct die_info *die
dee91e82 10691 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 10692
1d325ec1 10693 if (die == NULL)
c906108c 10694 {
639d11d3
DC
10695 *new_info_ptr = cur_ptr;
10696 return first_die;
c906108c 10697 }
1d325ec1
DJ
10698
10699 if (!first_die)
10700 first_die = die;
c906108c 10701 else
1d325ec1
DJ
10702 last_sibling->sibling = die;
10703
10704 last_sibling = die;
c906108c 10705 }
c906108c
SS
10706}
10707
3019eac3
DE
10708/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
10709 attributes.
10710 The caller is responsible for filling in the extra attributes
10711 and updating (*DIEP)->num_attrs.
10712 Set DIEP to point to a newly allocated die with its information,
10713 except for its child, sibling, and parent fields.
10714 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388
DE
10715
10716static gdb_byte *
3019eac3
DE
10717read_full_die_1 (const struct die_reader_specs *reader,
10718 struct die_info **diep, gdb_byte *info_ptr,
10719 int *has_children, int num_extra_attrs)
93311388 10720{
b64f50a1
JK
10721 unsigned int abbrev_number, bytes_read, i;
10722 sect_offset offset;
93311388
DE
10723 struct abbrev_info *abbrev;
10724 struct die_info *die;
10725 struct dwarf2_cu *cu = reader->cu;
10726 bfd *abfd = reader->abfd;
10727
b64f50a1 10728 offset.sect_off = info_ptr - reader->buffer;
93311388
DE
10729 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10730 info_ptr += bytes_read;
10731 if (!abbrev_number)
10732 {
10733 *diep = NULL;
10734 *has_children = 0;
10735 return info_ptr;
10736 }
10737
10738 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
10739 if (!abbrev)
348e048f
DE
10740 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
10741 abbrev_number,
10742 bfd_get_filename (abfd));
10743
3019eac3 10744 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
93311388
DE
10745 die->offset = offset;
10746 die->tag = abbrev->tag;
10747 die->abbrev = abbrev_number;
10748
3019eac3
DE
10749 /* Make the result usable.
10750 The caller needs to update num_attrs after adding the extra
10751 attributes. */
93311388
DE
10752 die->num_attrs = abbrev->num_attrs;
10753
10754 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
10755 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
10756 info_ptr);
93311388
DE
10757
10758 *diep = die;
10759 *has_children = abbrev->has_children;
10760 return info_ptr;
10761}
10762
3019eac3
DE
10763/* Read a die and all its attributes.
10764 Set DIEP to point to a newly allocated die with its information,
10765 except for its child, sibling, and parent fields.
10766 Set HAS_CHILDREN to tell whether the die has children or not. */
10767
10768static gdb_byte *
10769read_full_die (const struct die_reader_specs *reader,
10770 struct die_info **diep, gdb_byte *info_ptr,
10771 int *has_children)
10772{
10773 return read_full_die_1 (reader, diep, info_ptr, has_children, 0);
10774}
10775
c906108c
SS
10776/* In DWARF version 2, the description of the debugging information is
10777 stored in a separate .debug_abbrev section. Before we read any
10778 dies from a section we read in all abbreviations and install them
72bf9492
DJ
10779 in a hash table. This function also sets flags in CU describing
10780 the data found in the abbrev table. */
c906108c
SS
10781
10782static void
dee91e82
DE
10783dwarf2_read_abbrevs (struct dwarf2_cu *cu,
10784 struct dwarf2_section_info *abbrev_section)
10785
c906108c 10786{
dee91e82 10787 bfd *abfd = abbrev_section->asection->owner;
e7c27a73 10788 struct comp_unit_head *cu_header = &cu->header;
fe1b8b76 10789 gdb_byte *abbrev_ptr;
c906108c
SS
10790 struct abbrev_info *cur_abbrev;
10791 unsigned int abbrev_number, bytes_read, abbrev_name;
10792 unsigned int abbrev_form, hash_number;
f3dd6933
DJ
10793 struct attr_abbrev *cur_attrs;
10794 unsigned int allocated_attrs;
c906108c 10795
0963b4bd 10796 /* Initialize dwarf2 abbrevs. */
f3dd6933
DJ
10797 obstack_init (&cu->abbrev_obstack);
10798 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
10799 (ABBREV_HASH_SIZE
10800 * sizeof (struct abbrev_info *)));
10801 memset (cu->dwarf2_abbrevs, 0,
10802 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
c906108c 10803
dee91e82
DE
10804 dwarf2_read_section (cu->objfile, abbrev_section);
10805 abbrev_ptr = abbrev_section->buffer + cu_header->abbrev_offset.sect_off;
c906108c
SS
10806 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10807 abbrev_ptr += bytes_read;
10808
f3dd6933
DJ
10809 allocated_attrs = ATTR_ALLOC_CHUNK;
10810 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
6e70227d 10811
0963b4bd 10812 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
10813 while (abbrev_number)
10814 {
f3dd6933 10815 cur_abbrev = dwarf_alloc_abbrev (cu);
c906108c
SS
10816
10817 /* read in abbrev header */
10818 cur_abbrev->number = abbrev_number;
10819 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10820 abbrev_ptr += bytes_read;
10821 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
10822 abbrev_ptr += 1;
10823
10824 /* now read in declarations */
10825 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10826 abbrev_ptr += bytes_read;
10827 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10828 abbrev_ptr += bytes_read;
10829 while (abbrev_name)
10830 {
f3dd6933 10831 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 10832 {
f3dd6933
DJ
10833 allocated_attrs += ATTR_ALLOC_CHUNK;
10834 cur_attrs
10835 = xrealloc (cur_attrs, (allocated_attrs
10836 * sizeof (struct attr_abbrev)));
c906108c 10837 }
ae038cb0 10838
f3dd6933
DJ
10839 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
10840 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
c906108c
SS
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 }
10846
f3dd6933
DJ
10847 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
10848 (cur_abbrev->num_attrs
10849 * sizeof (struct attr_abbrev)));
10850 memcpy (cur_abbrev->attrs, cur_attrs,
10851 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
10852
c906108c 10853 hash_number = abbrev_number % ABBREV_HASH_SIZE;
f3dd6933
DJ
10854 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
10855 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
c906108c
SS
10856
10857 /* Get next abbreviation.
10858 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
10859 always properly terminated with an abbrev number of 0.
10860 Exit loop if we encounter an abbreviation which we have
10861 already read (which means we are about to read the abbreviations
10862 for the next compile unit) or if the end of the abbreviation
10863 table is reached. */
dee91e82
DE
10864 if ((unsigned int) (abbrev_ptr - abbrev_section->buffer)
10865 >= abbrev_section->size)
c906108c
SS
10866 break;
10867 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
10868 abbrev_ptr += bytes_read;
e7c27a73 10869 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
c906108c
SS
10870 break;
10871 }
f3dd6933
DJ
10872
10873 xfree (cur_attrs);
c906108c
SS
10874}
10875
f3dd6933 10876/* Release the memory used by the abbrev table for a compilation unit. */
c906108c 10877
c906108c 10878static void
f3dd6933 10879dwarf2_free_abbrev_table (void *ptr_to_cu)
c906108c 10880{
f3dd6933 10881 struct dwarf2_cu *cu = ptr_to_cu;
c906108c 10882
f3dd6933
DJ
10883 obstack_free (&cu->abbrev_obstack, NULL);
10884 cu->dwarf2_abbrevs = NULL;
c906108c
SS
10885}
10886
10887/* Lookup an abbrev_info structure in the abbrev hash table. */
10888
10889static struct abbrev_info *
e7c27a73 10890dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
c906108c
SS
10891{
10892 unsigned int hash_number;
10893 struct abbrev_info *abbrev;
10894
10895 hash_number = number % ABBREV_HASH_SIZE;
f3dd6933 10896 abbrev = cu->dwarf2_abbrevs[hash_number];
c906108c
SS
10897
10898 while (abbrev)
10899 {
10900 if (abbrev->number == number)
10901 return abbrev;
10902 else
10903 abbrev = abbrev->next;
10904 }
10905 return NULL;
10906}
10907
72bf9492
DJ
10908/* Returns nonzero if TAG represents a type that we might generate a partial
10909 symbol for. */
10910
10911static int
10912is_type_tag_for_partial (int tag)
10913{
10914 switch (tag)
10915 {
10916#if 0
10917 /* Some types that would be reasonable to generate partial symbols for,
10918 that we don't at present. */
10919 case DW_TAG_array_type:
10920 case DW_TAG_file_type:
10921 case DW_TAG_ptr_to_member_type:
10922 case DW_TAG_set_type:
10923 case DW_TAG_string_type:
10924 case DW_TAG_subroutine_type:
10925#endif
10926 case DW_TAG_base_type:
10927 case DW_TAG_class_type:
680b30c7 10928 case DW_TAG_interface_type:
72bf9492
DJ
10929 case DW_TAG_enumeration_type:
10930 case DW_TAG_structure_type:
10931 case DW_TAG_subrange_type:
10932 case DW_TAG_typedef:
10933 case DW_TAG_union_type:
10934 return 1;
10935 default:
10936 return 0;
10937 }
10938}
10939
10940/* Load all DIEs that are interesting for partial symbols into memory. */
10941
10942static struct partial_die_info *
dee91e82
DE
10943load_partial_dies (const struct die_reader_specs *reader,
10944 gdb_byte *info_ptr, int building_psymtab)
72bf9492 10945{
dee91e82 10946 struct dwarf2_cu *cu = reader->cu;
bb5ed363 10947 struct objfile *objfile = cu->objfile;
72bf9492
DJ
10948 struct partial_die_info *part_die;
10949 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
10950 struct abbrev_info *abbrev;
10951 unsigned int bytes_read;
5afb4e99 10952 unsigned int load_all = 0;
72bf9492
DJ
10953 int nesting_level = 1;
10954
10955 parent_die = NULL;
10956 last_die = NULL;
10957
7adf1e79
DE
10958 gdb_assert (cu->per_cu != NULL);
10959 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
10960 load_all = 1;
10961
72bf9492
DJ
10962 cu->partial_dies
10963 = htab_create_alloc_ex (cu->header.length / 12,
10964 partial_die_hash,
10965 partial_die_eq,
10966 NULL,
10967 &cu->comp_unit_obstack,
10968 hashtab_obstack_allocate,
10969 dummy_obstack_deallocate);
10970
10971 part_die = obstack_alloc (&cu->comp_unit_obstack,
10972 sizeof (struct partial_die_info));
10973
10974 while (1)
10975 {
10976 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
10977
10978 /* A NULL abbrev means the end of a series of children. */
10979 if (abbrev == NULL)
10980 {
10981 if (--nesting_level == 0)
10982 {
10983 /* PART_DIE was probably the last thing allocated on the
10984 comp_unit_obstack, so we could call obstack_free
10985 here. We don't do that because the waste is small,
10986 and will be cleaned up when we're done with this
10987 compilation unit. This way, we're also more robust
10988 against other users of the comp_unit_obstack. */
10989 return first_die;
10990 }
10991 info_ptr += bytes_read;
10992 last_die = parent_die;
10993 parent_die = parent_die->die_parent;
10994 continue;
10995 }
10996
98bfdba5
PA
10997 /* Check for template arguments. We never save these; if
10998 they're seen, we just mark the parent, and go on our way. */
10999 if (parent_die != NULL
11000 && cu->language == language_cplus
11001 && (abbrev->tag == DW_TAG_template_type_param
11002 || abbrev->tag == DW_TAG_template_value_param))
11003 {
11004 parent_die->has_template_arguments = 1;
11005
11006 if (!load_all)
11007 {
11008 /* We don't need a partial DIE for the template argument. */
dee91e82 11009 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
11010 continue;
11011 }
11012 }
11013
0d99eb77 11014 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
11015 Skip their other children. */
11016 if (!load_all
11017 && cu->language == language_cplus
11018 && parent_die != NULL
11019 && parent_die->tag == DW_TAG_subprogram)
11020 {
dee91e82 11021 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
11022 continue;
11023 }
11024
5afb4e99
DJ
11025 /* Check whether this DIE is interesting enough to save. Normally
11026 we would not be interested in members here, but there may be
11027 later variables referencing them via DW_AT_specification (for
11028 static members). */
11029 if (!load_all
11030 && !is_type_tag_for_partial (abbrev->tag)
72929c62 11031 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
11032 && abbrev->tag != DW_TAG_enumerator
11033 && abbrev->tag != DW_TAG_subprogram
bc30ff58 11034 && abbrev->tag != DW_TAG_lexical_block
72bf9492 11035 && abbrev->tag != DW_TAG_variable
5afb4e99 11036 && abbrev->tag != DW_TAG_namespace
f55ee35c 11037 && abbrev->tag != DW_TAG_module
95554aad
TT
11038 && abbrev->tag != DW_TAG_member
11039 && abbrev->tag != DW_TAG_imported_unit)
72bf9492
DJ
11040 {
11041 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11042 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
11043 continue;
11044 }
11045
dee91e82
DE
11046 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
11047 info_ptr);
72bf9492
DJ
11048
11049 /* This two-pass algorithm for processing partial symbols has a
11050 high cost in cache pressure. Thus, handle some simple cases
11051 here which cover the majority of C partial symbols. DIEs
11052 which neither have specification tags in them, nor could have
11053 specification tags elsewhere pointing at them, can simply be
11054 processed and discarded.
11055
11056 This segment is also optional; scan_partial_symbols and
11057 add_partial_symbol will handle these DIEs if we chain
11058 them in normally. When compilers which do not emit large
11059 quantities of duplicate debug information are more common,
11060 this code can probably be removed. */
11061
11062 /* Any complete simple types at the top level (pretty much all
11063 of them, for a language without namespaces), can be processed
11064 directly. */
11065 if (parent_die == NULL
11066 && part_die->has_specification == 0
11067 && part_die->is_declaration == 0
d8228535 11068 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
72bf9492
DJ
11069 || part_die->tag == DW_TAG_base_type
11070 || part_die->tag == DW_TAG_subrange_type))
11071 {
11072 if (building_psymtab && part_die->name != NULL)
04a679b8 11073 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11074 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363
DE
11075 &objfile->static_psymbols,
11076 0, (CORE_ADDR) 0, cu->language, objfile);
dee91e82 11077 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11078 continue;
11079 }
11080
d8228535
JK
11081 /* The exception for DW_TAG_typedef with has_children above is
11082 a workaround of GCC PR debug/47510. In the case of this complaint
11083 type_name_no_tag_or_error will error on such types later.
11084
11085 GDB skipped children of DW_TAG_typedef by the shortcut above and then
11086 it could not find the child DIEs referenced later, this is checked
11087 above. In correct DWARF DW_TAG_typedef should have no children. */
11088
11089 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
11090 complaint (&symfile_complaints,
11091 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
11092 "- DIE at 0x%x [in module %s]"),
b64f50a1 11093 part_die->offset.sect_off, objfile->name);
d8228535 11094
72bf9492
DJ
11095 /* If we're at the second level, and we're an enumerator, and
11096 our parent has no specification (meaning possibly lives in a
11097 namespace elsewhere), then we can add the partial symbol now
11098 instead of queueing it. */
11099 if (part_die->tag == DW_TAG_enumerator
11100 && parent_die != NULL
11101 && parent_die->die_parent == NULL
11102 && parent_die->tag == DW_TAG_enumeration_type
11103 && parent_die->has_specification == 0)
11104 {
11105 if (part_die->name == NULL)
3e43a32a
MS
11106 complaint (&symfile_complaints,
11107 _("malformed enumerator DIE ignored"));
72bf9492 11108 else if (building_psymtab)
04a679b8 11109 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
72bf9492 11110 VAR_DOMAIN, LOC_CONST,
987504bb
JJ
11111 (cu->language == language_cplus
11112 || cu->language == language_java)
bb5ed363
DE
11113 ? &objfile->global_psymbols
11114 : &objfile->static_psymbols,
11115 0, (CORE_ADDR) 0, cu->language, objfile);
72bf9492 11116
dee91e82 11117 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
72bf9492
DJ
11118 continue;
11119 }
11120
11121 /* We'll save this DIE so link it in. */
11122 part_die->die_parent = parent_die;
11123 part_die->die_sibling = NULL;
11124 part_die->die_child = NULL;
11125
11126 if (last_die && last_die == parent_die)
11127 last_die->die_child = part_die;
11128 else if (last_die)
11129 last_die->die_sibling = part_die;
11130
11131 last_die = part_die;
11132
11133 if (first_die == NULL)
11134 first_die = part_die;
11135
11136 /* Maybe add the DIE to the hash table. Not all DIEs that we
11137 find interesting need to be in the hash table, because we
11138 also have the parent/sibling/child chains; only those that we
11139 might refer to by offset later during partial symbol reading.
11140
11141 For now this means things that might have be the target of a
11142 DW_AT_specification, DW_AT_abstract_origin, or
11143 DW_AT_extension. DW_AT_extension will refer only to
11144 namespaces; DW_AT_abstract_origin refers to functions (and
11145 many things under the function DIE, but we do not recurse
11146 into function DIEs during partial symbol reading) and
11147 possibly variables as well; DW_AT_specification refers to
11148 declarations. Declarations ought to have the DW_AT_declaration
11149 flag. It happens that GCC forgets to put it in sometimes, but
11150 only for functions, not for types.
11151
11152 Adding more things than necessary to the hash table is harmless
11153 except for the performance cost. Adding too few will result in
5afb4e99
DJ
11154 wasted time in find_partial_die, when we reread the compilation
11155 unit with load_all_dies set. */
72bf9492 11156
5afb4e99 11157 if (load_all
72929c62 11158 || abbrev->tag == DW_TAG_constant
5afb4e99 11159 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
11160 || abbrev->tag == DW_TAG_variable
11161 || abbrev->tag == DW_TAG_namespace
11162 || part_die->is_declaration)
11163 {
11164 void **slot;
11165
11166 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
b64f50a1 11167 part_die->offset.sect_off, INSERT);
72bf9492
DJ
11168 *slot = part_die;
11169 }
11170
11171 part_die = obstack_alloc (&cu->comp_unit_obstack,
11172 sizeof (struct partial_die_info));
11173
11174 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 11175 we have no reason to follow the children of structures; for other
98bfdba5
PA
11176 languages we have to, so that we can get at method physnames
11177 to infer fully qualified class names, for DW_AT_specification,
11178 and for C++ template arguments. For C++, we also look one level
11179 inside functions to find template arguments (if the name of the
11180 function does not already contain the template arguments).
bc30ff58
JB
11181
11182 For Ada, we need to scan the children of subprograms and lexical
11183 blocks as well because Ada allows the definition of nested
11184 entities that could be interesting for the debugger, such as
11185 nested subprograms for instance. */
72bf9492 11186 if (last_die->has_children
5afb4e99
DJ
11187 && (load_all
11188 || last_die->tag == DW_TAG_namespace
f55ee35c 11189 || last_die->tag == DW_TAG_module
72bf9492 11190 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
11191 || (cu->language == language_cplus
11192 && last_die->tag == DW_TAG_subprogram
11193 && (last_die->name == NULL
11194 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
11195 || (cu->language != language_c
11196 && (last_die->tag == DW_TAG_class_type
680b30c7 11197 || last_die->tag == DW_TAG_interface_type
72bf9492 11198 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
11199 || last_die->tag == DW_TAG_union_type))
11200 || (cu->language == language_ada
11201 && (last_die->tag == DW_TAG_subprogram
11202 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
11203 {
11204 nesting_level++;
11205 parent_die = last_die;
11206 continue;
11207 }
11208
11209 /* Otherwise we skip to the next sibling, if any. */
dee91e82 11210 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
11211
11212 /* Back to the top, do it again. */
11213 }
11214}
11215
c906108c
SS
11216/* Read a minimal amount of information into the minimal die structure. */
11217
fe1b8b76 11218static gdb_byte *
dee91e82
DE
11219read_partial_die (const struct die_reader_specs *reader,
11220 struct partial_die_info *part_die,
11221 struct abbrev_info *abbrev, unsigned int abbrev_len,
11222 gdb_byte *info_ptr)
c906108c 11223{
dee91e82 11224 struct dwarf2_cu *cu = reader->cu;
bb5ed363 11225 struct objfile *objfile = cu->objfile;
dee91e82 11226 gdb_byte *buffer = reader->buffer;
fa238c03 11227 unsigned int i;
c906108c 11228 struct attribute attr;
c5aa993b 11229 int has_low_pc_attr = 0;
c906108c 11230 int has_high_pc_attr = 0;
91da1414 11231 int high_pc_relative = 0;
c906108c 11232
72bf9492 11233 memset (part_die, 0, sizeof (struct partial_die_info));
c906108c 11234
b64f50a1 11235 part_die->offset.sect_off = info_ptr - buffer;
72bf9492
DJ
11236
11237 info_ptr += abbrev_len;
11238
11239 if (abbrev == NULL)
11240 return info_ptr;
11241
c906108c
SS
11242 part_die->tag = abbrev->tag;
11243 part_die->has_children = abbrev->has_children;
c906108c
SS
11244
11245 for (i = 0; i < abbrev->num_attrs; ++i)
11246 {
dee91e82 11247 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
c906108c
SS
11248
11249 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 11250 partial symbol table. */
c906108c
SS
11251 switch (attr.name)
11252 {
11253 case DW_AT_name:
71c25dea
TT
11254 switch (part_die->tag)
11255 {
11256 case DW_TAG_compile_unit:
95554aad 11257 case DW_TAG_partial_unit:
348e048f 11258 case DW_TAG_type_unit:
71c25dea
TT
11259 /* Compilation units have a DW_AT_name that is a filename, not
11260 a source language identifier. */
11261 case DW_TAG_enumeration_type:
11262 case DW_TAG_enumerator:
11263 /* These tags always have simple identifiers already; no need
11264 to canonicalize them. */
11265 part_die->name = DW_STRING (&attr);
11266 break;
11267 default:
11268 part_die->name
11269 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
bb5ed363 11270 &objfile->objfile_obstack);
71c25dea
TT
11271 break;
11272 }
c906108c 11273 break;
31ef98ae 11274 case DW_AT_linkage_name:
c906108c 11275 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
11276 /* Note that both forms of linkage name might appear. We
11277 assume they will be the same, and we only store the last
11278 one we see. */
94af9270
KS
11279 if (cu->language == language_ada)
11280 part_die->name = DW_STRING (&attr);
abc72ce4 11281 part_die->linkage_name = DW_STRING (&attr);
c906108c
SS
11282 break;
11283 case DW_AT_low_pc:
11284 has_low_pc_attr = 1;
11285 part_die->lowpc = DW_ADDR (&attr);
11286 break;
11287 case DW_AT_high_pc:
11288 has_high_pc_attr = 1;
3019eac3
DE
11289 if (attr.form == DW_FORM_addr
11290 || attr.form == DW_FORM_GNU_addr_index)
91da1414
MW
11291 part_die->highpc = DW_ADDR (&attr);
11292 else
11293 {
11294 high_pc_relative = 1;
11295 part_die->highpc = DW_UNSND (&attr);
11296 }
c906108c
SS
11297 break;
11298 case DW_AT_location:
0963b4bd 11299 /* Support the .debug_loc offsets. */
8e19ed76
PS
11300 if (attr_form_is_block (&attr))
11301 {
95554aad 11302 part_die->d.locdesc = DW_BLOCK (&attr);
8e19ed76 11303 }
3690dd37 11304 else if (attr_form_is_section_offset (&attr))
8e19ed76 11305 {
4d3c2250 11306 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
11307 }
11308 else
11309 {
4d3c2250
KB
11310 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
11311 "partial symbol information");
8e19ed76 11312 }
c906108c 11313 break;
c906108c
SS
11314 case DW_AT_external:
11315 part_die->is_external = DW_UNSND (&attr);
11316 break;
11317 case DW_AT_declaration:
11318 part_die->is_declaration = DW_UNSND (&attr);
11319 break;
11320 case DW_AT_type:
11321 part_die->has_type = 1;
11322 break;
11323 case DW_AT_abstract_origin:
11324 case DW_AT_specification:
72bf9492
DJ
11325 case DW_AT_extension:
11326 part_die->has_specification = 1;
c764a876 11327 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
c906108c
SS
11328 break;
11329 case DW_AT_sibling:
11330 /* Ignore absolute siblings, they might point outside of
11331 the current compile unit. */
11332 if (attr.form == DW_FORM_ref_addr)
3e43a32a
MS
11333 complaint (&symfile_complaints,
11334 _("ignoring absolute DW_AT_sibling"));
c906108c 11335 else
b64f50a1 11336 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
c906108c 11337 break;
fa4028e9
JB
11338 case DW_AT_byte_size:
11339 part_die->has_byte_size = 1;
11340 break;
68511cec
CES
11341 case DW_AT_calling_convention:
11342 /* DWARF doesn't provide a way to identify a program's source-level
11343 entry point. DW_AT_calling_convention attributes are only meant
11344 to describe functions' calling conventions.
11345
11346 However, because it's a necessary piece of information in
11347 Fortran, and because DW_CC_program is the only piece of debugging
11348 information whose definition refers to a 'main program' at all,
11349 several compilers have begun marking Fortran main programs with
11350 DW_CC_program --- even when those functions use the standard
11351 calling conventions.
11352
11353 So until DWARF specifies a way to provide this information and
11354 compilers pick up the new representation, we'll support this
11355 practice. */
11356 if (DW_UNSND (&attr) == DW_CC_program
11357 && cu->language == language_fortran)
01f8c46d
JK
11358 {
11359 set_main_name (part_die->name);
11360
11361 /* As this DIE has a static linkage the name would be difficult
11362 to look up later. */
11363 language_of_main = language_fortran;
11364 }
68511cec 11365 break;
481860b3
GB
11366 case DW_AT_inline:
11367 if (DW_UNSND (&attr) == DW_INL_inlined
11368 || DW_UNSND (&attr) == DW_INL_declared_inlined)
11369 part_die->may_be_inlined = 1;
11370 break;
95554aad
TT
11371
11372 case DW_AT_import:
11373 if (part_die->tag == DW_TAG_imported_unit)
11374 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
11375 break;
11376
c906108c
SS
11377 default:
11378 break;
11379 }
11380 }
11381
91da1414
MW
11382 if (high_pc_relative)
11383 part_die->highpc += part_die->lowpc;
11384
9373cf26
JK
11385 if (has_low_pc_attr && has_high_pc_attr)
11386 {
11387 /* When using the GNU linker, .gnu.linkonce. sections are used to
11388 eliminate duplicate copies of functions and vtables and such.
11389 The linker will arbitrarily choose one and discard the others.
11390 The AT_*_pc values for such functions refer to local labels in
11391 these sections. If the section from that file was discarded, the
11392 labels are not in the output, so the relocs get a value of 0.
11393 If this is a discarded function, mark the pc bounds as invalid,
11394 so that GDB will ignore it. */
11395 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
11396 {
bb5ed363 11397 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11398
11399 complaint (&symfile_complaints,
11400 _("DW_AT_low_pc %s is zero "
11401 "for DIE at 0x%x [in module %s]"),
11402 paddress (gdbarch, part_die->lowpc),
b64f50a1 11403 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11404 }
11405 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
11406 else if (part_die->lowpc >= part_die->highpc)
11407 {
bb5ed363 11408 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26
JK
11409
11410 complaint (&symfile_complaints,
11411 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
11412 "for DIE at 0x%x [in module %s]"),
11413 paddress (gdbarch, part_die->lowpc),
11414 paddress (gdbarch, part_die->highpc),
b64f50a1 11415 part_die->offset.sect_off, objfile->name);
9373cf26
JK
11416 }
11417 else
11418 part_die->has_pc_info = 1;
11419 }
85cbf3d3 11420
c906108c
SS
11421 return info_ptr;
11422}
11423
72bf9492
DJ
11424/* Find a cached partial DIE at OFFSET in CU. */
11425
11426static struct partial_die_info *
b64f50a1 11427find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
72bf9492
DJ
11428{
11429 struct partial_die_info *lookup_die = NULL;
11430 struct partial_die_info part_die;
11431
11432 part_die.offset = offset;
b64f50a1
JK
11433 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
11434 offset.sect_off);
72bf9492 11435
72bf9492
DJ
11436 return lookup_die;
11437}
11438
348e048f
DE
11439/* Find a partial DIE at OFFSET, which may or may not be in CU,
11440 except in the case of .debug_types DIEs which do not reference
11441 outside their CU (they do however referencing other types via
55f1336d 11442 DW_FORM_ref_sig8). */
72bf9492
DJ
11443
11444static struct partial_die_info *
b64f50a1 11445find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
72bf9492 11446{
bb5ed363 11447 struct objfile *objfile = cu->objfile;
5afb4e99
DJ
11448 struct dwarf2_per_cu_data *per_cu = NULL;
11449 struct partial_die_info *pd = NULL;
72bf9492 11450
45452591 11451 if (offset_in_cu_p (&cu->header, offset))
5afb4e99
DJ
11452 {
11453 pd = find_partial_die_in_comp_unit (offset, cu);
11454 if (pd != NULL)
11455 return pd;
0d99eb77
DE
11456 /* We missed recording what we needed.
11457 Load all dies and try again. */
11458 per_cu = cu->per_cu;
5afb4e99 11459 }
0d99eb77
DE
11460 else
11461 {
11462 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 11463 if (cu->per_cu->is_debug_types)
0d99eb77
DE
11464 {
11465 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
11466 " external reference to offset 0x%lx [in module %s].\n"),
11467 (long) cu->header.offset.sect_off, (long) offset.sect_off,
11468 bfd_get_filename (objfile->obfd));
11469 }
11470 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
72bf9492 11471
0d99eb77
DE
11472 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
11473 load_partial_comp_unit (per_cu);
ae038cb0 11474
0d99eb77
DE
11475 per_cu->cu->last_used = 0;
11476 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11477 }
5afb4e99 11478
dee91e82
DE
11479 /* If we didn't find it, and not all dies have been loaded,
11480 load them all and try again. */
11481
5afb4e99
DJ
11482 if (pd == NULL && per_cu->load_all_dies == 0)
11483 {
5afb4e99 11484 per_cu->load_all_dies = 1;
fd820528
DE
11485
11486 /* This is nasty. When we reread the DIEs, somewhere up the call chain
11487 THIS_CU->cu may already be in use. So we can't just free it and
11488 replace its DIEs with the ones we read in. Instead, we leave those
11489 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
11490 and clobber THIS_CU->cu->partial_dies with the hash table for the new
11491 set. */
dee91e82 11492 load_partial_comp_unit (per_cu);
5afb4e99
DJ
11493
11494 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
11495 }
11496
11497 if (pd == NULL)
11498 internal_error (__FILE__, __LINE__,
3e43a32a
MS
11499 _("could not find partial DIE 0x%x "
11500 "in cache [from module %s]\n"),
b64f50a1 11501 offset.sect_off, bfd_get_filename (objfile->obfd));
5afb4e99 11502 return pd;
72bf9492
DJ
11503}
11504
abc72ce4
DE
11505/* See if we can figure out if the class lives in a namespace. We do
11506 this by looking for a member function; its demangled name will
11507 contain namespace info, if there is any. */
11508
11509static void
11510guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
11511 struct dwarf2_cu *cu)
11512{
11513 /* NOTE: carlton/2003-10-07: Getting the info this way changes
11514 what template types look like, because the demangler
11515 frequently doesn't give the same name as the debug info. We
11516 could fix this by only using the demangled name to get the
11517 prefix (but see comment in read_structure_type). */
11518
11519 struct partial_die_info *real_pdi;
11520 struct partial_die_info *child_pdi;
11521
11522 /* If this DIE (this DIE's specification, if any) has a parent, then
11523 we should not do this. We'll prepend the parent's fully qualified
11524 name when we create the partial symbol. */
11525
11526 real_pdi = struct_pdi;
11527 while (real_pdi->has_specification)
11528 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
11529
11530 if (real_pdi->die_parent != NULL)
11531 return;
11532
11533 for (child_pdi = struct_pdi->die_child;
11534 child_pdi != NULL;
11535 child_pdi = child_pdi->die_sibling)
11536 {
11537 if (child_pdi->tag == DW_TAG_subprogram
11538 && child_pdi->linkage_name != NULL)
11539 {
11540 char *actual_class_name
11541 = language_class_name_from_physname (cu->language_defn,
11542 child_pdi->linkage_name);
11543 if (actual_class_name != NULL)
11544 {
11545 struct_pdi->name
11546 = obsavestring (actual_class_name,
11547 strlen (actual_class_name),
11548 &cu->objfile->objfile_obstack);
11549 xfree (actual_class_name);
11550 }
11551 break;
11552 }
11553 }
11554}
11555
72bf9492
DJ
11556/* Adjust PART_DIE before generating a symbol for it. This function
11557 may set the is_external flag or change the DIE's name. */
11558
11559static void
11560fixup_partial_die (struct partial_die_info *part_die,
11561 struct dwarf2_cu *cu)
11562{
abc72ce4
DE
11563 /* Once we've fixed up a die, there's no point in doing so again.
11564 This also avoids a memory leak if we were to call
11565 guess_partial_die_structure_name multiple times. */
11566 if (part_die->fixup_called)
11567 return;
11568
72bf9492
DJ
11569 /* If we found a reference attribute and the DIE has no name, try
11570 to find a name in the referred to DIE. */
11571
11572 if (part_die->name == NULL && part_die->has_specification)
11573 {
11574 struct partial_die_info *spec_die;
72bf9492 11575
10b3939b 11576 spec_die = find_partial_die (part_die->spec_offset, cu);
72bf9492 11577
10b3939b 11578 fixup_partial_die (spec_die, cu);
72bf9492
DJ
11579
11580 if (spec_die->name)
11581 {
11582 part_die->name = spec_die->name;
11583
11584 /* Copy DW_AT_external attribute if it is set. */
11585 if (spec_die->is_external)
11586 part_die->is_external = spec_die->is_external;
11587 }
11588 }
11589
11590 /* Set default names for some unnamed DIEs. */
72bf9492
DJ
11591
11592 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
2b1dbab0 11593 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 11594
abc72ce4
DE
11595 /* If there is no parent die to provide a namespace, and there are
11596 children, see if we can determine the namespace from their linkage
122d1940 11597 name. */
abc72ce4 11598 if (cu->language == language_cplus
8b70b953 11599 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
11600 && part_die->die_parent == NULL
11601 && part_die->has_children
11602 && (part_die->tag == DW_TAG_class_type
11603 || part_die->tag == DW_TAG_structure_type
11604 || part_die->tag == DW_TAG_union_type))
11605 guess_partial_die_structure_name (part_die, cu);
11606
53832f31
TT
11607 /* GCC might emit a nameless struct or union that has a linkage
11608 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
11609 if (part_die->name == NULL
96408a79
SA
11610 && (part_die->tag == DW_TAG_class_type
11611 || part_die->tag == DW_TAG_interface_type
11612 || part_die->tag == DW_TAG_structure_type
11613 || part_die->tag == DW_TAG_union_type)
53832f31
TT
11614 && part_die->linkage_name != NULL)
11615 {
11616 char *demangled;
11617
11618 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
11619 if (demangled)
11620 {
96408a79
SA
11621 const char *base;
11622
11623 /* Strip any leading namespaces/classes, keep only the base name.
11624 DW_AT_name for named DIEs does not contain the prefixes. */
11625 base = strrchr (demangled, ':');
11626 if (base && base > demangled && base[-1] == ':')
11627 base++;
11628 else
11629 base = demangled;
11630
11631 part_die->name = obsavestring (base, strlen (base),
53832f31
TT
11632 &cu->objfile->objfile_obstack);
11633 xfree (demangled);
11634 }
11635 }
11636
abc72ce4 11637 part_die->fixup_called = 1;
72bf9492
DJ
11638}
11639
a8329558 11640/* Read an attribute value described by an attribute form. */
c906108c 11641
fe1b8b76 11642static gdb_byte *
dee91e82
DE
11643read_attribute_value (const struct die_reader_specs *reader,
11644 struct attribute *attr, unsigned form,
11645 gdb_byte *info_ptr)
c906108c 11646{
dee91e82
DE
11647 struct dwarf2_cu *cu = reader->cu;
11648 bfd *abfd = reader->abfd;
e7c27a73 11649 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
11650 unsigned int bytes_read;
11651 struct dwarf_block *blk;
11652
a8329558
KW
11653 attr->form = form;
11654 switch (form)
c906108c 11655 {
c906108c 11656 case DW_FORM_ref_addr:
ae411497 11657 if (cu->header.version == 2)
4568ecf9 11658 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 11659 else
4568ecf9
DE
11660 DW_UNSND (attr) = read_offset (abfd, info_ptr,
11661 &cu->header, &bytes_read);
ae411497
TT
11662 info_ptr += bytes_read;
11663 break;
11664 case DW_FORM_addr:
e7c27a73 11665 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
107d2387 11666 info_ptr += bytes_read;
c906108c
SS
11667 break;
11668 case DW_FORM_block2:
7b5a2f43 11669 blk = dwarf_alloc_block (cu);
c906108c
SS
11670 blk->size = read_2_bytes (abfd, info_ptr);
11671 info_ptr += 2;
11672 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11673 info_ptr += blk->size;
11674 DW_BLOCK (attr) = blk;
11675 break;
11676 case DW_FORM_block4:
7b5a2f43 11677 blk = dwarf_alloc_block (cu);
c906108c
SS
11678 blk->size = read_4_bytes (abfd, info_ptr);
11679 info_ptr += 4;
11680 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11681 info_ptr += blk->size;
11682 DW_BLOCK (attr) = blk;
11683 break;
11684 case DW_FORM_data2:
11685 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
11686 info_ptr += 2;
11687 break;
11688 case DW_FORM_data4:
11689 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
11690 info_ptr += 4;
11691 break;
11692 case DW_FORM_data8:
11693 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
11694 info_ptr += 8;
11695 break;
2dc7f7b3
TT
11696 case DW_FORM_sec_offset:
11697 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
11698 info_ptr += bytes_read;
11699 break;
c906108c 11700 case DW_FORM_string:
9b1c24c8 11701 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 11702 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
11703 info_ptr += bytes_read;
11704 break;
4bdf3d34
JJ
11705 case DW_FORM_strp:
11706 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
11707 &bytes_read);
8285870a 11708 DW_STRING_IS_CANONICAL (attr) = 0;
4bdf3d34
JJ
11709 info_ptr += bytes_read;
11710 break;
2dc7f7b3 11711 case DW_FORM_exprloc:
c906108c 11712 case DW_FORM_block:
7b5a2f43 11713 blk = dwarf_alloc_block (cu);
c906108c
SS
11714 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11715 info_ptr += bytes_read;
11716 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11717 info_ptr += blk->size;
11718 DW_BLOCK (attr) = blk;
11719 break;
11720 case DW_FORM_block1:
7b5a2f43 11721 blk = dwarf_alloc_block (cu);
c906108c
SS
11722 blk->size = read_1_byte (abfd, info_ptr);
11723 info_ptr += 1;
11724 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
11725 info_ptr += blk->size;
11726 DW_BLOCK (attr) = blk;
11727 break;
11728 case DW_FORM_data1:
11729 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11730 info_ptr += 1;
11731 break;
11732 case DW_FORM_flag:
11733 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
11734 info_ptr += 1;
11735 break;
2dc7f7b3
TT
11736 case DW_FORM_flag_present:
11737 DW_UNSND (attr) = 1;
11738 break;
c906108c
SS
11739 case DW_FORM_sdata:
11740 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
11741 info_ptr += bytes_read;
11742 break;
11743 case DW_FORM_udata:
11744 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11745 info_ptr += bytes_read;
11746 break;
11747 case DW_FORM_ref1:
4568ecf9
DE
11748 DW_UNSND (attr) = (cu->header.offset.sect_off
11749 + read_1_byte (abfd, info_ptr));
c906108c
SS
11750 info_ptr += 1;
11751 break;
11752 case DW_FORM_ref2:
4568ecf9
DE
11753 DW_UNSND (attr) = (cu->header.offset.sect_off
11754 + read_2_bytes (abfd, info_ptr));
c906108c
SS
11755 info_ptr += 2;
11756 break;
11757 case DW_FORM_ref4:
4568ecf9
DE
11758 DW_UNSND (attr) = (cu->header.offset.sect_off
11759 + read_4_bytes (abfd, info_ptr));
c906108c
SS
11760 info_ptr += 4;
11761 break;
613e1657 11762 case DW_FORM_ref8:
4568ecf9
DE
11763 DW_UNSND (attr) = (cu->header.offset.sect_off
11764 + read_8_bytes (abfd, info_ptr));
613e1657
KB
11765 info_ptr += 8;
11766 break;
55f1336d 11767 case DW_FORM_ref_sig8:
348e048f
DE
11768 /* Convert the signature to something we can record in DW_UNSND
11769 for later lookup.
11770 NOTE: This is NULL if the type wasn't found. */
11771 DW_SIGNATURED_TYPE (attr) =
e319fa28 11772 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
348e048f
DE
11773 info_ptr += 8;
11774 break;
c906108c 11775 case DW_FORM_ref_udata:
4568ecf9
DE
11776 DW_UNSND (attr) = (cu->header.offset.sect_off
11777 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
11778 info_ptr += bytes_read;
11779 break;
c906108c 11780 case DW_FORM_indirect:
a8329558
KW
11781 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11782 info_ptr += bytes_read;
dee91e82 11783 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
a8329558 11784 break;
3019eac3
DE
11785 case DW_FORM_GNU_addr_index:
11786 if (reader->dwo_file == NULL)
11787 {
11788 /* For now flag a hard error.
11789 Later we can turn this into a complaint. */
11790 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11791 dwarf_form_name (form),
11792 bfd_get_filename (abfd));
11793 }
11794 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
11795 info_ptr += bytes_read;
11796 break;
11797 case DW_FORM_GNU_str_index:
11798 if (reader->dwo_file == NULL)
11799 {
11800 /* For now flag a hard error.
11801 Later we can turn this into a complaint if warranted. */
11802 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
11803 dwarf_form_name (form),
11804 bfd_get_filename (abfd));
11805 }
11806 {
11807 ULONGEST str_index =
11808 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
11809
11810 DW_STRING (attr) = read_str_index (reader, cu, str_index);
11811 DW_STRING_IS_CANONICAL (attr) = 0;
11812 info_ptr += bytes_read;
11813 }
11814 break;
c906108c 11815 default:
8a3fe4f8 11816 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
11817 dwarf_form_name (form),
11818 bfd_get_filename (abfd));
c906108c 11819 }
28e94949
JB
11820
11821 /* We have seen instances where the compiler tried to emit a byte
11822 size attribute of -1 which ended up being encoded as an unsigned
11823 0xffffffff. Although 0xffffffff is technically a valid size value,
11824 an object of this size seems pretty unlikely so we can relatively
11825 safely treat these cases as if the size attribute was invalid and
11826 treat them as zero by default. */
11827 if (attr->name == DW_AT_byte_size
11828 && form == DW_FORM_data4
11829 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
11830 {
11831 complaint
11832 (&symfile_complaints,
43bbcdc2
PH
11833 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
11834 hex_string (DW_UNSND (attr)));
01c66ae6
JB
11835 DW_UNSND (attr) = 0;
11836 }
28e94949 11837
c906108c
SS
11838 return info_ptr;
11839}
11840
a8329558
KW
11841/* Read an attribute described by an abbreviated attribute. */
11842
fe1b8b76 11843static gdb_byte *
dee91e82
DE
11844read_attribute (const struct die_reader_specs *reader,
11845 struct attribute *attr, struct attr_abbrev *abbrev,
11846 gdb_byte *info_ptr)
a8329558
KW
11847{
11848 attr->name = abbrev->name;
dee91e82 11849 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
a8329558
KW
11850}
11851
0963b4bd 11852/* Read dwarf information from a buffer. */
c906108c
SS
11853
11854static unsigned int
fe1b8b76 11855read_1_byte (bfd *abfd, gdb_byte *buf)
c906108c 11856{
fe1b8b76 11857 return bfd_get_8 (abfd, buf);
c906108c
SS
11858}
11859
11860static int
fe1b8b76 11861read_1_signed_byte (bfd *abfd, gdb_byte *buf)
c906108c 11862{
fe1b8b76 11863 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
11864}
11865
11866static unsigned int
fe1b8b76 11867read_2_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11868{
fe1b8b76 11869 return bfd_get_16 (abfd, buf);
c906108c
SS
11870}
11871
21ae7a4d
JK
11872static int
11873read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
11874{
11875 return bfd_get_signed_16 (abfd, buf);
11876}
11877
c906108c 11878static unsigned int
fe1b8b76 11879read_4_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11880{
fe1b8b76 11881 return bfd_get_32 (abfd, buf);
c906108c
SS
11882}
11883
21ae7a4d
JK
11884static int
11885read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
11886{
11887 return bfd_get_signed_32 (abfd, buf);
11888}
11889
93311388 11890static ULONGEST
fe1b8b76 11891read_8_bytes (bfd *abfd, gdb_byte *buf)
c906108c 11892{
fe1b8b76 11893 return bfd_get_64 (abfd, buf);
c906108c
SS
11894}
11895
11896static CORE_ADDR
fe1b8b76 11897read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 11898 unsigned int *bytes_read)
c906108c 11899{
e7c27a73 11900 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
11901 CORE_ADDR retval = 0;
11902
107d2387 11903 if (cu_header->signed_addr_p)
c906108c 11904 {
107d2387
AC
11905 switch (cu_header->addr_size)
11906 {
11907 case 2:
fe1b8b76 11908 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
11909 break;
11910 case 4:
fe1b8b76 11911 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
11912 break;
11913 case 8:
fe1b8b76 11914 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
11915 break;
11916 default:
8e65ff28 11917 internal_error (__FILE__, __LINE__,
e2e0b3e5 11918 _("read_address: bad switch, signed [in module %s]"),
659b0389 11919 bfd_get_filename (abfd));
107d2387
AC
11920 }
11921 }
11922 else
11923 {
11924 switch (cu_header->addr_size)
11925 {
11926 case 2:
fe1b8b76 11927 retval = bfd_get_16 (abfd, buf);
107d2387
AC
11928 break;
11929 case 4:
fe1b8b76 11930 retval = bfd_get_32 (abfd, buf);
107d2387
AC
11931 break;
11932 case 8:
fe1b8b76 11933 retval = bfd_get_64 (abfd, buf);
107d2387
AC
11934 break;
11935 default:
8e65ff28 11936 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
11937 _("read_address: bad switch, "
11938 "unsigned [in module %s]"),
659b0389 11939 bfd_get_filename (abfd));
107d2387 11940 }
c906108c 11941 }
64367e0a 11942
107d2387
AC
11943 *bytes_read = cu_header->addr_size;
11944 return retval;
c906108c
SS
11945}
11946
f7ef9339 11947/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
11948 specification allows the initial length to take up either 4 bytes
11949 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
11950 bytes describe the length and all offsets will be 8 bytes in length
11951 instead of 4.
11952
f7ef9339
KB
11953 An older, non-standard 64-bit format is also handled by this
11954 function. The older format in question stores the initial length
11955 as an 8-byte quantity without an escape value. Lengths greater
11956 than 2^32 aren't very common which means that the initial 4 bytes
11957 is almost always zero. Since a length value of zero doesn't make
11958 sense for the 32-bit format, this initial zero can be considered to
11959 be an escape value which indicates the presence of the older 64-bit
11960 format. As written, the code can't detect (old format) lengths
917c78fc
MK
11961 greater than 4GB. If it becomes necessary to handle lengths
11962 somewhat larger than 4GB, we could allow other small values (such
11963 as the non-sensical values of 1, 2, and 3) to also be used as
11964 escape values indicating the presence of the old format.
f7ef9339 11965
917c78fc
MK
11966 The value returned via bytes_read should be used to increment the
11967 relevant pointer after calling read_initial_length().
c764a876 11968
613e1657
KB
11969 [ Note: read_initial_length() and read_offset() are based on the
11970 document entitled "DWARF Debugging Information Format", revision
f7ef9339 11971 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
11972 from:
11973
f7ef9339 11974 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 11975
613e1657
KB
11976 This document is only a draft and is subject to change. (So beware.)
11977
f7ef9339 11978 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
11979 determined empirically by examining 64-bit ELF files produced by
11980 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
11981
11982 - Kevin, July 16, 2002
613e1657
KB
11983 ] */
11984
11985static LONGEST
c764a876 11986read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
613e1657 11987{
fe1b8b76 11988 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 11989
dd373385 11990 if (length == 0xffffffff)
613e1657 11991 {
fe1b8b76 11992 length = bfd_get_64 (abfd, buf + 4);
613e1657 11993 *bytes_read = 12;
613e1657 11994 }
dd373385 11995 else if (length == 0)
f7ef9339 11996 {
dd373385 11997 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 11998 length = bfd_get_64 (abfd, buf);
f7ef9339 11999 *bytes_read = 8;
f7ef9339 12000 }
613e1657
KB
12001 else
12002 {
12003 *bytes_read = 4;
613e1657
KB
12004 }
12005
c764a876
DE
12006 return length;
12007}
dd373385 12008
c764a876
DE
12009/* Cover function for read_initial_length.
12010 Returns the length of the object at BUF, and stores the size of the
12011 initial length in *BYTES_READ and stores the size that offsets will be in
12012 *OFFSET_SIZE.
12013 If the initial length size is not equivalent to that specified in
12014 CU_HEADER then issue a complaint.
12015 This is useful when reading non-comp-unit headers. */
dd373385 12016
c764a876
DE
12017static LONGEST
12018read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
12019 const struct comp_unit_head *cu_header,
12020 unsigned int *bytes_read,
12021 unsigned int *offset_size)
12022{
12023 LONGEST length = read_initial_length (abfd, buf, bytes_read);
12024
12025 gdb_assert (cu_header->initial_length_size == 4
12026 || cu_header->initial_length_size == 8
12027 || cu_header->initial_length_size == 12);
12028
12029 if (cu_header->initial_length_size != *bytes_read)
12030 complaint (&symfile_complaints,
12031 _("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 12032
c764a876 12033 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 12034 return length;
613e1657
KB
12035}
12036
12037/* Read an offset from the data stream. The size of the offset is
917c78fc 12038 given by cu_header->offset_size. */
613e1657
KB
12039
12040static LONGEST
fe1b8b76 12041read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
891d2f0b 12042 unsigned int *bytes_read)
c764a876
DE
12043{
12044 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 12045
c764a876
DE
12046 *bytes_read = cu_header->offset_size;
12047 return offset;
12048}
12049
12050/* Read an offset from the data stream. */
12051
12052static LONGEST
12053read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
613e1657
KB
12054{
12055 LONGEST retval = 0;
12056
c764a876 12057 switch (offset_size)
613e1657
KB
12058 {
12059 case 4:
fe1b8b76 12060 retval = bfd_get_32 (abfd, buf);
613e1657
KB
12061 break;
12062 case 8:
fe1b8b76 12063 retval = bfd_get_64 (abfd, buf);
613e1657
KB
12064 break;
12065 default:
8e65ff28 12066 internal_error (__FILE__, __LINE__,
c764a876 12067 _("read_offset_1: bad switch [in module %s]"),
659b0389 12068 bfd_get_filename (abfd));
613e1657
KB
12069 }
12070
917c78fc 12071 return retval;
613e1657
KB
12072}
12073
fe1b8b76
JB
12074static gdb_byte *
12075read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
c906108c
SS
12076{
12077 /* If the size of a host char is 8 bits, we can return a pointer
12078 to the buffer, otherwise we have to copy the data to a buffer
12079 allocated on the temporary obstack. */
4bdf3d34 12080 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 12081 return buf;
c906108c
SS
12082}
12083
12084static char *
9b1c24c8 12085read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c
SS
12086{
12087 /* If the size of a host char is 8 bits, we can return a pointer
12088 to the string, otherwise we have to copy the string to a buffer
12089 allocated on the temporary obstack. */
4bdf3d34 12090 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
12091 if (*buf == '\0')
12092 {
12093 *bytes_read_ptr = 1;
12094 return NULL;
12095 }
fe1b8b76
JB
12096 *bytes_read_ptr = strlen ((char *) buf) + 1;
12097 return (char *) buf;
4bdf3d34
JJ
12098}
12099
12100static char *
cf2c3c16 12101read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
4bdf3d34 12102{
be391dca 12103 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
dce234bc 12104 if (dwarf2_per_objfile->str.buffer == NULL)
cf2c3c16
TT
12105 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
12106 bfd_get_filename (abfd));
dce234bc 12107 if (str_offset >= dwarf2_per_objfile->str.size)
cf2c3c16
TT
12108 error (_("DW_FORM_strp pointing outside of "
12109 ".debug_str section [in module %s]"),
12110 bfd_get_filename (abfd));
4bdf3d34 12111 gdb_assert (HOST_CHAR_BIT == 8);
dce234bc 12112 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
4bdf3d34 12113 return NULL;
dce234bc 12114 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
c906108c
SS
12115}
12116
cf2c3c16
TT
12117static char *
12118read_indirect_string (bfd *abfd, gdb_byte *buf,
12119 const struct comp_unit_head *cu_header,
12120 unsigned int *bytes_read_ptr)
12121{
12122 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
12123
12124 return read_indirect_string_at_offset (abfd, str_offset);
12125}
12126
12df843f 12127static ULONGEST
fe1b8b76 12128read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12129{
12df843f 12130 ULONGEST result;
ce5d95e1 12131 unsigned int num_read;
c906108c
SS
12132 int i, shift;
12133 unsigned char byte;
12134
12135 result = 0;
12136 shift = 0;
12137 num_read = 0;
12138 i = 0;
12139 while (1)
12140 {
fe1b8b76 12141 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12142 buf++;
12143 num_read++;
12df843f 12144 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
12145 if ((byte & 128) == 0)
12146 {
12147 break;
12148 }
12149 shift += 7;
12150 }
12151 *bytes_read_ptr = num_read;
12152 return result;
12153}
12154
12df843f 12155static LONGEST
fe1b8b76 12156read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
c906108c 12157{
12df843f 12158 LONGEST result;
77e0b926 12159 int i, shift, num_read;
c906108c
SS
12160 unsigned char byte;
12161
12162 result = 0;
12163 shift = 0;
c906108c
SS
12164 num_read = 0;
12165 i = 0;
12166 while (1)
12167 {
fe1b8b76 12168 byte = bfd_get_8 (abfd, buf);
c906108c
SS
12169 buf++;
12170 num_read++;
12df843f 12171 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
12172 shift += 7;
12173 if ((byte & 128) == 0)
12174 {
12175 break;
12176 }
12177 }
77e0b926 12178 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 12179 result |= -(((LONGEST) 1) << shift);
c906108c
SS
12180 *bytes_read_ptr = num_read;
12181 return result;
12182}
12183
3019eac3
DE
12184/* Given index ADDR_INDEX in .debug_addr, fetch the value.
12185 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
12186 ADDR_SIZE is the size of addresses from the CU header. */
12187
12188static CORE_ADDR
12189read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
12190{
12191 struct objfile *objfile = dwarf2_per_objfile->objfile;
12192 bfd *abfd = objfile->obfd;
12193 const gdb_byte *info_ptr;
12194
12195 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
12196 if (dwarf2_per_objfile->addr.buffer == NULL)
12197 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
12198 objfile->name);
12199 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
12200 error (_("DW_FORM_addr_index pointing outside of "
12201 ".debug_addr section [in module %s]"),
12202 objfile->name);
12203 info_ptr = (dwarf2_per_objfile->addr.buffer
12204 + addr_base + addr_index * addr_size);
12205 if (addr_size == 4)
12206 return bfd_get_32 (abfd, info_ptr);
12207 else
12208 return bfd_get_64 (abfd, info_ptr);
12209}
12210
12211/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
12212
12213static CORE_ADDR
12214read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
12215{
12216 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
12217}
12218
12219/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
12220
12221static CORE_ADDR
12222read_addr_index_from_leb128 (struct dwarf2_cu *cu, gdb_byte *info_ptr,
12223 unsigned int *bytes_read)
12224{
12225 bfd *abfd = cu->objfile->obfd;
12226 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
12227
12228 return read_addr_index (cu, addr_index);
12229}
12230
12231/* Data structure to pass results from dwarf2_read_addr_index_reader
12232 back to dwarf2_read_addr_index. */
12233
12234struct dwarf2_read_addr_index_data
12235{
12236 ULONGEST addr_base;
12237 int addr_size;
12238};
12239
12240/* die_reader_func for dwarf2_read_addr_index. */
12241
12242static void
12243dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
12244 gdb_byte *info_ptr,
12245 struct die_info *comp_unit_die,
12246 int has_children,
12247 void *data)
12248{
12249 struct dwarf2_cu *cu = reader->cu;
12250 struct dwarf2_read_addr_index_data *aidata =
12251 (struct dwarf2_read_addr_index_data *) data;
12252
12253 aidata->addr_base = cu->addr_base;
12254 aidata->addr_size = cu->header.addr_size;
12255}
12256
12257/* Given an index in .debug_addr, fetch the value.
12258 NOTE: This can be called during dwarf expression evaluation,
12259 long after the debug information has been read, and thus per_cu->cu
12260 may no longer exist. */
12261
12262CORE_ADDR
12263dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
12264 unsigned int addr_index)
12265{
12266 struct objfile *objfile = per_cu->objfile;
12267 struct dwarf2_cu *cu = per_cu->cu;
12268 ULONGEST addr_base;
12269 int addr_size;
12270
12271 /* This is intended to be called from outside this file. */
12272 dw2_setup (objfile);
12273
12274 /* We need addr_base and addr_size.
12275 If we don't have PER_CU->cu, we have to get it.
12276 Nasty, but the alternative is storing the needed info in PER_CU,
12277 which at this point doesn't seem justified: it's not clear how frequently
12278 it would get used and it would increase the size of every PER_CU.
12279 Entry points like dwarf2_per_cu_addr_size do a similar thing
12280 so we're not in uncharted territory here.
12281 Alas we need to be a bit more complicated as addr_base is contained
12282 in the DIE.
12283
12284 We don't need to read the entire CU(/TU).
12285 We just need the header and top level die.
12286 IWBN to use the aging mechanism to let us lazily later discard the CU.
12287 See however init_cutu_and_read_dies_simple. */
12288
12289 if (cu != NULL)
12290 {
12291 addr_base = cu->addr_base;
12292 addr_size = cu->header.addr_size;
12293 }
12294 else
12295 {
12296 struct dwarf2_read_addr_index_data aidata;
12297
12298 init_cutu_and_read_dies_simple (per_cu, dwarf2_read_addr_index_reader,
12299 &aidata);
12300 addr_base = aidata.addr_base;
12301 addr_size = aidata.addr_size;
12302 }
12303
12304 return read_addr_index_1 (addr_index, addr_base, addr_size);
12305}
12306
12307/* Given a DW_AT_str_index, fetch the string. */
12308
12309static char *
12310read_str_index (const struct die_reader_specs *reader,
12311 struct dwarf2_cu *cu, ULONGEST str_index)
12312{
12313 struct objfile *objfile = dwarf2_per_objfile->objfile;
12314 const char *dwo_name = objfile->name;
12315 bfd *abfd = objfile->obfd;
12316 struct dwo_sections *sections = &reader->dwo_file->sections;
12317 gdb_byte *info_ptr;
12318 ULONGEST str_offset;
12319
12320 dwarf2_read_section (objfile, &sections->str);
12321 dwarf2_read_section (objfile, &sections->str_offsets);
12322 if (sections->str.buffer == NULL)
12323 error (_("DW_FORM_str_index used without .debug_str.dwo section"
12324 " in CU at offset 0x%lx [in module %s]"),
12325 (long) cu->header.offset.sect_off, dwo_name);
12326 if (sections->str_offsets.buffer == NULL)
12327 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
12328 " in CU at offset 0x%lx [in module %s]"),
12329 (long) cu->header.offset.sect_off, dwo_name);
12330 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
12331 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
12332 " section in CU at offset 0x%lx [in module %s]"),
12333 (long) cu->header.offset.sect_off, dwo_name);
12334 info_ptr = (sections->str_offsets.buffer
12335 + str_index * cu->header.offset_size);
12336 if (cu->header.offset_size == 4)
12337 str_offset = bfd_get_32 (abfd, info_ptr);
12338 else
12339 str_offset = bfd_get_64 (abfd, info_ptr);
12340 if (str_offset >= sections->str.size)
12341 error (_("Offset from DW_FORM_str_index pointing outside of"
12342 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
12343 (long) cu->header.offset.sect_off, dwo_name);
12344 return (char *) (sections->str.buffer + str_offset);
12345}
12346
3019eac3
DE
12347/* Return the length of an LEB128 number in BUF. */
12348
12349static int
12350leb128_size (const gdb_byte *buf)
12351{
12352 const gdb_byte *begin = buf;
12353 gdb_byte byte;
12354
12355 while (1)
12356 {
12357 byte = *buf++;
12358 if ((byte & 128) == 0)
12359 return buf - begin;
12360 }
12361}
12362
c906108c 12363static void
e142c38c 12364set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
12365{
12366 switch (lang)
12367 {
12368 case DW_LANG_C89:
76bee0cc 12369 case DW_LANG_C99:
c906108c 12370 case DW_LANG_C:
e142c38c 12371 cu->language = language_c;
c906108c
SS
12372 break;
12373 case DW_LANG_C_plus_plus:
e142c38c 12374 cu->language = language_cplus;
c906108c 12375 break;
6aecb9c2
JB
12376 case DW_LANG_D:
12377 cu->language = language_d;
12378 break;
c906108c
SS
12379 case DW_LANG_Fortran77:
12380 case DW_LANG_Fortran90:
b21b22e0 12381 case DW_LANG_Fortran95:
e142c38c 12382 cu->language = language_fortran;
c906108c 12383 break;
a766d390
DE
12384 case DW_LANG_Go:
12385 cu->language = language_go;
12386 break;
c906108c 12387 case DW_LANG_Mips_Assembler:
e142c38c 12388 cu->language = language_asm;
c906108c 12389 break;
bebd888e 12390 case DW_LANG_Java:
e142c38c 12391 cu->language = language_java;
bebd888e 12392 break;
c906108c 12393 case DW_LANG_Ada83:
8aaf0b47 12394 case DW_LANG_Ada95:
bc5f45f8
JB
12395 cu->language = language_ada;
12396 break;
72019c9c
GM
12397 case DW_LANG_Modula2:
12398 cu->language = language_m2;
12399 break;
fe8e67fd
PM
12400 case DW_LANG_Pascal83:
12401 cu->language = language_pascal;
12402 break;
22566fbd
DJ
12403 case DW_LANG_ObjC:
12404 cu->language = language_objc;
12405 break;
c906108c
SS
12406 case DW_LANG_Cobol74:
12407 case DW_LANG_Cobol85:
c906108c 12408 default:
e142c38c 12409 cu->language = language_minimal;
c906108c
SS
12410 break;
12411 }
e142c38c 12412 cu->language_defn = language_def (cu->language);
c906108c
SS
12413}
12414
12415/* Return the named attribute or NULL if not there. */
12416
12417static struct attribute *
e142c38c 12418dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 12419{
a48e046c 12420 for (;;)
c906108c 12421 {
a48e046c
TT
12422 unsigned int i;
12423 struct attribute *spec = NULL;
12424
12425 for (i = 0; i < die->num_attrs; ++i)
12426 {
12427 if (die->attrs[i].name == name)
12428 return &die->attrs[i];
12429 if (die->attrs[i].name == DW_AT_specification
12430 || die->attrs[i].name == DW_AT_abstract_origin)
12431 spec = &die->attrs[i];
12432 }
12433
12434 if (!spec)
12435 break;
c906108c 12436
f2f0e013 12437 die = follow_die_ref (die, spec, &cu);
f2f0e013 12438 }
c5aa993b 12439
c906108c
SS
12440 return NULL;
12441}
12442
348e048f
DE
12443/* Return the named attribute or NULL if not there,
12444 but do not follow DW_AT_specification, etc.
12445 This is for use in contexts where we're reading .debug_types dies.
12446 Following DW_AT_specification, DW_AT_abstract_origin will take us
12447 back up the chain, and we want to go down. */
12448
12449static struct attribute *
12450dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
12451 struct dwarf2_cu *cu)
12452{
12453 unsigned int i;
12454
12455 for (i = 0; i < die->num_attrs; ++i)
12456 if (die->attrs[i].name == name)
12457 return &die->attrs[i];
12458
12459 return NULL;
12460}
12461
05cf31d1
JB
12462/* Return non-zero iff the attribute NAME is defined for the given DIE,
12463 and holds a non-zero value. This function should only be used for
2dc7f7b3 12464 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
12465
12466static int
12467dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
12468{
12469 struct attribute *attr = dwarf2_attr (die, name, cu);
12470
12471 return (attr && DW_UNSND (attr));
12472}
12473
3ca72b44 12474static int
e142c38c 12475die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 12476{
05cf31d1
JB
12477 /* A DIE is a declaration if it has a DW_AT_declaration attribute
12478 which value is non-zero. However, we have to be careful with
12479 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
12480 (via dwarf2_flag_true_p) follows this attribute. So we may
12481 end up accidently finding a declaration attribute that belongs
12482 to a different DIE referenced by the specification attribute,
12483 even though the given DIE does not have a declaration attribute. */
12484 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
12485 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
12486}
12487
63d06c5c 12488/* Return the die giving the specification for DIE, if there is
f2f0e013 12489 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
12490 containing the return value on output. If there is no
12491 specification, but there is an abstract origin, that is
12492 returned. */
63d06c5c
DC
12493
12494static struct die_info *
f2f0e013 12495die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 12496{
f2f0e013
DJ
12497 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
12498 *spec_cu);
63d06c5c 12499
edb3359d
DJ
12500 if (spec_attr == NULL)
12501 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
12502
63d06c5c
DC
12503 if (spec_attr == NULL)
12504 return NULL;
12505 else
f2f0e013 12506 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 12507}
c906108c 12508
debd256d 12509/* Free the line_header structure *LH, and any arrays and strings it
ae2de4f8
DE
12510 refers to.
12511 NOTE: This is also used as a "cleanup" function. */
12512
debd256d
JB
12513static void
12514free_line_header (struct line_header *lh)
12515{
12516 if (lh->standard_opcode_lengths)
a8bc7b56 12517 xfree (lh->standard_opcode_lengths);
debd256d
JB
12518
12519 /* Remember that all the lh->file_names[i].name pointers are
12520 pointers into debug_line_buffer, and don't need to be freed. */
12521 if (lh->file_names)
a8bc7b56 12522 xfree (lh->file_names);
debd256d
JB
12523
12524 /* Similarly for the include directory names. */
12525 if (lh->include_dirs)
a8bc7b56 12526 xfree (lh->include_dirs);
debd256d 12527
a8bc7b56 12528 xfree (lh);
debd256d
JB
12529}
12530
debd256d 12531/* Add an entry to LH's include directory table. */
ae2de4f8 12532
debd256d
JB
12533static void
12534add_include_dir (struct line_header *lh, char *include_dir)
c906108c 12535{
debd256d
JB
12536 /* Grow the array if necessary. */
12537 if (lh->include_dirs_size == 0)
c5aa993b 12538 {
debd256d
JB
12539 lh->include_dirs_size = 1; /* for testing */
12540 lh->include_dirs = xmalloc (lh->include_dirs_size
12541 * sizeof (*lh->include_dirs));
12542 }
12543 else if (lh->num_include_dirs >= lh->include_dirs_size)
12544 {
12545 lh->include_dirs_size *= 2;
12546 lh->include_dirs = xrealloc (lh->include_dirs,
12547 (lh->include_dirs_size
12548 * sizeof (*lh->include_dirs)));
c5aa993b 12549 }
c906108c 12550
debd256d
JB
12551 lh->include_dirs[lh->num_include_dirs++] = include_dir;
12552}
6e70227d 12553
debd256d 12554/* Add an entry to LH's file name table. */
ae2de4f8 12555
debd256d
JB
12556static void
12557add_file_name (struct line_header *lh,
12558 char *name,
12559 unsigned int dir_index,
12560 unsigned int mod_time,
12561 unsigned int length)
12562{
12563 struct file_entry *fe;
12564
12565 /* Grow the array if necessary. */
12566 if (lh->file_names_size == 0)
12567 {
12568 lh->file_names_size = 1; /* for testing */
12569 lh->file_names = xmalloc (lh->file_names_size
12570 * sizeof (*lh->file_names));
12571 }
12572 else if (lh->num_file_names >= lh->file_names_size)
12573 {
12574 lh->file_names_size *= 2;
12575 lh->file_names = xrealloc (lh->file_names,
12576 (lh->file_names_size
12577 * sizeof (*lh->file_names)));
12578 }
12579
12580 fe = &lh->file_names[lh->num_file_names++];
12581 fe->name = name;
12582 fe->dir_index = dir_index;
12583 fe->mod_time = mod_time;
12584 fe->length = length;
aaa75496 12585 fe->included_p = 0;
cb1df416 12586 fe->symtab = NULL;
debd256d 12587}
6e70227d 12588
debd256d 12589/* Read the statement program header starting at OFFSET in
3019eac3 12590 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 12591 to a struct line_header, allocated using xmalloc.
debd256d
JB
12592
12593 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
12594 the returned object point into the dwarf line section buffer,
12595 and must not be freed. */
ae2de4f8 12596
debd256d 12597static struct line_header *
3019eac3 12598dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
debd256d
JB
12599{
12600 struct cleanup *back_to;
12601 struct line_header *lh;
fe1b8b76 12602 gdb_byte *line_ptr;
c764a876 12603 unsigned int bytes_read, offset_size;
debd256d
JB
12604 int i;
12605 char *cur_dir, *cur_file;
3019eac3
DE
12606 struct dwarf2_section_info *section;
12607 bfd *abfd;
12608
12609 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
12610 DWO file. */
12611 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12612 section = &cu->dwo_unit->dwo_file->sections.line;
12613 else
12614 section = &dwarf2_per_objfile->line;
debd256d 12615
3019eac3
DE
12616 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
12617 if (section->buffer == NULL)
debd256d 12618 {
3019eac3
DE
12619 if (cu->dwo_unit && cu->per_cu->is_debug_types)
12620 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
12621 else
12622 complaint (&symfile_complaints, _("missing .debug_line section"));
debd256d
JB
12623 return 0;
12624 }
12625
fceca515
DE
12626 /* We can't do this until we know the section is non-empty.
12627 Only then do we know we have such a section. */
12628 abfd = section->asection->owner;
12629
a738430d
MK
12630 /* Make sure that at least there's room for the total_length field.
12631 That could be 12 bytes long, but we're just going to fudge that. */
3019eac3 12632 if (offset + 4 >= section->size)
debd256d 12633 {
4d3c2250 12634 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12635 return 0;
12636 }
12637
12638 lh = xmalloc (sizeof (*lh));
12639 memset (lh, 0, sizeof (*lh));
12640 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
12641 (void *) lh);
12642
3019eac3 12643 line_ptr = section->buffer + offset;
debd256d 12644
a738430d 12645 /* Read in the header. */
6e70227d 12646 lh->total_length =
c764a876
DE
12647 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
12648 &bytes_read, &offset_size);
debd256d 12649 line_ptr += bytes_read;
3019eac3 12650 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 12651 {
4d3c2250 12652 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
12653 return 0;
12654 }
12655 lh->statement_program_end = line_ptr + lh->total_length;
12656 lh->version = read_2_bytes (abfd, line_ptr);
12657 line_ptr += 2;
c764a876
DE
12658 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
12659 line_ptr += offset_size;
debd256d
JB
12660 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
12661 line_ptr += 1;
2dc7f7b3
TT
12662 if (lh->version >= 4)
12663 {
12664 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
12665 line_ptr += 1;
12666 }
12667 else
12668 lh->maximum_ops_per_instruction = 1;
12669
12670 if (lh->maximum_ops_per_instruction == 0)
12671 {
12672 lh->maximum_ops_per_instruction = 1;
12673 complaint (&symfile_complaints,
3e43a32a
MS
12674 _("invalid maximum_ops_per_instruction "
12675 "in `.debug_line' section"));
2dc7f7b3
TT
12676 }
12677
debd256d
JB
12678 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
12679 line_ptr += 1;
12680 lh->line_base = read_1_signed_byte (abfd, line_ptr);
12681 line_ptr += 1;
12682 lh->line_range = read_1_byte (abfd, line_ptr);
12683 line_ptr += 1;
12684 lh->opcode_base = read_1_byte (abfd, line_ptr);
12685 line_ptr += 1;
12686 lh->standard_opcode_lengths
fe1b8b76 12687 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
debd256d
JB
12688
12689 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
12690 for (i = 1; i < lh->opcode_base; ++i)
12691 {
12692 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
12693 line_ptr += 1;
12694 }
12695
a738430d 12696 /* Read directory table. */
9b1c24c8 12697 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12698 {
12699 line_ptr += bytes_read;
12700 add_include_dir (lh, cur_dir);
12701 }
12702 line_ptr += bytes_read;
12703
a738430d 12704 /* Read file name table. */
9b1c24c8 12705 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
debd256d
JB
12706 {
12707 unsigned int dir_index, mod_time, length;
12708
12709 line_ptr += bytes_read;
12710 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12711 line_ptr += bytes_read;
12712 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12713 line_ptr += bytes_read;
12714 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12715 line_ptr += bytes_read;
12716
12717 add_file_name (lh, cur_file, dir_index, mod_time, length);
12718 }
12719 line_ptr += bytes_read;
6e70227d 12720 lh->statement_program_start = line_ptr;
debd256d 12721
3019eac3 12722 if (line_ptr > (section->buffer + section->size))
4d3c2250 12723 complaint (&symfile_complaints,
3e43a32a
MS
12724 _("line number info header doesn't "
12725 "fit in `.debug_line' section"));
debd256d
JB
12726
12727 discard_cleanups (back_to);
12728 return lh;
12729}
c906108c 12730
c6da4cef
DE
12731/* Subroutine of dwarf_decode_lines to simplify it.
12732 Return the file name of the psymtab for included file FILE_INDEX
12733 in line header LH of PST.
12734 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
12735 If space for the result is malloc'd, it will be freed by a cleanup.
12736 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
12737
12738static char *
12739psymtab_include_file_name (const struct line_header *lh, int file_index,
12740 const struct partial_symtab *pst,
12741 const char *comp_dir)
12742{
12743 const struct file_entry fe = lh->file_names [file_index];
12744 char *include_name = fe.name;
12745 char *include_name_to_compare = include_name;
12746 char *dir_name = NULL;
72b9f47f
TT
12747 const char *pst_filename;
12748 char *copied_name = NULL;
c6da4cef
DE
12749 int file_is_pst;
12750
12751 if (fe.dir_index)
12752 dir_name = lh->include_dirs[fe.dir_index - 1];
12753
12754 if (!IS_ABSOLUTE_PATH (include_name)
12755 && (dir_name != NULL || comp_dir != NULL))
12756 {
12757 /* Avoid creating a duplicate psymtab for PST.
12758 We do this by comparing INCLUDE_NAME and PST_FILENAME.
12759 Before we do the comparison, however, we need to account
12760 for DIR_NAME and COMP_DIR.
12761 First prepend dir_name (if non-NULL). If we still don't
12762 have an absolute path prepend comp_dir (if non-NULL).
12763 However, the directory we record in the include-file's
12764 psymtab does not contain COMP_DIR (to match the
12765 corresponding symtab(s)).
12766
12767 Example:
12768
12769 bash$ cd /tmp
12770 bash$ gcc -g ./hello.c
12771 include_name = "hello.c"
12772 dir_name = "."
12773 DW_AT_comp_dir = comp_dir = "/tmp"
12774 DW_AT_name = "./hello.c" */
12775
12776 if (dir_name != NULL)
12777 {
12778 include_name = concat (dir_name, SLASH_STRING,
12779 include_name, (char *)NULL);
12780 include_name_to_compare = include_name;
12781 make_cleanup (xfree, include_name);
12782 }
12783 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
12784 {
12785 include_name_to_compare = concat (comp_dir, SLASH_STRING,
12786 include_name, (char *)NULL);
12787 }
12788 }
12789
12790 pst_filename = pst->filename;
12791 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
12792 {
72b9f47f
TT
12793 copied_name = concat (pst->dirname, SLASH_STRING,
12794 pst_filename, (char *)NULL);
12795 pst_filename = copied_name;
c6da4cef
DE
12796 }
12797
1e3fad37 12798 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef
DE
12799
12800 if (include_name_to_compare != include_name)
12801 xfree (include_name_to_compare);
72b9f47f
TT
12802 if (copied_name != NULL)
12803 xfree (copied_name);
c6da4cef
DE
12804
12805 if (file_is_pst)
12806 return NULL;
12807 return include_name;
12808}
12809
c91513d8
PP
12810/* Ignore this record_line request. */
12811
12812static void
12813noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
12814{
12815 return;
12816}
12817
f3f5162e
DE
12818/* Subroutine of dwarf_decode_lines to simplify it.
12819 Process the line number information in LH. */
debd256d 12820
c906108c 12821static void
f3f5162e
DE
12822dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
12823 struct dwarf2_cu *cu, struct partial_symtab *pst)
c906108c 12824{
a8c50c1f 12825 gdb_byte *line_ptr, *extended_end;
fe1b8b76 12826 gdb_byte *line_end;
a8c50c1f 12827 unsigned int bytes_read, extended_len;
c906108c 12828 unsigned char op_code, extended_op, adj_opcode;
e142c38c
DJ
12829 CORE_ADDR baseaddr;
12830 struct objfile *objfile = cu->objfile;
f3f5162e 12831 bfd *abfd = objfile->obfd;
fbf65064 12832 struct gdbarch *gdbarch = get_objfile_arch (objfile);
aaa75496 12833 const int decode_for_pst_p = (pst != NULL);
f3f5162e 12834 struct subfile *last_subfile = NULL;
c91513d8
PP
12835 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
12836 = record_line;
e142c38c
DJ
12837
12838 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 12839
debd256d
JB
12840 line_ptr = lh->statement_program_start;
12841 line_end = lh->statement_program_end;
c906108c
SS
12842
12843 /* Read the statement sequences until there's nothing left. */
12844 while (line_ptr < line_end)
12845 {
12846 /* state machine registers */
12847 CORE_ADDR address = 0;
12848 unsigned int file = 1;
12849 unsigned int line = 1;
12850 unsigned int column = 0;
debd256d 12851 int is_stmt = lh->default_is_stmt;
c906108c
SS
12852 int basic_block = 0;
12853 int end_sequence = 0;
fbf65064 12854 CORE_ADDR addr;
2dc7f7b3 12855 unsigned char op_index = 0;
c906108c 12856
aaa75496 12857 if (!decode_for_pst_p && lh->num_file_names >= file)
c906108c 12858 {
aaa75496 12859 /* Start a subfile for the current file of the state machine. */
debd256d
JB
12860 /* lh->include_dirs and lh->file_names are 0-based, but the
12861 directory and file name numbers in the statement program
12862 are 1-based. */
12863 struct file_entry *fe = &lh->file_names[file - 1];
4f1520fb 12864 char *dir = NULL;
a738430d 12865
debd256d
JB
12866 if (fe->dir_index)
12867 dir = lh->include_dirs[fe->dir_index - 1];
4f1520fb
FR
12868
12869 dwarf2_start_subfile (fe->name, dir, comp_dir);
c906108c
SS
12870 }
12871
a738430d 12872 /* Decode the table. */
c5aa993b 12873 while (!end_sequence)
c906108c
SS
12874 {
12875 op_code = read_1_byte (abfd, line_ptr);
12876 line_ptr += 1;
59205f5a
JB
12877 if (line_ptr > line_end)
12878 {
12879 dwarf2_debug_line_missing_end_sequence_complaint ();
12880 break;
12881 }
9aa1fe7e 12882
debd256d 12883 if (op_code >= lh->opcode_base)
6e70227d 12884 {
a738430d 12885 /* Special operand. */
debd256d 12886 adj_opcode = op_code - lh->opcode_base;
2dc7f7b3
TT
12887 address += (((op_index + (adj_opcode / lh->line_range))
12888 / lh->maximum_ops_per_instruction)
12889 * lh->minimum_instruction_length);
12890 op_index = ((op_index + (adj_opcode / lh->line_range))
12891 % lh->maximum_ops_per_instruction);
debd256d 12892 line += lh->line_base + (adj_opcode % lh->line_range);
59205f5a 12893 if (lh->num_file_names < file || file == 0)
25e43795 12894 dwarf2_debug_line_missing_file_complaint ();
2dc7f7b3
TT
12895 /* For now we ignore lines not starting on an
12896 instruction boundary. */
12897 else if (op_index == 0)
25e43795
DJ
12898 {
12899 lh->file_names[file - 1].included_p = 1;
ca5f395d 12900 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
12901 {
12902 if (last_subfile != current_subfile)
12903 {
12904 addr = gdbarch_addr_bits_remove (gdbarch, address);
12905 if (last_subfile)
c91513d8 12906 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
12907 last_subfile = current_subfile;
12908 }
25e43795 12909 /* Append row to matrix using current values. */
7019d805 12910 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 12911 (*p_record_line) (current_subfile, line, addr);
366da635 12912 }
25e43795 12913 }
ca5f395d 12914 basic_block = 0;
9aa1fe7e
GK
12915 }
12916 else switch (op_code)
c906108c
SS
12917 {
12918 case DW_LNS_extended_op:
3e43a32a
MS
12919 extended_len = read_unsigned_leb128 (abfd, line_ptr,
12920 &bytes_read);
473b7be6 12921 line_ptr += bytes_read;
a8c50c1f 12922 extended_end = line_ptr + extended_len;
c906108c
SS
12923 extended_op = read_1_byte (abfd, line_ptr);
12924 line_ptr += 1;
12925 switch (extended_op)
12926 {
12927 case DW_LNE_end_sequence:
c91513d8 12928 p_record_line = record_line;
c906108c 12929 end_sequence = 1;
c906108c
SS
12930 break;
12931 case DW_LNE_set_address:
e7c27a73 12932 address = read_address (abfd, line_ptr, cu, &bytes_read);
c91513d8
PP
12933
12934 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
12935 {
12936 /* This line table is for a function which has been
12937 GCd by the linker. Ignore it. PR gdb/12528 */
12938
12939 long line_offset
12940 = line_ptr - dwarf2_per_objfile->line.buffer;
12941
12942 complaint (&symfile_complaints,
12943 _(".debug_line address at offset 0x%lx is 0 "
12944 "[in module %s]"),
bb5ed363 12945 line_offset, objfile->name);
c91513d8
PP
12946 p_record_line = noop_record_line;
12947 }
12948
2dc7f7b3 12949 op_index = 0;
107d2387
AC
12950 line_ptr += bytes_read;
12951 address += baseaddr;
c906108c
SS
12952 break;
12953 case DW_LNE_define_file:
debd256d
JB
12954 {
12955 char *cur_file;
12956 unsigned int dir_index, mod_time, length;
6e70227d 12957
3e43a32a
MS
12958 cur_file = read_direct_string (abfd, line_ptr,
12959 &bytes_read);
debd256d
JB
12960 line_ptr += bytes_read;
12961 dir_index =
12962 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12963 line_ptr += bytes_read;
12964 mod_time =
12965 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12966 line_ptr += bytes_read;
12967 length =
12968 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
12969 line_ptr += bytes_read;
12970 add_file_name (lh, cur_file, dir_index, mod_time, length);
12971 }
c906108c 12972 break;
d0c6ba3d
CC
12973 case DW_LNE_set_discriminator:
12974 /* The discriminator is not interesting to the debugger;
12975 just ignore it. */
12976 line_ptr = extended_end;
12977 break;
c906108c 12978 default:
4d3c2250 12979 complaint (&symfile_complaints,
e2e0b3e5 12980 _("mangled .debug_line section"));
debd256d 12981 return;
c906108c 12982 }
a8c50c1f
DJ
12983 /* Make sure that we parsed the extended op correctly. If e.g.
12984 we expected a different address size than the producer used,
12985 we may have read the wrong number of bytes. */
12986 if (line_ptr != extended_end)
12987 {
12988 complaint (&symfile_complaints,
12989 _("mangled .debug_line section"));
12990 return;
12991 }
c906108c
SS
12992 break;
12993 case DW_LNS_copy:
59205f5a 12994 if (lh->num_file_names < file || file == 0)
25e43795
DJ
12995 dwarf2_debug_line_missing_file_complaint ();
12996 else
366da635 12997 {
25e43795 12998 lh->file_names[file - 1].included_p = 1;
ca5f395d 12999 if (!decode_for_pst_p && is_stmt)
fbf65064
UW
13000 {
13001 if (last_subfile != current_subfile)
13002 {
13003 addr = gdbarch_addr_bits_remove (gdbarch, address);
13004 if (last_subfile)
c91513d8 13005 (*p_record_line) (last_subfile, 0, addr);
fbf65064
UW
13006 last_subfile = current_subfile;
13007 }
7019d805 13008 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13009 (*p_record_line) (current_subfile, line, addr);
fbf65064 13010 }
366da635 13011 }
c906108c
SS
13012 basic_block = 0;
13013 break;
13014 case DW_LNS_advance_pc:
2dc7f7b3
TT
13015 {
13016 CORE_ADDR adjust
13017 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13018
13019 address += (((op_index + adjust)
13020 / lh->maximum_ops_per_instruction)
13021 * lh->minimum_instruction_length);
13022 op_index = ((op_index + adjust)
13023 % lh->maximum_ops_per_instruction);
13024 line_ptr += bytes_read;
13025 }
c906108c
SS
13026 break;
13027 case DW_LNS_advance_line:
13028 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
13029 line_ptr += bytes_read;
13030 break;
13031 case DW_LNS_set_file:
debd256d 13032 {
a738430d
MK
13033 /* The arrays lh->include_dirs and lh->file_names are
13034 0-based, but the directory and file name numbers in
13035 the statement program are 1-based. */
debd256d 13036 struct file_entry *fe;
4f1520fb 13037 char *dir = NULL;
a738430d 13038
debd256d
JB
13039 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13040 line_ptr += bytes_read;
59205f5a 13041 if (lh->num_file_names < file || file == 0)
25e43795
DJ
13042 dwarf2_debug_line_missing_file_complaint ();
13043 else
13044 {
13045 fe = &lh->file_names[file - 1];
13046 if (fe->dir_index)
13047 dir = lh->include_dirs[fe->dir_index - 1];
13048 if (!decode_for_pst_p)
13049 {
13050 last_subfile = current_subfile;
13051 dwarf2_start_subfile (fe->name, dir, comp_dir);
13052 }
13053 }
debd256d 13054 }
c906108c
SS
13055 break;
13056 case DW_LNS_set_column:
13057 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13058 line_ptr += bytes_read;
13059 break;
13060 case DW_LNS_negate_stmt:
13061 is_stmt = (!is_stmt);
13062 break;
13063 case DW_LNS_set_basic_block:
13064 basic_block = 1;
13065 break;
c2c6d25f
JM
13066 /* Add to the address register of the state machine the
13067 address increment value corresponding to special opcode
a738430d
MK
13068 255. I.e., this value is scaled by the minimum
13069 instruction length since special opcode 255 would have
b021a221 13070 scaled the increment. */
c906108c 13071 case DW_LNS_const_add_pc:
2dc7f7b3
TT
13072 {
13073 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
13074
13075 address += (((op_index + adjust)
13076 / lh->maximum_ops_per_instruction)
13077 * lh->minimum_instruction_length);
13078 op_index = ((op_index + adjust)
13079 % lh->maximum_ops_per_instruction);
13080 }
c906108c
SS
13081 break;
13082 case DW_LNS_fixed_advance_pc:
13083 address += read_2_bytes (abfd, line_ptr);
2dc7f7b3 13084 op_index = 0;
c906108c
SS
13085 line_ptr += 2;
13086 break;
9aa1fe7e 13087 default:
a738430d
MK
13088 {
13089 /* Unknown standard opcode, ignore it. */
9aa1fe7e 13090 int i;
a738430d 13091
debd256d 13092 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
13093 {
13094 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
13095 line_ptr += bytes_read;
13096 }
13097 }
c906108c
SS
13098 }
13099 }
59205f5a
JB
13100 if (lh->num_file_names < file || file == 0)
13101 dwarf2_debug_line_missing_file_complaint ();
13102 else
13103 {
13104 lh->file_names[file - 1].included_p = 1;
13105 if (!decode_for_pst_p)
fbf65064
UW
13106 {
13107 addr = gdbarch_addr_bits_remove (gdbarch, address);
c91513d8 13108 (*p_record_line) (current_subfile, 0, addr);
fbf65064 13109 }
59205f5a 13110 }
c906108c 13111 }
f3f5162e
DE
13112}
13113
13114/* Decode the Line Number Program (LNP) for the given line_header
13115 structure and CU. The actual information extracted and the type
13116 of structures created from the LNP depends on the value of PST.
13117
13118 1. If PST is NULL, then this procedure uses the data from the program
13119 to create all necessary symbol tables, and their linetables.
13120
13121 2. If PST is not NULL, this procedure reads the program to determine
13122 the list of files included by the unit represented by PST, and
13123 builds all the associated partial symbol tables.
13124
13125 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
13126 It is used for relative paths in the line table.
13127 NOTE: When processing partial symtabs (pst != NULL),
13128 comp_dir == pst->dirname.
13129
13130 NOTE: It is important that psymtabs have the same file name (via strcmp)
13131 as the corresponding symtab. Since COMP_DIR is not used in the name of the
13132 symtab we don't use it in the name of the psymtabs we create.
13133 E.g. expand_line_sal requires this when finding psymtabs to expand.
13134 A good testcase for this is mb-inline.exp. */
13135
13136static void
13137dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
13138 struct dwarf2_cu *cu, struct partial_symtab *pst,
13139 int want_line_info)
13140{
13141 struct objfile *objfile = cu->objfile;
13142 const int decode_for_pst_p = (pst != NULL);
13143 struct subfile *first_subfile = current_subfile;
13144
13145 if (want_line_info)
13146 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
aaa75496
JB
13147
13148 if (decode_for_pst_p)
13149 {
13150 int file_index;
13151
13152 /* Now that we're done scanning the Line Header Program, we can
13153 create the psymtab of each included file. */
13154 for (file_index = 0; file_index < lh->num_file_names; file_index++)
13155 if (lh->file_names[file_index].included_p == 1)
13156 {
c6da4cef
DE
13157 char *include_name =
13158 psymtab_include_file_name (lh, file_index, pst, comp_dir);
13159 if (include_name != NULL)
aaa75496
JB
13160 dwarf2_create_include_psymtab (include_name, pst, objfile);
13161 }
13162 }
cb1df416
DJ
13163 else
13164 {
13165 /* Make sure a symtab is created for every file, even files
13166 which contain only variables (i.e. no code with associated
13167 line numbers). */
cb1df416 13168 int i;
cb1df416
DJ
13169
13170 for (i = 0; i < lh->num_file_names; i++)
13171 {
13172 char *dir = NULL;
f3f5162e 13173 struct file_entry *fe;
9a619af0 13174
cb1df416
DJ
13175 fe = &lh->file_names[i];
13176 if (fe->dir_index)
13177 dir = lh->include_dirs[fe->dir_index - 1];
13178 dwarf2_start_subfile (fe->name, dir, comp_dir);
13179
13180 /* Skip the main file; we don't need it, and it must be
13181 allocated last, so that it will show up before the
13182 non-primary symtabs in the objfile's symtab list. */
13183 if (current_subfile == first_subfile)
13184 continue;
13185
13186 if (current_subfile->symtab == NULL)
13187 current_subfile->symtab = allocate_symtab (current_subfile->name,
bb5ed363 13188 objfile);
cb1df416
DJ
13189 fe->symtab = current_subfile->symtab;
13190 }
13191 }
c906108c
SS
13192}
13193
13194/* Start a subfile for DWARF. FILENAME is the name of the file and
13195 DIRNAME the name of the source directory which contains FILENAME
4f1520fb
FR
13196 or NULL if not known. COMP_DIR is the compilation directory for the
13197 linetable's compilation unit or NULL if not known.
c906108c
SS
13198 This routine tries to keep line numbers from identical absolute and
13199 relative file names in a common subfile.
13200
13201 Using the `list' example from the GDB testsuite, which resides in
13202 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
13203 of /srcdir/list0.c yields the following debugging information for list0.c:
13204
c5aa993b
JM
13205 DW_AT_name: /srcdir/list0.c
13206 DW_AT_comp_dir: /compdir
357e46e7 13207 files.files[0].name: list0.h
c5aa993b 13208 files.files[0].dir: /srcdir
357e46e7 13209 files.files[1].name: list0.c
c5aa993b 13210 files.files[1].dir: /srcdir
c906108c
SS
13211
13212 The line number information for list0.c has to end up in a single
4f1520fb
FR
13213 subfile, so that `break /srcdir/list0.c:1' works as expected.
13214 start_subfile will ensure that this happens provided that we pass the
13215 concatenation of files.files[1].dir and files.files[1].name as the
13216 subfile's name. */
c906108c
SS
13217
13218static void
3e43a32a
MS
13219dwarf2_start_subfile (char *filename, const char *dirname,
13220 const char *comp_dir)
c906108c 13221{
4f1520fb
FR
13222 char *fullname;
13223
13224 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
13225 `start_symtab' will always pass the contents of DW_AT_comp_dir as
13226 second argument to start_subfile. To be consistent, we do the
13227 same here. In order not to lose the line information directory,
13228 we concatenate it to the filename when it makes sense.
13229 Note that the Dwarf3 standard says (speaking of filenames in line
13230 information): ``The directory index is ignored for file names
13231 that represent full path names''. Thus ignoring dirname in the
13232 `else' branch below isn't an issue. */
c906108c 13233
d5166ae1 13234 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
4f1520fb
FR
13235 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
13236 else
13237 fullname = filename;
c906108c 13238
4f1520fb
FR
13239 start_subfile (fullname, comp_dir);
13240
13241 if (fullname != filename)
13242 xfree (fullname);
c906108c
SS
13243}
13244
4c2df51b
DJ
13245static void
13246var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 13247 struct dwarf2_cu *cu)
4c2df51b 13248{
e7c27a73
DJ
13249 struct objfile *objfile = cu->objfile;
13250 struct comp_unit_head *cu_header = &cu->header;
13251
4c2df51b
DJ
13252 /* NOTE drow/2003-01-30: There used to be a comment and some special
13253 code here to turn a symbol with DW_AT_external and a
13254 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
13255 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
13256 with some versions of binutils) where shared libraries could have
13257 relocations against symbols in their debug information - the
13258 minimal symbol would have the right address, but the debug info
13259 would not. It's no longer necessary, because we will explicitly
13260 apply relocations when we read in the debug information now. */
13261
13262 /* A DW_AT_location attribute with no contents indicates that a
13263 variable has been optimized away. */
13264 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
13265 {
13266 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
13267 return;
13268 }
13269
13270 /* Handle one degenerate form of location expression specially, to
13271 preserve GDB's previous behavior when section offsets are
3019eac3
DE
13272 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
13273 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
13274
13275 if (attr_form_is_block (attr)
3019eac3
DE
13276 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
13277 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
13278 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
13279 && (DW_BLOCK (attr)->size
13280 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 13281 {
891d2f0b 13282 unsigned int dummy;
4c2df51b 13283
3019eac3
DE
13284 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
13285 SYMBOL_VALUE_ADDRESS (sym) =
13286 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
13287 else
13288 SYMBOL_VALUE_ADDRESS (sym) =
13289 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
907fc202 13290 SYMBOL_CLASS (sym) = LOC_STATIC;
4c2df51b
DJ
13291 fixup_symbol_section (sym, objfile);
13292 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
13293 SYMBOL_SECTION (sym));
4c2df51b
DJ
13294 return;
13295 }
13296
13297 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
13298 expression evaluator, and use LOC_COMPUTED only when necessary
13299 (i.e. when the value of a register or memory location is
13300 referenced, or a thread-local block, etc.). Then again, it might
13301 not be worthwhile. I'm assuming that it isn't unless performance
13302 or memory numbers show me otherwise. */
13303
e7c27a73 13304 dwarf2_symbol_mark_computed (attr, sym, cu);
4c2df51b 13305 SYMBOL_CLASS (sym) = LOC_COMPUTED;
8be455d7
JK
13306
13307 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
13308 cu->has_loclist = 1;
4c2df51b
DJ
13309}
13310
c906108c
SS
13311/* Given a pointer to a DWARF information entry, figure out if we need
13312 to make a symbol table entry for it, and if so, create a new entry
13313 and return a pointer to it.
13314 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
13315 used the passed type.
13316 If SPACE is not NULL, use it to hold the new symbol. If it is
13317 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
13318
13319static struct symbol *
34eaf542
TT
13320new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
13321 struct symbol *space)
c906108c 13322{
e7c27a73 13323 struct objfile *objfile = cu->objfile;
c906108c
SS
13324 struct symbol *sym = NULL;
13325 char *name;
13326 struct attribute *attr = NULL;
13327 struct attribute *attr2 = NULL;
e142c38c 13328 CORE_ADDR baseaddr;
e37fd15a
SW
13329 struct pending **list_to_add = NULL;
13330
edb3359d 13331 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
13332
13333 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 13334
94af9270 13335 name = dwarf2_name (die, cu);
c906108c
SS
13336 if (name)
13337 {
94af9270 13338 const char *linkagename;
34eaf542 13339 int suppress_add = 0;
94af9270 13340
34eaf542
TT
13341 if (space)
13342 sym = space;
13343 else
13344 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
c906108c 13345 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
13346
13347 /* Cache this symbol's name and the name's demangled form (if any). */
33e5013e 13348 SYMBOL_SET_LANGUAGE (sym, cu->language);
94af9270
KS
13349 linkagename = dwarf2_physname (name, die, cu);
13350 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 13351
f55ee35c
JK
13352 /* Fortran does not have mangling standard and the mangling does differ
13353 between gfortran, iFort etc. */
13354 if (cu->language == language_fortran
b250c185 13355 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d
SW
13356 symbol_set_demangled_name (&(sym->ginfo),
13357 (char *) dwarf2_full_name (name, die, cu),
13358 NULL);
f55ee35c 13359
c906108c 13360 /* Default assumptions.
c5aa993b 13361 Use the passed type or decode it from the die. */
176620f1 13362 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
875dc2fc 13363 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
13364 if (type != NULL)
13365 SYMBOL_TYPE (sym) = type;
13366 else
e7c27a73 13367 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
13368 attr = dwarf2_attr (die,
13369 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
13370 cu);
c906108c
SS
13371 if (attr)
13372 {
13373 SYMBOL_LINE (sym) = DW_UNSND (attr);
13374 }
cb1df416 13375
edb3359d
DJ
13376 attr = dwarf2_attr (die,
13377 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
13378 cu);
cb1df416
DJ
13379 if (attr)
13380 {
13381 int file_index = DW_UNSND (attr);
9a619af0 13382
cb1df416
DJ
13383 if (cu->line_header == NULL
13384 || file_index > cu->line_header->num_file_names)
13385 complaint (&symfile_complaints,
13386 _("file index out of range"));
1c3d648d 13387 else if (file_index > 0)
cb1df416
DJ
13388 {
13389 struct file_entry *fe;
9a619af0 13390
cb1df416
DJ
13391 fe = &cu->line_header->file_names[file_index - 1];
13392 SYMBOL_SYMTAB (sym) = fe->symtab;
13393 }
13394 }
13395
c906108c
SS
13396 switch (die->tag)
13397 {
13398 case DW_TAG_label:
e142c38c 13399 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c
SS
13400 if (attr)
13401 {
13402 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
13403 }
0f5238ed
TT
13404 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
13405 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
c906108c 13406 SYMBOL_CLASS (sym) = LOC_LABEL;
0f5238ed 13407 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
13408 break;
13409 case DW_TAG_subprogram:
13410 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13411 finish_block. */
13412 SYMBOL_CLASS (sym) = LOC_BLOCK;
e142c38c 13413 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
13414 if ((attr2 && (DW_UNSND (attr2) != 0))
13415 || cu->language == language_ada)
c906108c 13416 {
2cfa0c8d
JB
13417 /* Subprograms marked external are stored as a global symbol.
13418 Ada subprograms, whether marked external or not, are always
13419 stored as a global symbol, because we want to be able to
13420 access them globally. For instance, we want to be able
13421 to break on a nested subprogram without having to
13422 specify the context. */
e37fd15a 13423 list_to_add = &global_symbols;
c906108c
SS
13424 }
13425 else
13426 {
e37fd15a 13427 list_to_add = cu->list_in_scope;
c906108c
SS
13428 }
13429 break;
edb3359d
DJ
13430 case DW_TAG_inlined_subroutine:
13431 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
13432 finish_block. */
13433 SYMBOL_CLASS (sym) = LOC_BLOCK;
13434 SYMBOL_INLINED (sym) = 1;
481860b3 13435 list_to_add = cu->list_in_scope;
edb3359d 13436 break;
34eaf542
TT
13437 case DW_TAG_template_value_param:
13438 suppress_add = 1;
13439 /* Fall through. */
72929c62 13440 case DW_TAG_constant:
c906108c 13441 case DW_TAG_variable:
254e6b9e 13442 case DW_TAG_member:
0963b4bd
MS
13443 /* Compilation with minimal debug info may result in
13444 variables with missing type entries. Change the
13445 misleading `void' type to something sensible. */
c906108c 13446 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
64c50499 13447 SYMBOL_TYPE (sym)
46bf5051 13448 = objfile_type (objfile)->nodebug_data_symbol;
64c50499 13449
e142c38c 13450 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
13451 /* In the case of DW_TAG_member, we should only be called for
13452 static const members. */
13453 if (die->tag == DW_TAG_member)
13454 {
3863f96c
DE
13455 /* dwarf2_add_field uses die_is_declaration,
13456 so we do the same. */
254e6b9e
DE
13457 gdb_assert (die_is_declaration (die, cu));
13458 gdb_assert (attr);
13459 }
c906108c
SS
13460 if (attr)
13461 {
e7c27a73 13462 dwarf2_const_value (attr, sym, cu);
e142c38c 13463 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 13464 if (!suppress_add)
34eaf542
TT
13465 {
13466 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 13467 list_to_add = &global_symbols;
34eaf542 13468 else
e37fd15a 13469 list_to_add = cu->list_in_scope;
34eaf542 13470 }
c906108c
SS
13471 break;
13472 }
e142c38c 13473 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13474 if (attr)
13475 {
e7c27a73 13476 var_decode_location (attr, sym, cu);
e142c38c 13477 attr2 = dwarf2_attr (die, DW_AT_external, cu);
caac4577
JG
13478 if (SYMBOL_CLASS (sym) == LOC_STATIC
13479 && SYMBOL_VALUE_ADDRESS (sym) == 0
13480 && !dwarf2_per_objfile->has_section_at_zero)
13481 {
13482 /* When a static variable is eliminated by the linker,
13483 the corresponding debug information is not stripped
13484 out, but the variable address is set to null;
13485 do not add such variables into symbol table. */
13486 }
13487 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 13488 {
f55ee35c
JK
13489 /* Workaround gfortran PR debug/40040 - it uses
13490 DW_AT_location for variables in -fPIC libraries which may
13491 get overriden by other libraries/executable and get
13492 a different address. Resolve it by the minimal symbol
13493 which may come from inferior's executable using copy
13494 relocation. Make this workaround only for gfortran as for
13495 other compilers GDB cannot guess the minimal symbol
13496 Fortran mangling kind. */
13497 if (cu->language == language_fortran && die->parent
13498 && die->parent->tag == DW_TAG_module
13499 && cu->producer
13500 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
13501 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
13502
1c809c68
TT
13503 /* A variable with DW_AT_external is never static,
13504 but it may be block-scoped. */
13505 list_to_add = (cu->list_in_scope == &file_symbols
13506 ? &global_symbols : cu->list_in_scope);
1c809c68 13507 }
c906108c 13508 else
e37fd15a 13509 list_to_add = cu->list_in_scope;
c906108c
SS
13510 }
13511 else
13512 {
13513 /* We do not know the address of this symbol.
c5aa993b
JM
13514 If it is an external symbol and we have type information
13515 for it, enter the symbol as a LOC_UNRESOLVED symbol.
13516 The address of the variable will then be determined from
13517 the minimal symbol table whenever the variable is
13518 referenced. */
e142c38c 13519 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c906108c 13520 if (attr2 && (DW_UNSND (attr2) != 0)
e142c38c 13521 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 13522 {
0fe7935b
DJ
13523 /* A variable with DW_AT_external is never static, but it
13524 may be block-scoped. */
13525 list_to_add = (cu->list_in_scope == &file_symbols
13526 ? &global_symbols : cu->list_in_scope);
13527
c906108c 13528 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
c906108c 13529 }
442ddf59
JK
13530 else if (!die_is_declaration (die, cu))
13531 {
13532 /* Use the default LOC_OPTIMIZED_OUT class. */
13533 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
13534 if (!suppress_add)
13535 list_to_add = cu->list_in_scope;
442ddf59 13536 }
c906108c
SS
13537 }
13538 break;
13539 case DW_TAG_formal_parameter:
edb3359d
DJ
13540 /* If we are inside a function, mark this as an argument. If
13541 not, we might be looking at an argument to an inlined function
13542 when we do not have enough information to show inlined frames;
13543 pretend it's a local variable in that case so that the user can
13544 still see it. */
13545 if (context_stack_depth > 0
13546 && context_stack[context_stack_depth - 1].name != NULL)
13547 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 13548 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
13549 if (attr)
13550 {
e7c27a73 13551 var_decode_location (attr, sym, cu);
c906108c 13552 }
e142c38c 13553 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13554 if (attr)
13555 {
e7c27a73 13556 dwarf2_const_value (attr, sym, cu);
c906108c 13557 }
f346a30d 13558
e37fd15a 13559 list_to_add = cu->list_in_scope;
c906108c
SS
13560 break;
13561 case DW_TAG_unspecified_parameters:
13562 /* From varargs functions; gdb doesn't seem to have any
13563 interest in this information, so just ignore it for now.
13564 (FIXME?) */
13565 break;
34eaf542
TT
13566 case DW_TAG_template_type_param:
13567 suppress_add = 1;
13568 /* Fall through. */
c906108c 13569 case DW_TAG_class_type:
680b30c7 13570 case DW_TAG_interface_type:
c906108c
SS
13571 case DW_TAG_structure_type:
13572 case DW_TAG_union_type:
72019c9c 13573 case DW_TAG_set_type:
c906108c
SS
13574 case DW_TAG_enumeration_type:
13575 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13576 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 13577
63d06c5c 13578 {
987504bb 13579 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
63d06c5c
DC
13580 really ever be static objects: otherwise, if you try
13581 to, say, break of a class's method and you're in a file
13582 which doesn't mention that class, it won't work unless
13583 the check for all static symbols in lookup_symbol_aux
13584 saves you. See the OtherFileClass tests in
13585 gdb.c++/namespace.exp. */
13586
e37fd15a 13587 if (!suppress_add)
34eaf542 13588 {
34eaf542
TT
13589 list_to_add = (cu->list_in_scope == &file_symbols
13590 && (cu->language == language_cplus
13591 || cu->language == language_java)
13592 ? &global_symbols : cu->list_in_scope);
63d06c5c 13593
64382290
TT
13594 /* The semantics of C++ state that "struct foo {
13595 ... }" also defines a typedef for "foo". A Java
13596 class declaration also defines a typedef for the
13597 class. */
13598 if (cu->language == language_cplus
13599 || cu->language == language_java
13600 || cu->language == language_ada)
13601 {
13602 /* The symbol's name is already allocated along
13603 with this objfile, so we don't need to
13604 duplicate it for the type. */
13605 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
13606 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
13607 }
63d06c5c
DC
13608 }
13609 }
c906108c
SS
13610 break;
13611 case DW_TAG_typedef:
63d06c5c
DC
13612 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
13613 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13614 list_to_add = cu->list_in_scope;
63d06c5c 13615 break;
c906108c 13616 case DW_TAG_base_type:
a02abb62 13617 case DW_TAG_subrange_type:
c906108c 13618 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
176620f1 13619 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 13620 list_to_add = cu->list_in_scope;
c906108c
SS
13621 break;
13622 case DW_TAG_enumerator:
e142c38c 13623 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
13624 if (attr)
13625 {
e7c27a73 13626 dwarf2_const_value (attr, sym, cu);
c906108c 13627 }
63d06c5c
DC
13628 {
13629 /* NOTE: carlton/2003-11-10: See comment above in the
13630 DW_TAG_class_type, etc. block. */
13631
e142c38c 13632 list_to_add = (cu->list_in_scope == &file_symbols
987504bb
JJ
13633 && (cu->language == language_cplus
13634 || cu->language == language_java)
e142c38c 13635 ? &global_symbols : cu->list_in_scope);
63d06c5c 13636 }
c906108c 13637 break;
5c4e30ca
DC
13638 case DW_TAG_namespace:
13639 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
e37fd15a 13640 list_to_add = &global_symbols;
5c4e30ca 13641 break;
c906108c
SS
13642 default:
13643 /* Not a tag we recognize. Hopefully we aren't processing
13644 trash data, but since we must specifically ignore things
13645 we don't recognize, there is nothing else we should do at
0963b4bd 13646 this point. */
e2e0b3e5 13647 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
4d3c2250 13648 dwarf_tag_name (die->tag));
c906108c
SS
13649 break;
13650 }
df8a16a1 13651
e37fd15a
SW
13652 if (suppress_add)
13653 {
13654 sym->hash_next = objfile->template_symbols;
13655 objfile->template_symbols = sym;
13656 list_to_add = NULL;
13657 }
13658
13659 if (list_to_add != NULL)
13660 add_symbol_to_list (sym, list_to_add);
13661
df8a16a1
DJ
13662 /* For the benefit of old versions of GCC, check for anonymous
13663 namespaces based on the demangled name. */
13664 if (!processing_has_namespace_info
94af9270 13665 && cu->language == language_cplus)
a10964d1 13666 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
13667 }
13668 return (sym);
13669}
13670
34eaf542
TT
13671/* A wrapper for new_symbol_full that always allocates a new symbol. */
13672
13673static struct symbol *
13674new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
13675{
13676 return new_symbol_full (die, type, cu, NULL);
13677}
13678
98bfdba5
PA
13679/* Given an attr with a DW_FORM_dataN value in host byte order,
13680 zero-extend it as appropriate for the symbol's type. The DWARF
13681 standard (v4) is not entirely clear about the meaning of using
13682 DW_FORM_dataN for a constant with a signed type, where the type is
13683 wider than the data. The conclusion of a discussion on the DWARF
13684 list was that this is unspecified. We choose to always zero-extend
13685 because that is the interpretation long in use by GCC. */
c906108c 13686
98bfdba5
PA
13687static gdb_byte *
13688dwarf2_const_value_data (struct attribute *attr, struct type *type,
13689 const char *name, struct obstack *obstack,
12df843f 13690 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 13691{
e7c27a73 13692 struct objfile *objfile = cu->objfile;
e17a4113
UW
13693 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
13694 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
13695 LONGEST l = DW_UNSND (attr);
13696
13697 if (bits < sizeof (*value) * 8)
13698 {
13699 l &= ((LONGEST) 1 << bits) - 1;
13700 *value = l;
13701 }
13702 else if (bits == sizeof (*value) * 8)
13703 *value = l;
13704 else
13705 {
13706 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
13707 store_unsigned_integer (bytes, bits / 8, byte_order, l);
13708 return bytes;
13709 }
13710
13711 return NULL;
13712}
13713
13714/* Read a constant value from an attribute. Either set *VALUE, or if
13715 the value does not fit in *VALUE, set *BYTES - either already
13716 allocated on the objfile obstack, or newly allocated on OBSTACK,
13717 or, set *BATON, if we translated the constant to a location
13718 expression. */
13719
13720static void
13721dwarf2_const_value_attr (struct attribute *attr, struct type *type,
13722 const char *name, struct obstack *obstack,
13723 struct dwarf2_cu *cu,
12df843f 13724 LONGEST *value, gdb_byte **bytes,
98bfdba5
PA
13725 struct dwarf2_locexpr_baton **baton)
13726{
13727 struct objfile *objfile = cu->objfile;
13728 struct comp_unit_head *cu_header = &cu->header;
c906108c 13729 struct dwarf_block *blk;
98bfdba5
PA
13730 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
13731 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
13732
13733 *value = 0;
13734 *bytes = NULL;
13735 *baton = NULL;
c906108c
SS
13736
13737 switch (attr->form)
13738 {
13739 case DW_FORM_addr:
3019eac3 13740 case DW_FORM_GNU_addr_index:
ac56253d 13741 {
ac56253d
TT
13742 gdb_byte *data;
13743
98bfdba5
PA
13744 if (TYPE_LENGTH (type) != cu_header->addr_size)
13745 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 13746 cu_header->addr_size,
98bfdba5 13747 TYPE_LENGTH (type));
ac56253d
TT
13748 /* Symbols of this form are reasonably rare, so we just
13749 piggyback on the existing location code rather than writing
13750 a new implementation of symbol_computed_ops. */
98bfdba5
PA
13751 *baton = obstack_alloc (&objfile->objfile_obstack,
13752 sizeof (struct dwarf2_locexpr_baton));
13753 (*baton)->per_cu = cu->per_cu;
13754 gdb_assert ((*baton)->per_cu);
ac56253d 13755
98bfdba5
PA
13756 (*baton)->size = 2 + cu_header->addr_size;
13757 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
13758 (*baton)->data = data;
ac56253d
TT
13759
13760 data[0] = DW_OP_addr;
13761 store_unsigned_integer (&data[1], cu_header->addr_size,
13762 byte_order, DW_ADDR (attr));
13763 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 13764 }
c906108c 13765 break;
4ac36638 13766 case DW_FORM_string:
93b5768b 13767 case DW_FORM_strp:
3019eac3 13768 case DW_FORM_GNU_str_index:
98bfdba5
PA
13769 /* DW_STRING is already allocated on the objfile obstack, point
13770 directly to it. */
13771 *bytes = (gdb_byte *) DW_STRING (attr);
93b5768b 13772 break;
c906108c
SS
13773 case DW_FORM_block1:
13774 case DW_FORM_block2:
13775 case DW_FORM_block4:
13776 case DW_FORM_block:
2dc7f7b3 13777 case DW_FORM_exprloc:
c906108c 13778 blk = DW_BLOCK (attr);
98bfdba5
PA
13779 if (TYPE_LENGTH (type) != blk->size)
13780 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
13781 TYPE_LENGTH (type));
13782 *bytes = blk->data;
c906108c 13783 break;
2df3850c
JM
13784
13785 /* The DW_AT_const_value attributes are supposed to carry the
13786 symbol's value "represented as it would be on the target
13787 architecture." By the time we get here, it's already been
13788 converted to host endianness, so we just need to sign- or
13789 zero-extend it as appropriate. */
13790 case DW_FORM_data1:
3e43a32a
MS
13791 *bytes = dwarf2_const_value_data (attr, type, name,
13792 obstack, cu, value, 8);
2df3850c 13793 break;
c906108c 13794 case DW_FORM_data2:
3e43a32a
MS
13795 *bytes = dwarf2_const_value_data (attr, type, name,
13796 obstack, cu, value, 16);
2df3850c 13797 break;
c906108c 13798 case DW_FORM_data4:
3e43a32a
MS
13799 *bytes = dwarf2_const_value_data (attr, type, name,
13800 obstack, cu, value, 32);
2df3850c 13801 break;
c906108c 13802 case DW_FORM_data8:
3e43a32a
MS
13803 *bytes = dwarf2_const_value_data (attr, type, name,
13804 obstack, cu, value, 64);
2df3850c
JM
13805 break;
13806
c906108c 13807 case DW_FORM_sdata:
98bfdba5 13808 *value = DW_SND (attr);
2df3850c
JM
13809 break;
13810
c906108c 13811 case DW_FORM_udata:
98bfdba5 13812 *value = DW_UNSND (attr);
c906108c 13813 break;
2df3850c 13814
c906108c 13815 default:
4d3c2250 13816 complaint (&symfile_complaints,
e2e0b3e5 13817 _("unsupported const value attribute form: '%s'"),
4d3c2250 13818 dwarf_form_name (attr->form));
98bfdba5 13819 *value = 0;
c906108c
SS
13820 break;
13821 }
13822}
13823
2df3850c 13824
98bfdba5
PA
13825/* Copy constant value from an attribute to a symbol. */
13826
2df3850c 13827static void
98bfdba5
PA
13828dwarf2_const_value (struct attribute *attr, struct symbol *sym,
13829 struct dwarf2_cu *cu)
2df3850c 13830{
98bfdba5
PA
13831 struct objfile *objfile = cu->objfile;
13832 struct comp_unit_head *cu_header = &cu->header;
12df843f 13833 LONGEST value;
98bfdba5
PA
13834 gdb_byte *bytes;
13835 struct dwarf2_locexpr_baton *baton;
2df3850c 13836
98bfdba5
PA
13837 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
13838 SYMBOL_PRINT_NAME (sym),
13839 &objfile->objfile_obstack, cu,
13840 &value, &bytes, &baton);
2df3850c 13841
98bfdba5
PA
13842 if (baton != NULL)
13843 {
13844 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13845 SYMBOL_LOCATION_BATON (sym) = baton;
13846 SYMBOL_CLASS (sym) = LOC_COMPUTED;
13847 }
13848 else if (bytes != NULL)
13849 {
13850 SYMBOL_VALUE_BYTES (sym) = bytes;
13851 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
13852 }
13853 else
13854 {
13855 SYMBOL_VALUE (sym) = value;
13856 SYMBOL_CLASS (sym) = LOC_CONST;
13857 }
2df3850c
JM
13858}
13859
c906108c
SS
13860/* Return the type of the die in question using its DW_AT_type attribute. */
13861
13862static struct type *
e7c27a73 13863die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13864{
c906108c 13865 struct attribute *type_attr;
c906108c 13866
e142c38c 13867 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
13868 if (!type_attr)
13869 {
13870 /* A missing DW_AT_type represents a void type. */
46bf5051 13871 return objfile_type (cu->objfile)->builtin_void;
c906108c 13872 }
348e048f 13873
673bfd45 13874 return lookup_die_type (die, type_attr, cu);
c906108c
SS
13875}
13876
b4ba55a1
JB
13877/* True iff CU's producer generates GNAT Ada auxiliary information
13878 that allows to find parallel types through that information instead
13879 of having to do expensive parallel lookups by type name. */
13880
13881static int
13882need_gnat_info (struct dwarf2_cu *cu)
13883{
13884 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
13885 of GNAT produces this auxiliary information, without any indication
13886 that it is produced. Part of enhancing the FSF version of GNAT
13887 to produce that information will be to put in place an indicator
13888 that we can use in order to determine whether the descriptive type
13889 info is available or not. One suggestion that has been made is
13890 to use a new attribute, attached to the CU die. For now, assume
13891 that the descriptive type info is not available. */
13892 return 0;
13893}
13894
b4ba55a1
JB
13895/* Return the auxiliary type of the die in question using its
13896 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
13897 attribute is not present. */
13898
13899static struct type *
13900die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
13901{
b4ba55a1 13902 struct attribute *type_attr;
b4ba55a1
JB
13903
13904 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
13905 if (!type_attr)
13906 return NULL;
13907
673bfd45 13908 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
13909}
13910
13911/* If DIE has a descriptive_type attribute, then set the TYPE's
13912 descriptive type accordingly. */
13913
13914static void
13915set_descriptive_type (struct type *type, struct die_info *die,
13916 struct dwarf2_cu *cu)
13917{
13918 struct type *descriptive_type = die_descriptive_type (die, cu);
13919
13920 if (descriptive_type)
13921 {
13922 ALLOCATE_GNAT_AUX_TYPE (type);
13923 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
13924 }
13925}
13926
c906108c
SS
13927/* Return the containing type of the die in question using its
13928 DW_AT_containing_type attribute. */
13929
13930static struct type *
e7c27a73 13931die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13932{
c906108c 13933 struct attribute *type_attr;
c906108c 13934
e142c38c 13935 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
13936 if (!type_attr)
13937 error (_("Dwarf Error: Problem turning containing type into gdb type "
13938 "[in module %s]"), cu->objfile->name);
13939
673bfd45 13940 return lookup_die_type (die, type_attr, cu);
c906108c
SS
13941}
13942
673bfd45
DE
13943/* Look up the type of DIE in CU using its type attribute ATTR.
13944 If there is no type substitute an error marker. */
13945
c906108c 13946static struct type *
673bfd45
DE
13947lookup_die_type (struct die_info *die, struct attribute *attr,
13948 struct dwarf2_cu *cu)
c906108c 13949{
bb5ed363 13950 struct objfile *objfile = cu->objfile;
f792889a
DJ
13951 struct type *this_type;
13952
673bfd45
DE
13953 /* First see if we have it cached. */
13954
13955 if (is_ref_attr (attr))
13956 {
b64f50a1 13957 sect_offset offset = dwarf2_get_ref_die_offset (attr);
673bfd45
DE
13958
13959 this_type = get_die_type_at_offset (offset, cu->per_cu);
13960 }
55f1336d 13961 else if (attr->form == DW_FORM_ref_sig8)
673bfd45
DE
13962 {
13963 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
673bfd45
DE
13964
13965 /* sig_type will be NULL if the signatured type is missing from
13966 the debug info. */
13967 if (sig_type == NULL)
13968 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13969 "at 0x%x [in module %s]"),
b64f50a1 13970 die->offset.sect_off, objfile->name);
673bfd45 13971
3019eac3
DE
13972 gdb_assert (sig_type->per_cu.is_debug_types);
13973 /* If we haven't filled in type_offset_in_section yet, then we
13974 haven't read the type in yet. */
13975 this_type = NULL;
13976 if (sig_type->type_offset_in_section.sect_off != 0)
13977 {
13978 this_type =
13979 get_die_type_at_offset (sig_type->type_offset_in_section,
13980 &sig_type->per_cu);
13981 }
673bfd45
DE
13982 }
13983 else
13984 {
13985 dump_die_for_error (die);
13986 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
bb5ed363 13987 dwarf_attr_name (attr->name), objfile->name);
673bfd45
DE
13988 }
13989
13990 /* If not cached we need to read it in. */
13991
13992 if (this_type == NULL)
13993 {
13994 struct die_info *type_die;
13995 struct dwarf2_cu *type_cu = cu;
13996
13997 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
3019eac3
DE
13998 /* If we found the type now, it's probably because the type came
13999 from an inter-CU reference and the type's CU got expanded before
14000 ours. */
14001 this_type = get_die_type (type_die, type_cu);
14002 if (this_type == NULL)
14003 this_type = read_type_die_1 (type_die, type_cu);
673bfd45
DE
14004 }
14005
14006 /* If we still don't have a type use an error marker. */
14007
14008 if (this_type == NULL)
c906108c 14009 {
b00fdb78
TT
14010 char *message, *saved;
14011
14012 /* read_type_die already issued a complaint. */
14013 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
bb5ed363 14014 objfile->name,
b64f50a1
JK
14015 cu->header.offset.sect_off,
14016 die->offset.sect_off);
bb5ed363 14017 saved = obstack_copy0 (&objfile->objfile_obstack,
b00fdb78
TT
14018 message, strlen (message));
14019 xfree (message);
14020
bb5ed363 14021 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
c906108c 14022 }
673bfd45 14023
f792889a 14024 return this_type;
c906108c
SS
14025}
14026
673bfd45
DE
14027/* Return the type in DIE, CU.
14028 Returns NULL for invalid types.
14029
14030 This first does a lookup in the appropriate type_hash table,
14031 and only reads the die in if necessary.
14032
14033 NOTE: This can be called when reading in partial or full symbols. */
14034
f792889a 14035static struct type *
e7c27a73 14036read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14037{
f792889a
DJ
14038 struct type *this_type;
14039
14040 this_type = get_die_type (die, cu);
14041 if (this_type)
14042 return this_type;
14043
673bfd45
DE
14044 return read_type_die_1 (die, cu);
14045}
14046
14047/* Read the type in DIE, CU.
14048 Returns NULL for invalid types. */
14049
14050static struct type *
14051read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
14052{
14053 struct type *this_type = NULL;
14054
c906108c
SS
14055 switch (die->tag)
14056 {
14057 case DW_TAG_class_type:
680b30c7 14058 case DW_TAG_interface_type:
c906108c
SS
14059 case DW_TAG_structure_type:
14060 case DW_TAG_union_type:
f792889a 14061 this_type = read_structure_type (die, cu);
c906108c
SS
14062 break;
14063 case DW_TAG_enumeration_type:
f792889a 14064 this_type = read_enumeration_type (die, cu);
c906108c
SS
14065 break;
14066 case DW_TAG_subprogram:
14067 case DW_TAG_subroutine_type:
edb3359d 14068 case DW_TAG_inlined_subroutine:
f792889a 14069 this_type = read_subroutine_type (die, cu);
c906108c
SS
14070 break;
14071 case DW_TAG_array_type:
f792889a 14072 this_type = read_array_type (die, cu);
c906108c 14073 break;
72019c9c 14074 case DW_TAG_set_type:
f792889a 14075 this_type = read_set_type (die, cu);
72019c9c 14076 break;
c906108c 14077 case DW_TAG_pointer_type:
f792889a 14078 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
14079 break;
14080 case DW_TAG_ptr_to_member_type:
f792889a 14081 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
14082 break;
14083 case DW_TAG_reference_type:
f792889a 14084 this_type = read_tag_reference_type (die, cu);
c906108c
SS
14085 break;
14086 case DW_TAG_const_type:
f792889a 14087 this_type = read_tag_const_type (die, cu);
c906108c
SS
14088 break;
14089 case DW_TAG_volatile_type:
f792889a 14090 this_type = read_tag_volatile_type (die, cu);
c906108c
SS
14091 break;
14092 case DW_TAG_string_type:
f792889a 14093 this_type = read_tag_string_type (die, cu);
c906108c
SS
14094 break;
14095 case DW_TAG_typedef:
f792889a 14096 this_type = read_typedef (die, cu);
c906108c 14097 break;
a02abb62 14098 case DW_TAG_subrange_type:
f792889a 14099 this_type = read_subrange_type (die, cu);
a02abb62 14100 break;
c906108c 14101 case DW_TAG_base_type:
f792889a 14102 this_type = read_base_type (die, cu);
c906108c 14103 break;
81a17f79 14104 case DW_TAG_unspecified_type:
f792889a 14105 this_type = read_unspecified_type (die, cu);
81a17f79 14106 break;
0114d602
DJ
14107 case DW_TAG_namespace:
14108 this_type = read_namespace_type (die, cu);
14109 break;
f55ee35c
JK
14110 case DW_TAG_module:
14111 this_type = read_module_type (die, cu);
14112 break;
c906108c 14113 default:
3e43a32a
MS
14114 complaint (&symfile_complaints,
14115 _("unexpected tag in read_type_die: '%s'"),
4d3c2250 14116 dwarf_tag_name (die->tag));
c906108c
SS
14117 break;
14118 }
63d06c5c 14119
f792889a 14120 return this_type;
63d06c5c
DC
14121}
14122
abc72ce4
DE
14123/* See if we can figure out if the class lives in a namespace. We do
14124 this by looking for a member function; its demangled name will
14125 contain namespace info, if there is any.
14126 Return the computed name or NULL.
14127 Space for the result is allocated on the objfile's obstack.
14128 This is the full-die version of guess_partial_die_structure_name.
14129 In this case we know DIE has no useful parent. */
14130
14131static char *
14132guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
14133{
14134 struct die_info *spec_die;
14135 struct dwarf2_cu *spec_cu;
14136 struct die_info *child;
14137
14138 spec_cu = cu;
14139 spec_die = die_specification (die, &spec_cu);
14140 if (spec_die != NULL)
14141 {
14142 die = spec_die;
14143 cu = spec_cu;
14144 }
14145
14146 for (child = die->child;
14147 child != NULL;
14148 child = child->sibling)
14149 {
14150 if (child->tag == DW_TAG_subprogram)
14151 {
14152 struct attribute *attr;
14153
14154 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
14155 if (attr == NULL)
14156 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
14157 if (attr != NULL)
14158 {
14159 char *actual_name
14160 = language_class_name_from_physname (cu->language_defn,
14161 DW_STRING (attr));
14162 char *name = NULL;
14163
14164 if (actual_name != NULL)
14165 {
14166 char *die_name = dwarf2_name (die, cu);
14167
14168 if (die_name != NULL
14169 && strcmp (die_name, actual_name) != 0)
14170 {
14171 /* Strip off the class name from the full name.
14172 We want the prefix. */
14173 int die_name_len = strlen (die_name);
14174 int actual_name_len = strlen (actual_name);
14175
14176 /* Test for '::' as a sanity check. */
14177 if (actual_name_len > die_name_len + 2
3e43a32a
MS
14178 && actual_name[actual_name_len
14179 - die_name_len - 1] == ':')
abc72ce4
DE
14180 name =
14181 obsavestring (actual_name,
14182 actual_name_len - die_name_len - 2,
14183 &cu->objfile->objfile_obstack);
14184 }
14185 }
14186 xfree (actual_name);
14187 return name;
14188 }
14189 }
14190 }
14191
14192 return NULL;
14193}
14194
96408a79
SA
14195/* GCC might emit a nameless typedef that has a linkage name. Determine the
14196 prefix part in such case. See
14197 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14198
14199static char *
14200anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
14201{
14202 struct attribute *attr;
14203 char *base;
14204
14205 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
14206 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
14207 return NULL;
14208
14209 attr = dwarf2_attr (die, DW_AT_name, cu);
14210 if (attr != NULL && DW_STRING (attr) != NULL)
14211 return NULL;
14212
14213 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14214 if (attr == NULL)
14215 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14216 if (attr == NULL || DW_STRING (attr) == NULL)
14217 return NULL;
14218
14219 /* dwarf2_name had to be already called. */
14220 gdb_assert (DW_STRING_IS_CANONICAL (attr));
14221
14222 /* Strip the base name, keep any leading namespaces/classes. */
14223 base = strrchr (DW_STRING (attr), ':');
14224 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
14225 return "";
14226
14227 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
14228 &cu->objfile->objfile_obstack);
14229}
14230
fdde2d81 14231/* Return the name of the namespace/class that DIE is defined within,
0114d602 14232 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 14233
0114d602
DJ
14234 For example, if we're within the method foo() in the following
14235 code:
14236
14237 namespace N {
14238 class C {
14239 void foo () {
14240 }
14241 };
14242 }
14243
14244 then determine_prefix on foo's die will return "N::C". */
fdde2d81 14245
0d5cff50 14246static const char *
e142c38c 14247determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 14248{
0114d602
DJ
14249 struct die_info *parent, *spec_die;
14250 struct dwarf2_cu *spec_cu;
14251 struct type *parent_type;
96408a79 14252 char *retval;
63d06c5c 14253
f55ee35c
JK
14254 if (cu->language != language_cplus && cu->language != language_java
14255 && cu->language != language_fortran)
0114d602
DJ
14256 return "";
14257
96408a79
SA
14258 retval = anonymous_struct_prefix (die, cu);
14259 if (retval)
14260 return retval;
14261
0114d602
DJ
14262 /* We have to be careful in the presence of DW_AT_specification.
14263 For example, with GCC 3.4, given the code
14264
14265 namespace N {
14266 void foo() {
14267 // Definition of N::foo.
14268 }
14269 }
14270
14271 then we'll have a tree of DIEs like this:
14272
14273 1: DW_TAG_compile_unit
14274 2: DW_TAG_namespace // N
14275 3: DW_TAG_subprogram // declaration of N::foo
14276 4: DW_TAG_subprogram // definition of N::foo
14277 DW_AT_specification // refers to die #3
14278
14279 Thus, when processing die #4, we have to pretend that we're in
14280 the context of its DW_AT_specification, namely the contex of die
14281 #3. */
14282 spec_cu = cu;
14283 spec_die = die_specification (die, &spec_cu);
14284 if (spec_die == NULL)
14285 parent = die->parent;
14286 else
63d06c5c 14287 {
0114d602
DJ
14288 parent = spec_die->parent;
14289 cu = spec_cu;
63d06c5c 14290 }
0114d602
DJ
14291
14292 if (parent == NULL)
14293 return "";
98bfdba5
PA
14294 else if (parent->building_fullname)
14295 {
14296 const char *name;
14297 const char *parent_name;
14298
14299 /* It has been seen on RealView 2.2 built binaries,
14300 DW_TAG_template_type_param types actually _defined_ as
14301 children of the parent class:
14302
14303 enum E {};
14304 template class <class Enum> Class{};
14305 Class<enum E> class_e;
14306
14307 1: DW_TAG_class_type (Class)
14308 2: DW_TAG_enumeration_type (E)
14309 3: DW_TAG_enumerator (enum1:0)
14310 3: DW_TAG_enumerator (enum2:1)
14311 ...
14312 2: DW_TAG_template_type_param
14313 DW_AT_type DW_FORM_ref_udata (E)
14314
14315 Besides being broken debug info, it can put GDB into an
14316 infinite loop. Consider:
14317
14318 When we're building the full name for Class<E>, we'll start
14319 at Class, and go look over its template type parameters,
14320 finding E. We'll then try to build the full name of E, and
14321 reach here. We're now trying to build the full name of E,
14322 and look over the parent DIE for containing scope. In the
14323 broken case, if we followed the parent DIE of E, we'd again
14324 find Class, and once again go look at its template type
14325 arguments, etc., etc. Simply don't consider such parent die
14326 as source-level parent of this die (it can't be, the language
14327 doesn't allow it), and break the loop here. */
14328 name = dwarf2_name (die, cu);
14329 parent_name = dwarf2_name (parent, cu);
14330 complaint (&symfile_complaints,
14331 _("template param type '%s' defined within parent '%s'"),
14332 name ? name : "<unknown>",
14333 parent_name ? parent_name : "<unknown>");
14334 return "";
14335 }
63d06c5c 14336 else
0114d602
DJ
14337 switch (parent->tag)
14338 {
63d06c5c 14339 case DW_TAG_namespace:
0114d602 14340 parent_type = read_type_die (parent, cu);
acebe513
UW
14341 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
14342 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
14343 Work around this problem here. */
14344 if (cu->language == language_cplus
14345 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
14346 return "";
0114d602
DJ
14347 /* We give a name to even anonymous namespaces. */
14348 return TYPE_TAG_NAME (parent_type);
63d06c5c 14349 case DW_TAG_class_type:
680b30c7 14350 case DW_TAG_interface_type:
63d06c5c 14351 case DW_TAG_structure_type:
0114d602 14352 case DW_TAG_union_type:
f55ee35c 14353 case DW_TAG_module:
0114d602
DJ
14354 parent_type = read_type_die (parent, cu);
14355 if (TYPE_TAG_NAME (parent_type) != NULL)
14356 return TYPE_TAG_NAME (parent_type);
14357 else
14358 /* An anonymous structure is only allowed non-static data
14359 members; no typedefs, no member functions, et cetera.
14360 So it does not need a prefix. */
14361 return "";
abc72ce4 14362 case DW_TAG_compile_unit:
95554aad 14363 case DW_TAG_partial_unit:
abc72ce4
DE
14364 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
14365 if (cu->language == language_cplus
8b70b953 14366 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
14367 && die->child != NULL
14368 && (die->tag == DW_TAG_class_type
14369 || die->tag == DW_TAG_structure_type
14370 || die->tag == DW_TAG_union_type))
14371 {
14372 char *name = guess_full_die_structure_name (die, cu);
14373 if (name != NULL)
14374 return name;
14375 }
14376 return "";
63d06c5c 14377 default:
8176b9b8 14378 return determine_prefix (parent, cu);
63d06c5c 14379 }
63d06c5c
DC
14380}
14381
3e43a32a
MS
14382/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
14383 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
14384 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
14385 an obconcat, otherwise allocate storage for the result. The CU argument is
14386 used to determine the language and hence, the appropriate separator. */
987504bb 14387
f55ee35c 14388#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
14389
14390static char *
f55ee35c
JK
14391typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
14392 int physname, struct dwarf2_cu *cu)
63d06c5c 14393{
f55ee35c 14394 const char *lead = "";
5c315b68 14395 const char *sep;
63d06c5c 14396
3e43a32a
MS
14397 if (suffix == NULL || suffix[0] == '\0'
14398 || prefix == NULL || prefix[0] == '\0')
987504bb
JJ
14399 sep = "";
14400 else if (cu->language == language_java)
14401 sep = ".";
f55ee35c
JK
14402 else if (cu->language == language_fortran && physname)
14403 {
14404 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
14405 DW_AT_MIPS_linkage_name is preferred and used instead. */
14406
14407 lead = "__";
14408 sep = "_MOD_";
14409 }
987504bb
JJ
14410 else
14411 sep = "::";
63d06c5c 14412
6dd47d34
DE
14413 if (prefix == NULL)
14414 prefix = "";
14415 if (suffix == NULL)
14416 suffix = "";
14417
987504bb
JJ
14418 if (obs == NULL)
14419 {
3e43a32a
MS
14420 char *retval
14421 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
9a619af0 14422
f55ee35c
JK
14423 strcpy (retval, lead);
14424 strcat (retval, prefix);
6dd47d34
DE
14425 strcat (retval, sep);
14426 strcat (retval, suffix);
63d06c5c
DC
14427 return retval;
14428 }
987504bb
JJ
14429 else
14430 {
14431 /* We have an obstack. */
f55ee35c 14432 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 14433 }
63d06c5c
DC
14434}
14435
c906108c
SS
14436/* Return sibling of die, NULL if no sibling. */
14437
f9aca02d 14438static struct die_info *
fba45db2 14439sibling_die (struct die_info *die)
c906108c 14440{
639d11d3 14441 return die->sibling;
c906108c
SS
14442}
14443
71c25dea
TT
14444/* Get name of a die, return NULL if not found. */
14445
14446static char *
14447dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
14448 struct obstack *obstack)
14449{
14450 if (name && cu->language == language_cplus)
14451 {
14452 char *canon_name = cp_canonicalize_string (name);
14453
14454 if (canon_name != NULL)
14455 {
14456 if (strcmp (canon_name, name) != 0)
14457 name = obsavestring (canon_name, strlen (canon_name),
14458 obstack);
14459 xfree (canon_name);
14460 }
14461 }
14462
14463 return name;
c906108c
SS
14464}
14465
9219021c
DC
14466/* Get name of a die, return NULL if not found. */
14467
14468static char *
e142c38c 14469dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
14470{
14471 struct attribute *attr;
14472
e142c38c 14473 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31
TT
14474 if ((!attr || !DW_STRING (attr))
14475 && die->tag != DW_TAG_class_type
14476 && die->tag != DW_TAG_interface_type
14477 && die->tag != DW_TAG_structure_type
14478 && die->tag != DW_TAG_union_type)
71c25dea
TT
14479 return NULL;
14480
14481 switch (die->tag)
14482 {
14483 case DW_TAG_compile_unit:
95554aad 14484 case DW_TAG_partial_unit:
71c25dea
TT
14485 /* Compilation units have a DW_AT_name that is a filename, not
14486 a source language identifier. */
14487 case DW_TAG_enumeration_type:
14488 case DW_TAG_enumerator:
14489 /* These tags always have simple identifiers already; no need
14490 to canonicalize them. */
14491 return DW_STRING (attr);
907af001 14492
418835cc
KS
14493 case DW_TAG_subprogram:
14494 /* Java constructors will all be named "<init>", so return
14495 the class name when we see this special case. */
14496 if (cu->language == language_java
14497 && DW_STRING (attr) != NULL
14498 && strcmp (DW_STRING (attr), "<init>") == 0)
14499 {
14500 struct dwarf2_cu *spec_cu = cu;
14501 struct die_info *spec_die;
14502
14503 /* GCJ will output '<init>' for Java constructor names.
14504 For this special case, return the name of the parent class. */
14505
14506 /* GCJ may output suprogram DIEs with AT_specification set.
14507 If so, use the name of the specified DIE. */
14508 spec_die = die_specification (die, &spec_cu);
14509 if (spec_die != NULL)
14510 return dwarf2_name (spec_die, spec_cu);
14511
14512 do
14513 {
14514 die = die->parent;
14515 if (die->tag == DW_TAG_class_type)
14516 return dwarf2_name (die, cu);
14517 }
95554aad
TT
14518 while (die->tag != DW_TAG_compile_unit
14519 && die->tag != DW_TAG_partial_unit);
418835cc 14520 }
907af001
UW
14521 break;
14522
14523 case DW_TAG_class_type:
14524 case DW_TAG_interface_type:
14525 case DW_TAG_structure_type:
14526 case DW_TAG_union_type:
14527 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
14528 structures or unions. These were of the form "._%d" in GCC 4.1,
14529 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
14530 and GCC 4.4. We work around this problem by ignoring these. */
53832f31
TT
14531 if (attr && DW_STRING (attr)
14532 && (strncmp (DW_STRING (attr), "._", 2) == 0
14533 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
907af001 14534 return NULL;
53832f31
TT
14535
14536 /* GCC might emit a nameless typedef that has a linkage name. See
14537 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14538 if (!attr || DW_STRING (attr) == NULL)
14539 {
df5c6c50 14540 char *demangled = NULL;
53832f31
TT
14541
14542 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
14543 if (attr == NULL)
14544 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
14545
14546 if (attr == NULL || DW_STRING (attr) == NULL)
14547 return NULL;
14548
df5c6c50
JK
14549 /* Avoid demangling DW_STRING (attr) the second time on a second
14550 call for the same DIE. */
14551 if (!DW_STRING_IS_CANONICAL (attr))
14552 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
14553
14554 if (demangled)
14555 {
96408a79
SA
14556 char *base;
14557
53832f31 14558 /* FIXME: we already did this for the partial symbol... */
96408a79
SA
14559 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
14560 &cu->objfile->objfile_obstack);
53832f31
TT
14561 DW_STRING_IS_CANONICAL (attr) = 1;
14562 xfree (demangled);
96408a79
SA
14563
14564 /* Strip any leading namespaces/classes, keep only the base name.
14565 DW_AT_name for named DIEs does not contain the prefixes. */
14566 base = strrchr (DW_STRING (attr), ':');
14567 if (base && base > DW_STRING (attr) && base[-1] == ':')
14568 return &base[1];
14569 else
14570 return DW_STRING (attr);
53832f31
TT
14571 }
14572 }
907af001
UW
14573 break;
14574
71c25dea 14575 default:
907af001
UW
14576 break;
14577 }
14578
14579 if (!DW_STRING_IS_CANONICAL (attr))
14580 {
14581 DW_STRING (attr)
14582 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
14583 &cu->objfile->objfile_obstack);
14584 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 14585 }
907af001 14586 return DW_STRING (attr);
9219021c
DC
14587}
14588
14589/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
14590 is none. *EXT_CU is the CU containing DIE on input, and the CU
14591 containing the return value on output. */
9219021c
DC
14592
14593static struct die_info *
f2f0e013 14594dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
14595{
14596 struct attribute *attr;
9219021c 14597
f2f0e013 14598 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
14599 if (attr == NULL)
14600 return NULL;
14601
f2f0e013 14602 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
14603}
14604
c906108c
SS
14605/* Convert a DIE tag into its string name. */
14606
f39c6ffd 14607static const char *
aa1ee363 14608dwarf_tag_name (unsigned tag)
c906108c 14609{
f39c6ffd
TT
14610 const char *name = get_DW_TAG_name (tag);
14611
14612 if (name == NULL)
14613 return "DW_TAG_<unknown>";
14614
14615 return name;
c906108c
SS
14616}
14617
14618/* Convert a DWARF attribute code into its string name. */
14619
f39c6ffd 14620static const char *
aa1ee363 14621dwarf_attr_name (unsigned attr)
c906108c 14622{
f39c6ffd
TT
14623 const char *name;
14624
c764a876 14625#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
14626 if (attr == DW_AT_MIPS_fde)
14627 return "DW_AT_MIPS_fde";
14628#else
14629 if (attr == DW_AT_HP_block_index)
14630 return "DW_AT_HP_block_index";
c764a876 14631#endif
f39c6ffd
TT
14632
14633 name = get_DW_AT_name (attr);
14634
14635 if (name == NULL)
14636 return "DW_AT_<unknown>";
14637
14638 return name;
c906108c
SS
14639}
14640
14641/* Convert a DWARF value form code into its string name. */
14642
f39c6ffd 14643static const char *
aa1ee363 14644dwarf_form_name (unsigned form)
c906108c 14645{
f39c6ffd
TT
14646 const char *name = get_DW_FORM_name (form);
14647
14648 if (name == NULL)
14649 return "DW_FORM_<unknown>";
14650
14651 return name;
c906108c
SS
14652}
14653
14654static char *
fba45db2 14655dwarf_bool_name (unsigned mybool)
c906108c
SS
14656{
14657 if (mybool)
14658 return "TRUE";
14659 else
14660 return "FALSE";
14661}
14662
14663/* Convert a DWARF type code into its string name. */
14664
f39c6ffd 14665static const char *
aa1ee363 14666dwarf_type_encoding_name (unsigned enc)
c906108c 14667{
f39c6ffd 14668 const char *name = get_DW_ATE_name (enc);
c906108c 14669
f39c6ffd
TT
14670 if (name == NULL)
14671 return "DW_ATE_<unknown>";
c906108c 14672
f39c6ffd 14673 return name;
c906108c 14674}
c906108c 14675
f9aca02d 14676static void
d97bc12b 14677dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
14678{
14679 unsigned int i;
14680
d97bc12b
DE
14681 print_spaces (indent, f);
14682 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
b64f50a1 14683 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
d97bc12b
DE
14684
14685 if (die->parent != NULL)
14686 {
14687 print_spaces (indent, f);
14688 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
b64f50a1 14689 die->parent->offset.sect_off);
d97bc12b
DE
14690 }
14691
14692 print_spaces (indent, f);
14693 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 14694 dwarf_bool_name (die->child != NULL));
c906108c 14695
d97bc12b
DE
14696 print_spaces (indent, f);
14697 fprintf_unfiltered (f, " attributes:\n");
14698
c906108c
SS
14699 for (i = 0; i < die->num_attrs; ++i)
14700 {
d97bc12b
DE
14701 print_spaces (indent, f);
14702 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
14703 dwarf_attr_name (die->attrs[i].name),
14704 dwarf_form_name (die->attrs[i].form));
d97bc12b 14705
c906108c
SS
14706 switch (die->attrs[i].form)
14707 {
c906108c 14708 case DW_FORM_addr:
3019eac3 14709 case DW_FORM_GNU_addr_index:
d97bc12b 14710 fprintf_unfiltered (f, "address: ");
5af949e3 14711 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
14712 break;
14713 case DW_FORM_block2:
14714 case DW_FORM_block4:
14715 case DW_FORM_block:
14716 case DW_FORM_block1:
3e43a32a
MS
14717 fprintf_unfiltered (f, "block: size %d",
14718 DW_BLOCK (&die->attrs[i])->size);
c906108c 14719 break;
2dc7f7b3
TT
14720 case DW_FORM_exprloc:
14721 fprintf_unfiltered (f, "expression: size %u",
14722 DW_BLOCK (&die->attrs[i])->size);
14723 break;
4568ecf9
DE
14724 case DW_FORM_ref_addr:
14725 fprintf_unfiltered (f, "ref address: ");
14726 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14727 break;
10b3939b
DJ
14728 case DW_FORM_ref1:
14729 case DW_FORM_ref2:
14730 case DW_FORM_ref4:
4568ecf9
DE
14731 case DW_FORM_ref8:
14732 case DW_FORM_ref_udata:
d97bc12b 14733 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 14734 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 14735 break;
c906108c
SS
14736 case DW_FORM_data1:
14737 case DW_FORM_data2:
14738 case DW_FORM_data4:
ce5d95e1 14739 case DW_FORM_data8:
c906108c
SS
14740 case DW_FORM_udata:
14741 case DW_FORM_sdata:
43bbcdc2
PH
14742 fprintf_unfiltered (f, "constant: %s",
14743 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 14744 break;
2dc7f7b3
TT
14745 case DW_FORM_sec_offset:
14746 fprintf_unfiltered (f, "section offset: %s",
14747 pulongest (DW_UNSND (&die->attrs[i])));
14748 break;
55f1336d 14749 case DW_FORM_ref_sig8:
348e048f
DE
14750 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14751 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
b64f50a1 14752 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
348e048f
DE
14753 else
14754 fprintf_unfiltered (f, "signatured type, offset: unknown");
14755 break;
c906108c 14756 case DW_FORM_string:
4bdf3d34 14757 case DW_FORM_strp:
3019eac3 14758 case DW_FORM_GNU_str_index:
8285870a 14759 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 14760 DW_STRING (&die->attrs[i])
8285870a
JK
14761 ? DW_STRING (&die->attrs[i]) : "",
14762 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
14763 break;
14764 case DW_FORM_flag:
14765 if (DW_UNSND (&die->attrs[i]))
d97bc12b 14766 fprintf_unfiltered (f, "flag: TRUE");
c906108c 14767 else
d97bc12b 14768 fprintf_unfiltered (f, "flag: FALSE");
c906108c 14769 break;
2dc7f7b3
TT
14770 case DW_FORM_flag_present:
14771 fprintf_unfiltered (f, "flag: TRUE");
14772 break;
a8329558 14773 case DW_FORM_indirect:
0963b4bd
MS
14774 /* The reader will have reduced the indirect form to
14775 the "base form" so this form should not occur. */
3e43a32a
MS
14776 fprintf_unfiltered (f,
14777 "unexpected attribute form: DW_FORM_indirect");
a8329558 14778 break;
c906108c 14779 default:
d97bc12b 14780 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 14781 die->attrs[i].form);
d97bc12b 14782 break;
c906108c 14783 }
d97bc12b 14784 fprintf_unfiltered (f, "\n");
c906108c
SS
14785 }
14786}
14787
f9aca02d 14788static void
d97bc12b 14789dump_die_for_error (struct die_info *die)
c906108c 14790{
d97bc12b
DE
14791 dump_die_shallow (gdb_stderr, 0, die);
14792}
14793
14794static void
14795dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14796{
14797 int indent = level * 4;
14798
14799 gdb_assert (die != NULL);
14800
14801 if (level >= max_level)
14802 return;
14803
14804 dump_die_shallow (f, indent, die);
14805
14806 if (die->child != NULL)
c906108c 14807 {
d97bc12b
DE
14808 print_spaces (indent, f);
14809 fprintf_unfiltered (f, " Children:");
14810 if (level + 1 < max_level)
14811 {
14812 fprintf_unfiltered (f, "\n");
14813 dump_die_1 (f, level + 1, max_level, die->child);
14814 }
14815 else
14816 {
3e43a32a
MS
14817 fprintf_unfiltered (f,
14818 " [not printed, max nesting level reached]\n");
d97bc12b
DE
14819 }
14820 }
14821
14822 if (die->sibling != NULL && level > 0)
14823 {
14824 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
14825 }
14826}
14827
d97bc12b
DE
14828/* This is called from the pdie macro in gdbinit.in.
14829 It's not static so gcc will keep a copy callable from gdb. */
14830
14831void
14832dump_die (struct die_info *die, int max_level)
14833{
14834 dump_die_1 (gdb_stdlog, 0, max_level, die);
14835}
14836
f9aca02d 14837static void
51545339 14838store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14839{
51545339 14840 void **slot;
c906108c 14841
b64f50a1
JK
14842 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14843 INSERT);
51545339
DJ
14844
14845 *slot = die;
c906108c
SS
14846}
14847
b64f50a1
JK
14848/* DW_ADDR is always stored already as sect_offset; despite for the forms
14849 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14850
93311388
DE
14851static int
14852is_ref_attr (struct attribute *attr)
c906108c 14853{
c906108c
SS
14854 switch (attr->form)
14855 {
14856 case DW_FORM_ref_addr:
c906108c
SS
14857 case DW_FORM_ref1:
14858 case DW_FORM_ref2:
14859 case DW_FORM_ref4:
613e1657 14860 case DW_FORM_ref8:
c906108c 14861 case DW_FORM_ref_udata:
93311388 14862 return 1;
c906108c 14863 default:
93311388 14864 return 0;
c906108c 14865 }
93311388
DE
14866}
14867
b64f50a1
JK
14868/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14869 required kind. */
14870
14871static sect_offset
93311388
DE
14872dwarf2_get_ref_die_offset (struct attribute *attr)
14873{
4568ecf9 14874 sect_offset retval = { DW_UNSND (attr) };
b64f50a1 14875
93311388 14876 if (is_ref_attr (attr))
b64f50a1 14877 return retval;
93311388 14878
b64f50a1 14879 retval.sect_off = 0;
93311388
DE
14880 complaint (&symfile_complaints,
14881 _("unsupported die ref attribute form: '%s'"),
14882 dwarf_form_name (attr->form));
b64f50a1 14883 return retval;
c906108c
SS
14884}
14885
43bbcdc2
PH
14886/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14887 * the value held by the attribute is not constant. */
a02abb62 14888
43bbcdc2 14889static LONGEST
a02abb62
JB
14890dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14891{
14892 if (attr->form == DW_FORM_sdata)
14893 return DW_SND (attr);
14894 else if (attr->form == DW_FORM_udata
14895 || attr->form == DW_FORM_data1
14896 || attr->form == DW_FORM_data2
14897 || attr->form == DW_FORM_data4
14898 || attr->form == DW_FORM_data8)
14899 return DW_UNSND (attr);
14900 else
14901 {
3e43a32a
MS
14902 complaint (&symfile_complaints,
14903 _("Attribute value is not a constant (%s)"),
a02abb62
JB
14904 dwarf_form_name (attr->form));
14905 return default_value;
14906 }
14907}
14908
03dd20cc 14909/* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
348e048f
DE
14910 unit and add it to our queue.
14911 The result is non-zero if PER_CU was queued, otherwise the result is zero
14912 meaning either PER_CU is already queued or it is already loaded. */
03dd20cc 14913
348e048f 14914static int
03dd20cc 14915maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
95554aad
TT
14916 struct dwarf2_per_cu_data *per_cu,
14917 enum language pretend_language)
03dd20cc 14918{
98bfdba5
PA
14919 /* We may arrive here during partial symbol reading, if we need full
14920 DIEs to process an unusual case (e.g. template arguments). Do
14921 not queue PER_CU, just tell our caller to load its DIEs. */
14922 if (dwarf2_per_objfile->reading_partial_symbols)
14923 {
14924 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14925 return 1;
14926 return 0;
14927 }
14928
03dd20cc
DJ
14929 /* Mark the dependence relation so that we don't flush PER_CU
14930 too early. */
14931 dwarf2_add_dependence (this_cu, per_cu);
14932
14933 /* If it's already on the queue, we have nothing to do. */
14934 if (per_cu->queued)
348e048f 14935 return 0;
03dd20cc
DJ
14936
14937 /* If the compilation unit is already loaded, just mark it as
14938 used. */
14939 if (per_cu->cu != NULL)
14940 {
14941 per_cu->cu->last_used = 0;
348e048f 14942 return 0;
03dd20cc
DJ
14943 }
14944
14945 /* Add it to the queue. */
95554aad 14946 queue_comp_unit (per_cu, pretend_language);
348e048f
DE
14947
14948 return 1;
14949}
14950
14951/* Follow reference or signature attribute ATTR of SRC_DIE.
14952 On entry *REF_CU is the CU of SRC_DIE.
14953 On exit *REF_CU is the CU of the result. */
14954
14955static struct die_info *
14956follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14957 struct dwarf2_cu **ref_cu)
14958{
14959 struct die_info *die;
14960
14961 if (is_ref_attr (attr))
14962 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 14963 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
14964 die = follow_die_sig (src_die, attr, ref_cu);
14965 else
14966 {
14967 dump_die_for_error (src_die);
14968 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14969 (*ref_cu)->objfile->name);
14970 }
14971
14972 return die;
03dd20cc
DJ
14973}
14974
5c631832 14975/* Follow reference OFFSET.
673bfd45
DE
14976 On entry *REF_CU is the CU of the source die referencing OFFSET.
14977 On exit *REF_CU is the CU of the result.
14978 Returns NULL if OFFSET is invalid. */
f504f079 14979
f9aca02d 14980static struct die_info *
b64f50a1 14981follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
c906108c 14982{
10b3939b 14983 struct die_info temp_die;
f2f0e013 14984 struct dwarf2_cu *target_cu, *cu = *ref_cu;
10b3939b 14985
348e048f
DE
14986 gdb_assert (cu->per_cu != NULL);
14987
98bfdba5
PA
14988 target_cu = cu;
14989
3019eac3 14990 if (cu->per_cu->is_debug_types)
348e048f
DE
14991 {
14992 /* .debug_types CUs cannot reference anything outside their CU.
14993 If they need to, they have to reference a signatured type via
55f1336d 14994 DW_FORM_ref_sig8. */
348e048f 14995 if (! offset_in_cu_p (&cu->header, offset))
5c631832 14996 return NULL;
348e048f
DE
14997 }
14998 else if (! offset_in_cu_p (&cu->header, offset))
10b3939b
DJ
14999 {
15000 struct dwarf2_per_cu_data *per_cu;
9a619af0 15001
45452591 15002 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
03dd20cc
DJ
15003
15004 /* If necessary, add it to the queue and load its DIEs. */
95554aad
TT
15005 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
15006 load_full_comp_unit (per_cu, cu->language);
03dd20cc 15007
10b3939b
DJ
15008 target_cu = per_cu->cu;
15009 }
98bfdba5
PA
15010 else if (cu->dies == NULL)
15011 {
15012 /* We're loading full DIEs during partial symbol reading. */
15013 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
95554aad 15014 load_full_comp_unit (cu->per_cu, language_minimal);
98bfdba5 15015 }
c906108c 15016
f2f0e013 15017 *ref_cu = target_cu;
51545339 15018 temp_die.offset = offset;
b64f50a1 15019 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
5c631832 15020}
10b3939b 15021
5c631832
JK
15022/* Follow reference attribute ATTR of SRC_DIE.
15023 On entry *REF_CU is the CU of SRC_DIE.
15024 On exit *REF_CU is the CU of the result. */
15025
15026static struct die_info *
15027follow_die_ref (struct die_info *src_die, struct attribute *attr,
15028 struct dwarf2_cu **ref_cu)
15029{
b64f50a1 15030 sect_offset offset = dwarf2_get_ref_die_offset (attr);
5c631832
JK
15031 struct dwarf2_cu *cu = *ref_cu;
15032 struct die_info *die;
15033
15034 die = follow_die_offset (offset, ref_cu);
15035 if (!die)
15036 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
15037 "at 0x%x [in module %s]"),
b64f50a1 15038 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
348e048f 15039
5c631832
JK
15040 return die;
15041}
15042
d83e736b
JK
15043/* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
15044 Returned value is intended for DW_OP_call*. Returned
15045 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
5c631832
JK
15046
15047struct dwarf2_locexpr_baton
b64f50a1 15048dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
8cf6f0b1
TT
15049 struct dwarf2_per_cu_data *per_cu,
15050 CORE_ADDR (*get_frame_pc) (void *baton),
15051 void *baton)
5c631832 15052{
b64f50a1 15053 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
918dd910 15054 struct dwarf2_cu *cu;
5c631832
JK
15055 struct die_info *die;
15056 struct attribute *attr;
15057 struct dwarf2_locexpr_baton retval;
15058
8cf6f0b1
TT
15059 dw2_setup (per_cu->objfile);
15060
918dd910
JK
15061 if (per_cu->cu == NULL)
15062 load_cu (per_cu);
15063 cu = per_cu->cu;
15064
5c631832
JK
15065 die = follow_die_offset (offset, &cu);
15066 if (!die)
15067 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
b64f50a1 15068 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15069
15070 attr = dwarf2_attr (die, DW_AT_location, cu);
15071 if (!attr)
15072 {
e103e986
JK
15073 /* DWARF: "If there is no such attribute, then there is no effect.".
15074 DATA is ignored if SIZE is 0. */
5c631832 15075
e103e986 15076 retval.data = NULL;
5c631832
JK
15077 retval.size = 0;
15078 }
8cf6f0b1
TT
15079 else if (attr_form_is_section_offset (attr))
15080 {
15081 struct dwarf2_loclist_baton loclist_baton;
15082 CORE_ADDR pc = (*get_frame_pc) (baton);
15083 size_t size;
15084
15085 fill_in_loclist_baton (cu, &loclist_baton, attr);
15086
15087 retval.data = dwarf2_find_location_expression (&loclist_baton,
15088 &size, pc);
15089 retval.size = size;
15090 }
5c631832
JK
15091 else
15092 {
15093 if (!attr_form_is_block (attr))
15094 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
15095 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
b64f50a1 15096 offset.sect_off, per_cu->objfile->name);
5c631832
JK
15097
15098 retval.data = DW_BLOCK (attr)->data;
15099 retval.size = DW_BLOCK (attr)->size;
15100 }
15101 retval.per_cu = cu->per_cu;
918dd910 15102
918dd910
JK
15103 age_cached_comp_units ();
15104
5c631832 15105 return retval;
348e048f
DE
15106}
15107
8a9b8146
TT
15108/* Return the type of the DIE at DIE_OFFSET in the CU named by
15109 PER_CU. */
15110
15111struct type *
b64f50a1 15112dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
15113 struct dwarf2_per_cu_data *per_cu)
15114{
b64f50a1
JK
15115 sect_offset die_offset_sect;
15116
8a9b8146 15117 dw2_setup (per_cu->objfile);
b64f50a1
JK
15118
15119 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
15120 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
15121}
15122
348e048f
DE
15123/* Follow the signature attribute ATTR in SRC_DIE.
15124 On entry *REF_CU is the CU of SRC_DIE.
15125 On exit *REF_CU is the CU of the result. */
15126
15127static struct die_info *
15128follow_die_sig (struct die_info *src_die, struct attribute *attr,
15129 struct dwarf2_cu **ref_cu)
15130{
15131 struct objfile *objfile = (*ref_cu)->objfile;
15132 struct die_info temp_die;
15133 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
15134 struct dwarf2_cu *sig_cu;
15135 struct die_info *die;
15136
15137 /* sig_type will be NULL if the signatured type is missing from
15138 the debug info. */
15139 if (sig_type == NULL)
15140 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
15141 "at 0x%x [in module %s]"),
b64f50a1 15142 src_die->offset.sect_off, objfile->name);
348e048f
DE
15143
15144 /* If necessary, add it to the queue and load its DIEs. */
15145
95554aad 15146 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 15147 read_signatured_type (sig_type);
348e048f
DE
15148
15149 gdb_assert (sig_type->per_cu.cu != NULL);
15150
15151 sig_cu = sig_type->per_cu.cu;
3019eac3
DE
15152 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
15153 temp_die.offset = sig_type->type_offset_in_section;
b64f50a1
JK
15154 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
15155 temp_die.offset.sect_off);
348e048f
DE
15156 if (die)
15157 {
15158 *ref_cu = sig_cu;
15159 return die;
15160 }
15161
3e43a32a
MS
15162 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
15163 "from DIE at 0x%x [in module %s]"),
b64f50a1 15164 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
348e048f
DE
15165}
15166
15167/* Given an offset of a signatured type, return its signatured_type. */
15168
15169static struct signatured_type *
8b70b953
TT
15170lookup_signatured_type_at_offset (struct objfile *objfile,
15171 struct dwarf2_section_info *section,
b64f50a1 15172 sect_offset offset)
348e048f 15173{
b64f50a1 15174 gdb_byte *info_ptr = section->buffer + offset.sect_off;
348e048f
DE
15175 unsigned int length, initial_length_size;
15176 unsigned int sig_offset;
52dc124a 15177 struct signatured_type find_entry, *sig_type;
348e048f
DE
15178
15179 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
15180 sig_offset = (initial_length_size
15181 + 2 /*version*/
15182 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
15183 + 1 /*address_size*/);
15184 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
52dc124a 15185 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
348e048f
DE
15186
15187 /* This is only used to lookup previously recorded types.
15188 If we didn't find it, it's our bug. */
52dc124a
DE
15189 gdb_assert (sig_type != NULL);
15190 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
348e048f 15191
52dc124a 15192 return sig_type;
348e048f
DE
15193}
15194
e5fe5e75 15195/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
15196
15197static void
e5fe5e75 15198load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 15199{
e5fe5e75 15200 struct objfile *objfile = per_cu->objfile;
3019eac3 15201 struct dwarf2_section_info *sect = per_cu->info_or_types_section;
b64f50a1 15202 sect_offset offset = per_cu->offset;
52dc124a 15203 struct signatured_type *sig_type;
348e048f 15204
8b70b953 15205 dwarf2_read_section (objfile, sect);
be391dca 15206
348e048f 15207 /* We have the section offset, but we need the signature to do the
e5fe5e75
DE
15208 hash table lookup. */
15209 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
15210 the signature to assert we found the right one.
15211 Ok, but it's a lot of work. We should simplify things so any needed
15212 assert doesn't require all this clumsiness. */
52dc124a 15213 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
348e048f 15214
dee91e82 15215 gdb_assert (&sig_type->per_cu == per_cu);
52dc124a 15216 gdb_assert (sig_type->per_cu.cu == NULL);
348e048f 15217
52dc124a 15218 read_signatured_type (sig_type);
348e048f 15219
52dc124a 15220 gdb_assert (sig_type->per_cu.cu != NULL);
348e048f
DE
15221}
15222
dee91e82
DE
15223/* die_reader_func for read_signatured_type.
15224 This is identical to load_full_comp_unit_reader,
15225 but is kept separate for now. */
348e048f
DE
15226
15227static void
dee91e82
DE
15228read_signatured_type_reader (const struct die_reader_specs *reader,
15229 gdb_byte *info_ptr,
15230 struct die_info *comp_unit_die,
15231 int has_children,
15232 void *data)
348e048f 15233{
dee91e82 15234 struct dwarf2_cu *cu = reader->cu;
348e048f 15235
dee91e82
DE
15236 gdb_assert (cu->die_hash == NULL);
15237 cu->die_hash =
15238 htab_create_alloc_ex (cu->header.length / 12,
15239 die_hash,
15240 die_eq,
15241 NULL,
15242 &cu->comp_unit_obstack,
15243 hashtab_obstack_allocate,
15244 dummy_obstack_deallocate);
348e048f 15245
dee91e82
DE
15246 if (has_children)
15247 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
15248 &info_ptr, comp_unit_die);
15249 cu->dies = comp_unit_die;
15250 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
15251
15252 /* We try not to read any attributes in this function, because not
9cdd5dbd 15253 all CUs needed for references have been loaded yet, and symbol
348e048f 15254 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
15255 or we won't be able to build types correctly.
15256 Similarly, if we do not read the producer, we can not apply
15257 producer-specific interpretation. */
95554aad 15258 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 15259}
348e048f 15260
3019eac3
DE
15261/* Read in a signatured type and build its CU and DIEs.
15262 If the type is a stub for the real type in a DWO file,
15263 read in the real type from the DWO file as well. */
dee91e82
DE
15264
15265static void
15266read_signatured_type (struct signatured_type *sig_type)
15267{
15268 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 15269
3019eac3 15270 gdb_assert (per_cu->is_debug_types);
dee91e82 15271 gdb_assert (per_cu->cu == NULL);
348e048f 15272
fd820528 15273 init_cutu_and_read_dies (per_cu, 0, 1, read_signatured_type_reader, NULL);
c906108c
SS
15274}
15275
c906108c
SS
15276/* Decode simple location descriptions.
15277 Given a pointer to a dwarf block that defines a location, compute
15278 the location and return the value.
15279
4cecd739
DJ
15280 NOTE drow/2003-11-18: This function is called in two situations
15281 now: for the address of static or global variables (partial symbols
15282 only) and for offsets into structures which are expected to be
15283 (more or less) constant. The partial symbol case should go away,
15284 and only the constant case should remain. That will let this
15285 function complain more accurately. A few special modes are allowed
15286 without complaint for global variables (for instance, global
15287 register values and thread-local values).
c906108c
SS
15288
15289 A location description containing no operations indicates that the
4cecd739 15290 object is optimized out. The return value is 0 for that case.
6b992462
DJ
15291 FIXME drow/2003-11-16: No callers check for this case any more; soon all
15292 callers will only want a very basic result and this can become a
21ae7a4d
JK
15293 complaint.
15294
15295 Note that stack[0] is unused except as a default error return. */
c906108c
SS
15296
15297static CORE_ADDR
e7c27a73 15298decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 15299{
e7c27a73 15300 struct objfile *objfile = cu->objfile;
21ae7a4d
JK
15301 int i;
15302 int size = blk->size;
15303 gdb_byte *data = blk->data;
15304 CORE_ADDR stack[64];
15305 int stacki;
15306 unsigned int bytes_read, unsnd;
15307 gdb_byte op;
c906108c 15308
21ae7a4d
JK
15309 i = 0;
15310 stacki = 0;
15311 stack[stacki] = 0;
15312 stack[++stacki] = 0;
15313
15314 while (i < size)
15315 {
15316 op = data[i++];
15317 switch (op)
15318 {
15319 case DW_OP_lit0:
15320 case DW_OP_lit1:
15321 case DW_OP_lit2:
15322 case DW_OP_lit3:
15323 case DW_OP_lit4:
15324 case DW_OP_lit5:
15325 case DW_OP_lit6:
15326 case DW_OP_lit7:
15327 case DW_OP_lit8:
15328 case DW_OP_lit9:
15329 case DW_OP_lit10:
15330 case DW_OP_lit11:
15331 case DW_OP_lit12:
15332 case DW_OP_lit13:
15333 case DW_OP_lit14:
15334 case DW_OP_lit15:
15335 case DW_OP_lit16:
15336 case DW_OP_lit17:
15337 case DW_OP_lit18:
15338 case DW_OP_lit19:
15339 case DW_OP_lit20:
15340 case DW_OP_lit21:
15341 case DW_OP_lit22:
15342 case DW_OP_lit23:
15343 case DW_OP_lit24:
15344 case DW_OP_lit25:
15345 case DW_OP_lit26:
15346 case DW_OP_lit27:
15347 case DW_OP_lit28:
15348 case DW_OP_lit29:
15349 case DW_OP_lit30:
15350 case DW_OP_lit31:
15351 stack[++stacki] = op - DW_OP_lit0;
15352 break;
f1bea926 15353
21ae7a4d
JK
15354 case DW_OP_reg0:
15355 case DW_OP_reg1:
15356 case DW_OP_reg2:
15357 case DW_OP_reg3:
15358 case DW_OP_reg4:
15359 case DW_OP_reg5:
15360 case DW_OP_reg6:
15361 case DW_OP_reg7:
15362 case DW_OP_reg8:
15363 case DW_OP_reg9:
15364 case DW_OP_reg10:
15365 case DW_OP_reg11:
15366 case DW_OP_reg12:
15367 case DW_OP_reg13:
15368 case DW_OP_reg14:
15369 case DW_OP_reg15:
15370 case DW_OP_reg16:
15371 case DW_OP_reg17:
15372 case DW_OP_reg18:
15373 case DW_OP_reg19:
15374 case DW_OP_reg20:
15375 case DW_OP_reg21:
15376 case DW_OP_reg22:
15377 case DW_OP_reg23:
15378 case DW_OP_reg24:
15379 case DW_OP_reg25:
15380 case DW_OP_reg26:
15381 case DW_OP_reg27:
15382 case DW_OP_reg28:
15383 case DW_OP_reg29:
15384 case DW_OP_reg30:
15385 case DW_OP_reg31:
15386 stack[++stacki] = op - DW_OP_reg0;
15387 if (i < size)
15388 dwarf2_complex_location_expr_complaint ();
15389 break;
c906108c 15390
21ae7a4d
JK
15391 case DW_OP_regx:
15392 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
15393 i += bytes_read;
15394 stack[++stacki] = unsnd;
15395 if (i < size)
15396 dwarf2_complex_location_expr_complaint ();
15397 break;
c906108c 15398
21ae7a4d
JK
15399 case DW_OP_addr:
15400 stack[++stacki] = read_address (objfile->obfd, &data[i],
15401 cu, &bytes_read);
15402 i += bytes_read;
15403 break;
d53d4ac5 15404
21ae7a4d
JK
15405 case DW_OP_const1u:
15406 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
15407 i += 1;
15408 break;
15409
15410 case DW_OP_const1s:
15411 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
15412 i += 1;
15413 break;
15414
15415 case DW_OP_const2u:
15416 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
15417 i += 2;
15418 break;
15419
15420 case DW_OP_const2s:
15421 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
15422 i += 2;
15423 break;
d53d4ac5 15424
21ae7a4d
JK
15425 case DW_OP_const4u:
15426 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
15427 i += 4;
15428 break;
15429
15430 case DW_OP_const4s:
15431 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
15432 i += 4;
15433 break;
15434
585861ea
JK
15435 case DW_OP_const8u:
15436 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
15437 i += 8;
15438 break;
15439
21ae7a4d
JK
15440 case DW_OP_constu:
15441 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
15442 &bytes_read);
15443 i += bytes_read;
15444 break;
15445
15446 case DW_OP_consts:
15447 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
15448 i += bytes_read;
15449 break;
15450
15451 case DW_OP_dup:
15452 stack[stacki + 1] = stack[stacki];
15453 stacki++;
15454 break;
15455
15456 case DW_OP_plus:
15457 stack[stacki - 1] += stack[stacki];
15458 stacki--;
15459 break;
15460
15461 case DW_OP_plus_uconst:
15462 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
15463 &bytes_read);
15464 i += bytes_read;
15465 break;
15466
15467 case DW_OP_minus:
15468 stack[stacki - 1] -= stack[stacki];
15469 stacki--;
15470 break;
15471
15472 case DW_OP_deref:
15473 /* If we're not the last op, then we definitely can't encode
15474 this using GDB's address_class enum. This is valid for partial
15475 global symbols, although the variable's address will be bogus
15476 in the psymtab. */
15477 if (i < size)
15478 dwarf2_complex_location_expr_complaint ();
15479 break;
15480
15481 case DW_OP_GNU_push_tls_address:
15482 /* The top of the stack has the offset from the beginning
15483 of the thread control block at which the variable is located. */
15484 /* Nothing should follow this operator, so the top of stack would
15485 be returned. */
15486 /* This is valid for partial global symbols, but the variable's
585861ea
JK
15487 address will be bogus in the psymtab. Make it always at least
15488 non-zero to not look as a variable garbage collected by linker
15489 which have DW_OP_addr 0. */
21ae7a4d
JK
15490 if (i < size)
15491 dwarf2_complex_location_expr_complaint ();
585861ea 15492 stack[stacki]++;
21ae7a4d
JK
15493 break;
15494
15495 case DW_OP_GNU_uninit:
15496 break;
15497
3019eac3
DE
15498 case DW_OP_GNU_addr_index:
15499 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
15500 &bytes_read);
15501 i += bytes_read;
15502 break;
15503
21ae7a4d
JK
15504 default:
15505 {
f39c6ffd 15506 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
15507
15508 if (name)
15509 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
15510 name);
15511 else
15512 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
15513 op);
15514 }
15515
15516 return (stack[stacki]);
d53d4ac5 15517 }
3c6e0cb3 15518
21ae7a4d
JK
15519 /* Enforce maximum stack depth of SIZE-1 to avoid writing
15520 outside of the allocated space. Also enforce minimum>0. */
15521 if (stacki >= ARRAY_SIZE (stack) - 1)
15522 {
15523 complaint (&symfile_complaints,
15524 _("location description stack overflow"));
15525 return 0;
15526 }
15527
15528 if (stacki <= 0)
15529 {
15530 complaint (&symfile_complaints,
15531 _("location description stack underflow"));
15532 return 0;
15533 }
15534 }
15535 return (stack[stacki]);
c906108c
SS
15536}
15537
15538/* memory allocation interface */
15539
c906108c 15540static struct dwarf_block *
7b5a2f43 15541dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c
SS
15542{
15543 struct dwarf_block *blk;
15544
15545 blk = (struct dwarf_block *)
7b5a2f43 15546 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
c906108c
SS
15547 return (blk);
15548}
15549
15550static struct abbrev_info *
f3dd6933 15551dwarf_alloc_abbrev (struct dwarf2_cu *cu)
c906108c
SS
15552{
15553 struct abbrev_info *abbrev;
15554
f3dd6933
DJ
15555 abbrev = (struct abbrev_info *)
15556 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
c906108c
SS
15557 memset (abbrev, 0, sizeof (struct abbrev_info));
15558 return (abbrev);
15559}
15560
15561static struct die_info *
b60c80d6 15562dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
15563{
15564 struct die_info *die;
b60c80d6
DJ
15565 size_t size = sizeof (struct die_info);
15566
15567 if (num_attrs > 1)
15568 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 15569
b60c80d6 15570 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
15571 memset (die, 0, sizeof (struct die_info));
15572 return (die);
15573}
2e276125
JB
15574
15575\f
15576/* Macro support. */
15577
2e276125
JB
15578/* Return the full name of file number I in *LH's file name table.
15579 Use COMP_DIR as the name of the current directory of the
15580 compilation. The result is allocated using xmalloc; the caller is
15581 responsible for freeing it. */
15582static char *
15583file_full_name (int file, struct line_header *lh, const char *comp_dir)
15584{
6a83a1e6
EZ
15585 /* Is the file number a valid index into the line header's file name
15586 table? Remember that file numbers start with one, not zero. */
15587 if (1 <= file && file <= lh->num_file_names)
15588 {
15589 struct file_entry *fe = &lh->file_names[file - 1];
6e70227d 15590
6a83a1e6
EZ
15591 if (IS_ABSOLUTE_PATH (fe->name))
15592 return xstrdup (fe->name);
15593 else
15594 {
15595 const char *dir;
15596 int dir_len;
15597 char *full_name;
15598
15599 if (fe->dir_index)
15600 dir = lh->include_dirs[fe->dir_index - 1];
15601 else
15602 dir = comp_dir;
15603
15604 if (dir)
15605 {
15606 dir_len = strlen (dir);
15607 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15608 strcpy (full_name, dir);
15609 full_name[dir_len] = '/';
15610 strcpy (full_name + dir_len + 1, fe->name);
15611 return full_name;
15612 }
15613 else
15614 return xstrdup (fe->name);
15615 }
15616 }
2e276125
JB
15617 else
15618 {
6a83a1e6
EZ
15619 /* The compiler produced a bogus file number. We can at least
15620 record the macro definitions made in the file, even if we
15621 won't be able to find the file by name. */
15622 char fake_name[80];
9a619af0 15623
6a83a1e6 15624 sprintf (fake_name, "<bad macro file number %d>", file);
2e276125 15625
6e70227d 15626 complaint (&symfile_complaints,
6a83a1e6
EZ
15627 _("bad file number in macro information (%d)"),
15628 file);
2e276125 15629
6a83a1e6 15630 return xstrdup (fake_name);
2e276125
JB
15631 }
15632}
15633
15634
15635static struct macro_source_file *
15636macro_start_file (int file, int line,
15637 struct macro_source_file *current_file,
15638 const char *comp_dir,
15639 struct line_header *lh, struct objfile *objfile)
15640{
15641 /* The full name of this source file. */
15642 char *full_name = file_full_name (file, lh, comp_dir);
15643
15644 /* We don't create a macro table for this compilation unit
15645 at all until we actually get a filename. */
15646 if (! pending_macros)
4a146b47 15647 pending_macros = new_macro_table (&objfile->objfile_obstack,
af5f3db6 15648 objfile->macro_cache);
2e276125
JB
15649
15650 if (! current_file)
abc9d0dc
TT
15651 {
15652 /* If we have no current file, then this must be the start_file
15653 directive for the compilation unit's main source file. */
15654 current_file = macro_set_main (pending_macros, full_name);
15655 macro_define_special (pending_macros);
15656 }
2e276125
JB
15657 else
15658 current_file = macro_include (current_file, line, full_name);
15659
15660 xfree (full_name);
6e70227d 15661
2e276125
JB
15662 return current_file;
15663}
15664
15665
15666/* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15667 followed by a null byte. */
15668static char *
15669copy_string (const char *buf, int len)
15670{
15671 char *s = xmalloc (len + 1);
9a619af0 15672
2e276125
JB
15673 memcpy (s, buf, len);
15674 s[len] = '\0';
2e276125
JB
15675 return s;
15676}
15677
15678
15679static const char *
15680consume_improper_spaces (const char *p, const char *body)
15681{
15682 if (*p == ' ')
15683 {
4d3c2250 15684 complaint (&symfile_complaints,
3e43a32a
MS
15685 _("macro definition contains spaces "
15686 "in formal argument list:\n`%s'"),
4d3c2250 15687 body);
2e276125
JB
15688
15689 while (*p == ' ')
15690 p++;
15691 }
15692
15693 return p;
15694}
15695
15696
15697static void
15698parse_macro_definition (struct macro_source_file *file, int line,
15699 const char *body)
15700{
15701 const char *p;
15702
15703 /* The body string takes one of two forms. For object-like macro
15704 definitions, it should be:
15705
15706 <macro name> " " <definition>
15707
15708 For function-like macro definitions, it should be:
15709
15710 <macro name> "() " <definition>
15711 or
15712 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15713
15714 Spaces may appear only where explicitly indicated, and in the
15715 <definition>.
15716
15717 The Dwarf 2 spec says that an object-like macro's name is always
15718 followed by a space, but versions of GCC around March 2002 omit
6e70227d 15719 the space when the macro's definition is the empty string.
2e276125
JB
15720
15721 The Dwarf 2 spec says that there should be no spaces between the
15722 formal arguments in a function-like macro's formal argument list,
15723 but versions of GCC around March 2002 include spaces after the
15724 commas. */
15725
15726
15727 /* Find the extent of the macro name. The macro name is terminated
15728 by either a space or null character (for an object-like macro) or
15729 an opening paren (for a function-like macro). */
15730 for (p = body; *p; p++)
15731 if (*p == ' ' || *p == '(')
15732 break;
15733
15734 if (*p == ' ' || *p == '\0')
15735 {
15736 /* It's an object-like macro. */
15737 int name_len = p - body;
15738 char *name = copy_string (body, name_len);
15739 const char *replacement;
15740
15741 if (*p == ' ')
15742 replacement = body + name_len + 1;
15743 else
15744 {
4d3c2250 15745 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15746 replacement = body + name_len;
15747 }
6e70227d 15748
2e276125
JB
15749 macro_define_object (file, line, name, replacement);
15750
15751 xfree (name);
15752 }
15753 else if (*p == '(')
15754 {
15755 /* It's a function-like macro. */
15756 char *name = copy_string (body, p - body);
15757 int argc = 0;
15758 int argv_size = 1;
15759 char **argv = xmalloc (argv_size * sizeof (*argv));
15760
15761 p++;
15762
15763 p = consume_improper_spaces (p, body);
15764
15765 /* Parse the formal argument list. */
15766 while (*p && *p != ')')
15767 {
15768 /* Find the extent of the current argument name. */
15769 const char *arg_start = p;
15770
15771 while (*p && *p != ',' && *p != ')' && *p != ' ')
15772 p++;
15773
15774 if (! *p || p == arg_start)
4d3c2250 15775 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15776 else
15777 {
15778 /* Make sure argv has room for the new argument. */
15779 if (argc >= argv_size)
15780 {
15781 argv_size *= 2;
15782 argv = xrealloc (argv, argv_size * sizeof (*argv));
15783 }
15784
15785 argv[argc++] = copy_string (arg_start, p - arg_start);
15786 }
15787
15788 p = consume_improper_spaces (p, body);
15789
15790 /* Consume the comma, if present. */
15791 if (*p == ',')
15792 {
15793 p++;
15794
15795 p = consume_improper_spaces (p, body);
15796 }
15797 }
15798
15799 if (*p == ')')
15800 {
15801 p++;
15802
15803 if (*p == ' ')
15804 /* Perfectly formed definition, no complaints. */
15805 macro_define_function (file, line, name,
6e70227d 15806 argc, (const char **) argv,
2e276125
JB
15807 p + 1);
15808 else if (*p == '\0')
15809 {
15810 /* Complain, but do define it. */
4d3c2250 15811 dwarf2_macro_malformed_definition_complaint (body);
2e276125 15812 macro_define_function (file, line, name,
6e70227d 15813 argc, (const char **) argv,
2e276125
JB
15814 p);
15815 }
15816 else
15817 /* Just complain. */
4d3c2250 15818 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15819 }
15820 else
15821 /* Just complain. */
4d3c2250 15822 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15823
15824 xfree (name);
15825 {
15826 int i;
15827
15828 for (i = 0; i < argc; i++)
15829 xfree (argv[i]);
15830 }
15831 xfree (argv);
15832 }
15833 else
4d3c2250 15834 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
15835}
15836
cf2c3c16
TT
15837/* Skip some bytes from BYTES according to the form given in FORM.
15838 Returns the new pointer. */
2e276125 15839
cf2c3c16 15840static gdb_byte *
f664829e 15841skip_form_bytes (bfd *abfd, gdb_byte *bytes, gdb_byte *buffer_end,
cf2c3c16
TT
15842 enum dwarf_form form,
15843 unsigned int offset_size,
15844 struct dwarf2_section_info *section)
2e276125 15845{
cf2c3c16 15846 unsigned int bytes_read;
2e276125 15847
cf2c3c16 15848 switch (form)
2e276125 15849 {
cf2c3c16
TT
15850 case DW_FORM_data1:
15851 case DW_FORM_flag:
15852 ++bytes;
15853 break;
15854
15855 case DW_FORM_data2:
15856 bytes += 2;
15857 break;
15858
15859 case DW_FORM_data4:
15860 bytes += 4;
15861 break;
15862
15863 case DW_FORM_data8:
15864 bytes += 8;
15865 break;
15866
15867 case DW_FORM_string:
15868 read_direct_string (abfd, bytes, &bytes_read);
15869 bytes += bytes_read;
15870 break;
15871
15872 case DW_FORM_sec_offset:
15873 case DW_FORM_strp:
15874 bytes += offset_size;
15875 break;
15876
15877 case DW_FORM_block:
15878 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15879 bytes += bytes_read;
15880 break;
15881
15882 case DW_FORM_block1:
15883 bytes += 1 + read_1_byte (abfd, bytes);
15884 break;
15885 case DW_FORM_block2:
15886 bytes += 2 + read_2_bytes (abfd, bytes);
15887 break;
15888 case DW_FORM_block4:
15889 bytes += 4 + read_4_bytes (abfd, bytes);
15890 break;
15891
15892 case DW_FORM_sdata:
15893 case DW_FORM_udata:
3019eac3
DE
15894 case DW_FORM_GNU_addr_index:
15895 case DW_FORM_GNU_str_index:
f664829e
DE
15896 bytes = (gdb_byte *) gdb_skip_leb128 (bytes, buffer_end);
15897 if (bytes == NULL)
15898 {
15899 dwarf2_section_buffer_overflow_complaint (section);
15900 return NULL;
15901 }
cf2c3c16
TT
15902 break;
15903
15904 default:
15905 {
15906 complain:
15907 complaint (&symfile_complaints,
15908 _("invalid form 0x%x in `%s'"),
15909 form,
15910 section->asection->name);
15911 return NULL;
15912 }
2e276125
JB
15913 }
15914
cf2c3c16
TT
15915 return bytes;
15916}
757a13d0 15917
cf2c3c16
TT
15918/* A helper for dwarf_decode_macros that handles skipping an unknown
15919 opcode. Returns an updated pointer to the macro data buffer; or,
15920 on error, issues a complaint and returns NULL. */
757a13d0 15921
cf2c3c16
TT
15922static gdb_byte *
15923skip_unknown_opcode (unsigned int opcode,
15924 gdb_byte **opcode_definitions,
f664829e 15925 gdb_byte *mac_ptr, gdb_byte *mac_end,
cf2c3c16
TT
15926 bfd *abfd,
15927 unsigned int offset_size,
15928 struct dwarf2_section_info *section)
15929{
15930 unsigned int bytes_read, i;
15931 unsigned long arg;
15932 gdb_byte *defn;
2e276125 15933
cf2c3c16 15934 if (opcode_definitions[opcode] == NULL)
2e276125 15935 {
cf2c3c16
TT
15936 complaint (&symfile_complaints,
15937 _("unrecognized DW_MACFINO opcode 0x%x"),
15938 opcode);
15939 return NULL;
15940 }
2e276125 15941
cf2c3c16
TT
15942 defn = opcode_definitions[opcode];
15943 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15944 defn += bytes_read;
2e276125 15945
cf2c3c16
TT
15946 for (i = 0; i < arg; ++i)
15947 {
f664829e
DE
15948 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
15949 section);
cf2c3c16
TT
15950 if (mac_ptr == NULL)
15951 {
15952 /* skip_form_bytes already issued the complaint. */
15953 return NULL;
15954 }
15955 }
757a13d0 15956
cf2c3c16
TT
15957 return mac_ptr;
15958}
757a13d0 15959
cf2c3c16
TT
15960/* A helper function which parses the header of a macro section.
15961 If the macro section is the extended (for now called "GNU") type,
15962 then this updates *OFFSET_SIZE. Returns a pointer to just after
15963 the header, or issues a complaint and returns NULL on error. */
757a13d0 15964
cf2c3c16
TT
15965static gdb_byte *
15966dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15967 bfd *abfd,
15968 gdb_byte *mac_ptr,
15969 unsigned int *offset_size,
15970 int section_is_gnu)
15971{
15972 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 15973
cf2c3c16
TT
15974 if (section_is_gnu)
15975 {
15976 unsigned int version, flags;
757a13d0 15977
cf2c3c16
TT
15978 version = read_2_bytes (abfd, mac_ptr);
15979 if (version != 4)
15980 {
15981 complaint (&symfile_complaints,
15982 _("unrecognized version `%d' in .debug_macro section"),
15983 version);
15984 return NULL;
15985 }
15986 mac_ptr += 2;
757a13d0 15987
cf2c3c16
TT
15988 flags = read_1_byte (abfd, mac_ptr);
15989 ++mac_ptr;
15990 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 15991
cf2c3c16
TT
15992 if ((flags & 2) != 0)
15993 /* We don't need the line table offset. */
15994 mac_ptr += *offset_size;
757a13d0 15995
cf2c3c16
TT
15996 /* Vendor opcode descriptions. */
15997 if ((flags & 4) != 0)
15998 {
15999 unsigned int i, count;
757a13d0 16000
cf2c3c16
TT
16001 count = read_1_byte (abfd, mac_ptr);
16002 ++mac_ptr;
16003 for (i = 0; i < count; ++i)
16004 {
16005 unsigned int opcode, bytes_read;
16006 unsigned long arg;
16007
16008 opcode = read_1_byte (abfd, mac_ptr);
16009 ++mac_ptr;
16010 opcode_definitions[opcode] = mac_ptr;
16011 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16012 mac_ptr += bytes_read;
16013 mac_ptr += arg;
16014 }
757a13d0 16015 }
cf2c3c16 16016 }
757a13d0 16017
cf2c3c16
TT
16018 return mac_ptr;
16019}
757a13d0 16020
cf2c3c16 16021/* A helper for dwarf_decode_macros that handles the GNU extensions,
8fc3fc34 16022 including DW_MACRO_GNU_transparent_include. */
cf2c3c16
TT
16023
16024static void
16025dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
16026 struct macro_source_file *current_file,
16027 struct line_header *lh, char *comp_dir,
16028 struct dwarf2_section_info *section,
16029 int section_is_gnu,
16030 unsigned int offset_size,
8fc3fc34
TT
16031 struct objfile *objfile,
16032 htab_t include_hash)
cf2c3c16
TT
16033{
16034 enum dwarf_macro_record_type macinfo_type;
16035 int at_commandline;
16036 gdb_byte *opcode_definitions[256];
757a13d0 16037
cf2c3c16
TT
16038 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16039 &offset_size, section_is_gnu);
16040 if (mac_ptr == NULL)
16041 {
16042 /* We already issued a complaint. */
16043 return;
16044 }
757a13d0
JK
16045
16046 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
16047 GDB is still reading the definitions from command line. First
16048 DW_MACINFO_start_file will need to be ignored as it was already executed
16049 to create CURRENT_FILE for the main source holding also the command line
16050 definitions. On first met DW_MACINFO_start_file this flag is reset to
16051 normally execute all the remaining DW_MACINFO_start_file macinfos. */
16052
16053 at_commandline = 1;
16054
16055 do
16056 {
16057 /* Do we at least have room for a macinfo type byte? */
16058 if (mac_ptr >= mac_end)
16059 {
f664829e 16060 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
16061 break;
16062 }
16063
16064 macinfo_type = read_1_byte (abfd, mac_ptr);
16065 mac_ptr++;
16066
cf2c3c16
TT
16067 /* Note that we rely on the fact that the corresponding GNU and
16068 DWARF constants are the same. */
757a13d0
JK
16069 switch (macinfo_type)
16070 {
16071 /* A zero macinfo type indicates the end of the macro
16072 information. */
16073 case 0:
16074 break;
2e276125 16075
cf2c3c16
TT
16076 case DW_MACRO_GNU_define:
16077 case DW_MACRO_GNU_undef:
16078 case DW_MACRO_GNU_define_indirect:
16079 case DW_MACRO_GNU_undef_indirect:
2e276125 16080 {
891d2f0b 16081 unsigned int bytes_read;
2e276125
JB
16082 int line;
16083 char *body;
cf2c3c16 16084 int is_define;
2e276125 16085
cf2c3c16
TT
16086 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16087 mac_ptr += bytes_read;
16088
16089 if (macinfo_type == DW_MACRO_GNU_define
16090 || macinfo_type == DW_MACRO_GNU_undef)
16091 {
16092 body = read_direct_string (abfd, mac_ptr, &bytes_read);
16093 mac_ptr += bytes_read;
16094 }
16095 else
16096 {
16097 LONGEST str_offset;
16098
16099 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
16100 mac_ptr += offset_size;
2e276125 16101
cf2c3c16
TT
16102 body = read_indirect_string_at_offset (abfd, str_offset);
16103 }
16104
16105 is_define = (macinfo_type == DW_MACRO_GNU_define
16106 || macinfo_type == DW_MACRO_GNU_define_indirect);
2e276125 16107 if (! current_file)
757a13d0
JK
16108 {
16109 /* DWARF violation as no main source is present. */
16110 complaint (&symfile_complaints,
16111 _("debug info with no main source gives macro %s "
16112 "on line %d: %s"),
cf2c3c16
TT
16113 is_define ? _("definition") : _("undefinition"),
16114 line, body);
757a13d0
JK
16115 break;
16116 }
3e43a32a
MS
16117 if ((line == 0 && !at_commandline)
16118 || (line != 0 && at_commandline))
4d3c2250 16119 complaint (&symfile_complaints,
757a13d0
JK
16120 _("debug info gives %s macro %s with %s line %d: %s"),
16121 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 16122 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
16123 line == 0 ? _("zero") : _("non-zero"), line, body);
16124
cf2c3c16 16125 if (is_define)
757a13d0 16126 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
16127 else
16128 {
16129 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
16130 || macinfo_type == DW_MACRO_GNU_undef_indirect);
16131 macro_undef (current_file, line, body);
16132 }
2e276125
JB
16133 }
16134 break;
16135
cf2c3c16 16136 case DW_MACRO_GNU_start_file:
2e276125 16137 {
891d2f0b 16138 unsigned int bytes_read;
2e276125
JB
16139 int line, file;
16140
16141 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16142 mac_ptr += bytes_read;
16143 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16144 mac_ptr += bytes_read;
16145
3e43a32a
MS
16146 if ((line == 0 && !at_commandline)
16147 || (line != 0 && at_commandline))
757a13d0
JK
16148 complaint (&symfile_complaints,
16149 _("debug info gives source %d included "
16150 "from %s at %s line %d"),
16151 file, at_commandline ? _("command-line") : _("file"),
16152 line == 0 ? _("zero") : _("non-zero"), line);
16153
16154 if (at_commandline)
16155 {
cf2c3c16
TT
16156 /* This DW_MACRO_GNU_start_file was executed in the
16157 pass one. */
757a13d0
JK
16158 at_commandline = 0;
16159 }
16160 else
16161 current_file = macro_start_file (file, line,
16162 current_file, comp_dir,
cf2c3c16 16163 lh, objfile);
2e276125
JB
16164 }
16165 break;
16166
cf2c3c16 16167 case DW_MACRO_GNU_end_file:
2e276125 16168 if (! current_file)
4d3c2250 16169 complaint (&symfile_complaints,
3e43a32a
MS
16170 _("macro debug info has an unmatched "
16171 "`close_file' directive"));
2e276125
JB
16172 else
16173 {
16174 current_file = current_file->included_by;
16175 if (! current_file)
16176 {
cf2c3c16 16177 enum dwarf_macro_record_type next_type;
2e276125
JB
16178
16179 /* GCC circa March 2002 doesn't produce the zero
16180 type byte marking the end of the compilation
16181 unit. Complain if it's not there, but exit no
16182 matter what. */
16183
16184 /* Do we at least have room for a macinfo type byte? */
16185 if (mac_ptr >= mac_end)
16186 {
f664829e 16187 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
16188 return;
16189 }
16190
16191 /* We don't increment mac_ptr here, so this is just
16192 a look-ahead. */
16193 next_type = read_1_byte (abfd, mac_ptr);
16194 if (next_type != 0)
4d3c2250 16195 complaint (&symfile_complaints,
3e43a32a
MS
16196 _("no terminating 0-type entry for "
16197 "macros in `.debug_macinfo' section"));
2e276125
JB
16198
16199 return;
16200 }
16201 }
16202 break;
16203
cf2c3c16
TT
16204 case DW_MACRO_GNU_transparent_include:
16205 {
16206 LONGEST offset;
8fc3fc34 16207 void **slot;
cf2c3c16
TT
16208
16209 offset = read_offset_1 (abfd, mac_ptr, offset_size);
16210 mac_ptr += offset_size;
16211
8fc3fc34
TT
16212 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16213 if (*slot != NULL)
16214 {
16215 /* This has actually happened; see
16216 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
16217 complaint (&symfile_complaints,
16218 _("recursive DW_MACRO_GNU_transparent_include in "
16219 ".debug_macro section"));
16220 }
16221 else
16222 {
16223 *slot = mac_ptr;
16224
16225 dwarf_decode_macro_bytes (abfd,
16226 section->buffer + offset,
16227 mac_end, current_file,
16228 lh, comp_dir,
16229 section, section_is_gnu,
16230 offset_size, objfile, include_hash);
16231
16232 htab_remove_elt (include_hash, mac_ptr);
16233 }
cf2c3c16
TT
16234 }
16235 break;
16236
2e276125 16237 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
16238 if (!section_is_gnu)
16239 {
16240 unsigned int bytes_read;
16241 int constant;
2e276125 16242
cf2c3c16
TT
16243 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16244 mac_ptr += bytes_read;
16245 read_direct_string (abfd, mac_ptr, &bytes_read);
16246 mac_ptr += bytes_read;
2e276125 16247
cf2c3c16
TT
16248 /* We don't recognize any vendor extensions. */
16249 break;
16250 }
16251 /* FALLTHROUGH */
16252
16253 default:
16254 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 16255 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
16256 section);
16257 if (mac_ptr == NULL)
16258 return;
16259 break;
2e276125 16260 }
757a13d0 16261 } while (macinfo_type != 0);
2e276125 16262}
8e19ed76 16263
cf2c3c16
TT
16264static void
16265dwarf_decode_macros (struct line_header *lh, unsigned int offset,
16266 char *comp_dir, bfd *abfd,
16267 struct dwarf2_cu *cu,
16268 struct dwarf2_section_info *section,
fceca515 16269 int section_is_gnu, const char *section_name)
cf2c3c16 16270{
bb5ed363 16271 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
16272 gdb_byte *mac_ptr, *mac_end;
16273 struct macro_source_file *current_file = 0;
16274 enum dwarf_macro_record_type macinfo_type;
16275 unsigned int offset_size = cu->header.offset_size;
16276 gdb_byte *opcode_definitions[256];
8fc3fc34
TT
16277 struct cleanup *cleanup;
16278 htab_t include_hash;
16279 void **slot;
cf2c3c16 16280
bb5ed363 16281 dwarf2_read_section (objfile, section);
cf2c3c16
TT
16282 if (section->buffer == NULL)
16283 {
fceca515 16284 complaint (&symfile_complaints, _("missing %s section"), section_name);
cf2c3c16
TT
16285 return;
16286 }
16287
16288 /* First pass: Find the name of the base filename.
16289 This filename is needed in order to process all macros whose definition
16290 (or undefinition) comes from the command line. These macros are defined
16291 before the first DW_MACINFO_start_file entry, and yet still need to be
16292 associated to the base file.
16293
16294 To determine the base file name, we scan the macro definitions until we
16295 reach the first DW_MACINFO_start_file entry. We then initialize
16296 CURRENT_FILE accordingly so that any macro definition found before the
16297 first DW_MACINFO_start_file can still be associated to the base file. */
16298
16299 mac_ptr = section->buffer + offset;
16300 mac_end = section->buffer + section->size;
16301
16302 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
16303 &offset_size, section_is_gnu);
16304 if (mac_ptr == NULL)
16305 {
16306 /* We already issued a complaint. */
16307 return;
16308 }
16309
16310 do
16311 {
16312 /* Do we at least have room for a macinfo type byte? */
16313 if (mac_ptr >= mac_end)
16314 {
16315 /* Complaint is printed during the second pass as GDB will probably
16316 stop the first pass earlier upon finding
16317 DW_MACINFO_start_file. */
16318 break;
16319 }
16320
16321 macinfo_type = read_1_byte (abfd, mac_ptr);
16322 mac_ptr++;
16323
16324 /* Note that we rely on the fact that the corresponding GNU and
16325 DWARF constants are the same. */
16326 switch (macinfo_type)
16327 {
16328 /* A zero macinfo type indicates the end of the macro
16329 information. */
16330 case 0:
16331 break;
16332
16333 case DW_MACRO_GNU_define:
16334 case DW_MACRO_GNU_undef:
16335 /* Only skip the data by MAC_PTR. */
16336 {
16337 unsigned int bytes_read;
16338
16339 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16340 mac_ptr += bytes_read;
16341 read_direct_string (abfd, mac_ptr, &bytes_read);
16342 mac_ptr += bytes_read;
16343 }
16344 break;
16345
16346 case DW_MACRO_GNU_start_file:
16347 {
16348 unsigned int bytes_read;
16349 int line, file;
16350
16351 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16352 mac_ptr += bytes_read;
16353 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16354 mac_ptr += bytes_read;
16355
16356 current_file = macro_start_file (file, line, current_file,
bb5ed363 16357 comp_dir, lh, objfile);
cf2c3c16
TT
16358 }
16359 break;
16360
16361 case DW_MACRO_GNU_end_file:
16362 /* No data to skip by MAC_PTR. */
16363 break;
16364
16365 case DW_MACRO_GNU_define_indirect:
16366 case DW_MACRO_GNU_undef_indirect:
16367 {
16368 unsigned int bytes_read;
16369
16370 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16371 mac_ptr += bytes_read;
16372 mac_ptr += offset_size;
16373 }
16374 break;
16375
16376 case DW_MACRO_GNU_transparent_include:
16377 /* Note that, according to the spec, a transparent include
16378 chain cannot call DW_MACRO_GNU_start_file. So, we can just
16379 skip this opcode. */
16380 mac_ptr += offset_size;
16381 break;
16382
16383 case DW_MACINFO_vendor_ext:
16384 /* Only skip the data by MAC_PTR. */
16385 if (!section_is_gnu)
16386 {
16387 unsigned int bytes_read;
16388
16389 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
16390 mac_ptr += bytes_read;
16391 read_direct_string (abfd, mac_ptr, &bytes_read);
16392 mac_ptr += bytes_read;
16393 }
16394 /* FALLTHROUGH */
16395
16396 default:
16397 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 16398 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
16399 section);
16400 if (mac_ptr == NULL)
16401 return;
16402 break;
16403 }
16404 } while (macinfo_type != 0 && current_file == NULL);
16405
16406 /* Second pass: Process all entries.
16407
16408 Use the AT_COMMAND_LINE flag to determine whether we are still processing
16409 command-line macro definitions/undefinitions. This flag is unset when we
16410 reach the first DW_MACINFO_start_file entry. */
16411
8fc3fc34
TT
16412 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
16413 NULL, xcalloc, xfree);
16414 cleanup = make_cleanup_htab_delete (include_hash);
16415 mac_ptr = section->buffer + offset;
16416 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
16417 *slot = mac_ptr;
16418 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
cf2c3c16 16419 current_file, lh, comp_dir, section, section_is_gnu,
8fc3fc34
TT
16420 offset_size, objfile, include_hash);
16421 do_cleanups (cleanup);
cf2c3c16
TT
16422}
16423
8e19ed76 16424/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 16425 if so return true else false. */
380bca97 16426
8e19ed76
PS
16427static int
16428attr_form_is_block (struct attribute *attr)
16429{
16430 return (attr == NULL ? 0 :
16431 attr->form == DW_FORM_block1
16432 || attr->form == DW_FORM_block2
16433 || attr->form == DW_FORM_block4
2dc7f7b3
TT
16434 || attr->form == DW_FORM_block
16435 || attr->form == DW_FORM_exprloc);
8e19ed76 16436}
4c2df51b 16437
c6a0999f
JB
16438/* Return non-zero if ATTR's value is a section offset --- classes
16439 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
16440 You may use DW_UNSND (attr) to retrieve such offsets.
16441
16442 Section 7.5.4, "Attribute Encodings", explains that no attribute
16443 may have a value that belongs to more than one of these classes; it
16444 would be ambiguous if we did, because we use the same forms for all
16445 of them. */
380bca97 16446
3690dd37
JB
16447static int
16448attr_form_is_section_offset (struct attribute *attr)
16449{
16450 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
16451 || attr->form == DW_FORM_data8
16452 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
16453}
16454
3690dd37
JB
16455/* Return non-zero if ATTR's value falls in the 'constant' class, or
16456 zero otherwise. When this function returns true, you can apply
16457 dwarf2_get_attr_constant_value to it.
16458
16459 However, note that for some attributes you must check
16460 attr_form_is_section_offset before using this test. DW_FORM_data4
16461 and DW_FORM_data8 are members of both the constant class, and of
16462 the classes that contain offsets into other debug sections
16463 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
16464 that, if an attribute's can be either a constant or one of the
16465 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
16466 taken as section offsets, not constants. */
380bca97 16467
3690dd37
JB
16468static int
16469attr_form_is_constant (struct attribute *attr)
16470{
16471 switch (attr->form)
16472 {
16473 case DW_FORM_sdata:
16474 case DW_FORM_udata:
16475 case DW_FORM_data1:
16476 case DW_FORM_data2:
16477 case DW_FORM_data4:
16478 case DW_FORM_data8:
16479 return 1;
16480 default:
16481 return 0;
16482 }
16483}
16484
3019eac3
DE
16485/* Return the .debug_loc section to use for CU.
16486 For DWO files use .debug_loc.dwo. */
16487
16488static struct dwarf2_section_info *
16489cu_debug_loc_section (struct dwarf2_cu *cu)
16490{
16491 if (cu->dwo_unit)
16492 return &cu->dwo_unit->dwo_file->sections.loc;
16493 return &dwarf2_per_objfile->loc;
16494}
16495
8cf6f0b1
TT
16496/* A helper function that fills in a dwarf2_loclist_baton. */
16497
16498static void
16499fill_in_loclist_baton (struct dwarf2_cu *cu,
16500 struct dwarf2_loclist_baton *baton,
16501 struct attribute *attr)
16502{
3019eac3
DE
16503 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
16504
16505 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
16506
16507 baton->per_cu = cu->per_cu;
16508 gdb_assert (baton->per_cu);
16509 /* We don't know how long the location list is, but make sure we
16510 don't run off the edge of the section. */
3019eac3
DE
16511 baton->size = section->size - DW_UNSND (attr);
16512 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 16513 baton->base_address = cu->base_address;
f664829e 16514 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
16515}
16516
4c2df51b
DJ
16517static void
16518dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
e7c27a73 16519 struct dwarf2_cu *cu)
4c2df51b 16520{
bb5ed363 16521 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 16522 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 16523
3690dd37 16524 if (attr_form_is_section_offset (attr)
3019eac3 16525 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
16526 the section. If so, fall through to the complaint in the
16527 other branch. */
3019eac3 16528 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 16529 {
0d53c4c4 16530 struct dwarf2_loclist_baton *baton;
4c2df51b 16531
bb5ed363 16532 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16533 sizeof (struct dwarf2_loclist_baton));
4c2df51b 16534
8cf6f0b1 16535 fill_in_loclist_baton (cu, baton, attr);
be391dca 16536
d00adf39 16537 if (cu->base_known == 0)
0d53c4c4 16538 complaint (&symfile_complaints,
3e43a32a
MS
16539 _("Location list used without "
16540 "specifying the CU base address."));
4c2df51b 16541
768a979c 16542 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
0d53c4c4
DJ
16543 SYMBOL_LOCATION_BATON (sym) = baton;
16544 }
16545 else
16546 {
16547 struct dwarf2_locexpr_baton *baton;
16548
bb5ed363 16549 baton = obstack_alloc (&objfile->objfile_obstack,
0d53c4c4 16550 sizeof (struct dwarf2_locexpr_baton));
ae0d2f24
UW
16551 baton->per_cu = cu->per_cu;
16552 gdb_assert (baton->per_cu);
0d53c4c4
DJ
16553
16554 if (attr_form_is_block (attr))
16555 {
16556 /* Note that we're just copying the block's data pointer
16557 here, not the actual data. We're still pointing into the
6502dd73
DJ
16558 info_buffer for SYM's objfile; right now we never release
16559 that buffer, but when we do clean up properly this may
16560 need to change. */
0d53c4c4
DJ
16561 baton->size = DW_BLOCK (attr)->size;
16562 baton->data = DW_BLOCK (attr)->data;
16563 }
16564 else
16565 {
16566 dwarf2_invalid_attrib_class_complaint ("location description",
16567 SYMBOL_NATURAL_NAME (sym));
16568 baton->size = 0;
0d53c4c4 16569 }
6e70227d 16570
768a979c 16571 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
0d53c4c4
DJ
16572 SYMBOL_LOCATION_BATON (sym) = baton;
16573 }
4c2df51b 16574}
6502dd73 16575
9aa1f1e3
TT
16576/* Return the OBJFILE associated with the compilation unit CU. If CU
16577 came from a separate debuginfo file, then the master objfile is
16578 returned. */
ae0d2f24
UW
16579
16580struct objfile *
16581dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
16582{
9291a0cd 16583 struct objfile *objfile = per_cu->objfile;
ae0d2f24
UW
16584
16585 /* Return the master objfile, so that we can report and look up the
16586 correct file containing this variable. */
16587 if (objfile->separate_debug_objfile_backlink)
16588 objfile = objfile->separate_debug_objfile_backlink;
16589
16590 return objfile;
16591}
16592
96408a79
SA
16593/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
16594 (CU_HEADERP is unused in such case) or prepare a temporary copy at
16595 CU_HEADERP first. */
16596
16597static const struct comp_unit_head *
16598per_cu_header_read_in (struct comp_unit_head *cu_headerp,
16599 struct dwarf2_per_cu_data *per_cu)
16600{
16601 struct objfile *objfile;
16602 struct dwarf2_per_objfile *per_objfile;
16603 gdb_byte *info_ptr;
16604
16605 if (per_cu->cu)
16606 return &per_cu->cu->header;
16607
16608 objfile = per_cu->objfile;
16609 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
b64f50a1 16610 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
96408a79
SA
16611
16612 memset (cu_headerp, 0, sizeof (*cu_headerp));
16613 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16614
16615 return cu_headerp;
16616}
16617
ae0d2f24
UW
16618/* Return the address size given in the compilation unit header for CU. */
16619
98714339 16620int
ae0d2f24
UW
16621dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16622{
96408a79
SA
16623 struct comp_unit_head cu_header_local;
16624 const struct comp_unit_head *cu_headerp;
c471e790 16625
96408a79
SA
16626 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16627
16628 return cu_headerp->addr_size;
ae0d2f24
UW
16629}
16630
9eae7c52
TT
16631/* Return the offset size given in the compilation unit header for CU. */
16632
16633int
16634dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16635{
96408a79
SA
16636 struct comp_unit_head cu_header_local;
16637 const struct comp_unit_head *cu_headerp;
9c6c53f7 16638
96408a79
SA
16639 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16640
16641 return cu_headerp->offset_size;
16642}
16643
16644/* See its dwarf2loc.h declaration. */
16645
16646int
16647dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16648{
16649 struct comp_unit_head cu_header_local;
16650 const struct comp_unit_head *cu_headerp;
16651
16652 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16653
16654 if (cu_headerp->version == 2)
16655 return cu_headerp->addr_size;
16656 else
16657 return cu_headerp->offset_size;
181cebd4
JK
16658}
16659
9aa1f1e3
TT
16660/* Return the text offset of the CU. The returned offset comes from
16661 this CU's objfile. If this objfile came from a separate debuginfo
16662 file, then the offset may be different from the corresponding
16663 offset in the parent objfile. */
16664
16665CORE_ADDR
16666dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16667{
bb3fa9d0 16668 struct objfile *objfile = per_cu->objfile;
9aa1f1e3
TT
16669
16670 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16671}
16672
348e048f
DE
16673/* Locate the .debug_info compilation unit from CU's objfile which contains
16674 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
16675
16676static struct dwarf2_per_cu_data *
b64f50a1 16677dwarf2_find_containing_comp_unit (sect_offset offset,
ae038cb0
DJ
16678 struct objfile *objfile)
16679{
16680 struct dwarf2_per_cu_data *this_cu;
16681 int low, high;
16682
ae038cb0
DJ
16683 low = 0;
16684 high = dwarf2_per_objfile->n_comp_units - 1;
16685 while (high > low)
16686 {
16687 int mid = low + (high - low) / 2;
9a619af0 16688
b64f50a1
JK
16689 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16690 >= offset.sect_off)
ae038cb0
DJ
16691 high = mid;
16692 else
16693 low = mid + 1;
16694 }
16695 gdb_assert (low == high);
b64f50a1
JK
16696 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16697 > offset.sect_off)
ae038cb0 16698 {
10b3939b 16699 if (low == 0)
8a3fe4f8
AC
16700 error (_("Dwarf Error: could not find partial DIE containing "
16701 "offset 0x%lx [in module %s]"),
b64f50a1 16702 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
10b3939b 16703
b64f50a1
JK
16704 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16705 <= offset.sect_off);
ae038cb0
DJ
16706 return dwarf2_per_objfile->all_comp_units[low-1];
16707 }
16708 else
16709 {
16710 this_cu = dwarf2_per_objfile->all_comp_units[low];
16711 if (low == dwarf2_per_objfile->n_comp_units - 1
b64f50a1
JK
16712 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16713 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16714 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
ae038cb0
DJ
16715 return this_cu;
16716 }
16717}
16718
23745b47 16719/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 16720
9816fde3 16721static void
23745b47 16722init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
93311388 16723{
9816fde3 16724 memset (cu, 0, sizeof (*cu));
23745b47
DE
16725 per_cu->cu = cu;
16726 cu->per_cu = per_cu;
16727 cu->objfile = per_cu->objfile;
93311388 16728 obstack_init (&cu->comp_unit_obstack);
9816fde3
JK
16729}
16730
16731/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16732
16733static void
95554aad
TT
16734prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
16735 enum language pretend_language)
9816fde3
JK
16736{
16737 struct attribute *attr;
16738
16739 /* Set the language we're debugging. */
16740 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16741 if (attr)
16742 set_cu_language (DW_UNSND (attr), cu);
16743 else
9cded63f 16744 {
95554aad 16745 cu->language = pretend_language;
9cded63f
TT
16746 cu->language_defn = language_def (cu->language);
16747 }
dee91e82
DE
16748
16749 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
16750 if (attr)
16751 cu->producer = DW_STRING (attr);
93311388
DE
16752}
16753
ae038cb0
DJ
16754/* Release one cached compilation unit, CU. We unlink it from the tree
16755 of compilation units, but we don't remove it from the read_in_chain;
93311388
DE
16756 the caller is responsible for that.
16757 NOTE: DATA is a void * because this function is also used as a
16758 cleanup routine. */
ae038cb0
DJ
16759
16760static void
68dc6402 16761free_heap_comp_unit (void *data)
ae038cb0
DJ
16762{
16763 struct dwarf2_cu *cu = data;
16764
23745b47
DE
16765 gdb_assert (cu->per_cu != NULL);
16766 cu->per_cu->cu = NULL;
ae038cb0
DJ
16767 cu->per_cu = NULL;
16768
16769 obstack_free (&cu->comp_unit_obstack, NULL);
16770
16771 xfree (cu);
16772}
16773
72bf9492 16774/* This cleanup function is passed the address of a dwarf2_cu on the stack
ae038cb0 16775 when we're finished with it. We can't free the pointer itself, but be
dee91e82 16776 sure to unlink it from the cache. Also release any associated storage. */
72bf9492
DJ
16777
16778static void
16779free_stack_comp_unit (void *data)
16780{
16781 struct dwarf2_cu *cu = data;
16782
23745b47
DE
16783 gdb_assert (cu->per_cu != NULL);
16784 cu->per_cu->cu = NULL;
16785 cu->per_cu = NULL;
16786
72bf9492
DJ
16787 obstack_free (&cu->comp_unit_obstack, NULL);
16788 cu->partial_dies = NULL;
ae038cb0
DJ
16789}
16790
16791/* Free all cached compilation units. */
16792
16793static void
16794free_cached_comp_units (void *data)
16795{
16796 struct dwarf2_per_cu_data *per_cu, **last_chain;
16797
16798 per_cu = dwarf2_per_objfile->read_in_chain;
16799 last_chain = &dwarf2_per_objfile->read_in_chain;
16800 while (per_cu != NULL)
16801 {
16802 struct dwarf2_per_cu_data *next_cu;
16803
16804 next_cu = per_cu->cu->read_in_chain;
16805
68dc6402 16806 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16807 *last_chain = next_cu;
16808
16809 per_cu = next_cu;
16810 }
16811}
16812
16813/* Increase the age counter on each cached compilation unit, and free
16814 any that are too old. */
16815
16816static void
16817age_cached_comp_units (void)
16818{
16819 struct dwarf2_per_cu_data *per_cu, **last_chain;
16820
16821 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16822 per_cu = dwarf2_per_objfile->read_in_chain;
16823 while (per_cu != NULL)
16824 {
16825 per_cu->cu->last_used ++;
16826 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16827 dwarf2_mark (per_cu->cu);
16828 per_cu = per_cu->cu->read_in_chain;
16829 }
16830
16831 per_cu = dwarf2_per_objfile->read_in_chain;
16832 last_chain = &dwarf2_per_objfile->read_in_chain;
16833 while (per_cu != NULL)
16834 {
16835 struct dwarf2_per_cu_data *next_cu;
16836
16837 next_cu = per_cu->cu->read_in_chain;
16838
16839 if (!per_cu->cu->mark)
16840 {
68dc6402 16841 free_heap_comp_unit (per_cu->cu);
ae038cb0
DJ
16842 *last_chain = next_cu;
16843 }
16844 else
16845 last_chain = &per_cu->cu->read_in_chain;
16846
16847 per_cu = next_cu;
16848 }
16849}
16850
16851/* Remove a single compilation unit from the cache. */
16852
16853static void
dee91e82 16854free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
16855{
16856 struct dwarf2_per_cu_data *per_cu, **last_chain;
16857
16858 per_cu = dwarf2_per_objfile->read_in_chain;
16859 last_chain = &dwarf2_per_objfile->read_in_chain;
16860 while (per_cu != NULL)
16861 {
16862 struct dwarf2_per_cu_data *next_cu;
16863
16864 next_cu = per_cu->cu->read_in_chain;
16865
dee91e82 16866 if (per_cu == target_per_cu)
ae038cb0 16867 {
68dc6402 16868 free_heap_comp_unit (per_cu->cu);
dee91e82 16869 per_cu->cu = NULL;
ae038cb0
DJ
16870 *last_chain = next_cu;
16871 break;
16872 }
16873 else
16874 last_chain = &per_cu->cu->read_in_chain;
16875
16876 per_cu = next_cu;
16877 }
16878}
16879
fe3e1990
DJ
16880/* Release all extra memory associated with OBJFILE. */
16881
16882void
16883dwarf2_free_objfile (struct objfile *objfile)
16884{
16885 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16886
16887 if (dwarf2_per_objfile == NULL)
16888 return;
16889
16890 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16891 free_cached_comp_units (NULL);
16892
7b9f3c50
DE
16893 if (dwarf2_per_objfile->quick_file_names_table)
16894 htab_delete (dwarf2_per_objfile->quick_file_names_table);
9291a0cd 16895
fe3e1990
DJ
16896 /* Everything else should be on the objfile obstack. */
16897}
16898
dee91e82
DE
16899/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
16900 We store these in a hash table separate from the DIEs, and preserve them
16901 when the DIEs are flushed out of cache.
16902
16903 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3
DE
16904 uniquely identify the type. A file may have multiple .debug_types sections,
16905 or the type may come from a DWO file. We have to use something in
16906 dwarf2_per_cu_data (or the pointer to it) because we can enter the lookup
16907 routine, get_die_type_at_offset, from outside this file, and thus won't
16908 necessarily have PER_CU->cu. Fortunately, PER_CU is stable for the life
16909 of the objfile. */
1c379e20 16910
dee91e82 16911struct dwarf2_per_cu_offset_and_type
1c379e20 16912{
dee91e82 16913 const struct dwarf2_per_cu_data *per_cu;
b64f50a1 16914 sect_offset offset;
1c379e20
DJ
16915 struct type *type;
16916};
16917
dee91e82 16918/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
16919
16920static hashval_t
dee91e82 16921per_cu_offset_and_type_hash (const void *item)
1c379e20 16922{
dee91e82 16923 const struct dwarf2_per_cu_offset_and_type *ofs = item;
9a619af0 16924
dee91e82 16925 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
1c379e20
DJ
16926}
16927
dee91e82 16928/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
16929
16930static int
dee91e82 16931per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 16932{
dee91e82
DE
16933 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
16934 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
9a619af0 16935
dee91e82
DE
16936 return (ofs_lhs->per_cu == ofs_rhs->per_cu
16937 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
1c379e20
DJ
16938}
16939
16940/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
16941 table if necessary. For convenience, return TYPE.
16942
16943 The DIEs reading must have careful ordering to:
16944 * Not cause infite loops trying to read in DIEs as a prerequisite for
16945 reading current DIE.
16946 * Not trying to dereference contents of still incompletely read in types
16947 while reading in other DIEs.
16948 * Enable referencing still incompletely read in types just by a pointer to
16949 the type without accessing its fields.
16950
16951 Therefore caller should follow these rules:
16952 * Try to fetch any prerequisite types we may need to build this DIE type
16953 before building the type and calling set_die_type.
e71ec853 16954 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
16955 possible before fetching more types to complete the current type.
16956 * Make the type as complete as possible before fetching more types. */
1c379e20 16957
f792889a 16958static struct type *
1c379e20
DJ
16959set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16960{
dee91e82 16961 struct dwarf2_per_cu_offset_and_type **slot, ofs;
673bfd45 16962 struct objfile *objfile = cu->objfile;
1c379e20 16963
b4ba55a1
JB
16964 /* For Ada types, make sure that the gnat-specific data is always
16965 initialized (if not already set). There are a few types where
16966 we should not be doing so, because the type-specific area is
16967 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16968 where the type-specific area is used to store the floatformat).
16969 But this is not a problem, because the gnat-specific information
16970 is actually not needed for these types. */
16971 if (need_gnat_info (cu)
16972 && TYPE_CODE (type) != TYPE_CODE_FUNC
16973 && TYPE_CODE (type) != TYPE_CODE_FLT
16974 && !HAVE_GNAT_AUX_INFO (type))
16975 INIT_GNAT_SPECIFIC (type);
16976
dee91e82 16977 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 16978 {
dee91e82
DE
16979 dwarf2_per_objfile->die_type_hash =
16980 htab_create_alloc_ex (127,
16981 per_cu_offset_and_type_hash,
16982 per_cu_offset_and_type_eq,
16983 NULL,
16984 &objfile->objfile_obstack,
16985 hashtab_obstack_allocate,
16986 dummy_obstack_deallocate);
f792889a 16987 }
1c379e20 16988
dee91e82 16989 ofs.per_cu = cu->per_cu;
1c379e20
DJ
16990 ofs.offset = die->offset;
16991 ofs.type = type;
dee91e82
DE
16992 slot = (struct dwarf2_per_cu_offset_and_type **)
16993 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57
JK
16994 if (*slot)
16995 complaint (&symfile_complaints,
16996 _("A problem internal to GDB: DIE 0x%x has type already set"),
b64f50a1 16997 die->offset.sect_off);
673bfd45 16998 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
1c379e20 16999 **slot = ofs;
f792889a 17000 return type;
1c379e20
DJ
17001}
17002
380bca97 17003/* Look up the type for the die at OFFSET in the appropriate type_hash
673bfd45 17004 table, or return NULL if the die does not have a saved type. */
1c379e20
DJ
17005
17006static struct type *
b64f50a1 17007get_die_type_at_offset (sect_offset offset,
673bfd45 17008 struct dwarf2_per_cu_data *per_cu)
1c379e20 17009{
dee91e82 17010 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 17011
dee91e82 17012 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 17013 return NULL;
1c379e20 17014
dee91e82 17015 ofs.per_cu = per_cu;
673bfd45 17016 ofs.offset = offset;
dee91e82 17017 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
1c379e20
DJ
17018 if (slot)
17019 return slot->type;
17020 else
17021 return NULL;
17022}
17023
673bfd45
DE
17024/* Look up the type for DIE in the appropriate type_hash table,
17025 or return NULL if DIE does not have a saved type. */
17026
17027static struct type *
17028get_die_type (struct die_info *die, struct dwarf2_cu *cu)
17029{
17030 return get_die_type_at_offset (die->offset, cu->per_cu);
17031}
17032
10b3939b
DJ
17033/* Add a dependence relationship from CU to REF_PER_CU. */
17034
17035static void
17036dwarf2_add_dependence (struct dwarf2_cu *cu,
17037 struct dwarf2_per_cu_data *ref_per_cu)
17038{
17039 void **slot;
17040
17041 if (cu->dependencies == NULL)
17042 cu->dependencies
17043 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
17044 NULL, &cu->comp_unit_obstack,
17045 hashtab_obstack_allocate,
17046 dummy_obstack_deallocate);
17047
17048 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
17049 if (*slot == NULL)
17050 *slot = ref_per_cu;
17051}
1c379e20 17052
f504f079
DE
17053/* Subroutine of dwarf2_mark to pass to htab_traverse.
17054 Set the mark field in every compilation unit in the
ae038cb0
DJ
17055 cache that we must keep because we are keeping CU. */
17056
10b3939b
DJ
17057static int
17058dwarf2_mark_helper (void **slot, void *data)
17059{
17060 struct dwarf2_per_cu_data *per_cu;
17061
17062 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
17063
17064 /* cu->dependencies references may not yet have been ever read if QUIT aborts
17065 reading of the chain. As such dependencies remain valid it is not much
17066 useful to track and undo them during QUIT cleanups. */
17067 if (per_cu->cu == NULL)
17068 return 1;
17069
10b3939b
DJ
17070 if (per_cu->cu->mark)
17071 return 1;
17072 per_cu->cu->mark = 1;
17073
17074 if (per_cu->cu->dependencies != NULL)
17075 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
17076
17077 return 1;
17078}
17079
f504f079
DE
17080/* Set the mark field in CU and in every other compilation unit in the
17081 cache that we must keep because we are keeping CU. */
17082
ae038cb0
DJ
17083static void
17084dwarf2_mark (struct dwarf2_cu *cu)
17085{
17086 if (cu->mark)
17087 return;
17088 cu->mark = 1;
10b3939b
DJ
17089 if (cu->dependencies != NULL)
17090 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
17091}
17092
17093static void
17094dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
17095{
17096 while (per_cu)
17097 {
17098 per_cu->cu->mark = 0;
17099 per_cu = per_cu->cu->read_in_chain;
17100 }
72bf9492
DJ
17101}
17102
72bf9492
DJ
17103/* Trivial hash function for partial_die_info: the hash value of a DIE
17104 is its offset in .debug_info for this objfile. */
17105
17106static hashval_t
17107partial_die_hash (const void *item)
17108{
17109 const struct partial_die_info *part_die = item;
9a619af0 17110
b64f50a1 17111 return part_die->offset.sect_off;
72bf9492
DJ
17112}
17113
17114/* Trivial comparison function for partial_die_info structures: two DIEs
17115 are equal if they have the same offset. */
17116
17117static int
17118partial_die_eq (const void *item_lhs, const void *item_rhs)
17119{
17120 const struct partial_die_info *part_die_lhs = item_lhs;
17121 const struct partial_die_info *part_die_rhs = item_rhs;
9a619af0 17122
b64f50a1 17123 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
72bf9492
DJ
17124}
17125
ae038cb0
DJ
17126static struct cmd_list_element *set_dwarf2_cmdlist;
17127static struct cmd_list_element *show_dwarf2_cmdlist;
17128
17129static void
17130set_dwarf2_cmd (char *args, int from_tty)
17131{
17132 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
17133}
17134
17135static void
17136show_dwarf2_cmd (char *args, int from_tty)
6e70227d 17137{
ae038cb0
DJ
17138 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
17139}
17140
dce234bc
PP
17141/* If section described by INFO was mmapped, munmap it now. */
17142
17143static void
17144munmap_section_buffer (struct dwarf2_section_info *info)
17145{
b315ab21 17146 if (info->map_addr != NULL)
dce234bc
PP
17147 {
17148#ifdef HAVE_MMAP
b315ab21 17149 int res;
9a619af0 17150
b315ab21
TG
17151 res = munmap (info->map_addr, info->map_len);
17152 gdb_assert (res == 0);
dce234bc
PP
17153#else
17154 /* Without HAVE_MMAP, we should never be here to begin with. */
f3574227 17155 gdb_assert_not_reached ("no mmap support");
dce234bc
PP
17156#endif
17157 }
17158}
17159
17160/* munmap debug sections for OBJFILE, if necessary. */
17161
17162static void
c1bd65d0 17163dwarf2_per_objfile_free (struct objfile *objfile, void *d)
dce234bc
PP
17164{
17165 struct dwarf2_per_objfile *data = d;
8b70b953
TT
17166 int ix;
17167 struct dwarf2_section_info *section;
9a619af0 17168
16be1145
DE
17169 /* This is sorted according to the order they're defined in to make it easier
17170 to keep in sync. */
dce234bc
PP
17171 munmap_section_buffer (&data->info);
17172 munmap_section_buffer (&data->abbrev);
17173 munmap_section_buffer (&data->line);
16be1145 17174 munmap_section_buffer (&data->loc);
dce234bc 17175 munmap_section_buffer (&data->macinfo);
cf2c3c16 17176 munmap_section_buffer (&data->macro);
16be1145 17177 munmap_section_buffer (&data->str);
dce234bc 17178 munmap_section_buffer (&data->ranges);
3019eac3 17179 munmap_section_buffer (&data->addr);
dce234bc
PP
17180 munmap_section_buffer (&data->frame);
17181 munmap_section_buffer (&data->eh_frame);
9291a0cd 17182 munmap_section_buffer (&data->gdb_index);
8b70b953
TT
17183
17184 for (ix = 0;
17185 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
17186 ++ix)
17187 munmap_section_buffer (section);
17188
95554aad
TT
17189 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
17190 VEC_free (dwarf2_per_cu_ptr,
17191 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
17192
8b70b953 17193 VEC_free (dwarf2_section_info_def, data->types);
3019eac3
DE
17194
17195 if (data->dwo_files)
17196 free_dwo_files (data->dwo_files, objfile);
9291a0cd
TT
17197}
17198
17199\f
ae2de4f8 17200/* The "save gdb-index" command. */
9291a0cd
TT
17201
17202/* The contents of the hash table we create when building the string
17203 table. */
17204struct strtab_entry
17205{
17206 offset_type offset;
17207 const char *str;
17208};
17209
559a7a62
JK
17210/* Hash function for a strtab_entry.
17211
17212 Function is used only during write_hash_table so no index format backward
17213 compatibility is needed. */
b89be57b 17214
9291a0cd
TT
17215static hashval_t
17216hash_strtab_entry (const void *e)
17217{
17218 const struct strtab_entry *entry = e;
559a7a62 17219 return mapped_index_string_hash (INT_MAX, entry->str);
9291a0cd
TT
17220}
17221
17222/* Equality function for a strtab_entry. */
b89be57b 17223
9291a0cd
TT
17224static int
17225eq_strtab_entry (const void *a, const void *b)
17226{
17227 const struct strtab_entry *ea = a;
17228 const struct strtab_entry *eb = b;
17229 return !strcmp (ea->str, eb->str);
17230}
17231
17232/* Create a strtab_entry hash table. */
b89be57b 17233
9291a0cd
TT
17234static htab_t
17235create_strtab (void)
17236{
17237 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
17238 xfree, xcalloc, xfree);
17239}
17240
17241/* Add a string to the constant pool. Return the string's offset in
17242 host order. */
b89be57b 17243
9291a0cd
TT
17244static offset_type
17245add_string (htab_t table, struct obstack *cpool, const char *str)
17246{
17247 void **slot;
17248 struct strtab_entry entry;
17249 struct strtab_entry *result;
17250
17251 entry.str = str;
17252 slot = htab_find_slot (table, &entry, INSERT);
17253 if (*slot)
17254 result = *slot;
17255 else
17256 {
17257 result = XNEW (struct strtab_entry);
17258 result->offset = obstack_object_size (cpool);
17259 result->str = str;
17260 obstack_grow_str0 (cpool, str);
17261 *slot = result;
17262 }
17263 return result->offset;
17264}
17265
17266/* An entry in the symbol table. */
17267struct symtab_index_entry
17268{
17269 /* The name of the symbol. */
17270 const char *name;
17271 /* The offset of the name in the constant pool. */
17272 offset_type index_offset;
17273 /* A sorted vector of the indices of all the CUs that hold an object
17274 of this name. */
17275 VEC (offset_type) *cu_indices;
17276};
17277
17278/* The symbol table. This is a power-of-2-sized hash table. */
17279struct mapped_symtab
17280{
17281 offset_type n_elements;
17282 offset_type size;
17283 struct symtab_index_entry **data;
17284};
17285
17286/* Hash function for a symtab_index_entry. */
b89be57b 17287
9291a0cd
TT
17288static hashval_t
17289hash_symtab_entry (const void *e)
17290{
17291 const struct symtab_index_entry *entry = e;
17292 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
17293 sizeof (offset_type) * VEC_length (offset_type,
17294 entry->cu_indices),
17295 0);
17296}
17297
17298/* Equality function for a symtab_index_entry. */
b89be57b 17299
9291a0cd
TT
17300static int
17301eq_symtab_entry (const void *a, const void *b)
17302{
17303 const struct symtab_index_entry *ea = a;
17304 const struct symtab_index_entry *eb = b;
17305 int len = VEC_length (offset_type, ea->cu_indices);
17306 if (len != VEC_length (offset_type, eb->cu_indices))
17307 return 0;
17308 return !memcmp (VEC_address (offset_type, ea->cu_indices),
17309 VEC_address (offset_type, eb->cu_indices),
17310 sizeof (offset_type) * len);
17311}
17312
17313/* Destroy a symtab_index_entry. */
b89be57b 17314
9291a0cd
TT
17315static void
17316delete_symtab_entry (void *p)
17317{
17318 struct symtab_index_entry *entry = p;
17319 VEC_free (offset_type, entry->cu_indices);
17320 xfree (entry);
17321}
17322
17323/* Create a hash table holding symtab_index_entry objects. */
b89be57b 17324
9291a0cd 17325static htab_t
3876f04e 17326create_symbol_hash_table (void)
9291a0cd
TT
17327{
17328 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
17329 delete_symtab_entry, xcalloc, xfree);
17330}
17331
17332/* Create a new mapped symtab object. */
b89be57b 17333
9291a0cd
TT
17334static struct mapped_symtab *
17335create_mapped_symtab (void)
17336{
17337 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
17338 symtab->n_elements = 0;
17339 symtab->size = 1024;
17340 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17341 return symtab;
17342}
17343
17344/* Destroy a mapped_symtab. */
b89be57b 17345
9291a0cd
TT
17346static void
17347cleanup_mapped_symtab (void *p)
17348{
17349 struct mapped_symtab *symtab = p;
17350 /* The contents of the array are freed when the other hash table is
17351 destroyed. */
17352 xfree (symtab->data);
17353 xfree (symtab);
17354}
17355
17356/* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
559a7a62
JK
17357 the slot.
17358
17359 Function is used only during write_hash_table so no index format backward
17360 compatibility is needed. */
b89be57b 17361
9291a0cd
TT
17362static struct symtab_index_entry **
17363find_slot (struct mapped_symtab *symtab, const char *name)
17364{
559a7a62 17365 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
9291a0cd
TT
17366
17367 index = hash & (symtab->size - 1);
17368 step = ((hash * 17) & (symtab->size - 1)) | 1;
17369
17370 for (;;)
17371 {
17372 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
17373 return &symtab->data[index];
17374 index = (index + step) & (symtab->size - 1);
17375 }
17376}
17377
17378/* Expand SYMTAB's hash table. */
b89be57b 17379
9291a0cd
TT
17380static void
17381hash_expand (struct mapped_symtab *symtab)
17382{
17383 offset_type old_size = symtab->size;
17384 offset_type i;
17385 struct symtab_index_entry **old_entries = symtab->data;
17386
17387 symtab->size *= 2;
17388 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
17389
17390 for (i = 0; i < old_size; ++i)
17391 {
17392 if (old_entries[i])
17393 {
17394 struct symtab_index_entry **slot = find_slot (symtab,
17395 old_entries[i]->name);
17396 *slot = old_entries[i];
17397 }
17398 }
17399
17400 xfree (old_entries);
17401}
17402
17403/* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
17404 is the index of the CU in which the symbol appears. */
b89be57b 17405
9291a0cd
TT
17406static void
17407add_index_entry (struct mapped_symtab *symtab, const char *name,
17408 offset_type cu_index)
17409{
17410 struct symtab_index_entry **slot;
17411
17412 ++symtab->n_elements;
17413 if (4 * symtab->n_elements / 3 >= symtab->size)
17414 hash_expand (symtab);
17415
17416 slot = find_slot (symtab, name);
17417 if (!*slot)
17418 {
17419 *slot = XNEW (struct symtab_index_entry);
17420 (*slot)->name = name;
17421 (*slot)->cu_indices = NULL;
17422 }
17423 /* Don't push an index twice. Due to how we add entries we only
17424 have to check the last one. */
17425 if (VEC_empty (offset_type, (*slot)->cu_indices)
cf31e6f9 17426 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
9291a0cd
TT
17427 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
17428}
17429
17430/* Add a vector of indices to the constant pool. */
b89be57b 17431
9291a0cd 17432static offset_type
3876f04e 17433add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
9291a0cd
TT
17434 struct symtab_index_entry *entry)
17435{
17436 void **slot;
17437
3876f04e 17438 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
9291a0cd
TT
17439 if (!*slot)
17440 {
17441 offset_type len = VEC_length (offset_type, entry->cu_indices);
17442 offset_type val = MAYBE_SWAP (len);
17443 offset_type iter;
17444 int i;
17445
17446 *slot = entry;
17447 entry->index_offset = obstack_object_size (cpool);
17448
17449 obstack_grow (cpool, &val, sizeof (val));
17450 for (i = 0;
17451 VEC_iterate (offset_type, entry->cu_indices, i, iter);
17452 ++i)
17453 {
17454 val = MAYBE_SWAP (iter);
17455 obstack_grow (cpool, &val, sizeof (val));
17456 }
17457 }
17458 else
17459 {
17460 struct symtab_index_entry *old_entry = *slot;
17461 entry->index_offset = old_entry->index_offset;
17462 entry = old_entry;
17463 }
17464 return entry->index_offset;
17465}
17466
17467/* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
17468 constant pool entries going into the obstack CPOOL. */
b89be57b 17469
9291a0cd
TT
17470static void
17471write_hash_table (struct mapped_symtab *symtab,
17472 struct obstack *output, struct obstack *cpool)
17473{
17474 offset_type i;
3876f04e 17475 htab_t symbol_hash_table;
9291a0cd
TT
17476 htab_t str_table;
17477
3876f04e 17478 symbol_hash_table = create_symbol_hash_table ();
9291a0cd 17479 str_table = create_strtab ();
3876f04e 17480
9291a0cd
TT
17481 /* We add all the index vectors to the constant pool first, to
17482 ensure alignment is ok. */
17483 for (i = 0; i < symtab->size; ++i)
17484 {
17485 if (symtab->data[i])
3876f04e 17486 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
9291a0cd
TT
17487 }
17488
17489 /* Now write out the hash table. */
17490 for (i = 0; i < symtab->size; ++i)
17491 {
17492 offset_type str_off, vec_off;
17493
17494 if (symtab->data[i])
17495 {
17496 str_off = add_string (str_table, cpool, symtab->data[i]->name);
17497 vec_off = symtab->data[i]->index_offset;
17498 }
17499 else
17500 {
17501 /* While 0 is a valid constant pool index, it is not valid
17502 to have 0 for both offsets. */
17503 str_off = 0;
17504 vec_off = 0;
17505 }
17506
17507 str_off = MAYBE_SWAP (str_off);
17508 vec_off = MAYBE_SWAP (vec_off);
17509
17510 obstack_grow (output, &str_off, sizeof (str_off));
17511 obstack_grow (output, &vec_off, sizeof (vec_off));
17512 }
17513
17514 htab_delete (str_table);
3876f04e 17515 htab_delete (symbol_hash_table);
9291a0cd
TT
17516}
17517
0a5429f6
DE
17518/* Struct to map psymtab to CU index in the index file. */
17519struct psymtab_cu_index_map
17520{
17521 struct partial_symtab *psymtab;
17522 unsigned int cu_index;
17523};
17524
17525static hashval_t
17526hash_psymtab_cu_index (const void *item)
17527{
17528 const struct psymtab_cu_index_map *map = item;
17529
17530 return htab_hash_pointer (map->psymtab);
17531}
17532
17533static int
17534eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
17535{
17536 const struct psymtab_cu_index_map *lhs = item_lhs;
17537 const struct psymtab_cu_index_map *rhs = item_rhs;
17538
17539 return lhs->psymtab == rhs->psymtab;
17540}
17541
17542/* Helper struct for building the address table. */
17543struct addrmap_index_data
17544{
17545 struct objfile *objfile;
17546 struct obstack *addr_obstack;
17547 htab_t cu_index_htab;
17548
17549 /* Non-zero if the previous_* fields are valid.
17550 We can't write an entry until we see the next entry (since it is only then
17551 that we know the end of the entry). */
17552 int previous_valid;
17553 /* Index of the CU in the table of all CUs in the index file. */
17554 unsigned int previous_cu_index;
0963b4bd 17555 /* Start address of the CU. */
0a5429f6
DE
17556 CORE_ADDR previous_cu_start;
17557};
17558
17559/* Write an address entry to OBSTACK. */
b89be57b 17560
9291a0cd 17561static void
0a5429f6
DE
17562add_address_entry (struct objfile *objfile, struct obstack *obstack,
17563 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
9291a0cd 17564{
0a5429f6 17565 offset_type cu_index_to_write;
9291a0cd
TT
17566 char addr[8];
17567 CORE_ADDR baseaddr;
17568
17569 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17570
0a5429f6
DE
17571 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
17572 obstack_grow (obstack, addr, 8);
17573 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
17574 obstack_grow (obstack, addr, 8);
17575 cu_index_to_write = MAYBE_SWAP (cu_index);
17576 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
17577}
17578
17579/* Worker function for traversing an addrmap to build the address table. */
17580
17581static int
17582add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
17583{
17584 struct addrmap_index_data *data = datap;
17585 struct partial_symtab *pst = obj;
0a5429f6
DE
17586
17587 if (data->previous_valid)
17588 add_address_entry (data->objfile, data->addr_obstack,
17589 data->previous_cu_start, start_addr,
17590 data->previous_cu_index);
17591
17592 data->previous_cu_start = start_addr;
17593 if (pst != NULL)
17594 {
17595 struct psymtab_cu_index_map find_map, *map;
17596 find_map.psymtab = pst;
17597 map = htab_find (data->cu_index_htab, &find_map);
17598 gdb_assert (map != NULL);
17599 data->previous_cu_index = map->cu_index;
17600 data->previous_valid = 1;
17601 }
17602 else
17603 data->previous_valid = 0;
17604
17605 return 0;
17606}
17607
17608/* Write OBJFILE's address map to OBSTACK.
17609 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17610 in the index file. */
17611
17612static void
17613write_address_map (struct objfile *objfile, struct obstack *obstack,
17614 htab_t cu_index_htab)
17615{
17616 struct addrmap_index_data addrmap_index_data;
17617
17618 /* When writing the address table, we have to cope with the fact that
17619 the addrmap iterator only provides the start of a region; we have to
17620 wait until the next invocation to get the start of the next region. */
17621
17622 addrmap_index_data.objfile = objfile;
17623 addrmap_index_data.addr_obstack = obstack;
17624 addrmap_index_data.cu_index_htab = cu_index_htab;
17625 addrmap_index_data.previous_valid = 0;
17626
17627 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17628 &addrmap_index_data);
17629
17630 /* It's highly unlikely the last entry (end address = 0xff...ff)
17631 is valid, but we should still handle it.
17632 The end address is recorded as the start of the next region, but that
17633 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17634 anyway. */
17635 if (addrmap_index_data.previous_valid)
17636 add_address_entry (objfile, obstack,
17637 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17638 addrmap_index_data.previous_cu_index);
9291a0cd
TT
17639}
17640
17641/* Add a list of partial symbols to SYMTAB. */
b89be57b 17642
9291a0cd
TT
17643static void
17644write_psymbols (struct mapped_symtab *symtab,
987d643c 17645 htab_t psyms_seen,
9291a0cd
TT
17646 struct partial_symbol **psymp,
17647 int count,
987d643c
TT
17648 offset_type cu_index,
17649 int is_static)
9291a0cd
TT
17650{
17651 for (; count-- > 0; ++psymp)
17652 {
987d643c
TT
17653 void **slot, *lookup;
17654
9291a0cd
TT
17655 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
17656 error (_("Ada is not currently supported by the index"));
987d643c
TT
17657
17658 /* We only want to add a given psymbol once. However, we also
17659 want to account for whether it is global or static. So, we
17660 may add it twice, using slightly different values. */
17661 if (is_static)
17662 {
17663 uintptr_t val = 1 | (uintptr_t) *psymp;
17664
17665 lookup = (void *) val;
17666 }
17667 else
17668 lookup = *psymp;
17669
17670 /* Only add a given psymbol once. */
17671 slot = htab_find_slot (psyms_seen, lookup, INSERT);
17672 if (!*slot)
17673 {
17674 *slot = lookup;
bb2f58dc 17675 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
987d643c 17676 }
9291a0cd
TT
17677 }
17678}
17679
17680/* Write the contents of an ("unfinished") obstack to FILE. Throw an
17681 exception if there is an error. */
b89be57b 17682
9291a0cd
TT
17683static void
17684write_obstack (FILE *file, struct obstack *obstack)
17685{
17686 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17687 file)
17688 != obstack_object_size (obstack))
17689 error (_("couldn't data write to file"));
17690}
17691
17692/* Unlink a file if the argument is not NULL. */
b89be57b 17693
9291a0cd
TT
17694static void
17695unlink_if_set (void *p)
17696{
17697 char **filename = p;
17698 if (*filename)
17699 unlink (*filename);
17700}
17701
1fd400ff
TT
17702/* A helper struct used when iterating over debug_types. */
17703struct signatured_type_index_data
17704{
17705 struct objfile *objfile;
17706 struct mapped_symtab *symtab;
17707 struct obstack *types_list;
987d643c 17708 htab_t psyms_seen;
1fd400ff
TT
17709 int cu_index;
17710};
17711
17712/* A helper function that writes a single signatured_type to an
17713 obstack. */
b89be57b 17714
1fd400ff
TT
17715static int
17716write_one_signatured_type (void **slot, void *d)
17717{
17718 struct signatured_type_index_data *info = d;
17719 struct signatured_type *entry = (struct signatured_type *) *slot;
e254ef6a
DE
17720 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17721 struct partial_symtab *psymtab = per_cu->v.psymtab;
1fd400ff
TT
17722 gdb_byte val[8];
17723
17724 write_psymbols (info->symtab,
987d643c 17725 info->psyms_seen,
3e43a32a
MS
17726 info->objfile->global_psymbols.list
17727 + psymtab->globals_offset,
987d643c
TT
17728 psymtab->n_global_syms, info->cu_index,
17729 0);
1fd400ff 17730 write_psymbols (info->symtab,
987d643c 17731 info->psyms_seen,
3e43a32a
MS
17732 info->objfile->static_psymbols.list
17733 + psymtab->statics_offset,
987d643c
TT
17734 psymtab->n_static_syms, info->cu_index,
17735 1);
1fd400ff 17736
b64f50a1
JK
17737 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17738 entry->per_cu.offset.sect_off);
1fd400ff 17739 obstack_grow (info->types_list, val, 8);
3019eac3
DE
17740 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17741 entry->type_offset_in_tu.cu_off);
1fd400ff
TT
17742 obstack_grow (info->types_list, val, 8);
17743 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17744 obstack_grow (info->types_list, val, 8);
17745
17746 ++info->cu_index;
17747
17748 return 1;
17749}
17750
95554aad
TT
17751/* Recurse into all "included" dependencies and write their symbols as
17752 if they appeared in this psymtab. */
17753
17754static void
17755recursively_write_psymbols (struct objfile *objfile,
17756 struct partial_symtab *psymtab,
17757 struct mapped_symtab *symtab,
17758 htab_t psyms_seen,
17759 offset_type cu_index)
17760{
17761 int i;
17762
17763 for (i = 0; i < psymtab->number_of_dependencies; ++i)
17764 if (psymtab->dependencies[i]->user != NULL)
17765 recursively_write_psymbols (objfile, psymtab->dependencies[i],
17766 symtab, psyms_seen, cu_index);
17767
17768 write_psymbols (symtab,
17769 psyms_seen,
17770 objfile->global_psymbols.list + psymtab->globals_offset,
17771 psymtab->n_global_syms, cu_index,
17772 0);
17773 write_psymbols (symtab,
17774 psyms_seen,
17775 objfile->static_psymbols.list + psymtab->statics_offset,
17776 psymtab->n_static_syms, cu_index,
17777 1);
17778}
17779
9291a0cd 17780/* Create an index file for OBJFILE in the directory DIR. */
b89be57b 17781
9291a0cd
TT
17782static void
17783write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17784{
17785 struct cleanup *cleanup;
17786 char *filename, *cleanup_filename;
1fd400ff
TT
17787 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
17788 struct obstack cu_list, types_cu_list;
9291a0cd
TT
17789 int i;
17790 FILE *out_file;
17791 struct mapped_symtab *symtab;
17792 offset_type val, size_of_contents, total_len;
17793 struct stat st;
987d643c 17794 htab_t psyms_seen;
0a5429f6
DE
17795 htab_t cu_index_htab;
17796 struct psymtab_cu_index_map *psymtab_cu_index_map;
9291a0cd 17797
b4f2f049 17798 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
9291a0cd 17799 return;
b4f2f049 17800
9291a0cd
TT
17801 if (dwarf2_per_objfile->using_index)
17802 error (_("Cannot use an index to create the index"));
17803
8b70b953
TT
17804 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17805 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17806
9291a0cd 17807 if (stat (objfile->name, &st) < 0)
7e17e088 17808 perror_with_name (objfile->name);
9291a0cd
TT
17809
17810 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17811 INDEX_SUFFIX, (char *) NULL);
17812 cleanup = make_cleanup (xfree, filename);
17813
17814 out_file = fopen (filename, "wb");
17815 if (!out_file)
17816 error (_("Can't open `%s' for writing"), filename);
17817
17818 cleanup_filename = filename;
17819 make_cleanup (unlink_if_set, &cleanup_filename);
17820
17821 symtab = create_mapped_symtab ();
17822 make_cleanup (cleanup_mapped_symtab, symtab);
17823
17824 obstack_init (&addr_obstack);
17825 make_cleanup_obstack_free (&addr_obstack);
17826
17827 obstack_init (&cu_list);
17828 make_cleanup_obstack_free (&cu_list);
17829
1fd400ff
TT
17830 obstack_init (&types_cu_list);
17831 make_cleanup_obstack_free (&types_cu_list);
17832
987d643c
TT
17833 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17834 NULL, xcalloc, xfree);
96408a79 17835 make_cleanup_htab_delete (psyms_seen);
987d643c 17836
0a5429f6
DE
17837 /* While we're scanning CU's create a table that maps a psymtab pointer
17838 (which is what addrmap records) to its index (which is what is recorded
17839 in the index file). This will later be needed to write the address
17840 table. */
17841 cu_index_htab = htab_create_alloc (100,
17842 hash_psymtab_cu_index,
17843 eq_psymtab_cu_index,
17844 NULL, xcalloc, xfree);
96408a79 17845 make_cleanup_htab_delete (cu_index_htab);
0a5429f6
DE
17846 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17847 xmalloc (sizeof (struct psymtab_cu_index_map)
17848 * dwarf2_per_objfile->n_comp_units);
17849 make_cleanup (xfree, psymtab_cu_index_map);
17850
17851 /* The CU list is already sorted, so we don't need to do additional
1fd400ff
TT
17852 work here. Also, the debug_types entries do not appear in
17853 all_comp_units, but only in their own hash table. */
9291a0cd
TT
17854 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17855 {
3e43a32a
MS
17856 struct dwarf2_per_cu_data *per_cu
17857 = dwarf2_per_objfile->all_comp_units[i];
e254ef6a 17858 struct partial_symtab *psymtab = per_cu->v.psymtab;
9291a0cd 17859 gdb_byte val[8];
0a5429f6
DE
17860 struct psymtab_cu_index_map *map;
17861 void **slot;
9291a0cd 17862
95554aad
TT
17863 if (psymtab->user == NULL)
17864 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
9291a0cd 17865
0a5429f6
DE
17866 map = &psymtab_cu_index_map[i];
17867 map->psymtab = psymtab;
17868 map->cu_index = i;
17869 slot = htab_find_slot (cu_index_htab, map, INSERT);
17870 gdb_assert (slot != NULL);
17871 gdb_assert (*slot == NULL);
17872 *slot = map;
9291a0cd 17873
b64f50a1
JK
17874 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17875 per_cu->offset.sect_off);
9291a0cd 17876 obstack_grow (&cu_list, val, 8);
e254ef6a 17877 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
9291a0cd
TT
17878 obstack_grow (&cu_list, val, 8);
17879 }
17880
0a5429f6
DE
17881 /* Dump the address map. */
17882 write_address_map (objfile, &addr_obstack, cu_index_htab);
17883
1fd400ff
TT
17884 /* Write out the .debug_type entries, if any. */
17885 if (dwarf2_per_objfile->signatured_types)
17886 {
17887 struct signatured_type_index_data sig_data;
17888
17889 sig_data.objfile = objfile;
17890 sig_data.symtab = symtab;
17891 sig_data.types_list = &types_cu_list;
987d643c 17892 sig_data.psyms_seen = psyms_seen;
1fd400ff
TT
17893 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17894 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17895 write_one_signatured_type, &sig_data);
17896 }
17897
9291a0cd
TT
17898 obstack_init (&constant_pool);
17899 make_cleanup_obstack_free (&constant_pool);
17900 obstack_init (&symtab_obstack);
17901 make_cleanup_obstack_free (&symtab_obstack);
17902 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17903
17904 obstack_init (&contents);
17905 make_cleanup_obstack_free (&contents);
1fd400ff 17906 size_of_contents = 6 * sizeof (offset_type);
9291a0cd
TT
17907 total_len = size_of_contents;
17908
17909 /* The version number. */
481860b3 17910 val = MAYBE_SWAP (6);
9291a0cd
TT
17911 obstack_grow (&contents, &val, sizeof (val));
17912
17913 /* The offset of the CU list from the start of the file. */
17914 val = MAYBE_SWAP (total_len);
17915 obstack_grow (&contents, &val, sizeof (val));
17916 total_len += obstack_object_size (&cu_list);
17917
1fd400ff
TT
17918 /* The offset of the types CU list from the start of the file. */
17919 val = MAYBE_SWAP (total_len);
17920 obstack_grow (&contents, &val, sizeof (val));
17921 total_len += obstack_object_size (&types_cu_list);
17922
9291a0cd
TT
17923 /* The offset of the address table from the start of the file. */
17924 val = MAYBE_SWAP (total_len);
17925 obstack_grow (&contents, &val, sizeof (val));
17926 total_len += obstack_object_size (&addr_obstack);
17927
17928 /* The offset of the symbol table from the start of the file. */
17929 val = MAYBE_SWAP (total_len);
17930 obstack_grow (&contents, &val, sizeof (val));
17931 total_len += obstack_object_size (&symtab_obstack);
17932
17933 /* The offset of the constant pool from the start of the file. */
17934 val = MAYBE_SWAP (total_len);
17935 obstack_grow (&contents, &val, sizeof (val));
17936 total_len += obstack_object_size (&constant_pool);
17937
17938 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17939
17940 write_obstack (out_file, &contents);
17941 write_obstack (out_file, &cu_list);
1fd400ff 17942 write_obstack (out_file, &types_cu_list);
9291a0cd
TT
17943 write_obstack (out_file, &addr_obstack);
17944 write_obstack (out_file, &symtab_obstack);
17945 write_obstack (out_file, &constant_pool);
17946
17947 fclose (out_file);
17948
17949 /* We want to keep the file, so we set cleanup_filename to NULL
17950 here. See unlink_if_set. */
17951 cleanup_filename = NULL;
17952
17953 do_cleanups (cleanup);
17954}
17955
90476074
TT
17956/* Implementation of the `save gdb-index' command.
17957
17958 Note that the file format used by this command is documented in the
17959 GDB manual. Any changes here must be documented there. */
11570e71 17960
9291a0cd
TT
17961static void
17962save_gdb_index_command (char *arg, int from_tty)
17963{
17964 struct objfile *objfile;
17965
17966 if (!arg || !*arg)
96d19272 17967 error (_("usage: save gdb-index DIRECTORY"));
9291a0cd
TT
17968
17969 ALL_OBJFILES (objfile)
17970 {
17971 struct stat st;
17972
17973 /* If the objfile does not correspond to an actual file, skip it. */
17974 if (stat (objfile->name, &st) < 0)
17975 continue;
17976
17977 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17978 if (dwarf2_per_objfile)
17979 {
17980 volatile struct gdb_exception except;
17981
17982 TRY_CATCH (except, RETURN_MASK_ERROR)
17983 {
17984 write_psymtabs_to_index (objfile, arg);
17985 }
17986 if (except.reason < 0)
17987 exception_fprintf (gdb_stderr, except,
17988 _("Error while writing index for `%s': "),
17989 objfile->name);
17990 }
17991 }
dce234bc
PP
17992}
17993
9291a0cd
TT
17994\f
17995
9eae7c52
TT
17996int dwarf2_always_disassemble;
17997
17998static void
17999show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
18000 struct cmd_list_element *c, const char *value)
18001{
3e43a32a
MS
18002 fprintf_filtered (file,
18003 _("Whether to always disassemble "
18004 "DWARF expressions is %s.\n"),
9eae7c52
TT
18005 value);
18006}
18007
900e11f9
JK
18008static void
18009show_check_physname (struct ui_file *file, int from_tty,
18010 struct cmd_list_element *c, const char *value)
18011{
18012 fprintf_filtered (file,
18013 _("Whether to check \"physname\" is %s.\n"),
18014 value);
18015}
18016
6502dd73
DJ
18017void _initialize_dwarf2_read (void);
18018
18019void
18020_initialize_dwarf2_read (void)
18021{
96d19272
JK
18022 struct cmd_list_element *c;
18023
dce234bc 18024 dwarf2_objfile_data_key
c1bd65d0 18025 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
ae038cb0 18026
1bedd215
AC
18027 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
18028Set DWARF 2 specific variables.\n\
18029Configure DWARF 2 variables such as the cache size"),
ae038cb0
DJ
18030 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
18031 0/*allow-unknown*/, &maintenance_set_cmdlist);
18032
1bedd215
AC
18033 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
18034Show DWARF 2 specific variables\n\
18035Show DWARF 2 variables such as the cache size"),
ae038cb0
DJ
18036 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
18037 0/*allow-unknown*/, &maintenance_show_cmdlist);
18038
18039 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
7915a72c
AC
18040 &dwarf2_max_cache_age, _("\
18041Set the upper bound on the age of cached dwarf2 compilation units."), _("\
18042Show the upper bound on the age of cached dwarf2 compilation units."), _("\
18043A higher limit means that cached compilation units will be stored\n\
18044in memory longer, and more total memory will be used. Zero disables\n\
18045caching, which can slow down startup."),
2c5b56ce 18046 NULL,
920d2a44 18047 show_dwarf2_max_cache_age,
2c5b56ce 18048 &set_dwarf2_cmdlist,
ae038cb0 18049 &show_dwarf2_cmdlist);
d97bc12b 18050
9eae7c52
TT
18051 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
18052 &dwarf2_always_disassemble, _("\
18053Set whether `info address' always disassembles DWARF expressions."), _("\
18054Show whether `info address' always disassembles DWARF expressions."), _("\
18055When enabled, DWARF expressions are always printed in an assembly-like\n\
18056syntax. When disabled, expressions will be printed in a more\n\
18057conversational style, when possible."),
18058 NULL,
18059 show_dwarf2_always_disassemble,
18060 &set_dwarf2_cmdlist,
18061 &show_dwarf2_cmdlist);
18062
d97bc12b
DE
18063 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
18064Set debugging of the dwarf2 DIE reader."), _("\
18065Show debugging of the dwarf2 DIE reader."), _("\
18066When enabled (non-zero), DIEs are dumped after they are read in.\n\
18067The value is the maximum depth to print."),
18068 NULL,
18069 NULL,
18070 &setdebuglist, &showdebuglist);
9291a0cd 18071
900e11f9
JK
18072 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
18073Set cross-checking of \"physname\" code against demangler."), _("\
18074Show cross-checking of \"physname\" code against demangler."), _("\
18075When enabled, GDB's internal \"physname\" code is checked against\n\
18076the demangler."),
18077 NULL, show_check_physname,
18078 &setdebuglist, &showdebuglist);
18079
96d19272 18080 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
11570e71 18081 _("\
fc1a9d6e 18082Save a gdb-index file.\n\
11570e71 18083Usage: save gdb-index DIRECTORY"),
96d19272
JK
18084 &save_cmdlist);
18085 set_cmd_completer (c, filename_completer);
6502dd73 18086}
This page took 2.500598 seconds and 4 git commands to generate.