xref: /netbsd-src/external/gpl3/binutils.old/dist/binutils/dwarf.h (revision e992f068c547fd6e84b3f104dc2340adcc955732)
1 /* dwarf.h - DWARF support header file
2    Copyright (C) 2005-2022 Free Software Foundation, Inc.
3 
4    This file is part of GNU Binutils.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20 
21 #include "dwarf2.h" /* for enum dwarf_unit_type */
22 
23 typedef unsigned HOST_WIDEST_INT  dwarf_vma;
24 typedef HOST_WIDEST_INT           dwarf_signed_vma;
25 typedef unsigned HOST_WIDEST_INT  dwarf_size_type;
26 
27 /* Structure found in the .debug_line section.  */
28 typedef struct
29 {
30   dwarf_vma	 li_length;
31   unsigned short li_version;
32   unsigned char  li_address_size;
33   unsigned char  li_segment_size;
34   dwarf_vma      li_prologue_length;
35   unsigned char  li_min_insn_length;
36   unsigned char  li_max_ops_per_insn;
37   unsigned char  li_default_is_stmt;
38   int            li_line_base;
39   unsigned char  li_line_range;
40   unsigned char  li_opcode_base;
41   unsigned int   li_offset_size;
42 }
43 DWARF2_Internal_LineInfo;
44 
45 /* Structure found in .debug_pubnames section.  */
46 typedef struct
47 {
48   dwarf_vma	 pn_length;
49   unsigned short pn_version;
50   dwarf_vma	 pn_offset;
51   dwarf_vma	 pn_size;
52 }
53 DWARF2_Internal_PubNames;
54 
55 /* Structure found in .debug_info section.  */
56 typedef struct
57 {
58   dwarf_vma	 cu_length;
59   unsigned short cu_version;
60   dwarf_vma	 cu_abbrev_offset;
61   unsigned char  cu_pointer_size;
62   enum dwarf_unit_type cu_unit_type;
63 }
64 DWARF2_Internal_CompUnit;
65 
66 /* Structure found in .debug_aranges section.  */
67 typedef struct
68 {
69   dwarf_vma	 ar_length;
70   unsigned short ar_version;
71   dwarf_vma	 ar_info_offset;
72   unsigned char  ar_pointer_size;
73   unsigned char  ar_segment_size;
74 }
75 DWARF2_Internal_ARange;
76 
77 /* N.B. The order here must match the order in debug_displays.  */
78 
79 enum dwarf_section_display_enum
80 {
81   abbrev = 0,
82   aranges,
83   frame,
84   info,
85   line,
86   pubnames,
87   gnu_pubnames,
88   eh_frame,
89   macinfo,
90   macro,
91   str,
92   line_str,
93   loc,
94   loclists,
95   loclists_dwo,
96   pubtypes,
97   gnu_pubtypes,
98   ranges,
99   rnglists,
100   rnglists_dwo,
101   static_func,
102   static_vars,
103   types,
104   weaknames,
105   gdb_index,
106   debug_names,
107   trace_info,
108   trace_abbrev,
109   trace_aranges,
110   info_dwo,
111   abbrev_dwo,
112   types_dwo,
113   line_dwo,
114   loc_dwo,
115   macro_dwo,
116   macinfo_dwo,
117   str_dwo,
118   str_index,
119   str_index_dwo,
120   debug_addr,
121   dwp_cu_index,
122   dwp_tu_index,
123   gnu_debuglink,
124   gnu_debugaltlink,
125   debug_sup,
126   separate_debug_str,
127   note_gnu_build_id,
128   max
129 };
130 
131 struct dwarf_section
132 {
133   /* A debug section has a different name when it's stored compressed
134      or not.  XCOFF DWARF section also have a special name.
135      COMPRESSED_NAME, UNCOMPRESSED_NAME and XCOFF_NAME are the three
136      possibilities.  NAME is set to whichever one is used for this
137      input file, as determined by load_debug_section().  */
138   const char *                     uncompressed_name;
139   const char *                     compressed_name;
140   const char *                     xcoff_name;
141   const char *                     name;
142   /* If non-NULL then FILENAME is the name of the separate debug info
143      file containing the section.  */
144   const char *                     filename;
145   unsigned char *                  start;
146   dwarf_vma                        address;
147   dwarf_size_type                  size;
148   enum dwarf_section_display_enum  abbrev_sec;
149   /* Used by clients to help them implement the reloc_at callback.  */
150   void *                           reloc_info;
151   unsigned long                    num_relocs;
152 };
153 
154 /* A structure containing the name of a debug section
155    and a pointer to a function that can decode it.  */
156 struct dwarf_section_display
157 {
158   struct dwarf_section section;
159   int (*display) (struct dwarf_section *, void *);
160   int *enabled;
161   bool relocate;
162 };
163 
164 extern struct dwarf_section_display debug_displays [];
165 
166 /* This structure records the information that
167    we extract from the.debug_info section.  */
168 typedef struct
169 {
170   unsigned int   pointer_size;
171   unsigned int   offset_size;
172   int            dwarf_version;
173   dwarf_vma	 cu_offset;
174   dwarf_vma	 base_address;
175   /* This field is filled in when reading the attribute DW_AT_GNU_addr_base and
176      is used with the form DW_FORM_GNU_addr_index.  */
177   dwarf_vma	 addr_base;
178   /* This field is filled in when reading the attribute DW_AT_GNU_ranges_base and
179      is used when calculating ranges.  */
180   dwarf_vma	 ranges_base;
181   /* This is an array of offsets to the location list table.  */
182   dwarf_vma *    loc_offsets;
183   /* This is an array of offsets to the location view table.  */
184   dwarf_vma *    loc_views;
185   int *          have_frame_base;
186 
187   /* Information for associating location lists with CUs.  */
188   unsigned int   num_loc_offsets;
189   unsigned int   max_loc_offsets;
190   unsigned int   num_loc_views;
191   dwarf_vma      loclists_base;
192 
193   /* List of .debug_ranges offsets seen in this .debug_info.  */
194   dwarf_vma *    range_lists;
195   unsigned int   num_range_lists;
196   unsigned int   max_range_lists;
197   dwarf_vma      rnglists_base;
198   dwarf_vma      str_offsets_base;
199 }
200 debug_info;
201 
202 typedef struct separate_info
203 {
204   void *                  handle;    /* The pointer returned by open_debug_file().  */
205   const char *            filename;
206   struct separate_info *  next;
207 } separate_info;
208 
209 extern separate_info * first_separate_info;
210 
211 extern unsigned int eh_addr_size;
212 
213 extern int do_debug_info;
214 extern int do_debug_abbrevs;
215 extern int do_debug_lines;
216 extern int do_debug_pubnames;
217 extern int do_debug_pubtypes;
218 extern int do_debug_aranges;
219 extern int do_debug_ranges;
220 extern int do_debug_frames;
221 extern int do_debug_frames_interp;
222 extern int do_debug_macinfo;
223 extern int do_debug_str;
224 extern int do_debug_str_offsets;
225 extern int do_debug_loc;
226 extern int do_gdb_index;
227 extern int do_trace_info;
228 extern int do_trace_abbrevs;
229 extern int do_trace_aranges;
230 extern int do_debug_addr;
231 extern int do_debug_cu_index;
232 extern int do_wide;
233 extern int do_debug_links;
234 extern int do_follow_links;
235 #ifdef HAVE_LIBDEBUGINFOD
236 extern int use_debuginfod;
237 #endif
238 extern bool do_checks;
239 
240 extern int dwarf_cutoff_level;
241 extern unsigned long dwarf_start_die;
242 
243 extern int dwarf_check;
244 
245 extern void init_dwarf_regnames_by_elf_machine_code (unsigned int);
246 extern void init_dwarf_regnames_by_bfd_arch_and_mach (enum bfd_architecture arch,
247 						      unsigned long mach);
248 
249 extern bool load_debug_section (enum dwarf_section_display_enum, void *);
250 extern void free_debug_section (enum dwarf_section_display_enum);
251 extern bool load_separate_debug_files (void *, const char *);
252 extern void close_debug_file (void *);
253 extern void *open_debug_file (const char *);
254 
255 extern void free_debug_memory (void);
256 
257 extern int dwarf_select_sections_by_names (const char *);
258 extern int dwarf_select_sections_by_letters (const char *);
259 extern void dwarf_select_sections_all (void);
260 
261 extern unsigned int * find_cu_tu_set (void *, unsigned int);
262 
263 extern void * cmalloc (size_t, size_t);
264 extern void * xcalloc2 (size_t, size_t);
265 extern void * xcmalloc (size_t, size_t);
266 extern void * xcrealloc (void *, size_t, size_t);
267 
268 /* A callback into the client.  Returns TRUE if there is a
269    relocation against the given debug section at the given
270    offset.  */
271 extern bool reloc_at (struct dwarf_section *, dwarf_vma);
272 
273 extern dwarf_vma read_leb128 (unsigned char *, const unsigned char *const,
274 			      bool, unsigned int *, int *);
275 
276 #if HAVE_LIBDEBUGINFOD
277 extern unsigned char * get_build_id (void *);
278 #endif
279 
280 static inline void
report_leb_status(int status)281 report_leb_status (int status)
282 {
283   if ((status & 1) != 0)
284     error (_("end of data encountered whilst reading LEB\n"));
285   else if ((status & 2) != 0)
286     error (_("read LEB value is too large to store in destination variable\n"));
287 }
288 
289 #define SKIP_ULEB(start, end)					\
290   do								\
291     {								\
292       unsigned int _len;					\
293       read_leb128 (start, end, false, &_len, NULL);		\
294       start += _len;						\
295     }								\
296   while (0)
297 
298 #define SKIP_SLEB(start, end)					\
299   do								\
300     {								\
301       unsigned int _len;					\
302       read_leb128 (start, end, true, &_len, NULL);		\
303       start += _len;						\
304     }								\
305   while (0)
306 
307 #define READ_ULEB(var, start, end)				\
308   do								\
309     {								\
310       dwarf_vma _val;						\
311       unsigned int _len;					\
312       int _status;						\
313 								\
314       _val = read_leb128 (start, end, false, &_len, &_status);	\
315       start += _len;						\
316       (var) = _val;						\
317       if ((var) != _val)					\
318 	_status |= 2;						\
319       report_leb_status (_status);				\
320     }								\
321   while (0)
322 
323 #define READ_SLEB(var, start, end)				\
324   do								\
325     {								\
326       dwarf_signed_vma _val;					\
327       unsigned int _len;					\
328       int _status;						\
329 								\
330       _val = read_leb128 (start, end, true, &_len, &_status);	\
331       start += _len;						\
332       (var) = _val;						\
333       if ((var) != _val)					\
334 	_status |= 2;						\
335       report_leb_status (_status);				\
336     }								\
337   while (0)
338