Commit | Line | Data |
---|---|---|
cd4fb1b2 SM |
1 | /* DWARF 2 debugging format support for GDB. |
2 | ||
b811d2c2 | 3 | Copyright (C) 1994-2020 Free Software Foundation, Inc. |
cd4fb1b2 SM |
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 | #ifndef DWARF2READ_H | |
21 | #define DWARF2READ_H | |
22 | ||
e4a62c65 | 23 | #include <unordered_map> |
87d6a7aa | 24 | #include "dwarf-index-cache.h" |
2c86cff9 | 25 | #include "dwarf2/section.h" |
cd4fb1b2 SM |
26 | #include "filename-seen-cache.h" |
27 | #include "gdb_obstack.h" | |
268a13a5 | 28 | #include "gdbsupport/hash_enum.h" |
891813be | 29 | #include "psympriv.h" |
cd4fb1b2 | 30 | |
3c3bb058 AB |
31 | /* Hold 'maintenance (set|show) dwarf' commands. */ |
32 | extern struct cmd_list_element *set_dwarf_cmdlist; | |
33 | extern struct cmd_list_element *show_dwarf_cmdlist; | |
34 | ||
e86f08d2 CB |
35 | extern bool dwarf_always_disassemble; |
36 | ||
cd4fb1b2 SM |
37 | struct tu_stats |
38 | { | |
39 | int nr_uniq_abbrev_tables; | |
40 | int nr_symtabs; | |
41 | int nr_symtab_sharers; | |
42 | int nr_stmt_less_type_units; | |
43 | int nr_all_type_units_reallocs; | |
44 | }; | |
45 | ||
46 | struct dwarf2_debug_sections; | |
47 | struct mapped_index; | |
48 | struct mapped_debug_names; | |
ff4c9fec | 49 | struct signatured_type; |
e4a62c65 | 50 | struct die_info; |
cd4fb1b2 SM |
51 | |
52 | /* Collection of data recorded per objfile. | |
53 | This hangs off of dwarf2_objfile_data_key. */ | |
54 | ||
5bfd760d | 55 | struct dwarf2_per_objfile |
cd4fb1b2 SM |
56 | { |
57 | /* Construct a dwarf2_per_objfile for OBJFILE. NAMES points to the | |
58 | dwarf2 section names, or is NULL if the standard ELF names are | |
4b610737 TT |
59 | used. CAN_COPY is true for formats where symbol |
60 | interposition is possible and so symbol values must follow copy | |
61 | relocation rules. */ | |
cd4fb1b2 | 62 | dwarf2_per_objfile (struct objfile *objfile, |
4b610737 TT |
63 | const dwarf2_debug_sections *names, |
64 | bool can_copy); | |
cd4fb1b2 SM |
65 | |
66 | ~dwarf2_per_objfile (); | |
67 | ||
68 | DISABLE_COPY_AND_ASSIGN (dwarf2_per_objfile); | |
69 | ||
ff4c9fec SM |
70 | /* Return the CU/TU given its index. |
71 | ||
72 | This is intended for loops like: | |
73 | ||
74 | for (i = 0; i < (dwarf2_per_objfile->n_comp_units | |
75 | + dwarf2_per_objfile->n_type_units); ++i) | |
76 | { | |
77 | dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i); | |
78 | ||
79 | ...; | |
80 | } | |
81 | */ | |
82 | dwarf2_per_cu_data *get_cutu (int index); | |
83 | ||
84 | /* Return the CU given its index. | |
85 | This differs from get_cutu in that it's for when you know INDEX refers to a | |
86 | CU. */ | |
87 | dwarf2_per_cu_data *get_cu (int index); | |
88 | ||
89 | /* Return the TU given its index. | |
90 | This differs from get_cutu in that it's for when you know INDEX refers to a | |
91 | TU. */ | |
92 | signatured_type *get_tu (int index); | |
93 | ||
cd4fb1b2 SM |
94 | /* Free all cached compilation units. */ |
95 | void free_cached_comp_units (); | |
96 | private: | |
97 | /* This function is mapped across the sections and remembers the | |
98 | offset and size of each of the debugging sections we are | |
99 | interested in. */ | |
100 | void locate_sections (bfd *abfd, asection *sectp, | |
101 | const dwarf2_debug_sections &names); | |
102 | ||
103 | public: | |
104 | dwarf2_section_info info {}; | |
105 | dwarf2_section_info abbrev {}; | |
106 | dwarf2_section_info line {}; | |
107 | dwarf2_section_info loc {}; | |
108 | dwarf2_section_info loclists {}; | |
109 | dwarf2_section_info macinfo {}; | |
110 | dwarf2_section_info macro {}; | |
111 | dwarf2_section_info str {}; | |
18a8505e | 112 | dwarf2_section_info str_offsets {}; |
cd4fb1b2 SM |
113 | dwarf2_section_info line_str {}; |
114 | dwarf2_section_info ranges {}; | |
115 | dwarf2_section_info rnglists {}; | |
116 | dwarf2_section_info addr {}; | |
117 | dwarf2_section_info frame {}; | |
118 | dwarf2_section_info eh_frame {}; | |
119 | dwarf2_section_info gdb_index {}; | |
120 | dwarf2_section_info debug_names {}; | |
121 | dwarf2_section_info debug_aranges {}; | |
122 | ||
fd5866f6 | 123 | std::vector<dwarf2_section_info> types; |
cd4fb1b2 SM |
124 | |
125 | /* Back link. */ | |
126 | struct objfile *objfile = NULL; | |
127 | ||
128 | /* Table of all the compilation units. This is used to locate | |
129 | the target compilation unit of a particular reference. */ | |
b76e467d | 130 | std::vector<dwarf2_per_cu_data *> all_comp_units; |
cd4fb1b2 | 131 | |
b2bdb8cf SM |
132 | /* The .debug_types-related CUs (TUs). */ |
133 | std::vector<signatured_type *> all_type_units; | |
cd4fb1b2 SM |
134 | |
135 | /* Table of struct type_unit_group objects. | |
136 | The hash key is the DW_AT_stmt_list value. */ | |
137 | htab_t type_unit_groups {}; | |
138 | ||
139 | /* A table mapping .debug_types signatures to its signatured_type entry. | |
140 | This is NULL if the .debug_types section hasn't been read in yet. */ | |
141 | htab_t signatured_types {}; | |
142 | ||
143 | /* Type unit statistics, to see how well the scaling improvements | |
144 | are doing. */ | |
145 | struct tu_stats tu_stats {}; | |
146 | ||
147 | /* A chain of compilation units that are currently read in, so that | |
148 | they can be freed later. */ | |
149 | dwarf2_per_cu_data *read_in_chain = NULL; | |
150 | ||
151 | /* A table mapping DW_AT_dwo_name values to struct dwo_file objects. | |
152 | This is NULL if the table hasn't been allocated yet. */ | |
51ac9db5 | 153 | htab_up dwo_files; |
cd4fb1b2 SM |
154 | |
155 | /* True if we've checked for whether there is a DWP file. */ | |
156 | bool dwp_checked = false; | |
157 | ||
158 | /* The DWP file if there is one, or NULL. */ | |
400174b1 | 159 | std::unique_ptr<struct dwp_file> dwp_file; |
cd4fb1b2 SM |
160 | |
161 | /* The shared '.dwz' file, if one exists. This is used when the | |
162 | original data was compressed using 'dwz -m'. */ | |
7ff8cb8c | 163 | std::unique_ptr<struct dwz_file> dwz_file; |
cd4fb1b2 | 164 | |
4b610737 TT |
165 | /* Whether copy relocations are supported by this object format. */ |
166 | bool can_copy; | |
167 | ||
cd4fb1b2 SM |
168 | /* A flag indicating whether this objfile has a section loaded at a |
169 | VMA of 0. */ | |
170 | bool has_section_at_zero = false; | |
171 | ||
172 | /* True if we are using the mapped index, | |
173 | or we are faking it for OBJF_READNOW's sake. */ | |
174 | bool using_index = false; | |
175 | ||
176 | /* The mapped index, or NULL if .gdb_index is missing or not being used. */ | |
3063847f | 177 | std::unique_ptr<mapped_index> index_table; |
cd4fb1b2 SM |
178 | |
179 | /* The mapped index, or NULL if .debug_names is missing or not being used. */ | |
180 | std::unique_ptr<mapped_debug_names> debug_names_table; | |
181 | ||
182 | /* When using index_table, this keeps track of all quick_file_names entries. | |
183 | TUs typically share line table entries with a CU, so we maintain a | |
184 | separate table of all line table entries to support the sharing. | |
185 | Note that while there can be way more TUs than CUs, we've already | |
186 | sorted all the TUs into "type unit groups", grouped by their | |
187 | DW_AT_stmt_list value. Therefore the only sharing done here is with a | |
188 | CU and its associated TU group if there is one. */ | |
189 | htab_t quick_file_names_table {}; | |
190 | ||
191 | /* Set during partial symbol reading, to prevent queueing of full | |
192 | symbols. */ | |
193 | bool reading_partial_symbols = false; | |
194 | ||
195 | /* Table mapping type DIEs to their struct type *. | |
196 | This is NULL if not allocated yet. | |
197 | The mapping is done via (CU/TU + DIE offset) -> type. */ | |
198 | htab_t die_type_hash {}; | |
199 | ||
200 | /* The CUs we recently read. */ | |
c5d0225d | 201 | std::vector<dwarf2_per_cu_data *> just_read_cus; |
cd4fb1b2 SM |
202 | |
203 | /* Table containing line_header indexed by offset and offset_in_dwz. */ | |
204 | htab_t line_header_hash {}; | |
205 | ||
206 | /* Table containing all filenames. This is an optional because the | |
207 | table is lazily constructed on first access. */ | |
208 | gdb::optional<filename_seen_cache> filenames_cache; | |
87d6a7aa SM |
209 | |
210 | /* If we loaded the index from an external file, this contains the | |
211 | resources associated to the open file, memory mapping, etc. */ | |
212 | std::unique_ptr<index_cache_resource> index_cache_res; | |
e4a62c65 TV |
213 | |
214 | /* Mapping from abstract origin DIE to concrete DIEs that reference it as | |
215 | DW_AT_abstract_origin. */ | |
fad03f6e TT |
216 | std::unordered_map<sect_offset, std::vector<sect_offset>, |
217 | gdb::hash_enum<sect_offset>> | |
e4a62c65 | 218 | abstract_to_concrete; |
cd4fb1b2 SM |
219 | }; |
220 | ||
221 | /* Get the dwarf2_per_objfile associated to OBJFILE. */ | |
222 | ||
223 | dwarf2_per_objfile *get_dwarf2_per_objfile (struct objfile *objfile); | |
224 | ||
891813be | 225 | /* A partial symtab specialized for DWARF. */ |
128a391f | 226 | struct dwarf2_psymtab : public standard_psymtab |
891813be TT |
227 | { |
228 | dwarf2_psymtab (const char *filename, struct objfile *objfile) | |
128a391f | 229 | : standard_psymtab (filename, objfile) |
891813be TT |
230 | { |
231 | } | |
232 | ||
233 | dwarf2_psymtab (const char *filename, struct objfile *objfile, | |
234 | CORE_ADDR addr) | |
128a391f | 235 | : standard_psymtab (filename, objfile, addr) |
891813be TT |
236 | { |
237 | } | |
238 | ||
239 | void read_symtab (struct objfile *) override; | |
8566b89b | 240 | void expand_psymtab (struct objfile *) override; |
891813be TT |
241 | |
242 | struct dwarf2_per_cu_data *per_cu_data; | |
243 | }; | |
244 | ||
cd4fb1b2 SM |
245 | /* Persistent data held for a compilation unit, even when not |
246 | processing it. We put a pointer to this structure in the | |
891813be | 247 | psymtab. */ |
cd4fb1b2 SM |
248 | |
249 | struct dwarf2_per_cu_data | |
250 | { | |
251 | /* The start offset and length of this compilation unit. | |
252 | NOTE: Unlike comp_unit_head.length, this length includes | |
253 | initial_length_size. | |
254 | If the DIE refers to a DWO file, this is always of the original die, | |
255 | not the DWO file. */ | |
256 | sect_offset sect_off; | |
257 | unsigned int length; | |
258 | ||
259 | /* DWARF standard version this data has been read from (such as 4 or 5). */ | |
260 | short dwarf_version; | |
261 | ||
262 | /* Flag indicating this compilation unit will be read in before | |
263 | any of the current compilation units are processed. */ | |
264 | unsigned int queued : 1; | |
265 | ||
266 | /* This flag will be set when reading partial DIEs if we need to load | |
267 | absolutely all DIEs for this compilation unit, instead of just the ones | |
268 | we think are interesting. It gets set if we look for a DIE in the | |
269 | hash table and don't find it. */ | |
270 | unsigned int load_all_dies : 1; | |
271 | ||
272 | /* Non-zero if this CU is from .debug_types. | |
273 | Struct dwarf2_per_cu_data is contained in struct signatured_type iff | |
274 | this is non-zero. */ | |
275 | unsigned int is_debug_types : 1; | |
276 | ||
277 | /* Non-zero if this CU is from the .dwz file. */ | |
278 | unsigned int is_dwz : 1; | |
279 | ||
280 | /* Non-zero if reading a TU directly from a DWO file, bypassing the stub. | |
281 | This flag is only valid if is_debug_types is true. | |
282 | We can't read a CU directly from a DWO file: There are required | |
283 | attributes in the stub. */ | |
284 | unsigned int reading_dwo_directly : 1; | |
285 | ||
286 | /* Non-zero if the TU has been read. | |
287 | This is used to assist the "Stay in DWO Optimization" for Fission: | |
288 | When reading a DWO, it's faster to read TUs from the DWO instead of | |
289 | fetching them from random other DWOs (due to comdat folding). | |
290 | If the TU has already been read, the optimization is unnecessary | |
291 | (and unwise - we don't want to change where gdb thinks the TU lives | |
292 | "midflight"). | |
293 | This flag is only valid if is_debug_types is true. */ | |
294 | unsigned int tu_read : 1; | |
295 | ||
296 | /* The section this CU/TU lives in. | |
297 | If the DIE refers to a DWO file, this is always the original die, | |
298 | not the DWO file. */ | |
299 | struct dwarf2_section_info *section; | |
300 | ||
301 | /* Set to non-NULL iff this CU is currently loaded. When it gets freed out | |
302 | of the CU cache it gets reset to NULL again. This is left as NULL for | |
303 | dummy CUs (a CU header, but nothing else). */ | |
304 | struct dwarf2_cu *cu; | |
305 | ||
306 | /* The corresponding dwarf2_per_objfile. */ | |
307 | struct dwarf2_per_objfile *dwarf2_per_objfile; | |
308 | ||
309 | /* When dwarf2_per_objfile->using_index is true, the 'quick' field | |
310 | is active. Otherwise, the 'psymtab' field is active. */ | |
311 | union | |
312 | { | |
313 | /* The partial symbol table associated with this compilation unit, | |
314 | or NULL for unread partial units. */ | |
891813be | 315 | dwarf2_psymtab *psymtab; |
cd4fb1b2 SM |
316 | |
317 | /* Data needed by the "quick" functions. */ | |
318 | struct dwarf2_per_cu_quick_data *quick; | |
319 | } v; | |
320 | ||
ae640021 AB |
321 | /* Return true of IMPORTED_SYMTABS is empty or not yet allocated. */ |
322 | bool imported_symtabs_empty () const | |
323 | { | |
324 | return (imported_symtabs == nullptr || imported_symtabs->empty ()); | |
325 | } | |
326 | ||
327 | /* Push P to the back of IMPORTED_SYMTABS, allocated IMPORTED_SYMTABS | |
328 | first if required. */ | |
329 | void imported_symtabs_push (dwarf2_per_cu_data *p) | |
330 | { | |
331 | if (imported_symtabs == nullptr) | |
332 | imported_symtabs = new std::vector <dwarf2_per_cu_data *>; | |
333 | imported_symtabs->push_back (p); | |
334 | } | |
335 | ||
336 | /* Return the size of IMPORTED_SYMTABS if it is allocated, otherwise | |
337 | return 0. */ | |
338 | size_t imported_symtabs_size () const | |
339 | { | |
340 | if (imported_symtabs == nullptr) | |
341 | return 0; | |
342 | return imported_symtabs->size (); | |
343 | } | |
344 | ||
345 | /* Delete IMPORTED_SYMTABS and set the pointer back to nullptr. */ | |
346 | void imported_symtabs_free () | |
347 | { | |
348 | delete imported_symtabs; | |
349 | imported_symtabs = nullptr; | |
350 | } | |
351 | ||
cd4fb1b2 SM |
352 | /* The CUs we import using DW_TAG_imported_unit. This is filled in |
353 | while reading psymtabs, used to compute the psymtab dependencies, | |
354 | and then cleared. Then it is filled in again while reading full | |
355 | symbols, and only deleted when the objfile is destroyed. | |
356 | ||
357 | This is also used to work around a difference between the way gold | |
358 | generates .gdb_index version <=7 and the way gdb does. Arguably this | |
359 | is a gold bug. For symbols coming from TUs, gold records in the index | |
360 | the CU that includes the TU instead of the TU itself. This breaks | |
361 | dw2_lookup_symbol: It assumes that if the index says symbol X lives | |
362 | in CU/TU Y, then one need only expand Y and a subsequent lookup in Y | |
363 | will find X. Alas TUs live in their own symtab, so after expanding CU Y | |
364 | we need to look in TU Z to find X. Fortunately, this is akin to | |
365 | DW_TAG_imported_unit, so we just use the same mechanism: For | |
366 | .gdb_index version <=7 this also records the TUs that the CU referred | |
367 | to. Concurrently with this change gdb was modified to emit version 8 | |
368 | indices so we only pay a price for gold generated indices. | |
ae640021 AB |
369 | http://sourceware.org/bugzilla/show_bug.cgi?id=15021. |
370 | ||
371 | This currently needs to be a public member due to how | |
372 | dwarf2_per_cu_data is allocated and used. Ideally in future things | |
373 | could be refactored to make this private. Until then please try to | |
374 | avoid direct access to this member, and instead use the helper | |
375 | functions above. */ | |
376 | std::vector <dwarf2_per_cu_data *> *imported_symtabs; | |
cd4fb1b2 SM |
377 | }; |
378 | ||
379 | /* Entry in the signatured_types hash table. */ | |
380 | ||
381 | struct signatured_type | |
382 | { | |
383 | /* The "per_cu" object of this type. | |
384 | This struct is used iff per_cu.is_debug_types. | |
385 | N.B.: This is the first member so that it's easy to convert pointers | |
386 | between them. */ | |
387 | struct dwarf2_per_cu_data per_cu; | |
388 | ||
389 | /* The type's signature. */ | |
390 | ULONGEST signature; | |
391 | ||
392 | /* Offset in the TU of the type's DIE, as read from the TU header. | |
393 | If this TU is a DWO stub and the definition lives in a DWO file | |
394 | (specified by DW_AT_GNU_dwo_name), this value is unusable. */ | |
395 | cu_offset type_offset_in_tu; | |
396 | ||
397 | /* Offset in the section of the type's DIE. | |
398 | If the definition lives in a DWO file, this is the offset in the | |
399 | .debug_types.dwo section. | |
400 | The value is zero until the actual value is known. | |
401 | Zero is otherwise not a valid section offset. */ | |
402 | sect_offset type_offset_in_section; | |
403 | ||
404 | /* Type units are grouped by their DW_AT_stmt_list entry so that they | |
405 | can share them. This points to the containing symtab. */ | |
406 | struct type_unit_group *type_unit_group; | |
407 | ||
408 | /* The type. | |
409 | The first time we encounter this type we fully read it in and install it | |
410 | in the symbol tables. Subsequent times we only need the type. */ | |
411 | struct type *type; | |
412 | ||
413 | /* Containing DWO unit. | |
414 | This field is valid iff per_cu.reading_dwo_directly. */ | |
415 | struct dwo_unit *dwo_unit; | |
416 | }; | |
417 | ||
c4973306 SM |
418 | /* This represents a '.dwz' file. */ |
419 | ||
420 | struct dwz_file | |
421 | { | |
422 | dwz_file (gdb_bfd_ref_ptr &&bfd) | |
423 | : dwz_bfd (std::move (bfd)) | |
424 | { | |
425 | } | |
426 | ||
427 | const char *filename () const | |
428 | { | |
00f93c44 | 429 | return bfd_get_filename (this->dwz_bfd.get ()); |
c4973306 SM |
430 | } |
431 | ||
432 | /* A dwz file can only contain a few sections. */ | |
433 | struct dwarf2_section_info abbrev {}; | |
434 | struct dwarf2_section_info info {}; | |
435 | struct dwarf2_section_info str {}; | |
436 | struct dwarf2_section_info line {}; | |
437 | struct dwarf2_section_info macro {}; | |
438 | struct dwarf2_section_info gdb_index {}; | |
439 | struct dwarf2_section_info debug_names {}; | |
440 | ||
441 | /* The dwz's BFD. */ | |
442 | gdb_bfd_ref_ptr dwz_bfd; | |
443 | ||
444 | /* If we loaded the index from an external file, this contains the | |
445 | resources associated to the open file, memory mapping, etc. */ | |
446 | std::unique_ptr<index_cache_resource> index_cache_res; | |
447 | }; | |
448 | ||
449 | /* Open the separate '.dwz' debug file, if needed. Return NULL if | |
450 | there is no .gnu_debugaltlink section in the file. Error if there | |
451 | is such a section but the file cannot be found. */ | |
452 | ||
453 | extern struct dwz_file *dwarf2_get_dwz_file | |
454 | (struct dwarf2_per_objfile *dwarf2_per_objfile); | |
455 | ||
cd4fb1b2 | 456 | #endif /* DWARF2READ_H */ |