xref: /netbsd-src/external/gpl3/gdb/dist/gdb/dwarf2/read-debug-names.c (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
1 /* Reading code for .debug_names
2 
3    Copyright (C) 2023-2024 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 "read-debug-names.h"
21 #include "dwarf2/aranges.h"
22 #include "dwarf2/cooked-index.h"
23 
24 #include "complaints.h"
25 #include "cp-support.h"
26 #include "dwz.h"
27 #include "mapped-index.h"
28 #include "read.h"
29 #include "stringify.h"
30 
31 /* This is just like cooked_index_functions, but overrides a single
32    method so the test suite can distinguish the .debug_names case from
33    the ordinary case.  */
34 struct dwarf2_debug_names_index : public cooked_index_functions
35 {
36   /* This dumps minimal information about .debug_names.  It is called
37      via "mt print objfiles".  The gdb.dwarf2/gdb-index.exp testcase
38      uses this to verify that .debug_names has been loaded.  */
39   void dump (struct objfile *objfile) override
40   {
41     gdb_printf (".debug_names: exists\n");
42     /* This could call the superclass method if that's useful.  */
43   }
44 };
45 
46 /* This is like a cooked index, but as it has been ingested from
47    .debug_names, it can't be used to write out an index.  */
48 class debug_names_index : public cooked_index
49 {
50 public:
51 
52   using cooked_index::cooked_index;
53 
54   cooked_index *index_for_writing () override
55   { return nullptr; }
56 
57   quick_symbol_functions_up make_quick_functions () const override
58   { return quick_symbol_functions_up (new dwarf2_debug_names_index); }
59 };
60 
61 /* A description of the mapped .debug_names.  */
62 
63 struct mapped_debug_names_reader
64 {
65   const gdb_byte *scan_one_entry (const char *name,
66 				  const gdb_byte *entry,
67 				  cooked_index_entry **result,
68 				  std::optional<ULONGEST> &parent);
69   void scan_entries (uint32_t index, const char *name, const gdb_byte *entry);
70   void scan_all_names ();
71 
72   dwarf2_per_objfile *per_objfile = nullptr;
73   bfd *abfd = nullptr;
74   bfd_endian dwarf5_byte_order {};
75   bool dwarf5_is_dwarf64 = false;
76   bool augmentation_is_gdb = false;
77   uint8_t offset_size = 0;
78   uint32_t cu_count = 0;
79   uint32_t tu_count = 0, bucket_count = 0, name_count = 0;
80   const gdb_byte *cu_table_reordered = nullptr;
81   const gdb_byte *tu_table_reordered = nullptr;
82   const uint32_t *bucket_table_reordered = nullptr;
83   const uint32_t *hash_table_reordered = nullptr;
84   const gdb_byte *name_table_string_offs_reordered = nullptr;
85   const gdb_byte *name_table_entry_offs_reordered = nullptr;
86   const gdb_byte *entry_pool = nullptr;
87 
88   struct index_val
89   {
90     ULONGEST dwarf_tag;
91     struct attr
92     {
93       /* Attribute name DW_IDX_*.  */
94       ULONGEST dw_idx;
95 
96       /* Attribute form DW_FORM_*.  */
97       ULONGEST form;
98 
99       /* Value if FORM is DW_FORM_implicit_const.  */
100       LONGEST implicit_const;
101     };
102     std::vector<attr> attr_vec;
103   };
104 
105   std::unordered_map<ULONGEST, index_val> abbrev_map;
106 
107   std::unique_ptr<cooked_index_shard> shard;
108   std::vector<std::pair<cooked_index_entry *, ULONGEST>> needs_parent;
109   std::vector<std::vector<cooked_index_entry *>> all_entries;
110 };
111 
112 /* Scan a single entry from the entries table.  Set *RESULT and PARENT
113    (if needed) and return the updated pointer on success, or return
114    nullptr on error, or at the end of the table.  */
115 
116 const gdb_byte *
117 mapped_debug_names_reader::scan_one_entry (const char *name,
118 					   const gdb_byte *entry,
119 					   cooked_index_entry **result,
120 					   std::optional<ULONGEST> &parent)
121 {
122   unsigned int bytes_read;
123   const ULONGEST abbrev = read_unsigned_leb128 (abfd, entry, &bytes_read);
124   entry += bytes_read;
125   if (abbrev == 0)
126     return nullptr;
127 
128   const auto indexval_it = abbrev_map.find (abbrev);
129   if (indexval_it == abbrev_map.cend ())
130     {
131       complaint (_("Wrong .debug_names undefined abbrev code %s "
132 		   "[in module %s]"),
133 		 pulongest (abbrev), bfd_get_filename (abfd));
134       return nullptr;
135     }
136 
137   const auto &indexval = indexval_it->second;
138   cooked_index_flag flags = 0;
139   sect_offset die_offset {};
140   enum language lang = language_unknown;
141   dwarf2_per_cu_data *per_cu = nullptr;
142   for (const auto &attr : indexval.attr_vec)
143     {
144       ULONGEST ull;
145       switch (attr.form)
146 	{
147 	case DW_FORM_implicit_const:
148 	  ull = attr.implicit_const;
149 	  break;
150 	case DW_FORM_flag_present:
151 	  ull = 1;
152 	  break;
153 	case DW_FORM_udata:
154 	  ull = read_unsigned_leb128 (abfd, entry, &bytes_read);
155 	  entry += bytes_read;
156 	  break;
157 	case DW_FORM_ref_addr:
158 	  ull = read_offset (abfd, entry, offset_size);
159 	  entry += offset_size;
160 	  break;
161 	case DW_FORM_ref4:
162 	  ull = read_4_bytes (abfd, entry);
163 	  entry += 4;
164 	  break;
165 	case DW_FORM_ref8:
166 	  ull = read_8_bytes (abfd, entry);
167 	  entry += 8;
168 	  break;
169 	case DW_FORM_ref_sig8:
170 	  ull = read_8_bytes (abfd, entry);
171 	  entry += 8;
172 	  break;
173 	default:
174 	  complaint (_("Unsupported .debug_names form %s [in module %s]"),
175 		     dwarf_form_name (attr.form),
176 		     bfd_get_filename (abfd));
177 	  return nullptr;
178 	}
179       switch (attr.dw_idx)
180 	{
181 	case DW_IDX_compile_unit:
182 	  {
183 	    /* Don't crash on bad data.  */
184 	    if (ull >= per_objfile->per_bfd->all_comp_units.size ())
185 	      {
186 		complaint (_(".debug_names entry has bad CU index %s"
187 			     " [in module %s]"),
188 			   pulongest (ull),
189 			   bfd_get_filename (abfd));
190 		continue;
191 	      }
192 	  }
193 	  per_cu = per_objfile->per_bfd->get_cu (ull);
194 	  break;
195 	case DW_IDX_type_unit:
196 	  /* Don't crash on bad data.  */
197 	  if (ull >= per_objfile->per_bfd->all_type_units.size ())
198 	    {
199 	      complaint (_(".debug_names entry has bad TU index %s"
200 			   " [in module %s]"),
201 			 pulongest (ull),
202 			 bfd_get_filename (abfd));
203 	      continue;
204 	    }
205 	  {
206 	    int nr_cus = per_objfile->per_bfd->all_comp_units.size ();
207 	    per_cu = per_objfile->per_bfd->get_cu (nr_cus + ull);
208 	  }
209 	  break;
210 	case DW_IDX_die_offset:
211 	  die_offset = sect_offset (ull);
212 	  /* In a per-CU index (as opposed to a per-module index), index
213 	     entries without CU attribute implicitly refer to the single CU.  */
214 	  if (per_cu == NULL)
215 	    per_cu = per_objfile->per_bfd->get_cu (0);
216 	  break;
217 	case DW_IDX_parent:
218 	  parent = ull;
219 	  break;
220 	case DW_IDX_GNU_internal:
221 	  if (augmentation_is_gdb && ull != 0)
222 	    flags |= IS_STATIC;
223 	  break;
224 	case DW_IDX_GNU_main:
225 	  if (augmentation_is_gdb && ull != 0)
226 	    flags |= IS_MAIN;
227 	  break;
228 	case DW_IDX_GNU_language:
229 	  if (augmentation_is_gdb)
230 	    lang = dwarf_lang_to_enum_language (ull);
231 	  break;
232 	case DW_IDX_GNU_linkage_name:
233 	  if (augmentation_is_gdb && ull != 0)
234 	    flags |= IS_LINKAGE;
235 	  break;
236 	}
237     }
238 
239   /* Skip if we couldn't find a valid CU/TU index.  */
240   if (per_cu != nullptr)
241     *result = shard->add (die_offset, (dwarf_tag) indexval.dwarf_tag, flags,
242 			  lang, name, nullptr, per_cu);
243   return entry;
244 }
245 
246 /* Scan all the entries for NAME, at name slot INDEX.  */
247 
248 void
249 mapped_debug_names_reader::scan_entries (uint32_t index,
250 					 const char *name,
251 					 const gdb_byte *entry)
252 {
253   std::vector<cooked_index_entry *> these_entries;
254 
255   while (true)
256     {
257       std::optional<ULONGEST> parent;
258       cooked_index_entry *this_entry;
259       entry = scan_one_entry (name, entry, &this_entry, parent);
260 
261       if (entry == nullptr)
262 	break;
263 
264       these_entries.push_back (this_entry);
265       if (parent.has_value ())
266 	needs_parent.emplace_back (this_entry, *parent);
267     }
268 
269   all_entries[index] = std::move (these_entries);
270 }
271 
272 /* Scan the name table and create all the entries.  */
273 
274 void
275 mapped_debug_names_reader::scan_all_names ()
276 {
277   all_entries.resize (name_count);
278 
279   /* In the first pass, create all the entries.  */
280   for (uint32_t i = 0; i < name_count; ++i)
281     {
282       const ULONGEST namei_string_offs
283 	= extract_unsigned_integer ((name_table_string_offs_reordered
284 				     + i * offset_size),
285 				    offset_size, dwarf5_byte_order);
286       const char *name = read_indirect_string_at_offset (per_objfile,
287 							 namei_string_offs);
288 
289       const ULONGEST namei_entry_offs
290 	= extract_unsigned_integer ((name_table_entry_offs_reordered
291 				     + i * offset_size),
292 				    offset_size, dwarf5_byte_order);
293       const gdb_byte *entry = entry_pool + namei_entry_offs;
294 
295       scan_entries (i, name, entry);
296     }
297 
298   /* Now update the parent pointers for all entries.  This has to be
299      done in a funny way because DWARF specifies the parent entry to
300      point to a name -- but we don't know which specific one.  */
301   for (auto [entry, parent_idx] : needs_parent)
302     {
303       /* Name entries are indexed from 1 in DWARF.  */
304       std::vector<cooked_index_entry *> &entries = all_entries[parent_idx - 1];
305       for (const auto &parent : entries)
306 	if (parent->lang == entry->lang)
307 	  {
308 	    entry->set_parent (parent);
309 	    break;
310 	  }
311     }
312 }
313 
314 /* A reader for .debug_names.  */
315 
316 struct cooked_index_debug_names : public cooked_index_worker
317 {
318   cooked_index_debug_names (dwarf2_per_objfile *per_objfile,
319 			    mapped_debug_names_reader &&map)
320     : cooked_index_worker (per_objfile),
321       m_map (std::move (map))
322   { }
323 
324   void do_reading () override;
325 
326   mapped_debug_names_reader m_map;
327 };
328 
329 void
330 cooked_index_debug_names::do_reading ()
331 {
332   complaint_interceptor complaint_handler;
333   std::vector<gdb_exception> exceptions;
334   try
335     {
336       m_map.scan_all_names ();
337     }
338   catch (const gdb_exception &exc)
339     {
340       exceptions.push_back (std::move (exc));
341     }
342 
343   dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
344   per_bfd->quick_file_names_table
345     = create_quick_file_names_table (per_bfd->all_units.size ());
346   m_results.emplace_back (nullptr,
347 			  complaint_handler.release (),
348 			  std::move (exceptions),
349 			  parent_map ());
350   std::vector<std::unique_ptr<cooked_index_shard>> indexes;
351   indexes.push_back (std::move (m_map.shard));
352   cooked_index *table
353     = (gdb::checked_static_cast<cooked_index *>
354        (per_bfd->index_table.get ()));
355   /* Note that this code never uses IS_PARENT_DEFERRED, so it is safe
356      to pass nullptr here.  */
357   table->set_contents (std::move (indexes), &m_warnings, nullptr);
358 
359   bfd_thread_cleanup ();
360 }
361 
362 /* Check the signatured type hash table from .debug_names.  */
363 
364 static bool
365 check_signatured_type_table_from_debug_names
366   (dwarf2_per_objfile *per_objfile,
367    const mapped_debug_names_reader &map,
368    struct dwarf2_section_info *section)
369 {
370   struct objfile *objfile = per_objfile->objfile;
371   dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
372   int nr_cus = per_bfd->all_comp_units.size ();
373   int nr_cus_tus = per_bfd->all_units.size ();
374 
375   section->read (objfile);
376 
377   uint32_t j = nr_cus;
378   for (uint32_t i = 0; i < map.tu_count; ++i)
379     {
380       sect_offset sect_off
381 	= (sect_offset) (extract_unsigned_integer
382 			 (map.tu_table_reordered + i * map.offset_size,
383 			  map.offset_size,
384 			  map.dwarf5_byte_order));
385 
386       bool found = false;
387       for (; j < nr_cus_tus; j++)
388 	if (per_bfd->get_cu (j)->sect_off == sect_off)
389 	  {
390 	    found = true;
391 	    break;
392 	  }
393       if (!found)
394 	{
395 	  warning (_("Section .debug_names has incorrect entry in TU table,"
396 		     " ignoring .debug_names."));
397 	  return false;
398 	}
399       per_bfd->all_comp_units_index_tus.push_back (per_bfd->get_cu (j));
400     }
401   return true;
402 }
403 
404 /* DWARF-5 debug_names reader.  */
405 
406 /* The old, no-longer-supported GDB augmentation.  */
407 static const gdb_byte old_gdb_augmentation[]
408      = { 'G', 'D', 'B', 0 };
409 static_assert (sizeof (old_gdb_augmentation) % 4 == 0);
410 
411 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension.  This
412    must have a size that is a multiple of 4.  */
413 const gdb_byte dwarf5_augmentation[8] = { 'G', 'D', 'B', '2', 0, 0, 0, 0 };
414 static_assert (sizeof (dwarf5_augmentation) % 4 == 0);
415 
416 /* A helper function that reads the .debug_names section in SECTION
417    and fills in MAP.  FILENAME is the name of the file containing the
418    section; it is used for error reporting.
419 
420    Returns true if all went well, false otherwise.  */
421 
422 static bool
423 read_debug_names_from_section (dwarf2_per_objfile *per_objfile,
424 			       const char *filename,
425 			       struct dwarf2_section_info *section,
426 			       mapped_debug_names_reader &map)
427 {
428   struct objfile *objfile = per_objfile->objfile;
429 
430   if (section->empty ())
431     return false;
432 
433   /* Older elfutils strip versions could keep the section in the main
434      executable while splitting it for the separate debug info file.  */
435   if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
436     return false;
437 
438   section->read (objfile);
439 
440   map.per_objfile = per_objfile;
441   map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
442 
443   const gdb_byte *addr = section->buffer;
444 
445   bfd *abfd = section->get_bfd_owner ();
446   map.abfd = abfd;
447 
448   unsigned int bytes_read;
449   LONGEST length = read_initial_length (abfd, addr, &bytes_read);
450   addr += bytes_read;
451 
452   map.dwarf5_is_dwarf64 = bytes_read != 4;
453   map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
454   if (bytes_read + length != section->size)
455     {
456       /* There may be multiple per-CU indices.  */
457       warning (_("Section .debug_names in %s length %s does not match "
458 		 "section length %s, ignoring .debug_names."),
459 	       filename, plongest (bytes_read + length),
460 	       pulongest (section->size));
461       return false;
462     }
463 
464   /* The version number.  */
465   uint16_t version = read_2_bytes (abfd, addr);
466   addr += 2;
467   if (version != 5)
468     {
469       warning (_("Section .debug_names in %s has unsupported version %d, "
470 		 "ignoring .debug_names."),
471 	       filename, version);
472       return false;
473     }
474 
475   /* Padding.  */
476   uint16_t padding = read_2_bytes (abfd, addr);
477   addr += 2;
478   if (padding != 0)
479     {
480       warning (_("Section .debug_names in %s has unsupported padding %d, "
481 		 "ignoring .debug_names."),
482 	       filename, padding);
483       return false;
484     }
485 
486   /* comp_unit_count - The number of CUs in the CU list.  */
487   map.cu_count = read_4_bytes (abfd, addr);
488   addr += 4;
489 
490   /* local_type_unit_count - The number of TUs in the local TU
491      list.  */
492   map.tu_count = read_4_bytes (abfd, addr);
493   addr += 4;
494 
495   /* foreign_type_unit_count - The number of TUs in the foreign TU
496      list.  */
497   uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
498   addr += 4;
499   if (foreign_tu_count != 0)
500     {
501       warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
502 		 "ignoring .debug_names."),
503 	       filename, static_cast<unsigned long> (foreign_tu_count));
504       return false;
505     }
506 
507   /* bucket_count - The number of hash buckets in the hash lookup
508      table.  */
509   map.bucket_count = read_4_bytes (abfd, addr);
510   addr += 4;
511 
512   /* name_count - The number of unique names in the index.  */
513   map.name_count = read_4_bytes (abfd, addr);
514   addr += 4;
515 
516   /* abbrev_table_size - The size in bytes of the abbreviations
517      table.  */
518   uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
519   addr += 4;
520 
521   /* augmentation_string_size - The size in bytes of the augmentation
522      string.  This value is rounded up to a multiple of 4.  */
523   uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
524   addr += 4;
525   augmentation_string_size += (-augmentation_string_size) & 3;
526 
527   if (augmentation_string_size == sizeof (old_gdb_augmentation)
528       && memcmp (addr, old_gdb_augmentation,
529 		 sizeof (old_gdb_augmentation)) == 0)
530     {
531       warning (_(".debug_names created by an old version of gdb; ignoring"));
532       return false;
533     }
534 
535   map.augmentation_is_gdb = ((augmentation_string_size
536 			      == sizeof (dwarf5_augmentation))
537 			     && memcmp (addr, dwarf5_augmentation,
538 					sizeof (dwarf5_augmentation)) == 0);
539 
540   if (!map.augmentation_is_gdb)
541     {
542       warning (_(".debug_names not created by gdb; ignoring"));
543       return false;
544     }
545 
546   addr += augmentation_string_size;
547 
548   /* List of CUs */
549   map.cu_table_reordered = addr;
550   addr += map.cu_count * map.offset_size;
551 
552   /* List of Local TUs */
553   map.tu_table_reordered = addr;
554   addr += map.tu_count * map.offset_size;
555 
556   /* Hash Lookup Table */
557   map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
558   addr += map.bucket_count * 4;
559   map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
560   if (map.bucket_count != 0)
561     addr += map.name_count * 4;
562 
563   /* Name Table */
564   map.name_table_string_offs_reordered = addr;
565   addr += map.name_count * map.offset_size;
566   map.name_table_entry_offs_reordered = addr;
567   addr += map.name_count * map.offset_size;
568 
569   const gdb_byte *abbrev_table_start = addr;
570   for (;;)
571     {
572       const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
573       addr += bytes_read;
574       if (index_num == 0)
575 	break;
576 
577       const auto insertpair
578 	= map.abbrev_map.emplace (index_num, mapped_debug_names_reader::index_val ());
579       if (!insertpair.second)
580 	{
581 	  warning (_("Section .debug_names in %s has duplicate index %s, "
582 		     "ignoring .debug_names."),
583 		   filename, pulongest (index_num));
584 	  return false;
585 	}
586       mapped_debug_names_reader::index_val &indexval = insertpair.first->second;
587       indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
588       addr += bytes_read;
589 
590       for (;;)
591 	{
592 	  mapped_debug_names_reader::index_val::attr attr;
593 	  attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
594 	  addr += bytes_read;
595 	  attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
596 	  addr += bytes_read;
597 	  if (attr.form == DW_FORM_implicit_const)
598 	    {
599 	      attr.implicit_const = read_signed_leb128 (abfd, addr,
600 							&bytes_read);
601 	      addr += bytes_read;
602 	    }
603 	  if (attr.dw_idx == 0 && attr.form == 0)
604 	    break;
605 	  indexval.attr_vec.push_back (std::move (attr));
606 	}
607     }
608   if (addr != abbrev_table_start + abbrev_table_size)
609     {
610       warning (_("Section .debug_names in %s has abbreviation_table "
611 		 "of size %s vs. written as %u, ignoring .debug_names."),
612 	       filename, plongest (addr - abbrev_table_start),
613 	       abbrev_table_size);
614       return false;
615     }
616   map.entry_pool = addr;
617 
618   return true;
619 }
620 
621 /* A helper for check_cus_from_debug_names that handles the MAP's CU
622    list.  */
623 
624 static bool
625 check_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
626 				  const mapped_debug_names_reader &map,
627 				  dwarf2_section_info &section,
628 				  bool is_dwz)
629 {
630   int nr_cus = per_bfd->all_comp_units.size ();
631 
632   if (!map.augmentation_is_gdb)
633     {
634       uint32_t j = 0;
635       for (uint32_t i = 0; i < map.cu_count; ++i)
636 	{
637 	  sect_offset sect_off
638 	    = (sect_offset) (extract_unsigned_integer
639 			     (map.cu_table_reordered + i * map.offset_size,
640 			      map.offset_size,
641 			      map.dwarf5_byte_order));
642 	  bool found = false;
643 	  for (; j < nr_cus; j++)
644 	    if (per_bfd->get_cu (j)->sect_off == sect_off)
645 	      {
646 		found = true;
647 		break;
648 	      }
649 	  if (!found)
650 	    {
651 	      warning (_("Section .debug_names has incorrect entry in CU table,"
652 			 " ignoring .debug_names."));
653 	      return false;
654 	    }
655 	  per_bfd->all_comp_units_index_cus.push_back (per_bfd->get_cu (j));
656 	}
657       return true;
658     }
659 
660   if (map.cu_count != nr_cus)
661     {
662       warning (_("Section .debug_names has incorrect number of CUs in CU table,"
663 		 " ignoring .debug_names."));
664       return false;
665     }
666 
667   for (uint32_t i = 0; i < map.cu_count; ++i)
668     {
669       sect_offset sect_off
670 	= (sect_offset) (extract_unsigned_integer
671 			 (map.cu_table_reordered + i * map.offset_size,
672 			  map.offset_size,
673 			  map.dwarf5_byte_order));
674       if (sect_off != per_bfd->get_cu (i)->sect_off)
675 	{
676 	  warning (_("Section .debug_names has incorrect entry in CU table,"
677 		     " ignoring .debug_names."));
678 	  return false;
679 	}
680     }
681 
682   return true;
683 }
684 
685 /* Read the CU list from the mapped index, and use it to create all
686    the CU objects for this dwarf2_per_objfile.  */
687 
688 static bool
689 check_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
690 			     const mapped_debug_names_reader &map,
691 			     const mapped_debug_names_reader &dwz_map)
692 {
693   if (!check_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
694 					false /* is_dwz */))
695     return false;
696 
697   if (dwz_map.cu_count == 0)
698     return true;
699 
700   dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
701   return check_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
702 					  true /* is_dwz */);
703 }
704 
705 /* This does all the work for dwarf2_read_debug_names, but putting it
706    into a separate function makes some cleanup a bit simpler.  */
707 
708 static bool
709 do_dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
710 {
711   mapped_debug_names_reader map;
712   mapped_debug_names_reader dwz_map;
713   struct objfile *objfile = per_objfile->objfile;
714   dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
715 
716   if (!read_debug_names_from_section (per_objfile, objfile_name (objfile),
717 				      &per_bfd->debug_names, map))
718     return false;
719 
720   /* Don't use the index if it's empty.  */
721   if (map.name_count == 0)
722     return false;
723 
724   /* If there is a .dwz file, read it so we can get its CU list as
725      well.  */
726   dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
727   if (dwz != NULL)
728     {
729       if (!read_debug_names_from_section (per_objfile,
730 					  bfd_get_filename (dwz->dwz_bfd.get ()),
731 					  &dwz->debug_names, dwz_map))
732 	{
733 	  warning (_("could not read '.debug_names' section from %s; skipping"),
734 		   bfd_get_filename (dwz->dwz_bfd.get ()));
735 	  return false;
736 	}
737     }
738 
739   create_all_units (per_objfile);
740   if (!check_cus_from_debug_names (per_bfd, map, dwz_map))
741     return false;
742 
743   if (map.tu_count != 0)
744     {
745       /* We can only handle a single .debug_types when we have an
746 	 index.  */
747       if (per_bfd->types.size () > 1)
748 	return false;
749 
750       dwarf2_section_info *section
751 	= (per_bfd->types.size () == 1
752 	   ? &per_bfd->types[0]
753 	   : &per_bfd->info);
754 
755       if (!check_signatured_type_table_from_debug_names (per_objfile,
756 							 map, section))
757 	return false;
758     }
759 
760   per_bfd->debug_aranges.read (per_objfile->objfile);
761   addrmap_mutable addrmap;
762   deferred_warnings warnings;
763   read_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges,
764 			     &addrmap, &warnings);
765   warnings.emit ();
766 
767   map.shard = std::make_unique<cooked_index_shard> ();
768   map.shard->install_addrmap (&addrmap);
769 
770   cooked_index *idx
771     = new debug_names_index (per_objfile,
772 			     (std::make_unique<cooked_index_debug_names>
773 			      (per_objfile, std::move (map))));
774   per_bfd->index_table.reset (idx);
775 
776   idx->start_reading ();
777 
778   return true;
779 }
780 
781 /* See read-debug-names.h.  */
782 
783 bool
784 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
785 {
786   bool result = do_dwarf2_read_debug_names (per_objfile);
787   if (!result)
788     per_objfile->per_bfd->all_units.clear ();
789   return result;
790 }
791