316bb861c007d51ceac8cb9e637d6594b1261c8c
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2013 Free Software Foundation, Inc.
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
10 support.
11
12 This file is part of GDB.
13
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
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
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.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
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
31 #include "defs.h"
32 #include "bfd.h"
33 #include "elf-bfd.h"
34 #include "symtab.h"
35 #include "gdbtypes.h"
36 #include "objfiles.h"
37 #include "dwarf2.h"
38 #include "buildsym.h"
39 #include "demangle.h"
40 #include "gdb-demangle.h"
41 #include "expression.h"
42 #include "filenames.h" /* for DOSish file names */
43 #include "macrotab.h"
44 #include "language.h"
45 #include "complaints.h"
46 #include "bcache.h"
47 #include "dwarf2expr.h"
48 #include "dwarf2loc.h"
49 #include "cp-support.h"
50 #include "hashtab.h"
51 #include "command.h"
52 #include "gdbcmd.h"
53 #include "block.h"
54 #include "addrmap.h"
55 #include "typeprint.h"
56 #include "jv-lang.h"
57 #include "psympriv.h"
58 #include "exceptions.h"
59 #include "gdb_stat.h"
60 #include "completer.h"
61 #include "vec.h"
62 #include "c-lang.h"
63 #include "go-lang.h"
64 #include "valprint.h"
65 #include "gdbcore.h" /* for gnutarget */
66 #include "gdb/gdb-index.h"
67 #include <ctype.h>
68 #include "gdb_bfd.h"
69 #include "f-lang.h"
70 #include "source.h"
71 #include "filestuff.h"
72
73 #include <fcntl.h>
74 #include "gdb_string.h"
75 #include "gdb_assert.h"
76 #include <sys/types.h>
77
78 typedef struct symbol *symbolp;
79 DEF_VEC_P (symbolp);
80
81 /* When non-zero, print basic high level tracing messages.
82 This is in contrast to the low level DIE reading of dwarf2_die_debug. */
83 static int dwarf2_read_debug = 0;
84
85 /* When non-zero, dump DIEs after they are read in. */
86 static unsigned int dwarf2_die_debug = 0;
87
88 /* When non-zero, cross-check physname against demangler. */
89 static int check_physname = 0;
90
91 /* When non-zero, do not reject deprecated .gdb_index sections. */
92 static int use_deprecated_index_sections = 0;
93
94 static const struct objfile_data *dwarf2_objfile_data_key;
95
96 /* The "aclass" indices for various kinds of computed DWARF symbols. */
97
98 static int dwarf2_locexpr_index;
99 static int dwarf2_loclist_index;
100 static int dwarf2_locexpr_block_index;
101 static int dwarf2_loclist_block_index;
102
103 struct dwarf2_section_info
104 {
105 asection *asection;
106 const gdb_byte *buffer;
107 bfd_size_type size;
108 /* True if we have tried to read this section. */
109 int readin;
110 };
111
112 typedef struct dwarf2_section_info dwarf2_section_info_def;
113 DEF_VEC_O (dwarf2_section_info_def);
114
115 /* All offsets in the index are of this type. It must be
116 architecture-independent. */
117 typedef uint32_t offset_type;
118
119 DEF_VEC_I (offset_type);
120
121 /* Ensure only legit values are used. */
122 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
123 do { \
124 gdb_assert ((unsigned int) (value) <= 1); \
125 GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
126 } while (0)
127
128 /* Ensure only legit values are used. */
129 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
130 do { \
131 gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
132 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
133 GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
134 } while (0)
135
136 /* Ensure we don't use more than the alloted nuber of bits for the CU. */
137 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
138 do { \
139 gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
140 GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
141 } while (0)
142
143 /* A description of the mapped index. The file format is described in
144 a comment by the code that writes the index. */
145 struct mapped_index
146 {
147 /* Index data format version. */
148 int version;
149
150 /* The total length of the buffer. */
151 off_t total_size;
152
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155
156 /* Size of the address table data in bytes. */
157 offset_type address_table_size;
158
159 /* The symbol table, implemented as a hash table. */
160 const offset_type *symbol_table;
161
162 /* Size in slots, each slot is 2 offset_types. */
163 offset_type symbol_table_slots;
164
165 /* A pointer to the constant pool. */
166 const char *constant_pool;
167 };
168
169 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
170 DEF_VEC_P (dwarf2_per_cu_ptr);
171
172 /* Collection of data recorded per objfile.
173 This hangs off of dwarf2_objfile_data_key. */
174
175 struct dwarf2_per_objfile
176 {
177 struct dwarf2_section_info info;
178 struct dwarf2_section_info abbrev;
179 struct dwarf2_section_info line;
180 struct dwarf2_section_info loc;
181 struct dwarf2_section_info macinfo;
182 struct dwarf2_section_info macro;
183 struct dwarf2_section_info str;
184 struct dwarf2_section_info ranges;
185 struct dwarf2_section_info addr;
186 struct dwarf2_section_info frame;
187 struct dwarf2_section_info eh_frame;
188 struct dwarf2_section_info gdb_index;
189
190 VEC (dwarf2_section_info_def) *types;
191
192 /* Back link. */
193 struct objfile *objfile;
194
195 /* Table of all the compilation units. This is used to locate
196 the target compilation unit of a particular reference. */
197 struct dwarf2_per_cu_data **all_comp_units;
198
199 /* The number of compilation units in ALL_COMP_UNITS. */
200 int n_comp_units;
201
202 /* The number of .debug_types-related CUs. */
203 int n_type_units;
204
205 /* The .debug_types-related CUs (TUs).
206 This is stored in malloc space because we may realloc it. */
207 struct signatured_type **all_type_units;
208
209 /* The number of entries in all_type_unit_groups. */
210 int n_type_unit_groups;
211
212 /* Table of type unit groups.
213 This exists to make it easy to iterate over all CUs and TU groups. */
214 struct type_unit_group **all_type_unit_groups;
215
216 /* Table of struct type_unit_group objects.
217 The hash key is the DW_AT_stmt_list value. */
218 htab_t type_unit_groups;
219
220 /* A table mapping .debug_types signatures to its signatured_type entry.
221 This is NULL if the .debug_types section hasn't been read in yet. */
222 htab_t signatured_types;
223
224 /* Type unit statistics, to see how well the scaling improvements
225 are doing. */
226 struct tu_stats
227 {
228 int nr_uniq_abbrev_tables;
229 int nr_symtabs;
230 int nr_symtab_sharers;
231 int nr_stmt_less_type_units;
232 } tu_stats;
233
234 /* A chain of compilation units that are currently read in, so that
235 they can be freed later. */
236 struct dwarf2_per_cu_data *read_in_chain;
237
238 /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
239 This is NULL if the table hasn't been allocated yet. */
240 htab_t dwo_files;
241
242 /* Non-zero if we've check for whether there is a DWP file. */
243 int dwp_checked;
244
245 /* The DWP file if there is one, or NULL. */
246 struct dwp_file *dwp_file;
247
248 /* The shared '.dwz' file, if one exists. This is used when the
249 original data was compressed using 'dwz -m'. */
250 struct dwz_file *dwz_file;
251
252 /* A flag indicating wether this objfile has a section loaded at a
253 VMA of 0. */
254 int has_section_at_zero;
255
256 /* True if we are using the mapped index,
257 or we are faking it for OBJF_READNOW's sake. */
258 unsigned char using_index;
259
260 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
261 struct mapped_index *index_table;
262
263 /* When using index_table, this keeps track of all quick_file_names entries.
264 TUs typically share line table entries with a CU, so we maintain a
265 separate table of all line table entries to support the sharing.
266 Note that while there can be way more TUs than CUs, we've already
267 sorted all the TUs into "type unit groups", grouped by their
268 DW_AT_stmt_list value. Therefore the only sharing done here is with a
269 CU and its associated TU group if there is one. */
270 htab_t quick_file_names_table;
271
272 /* Set during partial symbol reading, to prevent queueing of full
273 symbols. */
274 int reading_partial_symbols;
275
276 /* Table mapping type DIEs to their struct type *.
277 This is NULL if not allocated yet.
278 The mapping is done via (CU/TU + DIE offset) -> type. */
279 htab_t die_type_hash;
280
281 /* The CUs we recently read. */
282 VEC (dwarf2_per_cu_ptr) *just_read_cus;
283 };
284
285 static struct dwarf2_per_objfile *dwarf2_per_objfile;
286
287 /* Default names of the debugging sections. */
288
289 /* Note that if the debugging section has been compressed, it might
290 have a name like .zdebug_info. */
291
292 static const struct dwarf2_debug_sections dwarf2_elf_names =
293 {
294 { ".debug_info", ".zdebug_info" },
295 { ".debug_abbrev", ".zdebug_abbrev" },
296 { ".debug_line", ".zdebug_line" },
297 { ".debug_loc", ".zdebug_loc" },
298 { ".debug_macinfo", ".zdebug_macinfo" },
299 { ".debug_macro", ".zdebug_macro" },
300 { ".debug_str", ".zdebug_str" },
301 { ".debug_ranges", ".zdebug_ranges" },
302 { ".debug_types", ".zdebug_types" },
303 { ".debug_addr", ".zdebug_addr" },
304 { ".debug_frame", ".zdebug_frame" },
305 { ".eh_frame", NULL },
306 { ".gdb_index", ".zgdb_index" },
307 23
308 };
309
310 /* List of DWO/DWP sections. */
311
312 static const struct dwop_section_names
313 {
314 struct dwarf2_section_names abbrev_dwo;
315 struct dwarf2_section_names info_dwo;
316 struct dwarf2_section_names line_dwo;
317 struct dwarf2_section_names loc_dwo;
318 struct dwarf2_section_names macinfo_dwo;
319 struct dwarf2_section_names macro_dwo;
320 struct dwarf2_section_names str_dwo;
321 struct dwarf2_section_names str_offsets_dwo;
322 struct dwarf2_section_names types_dwo;
323 struct dwarf2_section_names cu_index;
324 struct dwarf2_section_names tu_index;
325 }
326 dwop_section_names =
327 {
328 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
329 { ".debug_info.dwo", ".zdebug_info.dwo" },
330 { ".debug_line.dwo", ".zdebug_line.dwo" },
331 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
332 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
333 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
334 { ".debug_str.dwo", ".zdebug_str.dwo" },
335 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
336 { ".debug_types.dwo", ".zdebug_types.dwo" },
337 { ".debug_cu_index", ".zdebug_cu_index" },
338 { ".debug_tu_index", ".zdebug_tu_index" },
339 };
340
341 /* local data types */
342
343 /* The data in a compilation unit header, after target2host
344 translation, looks like this. */
345 struct comp_unit_head
346 {
347 unsigned int length;
348 short version;
349 unsigned char addr_size;
350 unsigned char signed_addr_p;
351 sect_offset abbrev_offset;
352
353 /* Size of file offsets; either 4 or 8. */
354 unsigned int offset_size;
355
356 /* Size of the length field; either 4 or 12. */
357 unsigned int initial_length_size;
358
359 /* Offset to the first byte of this compilation unit header in the
360 .debug_info section, for resolving relative reference dies. */
361 sect_offset offset;
362
363 /* Offset to first die in this cu from the start of the cu.
364 This will be the first byte following the compilation unit header. */
365 cu_offset first_die_offset;
366 };
367
368 /* Type used for delaying computation of method physnames.
369 See comments for compute_delayed_physnames. */
370 struct delayed_method_info
371 {
372 /* The type to which the method is attached, i.e., its parent class. */
373 struct type *type;
374
375 /* The index of the method in the type's function fieldlists. */
376 int fnfield_index;
377
378 /* The index of the method in the fieldlist. */
379 int index;
380
381 /* The name of the DIE. */
382 const char *name;
383
384 /* The DIE associated with this method. */
385 struct die_info *die;
386 };
387
388 typedef struct delayed_method_info delayed_method_info;
389 DEF_VEC_O (delayed_method_info);
390
391 /* Internal state when decoding a particular compilation unit. */
392 struct dwarf2_cu
393 {
394 /* The objfile containing this compilation unit. */
395 struct objfile *objfile;
396
397 /* The header of the compilation unit. */
398 struct comp_unit_head header;
399
400 /* Base address of this compilation unit. */
401 CORE_ADDR base_address;
402
403 /* Non-zero if base_address has been set. */
404 int base_known;
405
406 /* The language we are debugging. */
407 enum language language;
408 const struct language_defn *language_defn;
409
410 const char *producer;
411
412 /* The generic symbol table building routines have separate lists for
413 file scope symbols and all all other scopes (local scopes). So
414 we need to select the right one to pass to add_symbol_to_list().
415 We do it by keeping a pointer to the correct list in list_in_scope.
416
417 FIXME: The original dwarf code just treated the file scope as the
418 first local scope, and all other local scopes as nested local
419 scopes, and worked fine. Check to see if we really need to
420 distinguish these in buildsym.c. */
421 struct pending **list_in_scope;
422
423 /* The abbrev table for this CU.
424 Normally this points to the abbrev table in the objfile.
425 But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file. */
426 struct abbrev_table *abbrev_table;
427
428 /* Hash table holding all the loaded partial DIEs
429 with partial_die->offset.SECT_OFF as hash. */
430 htab_t partial_dies;
431
432 /* Storage for things with the same lifetime as this read-in compilation
433 unit, including partial DIEs. */
434 struct obstack comp_unit_obstack;
435
436 /* When multiple dwarf2_cu structures are living in memory, this field
437 chains them all together, so that they can be released efficiently.
438 We will probably also want a generation counter so that most-recently-used
439 compilation units are cached... */
440 struct dwarf2_per_cu_data *read_in_chain;
441
442 /* Backchain to our per_cu entry if the tree has been built. */
443 struct dwarf2_per_cu_data *per_cu;
444
445 /* How many compilation units ago was this CU last referenced? */
446 int last_used;
447
448 /* A hash table of DIE cu_offset for following references with
449 die_info->offset.sect_off as hash. */
450 htab_t die_hash;
451
452 /* Full DIEs if read in. */
453 struct die_info *dies;
454
455 /* A set of pointers to dwarf2_per_cu_data objects for compilation
456 units referenced by this one. Only set during full symbol processing;
457 partial symbol tables do not have dependencies. */
458 htab_t dependencies;
459
460 /* Header data from the line table, during full symbol processing. */
461 struct line_header *line_header;
462
463 /* A list of methods which need to have physnames computed
464 after all type information has been read. */
465 VEC (delayed_method_info) *method_list;
466
467 /* To be copied to symtab->call_site_htab. */
468 htab_t call_site_htab;
469
470 /* Non-NULL if this CU came from a DWO file.
471 There is an invariant here that is important to remember:
472 Except for attributes copied from the top level DIE in the "main"
473 (or "stub") file in preparation for reading the DWO file
474 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
475 Either there isn't a DWO file (in which case this is NULL and the point
476 is moot), or there is and either we're not going to read it (in which
477 case this is NULL) or there is and we are reading it (in which case this
478 is non-NULL). */
479 struct dwo_unit *dwo_unit;
480
481 /* The DW_AT_addr_base attribute if present, zero otherwise
482 (zero is a valid value though).
483 Note this value comes from the stub CU/TU's DIE. */
484 ULONGEST addr_base;
485
486 /* The DW_AT_ranges_base attribute if present, zero otherwise
487 (zero is a valid value though).
488 Note this value comes from the stub CU/TU's DIE.
489 Also note that the value is zero in the non-DWO case so this value can
490 be used without needing to know whether DWO files are in use or not.
491 N.B. This does not apply to DW_AT_ranges appearing in
492 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
493 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
494 DW_AT_ranges_base *would* have to be applied, and we'd have to care
495 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
496 ULONGEST ranges_base;
497
498 /* Mark used when releasing cached dies. */
499 unsigned int mark : 1;
500
501 /* This CU references .debug_loc. See the symtab->locations_valid field.
502 This test is imperfect as there may exist optimized debug code not using
503 any location list and still facing inlining issues if handled as
504 unoptimized code. For a future better test see GCC PR other/32998. */
505 unsigned int has_loclist : 1;
506
507 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
508 if all the producer_is_* fields are valid. This information is cached
509 because profiling CU expansion showed excessive time spent in
510 producer_is_gxx_lt_4_6. */
511 unsigned int checked_producer : 1;
512 unsigned int producer_is_gxx_lt_4_6 : 1;
513 unsigned int producer_is_gcc_lt_4_3 : 1;
514 unsigned int producer_is_icc : 1;
515
516 /* When set, the file that we're processing is known to have
517 debugging info for C++ namespaces. GCC 3.3.x did not produce
518 this information, but later versions do. */
519
520 unsigned int processing_has_namespace_info : 1;
521 };
522
523 /* Persistent data held for a compilation unit, even when not
524 processing it. We put a pointer to this structure in the
525 read_symtab_private field of the psymtab. */
526
527 struct dwarf2_per_cu_data
528 {
529 /* The start offset and length of this compilation unit.
530 NOTE: Unlike comp_unit_head.length, this length includes
531 initial_length_size.
532 If the DIE refers to a DWO file, this is always of the original die,
533 not the DWO file. */
534 sect_offset offset;
535 unsigned int length;
536
537 /* Flag indicating this compilation unit will be read in before
538 any of the current compilation units are processed. */
539 unsigned int queued : 1;
540
541 /* This flag will be set when reading partial DIEs if we need to load
542 absolutely all DIEs for this compilation unit, instead of just the ones
543 we think are interesting. It gets set if we look for a DIE in the
544 hash table and don't find it. */
545 unsigned int load_all_dies : 1;
546
547 /* Non-zero if this CU is from .debug_types.
548 Struct dwarf2_per_cu_data is contained in struct signatured_type iff
549 this is non-zero. */
550 unsigned int is_debug_types : 1;
551
552 /* Non-zero if this CU is from the .dwz file. */
553 unsigned int is_dwz : 1;
554
555 /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
556 This flag is only valid if is_debug_types is true.
557 We can't read a CU directly from a DWO file: There are required
558 attributes in the stub. */
559 unsigned int reading_dwo_directly : 1;
560
561 /* The section this CU/TU lives in.
562 If the DIE refers to a DWO file, this is always the original die,
563 not the DWO file. */
564 struct dwarf2_section_info *section;
565
566 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
567 of the CU cache it gets reset to NULL again. */
568 struct dwarf2_cu *cu;
569
570 /* The corresponding objfile.
571 Normally we can get the objfile from dwarf2_per_objfile.
572 However we can enter this file with just a "per_cu" handle. */
573 struct objfile *objfile;
574
575 /* When using partial symbol tables, the 'psymtab' field is active.
576 Otherwise the 'quick' field is active. */
577 union
578 {
579 /* The partial symbol table associated with this compilation unit,
580 or NULL for unread partial units. */
581 struct partial_symtab *psymtab;
582
583 /* Data needed by the "quick" functions. */
584 struct dwarf2_per_cu_quick_data *quick;
585 } v;
586
587 /* The CUs we import using DW_TAG_imported_unit. This is filled in
588 while reading psymtabs, used to compute the psymtab dependencies,
589 and then cleared. Then it is filled in again while reading full
590 symbols, and only deleted when the objfile is destroyed.
591
592 This is also used to work around a difference between the way gold
593 generates .gdb_index version <=7 and the way gdb does. Arguably this
594 is a gold bug. For symbols coming from TUs, gold records in the index
595 the CU that includes the TU instead of the TU itself. This breaks
596 dw2_lookup_symbol: It assumes that if the index says symbol X lives
597 in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
598 will find X. Alas TUs live in their own symtab, so after expanding CU Y
599 we need to look in TU Z to find X. Fortunately, this is akin to
600 DW_TAG_imported_unit, so we just use the same mechanism: For
601 .gdb_index version <=7 this also records the TUs that the CU referred
602 to. Concurrently with this change gdb was modified to emit version 8
603 indices so we only pay a price for gold generated indices. */
604 VEC (dwarf2_per_cu_ptr) *imported_symtabs;
605 };
606
607 /* Entry in the signatured_types hash table. */
608
609 struct signatured_type
610 {
611 /* The "per_cu" object of this type.
612 This struct is used iff per_cu.is_debug_types.
613 N.B.: This is the first member so that it's easy to convert pointers
614 between them. */
615 struct dwarf2_per_cu_data per_cu;
616
617 /* The type's signature. */
618 ULONGEST signature;
619
620 /* Offset in the TU of the type's DIE, as read from the TU header.
621 If this TU is a DWO stub and the definition lives in a DWO file
622 (specified by DW_AT_GNU_dwo_name), this value is unusable. */
623 cu_offset type_offset_in_tu;
624
625 /* Offset in the section of the type's DIE.
626 If the definition lives in a DWO file, this is the offset in the
627 .debug_types.dwo section.
628 The value is zero until the actual value is known.
629 Zero is otherwise not a valid section offset. */
630 sect_offset type_offset_in_section;
631
632 /* Type units are grouped by their DW_AT_stmt_list entry so that they
633 can share them. This points to the containing symtab. */
634 struct type_unit_group *type_unit_group;
635
636 /* The type.
637 The first time we encounter this type we fully read it in and install it
638 in the symbol tables. Subsequent times we only need the type. */
639 struct type *type;
640
641 /* Containing DWO unit.
642 This field is valid iff per_cu.reading_dwo_directly. */
643 struct dwo_unit *dwo_unit;
644 };
645
646 typedef struct signatured_type *sig_type_ptr;
647 DEF_VEC_P (sig_type_ptr);
648
649 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
650 This includes type_unit_group and quick_file_names. */
651
652 struct stmt_list_hash
653 {
654 /* The DWO unit this table is from or NULL if there is none. */
655 struct dwo_unit *dwo_unit;
656
657 /* Offset in .debug_line or .debug_line.dwo. */
658 sect_offset line_offset;
659 };
660
661 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
662 an object of this type. */
663
664 struct type_unit_group
665 {
666 /* dwarf2read.c's main "handle" on a TU symtab.
667 To simplify things we create an artificial CU that "includes" all the
668 type units using this stmt_list so that the rest of the code still has
669 a "per_cu" handle on the symtab.
670 This PER_CU is recognized by having no section. */
671 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
672 struct dwarf2_per_cu_data per_cu;
673
674 /* The TUs that share this DW_AT_stmt_list entry.
675 This is added to while parsing type units to build partial symtabs,
676 and is deleted afterwards and not used again. */
677 VEC (sig_type_ptr) *tus;
678
679 /* The primary symtab.
680 Type units in a group needn't all be defined in the same source file,
681 so we create an essentially anonymous symtab as the primary symtab. */
682 struct symtab *primary_symtab;
683
684 /* The data used to construct the hash key. */
685 struct stmt_list_hash hash;
686
687 /* The number of symtabs from the line header.
688 The value here must match line_header.num_file_names. */
689 unsigned int num_symtabs;
690
691 /* The symbol tables for this TU (obtained from the files listed in
692 DW_AT_stmt_list).
693 WARNING: The order of entries here must match the order of entries
694 in the line header. After the first TU using this type_unit_group, the
695 line header for the subsequent TUs is recreated from this. This is done
696 because we need to use the same symtabs for each TU using the same
697 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
698 there's no guarantee the line header doesn't have duplicate entries. */
699 struct symtab **symtabs;
700 };
701
702 /* These sections are what may appear in a DWO file. */
703
704 struct dwo_sections
705 {
706 struct dwarf2_section_info abbrev;
707 struct dwarf2_section_info line;
708 struct dwarf2_section_info loc;
709 struct dwarf2_section_info macinfo;
710 struct dwarf2_section_info macro;
711 struct dwarf2_section_info str;
712 struct dwarf2_section_info str_offsets;
713 /* In the case of a virtual DWO file, these two are unused. */
714 struct dwarf2_section_info info;
715 VEC (dwarf2_section_info_def) *types;
716 };
717
718 /* CUs/TUs in DWP/DWO files. */
719
720 struct dwo_unit
721 {
722 /* Backlink to the containing struct dwo_file. */
723 struct dwo_file *dwo_file;
724
725 /* The "id" that distinguishes this CU/TU.
726 .debug_info calls this "dwo_id", .debug_types calls this "signature".
727 Since signatures came first, we stick with it for consistency. */
728 ULONGEST signature;
729
730 /* The section this CU/TU lives in, in the DWO file. */
731 struct dwarf2_section_info *section;
732
733 /* Same as dwarf2_per_cu_data:{offset,length} but for the DWO section. */
734 sect_offset offset;
735 unsigned int length;
736
737 /* For types, offset in the type's DIE of the type defined by this TU. */
738 cu_offset type_offset_in_tu;
739 };
740
741 /* Data for one DWO file.
742 This includes virtual DWO files that have been packaged into a
743 DWP file. */
744
745 struct dwo_file
746 {
747 /* The DW_AT_GNU_dwo_name attribute.
748 For virtual DWO files the name is constructed from the section offsets
749 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
750 from related CU+TUs. */
751 const char *dwo_name;
752
753 /* The DW_AT_comp_dir attribute. */
754 const char *comp_dir;
755
756 /* The bfd, when the file is open. Otherwise this is NULL.
757 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
758 bfd *dbfd;
759
760 /* Section info for this file. */
761 struct dwo_sections sections;
762
763 /* The CU in the file.
764 We only support one because having more than one requires hacking the
765 dwo_name of each to match, which is highly unlikely to happen.
766 Doing this means all TUs can share comp_dir: We also assume that
767 DW_AT_comp_dir across all TUs in a DWO file will be identical. */
768 struct dwo_unit *cu;
769
770 /* Table of TUs in the file.
771 Each element is a struct dwo_unit. */
772 htab_t tus;
773 };
774
775 /* These sections are what may appear in a DWP file. */
776
777 struct dwp_sections
778 {
779 struct dwarf2_section_info str;
780 struct dwarf2_section_info cu_index;
781 struct dwarf2_section_info tu_index;
782 /* The .debug_info.dwo, .debug_types.dwo, and other sections are referenced
783 by section number. We don't need to record them here. */
784 };
785
786 /* These sections are what may appear in a virtual DWO file. */
787
788 struct virtual_dwo_sections
789 {
790 struct dwarf2_section_info abbrev;
791 struct dwarf2_section_info line;
792 struct dwarf2_section_info loc;
793 struct dwarf2_section_info macinfo;
794 struct dwarf2_section_info macro;
795 struct dwarf2_section_info str_offsets;
796 /* Each DWP hash table entry records one CU or one TU.
797 That is recorded here, and copied to dwo_unit.section. */
798 struct dwarf2_section_info info_or_types;
799 };
800
801 /* Contents of DWP hash tables. */
802
803 struct dwp_hash_table
804 {
805 uint32_t nr_units, nr_slots;
806 const gdb_byte *hash_table, *unit_table, *section_pool;
807 };
808
809 /* Data for one DWP file. */
810
811 struct dwp_file
812 {
813 /* Name of the file. */
814 const char *name;
815
816 /* The bfd, when the file is open. Otherwise this is NULL. */
817 bfd *dbfd;
818
819 /* Section info for this file. */
820 struct dwp_sections sections;
821
822 /* Table of CUs in the file. */
823 const struct dwp_hash_table *cus;
824
825 /* Table of TUs in the file. */
826 const struct dwp_hash_table *tus;
827
828 /* Table of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
829 htab_t loaded_cutus;
830
831 /* Table to map ELF section numbers to their sections. */
832 unsigned int num_sections;
833 asection **elf_sections;
834 };
835
836 /* This represents a '.dwz' file. */
837
838 struct dwz_file
839 {
840 /* A dwz file can only contain a few sections. */
841 struct dwarf2_section_info abbrev;
842 struct dwarf2_section_info info;
843 struct dwarf2_section_info str;
844 struct dwarf2_section_info line;
845 struct dwarf2_section_info macro;
846 struct dwarf2_section_info gdb_index;
847
848 /* The dwz's BFD. */
849 bfd *dwz_bfd;
850 };
851
852 /* Struct used to pass misc. parameters to read_die_and_children, et
853 al. which are used for both .debug_info and .debug_types dies.
854 All parameters here are unchanging for the life of the call. This
855 struct exists to abstract away the constant parameters of die reading. */
856
857 struct die_reader_specs
858 {
859 /* die_section->asection->owner. */
860 bfd* abfd;
861
862 /* The CU of the DIE we are parsing. */
863 struct dwarf2_cu *cu;
864
865 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
866 struct dwo_file *dwo_file;
867
868 /* The section the die comes from.
869 This is either .debug_info or .debug_types, or the .dwo variants. */
870 struct dwarf2_section_info *die_section;
871
872 /* die_section->buffer. */
873 const gdb_byte *buffer;
874
875 /* The end of the buffer. */
876 const gdb_byte *buffer_end;
877
878 /* The value of the DW_AT_comp_dir attribute. */
879 const char *comp_dir;
880 };
881
882 /* Type of function passed to init_cutu_and_read_dies, et.al. */
883 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
884 const gdb_byte *info_ptr,
885 struct die_info *comp_unit_die,
886 int has_children,
887 void *data);
888
889 /* The line number information for a compilation unit (found in the
890 .debug_line section) begins with a "statement program header",
891 which contains the following information. */
892 struct line_header
893 {
894 unsigned int total_length;
895 unsigned short version;
896 unsigned int header_length;
897 unsigned char minimum_instruction_length;
898 unsigned char maximum_ops_per_instruction;
899 unsigned char default_is_stmt;
900 int line_base;
901 unsigned char line_range;
902 unsigned char opcode_base;
903
904 /* standard_opcode_lengths[i] is the number of operands for the
905 standard opcode whose value is i. This means that
906 standard_opcode_lengths[0] is unused, and the last meaningful
907 element is standard_opcode_lengths[opcode_base - 1]. */
908 unsigned char *standard_opcode_lengths;
909
910 /* The include_directories table. NOTE! These strings are not
911 allocated with xmalloc; instead, they are pointers into
912 debug_line_buffer. If you try to free them, `free' will get
913 indigestion. */
914 unsigned int num_include_dirs, include_dirs_size;
915 const char **include_dirs;
916
917 /* The file_names table. NOTE! These strings are not allocated
918 with xmalloc; instead, they are pointers into debug_line_buffer.
919 Don't try to free them directly. */
920 unsigned int num_file_names, file_names_size;
921 struct file_entry
922 {
923 const char *name;
924 unsigned int dir_index;
925 unsigned int mod_time;
926 unsigned int length;
927 int included_p; /* Non-zero if referenced by the Line Number Program. */
928 struct symtab *symtab; /* The associated symbol table, if any. */
929 } *file_names;
930
931 /* The start and end of the statement program following this
932 header. These point into dwarf2_per_objfile->line_buffer. */
933 const gdb_byte *statement_program_start, *statement_program_end;
934 };
935
936 /* When we construct a partial symbol table entry we only
937 need this much information. */
938 struct partial_die_info
939 {
940 /* Offset of this DIE. */
941 sect_offset offset;
942
943 /* DWARF-2 tag for this DIE. */
944 ENUM_BITFIELD(dwarf_tag) tag : 16;
945
946 /* Assorted flags describing the data found in this DIE. */
947 unsigned int has_children : 1;
948 unsigned int is_external : 1;
949 unsigned int is_declaration : 1;
950 unsigned int has_type : 1;
951 unsigned int has_specification : 1;
952 unsigned int has_pc_info : 1;
953 unsigned int may_be_inlined : 1;
954
955 /* Flag set if the SCOPE field of this structure has been
956 computed. */
957 unsigned int scope_set : 1;
958
959 /* Flag set if the DIE has a byte_size attribute. */
960 unsigned int has_byte_size : 1;
961
962 /* Flag set if any of the DIE's children are template arguments. */
963 unsigned int has_template_arguments : 1;
964
965 /* Flag set if fixup_partial_die has been called on this die. */
966 unsigned int fixup_called : 1;
967
968 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
969 unsigned int is_dwz : 1;
970
971 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
972 unsigned int spec_is_dwz : 1;
973
974 /* The name of this DIE. Normally the value of DW_AT_name, but
975 sometimes a default name for unnamed DIEs. */
976 const char *name;
977
978 /* The linkage name, if present. */
979 const char *linkage_name;
980
981 /* The scope to prepend to our children. This is generally
982 allocated on the comp_unit_obstack, so will disappear
983 when this compilation unit leaves the cache. */
984 const char *scope;
985
986 /* Some data associated with the partial DIE. The tag determines
987 which field is live. */
988 union
989 {
990 /* The location description associated with this DIE, if any. */
991 struct dwarf_block *locdesc;
992 /* The offset of an import, for DW_TAG_imported_unit. */
993 sect_offset offset;
994 } d;
995
996 /* If HAS_PC_INFO, the PC range associated with this DIE. */
997 CORE_ADDR lowpc;
998 CORE_ADDR highpc;
999
1000 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1001 DW_AT_sibling, if any. */
1002 /* NOTE: This member isn't strictly necessary, read_partial_die could
1003 return DW_AT_sibling values to its caller load_partial_dies. */
1004 const gdb_byte *sibling;
1005
1006 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1007 DW_AT_specification (or DW_AT_abstract_origin or
1008 DW_AT_extension). */
1009 sect_offset spec_offset;
1010
1011 /* Pointers to this DIE's parent, first child, and next sibling,
1012 if any. */
1013 struct partial_die_info *die_parent, *die_child, *die_sibling;
1014 };
1015
1016 /* This data structure holds the information of an abbrev. */
1017 struct abbrev_info
1018 {
1019 unsigned int number; /* number identifying abbrev */
1020 enum dwarf_tag tag; /* dwarf tag */
1021 unsigned short has_children; /* boolean */
1022 unsigned short num_attrs; /* number of attributes */
1023 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1024 struct abbrev_info *next; /* next in chain */
1025 };
1026
1027 struct attr_abbrev
1028 {
1029 ENUM_BITFIELD(dwarf_attribute) name : 16;
1030 ENUM_BITFIELD(dwarf_form) form : 16;
1031 };
1032
1033 /* Size of abbrev_table.abbrev_hash_table. */
1034 #define ABBREV_HASH_SIZE 121
1035
1036 /* Top level data structure to contain an abbreviation table. */
1037
1038 struct abbrev_table
1039 {
1040 /* Where the abbrev table came from.
1041 This is used as a sanity check when the table is used. */
1042 sect_offset offset;
1043
1044 /* Storage for the abbrev table. */
1045 struct obstack abbrev_obstack;
1046
1047 /* Hash table of abbrevs.
1048 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1049 It could be statically allocated, but the previous code didn't so we
1050 don't either. */
1051 struct abbrev_info **abbrevs;
1052 };
1053
1054 /* Attributes have a name and a value. */
1055 struct attribute
1056 {
1057 ENUM_BITFIELD(dwarf_attribute) name : 16;
1058 ENUM_BITFIELD(dwarf_form) form : 15;
1059
1060 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1061 field should be in u.str (existing only for DW_STRING) but it is kept
1062 here for better struct attribute alignment. */
1063 unsigned int string_is_canonical : 1;
1064
1065 union
1066 {
1067 const char *str;
1068 struct dwarf_block *blk;
1069 ULONGEST unsnd;
1070 LONGEST snd;
1071 CORE_ADDR addr;
1072 ULONGEST signature;
1073 }
1074 u;
1075 };
1076
1077 /* This data structure holds a complete die structure. */
1078 struct die_info
1079 {
1080 /* DWARF-2 tag for this DIE. */
1081 ENUM_BITFIELD(dwarf_tag) tag : 16;
1082
1083 /* Number of attributes */
1084 unsigned char num_attrs;
1085
1086 /* True if we're presently building the full type name for the
1087 type derived from this DIE. */
1088 unsigned char building_fullname : 1;
1089
1090 /* Abbrev number */
1091 unsigned int abbrev;
1092
1093 /* Offset in .debug_info or .debug_types section. */
1094 sect_offset offset;
1095
1096 /* The dies in a compilation unit form an n-ary tree. PARENT
1097 points to this die's parent; CHILD points to the first child of
1098 this node; and all the children of a given node are chained
1099 together via their SIBLING fields. */
1100 struct die_info *child; /* Its first child, if any. */
1101 struct die_info *sibling; /* Its next sibling, if any. */
1102 struct die_info *parent; /* Its parent, if any. */
1103
1104 /* An array of attributes, with NUM_ATTRS elements. There may be
1105 zero, but it's not common and zero-sized arrays are not
1106 sufficiently portable C. */
1107 struct attribute attrs[1];
1108 };
1109
1110 /* Get at parts of an attribute structure. */
1111
1112 #define DW_STRING(attr) ((attr)->u.str)
1113 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
1114 #define DW_UNSND(attr) ((attr)->u.unsnd)
1115 #define DW_BLOCK(attr) ((attr)->u.blk)
1116 #define DW_SND(attr) ((attr)->u.snd)
1117 #define DW_ADDR(attr) ((attr)->u.addr)
1118 #define DW_SIGNATURE(attr) ((attr)->u.signature)
1119
1120 /* Blocks are a bunch of untyped bytes. */
1121 struct dwarf_block
1122 {
1123 size_t size;
1124
1125 /* Valid only if SIZE is not zero. */
1126 const gdb_byte *data;
1127 };
1128
1129 #ifndef ATTR_ALLOC_CHUNK
1130 #define ATTR_ALLOC_CHUNK 4
1131 #endif
1132
1133 /* Allocate fields for structs, unions and enums in this size. */
1134 #ifndef DW_FIELD_ALLOC_CHUNK
1135 #define DW_FIELD_ALLOC_CHUNK 4
1136 #endif
1137
1138 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1139 but this would require a corresponding change in unpack_field_as_long
1140 and friends. */
1141 static int bits_per_byte = 8;
1142
1143 /* The routines that read and process dies for a C struct or C++ class
1144 pass lists of data member fields and lists of member function fields
1145 in an instance of a field_info structure, as defined below. */
1146 struct field_info
1147 {
1148 /* List of data member and baseclasses fields. */
1149 struct nextfield
1150 {
1151 struct nextfield *next;
1152 int accessibility;
1153 int virtuality;
1154 struct field field;
1155 }
1156 *fields, *baseclasses;
1157
1158 /* Number of fields (including baseclasses). */
1159 int nfields;
1160
1161 /* Number of baseclasses. */
1162 int nbaseclasses;
1163
1164 /* Set if the accesibility of one of the fields is not public. */
1165 int non_public_fields;
1166
1167 /* Member function fields array, entries are allocated in the order they
1168 are encountered in the object file. */
1169 struct nextfnfield
1170 {
1171 struct nextfnfield *next;
1172 struct fn_field fnfield;
1173 }
1174 *fnfields;
1175
1176 /* Member function fieldlist array, contains name of possibly overloaded
1177 member function, number of overloaded member functions and a pointer
1178 to the head of the member function field chain. */
1179 struct fnfieldlist
1180 {
1181 const char *name;
1182 int length;
1183 struct nextfnfield *head;
1184 }
1185 *fnfieldlists;
1186
1187 /* Number of entries in the fnfieldlists array. */
1188 int nfnfields;
1189
1190 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1191 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1192 struct typedef_field_list
1193 {
1194 struct typedef_field field;
1195 struct typedef_field_list *next;
1196 }
1197 *typedef_field_list;
1198 unsigned typedef_field_list_count;
1199 };
1200
1201 /* One item on the queue of compilation units to read in full symbols
1202 for. */
1203 struct dwarf2_queue_item
1204 {
1205 struct dwarf2_per_cu_data *per_cu;
1206 enum language pretend_language;
1207 struct dwarf2_queue_item *next;
1208 };
1209
1210 /* The current queue. */
1211 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1212
1213 /* Loaded secondary compilation units are kept in memory until they
1214 have not been referenced for the processing of this many
1215 compilation units. Set this to zero to disable caching. Cache
1216 sizes of up to at least twenty will improve startup time for
1217 typical inter-CU-reference binaries, at an obvious memory cost. */
1218 static int dwarf2_max_cache_age = 5;
1219 static void
1220 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
1221 struct cmd_list_element *c, const char *value)
1222 {
1223 fprintf_filtered (file, _("The upper bound on the age of cached "
1224 "dwarf2 compilation units is %s.\n"),
1225 value);
1226 }
1227
1228
1229 /* Various complaints about symbol reading that don't abort the process. */
1230
1231 static void
1232 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1233 {
1234 complaint (&symfile_complaints,
1235 _("statement list doesn't fit in .debug_line section"));
1236 }
1237
1238 static void
1239 dwarf2_debug_line_missing_file_complaint (void)
1240 {
1241 complaint (&symfile_complaints,
1242 _(".debug_line section has line data without a file"));
1243 }
1244
1245 static void
1246 dwarf2_debug_line_missing_end_sequence_complaint (void)
1247 {
1248 complaint (&symfile_complaints,
1249 _(".debug_line section has line "
1250 "program sequence without an end"));
1251 }
1252
1253 static void
1254 dwarf2_complex_location_expr_complaint (void)
1255 {
1256 complaint (&symfile_complaints, _("location expression too complex"));
1257 }
1258
1259 static void
1260 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1261 int arg3)
1262 {
1263 complaint (&symfile_complaints,
1264 _("const value length mismatch for '%s', got %d, expected %d"),
1265 arg1, arg2, arg3);
1266 }
1267
1268 static void
1269 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
1270 {
1271 complaint (&symfile_complaints,
1272 _("debug info runs off end of %s section"
1273 " [in module %s]"),
1274 section->asection->name,
1275 bfd_get_filename (section->asection->owner));
1276 }
1277
1278 static void
1279 dwarf2_macro_malformed_definition_complaint (const char *arg1)
1280 {
1281 complaint (&symfile_complaints,
1282 _("macro debug info contains a "
1283 "malformed macro definition:\n`%s'"),
1284 arg1);
1285 }
1286
1287 static void
1288 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1289 {
1290 complaint (&symfile_complaints,
1291 _("invalid attribute class or form for '%s' in '%s'"),
1292 arg1, arg2);
1293 }
1294
1295 /* local function prototypes */
1296
1297 static void dwarf2_locate_sections (bfd *, asection *, void *);
1298
1299 static void dwarf2_find_base_address (struct die_info *die,
1300 struct dwarf2_cu *cu);
1301
1302 static struct partial_symtab *create_partial_symtab
1303 (struct dwarf2_per_cu_data *per_cu, const char *name);
1304
1305 static void dwarf2_build_psymtabs_hard (struct objfile *);
1306
1307 static void scan_partial_symbols (struct partial_die_info *,
1308 CORE_ADDR *, CORE_ADDR *,
1309 int, struct dwarf2_cu *);
1310
1311 static void add_partial_symbol (struct partial_die_info *,
1312 struct dwarf2_cu *);
1313
1314 static void add_partial_namespace (struct partial_die_info *pdi,
1315 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1316 int need_pc, struct dwarf2_cu *cu);
1317
1318 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1319 CORE_ADDR *highpc, int need_pc,
1320 struct dwarf2_cu *cu);
1321
1322 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1323 struct dwarf2_cu *cu);
1324
1325 static void add_partial_subprogram (struct partial_die_info *pdi,
1326 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1327 int need_pc, struct dwarf2_cu *cu);
1328
1329 static void dwarf2_read_symtab (struct partial_symtab *,
1330 struct objfile *);
1331
1332 static void psymtab_to_symtab_1 (struct partial_symtab *);
1333
1334 static struct abbrev_info *abbrev_table_lookup_abbrev
1335 (const struct abbrev_table *, unsigned int);
1336
1337 static struct abbrev_table *abbrev_table_read_table
1338 (struct dwarf2_section_info *, sect_offset);
1339
1340 static void abbrev_table_free (struct abbrev_table *);
1341
1342 static void abbrev_table_free_cleanup (void *);
1343
1344 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
1345 struct dwarf2_section_info *);
1346
1347 static void dwarf2_free_abbrev_table (void *);
1348
1349 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1350
1351 static struct partial_die_info *load_partial_dies
1352 (const struct die_reader_specs *, const gdb_byte *, int);
1353
1354 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
1355 struct partial_die_info *,
1356 struct abbrev_info *,
1357 unsigned int,
1358 const gdb_byte *);
1359
1360 static struct partial_die_info *find_partial_die (sect_offset, int,
1361 struct dwarf2_cu *);
1362
1363 static void fixup_partial_die (struct partial_die_info *,
1364 struct dwarf2_cu *);
1365
1366 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1367 struct attribute *, struct attr_abbrev *,
1368 const gdb_byte *);
1369
1370 static unsigned int read_1_byte (bfd *, const gdb_byte *);
1371
1372 static int read_1_signed_byte (bfd *, const gdb_byte *);
1373
1374 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
1375
1376 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
1377
1378 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
1379
1380 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
1381 unsigned int *);
1382
1383 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
1384
1385 static LONGEST read_checked_initial_length_and_offset
1386 (bfd *, const gdb_byte *, const struct comp_unit_head *,
1387 unsigned int *, unsigned int *);
1388
1389 static LONGEST read_offset (bfd *, const gdb_byte *,
1390 const struct comp_unit_head *,
1391 unsigned int *);
1392
1393 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
1394
1395 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
1396 sect_offset);
1397
1398 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
1399
1400 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
1401
1402 static const char *read_indirect_string (bfd *, const gdb_byte *,
1403 const struct comp_unit_head *,
1404 unsigned int *);
1405
1406 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
1407
1408 static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
1409
1410 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
1411
1412 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1413 const gdb_byte *,
1414 unsigned int *);
1415
1416 static const char *read_str_index (const struct die_reader_specs *reader,
1417 struct dwarf2_cu *cu, ULONGEST str_index);
1418
1419 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1420
1421 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1422 struct dwarf2_cu *);
1423
1424 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1425 unsigned int);
1426
1427 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1428 struct dwarf2_cu *cu);
1429
1430 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1431
1432 static struct die_info *die_specification (struct die_info *die,
1433 struct dwarf2_cu **);
1434
1435 static void free_line_header (struct line_header *lh);
1436
1437 static struct line_header *dwarf_decode_line_header (unsigned int offset,
1438 struct dwarf2_cu *cu);
1439
1440 static void dwarf_decode_lines (struct line_header *, const char *,
1441 struct dwarf2_cu *, struct partial_symtab *,
1442 int);
1443
1444 static void dwarf2_start_subfile (const char *, const char *, const char *);
1445
1446 static void dwarf2_start_symtab (struct dwarf2_cu *,
1447 const char *, const char *, CORE_ADDR);
1448
1449 static struct symbol *new_symbol (struct die_info *, struct type *,
1450 struct dwarf2_cu *);
1451
1452 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1453 struct dwarf2_cu *, struct symbol *);
1454
1455 static void dwarf2_const_value (struct attribute *, struct symbol *,
1456 struct dwarf2_cu *);
1457
1458 static void dwarf2_const_value_attr (struct attribute *attr,
1459 struct type *type,
1460 const char *name,
1461 struct obstack *obstack,
1462 struct dwarf2_cu *cu, LONGEST *value,
1463 const gdb_byte **bytes,
1464 struct dwarf2_locexpr_baton **baton);
1465
1466 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1467
1468 static int need_gnat_info (struct dwarf2_cu *);
1469
1470 static struct type *die_descriptive_type (struct die_info *,
1471 struct dwarf2_cu *);
1472
1473 static void set_descriptive_type (struct type *, struct die_info *,
1474 struct dwarf2_cu *);
1475
1476 static struct type *die_containing_type (struct die_info *,
1477 struct dwarf2_cu *);
1478
1479 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1480 struct dwarf2_cu *);
1481
1482 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1483
1484 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1485
1486 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1487
1488 static char *typename_concat (struct obstack *obs, const char *prefix,
1489 const char *suffix, int physname,
1490 struct dwarf2_cu *cu);
1491
1492 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1493
1494 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1495
1496 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1497
1498 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1499
1500 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1501
1502 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1503 struct dwarf2_cu *, struct partial_symtab *);
1504
1505 static int dwarf2_get_pc_bounds (struct die_info *,
1506 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1507 struct partial_symtab *);
1508
1509 static void get_scope_pc_bounds (struct die_info *,
1510 CORE_ADDR *, CORE_ADDR *,
1511 struct dwarf2_cu *);
1512
1513 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1514 CORE_ADDR, struct dwarf2_cu *);
1515
1516 static void dwarf2_add_field (struct field_info *, struct die_info *,
1517 struct dwarf2_cu *);
1518
1519 static void dwarf2_attach_fields_to_type (struct field_info *,
1520 struct type *, struct dwarf2_cu *);
1521
1522 static void dwarf2_add_member_fn (struct field_info *,
1523 struct die_info *, struct type *,
1524 struct dwarf2_cu *);
1525
1526 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1527 struct type *,
1528 struct dwarf2_cu *);
1529
1530 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1531
1532 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1533
1534 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1535
1536 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1537
1538 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1539
1540 static struct type *read_module_type (struct die_info *die,
1541 struct dwarf2_cu *cu);
1542
1543 static const char *namespace_name (struct die_info *die,
1544 int *is_anonymous, struct dwarf2_cu *);
1545
1546 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1547
1548 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1549
1550 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1551 struct dwarf2_cu *);
1552
1553 static struct die_info *read_die_and_siblings_1
1554 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1555 struct die_info *);
1556
1557 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1558 const gdb_byte *info_ptr,
1559 const gdb_byte **new_info_ptr,
1560 struct die_info *parent);
1561
1562 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1563 struct die_info **, const gdb_byte *,
1564 int *, int);
1565
1566 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1567 struct die_info **, const gdb_byte *,
1568 int *);
1569
1570 static void process_die (struct die_info *, struct dwarf2_cu *);
1571
1572 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1573 struct obstack *);
1574
1575 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1576
1577 static const char *dwarf2_full_name (const char *name,
1578 struct die_info *die,
1579 struct dwarf2_cu *cu);
1580
1581 static const char *dwarf2_physname (const char *name, struct die_info *die,
1582 struct dwarf2_cu *cu);
1583
1584 static struct die_info *dwarf2_extension (struct die_info *die,
1585 struct dwarf2_cu **);
1586
1587 static const char *dwarf_tag_name (unsigned int);
1588
1589 static const char *dwarf_attr_name (unsigned int);
1590
1591 static const char *dwarf_form_name (unsigned int);
1592
1593 static char *dwarf_bool_name (unsigned int);
1594
1595 static const char *dwarf_type_encoding_name (unsigned int);
1596
1597 static struct die_info *sibling_die (struct die_info *);
1598
1599 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1600
1601 static void dump_die_for_error (struct die_info *);
1602
1603 static void dump_die_1 (struct ui_file *, int level, int max_level,
1604 struct die_info *);
1605
1606 /*static*/ void dump_die (struct die_info *, int max_level);
1607
1608 static void store_in_ref_table (struct die_info *,
1609 struct dwarf2_cu *);
1610
1611 static int is_ref_attr (struct attribute *);
1612
1613 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1614
1615 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1616
1617 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1618 struct attribute *,
1619 struct dwarf2_cu **);
1620
1621 static struct die_info *follow_die_ref (struct die_info *,
1622 struct attribute *,
1623 struct dwarf2_cu **);
1624
1625 static struct die_info *follow_die_sig (struct die_info *,
1626 struct attribute *,
1627 struct dwarf2_cu **);
1628
1629 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1630 struct dwarf2_cu *);
1631
1632 static struct type *get_DW_AT_signature_type (struct die_info *,
1633 struct attribute *,
1634 struct dwarf2_cu *);
1635
1636 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1637
1638 static void read_signatured_type (struct signatured_type *);
1639
1640 static struct type_unit_group *get_type_unit_group
1641 (struct dwarf2_cu *, struct attribute *);
1642
1643 static void build_type_unit_groups (die_reader_func_ftype *, void *);
1644
1645 /* memory allocation interface */
1646
1647 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1648
1649 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1650
1651 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
1652 const char *, int);
1653
1654 static int attr_form_is_block (struct attribute *);
1655
1656 static int attr_form_is_section_offset (struct attribute *);
1657
1658 static int attr_form_is_constant (struct attribute *);
1659
1660 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1661 struct dwarf2_loclist_baton *baton,
1662 struct attribute *attr);
1663
1664 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1665 struct symbol *sym,
1666 struct dwarf2_cu *cu,
1667 int is_block);
1668
1669 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1670 const gdb_byte *info_ptr,
1671 struct abbrev_info *abbrev);
1672
1673 static void free_stack_comp_unit (void *);
1674
1675 static hashval_t partial_die_hash (const void *item);
1676
1677 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1678
1679 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1680 (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
1681
1682 static void init_one_comp_unit (struct dwarf2_cu *cu,
1683 struct dwarf2_per_cu_data *per_cu);
1684
1685 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1686 struct die_info *comp_unit_die,
1687 enum language pretend_language);
1688
1689 static void free_heap_comp_unit (void *);
1690
1691 static void free_cached_comp_units (void *);
1692
1693 static void age_cached_comp_units (void);
1694
1695 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1696
1697 static struct type *set_die_type (struct die_info *, struct type *,
1698 struct dwarf2_cu *);
1699
1700 static void create_all_comp_units (struct objfile *);
1701
1702 static int create_all_type_units (struct objfile *);
1703
1704 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1705 enum language);
1706
1707 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1708 enum language);
1709
1710 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1711 enum language);
1712
1713 static void dwarf2_add_dependence (struct dwarf2_cu *,
1714 struct dwarf2_per_cu_data *);
1715
1716 static void dwarf2_mark (struct dwarf2_cu *);
1717
1718 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1719
1720 static struct type *get_die_type_at_offset (sect_offset,
1721 struct dwarf2_per_cu_data *);
1722
1723 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1724
1725 static void dwarf2_release_queue (void *dummy);
1726
1727 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1728 enum language pretend_language);
1729
1730 static int maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
1731 struct dwarf2_per_cu_data *per_cu,
1732 enum language pretend_language);
1733
1734 static void process_queue (void);
1735
1736 static void find_file_and_directory (struct die_info *die,
1737 struct dwarf2_cu *cu,
1738 const char **name, const char **comp_dir);
1739
1740 static char *file_full_name (int file, struct line_header *lh,
1741 const char *comp_dir);
1742
1743 static const gdb_byte *read_and_check_comp_unit_head
1744 (struct comp_unit_head *header,
1745 struct dwarf2_section_info *section,
1746 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
1747 int is_debug_types_section);
1748
1749 static void init_cutu_and_read_dies
1750 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
1751 int use_existing_cu, int keep,
1752 die_reader_func_ftype *die_reader_func, void *data);
1753
1754 static void init_cutu_and_read_dies_simple
1755 (struct dwarf2_per_cu_data *this_cu,
1756 die_reader_func_ftype *die_reader_func, void *data);
1757
1758 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1759
1760 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1761
1762 static struct dwo_unit *lookup_dwo_in_dwp
1763 (struct dwp_file *dwp_file, const struct dwp_hash_table *htab,
1764 const char *comp_dir, ULONGEST signature, int is_debug_types);
1765
1766 static struct dwp_file *get_dwp_file (void);
1767
1768 static struct dwo_unit *lookup_dwo_comp_unit
1769 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1770
1771 static struct dwo_unit *lookup_dwo_type_unit
1772 (struct signatured_type *, const char *, const char *);
1773
1774 static void free_dwo_file_cleanup (void *);
1775
1776 static void process_cu_includes (void);
1777
1778 static void check_producer (struct dwarf2_cu *cu);
1779
1780 #if WORDS_BIGENDIAN
1781
1782 /* Convert VALUE between big- and little-endian. */
1783 static offset_type
1784 byte_swap (offset_type value)
1785 {
1786 offset_type result;
1787
1788 result = (value & 0xff) << 24;
1789 result |= (value & 0xff00) << 8;
1790 result |= (value & 0xff0000) >> 8;
1791 result |= (value & 0xff000000) >> 24;
1792 return result;
1793 }
1794
1795 #define MAYBE_SWAP(V) byte_swap (V)
1796
1797 #else
1798 #define MAYBE_SWAP(V) (V)
1799 #endif /* WORDS_BIGENDIAN */
1800
1801 /* The suffix for an index file. */
1802 #define INDEX_SUFFIX ".gdb-index"
1803
1804 /* Try to locate the sections we need for DWARF 2 debugging
1805 information and return true if we have enough to do something.
1806 NAMES points to the dwarf2 section names, or is NULL if the standard
1807 ELF names are used. */
1808
1809 int
1810 dwarf2_has_info (struct objfile *objfile,
1811 const struct dwarf2_debug_sections *names)
1812 {
1813 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1814 if (!dwarf2_per_objfile)
1815 {
1816 /* Initialize per-objfile state. */
1817 struct dwarf2_per_objfile *data
1818 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1819
1820 memset (data, 0, sizeof (*data));
1821 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1822 dwarf2_per_objfile = data;
1823
1824 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1825 (void *) names);
1826 dwarf2_per_objfile->objfile = objfile;
1827 }
1828 return (dwarf2_per_objfile->info.asection != NULL
1829 && dwarf2_per_objfile->abbrev.asection != NULL);
1830 }
1831
1832 /* When loading sections, we look either for uncompressed section or for
1833 compressed section names. */
1834
1835 static int
1836 section_is_p (const char *section_name,
1837 const struct dwarf2_section_names *names)
1838 {
1839 if (names->normal != NULL
1840 && strcmp (section_name, names->normal) == 0)
1841 return 1;
1842 if (names->compressed != NULL
1843 && strcmp (section_name, names->compressed) == 0)
1844 return 1;
1845 return 0;
1846 }
1847
1848 /* This function is mapped across the sections and remembers the
1849 offset and size of each of the debugging sections we are interested
1850 in. */
1851
1852 static void
1853 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1854 {
1855 const struct dwarf2_debug_sections *names;
1856 flagword aflag = bfd_get_section_flags (abfd, sectp);
1857
1858 if (vnames == NULL)
1859 names = &dwarf2_elf_names;
1860 else
1861 names = (const struct dwarf2_debug_sections *) vnames;
1862
1863 if ((aflag & SEC_HAS_CONTENTS) == 0)
1864 {
1865 }
1866 else if (section_is_p (sectp->name, &names->info))
1867 {
1868 dwarf2_per_objfile->info.asection = sectp;
1869 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1870 }
1871 else if (section_is_p (sectp->name, &names->abbrev))
1872 {
1873 dwarf2_per_objfile->abbrev.asection = sectp;
1874 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1875 }
1876 else if (section_is_p (sectp->name, &names->line))
1877 {
1878 dwarf2_per_objfile->line.asection = sectp;
1879 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1880 }
1881 else if (section_is_p (sectp->name, &names->loc))
1882 {
1883 dwarf2_per_objfile->loc.asection = sectp;
1884 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1885 }
1886 else if (section_is_p (sectp->name, &names->macinfo))
1887 {
1888 dwarf2_per_objfile->macinfo.asection = sectp;
1889 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1890 }
1891 else if (section_is_p (sectp->name, &names->macro))
1892 {
1893 dwarf2_per_objfile->macro.asection = sectp;
1894 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1895 }
1896 else if (section_is_p (sectp->name, &names->str))
1897 {
1898 dwarf2_per_objfile->str.asection = sectp;
1899 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1900 }
1901 else if (section_is_p (sectp->name, &names->addr))
1902 {
1903 dwarf2_per_objfile->addr.asection = sectp;
1904 dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
1905 }
1906 else if (section_is_p (sectp->name, &names->frame))
1907 {
1908 dwarf2_per_objfile->frame.asection = sectp;
1909 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1910 }
1911 else if (section_is_p (sectp->name, &names->eh_frame))
1912 {
1913 dwarf2_per_objfile->eh_frame.asection = sectp;
1914 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1915 }
1916 else if (section_is_p (sectp->name, &names->ranges))
1917 {
1918 dwarf2_per_objfile->ranges.asection = sectp;
1919 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1920 }
1921 else if (section_is_p (sectp->name, &names->types))
1922 {
1923 struct dwarf2_section_info type_section;
1924
1925 memset (&type_section, 0, sizeof (type_section));
1926 type_section.asection = sectp;
1927 type_section.size = bfd_get_section_size (sectp);
1928
1929 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1930 &type_section);
1931 }
1932 else if (section_is_p (sectp->name, &names->gdb_index))
1933 {
1934 dwarf2_per_objfile->gdb_index.asection = sectp;
1935 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1936 }
1937
1938 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1939 && bfd_section_vma (abfd, sectp) == 0)
1940 dwarf2_per_objfile->has_section_at_zero = 1;
1941 }
1942
1943 /* A helper function that decides whether a section is empty,
1944 or not present. */
1945
1946 static int
1947 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1948 {
1949 return info->asection == NULL || info->size == 0;
1950 }
1951
1952 /* Read the contents of the section INFO.
1953 OBJFILE is the main object file, but not necessarily the file where
1954 the section comes from. E.g., for DWO files INFO->asection->owner
1955 is the bfd of the DWO file.
1956 If the section is compressed, uncompress it before returning. */
1957
1958 static void
1959 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1960 {
1961 asection *sectp = info->asection;
1962 bfd *abfd;
1963 gdb_byte *buf, *retbuf;
1964 unsigned char header[4];
1965
1966 if (info->readin)
1967 return;
1968 info->buffer = NULL;
1969 info->readin = 1;
1970
1971 if (dwarf2_section_empty_p (info))
1972 return;
1973
1974 abfd = sectp->owner;
1975
1976 /* If the section has relocations, we must read it ourselves.
1977 Otherwise we attach it to the BFD. */
1978 if ((sectp->flags & SEC_RELOC) == 0)
1979 {
1980 info->buffer = gdb_bfd_map_section (sectp, &info->size);
1981 return;
1982 }
1983
1984 buf = obstack_alloc (&objfile->objfile_obstack, info->size);
1985 info->buffer = buf;
1986
1987 /* When debugging .o files, we may need to apply relocations; see
1988 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1989 We never compress sections in .o files, so we only need to
1990 try this when the section is not compressed. */
1991 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1992 if (retbuf != NULL)
1993 {
1994 info->buffer = retbuf;
1995 return;
1996 }
1997
1998 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1999 || bfd_bread (buf, info->size, abfd) != info->size)
2000 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
2001 bfd_get_filename (abfd));
2002 }
2003
2004 /* A helper function that returns the size of a section in a safe way.
2005 If you are positive that the section has been read before using the
2006 size, then it is safe to refer to the dwarf2_section_info object's
2007 "size" field directly. In other cases, you must call this
2008 function, because for compressed sections the size field is not set
2009 correctly until the section has been read. */
2010
2011 static bfd_size_type
2012 dwarf2_section_size (struct objfile *objfile,
2013 struct dwarf2_section_info *info)
2014 {
2015 if (!info->readin)
2016 dwarf2_read_section (objfile, info);
2017 return info->size;
2018 }
2019
2020 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2021 SECTION_NAME. */
2022
2023 void
2024 dwarf2_get_section_info (struct objfile *objfile,
2025 enum dwarf2_section_enum sect,
2026 asection **sectp, const gdb_byte **bufp,
2027 bfd_size_type *sizep)
2028 {
2029 struct dwarf2_per_objfile *data
2030 = objfile_data (objfile, dwarf2_objfile_data_key);
2031 struct dwarf2_section_info *info;
2032
2033 /* We may see an objfile without any DWARF, in which case we just
2034 return nothing. */
2035 if (data == NULL)
2036 {
2037 *sectp = NULL;
2038 *bufp = NULL;
2039 *sizep = 0;
2040 return;
2041 }
2042 switch (sect)
2043 {
2044 case DWARF2_DEBUG_FRAME:
2045 info = &data->frame;
2046 break;
2047 case DWARF2_EH_FRAME:
2048 info = &data->eh_frame;
2049 break;
2050 default:
2051 gdb_assert_not_reached ("unexpected section");
2052 }
2053
2054 dwarf2_read_section (objfile, info);
2055
2056 *sectp = info->asection;
2057 *bufp = info->buffer;
2058 *sizep = info->size;
2059 }
2060
2061 /* A helper function to find the sections for a .dwz file. */
2062
2063 static void
2064 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2065 {
2066 struct dwz_file *dwz_file = arg;
2067
2068 /* Note that we only support the standard ELF names, because .dwz
2069 is ELF-only (at the time of writing). */
2070 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2071 {
2072 dwz_file->abbrev.asection = sectp;
2073 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2074 }
2075 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2076 {
2077 dwz_file->info.asection = sectp;
2078 dwz_file->info.size = bfd_get_section_size (sectp);
2079 }
2080 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2081 {
2082 dwz_file->str.asection = sectp;
2083 dwz_file->str.size = bfd_get_section_size (sectp);
2084 }
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2086 {
2087 dwz_file->line.asection = sectp;
2088 dwz_file->line.size = bfd_get_section_size (sectp);
2089 }
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2091 {
2092 dwz_file->macro.asection = sectp;
2093 dwz_file->macro.size = bfd_get_section_size (sectp);
2094 }
2095 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2096 {
2097 dwz_file->gdb_index.asection = sectp;
2098 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2099 }
2100 }
2101
2102 /* Open the separate '.dwz' debug file, if needed. Error if the file
2103 cannot be found. */
2104
2105 static struct dwz_file *
2106 dwarf2_get_dwz_file (void)
2107 {
2108 bfd *abfd, *dwz_bfd;
2109 asection *section;
2110 gdb_byte *data;
2111 struct cleanup *cleanup;
2112 const char *filename;
2113 struct dwz_file *result;
2114
2115 if (dwarf2_per_objfile->dwz_file != NULL)
2116 return dwarf2_per_objfile->dwz_file;
2117
2118 abfd = dwarf2_per_objfile->objfile->obfd;
2119 section = bfd_get_section_by_name (abfd, ".gnu_debugaltlink");
2120 if (section == NULL)
2121 error (_("could not find '.gnu_debugaltlink' section"));
2122 if (!bfd_malloc_and_get_section (abfd, section, &data))
2123 error (_("could not read '.gnu_debugaltlink' section: %s"),
2124 bfd_errmsg (bfd_get_error ()));
2125 cleanup = make_cleanup (xfree, data);
2126
2127 filename = (const char *) data;
2128 if (!IS_ABSOLUTE_PATH (filename))
2129 {
2130 char *abs = gdb_realpath (dwarf2_per_objfile->objfile->name);
2131 char *rel;
2132
2133 make_cleanup (xfree, abs);
2134 abs = ldirname (abs);
2135 make_cleanup (xfree, abs);
2136
2137 rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
2138 make_cleanup (xfree, rel);
2139 filename = rel;
2140 }
2141
2142 /* The format is just a NUL-terminated file name, followed by the
2143 build-id. For now, though, we ignore the build-id. */
2144 dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
2145 if (dwz_bfd == NULL)
2146 error (_("could not read '%s': %s"), filename,
2147 bfd_errmsg (bfd_get_error ()));
2148
2149 if (!bfd_check_format (dwz_bfd, bfd_object))
2150 {
2151 gdb_bfd_unref (dwz_bfd);
2152 error (_("file '%s' was not usable: %s"), filename,
2153 bfd_errmsg (bfd_get_error ()));
2154 }
2155
2156 result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
2157 struct dwz_file);
2158 result->dwz_bfd = dwz_bfd;
2159
2160 bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
2161
2162 do_cleanups (cleanup);
2163
2164 dwarf2_per_objfile->dwz_file = result;
2165 return result;
2166 }
2167 \f
2168 /* DWARF quick_symbols_functions support. */
2169
2170 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2171 unique line tables, so we maintain a separate table of all .debug_line
2172 derived entries to support the sharing.
2173 All the quick functions need is the list of file names. We discard the
2174 line_header when we're done and don't need to record it here. */
2175 struct quick_file_names
2176 {
2177 /* The data used to construct the hash key. */
2178 struct stmt_list_hash hash;
2179
2180 /* The number of entries in file_names, real_names. */
2181 unsigned int num_file_names;
2182
2183 /* The file names from the line table, after being run through
2184 file_full_name. */
2185 const char **file_names;
2186
2187 /* The file names from the line table after being run through
2188 gdb_realpath. These are computed lazily. */
2189 const char **real_names;
2190 };
2191
2192 /* When using the index (and thus not using psymtabs), each CU has an
2193 object of this type. This is used to hold information needed by
2194 the various "quick" methods. */
2195 struct dwarf2_per_cu_quick_data
2196 {
2197 /* The file table. This can be NULL if there was no file table
2198 or it's currently not read in.
2199 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2200 struct quick_file_names *file_names;
2201
2202 /* The corresponding symbol table. This is NULL if symbols for this
2203 CU have not yet been read. */
2204 struct symtab *symtab;
2205
2206 /* A temporary mark bit used when iterating over all CUs in
2207 expand_symtabs_matching. */
2208 unsigned int mark : 1;
2209
2210 /* True if we've tried to read the file table and found there isn't one.
2211 There will be no point in trying to read it again next time. */
2212 unsigned int no_file_data : 1;
2213 };
2214
2215 /* Utility hash function for a stmt_list_hash. */
2216
2217 static hashval_t
2218 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2219 {
2220 hashval_t v = 0;
2221
2222 if (stmt_list_hash->dwo_unit != NULL)
2223 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2224 v += stmt_list_hash->line_offset.sect_off;
2225 return v;
2226 }
2227
2228 /* Utility equality function for a stmt_list_hash. */
2229
2230 static int
2231 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2232 const struct stmt_list_hash *rhs)
2233 {
2234 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2235 return 0;
2236 if (lhs->dwo_unit != NULL
2237 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2238 return 0;
2239
2240 return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
2241 }
2242
2243 /* Hash function for a quick_file_names. */
2244
2245 static hashval_t
2246 hash_file_name_entry (const void *e)
2247 {
2248 const struct quick_file_names *file_data = e;
2249
2250 return hash_stmt_list_entry (&file_data->hash);
2251 }
2252
2253 /* Equality function for a quick_file_names. */
2254
2255 static int
2256 eq_file_name_entry (const void *a, const void *b)
2257 {
2258 const struct quick_file_names *ea = a;
2259 const struct quick_file_names *eb = b;
2260
2261 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2262 }
2263
2264 /* Delete function for a quick_file_names. */
2265
2266 static void
2267 delete_file_name_entry (void *e)
2268 {
2269 struct quick_file_names *file_data = e;
2270 int i;
2271
2272 for (i = 0; i < file_data->num_file_names; ++i)
2273 {
2274 xfree ((void*) file_data->file_names[i]);
2275 if (file_data->real_names)
2276 xfree ((void*) file_data->real_names[i]);
2277 }
2278
2279 /* The space for the struct itself lives on objfile_obstack,
2280 so we don't free it here. */
2281 }
2282
2283 /* Create a quick_file_names hash table. */
2284
2285 static htab_t
2286 create_quick_file_names_table (unsigned int nr_initial_entries)
2287 {
2288 return htab_create_alloc (nr_initial_entries,
2289 hash_file_name_entry, eq_file_name_entry,
2290 delete_file_name_entry, xcalloc, xfree);
2291 }
2292
2293 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2294 have to be created afterwards. You should call age_cached_comp_units after
2295 processing PER_CU->CU. dw2_setup must have been already called. */
2296
2297 static void
2298 load_cu (struct dwarf2_per_cu_data *per_cu)
2299 {
2300 if (per_cu->is_debug_types)
2301 load_full_type_unit (per_cu);
2302 else
2303 load_full_comp_unit (per_cu, language_minimal);
2304
2305 gdb_assert (per_cu->cu != NULL);
2306
2307 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2308 }
2309
2310 /* Read in the symbols for PER_CU. */
2311
2312 static void
2313 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2314 {
2315 struct cleanup *back_to;
2316
2317 /* Skip type_unit_groups, reading the type units they contain
2318 is handled elsewhere. */
2319 if (IS_TYPE_UNIT_GROUP (per_cu))
2320 return;
2321
2322 back_to = make_cleanup (dwarf2_release_queue, NULL);
2323
2324 if (dwarf2_per_objfile->using_index
2325 ? per_cu->v.quick->symtab == NULL
2326 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2327 {
2328 queue_comp_unit (per_cu, language_minimal);
2329 load_cu (per_cu);
2330 }
2331
2332 process_queue ();
2333
2334 /* Age the cache, releasing compilation units that have not
2335 been used recently. */
2336 age_cached_comp_units ();
2337
2338 do_cleanups (back_to);
2339 }
2340
2341 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2342 the objfile from which this CU came. Returns the resulting symbol
2343 table. */
2344
2345 static struct symtab *
2346 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
2347 {
2348 gdb_assert (dwarf2_per_objfile->using_index);
2349 if (!per_cu->v.quick->symtab)
2350 {
2351 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
2352 increment_reading_symtab ();
2353 dw2_do_instantiate_symtab (per_cu);
2354 process_cu_includes ();
2355 do_cleanups (back_to);
2356 }
2357 return per_cu->v.quick->symtab;
2358 }
2359
2360 /* Return the CU given its index.
2361
2362 This is intended for loops like:
2363
2364 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2365 + dwarf2_per_objfile->n_type_units); ++i)
2366 {
2367 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2368
2369 ...;
2370 }
2371 */
2372
2373 static struct dwarf2_per_cu_data *
2374 dw2_get_cu (int index)
2375 {
2376 if (index >= dwarf2_per_objfile->n_comp_units)
2377 {
2378 index -= dwarf2_per_objfile->n_comp_units;
2379 gdb_assert (index < dwarf2_per_objfile->n_type_units);
2380 return &dwarf2_per_objfile->all_type_units[index]->per_cu;
2381 }
2382
2383 return dwarf2_per_objfile->all_comp_units[index];
2384 }
2385
2386 /* Return the primary CU given its index.
2387 The difference between this function and dw2_get_cu is in the handling
2388 of type units (TUs). Here we return the type_unit_group object.
2389
2390 This is intended for loops like:
2391
2392 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2393 + dwarf2_per_objfile->n_type_unit_groups); ++i)
2394 {
2395 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
2396
2397 ...;
2398 }
2399 */
2400
2401 static struct dwarf2_per_cu_data *
2402 dw2_get_primary_cu (int index)
2403 {
2404 if (index >= dwarf2_per_objfile->n_comp_units)
2405 {
2406 index -= dwarf2_per_objfile->n_comp_units;
2407 gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
2408 return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
2409 }
2410
2411 return dwarf2_per_objfile->all_comp_units[index];
2412 }
2413
2414 /* A helper for create_cus_from_index that handles a given list of
2415 CUs. */
2416
2417 static void
2418 create_cus_from_index_list (struct objfile *objfile,
2419 const gdb_byte *cu_list, offset_type n_elements,
2420 struct dwarf2_section_info *section,
2421 int is_dwz,
2422 int base_offset)
2423 {
2424 offset_type i;
2425
2426 for (i = 0; i < n_elements; i += 2)
2427 {
2428 struct dwarf2_per_cu_data *the_cu;
2429 ULONGEST offset, length;
2430
2431 gdb_static_assert (sizeof (ULONGEST) >= 8);
2432 offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2433 length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2434 cu_list += 2 * 8;
2435
2436 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2437 struct dwarf2_per_cu_data);
2438 the_cu->offset.sect_off = offset;
2439 the_cu->length = length;
2440 the_cu->objfile = objfile;
2441 the_cu->section = section;
2442 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2443 struct dwarf2_per_cu_quick_data);
2444 the_cu->is_dwz = is_dwz;
2445 dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
2446 }
2447 }
2448
2449 /* Read the CU list from the mapped index, and use it to create all
2450 the CU objects for this objfile. */
2451
2452 static void
2453 create_cus_from_index (struct objfile *objfile,
2454 const gdb_byte *cu_list, offset_type cu_list_elements,
2455 const gdb_byte *dwz_list, offset_type dwz_elements)
2456 {
2457 struct dwz_file *dwz;
2458
2459 dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
2460 dwarf2_per_objfile->all_comp_units
2461 = obstack_alloc (&objfile->objfile_obstack,
2462 dwarf2_per_objfile->n_comp_units
2463 * sizeof (struct dwarf2_per_cu_data *));
2464
2465 create_cus_from_index_list (objfile, cu_list, cu_list_elements,
2466 &dwarf2_per_objfile->info, 0, 0);
2467
2468 if (dwz_elements == 0)
2469 return;
2470
2471 dwz = dwarf2_get_dwz_file ();
2472 create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
2473 cu_list_elements / 2);
2474 }
2475
2476 /* Create the signatured type hash table from the index. */
2477
2478 static void
2479 create_signatured_type_table_from_index (struct objfile *objfile,
2480 struct dwarf2_section_info *section,
2481 const gdb_byte *bytes,
2482 offset_type elements)
2483 {
2484 offset_type i;
2485 htab_t sig_types_hash;
2486
2487 dwarf2_per_objfile->n_type_units = elements / 3;
2488 dwarf2_per_objfile->all_type_units
2489 = xmalloc (dwarf2_per_objfile->n_type_units
2490 * sizeof (struct signatured_type *));
2491
2492 sig_types_hash = allocate_signatured_type_table (objfile);
2493
2494 for (i = 0; i < elements; i += 3)
2495 {
2496 struct signatured_type *sig_type;
2497 ULONGEST offset, type_offset_in_tu, signature;
2498 void **slot;
2499
2500 gdb_static_assert (sizeof (ULONGEST) >= 8);
2501 offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2502 type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
2503 BFD_ENDIAN_LITTLE);
2504 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2505 bytes += 3 * 8;
2506
2507 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2508 struct signatured_type);
2509 sig_type->signature = signature;
2510 sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
2511 sig_type->per_cu.is_debug_types = 1;
2512 sig_type->per_cu.section = section;
2513 sig_type->per_cu.offset.sect_off = offset;
2514 sig_type->per_cu.objfile = objfile;
2515 sig_type->per_cu.v.quick
2516 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2517 struct dwarf2_per_cu_quick_data);
2518
2519 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
2520 *slot = sig_type;
2521
2522 dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
2523 }
2524
2525 dwarf2_per_objfile->signatured_types = sig_types_hash;
2526 }
2527
2528 /* Read the address map data from the mapped index, and use it to
2529 populate the objfile's psymtabs_addrmap. */
2530
2531 static void
2532 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2533 {
2534 const gdb_byte *iter, *end;
2535 struct obstack temp_obstack;
2536 struct addrmap *mutable_map;
2537 struct cleanup *cleanup;
2538 CORE_ADDR baseaddr;
2539
2540 obstack_init (&temp_obstack);
2541 cleanup = make_cleanup_obstack_free (&temp_obstack);
2542 mutable_map = addrmap_create_mutable (&temp_obstack);
2543
2544 iter = index->address_table;
2545 end = iter + index->address_table_size;
2546
2547 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2548
2549 while (iter < end)
2550 {
2551 ULONGEST hi, lo, cu_index;
2552 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2553 iter += 8;
2554 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2555 iter += 8;
2556 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2557 iter += 4;
2558
2559 if (cu_index < dwarf2_per_objfile->n_comp_units)
2560 {
2561 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2562 dw2_get_cu (cu_index));
2563 }
2564 else
2565 {
2566 complaint (&symfile_complaints,
2567 _(".gdb_index address table has invalid CU number %u"),
2568 (unsigned) cu_index);
2569 }
2570 }
2571
2572 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2573 &objfile->objfile_obstack);
2574 do_cleanups (cleanup);
2575 }
2576
2577 /* The hash function for strings in the mapped index. This is the same as
2578 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2579 implementation. This is necessary because the hash function is tied to the
2580 format of the mapped index file. The hash values do not have to match with
2581 SYMBOL_HASH_NEXT.
2582
2583 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2584
2585 static hashval_t
2586 mapped_index_string_hash (int index_version, const void *p)
2587 {
2588 const unsigned char *str = (const unsigned char *) p;
2589 hashval_t r = 0;
2590 unsigned char c;
2591
2592 while ((c = *str++) != 0)
2593 {
2594 if (index_version >= 5)
2595 c = tolower (c);
2596 r = r * 67 + c - 113;
2597 }
2598
2599 return r;
2600 }
2601
2602 /* Find a slot in the mapped index INDEX for the object named NAME.
2603 If NAME is found, set *VEC_OUT to point to the CU vector in the
2604 constant pool and return 1. If NAME cannot be found, return 0. */
2605
2606 static int
2607 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2608 offset_type **vec_out)
2609 {
2610 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2611 offset_type hash;
2612 offset_type slot, step;
2613 int (*cmp) (const char *, const char *);
2614
2615 if (current_language->la_language == language_cplus
2616 || current_language->la_language == language_java
2617 || current_language->la_language == language_fortran)
2618 {
2619 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2620 not contain any. */
2621 const char *paren = strchr (name, '(');
2622
2623 if (paren)
2624 {
2625 char *dup;
2626
2627 dup = xmalloc (paren - name + 1);
2628 memcpy (dup, name, paren - name);
2629 dup[paren - name] = 0;
2630
2631 make_cleanup (xfree, dup);
2632 name = dup;
2633 }
2634 }
2635
2636 /* Index version 4 did not support case insensitive searches. But the
2637 indices for case insensitive languages are built in lowercase, therefore
2638 simulate our NAME being searched is also lowercased. */
2639 hash = mapped_index_string_hash ((index->version == 4
2640 && case_sensitivity == case_sensitive_off
2641 ? 5 : index->version),
2642 name);
2643
2644 slot = hash & (index->symbol_table_slots - 1);
2645 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2646 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2647
2648 for (;;)
2649 {
2650 /* Convert a slot number to an offset into the table. */
2651 offset_type i = 2 * slot;
2652 const char *str;
2653 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2654 {
2655 do_cleanups (back_to);
2656 return 0;
2657 }
2658
2659 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2660 if (!cmp (name, str))
2661 {
2662 *vec_out = (offset_type *) (index->constant_pool
2663 + MAYBE_SWAP (index->symbol_table[i + 1]));
2664 do_cleanups (back_to);
2665 return 1;
2666 }
2667
2668 slot = (slot + step) & (index->symbol_table_slots - 1);
2669 }
2670 }
2671
2672 /* A helper function that reads the .gdb_index from SECTION and fills
2673 in MAP. FILENAME is the name of the file containing the section;
2674 it is used for error reporting. DEPRECATED_OK is nonzero if it is
2675 ok to use deprecated sections.
2676
2677 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2678 out parameters that are filled in with information about the CU and
2679 TU lists in the section.
2680
2681 Returns 1 if all went well, 0 otherwise. */
2682
2683 static int
2684 read_index_from_section (struct objfile *objfile,
2685 const char *filename,
2686 int deprecated_ok,
2687 struct dwarf2_section_info *section,
2688 struct mapped_index *map,
2689 const gdb_byte **cu_list,
2690 offset_type *cu_list_elements,
2691 const gdb_byte **types_list,
2692 offset_type *types_list_elements)
2693 {
2694 const gdb_byte *addr;
2695 offset_type version;
2696 offset_type *metadata;
2697 int i;
2698
2699 if (dwarf2_section_empty_p (section))
2700 return 0;
2701
2702 /* Older elfutils strip versions could keep the section in the main
2703 executable while splitting it for the separate debug info file. */
2704 if ((bfd_get_file_flags (section->asection) & SEC_HAS_CONTENTS) == 0)
2705 return 0;
2706
2707 dwarf2_read_section (objfile, section);
2708
2709 addr = section->buffer;
2710 /* Version check. */
2711 version = MAYBE_SWAP (*(offset_type *) addr);
2712 /* Versions earlier than 3 emitted every copy of a psymbol. This
2713 causes the index to behave very poorly for certain requests. Version 3
2714 contained incomplete addrmap. So, it seems better to just ignore such
2715 indices. */
2716 if (version < 4)
2717 {
2718 static int warning_printed = 0;
2719 if (!warning_printed)
2720 {
2721 warning (_("Skipping obsolete .gdb_index section in %s."),
2722 filename);
2723 warning_printed = 1;
2724 }
2725 return 0;
2726 }
2727 /* Index version 4 uses a different hash function than index version
2728 5 and later.
2729
2730 Versions earlier than 6 did not emit psymbols for inlined
2731 functions. Using these files will cause GDB not to be able to
2732 set breakpoints on inlined functions by name, so we ignore these
2733 indices unless the user has done
2734 "set use-deprecated-index-sections on". */
2735 if (version < 6 && !deprecated_ok)
2736 {
2737 static int warning_printed = 0;
2738 if (!warning_printed)
2739 {
2740 warning (_("\
2741 Skipping deprecated .gdb_index section in %s.\n\
2742 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2743 to use the section anyway."),
2744 filename);
2745 warning_printed = 1;
2746 }
2747 return 0;
2748 }
2749 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2750 of the TU (for symbols coming from TUs). It's just a performance bug, and
2751 we can't distinguish gdb-generated indices from gold-generated ones, so
2752 nothing to do here. */
2753
2754 /* Indexes with higher version than the one supported by GDB may be no
2755 longer backward compatible. */
2756 if (version > 8)
2757 return 0;
2758
2759 map->version = version;
2760 map->total_size = section->size;
2761
2762 metadata = (offset_type *) (addr + sizeof (offset_type));
2763
2764 i = 0;
2765 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2766 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2767 / 8);
2768 ++i;
2769
2770 *types_list = addr + MAYBE_SWAP (metadata[i]);
2771 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2772 - MAYBE_SWAP (metadata[i]))
2773 / 8);
2774 ++i;
2775
2776 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2777 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2778 - MAYBE_SWAP (metadata[i]));
2779 ++i;
2780
2781 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2782 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2783 - MAYBE_SWAP (metadata[i]))
2784 / (2 * sizeof (offset_type)));
2785 ++i;
2786
2787 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2788
2789 return 1;
2790 }
2791
2792
2793 /* Read the index file. If everything went ok, initialize the "quick"
2794 elements of all the CUs and return 1. Otherwise, return 0. */
2795
2796 static int
2797 dwarf2_read_index (struct objfile *objfile)
2798 {
2799 struct mapped_index local_map, *map;
2800 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2801 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2802
2803 if (!read_index_from_section (objfile, objfile->name,
2804 use_deprecated_index_sections,
2805 &dwarf2_per_objfile->gdb_index, &local_map,
2806 &cu_list, &cu_list_elements,
2807 &types_list, &types_list_elements))
2808 return 0;
2809
2810 /* Don't use the index if it's empty. */
2811 if (local_map.symbol_table_slots == 0)
2812 return 0;
2813
2814 /* If there is a .dwz file, read it so we can get its CU list as
2815 well. */
2816 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
2817 {
2818 struct dwz_file *dwz = dwarf2_get_dwz_file ();
2819 struct mapped_index dwz_map;
2820 const gdb_byte *dwz_types_ignore;
2821 offset_type dwz_types_elements_ignore;
2822
2823 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
2824 1,
2825 &dwz->gdb_index, &dwz_map,
2826 &dwz_list, &dwz_list_elements,
2827 &dwz_types_ignore,
2828 &dwz_types_elements_ignore))
2829 {
2830 warning (_("could not read '.gdb_index' section from %s; skipping"),
2831 bfd_get_filename (dwz->dwz_bfd));
2832 return 0;
2833 }
2834 }
2835
2836 create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
2837 dwz_list_elements);
2838
2839 if (types_list_elements)
2840 {
2841 struct dwarf2_section_info *section;
2842
2843 /* We can only handle a single .debug_types when we have an
2844 index. */
2845 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2846 return 0;
2847
2848 section = VEC_index (dwarf2_section_info_def,
2849 dwarf2_per_objfile->types, 0);
2850
2851 create_signatured_type_table_from_index (objfile, section, types_list,
2852 types_list_elements);
2853 }
2854
2855 create_addrmap_from_index (objfile, &local_map);
2856
2857 map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
2858 *map = local_map;
2859
2860 dwarf2_per_objfile->index_table = map;
2861 dwarf2_per_objfile->using_index = 1;
2862 dwarf2_per_objfile->quick_file_names_table =
2863 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2864
2865 return 1;
2866 }
2867
2868 /* A helper for the "quick" functions which sets the global
2869 dwarf2_per_objfile according to OBJFILE. */
2870
2871 static void
2872 dw2_setup (struct objfile *objfile)
2873 {
2874 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2875 gdb_assert (dwarf2_per_objfile);
2876 }
2877
2878 /* die_reader_func for dw2_get_file_names. */
2879
2880 static void
2881 dw2_get_file_names_reader (const struct die_reader_specs *reader,
2882 const gdb_byte *info_ptr,
2883 struct die_info *comp_unit_die,
2884 int has_children,
2885 void *data)
2886 {
2887 struct dwarf2_cu *cu = reader->cu;
2888 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
2889 struct objfile *objfile = dwarf2_per_objfile->objfile;
2890 struct dwarf2_per_cu_data *lh_cu;
2891 struct line_header *lh;
2892 struct attribute *attr;
2893 int i;
2894 const char *name, *comp_dir;
2895 void **slot;
2896 struct quick_file_names *qfn;
2897 unsigned int line_offset;
2898
2899 gdb_assert (! this_cu->is_debug_types);
2900
2901 /* Our callers never want to match partial units -- instead they
2902 will match the enclosing full CU. */
2903 if (comp_unit_die->tag == DW_TAG_partial_unit)
2904 {
2905 this_cu->v.quick->no_file_data = 1;
2906 return;
2907 }
2908
2909 lh_cu = this_cu;
2910 lh = NULL;
2911 slot = NULL;
2912 line_offset = 0;
2913
2914 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
2915 if (attr)
2916 {
2917 struct quick_file_names find_entry;
2918
2919 line_offset = DW_UNSND (attr);
2920
2921 /* We may have already read in this line header (TU line header sharing).
2922 If we have we're done. */
2923 find_entry.hash.dwo_unit = cu->dwo_unit;
2924 find_entry.hash.line_offset.sect_off = line_offset;
2925 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2926 &find_entry, INSERT);
2927 if (*slot != NULL)
2928 {
2929 lh_cu->v.quick->file_names = *slot;
2930 return;
2931 }
2932
2933 lh = dwarf_decode_line_header (line_offset, cu);
2934 }
2935 if (lh == NULL)
2936 {
2937 lh_cu->v.quick->no_file_data = 1;
2938 return;
2939 }
2940
2941 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2942 qfn->hash.dwo_unit = cu->dwo_unit;
2943 qfn->hash.line_offset.sect_off = line_offset;
2944 gdb_assert (slot != NULL);
2945 *slot = qfn;
2946
2947 find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
2948
2949 qfn->num_file_names = lh->num_file_names;
2950 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2951 lh->num_file_names * sizeof (char *));
2952 for (i = 0; i < lh->num_file_names; ++i)
2953 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2954 qfn->real_names = NULL;
2955
2956 free_line_header (lh);
2957
2958 lh_cu->v.quick->file_names = qfn;
2959 }
2960
2961 /* A helper for the "quick" functions which attempts to read the line
2962 table for THIS_CU. */
2963
2964 static struct quick_file_names *
2965 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
2966 {
2967 /* This should never be called for TUs. */
2968 gdb_assert (! this_cu->is_debug_types);
2969 /* Nor type unit groups. */
2970 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
2971
2972 if (this_cu->v.quick->file_names != NULL)
2973 return this_cu->v.quick->file_names;
2974 /* If we know there is no line data, no point in looking again. */
2975 if (this_cu->v.quick->no_file_data)
2976 return NULL;
2977
2978 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
2979
2980 if (this_cu->v.quick->no_file_data)
2981 return NULL;
2982 return this_cu->v.quick->file_names;
2983 }
2984
2985 /* A helper for the "quick" functions which computes and caches the
2986 real path for a given file name from the line table. */
2987
2988 static const char *
2989 dw2_get_real_path (struct objfile *objfile,
2990 struct quick_file_names *qfn, int index)
2991 {
2992 if (qfn->real_names == NULL)
2993 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2994 qfn->num_file_names, sizeof (char *));
2995
2996 if (qfn->real_names[index] == NULL)
2997 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2998
2999 return qfn->real_names[index];
3000 }
3001
3002 static struct symtab *
3003 dw2_find_last_source_symtab (struct objfile *objfile)
3004 {
3005 int index;
3006
3007 dw2_setup (objfile);
3008 index = dwarf2_per_objfile->n_comp_units - 1;
3009 return dw2_instantiate_symtab (dw2_get_cu (index));
3010 }
3011
3012 /* Traversal function for dw2_forget_cached_source_info. */
3013
3014 static int
3015 dw2_free_cached_file_names (void **slot, void *info)
3016 {
3017 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3018
3019 if (file_data->real_names)
3020 {
3021 int i;
3022
3023 for (i = 0; i < file_data->num_file_names; ++i)
3024 {
3025 xfree ((void*) file_data->real_names[i]);
3026 file_data->real_names[i] = NULL;
3027 }
3028 }
3029
3030 return 1;
3031 }
3032
3033 static void
3034 dw2_forget_cached_source_info (struct objfile *objfile)
3035 {
3036 dw2_setup (objfile);
3037
3038 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3039 dw2_free_cached_file_names, NULL);
3040 }
3041
3042 /* Helper function for dw2_map_symtabs_matching_filename that expands
3043 the symtabs and calls the iterator. */
3044
3045 static int
3046 dw2_map_expand_apply (struct objfile *objfile,
3047 struct dwarf2_per_cu_data *per_cu,
3048 const char *name, const char *real_path,
3049 int (*callback) (struct symtab *, void *),
3050 void *data)
3051 {
3052 struct symtab *last_made = objfile->symtabs;
3053
3054 /* Don't visit already-expanded CUs. */
3055 if (per_cu->v.quick->symtab)
3056 return 0;
3057
3058 /* This may expand more than one symtab, and we want to iterate over
3059 all of them. */
3060 dw2_instantiate_symtab (per_cu);
3061
3062 return iterate_over_some_symtabs (name, real_path, callback, data,
3063 objfile->symtabs, last_made);
3064 }
3065
3066 /* Implementation of the map_symtabs_matching_filename method. */
3067
3068 static int
3069 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
3070 const char *real_path,
3071 int (*callback) (struct symtab *, void *),
3072 void *data)
3073 {
3074 int i;
3075 const char *name_basename = lbasename (name);
3076
3077 dw2_setup (objfile);
3078
3079 /* The rule is CUs specify all the files, including those used by
3080 any TU, so there's no need to scan TUs here. */
3081
3082 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3083 {
3084 int j;
3085 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3086 struct quick_file_names *file_data;
3087
3088 /* We only need to look at symtabs not already expanded. */
3089 if (per_cu->v.quick->symtab)
3090 continue;
3091
3092 file_data = dw2_get_file_names (per_cu);
3093 if (file_data == NULL)
3094 continue;
3095
3096 for (j = 0; j < file_data->num_file_names; ++j)
3097 {
3098 const char *this_name = file_data->file_names[j];
3099 const char *this_real_name;
3100
3101 if (compare_filenames_for_search (this_name, name))
3102 {
3103 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3104 callback, data))
3105 return 1;
3106 continue;
3107 }
3108
3109 /* Before we invoke realpath, which can get expensive when many
3110 files are involved, do a quick comparison of the basenames. */
3111 if (! basenames_may_differ
3112 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3113 continue;
3114
3115 this_real_name = dw2_get_real_path (objfile, file_data, j);
3116 if (compare_filenames_for_search (this_real_name, name))
3117 {
3118 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3119 callback, data))
3120 return 1;
3121 continue;
3122 }
3123
3124 if (real_path != NULL)
3125 {
3126 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3127 gdb_assert (IS_ABSOLUTE_PATH (name));
3128 if (this_real_name != NULL
3129 && FILENAME_CMP (real_path, this_real_name) == 0)
3130 {
3131 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3132 callback, data))
3133 return 1;
3134 continue;
3135 }
3136 }
3137 }
3138 }
3139
3140 return 0;
3141 }
3142
3143 /* Struct used to manage iterating over all CUs looking for a symbol. */
3144
3145 struct dw2_symtab_iterator
3146 {
3147 /* The internalized form of .gdb_index. */
3148 struct mapped_index *index;
3149 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3150 int want_specific_block;
3151 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3152 Unused if !WANT_SPECIFIC_BLOCK. */
3153 int block_index;
3154 /* The kind of symbol we're looking for. */
3155 domain_enum domain;
3156 /* The list of CUs from the index entry of the symbol,
3157 or NULL if not found. */
3158 offset_type *vec;
3159 /* The next element in VEC to look at. */
3160 int next;
3161 /* The number of elements in VEC, or zero if there is no match. */
3162 int length;
3163 };
3164
3165 /* Initialize the index symtab iterator ITER.
3166 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3167 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
3168
3169 static void
3170 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3171 struct mapped_index *index,
3172 int want_specific_block,
3173 int block_index,
3174 domain_enum domain,
3175 const char *name)
3176 {
3177 iter->index = index;
3178 iter->want_specific_block = want_specific_block;
3179 iter->block_index = block_index;
3180 iter->domain = domain;
3181 iter->next = 0;
3182
3183 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3184 iter->length = MAYBE_SWAP (*iter->vec);
3185 else
3186 {
3187 iter->vec = NULL;
3188 iter->length = 0;
3189 }
3190 }
3191
3192 /* Return the next matching CU or NULL if there are no more. */
3193
3194 static struct dwarf2_per_cu_data *
3195 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3196 {
3197 for ( ; iter->next < iter->length; ++iter->next)
3198 {
3199 offset_type cu_index_and_attrs =
3200 MAYBE_SWAP (iter->vec[iter->next + 1]);
3201 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3202 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
3203 int want_static = iter->block_index != GLOBAL_BLOCK;
3204 /* This value is only valid for index versions >= 7. */
3205 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3206 gdb_index_symbol_kind symbol_kind =
3207 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3208 /* Only check the symbol attributes if they're present.
3209 Indices prior to version 7 don't record them,
3210 and indices >= 7 may elide them for certain symbols
3211 (gold does this). */
3212 int attrs_valid =
3213 (iter->index->version >= 7
3214 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3215
3216 /* Skip if already read in. */
3217 if (per_cu->v.quick->symtab)
3218 continue;
3219
3220 if (attrs_valid
3221 && iter->want_specific_block
3222 && want_static != is_static)
3223 continue;
3224
3225 /* Only check the symbol's kind if it has one. */
3226 if (attrs_valid)
3227 {
3228 switch (iter->domain)
3229 {
3230 case VAR_DOMAIN:
3231 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3232 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3233 /* Some types are also in VAR_DOMAIN. */
3234 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3235 continue;
3236 break;
3237 case STRUCT_DOMAIN:
3238 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3239 continue;
3240 break;
3241 case LABEL_DOMAIN:
3242 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3243 continue;
3244 break;
3245 default:
3246 break;
3247 }
3248 }
3249
3250 ++iter->next;
3251 return per_cu;
3252 }
3253
3254 return NULL;
3255 }
3256
3257 static struct symtab *
3258 dw2_lookup_symbol (struct objfile *objfile, int block_index,
3259 const char *name, domain_enum domain)
3260 {
3261 struct symtab *stab_best = NULL;
3262 struct mapped_index *index;
3263
3264 dw2_setup (objfile);
3265
3266 index = dwarf2_per_objfile->index_table;
3267
3268 /* index is NULL if OBJF_READNOW. */
3269 if (index)
3270 {
3271 struct dw2_symtab_iterator iter;
3272 struct dwarf2_per_cu_data *per_cu;
3273
3274 dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
3275
3276 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3277 {
3278 struct symbol *sym = NULL;
3279 struct symtab *stab = dw2_instantiate_symtab (per_cu);
3280
3281 /* Some caution must be observed with overloaded functions
3282 and methods, since the index will not contain any overload
3283 information (but NAME might contain it). */
3284 if (stab->primary)
3285 {
3286 struct blockvector *bv = BLOCKVECTOR (stab);
3287 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3288
3289 sym = lookup_block_symbol (block, name, domain);
3290 }
3291
3292 if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
3293 {
3294 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
3295 return stab;
3296
3297 stab_best = stab;
3298 }
3299
3300 /* Keep looking through other CUs. */
3301 }
3302 }
3303
3304 return stab_best;
3305 }
3306
3307 static void
3308 dw2_print_stats (struct objfile *objfile)
3309 {
3310 int i, total, count;
3311
3312 dw2_setup (objfile);
3313 total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
3314 count = 0;
3315 for (i = 0; i < total; ++i)
3316 {
3317 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3318
3319 if (!per_cu->v.quick->symtab)
3320 ++count;
3321 }
3322 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3323 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3324 }
3325
3326 static void
3327 dw2_dump (struct objfile *objfile)
3328 {
3329 /* Nothing worth printing. */
3330 }
3331
3332 static void
3333 dw2_relocate (struct objfile *objfile,
3334 const struct section_offsets *new_offsets,
3335 const struct section_offsets *delta)
3336 {
3337 /* There's nothing to relocate here. */
3338 }
3339
3340 static void
3341 dw2_expand_symtabs_for_function (struct objfile *objfile,
3342 const char *func_name)
3343 {
3344 struct mapped_index *index;
3345
3346 dw2_setup (objfile);
3347
3348 index = dwarf2_per_objfile->index_table;
3349
3350 /* index is NULL if OBJF_READNOW. */
3351 if (index)
3352 {
3353 struct dw2_symtab_iterator iter;
3354 struct dwarf2_per_cu_data *per_cu;
3355
3356 /* Note: It doesn't matter what we pass for block_index here. */
3357 dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
3358 func_name);
3359
3360 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3361 dw2_instantiate_symtab (per_cu);
3362 }
3363 }
3364
3365 static void
3366 dw2_expand_all_symtabs (struct objfile *objfile)
3367 {
3368 int i;
3369
3370 dw2_setup (objfile);
3371
3372 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3373 + dwarf2_per_objfile->n_type_units); ++i)
3374 {
3375 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3376
3377 dw2_instantiate_symtab (per_cu);
3378 }
3379 }
3380
3381 static void
3382 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3383 const char *fullname)
3384 {
3385 int i;
3386
3387 dw2_setup (objfile);
3388
3389 /* We don't need to consider type units here.
3390 This is only called for examining code, e.g. expand_line_sal.
3391 There can be an order of magnitude (or more) more type units
3392 than comp units, and we avoid them if we can. */
3393
3394 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3395 {
3396 int j;
3397 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3398 struct quick_file_names *file_data;
3399
3400 /* We only need to look at symtabs not already expanded. */
3401 if (per_cu->v.quick->symtab)
3402 continue;
3403
3404 file_data = dw2_get_file_names (per_cu);
3405 if (file_data == NULL)
3406 continue;
3407
3408 for (j = 0; j < file_data->num_file_names; ++j)
3409 {
3410 const char *this_fullname = file_data->file_names[j];
3411
3412 if (filename_cmp (this_fullname, fullname) == 0)
3413 {
3414 dw2_instantiate_symtab (per_cu);
3415 break;
3416 }
3417 }
3418 }
3419 }
3420
3421 /* A helper function for dw2_find_symbol_file that finds the primary
3422 file name for a given CU. This is a die_reader_func. */
3423
3424 static void
3425 dw2_get_primary_filename_reader (const struct die_reader_specs *reader,
3426 const gdb_byte *info_ptr,
3427 struct die_info *comp_unit_die,
3428 int has_children,
3429 void *data)
3430 {
3431 const char **result_ptr = data;
3432 struct dwarf2_cu *cu = reader->cu;
3433 struct attribute *attr;
3434
3435 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
3436 if (attr == NULL)
3437 *result_ptr = NULL;
3438 else
3439 *result_ptr = DW_STRING (attr);
3440 }
3441
3442 static const char *
3443 dw2_find_symbol_file (struct objfile *objfile, const char *name)
3444 {
3445 struct dwarf2_per_cu_data *per_cu;
3446 offset_type *vec;
3447 const char *filename;
3448
3449 dw2_setup (objfile);
3450
3451 /* index_table is NULL if OBJF_READNOW. */
3452 if (!dwarf2_per_objfile->index_table)
3453 {
3454 struct symtab *s;
3455
3456 ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
3457 {
3458 struct blockvector *bv = BLOCKVECTOR (s);
3459 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
3460 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
3461
3462 if (sym)
3463 {
3464 /* Only file extension of returned filename is recognized. */
3465 return SYMBOL_SYMTAB (sym)->filename;
3466 }
3467 }
3468 return NULL;
3469 }
3470
3471 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
3472 name, &vec))
3473 return NULL;
3474
3475 /* Note that this just looks at the very first one named NAME -- but
3476 actually we are looking for a function. find_main_filename
3477 should be rewritten so that it doesn't require a custom hook. It
3478 could just use the ordinary symbol tables. */
3479 /* vec[0] is the length, which must always be >0. */
3480 per_cu = dw2_get_cu (GDB_INDEX_CU_VALUE (MAYBE_SWAP (vec[1])));
3481
3482 if (per_cu->v.quick->symtab != NULL)
3483 {
3484 /* Only file extension of returned filename is recognized. */
3485 return per_cu->v.quick->symtab->filename;
3486 }
3487
3488 /* Initialize filename in case there's a problem reading the DWARF,
3489 dw2_get_primary_filename_reader may not get called. */
3490 filename = NULL;
3491 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
3492 dw2_get_primary_filename_reader, &filename);
3493
3494 /* Only file extension of returned filename is recognized. */
3495 return filename;
3496 }
3497
3498 static void
3499 dw2_map_matching_symbols (const char * name, domain_enum namespace,
3500 struct objfile *objfile, int global,
3501 int (*callback) (struct block *,
3502 struct symbol *, void *),
3503 void *data, symbol_compare_ftype *match,
3504 symbol_compare_ftype *ordered_compare)
3505 {
3506 /* Currently unimplemented; used for Ada. The function can be called if the
3507 current language is Ada for a non-Ada objfile using GNU index. As Ada
3508 does not look for non-Ada symbols this function should just return. */
3509 }
3510
3511 static void
3512 dw2_expand_symtabs_matching
3513 (struct objfile *objfile,
3514 int (*file_matcher) (const char *, void *, int basenames),
3515 int (*name_matcher) (const char *, void *),
3516 enum search_domain kind,
3517 void *data)
3518 {
3519 int i;
3520 offset_type iter;
3521 struct mapped_index *index;
3522
3523 dw2_setup (objfile);
3524
3525 /* index_table is NULL if OBJF_READNOW. */
3526 if (!dwarf2_per_objfile->index_table)
3527 return;
3528 index = dwarf2_per_objfile->index_table;
3529
3530 if (file_matcher != NULL)
3531 {
3532 struct cleanup *cleanup;
3533 htab_t visited_found, visited_not_found;
3534
3535 visited_found = htab_create_alloc (10,
3536 htab_hash_pointer, htab_eq_pointer,
3537 NULL, xcalloc, xfree);
3538 cleanup = make_cleanup_htab_delete (visited_found);
3539 visited_not_found = htab_create_alloc (10,
3540 htab_hash_pointer, htab_eq_pointer,
3541 NULL, xcalloc, xfree);
3542 make_cleanup_htab_delete (visited_not_found);
3543
3544 /* The rule is CUs specify all the files, including those used by
3545 any TU, so there's no need to scan TUs here. */
3546
3547 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3548 {
3549 int j;
3550 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3551 struct quick_file_names *file_data;
3552 void **slot;
3553
3554 per_cu->v.quick->mark = 0;
3555
3556 /* We only need to look at symtabs not already expanded. */
3557 if (per_cu->v.quick->symtab)
3558 continue;
3559
3560 file_data = dw2_get_file_names (per_cu);
3561 if (file_data == NULL)
3562 continue;
3563
3564 if (htab_find (visited_not_found, file_data) != NULL)
3565 continue;
3566 else if (htab_find (visited_found, file_data) != NULL)
3567 {
3568 per_cu->v.quick->mark = 1;
3569 continue;
3570 }
3571
3572 for (j = 0; j < file_data->num_file_names; ++j)
3573 {
3574 const char *this_real_name;
3575
3576 if (file_matcher (file_data->file_names[j], data, 0))
3577 {
3578 per_cu->v.quick->mark = 1;
3579 break;
3580 }
3581
3582 /* Before we invoke realpath, which can get expensive when many
3583 files are involved, do a quick comparison of the basenames. */
3584 if (!basenames_may_differ
3585 && !file_matcher (lbasename (file_data->file_names[j]),
3586 data, 1))
3587 continue;
3588
3589 this_real_name = dw2_get_real_path (objfile, file_data, j);
3590 if (file_matcher (this_real_name, data, 0))
3591 {
3592 per_cu->v.quick->mark = 1;
3593 break;
3594 }
3595 }
3596
3597 slot = htab_find_slot (per_cu->v.quick->mark
3598 ? visited_found
3599 : visited_not_found,
3600 file_data, INSERT);
3601 *slot = file_data;
3602 }
3603
3604 do_cleanups (cleanup);
3605 }
3606
3607 for (iter = 0; iter < index->symbol_table_slots; ++iter)
3608 {
3609 offset_type idx = 2 * iter;
3610 const char *name;
3611 offset_type *vec, vec_len, vec_idx;
3612
3613 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
3614 continue;
3615
3616 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
3617
3618 if (! (*name_matcher) (name, data))
3619 continue;
3620
3621 /* The name was matched, now expand corresponding CUs that were
3622 marked. */
3623 vec = (offset_type *) (index->constant_pool
3624 + MAYBE_SWAP (index->symbol_table[idx + 1]));
3625 vec_len = MAYBE_SWAP (vec[0]);
3626 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
3627 {
3628 struct dwarf2_per_cu_data *per_cu;
3629 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
3630 gdb_index_symbol_kind symbol_kind =
3631 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3632 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3633
3634 /* Don't crash on bad data. */
3635 if (cu_index >= (dwarf2_per_objfile->n_comp_units
3636 + dwarf2_per_objfile->n_type_units))
3637 continue;
3638
3639 /* Only check the symbol's kind if it has one.
3640 Indices prior to version 7 don't record it. */
3641 if (index->version >= 7)
3642 {
3643 switch (kind)
3644 {
3645 case VARIABLES_DOMAIN:
3646 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
3647 continue;
3648 break;
3649 case FUNCTIONS_DOMAIN:
3650 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
3651 continue;
3652 break;
3653 case TYPES_DOMAIN:
3654 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3655 continue;
3656 break;
3657 default:
3658 break;
3659 }
3660 }
3661
3662 per_cu = dw2_get_cu (cu_index);
3663 if (file_matcher == NULL || per_cu->v.quick->mark)
3664 dw2_instantiate_symtab (per_cu);
3665 }
3666 }
3667 }
3668
3669 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
3670 symtab. */
3671
3672 static struct symtab *
3673 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
3674 {
3675 int i;
3676
3677 if (BLOCKVECTOR (symtab) != NULL
3678 && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
3679 return symtab;
3680
3681 if (symtab->includes == NULL)
3682 return NULL;
3683
3684 for (i = 0; symtab->includes[i]; ++i)
3685 {
3686 struct symtab *s = symtab->includes[i];
3687
3688 s = recursively_find_pc_sect_symtab (s, pc);
3689 if (s != NULL)
3690 return s;
3691 }
3692
3693 return NULL;
3694 }
3695
3696 static struct symtab *
3697 dw2_find_pc_sect_symtab (struct objfile *objfile,
3698 struct minimal_symbol *msymbol,
3699 CORE_ADDR pc,
3700 struct obj_section *section,
3701 int warn_if_readin)
3702 {
3703 struct dwarf2_per_cu_data *data;
3704 struct symtab *result;
3705
3706 dw2_setup (objfile);
3707
3708 if (!objfile->psymtabs_addrmap)
3709 return NULL;
3710
3711 data = addrmap_find (objfile->psymtabs_addrmap, pc);
3712 if (!data)
3713 return NULL;
3714
3715 if (warn_if_readin && data->v.quick->symtab)
3716 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
3717 paddress (get_objfile_arch (objfile), pc));
3718
3719 result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
3720 gdb_assert (result != NULL);
3721 return result;
3722 }
3723
3724 static void
3725 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
3726 void *data, int need_fullname)
3727 {
3728 int i;
3729 struct cleanup *cleanup;
3730 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
3731 NULL, xcalloc, xfree);
3732
3733 cleanup = make_cleanup_htab_delete (visited);
3734 dw2_setup (objfile);
3735
3736 /* The rule is CUs specify all the files, including those used by
3737 any TU, so there's no need to scan TUs here.
3738 We can ignore file names coming from already-expanded CUs. */
3739
3740 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3741 {
3742 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3743
3744 if (per_cu->v.quick->symtab)
3745 {
3746 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
3747 INSERT);
3748
3749 *slot = per_cu->v.quick->file_names;
3750 }
3751 }
3752
3753 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3754 {
3755 int j;
3756 struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
3757 struct quick_file_names *file_data;
3758 void **slot;
3759
3760 /* We only need to look at symtabs not already expanded. */
3761 if (per_cu->v.quick->symtab)
3762 continue;
3763
3764 file_data = dw2_get_file_names (per_cu);
3765 if (file_data == NULL)
3766 continue;
3767
3768 slot = htab_find_slot (visited, file_data, INSERT);
3769 if (*slot)
3770 {
3771 /* Already visited. */
3772 continue;
3773 }
3774 *slot = file_data;
3775
3776 for (j = 0; j < file_data->num_file_names; ++j)
3777 {
3778 const char *this_real_name;
3779
3780 if (need_fullname)
3781 this_real_name = dw2_get_real_path (objfile, file_data, j);
3782 else
3783 this_real_name = NULL;
3784 (*fun) (file_data->file_names[j], this_real_name, data);
3785 }
3786 }
3787
3788 do_cleanups (cleanup);
3789 }
3790
3791 static int
3792 dw2_has_symbols (struct objfile *objfile)
3793 {
3794 return 1;
3795 }
3796
3797 const struct quick_symbol_functions dwarf2_gdb_index_functions =
3798 {
3799 dw2_has_symbols,
3800 dw2_find_last_source_symtab,
3801 dw2_forget_cached_source_info,
3802 dw2_map_symtabs_matching_filename,
3803 dw2_lookup_symbol,
3804 dw2_print_stats,
3805 dw2_dump,
3806 dw2_relocate,
3807 dw2_expand_symtabs_for_function,
3808 dw2_expand_all_symtabs,
3809 dw2_expand_symtabs_with_fullname,
3810 dw2_find_symbol_file,
3811 dw2_map_matching_symbols,
3812 dw2_expand_symtabs_matching,
3813 dw2_find_pc_sect_symtab,
3814 dw2_map_symbol_filenames
3815 };
3816
3817 /* Initialize for reading DWARF for this objfile. Return 0 if this
3818 file will use psymtabs, or 1 if using the GNU index. */
3819
3820 int
3821 dwarf2_initialize_objfile (struct objfile *objfile)
3822 {
3823 /* If we're about to read full symbols, don't bother with the
3824 indices. In this case we also don't care if some other debug
3825 format is making psymtabs, because they are all about to be
3826 expanded anyway. */
3827 if ((objfile->flags & OBJF_READNOW))
3828 {
3829 int i;
3830
3831 dwarf2_per_objfile->using_index = 1;
3832 create_all_comp_units (objfile);
3833 create_all_type_units (objfile);
3834 dwarf2_per_objfile->quick_file_names_table =
3835 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
3836
3837 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
3838 + dwarf2_per_objfile->n_type_units); ++i)
3839 {
3840 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3841
3842 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3843 struct dwarf2_per_cu_quick_data);
3844 }
3845
3846 /* Return 1 so that gdb sees the "quick" functions. However,
3847 these functions will be no-ops because we will have expanded
3848 all symtabs. */
3849 return 1;
3850 }
3851
3852 if (dwarf2_read_index (objfile))
3853 return 1;
3854
3855 return 0;
3856 }
3857
3858 \f
3859
3860 /* Build a partial symbol table. */
3861
3862 void
3863 dwarf2_build_psymtabs (struct objfile *objfile)
3864 {
3865 volatile struct gdb_exception except;
3866
3867 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
3868 {
3869 init_psymbol_list (objfile, 1024);
3870 }
3871
3872 TRY_CATCH (except, RETURN_MASK_ERROR)
3873 {
3874 /* This isn't really ideal: all the data we allocate on the
3875 objfile's obstack is still uselessly kept around. However,
3876 freeing it seems unsafe. */
3877 struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
3878
3879 dwarf2_build_psymtabs_hard (objfile);
3880 discard_cleanups (cleanups);
3881 }
3882 if (except.reason < 0)
3883 exception_print (gdb_stderr, except);
3884 }
3885
3886 /* Return the total length of the CU described by HEADER. */
3887
3888 static unsigned int
3889 get_cu_length (const struct comp_unit_head *header)
3890 {
3891 return header->initial_length_size + header->length;
3892 }
3893
3894 /* Return TRUE if OFFSET is within CU_HEADER. */
3895
3896 static inline int
3897 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3898 {
3899 sect_offset bottom = { cu_header->offset.sect_off };
3900 sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
3901
3902 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3903 }
3904
3905 /* Find the base address of the compilation unit for range lists and
3906 location lists. It will normally be specified by DW_AT_low_pc.
3907 In DWARF-3 draft 4, the base address could be overridden by
3908 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3909 compilation units with discontinuous ranges. */
3910
3911 static void
3912 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3913 {
3914 struct attribute *attr;
3915
3916 cu->base_known = 0;
3917 cu->base_address = 0;
3918
3919 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3920 if (attr)
3921 {
3922 cu->base_address = DW_ADDR (attr);
3923 cu->base_known = 1;
3924 }
3925 else
3926 {
3927 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3928 if (attr)
3929 {
3930 cu->base_address = DW_ADDR (attr);
3931 cu->base_known = 1;
3932 }
3933 }
3934 }
3935
3936 /* Read in the comp unit header information from the debug_info at info_ptr.
3937 NOTE: This leaves members offset, first_die_offset to be filled in
3938 by the caller. */
3939
3940 static const gdb_byte *
3941 read_comp_unit_head (struct comp_unit_head *cu_header,
3942 const gdb_byte *info_ptr, bfd *abfd)
3943 {
3944 int signed_addr;
3945 unsigned int bytes_read;
3946
3947 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3948 cu_header->initial_length_size = bytes_read;
3949 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3950 info_ptr += bytes_read;
3951 cu_header->version = read_2_bytes (abfd, info_ptr);
3952 info_ptr += 2;
3953 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3954 &bytes_read);
3955 info_ptr += bytes_read;
3956 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3957 info_ptr += 1;
3958 signed_addr = bfd_get_sign_extend_vma (abfd);
3959 if (signed_addr < 0)
3960 internal_error (__FILE__, __LINE__,
3961 _("read_comp_unit_head: dwarf from non elf file"));
3962 cu_header->signed_addr_p = signed_addr;
3963
3964 return info_ptr;
3965 }
3966
3967 /* Helper function that returns the proper abbrev section for
3968 THIS_CU. */
3969
3970 static struct dwarf2_section_info *
3971 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
3972 {
3973 struct dwarf2_section_info *abbrev;
3974
3975 if (this_cu->is_dwz)
3976 abbrev = &dwarf2_get_dwz_file ()->abbrev;
3977 else
3978 abbrev = &dwarf2_per_objfile->abbrev;
3979
3980 return abbrev;
3981 }
3982
3983 /* Subroutine of read_and_check_comp_unit_head and
3984 read_and_check_type_unit_head to simplify them.
3985 Perform various error checking on the header. */
3986
3987 static void
3988 error_check_comp_unit_head (struct comp_unit_head *header,
3989 struct dwarf2_section_info *section,
3990 struct dwarf2_section_info *abbrev_section)
3991 {
3992 bfd *abfd = section->asection->owner;
3993 const char *filename = bfd_get_filename (abfd);
3994
3995 if (header->version != 2 && header->version != 3 && header->version != 4)
3996 error (_("Dwarf Error: wrong version in compilation unit header "
3997 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3998 filename);
3999
4000 if (header->abbrev_offset.sect_off
4001 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
4002 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
4003 "(offset 0x%lx + 6) [in module %s]"),
4004 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
4005 filename);
4006
4007 /* Cast to unsigned long to use 64-bit arithmetic when possible to
4008 avoid potential 32-bit overflow. */
4009 if (((unsigned long) header->offset.sect_off + get_cu_length (header))
4010 > section->size)
4011 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
4012 "(offset 0x%lx + 0) [in module %s]"),
4013 (long) header->length, (long) header->offset.sect_off,
4014 filename);
4015 }
4016
4017 /* Read in a CU/TU header and perform some basic error checking.
4018 The contents of the header are stored in HEADER.
4019 The result is a pointer to the start of the first DIE. */
4020
4021 static const gdb_byte *
4022 read_and_check_comp_unit_head (struct comp_unit_head *header,
4023 struct dwarf2_section_info *section,
4024 struct dwarf2_section_info *abbrev_section,
4025 const gdb_byte *info_ptr,
4026 int is_debug_types_section)
4027 {
4028 const gdb_byte *beg_of_comp_unit = info_ptr;
4029 bfd *abfd = section->asection->owner;
4030
4031 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4032
4033 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4034
4035 /* If we're reading a type unit, skip over the signature and
4036 type_offset fields. */
4037 if (is_debug_types_section)
4038 info_ptr += 8 /*signature*/ + header->offset_size;
4039
4040 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4041
4042 error_check_comp_unit_head (header, section, abbrev_section);
4043
4044 return info_ptr;
4045 }
4046
4047 /* Read in the types comp unit header information from .debug_types entry at
4048 types_ptr. The result is a pointer to one past the end of the header. */
4049
4050 static const gdb_byte *
4051 read_and_check_type_unit_head (struct comp_unit_head *header,
4052 struct dwarf2_section_info *section,
4053 struct dwarf2_section_info *abbrev_section,
4054 const gdb_byte *info_ptr,
4055 ULONGEST *signature,
4056 cu_offset *type_offset_in_tu)
4057 {
4058 const gdb_byte *beg_of_comp_unit = info_ptr;
4059 bfd *abfd = section->asection->owner;
4060
4061 header->offset.sect_off = beg_of_comp_unit - section->buffer;
4062
4063 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
4064
4065 /* If we're reading a type unit, skip over the signature and
4066 type_offset fields. */
4067 if (signature != NULL)
4068 *signature = read_8_bytes (abfd, info_ptr);
4069 info_ptr += 8;
4070 if (type_offset_in_tu != NULL)
4071 type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
4072 header->offset_size);
4073 info_ptr += header->offset_size;
4074
4075 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4076
4077 error_check_comp_unit_head (header, section, abbrev_section);
4078
4079 return info_ptr;
4080 }
4081
4082 /* Fetch the abbreviation table offset from a comp or type unit header. */
4083
4084 static sect_offset
4085 read_abbrev_offset (struct dwarf2_section_info *section,
4086 sect_offset offset)
4087 {
4088 bfd *abfd = section->asection->owner;
4089 const gdb_byte *info_ptr;
4090 unsigned int length, initial_length_size, offset_size;
4091 sect_offset abbrev_offset;
4092
4093 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
4094 info_ptr = section->buffer + offset.sect_off;
4095 length = read_initial_length (abfd, info_ptr, &initial_length_size);
4096 offset_size = initial_length_size == 4 ? 4 : 8;
4097 info_ptr += initial_length_size + 2 /*version*/;
4098 abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
4099 return abbrev_offset;
4100 }
4101
4102 /* Allocate a new partial symtab for file named NAME and mark this new
4103 partial symtab as being an include of PST. */
4104
4105 static void
4106 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
4107 struct objfile *objfile)
4108 {
4109 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
4110
4111 if (!IS_ABSOLUTE_PATH (subpst->filename))
4112 {
4113 /* It shares objfile->objfile_obstack. */
4114 subpst->dirname = pst->dirname;
4115 }
4116
4117 subpst->section_offsets = pst->section_offsets;
4118 subpst->textlow = 0;
4119 subpst->texthigh = 0;
4120
4121 subpst->dependencies = (struct partial_symtab **)
4122 obstack_alloc (&objfile->objfile_obstack,
4123 sizeof (struct partial_symtab *));
4124 subpst->dependencies[0] = pst;
4125 subpst->number_of_dependencies = 1;
4126
4127 subpst->globals_offset = 0;
4128 subpst->n_global_syms = 0;
4129 subpst->statics_offset = 0;
4130 subpst->n_static_syms = 0;
4131 subpst->symtab = NULL;
4132 subpst->read_symtab = pst->read_symtab;
4133 subpst->readin = 0;
4134
4135 /* No private part is necessary for include psymtabs. This property
4136 can be used to differentiate between such include psymtabs and
4137 the regular ones. */
4138 subpst->read_symtab_private = NULL;
4139 }
4140
4141 /* Read the Line Number Program data and extract the list of files
4142 included by the source file represented by PST. Build an include
4143 partial symtab for each of these included files. */
4144
4145 static void
4146 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
4147 struct die_info *die,
4148 struct partial_symtab *pst)
4149 {
4150 struct line_header *lh = NULL;
4151 struct attribute *attr;
4152
4153 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
4154 if (attr)
4155 lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
4156 if (lh == NULL)
4157 return; /* No linetable, so no includes. */
4158
4159 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
4160 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
4161
4162 free_line_header (lh);
4163 }
4164
4165 static hashval_t
4166 hash_signatured_type (const void *item)
4167 {
4168 const struct signatured_type *sig_type = item;
4169
4170 /* This drops the top 32 bits of the signature, but is ok for a hash. */
4171 return sig_type->signature;
4172 }
4173
4174 static int
4175 eq_signatured_type (const void *item_lhs, const void *item_rhs)
4176 {
4177 const struct signatured_type *lhs = item_lhs;
4178 const struct signatured_type *rhs = item_rhs;
4179
4180 return lhs->signature == rhs->signature;
4181 }
4182
4183 /* Allocate a hash table for signatured types. */
4184
4185 static htab_t
4186 allocate_signatured_type_table (struct objfile *objfile)
4187 {
4188 return htab_create_alloc_ex (41,
4189 hash_signatured_type,
4190 eq_signatured_type,
4191 NULL,
4192 &objfile->objfile_obstack,
4193 hashtab_obstack_allocate,
4194 dummy_obstack_deallocate);
4195 }
4196
4197 /* A helper function to add a signatured type CU to a table. */
4198
4199 static int
4200 add_signatured_type_cu_to_table (void **slot, void *datum)
4201 {
4202 struct signatured_type *sigt = *slot;
4203 struct signatured_type ***datap = datum;
4204
4205 **datap = sigt;
4206 ++*datap;
4207
4208 return 1;
4209 }
4210
4211 /* Create the hash table of all entries in the .debug_types
4212 (or .debug_types.dwo) section(s).
4213 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
4214 otherwise it is NULL.
4215
4216 The result is a pointer to the hash table or NULL if there are no types.
4217
4218 Note: This function processes DWO files only, not DWP files. */
4219
4220 static htab_t
4221 create_debug_types_hash_table (struct dwo_file *dwo_file,
4222 VEC (dwarf2_section_info_def) *types)
4223 {
4224 struct objfile *objfile = dwarf2_per_objfile->objfile;
4225 htab_t types_htab = NULL;
4226 int ix;
4227 struct dwarf2_section_info *section;
4228 struct dwarf2_section_info *abbrev_section;
4229
4230 if (VEC_empty (dwarf2_section_info_def, types))
4231 return NULL;
4232
4233 abbrev_section = (dwo_file != NULL
4234 ? &dwo_file->sections.abbrev
4235 : &dwarf2_per_objfile->abbrev);
4236
4237 if (dwarf2_read_debug)
4238 fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
4239 dwo_file ? ".dwo" : "",
4240 bfd_get_filename (abbrev_section->asection->owner));
4241
4242 for (ix = 0;
4243 VEC_iterate (dwarf2_section_info_def, types, ix, section);
4244 ++ix)
4245 {
4246 bfd *abfd;
4247 const gdb_byte *info_ptr, *end_ptr;
4248 struct dwarf2_section_info *abbrev_section;
4249
4250 dwarf2_read_section (objfile, section);
4251 info_ptr = section->buffer;
4252
4253 if (info_ptr == NULL)
4254 continue;
4255
4256 /* We can't set abfd until now because the section may be empty or
4257 not present, in which case section->asection will be NULL. */
4258 abfd = section->asection->owner;
4259
4260 if (dwo_file)
4261 abbrev_section = &dwo_file->sections.abbrev;
4262 else
4263 abbrev_section = &dwarf2_per_objfile->abbrev;
4264
4265 /* We don't use init_cutu_and_read_dies_simple, or some such, here
4266 because we don't need to read any dies: the signature is in the
4267 header. */
4268
4269 end_ptr = info_ptr + section->size;
4270 while (info_ptr < end_ptr)
4271 {
4272 sect_offset offset;
4273 cu_offset type_offset_in_tu;
4274 ULONGEST signature;
4275 struct signatured_type *sig_type;
4276 struct dwo_unit *dwo_tu;
4277 void **slot;
4278 const gdb_byte *ptr = info_ptr;
4279 struct comp_unit_head header;
4280 unsigned int length;
4281
4282 offset.sect_off = ptr - section->buffer;
4283
4284 /* We need to read the type's signature in order to build the hash
4285 table, but we don't need anything else just yet. */
4286
4287 ptr = read_and_check_type_unit_head (&header, section,
4288 abbrev_section, ptr,
4289 &signature, &type_offset_in_tu);
4290
4291 length = get_cu_length (&header);
4292
4293 /* Skip dummy type units. */
4294 if (ptr >= info_ptr + length
4295 || peek_abbrev_code (abfd, ptr) == 0)
4296 {
4297 info_ptr += length;
4298 continue;
4299 }
4300
4301 if (types_htab == NULL)
4302 {
4303 if (dwo_file)
4304 types_htab = allocate_dwo_unit_table (objfile);
4305 else
4306 types_htab = allocate_signatured_type_table (objfile);
4307 }
4308
4309 if (dwo_file)
4310 {
4311 sig_type = NULL;
4312 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4313 struct dwo_unit);
4314 dwo_tu->dwo_file = dwo_file;
4315 dwo_tu->signature = signature;
4316 dwo_tu->type_offset_in_tu = type_offset_in_tu;
4317 dwo_tu->section = section;
4318 dwo_tu->offset = offset;
4319 dwo_tu->length = length;
4320 }
4321 else
4322 {
4323 /* N.B.: type_offset is not usable if this type uses a DWO file.
4324 The real type_offset is in the DWO file. */
4325 dwo_tu = NULL;
4326 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4327 struct signatured_type);
4328 sig_type->signature = signature;
4329 sig_type->type_offset_in_tu = type_offset_in_tu;
4330 sig_type->per_cu.objfile = objfile;
4331 sig_type->per_cu.is_debug_types = 1;
4332 sig_type->per_cu.section = section;
4333 sig_type->per_cu.offset = offset;
4334 sig_type->per_cu.length = length;
4335 }
4336
4337 slot = htab_find_slot (types_htab,
4338 dwo_file ? (void*) dwo_tu : (void *) sig_type,
4339 INSERT);
4340 gdb_assert (slot != NULL);
4341 if (*slot != NULL)
4342 {
4343 sect_offset dup_offset;
4344
4345 if (dwo_file)
4346 {
4347 const struct dwo_unit *dup_tu = *slot;
4348
4349 dup_offset = dup_tu->offset;
4350 }
4351 else
4352 {
4353 const struct signatured_type *dup_tu = *slot;
4354
4355 dup_offset = dup_tu->per_cu.offset;
4356 }
4357
4358 complaint (&symfile_complaints,
4359 _("debug type entry at offset 0x%x is duplicate to"
4360 " the entry at offset 0x%x, signature %s"),
4361 offset.sect_off, dup_offset.sect_off,
4362 hex_string (signature));
4363 }
4364 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
4365
4366 if (dwarf2_read_debug)
4367 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature %s\n",
4368 offset.sect_off,
4369 hex_string (signature));
4370
4371 info_ptr += length;
4372 }
4373 }
4374
4375 return types_htab;
4376 }
4377
4378 /* Create the hash table of all entries in the .debug_types section,
4379 and initialize all_type_units.
4380 The result is zero if there is an error (e.g. missing .debug_types section),
4381 otherwise non-zero. */
4382
4383 static int
4384 create_all_type_units (struct objfile *objfile)
4385 {
4386 htab_t types_htab;
4387 struct signatured_type **iter;
4388
4389 types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
4390 if (types_htab == NULL)
4391 {
4392 dwarf2_per_objfile->signatured_types = NULL;
4393 return 0;
4394 }
4395
4396 dwarf2_per_objfile->signatured_types = types_htab;
4397
4398 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
4399 dwarf2_per_objfile->all_type_units
4400 = xmalloc (dwarf2_per_objfile->n_type_units
4401 * sizeof (struct signatured_type *));
4402 iter = &dwarf2_per_objfile->all_type_units[0];
4403 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
4404 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
4405 == dwarf2_per_objfile->n_type_units);
4406
4407 return 1;
4408 }
4409
4410 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
4411 Fill in SIG_ENTRY with DWO_ENTRY. */
4412
4413 static void
4414 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
4415 struct signatured_type *sig_entry,
4416 struct dwo_unit *dwo_entry)
4417 {
4418 sig_entry->per_cu.section = dwo_entry->section;
4419 sig_entry->per_cu.offset = dwo_entry->offset;
4420 sig_entry->per_cu.length = dwo_entry->length;
4421 sig_entry->per_cu.reading_dwo_directly = 1;
4422 sig_entry->per_cu.objfile = objfile;
4423 gdb_assert (! sig_entry->per_cu.queued);
4424 gdb_assert (sig_entry->per_cu.cu == NULL);
4425 gdb_assert (sig_entry->per_cu.v.quick != NULL);
4426 gdb_assert (sig_entry->per_cu.v.quick->symtab == NULL);
4427 gdb_assert (sig_entry->signature == dwo_entry->signature);
4428 gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
4429 gdb_assert (sig_entry->type_unit_group == NULL);
4430 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
4431 sig_entry->dwo_unit = dwo_entry;
4432 }
4433
4434 /* Subroutine of lookup_signatured_type.
4435 Create the signatured_type data structure for a TU to be read in
4436 directly from a DWO file, bypassing the stub.
4437 We do this for the case where there is no DWP file and we're using
4438 .gdb_index: When reading a CU we want to stay in the DWO file containing
4439 that CU. Otherwise we could end up reading several other DWO files (due
4440 to comdat folding) to process the transitive closure of all the mentioned
4441 TUs, and that can be slow. The current DWO file will have every type
4442 signature that it needs.
4443 We only do this for .gdb_index because in the psymtab case we already have
4444 to read all the DWOs to build the type unit groups. */
4445
4446 static struct signatured_type *
4447 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4448 {
4449 struct objfile *objfile = dwarf2_per_objfile->objfile;
4450 struct dwo_file *dwo_file;
4451 struct dwo_unit find_dwo_entry, *dwo_entry;
4452 struct signatured_type find_sig_entry, *sig_entry;
4453
4454 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4455
4456 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
4457 dwo_unit of the TU itself. */
4458 dwo_file = cu->dwo_unit->dwo_file;
4459
4460 /* We only ever need to read in one copy of a signatured type.
4461 Just use the global signatured_types array. If this is the first time
4462 we're reading this type, replace the recorded data from .gdb_index with
4463 this TU. */
4464
4465 if (dwarf2_per_objfile->signatured_types == NULL)
4466 return NULL;
4467 find_sig_entry.signature = sig;
4468 sig_entry = htab_find (dwarf2_per_objfile->signatured_types, &find_sig_entry);
4469 if (sig_entry == NULL)
4470 return NULL;
4471 /* Have we already tried to read this TU? */
4472 if (sig_entry->dwo_unit != NULL)
4473 return sig_entry;
4474
4475 /* Ok, this is the first time we're reading this TU. */
4476 if (dwo_file->tus == NULL)
4477 return NULL;
4478 find_dwo_entry.signature = sig;
4479 dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
4480 if (dwo_entry == NULL)
4481 return NULL;
4482
4483 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4484 return sig_entry;
4485 }
4486
4487 /* Subroutine of lookup_dwp_signatured_type.
4488 Add an entry for signature SIG to dwarf2_per_objfile->signatured_types. */
4489
4490 static struct signatured_type *
4491 add_type_unit (ULONGEST sig)
4492 {
4493 struct objfile *objfile = dwarf2_per_objfile->objfile;
4494 int n_type_units = dwarf2_per_objfile->n_type_units;
4495 struct signatured_type *sig_type;
4496 void **slot;
4497
4498 ++n_type_units;
4499 dwarf2_per_objfile->all_type_units =
4500 xrealloc (dwarf2_per_objfile->all_type_units,
4501 n_type_units * sizeof (struct signatured_type *));
4502 dwarf2_per_objfile->n_type_units = n_type_units;
4503 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
4504 struct signatured_type);
4505 dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
4506 sig_type->signature = sig;
4507 sig_type->per_cu.is_debug_types = 1;
4508 sig_type->per_cu.v.quick =
4509 OBSTACK_ZALLOC (&objfile->objfile_obstack,
4510 struct dwarf2_per_cu_quick_data);
4511 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
4512 sig_type, INSERT);
4513 gdb_assert (*slot == NULL);
4514 *slot = sig_type;
4515 /* The rest of sig_type must be filled in by the caller. */
4516 return sig_type;
4517 }
4518
4519 /* Subroutine of lookup_signatured_type.
4520 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
4521 then try the DWP file.
4522 Normally this "can't happen", but if there's a bug in signature
4523 generation and/or the DWP file is built incorrectly, it can happen.
4524 Using the type directly from the DWP file means we don't have the stub
4525 which has some useful attributes (e.g., DW_AT_comp_dir), but they're
4526 not critical. [Eventually the stub may go away for type units anyway.] */
4527
4528 static struct signatured_type *
4529 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4530 {
4531 struct objfile *objfile = dwarf2_per_objfile->objfile;
4532 struct dwp_file *dwp_file = get_dwp_file ();
4533 struct dwo_unit *dwo_entry;
4534 struct signatured_type find_sig_entry, *sig_entry;
4535
4536 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
4537 gdb_assert (dwp_file != NULL);
4538
4539 if (dwarf2_per_objfile->signatured_types != NULL)
4540 {
4541 find_sig_entry.signature = sig;
4542 sig_entry = htab_find (dwarf2_per_objfile->signatured_types,
4543 &find_sig_entry);
4544 if (sig_entry != NULL)
4545 return sig_entry;
4546 }
4547
4548 /* This is the "shouldn't happen" case.
4549 Try the DWP file and hope for the best. */
4550 if (dwp_file->tus == NULL)
4551 return NULL;
4552 dwo_entry = lookup_dwo_in_dwp (dwp_file, dwp_file->tus, NULL,
4553 sig, 1 /* is_debug_types */);
4554 if (dwo_entry == NULL)
4555 return NULL;
4556
4557 sig_entry = add_type_unit (sig);
4558 fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
4559
4560 /* The caller will signal a complaint if we return NULL.
4561 Here we don't return NULL but we still want to complain. */
4562 complaint (&symfile_complaints,
4563 _("Bad type signature %s referenced by %s at 0x%x,"
4564 " coping by using copy in DWP [in module %s]"),
4565 hex_string (sig),
4566 cu->per_cu->is_debug_types ? "TU" : "CU",
4567 cu->per_cu->offset.sect_off,
4568 objfile->name);
4569
4570 return sig_entry;
4571 }
4572
4573 /* Lookup a signature based type for DW_FORM_ref_sig8.
4574 Returns NULL if signature SIG is not present in the table.
4575 It is up to the caller to complain about this. */
4576
4577 static struct signatured_type *
4578 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
4579 {
4580 if (cu->dwo_unit
4581 && dwarf2_per_objfile->using_index)
4582 {
4583 /* We're in a DWO/DWP file, and we're using .gdb_index.
4584 These cases require special processing. */
4585 if (get_dwp_file () == NULL)
4586 return lookup_dwo_signatured_type (cu, sig);
4587 else
4588 return lookup_dwp_signatured_type (cu, sig);
4589 }
4590 else
4591 {
4592 struct signatured_type find_entry, *entry;
4593
4594 if (dwarf2_per_objfile->signatured_types == NULL)
4595 return NULL;
4596 find_entry.signature = sig;
4597 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
4598 return entry;
4599 }
4600 }
4601 \f
4602 /* Low level DIE reading support. */
4603
4604 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
4605
4606 static void
4607 init_cu_die_reader (struct die_reader_specs *reader,
4608 struct dwarf2_cu *cu,
4609 struct dwarf2_section_info *section,
4610 struct dwo_file *dwo_file)
4611 {
4612 gdb_assert (section->readin && section->buffer != NULL);
4613 reader->abfd = section->asection->owner;
4614 reader->cu = cu;
4615 reader->dwo_file = dwo_file;
4616 reader->die_section = section;
4617 reader->buffer = section->buffer;
4618 reader->buffer_end = section->buffer + section->size;
4619 reader->comp_dir = NULL;
4620 }
4621
4622 /* Subroutine of init_cutu_and_read_dies to simplify it.
4623 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
4624 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
4625 already.
4626
4627 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
4628 from it to the DIE in the DWO. If NULL we are skipping the stub.
4629 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
4630 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
4631 attribute of the referencing CU. Exactly one of STUB_COMP_UNIT_DIE and
4632 COMP_DIR must be non-NULL.
4633 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
4634 are filled in with the info of the DIE from the DWO file.
4635 ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
4636 provided an abbrev table to use.
4637 The result is non-zero if a valid (non-dummy) DIE was found. */
4638
4639 static int
4640 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
4641 struct dwo_unit *dwo_unit,
4642 int abbrev_table_provided,
4643 struct die_info *stub_comp_unit_die,
4644 const char *stub_comp_dir,
4645 struct die_reader_specs *result_reader,
4646 const gdb_byte **result_info_ptr,
4647 struct die_info **result_comp_unit_die,
4648 int *result_has_children)
4649 {
4650 struct objfile *objfile = dwarf2_per_objfile->objfile;
4651 struct dwarf2_cu *cu = this_cu->cu;
4652 struct dwarf2_section_info *section;
4653 bfd *abfd;
4654 const gdb_byte *begin_info_ptr, *info_ptr;
4655 const char *comp_dir_string;
4656 ULONGEST signature; /* Or dwo_id. */
4657 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
4658 int i,num_extra_attrs;
4659 struct dwarf2_section_info *dwo_abbrev_section;
4660 struct attribute *attr;
4661 struct attribute comp_dir_attr;
4662 struct die_info *comp_unit_die;
4663
4664 /* Both can't be provided. */
4665 gdb_assert (! (stub_comp_unit_die && stub_comp_dir));
4666
4667 /* These attributes aren't processed until later:
4668 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
4669 However, the attribute is found in the stub which we won't have later.
4670 In order to not impose this complication on the rest of the code,
4671 we read them here and copy them to the DWO CU/TU die. */
4672
4673 stmt_list = NULL;
4674 low_pc = NULL;
4675 high_pc = NULL;
4676 ranges = NULL;
4677 comp_dir = NULL;
4678
4679 if (stub_comp_unit_die != NULL)
4680 {
4681 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
4682 DWO file. */
4683 if (! this_cu->is_debug_types)
4684 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
4685 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
4686 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
4687 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
4688 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
4689
4690 /* There should be a DW_AT_addr_base attribute here (if needed).
4691 We need the value before we can process DW_FORM_GNU_addr_index. */
4692 cu->addr_base = 0;
4693 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
4694 if (attr)
4695 cu->addr_base = DW_UNSND (attr);
4696
4697 /* There should be a DW_AT_ranges_base attribute here (if needed).
4698 We need the value before we can process DW_AT_ranges. */
4699 cu->ranges_base = 0;
4700 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
4701 if (attr)
4702 cu->ranges_base = DW_UNSND (attr);
4703 }
4704 else if (stub_comp_dir != NULL)
4705 {
4706 /* Reconstruct the comp_dir attribute to simplify the code below. */
4707 comp_dir = (struct attribute *)
4708 obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
4709 comp_dir->name = DW_AT_comp_dir;
4710 comp_dir->form = DW_FORM_string;
4711 DW_STRING_IS_CANONICAL (comp_dir) = 0;
4712 DW_STRING (comp_dir) = stub_comp_dir;
4713 }
4714
4715 /* Set up for reading the DWO CU/TU. */
4716 cu->dwo_unit = dwo_unit;
4717 section = dwo_unit->section;
4718 dwarf2_read_section (objfile, section);
4719 abfd = section->asection->owner;
4720 begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
4721 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
4722 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
4723
4724 if (this_cu->is_debug_types)
4725 {
4726 ULONGEST header_signature;
4727 cu_offset type_offset_in_tu;
4728 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
4729
4730 info_ptr = read_and_check_type_unit_head (&cu->header, section,
4731 dwo_abbrev_section,
4732 info_ptr,
4733 &header_signature,
4734 &type_offset_in_tu);
4735 /* This is not an assert because it can be caused by bad debug info. */
4736 if (sig_type->signature != header_signature)
4737 {
4738 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
4739 " TU at offset 0x%x [in module %s]"),
4740 hex_string (sig_type->signature),
4741 hex_string (header_signature),
4742 dwo_unit->offset.sect_off,
4743 bfd_get_filename (abfd));
4744 }
4745 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4746 /* For DWOs coming from DWP files, we don't know the CU length
4747 nor the type's offset in the TU until now. */
4748 dwo_unit->length = get_cu_length (&cu->header);
4749 dwo_unit->type_offset_in_tu = type_offset_in_tu;
4750
4751 /* Establish the type offset that can be used to lookup the type.
4752 For DWO files, we don't know it until now. */
4753 sig_type->type_offset_in_section.sect_off =
4754 dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
4755 }
4756 else
4757 {
4758 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
4759 dwo_abbrev_section,
4760 info_ptr, 0);
4761 gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
4762 /* For DWOs coming from DWP files, we don't know the CU length
4763 until now. */
4764 dwo_unit->length = get_cu_length (&cu->header);
4765 }
4766
4767 /* Replace the CU's original abbrev table with the DWO's.
4768 Reminder: We can't read the abbrev table until we've read the header. */
4769 if (abbrev_table_provided)
4770 {
4771 /* Don't free the provided abbrev table, the caller of
4772 init_cutu_and_read_dies owns it. */
4773 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4774 /* Ensure the DWO abbrev table gets freed. */
4775 make_cleanup (dwarf2_free_abbrev_table, cu);
4776 }
4777 else
4778 {
4779 dwarf2_free_abbrev_table (cu);
4780 dwarf2_read_abbrevs (cu, dwo_abbrev_section);
4781 /* Leave any existing abbrev table cleanup as is. */
4782 }
4783
4784 /* Read in the die, but leave space to copy over the attributes
4785 from the stub. This has the benefit of simplifying the rest of
4786 the code - all the work to maintain the illusion of a single
4787 DW_TAG_{compile,type}_unit DIE is done here. */
4788 num_extra_attrs = ((stmt_list != NULL)
4789 + (low_pc != NULL)
4790 + (high_pc != NULL)
4791 + (ranges != NULL)
4792 + (comp_dir != NULL));
4793 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
4794 result_has_children, num_extra_attrs);
4795
4796 /* Copy over the attributes from the stub to the DIE we just read in. */
4797 comp_unit_die = *result_comp_unit_die;
4798 i = comp_unit_die->num_attrs;
4799 if (stmt_list != NULL)
4800 comp_unit_die->attrs[i++] = *stmt_list;
4801 if (low_pc != NULL)
4802 comp_unit_die->attrs[i++] = *low_pc;
4803 if (high_pc != NULL)
4804 comp_unit_die->attrs[i++] = *high_pc;
4805 if (ranges != NULL)
4806 comp_unit_die->attrs[i++] = *ranges;
4807 if (comp_dir != NULL)
4808 comp_unit_die->attrs[i++] = *comp_dir;
4809 comp_unit_die->num_attrs += num_extra_attrs;
4810
4811 if (dwarf2_die_debug)
4812 {
4813 fprintf_unfiltered (gdb_stdlog,
4814 "Read die from %s@0x%x of %s:\n",
4815 bfd_section_name (abfd, section->asection),
4816 (unsigned) (begin_info_ptr - section->buffer),
4817 bfd_get_filename (abfd));
4818 dump_die (comp_unit_die, dwarf2_die_debug);
4819 }
4820
4821 /* Save the comp_dir attribute. If there is no DWP file then we'll read
4822 TUs by skipping the stub and going directly to the entry in the DWO file.
4823 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
4824 to get it via circuitous means. Blech. */
4825 if (comp_dir != NULL)
4826 result_reader->comp_dir = DW_STRING (comp_dir);
4827
4828 /* Skip dummy compilation units. */
4829 if (info_ptr >= begin_info_ptr + dwo_unit->length
4830 || peek_abbrev_code (abfd, info_ptr) == 0)
4831 return 0;
4832
4833 *result_info_ptr = info_ptr;
4834 return 1;
4835 }
4836
4837 /* Subroutine of init_cutu_and_read_dies to simplify it.
4838 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
4839 Returns NULL if the specified DWO unit cannot be found. */
4840
4841 static struct dwo_unit *
4842 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
4843 struct die_info *comp_unit_die)
4844 {
4845 struct dwarf2_cu *cu = this_cu->cu;
4846 struct attribute *attr;
4847 ULONGEST signature;
4848 struct dwo_unit *dwo_unit;
4849 const char *comp_dir, *dwo_name;
4850
4851 gdb_assert (cu != NULL);
4852
4853 /* Yeah, we look dwo_name up again, but it simplifies the code. */
4854 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
4855 gdb_assert (attr != NULL);
4856 dwo_name = DW_STRING (attr);
4857 comp_dir = NULL;
4858 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
4859 if (attr)
4860 comp_dir = DW_STRING (attr);
4861
4862 if (this_cu->is_debug_types)
4863 {
4864 struct signatured_type *sig_type;
4865
4866 /* Since this_cu is the first member of struct signatured_type,
4867 we can go from a pointer to one to a pointer to the other. */
4868 sig_type = (struct signatured_type *) this_cu;
4869 signature = sig_type->signature;
4870 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
4871 }
4872 else
4873 {
4874 struct attribute *attr;
4875
4876 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
4877 if (! attr)
4878 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
4879 " [in module %s]"),
4880 dwo_name, this_cu->objfile->name);
4881 signature = DW_UNSND (attr);
4882 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
4883 signature);
4884 }
4885
4886 return dwo_unit;
4887 }
4888
4889 /* Subroutine of init_cutu_and_read_dies to simplify it.
4890 Read a TU directly from a DWO file, bypassing the stub. */
4891
4892 static void
4893 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu, int keep,
4894 die_reader_func_ftype *die_reader_func,
4895 void *data)
4896 {
4897 struct dwarf2_cu *cu;
4898 struct signatured_type *sig_type;
4899 struct cleanup *cleanups, *free_cu_cleanup;
4900 struct die_reader_specs reader;
4901 const gdb_byte *info_ptr;
4902 struct die_info *comp_unit_die;
4903 int has_children;
4904
4905 /* Verify we can do the following downcast, and that we have the
4906 data we need. */
4907 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
4908 sig_type = (struct signatured_type *) this_cu;
4909 gdb_assert (sig_type->dwo_unit != NULL);
4910
4911 cleanups = make_cleanup (null_cleanup, NULL);
4912
4913 gdb_assert (this_cu->cu == NULL);
4914 cu = xmalloc (sizeof (*cu));
4915 init_one_comp_unit (cu, this_cu);
4916 /* If an error occurs while loading, release our storage. */
4917 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4918
4919 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
4920 0 /* abbrev_table_provided */,
4921 NULL /* stub_comp_unit_die */,
4922 sig_type->dwo_unit->dwo_file->comp_dir,
4923 &reader, &info_ptr,
4924 &comp_unit_die, &has_children) == 0)
4925 {
4926 /* Dummy die. */
4927 do_cleanups (cleanups);
4928 return;
4929 }
4930
4931 /* All the "real" work is done here. */
4932 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
4933
4934 /* This duplicates some code in init_cutu_and_read_dies,
4935 but the alternative is making the latter more complex.
4936 This function is only for the special case of using DWO files directly:
4937 no point in overly complicating the general case just to handle this. */
4938 if (keep)
4939 {
4940 /* We've successfully allocated this compilation unit. Let our
4941 caller clean it up when finished with it. */
4942 discard_cleanups (free_cu_cleanup);
4943
4944 /* We can only discard free_cu_cleanup and all subsequent cleanups.
4945 So we have to manually free the abbrev table. */
4946 dwarf2_free_abbrev_table (cu);
4947
4948 /* Link this CU into read_in_chain. */
4949 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4950 dwarf2_per_objfile->read_in_chain = this_cu;
4951 }
4952 else
4953 do_cleanups (free_cu_cleanup);
4954
4955 do_cleanups (cleanups);
4956 }
4957
4958 /* Initialize a CU (or TU) and read its DIEs.
4959 If the CU defers to a DWO file, read the DWO file as well.
4960
4961 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
4962 Otherwise the table specified in the comp unit header is read in and used.
4963 This is an optimization for when we already have the abbrev table.
4964
4965 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
4966 Otherwise, a new CU is allocated with xmalloc.
4967
4968 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
4969 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
4970
4971 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
4972 linker) then DIE_READER_FUNC will not get called. */
4973
4974 static void
4975 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
4976 struct abbrev_table *abbrev_table,
4977 int use_existing_cu, int keep,
4978 die_reader_func_ftype *die_reader_func,
4979 void *data)
4980 {
4981 struct objfile *objfile = dwarf2_per_objfile->objfile;
4982 struct dwarf2_section_info *section = this_cu->section;
4983 bfd *abfd = section->asection->owner;
4984 struct dwarf2_cu *cu;
4985 const gdb_byte *begin_info_ptr, *info_ptr;
4986 struct die_reader_specs reader;
4987 struct die_info *comp_unit_die;
4988 int has_children;
4989 struct attribute *attr;
4990 struct cleanup *cleanups, *free_cu_cleanup = NULL;
4991 struct signatured_type *sig_type = NULL;
4992 struct dwarf2_section_info *abbrev_section;
4993 /* Non-zero if CU currently points to a DWO file and we need to
4994 reread it. When this happens we need to reread the skeleton die
4995 before we can reread the DWO file (this only applies to CUs, not TUs). */
4996 int rereading_dwo_cu = 0;
4997
4998 if (dwarf2_die_debug)
4999 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5000 this_cu->is_debug_types ? "type" : "comp",
5001 this_cu->offset.sect_off);
5002
5003 if (use_existing_cu)
5004 gdb_assert (keep);
5005
5006 /* If we're reading a TU directly from a DWO file, including a virtual DWO
5007 file (instead of going through the stub), short-circuit all of this. */
5008 if (this_cu->reading_dwo_directly)
5009 {
5010 /* Narrow down the scope of possibilities to have to understand. */
5011 gdb_assert (this_cu->is_debug_types);
5012 gdb_assert (abbrev_table == NULL);
5013 gdb_assert (!use_existing_cu);
5014 init_tu_and_read_dwo_dies (this_cu, keep, die_reader_func, data);
5015 return;
5016 }
5017
5018 cleanups = make_cleanup (null_cleanup, NULL);
5019
5020 /* This is cheap if the section is already read in. */
5021 dwarf2_read_section (objfile, section);
5022
5023 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5024
5025 abbrev_section = get_abbrev_section_for_cu (this_cu);
5026
5027 if (use_existing_cu && this_cu->cu != NULL)
5028 {
5029 cu = this_cu->cu;
5030
5031 /* If this CU is from a DWO file we need to start over, we need to
5032 refetch the attributes from the skeleton CU.
5033 This could be optimized by retrieving those attributes from when we
5034 were here the first time: the previous comp_unit_die was stored in
5035 comp_unit_obstack. But there's no data yet that we need this
5036 optimization. */
5037 if (cu->dwo_unit != NULL)
5038 rereading_dwo_cu = 1;
5039 }
5040 else
5041 {
5042 /* If !use_existing_cu, this_cu->cu must be NULL. */
5043 gdb_assert (this_cu->cu == NULL);
5044
5045 cu = xmalloc (sizeof (*cu));
5046 init_one_comp_unit (cu, this_cu);
5047
5048 /* If an error occurs while loading, release our storage. */
5049 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
5050 }
5051
5052 /* Get the header. */
5053 if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
5054 {
5055 /* We already have the header, there's no need to read it in again. */
5056 info_ptr += cu->header.first_die_offset.cu_off;
5057 }
5058 else
5059 {
5060 if (this_cu->is_debug_types)
5061 {
5062 ULONGEST signature;
5063 cu_offset type_offset_in_tu;
5064
5065 info_ptr = read_and_check_type_unit_head (&cu->header, section,
5066 abbrev_section, info_ptr,
5067 &signature,
5068 &type_offset_in_tu);
5069
5070 /* Since per_cu is the first member of struct signatured_type,
5071 we can go from a pointer to one to a pointer to the other. */
5072 sig_type = (struct signatured_type *) this_cu;
5073 gdb_assert (sig_type->signature == signature);
5074 gdb_assert (sig_type->type_offset_in_tu.cu_off
5075 == type_offset_in_tu.cu_off);
5076 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5077
5078 /* LENGTH has not been set yet for type units if we're
5079 using .gdb_index. */
5080 this_cu->length = get_cu_length (&cu->header);
5081
5082 /* Establish the type offset that can be used to lookup the type. */
5083 sig_type->type_offset_in_section.sect_off =
5084 this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
5085 }
5086 else
5087 {
5088 info_ptr = read_and_check_comp_unit_head (&cu->header, section,
5089 abbrev_section,
5090 info_ptr, 0);
5091
5092 gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
5093 gdb_assert (this_cu->length == get_cu_length (&cu->header));
5094 }
5095 }
5096
5097 /* Skip dummy compilation units. */
5098 if (info_ptr >= begin_info_ptr + this_cu->length
5099 || peek_abbrev_code (abfd, info_ptr) == 0)
5100 {
5101 do_cleanups (cleanups);
5102 return;
5103 }
5104
5105 /* If we don't have them yet, read the abbrevs for this compilation unit.
5106 And if we need to read them now, make sure they're freed when we're
5107 done. Note that it's important that if the CU had an abbrev table
5108 on entry we don't free it when we're done: Somewhere up the call stack
5109 it may be in use. */
5110 if (abbrev_table != NULL)
5111 {
5112 gdb_assert (cu->abbrev_table == NULL);
5113 gdb_assert (cu->header.abbrev_offset.sect_off
5114 == abbrev_table->offset.sect_off);
5115 cu->abbrev_table = abbrev_table;
5116 }
5117 else if (cu->abbrev_table == NULL)
5118 {
5119 dwarf2_read_abbrevs (cu, abbrev_section);
5120 make_cleanup (dwarf2_free_abbrev_table, cu);
5121 }
5122 else if (rereading_dwo_cu)
5123 {
5124 dwarf2_free_abbrev_table (cu);
5125 dwarf2_read_abbrevs (cu, abbrev_section);
5126 }
5127
5128 /* Read the top level CU/TU die. */
5129 init_cu_die_reader (&reader, cu, section, NULL);
5130 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5131
5132 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
5133 from the DWO file.
5134 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
5135 DWO CU, that this test will fail (the attribute will not be present). */
5136 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
5137 if (attr)
5138 {
5139 struct dwo_unit *dwo_unit;
5140 struct die_info *dwo_comp_unit_die;
5141
5142 if (has_children)
5143 {
5144 complaint (&symfile_complaints,
5145 _("compilation unit with DW_AT_GNU_dwo_name"
5146 " has children (offset 0x%x) [in module %s]"),
5147 this_cu->offset.sect_off, bfd_get_filename (abfd));
5148 }
5149 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
5150 if (dwo_unit != NULL)
5151 {
5152 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
5153 abbrev_table != NULL,
5154 comp_unit_die, NULL,
5155 &reader, &info_ptr,
5156 &dwo_comp_unit_die, &has_children) == 0)
5157 {
5158 /* Dummy die. */
5159 do_cleanups (cleanups);
5160 return;
5161 }
5162 comp_unit_die = dwo_comp_unit_die;
5163 }
5164 else
5165 {
5166 /* Yikes, we couldn't find the rest of the DIE, we only have
5167 the stub. A complaint has already been logged. There's
5168 not much more we can do except pass on the stub DIE to
5169 die_reader_func. We don't want to throw an error on bad
5170 debug info. */
5171 }
5172 }
5173
5174 /* All of the above is setup for this call. Yikes. */
5175 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5176
5177 /* Done, clean up. */
5178 if (free_cu_cleanup != NULL)
5179 {
5180 if (keep)
5181 {
5182 /* We've successfully allocated this compilation unit. Let our
5183 caller clean it up when finished with it. */
5184 discard_cleanups (free_cu_cleanup);
5185
5186 /* We can only discard free_cu_cleanup and all subsequent cleanups.
5187 So we have to manually free the abbrev table. */
5188 dwarf2_free_abbrev_table (cu);
5189
5190 /* Link this CU into read_in_chain. */
5191 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
5192 dwarf2_per_objfile->read_in_chain = this_cu;
5193 }
5194 else
5195 do_cleanups (free_cu_cleanup);
5196 }
5197
5198 do_cleanups (cleanups);
5199 }
5200
5201 /* Read CU/TU THIS_CU in section SECTION,
5202 but do not follow DW_AT_GNU_dwo_name if present.
5203 DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
5204 to have already done the lookup to find the DWO/DWP file).
5205
5206 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
5207 THIS_CU->is_debug_types, but nothing else.
5208
5209 We fill in THIS_CU->length.
5210
5211 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
5212 linker) then DIE_READER_FUNC will not get called.
5213
5214 THIS_CU->cu is always freed when done.
5215 This is done in order to not leave THIS_CU->cu in a state where we have
5216 to care whether it refers to the "main" CU or the DWO CU. */
5217
5218 static void
5219 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
5220 struct dwarf2_section_info *abbrev_section,
5221 struct dwo_file *dwo_file,
5222 die_reader_func_ftype *die_reader_func,
5223 void *data)
5224 {
5225 struct objfile *objfile = dwarf2_per_objfile->objfile;
5226 struct dwarf2_section_info *section = this_cu->section;
5227 bfd *abfd = section->asection->owner;
5228 struct dwarf2_cu cu;
5229 const gdb_byte *begin_info_ptr, *info_ptr;
5230 struct die_reader_specs reader;
5231 struct cleanup *cleanups;
5232 struct die_info *comp_unit_die;
5233 int has_children;
5234
5235 if (dwarf2_die_debug)
5236 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
5237 this_cu->is_debug_types ? "type" : "comp",
5238 this_cu->offset.sect_off);
5239
5240 gdb_assert (this_cu->cu == NULL);
5241
5242 /* This is cheap if the section is already read in. */
5243 dwarf2_read_section (objfile, section);
5244
5245 init_one_comp_unit (&cu, this_cu);
5246
5247 cleanups = make_cleanup (free_stack_comp_unit, &cu);
5248
5249 begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
5250 info_ptr = read_and_check_comp_unit_head (&cu.header, section,
5251 abbrev_section, info_ptr,
5252 this_cu->is_debug_types);
5253
5254 this_cu->length = get_cu_length (&cu.header);
5255
5256 /* Skip dummy compilation units. */
5257 if (info_ptr >= begin_info_ptr + this_cu->length
5258 || peek_abbrev_code (abfd, info_ptr) == 0)
5259 {
5260 do_cleanups (cleanups);
5261 return;
5262 }
5263
5264 dwarf2_read_abbrevs (&cu, abbrev_section);
5265 make_cleanup (dwarf2_free_abbrev_table, &cu);
5266
5267 init_cu_die_reader (&reader, &cu, section, dwo_file);
5268 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
5269
5270 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
5271
5272 do_cleanups (cleanups);
5273 }
5274
5275 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
5276 does not lookup the specified DWO file.
5277 This cannot be used to read DWO files.
5278
5279 THIS_CU->cu is always freed when done.
5280 This is done in order to not leave THIS_CU->cu in a state where we have
5281 to care whether it refers to the "main" CU or the DWO CU.
5282 We can revisit this if the data shows there's a performance issue. */
5283
5284 static void
5285 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
5286 die_reader_func_ftype *die_reader_func,
5287 void *data)
5288 {
5289 init_cutu_and_read_dies_no_follow (this_cu,
5290 get_abbrev_section_for_cu (this_cu),
5291 NULL,
5292 die_reader_func, data);
5293 }
5294 \f
5295 /* Type Unit Groups.
5296
5297 Type Unit Groups are a way to collapse the set of all TUs (type units) into
5298 a more manageable set. The grouping is done by DW_AT_stmt_list entry
5299 so that all types coming from the same compilation (.o file) are grouped
5300 together. A future step could be to put the types in the same symtab as
5301 the CU the types ultimately came from. */
5302
5303 static hashval_t
5304 hash_type_unit_group (const void *item)
5305 {
5306 const struct type_unit_group *tu_group = item;
5307
5308 return hash_stmt_list_entry (&tu_group->hash);
5309 }
5310
5311 static int
5312 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
5313 {
5314 const struct type_unit_group *lhs = item_lhs;
5315 const struct type_unit_group *rhs = item_rhs;
5316
5317 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
5318 }
5319
5320 /* Allocate a hash table for type unit groups. */
5321
5322 static htab_t
5323 allocate_type_unit_groups_table (void)
5324 {
5325 return htab_create_alloc_ex (3,
5326 hash_type_unit_group,
5327 eq_type_unit_group,
5328 NULL,
5329 &dwarf2_per_objfile->objfile->objfile_obstack,
5330 hashtab_obstack_allocate,
5331 dummy_obstack_deallocate);
5332 }
5333
5334 /* Type units that don't have DW_AT_stmt_list are grouped into their own
5335 partial symtabs. We combine several TUs per psymtab to not let the size
5336 of any one psymtab grow too big. */
5337 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
5338 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
5339
5340 /* Helper routine for get_type_unit_group.
5341 Create the type_unit_group object used to hold one or more TUs. */
5342
5343 static struct type_unit_group *
5344 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
5345 {
5346 struct objfile *objfile = dwarf2_per_objfile->objfile;
5347 struct dwarf2_per_cu_data *per_cu;
5348 struct type_unit_group *tu_group;
5349
5350 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5351 struct type_unit_group);
5352 per_cu = &tu_group->per_cu;
5353 per_cu->objfile = objfile;
5354
5355 if (dwarf2_per_objfile->using_index)
5356 {
5357 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5358 struct dwarf2_per_cu_quick_data);
5359 }
5360 else
5361 {
5362 unsigned int line_offset = line_offset_struct.sect_off;
5363 struct partial_symtab *pst;
5364 char *name;
5365
5366 /* Give the symtab a useful name for debug purposes. */
5367 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
5368 name = xstrprintf ("<type_units_%d>",
5369 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
5370 else
5371 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
5372
5373 pst = create_partial_symtab (per_cu, name);
5374 pst->anonymous = 1;
5375
5376 xfree (name);
5377 }
5378
5379 tu_group->hash.dwo_unit = cu->dwo_unit;
5380 tu_group->hash.line_offset = line_offset_struct;
5381
5382 return tu_group;
5383 }
5384
5385 /* Look up the type_unit_group for type unit CU, and create it if necessary.
5386 STMT_LIST is a DW_AT_stmt_list attribute. */
5387
5388 static struct type_unit_group *
5389 get_type_unit_group (struct dwarf2_cu *cu, struct attribute *stmt_list)
5390 {
5391 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5392 struct type_unit_group *tu_group;
5393 void **slot;
5394 unsigned int line_offset;
5395 struct type_unit_group type_unit_group_for_lookup;
5396
5397 if (dwarf2_per_objfile->type_unit_groups == NULL)
5398 {
5399 dwarf2_per_objfile->type_unit_groups =
5400 allocate_type_unit_groups_table ();
5401 }
5402
5403 /* Do we need to create a new group, or can we use an existing one? */
5404
5405 if (stmt_list)
5406 {
5407 line_offset = DW_UNSND (stmt_list);
5408 ++tu_stats->nr_symtab_sharers;
5409 }
5410 else
5411 {
5412 /* Ugh, no stmt_list. Rare, but we have to handle it.
5413 We can do various things here like create one group per TU or
5414 spread them over multiple groups to split up the expansion work.
5415 To avoid worst case scenarios (too many groups or too large groups)
5416 we, umm, group them in bunches. */
5417 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
5418 | (tu_stats->nr_stmt_less_type_units
5419 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
5420 ++tu_stats->nr_stmt_less_type_units;
5421 }
5422
5423 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
5424 type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
5425 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
5426 &type_unit_group_for_lookup, INSERT);
5427 if (*slot != NULL)
5428 {
5429 tu_group = *slot;
5430 gdb_assert (tu_group != NULL);
5431 }
5432 else
5433 {
5434 sect_offset line_offset_struct;
5435
5436 line_offset_struct.sect_off = line_offset;
5437 tu_group = create_type_unit_group (cu, line_offset_struct);
5438 *slot = tu_group;
5439 ++tu_stats->nr_symtabs;
5440 }
5441
5442 return tu_group;
5443 }
5444
5445 /* Struct used to sort TUs by their abbreviation table offset. */
5446
5447 struct tu_abbrev_offset
5448 {
5449 struct signatured_type *sig_type;
5450 sect_offset abbrev_offset;
5451 };
5452
5453 /* Helper routine for build_type_unit_groups, passed to qsort. */
5454
5455 static int
5456 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
5457 {
5458 const struct tu_abbrev_offset * const *a = ap;
5459 const struct tu_abbrev_offset * const *b = bp;
5460 unsigned int aoff = (*a)->abbrev_offset.sect_off;
5461 unsigned int boff = (*b)->abbrev_offset.sect_off;
5462
5463 return (aoff > boff) - (aoff < boff);
5464 }
5465
5466 /* A helper function to add a type_unit_group to a table. */
5467
5468 static int
5469 add_type_unit_group_to_table (void **slot, void *datum)
5470 {
5471 struct type_unit_group *tu_group = *slot;
5472 struct type_unit_group ***datap = datum;
5473
5474 **datap = tu_group;
5475 ++*datap;
5476
5477 return 1;
5478 }
5479
5480 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
5481 each one passing FUNC,DATA.
5482
5483 The efficiency is because we sort TUs by the abbrev table they use and
5484 only read each abbrev table once. In one program there are 200K TUs
5485 sharing 8K abbrev tables.
5486
5487 The main purpose of this function is to support building the
5488 dwarf2_per_objfile->type_unit_groups table.
5489 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
5490 can collapse the search space by grouping them by stmt_list.
5491 The savings can be significant, in the same program from above the 200K TUs
5492 share 8K stmt_list tables.
5493
5494 FUNC is expected to call get_type_unit_group, which will create the
5495 struct type_unit_group if necessary and add it to
5496 dwarf2_per_objfile->type_unit_groups. */
5497
5498 static void
5499 build_type_unit_groups (die_reader_func_ftype *func, void *data)
5500 {
5501 struct objfile *objfile = dwarf2_per_objfile->objfile;
5502 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
5503 struct cleanup *cleanups;
5504 struct abbrev_table *abbrev_table;
5505 sect_offset abbrev_offset;
5506 struct tu_abbrev_offset *sorted_by_abbrev;
5507 struct type_unit_group **iter;
5508 int i;
5509
5510 /* It's up to the caller to not call us multiple times. */
5511 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
5512
5513 if (dwarf2_per_objfile->n_type_units == 0)
5514 return;
5515
5516 /* TUs typically share abbrev tables, and there can be way more TUs than
5517 abbrev tables. Sort by abbrev table to reduce the number of times we
5518 read each abbrev table in.
5519 Alternatives are to punt or to maintain a cache of abbrev tables.
5520 This is simpler and efficient enough for now.
5521
5522 Later we group TUs by their DW_AT_stmt_list value (as this defines the
5523 symtab to use). Typically TUs with the same abbrev offset have the same
5524 stmt_list value too so in practice this should work well.
5525
5526 The basic algorithm here is:
5527
5528 sort TUs by abbrev table
5529 for each TU with same abbrev table:
5530 read abbrev table if first user
5531 read TU top level DIE
5532 [IWBN if DWO skeletons had DW_AT_stmt_list]
5533 call FUNC */
5534
5535 if (dwarf2_read_debug)
5536 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
5537
5538 /* Sort in a separate table to maintain the order of all_type_units
5539 for .gdb_index: TU indices directly index all_type_units. */
5540 sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
5541 dwarf2_per_objfile->n_type_units);
5542 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5543 {
5544 struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
5545
5546 sorted_by_abbrev[i].sig_type = sig_type;
5547 sorted_by_abbrev[i].abbrev_offset =
5548 read_abbrev_offset (sig_type->per_cu.section,
5549 sig_type->per_cu.offset);
5550 }
5551 cleanups = make_cleanup (xfree, sorted_by_abbrev);
5552 qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
5553 sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
5554
5555 /* Note: In the .gdb_index case, get_type_unit_group may have already been
5556 called any number of times, so we don't reset tu_stats here. */
5557
5558 abbrev_offset.sect_off = ~(unsigned) 0;
5559 abbrev_table = NULL;
5560 make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
5561
5562 for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
5563 {
5564 const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
5565
5566 /* Switch to the next abbrev table if necessary. */
5567 if (abbrev_table == NULL
5568 || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
5569 {
5570 if (abbrev_table != NULL)
5571 {
5572 abbrev_table_free (abbrev_table);
5573 /* Reset to NULL in case abbrev_table_read_table throws
5574 an error: abbrev_table_free_cleanup will get called. */
5575 abbrev_table = NULL;
5576 }
5577 abbrev_offset = tu->abbrev_offset;
5578 abbrev_table =
5579 abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
5580 abbrev_offset);
5581 ++tu_stats->nr_uniq_abbrev_tables;
5582 }
5583
5584 init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
5585 func, data);
5586 }
5587
5588 /* type_unit_groups can be NULL if there is an error in the debug info.
5589 Just create an empty table so the rest of gdb doesn't have to watch
5590 for this error case. */
5591 if (dwarf2_per_objfile->type_unit_groups == NULL)
5592 {
5593 dwarf2_per_objfile->type_unit_groups =
5594 allocate_type_unit_groups_table ();
5595 dwarf2_per_objfile->n_type_unit_groups = 0;
5596 }
5597
5598 /* Create a vector of pointers to primary type units to make it easy to
5599 iterate over them and CUs. See dw2_get_primary_cu. */
5600 dwarf2_per_objfile->n_type_unit_groups =
5601 htab_elements (dwarf2_per_objfile->type_unit_groups);
5602 dwarf2_per_objfile->all_type_unit_groups =
5603 obstack_alloc (&objfile->objfile_obstack,
5604 dwarf2_per_objfile->n_type_unit_groups
5605 * sizeof (struct type_unit_group *));
5606 iter = &dwarf2_per_objfile->all_type_unit_groups[0];
5607 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5608 add_type_unit_group_to_table, &iter);
5609 gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
5610 == dwarf2_per_objfile->n_type_unit_groups);
5611
5612 do_cleanups (cleanups);
5613
5614 if (dwarf2_read_debug)
5615 {
5616 fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
5617 fprintf_unfiltered (gdb_stdlog, " %d TUs\n",
5618 dwarf2_per_objfile->n_type_units);
5619 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
5620 tu_stats->nr_uniq_abbrev_tables);
5621 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
5622 tu_stats->nr_symtabs);
5623 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
5624 tu_stats->nr_symtab_sharers);
5625 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
5626 tu_stats->nr_stmt_less_type_units);
5627 }
5628 }
5629 \f
5630 /* Partial symbol tables. */
5631
5632 /* Create a psymtab named NAME and assign it to PER_CU.
5633
5634 The caller must fill in the following details:
5635 dirname, textlow, texthigh. */
5636
5637 static struct partial_symtab *
5638 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
5639 {
5640 struct objfile *objfile = per_cu->objfile;
5641 struct partial_symtab *pst;
5642
5643 pst = start_psymtab_common (objfile, objfile->section_offsets,
5644 name, 0,
5645 objfile->global_psymbols.next,
5646 objfile->static_psymbols.next);
5647
5648 pst->psymtabs_addrmap_supported = 1;
5649
5650 /* This is the glue that links PST into GDB's symbol API. */
5651 pst->read_symtab_private = per_cu;
5652 pst->read_symtab = dwarf2_read_symtab;
5653 per_cu->v.psymtab = pst;
5654
5655 return pst;
5656 }
5657
5658 /* die_reader_func for process_psymtab_comp_unit. */
5659
5660 static void
5661 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
5662 const gdb_byte *info_ptr,
5663 struct die_info *comp_unit_die,
5664 int has_children,
5665 void *data)
5666 {
5667 struct dwarf2_cu *cu = reader->cu;
5668 struct objfile *objfile = cu->objfile;
5669 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5670 struct attribute *attr;
5671 CORE_ADDR baseaddr;
5672 CORE_ADDR best_lowpc = 0, best_highpc = 0;
5673 struct partial_symtab *pst;
5674 int has_pc_info;
5675 const char *filename;
5676 int *want_partial_unit_ptr = data;
5677
5678 if (comp_unit_die->tag == DW_TAG_partial_unit
5679 && (want_partial_unit_ptr == NULL
5680 || !*want_partial_unit_ptr))
5681 return;
5682
5683 gdb_assert (! per_cu->is_debug_types);
5684
5685 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
5686
5687 cu->list_in_scope = &file_symbols;
5688
5689 /* Allocate a new partial symbol table structure. */
5690 attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
5691 if (attr == NULL || !DW_STRING (attr))
5692 filename = "";
5693 else
5694 filename = DW_STRING (attr);
5695
5696 pst = create_partial_symtab (per_cu, filename);
5697
5698 /* This must be done before calling dwarf2_build_include_psymtabs. */
5699 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
5700 if (attr != NULL)
5701 pst->dirname = DW_STRING (attr);
5702
5703 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5704
5705 dwarf2_find_base_address (comp_unit_die, cu);
5706
5707 /* Possibly set the default values of LOWPC and HIGHPC from
5708 `DW_AT_ranges'. */
5709 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
5710 &best_highpc, cu, pst);
5711 if (has_pc_info == 1 && best_lowpc < best_highpc)
5712 /* Store the contiguous range if it is not empty; it can be empty for
5713 CUs with no code. */
5714 addrmap_set_empty (objfile->psymtabs_addrmap,
5715 best_lowpc + baseaddr,
5716 best_highpc + baseaddr - 1, pst);
5717
5718 /* Check if comp unit has_children.
5719 If so, read the rest of the partial symbols from this comp unit.
5720 If not, there's no more debug_info for this comp unit. */
5721 if (has_children)
5722 {
5723 struct partial_die_info *first_die;
5724 CORE_ADDR lowpc, highpc;
5725
5726 lowpc = ((CORE_ADDR) -1);
5727 highpc = ((CORE_ADDR) 0);
5728
5729 first_die = load_partial_dies (reader, info_ptr, 1);
5730
5731 scan_partial_symbols (first_die, &lowpc, &highpc,
5732 ! has_pc_info, cu);
5733
5734 /* If we didn't find a lowpc, set it to highpc to avoid
5735 complaints from `maint check'. */
5736 if (lowpc == ((CORE_ADDR) -1))
5737 lowpc = highpc;
5738
5739 /* If the compilation unit didn't have an explicit address range,
5740 then use the information extracted from its child dies. */
5741 if (! has_pc_info)
5742 {
5743 best_lowpc = lowpc;
5744 best_highpc = highpc;
5745 }
5746 }
5747 pst->textlow = best_lowpc + baseaddr;
5748 pst->texthigh = best_highpc + baseaddr;
5749
5750 pst->n_global_syms = objfile->global_psymbols.next -
5751 (objfile->global_psymbols.list + pst->globals_offset);
5752 pst->n_static_syms = objfile->static_psymbols.next -
5753 (objfile->static_psymbols.list + pst->statics_offset);
5754 sort_pst_symbols (objfile, pst);
5755
5756 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
5757 {
5758 int i;
5759 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5760 struct dwarf2_per_cu_data *iter;
5761
5762 /* Fill in 'dependencies' here; we fill in 'users' in a
5763 post-pass. */
5764 pst->number_of_dependencies = len;
5765 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5766 len * sizeof (struct symtab *));
5767 for (i = 0;
5768 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
5769 i, iter);
5770 ++i)
5771 pst->dependencies[i] = iter->v.psymtab;
5772
5773 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
5774 }
5775
5776 /* Get the list of files included in the current compilation unit,
5777 and build a psymtab for each of them. */
5778 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
5779
5780 if (dwarf2_read_debug)
5781 {
5782 struct gdbarch *gdbarch = get_objfile_arch (objfile);
5783
5784 fprintf_unfiltered (gdb_stdlog,
5785 "Psymtab for %s unit @0x%x: %s - %s"
5786 ", %d global, %d static syms\n",
5787 per_cu->is_debug_types ? "type" : "comp",
5788 per_cu->offset.sect_off,
5789 paddress (gdbarch, pst->textlow),
5790 paddress (gdbarch, pst->texthigh),
5791 pst->n_global_syms, pst->n_static_syms);
5792 }
5793 }
5794
5795 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5796 Process compilation unit THIS_CU for a psymtab. */
5797
5798 static void
5799 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
5800 int want_partial_unit)
5801 {
5802 /* If this compilation unit was already read in, free the
5803 cached copy in order to read it in again. This is
5804 necessary because we skipped some symbols when we first
5805 read in the compilation unit (see load_partial_dies).
5806 This problem could be avoided, but the benefit is unclear. */
5807 if (this_cu->cu != NULL)
5808 free_one_cached_comp_unit (this_cu);
5809
5810 gdb_assert (! this_cu->is_debug_types);
5811 init_cutu_and_read_dies (this_cu, NULL, 0, 0,
5812 process_psymtab_comp_unit_reader,
5813 &want_partial_unit);
5814
5815 /* Age out any secondary CUs. */
5816 age_cached_comp_units ();
5817 }
5818
5819 /* Reader function for build_type_psymtabs. */
5820
5821 static void
5822 build_type_psymtabs_reader (const struct die_reader_specs *reader,
5823 const gdb_byte *info_ptr,
5824 struct die_info *type_unit_die,
5825 int has_children,
5826 void *data)
5827 {
5828 struct objfile *objfile = dwarf2_per_objfile->objfile;
5829 struct dwarf2_cu *cu = reader->cu;
5830 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
5831 struct signatured_type *sig_type;
5832 struct type_unit_group *tu_group;
5833 struct attribute *attr;
5834 struct partial_die_info *first_die;
5835 CORE_ADDR lowpc, highpc;
5836 struct partial_symtab *pst;
5837
5838 gdb_assert (data == NULL);
5839 gdb_assert (per_cu->is_debug_types);
5840 sig_type = (struct signatured_type *) per_cu;
5841
5842 if (! has_children)
5843 return;
5844
5845 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
5846 tu_group = get_type_unit_group (cu, attr);
5847
5848 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
5849
5850 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
5851 cu->list_in_scope = &file_symbols;
5852 pst = create_partial_symtab (per_cu, "");
5853 pst->anonymous = 1;
5854
5855 first_die = load_partial_dies (reader, info_ptr, 1);
5856
5857 lowpc = (CORE_ADDR) -1;
5858 highpc = (CORE_ADDR) 0;
5859 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
5860
5861 pst->n_global_syms = objfile->global_psymbols.next -
5862 (objfile->global_psymbols.list + pst->globals_offset);
5863 pst->n_static_syms = objfile->static_psymbols.next -
5864 (objfile->static_psymbols.list + pst->statics_offset);
5865 sort_pst_symbols (objfile, pst);
5866 }
5867
5868 /* Traversal function for build_type_psymtabs. */
5869
5870 static int
5871 build_type_psymtab_dependencies (void **slot, void *info)
5872 {
5873 struct objfile *objfile = dwarf2_per_objfile->objfile;
5874 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
5875 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
5876 struct partial_symtab *pst = per_cu->v.psymtab;
5877 int len = VEC_length (sig_type_ptr, tu_group->tus);
5878 struct signatured_type *iter;
5879 int i;
5880
5881 gdb_assert (len > 0);
5882 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
5883
5884 pst->number_of_dependencies = len;
5885 pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
5886 len * sizeof (struct psymtab *));
5887 for (i = 0;
5888 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
5889 ++i)
5890 {
5891 gdb_assert (iter->per_cu.is_debug_types);
5892 pst->dependencies[i] = iter->per_cu.v.psymtab;
5893 iter->type_unit_group = tu_group;
5894 }
5895
5896 VEC_free (sig_type_ptr, tu_group->tus);
5897
5898 return 1;
5899 }
5900
5901 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
5902 Build partial symbol tables for the .debug_types comp-units. */
5903
5904 static void
5905 build_type_psymtabs (struct objfile *objfile)
5906 {
5907 if (! create_all_type_units (objfile))
5908 return;
5909
5910 build_type_unit_groups (build_type_psymtabs_reader, NULL);
5911
5912 /* Now that all TUs have been processed we can fill in the dependencies. */
5913 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
5914 build_type_psymtab_dependencies, NULL);
5915 }
5916
5917 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
5918
5919 static void
5920 psymtabs_addrmap_cleanup (void *o)
5921 {
5922 struct objfile *objfile = o;
5923
5924 objfile->psymtabs_addrmap = NULL;
5925 }
5926
5927 /* Compute the 'user' field for each psymtab in OBJFILE. */
5928
5929 static void
5930 set_partial_user (struct objfile *objfile)
5931 {
5932 int i;
5933
5934 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5935 {
5936 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5937 struct partial_symtab *pst = per_cu->v.psymtab;
5938 int j;
5939
5940 if (pst == NULL)
5941 continue;
5942
5943 for (j = 0; j < pst->number_of_dependencies; ++j)
5944 {
5945 /* Set the 'user' field only if it is not already set. */
5946 if (pst->dependencies[j]->user == NULL)
5947 pst->dependencies[j]->user = pst;
5948 }
5949 }
5950 }
5951
5952 /* Build the partial symbol table by doing a quick pass through the
5953 .debug_info and .debug_abbrev sections. */
5954
5955 static void
5956 dwarf2_build_psymtabs_hard (struct objfile *objfile)
5957 {
5958 struct cleanup *back_to, *addrmap_cleanup;
5959 struct obstack temp_obstack;
5960 int i;
5961
5962 if (dwarf2_read_debug)
5963 {
5964 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
5965 objfile->name);
5966 }
5967
5968 dwarf2_per_objfile->reading_partial_symbols = 1;
5969
5970 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
5971
5972 /* Any cached compilation units will be linked by the per-objfile
5973 read_in_chain. Make sure to free them when we're done. */
5974 back_to = make_cleanup (free_cached_comp_units, NULL);
5975
5976 build_type_psymtabs (objfile);
5977
5978 create_all_comp_units (objfile);
5979
5980 /* Create a temporary address map on a temporary obstack. We later
5981 copy this to the final obstack. */
5982 obstack_init (&temp_obstack);
5983 make_cleanup_obstack_free (&temp_obstack);
5984 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
5985 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
5986
5987 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
5988 {
5989 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
5990
5991 process_psymtab_comp_unit (per_cu, 0);
5992 }
5993
5994 set_partial_user (objfile);
5995
5996 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
5997 &objfile->objfile_obstack);
5998 discard_cleanups (addrmap_cleanup);
5999
6000 do_cleanups (back_to);
6001
6002 if (dwarf2_read_debug)
6003 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
6004 objfile->name);
6005 }
6006
6007 /* die_reader_func for load_partial_comp_unit. */
6008
6009 static void
6010 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
6011 const gdb_byte *info_ptr,
6012 struct die_info *comp_unit_die,
6013 int has_children,
6014 void *data)
6015 {
6016 struct dwarf2_cu *cu = reader->cu;
6017
6018 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
6019
6020 /* Check if comp unit has_children.
6021 If so, read the rest of the partial symbols from this comp unit.
6022 If not, there's no more debug_info for this comp unit. */
6023 if (has_children)
6024 load_partial_dies (reader, info_ptr, 0);
6025 }
6026
6027 /* Load the partial DIEs for a secondary CU into memory.
6028 This is also used when rereading a primary CU with load_all_dies. */
6029
6030 static void
6031 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
6032 {
6033 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
6034 load_partial_comp_unit_reader, NULL);
6035 }
6036
6037 static void
6038 read_comp_units_from_section (struct objfile *objfile,
6039 struct dwarf2_section_info *section,
6040 unsigned int is_dwz,
6041 int *n_allocated,
6042 int *n_comp_units,
6043 struct dwarf2_per_cu_data ***all_comp_units)
6044 {
6045 const gdb_byte *info_ptr;
6046 bfd *abfd = section->asection->owner;
6047
6048 if (dwarf2_read_debug)
6049 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
6050 section->asection->name, bfd_get_filename (abfd));
6051
6052 dwarf2_read_section (objfile, section);
6053
6054 info_ptr = section->buffer;
6055
6056 while (info_ptr < section->buffer + section->size)
6057 {
6058 unsigned int length, initial_length_size;
6059 struct dwarf2_per_cu_data *this_cu;
6060 sect_offset offset;
6061
6062 offset.sect_off = info_ptr - section->buffer;
6063
6064 /* Read just enough information to find out where the next
6065 compilation unit is. */
6066 length = read_initial_length (abfd, info_ptr, &initial_length_size);
6067
6068 /* Save the compilation unit for later lookup. */
6069 this_cu = obstack_alloc (&objfile->objfile_obstack,
6070 sizeof (struct dwarf2_per_cu_data));
6071 memset (this_cu, 0, sizeof (*this_cu));
6072 this_cu->offset = offset;
6073 this_cu->length = length + initial_length_size;
6074 this_cu->is_dwz = is_dwz;
6075 this_cu->objfile = objfile;
6076 this_cu->section = section;
6077
6078 if (*n_comp_units == *n_allocated)
6079 {
6080 *n_allocated *= 2;
6081 *all_comp_units = xrealloc (*all_comp_units,
6082 *n_allocated
6083 * sizeof (struct dwarf2_per_cu_data *));
6084 }
6085 (*all_comp_units)[*n_comp_units] = this_cu;
6086 ++*n_comp_units;
6087
6088 info_ptr = info_ptr + this_cu->length;
6089 }
6090 }
6091
6092 /* Create a list of all compilation units in OBJFILE.
6093 This is only done for -readnow and building partial symtabs. */
6094
6095 static void
6096 create_all_comp_units (struct objfile *objfile)
6097 {
6098 int n_allocated;
6099 int n_comp_units;
6100 struct dwarf2_per_cu_data **all_comp_units;
6101
6102 n_comp_units = 0;
6103 n_allocated = 10;
6104 all_comp_units = xmalloc (n_allocated
6105 * sizeof (struct dwarf2_per_cu_data *));
6106
6107 read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
6108 &n_allocated, &n_comp_units, &all_comp_units);
6109
6110 if (bfd_get_section_by_name (objfile->obfd, ".gnu_debugaltlink") != NULL)
6111 {
6112 struct dwz_file *dwz = dwarf2_get_dwz_file ();
6113
6114 read_comp_units_from_section (objfile, &dwz->info, 1,
6115 &n_allocated, &n_comp_units,
6116 &all_comp_units);
6117 }
6118
6119 dwarf2_per_objfile->all_comp_units
6120 = obstack_alloc (&objfile->objfile_obstack,
6121 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6122 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
6123 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
6124 xfree (all_comp_units);
6125 dwarf2_per_objfile->n_comp_units = n_comp_units;
6126 }
6127
6128 /* Process all loaded DIEs for compilation unit CU, starting at
6129 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
6130 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
6131 DW_AT_ranges). If NEED_PC is set, then this function will set
6132 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
6133 and record the covered ranges in the addrmap. */
6134
6135 static void
6136 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
6137 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6138 {
6139 struct partial_die_info *pdi;
6140
6141 /* Now, march along the PDI's, descending into ones which have
6142 interesting children but skipping the children of the other ones,
6143 until we reach the end of the compilation unit. */
6144
6145 pdi = first_die;
6146
6147 while (pdi != NULL)
6148 {
6149 fixup_partial_die (pdi, cu);
6150
6151 /* Anonymous namespaces or modules have no name but have interesting
6152 children, so we need to look at them. Ditto for anonymous
6153 enums. */
6154
6155 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
6156 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
6157 || pdi->tag == DW_TAG_imported_unit)
6158 {
6159 switch (pdi->tag)
6160 {
6161 case DW_TAG_subprogram:
6162 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6163 break;
6164 case DW_TAG_constant:
6165 case DW_TAG_variable:
6166 case DW_TAG_typedef:
6167 case DW_TAG_union_type:
6168 if (!pdi->is_declaration)
6169 {
6170 add_partial_symbol (pdi, cu);
6171 }
6172 break;
6173 case DW_TAG_class_type:
6174 case DW_TAG_interface_type:
6175 case DW_TAG_structure_type:
6176 if (!pdi->is_declaration)
6177 {
6178 add_partial_symbol (pdi, cu);
6179 }
6180 break;
6181 case DW_TAG_enumeration_type:
6182 if (!pdi->is_declaration)
6183 add_partial_enumeration (pdi, cu);
6184 break;
6185 case DW_TAG_base_type:
6186 case DW_TAG_subrange_type:
6187 /* File scope base type definitions are added to the partial
6188 symbol table. */
6189 add_partial_symbol (pdi, cu);
6190 break;
6191 case DW_TAG_namespace:
6192 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
6193 break;
6194 case DW_TAG_module:
6195 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
6196 break;
6197 case DW_TAG_imported_unit:
6198 {
6199 struct dwarf2_per_cu_data *per_cu;
6200
6201 /* For now we don't handle imported units in type units. */
6202 if (cu->per_cu->is_debug_types)
6203 {
6204 error (_("Dwarf Error: DW_TAG_imported_unit is not"
6205 " supported in type units [in module %s]"),
6206 cu->objfile->name);
6207 }
6208
6209 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
6210 pdi->is_dwz,
6211 cu->objfile);
6212
6213 /* Go read the partial unit, if needed. */
6214 if (per_cu->v.psymtab == NULL)
6215 process_psymtab_comp_unit (per_cu, 1);
6216
6217 VEC_safe_push (dwarf2_per_cu_ptr,
6218 cu->per_cu->imported_symtabs, per_cu);
6219 }
6220 break;
6221 default:
6222 break;
6223 }
6224 }
6225
6226 /* If the die has a sibling, skip to the sibling. */
6227
6228 pdi = pdi->die_sibling;
6229 }
6230 }
6231
6232 /* Functions used to compute the fully scoped name of a partial DIE.
6233
6234 Normally, this is simple. For C++, the parent DIE's fully scoped
6235 name is concatenated with "::" and the partial DIE's name. For
6236 Java, the same thing occurs except that "." is used instead of "::".
6237 Enumerators are an exception; they use the scope of their parent
6238 enumeration type, i.e. the name of the enumeration type is not
6239 prepended to the enumerator.
6240
6241 There are two complexities. One is DW_AT_specification; in this
6242 case "parent" means the parent of the target of the specification,
6243 instead of the direct parent of the DIE. The other is compilers
6244 which do not emit DW_TAG_namespace; in this case we try to guess
6245 the fully qualified name of structure types from their members'
6246 linkage names. This must be done using the DIE's children rather
6247 than the children of any DW_AT_specification target. We only need
6248 to do this for structures at the top level, i.e. if the target of
6249 any DW_AT_specification (if any; otherwise the DIE itself) does not
6250 have a parent. */
6251
6252 /* Compute the scope prefix associated with PDI's parent, in
6253 compilation unit CU. The result will be allocated on CU's
6254 comp_unit_obstack, or a copy of the already allocated PDI->NAME
6255 field. NULL is returned if no prefix is necessary. */
6256 static const char *
6257 partial_die_parent_scope (struct partial_die_info *pdi,
6258 struct dwarf2_cu *cu)
6259 {
6260 const char *grandparent_scope;
6261 struct partial_die_info *parent, *real_pdi;
6262
6263 /* We need to look at our parent DIE; if we have a DW_AT_specification,
6264 then this means the parent of the specification DIE. */
6265
6266 real_pdi = pdi;
6267 while (real_pdi->has_specification)
6268 real_pdi = find_partial_die (real_pdi->spec_offset,
6269 real_pdi->spec_is_dwz, cu);
6270
6271 parent = real_pdi->die_parent;
6272 if (parent == NULL)
6273 return NULL;
6274
6275 if (parent->scope_set)
6276 return parent->scope;
6277
6278 fixup_partial_die (parent, cu);
6279
6280 grandparent_scope = partial_die_parent_scope (parent, cu);
6281
6282 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
6283 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
6284 Work around this problem here. */
6285 if (cu->language == language_cplus
6286 && parent->tag == DW_TAG_namespace
6287 && strcmp (parent->name, "::") == 0
6288 && grandparent_scope == NULL)
6289 {
6290 parent->scope = NULL;
6291 parent->scope_set = 1;
6292 return NULL;
6293 }
6294
6295 if (pdi->tag == DW_TAG_enumerator)
6296 /* Enumerators should not get the name of the enumeration as a prefix. */
6297 parent->scope = grandparent_scope;
6298 else if (parent->tag == DW_TAG_namespace
6299 || parent->tag == DW_TAG_module
6300 || parent->tag == DW_TAG_structure_type
6301 || parent->tag == DW_TAG_class_type
6302 || parent->tag == DW_TAG_interface_type
6303 || parent->tag == DW_TAG_union_type
6304 || parent->tag == DW_TAG_enumeration_type)
6305 {
6306 if (grandparent_scope == NULL)
6307 parent->scope = parent->name;
6308 else
6309 parent->scope = typename_concat (&cu->comp_unit_obstack,
6310 grandparent_scope,
6311 parent->name, 0, cu);
6312 }
6313 else
6314 {
6315 /* FIXME drow/2004-04-01: What should we be doing with
6316 function-local names? For partial symbols, we should probably be
6317 ignoring them. */
6318 complaint (&symfile_complaints,
6319 _("unhandled containing DIE tag %d for DIE at %d"),
6320 parent->tag, pdi->offset.sect_off);
6321 parent->scope = grandparent_scope;
6322 }
6323
6324 parent->scope_set = 1;
6325 return parent->scope;
6326 }
6327
6328 /* Return the fully scoped name associated with PDI, from compilation unit
6329 CU. The result will be allocated with malloc. */
6330
6331 static char *
6332 partial_die_full_name (struct partial_die_info *pdi,
6333 struct dwarf2_cu *cu)
6334 {
6335 const char *parent_scope;
6336
6337 /* If this is a template instantiation, we can not work out the
6338 template arguments from partial DIEs. So, unfortunately, we have
6339 to go through the full DIEs. At least any work we do building
6340 types here will be reused if full symbols are loaded later. */
6341 if (pdi->has_template_arguments)
6342 {
6343 fixup_partial_die (pdi, cu);
6344
6345 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
6346 {
6347 struct die_info *die;
6348 struct attribute attr;
6349 struct dwarf2_cu *ref_cu = cu;
6350
6351 /* DW_FORM_ref_addr is using section offset. */
6352 attr.name = 0;
6353 attr.form = DW_FORM_ref_addr;
6354 attr.u.unsnd = pdi->offset.sect_off;
6355 die = follow_die_ref (NULL, &attr, &ref_cu);
6356
6357 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
6358 }
6359 }
6360
6361 parent_scope = partial_die_parent_scope (pdi, cu);
6362 if (parent_scope == NULL)
6363 return NULL;
6364 else
6365 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
6366 }
6367
6368 static void
6369 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
6370 {
6371 struct objfile *objfile = cu->objfile;
6372 CORE_ADDR addr = 0;
6373 const char *actual_name = NULL;
6374 CORE_ADDR baseaddr;
6375 char *built_actual_name;
6376
6377 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6378
6379 built_actual_name = partial_die_full_name (pdi, cu);
6380 if (built_actual_name != NULL)
6381 actual_name = built_actual_name;
6382
6383 if (actual_name == NULL)
6384 actual_name = pdi->name;
6385
6386 switch (pdi->tag)
6387 {
6388 case DW_TAG_subprogram:
6389 if (pdi->is_external || cu->language == language_ada)
6390 {
6391 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
6392 of the global scope. But in Ada, we want to be able to access
6393 nested procedures globally. So all Ada subprograms are stored
6394 in the global scope. */
6395 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6396 mst_text, objfile); */
6397 add_psymbol_to_list (actual_name, strlen (actual_name),
6398 built_actual_name != NULL,
6399 VAR_DOMAIN, LOC_BLOCK,
6400 &objfile->global_psymbols,
6401 0, pdi->lowpc + baseaddr,
6402 cu->language, objfile);
6403 }
6404 else
6405 {
6406 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
6407 mst_file_text, objfile); */
6408 add_psymbol_to_list (actual_name, strlen (actual_name),
6409 built_actual_name != NULL,
6410 VAR_DOMAIN, LOC_BLOCK,
6411 &objfile->static_psymbols,
6412 0, pdi->lowpc + baseaddr,
6413 cu->language, objfile);
6414 }
6415 break;
6416 case DW_TAG_constant:
6417 {
6418 struct psymbol_allocation_list *list;
6419
6420 if (pdi->is_external)
6421 list = &objfile->global_psymbols;
6422 else
6423 list = &objfile->static_psymbols;
6424 add_psymbol_to_list (actual_name, strlen (actual_name),
6425 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
6426 list, 0, 0, cu->language, objfile);
6427 }
6428 break;
6429 case DW_TAG_variable:
6430 if (pdi->d.locdesc)
6431 addr = decode_locdesc (pdi->d.locdesc, cu);
6432
6433 if (pdi->d.locdesc
6434 && addr == 0
6435 && !dwarf2_per_objfile->has_section_at_zero)
6436 {
6437 /* A global or static variable may also have been stripped
6438 out by the linker if unused, in which case its address
6439 will be nullified; do not add such variables into partial
6440 symbol table then. */
6441 }
6442 else if (pdi->is_external)
6443 {
6444 /* Global Variable.
6445 Don't enter into the minimal symbol tables as there is
6446 a minimal symbol table entry from the ELF symbols already.
6447 Enter into partial symbol table if it has a location
6448 descriptor or a type.
6449 If the location descriptor is missing, new_symbol will create
6450 a LOC_UNRESOLVED symbol, the address of the variable will then
6451 be determined from the minimal symbol table whenever the variable
6452 is referenced.
6453 The address for the partial symbol table entry is not
6454 used by GDB, but it comes in handy for debugging partial symbol
6455 table building. */
6456
6457 if (pdi->d.locdesc || pdi->has_type)
6458 add_psymbol_to_list (actual_name, strlen (actual_name),
6459 built_actual_name != NULL,
6460 VAR_DOMAIN, LOC_STATIC,
6461 &objfile->global_psymbols,
6462 0, addr + baseaddr,
6463 cu->language, objfile);
6464 }
6465 else
6466 {
6467 /* Static Variable. Skip symbols without location descriptors. */
6468 if (pdi->d.locdesc == NULL)
6469 {
6470 xfree (built_actual_name);
6471 return;
6472 }
6473 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
6474 mst_file_data, objfile); */
6475 add_psymbol_to_list (actual_name, strlen (actual_name),
6476 built_actual_name != NULL,
6477 VAR_DOMAIN, LOC_STATIC,
6478 &objfile->static_psymbols,
6479 0, addr + baseaddr,
6480 cu->language, objfile);
6481 }
6482 break;
6483 case DW_TAG_typedef:
6484 case DW_TAG_base_type:
6485 case DW_TAG_subrange_type:
6486 add_psymbol_to_list (actual_name, strlen (actual_name),
6487 built_actual_name != NULL,
6488 VAR_DOMAIN, LOC_TYPEDEF,
6489 &objfile->static_psymbols,
6490 0, (CORE_ADDR) 0, cu->language, objfile);
6491 break;
6492 case DW_TAG_namespace:
6493 add_psymbol_to_list (actual_name, strlen (actual_name),
6494 built_actual_name != NULL,
6495 VAR_DOMAIN, LOC_TYPEDEF,
6496 &objfile->global_psymbols,
6497 0, (CORE_ADDR) 0, cu->language, objfile);
6498 break;
6499 case DW_TAG_class_type:
6500 case DW_TAG_interface_type:
6501 case DW_TAG_structure_type:
6502 case DW_TAG_union_type:
6503 case DW_TAG_enumeration_type:
6504 /* Skip external references. The DWARF standard says in the section
6505 about "Structure, Union, and Class Type Entries": "An incomplete
6506 structure, union or class type is represented by a structure,
6507 union or class entry that does not have a byte size attribute
6508 and that has a DW_AT_declaration attribute." */
6509 if (!pdi->has_byte_size && pdi->is_declaration)
6510 {
6511 xfree (built_actual_name);
6512 return;
6513 }
6514
6515 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
6516 static vs. global. */
6517 add_psymbol_to_list (actual_name, strlen (actual_name),
6518 built_actual_name != NULL,
6519 STRUCT_DOMAIN, LOC_TYPEDEF,
6520 (cu->language == language_cplus
6521 || cu->language == language_java)
6522 ? &objfile->global_psymbols
6523 : &objfile->static_psymbols,
6524 0, (CORE_ADDR) 0, cu->language, objfile);
6525
6526 break;
6527 case DW_TAG_enumerator:
6528 add_psymbol_to_list (actual_name, strlen (actual_name),
6529 built_actual_name != NULL,
6530 VAR_DOMAIN, LOC_CONST,
6531 (cu->language == language_cplus
6532 || cu->language == language_java)
6533 ? &objfile->global_psymbols
6534 : &objfile->static_psymbols,
6535 0, (CORE_ADDR) 0, cu->language, objfile);
6536 break;
6537 default:
6538 break;
6539 }
6540
6541 xfree (built_actual_name);
6542 }
6543
6544 /* Read a partial die corresponding to a namespace; also, add a symbol
6545 corresponding to that namespace to the symbol table. NAMESPACE is
6546 the name of the enclosing namespace. */
6547
6548 static void
6549 add_partial_namespace (struct partial_die_info *pdi,
6550 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6551 int need_pc, struct dwarf2_cu *cu)
6552 {
6553 /* Add a symbol for the namespace. */
6554
6555 add_partial_symbol (pdi, cu);
6556
6557 /* Now scan partial symbols in that namespace. */
6558
6559 if (pdi->has_children)
6560 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6561 }
6562
6563 /* Read a partial die corresponding to a Fortran module. */
6564
6565 static void
6566 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
6567 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
6568 {
6569 /* Now scan partial symbols in that module. */
6570
6571 if (pdi->has_children)
6572 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
6573 }
6574
6575 /* Read a partial die corresponding to a subprogram and create a partial
6576 symbol for that subprogram. When the CU language allows it, this
6577 routine also defines a partial symbol for each nested subprogram
6578 that this subprogram contains.
6579
6580 DIE my also be a lexical block, in which case we simply search
6581 recursively for suprograms defined inside that lexical block.
6582 Again, this is only performed when the CU language allows this
6583 type of definitions. */
6584
6585 static void
6586 add_partial_subprogram (struct partial_die_info *pdi,
6587 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6588 int need_pc, struct dwarf2_cu *cu)
6589 {
6590 if (pdi->tag == DW_TAG_subprogram)
6591 {
6592 if (pdi->has_pc_info)
6593 {
6594 if (pdi->lowpc < *lowpc)
6595 *lowpc = pdi->lowpc;
6596 if (pdi->highpc > *highpc)
6597 *highpc = pdi->highpc;
6598 if (need_pc)
6599 {
6600 CORE_ADDR baseaddr;
6601 struct objfile *objfile = cu->objfile;
6602
6603 baseaddr = ANOFFSET (objfile->section_offsets,
6604 SECT_OFF_TEXT (objfile));
6605 addrmap_set_empty (objfile->psymtabs_addrmap,
6606 pdi->lowpc + baseaddr,
6607 pdi->highpc - 1 + baseaddr,
6608 cu->per_cu->v.psymtab);
6609 }
6610 }
6611
6612 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
6613 {
6614 if (!pdi->is_declaration)
6615 /* Ignore subprogram DIEs that do not have a name, they are
6616 illegal. Do not emit a complaint at this point, we will
6617 do so when we convert this psymtab into a symtab. */
6618 if (pdi->name)
6619 add_partial_symbol (pdi, cu);
6620 }
6621 }
6622
6623 if (! pdi->has_children)
6624 return;
6625
6626 if (cu->language == language_ada)
6627 {
6628 pdi = pdi->die_child;
6629 while (pdi != NULL)
6630 {
6631 fixup_partial_die (pdi, cu);
6632 if (pdi->tag == DW_TAG_subprogram
6633 || pdi->tag == DW_TAG_lexical_block)
6634 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
6635 pdi = pdi->die_sibling;
6636 }
6637 }
6638 }
6639
6640 /* Read a partial die corresponding to an enumeration type. */
6641
6642 static void
6643 add_partial_enumeration (struct partial_die_info *enum_pdi,
6644 struct dwarf2_cu *cu)
6645 {
6646 struct partial_die_info *pdi;
6647
6648 if (enum_pdi->name != NULL)
6649 add_partial_symbol (enum_pdi, cu);
6650
6651 pdi = enum_pdi->die_child;
6652 while (pdi)
6653 {
6654 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
6655 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
6656 else
6657 add_partial_symbol (pdi, cu);
6658 pdi = pdi->die_sibling;
6659 }
6660 }
6661
6662 /* Return the initial uleb128 in the die at INFO_PTR. */
6663
6664 static unsigned int
6665 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6666 {
6667 unsigned int bytes_read;
6668
6669 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6670 }
6671
6672 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
6673 Return the corresponding abbrev, or NULL if the number is zero (indicating
6674 an empty DIE). In either case *BYTES_READ will be set to the length of
6675 the initial number. */
6676
6677 static struct abbrev_info *
6678 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
6679 struct dwarf2_cu *cu)
6680 {
6681 bfd *abfd = cu->objfile->obfd;
6682 unsigned int abbrev_number;
6683 struct abbrev_info *abbrev;
6684
6685 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
6686
6687 if (abbrev_number == 0)
6688 return NULL;
6689
6690 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
6691 if (!abbrev)
6692 {
6693 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
6694 abbrev_number, bfd_get_filename (abfd));
6695 }
6696
6697 return abbrev;
6698 }
6699
6700 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6701 Returns a pointer to the end of a series of DIEs, terminated by an empty
6702 DIE. Any children of the skipped DIEs will also be skipped. */
6703
6704 static const gdb_byte *
6705 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
6706 {
6707 struct dwarf2_cu *cu = reader->cu;
6708 struct abbrev_info *abbrev;
6709 unsigned int bytes_read;
6710
6711 while (1)
6712 {
6713 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
6714 if (abbrev == NULL)
6715 return info_ptr + bytes_read;
6716 else
6717 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
6718 }
6719 }
6720
6721 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
6722 INFO_PTR should point just after the initial uleb128 of a DIE, and the
6723 abbrev corresponding to that skipped uleb128 should be passed in
6724 ABBREV. Returns a pointer to this DIE's sibling, skipping any
6725 children. */
6726
6727 static const gdb_byte *
6728 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
6729 struct abbrev_info *abbrev)
6730 {
6731 unsigned int bytes_read;
6732 struct attribute attr;
6733 bfd *abfd = reader->abfd;
6734 struct dwarf2_cu *cu = reader->cu;
6735 const gdb_byte *buffer = reader->buffer;
6736 const gdb_byte *buffer_end = reader->buffer_end;
6737 const gdb_byte *start_info_ptr = info_ptr;
6738 unsigned int form, i;
6739
6740 for (i = 0; i < abbrev->num_attrs; i++)
6741 {
6742 /* The only abbrev we care about is DW_AT_sibling. */
6743 if (abbrev->attrs[i].name == DW_AT_sibling)
6744 {
6745 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
6746 if (attr.form == DW_FORM_ref_addr)
6747 complaint (&symfile_complaints,
6748 _("ignoring absolute DW_AT_sibling"));
6749 else
6750 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
6751 }
6752
6753 /* If it isn't DW_AT_sibling, skip this attribute. */
6754 form = abbrev->attrs[i].form;
6755 skip_attribute:
6756 switch (form)
6757 {
6758 case DW_FORM_ref_addr:
6759 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
6760 and later it is offset sized. */
6761 if (cu->header.version == 2)
6762 info_ptr += cu->header.addr_size;
6763 else
6764 info_ptr += cu->header.offset_size;
6765 break;
6766 case DW_FORM_GNU_ref_alt:
6767 info_ptr += cu->header.offset_size;
6768 break;
6769 case DW_FORM_addr:
6770 info_ptr += cu->header.addr_size;
6771 break;
6772 case DW_FORM_data1:
6773 case DW_FORM_ref1:
6774 case DW_FORM_flag:
6775 info_ptr += 1;
6776 break;
6777 case DW_FORM_flag_present:
6778 break;
6779 case DW_FORM_data2:
6780 case DW_FORM_ref2:
6781 info_ptr += 2;
6782 break;
6783 case DW_FORM_data4:
6784 case DW_FORM_ref4:
6785 info_ptr += 4;
6786 break;
6787 case DW_FORM_data8:
6788 case DW_FORM_ref8:
6789 case DW_FORM_ref_sig8:
6790 info_ptr += 8;
6791 break;
6792 case DW_FORM_string:
6793 read_direct_string (abfd, info_ptr, &bytes_read);
6794 info_ptr += bytes_read;
6795 break;
6796 case DW_FORM_sec_offset:
6797 case DW_FORM_strp:
6798 case DW_FORM_GNU_strp_alt:
6799 info_ptr += cu->header.offset_size;
6800 break;
6801 case DW_FORM_exprloc:
6802 case DW_FORM_block:
6803 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6804 info_ptr += bytes_read;
6805 break;
6806 case DW_FORM_block1:
6807 info_ptr += 1 + read_1_byte (abfd, info_ptr);
6808 break;
6809 case DW_FORM_block2:
6810 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
6811 break;
6812 case DW_FORM_block4:
6813 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
6814 break;
6815 case DW_FORM_sdata:
6816 case DW_FORM_udata:
6817 case DW_FORM_ref_udata:
6818 case DW_FORM_GNU_addr_index:
6819 case DW_FORM_GNU_str_index:
6820 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
6821 break;
6822 case DW_FORM_indirect:
6823 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
6824 info_ptr += bytes_read;
6825 /* We need to continue parsing from here, so just go back to
6826 the top. */
6827 goto skip_attribute;
6828
6829 default:
6830 error (_("Dwarf Error: Cannot handle %s "
6831 "in DWARF reader [in module %s]"),
6832 dwarf_form_name (form),
6833 bfd_get_filename (abfd));
6834 }
6835 }
6836
6837 if (abbrev->has_children)
6838 return skip_children (reader, info_ptr);
6839 else
6840 return info_ptr;
6841 }
6842
6843 /* Locate ORIG_PDI's sibling.
6844 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
6845
6846 static const gdb_byte *
6847 locate_pdi_sibling (const struct die_reader_specs *reader,
6848 struct partial_die_info *orig_pdi,
6849 const gdb_byte *info_ptr)
6850 {
6851 /* Do we know the sibling already? */
6852
6853 if (orig_pdi->sibling)
6854 return orig_pdi->sibling;
6855
6856 /* Are there any children to deal with? */
6857
6858 if (!orig_pdi->has_children)
6859 return info_ptr;
6860
6861 /* Skip the children the long way. */
6862
6863 return skip_children (reader, info_ptr);
6864 }
6865
6866 /* Expand this partial symbol table into a full symbol table. SELF is
6867 not NULL. */
6868
6869 static void
6870 dwarf2_read_symtab (struct partial_symtab *self,
6871 struct objfile *objfile)
6872 {
6873 if (self->readin)
6874 {
6875 warning (_("bug: psymtab for %s is already read in."),
6876 self->filename);
6877 }
6878 else
6879 {
6880 if (info_verbose)
6881 {
6882 printf_filtered (_("Reading in symbols for %s..."),
6883 self->filename);
6884 gdb_flush (gdb_stdout);
6885 }
6886
6887 /* Restore our global data. */
6888 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
6889
6890 /* If this psymtab is constructed from a debug-only objfile, the
6891 has_section_at_zero flag will not necessarily be correct. We
6892 can get the correct value for this flag by looking at the data
6893 associated with the (presumably stripped) associated objfile. */
6894 if (objfile->separate_debug_objfile_backlink)
6895 {
6896 struct dwarf2_per_objfile *dpo_backlink
6897 = objfile_data (objfile->separate_debug_objfile_backlink,
6898 dwarf2_objfile_data_key);
6899
6900 dwarf2_per_objfile->has_section_at_zero
6901 = dpo_backlink->has_section_at_zero;
6902 }
6903
6904 dwarf2_per_objfile->reading_partial_symbols = 0;
6905
6906 psymtab_to_symtab_1 (self);
6907
6908 /* Finish up the debug error message. */
6909 if (info_verbose)
6910 printf_filtered (_("done.\n"));
6911 }
6912
6913 process_cu_includes ();
6914 }
6915 \f
6916 /* Reading in full CUs. */
6917
6918 /* Add PER_CU to the queue. */
6919
6920 static void
6921 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
6922 enum language pretend_language)
6923 {
6924 struct dwarf2_queue_item *item;
6925
6926 per_cu->queued = 1;
6927 item = xmalloc (sizeof (*item));
6928 item->per_cu = per_cu;
6929 item->pretend_language = pretend_language;
6930 item->next = NULL;
6931
6932 if (dwarf2_queue == NULL)
6933 dwarf2_queue = item;
6934 else
6935 dwarf2_queue_tail->next = item;
6936
6937 dwarf2_queue_tail = item;
6938 }
6939
6940 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
6941 unit and add it to our queue.
6942 The result is non-zero if PER_CU was queued, otherwise the result is zero
6943 meaning either PER_CU is already queued or it is already loaded. */
6944
6945 static int
6946 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
6947 struct dwarf2_per_cu_data *per_cu,
6948 enum language pretend_language)
6949 {
6950 /* We may arrive here during partial symbol reading, if we need full
6951 DIEs to process an unusual case (e.g. template arguments). Do
6952 not queue PER_CU, just tell our caller to load its DIEs. */
6953 if (dwarf2_per_objfile->reading_partial_symbols)
6954 {
6955 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
6956 return 1;
6957 return 0;
6958 }
6959
6960 /* Mark the dependence relation so that we don't flush PER_CU
6961 too early. */
6962 dwarf2_add_dependence (this_cu, per_cu);
6963
6964 /* If it's already on the queue, we have nothing to do. */
6965 if (per_cu->queued)
6966 return 0;
6967
6968 /* If the compilation unit is already loaded, just mark it as
6969 used. */
6970 if (per_cu->cu != NULL)
6971 {
6972 per_cu->cu->last_used = 0;
6973 return 0;
6974 }
6975
6976 /* Add it to the queue. */
6977 queue_comp_unit (per_cu, pretend_language);
6978
6979 return 1;
6980 }
6981
6982 /* Process the queue. */
6983
6984 static void
6985 process_queue (void)
6986 {
6987 struct dwarf2_queue_item *item, *next_item;
6988
6989 if (dwarf2_read_debug)
6990 {
6991 fprintf_unfiltered (gdb_stdlog,
6992 "Expanding one or more symtabs of objfile %s ...\n",
6993 dwarf2_per_objfile->objfile->name);
6994 }
6995
6996 /* The queue starts out with one item, but following a DIE reference
6997 may load a new CU, adding it to the end of the queue. */
6998 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
6999 {
7000 if (dwarf2_per_objfile->using_index
7001 ? !item->per_cu->v.quick->symtab
7002 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
7003 {
7004 struct dwarf2_per_cu_data *per_cu = item->per_cu;
7005
7006 if (dwarf2_read_debug)
7007 {
7008 fprintf_unfiltered (gdb_stdlog,
7009 "Expanding symtab of %s at offset 0x%x\n",
7010 per_cu->is_debug_types ? "TU" : "CU",
7011 per_cu->offset.sect_off);
7012 }
7013
7014 if (per_cu->is_debug_types)
7015 process_full_type_unit (per_cu, item->pretend_language);
7016 else
7017 process_full_comp_unit (per_cu, item->pretend_language);
7018
7019 if (dwarf2_read_debug)
7020 {
7021 fprintf_unfiltered (gdb_stdlog,
7022 "Done expanding %s at offset 0x%x\n",
7023 per_cu->is_debug_types ? "TU" : "CU",
7024 per_cu->offset.sect_off);
7025 }
7026 }
7027
7028 item->per_cu->queued = 0;
7029 next_item = item->next;
7030 xfree (item);
7031 }
7032
7033 dwarf2_queue_tail = NULL;
7034
7035 if (dwarf2_read_debug)
7036 {
7037 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
7038 dwarf2_per_objfile->objfile->name);
7039 }
7040 }
7041
7042 /* Free all allocated queue entries. This function only releases anything if
7043 an error was thrown; if the queue was processed then it would have been
7044 freed as we went along. */
7045
7046 static void
7047 dwarf2_release_queue (void *dummy)
7048 {
7049 struct dwarf2_queue_item *item, *last;
7050
7051 item = dwarf2_queue;
7052 while (item)
7053 {
7054 /* Anything still marked queued is likely to be in an
7055 inconsistent state, so discard it. */
7056 if (item->per_cu->queued)
7057 {
7058 if (item->per_cu->cu != NULL)
7059 free_one_cached_comp_unit (item->per_cu);
7060 item->per_cu->queued = 0;
7061 }
7062
7063 last = item;
7064 item = item->next;
7065 xfree (last);
7066 }
7067
7068 dwarf2_queue = dwarf2_queue_tail = NULL;
7069 }
7070
7071 /* Read in full symbols for PST, and anything it depends on. */
7072
7073 static void
7074 psymtab_to_symtab_1 (struct partial_symtab *pst)
7075 {
7076 struct dwarf2_per_cu_data *per_cu;
7077 int i;
7078
7079 if (pst->readin)
7080 return;
7081
7082 for (i = 0; i < pst->number_of_dependencies; i++)
7083 if (!pst->dependencies[i]->readin
7084 && pst->dependencies[i]->user == NULL)
7085 {
7086 /* Inform about additional files that need to be read in. */
7087 if (info_verbose)
7088 {
7089 /* FIXME: i18n: Need to make this a single string. */
7090 fputs_filtered (" ", gdb_stdout);
7091 wrap_here ("");
7092 fputs_filtered ("and ", gdb_stdout);
7093 wrap_here ("");
7094 printf_filtered ("%s...", pst->dependencies[i]->filename);
7095 wrap_here (""); /* Flush output. */
7096 gdb_flush (gdb_stdout);
7097 }
7098 psymtab_to_symtab_1 (pst->dependencies[i]);
7099 }
7100
7101 per_cu = pst->read_symtab_private;
7102
7103 if (per_cu == NULL)
7104 {
7105 /* It's an include file, no symbols to read for it.
7106 Everything is in the parent symtab. */
7107 pst->readin = 1;
7108 return;
7109 }
7110
7111 dw2_do_instantiate_symtab (per_cu);
7112 }
7113
7114 /* Trivial hash function for die_info: the hash value of a DIE
7115 is its offset in .debug_info for this objfile. */
7116
7117 static hashval_t
7118 die_hash (const void *item)
7119 {
7120 const struct die_info *die = item;
7121
7122 return die->offset.sect_off;
7123 }
7124
7125 /* Trivial comparison function for die_info structures: two DIEs
7126 are equal if they have the same offset. */
7127
7128 static int
7129 die_eq (const void *item_lhs, const void *item_rhs)
7130 {
7131 const struct die_info *die_lhs = item_lhs;
7132 const struct die_info *die_rhs = item_rhs;
7133
7134 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
7135 }
7136
7137 /* die_reader_func for load_full_comp_unit.
7138 This is identical to read_signatured_type_reader,
7139 but is kept separate for now. */
7140
7141 static void
7142 load_full_comp_unit_reader (const struct die_reader_specs *reader,
7143 const gdb_byte *info_ptr,
7144 struct die_info *comp_unit_die,
7145 int has_children,
7146 void *data)
7147 {
7148 struct dwarf2_cu *cu = reader->cu;
7149 enum language *language_ptr = data;
7150
7151 gdb_assert (cu->die_hash == NULL);
7152 cu->die_hash =
7153 htab_create_alloc_ex (cu->header.length / 12,
7154 die_hash,
7155 die_eq,
7156 NULL,
7157 &cu->comp_unit_obstack,
7158 hashtab_obstack_allocate,
7159 dummy_obstack_deallocate);
7160
7161 if (has_children)
7162 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
7163 &info_ptr, comp_unit_die);
7164 cu->dies = comp_unit_die;
7165 /* comp_unit_die is not stored in die_hash, no need. */
7166
7167 /* We try not to read any attributes in this function, because not
7168 all CUs needed for references have been loaded yet, and symbol
7169 table processing isn't initialized. But we have to set the CU language,
7170 or we won't be able to build types correctly.
7171 Similarly, if we do not read the producer, we can not apply
7172 producer-specific interpretation. */
7173 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
7174 }
7175
7176 /* Load the DIEs associated with PER_CU into memory. */
7177
7178 static void
7179 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
7180 enum language pretend_language)
7181 {
7182 gdb_assert (! this_cu->is_debug_types);
7183
7184 init_cutu_and_read_dies (this_cu, NULL, 1, 1,
7185 load_full_comp_unit_reader, &pretend_language);
7186 }
7187
7188 /* Add a DIE to the delayed physname list. */
7189
7190 static void
7191 add_to_method_list (struct type *type, int fnfield_index, int index,
7192 const char *name, struct die_info *die,
7193 struct dwarf2_cu *cu)
7194 {
7195 struct delayed_method_info mi;
7196 mi.type = type;
7197 mi.fnfield_index = fnfield_index;
7198 mi.index = index;
7199 mi.name = name;
7200 mi.die = die;
7201 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
7202 }
7203
7204 /* A cleanup for freeing the delayed method list. */
7205
7206 static void
7207 free_delayed_list (void *ptr)
7208 {
7209 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
7210 if (cu->method_list != NULL)
7211 {
7212 VEC_free (delayed_method_info, cu->method_list);
7213 cu->method_list = NULL;
7214 }
7215 }
7216
7217 /* Compute the physnames of any methods on the CU's method list.
7218
7219 The computation of method physnames is delayed in order to avoid the
7220 (bad) condition that one of the method's formal parameters is of an as yet
7221 incomplete type. */
7222
7223 static void
7224 compute_delayed_physnames (struct dwarf2_cu *cu)
7225 {
7226 int i;
7227 struct delayed_method_info *mi;
7228 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
7229 {
7230 const char *physname;
7231 struct fn_fieldlist *fn_flp
7232 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
7233 physname = dwarf2_physname (mi->name, mi->die, cu);
7234 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
7235 }
7236 }
7237
7238 /* Go objects should be embedded in a DW_TAG_module DIE,
7239 and it's not clear if/how imported objects will appear.
7240 To keep Go support simple until that's worked out,
7241 go back through what we've read and create something usable.
7242 We could do this while processing each DIE, and feels kinda cleaner,
7243 but that way is more invasive.
7244 This is to, for example, allow the user to type "p var" or "b main"
7245 without having to specify the package name, and allow lookups
7246 of module.object to work in contexts that use the expression
7247 parser. */
7248
7249 static void
7250 fixup_go_packaging (struct dwarf2_cu *cu)
7251 {
7252 char *package_name = NULL;
7253 struct pending *list;
7254 int i;
7255
7256 for (list = global_symbols; list != NULL; list = list->next)
7257 {
7258 for (i = 0; i < list->nsyms; ++i)
7259 {
7260 struct symbol *sym = list->symbol[i];
7261
7262 if (SYMBOL_LANGUAGE (sym) == language_go
7263 && SYMBOL_CLASS (sym) == LOC_BLOCK)
7264 {
7265 char *this_package_name = go_symbol_package_name (sym);
7266
7267 if (this_package_name == NULL)
7268 continue;
7269 if (package_name == NULL)
7270 package_name = this_package_name;
7271 else
7272 {
7273 if (strcmp (package_name, this_package_name) != 0)
7274 complaint (&symfile_complaints,
7275 _("Symtab %s has objects from two different Go packages: %s and %s"),
7276 (SYMBOL_SYMTAB (sym)
7277 ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
7278 : cu->objfile->name),
7279 this_package_name, package_name);
7280 xfree (this_package_name);
7281 }
7282 }
7283 }
7284 }
7285
7286 if (package_name != NULL)
7287 {
7288 struct objfile *objfile = cu->objfile;
7289 const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
7290 package_name,
7291 strlen (package_name));
7292 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
7293 saved_package_name, objfile);
7294 struct symbol *sym;
7295
7296 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7297
7298 sym = allocate_symbol (objfile);
7299 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
7300 SYMBOL_SET_NAMES (sym, saved_package_name,
7301 strlen (saved_package_name), 0, objfile);
7302 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
7303 e.g., "main" finds the "main" module and not C's main(). */
7304 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
7305 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
7306 SYMBOL_TYPE (sym) = type;
7307
7308 add_symbol_to_list (sym, &global_symbols);
7309
7310 xfree (package_name);
7311 }
7312 }
7313
7314 /* Return the symtab for PER_CU. This works properly regardless of
7315 whether we're using the index or psymtabs. */
7316
7317 static struct symtab *
7318 get_symtab (struct dwarf2_per_cu_data *per_cu)
7319 {
7320 return (dwarf2_per_objfile->using_index
7321 ? per_cu->v.quick->symtab
7322 : per_cu->v.psymtab->symtab);
7323 }
7324
7325 /* A helper function for computing the list of all symbol tables
7326 included by PER_CU. */
7327
7328 static void
7329 recursively_compute_inclusions (VEC (dwarf2_per_cu_ptr) **result,
7330 htab_t all_children,
7331 struct dwarf2_per_cu_data *per_cu)
7332 {
7333 void **slot;
7334 int ix;
7335 struct dwarf2_per_cu_data *iter;
7336
7337 slot = htab_find_slot (all_children, per_cu, INSERT);
7338 if (*slot != NULL)
7339 {
7340 /* This inclusion and its children have been processed. */
7341 return;
7342 }
7343
7344 *slot = per_cu;
7345 /* Only add a CU if it has a symbol table. */
7346 if (get_symtab (per_cu) != NULL)
7347 VEC_safe_push (dwarf2_per_cu_ptr, *result, per_cu);
7348
7349 for (ix = 0;
7350 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
7351 ++ix)
7352 recursively_compute_inclusions (result, all_children, iter);
7353 }
7354
7355 /* Compute the symtab 'includes' fields for the symtab related to
7356 PER_CU. */
7357
7358 static void
7359 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
7360 {
7361 gdb_assert (! per_cu->is_debug_types);
7362
7363 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
7364 {
7365 int ix, len;
7366 struct dwarf2_per_cu_data *iter;
7367 VEC (dwarf2_per_cu_ptr) *result_children = NULL;
7368 htab_t all_children;
7369 struct symtab *symtab = get_symtab (per_cu);
7370
7371 /* If we don't have a symtab, we can just skip this case. */
7372 if (symtab == NULL)
7373 return;
7374
7375 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
7376 NULL, xcalloc, xfree);
7377
7378 for (ix = 0;
7379 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
7380 ix, iter);
7381 ++ix)
7382 recursively_compute_inclusions (&result_children, all_children, iter);
7383
7384 /* Now we have a transitive closure of all the included CUs, and
7385 for .gdb_index version 7 the included TUs, so we can convert it
7386 to a list of symtabs. */
7387 len = VEC_length (dwarf2_per_cu_ptr, result_children);
7388 symtab->includes
7389 = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
7390 (len + 1) * sizeof (struct symtab *));
7391 for (ix = 0;
7392 VEC_iterate (dwarf2_per_cu_ptr, result_children, ix, iter);
7393 ++ix)
7394 symtab->includes[ix] = get_symtab (iter);
7395 symtab->includes[len] = NULL;
7396
7397 VEC_free (dwarf2_per_cu_ptr, result_children);
7398 htab_delete (all_children);
7399 }
7400 }
7401
7402 /* Compute the 'includes' field for the symtabs of all the CUs we just
7403 read. */
7404
7405 static void
7406 process_cu_includes (void)
7407 {
7408 int ix;
7409 struct dwarf2_per_cu_data *iter;
7410
7411 for (ix = 0;
7412 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
7413 ix, iter);
7414 ++ix)
7415 {
7416 if (! iter->is_debug_types)
7417 compute_symtab_includes (iter);
7418 }
7419
7420 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
7421 }
7422
7423 /* Generate full symbol information for PER_CU, whose DIEs have
7424 already been loaded into memory. */
7425
7426 static void
7427 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
7428 enum language pretend_language)
7429 {
7430 struct dwarf2_cu *cu = per_cu->cu;
7431 struct objfile *objfile = per_cu->objfile;
7432 CORE_ADDR lowpc, highpc;
7433 struct symtab *symtab;
7434 struct cleanup *back_to, *delayed_list_cleanup;
7435 CORE_ADDR baseaddr;
7436 struct block *static_block;
7437
7438 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7439
7440 buildsym_init ();
7441 back_to = make_cleanup (really_free_pendings, NULL);
7442 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7443
7444 cu->list_in_scope = &file_symbols;
7445
7446 cu->language = pretend_language;
7447 cu->language_defn = language_def (cu->language);
7448
7449 /* Do line number decoding in read_file_scope () */
7450 process_die (cu->dies, cu);
7451
7452 /* For now fudge the Go package. */
7453 if (cu->language == language_go)
7454 fixup_go_packaging (cu);
7455
7456 /* Now that we have processed all the DIEs in the CU, all the types
7457 should be complete, and it should now be safe to compute all of the
7458 physnames. */
7459 compute_delayed_physnames (cu);
7460 do_cleanups (delayed_list_cleanup);
7461
7462 /* Some compilers don't define a DW_AT_high_pc attribute for the
7463 compilation unit. If the DW_AT_high_pc is missing, synthesize
7464 it, by scanning the DIE's below the compilation unit. */
7465 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
7466
7467 static_block
7468 = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
7469
7470 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
7471 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
7472 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
7473 addrmap to help ensure it has an accurate map of pc values belonging to
7474 this comp unit. */
7475 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
7476
7477 symtab = end_symtab_from_static_block (static_block, objfile,
7478 SECT_OFF_TEXT (objfile), 0);
7479
7480 if (symtab != NULL)
7481 {
7482 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
7483
7484 /* Set symtab language to language from DW_AT_language. If the
7485 compilation is from a C file generated by language preprocessors, do
7486 not set the language if it was already deduced by start_subfile. */
7487 if (!(cu->language == language_c && symtab->language != language_c))
7488 symtab->language = cu->language;
7489
7490 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
7491 produce DW_AT_location with location lists but it can be possibly
7492 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
7493 there were bugs in prologue debug info, fixed later in GCC-4.5
7494 by "unwind info for epilogues" patch (which is not directly related).
7495
7496 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
7497 needed, it would be wrong due to missing DW_AT_producer there.
7498
7499 Still one can confuse GDB by using non-standard GCC compilation
7500 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
7501 */
7502 if (cu->has_loclist && gcc_4_minor >= 5)
7503 symtab->locations_valid = 1;
7504
7505 if (gcc_4_minor >= 5)
7506 symtab->epilogue_unwind_valid = 1;
7507
7508 symtab->call_site_htab = cu->call_site_htab;
7509 }
7510
7511 if (dwarf2_per_objfile->using_index)
7512 per_cu->v.quick->symtab = symtab;
7513 else
7514 {
7515 struct partial_symtab *pst = per_cu->v.psymtab;
7516 pst->symtab = symtab;
7517 pst->readin = 1;
7518 }
7519
7520 /* Push it for inclusion processing later. */
7521 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
7522
7523 do_cleanups (back_to);
7524 }
7525
7526 /* Generate full symbol information for type unit PER_CU, whose DIEs have
7527 already been loaded into memory. */
7528
7529 static void
7530 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
7531 enum language pretend_language)
7532 {
7533 struct dwarf2_cu *cu = per_cu->cu;
7534 struct objfile *objfile = per_cu->objfile;
7535 struct symtab *symtab;
7536 struct cleanup *back_to, *delayed_list_cleanup;
7537 struct signatured_type *sig_type;
7538
7539 gdb_assert (per_cu->is_debug_types);
7540 sig_type = (struct signatured_type *) per_cu;
7541
7542 buildsym_init ();
7543 back_to = make_cleanup (really_free_pendings, NULL);
7544 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
7545
7546 cu->list_in_scope = &file_symbols;
7547
7548 cu->language = pretend_language;
7549 cu->language_defn = language_def (cu->language);
7550
7551 /* The symbol tables are set up in read_type_unit_scope. */
7552 process_die (cu->dies, cu);
7553
7554 /* For now fudge the Go package. */
7555 if (cu->language == language_go)
7556 fixup_go_packaging (cu);
7557
7558 /* Now that we have processed all the DIEs in the CU, all the types
7559 should be complete, and it should now be safe to compute all of the
7560 physnames. */
7561 compute_delayed_physnames (cu);
7562 do_cleanups (delayed_list_cleanup);
7563
7564 /* TUs share symbol tables.
7565 If this is the first TU to use this symtab, complete the construction
7566 of it with end_expandable_symtab. Otherwise, complete the addition of
7567 this TU's symbols to the existing symtab. */
7568 if (sig_type->type_unit_group->primary_symtab == NULL)
7569 {
7570 symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
7571 sig_type->type_unit_group->primary_symtab = symtab;
7572
7573 if (symtab != NULL)
7574 {
7575 /* Set symtab language to language from DW_AT_language. If the
7576 compilation is from a C file generated by language preprocessors,
7577 do not set the language if it was already deduced by
7578 start_subfile. */
7579 if (!(cu->language == language_c && symtab->language != language_c))
7580 symtab->language = cu->language;
7581 }
7582 }
7583 else
7584 {
7585 augment_type_symtab (objfile,
7586 sig_type->type_unit_group->primary_symtab);
7587 symtab = sig_type->type_unit_group->primary_symtab;
7588 }
7589
7590 if (dwarf2_per_objfile->using_index)
7591 per_cu->v.quick->symtab = symtab;
7592 else
7593 {
7594 struct partial_symtab *pst = per_cu->v.psymtab;
7595 pst->symtab = symtab;
7596 pst->readin = 1;
7597 }
7598
7599 do_cleanups (back_to);
7600 }
7601
7602 /* Process an imported unit DIE. */
7603
7604 static void
7605 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
7606 {
7607 struct attribute *attr;
7608
7609 /* For now we don't handle imported units in type units. */
7610 if (cu->per_cu->is_debug_types)
7611 {
7612 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7613 " supported in type units [in module %s]"),
7614 cu->objfile->name);
7615 }
7616
7617 attr = dwarf2_attr (die, DW_AT_import, cu);
7618 if (attr != NULL)
7619 {
7620 struct dwarf2_per_cu_data *per_cu;
7621 struct symtab *imported_symtab;
7622 sect_offset offset;
7623 int is_dwz;
7624
7625 offset = dwarf2_get_ref_die_offset (attr);
7626 is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
7627 per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
7628
7629 /* Queue the unit, if needed. */
7630 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
7631 load_full_comp_unit (per_cu, cu->language);
7632
7633 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7634 per_cu);
7635 }
7636 }
7637
7638 /* Process a die and its children. */
7639
7640 static void
7641 process_die (struct die_info *die, struct dwarf2_cu *cu)
7642 {
7643 switch (die->tag)
7644 {
7645 case DW_TAG_padding:
7646 break;
7647 case DW_TAG_compile_unit:
7648 case DW_TAG_partial_unit:
7649 read_file_scope (die, cu);
7650 break;
7651 case DW_TAG_type_unit:
7652 read_type_unit_scope (die, cu);
7653 break;
7654 case DW_TAG_subprogram:
7655 case DW_TAG_inlined_subroutine:
7656 read_func_scope (die, cu);
7657 break;
7658 case DW_TAG_lexical_block:
7659 case DW_TAG_try_block:
7660 case DW_TAG_catch_block:
7661 read_lexical_block_scope (die, cu);
7662 break;
7663 case DW_TAG_GNU_call_site:
7664 read_call_site_scope (die, cu);
7665 break;
7666 case DW_TAG_class_type:
7667 case DW_TAG_interface_type:
7668 case DW_TAG_structure_type:
7669 case DW_TAG_union_type:
7670 process_structure_scope (die, cu);
7671 break;
7672 case DW_TAG_enumeration_type:
7673 process_enumeration_scope (die, cu);
7674 break;
7675
7676 /* These dies have a type, but processing them does not create
7677 a symbol or recurse to process the children. Therefore we can
7678 read them on-demand through read_type_die. */
7679 case DW_TAG_subroutine_type:
7680 case DW_TAG_set_type:
7681 case DW_TAG_array_type:
7682 case DW_TAG_pointer_type:
7683 case DW_TAG_ptr_to_member_type:
7684 case DW_TAG_reference_type:
7685 case DW_TAG_string_type:
7686 break;
7687
7688 case DW_TAG_base_type:
7689 case DW_TAG_subrange_type:
7690 case DW_TAG_typedef:
7691 /* Add a typedef symbol for the type definition, if it has a
7692 DW_AT_name. */
7693 new_symbol (die, read_type_die (die, cu), cu);
7694 break;
7695 case DW_TAG_common_block:
7696 read_common_block (die, cu);
7697 break;
7698 case DW_TAG_common_inclusion:
7699 break;
7700 case DW_TAG_namespace:
7701 cu->processing_has_namespace_info = 1;
7702 read_namespace (die, cu);
7703 break;
7704 case DW_TAG_module:
7705 cu->processing_has_namespace_info = 1;
7706 read_module (die, cu);
7707 break;
7708 case DW_TAG_imported_declaration:
7709 case DW_TAG_imported_module:
7710 cu->processing_has_namespace_info = 1;
7711 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
7712 || cu->language != language_fortran))
7713 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
7714 dwarf_tag_name (die->tag));
7715 read_import_statement (die, cu);
7716 break;
7717
7718 case DW_TAG_imported_unit:
7719 process_imported_unit_die (die, cu);
7720 break;
7721
7722 default:
7723 new_symbol (die, NULL, cu);
7724 break;
7725 }
7726 }
7727 \f
7728 /* DWARF name computation. */
7729
7730 /* A helper function for dwarf2_compute_name which determines whether DIE
7731 needs to have the name of the scope prepended to the name listed in the
7732 die. */
7733
7734 static int
7735 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
7736 {
7737 struct attribute *attr;
7738
7739 switch (die->tag)
7740 {
7741 case DW_TAG_namespace:
7742 case DW_TAG_typedef:
7743 case DW_TAG_class_type:
7744 case DW_TAG_interface_type:
7745 case DW_TAG_structure_type:
7746 case DW_TAG_union_type:
7747 case DW_TAG_enumeration_type:
7748 case DW_TAG_enumerator:
7749 case DW_TAG_subprogram:
7750 case DW_TAG_member:
7751 return 1;
7752
7753 case DW_TAG_variable:
7754 case DW_TAG_constant:
7755 /* We only need to prefix "globally" visible variables. These include
7756 any variable marked with DW_AT_external or any variable that
7757 lives in a namespace. [Variables in anonymous namespaces
7758 require prefixing, but they are not DW_AT_external.] */
7759
7760 if (dwarf2_attr (die, DW_AT_specification, cu))
7761 {
7762 struct dwarf2_cu *spec_cu = cu;
7763
7764 return die_needs_namespace (die_specification (die, &spec_cu),
7765 spec_cu);
7766 }
7767
7768 attr = dwarf2_attr (die, DW_AT_external, cu);
7769 if (attr == NULL && die->parent->tag != DW_TAG_namespace
7770 && die->parent->tag != DW_TAG_module)
7771 return 0;
7772 /* A variable in a lexical block of some kind does not need a
7773 namespace, even though in C++ such variables may be external
7774 and have a mangled name. */
7775 if (die->parent->tag == DW_TAG_lexical_block
7776 || die->parent->tag == DW_TAG_try_block
7777 || die->parent->tag == DW_TAG_catch_block
7778 || die->parent->tag == DW_TAG_subprogram)
7779 return 0;
7780 return 1;
7781
7782 default:
7783 return 0;
7784 }
7785 }
7786
7787 /* Retrieve the last character from a mem_file. */
7788
7789 static void
7790 do_ui_file_peek_last (void *object, const char *buffer, long length)
7791 {
7792 char *last_char_p = (char *) object;
7793
7794 if (length > 0)
7795 *last_char_p = buffer[length - 1];
7796 }
7797
7798 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
7799 compute the physname for the object, which include a method's:
7800 - formal parameters (C++/Java),
7801 - receiver type (Go),
7802 - return type (Java).
7803
7804 The term "physname" is a bit confusing.
7805 For C++, for example, it is the demangled name.
7806 For Go, for example, it's the mangled name.
7807
7808 For Ada, return the DIE's linkage name rather than the fully qualified
7809 name. PHYSNAME is ignored..
7810
7811 The result is allocated on the objfile_obstack and canonicalized. */
7812
7813 static const char *
7814 dwarf2_compute_name (const char *name,
7815 struct die_info *die, struct dwarf2_cu *cu,
7816 int physname)
7817 {
7818 struct objfile *objfile = cu->objfile;
7819
7820 if (name == NULL)
7821 name = dwarf2_name (die, cu);
7822
7823 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
7824 compute it by typename_concat inside GDB. */
7825 if (cu->language == language_ada
7826 || (cu->language == language_fortran && physname))
7827 {
7828 /* For Ada unit, we prefer the linkage name over the name, as
7829 the former contains the exported name, which the user expects
7830 to be able to reference. Ideally, we want the user to be able
7831 to reference this entity using either natural or linkage name,
7832 but we haven't started looking at this enhancement yet. */
7833 struct attribute *attr;
7834
7835 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
7836 if (attr == NULL)
7837 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
7838 if (attr && DW_STRING (attr))
7839 return DW_STRING (attr);
7840 }
7841
7842 /* These are the only languages we know how to qualify names in. */
7843 if (name != NULL
7844 && (cu->language == language_cplus || cu->language == language_java
7845 || cu->language == language_fortran))
7846 {
7847 if (die_needs_namespace (die, cu))
7848 {
7849 long length;
7850 const char *prefix;
7851 struct ui_file *buf;
7852
7853 prefix = determine_prefix (die, cu);
7854 buf = mem_fileopen ();
7855 if (*prefix != '\0')
7856 {
7857 char *prefixed_name = typename_concat (NULL, prefix, name,
7858 physname, cu);
7859
7860 fputs_unfiltered (prefixed_name, buf);
7861 xfree (prefixed_name);
7862 }
7863 else
7864 fputs_unfiltered (name, buf);
7865
7866 /* Template parameters may be specified in the DIE's DW_AT_name, or
7867 as children with DW_TAG_template_type_param or
7868 DW_TAG_value_type_param. If the latter, add them to the name
7869 here. If the name already has template parameters, then
7870 skip this step; some versions of GCC emit both, and
7871 it is more efficient to use the pre-computed name.
7872
7873 Something to keep in mind about this process: it is very
7874 unlikely, or in some cases downright impossible, to produce
7875 something that will match the mangled name of a function.
7876 If the definition of the function has the same debug info,
7877 we should be able to match up with it anyway. But fallbacks
7878 using the minimal symbol, for instance to find a method
7879 implemented in a stripped copy of libstdc++, will not work.
7880 If we do not have debug info for the definition, we will have to
7881 match them up some other way.
7882
7883 When we do name matching there is a related problem with function
7884 templates; two instantiated function templates are allowed to
7885 differ only by their return types, which we do not add here. */
7886
7887 if (cu->language == language_cplus && strchr (name, '<') == NULL)
7888 {
7889 struct attribute *attr;
7890 struct die_info *child;
7891 int first = 1;
7892
7893 die->building_fullname = 1;
7894
7895 for (child = die->child; child != NULL; child = child->sibling)
7896 {
7897 struct type *type;
7898 LONGEST value;
7899 const gdb_byte *bytes;
7900 struct dwarf2_locexpr_baton *baton;
7901 struct value *v;
7902
7903 if (child->tag != DW_TAG_template_type_param
7904 && child->tag != DW_TAG_template_value_param)
7905 continue;
7906
7907 if (first)
7908 {
7909 fputs_unfiltered ("<", buf);
7910 first = 0;
7911 }
7912 else
7913 fputs_unfiltered (", ", buf);
7914
7915 attr = dwarf2_attr (child, DW_AT_type, cu);
7916 if (attr == NULL)
7917 {
7918 complaint (&symfile_complaints,
7919 _("template parameter missing DW_AT_type"));
7920 fputs_unfiltered ("UNKNOWN_TYPE", buf);
7921 continue;
7922 }
7923 type = die_type (child, cu);
7924
7925 if (child->tag == DW_TAG_template_type_param)
7926 {
7927 c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
7928 continue;
7929 }
7930
7931 attr = dwarf2_attr (child, DW_AT_const_value, cu);
7932 if (attr == NULL)
7933 {
7934 complaint (&symfile_complaints,
7935 _("template parameter missing "
7936 "DW_AT_const_value"));
7937 fputs_unfiltered ("UNKNOWN_VALUE", buf);
7938 continue;
7939 }
7940
7941 dwarf2_const_value_attr (attr, type, name,
7942 &cu->comp_unit_obstack, cu,
7943 &value, &bytes, &baton);
7944
7945 if (TYPE_NOSIGN (type))
7946 /* GDB prints characters as NUMBER 'CHAR'. If that's
7947 changed, this can use value_print instead. */
7948 c_printchar (value, type, buf);
7949 else
7950 {
7951 struct value_print_options opts;
7952
7953 if (baton != NULL)
7954 v = dwarf2_evaluate_loc_desc (type, NULL,
7955 baton->data,
7956 baton->size,
7957 baton->per_cu);
7958 else if (bytes != NULL)
7959 {
7960 v = allocate_value (type);
7961 memcpy (value_contents_writeable (v), bytes,
7962 TYPE_LENGTH (type));
7963 }
7964 else
7965 v = value_from_longest (type, value);
7966
7967 /* Specify decimal so that we do not depend on
7968 the radix. */
7969 get_formatted_print_options (&opts, 'd');
7970 opts.raw = 1;
7971 value_print (v, buf, &opts);
7972 release_value (v);
7973 value_free (v);
7974 }
7975 }
7976
7977 die->building_fullname = 0;
7978
7979 if (!first)
7980 {
7981 /* Close the argument list, with a space if necessary
7982 (nested templates). */
7983 char last_char = '\0';
7984 ui_file_put (buf, do_ui_file_peek_last, &last_char);
7985 if (last_char == '>')
7986 fputs_unfiltered (" >", buf);
7987 else
7988 fputs_unfiltered (">", buf);
7989 }
7990 }
7991
7992 /* For Java and C++ methods, append formal parameter type
7993 information, if PHYSNAME. */
7994
7995 if (physname && die->tag == DW_TAG_subprogram
7996 && (cu->language == language_cplus
7997 || cu->language == language_java))
7998 {
7999 struct type *type = read_type_die (die, cu);
8000
8001 c_type_print_args (type, buf, 1, cu->language,
8002 &type_print_raw_options);
8003
8004 if (cu->language == language_java)
8005 {
8006 /* For java, we must append the return type to method
8007 names. */
8008 if (die->tag == DW_TAG_subprogram)
8009 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
8010 0, 0, &type_print_raw_options);
8011 }
8012 else if (cu->language == language_cplus)
8013 {
8014 /* Assume that an artificial first parameter is
8015 "this", but do not crash if it is not. RealView
8016 marks unnamed (and thus unused) parameters as
8017 artificial; there is no way to differentiate
8018 the two cases. */
8019 if (TYPE_NFIELDS (type) > 0
8020 && TYPE_FIELD_ARTIFICIAL (type, 0)
8021 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
8022 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
8023 0))))
8024 fputs_unfiltered (" const", buf);
8025 }
8026 }
8027
8028 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
8029 &length);
8030 ui_file_delete (buf);
8031
8032 if (cu->language == language_cplus)
8033 {
8034 const char *cname
8035 = dwarf2_canonicalize_name (name, cu,
8036 &objfile->objfile_obstack);
8037
8038 if (cname != NULL)
8039 name = cname;
8040 }
8041 }
8042 }
8043
8044 return name;
8045 }
8046
8047 /* Return the fully qualified name of DIE, based on its DW_AT_name.
8048 If scope qualifiers are appropriate they will be added. The result
8049 will be allocated on the objfile_obstack, or NULL if the DIE does
8050 not have a name. NAME may either be from a previous call to
8051 dwarf2_name or NULL.
8052
8053 The output string will be canonicalized (if C++/Java). */
8054
8055 static const char *
8056 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8057 {
8058 return dwarf2_compute_name (name, die, cu, 0);
8059 }
8060
8061 /* Construct a physname for the given DIE in CU. NAME may either be
8062 from a previous call to dwarf2_name or NULL. The result will be
8063 allocated on the objfile_objstack or NULL if the DIE does not have a
8064 name.
8065
8066 The output string will be canonicalized (if C++/Java). */
8067
8068 static const char *
8069 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
8070 {
8071 struct objfile *objfile = cu->objfile;
8072 struct attribute *attr;
8073 const char *retval, *mangled = NULL, *canon = NULL;
8074 struct cleanup *back_to;
8075 int need_copy = 1;
8076
8077 /* In this case dwarf2_compute_name is just a shortcut not building anything
8078 on its own. */
8079 if (!die_needs_namespace (die, cu))
8080 return dwarf2_compute_name (name, die, cu, 1);
8081
8082 back_to = make_cleanup (null_cleanup, NULL);
8083
8084 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
8085 if (!attr)
8086 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
8087
8088 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
8089 has computed. */
8090 if (attr && DW_STRING (attr))
8091 {
8092 char *demangled;
8093
8094 mangled = DW_STRING (attr);
8095
8096 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
8097 type. It is easier for GDB users to search for such functions as
8098 `name(params)' than `long name(params)'. In such case the minimal
8099 symbol names do not match the full symbol names but for template
8100 functions there is never a need to look up their definition from their
8101 declaration so the only disadvantage remains the minimal symbol
8102 variant `long name(params)' does not have the proper inferior type.
8103 */
8104
8105 if (cu->language == language_go)
8106 {
8107 /* This is a lie, but we already lie to the caller new_symbol_full.
8108 new_symbol_full assumes we return the mangled name.
8109 This just undoes that lie until things are cleaned up. */
8110 demangled = NULL;
8111 }
8112 else
8113 {
8114 demangled = gdb_demangle (mangled,
8115 (DMGL_PARAMS | DMGL_ANSI
8116 | (cu->language == language_java
8117 ? DMGL_JAVA | DMGL_RET_POSTFIX
8118 : DMGL_RET_DROP)));
8119 }
8120 if (demangled)
8121 {
8122 make_cleanup (xfree, demangled);
8123 canon = demangled;
8124 }
8125 else
8126 {
8127 canon = mangled;
8128 need_copy = 0;
8129 }
8130 }
8131
8132 if (canon == NULL || check_physname)
8133 {
8134 const char *physname = dwarf2_compute_name (name, die, cu, 1);
8135
8136 if (canon != NULL && strcmp (physname, canon) != 0)
8137 {
8138 /* It may not mean a bug in GDB. The compiler could also
8139 compute DW_AT_linkage_name incorrectly. But in such case
8140 GDB would need to be bug-to-bug compatible. */
8141
8142 complaint (&symfile_complaints,
8143 _("Computed physname <%s> does not match demangled <%s> "
8144 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
8145 physname, canon, mangled, die->offset.sect_off, objfile->name);
8146
8147 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
8148 is available here - over computed PHYSNAME. It is safer
8149 against both buggy GDB and buggy compilers. */
8150
8151 retval = canon;
8152 }
8153 else
8154 {
8155 retval = physname;
8156 need_copy = 0;
8157 }
8158 }
8159 else
8160 retval = canon;
8161
8162 if (need_copy)
8163 retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
8164
8165 do_cleanups (back_to);
8166 return retval;
8167 }
8168
8169 /* Read the import statement specified by the given die and record it. */
8170
8171 static void
8172 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
8173 {
8174 struct objfile *objfile = cu->objfile;
8175 struct attribute *import_attr;
8176 struct die_info *imported_die, *child_die;
8177 struct dwarf2_cu *imported_cu;
8178 const char *imported_name;
8179 const char *imported_name_prefix;
8180 const char *canonical_name;
8181 const char *import_alias;
8182 const char *imported_declaration = NULL;
8183 const char *import_prefix;
8184 VEC (const_char_ptr) *excludes = NULL;
8185 struct cleanup *cleanups;
8186
8187 import_attr = dwarf2_attr (die, DW_AT_import, cu);
8188 if (import_attr == NULL)
8189 {
8190 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8191 dwarf_tag_name (die->tag));
8192 return;
8193 }
8194
8195 imported_cu = cu;
8196 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
8197 imported_name = dwarf2_name (imported_die, imported_cu);
8198 if (imported_name == NULL)
8199 {
8200 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
8201
8202 The import in the following code:
8203 namespace A
8204 {
8205 typedef int B;
8206 }
8207
8208 int main ()
8209 {
8210 using A::B;
8211 B b;
8212 return b;
8213 }
8214
8215 ...
8216 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
8217 <52> DW_AT_decl_file : 1
8218 <53> DW_AT_decl_line : 6
8219 <54> DW_AT_import : <0x75>
8220 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
8221 <59> DW_AT_name : B
8222 <5b> DW_AT_decl_file : 1
8223 <5c> DW_AT_decl_line : 2
8224 <5d> DW_AT_type : <0x6e>
8225 ...
8226 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
8227 <76> DW_AT_byte_size : 4
8228 <77> DW_AT_encoding : 5 (signed)
8229
8230 imports the wrong die ( 0x75 instead of 0x58 ).
8231 This case will be ignored until the gcc bug is fixed. */
8232 return;
8233 }
8234
8235 /* Figure out the local name after import. */
8236 import_alias = dwarf2_name (die, cu);
8237
8238 /* Figure out where the statement is being imported to. */
8239 import_prefix = determine_prefix (die, cu);
8240
8241 /* Figure out what the scope of the imported die is and prepend it
8242 to the name of the imported die. */
8243 imported_name_prefix = determine_prefix (imported_die, imported_cu);
8244
8245 if (imported_die->tag != DW_TAG_namespace
8246 && imported_die->tag != DW_TAG_module)
8247 {
8248 imported_declaration = imported_name;
8249 canonical_name = imported_name_prefix;
8250 }
8251 else if (strlen (imported_name_prefix) > 0)
8252 canonical_name = obconcat (&objfile->objfile_obstack,
8253 imported_name_prefix, "::", imported_name,
8254 (char *) NULL);
8255 else
8256 canonical_name = imported_name;
8257
8258 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
8259
8260 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
8261 for (child_die = die->child; child_die && child_die->tag;
8262 child_die = sibling_die (child_die))
8263 {
8264 /* DWARF-4: A Fortran use statement with a “rename list” may be
8265 represented by an imported module entry with an import attribute
8266 referring to the module and owned entries corresponding to those
8267 entities that are renamed as part of being imported. */
8268
8269 if (child_die->tag != DW_TAG_imported_declaration)
8270 {
8271 complaint (&symfile_complaints,
8272 _("child DW_TAG_imported_declaration expected "
8273 "- DIE at 0x%x [in module %s]"),
8274 child_die->offset.sect_off, objfile->name);
8275 continue;
8276 }
8277
8278 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
8279 if (import_attr == NULL)
8280 {
8281 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
8282 dwarf_tag_name (child_die->tag));
8283 continue;
8284 }
8285
8286 imported_cu = cu;
8287 imported_die = follow_die_ref_or_sig (child_die, import_attr,
8288 &imported_cu);
8289 imported_name = dwarf2_name (imported_die, imported_cu);
8290 if (imported_name == NULL)
8291 {
8292 complaint (&symfile_complaints,
8293 _("child DW_TAG_imported_declaration has unknown "
8294 "imported name - DIE at 0x%x [in module %s]"),
8295 child_die->offset.sect_off, objfile->name);
8296 continue;
8297 }
8298
8299 VEC_safe_push (const_char_ptr, excludes, imported_name);
8300
8301 process_die (child_die, cu);
8302 }
8303
8304 cp_add_using_directive (import_prefix,
8305 canonical_name,
8306 import_alias,
8307 imported_declaration,
8308 excludes,
8309 0,
8310 &objfile->objfile_obstack);
8311
8312 do_cleanups (cleanups);
8313 }
8314
8315 /* Cleanup function for handle_DW_AT_stmt_list. */
8316
8317 static void
8318 free_cu_line_header (void *arg)
8319 {
8320 struct dwarf2_cu *cu = arg;
8321
8322 free_line_header (cu->line_header);
8323 cu->line_header = NULL;
8324 }
8325
8326 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
8327 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
8328 this, it was first present in GCC release 4.3.0. */
8329
8330 static int
8331 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
8332 {
8333 if (!cu->checked_producer)
8334 check_producer (cu);
8335
8336 return cu->producer_is_gcc_lt_4_3;
8337 }
8338
8339 static void
8340 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
8341 const char **name, const char **comp_dir)
8342 {
8343 struct attribute *attr;
8344
8345 *name = NULL;
8346 *comp_dir = NULL;
8347
8348 /* Find the filename. Do not use dwarf2_name here, since the filename
8349 is not a source language identifier. */
8350 attr = dwarf2_attr (die, DW_AT_name, cu);
8351 if (attr)
8352 {
8353 *name = DW_STRING (attr);
8354 }
8355
8356 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
8357 if (attr)
8358 *comp_dir = DW_STRING (attr);
8359 else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
8360 && IS_ABSOLUTE_PATH (*name))
8361 {
8362 char *d = ldirname (*name);
8363
8364 *comp_dir = d;
8365 if (d != NULL)
8366 make_cleanup (xfree, d);
8367 }
8368 if (*comp_dir != NULL)
8369 {
8370 /* Irix 6.2 native cc prepends <machine>.: to the compilation
8371 directory, get rid of it. */
8372 char *cp = strchr (*comp_dir, ':');
8373
8374 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
8375 *comp_dir = cp + 1;
8376 }
8377
8378 if (*name == NULL)
8379 *name = "<unknown>";
8380 }
8381
8382 /* Handle DW_AT_stmt_list for a compilation unit.
8383 DIE is the DW_TAG_compile_unit die for CU.
8384 COMP_DIR is the compilation directory.
8385 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
8386
8387 static void
8388 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
8389 const char *comp_dir) /* ARI: editCase function */
8390 {
8391 struct attribute *attr;
8392
8393 gdb_assert (! cu->per_cu->is_debug_types);
8394
8395 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8396 if (attr)
8397 {
8398 unsigned int line_offset = DW_UNSND (attr);
8399 struct line_header *line_header
8400 = dwarf_decode_line_header (line_offset, cu);
8401
8402 if (line_header)
8403 {
8404 cu->line_header = line_header;
8405 make_cleanup (free_cu_line_header, cu);
8406 dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
8407 }
8408 }
8409 }
8410
8411 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
8412
8413 static void
8414 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
8415 {
8416 struct objfile *objfile = dwarf2_per_objfile->objfile;
8417 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
8418 CORE_ADDR lowpc = ((CORE_ADDR) -1);
8419 CORE_ADDR highpc = ((CORE_ADDR) 0);
8420 struct attribute *attr;
8421 const char *name = NULL;
8422 const char *comp_dir = NULL;
8423 struct die_info *child_die;
8424 bfd *abfd = objfile->obfd;
8425 CORE_ADDR baseaddr;
8426
8427 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8428
8429 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
8430
8431 /* If we didn't find a lowpc, set it to highpc to avoid complaints
8432 from finish_block. */
8433 if (lowpc == ((CORE_ADDR) -1))
8434 lowpc = highpc;
8435 lowpc += baseaddr;
8436 highpc += baseaddr;
8437
8438 find_file_and_directory (die, cu, &name, &comp_dir);
8439
8440 prepare_one_comp_unit (cu, die, cu->language);
8441
8442 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
8443 standardised yet. As a workaround for the language detection we fall
8444 back to the DW_AT_producer string. */
8445 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
8446 cu->language = language_opencl;
8447
8448 /* Similar hack for Go. */
8449 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
8450 set_cu_language (DW_LANG_Go, cu);
8451
8452 dwarf2_start_symtab (cu, name, comp_dir, lowpc);
8453
8454 /* Decode line number information if present. We do this before
8455 processing child DIEs, so that the line header table is available
8456 for DW_AT_decl_file. */
8457 handle_DW_AT_stmt_list (die, cu, comp_dir);
8458
8459 /* Process all dies in compilation unit. */
8460 if (die->child != NULL)
8461 {
8462 child_die = die->child;
8463 while (child_die && child_die->tag)
8464 {
8465 process_die (child_die, cu);
8466 child_die = sibling_die (child_die);
8467 }
8468 }
8469
8470 /* Decode macro information, if present. Dwarf 2 macro information
8471 refers to information in the line number info statement program
8472 header, so we can only read it if we've read the header
8473 successfully. */
8474 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
8475 if (attr && cu->line_header)
8476 {
8477 if (dwarf2_attr (die, DW_AT_macro_info, cu))
8478 complaint (&symfile_complaints,
8479 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
8480
8481 dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
8482 }
8483 else
8484 {
8485 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
8486 if (attr && cu->line_header)
8487 {
8488 unsigned int macro_offset = DW_UNSND (attr);
8489
8490 dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
8491 }
8492 }
8493
8494 do_cleanups (back_to);
8495 }
8496
8497 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
8498 Create the set of symtabs used by this TU, or if this TU is sharing
8499 symtabs with another TU and the symtabs have already been created
8500 then restore those symtabs in the line header.
8501 We don't need the pc/line-number mapping for type units. */
8502
8503 static void
8504 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
8505 {
8506 struct objfile *objfile = dwarf2_per_objfile->objfile;
8507 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
8508 struct type_unit_group *tu_group;
8509 int first_time;
8510 struct line_header *lh;
8511 struct attribute *attr;
8512 unsigned int i, line_offset;
8513 struct signatured_type *sig_type;
8514
8515 gdb_assert (per_cu->is_debug_types);
8516 sig_type = (struct signatured_type *) per_cu;
8517
8518 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
8519
8520 /* If we're using .gdb_index (includes -readnow) then
8521 per_cu->type_unit_group may not have been set up yet. */
8522 if (sig_type->type_unit_group == NULL)
8523 sig_type->type_unit_group = get_type_unit_group (cu, attr);
8524 tu_group = sig_type->type_unit_group;
8525
8526 /* If we've already processed this stmt_list there's no real need to
8527 do it again, we could fake it and just recreate the part we need
8528 (file name,index -> symtab mapping). If data shows this optimization
8529 is useful we can do it then. */
8530 first_time = tu_group->primary_symtab == NULL;
8531
8532 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
8533 debug info. */
8534 lh = NULL;
8535 if (attr != NULL)
8536 {
8537 line_offset = DW_UNSND (attr);
8538 lh = dwarf_decode_line_header (line_offset, cu);
8539 }
8540 if (lh == NULL)
8541 {
8542 if (first_time)
8543 dwarf2_start_symtab (cu, "", NULL, 0);
8544 else
8545 {
8546 gdb_assert (tu_group->symtabs == NULL);
8547 restart_symtab (0);
8548 }
8549 /* Note: The primary symtab will get allocated at the end. */
8550 return;
8551 }
8552
8553 cu->line_header = lh;
8554 make_cleanup (free_cu_line_header, cu);
8555
8556 if (first_time)
8557 {
8558 dwarf2_start_symtab (cu, "", NULL, 0);
8559
8560 tu_group->num_symtabs = lh->num_file_names;
8561 tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
8562
8563 for (i = 0; i < lh->num_file_names; ++i)
8564 {
8565 const char *dir = NULL;
8566 struct file_entry *fe = &lh->file_names[i];
8567
8568 if (fe->dir_index)
8569 dir = lh->include_dirs[fe->dir_index - 1];
8570 dwarf2_start_subfile (fe->name, dir, NULL);
8571
8572 /* Note: We don't have to watch for the main subfile here, type units
8573 don't have DW_AT_name. */
8574
8575 if (current_subfile->symtab == NULL)
8576 {
8577 /* NOTE: start_subfile will recognize when it's been passed
8578 a file it has already seen. So we can't assume there's a
8579 simple mapping from lh->file_names to subfiles,
8580 lh->file_names may contain dups. */
8581 current_subfile->symtab = allocate_symtab (current_subfile->name,
8582 objfile);
8583 }
8584
8585 fe->symtab = current_subfile->symtab;
8586 tu_group->symtabs[i] = fe->symtab;
8587 }
8588 }
8589 else
8590 {
8591 restart_symtab (0);
8592
8593 for (i = 0; i < lh->num_file_names; ++i)
8594 {
8595 struct file_entry *fe = &lh->file_names[i];
8596
8597 fe->symtab = tu_group->symtabs[i];
8598 }
8599 }
8600
8601 /* The main symtab is allocated last. Type units don't have DW_AT_name
8602 so they don't have a "real" (so to speak) symtab anyway.
8603 There is later code that will assign the main symtab to all symbols
8604 that don't have one. We need to handle the case of a symbol with a
8605 missing symtab (DW_AT_decl_file) anyway. */
8606 }
8607
8608 /* Process DW_TAG_type_unit.
8609 For TUs we want to skip the first top level sibling if it's not the
8610 actual type being defined by this TU. In this case the first top
8611 level sibling is there to provide context only. */
8612
8613 static void
8614 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
8615 {
8616 struct die_info *child_die;
8617
8618 prepare_one_comp_unit (cu, die, language_minimal);
8619
8620 /* Initialize (or reinitialize) the machinery for building symtabs.
8621 We do this before processing child DIEs, so that the line header table
8622 is available for DW_AT_decl_file. */
8623 setup_type_unit_groups (die, cu);
8624
8625 if (die->child != NULL)
8626 {
8627 child_die = die->child;
8628 while (child_die && child_die->tag)
8629 {
8630 process_die (child_die, cu);
8631 child_die = sibling_die (child_die);
8632 }
8633 }
8634 }
8635 \f
8636 /* DWO/DWP files.
8637
8638 http://gcc.gnu.org/wiki/DebugFission
8639 http://gcc.gnu.org/wiki/DebugFissionDWP
8640
8641 To simplify handling of both DWO files ("object" files with the DWARF info)
8642 and DWP files (a file with the DWOs packaged up into one file), we treat
8643 DWP files as having a collection of virtual DWO files. */
8644
8645 static hashval_t
8646 hash_dwo_file (const void *item)
8647 {
8648 const struct dwo_file *dwo_file = item;
8649 hashval_t hash;
8650
8651 hash = htab_hash_string (dwo_file->dwo_name);
8652 if (dwo_file->comp_dir != NULL)
8653 hash += htab_hash_string (dwo_file->comp_dir);
8654 return hash;
8655 }
8656
8657 static int
8658 eq_dwo_file (const void *item_lhs, const void *item_rhs)
8659 {
8660 const struct dwo_file *lhs = item_lhs;
8661 const struct dwo_file *rhs = item_rhs;
8662
8663 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
8664 return 0;
8665 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
8666 return lhs->comp_dir == rhs->comp_dir;
8667 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
8668 }
8669
8670 /* Allocate a hash table for DWO files. */
8671
8672 static htab_t
8673 allocate_dwo_file_hash_table (void)
8674 {
8675 struct objfile *objfile = dwarf2_per_objfile->objfile;
8676
8677 return htab_create_alloc_ex (41,
8678 hash_dwo_file,
8679 eq_dwo_file,
8680 NULL,
8681 &objfile->objfile_obstack,
8682 hashtab_obstack_allocate,
8683 dummy_obstack_deallocate);
8684 }
8685
8686 /* Lookup DWO file DWO_NAME. */
8687
8688 static void **
8689 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
8690 {
8691 struct dwo_file find_entry;
8692 void **slot;
8693
8694 if (dwarf2_per_objfile->dwo_files == NULL)
8695 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
8696
8697 memset (&find_entry, 0, sizeof (find_entry));
8698 find_entry.dwo_name = dwo_name;
8699 find_entry.comp_dir = comp_dir;
8700 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
8701
8702 return slot;
8703 }
8704
8705 static hashval_t
8706 hash_dwo_unit (const void *item)
8707 {
8708 const struct dwo_unit *dwo_unit = item;
8709
8710 /* This drops the top 32 bits of the id, but is ok for a hash. */
8711 return dwo_unit->signature;
8712 }
8713
8714 static int
8715 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
8716 {
8717 const struct dwo_unit *lhs = item_lhs;
8718 const struct dwo_unit *rhs = item_rhs;
8719
8720 /* The signature is assumed to be unique within the DWO file.
8721 So while object file CU dwo_id's always have the value zero,
8722 that's OK, assuming each object file DWO file has only one CU,
8723 and that's the rule for now. */
8724 return lhs->signature == rhs->signature;
8725 }
8726
8727 /* Allocate a hash table for DWO CUs,TUs.
8728 There is one of these tables for each of CUs,TUs for each DWO file. */
8729
8730 static htab_t
8731 allocate_dwo_unit_table (struct objfile *objfile)
8732 {
8733 /* Start out with a pretty small number.
8734 Generally DWO files contain only one CU and maybe some TUs. */
8735 return htab_create_alloc_ex (3,
8736 hash_dwo_unit,
8737 eq_dwo_unit,
8738 NULL,
8739 &objfile->objfile_obstack,
8740 hashtab_obstack_allocate,
8741 dummy_obstack_deallocate);
8742 }
8743
8744 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
8745
8746 struct create_dwo_cu_data
8747 {
8748 struct dwo_file *dwo_file;
8749 struct dwo_unit dwo_unit;
8750 };
8751
8752 /* die_reader_func for create_dwo_cu. */
8753
8754 static void
8755 create_dwo_cu_reader (const struct die_reader_specs *reader,
8756 const gdb_byte *info_ptr,
8757 struct die_info *comp_unit_die,
8758 int has_children,
8759 void *datap)
8760 {
8761 struct dwarf2_cu *cu = reader->cu;
8762 struct objfile *objfile = dwarf2_per_objfile->objfile;
8763 sect_offset offset = cu->per_cu->offset;
8764 struct dwarf2_section_info *section = cu->per_cu->section;
8765 struct create_dwo_cu_data *data = datap;
8766 struct dwo_file *dwo_file = data->dwo_file;
8767 struct dwo_unit *dwo_unit = &data->dwo_unit;
8768 struct attribute *attr;
8769
8770 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
8771 if (attr == NULL)
8772 {
8773 complaint (&symfile_complaints,
8774 _("Dwarf Error: debug entry at offset 0x%x is missing"
8775 " its dwo_id [in module %s]"),
8776 offset.sect_off, dwo_file->dwo_name);
8777 return;
8778 }
8779
8780 dwo_unit->dwo_file = dwo_file;
8781 dwo_unit->signature = DW_UNSND (attr);
8782 dwo_unit->section = section;
8783 dwo_unit->offset = offset;
8784 dwo_unit->length = cu->per_cu->length;
8785
8786 if (dwarf2_read_debug)
8787 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, dwo_id %s\n",
8788 offset.sect_off, hex_string (dwo_unit->signature));
8789 }
8790
8791 /* Create the dwo_unit for the lone CU in DWO_FILE.
8792 Note: This function processes DWO files only, not DWP files. */
8793
8794 static struct dwo_unit *
8795 create_dwo_cu (struct dwo_file *dwo_file)
8796 {
8797 struct objfile *objfile = dwarf2_per_objfile->objfile;
8798 struct dwarf2_section_info *section = &dwo_file->sections.info;
8799 bfd *abfd;
8800 htab_t cu_htab;
8801 const gdb_byte *info_ptr, *end_ptr;
8802 struct create_dwo_cu_data create_dwo_cu_data;
8803 struct dwo_unit *dwo_unit;
8804
8805 dwarf2_read_section (objfile, section);
8806 info_ptr = section->buffer;
8807
8808 if (info_ptr == NULL)
8809 return NULL;
8810
8811 /* We can't set abfd until now because the section may be empty or
8812 not present, in which case section->asection will be NULL. */
8813 abfd = section->asection->owner;
8814
8815 if (dwarf2_read_debug)
8816 {
8817 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
8818 bfd_section_name (abfd, section->asection),
8819 bfd_get_filename (abfd));
8820 }
8821
8822 create_dwo_cu_data.dwo_file = dwo_file;
8823 dwo_unit = NULL;
8824
8825 end_ptr = info_ptr + section->size;
8826 while (info_ptr < end_ptr)
8827 {
8828 struct dwarf2_per_cu_data per_cu;
8829
8830 memset (&create_dwo_cu_data.dwo_unit, 0,
8831 sizeof (create_dwo_cu_data.dwo_unit));
8832 memset (&per_cu, 0, sizeof (per_cu));
8833 per_cu.objfile = objfile;
8834 per_cu.is_debug_types = 0;
8835 per_cu.offset.sect_off = info_ptr - section->buffer;
8836 per_cu.section = section;
8837
8838 init_cutu_and_read_dies_no_follow (&per_cu,
8839 &dwo_file->sections.abbrev,
8840 dwo_file,
8841 create_dwo_cu_reader,
8842 &create_dwo_cu_data);
8843
8844 if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
8845 {
8846 /* If we've already found one, complain. We only support one
8847 because having more than one requires hacking the dwo_name of
8848 each to match, which is highly unlikely to happen. */
8849 if (dwo_unit != NULL)
8850 {
8851 complaint (&symfile_complaints,
8852 _("Multiple CUs in DWO file %s [in module %s]"),
8853 dwo_file->dwo_name, objfile->name);
8854 break;
8855 }
8856
8857 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
8858 *dwo_unit = create_dwo_cu_data.dwo_unit;
8859 }
8860
8861 info_ptr += per_cu.length;
8862 }
8863
8864 return dwo_unit;
8865 }
8866
8867 /* DWP file .debug_{cu,tu}_index section format:
8868 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
8869
8870 DWP Version 1:
8871
8872 Both index sections have the same format, and serve to map a 64-bit
8873 signature to a set of section numbers. Each section begins with a header,
8874 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
8875 indexes, and a pool of 32-bit section numbers. The index sections will be
8876 aligned at 8-byte boundaries in the file.
8877
8878 The index section header consists of:
8879
8880 V, 32 bit version number
8881 -, 32 bits unused
8882 N, 32 bit number of compilation units or type units in the index
8883 M, 32 bit number of slots in the hash table
8884
8885 Numbers are recorded using the byte order of the application binary.
8886
8887 We assume that N and M will not exceed 2^32 - 1.
8888
8889 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
8890
8891 The hash table begins at offset 16 in the section, and consists of an array
8892 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
8893 order of the application binary). Unused slots in the hash table are 0.
8894 (We rely on the extreme unlikeliness of a signature being exactly 0.)
8895
8896 The parallel table begins immediately after the hash table
8897 (at offset 16 + 8 * M from the beginning of the section), and consists of an
8898 array of 32-bit indexes (using the byte order of the application binary),
8899 corresponding 1-1 with slots in the hash table. Each entry in the parallel
8900 table contains a 32-bit index into the pool of section numbers. For unused
8901 hash table slots, the corresponding entry in the parallel table will be 0.
8902
8903 Given a 64-bit compilation unit signature or a type signature S, an entry
8904 in the hash table is located as follows:
8905
8906 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
8907 the low-order k bits all set to 1.
8908
8909 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
8910
8911 3) If the hash table entry at index H matches the signature, use that
8912 entry. If the hash table entry at index H is unused (all zeroes),
8913 terminate the search: the signature is not present in the table.
8914
8915 4) Let H = (H + H') modulo M. Repeat at Step 3.
8916
8917 Because M > N and H' and M are relatively prime, the search is guaranteed
8918 to stop at an unused slot or find the match.
8919
8920 The pool of section numbers begins immediately following the hash table
8921 (at offset 16 + 12 * M from the beginning of the section). The pool of
8922 section numbers consists of an array of 32-bit words (using the byte order
8923 of the application binary). Each item in the array is indexed starting
8924 from 0. The hash table entry provides the index of the first section
8925 number in the set. Additional section numbers in the set follow, and the
8926 set is terminated by a 0 entry (section number 0 is not used in ELF).
8927
8928 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
8929 section must be the first entry in the set, and the .debug_abbrev.dwo must
8930 be the second entry. Other members of the set may follow in any order. */
8931
8932 /* Create a hash table to map DWO IDs to their CU/TU entry in
8933 .debug_{info,types}.dwo in DWP_FILE.
8934 Returns NULL if there isn't one.
8935 Note: This function processes DWP files only, not DWO files. */
8936
8937 static struct dwp_hash_table *
8938 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
8939 {
8940 struct objfile *objfile = dwarf2_per_objfile->objfile;
8941 bfd *dbfd = dwp_file->dbfd;
8942 const gdb_byte *index_ptr, *index_end;
8943 struct dwarf2_section_info *index;
8944 uint32_t version, nr_units, nr_slots;
8945 struct dwp_hash_table *htab;
8946
8947 if (is_debug_types)
8948 index = &dwp_file->sections.tu_index;
8949 else
8950 index = &dwp_file->sections.cu_index;
8951
8952 if (dwarf2_section_empty_p (index))
8953 return NULL;
8954 dwarf2_read_section (objfile, index);
8955
8956 index_ptr = index->buffer;
8957 index_end = index_ptr + index->size;
8958
8959 version = read_4_bytes (dbfd, index_ptr);
8960 index_ptr += 8; /* Skip the unused word. */
8961 nr_units = read_4_bytes (dbfd, index_ptr);
8962 index_ptr += 4;
8963 nr_slots = read_4_bytes (dbfd, index_ptr);
8964 index_ptr += 4;
8965
8966 if (version != 1)
8967 {
8968 error (_("Dwarf Error: unsupported DWP file version (%s)"
8969 " [in module %s]"),
8970 pulongest (version), dwp_file->name);
8971 }
8972 if (nr_slots != (nr_slots & -nr_slots))
8973 {
8974 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
8975 " is not power of 2 [in module %s]"),
8976 pulongest (nr_slots), dwp_file->name);
8977 }
8978
8979 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
8980 htab->nr_units = nr_units;
8981 htab->nr_slots = nr_slots;
8982 htab->hash_table = index_ptr;
8983 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
8984 htab->section_pool = htab->unit_table + sizeof (uint32_t) * nr_slots;
8985
8986 return htab;
8987 }
8988
8989 /* Update SECTIONS with the data from SECTP.
8990
8991 This function is like the other "locate" section routines that are
8992 passed to bfd_map_over_sections, but in this context the sections to
8993 read comes from the DWP hash table, not the full ELF section table.
8994
8995 The result is non-zero for success, or zero if an error was found. */
8996
8997 static int
8998 locate_virtual_dwo_sections (asection *sectp,
8999 struct virtual_dwo_sections *sections)
9000 {
9001 const struct dwop_section_names *names = &dwop_section_names;
9002
9003 if (section_is_p (sectp->name, &names->abbrev_dwo))
9004 {
9005 /* There can be only one. */
9006 if (sections->abbrev.asection != NULL)
9007 return 0;
9008 sections->abbrev.asection = sectp;
9009 sections->abbrev.size = bfd_get_section_size (sectp);
9010 }
9011 else if (section_is_p (sectp->name, &names->info_dwo)
9012 || section_is_p (sectp->name, &names->types_dwo))
9013 {
9014 /* There can be only one. */
9015 if (sections->info_or_types.asection != NULL)
9016 return 0;
9017 sections->info_or_types.asection = sectp;
9018 sections->info_or_types.size = bfd_get_section_size (sectp);
9019 }
9020 else if (section_is_p (sectp->name, &names->line_dwo))
9021 {
9022 /* There can be only one. */
9023 if (sections->line.asection != NULL)
9024 return 0;
9025 sections->line.asection = sectp;
9026 sections->line.size = bfd_get_section_size (sectp);
9027 }
9028 else if (section_is_p (sectp->name, &names->loc_dwo))
9029 {
9030 /* There can be only one. */
9031 if (sections->loc.asection != NULL)
9032 return 0;
9033 sections->loc.asection = sectp;
9034 sections->loc.size = bfd_get_section_size (sectp);
9035 }
9036 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9037 {
9038 /* There can be only one. */
9039 if (sections->macinfo.asection != NULL)
9040 return 0;
9041 sections->macinfo.asection = sectp;
9042 sections->macinfo.size = bfd_get_section_size (sectp);
9043 }
9044 else if (section_is_p (sectp->name, &names->macro_dwo))
9045 {
9046 /* There can be only one. */
9047 if (sections->macro.asection != NULL)
9048 return 0;
9049 sections->macro.asection = sectp;
9050 sections->macro.size = bfd_get_section_size (sectp);
9051 }
9052 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9053 {
9054 /* There can be only one. */
9055 if (sections->str_offsets.asection != NULL)
9056 return 0;
9057 sections->str_offsets.asection = sectp;
9058 sections->str_offsets.size = bfd_get_section_size (sectp);
9059 }
9060 else
9061 {
9062 /* No other kind of section is valid. */
9063 return 0;
9064 }
9065
9066 return 1;
9067 }
9068
9069 /* Create a dwo_unit object for the DWO with signature SIGNATURE.
9070 HTAB is the hash table from the DWP file.
9071 SECTION_INDEX is the index of the DWO in HTAB.
9072 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU. */
9073
9074 static struct dwo_unit *
9075 create_dwo_in_dwp (struct dwp_file *dwp_file,
9076 const struct dwp_hash_table *htab,
9077 uint32_t section_index,
9078 const char *comp_dir,
9079 ULONGEST signature, int is_debug_types)
9080 {
9081 struct objfile *objfile = dwarf2_per_objfile->objfile;
9082 bfd *dbfd = dwp_file->dbfd;
9083 const char *kind = is_debug_types ? "TU" : "CU";
9084 struct dwo_file *dwo_file;
9085 struct dwo_unit *dwo_unit;
9086 struct virtual_dwo_sections sections;
9087 void **dwo_file_slot;
9088 char *virtual_dwo_name;
9089 struct dwarf2_section_info *cutu;
9090 struct cleanup *cleanups;
9091 int i;
9092
9093 if (dwarf2_read_debug)
9094 {
9095 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP file: %s\n",
9096 kind,
9097 pulongest (section_index), hex_string (signature),
9098 dwp_file->name);
9099 }
9100
9101 /* Fetch the sections of this DWO.
9102 Put a limit on the number of sections we look for so that bad data
9103 doesn't cause us to loop forever. */
9104
9105 #define MAX_NR_DWO_SECTIONS \
9106 (1 /* .debug_info or .debug_types */ \
9107 + 1 /* .debug_abbrev */ \
9108 + 1 /* .debug_line */ \
9109 + 1 /* .debug_loc */ \
9110 + 1 /* .debug_str_offsets */ \
9111 + 1 /* .debug_macro */ \
9112 + 1 /* .debug_macinfo */ \
9113 + 1 /* trailing zero */)
9114
9115 memset (&sections, 0, sizeof (sections));
9116 cleanups = make_cleanup (null_cleanup, 0);
9117
9118 for (i = 0; i < MAX_NR_DWO_SECTIONS; ++i)
9119 {
9120 asection *sectp;
9121 uint32_t section_nr =
9122 read_4_bytes (dbfd,
9123 htab->section_pool
9124 + (section_index + i) * sizeof (uint32_t));
9125
9126 if (section_nr == 0)
9127 break;
9128 if (section_nr >= dwp_file->num_sections)
9129 {
9130 error (_("Dwarf Error: bad DWP hash table, section number too large"
9131 " [in module %s]"),
9132 dwp_file->name);
9133 }
9134
9135 sectp = dwp_file->elf_sections[section_nr];
9136 if (! locate_virtual_dwo_sections (sectp, &sections))
9137 {
9138 error (_("Dwarf Error: bad DWP hash table, invalid section found"
9139 " [in module %s]"),
9140 dwp_file->name);
9141 }
9142 }
9143
9144 if (i < 2
9145 || sections.info_or_types.asection == NULL
9146 || sections.abbrev.asection == NULL)
9147 {
9148 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
9149 " [in module %s]"),
9150 dwp_file->name);
9151 }
9152 if (i == MAX_NR_DWO_SECTIONS)
9153 {
9154 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
9155 " [in module %s]"),
9156 dwp_file->name);
9157 }
9158
9159 /* It's easier for the rest of the code if we fake a struct dwo_file and
9160 have dwo_unit "live" in that. At least for now.
9161
9162 The DWP file can be made up of a random collection of CUs and TUs.
9163 However, for each CU + set of TUs that came from the same original DWO
9164 file, we want to combine them back into a virtual DWO file to save space
9165 (fewer struct dwo_file objects to allocated). Remember that for really
9166 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
9167
9168 virtual_dwo_name =
9169 xstrprintf ("virtual-dwo/%d-%d-%d-%d",
9170 sections.abbrev.asection ? sections.abbrev.asection->id : 0,
9171 sections.line.asection ? sections.line.asection->id : 0,
9172 sections.loc.asection ? sections.loc.asection->id : 0,
9173 (sections.str_offsets.asection
9174 ? sections.str_offsets.asection->id
9175 : 0));
9176 make_cleanup (xfree, virtual_dwo_name);
9177 /* Can we use an existing virtual DWO file? */
9178 dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
9179 /* Create one if necessary. */
9180 if (*dwo_file_slot == NULL)
9181 {
9182 if (dwarf2_read_debug)
9183 {
9184 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
9185 virtual_dwo_name);
9186 }
9187 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9188 dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
9189 virtual_dwo_name,
9190 strlen (virtual_dwo_name));
9191 dwo_file->comp_dir = comp_dir;
9192 dwo_file->sections.abbrev = sections.abbrev;
9193 dwo_file->sections.line = sections.line;
9194 dwo_file->sections.loc = sections.loc;
9195 dwo_file->sections.macinfo = sections.macinfo;
9196 dwo_file->sections.macro = sections.macro;
9197 dwo_file->sections.str_offsets = sections.str_offsets;
9198 /* The "str" section is global to the entire DWP file. */
9199 dwo_file->sections.str = dwp_file->sections.str;
9200 /* The info or types section is assigned later to dwo_unit,
9201 there's no need to record it in dwo_file.
9202 Also, we can't simply record type sections in dwo_file because
9203 we record a pointer into the vector in dwo_unit. As we collect more
9204 types we'll grow the vector and eventually have to reallocate space
9205 for it, invalidating all the pointers into the current copy. */
9206 *dwo_file_slot = dwo_file;
9207 }
9208 else
9209 {
9210 if (dwarf2_read_debug)
9211 {
9212 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
9213 virtual_dwo_name);
9214 }
9215 dwo_file = *dwo_file_slot;
9216 }
9217 do_cleanups (cleanups);
9218
9219 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
9220 dwo_unit->dwo_file = dwo_file;
9221 dwo_unit->signature = signature;
9222 dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
9223 sizeof (struct dwarf2_section_info));
9224 *dwo_unit->section = sections.info_or_types;
9225 /* offset, length, type_offset_in_tu are set later. */
9226
9227 return dwo_unit;
9228 }
9229
9230 /* Lookup the DWO with SIGNATURE in DWP_FILE. */
9231
9232 static struct dwo_unit *
9233 lookup_dwo_in_dwp (struct dwp_file *dwp_file,
9234 const struct dwp_hash_table *htab,
9235 const char *comp_dir,
9236 ULONGEST signature, int is_debug_types)
9237 {
9238 bfd *dbfd = dwp_file->dbfd;
9239 uint32_t mask = htab->nr_slots - 1;
9240 uint32_t hash = signature & mask;
9241 uint32_t hash2 = ((signature >> 32) & mask) | 1;
9242 unsigned int i;
9243 void **slot;
9244 struct dwo_unit find_dwo_cu, *dwo_cu;
9245
9246 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
9247 find_dwo_cu.signature = signature;
9248 slot = htab_find_slot (dwp_file->loaded_cutus, &find_dwo_cu, INSERT);
9249
9250 if (*slot != NULL)
9251 return *slot;
9252
9253 /* Use a for loop so that we don't loop forever on bad debug info. */
9254 for (i = 0; i < htab->nr_slots; ++i)
9255 {
9256 ULONGEST signature_in_table;
9257
9258 signature_in_table =
9259 read_8_bytes (dbfd, htab->hash_table + hash * sizeof (uint64_t));
9260 if (signature_in_table == signature)
9261 {
9262 uint32_t section_index =
9263 read_4_bytes (dbfd, htab->unit_table + hash * sizeof (uint32_t));
9264
9265 *slot = create_dwo_in_dwp (dwp_file, htab, section_index,
9266 comp_dir, signature, is_debug_types);
9267 return *slot;
9268 }
9269 if (signature_in_table == 0)
9270 return NULL;
9271 hash = (hash + hash2) & mask;
9272 }
9273
9274 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
9275 " [in module %s]"),
9276 dwp_file->name);
9277 }
9278
9279 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
9280 Open the file specified by FILE_NAME and hand it off to BFD for
9281 preliminary analysis. Return a newly initialized bfd *, which
9282 includes a canonicalized copy of FILE_NAME.
9283 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
9284 In case of trouble, return NULL.
9285 NOTE: This function is derived from symfile_bfd_open. */
9286
9287 static bfd *
9288 try_open_dwop_file (const char *file_name, int is_dwp)
9289 {
9290 bfd *sym_bfd;
9291 int desc, flags;
9292 char *absolute_name;
9293 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
9294 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
9295 to debug_file_directory. */
9296 char *search_path;
9297 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
9298
9299 if (*debug_file_directory != '\0')
9300 search_path = concat (".", dirname_separator_string, debug_file_directory,
9301 NULL);
9302 else
9303 search_path = xstrdup (".");
9304
9305 flags = 0;
9306 if (is_dwp)
9307 flags |= OPF_SEARCH_IN_PATH;
9308 desc = openp (search_path, flags, file_name,
9309 O_RDONLY | O_BINARY, &absolute_name);
9310 xfree (search_path);
9311 if (desc < 0)
9312 return NULL;
9313
9314 sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
9315 xfree (absolute_name);
9316 if (sym_bfd == NULL)
9317 return NULL;
9318 bfd_set_cacheable (sym_bfd, 1);
9319
9320 if (!bfd_check_format (sym_bfd, bfd_object))
9321 {
9322 gdb_bfd_unref (sym_bfd); /* This also closes desc. */
9323 return NULL;
9324 }
9325
9326 return sym_bfd;
9327 }
9328
9329 /* Try to open DWO file FILE_NAME.
9330 COMP_DIR is the DW_AT_comp_dir attribute.
9331 The result is the bfd handle of the file.
9332 If there is a problem finding or opening the file, return NULL.
9333 Upon success, the canonicalized path of the file is stored in the bfd,
9334 same as symfile_bfd_open. */
9335
9336 static bfd *
9337 open_dwo_file (const char *file_name, const char *comp_dir)
9338 {
9339 bfd *abfd;
9340
9341 if (IS_ABSOLUTE_PATH (file_name))
9342 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
9343
9344 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
9345
9346 if (comp_dir != NULL)
9347 {
9348 char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
9349
9350 /* NOTE: If comp_dir is a relative path, this will also try the
9351 search path, which seems useful. */
9352 abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/);
9353 xfree (path_to_try);
9354 if (abfd != NULL)
9355 return abfd;
9356 }
9357
9358 /* That didn't work, try debug-file-directory, which, despite its name,
9359 is a list of paths. */
9360
9361 if (*debug_file_directory == '\0')
9362 return NULL;
9363
9364 return try_open_dwop_file (file_name, 0 /*is_dwp*/);
9365 }
9366
9367 /* This function is mapped across the sections and remembers the offset and
9368 size of each of the DWO debugging sections we are interested in. */
9369
9370 static void
9371 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
9372 {
9373 struct dwo_sections *dwo_sections = dwo_sections_ptr;
9374 const struct dwop_section_names *names = &dwop_section_names;
9375
9376 if (section_is_p (sectp->name, &names->abbrev_dwo))
9377 {
9378 dwo_sections->abbrev.asection = sectp;
9379 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
9380 }
9381 else if (section_is_p (sectp->name, &names->info_dwo))
9382 {
9383 dwo_sections->info.asection = sectp;
9384 dwo_sections->info.size = bfd_get_section_size (sectp);
9385 }
9386 else if (section_is_p (sectp->name, &names->line_dwo))
9387 {
9388 dwo_sections->line.asection = sectp;
9389 dwo_sections->line.size = bfd_get_section_size (sectp);
9390 }
9391 else if (section_is_p (sectp->name, &names->loc_dwo))
9392 {
9393 dwo_sections->loc.asection = sectp;
9394 dwo_sections->loc.size = bfd_get_section_size (sectp);
9395 }
9396 else if (section_is_p (sectp->name, &names->macinfo_dwo))
9397 {
9398 dwo_sections->macinfo.asection = sectp;
9399 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
9400 }
9401 else if (section_is_p (sectp->name, &names->macro_dwo))
9402 {
9403 dwo_sections->macro.asection = sectp;
9404 dwo_sections->macro.size = bfd_get_section_size (sectp);
9405 }
9406 else if (section_is_p (sectp->name, &names->str_dwo))
9407 {
9408 dwo_sections->str.asection = sectp;
9409 dwo_sections->str.size = bfd_get_section_size (sectp);
9410 }
9411 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
9412 {
9413 dwo_sections->str_offsets.asection = sectp;
9414 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
9415 }
9416 else if (section_is_p (sectp->name, &names->types_dwo))
9417 {
9418 struct dwarf2_section_info type_section;
9419
9420 memset (&type_section, 0, sizeof (type_section));
9421 type_section.asection = sectp;
9422 type_section.size = bfd_get_section_size (sectp);
9423 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
9424 &type_section);
9425 }
9426 }
9427
9428 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
9429 by PER_CU. This is for the non-DWP case.
9430 The result is NULL if DWO_NAME can't be found. */
9431
9432 static struct dwo_file *
9433 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
9434 const char *dwo_name, const char *comp_dir)
9435 {
9436 struct objfile *objfile = dwarf2_per_objfile->objfile;
9437 struct dwo_file *dwo_file;
9438 bfd *dbfd;
9439 struct cleanup *cleanups;
9440
9441 dbfd = open_dwo_file (dwo_name, comp_dir);
9442 if (dbfd == NULL)
9443 {
9444 if (dwarf2_read_debug)
9445 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
9446 return NULL;
9447 }
9448 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
9449 dwo_file->dwo_name = dwo_name;
9450 dwo_file->comp_dir = comp_dir;
9451 dwo_file->dbfd = dbfd;
9452
9453 cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
9454
9455 bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
9456
9457 dwo_file->cu = create_dwo_cu (dwo_file);
9458
9459 dwo_file->tus = create_debug_types_hash_table (dwo_file,
9460 dwo_file->sections.types);
9461
9462 discard_cleanups (cleanups);
9463
9464 if (dwarf2_read_debug)
9465 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
9466
9467 return dwo_file;
9468 }
9469
9470 /* This function is mapped across the sections and remembers the offset and
9471 size of each of the DWP debugging sections we are interested in. */
9472
9473 static void
9474 dwarf2_locate_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
9475 {
9476 struct dwp_file *dwp_file = dwp_file_ptr;
9477 const struct dwop_section_names *names = &dwop_section_names;
9478 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
9479
9480 /* Record the ELF section number for later lookup: this is what the
9481 .debug_cu_index,.debug_tu_index tables use. */
9482 gdb_assert (elf_section_nr < dwp_file->num_sections);
9483 dwp_file->elf_sections[elf_section_nr] = sectp;
9484
9485 /* Look for specific sections that we need. */
9486 if (section_is_p (sectp->name, &names->str_dwo))
9487 {
9488 dwp_file->sections.str.asection = sectp;
9489 dwp_file->sections.str.size = bfd_get_section_size (sectp);
9490 }
9491 else if (section_is_p (sectp->name, &names->cu_index))
9492 {
9493 dwp_file->sections.cu_index.asection = sectp;
9494 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
9495 }
9496 else if (section_is_p (sectp->name, &names->tu_index))
9497 {
9498 dwp_file->sections.tu_index.asection = sectp;
9499 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
9500 }
9501 }
9502
9503 /* Hash function for dwp_file loaded CUs/TUs. */
9504
9505 static hashval_t
9506 hash_dwp_loaded_cutus (const void *item)
9507 {
9508 const struct dwo_unit *dwo_unit = item;
9509
9510 /* This drops the top 32 bits of the signature, but is ok for a hash. */
9511 return dwo_unit->signature;
9512 }
9513
9514 /* Equality function for dwp_file loaded CUs/TUs. */
9515
9516 static int
9517 eq_dwp_loaded_cutus (const void *a, const void *b)
9518 {
9519 const struct dwo_unit *dua = a;
9520 const struct dwo_unit *dub = b;
9521
9522 return dua->signature == dub->signature;
9523 }
9524
9525 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
9526
9527 static htab_t
9528 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
9529 {
9530 return htab_create_alloc_ex (3,
9531 hash_dwp_loaded_cutus,
9532 eq_dwp_loaded_cutus,
9533 NULL,
9534 &objfile->objfile_obstack,
9535 hashtab_obstack_allocate,
9536 dummy_obstack_deallocate);
9537 }
9538
9539 /* Try to open DWP file FILE_NAME.
9540 The result is the bfd handle of the file.
9541 If there is a problem finding or opening the file, return NULL.
9542 Upon success, the canonicalized path of the file is stored in the bfd,
9543 same as symfile_bfd_open. */
9544
9545 static bfd *
9546 open_dwp_file (const char *file_name)
9547 {
9548 return try_open_dwop_file (file_name, 1 /*is_dwp*/);
9549 }
9550
9551 /* Initialize the use of the DWP file for the current objfile.
9552 By convention the name of the DWP file is ${objfile}.dwp.
9553 The result is NULL if it can't be found. */
9554
9555 static struct dwp_file *
9556 open_and_init_dwp_file (void)
9557 {
9558 struct objfile *objfile = dwarf2_per_objfile->objfile;
9559 struct dwp_file *dwp_file;
9560 char *dwp_name;
9561 bfd *dbfd;
9562 struct cleanup *cleanups;
9563
9564 dwp_name = xstrprintf ("%s.dwp", dwarf2_per_objfile->objfile->name);
9565 cleanups = make_cleanup (xfree, dwp_name);
9566
9567 dbfd = open_dwp_file (dwp_name);
9568 if (dbfd == NULL)
9569 {
9570 if (dwarf2_read_debug)
9571 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
9572 do_cleanups (cleanups);
9573 return NULL;
9574 }
9575 dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
9576 dwp_file->name = obstack_copy0 (&objfile->objfile_obstack,
9577 dwp_name, strlen (dwp_name));
9578 dwp_file->dbfd = dbfd;
9579 do_cleanups (cleanups);
9580
9581 /* +1: section 0 is unused */
9582 dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
9583 dwp_file->elf_sections =
9584 OBSTACK_CALLOC (&objfile->objfile_obstack,
9585 dwp_file->num_sections, asection *);
9586
9587 bfd_map_over_sections (dbfd, dwarf2_locate_dwp_sections, dwp_file);
9588
9589 dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
9590
9591 dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
9592
9593 dwp_file->loaded_cutus = allocate_dwp_loaded_cutus_table (objfile);
9594
9595 if (dwarf2_read_debug)
9596 {
9597 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
9598 fprintf_unfiltered (gdb_stdlog,
9599 " %s CUs, %s TUs\n",
9600 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
9601 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
9602 }
9603
9604 return dwp_file;
9605 }
9606
9607 /* Wrapper around open_and_init_dwp_file, only open it once. */
9608
9609 static struct dwp_file *
9610 get_dwp_file (void)
9611 {
9612 if (! dwarf2_per_objfile->dwp_checked)
9613 {
9614 dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
9615 dwarf2_per_objfile->dwp_checked = 1;
9616 }
9617 return dwarf2_per_objfile->dwp_file;
9618 }
9619
9620 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
9621 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
9622 or in the DWP file for the objfile, referenced by THIS_UNIT.
9623 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
9624 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
9625
9626 This is called, for example, when wanting to read a variable with a
9627 complex location. Therefore we don't want to do file i/o for every call.
9628 Therefore we don't want to look for a DWO file on every call.
9629 Therefore we first see if we've already seen SIGNATURE in a DWP file,
9630 then we check if we've already seen DWO_NAME, and only THEN do we check
9631 for a DWO file.
9632
9633 The result is a pointer to the dwo_unit object or NULL if we didn't find it
9634 (dwo_id mismatch or couldn't find the DWO/DWP file). */
9635
9636 static struct dwo_unit *
9637 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
9638 const char *dwo_name, const char *comp_dir,
9639 ULONGEST signature, int is_debug_types)
9640 {
9641 struct objfile *objfile = dwarf2_per_objfile->objfile;
9642 const char *kind = is_debug_types ? "TU" : "CU";
9643 void **dwo_file_slot;
9644 struct dwo_file *dwo_file;
9645 struct dwp_file *dwp_file;
9646
9647 /* First see if there's a DWP file.
9648 If we have a DWP file but didn't find the DWO inside it, don't
9649 look for the original DWO file. It makes gdb behave differently
9650 depending on whether one is debugging in the build tree. */
9651
9652 dwp_file = get_dwp_file ();
9653 if (dwp_file != NULL)
9654 {
9655 const struct dwp_hash_table *dwp_htab =
9656 is_debug_types ? dwp_file->tus : dwp_file->cus;
9657
9658 if (dwp_htab != NULL)
9659 {
9660 struct dwo_unit *dwo_cutu =
9661 lookup_dwo_in_dwp (dwp_file, dwp_htab, comp_dir,
9662 signature, is_debug_types);
9663
9664 if (dwo_cutu != NULL)
9665 {
9666 if (dwarf2_read_debug)
9667 {
9668 fprintf_unfiltered (gdb_stdlog,
9669 "Virtual DWO %s %s found: @%s\n",
9670 kind, hex_string (signature),
9671 host_address_to_string (dwo_cutu));
9672 }
9673 return dwo_cutu;
9674 }
9675 }
9676 }
9677 else
9678 {
9679 /* No DWP file, look for the DWO file. */
9680
9681 dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
9682 if (*dwo_file_slot == NULL)
9683 {
9684 /* Read in the file and build a table of the CUs/TUs it contains. */
9685 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
9686 }
9687 /* NOTE: This will be NULL if unable to open the file. */
9688 dwo_file = *dwo_file_slot;
9689
9690 if (dwo_file != NULL)
9691 {
9692 struct dwo_unit *dwo_cutu = NULL;
9693
9694 if (is_debug_types && dwo_file->tus)
9695 {
9696 struct dwo_unit find_dwo_cutu;
9697
9698 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
9699 find_dwo_cutu.signature = signature;
9700 dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
9701 }
9702 else if (!is_debug_types && dwo_file->cu)
9703 {
9704 if (signature == dwo_file->cu->signature)
9705 dwo_cutu = dwo_file->cu;
9706 }
9707
9708 if (dwo_cutu != NULL)
9709 {
9710 if (dwarf2_read_debug)
9711 {
9712 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
9713 kind, dwo_name, hex_string (signature),
9714 host_address_to_string (dwo_cutu));
9715 }
9716 return dwo_cutu;
9717 }
9718 }
9719 }
9720
9721 /* We didn't find it. This could mean a dwo_id mismatch, or
9722 someone deleted the DWO/DWP file, or the search path isn't set up
9723 correctly to find the file. */
9724
9725 if (dwarf2_read_debug)
9726 {
9727 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
9728 kind, dwo_name, hex_string (signature));
9729 }
9730
9731 complaint (&symfile_complaints,
9732 _("Could not find DWO %s %s(%s) referenced by %s at offset 0x%x"
9733 " [in module %s]"),
9734 kind, dwo_name, hex_string (signature),
9735 this_unit->is_debug_types ? "TU" : "CU",
9736 this_unit->offset.sect_off, objfile->name);
9737 return NULL;
9738 }
9739
9740 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
9741 See lookup_dwo_cutu_unit for details. */
9742
9743 static struct dwo_unit *
9744 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
9745 const char *dwo_name, const char *comp_dir,
9746 ULONGEST signature)
9747 {
9748 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
9749 }
9750
9751 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
9752 See lookup_dwo_cutu_unit for details. */
9753
9754 static struct dwo_unit *
9755 lookup_dwo_type_unit (struct signatured_type *this_tu,
9756 const char *dwo_name, const char *comp_dir)
9757 {
9758 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
9759 }
9760
9761 /* Free all resources associated with DWO_FILE.
9762 Close the DWO file and munmap the sections.
9763 All memory should be on the objfile obstack. */
9764
9765 static void
9766 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
9767 {
9768 int ix;
9769 struct dwarf2_section_info *section;
9770
9771 /* Note: dbfd is NULL for virtual DWO files. */
9772 gdb_bfd_unref (dwo_file->dbfd);
9773
9774 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
9775 }
9776
9777 /* Wrapper for free_dwo_file for use in cleanups. */
9778
9779 static void
9780 free_dwo_file_cleanup (void *arg)
9781 {
9782 struct dwo_file *dwo_file = (struct dwo_file *) arg;
9783 struct objfile *objfile = dwarf2_per_objfile->objfile;
9784
9785 free_dwo_file (dwo_file, objfile);
9786 }
9787
9788 /* Traversal function for free_dwo_files. */
9789
9790 static int
9791 free_dwo_file_from_slot (void **slot, void *info)
9792 {
9793 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
9794 struct objfile *objfile = (struct objfile *) info;
9795
9796 free_dwo_file (dwo_file, objfile);
9797
9798 return 1;
9799 }
9800
9801 /* Free all resources associated with DWO_FILES. */
9802
9803 static void
9804 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
9805 {
9806 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
9807 }
9808 \f
9809 /* Read in various DIEs. */
9810
9811 /* qsort helper for inherit_abstract_dies. */
9812
9813 static int
9814 unsigned_int_compar (const void *ap, const void *bp)
9815 {
9816 unsigned int a = *(unsigned int *) ap;
9817 unsigned int b = *(unsigned int *) bp;
9818
9819 return (a > b) - (b > a);
9820 }
9821
9822 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
9823 Inherit only the children of the DW_AT_abstract_origin DIE not being
9824 already referenced by DW_AT_abstract_origin from the children of the
9825 current DIE. */
9826
9827 static void
9828 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
9829 {
9830 struct die_info *child_die;
9831 unsigned die_children_count;
9832 /* CU offsets which were referenced by children of the current DIE. */
9833 sect_offset *offsets;
9834 sect_offset *offsets_end, *offsetp;
9835 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
9836 struct die_info *origin_die;
9837 /* Iterator of the ORIGIN_DIE children. */
9838 struct die_info *origin_child_die;
9839 struct cleanup *cleanups;
9840 struct attribute *attr;
9841 struct dwarf2_cu *origin_cu;
9842 struct pending **origin_previous_list_in_scope;
9843
9844 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
9845 if (!attr)
9846 return;
9847
9848 /* Note that following die references may follow to a die in a
9849 different cu. */
9850
9851 origin_cu = cu;
9852 origin_die = follow_die_ref (die, attr, &origin_cu);
9853
9854 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
9855 symbols in. */
9856 origin_previous_list_in_scope = origin_cu->list_in_scope;
9857 origin_cu->list_in_scope = cu->list_in_scope;
9858
9859 if (die->tag != origin_die->tag
9860 && !(die->tag == DW_TAG_inlined_subroutine
9861 && origin_die->tag == DW_TAG_subprogram))
9862 complaint (&symfile_complaints,
9863 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
9864 die->offset.sect_off, origin_die->offset.sect_off);
9865
9866 child_die = die->child;
9867 die_children_count = 0;
9868 while (child_die && child_die->tag)
9869 {
9870 child_die = sibling_die (child_die);
9871 die_children_count++;
9872 }
9873 offsets = xmalloc (sizeof (*offsets) * die_children_count);
9874 cleanups = make_cleanup (xfree, offsets);
9875
9876 offsets_end = offsets;
9877 child_die = die->child;
9878 while (child_die && child_die->tag)
9879 {
9880 /* For each CHILD_DIE, find the corresponding child of
9881 ORIGIN_DIE. If there is more than one layer of
9882 DW_AT_abstract_origin, follow them all; there shouldn't be,
9883 but GCC versions at least through 4.4 generate this (GCC PR
9884 40573). */
9885 struct die_info *child_origin_die = child_die;
9886 struct dwarf2_cu *child_origin_cu = cu;
9887
9888 while (1)
9889 {
9890 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
9891 child_origin_cu);
9892 if (attr == NULL)
9893 break;
9894 child_origin_die = follow_die_ref (child_origin_die, attr,
9895 &child_origin_cu);
9896 }
9897
9898 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
9899 counterpart may exist. */
9900 if (child_origin_die != child_die)
9901 {
9902 if (child_die->tag != child_origin_die->tag
9903 && !(child_die->tag == DW_TAG_inlined_subroutine
9904 && child_origin_die->tag == DW_TAG_subprogram))
9905 complaint (&symfile_complaints,
9906 _("Child DIE 0x%x and its abstract origin 0x%x have "
9907 "different tags"), child_die->offset.sect_off,
9908 child_origin_die->offset.sect_off);
9909 if (child_origin_die->parent != origin_die)
9910 complaint (&symfile_complaints,
9911 _("Child DIE 0x%x and its abstract origin 0x%x have "
9912 "different parents"), child_die->offset.sect_off,
9913 child_origin_die->offset.sect_off);
9914 else
9915 *offsets_end++ = child_origin_die->offset;
9916 }
9917 child_die = sibling_die (child_die);
9918 }
9919 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
9920 unsigned_int_compar);
9921 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
9922 if (offsetp[-1].sect_off == offsetp->sect_off)
9923 complaint (&symfile_complaints,
9924 _("Multiple children of DIE 0x%x refer "
9925 "to DIE 0x%x as their abstract origin"),
9926 die->offset.sect_off, offsetp->sect_off);
9927
9928 offsetp = offsets;
9929 origin_child_die = origin_die->child;
9930 while (origin_child_die && origin_child_die->tag)
9931 {
9932 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
9933 while (offsetp < offsets_end
9934 && offsetp->sect_off < origin_child_die->offset.sect_off)
9935 offsetp++;
9936 if (offsetp >= offsets_end
9937 || offsetp->sect_off > origin_child_die->offset.sect_off)
9938 {
9939 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
9940 process_die (origin_child_die, origin_cu);
9941 }
9942 origin_child_die = sibling_die (origin_child_die);
9943 }
9944 origin_cu->list_in_scope = origin_previous_list_in_scope;
9945
9946 do_cleanups (cleanups);
9947 }
9948
9949 static void
9950 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
9951 {
9952 struct objfile *objfile = cu->objfile;
9953 struct context_stack *new;
9954 CORE_ADDR lowpc;
9955 CORE_ADDR highpc;
9956 struct die_info *child_die;
9957 struct attribute *attr, *call_line, *call_file;
9958 const char *name;
9959 CORE_ADDR baseaddr;
9960 struct block *block;
9961 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
9962 VEC (symbolp) *template_args = NULL;
9963 struct template_symbol *templ_func = NULL;
9964
9965 if (inlined_func)
9966 {
9967 /* If we do not have call site information, we can't show the
9968 caller of this inlined function. That's too confusing, so
9969 only use the scope for local variables. */
9970 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
9971 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
9972 if (call_line == NULL || call_file == NULL)
9973 {
9974 read_lexical_block_scope (die, cu);
9975 return;
9976 }
9977 }
9978
9979 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
9980
9981 name = dwarf2_name (die, cu);
9982
9983 /* Ignore functions with missing or empty names. These are actually
9984 illegal according to the DWARF standard. */
9985 if (name == NULL)
9986 {
9987 complaint (&symfile_complaints,
9988 _("missing name for subprogram DIE at %d"),
9989 die->offset.sect_off);
9990 return;
9991 }
9992
9993 /* Ignore functions with missing or invalid low and high pc attributes. */
9994 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
9995 {
9996 attr = dwarf2_attr (die, DW_AT_external, cu);
9997 if (!attr || !DW_UNSND (attr))
9998 complaint (&symfile_complaints,
9999 _("cannot get low and high bounds "
10000 "for subprogram DIE at %d"),
10001 die->offset.sect_off);
10002 return;
10003 }
10004
10005 lowpc += baseaddr;
10006 highpc += baseaddr;
10007
10008 /* If we have any template arguments, then we must allocate a
10009 different sort of symbol. */
10010 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
10011 {
10012 if (child_die->tag == DW_TAG_template_type_param
10013 || child_die->tag == DW_TAG_template_value_param)
10014 {
10015 templ_func = allocate_template_symbol (objfile);
10016 templ_func->base.is_cplus_template_function = 1;
10017 break;
10018 }
10019 }
10020
10021 new = push_context (0, lowpc);
10022 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
10023 (struct symbol *) templ_func);
10024
10025 /* If there is a location expression for DW_AT_frame_base, record
10026 it. */
10027 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
10028 if (attr)
10029 dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
10030
10031 cu->list_in_scope = &local_symbols;
10032
10033 if (die->child != NULL)
10034 {
10035 child_die = die->child;
10036 while (child_die && child_die->tag)
10037 {
10038 if (child_die->tag == DW_TAG_template_type_param
10039 || child_die->tag == DW_TAG_template_value_param)
10040 {
10041 struct symbol *arg = new_symbol (child_die, NULL, cu);
10042
10043 if (arg != NULL)
10044 VEC_safe_push (symbolp, template_args, arg);
10045 }
10046 else
10047 process_die (child_die, cu);
10048 child_die = sibling_die (child_die);
10049 }
10050 }
10051
10052 inherit_abstract_dies (die, cu);
10053
10054 /* If we have a DW_AT_specification, we might need to import using
10055 directives from the context of the specification DIE. See the
10056 comment in determine_prefix. */
10057 if (cu->language == language_cplus
10058 && dwarf2_attr (die, DW_AT_specification, cu))
10059 {
10060 struct dwarf2_cu *spec_cu = cu;
10061 struct die_info *spec_die = die_specification (die, &spec_cu);
10062
10063 while (spec_die)
10064 {
10065 child_die = spec_die->child;
10066 while (child_die && child_die->tag)
10067 {
10068 if (child_die->tag == DW_TAG_imported_module)
10069 process_die (child_die, spec_cu);
10070 child_die = sibling_die (child_die);
10071 }
10072
10073 /* In some cases, GCC generates specification DIEs that
10074 themselves contain DW_AT_specification attributes. */
10075 spec_die = die_specification (spec_die, &spec_cu);
10076 }
10077 }
10078
10079 new = pop_context ();
10080 /* Make a block for the local symbols within. */
10081 block = finish_block (new->name, &local_symbols, new->old_blocks,
10082 lowpc, highpc, objfile);
10083
10084 /* For C++, set the block's scope. */
10085 if ((cu->language == language_cplus || cu->language == language_fortran)
10086 && cu->processing_has_namespace_info)
10087 block_set_scope (block, determine_prefix (die, cu),
10088 &objfile->objfile_obstack);
10089
10090 /* If we have address ranges, record them. */
10091 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10092
10093 /* Attach template arguments to function. */
10094 if (! VEC_empty (symbolp, template_args))
10095 {
10096 gdb_assert (templ_func != NULL);
10097
10098 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
10099 templ_func->template_arguments
10100 = obstack_alloc (&objfile->objfile_obstack,
10101 (templ_func->n_template_arguments
10102 * sizeof (struct symbol *)));
10103 memcpy (templ_func->template_arguments,
10104 VEC_address (symbolp, template_args),
10105 (templ_func->n_template_arguments * sizeof (struct symbol *)));
10106 VEC_free (symbolp, template_args);
10107 }
10108
10109 /* In C++, we can have functions nested inside functions (e.g., when
10110 a function declares a class that has methods). This means that
10111 when we finish processing a function scope, we may need to go
10112 back to building a containing block's symbol lists. */
10113 local_symbols = new->locals;
10114 using_directives = new->using_directives;
10115
10116 /* If we've finished processing a top-level function, subsequent
10117 symbols go in the file symbol list. */
10118 if (outermost_context_p ())
10119 cu->list_in_scope = &file_symbols;
10120 }
10121
10122 /* Process all the DIES contained within a lexical block scope. Start
10123 a new scope, process the dies, and then close the scope. */
10124
10125 static void
10126 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
10127 {
10128 struct objfile *objfile = cu->objfile;
10129 struct context_stack *new;
10130 CORE_ADDR lowpc, highpc;
10131 struct die_info *child_die;
10132 CORE_ADDR baseaddr;
10133
10134 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10135
10136 /* Ignore blocks with missing or invalid low and high pc attributes. */
10137 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
10138 as multiple lexical blocks? Handling children in a sane way would
10139 be nasty. Might be easier to properly extend generic blocks to
10140 describe ranges. */
10141 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10142 return;
10143 lowpc += baseaddr;
10144 highpc += baseaddr;
10145
10146 push_context (0, lowpc);
10147 if (die->child != NULL)
10148 {
10149 child_die = die->child;
10150 while (child_die && child_die->tag)
10151 {
10152 process_die (child_die, cu);
10153 child_die = sibling_die (child_die);
10154 }
10155 }
10156 new = pop_context ();
10157
10158 if (local_symbols != NULL || using_directives != NULL)
10159 {
10160 struct block *block
10161 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
10162 highpc, objfile);
10163
10164 /* Note that recording ranges after traversing children, as we
10165 do here, means that recording a parent's ranges entails
10166 walking across all its children's ranges as they appear in
10167 the address map, which is quadratic behavior.
10168
10169 It would be nicer to record the parent's ranges before
10170 traversing its children, simply overriding whatever you find
10171 there. But since we don't even decide whether to create a
10172 block until after we've traversed its children, that's hard
10173 to do. */
10174 dwarf2_record_block_ranges (die, block, baseaddr, cu);
10175 }
10176 local_symbols = new->locals;
10177 using_directives = new->using_directives;
10178 }
10179
10180 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
10181
10182 static void
10183 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
10184 {
10185 struct objfile *objfile = cu->objfile;
10186 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10187 CORE_ADDR pc, baseaddr;
10188 struct attribute *attr;
10189 struct call_site *call_site, call_site_local;
10190 void **slot;
10191 int nparams;
10192 struct die_info *child_die;
10193
10194 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10195
10196 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10197 if (!attr)
10198 {
10199 complaint (&symfile_complaints,
10200 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
10201 "DIE 0x%x [in module %s]"),
10202 die->offset.sect_off, objfile->name);
10203 return;
10204 }
10205 pc = DW_ADDR (attr) + baseaddr;
10206
10207 if (cu->call_site_htab == NULL)
10208 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
10209 NULL, &objfile->objfile_obstack,
10210 hashtab_obstack_allocate, NULL);
10211 call_site_local.pc = pc;
10212 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
10213 if (*slot != NULL)
10214 {
10215 complaint (&symfile_complaints,
10216 _("Duplicate PC %s for DW_TAG_GNU_call_site "
10217 "DIE 0x%x [in module %s]"),
10218 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
10219 return;
10220 }
10221
10222 /* Count parameters at the caller. */
10223
10224 nparams = 0;
10225 for (child_die = die->child; child_die && child_die->tag;
10226 child_die = sibling_die (child_die))
10227 {
10228 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10229 {
10230 complaint (&symfile_complaints,
10231 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
10232 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10233 child_die->tag, child_die->offset.sect_off, objfile->name);
10234 continue;
10235 }
10236
10237 nparams++;
10238 }
10239
10240 call_site = obstack_alloc (&objfile->objfile_obstack,
10241 (sizeof (*call_site)
10242 + (sizeof (*call_site->parameter)
10243 * (nparams - 1))));
10244 *slot = call_site;
10245 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
10246 call_site->pc = pc;
10247
10248 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
10249 {
10250 struct die_info *func_die;
10251
10252 /* Skip also over DW_TAG_inlined_subroutine. */
10253 for (func_die = die->parent;
10254 func_die && func_die->tag != DW_TAG_subprogram
10255 && func_die->tag != DW_TAG_subroutine_type;
10256 func_die = func_die->parent);
10257
10258 /* DW_AT_GNU_all_call_sites is a superset
10259 of DW_AT_GNU_all_tail_call_sites. */
10260 if (func_die
10261 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
10262 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
10263 {
10264 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
10265 not complete. But keep CALL_SITE for look ups via call_site_htab,
10266 both the initial caller containing the real return address PC and
10267 the final callee containing the current PC of a chain of tail
10268 calls do not need to have the tail call list complete. But any
10269 function candidate for a virtual tail call frame searched via
10270 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
10271 determined unambiguously. */
10272 }
10273 else
10274 {
10275 struct type *func_type = NULL;
10276
10277 if (func_die)
10278 func_type = get_die_type (func_die, cu);
10279 if (func_type != NULL)
10280 {
10281 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
10282
10283 /* Enlist this call site to the function. */
10284 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
10285 TYPE_TAIL_CALL_LIST (func_type) = call_site;
10286 }
10287 else
10288 complaint (&symfile_complaints,
10289 _("Cannot find function owning DW_TAG_GNU_call_site "
10290 "DIE 0x%x [in module %s]"),
10291 die->offset.sect_off, objfile->name);
10292 }
10293 }
10294
10295 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
10296 if (attr == NULL)
10297 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10298 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
10299 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
10300 /* Keep NULL DWARF_BLOCK. */;
10301 else if (attr_form_is_block (attr))
10302 {
10303 struct dwarf2_locexpr_baton *dlbaton;
10304
10305 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
10306 dlbaton->data = DW_BLOCK (attr)->data;
10307 dlbaton->size = DW_BLOCK (attr)->size;
10308 dlbaton->per_cu = cu->per_cu;
10309
10310 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
10311 }
10312 else if (is_ref_attr (attr))
10313 {
10314 struct dwarf2_cu *target_cu = cu;
10315 struct die_info *target_die;
10316
10317 target_die = follow_die_ref (die, attr, &target_cu);
10318 gdb_assert (target_cu->objfile == objfile);
10319 if (die_is_declaration (target_die, target_cu))
10320 {
10321 const char *target_physname = NULL;
10322 struct attribute *target_attr;
10323
10324 /* Prefer the mangled name; otherwise compute the demangled one. */
10325 target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
10326 if (target_attr == NULL)
10327 target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
10328 target_cu);
10329 if (target_attr != NULL && DW_STRING (target_attr) != NULL)
10330 target_physname = DW_STRING (target_attr);
10331 else
10332 target_physname = dwarf2_physname (NULL, target_die, target_cu);
10333 if (target_physname == NULL)
10334 complaint (&symfile_complaints,
10335 _("DW_AT_GNU_call_site_target target DIE has invalid "
10336 "physname, for referencing DIE 0x%x [in module %s]"),
10337 die->offset.sect_off, objfile->name);
10338 else
10339 SET_FIELD_PHYSNAME (call_site->target, target_physname);
10340 }
10341 else
10342 {
10343 CORE_ADDR lowpc;
10344
10345 /* DW_AT_entry_pc should be preferred. */
10346 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
10347 complaint (&symfile_complaints,
10348 _("DW_AT_GNU_call_site_target target DIE has invalid "
10349 "low pc, for referencing DIE 0x%x [in module %s]"),
10350 die->offset.sect_off, objfile->name);
10351 else
10352 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
10353 }
10354 }
10355 else
10356 complaint (&symfile_complaints,
10357 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
10358 "block nor reference, for DIE 0x%x [in module %s]"),
10359 die->offset.sect_off, objfile->name);
10360
10361 call_site->per_cu = cu->per_cu;
10362
10363 for (child_die = die->child;
10364 child_die && child_die->tag;
10365 child_die = sibling_die (child_die))
10366 {
10367 struct call_site_parameter *parameter;
10368 struct attribute *loc, *origin;
10369
10370 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
10371 {
10372 /* Already printed the complaint above. */
10373 continue;
10374 }
10375
10376 gdb_assert (call_site->parameter_count < nparams);
10377 parameter = &call_site->parameter[call_site->parameter_count];
10378
10379 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
10380 specifies DW_TAG_formal_parameter. Value of the data assumed for the
10381 register is contained in DW_AT_GNU_call_site_value. */
10382
10383 loc = dwarf2_attr (child_die, DW_AT_location, cu);
10384 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
10385 if (loc == NULL && origin != NULL && is_ref_attr (origin))
10386 {
10387 sect_offset offset;
10388
10389 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
10390 offset = dwarf2_get_ref_die_offset (origin);
10391 if (!offset_in_cu_p (&cu->header, offset))
10392 {
10393 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
10394 binding can be done only inside one CU. Such referenced DIE
10395 therefore cannot be even moved to DW_TAG_partial_unit. */
10396 complaint (&symfile_complaints,
10397 _("DW_AT_abstract_origin offset is not in CU for "
10398 "DW_TAG_GNU_call_site child DIE 0x%x "
10399 "[in module %s]"),
10400 child_die->offset.sect_off, objfile->name);
10401 continue;
10402 }
10403 parameter->u.param_offset.cu_off = (offset.sect_off
10404 - cu->header.offset.sect_off);
10405 }
10406 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
10407 {
10408 complaint (&symfile_complaints,
10409 _("No DW_FORM_block* DW_AT_location for "
10410 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10411 child_die->offset.sect_off, objfile->name);
10412 continue;
10413 }
10414 else
10415 {
10416 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
10417 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
10418 if (parameter->u.dwarf_reg != -1)
10419 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
10420 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
10421 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
10422 &parameter->u.fb_offset))
10423 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
10424 else
10425 {
10426 complaint (&symfile_complaints,
10427 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
10428 "for DW_FORM_block* DW_AT_location is supported for "
10429 "DW_TAG_GNU_call_site child DIE 0x%x "
10430 "[in module %s]"),
10431 child_die->offset.sect_off, objfile->name);
10432 continue;
10433 }
10434 }
10435
10436 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
10437 if (!attr_form_is_block (attr))
10438 {
10439 complaint (&symfile_complaints,
10440 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
10441 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10442 child_die->offset.sect_off, objfile->name);
10443 continue;
10444 }
10445 parameter->value = DW_BLOCK (attr)->data;
10446 parameter->value_size = DW_BLOCK (attr)->size;
10447
10448 /* Parameters are not pre-cleared by memset above. */
10449 parameter->data_value = NULL;
10450 parameter->data_value_size = 0;
10451 call_site->parameter_count++;
10452
10453 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
10454 if (attr)
10455 {
10456 if (!attr_form_is_block (attr))
10457 complaint (&symfile_complaints,
10458 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
10459 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
10460 child_die->offset.sect_off, objfile->name);
10461 else
10462 {
10463 parameter->data_value = DW_BLOCK (attr)->data;
10464 parameter->data_value_size = DW_BLOCK (attr)->size;
10465 }
10466 }
10467 }
10468 }
10469
10470 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
10471 Return 1 if the attributes are present and valid, otherwise, return 0.
10472 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
10473
10474 static int
10475 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
10476 CORE_ADDR *high_return, struct dwarf2_cu *cu,
10477 struct partial_symtab *ranges_pst)
10478 {
10479 struct objfile *objfile = cu->objfile;
10480 struct comp_unit_head *cu_header = &cu->header;
10481 bfd *obfd = objfile->obfd;
10482 unsigned int addr_size = cu_header->addr_size;
10483 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10484 /* Base address selection entry. */
10485 CORE_ADDR base;
10486 int found_base;
10487 unsigned int dummy;
10488 const gdb_byte *buffer;
10489 CORE_ADDR marker;
10490 int low_set;
10491 CORE_ADDR low = 0;
10492 CORE_ADDR high = 0;
10493 CORE_ADDR baseaddr;
10494
10495 found_base = cu->base_known;
10496 base = cu->base_address;
10497
10498 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10499 if (offset >= dwarf2_per_objfile->ranges.size)
10500 {
10501 complaint (&symfile_complaints,
10502 _("Offset %d out of bounds for DW_AT_ranges attribute"),
10503 offset);
10504 return 0;
10505 }
10506 buffer = dwarf2_per_objfile->ranges.buffer + offset;
10507
10508 /* Read in the largest possible address. */
10509 marker = read_address (obfd, buffer, cu, &dummy);
10510 if ((marker & mask) == mask)
10511 {
10512 /* If we found the largest possible address, then
10513 read the base address. */
10514 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10515 buffer += 2 * addr_size;
10516 offset += 2 * addr_size;
10517 found_base = 1;
10518 }
10519
10520 low_set = 0;
10521
10522 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10523
10524 while (1)
10525 {
10526 CORE_ADDR range_beginning, range_end;
10527
10528 range_beginning = read_address (obfd, buffer, cu, &dummy);
10529 buffer += addr_size;
10530 range_end = read_address (obfd, buffer, cu, &dummy);
10531 buffer += addr_size;
10532 offset += 2 * addr_size;
10533
10534 /* An end of list marker is a pair of zero addresses. */
10535 if (range_beginning == 0 && range_end == 0)
10536 /* Found the end of list entry. */
10537 break;
10538
10539 /* Each base address selection entry is a pair of 2 values.
10540 The first is the largest possible address, the second is
10541 the base address. Check for a base address here. */
10542 if ((range_beginning & mask) == mask)
10543 {
10544 /* If we found the largest possible address, then
10545 read the base address. */
10546 base = read_address (obfd, buffer + addr_size, cu, &dummy);
10547 found_base = 1;
10548 continue;
10549 }
10550
10551 if (!found_base)
10552 {
10553 /* We have no valid base address for the ranges
10554 data. */
10555 complaint (&symfile_complaints,
10556 _("Invalid .debug_ranges data (no base address)"));
10557 return 0;
10558 }
10559
10560 if (range_beginning > range_end)
10561 {
10562 /* Inverted range entries are invalid. */
10563 complaint (&symfile_complaints,
10564 _("Invalid .debug_ranges data (inverted range)"));
10565 return 0;
10566 }
10567
10568 /* Empty range entries have no effect. */
10569 if (range_beginning == range_end)
10570 continue;
10571
10572 range_beginning += base;
10573 range_end += base;
10574
10575 /* A not-uncommon case of bad debug info.
10576 Don't pollute the addrmap with bad data. */
10577 if (range_beginning + baseaddr == 0
10578 && !dwarf2_per_objfile->has_section_at_zero)
10579 {
10580 complaint (&symfile_complaints,
10581 _(".debug_ranges entry has start address of zero"
10582 " [in module %s]"), objfile->name);
10583 continue;
10584 }
10585
10586 if (ranges_pst != NULL)
10587 addrmap_set_empty (objfile->psymtabs_addrmap,
10588 range_beginning + baseaddr,
10589 range_end - 1 + baseaddr,
10590 ranges_pst);
10591
10592 /* FIXME: This is recording everything as a low-high
10593 segment of consecutive addresses. We should have a
10594 data structure for discontiguous block ranges
10595 instead. */
10596 if (! low_set)
10597 {
10598 low = range_beginning;
10599 high = range_end;
10600 low_set = 1;
10601 }
10602 else
10603 {
10604 if (range_beginning < low)
10605 low = range_beginning;
10606 if (range_end > high)
10607 high = range_end;
10608 }
10609 }
10610
10611 if (! low_set)
10612 /* If the first entry is an end-of-list marker, the range
10613 describes an empty scope, i.e. no instructions. */
10614 return 0;
10615
10616 if (low_return)
10617 *low_return = low;
10618 if (high_return)
10619 *high_return = high;
10620 return 1;
10621 }
10622
10623 /* Get low and high pc attributes from a die. Return 1 if the attributes
10624 are present and valid, otherwise, return 0. Return -1 if the range is
10625 discontinuous, i.e. derived from DW_AT_ranges information. */
10626
10627 static int
10628 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
10629 CORE_ADDR *highpc, struct dwarf2_cu *cu,
10630 struct partial_symtab *pst)
10631 {
10632 struct attribute *attr;
10633 struct attribute *attr_high;
10634 CORE_ADDR low = 0;
10635 CORE_ADDR high = 0;
10636 int ret = 0;
10637
10638 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10639 if (attr_high)
10640 {
10641 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10642 if (attr)
10643 {
10644 low = DW_ADDR (attr);
10645 if (attr_high->form == DW_FORM_addr
10646 || attr_high->form == DW_FORM_GNU_addr_index)
10647 high = DW_ADDR (attr_high);
10648 else
10649 high = low + DW_UNSND (attr_high);
10650 }
10651 else
10652 /* Found high w/o low attribute. */
10653 return 0;
10654
10655 /* Found consecutive range of addresses. */
10656 ret = 1;
10657 }
10658 else
10659 {
10660 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10661 if (attr != NULL)
10662 {
10663 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10664 We take advantage of the fact that DW_AT_ranges does not appear
10665 in DW_TAG_compile_unit of DWO files. */
10666 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10667 unsigned int ranges_offset = (DW_UNSND (attr)
10668 + (need_ranges_base
10669 ? cu->ranges_base
10670 : 0));
10671
10672 /* Value of the DW_AT_ranges attribute is the offset in the
10673 .debug_ranges section. */
10674 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
10675 return 0;
10676 /* Found discontinuous range of addresses. */
10677 ret = -1;
10678 }
10679 }
10680
10681 /* read_partial_die has also the strict LOW < HIGH requirement. */
10682 if (high <= low)
10683 return 0;
10684
10685 /* When using the GNU linker, .gnu.linkonce. sections are used to
10686 eliminate duplicate copies of functions and vtables and such.
10687 The linker will arbitrarily choose one and discard the others.
10688 The AT_*_pc values for such functions refer to local labels in
10689 these sections. If the section from that file was discarded, the
10690 labels are not in the output, so the relocs get a value of 0.
10691 If this is a discarded function, mark the pc bounds as invalid,
10692 so that GDB will ignore it. */
10693 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
10694 return 0;
10695
10696 *lowpc = low;
10697 if (highpc)
10698 *highpc = high;
10699 return ret;
10700 }
10701
10702 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
10703 its low and high PC addresses. Do nothing if these addresses could not
10704 be determined. Otherwise, set LOWPC to the low address if it is smaller,
10705 and HIGHPC to the high address if greater than HIGHPC. */
10706
10707 static void
10708 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
10709 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10710 struct dwarf2_cu *cu)
10711 {
10712 CORE_ADDR low, high;
10713 struct die_info *child = die->child;
10714
10715 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
10716 {
10717 *lowpc = min (*lowpc, low);
10718 *highpc = max (*highpc, high);
10719 }
10720
10721 /* If the language does not allow nested subprograms (either inside
10722 subprograms or lexical blocks), we're done. */
10723 if (cu->language != language_ada)
10724 return;
10725
10726 /* Check all the children of the given DIE. If it contains nested
10727 subprograms, then check their pc bounds. Likewise, we need to
10728 check lexical blocks as well, as they may also contain subprogram
10729 definitions. */
10730 while (child && child->tag)
10731 {
10732 if (child->tag == DW_TAG_subprogram
10733 || child->tag == DW_TAG_lexical_block)
10734 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
10735 child = sibling_die (child);
10736 }
10737 }
10738
10739 /* Get the low and high pc's represented by the scope DIE, and store
10740 them in *LOWPC and *HIGHPC. If the correct values can't be
10741 determined, set *LOWPC to -1 and *HIGHPC to 0. */
10742
10743 static void
10744 get_scope_pc_bounds (struct die_info *die,
10745 CORE_ADDR *lowpc, CORE_ADDR *highpc,
10746 struct dwarf2_cu *cu)
10747 {
10748 CORE_ADDR best_low = (CORE_ADDR) -1;
10749 CORE_ADDR best_high = (CORE_ADDR) 0;
10750 CORE_ADDR current_low, current_high;
10751
10752 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
10753 {
10754 best_low = current_low;
10755 best_high = current_high;
10756 }
10757 else
10758 {
10759 struct die_info *child = die->child;
10760
10761 while (child && child->tag)
10762 {
10763 switch (child->tag) {
10764 case DW_TAG_subprogram:
10765 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
10766 break;
10767 case DW_TAG_namespace:
10768 case DW_TAG_module:
10769 /* FIXME: carlton/2004-01-16: Should we do this for
10770 DW_TAG_class_type/DW_TAG_structure_type, too? I think
10771 that current GCC's always emit the DIEs corresponding
10772 to definitions of methods of classes as children of a
10773 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
10774 the DIEs giving the declarations, which could be
10775 anywhere). But I don't see any reason why the
10776 standards says that they have to be there. */
10777 get_scope_pc_bounds (child, &current_low, &current_high, cu);
10778
10779 if (current_low != ((CORE_ADDR) -1))
10780 {
10781 best_low = min (best_low, current_low);
10782 best_high = max (best_high, current_high);
10783 }
10784 break;
10785 default:
10786 /* Ignore. */
10787 break;
10788 }
10789
10790 child = sibling_die (child);
10791 }
10792 }
10793
10794 *lowpc = best_low;
10795 *highpc = best_high;
10796 }
10797
10798 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
10799 in DIE. */
10800
10801 static void
10802 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
10803 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
10804 {
10805 struct objfile *objfile = cu->objfile;
10806 struct attribute *attr;
10807 struct attribute *attr_high;
10808
10809 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
10810 if (attr_high)
10811 {
10812 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10813 if (attr)
10814 {
10815 CORE_ADDR low = DW_ADDR (attr);
10816 CORE_ADDR high;
10817 if (attr_high->form == DW_FORM_addr
10818 || attr_high->form == DW_FORM_GNU_addr_index)
10819 high = DW_ADDR (attr_high);
10820 else
10821 high = low + DW_UNSND (attr_high);
10822
10823 record_block_range (block, baseaddr + low, baseaddr + high - 1);
10824 }
10825 }
10826
10827 attr = dwarf2_attr (die, DW_AT_ranges, cu);
10828 if (attr)
10829 {
10830 bfd *obfd = objfile->obfd;
10831 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
10832 We take advantage of the fact that DW_AT_ranges does not appear
10833 in DW_TAG_compile_unit of DWO files. */
10834 int need_ranges_base = die->tag != DW_TAG_compile_unit;
10835
10836 /* The value of the DW_AT_ranges attribute is the offset of the
10837 address range list in the .debug_ranges section. */
10838 unsigned long offset = (DW_UNSND (attr)
10839 + (need_ranges_base ? cu->ranges_base : 0));
10840 const gdb_byte *buffer;
10841
10842 /* For some target architectures, but not others, the
10843 read_address function sign-extends the addresses it returns.
10844 To recognize base address selection entries, we need a
10845 mask. */
10846 unsigned int addr_size = cu->header.addr_size;
10847 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
10848
10849 /* The base address, to which the next pair is relative. Note
10850 that this 'base' is a DWARF concept: most entries in a range
10851 list are relative, to reduce the number of relocs against the
10852 debugging information. This is separate from this function's
10853 'baseaddr' argument, which GDB uses to relocate debugging
10854 information from a shared library based on the address at
10855 which the library was loaded. */
10856 CORE_ADDR base = cu->base_address;
10857 int base_known = cu->base_known;
10858
10859 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
10860 if (offset >= dwarf2_per_objfile->ranges.size)
10861 {
10862 complaint (&symfile_complaints,
10863 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
10864 offset);
10865 return;
10866 }
10867 buffer = dwarf2_per_objfile->ranges.buffer + offset;
10868
10869 for (;;)
10870 {
10871 unsigned int bytes_read;
10872 CORE_ADDR start, end;
10873
10874 start = read_address (obfd, buffer, cu, &bytes_read);
10875 buffer += bytes_read;
10876 end = read_address (obfd, buffer, cu, &bytes_read);
10877 buffer += bytes_read;
10878
10879 /* Did we find the end of the range list? */
10880 if (start == 0 && end == 0)
10881 break;
10882
10883 /* Did we find a base address selection entry? */
10884 else if ((start & base_select_mask) == base_select_mask)
10885 {
10886 base = end;
10887 base_known = 1;
10888 }
10889
10890 /* We found an ordinary address range. */
10891 else
10892 {
10893 if (!base_known)
10894 {
10895 complaint (&symfile_complaints,
10896 _("Invalid .debug_ranges data "
10897 "(no base address)"));
10898 return;
10899 }
10900
10901 if (start > end)
10902 {
10903 /* Inverted range entries are invalid. */
10904 complaint (&symfile_complaints,
10905 _("Invalid .debug_ranges data "
10906 "(inverted range)"));
10907 return;
10908 }
10909
10910 /* Empty range entries have no effect. */
10911 if (start == end)
10912 continue;
10913
10914 start += base + baseaddr;
10915 end += base + baseaddr;
10916
10917 /* A not-uncommon case of bad debug info.
10918 Don't pollute the addrmap with bad data. */
10919 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
10920 {
10921 complaint (&symfile_complaints,
10922 _(".debug_ranges entry has start address of zero"
10923 " [in module %s]"), objfile->name);
10924 continue;
10925 }
10926
10927 record_block_range (block, start, end - 1);
10928 }
10929 }
10930 }
10931 }
10932
10933 /* Check whether the producer field indicates either of GCC < 4.6, or the
10934 Intel C/C++ compiler, and cache the result in CU. */
10935
10936 static void
10937 check_producer (struct dwarf2_cu *cu)
10938 {
10939 const char *cs;
10940 int major, minor, release;
10941
10942 if (cu->producer == NULL)
10943 {
10944 /* For unknown compilers expect their behavior is DWARF version
10945 compliant.
10946
10947 GCC started to support .debug_types sections by -gdwarf-4 since
10948 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
10949 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
10950 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
10951 interpreted incorrectly by GDB now - GCC PR debug/48229. */
10952 }
10953 else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
10954 {
10955 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
10956
10957 cs = &cu->producer[strlen ("GNU ")];
10958 while (*cs && !isdigit (*cs))
10959 cs++;
10960 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
10961 {
10962 /* Not recognized as GCC. */
10963 }
10964 else
10965 {
10966 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
10967 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
10968 }
10969 }
10970 else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
10971 cu->producer_is_icc = 1;
10972 else
10973 {
10974 /* For other non-GCC compilers, expect their behavior is DWARF version
10975 compliant. */
10976 }
10977
10978 cu->checked_producer = 1;
10979 }
10980
10981 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
10982 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
10983 during 4.6.0 experimental. */
10984
10985 static int
10986 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
10987 {
10988 if (!cu->checked_producer)
10989 check_producer (cu);
10990
10991 return cu->producer_is_gxx_lt_4_6;
10992 }
10993
10994 /* Return the default accessibility type if it is not overriden by
10995 DW_AT_accessibility. */
10996
10997 static enum dwarf_access_attribute
10998 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
10999 {
11000 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11001 {
11002 /* The default DWARF 2 accessibility for members is public, the default
11003 accessibility for inheritance is private. */
11004
11005 if (die->tag != DW_TAG_inheritance)
11006 return DW_ACCESS_public;
11007 else
11008 return DW_ACCESS_private;
11009 }
11010 else
11011 {
11012 /* DWARF 3+ defines the default accessibility a different way. The same
11013 rules apply now for DW_TAG_inheritance as for the members and it only
11014 depends on the container kind. */
11015
11016 if (die->parent->tag == DW_TAG_class_type)
11017 return DW_ACCESS_private;
11018 else
11019 return DW_ACCESS_public;
11020 }
11021 }
11022
11023 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
11024 offset. If the attribute was not found return 0, otherwise return
11025 1. If it was found but could not properly be handled, set *OFFSET
11026 to 0. */
11027
11028 static int
11029 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
11030 LONGEST *offset)
11031 {
11032 struct attribute *attr;
11033
11034 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11035 if (attr != NULL)
11036 {
11037 *offset = 0;
11038
11039 /* Note that we do not check for a section offset first here.
11040 This is because DW_AT_data_member_location is new in DWARF 4,
11041 so if we see it, we can assume that a constant form is really
11042 a constant and not a section offset. */
11043 if (attr_form_is_constant (attr))
11044 *offset = dwarf2_get_attr_constant_value (attr, 0);
11045 else if (attr_form_is_section_offset (attr))
11046 dwarf2_complex_location_expr_complaint ();
11047 else if (attr_form_is_block (attr))
11048 *offset = decode_locdesc (DW_BLOCK (attr), cu);
11049 else
11050 dwarf2_complex_location_expr_complaint ();
11051
11052 return 1;
11053 }
11054
11055 return 0;
11056 }
11057
11058 /* Add an aggregate field to the field list. */
11059
11060 static void
11061 dwarf2_add_field (struct field_info *fip, struct die_info *die,
11062 struct dwarf2_cu *cu)
11063 {
11064 struct objfile *objfile = cu->objfile;
11065 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11066 struct nextfield *new_field;
11067 struct attribute *attr;
11068 struct field *fp;
11069 const char *fieldname = "";
11070
11071 /* Allocate a new field list entry and link it in. */
11072 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
11073 make_cleanup (xfree, new_field);
11074 memset (new_field, 0, sizeof (struct nextfield));
11075
11076 if (die->tag == DW_TAG_inheritance)
11077 {
11078 new_field->next = fip->baseclasses;
11079 fip->baseclasses = new_field;
11080 }
11081 else
11082 {
11083 new_field->next = fip->fields;
11084 fip->fields = new_field;
11085 }
11086 fip->nfields++;
11087
11088 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11089 if (attr)
11090 new_field->accessibility = DW_UNSND (attr);
11091 else
11092 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
11093 if (new_field->accessibility != DW_ACCESS_public)
11094 fip->non_public_fields = 1;
11095
11096 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11097 if (attr)
11098 new_field->virtuality = DW_UNSND (attr);
11099 else
11100 new_field->virtuality = DW_VIRTUALITY_none;
11101
11102 fp = &new_field->field;
11103
11104 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
11105 {
11106 LONGEST offset;
11107
11108 /* Data member other than a C++ static data member. */
11109
11110 /* Get type of field. */
11111 fp->type = die_type (die, cu);
11112
11113 SET_FIELD_BITPOS (*fp, 0);
11114
11115 /* Get bit size of field (zero if none). */
11116 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
11117 if (attr)
11118 {
11119 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
11120 }
11121 else
11122 {
11123 FIELD_BITSIZE (*fp) = 0;
11124 }
11125
11126 /* Get bit offset of field. */
11127 if (handle_data_member_location (die, cu, &offset))
11128 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
11129 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
11130 if (attr)
11131 {
11132 if (gdbarch_bits_big_endian (gdbarch))
11133 {
11134 /* For big endian bits, the DW_AT_bit_offset gives the
11135 additional bit offset from the MSB of the containing
11136 anonymous object to the MSB of the field. We don't
11137 have to do anything special since we don't need to
11138 know the size of the anonymous object. */
11139 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
11140 }
11141 else
11142 {
11143 /* For little endian bits, compute the bit offset to the
11144 MSB of the anonymous object, subtract off the number of
11145 bits from the MSB of the field to the MSB of the
11146 object, and then subtract off the number of bits of
11147 the field itself. The result is the bit offset of
11148 the LSB of the field. */
11149 int anonymous_size;
11150 int bit_offset = DW_UNSND (attr);
11151
11152 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11153 if (attr)
11154 {
11155 /* The size of the anonymous object containing
11156 the bit field is explicit, so use the
11157 indicated size (in bytes). */
11158 anonymous_size = DW_UNSND (attr);
11159 }
11160 else
11161 {
11162 /* The size of the anonymous object containing
11163 the bit field must be inferred from the type
11164 attribute of the data member containing the
11165 bit field. */
11166 anonymous_size = TYPE_LENGTH (fp->type);
11167 }
11168 SET_FIELD_BITPOS (*fp,
11169 (FIELD_BITPOS (*fp)
11170 + anonymous_size * bits_per_byte
11171 - bit_offset - FIELD_BITSIZE (*fp)));
11172 }
11173 }
11174
11175 /* Get name of field. */
11176 fieldname = dwarf2_name (die, cu);
11177 if (fieldname == NULL)
11178 fieldname = "";
11179
11180 /* The name is already allocated along with this objfile, so we don't
11181 need to duplicate it for the type. */
11182 fp->name = fieldname;
11183
11184 /* Change accessibility for artificial fields (e.g. virtual table
11185 pointer or virtual base class pointer) to private. */
11186 if (dwarf2_attr (die, DW_AT_artificial, cu))
11187 {
11188 FIELD_ARTIFICIAL (*fp) = 1;
11189 new_field->accessibility = DW_ACCESS_private;
11190 fip->non_public_fields = 1;
11191 }
11192 }
11193 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
11194 {
11195 /* C++ static member. */
11196
11197 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
11198 is a declaration, but all versions of G++ as of this writing
11199 (so through at least 3.2.1) incorrectly generate
11200 DW_TAG_variable tags. */
11201
11202 const char *physname;
11203
11204 /* Get name of field. */
11205 fieldname = dwarf2_name (die, cu);
11206 if (fieldname == NULL)
11207 return;
11208
11209 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11210 if (attr
11211 /* Only create a symbol if this is an external value.
11212 new_symbol checks this and puts the value in the global symbol
11213 table, which we want. If it is not external, new_symbol
11214 will try to put the value in cu->list_in_scope which is wrong. */
11215 && dwarf2_flag_true_p (die, DW_AT_external, cu))
11216 {
11217 /* A static const member, not much different than an enum as far as
11218 we're concerned, except that we can support more types. */
11219 new_symbol (die, NULL, cu);
11220 }
11221
11222 /* Get physical name. */
11223 physname = dwarf2_physname (fieldname, die, cu);
11224
11225 /* The name is already allocated along with this objfile, so we don't
11226 need to duplicate it for the type. */
11227 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
11228 FIELD_TYPE (*fp) = die_type (die, cu);
11229 FIELD_NAME (*fp) = fieldname;
11230 }
11231 else if (die->tag == DW_TAG_inheritance)
11232 {
11233 LONGEST offset;
11234
11235 /* C++ base class field. */
11236 if (handle_data_member_location (die, cu, &offset))
11237 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
11238 FIELD_BITSIZE (*fp) = 0;
11239 FIELD_TYPE (*fp) = die_type (die, cu);
11240 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
11241 fip->nbaseclasses++;
11242 }
11243 }
11244
11245 /* Add a typedef defined in the scope of the FIP's class. */
11246
11247 static void
11248 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
11249 struct dwarf2_cu *cu)
11250 {
11251 struct objfile *objfile = cu->objfile;
11252 struct typedef_field_list *new_field;
11253 struct attribute *attr;
11254 struct typedef_field *fp;
11255 char *fieldname = "";
11256
11257 /* Allocate a new field list entry and link it in. */
11258 new_field = xzalloc (sizeof (*new_field));
11259 make_cleanup (xfree, new_field);
11260
11261 gdb_assert (die->tag == DW_TAG_typedef);
11262
11263 fp = &new_field->field;
11264
11265 /* Get name of field. */
11266 fp->name = dwarf2_name (die, cu);
11267 if (fp->name == NULL)
11268 return;
11269
11270 fp->type = read_type_die (die, cu);
11271
11272 new_field->next = fip->typedef_field_list;
11273 fip->typedef_field_list = new_field;
11274 fip->typedef_field_list_count++;
11275 }
11276
11277 /* Create the vector of fields, and attach it to the type. */
11278
11279 static void
11280 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
11281 struct dwarf2_cu *cu)
11282 {
11283 int nfields = fip->nfields;
11284
11285 /* Record the field count, allocate space for the array of fields,
11286 and create blank accessibility bitfields if necessary. */
11287 TYPE_NFIELDS (type) = nfields;
11288 TYPE_FIELDS (type) = (struct field *)
11289 TYPE_ALLOC (type, sizeof (struct field) * nfields);
11290 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
11291
11292 if (fip->non_public_fields && cu->language != language_ada)
11293 {
11294 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11295
11296 TYPE_FIELD_PRIVATE_BITS (type) =
11297 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11298 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
11299
11300 TYPE_FIELD_PROTECTED_BITS (type) =
11301 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11302 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
11303
11304 TYPE_FIELD_IGNORE_BITS (type) =
11305 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
11306 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
11307 }
11308
11309 /* If the type has baseclasses, allocate and clear a bit vector for
11310 TYPE_FIELD_VIRTUAL_BITS. */
11311 if (fip->nbaseclasses && cu->language != language_ada)
11312 {
11313 int num_bytes = B_BYTES (fip->nbaseclasses);
11314 unsigned char *pointer;
11315
11316 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11317 pointer = TYPE_ALLOC (type, num_bytes);
11318 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
11319 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
11320 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
11321 }
11322
11323 /* Copy the saved-up fields into the field vector. Start from the head of
11324 the list, adding to the tail of the field array, so that they end up in
11325 the same order in the array in which they were added to the list. */
11326 while (nfields-- > 0)
11327 {
11328 struct nextfield *fieldp;
11329
11330 if (fip->fields)
11331 {
11332 fieldp = fip->fields;
11333 fip->fields = fieldp->next;
11334 }
11335 else
11336 {
11337 fieldp = fip->baseclasses;
11338 fip->baseclasses = fieldp->next;
11339 }
11340
11341 TYPE_FIELD (type, nfields) = fieldp->field;
11342 switch (fieldp->accessibility)
11343 {
11344 case DW_ACCESS_private:
11345 if (cu->language != language_ada)
11346 SET_TYPE_FIELD_PRIVATE (type, nfields);
11347 break;
11348
11349 case DW_ACCESS_protected:
11350 if (cu->language != language_ada)
11351 SET_TYPE_FIELD_PROTECTED (type, nfields);
11352 break;
11353
11354 case DW_ACCESS_public:
11355 break;
11356
11357 default:
11358 /* Unknown accessibility. Complain and treat it as public. */
11359 {
11360 complaint (&symfile_complaints, _("unsupported accessibility %d"),
11361 fieldp->accessibility);
11362 }
11363 break;
11364 }
11365 if (nfields < fip->nbaseclasses)
11366 {
11367 switch (fieldp->virtuality)
11368 {
11369 case DW_VIRTUALITY_virtual:
11370 case DW_VIRTUALITY_pure_virtual:
11371 if (cu->language == language_ada)
11372 error (_("unexpected virtuality in component of Ada type"));
11373 SET_TYPE_FIELD_VIRTUAL (type, nfields);
11374 break;
11375 }
11376 }
11377 }
11378 }
11379
11380 /* Return true if this member function is a constructor, false
11381 otherwise. */
11382
11383 static int
11384 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
11385 {
11386 const char *fieldname;
11387 const char *typename;
11388 int len;
11389
11390 if (die->parent == NULL)
11391 return 0;
11392
11393 if (die->parent->tag != DW_TAG_structure_type
11394 && die->parent->tag != DW_TAG_union_type
11395 && die->parent->tag != DW_TAG_class_type)
11396 return 0;
11397
11398 fieldname = dwarf2_name (die, cu);
11399 typename = dwarf2_name (die->parent, cu);
11400 if (fieldname == NULL || typename == NULL)
11401 return 0;
11402
11403 len = strlen (fieldname);
11404 return (strncmp (fieldname, typename, len) == 0
11405 && (typename[len] == '\0' || typename[len] == '<'));
11406 }
11407
11408 /* Add a member function to the proper fieldlist. */
11409
11410 static void
11411 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
11412 struct type *type, struct dwarf2_cu *cu)
11413 {
11414 struct objfile *objfile = cu->objfile;
11415 struct attribute *attr;
11416 struct fnfieldlist *flp;
11417 int i;
11418 struct fn_field *fnp;
11419 const char *fieldname;
11420 struct nextfnfield *new_fnfield;
11421 struct type *this_type;
11422 enum dwarf_access_attribute accessibility;
11423
11424 if (cu->language == language_ada)
11425 error (_("unexpected member function in Ada type"));
11426
11427 /* Get name of member function. */
11428 fieldname = dwarf2_name (die, cu);
11429 if (fieldname == NULL)
11430 return;
11431
11432 /* Look up member function name in fieldlist. */
11433 for (i = 0; i < fip->nfnfields; i++)
11434 {
11435 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
11436 break;
11437 }
11438
11439 /* Create new list element if necessary. */
11440 if (i < fip->nfnfields)
11441 flp = &fip->fnfieldlists[i];
11442 else
11443 {
11444 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
11445 {
11446 fip->fnfieldlists = (struct fnfieldlist *)
11447 xrealloc (fip->fnfieldlists,
11448 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
11449 * sizeof (struct fnfieldlist));
11450 if (fip->nfnfields == 0)
11451 make_cleanup (free_current_contents, &fip->fnfieldlists);
11452 }
11453 flp = &fip->fnfieldlists[fip->nfnfields];
11454 flp->name = fieldname;
11455 flp->length = 0;
11456 flp->head = NULL;
11457 i = fip->nfnfields++;
11458 }
11459
11460 /* Create a new member function field and chain it to the field list
11461 entry. */
11462 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
11463 make_cleanup (xfree, new_fnfield);
11464 memset (new_fnfield, 0, sizeof (struct nextfnfield));
11465 new_fnfield->next = flp->head;
11466 flp->head = new_fnfield;
11467 flp->length++;
11468
11469 /* Fill in the member function field info. */
11470 fnp = &new_fnfield->fnfield;
11471
11472 /* Delay processing of the physname until later. */
11473 if (cu->language == language_cplus || cu->language == language_java)
11474 {
11475 add_to_method_list (type, i, flp->length - 1, fieldname,
11476 die, cu);
11477 }
11478 else
11479 {
11480 const char *physname = dwarf2_physname (fieldname, die, cu);
11481 fnp->physname = physname ? physname : "";
11482 }
11483
11484 fnp->type = alloc_type (objfile);
11485 this_type = read_type_die (die, cu);
11486 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
11487 {
11488 int nparams = TYPE_NFIELDS (this_type);
11489
11490 /* TYPE is the domain of this method, and THIS_TYPE is the type
11491 of the method itself (TYPE_CODE_METHOD). */
11492 smash_to_method_type (fnp->type, type,
11493 TYPE_TARGET_TYPE (this_type),
11494 TYPE_FIELDS (this_type),
11495 TYPE_NFIELDS (this_type),
11496 TYPE_VARARGS (this_type));
11497
11498 /* Handle static member functions.
11499 Dwarf2 has no clean way to discern C++ static and non-static
11500 member functions. G++ helps GDB by marking the first
11501 parameter for non-static member functions (which is the this
11502 pointer) as artificial. We obtain this information from
11503 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
11504 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
11505 fnp->voffset = VOFFSET_STATIC;
11506 }
11507 else
11508 complaint (&symfile_complaints, _("member function type missing for '%s'"),
11509 dwarf2_full_name (fieldname, die, cu));
11510
11511 /* Get fcontext from DW_AT_containing_type if present. */
11512 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11513 fnp->fcontext = die_containing_type (die, cu);
11514
11515 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
11516 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
11517
11518 /* Get accessibility. */
11519 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11520 if (attr)
11521 accessibility = DW_UNSND (attr);
11522 else
11523 accessibility = dwarf2_default_access_attribute (die, cu);
11524 switch (accessibility)
11525 {
11526 case DW_ACCESS_private:
11527 fnp->is_private = 1;
11528 break;
11529 case DW_ACCESS_protected:
11530 fnp->is_protected = 1;
11531 break;
11532 }
11533
11534 /* Check for artificial methods. */
11535 attr = dwarf2_attr (die, DW_AT_artificial, cu);
11536 if (attr && DW_UNSND (attr) != 0)
11537 fnp->is_artificial = 1;
11538
11539 fnp->is_constructor = dwarf2_is_constructor (die, cu);
11540
11541 /* Get index in virtual function table if it is a virtual member
11542 function. For older versions of GCC, this is an offset in the
11543 appropriate virtual table, as specified by DW_AT_containing_type.
11544 For everyone else, it is an expression to be evaluated relative
11545 to the object address. */
11546
11547 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
11548 if (attr)
11549 {
11550 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
11551 {
11552 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
11553 {
11554 /* Old-style GCC. */
11555 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
11556 }
11557 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
11558 || (DW_BLOCK (attr)->size > 1
11559 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
11560 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
11561 {
11562 struct dwarf_block blk;
11563 int offset;
11564
11565 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
11566 ? 1 : 2);
11567 blk.size = DW_BLOCK (attr)->size - offset;
11568 blk.data = DW_BLOCK (attr)->data + offset;
11569 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
11570 if ((fnp->voffset % cu->header.addr_size) != 0)
11571 dwarf2_complex_location_expr_complaint ();
11572 else
11573 fnp->voffset /= cu->header.addr_size;
11574 fnp->voffset += 2;
11575 }
11576 else
11577 dwarf2_complex_location_expr_complaint ();
11578
11579 if (!fnp->fcontext)
11580 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
11581 }
11582 else if (attr_form_is_section_offset (attr))
11583 {
11584 dwarf2_complex_location_expr_complaint ();
11585 }
11586 else
11587 {
11588 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
11589 fieldname);
11590 }
11591 }
11592 else
11593 {
11594 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11595 if (attr && DW_UNSND (attr))
11596 {
11597 /* GCC does this, as of 2008-08-25; PR debug/37237. */
11598 complaint (&symfile_complaints,
11599 _("Member function \"%s\" (offset %d) is virtual "
11600 "but the vtable offset is not specified"),
11601 fieldname, die->offset.sect_off);
11602 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11603 TYPE_CPLUS_DYNAMIC (type) = 1;
11604 }
11605 }
11606 }
11607
11608 /* Create the vector of member function fields, and attach it to the type. */
11609
11610 static void
11611 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
11612 struct dwarf2_cu *cu)
11613 {
11614 struct fnfieldlist *flp;
11615 int i;
11616
11617 if (cu->language == language_ada)
11618 error (_("unexpected member functions in Ada type"));
11619
11620 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11621 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
11622 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
11623
11624 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
11625 {
11626 struct nextfnfield *nfp = flp->head;
11627 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
11628 int k;
11629
11630 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
11631 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
11632 fn_flp->fn_fields = (struct fn_field *)
11633 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
11634 for (k = flp->length; (k--, nfp); nfp = nfp->next)
11635 fn_flp->fn_fields[k] = nfp->fnfield;
11636 }
11637
11638 TYPE_NFN_FIELDS (type) = fip->nfnfields;
11639 }
11640
11641 /* Returns non-zero if NAME is the name of a vtable member in CU's
11642 language, zero otherwise. */
11643 static int
11644 is_vtable_name (const char *name, struct dwarf2_cu *cu)
11645 {
11646 static const char vptr[] = "_vptr";
11647 static const char vtable[] = "vtable";
11648
11649 /* Look for the C++ and Java forms of the vtable. */
11650 if ((cu->language == language_java
11651 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
11652 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
11653 && is_cplus_marker (name[sizeof (vptr) - 1])))
11654 return 1;
11655
11656 return 0;
11657 }
11658
11659 /* GCC outputs unnamed structures that are really pointers to member
11660 functions, with the ABI-specified layout. If TYPE describes
11661 such a structure, smash it into a member function type.
11662
11663 GCC shouldn't do this; it should just output pointer to member DIEs.
11664 This is GCC PR debug/28767. */
11665
11666 static void
11667 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
11668 {
11669 struct type *pfn_type, *domain_type, *new_type;
11670
11671 /* Check for a structure with no name and two children. */
11672 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
11673 return;
11674
11675 /* Check for __pfn and __delta members. */
11676 if (TYPE_FIELD_NAME (type, 0) == NULL
11677 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
11678 || TYPE_FIELD_NAME (type, 1) == NULL
11679 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
11680 return;
11681
11682 /* Find the type of the method. */
11683 pfn_type = TYPE_FIELD_TYPE (type, 0);
11684 if (pfn_type == NULL
11685 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
11686 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
11687 return;
11688
11689 /* Look for the "this" argument. */
11690 pfn_type = TYPE_TARGET_TYPE (pfn_type);
11691 if (TYPE_NFIELDS (pfn_type) == 0
11692 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
11693 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
11694 return;
11695
11696 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
11697 new_type = alloc_type (objfile);
11698 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
11699 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
11700 TYPE_VARARGS (pfn_type));
11701 smash_to_methodptr_type (type, new_type);
11702 }
11703
11704 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
11705 (icc). */
11706
11707 static int
11708 producer_is_icc (struct dwarf2_cu *cu)
11709 {
11710 if (!cu->checked_producer)
11711 check_producer (cu);
11712
11713 return cu->producer_is_icc;
11714 }
11715
11716 /* Called when we find the DIE that starts a structure or union scope
11717 (definition) to create a type for the structure or union. Fill in
11718 the type's name and general properties; the members will not be
11719 processed until process_structure_scope.
11720
11721 NOTE: we need to call these functions regardless of whether or not the
11722 DIE has a DW_AT_name attribute, since it might be an anonymous
11723 structure or union. This gets the type entered into our set of
11724 user defined types.
11725
11726 However, if the structure is incomplete (an opaque struct/union)
11727 then suppress creating a symbol table entry for it since gdb only
11728 wants to find the one with the complete definition. Note that if
11729 it is complete, we just call new_symbol, which does it's own
11730 checking about whether the struct/union is anonymous or not (and
11731 suppresses creating a symbol table entry itself). */
11732
11733 static struct type *
11734 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
11735 {
11736 struct objfile *objfile = cu->objfile;
11737 struct type *type;
11738 struct attribute *attr;
11739 const char *name;
11740
11741 /* If the definition of this type lives in .debug_types, read that type.
11742 Don't follow DW_AT_specification though, that will take us back up
11743 the chain and we want to go down. */
11744 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
11745 if (attr)
11746 {
11747 type = get_DW_AT_signature_type (die, attr, cu);
11748
11749 /* The type's CU may not be the same as CU.
11750 Ensure TYPE is recorded with CU in die_type_hash. */
11751 return set_die_type (die, type, cu);
11752 }
11753
11754 type = alloc_type (objfile);
11755 INIT_CPLUS_SPECIFIC (type);
11756
11757 name = dwarf2_name (die, cu);
11758 if (name != NULL)
11759 {
11760 if (cu->language == language_cplus
11761 || cu->language == language_java)
11762 {
11763 const char *full_name = dwarf2_full_name (name, die, cu);
11764
11765 /* dwarf2_full_name might have already finished building the DIE's
11766 type. If so, there is no need to continue. */
11767 if (get_die_type (die, cu) != NULL)
11768 return get_die_type (die, cu);
11769
11770 TYPE_TAG_NAME (type) = full_name;
11771 if (die->tag == DW_TAG_structure_type
11772 || die->tag == DW_TAG_class_type)
11773 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11774 }
11775 else
11776 {
11777 /* The name is already allocated along with this objfile, so
11778 we don't need to duplicate it for the type. */
11779 TYPE_TAG_NAME (type) = name;
11780 if (die->tag == DW_TAG_class_type)
11781 TYPE_NAME (type) = TYPE_TAG_NAME (type);
11782 }
11783 }
11784
11785 if (die->tag == DW_TAG_structure_type)
11786 {
11787 TYPE_CODE (type) = TYPE_CODE_STRUCT;
11788 }
11789 else if (die->tag == DW_TAG_union_type)
11790 {
11791 TYPE_CODE (type) = TYPE_CODE_UNION;
11792 }
11793 else
11794 {
11795 TYPE_CODE (type) = TYPE_CODE_CLASS;
11796 }
11797
11798 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
11799 TYPE_DECLARED_CLASS (type) = 1;
11800
11801 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
11802 if (attr)
11803 {
11804 TYPE_LENGTH (type) = DW_UNSND (attr);
11805 }
11806 else
11807 {
11808 TYPE_LENGTH (type) = 0;
11809 }
11810
11811 if (producer_is_icc (cu))
11812 {
11813 /* ICC does not output the required DW_AT_declaration
11814 on incomplete types, but gives them a size of zero. */
11815 }
11816 else
11817 TYPE_STUB_SUPPORTED (type) = 1;
11818
11819 if (die_is_declaration (die, cu))
11820 TYPE_STUB (type) = 1;
11821 else if (attr == NULL && die->child == NULL
11822 && producer_is_realview (cu->producer))
11823 /* RealView does not output the required DW_AT_declaration
11824 on incomplete types. */
11825 TYPE_STUB (type) = 1;
11826
11827 /* We need to add the type field to the die immediately so we don't
11828 infinitely recurse when dealing with pointers to the structure
11829 type within the structure itself. */
11830 set_die_type (die, type, cu);
11831
11832 /* set_die_type should be already done. */
11833 set_descriptive_type (type, die, cu);
11834
11835 return type;
11836 }
11837
11838 /* Finish creating a structure or union type, including filling in
11839 its members and creating a symbol for it. */
11840
11841 static void
11842 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
11843 {
11844 struct objfile *objfile = cu->objfile;
11845 struct die_info *child_die = die->child;
11846 struct type *type;
11847
11848 type = get_die_type (die, cu);
11849 if (type == NULL)
11850 type = read_structure_type (die, cu);
11851
11852 if (die->child != NULL && ! die_is_declaration (die, cu))
11853 {
11854 struct field_info fi;
11855 struct die_info *child_die;
11856 VEC (symbolp) *template_args = NULL;
11857 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
11858
11859 memset (&fi, 0, sizeof (struct field_info));
11860
11861 child_die = die->child;
11862
11863 while (child_die && child_die->tag)
11864 {
11865 if (child_die->tag == DW_TAG_member
11866 || child_die->tag == DW_TAG_variable)
11867 {
11868 /* NOTE: carlton/2002-11-05: A C++ static data member
11869 should be a DW_TAG_member that is a declaration, but
11870 all versions of G++ as of this writing (so through at
11871 least 3.2.1) incorrectly generate DW_TAG_variable
11872 tags for them instead. */
11873 dwarf2_add_field (&fi, child_die, cu);
11874 }
11875 else if (child_die->tag == DW_TAG_subprogram)
11876 {
11877 /* C++ member function. */
11878 dwarf2_add_member_fn (&fi, child_die, type, cu);
11879 }
11880 else if (child_die->tag == DW_TAG_inheritance)
11881 {
11882 /* C++ base class field. */
11883 dwarf2_add_field (&fi, child_die, cu);
11884 }
11885 else if (child_die->tag == DW_TAG_typedef)
11886 dwarf2_add_typedef (&fi, child_die, cu);
11887 else if (child_die->tag == DW_TAG_template_type_param
11888 || child_die->tag == DW_TAG_template_value_param)
11889 {
11890 struct symbol *arg = new_symbol (child_die, NULL, cu);
11891
11892 if (arg != NULL)
11893 VEC_safe_push (symbolp, template_args, arg);
11894 }
11895
11896 child_die = sibling_die (child_die);
11897 }
11898
11899 /* Attach template arguments to type. */
11900 if (! VEC_empty (symbolp, template_args))
11901 {
11902 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11903 TYPE_N_TEMPLATE_ARGUMENTS (type)
11904 = VEC_length (symbolp, template_args);
11905 TYPE_TEMPLATE_ARGUMENTS (type)
11906 = obstack_alloc (&objfile->objfile_obstack,
11907 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11908 * sizeof (struct symbol *)));
11909 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
11910 VEC_address (symbolp, template_args),
11911 (TYPE_N_TEMPLATE_ARGUMENTS (type)
11912 * sizeof (struct symbol *)));
11913 VEC_free (symbolp, template_args);
11914 }
11915
11916 /* Attach fields and member functions to the type. */
11917 if (fi.nfields)
11918 dwarf2_attach_fields_to_type (&fi, type, cu);
11919 if (fi.nfnfields)
11920 {
11921 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
11922
11923 /* Get the type which refers to the base class (possibly this
11924 class itself) which contains the vtable pointer for the current
11925 class from the DW_AT_containing_type attribute. This use of
11926 DW_AT_containing_type is a GNU extension. */
11927
11928 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
11929 {
11930 struct type *t = die_containing_type (die, cu);
11931
11932 TYPE_VPTR_BASETYPE (type) = t;
11933 if (type == t)
11934 {
11935 int i;
11936
11937 /* Our own class provides vtbl ptr. */
11938 for (i = TYPE_NFIELDS (t) - 1;
11939 i >= TYPE_N_BASECLASSES (t);
11940 --i)
11941 {
11942 const char *fieldname = TYPE_FIELD_NAME (t, i);
11943
11944 if (is_vtable_name (fieldname, cu))
11945 {
11946 TYPE_VPTR_FIELDNO (type) = i;
11947 break;
11948 }
11949 }
11950
11951 /* Complain if virtual function table field not found. */
11952 if (i < TYPE_N_BASECLASSES (t))
11953 complaint (&symfile_complaints,
11954 _("virtual function table pointer "
11955 "not found when defining class '%s'"),
11956 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
11957 "");
11958 }
11959 else
11960 {
11961 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
11962 }
11963 }
11964 else if (cu->producer
11965 && strncmp (cu->producer,
11966 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
11967 {
11968 /* The IBM XLC compiler does not provide direct indication
11969 of the containing type, but the vtable pointer is
11970 always named __vfp. */
11971
11972 int i;
11973
11974 for (i = TYPE_NFIELDS (type) - 1;
11975 i >= TYPE_N_BASECLASSES (type);
11976 --i)
11977 {
11978 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
11979 {
11980 TYPE_VPTR_FIELDNO (type) = i;
11981 TYPE_VPTR_BASETYPE (type) = type;
11982 break;
11983 }
11984 }
11985 }
11986 }
11987
11988 /* Copy fi.typedef_field_list linked list elements content into the
11989 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
11990 if (fi.typedef_field_list)
11991 {
11992 int i = fi.typedef_field_list_count;
11993
11994 ALLOCATE_CPLUS_STRUCT_TYPE (type);
11995 TYPE_TYPEDEF_FIELD_ARRAY (type)
11996 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
11997 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
11998
11999 /* Reverse the list order to keep the debug info elements order. */
12000 while (--i >= 0)
12001 {
12002 struct typedef_field *dest, *src;
12003
12004 dest = &TYPE_TYPEDEF_FIELD (type, i);
12005 src = &fi.typedef_field_list->field;
12006 fi.typedef_field_list = fi.typedef_field_list->next;
12007 *dest = *src;
12008 }
12009 }
12010
12011 do_cleanups (back_to);
12012
12013 if (HAVE_CPLUS_STRUCT (type))
12014 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
12015 }
12016
12017 quirk_gcc_member_function_pointer (type, objfile);
12018
12019 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12020 snapshots) has been known to create a die giving a declaration
12021 for a class that has, as a child, a die giving a definition for a
12022 nested class. So we have to process our children even if the
12023 current die is a declaration. Normally, of course, a declaration
12024 won't have any children at all. */
12025
12026 while (child_die != NULL && child_die->tag)
12027 {
12028 if (child_die->tag == DW_TAG_member
12029 || child_die->tag == DW_TAG_variable
12030 || child_die->tag == DW_TAG_inheritance
12031 || child_die->tag == DW_TAG_template_value_param
12032 || child_die->tag == DW_TAG_template_type_param)
12033 {
12034 /* Do nothing. */
12035 }
12036 else
12037 process_die (child_die, cu);
12038
12039 child_die = sibling_die (child_die);
12040 }
12041
12042 /* Do not consider external references. According to the DWARF standard,
12043 these DIEs are identified by the fact that they have no byte_size
12044 attribute, and a declaration attribute. */
12045 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12046 || !die_is_declaration (die, cu))
12047 new_symbol (die, type, cu);
12048 }
12049
12050 /* Given a DW_AT_enumeration_type die, set its type. We do not
12051 complete the type's fields yet, or create any symbols. */
12052
12053 static struct type *
12054 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
12055 {
12056 struct objfile *objfile = cu->objfile;
12057 struct type *type;
12058 struct attribute *attr;
12059 const char *name;
12060
12061 /* If the definition of this type lives in .debug_types, read that type.
12062 Don't follow DW_AT_specification though, that will take us back up
12063 the chain and we want to go down. */
12064 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
12065 if (attr)
12066 {
12067 type = get_DW_AT_signature_type (die, attr, cu);
12068
12069 /* The type's CU may not be the same as CU.
12070 Ensure TYPE is recorded with CU in die_type_hash. */
12071 return set_die_type (die, type, cu);
12072 }
12073
12074 type = alloc_type (objfile);
12075
12076 TYPE_CODE (type) = TYPE_CODE_ENUM;
12077 name = dwarf2_full_name (NULL, die, cu);
12078 if (name != NULL)
12079 TYPE_TAG_NAME (type) = name;
12080
12081 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12082 if (attr)
12083 {
12084 TYPE_LENGTH (type) = DW_UNSND (attr);
12085 }
12086 else
12087 {
12088 TYPE_LENGTH (type) = 0;
12089 }
12090
12091 /* The enumeration DIE can be incomplete. In Ada, any type can be
12092 declared as private in the package spec, and then defined only
12093 inside the package body. Such types are known as Taft Amendment
12094 Types. When another package uses such a type, an incomplete DIE
12095 may be generated by the compiler. */
12096 if (die_is_declaration (die, cu))
12097 TYPE_STUB (type) = 1;
12098
12099 return set_die_type (die, type, cu);
12100 }
12101
12102 /* Given a pointer to a die which begins an enumeration, process all
12103 the dies that define the members of the enumeration, and create the
12104 symbol for the enumeration type.
12105
12106 NOTE: We reverse the order of the element list. */
12107
12108 static void
12109 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12110 {
12111 struct type *this_type;
12112
12113 this_type = get_die_type (die, cu);
12114 if (this_type == NULL)
12115 this_type = read_enumeration_type (die, cu);
12116
12117 if (die->child != NULL)
12118 {
12119 struct die_info *child_die;
12120 struct symbol *sym;
12121 struct field *fields = NULL;
12122 int num_fields = 0;
12123 int unsigned_enum = 1;
12124 const char *name;
12125 int flag_enum = 1;
12126 ULONGEST mask = 0;
12127
12128 child_die = die->child;
12129 while (child_die && child_die->tag)
12130 {
12131 if (child_die->tag != DW_TAG_enumerator)
12132 {
12133 process_die (child_die, cu);
12134 }
12135 else
12136 {
12137 name = dwarf2_name (child_die, cu);
12138 if (name)
12139 {
12140 sym = new_symbol (child_die, this_type, cu);
12141 if (SYMBOL_VALUE (sym) < 0)
12142 {
12143 unsigned_enum = 0;
12144 flag_enum = 0;
12145 }
12146 else if ((mask & SYMBOL_VALUE (sym)) != 0)
12147 flag_enum = 0;
12148 else
12149 mask |= SYMBOL_VALUE (sym);
12150
12151 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
12152 {
12153 fields = (struct field *)
12154 xrealloc (fields,
12155 (num_fields + DW_FIELD_ALLOC_CHUNK)
12156 * sizeof (struct field));
12157 }
12158
12159 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
12160 FIELD_TYPE (fields[num_fields]) = NULL;
12161 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
12162 FIELD_BITSIZE (fields[num_fields]) = 0;
12163
12164 num_fields++;
12165 }
12166 }
12167
12168 child_die = sibling_die (child_die);
12169 }
12170
12171 if (num_fields)
12172 {
12173 TYPE_NFIELDS (this_type) = num_fields;
12174 TYPE_FIELDS (this_type) = (struct field *)
12175 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
12176 memcpy (TYPE_FIELDS (this_type), fields,
12177 sizeof (struct field) * num_fields);
12178 xfree (fields);
12179 }
12180 if (unsigned_enum)
12181 TYPE_UNSIGNED (this_type) = 1;
12182 if (flag_enum)
12183 TYPE_FLAG_ENUM (this_type) = 1;
12184 }
12185
12186 /* If we are reading an enum from a .debug_types unit, and the enum
12187 is a declaration, and the enum is not the signatured type in the
12188 unit, then we do not want to add a symbol for it. Adding a
12189 symbol would in some cases obscure the true definition of the
12190 enum, giving users an incomplete type when the definition is
12191 actually available. Note that we do not want to do this for all
12192 enums which are just declarations, because C++0x allows forward
12193 enum declarations. */
12194 if (cu->per_cu->is_debug_types
12195 && die_is_declaration (die, cu))
12196 {
12197 struct signatured_type *sig_type;
12198
12199 sig_type = (struct signatured_type *) cu->per_cu;
12200 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
12201 if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
12202 return;
12203 }
12204
12205 new_symbol (die, this_type, cu);
12206 }
12207
12208 /* Extract all information from a DW_TAG_array_type DIE and put it in
12209 the DIE's type field. For now, this only handles one dimensional
12210 arrays. */
12211
12212 static struct type *
12213 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
12214 {
12215 struct objfile *objfile = cu->objfile;
12216 struct die_info *child_die;
12217 struct type *type;
12218 struct type *element_type, *range_type, *index_type;
12219 struct type **range_types = NULL;
12220 struct attribute *attr;
12221 int ndim = 0;
12222 struct cleanup *back_to;
12223 const char *name;
12224
12225 element_type = die_type (die, cu);
12226
12227 /* The die_type call above may have already set the type for this DIE. */
12228 type = get_die_type (die, cu);
12229 if (type)
12230 return type;
12231
12232 /* Irix 6.2 native cc creates array types without children for
12233 arrays with unspecified length. */
12234 if (die->child == NULL)
12235 {
12236 index_type = objfile_type (objfile)->builtin_int;
12237 range_type = create_range_type (NULL, index_type, 0, -1);
12238 type = create_array_type (NULL, element_type, range_type);
12239 return set_die_type (die, type, cu);
12240 }
12241
12242 back_to = make_cleanup (null_cleanup, NULL);
12243 child_die = die->child;
12244 while (child_die && child_die->tag)
12245 {
12246 if (child_die->tag == DW_TAG_subrange_type)
12247 {
12248 struct type *child_type = read_type_die (child_die, cu);
12249
12250 if (child_type != NULL)
12251 {
12252 /* The range type was succesfully read. Save it for the
12253 array type creation. */
12254 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
12255 {
12256 range_types = (struct type **)
12257 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
12258 * sizeof (struct type *));
12259 if (ndim == 0)
12260 make_cleanup (free_current_contents, &range_types);
12261 }
12262 range_types[ndim++] = child_type;
12263 }
12264 }
12265 child_die = sibling_die (child_die);
12266 }
12267
12268 /* Dwarf2 dimensions are output from left to right, create the
12269 necessary array types in backwards order. */
12270
12271 type = element_type;
12272
12273 if (read_array_order (die, cu) == DW_ORD_col_major)
12274 {
12275 int i = 0;
12276
12277 while (i < ndim)
12278 type = create_array_type (NULL, type, range_types[i++]);
12279 }
12280 else
12281 {
12282 while (ndim-- > 0)
12283 type = create_array_type (NULL, type, range_types[ndim]);
12284 }
12285
12286 /* Understand Dwarf2 support for vector types (like they occur on
12287 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
12288 array type. This is not part of the Dwarf2/3 standard yet, but a
12289 custom vendor extension. The main difference between a regular
12290 array and the vector variant is that vectors are passed by value
12291 to functions. */
12292 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
12293 if (attr)
12294 make_vector_type (type);
12295
12296 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
12297 implementation may choose to implement triple vectors using this
12298 attribute. */
12299 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12300 if (attr)
12301 {
12302 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
12303 TYPE_LENGTH (type) = DW_UNSND (attr);
12304 else
12305 complaint (&symfile_complaints,
12306 _("DW_AT_byte_size for array type smaller "
12307 "than the total size of elements"));
12308 }
12309
12310 name = dwarf2_name (die, cu);
12311 if (name)
12312 TYPE_NAME (type) = name;
12313
12314 /* Install the type in the die. */
12315 set_die_type (die, type, cu);
12316
12317 /* set_die_type should be already done. */
12318 set_descriptive_type (type, die, cu);
12319
12320 do_cleanups (back_to);
12321
12322 return type;
12323 }
12324
12325 static enum dwarf_array_dim_ordering
12326 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
12327 {
12328 struct attribute *attr;
12329
12330 attr = dwarf2_attr (die, DW_AT_ordering, cu);
12331
12332 if (attr) return DW_SND (attr);
12333
12334 /* GNU F77 is a special case, as at 08/2004 array type info is the
12335 opposite order to the dwarf2 specification, but data is still
12336 laid out as per normal fortran.
12337
12338 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
12339 version checking. */
12340
12341 if (cu->language == language_fortran
12342 && cu->producer && strstr (cu->producer, "GNU F77"))
12343 {
12344 return DW_ORD_row_major;
12345 }
12346
12347 switch (cu->language_defn->la_array_ordering)
12348 {
12349 case array_column_major:
12350 return DW_ORD_col_major;
12351 case array_row_major:
12352 default:
12353 return DW_ORD_row_major;
12354 };
12355 }
12356
12357 /* Extract all information from a DW_TAG_set_type DIE and put it in
12358 the DIE's type field. */
12359
12360 static struct type *
12361 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
12362 {
12363 struct type *domain_type, *set_type;
12364 struct attribute *attr;
12365
12366 domain_type = die_type (die, cu);
12367
12368 /* The die_type call above may have already set the type for this DIE. */
12369 set_type = get_die_type (die, cu);
12370 if (set_type)
12371 return set_type;
12372
12373 set_type = create_set_type (NULL, domain_type);
12374
12375 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12376 if (attr)
12377 TYPE_LENGTH (set_type) = DW_UNSND (attr);
12378
12379 return set_die_type (die, set_type, cu);
12380 }
12381
12382 /* A helper for read_common_block that creates a locexpr baton.
12383 SYM is the symbol which we are marking as computed.
12384 COMMON_DIE is the DIE for the common block.
12385 COMMON_LOC is the location expression attribute for the common
12386 block itself.
12387 MEMBER_LOC is the location expression attribute for the particular
12388 member of the common block that we are processing.
12389 CU is the CU from which the above come. */
12390
12391 static void
12392 mark_common_block_symbol_computed (struct symbol *sym,
12393 struct die_info *common_die,
12394 struct attribute *common_loc,
12395 struct attribute *member_loc,
12396 struct dwarf2_cu *cu)
12397 {
12398 struct objfile *objfile = dwarf2_per_objfile->objfile;
12399 struct dwarf2_locexpr_baton *baton;
12400 gdb_byte *ptr;
12401 unsigned int cu_off;
12402 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
12403 LONGEST offset = 0;
12404
12405 gdb_assert (common_loc && member_loc);
12406 gdb_assert (attr_form_is_block (common_loc));
12407 gdb_assert (attr_form_is_block (member_loc)
12408 || attr_form_is_constant (member_loc));
12409
12410 baton = obstack_alloc (&objfile->objfile_obstack,
12411 sizeof (struct dwarf2_locexpr_baton));
12412 baton->per_cu = cu->per_cu;
12413 gdb_assert (baton->per_cu);
12414
12415 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
12416
12417 if (attr_form_is_constant (member_loc))
12418 {
12419 offset = dwarf2_get_attr_constant_value (member_loc, 0);
12420 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
12421 }
12422 else
12423 baton->size += DW_BLOCK (member_loc)->size;
12424
12425 ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
12426 baton->data = ptr;
12427
12428 *ptr++ = DW_OP_call4;
12429 cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
12430 store_unsigned_integer (ptr, 4, byte_order, cu_off);
12431 ptr += 4;
12432
12433 if (attr_form_is_constant (member_loc))
12434 {
12435 *ptr++ = DW_OP_addr;
12436 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
12437 ptr += cu->header.addr_size;
12438 }
12439 else
12440 {
12441 /* We have to copy the data here, because DW_OP_call4 will only
12442 use a DW_AT_location attribute. */
12443 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
12444 ptr += DW_BLOCK (member_loc)->size;
12445 }
12446
12447 *ptr++ = DW_OP_plus;
12448 gdb_assert (ptr - baton->data == baton->size);
12449
12450 SYMBOL_LOCATION_BATON (sym) = baton;
12451 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
12452 }
12453
12454 /* Create appropriate locally-scoped variables for all the
12455 DW_TAG_common_block entries. Also create a struct common_block
12456 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
12457 is used to sepate the common blocks name namespace from regular
12458 variable names. */
12459
12460 static void
12461 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
12462 {
12463 struct attribute *attr;
12464
12465 attr = dwarf2_attr (die, DW_AT_location, cu);
12466 if (attr)
12467 {
12468 /* Support the .debug_loc offsets. */
12469 if (attr_form_is_block (attr))
12470 {
12471 /* Ok. */
12472 }
12473 else if (attr_form_is_section_offset (attr))
12474 {
12475 dwarf2_complex_location_expr_complaint ();
12476 attr = NULL;
12477 }
12478 else
12479 {
12480 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
12481 "common block member");
12482 attr = NULL;
12483 }
12484 }
12485
12486 if (die->child != NULL)
12487 {
12488 struct objfile *objfile = cu->objfile;
12489 struct die_info *child_die;
12490 size_t n_entries = 0, size;
12491 struct common_block *common_block;
12492 struct symbol *sym;
12493
12494 for (child_die = die->child;
12495 child_die && child_die->tag;
12496 child_die = sibling_die (child_die))
12497 ++n_entries;
12498
12499 size = (sizeof (struct common_block)
12500 + (n_entries - 1) * sizeof (struct symbol *));
12501 common_block = obstack_alloc (&objfile->objfile_obstack, size);
12502 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
12503 common_block->n_entries = 0;
12504
12505 for (child_die = die->child;
12506 child_die && child_die->tag;
12507 child_die = sibling_die (child_die))
12508 {
12509 /* Create the symbol in the DW_TAG_common_block block in the current
12510 symbol scope. */
12511 sym = new_symbol (child_die, NULL, cu);
12512 if (sym != NULL)
12513 {
12514 struct attribute *member_loc;
12515
12516 common_block->contents[common_block->n_entries++] = sym;
12517
12518 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
12519 cu);
12520 if (member_loc)
12521 {
12522 /* GDB has handled this for a long time, but it is
12523 not specified by DWARF. It seems to have been
12524 emitted by gfortran at least as recently as:
12525 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
12526 complaint (&symfile_complaints,
12527 _("Variable in common block has "
12528 "DW_AT_data_member_location "
12529 "- DIE at 0x%x [in module %s]"),
12530 child_die->offset.sect_off, cu->objfile->name);
12531
12532 if (attr_form_is_section_offset (member_loc))
12533 dwarf2_complex_location_expr_complaint ();
12534 else if (attr_form_is_constant (member_loc)
12535 || attr_form_is_block (member_loc))
12536 {
12537 if (attr)
12538 mark_common_block_symbol_computed (sym, die, attr,
12539 member_loc, cu);
12540 }
12541 else
12542 dwarf2_complex_location_expr_complaint ();
12543 }
12544 }
12545 }
12546
12547 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
12548 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
12549 }
12550 }
12551
12552 /* Create a type for a C++ namespace. */
12553
12554 static struct type *
12555 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
12556 {
12557 struct objfile *objfile = cu->objfile;
12558 const char *previous_prefix, *name;
12559 int is_anonymous;
12560 struct type *type;
12561
12562 /* For extensions, reuse the type of the original namespace. */
12563 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
12564 {
12565 struct die_info *ext_die;
12566 struct dwarf2_cu *ext_cu = cu;
12567
12568 ext_die = dwarf2_extension (die, &ext_cu);
12569 type = read_type_die (ext_die, ext_cu);
12570
12571 /* EXT_CU may not be the same as CU.
12572 Ensure TYPE is recorded with CU in die_type_hash. */
12573 return set_die_type (die, type, cu);
12574 }
12575
12576 name = namespace_name (die, &is_anonymous, cu);
12577
12578 /* Now build the name of the current namespace. */
12579
12580 previous_prefix = determine_prefix (die, cu);
12581 if (previous_prefix[0] != '\0')
12582 name = typename_concat (&objfile->objfile_obstack,
12583 previous_prefix, name, 0, cu);
12584
12585 /* Create the type. */
12586 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
12587 objfile);
12588 TYPE_NAME (type) = name;
12589 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12590
12591 return set_die_type (die, type, cu);
12592 }
12593
12594 /* Read a C++ namespace. */
12595
12596 static void
12597 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
12598 {
12599 struct objfile *objfile = cu->objfile;
12600 int is_anonymous;
12601
12602 /* Add a symbol associated to this if we haven't seen the namespace
12603 before. Also, add a using directive if it's an anonymous
12604 namespace. */
12605
12606 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
12607 {
12608 struct type *type;
12609
12610 type = read_type_die (die, cu);
12611 new_symbol (die, type, cu);
12612
12613 namespace_name (die, &is_anonymous, cu);
12614 if (is_anonymous)
12615 {
12616 const char *previous_prefix = determine_prefix (die, cu);
12617
12618 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
12619 NULL, NULL, 0, &objfile->objfile_obstack);
12620 }
12621 }
12622
12623 if (die->child != NULL)
12624 {
12625 struct die_info *child_die = die->child;
12626
12627 while (child_die && child_die->tag)
12628 {
12629 process_die (child_die, cu);
12630 child_die = sibling_die (child_die);
12631 }
12632 }
12633 }
12634
12635 /* Read a Fortran module as type. This DIE can be only a declaration used for
12636 imported module. Still we need that type as local Fortran "use ... only"
12637 declaration imports depend on the created type in determine_prefix. */
12638
12639 static struct type *
12640 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
12641 {
12642 struct objfile *objfile = cu->objfile;
12643 const char *module_name;
12644 struct type *type;
12645
12646 module_name = dwarf2_name (die, cu);
12647 if (!module_name)
12648 complaint (&symfile_complaints,
12649 _("DW_TAG_module has no name, offset 0x%x"),
12650 die->offset.sect_off);
12651 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
12652
12653 /* determine_prefix uses TYPE_TAG_NAME. */
12654 TYPE_TAG_NAME (type) = TYPE_NAME (type);
12655
12656 return set_die_type (die, type, cu);
12657 }
12658
12659 /* Read a Fortran module. */
12660
12661 static void
12662 read_module (struct die_info *die, struct dwarf2_cu *cu)
12663 {
12664 struct die_info *child_die = die->child;
12665
12666 while (child_die && child_die->tag)
12667 {
12668 process_die (child_die, cu);
12669 child_die = sibling_die (child_die);
12670 }
12671 }
12672
12673 /* Return the name of the namespace represented by DIE. Set
12674 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
12675 namespace. */
12676
12677 static const char *
12678 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
12679 {
12680 struct die_info *current_die;
12681 const char *name = NULL;
12682
12683 /* Loop through the extensions until we find a name. */
12684
12685 for (current_die = die;
12686 current_die != NULL;
12687 current_die = dwarf2_extension (die, &cu))
12688 {
12689 name = dwarf2_name (current_die, cu);
12690 if (name != NULL)
12691 break;
12692 }
12693
12694 /* Is it an anonymous namespace? */
12695
12696 *is_anonymous = (name == NULL);
12697 if (*is_anonymous)
12698 name = CP_ANONYMOUS_NAMESPACE_STR;
12699
12700 return name;
12701 }
12702
12703 /* Extract all information from a DW_TAG_pointer_type DIE and add to
12704 the user defined type vector. */
12705
12706 static struct type *
12707 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
12708 {
12709 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
12710 struct comp_unit_head *cu_header = &cu->header;
12711 struct type *type;
12712 struct attribute *attr_byte_size;
12713 struct attribute *attr_address_class;
12714 int byte_size, addr_class;
12715 struct type *target_type;
12716
12717 target_type = die_type (die, cu);
12718
12719 /* The die_type call above may have already set the type for this DIE. */
12720 type = get_die_type (die, cu);
12721 if (type)
12722 return type;
12723
12724 type = lookup_pointer_type (target_type);
12725
12726 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
12727 if (attr_byte_size)
12728 byte_size = DW_UNSND (attr_byte_size);
12729 else
12730 byte_size = cu_header->addr_size;
12731
12732 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
12733 if (attr_address_class)
12734 addr_class = DW_UNSND (attr_address_class);
12735 else
12736 addr_class = DW_ADDR_none;
12737
12738 /* If the pointer size or address class is different than the
12739 default, create a type variant marked as such and set the
12740 length accordingly. */
12741 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
12742 {
12743 if (gdbarch_address_class_type_flags_p (gdbarch))
12744 {
12745 int type_flags;
12746
12747 type_flags = gdbarch_address_class_type_flags
12748 (gdbarch, byte_size, addr_class);
12749 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
12750 == 0);
12751 type = make_type_with_address_space (type, type_flags);
12752 }
12753 else if (TYPE_LENGTH (type) != byte_size)
12754 {
12755 complaint (&symfile_complaints,
12756 _("invalid pointer size %d"), byte_size);
12757 }
12758 else
12759 {
12760 /* Should we also complain about unhandled address classes? */
12761 }
12762 }
12763
12764 TYPE_LENGTH (type) = byte_size;
12765 return set_die_type (die, type, cu);
12766 }
12767
12768 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
12769 the user defined type vector. */
12770
12771 static struct type *
12772 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
12773 {
12774 struct type *type;
12775 struct type *to_type;
12776 struct type *domain;
12777
12778 to_type = die_type (die, cu);
12779 domain = die_containing_type (die, cu);
12780
12781 /* The calls above may have already set the type for this DIE. */
12782 type = get_die_type (die, cu);
12783 if (type)
12784 return type;
12785
12786 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
12787 type = lookup_methodptr_type (to_type);
12788 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
12789 {
12790 struct type *new_type = alloc_type (cu->objfile);
12791
12792 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
12793 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
12794 TYPE_VARARGS (to_type));
12795 type = lookup_methodptr_type (new_type);
12796 }
12797 else
12798 type = lookup_memberptr_type (to_type, domain);
12799
12800 return set_die_type (die, type, cu);
12801 }
12802
12803 /* Extract all information from a DW_TAG_reference_type DIE and add to
12804 the user defined type vector. */
12805
12806 static struct type *
12807 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
12808 {
12809 struct comp_unit_head *cu_header = &cu->header;
12810 struct type *type, *target_type;
12811 struct attribute *attr;
12812
12813 target_type = die_type (die, cu);
12814
12815 /* The die_type call above may have already set the type for this DIE. */
12816 type = get_die_type (die, cu);
12817 if (type)
12818 return type;
12819
12820 type = lookup_reference_type (target_type);
12821 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12822 if (attr)
12823 {
12824 TYPE_LENGTH (type) = DW_UNSND (attr);
12825 }
12826 else
12827 {
12828 TYPE_LENGTH (type) = cu_header->addr_size;
12829 }
12830 return set_die_type (die, type, cu);
12831 }
12832
12833 static struct type *
12834 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
12835 {
12836 struct type *base_type, *cv_type;
12837
12838 base_type = die_type (die, cu);
12839
12840 /* The die_type call above may have already set the type for this DIE. */
12841 cv_type = get_die_type (die, cu);
12842 if (cv_type)
12843 return cv_type;
12844
12845 /* In case the const qualifier is applied to an array type, the element type
12846 is so qualified, not the array type (section 6.7.3 of C99). */
12847 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
12848 {
12849 struct type *el_type, *inner_array;
12850
12851 base_type = copy_type (base_type);
12852 inner_array = base_type;
12853
12854 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
12855 {
12856 TYPE_TARGET_TYPE (inner_array) =
12857 copy_type (TYPE_TARGET_TYPE (inner_array));
12858 inner_array = TYPE_TARGET_TYPE (inner_array);
12859 }
12860
12861 el_type = TYPE_TARGET_TYPE (inner_array);
12862 TYPE_TARGET_TYPE (inner_array) =
12863 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
12864
12865 return set_die_type (die, base_type, cu);
12866 }
12867
12868 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
12869 return set_die_type (die, cv_type, cu);
12870 }
12871
12872 static struct type *
12873 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
12874 {
12875 struct type *base_type, *cv_type;
12876
12877 base_type = die_type (die, cu);
12878
12879 /* The die_type call above may have already set the type for this DIE. */
12880 cv_type = get_die_type (die, cu);
12881 if (cv_type)
12882 return cv_type;
12883
12884 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
12885 return set_die_type (die, cv_type, cu);
12886 }
12887
12888 /* Handle DW_TAG_restrict_type. */
12889
12890 static struct type *
12891 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
12892 {
12893 struct type *base_type, *cv_type;
12894
12895 base_type = die_type (die, cu);
12896
12897 /* The die_type call above may have already set the type for this DIE. */
12898 cv_type = get_die_type (die, cu);
12899 if (cv_type)
12900 return cv_type;
12901
12902 cv_type = make_restrict_type (base_type);
12903 return set_die_type (die, cv_type, cu);
12904 }
12905
12906 /* Extract all information from a DW_TAG_string_type DIE and add to
12907 the user defined type vector. It isn't really a user defined type,
12908 but it behaves like one, with other DIE's using an AT_user_def_type
12909 attribute to reference it. */
12910
12911 static struct type *
12912 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
12913 {
12914 struct objfile *objfile = cu->objfile;
12915 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12916 struct type *type, *range_type, *index_type, *char_type;
12917 struct attribute *attr;
12918 unsigned int length;
12919
12920 attr = dwarf2_attr (die, DW_AT_string_length, cu);
12921 if (attr)
12922 {
12923 length = DW_UNSND (attr);
12924 }
12925 else
12926 {
12927 /* Check for the DW_AT_byte_size attribute. */
12928 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12929 if (attr)
12930 {
12931 length = DW_UNSND (attr);
12932 }
12933 else
12934 {
12935 length = 1;
12936 }
12937 }
12938
12939 index_type = objfile_type (objfile)->builtin_int;
12940 range_type = create_range_type (NULL, index_type, 1, length);
12941 char_type = language_string_char_type (cu->language_defn, gdbarch);
12942 type = create_string_type (NULL, char_type, range_type);
12943
12944 return set_die_type (die, type, cu);
12945 }
12946
12947 /* Assuming that DIE corresponds to a function, returns nonzero
12948 if the function is prototyped. */
12949
12950 static int
12951 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
12952 {
12953 struct attribute *attr;
12954
12955 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
12956 if (attr && (DW_UNSND (attr) != 0))
12957 return 1;
12958
12959 /* The DWARF standard implies that the DW_AT_prototyped attribute
12960 is only meaninful for C, but the concept also extends to other
12961 languages that allow unprototyped functions (Eg: Objective C).
12962 For all other languages, assume that functions are always
12963 prototyped. */
12964 if (cu->language != language_c
12965 && cu->language != language_objc
12966 && cu->language != language_opencl)
12967 return 1;
12968
12969 /* RealView does not emit DW_AT_prototyped. We can not distinguish
12970 prototyped and unprototyped functions; default to prototyped,
12971 since that is more common in modern code (and RealView warns
12972 about unprototyped functions). */
12973 if (producer_is_realview (cu->producer))
12974 return 1;
12975
12976 return 0;
12977 }
12978
12979 /* Handle DIES due to C code like:
12980
12981 struct foo
12982 {
12983 int (*funcp)(int a, long l);
12984 int b;
12985 };
12986
12987 ('funcp' generates a DW_TAG_subroutine_type DIE). */
12988
12989 static struct type *
12990 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
12991 {
12992 struct objfile *objfile = cu->objfile;
12993 struct type *type; /* Type that this function returns. */
12994 struct type *ftype; /* Function that returns above type. */
12995 struct attribute *attr;
12996
12997 type = die_type (die, cu);
12998
12999 /* The die_type call above may have already set the type for this DIE. */
13000 ftype = get_die_type (die, cu);
13001 if (ftype)
13002 return ftype;
13003
13004 ftype = lookup_function_type (type);
13005
13006 if (prototyped_function_p (die, cu))
13007 TYPE_PROTOTYPED (ftype) = 1;
13008
13009 /* Store the calling convention in the type if it's available in
13010 the subroutine die. Otherwise set the calling convention to
13011 the default value DW_CC_normal. */
13012 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
13013 if (attr)
13014 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
13015 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
13016 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
13017 else
13018 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
13019
13020 /* We need to add the subroutine type to the die immediately so
13021 we don't infinitely recurse when dealing with parameters
13022 declared as the same subroutine type. */
13023 set_die_type (die, ftype, cu);
13024
13025 if (die->child != NULL)
13026 {
13027 struct type *void_type = objfile_type (objfile)->builtin_void;
13028 struct die_info *child_die;
13029 int nparams, iparams;
13030
13031 /* Count the number of parameters.
13032 FIXME: GDB currently ignores vararg functions, but knows about
13033 vararg member functions. */
13034 nparams = 0;
13035 child_die = die->child;
13036 while (child_die && child_die->tag)
13037 {
13038 if (child_die->tag == DW_TAG_formal_parameter)
13039 nparams++;
13040 else if (child_die->tag == DW_TAG_unspecified_parameters)
13041 TYPE_VARARGS (ftype) = 1;
13042 child_die = sibling_die (child_die);
13043 }
13044
13045 /* Allocate storage for parameters and fill them in. */
13046 TYPE_NFIELDS (ftype) = nparams;
13047 TYPE_FIELDS (ftype) = (struct field *)
13048 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
13049
13050 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
13051 even if we error out during the parameters reading below. */
13052 for (iparams = 0; iparams < nparams; iparams++)
13053 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
13054
13055 iparams = 0;
13056 child_die = die->child;
13057 while (child_die && child_die->tag)
13058 {
13059 if (child_die->tag == DW_TAG_formal_parameter)
13060 {
13061 struct type *arg_type;
13062
13063 /* DWARF version 2 has no clean way to discern C++
13064 static and non-static member functions. G++ helps
13065 GDB by marking the first parameter for non-static
13066 member functions (which is the this pointer) as
13067 artificial. We pass this information to
13068 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
13069
13070 DWARF version 3 added DW_AT_object_pointer, which GCC
13071 4.5 does not yet generate. */
13072 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
13073 if (attr)
13074 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
13075 else
13076 {
13077 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
13078
13079 /* GCC/43521: In java, the formal parameter
13080 "this" is sometimes not marked with DW_AT_artificial. */
13081 if (cu->language == language_java)
13082 {
13083 const char *name = dwarf2_name (child_die, cu);
13084
13085 if (name && !strcmp (name, "this"))
13086 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
13087 }
13088 }
13089 arg_type = die_type (child_die, cu);
13090
13091 /* RealView does not mark THIS as const, which the testsuite
13092 expects. GCC marks THIS as const in method definitions,
13093 but not in the class specifications (GCC PR 43053). */
13094 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
13095 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
13096 {
13097 int is_this = 0;
13098 struct dwarf2_cu *arg_cu = cu;
13099 const char *name = dwarf2_name (child_die, cu);
13100
13101 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
13102 if (attr)
13103 {
13104 /* If the compiler emits this, use it. */
13105 if (follow_die_ref (die, attr, &arg_cu) == child_die)
13106 is_this = 1;
13107 }
13108 else if (name && strcmp (name, "this") == 0)
13109 /* Function definitions will have the argument names. */
13110 is_this = 1;
13111 else if (name == NULL && iparams == 0)
13112 /* Declarations may not have the names, so like
13113 elsewhere in GDB, assume an artificial first
13114 argument is "this". */
13115 is_this = 1;
13116
13117 if (is_this)
13118 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
13119 arg_type, 0);
13120 }
13121
13122 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
13123 iparams++;
13124 }
13125 child_die = sibling_die (child_die);
13126 }
13127 }
13128
13129 return ftype;
13130 }
13131
13132 static struct type *
13133 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
13134 {
13135 struct objfile *objfile = cu->objfile;
13136 const char *name = NULL;
13137 struct type *this_type, *target_type;
13138
13139 name = dwarf2_full_name (NULL, die, cu);
13140 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
13141 TYPE_FLAG_TARGET_STUB, NULL, objfile);
13142 TYPE_NAME (this_type) = name;
13143 set_die_type (die, this_type, cu);
13144 target_type = die_type (die, cu);
13145 if (target_type != this_type)
13146 TYPE_TARGET_TYPE (this_type) = target_type;
13147 else
13148 {
13149 /* Self-referential typedefs are, it seems, not allowed by the DWARF
13150 spec and cause infinite loops in GDB. */
13151 complaint (&symfile_complaints,
13152 _("Self-referential DW_TAG_typedef "
13153 "- DIE at 0x%x [in module %s]"),
13154 die->offset.sect_off, objfile->name);
13155 TYPE_TARGET_TYPE (this_type) = NULL;
13156 }
13157 return this_type;
13158 }
13159
13160 /* Find a representation of a given base type and install
13161 it in the TYPE field of the die. */
13162
13163 static struct type *
13164 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
13165 {
13166 struct objfile *objfile = cu->objfile;
13167 struct type *type;
13168 struct attribute *attr;
13169 int encoding = 0, size = 0;
13170 const char *name;
13171 enum type_code code = TYPE_CODE_INT;
13172 int type_flags = 0;
13173 struct type *target_type = NULL;
13174
13175 attr = dwarf2_attr (die, DW_AT_encoding, cu);
13176 if (attr)
13177 {
13178 encoding = DW_UNSND (attr);
13179 }
13180 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13181 if (attr)
13182 {
13183 size = DW_UNSND (attr);
13184 }
13185 name = dwarf2_name (die, cu);
13186 if (!name)
13187 {
13188 complaint (&symfile_complaints,
13189 _("DW_AT_name missing from DW_TAG_base_type"));
13190 }
13191
13192 switch (encoding)
13193 {
13194 case DW_ATE_address:
13195 /* Turn DW_ATE_address into a void * pointer. */
13196 code = TYPE_CODE_PTR;
13197 type_flags |= TYPE_FLAG_UNSIGNED;
13198 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
13199 break;
13200 case DW_ATE_boolean:
13201 code = TYPE_CODE_BOOL;
13202 type_flags |= TYPE_FLAG_UNSIGNED;
13203 break;
13204 case DW_ATE_complex_float:
13205 code = TYPE_CODE_COMPLEX;
13206 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
13207 break;
13208 case DW_ATE_decimal_float:
13209 code = TYPE_CODE_DECFLOAT;
13210 break;
13211 case DW_ATE_float:
13212 code = TYPE_CODE_FLT;
13213 break;
13214 case DW_ATE_signed:
13215 break;
13216 case DW_ATE_unsigned:
13217 type_flags |= TYPE_FLAG_UNSIGNED;
13218 if (cu->language == language_fortran
13219 && name
13220 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
13221 code = TYPE_CODE_CHAR;
13222 break;
13223 case DW_ATE_signed_char:
13224 if (cu->language == language_ada || cu->language == language_m2
13225 || cu->language == language_pascal
13226 || cu->language == language_fortran)
13227 code = TYPE_CODE_CHAR;
13228 break;
13229 case DW_ATE_unsigned_char:
13230 if (cu->language == language_ada || cu->language == language_m2
13231 || cu->language == language_pascal
13232 || cu->language == language_fortran)
13233 code = TYPE_CODE_CHAR;
13234 type_flags |= TYPE_FLAG_UNSIGNED;
13235 break;
13236 case DW_ATE_UTF:
13237 /* We just treat this as an integer and then recognize the
13238 type by name elsewhere. */
13239 break;
13240
13241 default:
13242 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
13243 dwarf_type_encoding_name (encoding));
13244 break;
13245 }
13246
13247 type = init_type (code, size, type_flags, NULL, objfile);
13248 TYPE_NAME (type) = name;
13249 TYPE_TARGET_TYPE (type) = target_type;
13250
13251 if (name && strcmp (name, "char") == 0)
13252 TYPE_NOSIGN (type) = 1;
13253
13254 return set_die_type (die, type, cu);
13255 }
13256
13257 /* Read the given DW_AT_subrange DIE. */
13258
13259 static struct type *
13260 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
13261 {
13262 struct type *base_type, *orig_base_type;
13263 struct type *range_type;
13264 struct attribute *attr;
13265 LONGEST low, high;
13266 int low_default_is_valid;
13267 const char *name;
13268 LONGEST negative_mask;
13269
13270 orig_base_type = die_type (die, cu);
13271 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
13272 whereas the real type might be. So, we use ORIG_BASE_TYPE when
13273 creating the range type, but we use the result of check_typedef
13274 when examining properties of the type. */
13275 base_type = check_typedef (orig_base_type);
13276
13277 /* The die_type call above may have already set the type for this DIE. */
13278 range_type = get_die_type (die, cu);
13279 if (range_type)
13280 return range_type;
13281
13282 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
13283 omitting DW_AT_lower_bound. */
13284 switch (cu->language)
13285 {
13286 case language_c:
13287 case language_cplus:
13288 low = 0;
13289 low_default_is_valid = 1;
13290 break;
13291 case language_fortran:
13292 low = 1;
13293 low_default_is_valid = 1;
13294 break;
13295 case language_d:
13296 case language_java:
13297 case language_objc:
13298 low = 0;
13299 low_default_is_valid = (cu->header.version >= 4);
13300 break;
13301 case language_ada:
13302 case language_m2:
13303 case language_pascal:
13304 low = 1;
13305 low_default_is_valid = (cu->header.version >= 4);
13306 break;
13307 default:
13308 low = 0;
13309 low_default_is_valid = 0;
13310 break;
13311 }
13312
13313 /* FIXME: For variable sized arrays either of these could be
13314 a variable rather than a constant value. We'll allow it,
13315 but we don't know how to handle it. */
13316 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
13317 if (attr)
13318 low = dwarf2_get_attr_constant_value (attr, low);
13319 else if (!low_default_is_valid)
13320 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
13321 "- DIE at 0x%x [in module %s]"),
13322 die->offset.sect_off, cu->objfile->name);
13323
13324 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
13325 if (attr)
13326 {
13327 if (attr_form_is_block (attr) || is_ref_attr (attr))
13328 {
13329 /* GCC encodes arrays with unspecified or dynamic length
13330 with a DW_FORM_block1 attribute or a reference attribute.
13331 FIXME: GDB does not yet know how to handle dynamic
13332 arrays properly, treat them as arrays with unspecified
13333 length for now.
13334
13335 FIXME: jimb/2003-09-22: GDB does not really know
13336 how to handle arrays of unspecified length
13337 either; we just represent them as zero-length
13338 arrays. Choose an appropriate upper bound given
13339 the lower bound we've computed above. */
13340 high = low - 1;
13341 }
13342 else
13343 high = dwarf2_get_attr_constant_value (attr, 1);
13344 }
13345 else
13346 {
13347 attr = dwarf2_attr (die, DW_AT_count, cu);
13348 if (attr)
13349 {
13350 int count = dwarf2_get_attr_constant_value (attr, 1);
13351 high = low + count - 1;
13352 }
13353 else
13354 {
13355 /* Unspecified array length. */
13356 high = low - 1;
13357 }
13358 }
13359
13360 /* Dwarf-2 specifications explicitly allows to create subrange types
13361 without specifying a base type.
13362 In that case, the base type must be set to the type of
13363 the lower bound, upper bound or count, in that order, if any of these
13364 three attributes references an object that has a type.
13365 If no base type is found, the Dwarf-2 specifications say that
13366 a signed integer type of size equal to the size of an address should
13367 be used.
13368 For the following C code: `extern char gdb_int [];'
13369 GCC produces an empty range DIE.
13370 FIXME: muller/2010-05-28: Possible references to object for low bound,
13371 high bound or count are not yet handled by this code. */
13372 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
13373 {
13374 struct objfile *objfile = cu->objfile;
13375 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13376 int addr_size = gdbarch_addr_bit (gdbarch) /8;
13377 struct type *int_type = objfile_type (objfile)->builtin_int;
13378
13379 /* Test "int", "long int", and "long long int" objfile types,
13380 and select the first one having a size above or equal to the
13381 architecture address size. */
13382 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13383 base_type = int_type;
13384 else
13385 {
13386 int_type = objfile_type (objfile)->builtin_long;
13387 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13388 base_type = int_type;
13389 else
13390 {
13391 int_type = objfile_type (objfile)->builtin_long_long;
13392 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
13393 base_type = int_type;
13394 }
13395 }
13396 }
13397
13398 negative_mask =
13399 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
13400 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
13401 low |= negative_mask;
13402 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
13403 high |= negative_mask;
13404
13405 range_type = create_range_type (NULL, orig_base_type, low, high);
13406
13407 /* Mark arrays with dynamic length at least as an array of unspecified
13408 length. GDB could check the boundary but before it gets implemented at
13409 least allow accessing the array elements. */
13410 if (attr && attr_form_is_block (attr))
13411 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13412
13413 /* Ada expects an empty array on no boundary attributes. */
13414 if (attr == NULL && cu->language != language_ada)
13415 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
13416
13417 name = dwarf2_name (die, cu);
13418 if (name)
13419 TYPE_NAME (range_type) = name;
13420
13421 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13422 if (attr)
13423 TYPE_LENGTH (range_type) = DW_UNSND (attr);
13424
13425 set_die_type (die, range_type, cu);
13426
13427 /* set_die_type should be already done. */
13428 set_descriptive_type (range_type, die, cu);
13429
13430 return range_type;
13431 }
13432
13433 static struct type *
13434 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
13435 {
13436 struct type *type;
13437
13438 /* For now, we only support the C meaning of an unspecified type: void. */
13439
13440 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
13441 TYPE_NAME (type) = dwarf2_name (die, cu);
13442
13443 return set_die_type (die, type, cu);
13444 }
13445
13446 /* Read a single die and all its descendents. Set the die's sibling
13447 field to NULL; set other fields in the die correctly, and set all
13448 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
13449 location of the info_ptr after reading all of those dies. PARENT
13450 is the parent of the die in question. */
13451
13452 static struct die_info *
13453 read_die_and_children (const struct die_reader_specs *reader,
13454 const gdb_byte *info_ptr,
13455 const gdb_byte **new_info_ptr,
13456 struct die_info *parent)
13457 {
13458 struct die_info *die;
13459 const gdb_byte *cur_ptr;
13460 int has_children;
13461
13462 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
13463 if (die == NULL)
13464 {
13465 *new_info_ptr = cur_ptr;
13466 return NULL;
13467 }
13468 store_in_ref_table (die, reader->cu);
13469
13470 if (has_children)
13471 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
13472 else
13473 {
13474 die->child = NULL;
13475 *new_info_ptr = cur_ptr;
13476 }
13477
13478 die->sibling = NULL;
13479 die->parent = parent;
13480 return die;
13481 }
13482
13483 /* Read a die, all of its descendents, and all of its siblings; set
13484 all of the fields of all of the dies correctly. Arguments are as
13485 in read_die_and_children. */
13486
13487 static struct die_info *
13488 read_die_and_siblings_1 (const struct die_reader_specs *reader,
13489 const gdb_byte *info_ptr,
13490 const gdb_byte **new_info_ptr,
13491 struct die_info *parent)
13492 {
13493 struct die_info *first_die, *last_sibling;
13494 const gdb_byte *cur_ptr;
13495
13496 cur_ptr = info_ptr;
13497 first_die = last_sibling = NULL;
13498
13499 while (1)
13500 {
13501 struct die_info *die
13502 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
13503
13504 if (die == NULL)
13505 {
13506 *new_info_ptr = cur_ptr;
13507 return first_die;
13508 }
13509
13510 if (!first_die)
13511 first_die = die;
13512 else
13513 last_sibling->sibling = die;
13514
13515 last_sibling = die;
13516 }
13517 }
13518
13519 /* Read a die, all of its descendents, and all of its siblings; set
13520 all of the fields of all of the dies correctly. Arguments are as
13521 in read_die_and_children.
13522 This the main entry point for reading a DIE and all its children. */
13523
13524 static struct die_info *
13525 read_die_and_siblings (const struct die_reader_specs *reader,
13526 const gdb_byte *info_ptr,
13527 const gdb_byte **new_info_ptr,
13528 struct die_info *parent)
13529 {
13530 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
13531 new_info_ptr, parent);
13532
13533 if (dwarf2_die_debug)
13534 {
13535 fprintf_unfiltered (gdb_stdlog,
13536 "Read die from %s@0x%x of %s:\n",
13537 bfd_section_name (reader->abfd,
13538 reader->die_section->asection),
13539 (unsigned) (info_ptr - reader->die_section->buffer),
13540 bfd_get_filename (reader->abfd));
13541 dump_die (die, dwarf2_die_debug);
13542 }
13543
13544 return die;
13545 }
13546
13547 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
13548 attributes.
13549 The caller is responsible for filling in the extra attributes
13550 and updating (*DIEP)->num_attrs.
13551 Set DIEP to point to a newly allocated die with its information,
13552 except for its child, sibling, and parent fields.
13553 Set HAS_CHILDREN to tell whether the die has children or not. */
13554
13555 static const gdb_byte *
13556 read_full_die_1 (const struct die_reader_specs *reader,
13557 struct die_info **diep, const gdb_byte *info_ptr,
13558 int *has_children, int num_extra_attrs)
13559 {
13560 unsigned int abbrev_number, bytes_read, i;
13561 sect_offset offset;
13562 struct abbrev_info *abbrev;
13563 struct die_info *die;
13564 struct dwarf2_cu *cu = reader->cu;
13565 bfd *abfd = reader->abfd;
13566
13567 offset.sect_off = info_ptr - reader->buffer;
13568 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
13569 info_ptr += bytes_read;
13570 if (!abbrev_number)
13571 {
13572 *diep = NULL;
13573 *has_children = 0;
13574 return info_ptr;
13575 }
13576
13577 abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
13578 if (!abbrev)
13579 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
13580 abbrev_number,
13581 bfd_get_filename (abfd));
13582
13583 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
13584 die->offset = offset;
13585 die->tag = abbrev->tag;
13586 die->abbrev = abbrev_number;
13587
13588 /* Make the result usable.
13589 The caller needs to update num_attrs after adding the extra
13590 attributes. */
13591 die->num_attrs = abbrev->num_attrs;
13592
13593 for (i = 0; i < abbrev->num_attrs; ++i)
13594 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
13595 info_ptr);
13596
13597 *diep = die;
13598 *has_children = abbrev->has_children;
13599 return info_ptr;
13600 }
13601
13602 /* Read a die and all its attributes.
13603 Set DIEP to point to a newly allocated die with its information,
13604 except for its child, sibling, and parent fields.
13605 Set HAS_CHILDREN to tell whether the die has children or not. */
13606
13607 static const gdb_byte *
13608 read_full_die (const struct die_reader_specs *reader,
13609 struct die_info **diep, const gdb_byte *info_ptr,
13610 int *has_children)
13611 {
13612 const gdb_byte *result;
13613
13614 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
13615
13616 if (dwarf2_die_debug)
13617 {
13618 fprintf_unfiltered (gdb_stdlog,
13619 "Read die from %s@0x%x of %s:\n",
13620 bfd_section_name (reader->abfd,
13621 reader->die_section->asection),
13622 (unsigned) (info_ptr - reader->die_section->buffer),
13623 bfd_get_filename (reader->abfd));
13624 dump_die (*diep, dwarf2_die_debug);
13625 }
13626
13627 return result;
13628 }
13629 \f
13630 /* Abbreviation tables.
13631
13632 In DWARF version 2, the description of the debugging information is
13633 stored in a separate .debug_abbrev section. Before we read any
13634 dies from a section we read in all abbreviations and install them
13635 in a hash table. */
13636
13637 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
13638
13639 static struct abbrev_info *
13640 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
13641 {
13642 struct abbrev_info *abbrev;
13643
13644 abbrev = (struct abbrev_info *)
13645 obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
13646 memset (abbrev, 0, sizeof (struct abbrev_info));
13647 return abbrev;
13648 }
13649
13650 /* Add an abbreviation to the table. */
13651
13652 static void
13653 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
13654 unsigned int abbrev_number,
13655 struct abbrev_info *abbrev)
13656 {
13657 unsigned int hash_number;
13658
13659 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13660 abbrev->next = abbrev_table->abbrevs[hash_number];
13661 abbrev_table->abbrevs[hash_number] = abbrev;
13662 }
13663
13664 /* Look up an abbrev in the table.
13665 Returns NULL if the abbrev is not found. */
13666
13667 static struct abbrev_info *
13668 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
13669 unsigned int abbrev_number)
13670 {
13671 unsigned int hash_number;
13672 struct abbrev_info *abbrev;
13673
13674 hash_number = abbrev_number % ABBREV_HASH_SIZE;
13675 abbrev = abbrev_table->abbrevs[hash_number];
13676
13677 while (abbrev)
13678 {
13679 if (abbrev->number == abbrev_number)
13680 return abbrev;
13681 abbrev = abbrev->next;
13682 }
13683 return NULL;
13684 }
13685
13686 /* Read in an abbrev table. */
13687
13688 static struct abbrev_table *
13689 abbrev_table_read_table (struct dwarf2_section_info *section,
13690 sect_offset offset)
13691 {
13692 struct objfile *objfile = dwarf2_per_objfile->objfile;
13693 bfd *abfd = section->asection->owner;
13694 struct abbrev_table *abbrev_table;
13695 const gdb_byte *abbrev_ptr;
13696 struct abbrev_info *cur_abbrev;
13697 unsigned int abbrev_number, bytes_read, abbrev_name;
13698 unsigned int abbrev_form;
13699 struct attr_abbrev *cur_attrs;
13700 unsigned int allocated_attrs;
13701
13702 abbrev_table = XMALLOC (struct abbrev_table);
13703 abbrev_table->offset = offset;
13704 obstack_init (&abbrev_table->abbrev_obstack);
13705 abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
13706 (ABBREV_HASH_SIZE
13707 * sizeof (struct abbrev_info *)));
13708 memset (abbrev_table->abbrevs, 0,
13709 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
13710
13711 dwarf2_read_section (objfile, section);
13712 abbrev_ptr = section->buffer + offset.sect_off;
13713 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13714 abbrev_ptr += bytes_read;
13715
13716 allocated_attrs = ATTR_ALLOC_CHUNK;
13717 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
13718
13719 /* Loop until we reach an abbrev number of 0. */
13720 while (abbrev_number)
13721 {
13722 cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
13723
13724 /* read in abbrev header */
13725 cur_abbrev->number = abbrev_number;
13726 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13727 abbrev_ptr += bytes_read;
13728 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
13729 abbrev_ptr += 1;
13730
13731 /* now read in declarations */
13732 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13733 abbrev_ptr += bytes_read;
13734 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13735 abbrev_ptr += bytes_read;
13736 while (abbrev_name)
13737 {
13738 if (cur_abbrev->num_attrs == allocated_attrs)
13739 {
13740 allocated_attrs += ATTR_ALLOC_CHUNK;
13741 cur_attrs
13742 = xrealloc (cur_attrs, (allocated_attrs
13743 * sizeof (struct attr_abbrev)));
13744 }
13745
13746 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
13747 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
13748 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13749 abbrev_ptr += bytes_read;
13750 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13751 abbrev_ptr += bytes_read;
13752 }
13753
13754 cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
13755 (cur_abbrev->num_attrs
13756 * sizeof (struct attr_abbrev)));
13757 memcpy (cur_abbrev->attrs, cur_attrs,
13758 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
13759
13760 abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
13761
13762 /* Get next abbreviation.
13763 Under Irix6 the abbreviations for a compilation unit are not
13764 always properly terminated with an abbrev number of 0.
13765 Exit loop if we encounter an abbreviation which we have
13766 already read (which means we are about to read the abbreviations
13767 for the next compile unit) or if the end of the abbreviation
13768 table is reached. */
13769 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
13770 break;
13771 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
13772 abbrev_ptr += bytes_read;
13773 if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
13774 break;
13775 }
13776
13777 xfree (cur_attrs);
13778 return abbrev_table;
13779 }
13780
13781 /* Free the resources held by ABBREV_TABLE. */
13782
13783 static void
13784 abbrev_table_free (struct abbrev_table *abbrev_table)
13785 {
13786 obstack_free (&abbrev_table->abbrev_obstack, NULL);
13787 xfree (abbrev_table);
13788 }
13789
13790 /* Same as abbrev_table_free but as a cleanup.
13791 We pass in a pointer to the pointer to the table so that we can
13792 set the pointer to NULL when we're done. It also simplifies
13793 build_type_unit_groups. */
13794
13795 static void
13796 abbrev_table_free_cleanup (void *table_ptr)
13797 {
13798 struct abbrev_table **abbrev_table_ptr = table_ptr;
13799
13800 if (*abbrev_table_ptr != NULL)
13801 abbrev_table_free (*abbrev_table_ptr);
13802 *abbrev_table_ptr = NULL;
13803 }
13804
13805 /* Read the abbrev table for CU from ABBREV_SECTION. */
13806
13807 static void
13808 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
13809 struct dwarf2_section_info *abbrev_section)
13810 {
13811 cu->abbrev_table =
13812 abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
13813 }
13814
13815 /* Release the memory used by the abbrev table for a compilation unit. */
13816
13817 static void
13818 dwarf2_free_abbrev_table (void *ptr_to_cu)
13819 {
13820 struct dwarf2_cu *cu = ptr_to_cu;
13821
13822 if (cu->abbrev_table != NULL)
13823 abbrev_table_free (cu->abbrev_table);
13824 /* Set this to NULL so that we SEGV if we try to read it later,
13825 and also because free_comp_unit verifies this is NULL. */
13826 cu->abbrev_table = NULL;
13827 }
13828 \f
13829 /* Returns nonzero if TAG represents a type that we might generate a partial
13830 symbol for. */
13831
13832 static int
13833 is_type_tag_for_partial (int tag)
13834 {
13835 switch (tag)
13836 {
13837 #if 0
13838 /* Some types that would be reasonable to generate partial symbols for,
13839 that we don't at present. */
13840 case DW_TAG_array_type:
13841 case DW_TAG_file_type:
13842 case DW_TAG_ptr_to_member_type:
13843 case DW_TAG_set_type:
13844 case DW_TAG_string_type:
13845 case DW_TAG_subroutine_type:
13846 #endif
13847 case DW_TAG_base_type:
13848 case DW_TAG_class_type:
13849 case DW_TAG_interface_type:
13850 case DW_TAG_enumeration_type:
13851 case DW_TAG_structure_type:
13852 case DW_TAG_subrange_type:
13853 case DW_TAG_typedef:
13854 case DW_TAG_union_type:
13855 return 1;
13856 default:
13857 return 0;
13858 }
13859 }
13860
13861 /* Load all DIEs that are interesting for partial symbols into memory. */
13862
13863 static struct partial_die_info *
13864 load_partial_dies (const struct die_reader_specs *reader,
13865 const gdb_byte *info_ptr, int building_psymtab)
13866 {
13867 struct dwarf2_cu *cu = reader->cu;
13868 struct objfile *objfile = cu->objfile;
13869 struct partial_die_info *part_die;
13870 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
13871 struct abbrev_info *abbrev;
13872 unsigned int bytes_read;
13873 unsigned int load_all = 0;
13874 int nesting_level = 1;
13875
13876 parent_die = NULL;
13877 last_die = NULL;
13878
13879 gdb_assert (cu->per_cu != NULL);
13880 if (cu->per_cu->load_all_dies)
13881 load_all = 1;
13882
13883 cu->partial_dies
13884 = htab_create_alloc_ex (cu->header.length / 12,
13885 partial_die_hash,
13886 partial_die_eq,
13887 NULL,
13888 &cu->comp_unit_obstack,
13889 hashtab_obstack_allocate,
13890 dummy_obstack_deallocate);
13891
13892 part_die = obstack_alloc (&cu->comp_unit_obstack,
13893 sizeof (struct partial_die_info));
13894
13895 while (1)
13896 {
13897 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
13898
13899 /* A NULL abbrev means the end of a series of children. */
13900 if (abbrev == NULL)
13901 {
13902 if (--nesting_level == 0)
13903 {
13904 /* PART_DIE was probably the last thing allocated on the
13905 comp_unit_obstack, so we could call obstack_free
13906 here. We don't do that because the waste is small,
13907 and will be cleaned up when we're done with this
13908 compilation unit. This way, we're also more robust
13909 against other users of the comp_unit_obstack. */
13910 return first_die;
13911 }
13912 info_ptr += bytes_read;
13913 last_die = parent_die;
13914 parent_die = parent_die->die_parent;
13915 continue;
13916 }
13917
13918 /* Check for template arguments. We never save these; if
13919 they're seen, we just mark the parent, and go on our way. */
13920 if (parent_die != NULL
13921 && cu->language == language_cplus
13922 && (abbrev->tag == DW_TAG_template_type_param
13923 || abbrev->tag == DW_TAG_template_value_param))
13924 {
13925 parent_die->has_template_arguments = 1;
13926
13927 if (!load_all)
13928 {
13929 /* We don't need a partial DIE for the template argument. */
13930 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13931 continue;
13932 }
13933 }
13934
13935 /* We only recurse into c++ subprograms looking for template arguments.
13936 Skip their other children. */
13937 if (!load_all
13938 && cu->language == language_cplus
13939 && parent_die != NULL
13940 && parent_die->tag == DW_TAG_subprogram)
13941 {
13942 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13943 continue;
13944 }
13945
13946 /* Check whether this DIE is interesting enough to save. Normally
13947 we would not be interested in members here, but there may be
13948 later variables referencing them via DW_AT_specification (for
13949 static members). */
13950 if (!load_all
13951 && !is_type_tag_for_partial (abbrev->tag)
13952 && abbrev->tag != DW_TAG_constant
13953 && abbrev->tag != DW_TAG_enumerator
13954 && abbrev->tag != DW_TAG_subprogram
13955 && abbrev->tag != DW_TAG_lexical_block
13956 && abbrev->tag != DW_TAG_variable
13957 && abbrev->tag != DW_TAG_namespace
13958 && abbrev->tag != DW_TAG_module
13959 && abbrev->tag != DW_TAG_member
13960 && abbrev->tag != DW_TAG_imported_unit)
13961 {
13962 /* Otherwise we skip to the next sibling, if any. */
13963 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
13964 continue;
13965 }
13966
13967 info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
13968 info_ptr);
13969
13970 /* This two-pass algorithm for processing partial symbols has a
13971 high cost in cache pressure. Thus, handle some simple cases
13972 here which cover the majority of C partial symbols. DIEs
13973 which neither have specification tags in them, nor could have
13974 specification tags elsewhere pointing at them, can simply be
13975 processed and discarded.
13976
13977 This segment is also optional; scan_partial_symbols and
13978 add_partial_symbol will handle these DIEs if we chain
13979 them in normally. When compilers which do not emit large
13980 quantities of duplicate debug information are more common,
13981 this code can probably be removed. */
13982
13983 /* Any complete simple types at the top level (pretty much all
13984 of them, for a language without namespaces), can be processed
13985 directly. */
13986 if (parent_die == NULL
13987 && part_die->has_specification == 0
13988 && part_die->is_declaration == 0
13989 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
13990 || part_die->tag == DW_TAG_base_type
13991 || part_die->tag == DW_TAG_subrange_type))
13992 {
13993 if (building_psymtab && part_die->name != NULL)
13994 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
13995 VAR_DOMAIN, LOC_TYPEDEF,
13996 &objfile->static_psymbols,
13997 0, (CORE_ADDR) 0, cu->language, objfile);
13998 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
13999 continue;
14000 }
14001
14002 /* The exception for DW_TAG_typedef with has_children above is
14003 a workaround of GCC PR debug/47510. In the case of this complaint
14004 type_name_no_tag_or_error will error on such types later.
14005
14006 GDB skipped children of DW_TAG_typedef by the shortcut above and then
14007 it could not find the child DIEs referenced later, this is checked
14008 above. In correct DWARF DW_TAG_typedef should have no children. */
14009
14010 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
14011 complaint (&symfile_complaints,
14012 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
14013 "- DIE at 0x%x [in module %s]"),
14014 part_die->offset.sect_off, objfile->name);
14015
14016 /* If we're at the second level, and we're an enumerator, and
14017 our parent has no specification (meaning possibly lives in a
14018 namespace elsewhere), then we can add the partial symbol now
14019 instead of queueing it. */
14020 if (part_die->tag == DW_TAG_enumerator
14021 && parent_die != NULL
14022 && parent_die->die_parent == NULL
14023 && parent_die->tag == DW_TAG_enumeration_type
14024 && parent_die->has_specification == 0)
14025 {
14026 if (part_die->name == NULL)
14027 complaint (&symfile_complaints,
14028 _("malformed enumerator DIE ignored"));
14029 else if (building_psymtab)
14030 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
14031 VAR_DOMAIN, LOC_CONST,
14032 (cu->language == language_cplus
14033 || cu->language == language_java)
14034 ? &objfile->global_psymbols
14035 : &objfile->static_psymbols,
14036 0, (CORE_ADDR) 0, cu->language, objfile);
14037
14038 info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
14039 continue;
14040 }
14041
14042 /* We'll save this DIE so link it in. */
14043 part_die->die_parent = parent_die;
14044 part_die->die_sibling = NULL;
14045 part_die->die_child = NULL;
14046
14047 if (last_die && last_die == parent_die)
14048 last_die->die_child = part_die;
14049 else if (last_die)
14050 last_die->die_sibling = part_die;
14051
14052 last_die = part_die;
14053
14054 if (first_die == NULL)
14055 first_die = part_die;
14056
14057 /* Maybe add the DIE to the hash table. Not all DIEs that we
14058 find interesting need to be in the hash table, because we
14059 also have the parent/sibling/child chains; only those that we
14060 might refer to by offset later during partial symbol reading.
14061
14062 For now this means things that might have be the target of a
14063 DW_AT_specification, DW_AT_abstract_origin, or
14064 DW_AT_extension. DW_AT_extension will refer only to
14065 namespaces; DW_AT_abstract_origin refers to functions (and
14066 many things under the function DIE, but we do not recurse
14067 into function DIEs during partial symbol reading) and
14068 possibly variables as well; DW_AT_specification refers to
14069 declarations. Declarations ought to have the DW_AT_declaration
14070 flag. It happens that GCC forgets to put it in sometimes, but
14071 only for functions, not for types.
14072
14073 Adding more things than necessary to the hash table is harmless
14074 except for the performance cost. Adding too few will result in
14075 wasted time in find_partial_die, when we reread the compilation
14076 unit with load_all_dies set. */
14077
14078 if (load_all
14079 || abbrev->tag == DW_TAG_constant
14080 || abbrev->tag == DW_TAG_subprogram
14081 || abbrev->tag == DW_TAG_variable
14082 || abbrev->tag == DW_TAG_namespace
14083 || part_die->is_declaration)
14084 {
14085 void **slot;
14086
14087 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
14088 part_die->offset.sect_off, INSERT);
14089 *slot = part_die;
14090 }
14091
14092 part_die = obstack_alloc (&cu->comp_unit_obstack,
14093 sizeof (struct partial_die_info));
14094
14095 /* For some DIEs we want to follow their children (if any). For C
14096 we have no reason to follow the children of structures; for other
14097 languages we have to, so that we can get at method physnames
14098 to infer fully qualified class names, for DW_AT_specification,
14099 and for C++ template arguments. For C++, we also look one level
14100 inside functions to find template arguments (if the name of the
14101 function does not already contain the template arguments).
14102
14103 For Ada, we need to scan the children of subprograms and lexical
14104 blocks as well because Ada allows the definition of nested
14105 entities that could be interesting for the debugger, such as
14106 nested subprograms for instance. */
14107 if (last_die->has_children
14108 && (load_all
14109 || last_die->tag == DW_TAG_namespace
14110 || last_die->tag == DW_TAG_module
14111 || last_die->tag == DW_TAG_enumeration_type
14112 || (cu->language == language_cplus
14113 && last_die->tag == DW_TAG_subprogram
14114 && (last_die->name == NULL
14115 || strchr (last_die->name, '<') == NULL))
14116 || (cu->language != language_c
14117 && (last_die->tag == DW_TAG_class_type
14118 || last_die->tag == DW_TAG_interface_type
14119 || last_die->tag == DW_TAG_structure_type
14120 || last_die->tag == DW_TAG_union_type))
14121 || (cu->language == language_ada
14122 && (last_die->tag == DW_TAG_subprogram
14123 || last_die->tag == DW_TAG_lexical_block))))
14124 {
14125 nesting_level++;
14126 parent_die = last_die;
14127 continue;
14128 }
14129
14130 /* Otherwise we skip to the next sibling, if any. */
14131 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
14132
14133 /* Back to the top, do it again. */
14134 }
14135 }
14136
14137 /* Read a minimal amount of information into the minimal die structure. */
14138
14139 static const gdb_byte *
14140 read_partial_die (const struct die_reader_specs *reader,
14141 struct partial_die_info *part_die,
14142 struct abbrev_info *abbrev, unsigned int abbrev_len,
14143 const gdb_byte *info_ptr)
14144 {
14145 struct dwarf2_cu *cu = reader->cu;
14146 struct objfile *objfile = cu->objfile;
14147 const gdb_byte *buffer = reader->buffer;
14148 unsigned int i;
14149 struct attribute attr;
14150 int has_low_pc_attr = 0;
14151 int has_high_pc_attr = 0;
14152 int high_pc_relative = 0;
14153
14154 memset (part_die, 0, sizeof (struct partial_die_info));
14155
14156 part_die->offset.sect_off = info_ptr - buffer;
14157
14158 info_ptr += abbrev_len;
14159
14160 if (abbrev == NULL)
14161 return info_ptr;
14162
14163 part_die->tag = abbrev->tag;
14164 part_die->has_children = abbrev->has_children;
14165
14166 for (i = 0; i < abbrev->num_attrs; ++i)
14167 {
14168 info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
14169
14170 /* Store the data if it is of an attribute we want to keep in a
14171 partial symbol table. */
14172 switch (attr.name)
14173 {
14174 case DW_AT_name:
14175 switch (part_die->tag)
14176 {
14177 case DW_TAG_compile_unit:
14178 case DW_TAG_partial_unit:
14179 case DW_TAG_type_unit:
14180 /* Compilation units have a DW_AT_name that is a filename, not
14181 a source language identifier. */
14182 case DW_TAG_enumeration_type:
14183 case DW_TAG_enumerator:
14184 /* These tags always have simple identifiers already; no need
14185 to canonicalize them. */
14186 part_die->name = DW_STRING (&attr);
14187 break;
14188 default:
14189 part_die->name
14190 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
14191 &objfile->objfile_obstack);
14192 break;
14193 }
14194 break;
14195 case DW_AT_linkage_name:
14196 case DW_AT_MIPS_linkage_name:
14197 /* Note that both forms of linkage name might appear. We
14198 assume they will be the same, and we only store the last
14199 one we see. */
14200 if (cu->language == language_ada)
14201 part_die->name = DW_STRING (&attr);
14202 part_die->linkage_name = DW_STRING (&attr);
14203 break;
14204 case DW_AT_low_pc:
14205 has_low_pc_attr = 1;
14206 part_die->lowpc = DW_ADDR (&attr);
14207 break;
14208 case DW_AT_high_pc:
14209 has_high_pc_attr = 1;
14210 if (attr.form == DW_FORM_addr
14211 || attr.form == DW_FORM_GNU_addr_index)
14212 part_die->highpc = DW_ADDR (&attr);
14213 else
14214 {
14215 high_pc_relative = 1;
14216 part_die->highpc = DW_UNSND (&attr);
14217 }
14218 break;
14219 case DW_AT_location:
14220 /* Support the .debug_loc offsets. */
14221 if (attr_form_is_block (&attr))
14222 {
14223 part_die->d.locdesc = DW_BLOCK (&attr);
14224 }
14225 else if (attr_form_is_section_offset (&attr))
14226 {
14227 dwarf2_complex_location_expr_complaint ();
14228 }
14229 else
14230 {
14231 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
14232 "partial symbol information");
14233 }
14234 break;
14235 case DW_AT_external:
14236 part_die->is_external = DW_UNSND (&attr);
14237 break;
14238 case DW_AT_declaration:
14239 part_die->is_declaration = DW_UNSND (&attr);
14240 break;
14241 case DW_AT_type:
14242 part_die->has_type = 1;
14243 break;
14244 case DW_AT_abstract_origin:
14245 case DW_AT_specification:
14246 case DW_AT_extension:
14247 part_die->has_specification = 1;
14248 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
14249 part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14250 || cu->per_cu->is_dwz);
14251 break;
14252 case DW_AT_sibling:
14253 /* Ignore absolute siblings, they might point outside of
14254 the current compile unit. */
14255 if (attr.form == DW_FORM_ref_addr)
14256 complaint (&symfile_complaints,
14257 _("ignoring absolute DW_AT_sibling"));
14258 else
14259 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
14260 break;
14261 case DW_AT_byte_size:
14262 part_die->has_byte_size = 1;
14263 break;
14264 case DW_AT_calling_convention:
14265 /* DWARF doesn't provide a way to identify a program's source-level
14266 entry point. DW_AT_calling_convention attributes are only meant
14267 to describe functions' calling conventions.
14268
14269 However, because it's a necessary piece of information in
14270 Fortran, and because DW_CC_program is the only piece of debugging
14271 information whose definition refers to a 'main program' at all,
14272 several compilers have begun marking Fortran main programs with
14273 DW_CC_program --- even when those functions use the standard
14274 calling conventions.
14275
14276 So until DWARF specifies a way to provide this information and
14277 compilers pick up the new representation, we'll support this
14278 practice. */
14279 if (DW_UNSND (&attr) == DW_CC_program
14280 && cu->language == language_fortran)
14281 {
14282 set_main_name (part_die->name);
14283
14284 /* As this DIE has a static linkage the name would be difficult
14285 to look up later. */
14286 language_of_main = language_fortran;
14287 }
14288 break;
14289 case DW_AT_inline:
14290 if (DW_UNSND (&attr) == DW_INL_inlined
14291 || DW_UNSND (&attr) == DW_INL_declared_inlined)
14292 part_die->may_be_inlined = 1;
14293 break;
14294
14295 case DW_AT_import:
14296 if (part_die->tag == DW_TAG_imported_unit)
14297 {
14298 part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
14299 part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
14300 || cu->per_cu->is_dwz);
14301 }
14302 break;
14303
14304 default:
14305 break;
14306 }
14307 }
14308
14309 if (high_pc_relative)
14310 part_die->highpc += part_die->lowpc;
14311
14312 if (has_low_pc_attr && has_high_pc_attr)
14313 {
14314 /* When using the GNU linker, .gnu.linkonce. sections are used to
14315 eliminate duplicate copies of functions and vtables and such.
14316 The linker will arbitrarily choose one and discard the others.
14317 The AT_*_pc values for such functions refer to local labels in
14318 these sections. If the section from that file was discarded, the
14319 labels are not in the output, so the relocs get a value of 0.
14320 If this is a discarded function, mark the pc bounds as invalid,
14321 so that GDB will ignore it. */
14322 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
14323 {
14324 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14325
14326 complaint (&symfile_complaints,
14327 _("DW_AT_low_pc %s is zero "
14328 "for DIE at 0x%x [in module %s]"),
14329 paddress (gdbarch, part_die->lowpc),
14330 part_die->offset.sect_off, objfile->name);
14331 }
14332 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
14333 else if (part_die->lowpc >= part_die->highpc)
14334 {
14335 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14336
14337 complaint (&symfile_complaints,
14338 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
14339 "for DIE at 0x%x [in module %s]"),
14340 paddress (gdbarch, part_die->lowpc),
14341 paddress (gdbarch, part_die->highpc),
14342 part_die->offset.sect_off, objfile->name);
14343 }
14344 else
14345 part_die->has_pc_info = 1;
14346 }
14347
14348 return info_ptr;
14349 }
14350
14351 /* Find a cached partial DIE at OFFSET in CU. */
14352
14353 static struct partial_die_info *
14354 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
14355 {
14356 struct partial_die_info *lookup_die = NULL;
14357 struct partial_die_info part_die;
14358
14359 part_die.offset = offset;
14360 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
14361 offset.sect_off);
14362
14363 return lookup_die;
14364 }
14365
14366 /* Find a partial DIE at OFFSET, which may or may not be in CU,
14367 except in the case of .debug_types DIEs which do not reference
14368 outside their CU (they do however referencing other types via
14369 DW_FORM_ref_sig8). */
14370
14371 static struct partial_die_info *
14372 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
14373 {
14374 struct objfile *objfile = cu->objfile;
14375 struct dwarf2_per_cu_data *per_cu = NULL;
14376 struct partial_die_info *pd = NULL;
14377
14378 if (offset_in_dwz == cu->per_cu->is_dwz
14379 && offset_in_cu_p (&cu->header, offset))
14380 {
14381 pd = find_partial_die_in_comp_unit (offset, cu);
14382 if (pd != NULL)
14383 return pd;
14384 /* We missed recording what we needed.
14385 Load all dies and try again. */
14386 per_cu = cu->per_cu;
14387 }
14388 else
14389 {
14390 /* TUs don't reference other CUs/TUs (except via type signatures). */
14391 if (cu->per_cu->is_debug_types)
14392 {
14393 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
14394 " external reference to offset 0x%lx [in module %s].\n"),
14395 (long) cu->header.offset.sect_off, (long) offset.sect_off,
14396 bfd_get_filename (objfile->obfd));
14397 }
14398 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
14399 objfile);
14400
14401 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
14402 load_partial_comp_unit (per_cu);
14403
14404 per_cu->cu->last_used = 0;
14405 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14406 }
14407
14408 /* If we didn't find it, and not all dies have been loaded,
14409 load them all and try again. */
14410
14411 if (pd == NULL && per_cu->load_all_dies == 0)
14412 {
14413 per_cu->load_all_dies = 1;
14414
14415 /* This is nasty. When we reread the DIEs, somewhere up the call chain
14416 THIS_CU->cu may already be in use. So we can't just free it and
14417 replace its DIEs with the ones we read in. Instead, we leave those
14418 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
14419 and clobber THIS_CU->cu->partial_dies with the hash table for the new
14420 set. */
14421 load_partial_comp_unit (per_cu);
14422
14423 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
14424 }
14425
14426 if (pd == NULL)
14427 internal_error (__FILE__, __LINE__,
14428 _("could not find partial DIE 0x%x "
14429 "in cache [from module %s]\n"),
14430 offset.sect_off, bfd_get_filename (objfile->obfd));
14431 return pd;
14432 }
14433
14434 /* See if we can figure out if the class lives in a namespace. We do
14435 this by looking for a member function; its demangled name will
14436 contain namespace info, if there is any. */
14437
14438 static void
14439 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
14440 struct dwarf2_cu *cu)
14441 {
14442 /* NOTE: carlton/2003-10-07: Getting the info this way changes
14443 what template types look like, because the demangler
14444 frequently doesn't give the same name as the debug info. We
14445 could fix this by only using the demangled name to get the
14446 prefix (but see comment in read_structure_type). */
14447
14448 struct partial_die_info *real_pdi;
14449 struct partial_die_info *child_pdi;
14450
14451 /* If this DIE (this DIE's specification, if any) has a parent, then
14452 we should not do this. We'll prepend the parent's fully qualified
14453 name when we create the partial symbol. */
14454
14455 real_pdi = struct_pdi;
14456 while (real_pdi->has_specification)
14457 real_pdi = find_partial_die (real_pdi->spec_offset,
14458 real_pdi->spec_is_dwz, cu);
14459
14460 if (real_pdi->die_parent != NULL)
14461 return;
14462
14463 for (child_pdi = struct_pdi->die_child;
14464 child_pdi != NULL;
14465 child_pdi = child_pdi->die_sibling)
14466 {
14467 if (child_pdi->tag == DW_TAG_subprogram
14468 && child_pdi->linkage_name != NULL)
14469 {
14470 char *actual_class_name
14471 = language_class_name_from_physname (cu->language_defn,
14472 child_pdi->linkage_name);
14473 if (actual_class_name != NULL)
14474 {
14475 struct_pdi->name
14476 = obstack_copy0 (&cu->objfile->objfile_obstack,
14477 actual_class_name,
14478 strlen (actual_class_name));
14479 xfree (actual_class_name);
14480 }
14481 break;
14482 }
14483 }
14484 }
14485
14486 /* Adjust PART_DIE before generating a symbol for it. This function
14487 may set the is_external flag or change the DIE's name. */
14488
14489 static void
14490 fixup_partial_die (struct partial_die_info *part_die,
14491 struct dwarf2_cu *cu)
14492 {
14493 /* Once we've fixed up a die, there's no point in doing so again.
14494 This also avoids a memory leak if we were to call
14495 guess_partial_die_structure_name multiple times. */
14496 if (part_die->fixup_called)
14497 return;
14498
14499 /* If we found a reference attribute and the DIE has no name, try
14500 to find a name in the referred to DIE. */
14501
14502 if (part_die->name == NULL && part_die->has_specification)
14503 {
14504 struct partial_die_info *spec_die;
14505
14506 spec_die = find_partial_die (part_die->spec_offset,
14507 part_die->spec_is_dwz, cu);
14508
14509 fixup_partial_die (spec_die, cu);
14510
14511 if (spec_die->name)
14512 {
14513 part_die->name = spec_die->name;
14514
14515 /* Copy DW_AT_external attribute if it is set. */
14516 if (spec_die->is_external)
14517 part_die->is_external = spec_die->is_external;
14518 }
14519 }
14520
14521 /* Set default names for some unnamed DIEs. */
14522
14523 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
14524 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
14525
14526 /* If there is no parent die to provide a namespace, and there are
14527 children, see if we can determine the namespace from their linkage
14528 name. */
14529 if (cu->language == language_cplus
14530 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
14531 && part_die->die_parent == NULL
14532 && part_die->has_children
14533 && (part_die->tag == DW_TAG_class_type
14534 || part_die->tag == DW_TAG_structure_type
14535 || part_die->tag == DW_TAG_union_type))
14536 guess_partial_die_structure_name (part_die, cu);
14537
14538 /* GCC might emit a nameless struct or union that has a linkage
14539 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
14540 if (part_die->name == NULL
14541 && (part_die->tag == DW_TAG_class_type
14542 || part_die->tag == DW_TAG_interface_type
14543 || part_die->tag == DW_TAG_structure_type
14544 || part_die->tag == DW_TAG_union_type)
14545 && part_die->linkage_name != NULL)
14546 {
14547 char *demangled;
14548
14549 demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
14550 if (demangled)
14551 {
14552 const char *base;
14553
14554 /* Strip any leading namespaces/classes, keep only the base name.
14555 DW_AT_name for named DIEs does not contain the prefixes. */
14556 base = strrchr (demangled, ':');
14557 if (base && base > demangled && base[-1] == ':')
14558 base++;
14559 else
14560 base = demangled;
14561
14562 part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
14563 base, strlen (base));
14564 xfree (demangled);
14565 }
14566 }
14567
14568 part_die->fixup_called = 1;
14569 }
14570
14571 /* Read an attribute value described by an attribute form. */
14572
14573 static const gdb_byte *
14574 read_attribute_value (const struct die_reader_specs *reader,
14575 struct attribute *attr, unsigned form,
14576 const gdb_byte *info_ptr)
14577 {
14578 struct dwarf2_cu *cu = reader->cu;
14579 bfd *abfd = reader->abfd;
14580 struct comp_unit_head *cu_header = &cu->header;
14581 unsigned int bytes_read;
14582 struct dwarf_block *blk;
14583
14584 attr->form = form;
14585 switch (form)
14586 {
14587 case DW_FORM_ref_addr:
14588 if (cu->header.version == 2)
14589 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14590 else
14591 DW_UNSND (attr) = read_offset (abfd, info_ptr,
14592 &cu->header, &bytes_read);
14593 info_ptr += bytes_read;
14594 break;
14595 case DW_FORM_GNU_ref_alt:
14596 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14597 info_ptr += bytes_read;
14598 break;
14599 case DW_FORM_addr:
14600 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
14601 info_ptr += bytes_read;
14602 break;
14603 case DW_FORM_block2:
14604 blk = dwarf_alloc_block (cu);
14605 blk->size = read_2_bytes (abfd, info_ptr);
14606 info_ptr += 2;
14607 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14608 info_ptr += blk->size;
14609 DW_BLOCK (attr) = blk;
14610 break;
14611 case DW_FORM_block4:
14612 blk = dwarf_alloc_block (cu);
14613 blk->size = read_4_bytes (abfd, info_ptr);
14614 info_ptr += 4;
14615 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14616 info_ptr += blk->size;
14617 DW_BLOCK (attr) = blk;
14618 break;
14619 case DW_FORM_data2:
14620 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
14621 info_ptr += 2;
14622 break;
14623 case DW_FORM_data4:
14624 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
14625 info_ptr += 4;
14626 break;
14627 case DW_FORM_data8:
14628 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
14629 info_ptr += 8;
14630 break;
14631 case DW_FORM_sec_offset:
14632 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
14633 info_ptr += bytes_read;
14634 break;
14635 case DW_FORM_string:
14636 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
14637 DW_STRING_IS_CANONICAL (attr) = 0;
14638 info_ptr += bytes_read;
14639 break;
14640 case DW_FORM_strp:
14641 if (!cu->per_cu->is_dwz)
14642 {
14643 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
14644 &bytes_read);
14645 DW_STRING_IS_CANONICAL (attr) = 0;
14646 info_ptr += bytes_read;
14647 break;
14648 }
14649 /* FALLTHROUGH */
14650 case DW_FORM_GNU_strp_alt:
14651 {
14652 struct dwz_file *dwz = dwarf2_get_dwz_file ();
14653 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
14654 &bytes_read);
14655
14656 DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
14657 DW_STRING_IS_CANONICAL (attr) = 0;
14658 info_ptr += bytes_read;
14659 }
14660 break;
14661 case DW_FORM_exprloc:
14662 case DW_FORM_block:
14663 blk = dwarf_alloc_block (cu);
14664 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14665 info_ptr += bytes_read;
14666 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14667 info_ptr += blk->size;
14668 DW_BLOCK (attr) = blk;
14669 break;
14670 case DW_FORM_block1:
14671 blk = dwarf_alloc_block (cu);
14672 blk->size = read_1_byte (abfd, info_ptr);
14673 info_ptr += 1;
14674 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
14675 info_ptr += blk->size;
14676 DW_BLOCK (attr) = blk;
14677 break;
14678 case DW_FORM_data1:
14679 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14680 info_ptr += 1;
14681 break;
14682 case DW_FORM_flag:
14683 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
14684 info_ptr += 1;
14685 break;
14686 case DW_FORM_flag_present:
14687 DW_UNSND (attr) = 1;
14688 break;
14689 case DW_FORM_sdata:
14690 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
14691 info_ptr += bytes_read;
14692 break;
14693 case DW_FORM_udata:
14694 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14695 info_ptr += bytes_read;
14696 break;
14697 case DW_FORM_ref1:
14698 DW_UNSND (attr) = (cu->header.offset.sect_off
14699 + read_1_byte (abfd, info_ptr));
14700 info_ptr += 1;
14701 break;
14702 case DW_FORM_ref2:
14703 DW_UNSND (attr) = (cu->header.offset.sect_off
14704 + read_2_bytes (abfd, info_ptr));
14705 info_ptr += 2;
14706 break;
14707 case DW_FORM_ref4:
14708 DW_UNSND (attr) = (cu->header.offset.sect_off
14709 + read_4_bytes (abfd, info_ptr));
14710 info_ptr += 4;
14711 break;
14712 case DW_FORM_ref8:
14713 DW_UNSND (attr) = (cu->header.offset.sect_off
14714 + read_8_bytes (abfd, info_ptr));
14715 info_ptr += 8;
14716 break;
14717 case DW_FORM_ref_sig8:
14718 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
14719 info_ptr += 8;
14720 break;
14721 case DW_FORM_ref_udata:
14722 DW_UNSND (attr) = (cu->header.offset.sect_off
14723 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
14724 info_ptr += bytes_read;
14725 break;
14726 case DW_FORM_indirect:
14727 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14728 info_ptr += bytes_read;
14729 info_ptr = read_attribute_value (reader, attr, form, info_ptr);
14730 break;
14731 case DW_FORM_GNU_addr_index:
14732 if (reader->dwo_file == NULL)
14733 {
14734 /* For now flag a hard error.
14735 Later we can turn this into a complaint. */
14736 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14737 dwarf_form_name (form),
14738 bfd_get_filename (abfd));
14739 }
14740 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
14741 info_ptr += bytes_read;
14742 break;
14743 case DW_FORM_GNU_str_index:
14744 if (reader->dwo_file == NULL)
14745 {
14746 /* For now flag a hard error.
14747 Later we can turn this into a complaint if warranted. */
14748 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
14749 dwarf_form_name (form),
14750 bfd_get_filename (abfd));
14751 }
14752 {
14753 ULONGEST str_index =
14754 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14755
14756 DW_STRING (attr) = read_str_index (reader, cu, str_index);
14757 DW_STRING_IS_CANONICAL (attr) = 0;
14758 info_ptr += bytes_read;
14759 }
14760 break;
14761 default:
14762 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
14763 dwarf_form_name (form),
14764 bfd_get_filename (abfd));
14765 }
14766
14767 /* Super hack. */
14768 if (cu->per_cu->is_dwz && is_ref_attr (attr))
14769 attr->form = DW_FORM_GNU_ref_alt;
14770
14771 /* We have seen instances where the compiler tried to emit a byte
14772 size attribute of -1 which ended up being encoded as an unsigned
14773 0xffffffff. Although 0xffffffff is technically a valid size value,
14774 an object of this size seems pretty unlikely so we can relatively
14775 safely treat these cases as if the size attribute was invalid and
14776 treat them as zero by default. */
14777 if (attr->name == DW_AT_byte_size
14778 && form == DW_FORM_data4
14779 && DW_UNSND (attr) >= 0xffffffff)
14780 {
14781 complaint
14782 (&symfile_complaints,
14783 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
14784 hex_string (DW_UNSND (attr)));
14785 DW_UNSND (attr) = 0;
14786 }
14787
14788 return info_ptr;
14789 }
14790
14791 /* Read an attribute described by an abbreviated attribute. */
14792
14793 static const gdb_byte *
14794 read_attribute (const struct die_reader_specs *reader,
14795 struct attribute *attr, struct attr_abbrev *abbrev,
14796 const gdb_byte *info_ptr)
14797 {
14798 attr->name = abbrev->name;
14799 return read_attribute_value (reader, attr, abbrev->form, info_ptr);
14800 }
14801
14802 /* Read dwarf information from a buffer. */
14803
14804 static unsigned int
14805 read_1_byte (bfd *abfd, const gdb_byte *buf)
14806 {
14807 return bfd_get_8 (abfd, buf);
14808 }
14809
14810 static int
14811 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
14812 {
14813 return bfd_get_signed_8 (abfd, buf);
14814 }
14815
14816 static unsigned int
14817 read_2_bytes (bfd *abfd, const gdb_byte *buf)
14818 {
14819 return bfd_get_16 (abfd, buf);
14820 }
14821
14822 static int
14823 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
14824 {
14825 return bfd_get_signed_16 (abfd, buf);
14826 }
14827
14828 static unsigned int
14829 read_4_bytes (bfd *abfd, const gdb_byte *buf)
14830 {
14831 return bfd_get_32 (abfd, buf);
14832 }
14833
14834 static int
14835 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
14836 {
14837 return bfd_get_signed_32 (abfd, buf);
14838 }
14839
14840 static ULONGEST
14841 read_8_bytes (bfd *abfd, const gdb_byte *buf)
14842 {
14843 return bfd_get_64 (abfd, buf);
14844 }
14845
14846 static CORE_ADDR
14847 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
14848 unsigned int *bytes_read)
14849 {
14850 struct comp_unit_head *cu_header = &cu->header;
14851 CORE_ADDR retval = 0;
14852
14853 if (cu_header->signed_addr_p)
14854 {
14855 switch (cu_header->addr_size)
14856 {
14857 case 2:
14858 retval = bfd_get_signed_16 (abfd, buf);
14859 break;
14860 case 4:
14861 retval = bfd_get_signed_32 (abfd, buf);
14862 break;
14863 case 8:
14864 retval = bfd_get_signed_64 (abfd, buf);
14865 break;
14866 default:
14867 internal_error (__FILE__, __LINE__,
14868 _("read_address: bad switch, signed [in module %s]"),
14869 bfd_get_filename (abfd));
14870 }
14871 }
14872 else
14873 {
14874 switch (cu_header->addr_size)
14875 {
14876 case 2:
14877 retval = bfd_get_16 (abfd, buf);
14878 break;
14879 case 4:
14880 retval = bfd_get_32 (abfd, buf);
14881 break;
14882 case 8:
14883 retval = bfd_get_64 (abfd, buf);
14884 break;
14885 default:
14886 internal_error (__FILE__, __LINE__,
14887 _("read_address: bad switch, "
14888 "unsigned [in module %s]"),
14889 bfd_get_filename (abfd));
14890 }
14891 }
14892
14893 *bytes_read = cu_header->addr_size;
14894 return retval;
14895 }
14896
14897 /* Read the initial length from a section. The (draft) DWARF 3
14898 specification allows the initial length to take up either 4 bytes
14899 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
14900 bytes describe the length and all offsets will be 8 bytes in length
14901 instead of 4.
14902
14903 An older, non-standard 64-bit format is also handled by this
14904 function. The older format in question stores the initial length
14905 as an 8-byte quantity without an escape value. Lengths greater
14906 than 2^32 aren't very common which means that the initial 4 bytes
14907 is almost always zero. Since a length value of zero doesn't make
14908 sense for the 32-bit format, this initial zero can be considered to
14909 be an escape value which indicates the presence of the older 64-bit
14910 format. As written, the code can't detect (old format) lengths
14911 greater than 4GB. If it becomes necessary to handle lengths
14912 somewhat larger than 4GB, we could allow other small values (such
14913 as the non-sensical values of 1, 2, and 3) to also be used as
14914 escape values indicating the presence of the old format.
14915
14916 The value returned via bytes_read should be used to increment the
14917 relevant pointer after calling read_initial_length().
14918
14919 [ Note: read_initial_length() and read_offset() are based on the
14920 document entitled "DWARF Debugging Information Format", revision
14921 3, draft 8, dated November 19, 2001. This document was obtained
14922 from:
14923
14924 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
14925
14926 This document is only a draft and is subject to change. (So beware.)
14927
14928 Details regarding the older, non-standard 64-bit format were
14929 determined empirically by examining 64-bit ELF files produced by
14930 the SGI toolchain on an IRIX 6.5 machine.
14931
14932 - Kevin, July 16, 2002
14933 ] */
14934
14935 static LONGEST
14936 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
14937 {
14938 LONGEST length = bfd_get_32 (abfd, buf);
14939
14940 if (length == 0xffffffff)
14941 {
14942 length = bfd_get_64 (abfd, buf + 4);
14943 *bytes_read = 12;
14944 }
14945 else if (length == 0)
14946 {
14947 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
14948 length = bfd_get_64 (abfd, buf);
14949 *bytes_read = 8;
14950 }
14951 else
14952 {
14953 *bytes_read = 4;
14954 }
14955
14956 return length;
14957 }
14958
14959 /* Cover function for read_initial_length.
14960 Returns the length of the object at BUF, and stores the size of the
14961 initial length in *BYTES_READ and stores the size that offsets will be in
14962 *OFFSET_SIZE.
14963 If the initial length size is not equivalent to that specified in
14964 CU_HEADER then issue a complaint.
14965 This is useful when reading non-comp-unit headers. */
14966
14967 static LONGEST
14968 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
14969 const struct comp_unit_head *cu_header,
14970 unsigned int *bytes_read,
14971 unsigned int *offset_size)
14972 {
14973 LONGEST length = read_initial_length (abfd, buf, bytes_read);
14974
14975 gdb_assert (cu_header->initial_length_size == 4
14976 || cu_header->initial_length_size == 8
14977 || cu_header->initial_length_size == 12);
14978
14979 if (cu_header->initial_length_size != *bytes_read)
14980 complaint (&symfile_complaints,
14981 _("intermixed 32-bit and 64-bit DWARF sections"));
14982
14983 *offset_size = (*bytes_read == 4) ? 4 : 8;
14984 return length;
14985 }
14986
14987 /* Read an offset from the data stream. The size of the offset is
14988 given by cu_header->offset_size. */
14989
14990 static LONGEST
14991 read_offset (bfd *abfd, const gdb_byte *buf,
14992 const struct comp_unit_head *cu_header,
14993 unsigned int *bytes_read)
14994 {
14995 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
14996
14997 *bytes_read = cu_header->offset_size;
14998 return offset;
14999 }
15000
15001 /* Read an offset from the data stream. */
15002
15003 static LONGEST
15004 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
15005 {
15006 LONGEST retval = 0;
15007
15008 switch (offset_size)
15009 {
15010 case 4:
15011 retval = bfd_get_32 (abfd, buf);
15012 break;
15013 case 8:
15014 retval = bfd_get_64 (abfd, buf);
15015 break;
15016 default:
15017 internal_error (__FILE__, __LINE__,
15018 _("read_offset_1: bad switch [in module %s]"),
15019 bfd_get_filename (abfd));
15020 }
15021
15022 return retval;
15023 }
15024
15025 static const gdb_byte *
15026 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
15027 {
15028 /* If the size of a host char is 8 bits, we can return a pointer
15029 to the buffer, otherwise we have to copy the data to a buffer
15030 allocated on the temporary obstack. */
15031 gdb_assert (HOST_CHAR_BIT == 8);
15032 return buf;
15033 }
15034
15035 static const char *
15036 read_direct_string (bfd *abfd, const gdb_byte *buf,
15037 unsigned int *bytes_read_ptr)
15038 {
15039 /* If the size of a host char is 8 bits, we can return a pointer
15040 to the string, otherwise we have to copy the string to a buffer
15041 allocated on the temporary obstack. */
15042 gdb_assert (HOST_CHAR_BIT == 8);
15043 if (*buf == '\0')
15044 {
15045 *bytes_read_ptr = 1;
15046 return NULL;
15047 }
15048 *bytes_read_ptr = strlen ((const char *) buf) + 1;
15049 return (const char *) buf;
15050 }
15051
15052 static const char *
15053 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
15054 {
15055 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
15056 if (dwarf2_per_objfile->str.buffer == NULL)
15057 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
15058 bfd_get_filename (abfd));
15059 if (str_offset >= dwarf2_per_objfile->str.size)
15060 error (_("DW_FORM_strp pointing outside of "
15061 ".debug_str section [in module %s]"),
15062 bfd_get_filename (abfd));
15063 gdb_assert (HOST_CHAR_BIT == 8);
15064 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
15065 return NULL;
15066 return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
15067 }
15068
15069 /* Read a string at offset STR_OFFSET in the .debug_str section from
15070 the .dwz file DWZ. Throw an error if the offset is too large. If
15071 the string consists of a single NUL byte, return NULL; otherwise
15072 return a pointer to the string. */
15073
15074 static const char *
15075 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
15076 {
15077 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
15078
15079 if (dwz->str.buffer == NULL)
15080 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
15081 "section [in module %s]"),
15082 bfd_get_filename (dwz->dwz_bfd));
15083 if (str_offset >= dwz->str.size)
15084 error (_("DW_FORM_GNU_strp_alt pointing outside of "
15085 ".debug_str section [in module %s]"),
15086 bfd_get_filename (dwz->dwz_bfd));
15087 gdb_assert (HOST_CHAR_BIT == 8);
15088 if (dwz->str.buffer[str_offset] == '\0')
15089 return NULL;
15090 return (const char *) (dwz->str.buffer + str_offset);
15091 }
15092
15093 static const char *
15094 read_indirect_string (bfd *abfd, const gdb_byte *buf,
15095 const struct comp_unit_head *cu_header,
15096 unsigned int *bytes_read_ptr)
15097 {
15098 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
15099
15100 return read_indirect_string_at_offset (abfd, str_offset);
15101 }
15102
15103 static ULONGEST
15104 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
15105 unsigned int *bytes_read_ptr)
15106 {
15107 ULONGEST result;
15108 unsigned int num_read;
15109 int i, shift;
15110 unsigned char byte;
15111
15112 result = 0;
15113 shift = 0;
15114 num_read = 0;
15115 i = 0;
15116 while (1)
15117 {
15118 byte = bfd_get_8 (abfd, buf);
15119 buf++;
15120 num_read++;
15121 result |= ((ULONGEST) (byte & 127) << shift);
15122 if ((byte & 128) == 0)
15123 {
15124 break;
15125 }
15126 shift += 7;
15127 }
15128 *bytes_read_ptr = num_read;
15129 return result;
15130 }
15131
15132 static LONGEST
15133 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
15134 unsigned int *bytes_read_ptr)
15135 {
15136 LONGEST result;
15137 int i, shift, num_read;
15138 unsigned char byte;
15139
15140 result = 0;
15141 shift = 0;
15142 num_read = 0;
15143 i = 0;
15144 while (1)
15145 {
15146 byte = bfd_get_8 (abfd, buf);
15147 buf++;
15148 num_read++;
15149 result |= ((LONGEST) (byte & 127) << shift);
15150 shift += 7;
15151 if ((byte & 128) == 0)
15152 {
15153 break;
15154 }
15155 }
15156 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
15157 result |= -(((LONGEST) 1) << shift);
15158 *bytes_read_ptr = num_read;
15159 return result;
15160 }
15161
15162 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
15163 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
15164 ADDR_SIZE is the size of addresses from the CU header. */
15165
15166 static CORE_ADDR
15167 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
15168 {
15169 struct objfile *objfile = dwarf2_per_objfile->objfile;
15170 bfd *abfd = objfile->obfd;
15171 const gdb_byte *info_ptr;
15172
15173 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
15174 if (dwarf2_per_objfile->addr.buffer == NULL)
15175 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
15176 objfile->name);
15177 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
15178 error (_("DW_FORM_addr_index pointing outside of "
15179 ".debug_addr section [in module %s]"),
15180 objfile->name);
15181 info_ptr = (dwarf2_per_objfile->addr.buffer
15182 + addr_base + addr_index * addr_size);
15183 if (addr_size == 4)
15184 return bfd_get_32 (abfd, info_ptr);
15185 else
15186 return bfd_get_64 (abfd, info_ptr);
15187 }
15188
15189 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
15190
15191 static CORE_ADDR
15192 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
15193 {
15194 return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
15195 }
15196
15197 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
15198
15199 static CORE_ADDR
15200 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
15201 unsigned int *bytes_read)
15202 {
15203 bfd *abfd = cu->objfile->obfd;
15204 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
15205
15206 return read_addr_index (cu, addr_index);
15207 }
15208
15209 /* Data structure to pass results from dwarf2_read_addr_index_reader
15210 back to dwarf2_read_addr_index. */
15211
15212 struct dwarf2_read_addr_index_data
15213 {
15214 ULONGEST addr_base;
15215 int addr_size;
15216 };
15217
15218 /* die_reader_func for dwarf2_read_addr_index. */
15219
15220 static void
15221 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
15222 const gdb_byte *info_ptr,
15223 struct die_info *comp_unit_die,
15224 int has_children,
15225 void *data)
15226 {
15227 struct dwarf2_cu *cu = reader->cu;
15228 struct dwarf2_read_addr_index_data *aidata =
15229 (struct dwarf2_read_addr_index_data *) data;
15230
15231 aidata->addr_base = cu->addr_base;
15232 aidata->addr_size = cu->header.addr_size;
15233 }
15234
15235 /* Given an index in .debug_addr, fetch the value.
15236 NOTE: This can be called during dwarf expression evaluation,
15237 long after the debug information has been read, and thus per_cu->cu
15238 may no longer exist. */
15239
15240 CORE_ADDR
15241 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
15242 unsigned int addr_index)
15243 {
15244 struct objfile *objfile = per_cu->objfile;
15245 struct dwarf2_cu *cu = per_cu->cu;
15246 ULONGEST addr_base;
15247 int addr_size;
15248
15249 /* This is intended to be called from outside this file. */
15250 dw2_setup (objfile);
15251
15252 /* We need addr_base and addr_size.
15253 If we don't have PER_CU->cu, we have to get it.
15254 Nasty, but the alternative is storing the needed info in PER_CU,
15255 which at this point doesn't seem justified: it's not clear how frequently
15256 it would get used and it would increase the size of every PER_CU.
15257 Entry points like dwarf2_per_cu_addr_size do a similar thing
15258 so we're not in uncharted territory here.
15259 Alas we need to be a bit more complicated as addr_base is contained
15260 in the DIE.
15261
15262 We don't need to read the entire CU(/TU).
15263 We just need the header and top level die.
15264
15265 IWBN to use the aging mechanism to let us lazily later discard the CU.
15266 For now we skip this optimization. */
15267
15268 if (cu != NULL)
15269 {
15270 addr_base = cu->addr_base;
15271 addr_size = cu->header.addr_size;
15272 }
15273 else
15274 {
15275 struct dwarf2_read_addr_index_data aidata;
15276
15277 /* Note: We can't use init_cutu_and_read_dies_simple here,
15278 we need addr_base. */
15279 init_cutu_and_read_dies (per_cu, NULL, 0, 0,
15280 dwarf2_read_addr_index_reader, &aidata);
15281 addr_base = aidata.addr_base;
15282 addr_size = aidata.addr_size;
15283 }
15284
15285 return read_addr_index_1 (addr_index, addr_base, addr_size);
15286 }
15287
15288 /* Given a DW_AT_str_index, fetch the string. */
15289
15290 static const char *
15291 read_str_index (const struct die_reader_specs *reader,
15292 struct dwarf2_cu *cu, ULONGEST str_index)
15293 {
15294 struct objfile *objfile = dwarf2_per_objfile->objfile;
15295 const char *dwo_name = objfile->name;
15296 bfd *abfd = objfile->obfd;
15297 struct dwo_sections *sections = &reader->dwo_file->sections;
15298 const gdb_byte *info_ptr;
15299 ULONGEST str_offset;
15300
15301 dwarf2_read_section (objfile, &sections->str);
15302 dwarf2_read_section (objfile, &sections->str_offsets);
15303 if (sections->str.buffer == NULL)
15304 error (_("DW_FORM_str_index used without .debug_str.dwo section"
15305 " in CU at offset 0x%lx [in module %s]"),
15306 (long) cu->header.offset.sect_off, dwo_name);
15307 if (sections->str_offsets.buffer == NULL)
15308 error (_("DW_FORM_str_index used without .debug_str_offsets.dwo section"
15309 " in CU at offset 0x%lx [in module %s]"),
15310 (long) cu->header.offset.sect_off, dwo_name);
15311 if (str_index * cu->header.offset_size >= sections->str_offsets.size)
15312 error (_("DW_FORM_str_index pointing outside of .debug_str_offsets.dwo"
15313 " section in CU at offset 0x%lx [in module %s]"),
15314 (long) cu->header.offset.sect_off, dwo_name);
15315 info_ptr = (sections->str_offsets.buffer
15316 + str_index * cu->header.offset_size);
15317 if (cu->header.offset_size == 4)
15318 str_offset = bfd_get_32 (abfd, info_ptr);
15319 else
15320 str_offset = bfd_get_64 (abfd, info_ptr);
15321 if (str_offset >= sections->str.size)
15322 error (_("Offset from DW_FORM_str_index pointing outside of"
15323 " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
15324 (long) cu->header.offset.sect_off, dwo_name);
15325 return (const char *) (sections->str.buffer + str_offset);
15326 }
15327
15328 /* Return the length of an LEB128 number in BUF. */
15329
15330 static int
15331 leb128_size (const gdb_byte *buf)
15332 {
15333 const gdb_byte *begin = buf;
15334 gdb_byte byte;
15335
15336 while (1)
15337 {
15338 byte = *buf++;
15339 if ((byte & 128) == 0)
15340 return buf - begin;
15341 }
15342 }
15343
15344 static void
15345 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
15346 {
15347 switch (lang)
15348 {
15349 case DW_LANG_C89:
15350 case DW_LANG_C99:
15351 case DW_LANG_C:
15352 case DW_LANG_UPC:
15353 cu->language = language_c;
15354 break;
15355 case DW_LANG_C_plus_plus:
15356 cu->language = language_cplus;
15357 break;
15358 case DW_LANG_D:
15359 cu->language = language_d;
15360 break;
15361 case DW_LANG_Fortran77:
15362 case DW_LANG_Fortran90:
15363 case DW_LANG_Fortran95:
15364 cu->language = language_fortran;
15365 break;
15366 case DW_LANG_Go:
15367 cu->language = language_go;
15368 break;
15369 case DW_LANG_Mips_Assembler:
15370 cu->language = language_asm;
15371 break;
15372 case DW_LANG_Java:
15373 cu->language = language_java;
15374 break;
15375 case DW_LANG_Ada83:
15376 case DW_LANG_Ada95:
15377 cu->language = language_ada;
15378 break;
15379 case DW_LANG_Modula2:
15380 cu->language = language_m2;
15381 break;
15382 case DW_LANG_Pascal83:
15383 cu->language = language_pascal;
15384 break;
15385 case DW_LANG_ObjC:
15386 cu->language = language_objc;
15387 break;
15388 case DW_LANG_Cobol74:
15389 case DW_LANG_Cobol85:
15390 default:
15391 cu->language = language_minimal;
15392 break;
15393 }
15394 cu->language_defn = language_def (cu->language);
15395 }
15396
15397 /* Return the named attribute or NULL if not there. */
15398
15399 static struct attribute *
15400 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
15401 {
15402 for (;;)
15403 {
15404 unsigned int i;
15405 struct attribute *spec = NULL;
15406
15407 for (i = 0; i < die->num_attrs; ++i)
15408 {
15409 if (die->attrs[i].name == name)
15410 return &die->attrs[i];
15411 if (die->attrs[i].name == DW_AT_specification
15412 || die->attrs[i].name == DW_AT_abstract_origin)
15413 spec = &die->attrs[i];
15414 }
15415
15416 if (!spec)
15417 break;
15418
15419 die = follow_die_ref (die, spec, &cu);
15420 }
15421
15422 return NULL;
15423 }
15424
15425 /* Return the named attribute or NULL if not there,
15426 but do not follow DW_AT_specification, etc.
15427 This is for use in contexts where we're reading .debug_types dies.
15428 Following DW_AT_specification, DW_AT_abstract_origin will take us
15429 back up the chain, and we want to go down. */
15430
15431 static struct attribute *
15432 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
15433 {
15434 unsigned int i;
15435
15436 for (i = 0; i < die->num_attrs; ++i)
15437 if (die->attrs[i].name == name)
15438 return &die->attrs[i];
15439
15440 return NULL;
15441 }
15442
15443 /* Return non-zero iff the attribute NAME is defined for the given DIE,
15444 and holds a non-zero value. This function should only be used for
15445 DW_FORM_flag or DW_FORM_flag_present attributes. */
15446
15447 static int
15448 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
15449 {
15450 struct attribute *attr = dwarf2_attr (die, name, cu);
15451
15452 return (attr && DW_UNSND (attr));
15453 }
15454
15455 static int
15456 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
15457 {
15458 /* A DIE is a declaration if it has a DW_AT_declaration attribute
15459 which value is non-zero. However, we have to be careful with
15460 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
15461 (via dwarf2_flag_true_p) follows this attribute. So we may
15462 end up accidently finding a declaration attribute that belongs
15463 to a different DIE referenced by the specification attribute,
15464 even though the given DIE does not have a declaration attribute. */
15465 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
15466 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
15467 }
15468
15469 /* Return the die giving the specification for DIE, if there is
15470 one. *SPEC_CU is the CU containing DIE on input, and the CU
15471 containing the return value on output. If there is no
15472 specification, but there is an abstract origin, that is
15473 returned. */
15474
15475 static struct die_info *
15476 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
15477 {
15478 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
15479 *spec_cu);
15480
15481 if (spec_attr == NULL)
15482 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
15483
15484 if (spec_attr == NULL)
15485 return NULL;
15486 else
15487 return follow_die_ref (die, spec_attr, spec_cu);
15488 }
15489
15490 /* Free the line_header structure *LH, and any arrays and strings it
15491 refers to.
15492 NOTE: This is also used as a "cleanup" function. */
15493
15494 static void
15495 free_line_header (struct line_header *lh)
15496 {
15497 if (lh->standard_opcode_lengths)
15498 xfree (lh->standard_opcode_lengths);
15499
15500 /* Remember that all the lh->file_names[i].name pointers are
15501 pointers into debug_line_buffer, and don't need to be freed. */
15502 if (lh->file_names)
15503 xfree (lh->file_names);
15504
15505 /* Similarly for the include directory names. */
15506 if (lh->include_dirs)
15507 xfree (lh->include_dirs);
15508
15509 xfree (lh);
15510 }
15511
15512 /* Add an entry to LH's include directory table. */
15513
15514 static void
15515 add_include_dir (struct line_header *lh, const char *include_dir)
15516 {
15517 /* Grow the array if necessary. */
15518 if (lh->include_dirs_size == 0)
15519 {
15520 lh->include_dirs_size = 1; /* for testing */
15521 lh->include_dirs = xmalloc (lh->include_dirs_size
15522 * sizeof (*lh->include_dirs));
15523 }
15524 else if (lh->num_include_dirs >= lh->include_dirs_size)
15525 {
15526 lh->include_dirs_size *= 2;
15527 lh->include_dirs = xrealloc (lh->include_dirs,
15528 (lh->include_dirs_size
15529 * sizeof (*lh->include_dirs)));
15530 }
15531
15532 lh->include_dirs[lh->num_include_dirs++] = include_dir;
15533 }
15534
15535 /* Add an entry to LH's file name table. */
15536
15537 static void
15538 add_file_name (struct line_header *lh,
15539 const char *name,
15540 unsigned int dir_index,
15541 unsigned int mod_time,
15542 unsigned int length)
15543 {
15544 struct file_entry *fe;
15545
15546 /* Grow the array if necessary. */
15547 if (lh->file_names_size == 0)
15548 {
15549 lh->file_names_size = 1; /* for testing */
15550 lh->file_names = xmalloc (lh->file_names_size
15551 * sizeof (*lh->file_names));
15552 }
15553 else if (lh->num_file_names >= lh->file_names_size)
15554 {
15555 lh->file_names_size *= 2;
15556 lh->file_names = xrealloc (lh->file_names,
15557 (lh->file_names_size
15558 * sizeof (*lh->file_names)));
15559 }
15560
15561 fe = &lh->file_names[lh->num_file_names++];
15562 fe->name = name;
15563 fe->dir_index = dir_index;
15564 fe->mod_time = mod_time;
15565 fe->length = length;
15566 fe->included_p = 0;
15567 fe->symtab = NULL;
15568 }
15569
15570 /* A convenience function to find the proper .debug_line section for a
15571 CU. */
15572
15573 static struct dwarf2_section_info *
15574 get_debug_line_section (struct dwarf2_cu *cu)
15575 {
15576 struct dwarf2_section_info *section;
15577
15578 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
15579 DWO file. */
15580 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15581 section = &cu->dwo_unit->dwo_file->sections.line;
15582 else if (cu->per_cu->is_dwz)
15583 {
15584 struct dwz_file *dwz = dwarf2_get_dwz_file ();
15585
15586 section = &dwz->line;
15587 }
15588 else
15589 section = &dwarf2_per_objfile->line;
15590
15591 return section;
15592 }
15593
15594 /* Read the statement program header starting at OFFSET in
15595 .debug_line, or .debug_line.dwo. Return a pointer
15596 to a struct line_header, allocated using xmalloc.
15597
15598 NOTE: the strings in the include directory and file name tables of
15599 the returned object point into the dwarf line section buffer,
15600 and must not be freed. */
15601
15602 static struct line_header *
15603 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
15604 {
15605 struct cleanup *back_to;
15606 struct line_header *lh;
15607 const gdb_byte *line_ptr;
15608 unsigned int bytes_read, offset_size;
15609 int i;
15610 const char *cur_dir, *cur_file;
15611 struct dwarf2_section_info *section;
15612 bfd *abfd;
15613
15614 section = get_debug_line_section (cu);
15615 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
15616 if (section->buffer == NULL)
15617 {
15618 if (cu->dwo_unit && cu->per_cu->is_debug_types)
15619 complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
15620 else
15621 complaint (&symfile_complaints, _("missing .debug_line section"));
15622 return 0;
15623 }
15624
15625 /* We can't do this until we know the section is non-empty.
15626 Only then do we know we have such a section. */
15627 abfd = section->asection->owner;
15628
15629 /* Make sure that at least there's room for the total_length field.
15630 That could be 12 bytes long, but we're just going to fudge that. */
15631 if (offset + 4 >= section->size)
15632 {
15633 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15634 return 0;
15635 }
15636
15637 lh = xmalloc (sizeof (*lh));
15638 memset (lh, 0, sizeof (*lh));
15639 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
15640 (void *) lh);
15641
15642 line_ptr = section->buffer + offset;
15643
15644 /* Read in the header. */
15645 lh->total_length =
15646 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
15647 &bytes_read, &offset_size);
15648 line_ptr += bytes_read;
15649 if (line_ptr + lh->total_length > (section->buffer + section->size))
15650 {
15651 dwarf2_statement_list_fits_in_line_number_section_complaint ();
15652 return 0;
15653 }
15654 lh->statement_program_end = line_ptr + lh->total_length;
15655 lh->version = read_2_bytes (abfd, line_ptr);
15656 line_ptr += 2;
15657 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
15658 line_ptr += offset_size;
15659 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
15660 line_ptr += 1;
15661 if (lh->version >= 4)
15662 {
15663 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
15664 line_ptr += 1;
15665 }
15666 else
15667 lh->maximum_ops_per_instruction = 1;
15668
15669 if (lh->maximum_ops_per_instruction == 0)
15670 {
15671 lh->maximum_ops_per_instruction = 1;
15672 complaint (&symfile_complaints,
15673 _("invalid maximum_ops_per_instruction "
15674 "in `.debug_line' section"));
15675 }
15676
15677 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
15678 line_ptr += 1;
15679 lh->line_base = read_1_signed_byte (abfd, line_ptr);
15680 line_ptr += 1;
15681 lh->line_range = read_1_byte (abfd, line_ptr);
15682 line_ptr += 1;
15683 lh->opcode_base = read_1_byte (abfd, line_ptr);
15684 line_ptr += 1;
15685 lh->standard_opcode_lengths
15686 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
15687
15688 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
15689 for (i = 1; i < lh->opcode_base; ++i)
15690 {
15691 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
15692 line_ptr += 1;
15693 }
15694
15695 /* Read directory table. */
15696 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15697 {
15698 line_ptr += bytes_read;
15699 add_include_dir (lh, cur_dir);
15700 }
15701 line_ptr += bytes_read;
15702
15703 /* Read file name table. */
15704 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
15705 {
15706 unsigned int dir_index, mod_time, length;
15707
15708 line_ptr += bytes_read;
15709 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15710 line_ptr += bytes_read;
15711 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15712 line_ptr += bytes_read;
15713 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15714 line_ptr += bytes_read;
15715
15716 add_file_name (lh, cur_file, dir_index, mod_time, length);
15717 }
15718 line_ptr += bytes_read;
15719 lh->statement_program_start = line_ptr;
15720
15721 if (line_ptr > (section->buffer + section->size))
15722 complaint (&symfile_complaints,
15723 _("line number info header doesn't "
15724 "fit in `.debug_line' section"));
15725
15726 discard_cleanups (back_to);
15727 return lh;
15728 }
15729
15730 /* Subroutine of dwarf_decode_lines to simplify it.
15731 Return the file name of the psymtab for included file FILE_INDEX
15732 in line header LH of PST.
15733 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
15734 If space for the result is malloc'd, it will be freed by a cleanup.
15735 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
15736
15737 The function creates dangling cleanup registration. */
15738
15739 static const char *
15740 psymtab_include_file_name (const struct line_header *lh, int file_index,
15741 const struct partial_symtab *pst,
15742 const char *comp_dir)
15743 {
15744 const struct file_entry fe = lh->file_names [file_index];
15745 const char *include_name = fe.name;
15746 const char *include_name_to_compare = include_name;
15747 const char *dir_name = NULL;
15748 const char *pst_filename;
15749 char *copied_name = NULL;
15750 int file_is_pst;
15751
15752 if (fe.dir_index)
15753 dir_name = lh->include_dirs[fe.dir_index - 1];
15754
15755 if (!IS_ABSOLUTE_PATH (include_name)
15756 && (dir_name != NULL || comp_dir != NULL))
15757 {
15758 /* Avoid creating a duplicate psymtab for PST.
15759 We do this by comparing INCLUDE_NAME and PST_FILENAME.
15760 Before we do the comparison, however, we need to account
15761 for DIR_NAME and COMP_DIR.
15762 First prepend dir_name (if non-NULL). If we still don't
15763 have an absolute path prepend comp_dir (if non-NULL).
15764 However, the directory we record in the include-file's
15765 psymtab does not contain COMP_DIR (to match the
15766 corresponding symtab(s)).
15767
15768 Example:
15769
15770 bash$ cd /tmp
15771 bash$ gcc -g ./hello.c
15772 include_name = "hello.c"
15773 dir_name = "."
15774 DW_AT_comp_dir = comp_dir = "/tmp"
15775 DW_AT_name = "./hello.c" */
15776
15777 if (dir_name != NULL)
15778 {
15779 char *tem = concat (dir_name, SLASH_STRING,
15780 include_name, (char *)NULL);
15781
15782 make_cleanup (xfree, tem);
15783 include_name = tem;
15784 include_name_to_compare = include_name;
15785 }
15786 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
15787 {
15788 char *tem = concat (comp_dir, SLASH_STRING,
15789 include_name, (char *)NULL);
15790
15791 make_cleanup (xfree, tem);
15792 include_name_to_compare = tem;
15793 }
15794 }
15795
15796 pst_filename = pst->filename;
15797 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
15798 {
15799 copied_name = concat (pst->dirname, SLASH_STRING,
15800 pst_filename, (char *)NULL);
15801 pst_filename = copied_name;
15802 }
15803
15804 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
15805
15806 if (copied_name != NULL)
15807 xfree (copied_name);
15808
15809 if (file_is_pst)
15810 return NULL;
15811 return include_name;
15812 }
15813
15814 /* Ignore this record_line request. */
15815
15816 static void
15817 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
15818 {
15819 return;
15820 }
15821
15822 /* Subroutine of dwarf_decode_lines to simplify it.
15823 Process the line number information in LH. */
15824
15825 static void
15826 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
15827 struct dwarf2_cu *cu, struct partial_symtab *pst)
15828 {
15829 const gdb_byte *line_ptr, *extended_end;
15830 const gdb_byte *line_end;
15831 unsigned int bytes_read, extended_len;
15832 unsigned char op_code, extended_op, adj_opcode;
15833 CORE_ADDR baseaddr;
15834 struct objfile *objfile = cu->objfile;
15835 bfd *abfd = objfile->obfd;
15836 struct gdbarch *gdbarch = get_objfile_arch (objfile);
15837 const int decode_for_pst_p = (pst != NULL);
15838 struct subfile *last_subfile = NULL;
15839 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
15840 = record_line;
15841
15842 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15843
15844 line_ptr = lh->statement_program_start;
15845 line_end = lh->statement_program_end;
15846
15847 /* Read the statement sequences until there's nothing left. */
15848 while (line_ptr < line_end)
15849 {
15850 /* state machine registers */
15851 CORE_ADDR address = 0;
15852 unsigned int file = 1;
15853 unsigned int line = 1;
15854 unsigned int column = 0;
15855 int is_stmt = lh->default_is_stmt;
15856 int basic_block = 0;
15857 int end_sequence = 0;
15858 CORE_ADDR addr;
15859 unsigned char op_index = 0;
15860
15861 if (!decode_for_pst_p && lh->num_file_names >= file)
15862 {
15863 /* Start a subfile for the current file of the state machine. */
15864 /* lh->include_dirs and lh->file_names are 0-based, but the
15865 directory and file name numbers in the statement program
15866 are 1-based. */
15867 struct file_entry *fe = &lh->file_names[file - 1];
15868 const char *dir = NULL;
15869
15870 if (fe->dir_index)
15871 dir = lh->include_dirs[fe->dir_index - 1];
15872
15873 dwarf2_start_subfile (fe->name, dir, comp_dir);
15874 }
15875
15876 /* Decode the table. */
15877 while (!end_sequence)
15878 {
15879 op_code = read_1_byte (abfd, line_ptr);
15880 line_ptr += 1;
15881 if (line_ptr > line_end)
15882 {
15883 dwarf2_debug_line_missing_end_sequence_complaint ();
15884 break;
15885 }
15886
15887 if (op_code >= lh->opcode_base)
15888 {
15889 /* Special operand. */
15890 adj_opcode = op_code - lh->opcode_base;
15891 address += (((op_index + (adj_opcode / lh->line_range))
15892 / lh->maximum_ops_per_instruction)
15893 * lh->minimum_instruction_length);
15894 op_index = ((op_index + (adj_opcode / lh->line_range))
15895 % lh->maximum_ops_per_instruction);
15896 line += lh->line_base + (adj_opcode % lh->line_range);
15897 if (lh->num_file_names < file || file == 0)
15898 dwarf2_debug_line_missing_file_complaint ();
15899 /* For now we ignore lines not starting on an
15900 instruction boundary. */
15901 else if (op_index == 0)
15902 {
15903 lh->file_names[file - 1].included_p = 1;
15904 if (!decode_for_pst_p && is_stmt)
15905 {
15906 if (last_subfile != current_subfile)
15907 {
15908 addr = gdbarch_addr_bits_remove (gdbarch, address);
15909 if (last_subfile)
15910 (*p_record_line) (last_subfile, 0, addr);
15911 last_subfile = current_subfile;
15912 }
15913 /* Append row to matrix using current values. */
15914 addr = gdbarch_addr_bits_remove (gdbarch, address);
15915 (*p_record_line) (current_subfile, line, addr);
15916 }
15917 }
15918 basic_block = 0;
15919 }
15920 else switch (op_code)
15921 {
15922 case DW_LNS_extended_op:
15923 extended_len = read_unsigned_leb128 (abfd, line_ptr,
15924 &bytes_read);
15925 line_ptr += bytes_read;
15926 extended_end = line_ptr + extended_len;
15927 extended_op = read_1_byte (abfd, line_ptr);
15928 line_ptr += 1;
15929 switch (extended_op)
15930 {
15931 case DW_LNE_end_sequence:
15932 p_record_line = record_line;
15933 end_sequence = 1;
15934 break;
15935 case DW_LNE_set_address:
15936 address = read_address (abfd, line_ptr, cu, &bytes_read);
15937
15938 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
15939 {
15940 /* This line table is for a function which has been
15941 GCd by the linker. Ignore it. PR gdb/12528 */
15942
15943 long line_offset
15944 = line_ptr - get_debug_line_section (cu)->buffer;
15945
15946 complaint (&symfile_complaints,
15947 _(".debug_line address at offset 0x%lx is 0 "
15948 "[in module %s]"),
15949 line_offset, objfile->name);
15950 p_record_line = noop_record_line;
15951 }
15952
15953 op_index = 0;
15954 line_ptr += bytes_read;
15955 address += baseaddr;
15956 break;
15957 case DW_LNE_define_file:
15958 {
15959 const char *cur_file;
15960 unsigned int dir_index, mod_time, length;
15961
15962 cur_file = read_direct_string (abfd, line_ptr,
15963 &bytes_read);
15964 line_ptr += bytes_read;
15965 dir_index =
15966 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15967 line_ptr += bytes_read;
15968 mod_time =
15969 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15970 line_ptr += bytes_read;
15971 length =
15972 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
15973 line_ptr += bytes_read;
15974 add_file_name (lh, cur_file, dir_index, mod_time, length);
15975 }
15976 break;
15977 case DW_LNE_set_discriminator:
15978 /* The discriminator is not interesting to the debugger;
15979 just ignore it. */
15980 line_ptr = extended_end;
15981 break;
15982 default:
15983 complaint (&symfile_complaints,
15984 _("mangled .debug_line section"));
15985 return;
15986 }
15987 /* Make sure that we parsed the extended op correctly. If e.g.
15988 we expected a different address size than the producer used,
15989 we may have read the wrong number of bytes. */
15990 if (line_ptr != extended_end)
15991 {
15992 complaint (&symfile_complaints,
15993 _("mangled .debug_line section"));
15994 return;
15995 }
15996 break;
15997 case DW_LNS_copy:
15998 if (lh->num_file_names < file || file == 0)
15999 dwarf2_debug_line_missing_file_complaint ();
16000 else
16001 {
16002 lh->file_names[file - 1].included_p = 1;
16003 if (!decode_for_pst_p && is_stmt)
16004 {
16005 if (last_subfile != current_subfile)
16006 {
16007 addr = gdbarch_addr_bits_remove (gdbarch, address);
16008 if (last_subfile)
16009 (*p_record_line) (last_subfile, 0, addr);
16010 last_subfile = current_subfile;
16011 }
16012 addr = gdbarch_addr_bits_remove (gdbarch, address);
16013 (*p_record_line) (current_subfile, line, addr);
16014 }
16015 }
16016 basic_block = 0;
16017 break;
16018 case DW_LNS_advance_pc:
16019 {
16020 CORE_ADDR adjust
16021 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16022
16023 address += (((op_index + adjust)
16024 / lh->maximum_ops_per_instruction)
16025 * lh->minimum_instruction_length);
16026 op_index = ((op_index + adjust)
16027 % lh->maximum_ops_per_instruction);
16028 line_ptr += bytes_read;
16029 }
16030 break;
16031 case DW_LNS_advance_line:
16032 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16033 line_ptr += bytes_read;
16034 break;
16035 case DW_LNS_set_file:
16036 {
16037 /* The arrays lh->include_dirs and lh->file_names are
16038 0-based, but the directory and file name numbers in
16039 the statement program are 1-based. */
16040 struct file_entry *fe;
16041 const char *dir = NULL;
16042
16043 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16044 line_ptr += bytes_read;
16045 if (lh->num_file_names < file || file == 0)
16046 dwarf2_debug_line_missing_file_complaint ();
16047 else
16048 {
16049 fe = &lh->file_names[file - 1];
16050 if (fe->dir_index)
16051 dir = lh->include_dirs[fe->dir_index - 1];
16052 if (!decode_for_pst_p)
16053 {
16054 last_subfile = current_subfile;
16055 dwarf2_start_subfile (fe->name, dir, comp_dir);
16056 }
16057 }
16058 }
16059 break;
16060 case DW_LNS_set_column:
16061 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16062 line_ptr += bytes_read;
16063 break;
16064 case DW_LNS_negate_stmt:
16065 is_stmt = (!is_stmt);
16066 break;
16067 case DW_LNS_set_basic_block:
16068 basic_block = 1;
16069 break;
16070 /* Add to the address register of the state machine the
16071 address increment value corresponding to special opcode
16072 255. I.e., this value is scaled by the minimum
16073 instruction length since special opcode 255 would have
16074 scaled the increment. */
16075 case DW_LNS_const_add_pc:
16076 {
16077 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
16078
16079 address += (((op_index + adjust)
16080 / lh->maximum_ops_per_instruction)
16081 * lh->minimum_instruction_length);
16082 op_index = ((op_index + adjust)
16083 % lh->maximum_ops_per_instruction);
16084 }
16085 break;
16086 case DW_LNS_fixed_advance_pc:
16087 address += read_2_bytes (abfd, line_ptr);
16088 op_index = 0;
16089 line_ptr += 2;
16090 break;
16091 default:
16092 {
16093 /* Unknown standard opcode, ignore it. */
16094 int i;
16095
16096 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
16097 {
16098 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16099 line_ptr += bytes_read;
16100 }
16101 }
16102 }
16103 }
16104 if (lh->num_file_names < file || file == 0)
16105 dwarf2_debug_line_missing_file_complaint ();
16106 else
16107 {
16108 lh->file_names[file - 1].included_p = 1;
16109 if (!decode_for_pst_p)
16110 {
16111 addr = gdbarch_addr_bits_remove (gdbarch, address);
16112 (*p_record_line) (current_subfile, 0, addr);
16113 }
16114 }
16115 }
16116 }
16117
16118 /* Decode the Line Number Program (LNP) for the given line_header
16119 structure and CU. The actual information extracted and the type
16120 of structures created from the LNP depends on the value of PST.
16121
16122 1. If PST is NULL, then this procedure uses the data from the program
16123 to create all necessary symbol tables, and their linetables.
16124
16125 2. If PST is not NULL, this procedure reads the program to determine
16126 the list of files included by the unit represented by PST, and
16127 builds all the associated partial symbol tables.
16128
16129 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
16130 It is used for relative paths in the line table.
16131 NOTE: When processing partial symtabs (pst != NULL),
16132 comp_dir == pst->dirname.
16133
16134 NOTE: It is important that psymtabs have the same file name (via strcmp)
16135 as the corresponding symtab. Since COMP_DIR is not used in the name of the
16136 symtab we don't use it in the name of the psymtabs we create.
16137 E.g. expand_line_sal requires this when finding psymtabs to expand.
16138 A good testcase for this is mb-inline.exp. */
16139
16140 static void
16141 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
16142 struct dwarf2_cu *cu, struct partial_symtab *pst,
16143 int want_line_info)
16144 {
16145 struct objfile *objfile = cu->objfile;
16146 const int decode_for_pst_p = (pst != NULL);
16147 struct subfile *first_subfile = current_subfile;
16148
16149 if (want_line_info)
16150 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
16151
16152 if (decode_for_pst_p)
16153 {
16154 int file_index;
16155
16156 /* Now that we're done scanning the Line Header Program, we can
16157 create the psymtab of each included file. */
16158 for (file_index = 0; file_index < lh->num_file_names; file_index++)
16159 if (lh->file_names[file_index].included_p == 1)
16160 {
16161 const char *include_name =
16162 psymtab_include_file_name (lh, file_index, pst, comp_dir);
16163 if (include_name != NULL)
16164 dwarf2_create_include_psymtab (include_name, pst, objfile);
16165 }
16166 }
16167 else
16168 {
16169 /* Make sure a symtab is created for every file, even files
16170 which contain only variables (i.e. no code with associated
16171 line numbers). */
16172 int i;
16173
16174 for (i = 0; i < lh->num_file_names; i++)
16175 {
16176 const char *dir = NULL;
16177 struct file_entry *fe;
16178
16179 fe = &lh->file_names[i];
16180 if (fe->dir_index)
16181 dir = lh->include_dirs[fe->dir_index - 1];
16182 dwarf2_start_subfile (fe->name, dir, comp_dir);
16183
16184 /* Skip the main file; we don't need it, and it must be
16185 allocated last, so that it will show up before the
16186 non-primary symtabs in the objfile's symtab list. */
16187 if (current_subfile == first_subfile)
16188 continue;
16189
16190 if (current_subfile->symtab == NULL)
16191 current_subfile->symtab = allocate_symtab (current_subfile->name,
16192 objfile);
16193 fe->symtab = current_subfile->symtab;
16194 }
16195 }
16196 }
16197
16198 /* Start a subfile for DWARF. FILENAME is the name of the file and
16199 DIRNAME the name of the source directory which contains FILENAME
16200 or NULL if not known. COMP_DIR is the compilation directory for the
16201 linetable's compilation unit or NULL if not known.
16202 This routine tries to keep line numbers from identical absolute and
16203 relative file names in a common subfile.
16204
16205 Using the `list' example from the GDB testsuite, which resides in
16206 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
16207 of /srcdir/list0.c yields the following debugging information for list0.c:
16208
16209 DW_AT_name: /srcdir/list0.c
16210 DW_AT_comp_dir: /compdir
16211 files.files[0].name: list0.h
16212 files.files[0].dir: /srcdir
16213 files.files[1].name: list0.c
16214 files.files[1].dir: /srcdir
16215
16216 The line number information for list0.c has to end up in a single
16217 subfile, so that `break /srcdir/list0.c:1' works as expected.
16218 start_subfile will ensure that this happens provided that we pass the
16219 concatenation of files.files[1].dir and files.files[1].name as the
16220 subfile's name. */
16221
16222 static void
16223 dwarf2_start_subfile (const char *filename, const char *dirname,
16224 const char *comp_dir)
16225 {
16226 char *copy = NULL;
16227
16228 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
16229 `start_symtab' will always pass the contents of DW_AT_comp_dir as
16230 second argument to start_subfile. To be consistent, we do the
16231 same here. In order not to lose the line information directory,
16232 we concatenate it to the filename when it makes sense.
16233 Note that the Dwarf3 standard says (speaking of filenames in line
16234 information): ``The directory index is ignored for file names
16235 that represent full path names''. Thus ignoring dirname in the
16236 `else' branch below isn't an issue. */
16237
16238 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
16239 {
16240 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
16241 filename = copy;
16242 }
16243
16244 start_subfile (filename, comp_dir);
16245
16246 if (copy != NULL)
16247 xfree (copy);
16248 }
16249
16250 /* Start a symtab for DWARF.
16251 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
16252
16253 static void
16254 dwarf2_start_symtab (struct dwarf2_cu *cu,
16255 const char *name, const char *comp_dir, CORE_ADDR low_pc)
16256 {
16257 start_symtab (name, comp_dir, low_pc);
16258 record_debugformat ("DWARF 2");
16259 record_producer (cu->producer);
16260
16261 /* We assume that we're processing GCC output. */
16262 processing_gcc_compilation = 2;
16263
16264 cu->processing_has_namespace_info = 0;
16265 }
16266
16267 static void
16268 var_decode_location (struct attribute *attr, struct symbol *sym,
16269 struct dwarf2_cu *cu)
16270 {
16271 struct objfile *objfile = cu->objfile;
16272 struct comp_unit_head *cu_header = &cu->header;
16273
16274 /* NOTE drow/2003-01-30: There used to be a comment and some special
16275 code here to turn a symbol with DW_AT_external and a
16276 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
16277 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
16278 with some versions of binutils) where shared libraries could have
16279 relocations against symbols in their debug information - the
16280 minimal symbol would have the right address, but the debug info
16281 would not. It's no longer necessary, because we will explicitly
16282 apply relocations when we read in the debug information now. */
16283
16284 /* A DW_AT_location attribute with no contents indicates that a
16285 variable has been optimized away. */
16286 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
16287 {
16288 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
16289 return;
16290 }
16291
16292 /* Handle one degenerate form of location expression specially, to
16293 preserve GDB's previous behavior when section offsets are
16294 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
16295 then mark this symbol as LOC_STATIC. */
16296
16297 if (attr_form_is_block (attr)
16298 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
16299 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
16300 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
16301 && (DW_BLOCK (attr)->size
16302 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
16303 {
16304 unsigned int dummy;
16305
16306 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
16307 SYMBOL_VALUE_ADDRESS (sym) =
16308 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
16309 else
16310 SYMBOL_VALUE_ADDRESS (sym) =
16311 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
16312 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
16313 fixup_symbol_section (sym, objfile);
16314 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
16315 SYMBOL_SECTION (sym));
16316 return;
16317 }
16318
16319 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
16320 expression evaluator, and use LOC_COMPUTED only when necessary
16321 (i.e. when the value of a register or memory location is
16322 referenced, or a thread-local block, etc.). Then again, it might
16323 not be worthwhile. I'm assuming that it isn't unless performance
16324 or memory numbers show me otherwise. */
16325
16326 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
16327
16328 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
16329 cu->has_loclist = 1;
16330 }
16331
16332 /* Given a pointer to a DWARF information entry, figure out if we need
16333 to make a symbol table entry for it, and if so, create a new entry
16334 and return a pointer to it.
16335 If TYPE is NULL, determine symbol type from the die, otherwise
16336 used the passed type.
16337 If SPACE is not NULL, use it to hold the new symbol. If it is
16338 NULL, allocate a new symbol on the objfile's obstack. */
16339
16340 static struct symbol *
16341 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
16342 struct symbol *space)
16343 {
16344 struct objfile *objfile = cu->objfile;
16345 struct symbol *sym = NULL;
16346 const char *name;
16347 struct attribute *attr = NULL;
16348 struct attribute *attr2 = NULL;
16349 CORE_ADDR baseaddr;
16350 struct pending **list_to_add = NULL;
16351
16352 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
16353
16354 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16355
16356 name = dwarf2_name (die, cu);
16357 if (name)
16358 {
16359 const char *linkagename;
16360 int suppress_add = 0;
16361
16362 if (space)
16363 sym = space;
16364 else
16365 sym = allocate_symbol (objfile);
16366 OBJSTAT (objfile, n_syms++);
16367
16368 /* Cache this symbol's name and the name's demangled form (if any). */
16369 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
16370 linkagename = dwarf2_physname (name, die, cu);
16371 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
16372
16373 /* Fortran does not have mangling standard and the mangling does differ
16374 between gfortran, iFort etc. */
16375 if (cu->language == language_fortran
16376 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
16377 symbol_set_demangled_name (&(sym->ginfo),
16378 dwarf2_full_name (name, die, cu),
16379 NULL);
16380
16381 /* Default assumptions.
16382 Use the passed type or decode it from the die. */
16383 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16384 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
16385 if (type != NULL)
16386 SYMBOL_TYPE (sym) = type;
16387 else
16388 SYMBOL_TYPE (sym) = die_type (die, cu);
16389 attr = dwarf2_attr (die,
16390 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
16391 cu);
16392 if (attr)
16393 {
16394 SYMBOL_LINE (sym) = DW_UNSND (attr);
16395 }
16396
16397 attr = dwarf2_attr (die,
16398 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
16399 cu);
16400 if (attr)
16401 {
16402 int file_index = DW_UNSND (attr);
16403
16404 if (cu->line_header == NULL
16405 || file_index > cu->line_header->num_file_names)
16406 complaint (&symfile_complaints,
16407 _("file index out of range"));
16408 else if (file_index > 0)
16409 {
16410 struct file_entry *fe;
16411
16412 fe = &cu->line_header->file_names[file_index - 1];
16413 SYMBOL_SYMTAB (sym) = fe->symtab;
16414 }
16415 }
16416
16417 switch (die->tag)
16418 {
16419 case DW_TAG_label:
16420 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
16421 if (attr)
16422 {
16423 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
16424 }
16425 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
16426 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
16427 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
16428 add_symbol_to_list (sym, cu->list_in_scope);
16429 break;
16430 case DW_TAG_subprogram:
16431 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16432 finish_block. */
16433 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
16434 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16435 if ((attr2 && (DW_UNSND (attr2) != 0))
16436 || cu->language == language_ada)
16437 {
16438 /* Subprograms marked external are stored as a global symbol.
16439 Ada subprograms, whether marked external or not, are always
16440 stored as a global symbol, because we want to be able to
16441 access them globally. For instance, we want to be able
16442 to break on a nested subprogram without having to
16443 specify the context. */
16444 list_to_add = &global_symbols;
16445 }
16446 else
16447 {
16448 list_to_add = cu->list_in_scope;
16449 }
16450 break;
16451 case DW_TAG_inlined_subroutine:
16452 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
16453 finish_block. */
16454 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
16455 SYMBOL_INLINED (sym) = 1;
16456 list_to_add = cu->list_in_scope;
16457 break;
16458 case DW_TAG_template_value_param:
16459 suppress_add = 1;
16460 /* Fall through. */
16461 case DW_TAG_constant:
16462 case DW_TAG_variable:
16463 case DW_TAG_member:
16464 /* Compilation with minimal debug info may result in
16465 variables with missing type entries. Change the
16466 misleading `void' type to something sensible. */
16467 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
16468 SYMBOL_TYPE (sym)
16469 = objfile_type (objfile)->nodebug_data_symbol;
16470
16471 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16472 /* In the case of DW_TAG_member, we should only be called for
16473 static const members. */
16474 if (die->tag == DW_TAG_member)
16475 {
16476 /* dwarf2_add_field uses die_is_declaration,
16477 so we do the same. */
16478 gdb_assert (die_is_declaration (die, cu));
16479 gdb_assert (attr);
16480 }
16481 if (attr)
16482 {
16483 dwarf2_const_value (attr, sym, cu);
16484 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16485 if (!suppress_add)
16486 {
16487 if (attr2 && (DW_UNSND (attr2) != 0))
16488 list_to_add = &global_symbols;
16489 else
16490 list_to_add = cu->list_in_scope;
16491 }
16492 break;
16493 }
16494 attr = dwarf2_attr (die, DW_AT_location, cu);
16495 if (attr)
16496 {
16497 var_decode_location (attr, sym, cu);
16498 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16499
16500 /* Fortran explicitly imports any global symbols to the local
16501 scope by DW_TAG_common_block. */
16502 if (cu->language == language_fortran && die->parent
16503 && die->parent->tag == DW_TAG_common_block)
16504 attr2 = NULL;
16505
16506 if (SYMBOL_CLASS (sym) == LOC_STATIC
16507 && SYMBOL_VALUE_ADDRESS (sym) == 0
16508 && !dwarf2_per_objfile->has_section_at_zero)
16509 {
16510 /* When a static variable is eliminated by the linker,
16511 the corresponding debug information is not stripped
16512 out, but the variable address is set to null;
16513 do not add such variables into symbol table. */
16514 }
16515 else if (attr2 && (DW_UNSND (attr2) != 0))
16516 {
16517 /* Workaround gfortran PR debug/40040 - it uses
16518 DW_AT_location for variables in -fPIC libraries which may
16519 get overriden by other libraries/executable and get
16520 a different address. Resolve it by the minimal symbol
16521 which may come from inferior's executable using copy
16522 relocation. Make this workaround only for gfortran as for
16523 other compilers GDB cannot guess the minimal symbol
16524 Fortran mangling kind. */
16525 if (cu->language == language_fortran && die->parent
16526 && die->parent->tag == DW_TAG_module
16527 && cu->producer
16528 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
16529 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16530
16531 /* A variable with DW_AT_external is never static,
16532 but it may be block-scoped. */
16533 list_to_add = (cu->list_in_scope == &file_symbols
16534 ? &global_symbols : cu->list_in_scope);
16535 }
16536 else
16537 list_to_add = cu->list_in_scope;
16538 }
16539 else
16540 {
16541 /* We do not know the address of this symbol.
16542 If it is an external symbol and we have type information
16543 for it, enter the symbol as a LOC_UNRESOLVED symbol.
16544 The address of the variable will then be determined from
16545 the minimal symbol table whenever the variable is
16546 referenced. */
16547 attr2 = dwarf2_attr (die, DW_AT_external, cu);
16548
16549 /* Fortran explicitly imports any global symbols to the local
16550 scope by DW_TAG_common_block. */
16551 if (cu->language == language_fortran && die->parent
16552 && die->parent->tag == DW_TAG_common_block)
16553 {
16554 /* SYMBOL_CLASS doesn't matter here because
16555 read_common_block is going to reset it. */
16556 if (!suppress_add)
16557 list_to_add = cu->list_in_scope;
16558 }
16559 else if (attr2 && (DW_UNSND (attr2) != 0)
16560 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
16561 {
16562 /* A variable with DW_AT_external is never static, but it
16563 may be block-scoped. */
16564 list_to_add = (cu->list_in_scope == &file_symbols
16565 ? &global_symbols : cu->list_in_scope);
16566
16567 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
16568 }
16569 else if (!die_is_declaration (die, cu))
16570 {
16571 /* Use the default LOC_OPTIMIZED_OUT class. */
16572 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
16573 if (!suppress_add)
16574 list_to_add = cu->list_in_scope;
16575 }
16576 }
16577 break;
16578 case DW_TAG_formal_parameter:
16579 /* If we are inside a function, mark this as an argument. If
16580 not, we might be looking at an argument to an inlined function
16581 when we do not have enough information to show inlined frames;
16582 pretend it's a local variable in that case so that the user can
16583 still see it. */
16584 if (context_stack_depth > 0
16585 && context_stack[context_stack_depth - 1].name != NULL)
16586 SYMBOL_IS_ARGUMENT (sym) = 1;
16587 attr = dwarf2_attr (die, DW_AT_location, cu);
16588 if (attr)
16589 {
16590 var_decode_location (attr, sym, cu);
16591 }
16592 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16593 if (attr)
16594 {
16595 dwarf2_const_value (attr, sym, cu);
16596 }
16597
16598 list_to_add = cu->list_in_scope;
16599 break;
16600 case DW_TAG_unspecified_parameters:
16601 /* From varargs functions; gdb doesn't seem to have any
16602 interest in this information, so just ignore it for now.
16603 (FIXME?) */
16604 break;
16605 case DW_TAG_template_type_param:
16606 suppress_add = 1;
16607 /* Fall through. */
16608 case DW_TAG_class_type:
16609 case DW_TAG_interface_type:
16610 case DW_TAG_structure_type:
16611 case DW_TAG_union_type:
16612 case DW_TAG_set_type:
16613 case DW_TAG_enumeration_type:
16614 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16615 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
16616
16617 {
16618 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
16619 really ever be static objects: otherwise, if you try
16620 to, say, break of a class's method and you're in a file
16621 which doesn't mention that class, it won't work unless
16622 the check for all static symbols in lookup_symbol_aux
16623 saves you. See the OtherFileClass tests in
16624 gdb.c++/namespace.exp. */
16625
16626 if (!suppress_add)
16627 {
16628 list_to_add = (cu->list_in_scope == &file_symbols
16629 && (cu->language == language_cplus
16630 || cu->language == language_java)
16631 ? &global_symbols : cu->list_in_scope);
16632
16633 /* The semantics of C++ state that "struct foo {
16634 ... }" also defines a typedef for "foo". A Java
16635 class declaration also defines a typedef for the
16636 class. */
16637 if (cu->language == language_cplus
16638 || cu->language == language_java
16639 || cu->language == language_ada)
16640 {
16641 /* The symbol's name is already allocated along
16642 with this objfile, so we don't need to
16643 duplicate it for the type. */
16644 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
16645 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
16646 }
16647 }
16648 }
16649 break;
16650 case DW_TAG_typedef:
16651 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16652 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16653 list_to_add = cu->list_in_scope;
16654 break;
16655 case DW_TAG_base_type:
16656 case DW_TAG_subrange_type:
16657 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16658 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
16659 list_to_add = cu->list_in_scope;
16660 break;
16661 case DW_TAG_enumerator:
16662 attr = dwarf2_attr (die, DW_AT_const_value, cu);
16663 if (attr)
16664 {
16665 dwarf2_const_value (attr, sym, cu);
16666 }
16667 {
16668 /* NOTE: carlton/2003-11-10: See comment above in the
16669 DW_TAG_class_type, etc. block. */
16670
16671 list_to_add = (cu->list_in_scope == &file_symbols
16672 && (cu->language == language_cplus
16673 || cu->language == language_java)
16674 ? &global_symbols : cu->list_in_scope);
16675 }
16676 break;
16677 case DW_TAG_namespace:
16678 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
16679 list_to_add = &global_symbols;
16680 break;
16681 case DW_TAG_common_block:
16682 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
16683 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
16684 add_symbol_to_list (sym, cu->list_in_scope);
16685 break;
16686 default:
16687 /* Not a tag we recognize. Hopefully we aren't processing
16688 trash data, but since we must specifically ignore things
16689 we don't recognize, there is nothing else we should do at
16690 this point. */
16691 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
16692 dwarf_tag_name (die->tag));
16693 break;
16694 }
16695
16696 if (suppress_add)
16697 {
16698 sym->hash_next = objfile->template_symbols;
16699 objfile->template_symbols = sym;
16700 list_to_add = NULL;
16701 }
16702
16703 if (list_to_add != NULL)
16704 add_symbol_to_list (sym, list_to_add);
16705
16706 /* For the benefit of old versions of GCC, check for anonymous
16707 namespaces based on the demangled name. */
16708 if (!cu->processing_has_namespace_info
16709 && cu->language == language_cplus)
16710 cp_scan_for_anonymous_namespaces (sym, objfile);
16711 }
16712 return (sym);
16713 }
16714
16715 /* A wrapper for new_symbol_full that always allocates a new symbol. */
16716
16717 static struct symbol *
16718 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16719 {
16720 return new_symbol_full (die, type, cu, NULL);
16721 }
16722
16723 /* Given an attr with a DW_FORM_dataN value in host byte order,
16724 zero-extend it as appropriate for the symbol's type. The DWARF
16725 standard (v4) is not entirely clear about the meaning of using
16726 DW_FORM_dataN for a constant with a signed type, where the type is
16727 wider than the data. The conclusion of a discussion on the DWARF
16728 list was that this is unspecified. We choose to always zero-extend
16729 because that is the interpretation long in use by GCC. */
16730
16731 static gdb_byte *
16732 dwarf2_const_value_data (struct attribute *attr, struct obstack *obstack,
16733 struct dwarf2_cu *cu, LONGEST *value, int bits)
16734 {
16735 struct objfile *objfile = cu->objfile;
16736 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
16737 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
16738 LONGEST l = DW_UNSND (attr);
16739
16740 if (bits < sizeof (*value) * 8)
16741 {
16742 l &= ((LONGEST) 1 << bits) - 1;
16743 *value = l;
16744 }
16745 else if (bits == sizeof (*value) * 8)
16746 *value = l;
16747 else
16748 {
16749 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
16750 store_unsigned_integer (bytes, bits / 8, byte_order, l);
16751 return bytes;
16752 }
16753
16754 return NULL;
16755 }
16756
16757 /* Read a constant value from an attribute. Either set *VALUE, or if
16758 the value does not fit in *VALUE, set *BYTES - either already
16759 allocated on the objfile obstack, or newly allocated on OBSTACK,
16760 or, set *BATON, if we translated the constant to a location
16761 expression. */
16762
16763 static void
16764 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
16765 const char *name, struct obstack *obstack,
16766 struct dwarf2_cu *cu,
16767 LONGEST *value, const gdb_byte **bytes,
16768 struct dwarf2_locexpr_baton **baton)
16769 {
16770 struct objfile *objfile = cu->objfile;
16771 struct comp_unit_head *cu_header = &cu->header;
16772 struct dwarf_block *blk;
16773 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
16774 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
16775
16776 *value = 0;
16777 *bytes = NULL;
16778 *baton = NULL;
16779
16780 switch (attr->form)
16781 {
16782 case DW_FORM_addr:
16783 case DW_FORM_GNU_addr_index:
16784 {
16785 gdb_byte *data;
16786
16787 if (TYPE_LENGTH (type) != cu_header->addr_size)
16788 dwarf2_const_value_length_mismatch_complaint (name,
16789 cu_header->addr_size,
16790 TYPE_LENGTH (type));
16791 /* Symbols of this form are reasonably rare, so we just
16792 piggyback on the existing location code rather than writing
16793 a new implementation of symbol_computed_ops. */
16794 *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
16795 (*baton)->per_cu = cu->per_cu;
16796 gdb_assert ((*baton)->per_cu);
16797
16798 (*baton)->size = 2 + cu_header->addr_size;
16799 data = obstack_alloc (obstack, (*baton)->size);
16800 (*baton)->data = data;
16801
16802 data[0] = DW_OP_addr;
16803 store_unsigned_integer (&data[1], cu_header->addr_size,
16804 byte_order, DW_ADDR (attr));
16805 data[cu_header->addr_size + 1] = DW_OP_stack_value;
16806 }
16807 break;
16808 case DW_FORM_string:
16809 case DW_FORM_strp:
16810 case DW_FORM_GNU_str_index:
16811 case DW_FORM_GNU_strp_alt:
16812 /* DW_STRING is already allocated on the objfile obstack, point
16813 directly to it. */
16814 *bytes = (const gdb_byte *) DW_STRING (attr);
16815 break;
16816 case DW_FORM_block1:
16817 case DW_FORM_block2:
16818 case DW_FORM_block4:
16819 case DW_FORM_block:
16820 case DW_FORM_exprloc:
16821 blk = DW_BLOCK (attr);
16822 if (TYPE_LENGTH (type) != blk->size)
16823 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
16824 TYPE_LENGTH (type));
16825 *bytes = blk->data;
16826 break;
16827
16828 /* The DW_AT_const_value attributes are supposed to carry the
16829 symbol's value "represented as it would be on the target
16830 architecture." By the time we get here, it's already been
16831 converted to host endianness, so we just need to sign- or
16832 zero-extend it as appropriate. */
16833 case DW_FORM_data1:
16834 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
16835 break;
16836 case DW_FORM_data2:
16837 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
16838 break;
16839 case DW_FORM_data4:
16840 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
16841 break;
16842 case DW_FORM_data8:
16843 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
16844 break;
16845
16846 case DW_FORM_sdata:
16847 *value = DW_SND (attr);
16848 break;
16849
16850 case DW_FORM_udata:
16851 *value = DW_UNSND (attr);
16852 break;
16853
16854 default:
16855 complaint (&symfile_complaints,
16856 _("unsupported const value attribute form: '%s'"),
16857 dwarf_form_name (attr->form));
16858 *value = 0;
16859 break;
16860 }
16861 }
16862
16863
16864 /* Copy constant value from an attribute to a symbol. */
16865
16866 static void
16867 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
16868 struct dwarf2_cu *cu)
16869 {
16870 struct objfile *objfile = cu->objfile;
16871 struct comp_unit_head *cu_header = &cu->header;
16872 LONGEST value;
16873 const gdb_byte *bytes;
16874 struct dwarf2_locexpr_baton *baton;
16875
16876 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
16877 SYMBOL_PRINT_NAME (sym),
16878 &objfile->objfile_obstack, cu,
16879 &value, &bytes, &baton);
16880
16881 if (baton != NULL)
16882 {
16883 SYMBOL_LOCATION_BATON (sym) = baton;
16884 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16885 }
16886 else if (bytes != NULL)
16887 {
16888 SYMBOL_VALUE_BYTES (sym) = bytes;
16889 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
16890 }
16891 else
16892 {
16893 SYMBOL_VALUE (sym) = value;
16894 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
16895 }
16896 }
16897
16898 /* Return the type of the die in question using its DW_AT_type attribute. */
16899
16900 static struct type *
16901 die_type (struct die_info *die, struct dwarf2_cu *cu)
16902 {
16903 struct attribute *type_attr;
16904
16905 type_attr = dwarf2_attr (die, DW_AT_type, cu);
16906 if (!type_attr)
16907 {
16908 /* A missing DW_AT_type represents a void type. */
16909 return objfile_type (cu->objfile)->builtin_void;
16910 }
16911
16912 return lookup_die_type (die, type_attr, cu);
16913 }
16914
16915 /* True iff CU's producer generates GNAT Ada auxiliary information
16916 that allows to find parallel types through that information instead
16917 of having to do expensive parallel lookups by type name. */
16918
16919 static int
16920 need_gnat_info (struct dwarf2_cu *cu)
16921 {
16922 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
16923 of GNAT produces this auxiliary information, without any indication
16924 that it is produced. Part of enhancing the FSF version of GNAT
16925 to produce that information will be to put in place an indicator
16926 that we can use in order to determine whether the descriptive type
16927 info is available or not. One suggestion that has been made is
16928 to use a new attribute, attached to the CU die. For now, assume
16929 that the descriptive type info is not available. */
16930 return 0;
16931 }
16932
16933 /* Return the auxiliary type of the die in question using its
16934 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
16935 attribute is not present. */
16936
16937 static struct type *
16938 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
16939 {
16940 struct attribute *type_attr;
16941
16942 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
16943 if (!type_attr)
16944 return NULL;
16945
16946 return lookup_die_type (die, type_attr, cu);
16947 }
16948
16949 /* If DIE has a descriptive_type attribute, then set the TYPE's
16950 descriptive type accordingly. */
16951
16952 static void
16953 set_descriptive_type (struct type *type, struct die_info *die,
16954 struct dwarf2_cu *cu)
16955 {
16956 struct type *descriptive_type = die_descriptive_type (die, cu);
16957
16958 if (descriptive_type)
16959 {
16960 ALLOCATE_GNAT_AUX_TYPE (type);
16961 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
16962 }
16963 }
16964
16965 /* Return the containing type of the die in question using its
16966 DW_AT_containing_type attribute. */
16967
16968 static struct type *
16969 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
16970 {
16971 struct attribute *type_attr;
16972
16973 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
16974 if (!type_attr)
16975 error (_("Dwarf Error: Problem turning containing type into gdb type "
16976 "[in module %s]"), cu->objfile->name);
16977
16978 return lookup_die_type (die, type_attr, cu);
16979 }
16980
16981 /* Return an error marker type to use for the ill formed type in DIE/CU. */
16982
16983 static struct type *
16984 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
16985 {
16986 struct objfile *objfile = dwarf2_per_objfile->objfile;
16987 char *message, *saved;
16988
16989 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
16990 objfile->name,
16991 cu->header.offset.sect_off,
16992 die->offset.sect_off);
16993 saved = obstack_copy0 (&objfile->objfile_obstack,
16994 message, strlen (message));
16995 xfree (message);
16996
16997 return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
16998 }
16999
17000 /* Look up the type of DIE in CU using its type attribute ATTR.
17001 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
17002 DW_AT_containing_type.
17003 If there is no type substitute an error marker. */
17004
17005 static struct type *
17006 lookup_die_type (struct die_info *die, struct attribute *attr,
17007 struct dwarf2_cu *cu)
17008 {
17009 struct objfile *objfile = cu->objfile;
17010 struct type *this_type;
17011
17012 gdb_assert (attr->name == DW_AT_type
17013 || attr->name == DW_AT_GNAT_descriptive_type
17014 || attr->name == DW_AT_containing_type);
17015
17016 /* First see if we have it cached. */
17017
17018 if (attr->form == DW_FORM_GNU_ref_alt)
17019 {
17020 struct dwarf2_per_cu_data *per_cu;
17021 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17022
17023 per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
17024 this_type = get_die_type_at_offset (offset, per_cu);
17025 }
17026 else if (is_ref_attr (attr))
17027 {
17028 sect_offset offset = dwarf2_get_ref_die_offset (attr);
17029
17030 this_type = get_die_type_at_offset (offset, cu->per_cu);
17031 }
17032 else if (attr->form == DW_FORM_ref_sig8)
17033 {
17034 ULONGEST signature = DW_SIGNATURE (attr);
17035
17036 return get_signatured_type (die, signature, cu);
17037 }
17038 else
17039 {
17040 complaint (&symfile_complaints,
17041 _("Dwarf Error: Bad type attribute %s in DIE"
17042 " at 0x%x [in module %s]"),
17043 dwarf_attr_name (attr->name), die->offset.sect_off,
17044 objfile->name);
17045 return build_error_marker_type (cu, die);
17046 }
17047
17048 /* If not cached we need to read it in. */
17049
17050 if (this_type == NULL)
17051 {
17052 struct die_info *type_die = NULL;
17053 struct dwarf2_cu *type_cu = cu;
17054
17055 if (is_ref_attr (attr))
17056 type_die = follow_die_ref (die, attr, &type_cu);
17057 if (type_die == NULL)
17058 return build_error_marker_type (cu, die);
17059 /* If we find the type now, it's probably because the type came
17060 from an inter-CU reference and the type's CU got expanded before
17061 ours. */
17062 this_type = read_type_die (type_die, type_cu);
17063 }
17064
17065 /* If we still don't have a type use an error marker. */
17066
17067 if (this_type == NULL)
17068 return build_error_marker_type (cu, die);
17069
17070 return this_type;
17071 }
17072
17073 /* Return the type in DIE, CU.
17074 Returns NULL for invalid types.
17075
17076 This first does a lookup in die_type_hash,
17077 and only reads the die in if necessary.
17078
17079 NOTE: This can be called when reading in partial or full symbols. */
17080
17081 static struct type *
17082 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
17083 {
17084 struct type *this_type;
17085
17086 this_type = get_die_type (die, cu);
17087 if (this_type)
17088 return this_type;
17089
17090 return read_type_die_1 (die, cu);
17091 }
17092
17093 /* Read the type in DIE, CU.
17094 Returns NULL for invalid types. */
17095
17096 static struct type *
17097 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
17098 {
17099 struct type *this_type = NULL;
17100
17101 switch (die->tag)
17102 {
17103 case DW_TAG_class_type:
17104 case DW_TAG_interface_type:
17105 case DW_TAG_structure_type:
17106 case DW_TAG_union_type:
17107 this_type = read_structure_type (die, cu);
17108 break;
17109 case DW_TAG_enumeration_type:
17110 this_type = read_enumeration_type (die, cu);
17111 break;
17112 case DW_TAG_subprogram:
17113 case DW_TAG_subroutine_type:
17114 case DW_TAG_inlined_subroutine:
17115 this_type = read_subroutine_type (die, cu);
17116 break;
17117 case DW_TAG_array_type:
17118 this_type = read_array_type (die, cu);
17119 break;
17120 case DW_TAG_set_type:
17121 this_type = read_set_type (die, cu);
17122 break;
17123 case DW_TAG_pointer_type:
17124 this_type = read_tag_pointer_type (die, cu);
17125 break;
17126 case DW_TAG_ptr_to_member_type:
17127 this_type = read_tag_ptr_to_member_type (die, cu);
17128 break;
17129 case DW_TAG_reference_type:
17130 this_type = read_tag_reference_type (die, cu);
17131 break;
17132 case DW_TAG_const_type:
17133 this_type = read_tag_const_type (die, cu);
17134 break;
17135 case DW_TAG_volatile_type:
17136 this_type = read_tag_volatile_type (die, cu);
17137 break;
17138 case DW_TAG_restrict_type:
17139 this_type = read_tag_restrict_type (die, cu);
17140 break;
17141 case DW_TAG_string_type:
17142 this_type = read_tag_string_type (die, cu);
17143 break;
17144 case DW_TAG_typedef:
17145 this_type = read_typedef (die, cu);
17146 break;
17147 case DW_TAG_subrange_type:
17148 this_type = read_subrange_type (die, cu);
17149 break;
17150 case DW_TAG_base_type:
17151 this_type = read_base_type (die, cu);
17152 break;
17153 case DW_TAG_unspecified_type:
17154 this_type = read_unspecified_type (die, cu);
17155 break;
17156 case DW_TAG_namespace:
17157 this_type = read_namespace_type (die, cu);
17158 break;
17159 case DW_TAG_module:
17160 this_type = read_module_type (die, cu);
17161 break;
17162 default:
17163 complaint (&symfile_complaints,
17164 _("unexpected tag in read_type_die: '%s'"),
17165 dwarf_tag_name (die->tag));
17166 break;
17167 }
17168
17169 return this_type;
17170 }
17171
17172 /* See if we can figure out if the class lives in a namespace. We do
17173 this by looking for a member function; its demangled name will
17174 contain namespace info, if there is any.
17175 Return the computed name or NULL.
17176 Space for the result is allocated on the objfile's obstack.
17177 This is the full-die version of guess_partial_die_structure_name.
17178 In this case we know DIE has no useful parent. */
17179
17180 static char *
17181 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
17182 {
17183 struct die_info *spec_die;
17184 struct dwarf2_cu *spec_cu;
17185 struct die_info *child;
17186
17187 spec_cu = cu;
17188 spec_die = die_specification (die, &spec_cu);
17189 if (spec_die != NULL)
17190 {
17191 die = spec_die;
17192 cu = spec_cu;
17193 }
17194
17195 for (child = die->child;
17196 child != NULL;
17197 child = child->sibling)
17198 {
17199 if (child->tag == DW_TAG_subprogram)
17200 {
17201 struct attribute *attr;
17202
17203 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
17204 if (attr == NULL)
17205 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
17206 if (attr != NULL)
17207 {
17208 char *actual_name
17209 = language_class_name_from_physname (cu->language_defn,
17210 DW_STRING (attr));
17211 char *name = NULL;
17212
17213 if (actual_name != NULL)
17214 {
17215 const char *die_name = dwarf2_name (die, cu);
17216
17217 if (die_name != NULL
17218 && strcmp (die_name, actual_name) != 0)
17219 {
17220 /* Strip off the class name from the full name.
17221 We want the prefix. */
17222 int die_name_len = strlen (die_name);
17223 int actual_name_len = strlen (actual_name);
17224
17225 /* Test for '::' as a sanity check. */
17226 if (actual_name_len > die_name_len + 2
17227 && actual_name[actual_name_len
17228 - die_name_len - 1] == ':')
17229 name =
17230 obstack_copy0 (&cu->objfile->objfile_obstack,
17231 actual_name,
17232 actual_name_len - die_name_len - 2);
17233 }
17234 }
17235 xfree (actual_name);
17236 return name;
17237 }
17238 }
17239 }
17240
17241 return NULL;
17242 }
17243
17244 /* GCC might emit a nameless typedef that has a linkage name. Determine the
17245 prefix part in such case. See
17246 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17247
17248 static char *
17249 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
17250 {
17251 struct attribute *attr;
17252 char *base;
17253
17254 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
17255 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
17256 return NULL;
17257
17258 attr = dwarf2_attr (die, DW_AT_name, cu);
17259 if (attr != NULL && DW_STRING (attr) != NULL)
17260 return NULL;
17261
17262 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17263 if (attr == NULL)
17264 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17265 if (attr == NULL || DW_STRING (attr) == NULL)
17266 return NULL;
17267
17268 /* dwarf2_name had to be already called. */
17269 gdb_assert (DW_STRING_IS_CANONICAL (attr));
17270
17271 /* Strip the base name, keep any leading namespaces/classes. */
17272 base = strrchr (DW_STRING (attr), ':');
17273 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
17274 return "";
17275
17276 return obstack_copy0 (&cu->objfile->objfile_obstack,
17277 DW_STRING (attr), &base[-1] - DW_STRING (attr));
17278 }
17279
17280 /* Return the name of the namespace/class that DIE is defined within,
17281 or "" if we can't tell. The caller should not xfree the result.
17282
17283 For example, if we're within the method foo() in the following
17284 code:
17285
17286 namespace N {
17287 class C {
17288 void foo () {
17289 }
17290 };
17291 }
17292
17293 then determine_prefix on foo's die will return "N::C". */
17294
17295 static const char *
17296 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
17297 {
17298 struct die_info *parent, *spec_die;
17299 struct dwarf2_cu *spec_cu;
17300 struct type *parent_type;
17301 char *retval;
17302
17303 if (cu->language != language_cplus && cu->language != language_java
17304 && cu->language != language_fortran)
17305 return "";
17306
17307 retval = anonymous_struct_prefix (die, cu);
17308 if (retval)
17309 return retval;
17310
17311 /* We have to be careful in the presence of DW_AT_specification.
17312 For example, with GCC 3.4, given the code
17313
17314 namespace N {
17315 void foo() {
17316 // Definition of N::foo.
17317 }
17318 }
17319
17320 then we'll have a tree of DIEs like this:
17321
17322 1: DW_TAG_compile_unit
17323 2: DW_TAG_namespace // N
17324 3: DW_TAG_subprogram // declaration of N::foo
17325 4: DW_TAG_subprogram // definition of N::foo
17326 DW_AT_specification // refers to die #3
17327
17328 Thus, when processing die #4, we have to pretend that we're in
17329 the context of its DW_AT_specification, namely the contex of die
17330 #3. */
17331 spec_cu = cu;
17332 spec_die = die_specification (die, &spec_cu);
17333 if (spec_die == NULL)
17334 parent = die->parent;
17335 else
17336 {
17337 parent = spec_die->parent;
17338 cu = spec_cu;
17339 }
17340
17341 if (parent == NULL)
17342 return "";
17343 else if (parent->building_fullname)
17344 {
17345 const char *name;
17346 const char *parent_name;
17347
17348 /* It has been seen on RealView 2.2 built binaries,
17349 DW_TAG_template_type_param types actually _defined_ as
17350 children of the parent class:
17351
17352 enum E {};
17353 template class <class Enum> Class{};
17354 Class<enum E> class_e;
17355
17356 1: DW_TAG_class_type (Class)
17357 2: DW_TAG_enumeration_type (E)
17358 3: DW_TAG_enumerator (enum1:0)
17359 3: DW_TAG_enumerator (enum2:1)
17360 ...
17361 2: DW_TAG_template_type_param
17362 DW_AT_type DW_FORM_ref_udata (E)
17363
17364 Besides being broken debug info, it can put GDB into an
17365 infinite loop. Consider:
17366
17367 When we're building the full name for Class<E>, we'll start
17368 at Class, and go look over its template type parameters,
17369 finding E. We'll then try to build the full name of E, and
17370 reach here. We're now trying to build the full name of E,
17371 and look over the parent DIE for containing scope. In the
17372 broken case, if we followed the parent DIE of E, we'd again
17373 find Class, and once again go look at its template type
17374 arguments, etc., etc. Simply don't consider such parent die
17375 as source-level parent of this die (it can't be, the language
17376 doesn't allow it), and break the loop here. */
17377 name = dwarf2_name (die, cu);
17378 parent_name = dwarf2_name (parent, cu);
17379 complaint (&symfile_complaints,
17380 _("template param type '%s' defined within parent '%s'"),
17381 name ? name : "<unknown>",
17382 parent_name ? parent_name : "<unknown>");
17383 return "";
17384 }
17385 else
17386 switch (parent->tag)
17387 {
17388 case DW_TAG_namespace:
17389 parent_type = read_type_die (parent, cu);
17390 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
17391 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
17392 Work around this problem here. */
17393 if (cu->language == language_cplus
17394 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
17395 return "";
17396 /* We give a name to even anonymous namespaces. */
17397 return TYPE_TAG_NAME (parent_type);
17398 case DW_TAG_class_type:
17399 case DW_TAG_interface_type:
17400 case DW_TAG_structure_type:
17401 case DW_TAG_union_type:
17402 case DW_TAG_module:
17403 parent_type = read_type_die (parent, cu);
17404 if (TYPE_TAG_NAME (parent_type) != NULL)
17405 return TYPE_TAG_NAME (parent_type);
17406 else
17407 /* An anonymous structure is only allowed non-static data
17408 members; no typedefs, no member functions, et cetera.
17409 So it does not need a prefix. */
17410 return "";
17411 case DW_TAG_compile_unit:
17412 case DW_TAG_partial_unit:
17413 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
17414 if (cu->language == language_cplus
17415 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
17416 && die->child != NULL
17417 && (die->tag == DW_TAG_class_type
17418 || die->tag == DW_TAG_structure_type
17419 || die->tag == DW_TAG_union_type))
17420 {
17421 char *name = guess_full_die_structure_name (die, cu);
17422 if (name != NULL)
17423 return name;
17424 }
17425 return "";
17426 default:
17427 return determine_prefix (parent, cu);
17428 }
17429 }
17430
17431 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
17432 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
17433 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
17434 an obconcat, otherwise allocate storage for the result. The CU argument is
17435 used to determine the language and hence, the appropriate separator. */
17436
17437 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
17438
17439 static char *
17440 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
17441 int physname, struct dwarf2_cu *cu)
17442 {
17443 const char *lead = "";
17444 const char *sep;
17445
17446 if (suffix == NULL || suffix[0] == '\0'
17447 || prefix == NULL || prefix[0] == '\0')
17448 sep = "";
17449 else if (cu->language == language_java)
17450 sep = ".";
17451 else if (cu->language == language_fortran && physname)
17452 {
17453 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
17454 DW_AT_MIPS_linkage_name is preferred and used instead. */
17455
17456 lead = "__";
17457 sep = "_MOD_";
17458 }
17459 else
17460 sep = "::";
17461
17462 if (prefix == NULL)
17463 prefix = "";
17464 if (suffix == NULL)
17465 suffix = "";
17466
17467 if (obs == NULL)
17468 {
17469 char *retval
17470 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
17471
17472 strcpy (retval, lead);
17473 strcat (retval, prefix);
17474 strcat (retval, sep);
17475 strcat (retval, suffix);
17476 return retval;
17477 }
17478 else
17479 {
17480 /* We have an obstack. */
17481 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
17482 }
17483 }
17484
17485 /* Return sibling of die, NULL if no sibling. */
17486
17487 static struct die_info *
17488 sibling_die (struct die_info *die)
17489 {
17490 return die->sibling;
17491 }
17492
17493 /* Get name of a die, return NULL if not found. */
17494
17495 static const char *
17496 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
17497 struct obstack *obstack)
17498 {
17499 if (name && cu->language == language_cplus)
17500 {
17501 char *canon_name = cp_canonicalize_string (name);
17502
17503 if (canon_name != NULL)
17504 {
17505 if (strcmp (canon_name, name) != 0)
17506 name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
17507 xfree (canon_name);
17508 }
17509 }
17510
17511 return name;
17512 }
17513
17514 /* Get name of a die, return NULL if not found. */
17515
17516 static const char *
17517 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
17518 {
17519 struct attribute *attr;
17520
17521 attr = dwarf2_attr (die, DW_AT_name, cu);
17522 if ((!attr || !DW_STRING (attr))
17523 && die->tag != DW_TAG_class_type
17524 && die->tag != DW_TAG_interface_type
17525 && die->tag != DW_TAG_structure_type
17526 && die->tag != DW_TAG_union_type)
17527 return NULL;
17528
17529 switch (die->tag)
17530 {
17531 case DW_TAG_compile_unit:
17532 case DW_TAG_partial_unit:
17533 /* Compilation units have a DW_AT_name that is a filename, not
17534 a source language identifier. */
17535 case DW_TAG_enumeration_type:
17536 case DW_TAG_enumerator:
17537 /* These tags always have simple identifiers already; no need
17538 to canonicalize them. */
17539 return DW_STRING (attr);
17540
17541 case DW_TAG_subprogram:
17542 /* Java constructors will all be named "<init>", so return
17543 the class name when we see this special case. */
17544 if (cu->language == language_java
17545 && DW_STRING (attr) != NULL
17546 && strcmp (DW_STRING (attr), "<init>") == 0)
17547 {
17548 struct dwarf2_cu *spec_cu = cu;
17549 struct die_info *spec_die;
17550
17551 /* GCJ will output '<init>' for Java constructor names.
17552 For this special case, return the name of the parent class. */
17553
17554 /* GCJ may output suprogram DIEs with AT_specification set.
17555 If so, use the name of the specified DIE. */
17556 spec_die = die_specification (die, &spec_cu);
17557 if (spec_die != NULL)
17558 return dwarf2_name (spec_die, spec_cu);
17559
17560 do
17561 {
17562 die = die->parent;
17563 if (die->tag == DW_TAG_class_type)
17564 return dwarf2_name (die, cu);
17565 }
17566 while (die->tag != DW_TAG_compile_unit
17567 && die->tag != DW_TAG_partial_unit);
17568 }
17569 break;
17570
17571 case DW_TAG_class_type:
17572 case DW_TAG_interface_type:
17573 case DW_TAG_structure_type:
17574 case DW_TAG_union_type:
17575 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
17576 structures or unions. These were of the form "._%d" in GCC 4.1,
17577 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
17578 and GCC 4.4. We work around this problem by ignoring these. */
17579 if (attr && DW_STRING (attr)
17580 && (strncmp (DW_STRING (attr), "._", 2) == 0
17581 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
17582 return NULL;
17583
17584 /* GCC might emit a nameless typedef that has a linkage name. See
17585 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
17586 if (!attr || DW_STRING (attr) == NULL)
17587 {
17588 char *demangled = NULL;
17589
17590 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
17591 if (attr == NULL)
17592 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
17593
17594 if (attr == NULL || DW_STRING (attr) == NULL)
17595 return NULL;
17596
17597 /* Avoid demangling DW_STRING (attr) the second time on a second
17598 call for the same DIE. */
17599 if (!DW_STRING_IS_CANONICAL (attr))
17600 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
17601
17602 if (demangled)
17603 {
17604 char *base;
17605
17606 /* FIXME: we already did this for the partial symbol... */
17607 DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
17608 demangled, strlen (demangled));
17609 DW_STRING_IS_CANONICAL (attr) = 1;
17610 xfree (demangled);
17611
17612 /* Strip any leading namespaces/classes, keep only the base name.
17613 DW_AT_name for named DIEs does not contain the prefixes. */
17614 base = strrchr (DW_STRING (attr), ':');
17615 if (base && base > DW_STRING (attr) && base[-1] == ':')
17616 return &base[1];
17617 else
17618 return DW_STRING (attr);
17619 }
17620 }
17621 break;
17622
17623 default:
17624 break;
17625 }
17626
17627 if (!DW_STRING_IS_CANONICAL (attr))
17628 {
17629 DW_STRING (attr)
17630 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
17631 &cu->objfile->objfile_obstack);
17632 DW_STRING_IS_CANONICAL (attr) = 1;
17633 }
17634 return DW_STRING (attr);
17635 }
17636
17637 /* Return the die that this die in an extension of, or NULL if there
17638 is none. *EXT_CU is the CU containing DIE on input, and the CU
17639 containing the return value on output. */
17640
17641 static struct die_info *
17642 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
17643 {
17644 struct attribute *attr;
17645
17646 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
17647 if (attr == NULL)
17648 return NULL;
17649
17650 return follow_die_ref (die, attr, ext_cu);
17651 }
17652
17653 /* Convert a DIE tag into its string name. */
17654
17655 static const char *
17656 dwarf_tag_name (unsigned tag)
17657 {
17658 const char *name = get_DW_TAG_name (tag);
17659
17660 if (name == NULL)
17661 return "DW_TAG_<unknown>";
17662
17663 return name;
17664 }
17665
17666 /* Convert a DWARF attribute code into its string name. */
17667
17668 static const char *
17669 dwarf_attr_name (unsigned attr)
17670 {
17671 const char *name;
17672
17673 #ifdef MIPS /* collides with DW_AT_HP_block_index */
17674 if (attr == DW_AT_MIPS_fde)
17675 return "DW_AT_MIPS_fde";
17676 #else
17677 if (attr == DW_AT_HP_block_index)
17678 return "DW_AT_HP_block_index";
17679 #endif
17680
17681 name = get_DW_AT_name (attr);
17682
17683 if (name == NULL)
17684 return "DW_AT_<unknown>";
17685
17686 return name;
17687 }
17688
17689 /* Convert a DWARF value form code into its string name. */
17690
17691 static const char *
17692 dwarf_form_name (unsigned form)
17693 {
17694 const char *name = get_DW_FORM_name (form);
17695
17696 if (name == NULL)
17697 return "DW_FORM_<unknown>";
17698
17699 return name;
17700 }
17701
17702 static char *
17703 dwarf_bool_name (unsigned mybool)
17704 {
17705 if (mybool)
17706 return "TRUE";
17707 else
17708 return "FALSE";
17709 }
17710
17711 /* Convert a DWARF type code into its string name. */
17712
17713 static const char *
17714 dwarf_type_encoding_name (unsigned enc)
17715 {
17716 const char *name = get_DW_ATE_name (enc);
17717
17718 if (name == NULL)
17719 return "DW_ATE_<unknown>";
17720
17721 return name;
17722 }
17723
17724 static void
17725 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
17726 {
17727 unsigned int i;
17728
17729 print_spaces (indent, f);
17730 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
17731 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
17732
17733 if (die->parent != NULL)
17734 {
17735 print_spaces (indent, f);
17736 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
17737 die->parent->offset.sect_off);
17738 }
17739
17740 print_spaces (indent, f);
17741 fprintf_unfiltered (f, " has children: %s\n",
17742 dwarf_bool_name (die->child != NULL));
17743
17744 print_spaces (indent, f);
17745 fprintf_unfiltered (f, " attributes:\n");
17746
17747 for (i = 0; i < die->num_attrs; ++i)
17748 {
17749 print_spaces (indent, f);
17750 fprintf_unfiltered (f, " %s (%s) ",
17751 dwarf_attr_name (die->attrs[i].name),
17752 dwarf_form_name (die->attrs[i].form));
17753
17754 switch (die->attrs[i].form)
17755 {
17756 case DW_FORM_addr:
17757 case DW_FORM_GNU_addr_index:
17758 fprintf_unfiltered (f, "address: ");
17759 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
17760 break;
17761 case DW_FORM_block2:
17762 case DW_FORM_block4:
17763 case DW_FORM_block:
17764 case DW_FORM_block1:
17765 fprintf_unfiltered (f, "block: size %s",
17766 pulongest (DW_BLOCK (&die->attrs[i])->size));
17767 break;
17768 case DW_FORM_exprloc:
17769 fprintf_unfiltered (f, "expression: size %s",
17770 pulongest (DW_BLOCK (&die->attrs[i])->size));
17771 break;
17772 case DW_FORM_ref_addr:
17773 fprintf_unfiltered (f, "ref address: ");
17774 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17775 break;
17776 case DW_FORM_GNU_ref_alt:
17777 fprintf_unfiltered (f, "alt ref address: ");
17778 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
17779 break;
17780 case DW_FORM_ref1:
17781 case DW_FORM_ref2:
17782 case DW_FORM_ref4:
17783 case DW_FORM_ref8:
17784 case DW_FORM_ref_udata:
17785 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
17786 (long) (DW_UNSND (&die->attrs[i])));
17787 break;
17788 case DW_FORM_data1:
17789 case DW_FORM_data2:
17790 case DW_FORM_data4:
17791 case DW_FORM_data8:
17792 case DW_FORM_udata:
17793 case DW_FORM_sdata:
17794 fprintf_unfiltered (f, "constant: %s",
17795 pulongest (DW_UNSND (&die->attrs[i])));
17796 break;
17797 case DW_FORM_sec_offset:
17798 fprintf_unfiltered (f, "section offset: %s",
17799 pulongest (DW_UNSND (&die->attrs[i])));
17800 break;
17801 case DW_FORM_ref_sig8:
17802 fprintf_unfiltered (f, "signature: %s",
17803 hex_string (DW_SIGNATURE (&die->attrs[i])));
17804 break;
17805 case DW_FORM_string:
17806 case DW_FORM_strp:
17807 case DW_FORM_GNU_str_index:
17808 case DW_FORM_GNU_strp_alt:
17809 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
17810 DW_STRING (&die->attrs[i])
17811 ? DW_STRING (&die->attrs[i]) : "",
17812 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
17813 break;
17814 case DW_FORM_flag:
17815 if (DW_UNSND (&die->attrs[i]))
17816 fprintf_unfiltered (f, "flag: TRUE");
17817 else
17818 fprintf_unfiltered (f, "flag: FALSE");
17819 break;
17820 case DW_FORM_flag_present:
17821 fprintf_unfiltered (f, "flag: TRUE");
17822 break;
17823 case DW_FORM_indirect:
17824 /* The reader will have reduced the indirect form to
17825 the "base form" so this form should not occur. */
17826 fprintf_unfiltered (f,
17827 "unexpected attribute form: DW_FORM_indirect");
17828 break;
17829 default:
17830 fprintf_unfiltered (f, "unsupported attribute form: %d.",
17831 die->attrs[i].form);
17832 break;
17833 }
17834 fprintf_unfiltered (f, "\n");
17835 }
17836 }
17837
17838 static void
17839 dump_die_for_error (struct die_info *die)
17840 {
17841 dump_die_shallow (gdb_stderr, 0, die);
17842 }
17843
17844 static void
17845 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
17846 {
17847 int indent = level * 4;
17848
17849 gdb_assert (die != NULL);
17850
17851 if (level >= max_level)
17852 return;
17853
17854 dump_die_shallow (f, indent, die);
17855
17856 if (die->child != NULL)
17857 {
17858 print_spaces (indent, f);
17859 fprintf_unfiltered (f, " Children:");
17860 if (level + 1 < max_level)
17861 {
17862 fprintf_unfiltered (f, "\n");
17863 dump_die_1 (f, level + 1, max_level, die->child);
17864 }
17865 else
17866 {
17867 fprintf_unfiltered (f,
17868 " [not printed, max nesting level reached]\n");
17869 }
17870 }
17871
17872 if (die->sibling != NULL && level > 0)
17873 {
17874 dump_die_1 (f, level, max_level, die->sibling);
17875 }
17876 }
17877
17878 /* This is called from the pdie macro in gdbinit.in.
17879 It's not static so gcc will keep a copy callable from gdb. */
17880
17881 void
17882 dump_die (struct die_info *die, int max_level)
17883 {
17884 dump_die_1 (gdb_stdlog, 0, max_level, die);
17885 }
17886
17887 static void
17888 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
17889 {
17890 void **slot;
17891
17892 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
17893 INSERT);
17894
17895 *slot = die;
17896 }
17897
17898 /* DW_ADDR is always stored already as sect_offset; despite for the forms
17899 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
17900
17901 static int
17902 is_ref_attr (struct attribute *attr)
17903 {
17904 switch (attr->form)
17905 {
17906 case DW_FORM_ref_addr:
17907 case DW_FORM_ref1:
17908 case DW_FORM_ref2:
17909 case DW_FORM_ref4:
17910 case DW_FORM_ref8:
17911 case DW_FORM_ref_udata:
17912 case DW_FORM_GNU_ref_alt:
17913 return 1;
17914 default:
17915 return 0;
17916 }
17917 }
17918
17919 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
17920 required kind. */
17921
17922 static sect_offset
17923 dwarf2_get_ref_die_offset (struct attribute *attr)
17924 {
17925 sect_offset retval = { DW_UNSND (attr) };
17926
17927 if (is_ref_attr (attr))
17928 return retval;
17929
17930 retval.sect_off = 0;
17931 complaint (&symfile_complaints,
17932 _("unsupported die ref attribute form: '%s'"),
17933 dwarf_form_name (attr->form));
17934 return retval;
17935 }
17936
17937 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
17938 * the value held by the attribute is not constant. */
17939
17940 static LONGEST
17941 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
17942 {
17943 if (attr->form == DW_FORM_sdata)
17944 return DW_SND (attr);
17945 else if (attr->form == DW_FORM_udata
17946 || attr->form == DW_FORM_data1
17947 || attr->form == DW_FORM_data2
17948 || attr->form == DW_FORM_data4
17949 || attr->form == DW_FORM_data8)
17950 return DW_UNSND (attr);
17951 else
17952 {
17953 complaint (&symfile_complaints,
17954 _("Attribute value is not a constant (%s)"),
17955 dwarf_form_name (attr->form));
17956 return default_value;
17957 }
17958 }
17959
17960 /* Follow reference or signature attribute ATTR of SRC_DIE.
17961 On entry *REF_CU is the CU of SRC_DIE.
17962 On exit *REF_CU is the CU of the result. */
17963
17964 static struct die_info *
17965 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
17966 struct dwarf2_cu **ref_cu)
17967 {
17968 struct die_info *die;
17969
17970 if (is_ref_attr (attr))
17971 die = follow_die_ref (src_die, attr, ref_cu);
17972 else if (attr->form == DW_FORM_ref_sig8)
17973 die = follow_die_sig (src_die, attr, ref_cu);
17974 else
17975 {
17976 dump_die_for_error (src_die);
17977 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
17978 (*ref_cu)->objfile->name);
17979 }
17980
17981 return die;
17982 }
17983
17984 /* Follow reference OFFSET.
17985 On entry *REF_CU is the CU of the source die referencing OFFSET.
17986 On exit *REF_CU is the CU of the result.
17987 Returns NULL if OFFSET is invalid. */
17988
17989 static struct die_info *
17990 follow_die_offset (sect_offset offset, int offset_in_dwz,
17991 struct dwarf2_cu **ref_cu)
17992 {
17993 struct die_info temp_die;
17994 struct dwarf2_cu *target_cu, *cu = *ref_cu;
17995
17996 gdb_assert (cu->per_cu != NULL);
17997
17998 target_cu = cu;
17999
18000 if (cu->per_cu->is_debug_types)
18001 {
18002 /* .debug_types CUs cannot reference anything outside their CU.
18003 If they need to, they have to reference a signatured type via
18004 DW_FORM_ref_sig8. */
18005 if (! offset_in_cu_p (&cu->header, offset))
18006 return NULL;
18007 }
18008 else if (offset_in_dwz != cu->per_cu->is_dwz
18009 || ! offset_in_cu_p (&cu->header, offset))
18010 {
18011 struct dwarf2_per_cu_data *per_cu;
18012
18013 per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
18014 cu->objfile);
18015
18016 /* If necessary, add it to the queue and load its DIEs. */
18017 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
18018 load_full_comp_unit (per_cu, cu->language);
18019
18020 target_cu = per_cu->cu;
18021 }
18022 else if (cu->dies == NULL)
18023 {
18024 /* We're loading full DIEs during partial symbol reading. */
18025 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
18026 load_full_comp_unit (cu->per_cu, language_minimal);
18027 }
18028
18029 *ref_cu = target_cu;
18030 temp_die.offset = offset;
18031 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
18032 }
18033
18034 /* Follow reference attribute ATTR of SRC_DIE.
18035 On entry *REF_CU is the CU of SRC_DIE.
18036 On exit *REF_CU is the CU of the result. */
18037
18038 static struct die_info *
18039 follow_die_ref (struct die_info *src_die, struct attribute *attr,
18040 struct dwarf2_cu **ref_cu)
18041 {
18042 sect_offset offset = dwarf2_get_ref_die_offset (attr);
18043 struct dwarf2_cu *cu = *ref_cu;
18044 struct die_info *die;
18045
18046 die = follow_die_offset (offset,
18047 (attr->form == DW_FORM_GNU_ref_alt
18048 || cu->per_cu->is_dwz),
18049 ref_cu);
18050 if (!die)
18051 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
18052 "at 0x%x [in module %s]"),
18053 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
18054
18055 return die;
18056 }
18057
18058 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
18059 Returned value is intended for DW_OP_call*. Returned
18060 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
18061
18062 struct dwarf2_locexpr_baton
18063 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
18064 struct dwarf2_per_cu_data *per_cu,
18065 CORE_ADDR (*get_frame_pc) (void *baton),
18066 void *baton)
18067 {
18068 struct dwarf2_cu *cu;
18069 struct die_info *die;
18070 struct attribute *attr;
18071 struct dwarf2_locexpr_baton retval;
18072
18073 dw2_setup (per_cu->objfile);
18074
18075 if (per_cu->cu == NULL)
18076 load_cu (per_cu);
18077 cu = per_cu->cu;
18078
18079 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18080 if (!die)
18081 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18082 offset.sect_off, per_cu->objfile->name);
18083
18084 attr = dwarf2_attr (die, DW_AT_location, cu);
18085 if (!attr)
18086 {
18087 /* DWARF: "If there is no such attribute, then there is no effect.".
18088 DATA is ignored if SIZE is 0. */
18089
18090 retval.data = NULL;
18091 retval.size = 0;
18092 }
18093 else if (attr_form_is_section_offset (attr))
18094 {
18095 struct dwarf2_loclist_baton loclist_baton;
18096 CORE_ADDR pc = (*get_frame_pc) (baton);
18097 size_t size;
18098
18099 fill_in_loclist_baton (cu, &loclist_baton, attr);
18100
18101 retval.data = dwarf2_find_location_expression (&loclist_baton,
18102 &size, pc);
18103 retval.size = size;
18104 }
18105 else
18106 {
18107 if (!attr_form_is_block (attr))
18108 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
18109 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
18110 offset.sect_off, per_cu->objfile->name);
18111
18112 retval.data = DW_BLOCK (attr)->data;
18113 retval.size = DW_BLOCK (attr)->size;
18114 }
18115 retval.per_cu = cu->per_cu;
18116
18117 age_cached_comp_units ();
18118
18119 return retval;
18120 }
18121
18122 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
18123 offset. */
18124
18125 struct dwarf2_locexpr_baton
18126 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
18127 struct dwarf2_per_cu_data *per_cu,
18128 CORE_ADDR (*get_frame_pc) (void *baton),
18129 void *baton)
18130 {
18131 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
18132
18133 return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
18134 }
18135
18136 /* Write a constant of a given type as target-ordered bytes into
18137 OBSTACK. */
18138
18139 static const gdb_byte *
18140 write_constant_as_bytes (struct obstack *obstack,
18141 enum bfd_endian byte_order,
18142 struct type *type,
18143 ULONGEST value,
18144 LONGEST *len)
18145 {
18146 gdb_byte *result;
18147
18148 *len = TYPE_LENGTH (type);
18149 result = obstack_alloc (obstack, *len);
18150 store_unsigned_integer (result, *len, byte_order, value);
18151
18152 return result;
18153 }
18154
18155 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
18156 pointer to the constant bytes and set LEN to the length of the
18157 data. If memory is needed, allocate it on OBSTACK. If the DIE
18158 does not have a DW_AT_const_value, return NULL. */
18159
18160 const gdb_byte *
18161 dwarf2_fetch_constant_bytes (sect_offset offset,
18162 struct dwarf2_per_cu_data *per_cu,
18163 struct obstack *obstack,
18164 LONGEST *len)
18165 {
18166 struct dwarf2_cu *cu;
18167 struct die_info *die;
18168 struct attribute *attr;
18169 const gdb_byte *result = NULL;
18170 struct type *type;
18171 LONGEST value;
18172 enum bfd_endian byte_order;
18173
18174 dw2_setup (per_cu->objfile);
18175
18176 if (per_cu->cu == NULL)
18177 load_cu (per_cu);
18178 cu = per_cu->cu;
18179
18180 die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18181 if (!die)
18182 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18183 offset.sect_off, per_cu->objfile->name);
18184
18185
18186 attr = dwarf2_attr (die, DW_AT_const_value, cu);
18187 if (attr == NULL)
18188 return NULL;
18189
18190 byte_order = (bfd_big_endian (per_cu->objfile->obfd)
18191 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
18192
18193 switch (attr->form)
18194 {
18195 case DW_FORM_addr:
18196 case DW_FORM_GNU_addr_index:
18197 {
18198 gdb_byte *tem;
18199
18200 *len = cu->header.addr_size;
18201 tem = obstack_alloc (obstack, *len);
18202 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
18203 result = tem;
18204 }
18205 break;
18206 case DW_FORM_string:
18207 case DW_FORM_strp:
18208 case DW_FORM_GNU_str_index:
18209 case DW_FORM_GNU_strp_alt:
18210 /* DW_STRING is already allocated on the objfile obstack, point
18211 directly to it. */
18212 result = (const gdb_byte *) DW_STRING (attr);
18213 *len = strlen (DW_STRING (attr));
18214 break;
18215 case DW_FORM_block1:
18216 case DW_FORM_block2:
18217 case DW_FORM_block4:
18218 case DW_FORM_block:
18219 case DW_FORM_exprloc:
18220 result = DW_BLOCK (attr)->data;
18221 *len = DW_BLOCK (attr)->size;
18222 break;
18223
18224 /* The DW_AT_const_value attributes are supposed to carry the
18225 symbol's value "represented as it would be on the target
18226 architecture." By the time we get here, it's already been
18227 converted to host endianness, so we just need to sign- or
18228 zero-extend it as appropriate. */
18229 case DW_FORM_data1:
18230 type = die_type (die, cu);
18231 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
18232 if (result == NULL)
18233 result = write_constant_as_bytes (obstack, byte_order,
18234 type, value, len);
18235 break;
18236 case DW_FORM_data2:
18237 type = die_type (die, cu);
18238 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
18239 if (result == NULL)
18240 result = write_constant_as_bytes (obstack, byte_order,
18241 type, value, len);
18242 break;
18243 case DW_FORM_data4:
18244 type = die_type (die, cu);
18245 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
18246 if (result == NULL)
18247 result = write_constant_as_bytes (obstack, byte_order,
18248 type, value, len);
18249 break;
18250 case DW_FORM_data8:
18251 type = die_type (die, cu);
18252 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
18253 if (result == NULL)
18254 result = write_constant_as_bytes (obstack, byte_order,
18255 type, value, len);
18256 break;
18257
18258 case DW_FORM_sdata:
18259 type = die_type (die, cu);
18260 result = write_constant_as_bytes (obstack, byte_order,
18261 type, DW_SND (attr), len);
18262 break;
18263
18264 case DW_FORM_udata:
18265 type = die_type (die, cu);
18266 result = write_constant_as_bytes (obstack, byte_order,
18267 type, DW_UNSND (attr), len);
18268 break;
18269
18270 default:
18271 complaint (&symfile_complaints,
18272 _("unsupported const value attribute form: '%s'"),
18273 dwarf_form_name (attr->form));
18274 break;
18275 }
18276
18277 return result;
18278 }
18279
18280 /* Return the type of the DIE at DIE_OFFSET in the CU named by
18281 PER_CU. */
18282
18283 struct type *
18284 dwarf2_get_die_type (cu_offset die_offset,
18285 struct dwarf2_per_cu_data *per_cu)
18286 {
18287 sect_offset die_offset_sect;
18288
18289 dw2_setup (per_cu->objfile);
18290
18291 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
18292 return get_die_type_at_offset (die_offset_sect, per_cu);
18293 }
18294
18295 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
18296 On entry *REF_CU is the CU of SRC_DIE.
18297 On exit *REF_CU is the CU of the result.
18298 Returns NULL if the referenced DIE isn't found. */
18299
18300 static struct die_info *
18301 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
18302 struct dwarf2_cu **ref_cu)
18303 {
18304 struct objfile *objfile = (*ref_cu)->objfile;
18305 struct die_info temp_die;
18306 struct dwarf2_cu *sig_cu;
18307 struct die_info *die;
18308
18309 /* While it might be nice to assert sig_type->type == NULL here,
18310 we can get here for DW_AT_imported_declaration where we need
18311 the DIE not the type. */
18312
18313 /* If necessary, add it to the queue and load its DIEs. */
18314
18315 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
18316 read_signatured_type (sig_type);
18317
18318 gdb_assert (sig_type->per_cu.cu != NULL);
18319
18320 sig_cu = sig_type->per_cu.cu;
18321 gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
18322 temp_die.offset = sig_type->type_offset_in_section;
18323 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
18324 temp_die.offset.sect_off);
18325 if (die)
18326 {
18327 /* For .gdb_index version 7 keep track of included TUs.
18328 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
18329 if (dwarf2_per_objfile->index_table != NULL
18330 && dwarf2_per_objfile->index_table->version <= 7)
18331 {
18332 VEC_safe_push (dwarf2_per_cu_ptr,
18333 (*ref_cu)->per_cu->imported_symtabs,
18334 sig_cu->per_cu);
18335 }
18336
18337 *ref_cu = sig_cu;
18338 return die;
18339 }
18340
18341 return NULL;
18342 }
18343
18344 /* Follow signatured type referenced by ATTR in SRC_DIE.
18345 On entry *REF_CU is the CU of SRC_DIE.
18346 On exit *REF_CU is the CU of the result.
18347 The result is the DIE of the type.
18348 If the referenced type cannot be found an error is thrown. */
18349
18350 static struct die_info *
18351 follow_die_sig (struct die_info *src_die, struct attribute *attr,
18352 struct dwarf2_cu **ref_cu)
18353 {
18354 ULONGEST signature = DW_SIGNATURE (attr);
18355 struct signatured_type *sig_type;
18356 struct die_info *die;
18357
18358 gdb_assert (attr->form == DW_FORM_ref_sig8);
18359
18360 sig_type = lookup_signatured_type (*ref_cu, signature);
18361 /* sig_type will be NULL if the signatured type is missing from
18362 the debug info. */
18363 if (sig_type == NULL)
18364 {
18365 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
18366 " from DIE at 0x%x [in module %s]"),
18367 hex_string (signature), src_die->offset.sect_off,
18368 (*ref_cu)->objfile->name);
18369 }
18370
18371 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
18372 if (die == NULL)
18373 {
18374 dump_die_for_error (src_die);
18375 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
18376 " from DIE at 0x%x [in module %s]"),
18377 hex_string (signature), src_die->offset.sect_off,
18378 (*ref_cu)->objfile->name);
18379 }
18380
18381 return die;
18382 }
18383
18384 /* Get the type specified by SIGNATURE referenced in DIE/CU,
18385 reading in and processing the type unit if necessary. */
18386
18387 static struct type *
18388 get_signatured_type (struct die_info *die, ULONGEST signature,
18389 struct dwarf2_cu *cu)
18390 {
18391 struct signatured_type *sig_type;
18392 struct dwarf2_cu *type_cu;
18393 struct die_info *type_die;
18394 struct type *type;
18395
18396 sig_type = lookup_signatured_type (cu, signature);
18397 /* sig_type will be NULL if the signatured type is missing from
18398 the debug info. */
18399 if (sig_type == NULL)
18400 {
18401 complaint (&symfile_complaints,
18402 _("Dwarf Error: Cannot find signatured DIE %s referenced"
18403 " from DIE at 0x%x [in module %s]"),
18404 hex_string (signature), die->offset.sect_off,
18405 dwarf2_per_objfile->objfile->name);
18406 return build_error_marker_type (cu, die);
18407 }
18408
18409 /* If we already know the type we're done. */
18410 if (sig_type->type != NULL)
18411 return sig_type->type;
18412
18413 type_cu = cu;
18414 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
18415 if (type_die != NULL)
18416 {
18417 /* N.B. We need to call get_die_type to ensure only one type for this DIE
18418 is created. This is important, for example, because for c++ classes
18419 we need TYPE_NAME set which is only done by new_symbol. Blech. */
18420 type = read_type_die (type_die, type_cu);
18421 if (type == NULL)
18422 {
18423 complaint (&symfile_complaints,
18424 _("Dwarf Error: Cannot build signatured type %s"
18425 " referenced from DIE at 0x%x [in module %s]"),
18426 hex_string (signature), die->offset.sect_off,
18427 dwarf2_per_objfile->objfile->name);
18428 type = build_error_marker_type (cu, die);
18429 }
18430 }
18431 else
18432 {
18433 complaint (&symfile_complaints,
18434 _("Dwarf Error: Problem reading signatured DIE %s referenced"
18435 " from DIE at 0x%x [in module %s]"),
18436 hex_string (signature), die->offset.sect_off,
18437 dwarf2_per_objfile->objfile->name);
18438 type = build_error_marker_type (cu, die);
18439 }
18440 sig_type->type = type;
18441
18442 return type;
18443 }
18444
18445 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
18446 reading in and processing the type unit if necessary. */
18447
18448 static struct type *
18449 get_DW_AT_signature_type (struct die_info *die, struct attribute *attr,
18450 struct dwarf2_cu *cu) /* ARI: editCase function */
18451 {
18452 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
18453 if (is_ref_attr (attr))
18454 {
18455 struct dwarf2_cu *type_cu = cu;
18456 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
18457
18458 return read_type_die (type_die, type_cu);
18459 }
18460 else if (attr->form == DW_FORM_ref_sig8)
18461 {
18462 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
18463 }
18464 else
18465 {
18466 complaint (&symfile_complaints,
18467 _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
18468 " at 0x%x [in module %s]"),
18469 dwarf_form_name (attr->form), die->offset.sect_off,
18470 dwarf2_per_objfile->objfile->name);
18471 return build_error_marker_type (cu, die);
18472 }
18473 }
18474
18475 /* Load the DIEs associated with type unit PER_CU into memory. */
18476
18477 static void
18478 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
18479 {
18480 struct signatured_type *sig_type;
18481
18482 /* Caller is responsible for ensuring type_unit_groups don't get here. */
18483 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
18484
18485 /* We have the per_cu, but we need the signatured_type.
18486 Fortunately this is an easy translation. */
18487 gdb_assert (per_cu->is_debug_types);
18488 sig_type = (struct signatured_type *) per_cu;
18489
18490 gdb_assert (per_cu->cu == NULL);
18491
18492 read_signatured_type (sig_type);
18493
18494 gdb_assert (per_cu->cu != NULL);
18495 }
18496
18497 /* die_reader_func for read_signatured_type.
18498 This is identical to load_full_comp_unit_reader,
18499 but is kept separate for now. */
18500
18501 static void
18502 read_signatured_type_reader (const struct die_reader_specs *reader,
18503 const gdb_byte *info_ptr,
18504 struct die_info *comp_unit_die,
18505 int has_children,
18506 void *data)
18507 {
18508 struct dwarf2_cu *cu = reader->cu;
18509
18510 gdb_assert (cu->die_hash == NULL);
18511 cu->die_hash =
18512 htab_create_alloc_ex (cu->header.length / 12,
18513 die_hash,
18514 die_eq,
18515 NULL,
18516 &cu->comp_unit_obstack,
18517 hashtab_obstack_allocate,
18518 dummy_obstack_deallocate);
18519
18520 if (has_children)
18521 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
18522 &info_ptr, comp_unit_die);
18523 cu->dies = comp_unit_die;
18524 /* comp_unit_die is not stored in die_hash, no need. */
18525
18526 /* We try not to read any attributes in this function, because not
18527 all CUs needed for references have been loaded yet, and symbol
18528 table processing isn't initialized. But we have to set the CU language,
18529 or we won't be able to build types correctly.
18530 Similarly, if we do not read the producer, we can not apply
18531 producer-specific interpretation. */
18532 prepare_one_comp_unit (cu, cu->dies, language_minimal);
18533 }
18534
18535 /* Read in a signatured type and build its CU and DIEs.
18536 If the type is a stub for the real type in a DWO file,
18537 read in the real type from the DWO file as well. */
18538
18539 static void
18540 read_signatured_type (struct signatured_type *sig_type)
18541 {
18542 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
18543
18544 gdb_assert (per_cu->is_debug_types);
18545 gdb_assert (per_cu->cu == NULL);
18546
18547 init_cutu_and_read_dies (per_cu, NULL, 0, 1,
18548 read_signatured_type_reader, NULL);
18549 }
18550
18551 /* Decode simple location descriptions.
18552 Given a pointer to a dwarf block that defines a location, compute
18553 the location and return the value.
18554
18555 NOTE drow/2003-11-18: This function is called in two situations
18556 now: for the address of static or global variables (partial symbols
18557 only) and for offsets into structures which are expected to be
18558 (more or less) constant. The partial symbol case should go away,
18559 and only the constant case should remain. That will let this
18560 function complain more accurately. A few special modes are allowed
18561 without complaint for global variables (for instance, global
18562 register values and thread-local values).
18563
18564 A location description containing no operations indicates that the
18565 object is optimized out. The return value is 0 for that case.
18566 FIXME drow/2003-11-16: No callers check for this case any more; soon all
18567 callers will only want a very basic result and this can become a
18568 complaint.
18569
18570 Note that stack[0] is unused except as a default error return. */
18571
18572 static CORE_ADDR
18573 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
18574 {
18575 struct objfile *objfile = cu->objfile;
18576 size_t i;
18577 size_t size = blk->size;
18578 const gdb_byte *data = blk->data;
18579 CORE_ADDR stack[64];
18580 int stacki;
18581 unsigned int bytes_read, unsnd;
18582 gdb_byte op;
18583
18584 i = 0;
18585 stacki = 0;
18586 stack[stacki] = 0;
18587 stack[++stacki] = 0;
18588
18589 while (i < size)
18590 {
18591 op = data[i++];
18592 switch (op)
18593 {
18594 case DW_OP_lit0:
18595 case DW_OP_lit1:
18596 case DW_OP_lit2:
18597 case DW_OP_lit3:
18598 case DW_OP_lit4:
18599 case DW_OP_lit5:
18600 case DW_OP_lit6:
18601 case DW_OP_lit7:
18602 case DW_OP_lit8:
18603 case DW_OP_lit9:
18604 case DW_OP_lit10:
18605 case DW_OP_lit11:
18606 case DW_OP_lit12:
18607 case DW_OP_lit13:
18608 case DW_OP_lit14:
18609 case DW_OP_lit15:
18610 case DW_OP_lit16:
18611 case DW_OP_lit17:
18612 case DW_OP_lit18:
18613 case DW_OP_lit19:
18614 case DW_OP_lit20:
18615 case DW_OP_lit21:
18616 case DW_OP_lit22:
18617 case DW_OP_lit23:
18618 case DW_OP_lit24:
18619 case DW_OP_lit25:
18620 case DW_OP_lit26:
18621 case DW_OP_lit27:
18622 case DW_OP_lit28:
18623 case DW_OP_lit29:
18624 case DW_OP_lit30:
18625 case DW_OP_lit31:
18626 stack[++stacki] = op - DW_OP_lit0;
18627 break;
18628
18629 case DW_OP_reg0:
18630 case DW_OP_reg1:
18631 case DW_OP_reg2:
18632 case DW_OP_reg3:
18633 case DW_OP_reg4:
18634 case DW_OP_reg5:
18635 case DW_OP_reg6:
18636 case DW_OP_reg7:
18637 case DW_OP_reg8:
18638 case DW_OP_reg9:
18639 case DW_OP_reg10:
18640 case DW_OP_reg11:
18641 case DW_OP_reg12:
18642 case DW_OP_reg13:
18643 case DW_OP_reg14:
18644 case DW_OP_reg15:
18645 case DW_OP_reg16:
18646 case DW_OP_reg17:
18647 case DW_OP_reg18:
18648 case DW_OP_reg19:
18649 case DW_OP_reg20:
18650 case DW_OP_reg21:
18651 case DW_OP_reg22:
18652 case DW_OP_reg23:
18653 case DW_OP_reg24:
18654 case DW_OP_reg25:
18655 case DW_OP_reg26:
18656 case DW_OP_reg27:
18657 case DW_OP_reg28:
18658 case DW_OP_reg29:
18659 case DW_OP_reg30:
18660 case DW_OP_reg31:
18661 stack[++stacki] = op - DW_OP_reg0;
18662 if (i < size)
18663 dwarf2_complex_location_expr_complaint ();
18664 break;
18665
18666 case DW_OP_regx:
18667 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
18668 i += bytes_read;
18669 stack[++stacki] = unsnd;
18670 if (i < size)
18671 dwarf2_complex_location_expr_complaint ();
18672 break;
18673
18674 case DW_OP_addr:
18675 stack[++stacki] = read_address (objfile->obfd, &data[i],
18676 cu, &bytes_read);
18677 i += bytes_read;
18678 break;
18679
18680 case DW_OP_const1u:
18681 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
18682 i += 1;
18683 break;
18684
18685 case DW_OP_const1s:
18686 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
18687 i += 1;
18688 break;
18689
18690 case DW_OP_const2u:
18691 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
18692 i += 2;
18693 break;
18694
18695 case DW_OP_const2s:
18696 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
18697 i += 2;
18698 break;
18699
18700 case DW_OP_const4u:
18701 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
18702 i += 4;
18703 break;
18704
18705 case DW_OP_const4s:
18706 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
18707 i += 4;
18708 break;
18709
18710 case DW_OP_const8u:
18711 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
18712 i += 8;
18713 break;
18714
18715 case DW_OP_constu:
18716 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
18717 &bytes_read);
18718 i += bytes_read;
18719 break;
18720
18721 case DW_OP_consts:
18722 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
18723 i += bytes_read;
18724 break;
18725
18726 case DW_OP_dup:
18727 stack[stacki + 1] = stack[stacki];
18728 stacki++;
18729 break;
18730
18731 case DW_OP_plus:
18732 stack[stacki - 1] += stack[stacki];
18733 stacki--;
18734 break;
18735
18736 case DW_OP_plus_uconst:
18737 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
18738 &bytes_read);
18739 i += bytes_read;
18740 break;
18741
18742 case DW_OP_minus:
18743 stack[stacki - 1] -= stack[stacki];
18744 stacki--;
18745 break;
18746
18747 case DW_OP_deref:
18748 /* If we're not the last op, then we definitely can't encode
18749 this using GDB's address_class enum. This is valid for partial
18750 global symbols, although the variable's address will be bogus
18751 in the psymtab. */
18752 if (i < size)
18753 dwarf2_complex_location_expr_complaint ();
18754 break;
18755
18756 case DW_OP_GNU_push_tls_address:
18757 /* The top of the stack has the offset from the beginning
18758 of the thread control block at which the variable is located. */
18759 /* Nothing should follow this operator, so the top of stack would
18760 be returned. */
18761 /* This is valid for partial global symbols, but the variable's
18762 address will be bogus in the psymtab. Make it always at least
18763 non-zero to not look as a variable garbage collected by linker
18764 which have DW_OP_addr 0. */
18765 if (i < size)
18766 dwarf2_complex_location_expr_complaint ();
18767 stack[stacki]++;
18768 break;
18769
18770 case DW_OP_GNU_uninit:
18771 break;
18772
18773 case DW_OP_GNU_addr_index:
18774 case DW_OP_GNU_const_index:
18775 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
18776 &bytes_read);
18777 i += bytes_read;
18778 break;
18779
18780 default:
18781 {
18782 const char *name = get_DW_OP_name (op);
18783
18784 if (name)
18785 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
18786 name);
18787 else
18788 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
18789 op);
18790 }
18791
18792 return (stack[stacki]);
18793 }
18794
18795 /* Enforce maximum stack depth of SIZE-1 to avoid writing
18796 outside of the allocated space. Also enforce minimum>0. */
18797 if (stacki >= ARRAY_SIZE (stack) - 1)
18798 {
18799 complaint (&symfile_complaints,
18800 _("location description stack overflow"));
18801 return 0;
18802 }
18803
18804 if (stacki <= 0)
18805 {
18806 complaint (&symfile_complaints,
18807 _("location description stack underflow"));
18808 return 0;
18809 }
18810 }
18811 return (stack[stacki]);
18812 }
18813
18814 /* memory allocation interface */
18815
18816 static struct dwarf_block *
18817 dwarf_alloc_block (struct dwarf2_cu *cu)
18818 {
18819 struct dwarf_block *blk;
18820
18821 blk = (struct dwarf_block *)
18822 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
18823 return (blk);
18824 }
18825
18826 static struct die_info *
18827 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
18828 {
18829 struct die_info *die;
18830 size_t size = sizeof (struct die_info);
18831
18832 if (num_attrs > 1)
18833 size += (num_attrs - 1) * sizeof (struct attribute);
18834
18835 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
18836 memset (die, 0, sizeof (struct die_info));
18837 return (die);
18838 }
18839
18840 \f
18841 /* Macro support. */
18842
18843 /* Return file name relative to the compilation directory of file number I in
18844 *LH's file name table. The result is allocated using xmalloc; the caller is
18845 responsible for freeing it. */
18846
18847 static char *
18848 file_file_name (int file, struct line_header *lh)
18849 {
18850 /* Is the file number a valid index into the line header's file name
18851 table? Remember that file numbers start with one, not zero. */
18852 if (1 <= file && file <= lh->num_file_names)
18853 {
18854 struct file_entry *fe = &lh->file_names[file - 1];
18855
18856 if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
18857 return xstrdup (fe->name);
18858 return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
18859 fe->name, NULL);
18860 }
18861 else
18862 {
18863 /* The compiler produced a bogus file number. We can at least
18864 record the macro definitions made in the file, even if we
18865 won't be able to find the file by name. */
18866 char fake_name[80];
18867
18868 xsnprintf (fake_name, sizeof (fake_name),
18869 "<bad macro file number %d>", file);
18870
18871 complaint (&symfile_complaints,
18872 _("bad file number in macro information (%d)"),
18873 file);
18874
18875 return xstrdup (fake_name);
18876 }
18877 }
18878
18879 /* Return the full name of file number I in *LH's file name table.
18880 Use COMP_DIR as the name of the current directory of the
18881 compilation. The result is allocated using xmalloc; the caller is
18882 responsible for freeing it. */
18883 static char *
18884 file_full_name (int file, struct line_header *lh, const char *comp_dir)
18885 {
18886 /* Is the file number a valid index into the line header's file name
18887 table? Remember that file numbers start with one, not zero. */
18888 if (1 <= file && file <= lh->num_file_names)
18889 {
18890 char *relative = file_file_name (file, lh);
18891
18892 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
18893 return relative;
18894 return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
18895 }
18896 else
18897 return file_file_name (file, lh);
18898 }
18899
18900
18901 static struct macro_source_file *
18902 macro_start_file (int file, int line,
18903 struct macro_source_file *current_file,
18904 const char *comp_dir,
18905 struct line_header *lh, struct objfile *objfile)
18906 {
18907 /* File name relative to the compilation directory of this source file. */
18908 char *file_name = file_file_name (file, lh);
18909
18910 /* We don't create a macro table for this compilation unit
18911 at all until we actually get a filename. */
18912 if (! pending_macros)
18913 pending_macros = new_macro_table (&objfile->per_bfd->storage_obstack,
18914 objfile->per_bfd->macro_cache,
18915 comp_dir);
18916
18917 if (! current_file)
18918 {
18919 /* If we have no current file, then this must be the start_file
18920 directive for the compilation unit's main source file. */
18921 current_file = macro_set_main (pending_macros, file_name);
18922 macro_define_special (pending_macros);
18923 }
18924 else
18925 current_file = macro_include (current_file, line, file_name);
18926
18927 xfree (file_name);
18928
18929 return current_file;
18930 }
18931
18932
18933 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
18934 followed by a null byte. */
18935 static char *
18936 copy_string (const char *buf, int len)
18937 {
18938 char *s = xmalloc (len + 1);
18939
18940 memcpy (s, buf, len);
18941 s[len] = '\0';
18942 return s;
18943 }
18944
18945
18946 static const char *
18947 consume_improper_spaces (const char *p, const char *body)
18948 {
18949 if (*p == ' ')
18950 {
18951 complaint (&symfile_complaints,
18952 _("macro definition contains spaces "
18953 "in formal argument list:\n`%s'"),
18954 body);
18955
18956 while (*p == ' ')
18957 p++;
18958 }
18959
18960 return p;
18961 }
18962
18963
18964 static void
18965 parse_macro_definition (struct macro_source_file *file, int line,
18966 const char *body)
18967 {
18968 const char *p;
18969
18970 /* The body string takes one of two forms. For object-like macro
18971 definitions, it should be:
18972
18973 <macro name> " " <definition>
18974
18975 For function-like macro definitions, it should be:
18976
18977 <macro name> "() " <definition>
18978 or
18979 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
18980
18981 Spaces may appear only where explicitly indicated, and in the
18982 <definition>.
18983
18984 The Dwarf 2 spec says that an object-like macro's name is always
18985 followed by a space, but versions of GCC around March 2002 omit
18986 the space when the macro's definition is the empty string.
18987
18988 The Dwarf 2 spec says that there should be no spaces between the
18989 formal arguments in a function-like macro's formal argument list,
18990 but versions of GCC around March 2002 include spaces after the
18991 commas. */
18992
18993
18994 /* Find the extent of the macro name. The macro name is terminated
18995 by either a space or null character (for an object-like macro) or
18996 an opening paren (for a function-like macro). */
18997 for (p = body; *p; p++)
18998 if (*p == ' ' || *p == '(')
18999 break;
19000
19001 if (*p == ' ' || *p == '\0')
19002 {
19003 /* It's an object-like macro. */
19004 int name_len = p - body;
19005 char *name = copy_string (body, name_len);
19006 const char *replacement;
19007
19008 if (*p == ' ')
19009 replacement = body + name_len + 1;
19010 else
19011 {
19012 dwarf2_macro_malformed_definition_complaint (body);
19013 replacement = body + name_len;
19014 }
19015
19016 macro_define_object (file, line, name, replacement);
19017
19018 xfree (name);
19019 }
19020 else if (*p == '(')
19021 {
19022 /* It's a function-like macro. */
19023 char *name = copy_string (body, p - body);
19024 int argc = 0;
19025 int argv_size = 1;
19026 char **argv = xmalloc (argv_size * sizeof (*argv));
19027
19028 p++;
19029
19030 p = consume_improper_spaces (p, body);
19031
19032 /* Parse the formal argument list. */
19033 while (*p && *p != ')')
19034 {
19035 /* Find the extent of the current argument name. */
19036 const char *arg_start = p;
19037
19038 while (*p && *p != ',' && *p != ')' && *p != ' ')
19039 p++;
19040
19041 if (! *p || p == arg_start)
19042 dwarf2_macro_malformed_definition_complaint (body);
19043 else
19044 {
19045 /* Make sure argv has room for the new argument. */
19046 if (argc >= argv_size)
19047 {
19048 argv_size *= 2;
19049 argv = xrealloc (argv, argv_size * sizeof (*argv));
19050 }
19051
19052 argv[argc++] = copy_string (arg_start, p - arg_start);
19053 }
19054
19055 p = consume_improper_spaces (p, body);
19056
19057 /* Consume the comma, if present. */
19058 if (*p == ',')
19059 {
19060 p++;
19061
19062 p = consume_improper_spaces (p, body);
19063 }
19064 }
19065
19066 if (*p == ')')
19067 {
19068 p++;
19069
19070 if (*p == ' ')
19071 /* Perfectly formed definition, no complaints. */
19072 macro_define_function (file, line, name,
19073 argc, (const char **) argv,
19074 p + 1);
19075 else if (*p == '\0')
19076 {
19077 /* Complain, but do define it. */
19078 dwarf2_macro_malformed_definition_complaint (body);
19079 macro_define_function (file, line, name,
19080 argc, (const char **) argv,
19081 p);
19082 }
19083 else
19084 /* Just complain. */
19085 dwarf2_macro_malformed_definition_complaint (body);
19086 }
19087 else
19088 /* Just complain. */
19089 dwarf2_macro_malformed_definition_complaint (body);
19090
19091 xfree (name);
19092 {
19093 int i;
19094
19095 for (i = 0; i < argc; i++)
19096 xfree (argv[i]);
19097 }
19098 xfree (argv);
19099 }
19100 else
19101 dwarf2_macro_malformed_definition_complaint (body);
19102 }
19103
19104 /* Skip some bytes from BYTES according to the form given in FORM.
19105 Returns the new pointer. */
19106
19107 static const gdb_byte *
19108 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
19109 enum dwarf_form form,
19110 unsigned int offset_size,
19111 struct dwarf2_section_info *section)
19112 {
19113 unsigned int bytes_read;
19114
19115 switch (form)
19116 {
19117 case DW_FORM_data1:
19118 case DW_FORM_flag:
19119 ++bytes;
19120 break;
19121
19122 case DW_FORM_data2:
19123 bytes += 2;
19124 break;
19125
19126 case DW_FORM_data4:
19127 bytes += 4;
19128 break;
19129
19130 case DW_FORM_data8:
19131 bytes += 8;
19132 break;
19133
19134 case DW_FORM_string:
19135 read_direct_string (abfd, bytes, &bytes_read);
19136 bytes += bytes_read;
19137 break;
19138
19139 case DW_FORM_sec_offset:
19140 case DW_FORM_strp:
19141 case DW_FORM_GNU_strp_alt:
19142 bytes += offset_size;
19143 break;
19144
19145 case DW_FORM_block:
19146 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
19147 bytes += bytes_read;
19148 break;
19149
19150 case DW_FORM_block1:
19151 bytes += 1 + read_1_byte (abfd, bytes);
19152 break;
19153 case DW_FORM_block2:
19154 bytes += 2 + read_2_bytes (abfd, bytes);
19155 break;
19156 case DW_FORM_block4:
19157 bytes += 4 + read_4_bytes (abfd, bytes);
19158 break;
19159
19160 case DW_FORM_sdata:
19161 case DW_FORM_udata:
19162 case DW_FORM_GNU_addr_index:
19163 case DW_FORM_GNU_str_index:
19164 bytes = gdb_skip_leb128 (bytes, buffer_end);
19165 if (bytes == NULL)
19166 {
19167 dwarf2_section_buffer_overflow_complaint (section);
19168 return NULL;
19169 }
19170 break;
19171
19172 default:
19173 {
19174 complain:
19175 complaint (&symfile_complaints,
19176 _("invalid form 0x%x in `%s'"),
19177 form,
19178 section->asection->name);
19179 return NULL;
19180 }
19181 }
19182
19183 return bytes;
19184 }
19185
19186 /* A helper for dwarf_decode_macros that handles skipping an unknown
19187 opcode. Returns an updated pointer to the macro data buffer; or,
19188 on error, issues a complaint and returns NULL. */
19189
19190 static const gdb_byte *
19191 skip_unknown_opcode (unsigned int opcode,
19192 const gdb_byte **opcode_definitions,
19193 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
19194 bfd *abfd,
19195 unsigned int offset_size,
19196 struct dwarf2_section_info *section)
19197 {
19198 unsigned int bytes_read, i;
19199 unsigned long arg;
19200 const gdb_byte *defn;
19201
19202 if (opcode_definitions[opcode] == NULL)
19203 {
19204 complaint (&symfile_complaints,
19205 _("unrecognized DW_MACFINO opcode 0x%x"),
19206 opcode);
19207 return NULL;
19208 }
19209
19210 defn = opcode_definitions[opcode];
19211 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
19212 defn += bytes_read;
19213
19214 for (i = 0; i < arg; ++i)
19215 {
19216 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
19217 section);
19218 if (mac_ptr == NULL)
19219 {
19220 /* skip_form_bytes already issued the complaint. */
19221 return NULL;
19222 }
19223 }
19224
19225 return mac_ptr;
19226 }
19227
19228 /* A helper function which parses the header of a macro section.
19229 If the macro section is the extended (for now called "GNU") type,
19230 then this updates *OFFSET_SIZE. Returns a pointer to just after
19231 the header, or issues a complaint and returns NULL on error. */
19232
19233 static const gdb_byte *
19234 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
19235 bfd *abfd,
19236 const gdb_byte *mac_ptr,
19237 unsigned int *offset_size,
19238 int section_is_gnu)
19239 {
19240 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
19241
19242 if (section_is_gnu)
19243 {
19244 unsigned int version, flags;
19245
19246 version = read_2_bytes (abfd, mac_ptr);
19247 if (version != 4)
19248 {
19249 complaint (&symfile_complaints,
19250 _("unrecognized version `%d' in .debug_macro section"),
19251 version);
19252 return NULL;
19253 }
19254 mac_ptr += 2;
19255
19256 flags = read_1_byte (abfd, mac_ptr);
19257 ++mac_ptr;
19258 *offset_size = (flags & 1) ? 8 : 4;
19259
19260 if ((flags & 2) != 0)
19261 /* We don't need the line table offset. */
19262 mac_ptr += *offset_size;
19263
19264 /* Vendor opcode descriptions. */
19265 if ((flags & 4) != 0)
19266 {
19267 unsigned int i, count;
19268
19269 count = read_1_byte (abfd, mac_ptr);
19270 ++mac_ptr;
19271 for (i = 0; i < count; ++i)
19272 {
19273 unsigned int opcode, bytes_read;
19274 unsigned long arg;
19275
19276 opcode = read_1_byte (abfd, mac_ptr);
19277 ++mac_ptr;
19278 opcode_definitions[opcode] = mac_ptr;
19279 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19280 mac_ptr += bytes_read;
19281 mac_ptr += arg;
19282 }
19283 }
19284 }
19285
19286 return mac_ptr;
19287 }
19288
19289 /* A helper for dwarf_decode_macros that handles the GNU extensions,
19290 including DW_MACRO_GNU_transparent_include. */
19291
19292 static void
19293 dwarf_decode_macro_bytes (bfd *abfd,
19294 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
19295 struct macro_source_file *current_file,
19296 struct line_header *lh, const char *comp_dir,
19297 struct dwarf2_section_info *section,
19298 int section_is_gnu, int section_is_dwz,
19299 unsigned int offset_size,
19300 struct objfile *objfile,
19301 htab_t include_hash)
19302 {
19303 enum dwarf_macro_record_type macinfo_type;
19304 int at_commandline;
19305 const gdb_byte *opcode_definitions[256];
19306
19307 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19308 &offset_size, section_is_gnu);
19309 if (mac_ptr == NULL)
19310 {
19311 /* We already issued a complaint. */
19312 return;
19313 }
19314
19315 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
19316 GDB is still reading the definitions from command line. First
19317 DW_MACINFO_start_file will need to be ignored as it was already executed
19318 to create CURRENT_FILE for the main source holding also the command line
19319 definitions. On first met DW_MACINFO_start_file this flag is reset to
19320 normally execute all the remaining DW_MACINFO_start_file macinfos. */
19321
19322 at_commandline = 1;
19323
19324 do
19325 {
19326 /* Do we at least have room for a macinfo type byte? */
19327 if (mac_ptr >= mac_end)
19328 {
19329 dwarf2_section_buffer_overflow_complaint (section);
19330 break;
19331 }
19332
19333 macinfo_type = read_1_byte (abfd, mac_ptr);
19334 mac_ptr++;
19335
19336 /* Note that we rely on the fact that the corresponding GNU and
19337 DWARF constants are the same. */
19338 switch (macinfo_type)
19339 {
19340 /* A zero macinfo type indicates the end of the macro
19341 information. */
19342 case 0:
19343 break;
19344
19345 case DW_MACRO_GNU_define:
19346 case DW_MACRO_GNU_undef:
19347 case DW_MACRO_GNU_define_indirect:
19348 case DW_MACRO_GNU_undef_indirect:
19349 case DW_MACRO_GNU_define_indirect_alt:
19350 case DW_MACRO_GNU_undef_indirect_alt:
19351 {
19352 unsigned int bytes_read;
19353 int line;
19354 const char *body;
19355 int is_define;
19356
19357 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19358 mac_ptr += bytes_read;
19359
19360 if (macinfo_type == DW_MACRO_GNU_define
19361 || macinfo_type == DW_MACRO_GNU_undef)
19362 {
19363 body = read_direct_string (abfd, mac_ptr, &bytes_read);
19364 mac_ptr += bytes_read;
19365 }
19366 else
19367 {
19368 LONGEST str_offset;
19369
19370 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
19371 mac_ptr += offset_size;
19372
19373 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
19374 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
19375 || section_is_dwz)
19376 {
19377 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19378
19379 body = read_indirect_string_from_dwz (dwz, str_offset);
19380 }
19381 else
19382 body = read_indirect_string_at_offset (abfd, str_offset);
19383 }
19384
19385 is_define = (macinfo_type == DW_MACRO_GNU_define
19386 || macinfo_type == DW_MACRO_GNU_define_indirect
19387 || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
19388 if (! current_file)
19389 {
19390 /* DWARF violation as no main source is present. */
19391 complaint (&symfile_complaints,
19392 _("debug info with no main source gives macro %s "
19393 "on line %d: %s"),
19394 is_define ? _("definition") : _("undefinition"),
19395 line, body);
19396 break;
19397 }
19398 if ((line == 0 && !at_commandline)
19399 || (line != 0 && at_commandline))
19400 complaint (&symfile_complaints,
19401 _("debug info gives %s macro %s with %s line %d: %s"),
19402 at_commandline ? _("command-line") : _("in-file"),
19403 is_define ? _("definition") : _("undefinition"),
19404 line == 0 ? _("zero") : _("non-zero"), line, body);
19405
19406 if (is_define)
19407 parse_macro_definition (current_file, line, body);
19408 else
19409 {
19410 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
19411 || macinfo_type == DW_MACRO_GNU_undef_indirect
19412 || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
19413 macro_undef (current_file, line, body);
19414 }
19415 }
19416 break;
19417
19418 case DW_MACRO_GNU_start_file:
19419 {
19420 unsigned int bytes_read;
19421 int line, file;
19422
19423 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19424 mac_ptr += bytes_read;
19425 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19426 mac_ptr += bytes_read;
19427
19428 if ((line == 0 && !at_commandline)
19429 || (line != 0 && at_commandline))
19430 complaint (&symfile_complaints,
19431 _("debug info gives source %d included "
19432 "from %s at %s line %d"),
19433 file, at_commandline ? _("command-line") : _("file"),
19434 line == 0 ? _("zero") : _("non-zero"), line);
19435
19436 if (at_commandline)
19437 {
19438 /* This DW_MACRO_GNU_start_file was executed in the
19439 pass one. */
19440 at_commandline = 0;
19441 }
19442 else
19443 current_file = macro_start_file (file, line,
19444 current_file, comp_dir,
19445 lh, objfile);
19446 }
19447 break;
19448
19449 case DW_MACRO_GNU_end_file:
19450 if (! current_file)
19451 complaint (&symfile_complaints,
19452 _("macro debug info has an unmatched "
19453 "`close_file' directive"));
19454 else
19455 {
19456 current_file = current_file->included_by;
19457 if (! current_file)
19458 {
19459 enum dwarf_macro_record_type next_type;
19460
19461 /* GCC circa March 2002 doesn't produce the zero
19462 type byte marking the end of the compilation
19463 unit. Complain if it's not there, but exit no
19464 matter what. */
19465
19466 /* Do we at least have room for a macinfo type byte? */
19467 if (mac_ptr >= mac_end)
19468 {
19469 dwarf2_section_buffer_overflow_complaint (section);
19470 return;
19471 }
19472
19473 /* We don't increment mac_ptr here, so this is just
19474 a look-ahead. */
19475 next_type = read_1_byte (abfd, mac_ptr);
19476 if (next_type != 0)
19477 complaint (&symfile_complaints,
19478 _("no terminating 0-type entry for "
19479 "macros in `.debug_macinfo' section"));
19480
19481 return;
19482 }
19483 }
19484 break;
19485
19486 case DW_MACRO_GNU_transparent_include:
19487 case DW_MACRO_GNU_transparent_include_alt:
19488 {
19489 LONGEST offset;
19490 void **slot;
19491 bfd *include_bfd = abfd;
19492 struct dwarf2_section_info *include_section = section;
19493 struct dwarf2_section_info alt_section;
19494 const gdb_byte *include_mac_end = mac_end;
19495 int is_dwz = section_is_dwz;
19496 const gdb_byte *new_mac_ptr;
19497
19498 offset = read_offset_1 (abfd, mac_ptr, offset_size);
19499 mac_ptr += offset_size;
19500
19501 if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
19502 {
19503 struct dwz_file *dwz = dwarf2_get_dwz_file ();
19504
19505 dwarf2_read_section (dwarf2_per_objfile->objfile,
19506 &dwz->macro);
19507
19508 include_bfd = dwz->macro.asection->owner;
19509 include_section = &dwz->macro;
19510 include_mac_end = dwz->macro.buffer + dwz->macro.size;
19511 is_dwz = 1;
19512 }
19513
19514 new_mac_ptr = include_section->buffer + offset;
19515 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
19516
19517 if (*slot != NULL)
19518 {
19519 /* This has actually happened; see
19520 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
19521 complaint (&symfile_complaints,
19522 _("recursive DW_MACRO_GNU_transparent_include in "
19523 ".debug_macro section"));
19524 }
19525 else
19526 {
19527 *slot = (void *) new_mac_ptr;
19528
19529 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
19530 include_mac_end, current_file,
19531 lh, comp_dir,
19532 section, section_is_gnu, is_dwz,
19533 offset_size, objfile, include_hash);
19534
19535 htab_remove_elt (include_hash, (void *) new_mac_ptr);
19536 }
19537 }
19538 break;
19539
19540 case DW_MACINFO_vendor_ext:
19541 if (!section_is_gnu)
19542 {
19543 unsigned int bytes_read;
19544 int constant;
19545
19546 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19547 mac_ptr += bytes_read;
19548 read_direct_string (abfd, mac_ptr, &bytes_read);
19549 mac_ptr += bytes_read;
19550
19551 /* We don't recognize any vendor extensions. */
19552 break;
19553 }
19554 /* FALLTHROUGH */
19555
19556 default:
19557 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
19558 mac_ptr, mac_end, abfd, offset_size,
19559 section);
19560 if (mac_ptr == NULL)
19561 return;
19562 break;
19563 }
19564 } while (macinfo_type != 0);
19565 }
19566
19567 static void
19568 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
19569 const char *comp_dir, int section_is_gnu)
19570 {
19571 struct objfile *objfile = dwarf2_per_objfile->objfile;
19572 struct line_header *lh = cu->line_header;
19573 bfd *abfd;
19574 const gdb_byte *mac_ptr, *mac_end;
19575 struct macro_source_file *current_file = 0;
19576 enum dwarf_macro_record_type macinfo_type;
19577 unsigned int offset_size = cu->header.offset_size;
19578 const gdb_byte *opcode_definitions[256];
19579 struct cleanup *cleanup;
19580 htab_t include_hash;
19581 void **slot;
19582 struct dwarf2_section_info *section;
19583 const char *section_name;
19584
19585 if (cu->dwo_unit != NULL)
19586 {
19587 if (section_is_gnu)
19588 {
19589 section = &cu->dwo_unit->dwo_file->sections.macro;
19590 section_name = ".debug_macro.dwo";
19591 }
19592 else
19593 {
19594 section = &cu->dwo_unit->dwo_file->sections.macinfo;
19595 section_name = ".debug_macinfo.dwo";
19596 }
19597 }
19598 else
19599 {
19600 if (section_is_gnu)
19601 {
19602 section = &dwarf2_per_objfile->macro;
19603 section_name = ".debug_macro";
19604 }
19605 else
19606 {
19607 section = &dwarf2_per_objfile->macinfo;
19608 section_name = ".debug_macinfo";
19609 }
19610 }
19611
19612 dwarf2_read_section (objfile, section);
19613 if (section->buffer == NULL)
19614 {
19615 complaint (&symfile_complaints, _("missing %s section"), section_name);
19616 return;
19617 }
19618 abfd = section->asection->owner;
19619
19620 /* First pass: Find the name of the base filename.
19621 This filename is needed in order to process all macros whose definition
19622 (or undefinition) comes from the command line. These macros are defined
19623 before the first DW_MACINFO_start_file entry, and yet still need to be
19624 associated to the base file.
19625
19626 To determine the base file name, we scan the macro definitions until we
19627 reach the first DW_MACINFO_start_file entry. We then initialize
19628 CURRENT_FILE accordingly so that any macro definition found before the
19629 first DW_MACINFO_start_file can still be associated to the base file. */
19630
19631 mac_ptr = section->buffer + offset;
19632 mac_end = section->buffer + section->size;
19633
19634 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
19635 &offset_size, section_is_gnu);
19636 if (mac_ptr == NULL)
19637 {
19638 /* We already issued a complaint. */
19639 return;
19640 }
19641
19642 do
19643 {
19644 /* Do we at least have room for a macinfo type byte? */
19645 if (mac_ptr >= mac_end)
19646 {
19647 /* Complaint is printed during the second pass as GDB will probably
19648 stop the first pass earlier upon finding
19649 DW_MACINFO_start_file. */
19650 break;
19651 }
19652
19653 macinfo_type = read_1_byte (abfd, mac_ptr);
19654 mac_ptr++;
19655
19656 /* Note that we rely on the fact that the corresponding GNU and
19657 DWARF constants are the same. */
19658 switch (macinfo_type)
19659 {
19660 /* A zero macinfo type indicates the end of the macro
19661 information. */
19662 case 0:
19663 break;
19664
19665 case DW_MACRO_GNU_define:
19666 case DW_MACRO_GNU_undef:
19667 /* Only skip the data by MAC_PTR. */
19668 {
19669 unsigned int bytes_read;
19670
19671 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19672 mac_ptr += bytes_read;
19673 read_direct_string (abfd, mac_ptr, &bytes_read);
19674 mac_ptr += bytes_read;
19675 }
19676 break;
19677
19678 case DW_MACRO_GNU_start_file:
19679 {
19680 unsigned int bytes_read;
19681 int line, file;
19682
19683 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19684 mac_ptr += bytes_read;
19685 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19686 mac_ptr += bytes_read;
19687
19688 current_file = macro_start_file (file, line, current_file,
19689 comp_dir, lh, objfile);
19690 }
19691 break;
19692
19693 case DW_MACRO_GNU_end_file:
19694 /* No data to skip by MAC_PTR. */
19695 break;
19696
19697 case DW_MACRO_GNU_define_indirect:
19698 case DW_MACRO_GNU_undef_indirect:
19699 case DW_MACRO_GNU_define_indirect_alt:
19700 case DW_MACRO_GNU_undef_indirect_alt:
19701 {
19702 unsigned int bytes_read;
19703
19704 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19705 mac_ptr += bytes_read;
19706 mac_ptr += offset_size;
19707 }
19708 break;
19709
19710 case DW_MACRO_GNU_transparent_include:
19711 case DW_MACRO_GNU_transparent_include_alt:
19712 /* Note that, according to the spec, a transparent include
19713 chain cannot call DW_MACRO_GNU_start_file. So, we can just
19714 skip this opcode. */
19715 mac_ptr += offset_size;
19716 break;
19717
19718 case DW_MACINFO_vendor_ext:
19719 /* Only skip the data by MAC_PTR. */
19720 if (!section_is_gnu)
19721 {
19722 unsigned int bytes_read;
19723
19724 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
19725 mac_ptr += bytes_read;
19726 read_direct_string (abfd, mac_ptr, &bytes_read);
19727 mac_ptr += bytes_read;
19728 }
19729 /* FALLTHROUGH */
19730
19731 default:
19732 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
19733 mac_ptr, mac_end, abfd, offset_size,
19734 section);
19735 if (mac_ptr == NULL)
19736 return;
19737 break;
19738 }
19739 } while (macinfo_type != 0 && current_file == NULL);
19740
19741 /* Second pass: Process all entries.
19742
19743 Use the AT_COMMAND_LINE flag to determine whether we are still processing
19744 command-line macro definitions/undefinitions. This flag is unset when we
19745 reach the first DW_MACINFO_start_file entry. */
19746
19747 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
19748 NULL, xcalloc, xfree);
19749 cleanup = make_cleanup_htab_delete (include_hash);
19750 mac_ptr = section->buffer + offset;
19751 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
19752 *slot = (void *) mac_ptr;
19753 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
19754 current_file, lh, comp_dir, section,
19755 section_is_gnu, 0,
19756 offset_size, objfile, include_hash);
19757 do_cleanups (cleanup);
19758 }
19759
19760 /* Check if the attribute's form is a DW_FORM_block*
19761 if so return true else false. */
19762
19763 static int
19764 attr_form_is_block (struct attribute *attr)
19765 {
19766 return (attr == NULL ? 0 :
19767 attr->form == DW_FORM_block1
19768 || attr->form == DW_FORM_block2
19769 || attr->form == DW_FORM_block4
19770 || attr->form == DW_FORM_block
19771 || attr->form == DW_FORM_exprloc);
19772 }
19773
19774 /* Return non-zero if ATTR's value is a section offset --- classes
19775 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
19776 You may use DW_UNSND (attr) to retrieve such offsets.
19777
19778 Section 7.5.4, "Attribute Encodings", explains that no attribute
19779 may have a value that belongs to more than one of these classes; it
19780 would be ambiguous if we did, because we use the same forms for all
19781 of them. */
19782
19783 static int
19784 attr_form_is_section_offset (struct attribute *attr)
19785 {
19786 return (attr->form == DW_FORM_data4
19787 || attr->form == DW_FORM_data8
19788 || attr->form == DW_FORM_sec_offset);
19789 }
19790
19791 /* Return non-zero if ATTR's value falls in the 'constant' class, or
19792 zero otherwise. When this function returns true, you can apply
19793 dwarf2_get_attr_constant_value to it.
19794
19795 However, note that for some attributes you must check
19796 attr_form_is_section_offset before using this test. DW_FORM_data4
19797 and DW_FORM_data8 are members of both the constant class, and of
19798 the classes that contain offsets into other debug sections
19799 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
19800 that, if an attribute's can be either a constant or one of the
19801 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
19802 taken as section offsets, not constants. */
19803
19804 static int
19805 attr_form_is_constant (struct attribute *attr)
19806 {
19807 switch (attr->form)
19808 {
19809 case DW_FORM_sdata:
19810 case DW_FORM_udata:
19811 case DW_FORM_data1:
19812 case DW_FORM_data2:
19813 case DW_FORM_data4:
19814 case DW_FORM_data8:
19815 return 1;
19816 default:
19817 return 0;
19818 }
19819 }
19820
19821 /* Return the .debug_loc section to use for CU.
19822 For DWO files use .debug_loc.dwo. */
19823
19824 static struct dwarf2_section_info *
19825 cu_debug_loc_section (struct dwarf2_cu *cu)
19826 {
19827 if (cu->dwo_unit)
19828 return &cu->dwo_unit->dwo_file->sections.loc;
19829 return &dwarf2_per_objfile->loc;
19830 }
19831
19832 /* A helper function that fills in a dwarf2_loclist_baton. */
19833
19834 static void
19835 fill_in_loclist_baton (struct dwarf2_cu *cu,
19836 struct dwarf2_loclist_baton *baton,
19837 struct attribute *attr)
19838 {
19839 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19840
19841 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
19842
19843 baton->per_cu = cu->per_cu;
19844 gdb_assert (baton->per_cu);
19845 /* We don't know how long the location list is, but make sure we
19846 don't run off the edge of the section. */
19847 baton->size = section->size - DW_UNSND (attr);
19848 baton->data = section->buffer + DW_UNSND (attr);
19849 baton->base_address = cu->base_address;
19850 baton->from_dwo = cu->dwo_unit != NULL;
19851 }
19852
19853 static void
19854 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
19855 struct dwarf2_cu *cu, int is_block)
19856 {
19857 struct objfile *objfile = dwarf2_per_objfile->objfile;
19858 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19859
19860 if (attr_form_is_section_offset (attr)
19861 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
19862 the section. If so, fall through to the complaint in the
19863 other branch. */
19864 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
19865 {
19866 struct dwarf2_loclist_baton *baton;
19867
19868 baton = obstack_alloc (&objfile->objfile_obstack,
19869 sizeof (struct dwarf2_loclist_baton));
19870
19871 fill_in_loclist_baton (cu, baton, attr);
19872
19873 if (cu->base_known == 0)
19874 complaint (&symfile_complaints,
19875 _("Location list used without "
19876 "specifying the CU base address."));
19877
19878 SYMBOL_ACLASS_INDEX (sym) = (is_block
19879 ? dwarf2_loclist_block_index
19880 : dwarf2_loclist_index);
19881 SYMBOL_LOCATION_BATON (sym) = baton;
19882 }
19883 else
19884 {
19885 struct dwarf2_locexpr_baton *baton;
19886
19887 baton = obstack_alloc (&objfile->objfile_obstack,
19888 sizeof (struct dwarf2_locexpr_baton));
19889 baton->per_cu = cu->per_cu;
19890 gdb_assert (baton->per_cu);
19891
19892 if (attr_form_is_block (attr))
19893 {
19894 /* Note that we're just copying the block's data pointer
19895 here, not the actual data. We're still pointing into the
19896 info_buffer for SYM's objfile; right now we never release
19897 that buffer, but when we do clean up properly this may
19898 need to change. */
19899 baton->size = DW_BLOCK (attr)->size;
19900 baton->data = DW_BLOCK (attr)->data;
19901 }
19902 else
19903 {
19904 dwarf2_invalid_attrib_class_complaint ("location description",
19905 SYMBOL_NATURAL_NAME (sym));
19906 baton->size = 0;
19907 }
19908
19909 SYMBOL_ACLASS_INDEX (sym) = (is_block
19910 ? dwarf2_locexpr_block_index
19911 : dwarf2_locexpr_index);
19912 SYMBOL_LOCATION_BATON (sym) = baton;
19913 }
19914 }
19915
19916 /* Return the OBJFILE associated with the compilation unit CU. If CU
19917 came from a separate debuginfo file, then the master objfile is
19918 returned. */
19919
19920 struct objfile *
19921 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
19922 {
19923 struct objfile *objfile = per_cu->objfile;
19924
19925 /* Return the master objfile, so that we can report and look up the
19926 correct file containing this variable. */
19927 if (objfile->separate_debug_objfile_backlink)
19928 objfile = objfile->separate_debug_objfile_backlink;
19929
19930 return objfile;
19931 }
19932
19933 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
19934 (CU_HEADERP is unused in such case) or prepare a temporary copy at
19935 CU_HEADERP first. */
19936
19937 static const struct comp_unit_head *
19938 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
19939 struct dwarf2_per_cu_data *per_cu)
19940 {
19941 const gdb_byte *info_ptr;
19942
19943 if (per_cu->cu)
19944 return &per_cu->cu->header;
19945
19946 info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
19947
19948 memset (cu_headerp, 0, sizeof (*cu_headerp));
19949 read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
19950
19951 return cu_headerp;
19952 }
19953
19954 /* Return the address size given in the compilation unit header for CU. */
19955
19956 int
19957 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
19958 {
19959 struct comp_unit_head cu_header_local;
19960 const struct comp_unit_head *cu_headerp;
19961
19962 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19963
19964 return cu_headerp->addr_size;
19965 }
19966
19967 /* Return the offset size given in the compilation unit header for CU. */
19968
19969 int
19970 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
19971 {
19972 struct comp_unit_head cu_header_local;
19973 const struct comp_unit_head *cu_headerp;
19974
19975 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19976
19977 return cu_headerp->offset_size;
19978 }
19979
19980 /* See its dwarf2loc.h declaration. */
19981
19982 int
19983 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
19984 {
19985 struct comp_unit_head cu_header_local;
19986 const struct comp_unit_head *cu_headerp;
19987
19988 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
19989
19990 if (cu_headerp->version == 2)
19991 return cu_headerp->addr_size;
19992 else
19993 return cu_headerp->offset_size;
19994 }
19995
19996 /* Return the text offset of the CU. The returned offset comes from
19997 this CU's objfile. If this objfile came from a separate debuginfo
19998 file, then the offset may be different from the corresponding
19999 offset in the parent objfile. */
20000
20001 CORE_ADDR
20002 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
20003 {
20004 struct objfile *objfile = per_cu->objfile;
20005
20006 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20007 }
20008
20009 /* Locate the .debug_info compilation unit from CU's objfile which contains
20010 the DIE at OFFSET. Raises an error on failure. */
20011
20012 static struct dwarf2_per_cu_data *
20013 dwarf2_find_containing_comp_unit (sect_offset offset,
20014 unsigned int offset_in_dwz,
20015 struct objfile *objfile)
20016 {
20017 struct dwarf2_per_cu_data *this_cu;
20018 int low, high;
20019 const sect_offset *cu_off;
20020
20021 low = 0;
20022 high = dwarf2_per_objfile->n_comp_units - 1;
20023 while (high > low)
20024 {
20025 struct dwarf2_per_cu_data *mid_cu;
20026 int mid = low + (high - low) / 2;
20027
20028 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
20029 cu_off = &mid_cu->offset;
20030 if (mid_cu->is_dwz > offset_in_dwz
20031 || (mid_cu->is_dwz == offset_in_dwz
20032 && cu_off->sect_off >= offset.sect_off))
20033 high = mid;
20034 else
20035 low = mid + 1;
20036 }
20037 gdb_assert (low == high);
20038 this_cu = dwarf2_per_objfile->all_comp_units[low];
20039 cu_off = &this_cu->offset;
20040 if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
20041 {
20042 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
20043 error (_("Dwarf Error: could not find partial DIE containing "
20044 "offset 0x%lx [in module %s]"),
20045 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
20046
20047 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
20048 <= offset.sect_off);
20049 return dwarf2_per_objfile->all_comp_units[low-1];
20050 }
20051 else
20052 {
20053 this_cu = dwarf2_per_objfile->all_comp_units[low];
20054 if (low == dwarf2_per_objfile->n_comp_units - 1
20055 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
20056 error (_("invalid dwarf2 offset %u"), offset.sect_off);
20057 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
20058 return this_cu;
20059 }
20060 }
20061
20062 /* Initialize dwarf2_cu CU, owned by PER_CU. */
20063
20064 static void
20065 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
20066 {
20067 memset (cu, 0, sizeof (*cu));
20068 per_cu->cu = cu;
20069 cu->per_cu = per_cu;
20070 cu->objfile = per_cu->objfile;
20071 obstack_init (&cu->comp_unit_obstack);
20072 }
20073
20074 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
20075
20076 static void
20077 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
20078 enum language pretend_language)
20079 {
20080 struct attribute *attr;
20081
20082 /* Set the language we're debugging. */
20083 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
20084 if (attr)
20085 set_cu_language (DW_UNSND (attr), cu);
20086 else
20087 {
20088 cu->language = pretend_language;
20089 cu->language_defn = language_def (cu->language);
20090 }
20091
20092 attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
20093 if (attr)
20094 cu->producer = DW_STRING (attr);
20095 }
20096
20097 /* Release one cached compilation unit, CU. We unlink it from the tree
20098 of compilation units, but we don't remove it from the read_in_chain;
20099 the caller is responsible for that.
20100 NOTE: DATA is a void * because this function is also used as a
20101 cleanup routine. */
20102
20103 static void
20104 free_heap_comp_unit (void *data)
20105 {
20106 struct dwarf2_cu *cu = data;
20107
20108 gdb_assert (cu->per_cu != NULL);
20109 cu->per_cu->cu = NULL;
20110 cu->per_cu = NULL;
20111
20112 obstack_free (&cu->comp_unit_obstack, NULL);
20113
20114 xfree (cu);
20115 }
20116
20117 /* This cleanup function is passed the address of a dwarf2_cu on the stack
20118 when we're finished with it. We can't free the pointer itself, but be
20119 sure to unlink it from the cache. Also release any associated storage. */
20120
20121 static void
20122 free_stack_comp_unit (void *data)
20123 {
20124 struct dwarf2_cu *cu = data;
20125
20126 gdb_assert (cu->per_cu != NULL);
20127 cu->per_cu->cu = NULL;
20128 cu->per_cu = NULL;
20129
20130 obstack_free (&cu->comp_unit_obstack, NULL);
20131 cu->partial_dies = NULL;
20132 }
20133
20134 /* Free all cached compilation units. */
20135
20136 static void
20137 free_cached_comp_units (void *data)
20138 {
20139 struct dwarf2_per_cu_data *per_cu, **last_chain;
20140
20141 per_cu = dwarf2_per_objfile->read_in_chain;
20142 last_chain = &dwarf2_per_objfile->read_in_chain;
20143 while (per_cu != NULL)
20144 {
20145 struct dwarf2_per_cu_data *next_cu;
20146
20147 next_cu = per_cu->cu->read_in_chain;
20148
20149 free_heap_comp_unit (per_cu->cu);
20150 *last_chain = next_cu;
20151
20152 per_cu = next_cu;
20153 }
20154 }
20155
20156 /* Increase the age counter on each cached compilation unit, and free
20157 any that are too old. */
20158
20159 static void
20160 age_cached_comp_units (void)
20161 {
20162 struct dwarf2_per_cu_data *per_cu, **last_chain;
20163
20164 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
20165 per_cu = dwarf2_per_objfile->read_in_chain;
20166 while (per_cu != NULL)
20167 {
20168 per_cu->cu->last_used ++;
20169 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
20170 dwarf2_mark (per_cu->cu);
20171 per_cu = per_cu->cu->read_in_chain;
20172 }
20173
20174 per_cu = dwarf2_per_objfile->read_in_chain;
20175 last_chain = &dwarf2_per_objfile->read_in_chain;
20176 while (per_cu != NULL)
20177 {
20178 struct dwarf2_per_cu_data *next_cu;
20179
20180 next_cu = per_cu->cu->read_in_chain;
20181
20182 if (!per_cu->cu->mark)
20183 {
20184 free_heap_comp_unit (per_cu->cu);
20185 *last_chain = next_cu;
20186 }
20187 else
20188 last_chain = &per_cu->cu->read_in_chain;
20189
20190 per_cu = next_cu;
20191 }
20192 }
20193
20194 /* Remove a single compilation unit from the cache. */
20195
20196 static void
20197 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
20198 {
20199 struct dwarf2_per_cu_data *per_cu, **last_chain;
20200
20201 per_cu = dwarf2_per_objfile->read_in_chain;
20202 last_chain = &dwarf2_per_objfile->read_in_chain;
20203 while (per_cu != NULL)
20204 {
20205 struct dwarf2_per_cu_data *next_cu;
20206
20207 next_cu = per_cu->cu->read_in_chain;
20208
20209 if (per_cu == target_per_cu)
20210 {
20211 free_heap_comp_unit (per_cu->cu);
20212 per_cu->cu = NULL;
20213 *last_chain = next_cu;
20214 break;
20215 }
20216 else
20217 last_chain = &per_cu->cu->read_in_chain;
20218
20219 per_cu = next_cu;
20220 }
20221 }
20222
20223 /* Release all extra memory associated with OBJFILE. */
20224
20225 void
20226 dwarf2_free_objfile (struct objfile *objfile)
20227 {
20228 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
20229
20230 if (dwarf2_per_objfile == NULL)
20231 return;
20232
20233 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
20234 free_cached_comp_units (NULL);
20235
20236 if (dwarf2_per_objfile->quick_file_names_table)
20237 htab_delete (dwarf2_per_objfile->quick_file_names_table);
20238
20239 /* Everything else should be on the objfile obstack. */
20240 }
20241
20242 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
20243 We store these in a hash table separate from the DIEs, and preserve them
20244 when the DIEs are flushed out of cache.
20245
20246 The CU "per_cu" pointer is needed because offset alone is not enough to
20247 uniquely identify the type. A file may have multiple .debug_types sections,
20248 or the type may come from a DWO file. Furthermore, while it's more logical
20249 to use per_cu->section+offset, with Fission the section with the data is in
20250 the DWO file but we don't know that section at the point we need it.
20251 We have to use something in dwarf2_per_cu_data (or the pointer to it)
20252 because we can enter the lookup routine, get_die_type_at_offset, from
20253 outside this file, and thus won't necessarily have PER_CU->cu.
20254 Fortunately, PER_CU is stable for the life of the objfile. */
20255
20256 struct dwarf2_per_cu_offset_and_type
20257 {
20258 const struct dwarf2_per_cu_data *per_cu;
20259 sect_offset offset;
20260 struct type *type;
20261 };
20262
20263 /* Hash function for a dwarf2_per_cu_offset_and_type. */
20264
20265 static hashval_t
20266 per_cu_offset_and_type_hash (const void *item)
20267 {
20268 const struct dwarf2_per_cu_offset_and_type *ofs = item;
20269
20270 return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
20271 }
20272
20273 /* Equality function for a dwarf2_per_cu_offset_and_type. */
20274
20275 static int
20276 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
20277 {
20278 const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
20279 const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
20280
20281 return (ofs_lhs->per_cu == ofs_rhs->per_cu
20282 && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
20283 }
20284
20285 /* Set the type associated with DIE to TYPE. Save it in CU's hash
20286 table if necessary. For convenience, return TYPE.
20287
20288 The DIEs reading must have careful ordering to:
20289 * Not cause infite loops trying to read in DIEs as a prerequisite for
20290 reading current DIE.
20291 * Not trying to dereference contents of still incompletely read in types
20292 while reading in other DIEs.
20293 * Enable referencing still incompletely read in types just by a pointer to
20294 the type without accessing its fields.
20295
20296 Therefore caller should follow these rules:
20297 * Try to fetch any prerequisite types we may need to build this DIE type
20298 before building the type and calling set_die_type.
20299 * After building type call set_die_type for current DIE as soon as
20300 possible before fetching more types to complete the current type.
20301 * Make the type as complete as possible before fetching more types. */
20302
20303 static struct type *
20304 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
20305 {
20306 struct dwarf2_per_cu_offset_and_type **slot, ofs;
20307 struct objfile *objfile = cu->objfile;
20308
20309 /* For Ada types, make sure that the gnat-specific data is always
20310 initialized (if not already set). There are a few types where
20311 we should not be doing so, because the type-specific area is
20312 already used to hold some other piece of info (eg: TYPE_CODE_FLT
20313 where the type-specific area is used to store the floatformat).
20314 But this is not a problem, because the gnat-specific information
20315 is actually not needed for these types. */
20316 if (need_gnat_info (cu)
20317 && TYPE_CODE (type) != TYPE_CODE_FUNC
20318 && TYPE_CODE (type) != TYPE_CODE_FLT
20319 && !HAVE_GNAT_AUX_INFO (type))
20320 INIT_GNAT_SPECIFIC (type);
20321
20322 if (dwarf2_per_objfile->die_type_hash == NULL)
20323 {
20324 dwarf2_per_objfile->die_type_hash =
20325 htab_create_alloc_ex (127,
20326 per_cu_offset_and_type_hash,
20327 per_cu_offset_and_type_eq,
20328 NULL,
20329 &objfile->objfile_obstack,
20330 hashtab_obstack_allocate,
20331 dummy_obstack_deallocate);
20332 }
20333
20334 ofs.per_cu = cu->per_cu;
20335 ofs.offset = die->offset;
20336 ofs.type = type;
20337 slot = (struct dwarf2_per_cu_offset_and_type **)
20338 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
20339 if (*slot)
20340 complaint (&symfile_complaints,
20341 _("A problem internal to GDB: DIE 0x%x has type already set"),
20342 die->offset.sect_off);
20343 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
20344 **slot = ofs;
20345 return type;
20346 }
20347
20348 /* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
20349 or return NULL if the die does not have a saved type. */
20350
20351 static struct type *
20352 get_die_type_at_offset (sect_offset offset,
20353 struct dwarf2_per_cu_data *per_cu)
20354 {
20355 struct dwarf2_per_cu_offset_and_type *slot, ofs;
20356
20357 if (dwarf2_per_objfile->die_type_hash == NULL)
20358 return NULL;
20359
20360 ofs.per_cu = per_cu;
20361 ofs.offset = offset;
20362 slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
20363 if (slot)
20364 return slot->type;
20365 else
20366 return NULL;
20367 }
20368
20369 /* Look up the type for DIE in CU in die_type_hash,
20370 or return NULL if DIE does not have a saved type. */
20371
20372 static struct type *
20373 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
20374 {
20375 return get_die_type_at_offset (die->offset, cu->per_cu);
20376 }
20377
20378 /* Add a dependence relationship from CU to REF_PER_CU. */
20379
20380 static void
20381 dwarf2_add_dependence (struct dwarf2_cu *cu,
20382 struct dwarf2_per_cu_data *ref_per_cu)
20383 {
20384 void **slot;
20385
20386 if (cu->dependencies == NULL)
20387 cu->dependencies
20388 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
20389 NULL, &cu->comp_unit_obstack,
20390 hashtab_obstack_allocate,
20391 dummy_obstack_deallocate);
20392
20393 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
20394 if (*slot == NULL)
20395 *slot = ref_per_cu;
20396 }
20397
20398 /* Subroutine of dwarf2_mark to pass to htab_traverse.
20399 Set the mark field in every compilation unit in the
20400 cache that we must keep because we are keeping CU. */
20401
20402 static int
20403 dwarf2_mark_helper (void **slot, void *data)
20404 {
20405 struct dwarf2_per_cu_data *per_cu;
20406
20407 per_cu = (struct dwarf2_per_cu_data *) *slot;
20408
20409 /* cu->dependencies references may not yet have been ever read if QUIT aborts
20410 reading of the chain. As such dependencies remain valid it is not much
20411 useful to track and undo them during QUIT cleanups. */
20412 if (per_cu->cu == NULL)
20413 return 1;
20414
20415 if (per_cu->cu->mark)
20416 return 1;
20417 per_cu->cu->mark = 1;
20418
20419 if (per_cu->cu->dependencies != NULL)
20420 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
20421
20422 return 1;
20423 }
20424
20425 /* Set the mark field in CU and in every other compilation unit in the
20426 cache that we must keep because we are keeping CU. */
20427
20428 static void
20429 dwarf2_mark (struct dwarf2_cu *cu)
20430 {
20431 if (cu->mark)
20432 return;
20433 cu->mark = 1;
20434 if (cu->dependencies != NULL)
20435 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
20436 }
20437
20438 static void
20439 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
20440 {
20441 while (per_cu)
20442 {
20443 per_cu->cu->mark = 0;
20444 per_cu = per_cu->cu->read_in_chain;
20445 }
20446 }
20447
20448 /* Trivial hash function for partial_die_info: the hash value of a DIE
20449 is its offset in .debug_info for this objfile. */
20450
20451 static hashval_t
20452 partial_die_hash (const void *item)
20453 {
20454 const struct partial_die_info *part_die = item;
20455
20456 return part_die->offset.sect_off;
20457 }
20458
20459 /* Trivial comparison function for partial_die_info structures: two DIEs
20460 are equal if they have the same offset. */
20461
20462 static int
20463 partial_die_eq (const void *item_lhs, const void *item_rhs)
20464 {
20465 const struct partial_die_info *part_die_lhs = item_lhs;
20466 const struct partial_die_info *part_die_rhs = item_rhs;
20467
20468 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
20469 }
20470
20471 static struct cmd_list_element *set_dwarf2_cmdlist;
20472 static struct cmd_list_element *show_dwarf2_cmdlist;
20473
20474 static void
20475 set_dwarf2_cmd (char *args, int from_tty)
20476 {
20477 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
20478 }
20479
20480 static void
20481 show_dwarf2_cmd (char *args, int from_tty)
20482 {
20483 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
20484 }
20485
20486 /* Free data associated with OBJFILE, if necessary. */
20487
20488 static void
20489 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
20490 {
20491 struct dwarf2_per_objfile *data = d;
20492 int ix;
20493
20494 for (ix = 0; ix < dwarf2_per_objfile->n_comp_units; ++ix)
20495 VEC_free (dwarf2_per_cu_ptr,
20496 dwarf2_per_objfile->all_comp_units[ix]->imported_symtabs);
20497
20498 for (ix = 0; ix < dwarf2_per_objfile->n_type_units; ++ix)
20499 VEC_free (dwarf2_per_cu_ptr,
20500 dwarf2_per_objfile->all_type_units[ix]->per_cu.imported_symtabs);
20501 xfree (dwarf2_per_objfile->all_type_units);
20502
20503 VEC_free (dwarf2_section_info_def, data->types);
20504
20505 if (data->dwo_files)
20506 free_dwo_files (data->dwo_files, objfile);
20507 if (data->dwp_file)
20508 gdb_bfd_unref (data->dwp_file->dbfd);
20509
20510 if (data->dwz_file && data->dwz_file->dwz_bfd)
20511 gdb_bfd_unref (data->dwz_file->dwz_bfd);
20512 }
20513
20514 \f
20515 /* The "save gdb-index" command. */
20516
20517 /* The contents of the hash table we create when building the string
20518 table. */
20519 struct strtab_entry
20520 {
20521 offset_type offset;
20522 const char *str;
20523 };
20524
20525 /* Hash function for a strtab_entry.
20526
20527 Function is used only during write_hash_table so no index format backward
20528 compatibility is needed. */
20529
20530 static hashval_t
20531 hash_strtab_entry (const void *e)
20532 {
20533 const struct strtab_entry *entry = e;
20534 return mapped_index_string_hash (INT_MAX, entry->str);
20535 }
20536
20537 /* Equality function for a strtab_entry. */
20538
20539 static int
20540 eq_strtab_entry (const void *a, const void *b)
20541 {
20542 const struct strtab_entry *ea = a;
20543 const struct strtab_entry *eb = b;
20544 return !strcmp (ea->str, eb->str);
20545 }
20546
20547 /* Create a strtab_entry hash table. */
20548
20549 static htab_t
20550 create_strtab (void)
20551 {
20552 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
20553 xfree, xcalloc, xfree);
20554 }
20555
20556 /* Add a string to the constant pool. Return the string's offset in
20557 host order. */
20558
20559 static offset_type
20560 add_string (htab_t table, struct obstack *cpool, const char *str)
20561 {
20562 void **slot;
20563 struct strtab_entry entry;
20564 struct strtab_entry *result;
20565
20566 entry.str = str;
20567 slot = htab_find_slot (table, &entry, INSERT);
20568 if (*slot)
20569 result = *slot;
20570 else
20571 {
20572 result = XNEW (struct strtab_entry);
20573 result->offset = obstack_object_size (cpool);
20574 result->str = str;
20575 obstack_grow_str0 (cpool, str);
20576 *slot = result;
20577 }
20578 return result->offset;
20579 }
20580
20581 /* An entry in the symbol table. */
20582 struct symtab_index_entry
20583 {
20584 /* The name of the symbol. */
20585 const char *name;
20586 /* The offset of the name in the constant pool. */
20587 offset_type index_offset;
20588 /* A sorted vector of the indices of all the CUs that hold an object
20589 of this name. */
20590 VEC (offset_type) *cu_indices;
20591 };
20592
20593 /* The symbol table. This is a power-of-2-sized hash table. */
20594 struct mapped_symtab
20595 {
20596 offset_type n_elements;
20597 offset_type size;
20598 struct symtab_index_entry **data;
20599 };
20600
20601 /* Hash function for a symtab_index_entry. */
20602
20603 static hashval_t
20604 hash_symtab_entry (const void *e)
20605 {
20606 const struct symtab_index_entry *entry = e;
20607 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
20608 sizeof (offset_type) * VEC_length (offset_type,
20609 entry->cu_indices),
20610 0);
20611 }
20612
20613 /* Equality function for a symtab_index_entry. */
20614
20615 static int
20616 eq_symtab_entry (const void *a, const void *b)
20617 {
20618 const struct symtab_index_entry *ea = a;
20619 const struct symtab_index_entry *eb = b;
20620 int len = VEC_length (offset_type, ea->cu_indices);
20621 if (len != VEC_length (offset_type, eb->cu_indices))
20622 return 0;
20623 return !memcmp (VEC_address (offset_type, ea->cu_indices),
20624 VEC_address (offset_type, eb->cu_indices),
20625 sizeof (offset_type) * len);
20626 }
20627
20628 /* Destroy a symtab_index_entry. */
20629
20630 static void
20631 delete_symtab_entry (void *p)
20632 {
20633 struct symtab_index_entry *entry = p;
20634 VEC_free (offset_type, entry->cu_indices);
20635 xfree (entry);
20636 }
20637
20638 /* Create a hash table holding symtab_index_entry objects. */
20639
20640 static htab_t
20641 create_symbol_hash_table (void)
20642 {
20643 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
20644 delete_symtab_entry, xcalloc, xfree);
20645 }
20646
20647 /* Create a new mapped symtab object. */
20648
20649 static struct mapped_symtab *
20650 create_mapped_symtab (void)
20651 {
20652 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
20653 symtab->n_elements = 0;
20654 symtab->size = 1024;
20655 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20656 return symtab;
20657 }
20658
20659 /* Destroy a mapped_symtab. */
20660
20661 static void
20662 cleanup_mapped_symtab (void *p)
20663 {
20664 struct mapped_symtab *symtab = p;
20665 /* The contents of the array are freed when the other hash table is
20666 destroyed. */
20667 xfree (symtab->data);
20668 xfree (symtab);
20669 }
20670
20671 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
20672 the slot.
20673
20674 Function is used only during write_hash_table so no index format backward
20675 compatibility is needed. */
20676
20677 static struct symtab_index_entry **
20678 find_slot (struct mapped_symtab *symtab, const char *name)
20679 {
20680 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
20681
20682 index = hash & (symtab->size - 1);
20683 step = ((hash * 17) & (symtab->size - 1)) | 1;
20684
20685 for (;;)
20686 {
20687 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
20688 return &symtab->data[index];
20689 index = (index + step) & (symtab->size - 1);
20690 }
20691 }
20692
20693 /* Expand SYMTAB's hash table. */
20694
20695 static void
20696 hash_expand (struct mapped_symtab *symtab)
20697 {
20698 offset_type old_size = symtab->size;
20699 offset_type i;
20700 struct symtab_index_entry **old_entries = symtab->data;
20701
20702 symtab->size *= 2;
20703 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
20704
20705 for (i = 0; i < old_size; ++i)
20706 {
20707 if (old_entries[i])
20708 {
20709 struct symtab_index_entry **slot = find_slot (symtab,
20710 old_entries[i]->name);
20711 *slot = old_entries[i];
20712 }
20713 }
20714
20715 xfree (old_entries);
20716 }
20717
20718 /* Add an entry to SYMTAB. NAME is the name of the symbol.
20719 CU_INDEX is the index of the CU in which the symbol appears.
20720 IS_STATIC is one if the symbol is static, otherwise zero (global). */
20721
20722 static void
20723 add_index_entry (struct mapped_symtab *symtab, const char *name,
20724 int is_static, gdb_index_symbol_kind kind,
20725 offset_type cu_index)
20726 {
20727 struct symtab_index_entry **slot;
20728 offset_type cu_index_and_attrs;
20729
20730 ++symtab->n_elements;
20731 if (4 * symtab->n_elements / 3 >= symtab->size)
20732 hash_expand (symtab);
20733
20734 slot = find_slot (symtab, name);
20735 if (!*slot)
20736 {
20737 *slot = XNEW (struct symtab_index_entry);
20738 (*slot)->name = name;
20739 /* index_offset is set later. */
20740 (*slot)->cu_indices = NULL;
20741 }
20742
20743 cu_index_and_attrs = 0;
20744 DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
20745 DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
20746 DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
20747
20748 /* We don't want to record an index value twice as we want to avoid the
20749 duplication.
20750 We process all global symbols and then all static symbols
20751 (which would allow us to avoid the duplication by only having to check
20752 the last entry pushed), but a symbol could have multiple kinds in one CU.
20753 To keep things simple we don't worry about the duplication here and
20754 sort and uniqufy the list after we've processed all symbols. */
20755 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
20756 }
20757
20758 /* qsort helper routine for uniquify_cu_indices. */
20759
20760 static int
20761 offset_type_compare (const void *ap, const void *bp)
20762 {
20763 offset_type a = *(offset_type *) ap;
20764 offset_type b = *(offset_type *) bp;
20765
20766 return (a > b) - (b > a);
20767 }
20768
20769 /* Sort and remove duplicates of all symbols' cu_indices lists. */
20770
20771 static void
20772 uniquify_cu_indices (struct mapped_symtab *symtab)
20773 {
20774 int i;
20775
20776 for (i = 0; i < symtab->size; ++i)
20777 {
20778 struct symtab_index_entry *entry = symtab->data[i];
20779
20780 if (entry
20781 && entry->cu_indices != NULL)
20782 {
20783 unsigned int next_to_insert, next_to_check;
20784 offset_type last_value;
20785
20786 qsort (VEC_address (offset_type, entry->cu_indices),
20787 VEC_length (offset_type, entry->cu_indices),
20788 sizeof (offset_type), offset_type_compare);
20789
20790 last_value = VEC_index (offset_type, entry->cu_indices, 0);
20791 next_to_insert = 1;
20792 for (next_to_check = 1;
20793 next_to_check < VEC_length (offset_type, entry->cu_indices);
20794 ++next_to_check)
20795 {
20796 if (VEC_index (offset_type, entry->cu_indices, next_to_check)
20797 != last_value)
20798 {
20799 last_value = VEC_index (offset_type, entry->cu_indices,
20800 next_to_check);
20801 VEC_replace (offset_type, entry->cu_indices, next_to_insert,
20802 last_value);
20803 ++next_to_insert;
20804 }
20805 }
20806 VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
20807 }
20808 }
20809 }
20810
20811 /* Add a vector of indices to the constant pool. */
20812
20813 static offset_type
20814 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
20815 struct symtab_index_entry *entry)
20816 {
20817 void **slot;
20818
20819 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
20820 if (!*slot)
20821 {
20822 offset_type len = VEC_length (offset_type, entry->cu_indices);
20823 offset_type val = MAYBE_SWAP (len);
20824 offset_type iter;
20825 int i;
20826
20827 *slot = entry;
20828 entry->index_offset = obstack_object_size (cpool);
20829
20830 obstack_grow (cpool, &val, sizeof (val));
20831 for (i = 0;
20832 VEC_iterate (offset_type, entry->cu_indices, i, iter);
20833 ++i)
20834 {
20835 val = MAYBE_SWAP (iter);
20836 obstack_grow (cpool, &val, sizeof (val));
20837 }
20838 }
20839 else
20840 {
20841 struct symtab_index_entry *old_entry = *slot;
20842 entry->index_offset = old_entry->index_offset;
20843 entry = old_entry;
20844 }
20845 return entry->index_offset;
20846 }
20847
20848 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
20849 constant pool entries going into the obstack CPOOL. */
20850
20851 static void
20852 write_hash_table (struct mapped_symtab *symtab,
20853 struct obstack *output, struct obstack *cpool)
20854 {
20855 offset_type i;
20856 htab_t symbol_hash_table;
20857 htab_t str_table;
20858
20859 symbol_hash_table = create_symbol_hash_table ();
20860 str_table = create_strtab ();
20861
20862 /* We add all the index vectors to the constant pool first, to
20863 ensure alignment is ok. */
20864 for (i = 0; i < symtab->size; ++i)
20865 {
20866 if (symtab->data[i])
20867 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
20868 }
20869
20870 /* Now write out the hash table. */
20871 for (i = 0; i < symtab->size; ++i)
20872 {
20873 offset_type str_off, vec_off;
20874
20875 if (symtab->data[i])
20876 {
20877 str_off = add_string (str_table, cpool, symtab->data[i]->name);
20878 vec_off = symtab->data[i]->index_offset;
20879 }
20880 else
20881 {
20882 /* While 0 is a valid constant pool index, it is not valid
20883 to have 0 for both offsets. */
20884 str_off = 0;
20885 vec_off = 0;
20886 }
20887
20888 str_off = MAYBE_SWAP (str_off);
20889 vec_off = MAYBE_SWAP (vec_off);
20890
20891 obstack_grow (output, &str_off, sizeof (str_off));
20892 obstack_grow (output, &vec_off, sizeof (vec_off));
20893 }
20894
20895 htab_delete (str_table);
20896 htab_delete (symbol_hash_table);
20897 }
20898
20899 /* Struct to map psymtab to CU index in the index file. */
20900 struct psymtab_cu_index_map
20901 {
20902 struct partial_symtab *psymtab;
20903 unsigned int cu_index;
20904 };
20905
20906 static hashval_t
20907 hash_psymtab_cu_index (const void *item)
20908 {
20909 const struct psymtab_cu_index_map *map = item;
20910
20911 return htab_hash_pointer (map->psymtab);
20912 }
20913
20914 static int
20915 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
20916 {
20917 const struct psymtab_cu_index_map *lhs = item_lhs;
20918 const struct psymtab_cu_index_map *rhs = item_rhs;
20919
20920 return lhs->psymtab == rhs->psymtab;
20921 }
20922
20923 /* Helper struct for building the address table. */
20924 struct addrmap_index_data
20925 {
20926 struct objfile *objfile;
20927 struct obstack *addr_obstack;
20928 htab_t cu_index_htab;
20929
20930 /* Non-zero if the previous_* fields are valid.
20931 We can't write an entry until we see the next entry (since it is only then
20932 that we know the end of the entry). */
20933 int previous_valid;
20934 /* Index of the CU in the table of all CUs in the index file. */
20935 unsigned int previous_cu_index;
20936 /* Start address of the CU. */
20937 CORE_ADDR previous_cu_start;
20938 };
20939
20940 /* Write an address entry to OBSTACK. */
20941
20942 static void
20943 add_address_entry (struct objfile *objfile, struct obstack *obstack,
20944 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
20945 {
20946 offset_type cu_index_to_write;
20947 gdb_byte addr[8];
20948 CORE_ADDR baseaddr;
20949
20950 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20951
20952 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
20953 obstack_grow (obstack, addr, 8);
20954 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
20955 obstack_grow (obstack, addr, 8);
20956 cu_index_to_write = MAYBE_SWAP (cu_index);
20957 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
20958 }
20959
20960 /* Worker function for traversing an addrmap to build the address table. */
20961
20962 static int
20963 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
20964 {
20965 struct addrmap_index_data *data = datap;
20966 struct partial_symtab *pst = obj;
20967
20968 if (data->previous_valid)
20969 add_address_entry (data->objfile, data->addr_obstack,
20970 data->previous_cu_start, start_addr,
20971 data->previous_cu_index);
20972
20973 data->previous_cu_start = start_addr;
20974 if (pst != NULL)
20975 {
20976 struct psymtab_cu_index_map find_map, *map;
20977 find_map.psymtab = pst;
20978 map = htab_find (data->cu_index_htab, &find_map);
20979 gdb_assert (map != NULL);
20980 data->previous_cu_index = map->cu_index;
20981 data->previous_valid = 1;
20982 }
20983 else
20984 data->previous_valid = 0;
20985
20986 return 0;
20987 }
20988
20989 /* Write OBJFILE's address map to OBSTACK.
20990 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
20991 in the index file. */
20992
20993 static void
20994 write_address_map (struct objfile *objfile, struct obstack *obstack,
20995 htab_t cu_index_htab)
20996 {
20997 struct addrmap_index_data addrmap_index_data;
20998
20999 /* When writing the address table, we have to cope with the fact that
21000 the addrmap iterator only provides the start of a region; we have to
21001 wait until the next invocation to get the start of the next region. */
21002
21003 addrmap_index_data.objfile = objfile;
21004 addrmap_index_data.addr_obstack = obstack;
21005 addrmap_index_data.cu_index_htab = cu_index_htab;
21006 addrmap_index_data.previous_valid = 0;
21007
21008 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
21009 &addrmap_index_data);
21010
21011 /* It's highly unlikely the last entry (end address = 0xff...ff)
21012 is valid, but we should still handle it.
21013 The end address is recorded as the start of the next region, but that
21014 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
21015 anyway. */
21016 if (addrmap_index_data.previous_valid)
21017 add_address_entry (objfile, obstack,
21018 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
21019 addrmap_index_data.previous_cu_index);
21020 }
21021
21022 /* Return the symbol kind of PSYM. */
21023
21024 static gdb_index_symbol_kind
21025 symbol_kind (struct partial_symbol *psym)
21026 {
21027 domain_enum domain = PSYMBOL_DOMAIN (psym);
21028 enum address_class aclass = PSYMBOL_CLASS (psym);
21029
21030 switch (domain)
21031 {
21032 case VAR_DOMAIN:
21033 switch (aclass)
21034 {
21035 case LOC_BLOCK:
21036 return GDB_INDEX_SYMBOL_KIND_FUNCTION;
21037 case LOC_TYPEDEF:
21038 return GDB_INDEX_SYMBOL_KIND_TYPE;
21039 case LOC_COMPUTED:
21040 case LOC_CONST_BYTES:
21041 case LOC_OPTIMIZED_OUT:
21042 case LOC_STATIC:
21043 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21044 case LOC_CONST:
21045 /* Note: It's currently impossible to recognize psyms as enum values
21046 short of reading the type info. For now punt. */
21047 return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21048 default:
21049 /* There are other LOC_FOO values that one might want to classify
21050 as variables, but dwarf2read.c doesn't currently use them. */
21051 return GDB_INDEX_SYMBOL_KIND_OTHER;
21052 }
21053 case STRUCT_DOMAIN:
21054 return GDB_INDEX_SYMBOL_KIND_TYPE;
21055 default:
21056 return GDB_INDEX_SYMBOL_KIND_OTHER;
21057 }
21058 }
21059
21060 /* Add a list of partial symbols to SYMTAB. */
21061
21062 static void
21063 write_psymbols (struct mapped_symtab *symtab,
21064 htab_t psyms_seen,
21065 struct partial_symbol **psymp,
21066 int count,
21067 offset_type cu_index,
21068 int is_static)
21069 {
21070 for (; count-- > 0; ++psymp)
21071 {
21072 struct partial_symbol *psym = *psymp;
21073 void **slot;
21074
21075 if (SYMBOL_LANGUAGE (psym) == language_ada)
21076 error (_("Ada is not currently supported by the index"));
21077
21078 /* Only add a given psymbol once. */
21079 slot = htab_find_slot (psyms_seen, psym, INSERT);
21080 if (!*slot)
21081 {
21082 gdb_index_symbol_kind kind = symbol_kind (psym);
21083
21084 *slot = psym;
21085 add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
21086 is_static, kind, cu_index);
21087 }
21088 }
21089 }
21090
21091 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
21092 exception if there is an error. */
21093
21094 static void
21095 write_obstack (FILE *file, struct obstack *obstack)
21096 {
21097 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
21098 file)
21099 != obstack_object_size (obstack))
21100 error (_("couldn't data write to file"));
21101 }
21102
21103 /* Unlink a file if the argument is not NULL. */
21104
21105 static void
21106 unlink_if_set (void *p)
21107 {
21108 char **filename = p;
21109 if (*filename)
21110 unlink (*filename);
21111 }
21112
21113 /* A helper struct used when iterating over debug_types. */
21114 struct signatured_type_index_data
21115 {
21116 struct objfile *objfile;
21117 struct mapped_symtab *symtab;
21118 struct obstack *types_list;
21119 htab_t psyms_seen;
21120 int cu_index;
21121 };
21122
21123 /* A helper function that writes a single signatured_type to an
21124 obstack. */
21125
21126 static int
21127 write_one_signatured_type (void **slot, void *d)
21128 {
21129 struct signatured_type_index_data *info = d;
21130 struct signatured_type *entry = (struct signatured_type *) *slot;
21131 struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
21132 gdb_byte val[8];
21133
21134 write_psymbols (info->symtab,
21135 info->psyms_seen,
21136 info->objfile->global_psymbols.list
21137 + psymtab->globals_offset,
21138 psymtab->n_global_syms, info->cu_index,
21139 0);
21140 write_psymbols (info->symtab,
21141 info->psyms_seen,
21142 info->objfile->static_psymbols.list
21143 + psymtab->statics_offset,
21144 psymtab->n_static_syms, info->cu_index,
21145 1);
21146
21147 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21148 entry->per_cu.offset.sect_off);
21149 obstack_grow (info->types_list, val, 8);
21150 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21151 entry->type_offset_in_tu.cu_off);
21152 obstack_grow (info->types_list, val, 8);
21153 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
21154 obstack_grow (info->types_list, val, 8);
21155
21156 ++info->cu_index;
21157
21158 return 1;
21159 }
21160
21161 /* Recurse into all "included" dependencies and write their symbols as
21162 if they appeared in this psymtab. */
21163
21164 static void
21165 recursively_write_psymbols (struct objfile *objfile,
21166 struct partial_symtab *psymtab,
21167 struct mapped_symtab *symtab,
21168 htab_t psyms_seen,
21169 offset_type cu_index)
21170 {
21171 int i;
21172
21173 for (i = 0; i < psymtab->number_of_dependencies; ++i)
21174 if (psymtab->dependencies[i]->user != NULL)
21175 recursively_write_psymbols (objfile, psymtab->dependencies[i],
21176 symtab, psyms_seen, cu_index);
21177
21178 write_psymbols (symtab,
21179 psyms_seen,
21180 objfile->global_psymbols.list + psymtab->globals_offset,
21181 psymtab->n_global_syms, cu_index,
21182 0);
21183 write_psymbols (symtab,
21184 psyms_seen,
21185 objfile->static_psymbols.list + psymtab->statics_offset,
21186 psymtab->n_static_syms, cu_index,
21187 1);
21188 }
21189
21190 /* Create an index file for OBJFILE in the directory DIR. */
21191
21192 static void
21193 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
21194 {
21195 struct cleanup *cleanup;
21196 char *filename, *cleanup_filename;
21197 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
21198 struct obstack cu_list, types_cu_list;
21199 int i;
21200 FILE *out_file;
21201 struct mapped_symtab *symtab;
21202 offset_type val, size_of_contents, total_len;
21203 struct stat st;
21204 htab_t psyms_seen;
21205 htab_t cu_index_htab;
21206 struct psymtab_cu_index_map *psymtab_cu_index_map;
21207
21208 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
21209 return;
21210
21211 if (dwarf2_per_objfile->using_index)
21212 error (_("Cannot use an index to create the index"));
21213
21214 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
21215 error (_("Cannot make an index when the file has multiple .debug_types sections"));
21216
21217 if (stat (objfile->name, &st) < 0)
21218 perror_with_name (objfile->name);
21219
21220 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
21221 INDEX_SUFFIX, (char *) NULL);
21222 cleanup = make_cleanup (xfree, filename);
21223
21224 out_file = gdb_fopen_cloexec (filename, "wb");
21225 if (!out_file)
21226 error (_("Can't open `%s' for writing"), filename);
21227
21228 cleanup_filename = filename;
21229 make_cleanup (unlink_if_set, &cleanup_filename);
21230
21231 symtab = create_mapped_symtab ();
21232 make_cleanup (cleanup_mapped_symtab, symtab);
21233
21234 obstack_init (&addr_obstack);
21235 make_cleanup_obstack_free (&addr_obstack);
21236
21237 obstack_init (&cu_list);
21238 make_cleanup_obstack_free (&cu_list);
21239
21240 obstack_init (&types_cu_list);
21241 make_cleanup_obstack_free (&types_cu_list);
21242
21243 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
21244 NULL, xcalloc, xfree);
21245 make_cleanup_htab_delete (psyms_seen);
21246
21247 /* While we're scanning CU's create a table that maps a psymtab pointer
21248 (which is what addrmap records) to its index (which is what is recorded
21249 in the index file). This will later be needed to write the address
21250 table. */
21251 cu_index_htab = htab_create_alloc (100,
21252 hash_psymtab_cu_index,
21253 eq_psymtab_cu_index,
21254 NULL, xcalloc, xfree);
21255 make_cleanup_htab_delete (cu_index_htab);
21256 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
21257 xmalloc (sizeof (struct psymtab_cu_index_map)
21258 * dwarf2_per_objfile->n_comp_units);
21259 make_cleanup (xfree, psymtab_cu_index_map);
21260
21261 /* The CU list is already sorted, so we don't need to do additional
21262 work here. Also, the debug_types entries do not appear in
21263 all_comp_units, but only in their own hash table. */
21264 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
21265 {
21266 struct dwarf2_per_cu_data *per_cu
21267 = dwarf2_per_objfile->all_comp_units[i];
21268 struct partial_symtab *psymtab = per_cu->v.psymtab;
21269 gdb_byte val[8];
21270 struct psymtab_cu_index_map *map;
21271 void **slot;
21272
21273 /* CU of a shared file from 'dwz -m' may be unused by this main file.
21274 It may be referenced from a local scope but in such case it does not
21275 need to be present in .gdb_index. */
21276 if (psymtab == NULL)
21277 continue;
21278
21279 if (psymtab->user == NULL)
21280 recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
21281
21282 map = &psymtab_cu_index_map[i];
21283 map->psymtab = psymtab;
21284 map->cu_index = i;
21285 slot = htab_find_slot (cu_index_htab, map, INSERT);
21286 gdb_assert (slot != NULL);
21287 gdb_assert (*slot == NULL);
21288 *slot = map;
21289
21290 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
21291 per_cu->offset.sect_off);
21292 obstack_grow (&cu_list, val, 8);
21293 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
21294 obstack_grow (&cu_list, val, 8);
21295 }
21296
21297 /* Dump the address map. */
21298 write_address_map (objfile, &addr_obstack, cu_index_htab);
21299
21300 /* Write out the .debug_type entries, if any. */
21301 if (dwarf2_per_objfile->signatured_types)
21302 {
21303 struct signatured_type_index_data sig_data;
21304
21305 sig_data.objfile = objfile;
21306 sig_data.symtab = symtab;
21307 sig_data.types_list = &types_cu_list;
21308 sig_data.psyms_seen = psyms_seen;
21309 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
21310 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
21311 write_one_signatured_type, &sig_data);
21312 }
21313
21314 /* Now that we've processed all symbols we can shrink their cu_indices
21315 lists. */
21316 uniquify_cu_indices (symtab);
21317
21318 obstack_init (&constant_pool);
21319 make_cleanup_obstack_free (&constant_pool);
21320 obstack_init (&symtab_obstack);
21321 make_cleanup_obstack_free (&symtab_obstack);
21322 write_hash_table (symtab, &symtab_obstack, &constant_pool);
21323
21324 obstack_init (&contents);
21325 make_cleanup_obstack_free (&contents);
21326 size_of_contents = 6 * sizeof (offset_type);
21327 total_len = size_of_contents;
21328
21329 /* The version number. */
21330 val = MAYBE_SWAP (8);
21331 obstack_grow (&contents, &val, sizeof (val));
21332
21333 /* The offset of the CU list from the start of the file. */
21334 val = MAYBE_SWAP (total_len);
21335 obstack_grow (&contents, &val, sizeof (val));
21336 total_len += obstack_object_size (&cu_list);
21337
21338 /* The offset of the types CU list from the start of the file. */
21339 val = MAYBE_SWAP (total_len);
21340 obstack_grow (&contents, &val, sizeof (val));
21341 total_len += obstack_object_size (&types_cu_list);
21342
21343 /* The offset of the address table from the start of the file. */
21344 val = MAYBE_SWAP (total_len);
21345 obstack_grow (&contents, &val, sizeof (val));
21346 total_len += obstack_object_size (&addr_obstack);
21347
21348 /* The offset of the symbol table from the start of the file. */
21349 val = MAYBE_SWAP (total_len);
21350 obstack_grow (&contents, &val, sizeof (val));
21351 total_len += obstack_object_size (&symtab_obstack);
21352
21353 /* The offset of the constant pool from the start of the file. */
21354 val = MAYBE_SWAP (total_len);
21355 obstack_grow (&contents, &val, sizeof (val));
21356 total_len += obstack_object_size (&constant_pool);
21357
21358 gdb_assert (obstack_object_size (&contents) == size_of_contents);
21359
21360 write_obstack (out_file, &contents);
21361 write_obstack (out_file, &cu_list);
21362 write_obstack (out_file, &types_cu_list);
21363 write_obstack (out_file, &addr_obstack);
21364 write_obstack (out_file, &symtab_obstack);
21365 write_obstack (out_file, &constant_pool);
21366
21367 fclose (out_file);
21368
21369 /* We want to keep the file, so we set cleanup_filename to NULL
21370 here. See unlink_if_set. */
21371 cleanup_filename = NULL;
21372
21373 do_cleanups (cleanup);
21374 }
21375
21376 /* Implementation of the `save gdb-index' command.
21377
21378 Note that the file format used by this command is documented in the
21379 GDB manual. Any changes here must be documented there. */
21380
21381 static void
21382 save_gdb_index_command (char *arg, int from_tty)
21383 {
21384 struct objfile *objfile;
21385
21386 if (!arg || !*arg)
21387 error (_("usage: save gdb-index DIRECTORY"));
21388
21389 ALL_OBJFILES (objfile)
21390 {
21391 struct stat st;
21392
21393 /* If the objfile does not correspond to an actual file, skip it. */
21394 if (stat (objfile->name, &st) < 0)
21395 continue;
21396
21397 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
21398 if (dwarf2_per_objfile)
21399 {
21400 volatile struct gdb_exception except;
21401
21402 TRY_CATCH (except, RETURN_MASK_ERROR)
21403 {
21404 write_psymtabs_to_index (objfile, arg);
21405 }
21406 if (except.reason < 0)
21407 exception_fprintf (gdb_stderr, except,
21408 _("Error while writing index for `%s': "),
21409 objfile->name);
21410 }
21411 }
21412 }
21413
21414 \f
21415
21416 int dwarf2_always_disassemble;
21417
21418 static void
21419 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
21420 struct cmd_list_element *c, const char *value)
21421 {
21422 fprintf_filtered (file,
21423 _("Whether to always disassemble "
21424 "DWARF expressions is %s.\n"),
21425 value);
21426 }
21427
21428 static void
21429 show_check_physname (struct ui_file *file, int from_tty,
21430 struct cmd_list_element *c, const char *value)
21431 {
21432 fprintf_filtered (file,
21433 _("Whether to check \"physname\" is %s.\n"),
21434 value);
21435 }
21436
21437 void _initialize_dwarf2_read (void);
21438
21439 void
21440 _initialize_dwarf2_read (void)
21441 {
21442 struct cmd_list_element *c;
21443
21444 dwarf2_objfile_data_key
21445 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
21446
21447 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
21448 Set DWARF 2 specific variables.\n\
21449 Configure DWARF 2 variables such as the cache size"),
21450 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
21451 0/*allow-unknown*/, &maintenance_set_cmdlist);
21452
21453 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
21454 Show DWARF 2 specific variables\n\
21455 Show DWARF 2 variables such as the cache size"),
21456 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
21457 0/*allow-unknown*/, &maintenance_show_cmdlist);
21458
21459 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
21460 &dwarf2_max_cache_age, _("\
21461 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
21462 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
21463 A higher limit means that cached compilation units will be stored\n\
21464 in memory longer, and more total memory will be used. Zero disables\n\
21465 caching, which can slow down startup."),
21466 NULL,
21467 show_dwarf2_max_cache_age,
21468 &set_dwarf2_cmdlist,
21469 &show_dwarf2_cmdlist);
21470
21471 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
21472 &dwarf2_always_disassemble, _("\
21473 Set whether `info address' always disassembles DWARF expressions."), _("\
21474 Show whether `info address' always disassembles DWARF expressions."), _("\
21475 When enabled, DWARF expressions are always printed in an assembly-like\n\
21476 syntax. When disabled, expressions will be printed in a more\n\
21477 conversational style, when possible."),
21478 NULL,
21479 show_dwarf2_always_disassemble,
21480 &set_dwarf2_cmdlist,
21481 &show_dwarf2_cmdlist);
21482
21483 add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
21484 Set debugging of the dwarf2 reader."), _("\
21485 Show debugging of the dwarf2 reader."), _("\
21486 When enabled, debugging messages are printed during dwarf2 reading\n\
21487 and symtab expansion."),
21488 NULL,
21489 NULL,
21490 &setdebuglist, &showdebuglist);
21491
21492 add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
21493 Set debugging of the dwarf2 DIE reader."), _("\
21494 Show debugging of the dwarf2 DIE reader."), _("\
21495 When enabled (non-zero), DIEs are dumped after they are read in.\n\
21496 The value is the maximum depth to print."),
21497 NULL,
21498 NULL,
21499 &setdebuglist, &showdebuglist);
21500
21501 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
21502 Set cross-checking of \"physname\" code against demangler."), _("\
21503 Show cross-checking of \"physname\" code against demangler."), _("\
21504 When enabled, GDB's internal \"physname\" code is checked against\n\
21505 the demangler."),
21506 NULL, show_check_physname,
21507 &setdebuglist, &showdebuglist);
21508
21509 add_setshow_boolean_cmd ("use-deprecated-index-sections",
21510 no_class, &use_deprecated_index_sections, _("\
21511 Set whether to use deprecated gdb_index sections."), _("\
21512 Show whether to use deprecated gdb_index sections."), _("\
21513 When enabled, deprecated .gdb_index sections are used anyway.\n\
21514 Normally they are ignored either because of a missing feature or\n\
21515 performance issue.\n\
21516 Warning: This option must be enabled before gdb reads the file."),
21517 NULL,
21518 NULL,
21519 &setlist, &showlist);
21520
21521 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
21522 _("\
21523 Save a gdb-index file.\n\
21524 Usage: save gdb-index DIRECTORY"),
21525 &save_cmdlist);
21526 set_cmd_completer (c, filename_completer);
21527
21528 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
21529 &dwarf2_locexpr_funcs);
21530 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
21531 &dwarf2_loclist_funcs);
21532
21533 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
21534 &dwarf2_block_frame_base_locexpr_funcs);
21535 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
21536 &dwarf2_block_frame_base_loclist_funcs);
21537 }
This page took 0.513194 seconds and 4 git commands to generate.