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