Home | History | Annotate | Line # | Download | only in dwarf2
      1 /* DWARF CU data structure
      2 
      3    Copyright (C) 2021-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 "dwarf2/cu.h"
     21 #include "dwarf2/read.h"
     22 #include "objfiles.h"
     23 #include "filenames.h"
     24 #include "gdbsupport/pathstuff.h"
     25 
     26 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE.  */
     27 
     28 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
     29 		      dwarf2_per_objfile *per_objfile)
     30   : per_cu (per_cu),
     31     per_objfile (per_objfile),
     32     m_mark (false),
     33     has_loclist (false),
     34     checked_producer (false),
     35     producer_is_gxx_lt_4_6 (false),
     36     producer_is_gcc_lt_4_3 (false),
     37     producer_is_gcc_11 (false),
     38     producer_is_icc (false),
     39     producer_is_icc_lt_14 (false),
     40     producer_is_codewarrior (false),
     41     producer_is_clang (false),
     42     producer_is_gas_lt_2_38 (false),
     43     producer_is_gas_2_39 (false),
     44     producer_is_gas_ge_2_40 (false),
     45     processing_has_namespace_info (false)
     46 {
     47 }
     48 
     49 /* See cu.h.  */
     50 
     51 struct type *
     52 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
     53 {
     54   int addr_size = this->per_cu->addr_size ();
     55   return objfile_int_type (this->per_objfile->objfile, addr_size, unsigned_p);
     56 }
     57 
     58 /* Start a symtab for DWARF.  NAME, COMP_DIR, LOW_PC are passed to the
     59    buildsym_compunit constructor.  */
     60 
     61 struct compunit_symtab *
     62 dwarf2_cu::start_compunit_symtab (const char *name, const char *comp_dir,
     63 				  CORE_ADDR low_pc)
     64 {
     65   gdb_assert (m_builder == nullptr);
     66 
     67   std::string name_for_id_holder;
     68   const char *name_for_id = name;
     69 
     70   /* Prepend the compilation directory to the filename if needed (if not
     71      absolute already) to get the "name for id" for our main symtab.  The name
     72      for the main file coming from the line table header will be generated using
     73      the same logic, so will hopefully match what we pass here.  */
     74   if (!IS_ABSOLUTE_PATH (name) && comp_dir != nullptr)
     75     {
     76       name_for_id_holder = path_join (comp_dir, name);
     77       name_for_id = name_for_id_holder.c_str ();
     78     }
     79 
     80   m_builder.reset (new struct buildsym_compunit
     81 		   (this->per_objfile->objfile,
     82 		    name, comp_dir, name_for_id, lang (), low_pc));
     83 
     84   list_in_scope = get_builder ()->get_file_symbols ();
     85 
     86   /* DWARF versions are restricted to [2, 5], thanks to the check in
     87      read_comp_unit_head.  */
     88   gdb_assert (this->header.version >= 2 && this->header.version <= 5);
     89   static const char *debugformat_strings[] = {
     90     "DWARF 2",
     91     "DWARF 3",
     92     "DWARF 4",
     93     "DWARF 5",
     94   };
     95   const char *debugformat = debugformat_strings[this->header.version - 2];
     96 
     97   get_builder ()->record_debugformat (debugformat);
     98   get_builder ()->record_producer (producer);
     99 
    100   processing_has_namespace_info = false;
    101 
    102   return get_builder ()->get_compunit_symtab ();
    103 }
    104 
    105 /* See read.h.  */
    106 
    107 struct type *
    108 dwarf2_cu::addr_type () const
    109 {
    110   struct objfile *objfile = this->per_objfile->objfile;
    111   struct type *void_type = builtin_type (objfile)->builtin_void;
    112   struct type *addr_type = lookup_pointer_type (void_type);
    113   int addr_size = this->per_cu->addr_size ();
    114 
    115   if (addr_type->length () == addr_size)
    116     return addr_type;
    117 
    118   addr_type = addr_sized_int_type (addr_type->is_unsigned ());
    119   return addr_type;
    120 }
    121 
    122 /* A hashtab traversal function that marks the dependent CUs.  */
    123 
    124 static int
    125 dwarf2_mark_helper (void **slot, void *data)
    126 {
    127   dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
    128   dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
    129   dwarf2_cu *cu = per_objfile->get_cu (per_cu);
    130 
    131   /* cu->m_dependencies references may not yet have been ever read if
    132      QUIT aborts reading of the chain.  As such dependencies remain
    133      valid it is not much useful to track and undo them during QUIT
    134      cleanups.  */
    135   if (cu != nullptr)
    136     cu->mark ();
    137   return 1;
    138 }
    139 
    140 /* See dwarf2/cu.h.  */
    141 
    142 void
    143 dwarf2_cu::mark ()
    144 {
    145   if (!m_mark)
    146     {
    147       m_mark = true;
    148       if (m_dependencies != nullptr)
    149 	htab_traverse (m_dependencies, dwarf2_mark_helper, per_objfile);
    150     }
    151 }
    152 
    153 /* See dwarf2/cu.h.  */
    154 
    155 void
    156 dwarf2_cu::add_dependence (struct dwarf2_per_cu_data *ref_per_cu)
    157 {
    158   void **slot;
    159 
    160   if (m_dependencies == nullptr)
    161     m_dependencies
    162       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
    163 			      NULL, &comp_unit_obstack,
    164 			      hashtab_obstack_allocate,
    165 			      dummy_obstack_deallocate);
    166 
    167   slot = htab_find_slot (m_dependencies, ref_per_cu, INSERT);
    168   if (*slot == nullptr)
    169     *slot = ref_per_cu;
    170 }
    171 
    172 /* See dwarf2/cu.h.  */
    173 
    174 buildsym_compunit *
    175 dwarf2_cu::get_builder ()
    176 {
    177   /* If this CU has a builder associated with it, use that.  */
    178   if (m_builder != nullptr)
    179     return m_builder.get ();
    180 
    181   if (per_objfile->sym_cu != nullptr)
    182     return per_objfile->sym_cu->m_builder.get ();
    183 
    184   gdb_assert_not_reached ("");
    185 }
    186