Document array indexing for Python gdb.Value
[binutils-gdb.git] / gdb / dwarf2 / read-debug-names.c
1 /* Reading code for .debug_names
2
3 Copyright (C) 2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "read-debug-names.h"
22
23 #include "complaints.h"
24 #include "cp-support.h"
25 #include "dwz.h"
26 #include "mapped-index.h"
27 #include "read.h"
28 #include "stringify.h"
29
30 /* A description of the mapped .debug_names.
31 Uninitialized map has CU_COUNT 0. */
32
33 struct mapped_debug_names final : public mapped_index_base
34 {
35 bfd_endian dwarf5_byte_order;
36 bool dwarf5_is_dwarf64;
37 bool augmentation_is_gdb;
38 uint8_t offset_size;
39 uint32_t cu_count = 0;
40 uint32_t tu_count, bucket_count, name_count;
41 const gdb_byte *cu_table_reordered, *tu_table_reordered;
42 const uint32_t *bucket_table_reordered, *hash_table_reordered;
43 const gdb_byte *name_table_string_offs_reordered;
44 const gdb_byte *name_table_entry_offs_reordered;
45 const gdb_byte *entry_pool;
46
47 struct index_val
48 {
49 ULONGEST dwarf_tag;
50 struct attr
51 {
52 /* Attribute name DW_IDX_*. */
53 ULONGEST dw_idx;
54
55 /* Attribute form DW_FORM_*. */
56 ULONGEST form;
57
58 /* Value if FORM is DW_FORM_implicit_const. */
59 LONGEST implicit_const;
60 };
61 std::vector<attr> attr_vec;
62 };
63
64 std::unordered_map<ULONGEST, index_val> abbrev_map;
65
66 const char *namei_to_name
67 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
68
69 /* Implementation of the mapped_index_base virtual interface, for
70 the name_components cache. */
71
72 const char *symbol_name_at
73 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
74 { return namei_to_name (idx, per_objfile); }
75
76 size_t symbol_name_count () const override
77 { return this->name_count; }
78
79 quick_symbol_functions_up make_quick_functions () const override;
80 };
81
82 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
83 {
84 void dump (struct objfile *objfile) override;
85
86 void expand_matching_symbols
87 (struct objfile *,
88 const lookup_name_info &lookup_name,
89 domain_enum domain,
90 int global,
91 symbol_compare_ftype *ordered_compare) override;
92
93 bool expand_symtabs_matching
94 (struct objfile *objfile,
95 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
96 const lookup_name_info *lookup_name,
97 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
98 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
99 block_search_flags search_flags,
100 domain_enum domain,
101 enum search_domain kind) override;
102 };
103
104 quick_symbol_functions_up
105 mapped_debug_names::make_quick_functions () const
106 {
107 return quick_symbol_functions_up (new dwarf2_debug_names_index);
108 }
109
110 /* Create the signatured type hash table from .debug_names. */
111
112 static void
113 create_signatured_type_table_from_debug_names
114 (dwarf2_per_objfile *per_objfile,
115 const mapped_debug_names &map,
116 struct dwarf2_section_info *section,
117 struct dwarf2_section_info *abbrev_section)
118 {
119 struct objfile *objfile = per_objfile->objfile;
120
121 section->read (objfile);
122 abbrev_section->read (objfile);
123
124 htab_up sig_types_hash = allocate_signatured_type_table ();
125
126 for (uint32_t i = 0; i < map.tu_count; ++i)
127 {
128 signatured_type_up sig_type;
129 void **slot;
130
131 sect_offset sect_off
132 = (sect_offset) (extract_unsigned_integer
133 (map.tu_table_reordered + i * map.offset_size,
134 map.offset_size,
135 map.dwarf5_byte_order));
136
137 comp_unit_head cu_header;
138 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
139 abbrev_section,
140 section->buffer + to_underlying (sect_off),
141 rcuh_kind::TYPE);
142
143 sig_type = per_objfile->per_bfd->allocate_signatured_type
144 (cu_header.signature);
145 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
146 sig_type->section = section;
147 sig_type->sect_off = sect_off;
148
149 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
150 *slot = sig_type.get ();
151
152 per_objfile->per_bfd->all_units.emplace_back (sig_type.release ());
153 }
154
155 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
156 }
157
158 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
159 populate the index_addrmap. */
160
161 static void
162 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
163 struct dwarf2_section_info *section)
164 {
165 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
166
167 addrmap_mutable mutable_map;
168
169 if (read_addrmap_from_aranges (per_objfile, section, &mutable_map))
170 per_bfd->index_addrmap
171 = new (&per_bfd->obstack) addrmap_fixed (&per_bfd->obstack,
172 &mutable_map);
173 }
174
175 /* DWARF-5 debug_names reader. */
176
177 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
178 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
179
180 /* A helper function that reads the .debug_names section in SECTION
181 and fills in MAP. FILENAME is the name of the file containing the
182 section; it is used for error reporting.
183
184 Returns true if all went well, false otherwise. */
185
186 static bool
187 read_debug_names_from_section (struct objfile *objfile,
188 const char *filename,
189 struct dwarf2_section_info *section,
190 mapped_debug_names &map)
191 {
192 if (section->empty ())
193 return false;
194
195 /* Older elfutils strip versions could keep the section in the main
196 executable while splitting it for the separate debug info file. */
197 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
198 return false;
199
200 section->read (objfile);
201
202 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
203
204 const gdb_byte *addr = section->buffer;
205
206 bfd *const abfd = section->get_bfd_owner ();
207
208 unsigned int bytes_read;
209 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
210 addr += bytes_read;
211
212 map.dwarf5_is_dwarf64 = bytes_read != 4;
213 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
214 if (bytes_read + length != section->size)
215 {
216 /* There may be multiple per-CU indices. */
217 warning (_("Section .debug_names in %s length %s does not match "
218 "section length %s, ignoring .debug_names."),
219 filename, plongest (bytes_read + length),
220 pulongest (section->size));
221 return false;
222 }
223
224 /* The version number. */
225 uint16_t version = read_2_bytes (abfd, addr);
226 addr += 2;
227 if (version != 5)
228 {
229 warning (_("Section .debug_names in %s has unsupported version %d, "
230 "ignoring .debug_names."),
231 filename, version);
232 return false;
233 }
234
235 /* Padding. */
236 uint16_t padding = read_2_bytes (abfd, addr);
237 addr += 2;
238 if (padding != 0)
239 {
240 warning (_("Section .debug_names in %s has unsupported padding %d, "
241 "ignoring .debug_names."),
242 filename, padding);
243 return false;
244 }
245
246 /* comp_unit_count - The number of CUs in the CU list. */
247 map.cu_count = read_4_bytes (abfd, addr);
248 addr += 4;
249
250 /* local_type_unit_count - The number of TUs in the local TU
251 list. */
252 map.tu_count = read_4_bytes (abfd, addr);
253 addr += 4;
254
255 /* foreign_type_unit_count - The number of TUs in the foreign TU
256 list. */
257 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
258 addr += 4;
259 if (foreign_tu_count != 0)
260 {
261 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
262 "ignoring .debug_names."),
263 filename, static_cast<unsigned long> (foreign_tu_count));
264 return false;
265 }
266
267 /* bucket_count - The number of hash buckets in the hash lookup
268 table. */
269 map.bucket_count = read_4_bytes (abfd, addr);
270 addr += 4;
271
272 /* name_count - The number of unique names in the index. */
273 map.name_count = read_4_bytes (abfd, addr);
274 addr += 4;
275
276 /* abbrev_table_size - The size in bytes of the abbreviations
277 table. */
278 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
279 addr += 4;
280
281 /* augmentation_string_size - The size in bytes of the augmentation
282 string. This value is rounded up to a multiple of 4. */
283 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
284 addr += 4;
285 map.augmentation_is_gdb = ((augmentation_string_size
286 == sizeof (dwarf5_augmentation))
287 && memcmp (addr, dwarf5_augmentation,
288 sizeof (dwarf5_augmentation)) == 0);
289 augmentation_string_size += (-augmentation_string_size) & 3;
290 addr += augmentation_string_size;
291
292 /* List of CUs */
293 map.cu_table_reordered = addr;
294 addr += map.cu_count * map.offset_size;
295
296 /* List of Local TUs */
297 map.tu_table_reordered = addr;
298 addr += map.tu_count * map.offset_size;
299
300 /* Hash Lookup Table */
301 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
302 addr += map.bucket_count * 4;
303 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
304 addr += map.name_count * 4;
305
306 /* Name Table */
307 map.name_table_string_offs_reordered = addr;
308 addr += map.name_count * map.offset_size;
309 map.name_table_entry_offs_reordered = addr;
310 addr += map.name_count * map.offset_size;
311
312 const gdb_byte *abbrev_table_start = addr;
313 for (;;)
314 {
315 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
316 addr += bytes_read;
317 if (index_num == 0)
318 break;
319
320 const auto insertpair
321 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
322 if (!insertpair.second)
323 {
324 warning (_("Section .debug_names in %s has duplicate index %s, "
325 "ignoring .debug_names."),
326 filename, pulongest (index_num));
327 return false;
328 }
329 mapped_debug_names::index_val &indexval = insertpair.first->second;
330 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
331 addr += bytes_read;
332
333 for (;;)
334 {
335 mapped_debug_names::index_val::attr attr;
336 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
337 addr += bytes_read;
338 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
339 addr += bytes_read;
340 if (attr.form == DW_FORM_implicit_const)
341 {
342 attr.implicit_const = read_signed_leb128 (abfd, addr,
343 &bytes_read);
344 addr += bytes_read;
345 }
346 if (attr.dw_idx == 0 && attr.form == 0)
347 break;
348 indexval.attr_vec.push_back (std::move (attr));
349 }
350 }
351 if (addr != abbrev_table_start + abbrev_table_size)
352 {
353 warning (_("Section .debug_names in %s has abbreviation_table "
354 "of size %s vs. written as %u, ignoring .debug_names."),
355 filename, plongest (addr - abbrev_table_start),
356 abbrev_table_size);
357 return false;
358 }
359 map.entry_pool = addr;
360
361 return true;
362 }
363
364 /* A helper for create_cus_from_debug_names that handles the MAP's CU
365 list. */
366
367 static bool
368 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
369 const mapped_debug_names &map,
370 dwarf2_section_info &section,
371 bool is_dwz)
372 {
373 if (!map.augmentation_is_gdb)
374 {
375 for (uint32_t i = 0; i < map.cu_count; ++i)
376 {
377 sect_offset sect_off
378 = (sect_offset) (extract_unsigned_integer
379 (map.cu_table_reordered + i * map.offset_size,
380 map.offset_size,
381 map.dwarf5_byte_order));
382 /* We don't know the length of the CU, because the CU list in a
383 .debug_names index can be incomplete, so we can't use the start
384 of the next CU as end of this CU. We create the CUs here with
385 length 0, and in cutu_reader::cutu_reader we'll fill in the
386 actual length. */
387 dwarf2_per_cu_data_up per_cu
388 = create_cu_from_index_list (per_bfd, &section, is_dwz,
389 sect_off, 0);
390 per_bfd->all_units.push_back (std::move (per_cu));
391 }
392 return true;
393 }
394
395 sect_offset sect_off_prev;
396 for (uint32_t i = 0; i <= map.cu_count; ++i)
397 {
398 sect_offset sect_off_next;
399 if (i < map.cu_count)
400 {
401 sect_off_next
402 = (sect_offset) (extract_unsigned_integer
403 (map.cu_table_reordered + i * map.offset_size,
404 map.offset_size,
405 map.dwarf5_byte_order));
406 }
407 else
408 sect_off_next = (sect_offset) section.size;
409 if (i >= 1)
410 {
411 if (sect_off_next == sect_off_prev)
412 {
413 warning (_("Section .debug_names has duplicate entry in CU table,"
414 " ignoring .debug_names."));
415 return false;
416 }
417 if (sect_off_next < sect_off_prev)
418 {
419 warning (_("Section .debug_names has non-ascending CU table,"
420 " ignoring .debug_names."));
421 return false;
422 }
423 /* Note: we're not using length = sect_off_next - sect_off_prev,
424 to gracefully handle an incomplete CU list. */
425 const ULONGEST length = 0;
426 dwarf2_per_cu_data_up per_cu
427 = create_cu_from_index_list (per_bfd, &section, is_dwz,
428 sect_off_prev, length);
429 per_bfd->all_units.push_back (std::move (per_cu));
430 }
431 sect_off_prev = sect_off_next;
432 }
433
434 return true;
435 }
436
437 /* Read the CU list from the mapped index, and use it to create all
438 the CU objects for this dwarf2_per_objfile. */
439
440 static bool
441 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
442 const mapped_debug_names &map,
443 const mapped_debug_names &dwz_map)
444 {
445 gdb_assert (per_bfd->all_units.empty ());
446 per_bfd->all_units.reserve (map.cu_count + dwz_map.cu_count);
447
448 if (!create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
449 false /* is_dwz */))
450 return false;
451
452 if (dwz_map.cu_count == 0)
453 return true;
454
455 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
456 return create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
457 true /* is_dwz */);
458 }
459
460 /* See read-debug-names.h. */
461
462 bool
463 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
464 {
465 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
466 mapped_debug_names dwz_map;
467 struct objfile *objfile = per_objfile->objfile;
468 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
469
470 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
471 &per_bfd->debug_names, *map))
472 return false;
473
474 /* Don't use the index if it's empty. */
475 if (map->name_count == 0)
476 return false;
477
478 /* If there is a .dwz file, read it so we can get its CU list as
479 well. */
480 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
481 if (dwz != NULL)
482 {
483 if (!read_debug_names_from_section (objfile,
484 bfd_get_filename (dwz->dwz_bfd.get ()),
485 &dwz->debug_names, dwz_map))
486 {
487 warning (_("could not read '.debug_names' section from %s; skipping"),
488 bfd_get_filename (dwz->dwz_bfd.get ()));
489 return false;
490 }
491 }
492
493 if (!create_cus_from_debug_names (per_bfd, *map, dwz_map))
494 {
495 per_bfd->all_units.clear ();
496 return false;
497 }
498
499 if (map->tu_count != 0)
500 {
501 /* We can only handle a single .debug_types when we have an
502 index. */
503 if (per_bfd->types.size () > 1)
504 {
505 per_bfd->all_units.clear ();
506 return false;
507 }
508
509 dwarf2_section_info *section
510 = (per_bfd->types.size () == 1
511 ? &per_bfd->types[0]
512 : &per_bfd->info);
513
514 create_signatured_type_table_from_debug_names
515 (per_objfile, *map, section, &per_bfd->abbrev);
516 }
517
518 finalize_all_units (per_bfd);
519
520 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
521
522 per_bfd->index_table = std::move (map);
523 per_bfd->quick_file_names_table =
524 create_quick_file_names_table (per_bfd->all_units.size ());
525
526 return true;
527 }
528
529 /* Type used to manage iterating over all CUs looking for a symbol for
530 .debug_names. */
531
532 class dw2_debug_names_iterator
533 {
534 public:
535 dw2_debug_names_iterator (const mapped_debug_names &map,
536 block_search_flags block_index,
537 domain_enum domain,
538 const char *name, dwarf2_per_objfile *per_objfile)
539 : m_map (map), m_block_index (block_index), m_domain (domain),
540 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
541 m_per_objfile (per_objfile)
542 {}
543
544 dw2_debug_names_iterator (const mapped_debug_names &map,
545 search_domain search, uint32_t namei,
546 dwarf2_per_objfile *per_objfile,
547 domain_enum domain = UNDEF_DOMAIN)
548 : m_map (map),
549 m_domain (domain),
550 m_search (search),
551 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
552 m_per_objfile (per_objfile)
553 {}
554
555 dw2_debug_names_iterator (const mapped_debug_names &map,
556 block_search_flags block_index, domain_enum domain,
557 uint32_t namei, dwarf2_per_objfile *per_objfile)
558 : m_map (map), m_block_index (block_index), m_domain (domain),
559 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
560 m_per_objfile (per_objfile)
561 {}
562
563 /* Return the next matching CU or NULL if there are no more. */
564 dwarf2_per_cu_data *next ();
565
566 private:
567 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
568 const char *name,
569 dwarf2_per_objfile *per_objfile);
570 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
571 uint32_t namei,
572 dwarf2_per_objfile *per_objfile);
573
574 /* The internalized form of .debug_names. */
575 const mapped_debug_names &m_map;
576
577 /* Restrict the search to these blocks. */
578 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
579 | SEARCH_STATIC_BLOCK);
580
581 /* The kind of symbol we're looking for. */
582 const domain_enum m_domain = UNDEF_DOMAIN;
583 const search_domain m_search = ALL_DOMAIN;
584
585 /* The list of CUs from the index entry of the symbol, or NULL if
586 not found. */
587 const gdb_byte *m_addr;
588
589 dwarf2_per_objfile *m_per_objfile;
590 };
591
592 const char *
593 mapped_debug_names::namei_to_name
594 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
595 {
596 const ULONGEST namei_string_offs
597 = extract_unsigned_integer ((name_table_string_offs_reordered
598 + namei * offset_size),
599 offset_size,
600 dwarf5_byte_order);
601 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
602 }
603
604 /* Find a slot in .debug_names for the object named NAME. If NAME is
605 found, return pointer to its pool data. If NAME cannot be found,
606 return NULL. */
607
608 const gdb_byte *
609 dw2_debug_names_iterator::find_vec_in_debug_names
610 (const mapped_debug_names &map, const char *name,
611 dwarf2_per_objfile *per_objfile)
612 {
613 int (*cmp) (const char *, const char *);
614
615 gdb::unique_xmalloc_ptr<char> without_params;
616 if (current_language->la_language == language_cplus
617 || current_language->la_language == language_fortran
618 || current_language->la_language == language_d)
619 {
620 /* NAME is already canonical. Drop any qualifiers as
621 .debug_names does not contain any. */
622
623 if (strchr (name, '(') != NULL)
624 {
625 without_params = cp_remove_params (name);
626 if (without_params != NULL)
627 name = without_params.get ();
628 }
629 }
630
631 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
632
633 const uint32_t full_hash = dwarf5_djb_hash (name);
634 uint32_t namei
635 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
636 (map.bucket_table_reordered
637 + (full_hash % map.bucket_count)), 4,
638 map.dwarf5_byte_order);
639 if (namei == 0)
640 return NULL;
641 --namei;
642 if (namei >= map.name_count)
643 {
644 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
645 "[in module %s]"),
646 namei, map.name_count,
647 objfile_name (per_objfile->objfile));
648 return NULL;
649 }
650
651 for (;;)
652 {
653 const uint32_t namei_full_hash
654 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
655 (map.hash_table_reordered + namei), 4,
656 map.dwarf5_byte_order);
657 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
658 return NULL;
659
660 if (full_hash == namei_full_hash)
661 {
662 const char *const namei_string = map.namei_to_name (namei, per_objfile);
663
664 #if 0 /* An expensive sanity check. */
665 if (namei_full_hash != dwarf5_djb_hash (namei_string))
666 {
667 complaint (_("Wrong .debug_names hash for string at index %u "
668 "[in module %s]"),
669 namei, objfile_name (dwarf2_per_objfile->objfile));
670 return NULL;
671 }
672 #endif
673
674 if (cmp (namei_string, name) == 0)
675 {
676 const ULONGEST namei_entry_offs
677 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
678 + namei * map.offset_size),
679 map.offset_size, map.dwarf5_byte_order);
680 return map.entry_pool + namei_entry_offs;
681 }
682 }
683
684 ++namei;
685 if (namei >= map.name_count)
686 return NULL;
687 }
688 }
689
690 const gdb_byte *
691 dw2_debug_names_iterator::find_vec_in_debug_names
692 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
693 {
694 if (namei >= map.name_count)
695 {
696 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
697 "[in module %s]"),
698 namei, map.name_count,
699 objfile_name (per_objfile->objfile));
700 return NULL;
701 }
702
703 const ULONGEST namei_entry_offs
704 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
705 + namei * map.offset_size),
706 map.offset_size, map.dwarf5_byte_order);
707 return map.entry_pool + namei_entry_offs;
708 }
709
710 /* See dw2_debug_names_iterator. */
711
712 dwarf2_per_cu_data *
713 dw2_debug_names_iterator::next ()
714 {
715 if (m_addr == NULL)
716 return NULL;
717
718 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
719 struct objfile *objfile = m_per_objfile->objfile;
720 bfd *const abfd = objfile->obfd.get ();
721
722 again:
723
724 unsigned int bytes_read;
725 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
726 m_addr += bytes_read;
727 if (abbrev == 0)
728 return NULL;
729
730 const auto indexval_it = m_map.abbrev_map.find (abbrev);
731 if (indexval_it == m_map.abbrev_map.cend ())
732 {
733 complaint (_("Wrong .debug_names undefined abbrev code %s "
734 "[in module %s]"),
735 pulongest (abbrev), objfile_name (objfile));
736 return NULL;
737 }
738 const mapped_debug_names::index_val &indexval = indexval_it->second;
739 enum class symbol_linkage {
740 unknown,
741 static_,
742 extern_,
743 } symbol_linkage_ = symbol_linkage::unknown;
744 dwarf2_per_cu_data *per_cu = NULL;
745 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
746 {
747 ULONGEST ull;
748 switch (attr.form)
749 {
750 case DW_FORM_implicit_const:
751 ull = attr.implicit_const;
752 break;
753 case DW_FORM_flag_present:
754 ull = 1;
755 break;
756 case DW_FORM_udata:
757 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
758 m_addr += bytes_read;
759 break;
760 case DW_FORM_ref4:
761 ull = read_4_bytes (abfd, m_addr);
762 m_addr += 4;
763 break;
764 case DW_FORM_ref8:
765 ull = read_8_bytes (abfd, m_addr);
766 m_addr += 8;
767 break;
768 case DW_FORM_ref_sig8:
769 ull = read_8_bytes (abfd, m_addr);
770 m_addr += 8;
771 break;
772 default:
773 complaint (_("Unsupported .debug_names form %s [in module %s]"),
774 dwarf_form_name (attr.form),
775 objfile_name (objfile));
776 return NULL;
777 }
778 switch (attr.dw_idx)
779 {
780 case DW_IDX_compile_unit:
781 {
782 /* Don't crash on bad data. */
783 if (ull >= per_bfd->all_comp_units.size ())
784 {
785 complaint (_(".debug_names entry has bad CU index %s"
786 " [in module %s]"),
787 pulongest (ull),
788 objfile_name (objfile));
789 continue;
790 }
791 }
792 per_cu = per_bfd->get_cu (ull);
793 break;
794 case DW_IDX_type_unit:
795 /* Don't crash on bad data. */
796 if (ull >= per_bfd->all_type_units.size ())
797 {
798 complaint (_(".debug_names entry has bad TU index %s"
799 " [in module %s]"),
800 pulongest (ull),
801 objfile_name (objfile));
802 continue;
803 }
804 {
805 int nr_cus = per_bfd->all_comp_units.size ();
806 per_cu = per_bfd->get_cu (nr_cus + ull);
807 }
808 break;
809 case DW_IDX_die_offset:
810 /* In a per-CU index (as opposed to a per-module index), index
811 entries without CU attribute implicitly refer to the single CU. */
812 if (per_cu == NULL)
813 per_cu = per_bfd->get_cu (0);
814 break;
815 case DW_IDX_GNU_internal:
816 if (!m_map.augmentation_is_gdb)
817 break;
818 symbol_linkage_ = symbol_linkage::static_;
819 break;
820 case DW_IDX_GNU_external:
821 if (!m_map.augmentation_is_gdb)
822 break;
823 symbol_linkage_ = symbol_linkage::extern_;
824 break;
825 }
826 }
827
828 /* Skip if we couldn't find a valid CU/TU index. */
829 if (per_cu == nullptr)
830 goto again;
831
832 /* Skip if already read in. */
833 if (m_per_objfile->symtab_set_p (per_cu))
834 goto again;
835
836 /* Check static vs global. */
837 if (symbol_linkage_ != symbol_linkage::unknown)
838 {
839 if (symbol_linkage_ == symbol_linkage::static_)
840 {
841 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
842 goto again;
843 }
844 else
845 {
846 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
847 goto again;
848 }
849 }
850
851 /* Match dw2_symtab_iter_next, symbol_kind
852 and debug_names::psymbol_tag. */
853 switch (m_domain)
854 {
855 case VAR_DOMAIN:
856 switch (indexval.dwarf_tag)
857 {
858 case DW_TAG_variable:
859 case DW_TAG_subprogram:
860 /* Some types are also in VAR_DOMAIN. */
861 case DW_TAG_typedef:
862 case DW_TAG_structure_type:
863 break;
864 default:
865 goto again;
866 }
867 break;
868 case STRUCT_DOMAIN:
869 switch (indexval.dwarf_tag)
870 {
871 case DW_TAG_typedef:
872 case DW_TAG_structure_type:
873 break;
874 default:
875 goto again;
876 }
877 break;
878 case LABEL_DOMAIN:
879 switch (indexval.dwarf_tag)
880 {
881 case 0:
882 case DW_TAG_variable:
883 break;
884 default:
885 goto again;
886 }
887 break;
888 case MODULE_DOMAIN:
889 switch (indexval.dwarf_tag)
890 {
891 case DW_TAG_module:
892 break;
893 default:
894 goto again;
895 }
896 break;
897 default:
898 break;
899 }
900
901 /* Match dw2_expand_symtabs_matching, symbol_kind and
902 debug_names::psymbol_tag. */
903 switch (m_search)
904 {
905 case VARIABLES_DOMAIN:
906 switch (indexval.dwarf_tag)
907 {
908 case DW_TAG_variable:
909 break;
910 default:
911 goto again;
912 }
913 break;
914 case FUNCTIONS_DOMAIN:
915 switch (indexval.dwarf_tag)
916 {
917 case DW_TAG_subprogram:
918 break;
919 default:
920 goto again;
921 }
922 break;
923 case TYPES_DOMAIN:
924 switch (indexval.dwarf_tag)
925 {
926 case DW_TAG_typedef:
927 case DW_TAG_structure_type:
928 break;
929 default:
930 goto again;
931 }
932 break;
933 case MODULES_DOMAIN:
934 switch (indexval.dwarf_tag)
935 {
936 case DW_TAG_module:
937 break;
938 default:
939 goto again;
940 }
941 default:
942 break;
943 }
944
945 return per_cu;
946 }
947
948 /* This dumps minimal information about .debug_names. It is called
949 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
950 uses this to verify that .debug_names has been loaded. */
951
952 void
953 dwarf2_debug_names_index::dump (struct objfile *objfile)
954 {
955 gdb_printf (".debug_names: exists\n");
956 }
957
958 void
959 dwarf2_debug_names_index::expand_matching_symbols
960 (struct objfile *objfile,
961 const lookup_name_info &name, domain_enum domain,
962 int global,
963 symbol_compare_ftype *ordered_compare)
964 {
965 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
966
967 mapped_debug_names &map
968 = *(gdb::checked_static_cast<mapped_debug_names *>
969 (per_objfile->per_bfd->index_table.get ()));
970 const block_search_flags block_flags
971 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
972
973 const char *match_name = name.ada ().lookup_name ().c_str ();
974 auto matcher = [&] (const char *symname)
975 {
976 if (ordered_compare == nullptr)
977 return true;
978 return ordered_compare (symname, match_name) == 0;
979 };
980
981 dw2_expand_symtabs_matching_symbol (map, name, matcher,
982 [&] (offset_type namei)
983 {
984 /* The name was matched, now expand corresponding CUs that were
985 marked. */
986 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
987 per_objfile);
988
989 struct dwarf2_per_cu_data *per_cu;
990 while ((per_cu = iter.next ()) != NULL)
991 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
992 nullptr);
993 return true;
994 }, per_objfile);
995 }
996
997 bool
998 dwarf2_debug_names_index::expand_symtabs_matching
999 (struct objfile *objfile,
1000 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
1001 const lookup_name_info *lookup_name,
1002 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
1003 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
1004 block_search_flags search_flags,
1005 domain_enum domain,
1006 enum search_domain kind)
1007 {
1008 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1009
1010 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
1011
1012 /* This invariant is documented in quick-functions.h. */
1013 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
1014 if (lookup_name == nullptr)
1015 {
1016 for (dwarf2_per_cu_data *per_cu
1017 : all_units_range (per_objfile->per_bfd))
1018 {
1019 QUIT;
1020
1021 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
1022 file_matcher,
1023 expansion_notify))
1024 return false;
1025 }
1026 return true;
1027 }
1028
1029 mapped_debug_names &map
1030 = *(gdb::checked_static_cast<mapped_debug_names *>
1031 (per_objfile->per_bfd->index_table.get ()));
1032
1033 bool result
1034 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
1035 symbol_matcher,
1036 [&] (offset_type namei)
1037 {
1038 /* The name was matched, now expand corresponding CUs that were
1039 marked. */
1040 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
1041
1042 struct dwarf2_per_cu_data *per_cu;
1043 while ((per_cu = iter.next ()) != NULL)
1044 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
1045 file_matcher,
1046 expansion_notify))
1047 return false;
1048 return true;
1049 }, per_objfile);
1050
1051 return result;
1052 }