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