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