blob: 436287d80db00f70561a2d7531ea8d1bcb110abf [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Internal definitions for libdwarf.
2 Copyright (C) 2002, 2003, 2004, 2005 Red Hat, Inc.
3 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5 This program is Open Source software; you can redistribute it and/or
6 modify it under the terms of the Open Software License version 1.0 as
7 published by the Open Source Initiative.
8
9 You should have received a copy of the Open Software License along
10 with this program; if not, you may obtain a copy of the Open Software
11 License version 1.0 from http://www.opensource.org/licenses/osl.php or
12 by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
13 3001 King Ranch Road, Ukiah, CA 95482. */
14
15#ifndef _LIBDWP_H
16#define _LIBDWP_H 1
17
18#include <libintl.h>
19#include <stdbool.h>
20
21#include <libdw.h>
22
23
24/* gettext helper macros. */
25#define _(Str) dgettext ("elfutils", Str)
26
27
28/* Version of the DWARF specification we support. */
29#define DWARF_VERSION 2
30
31/* Version of the CIE format. */
32#define CIE_VERSION 1
33
34
Roland McGrath6724c902005-10-28 07:07:19 +000035/* Known location expressions already decoded. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000036struct loc_s
37{
38 void *addr;
Roland McGrath6724c902005-10-28 07:07:19 +000039 Dwarf_Op *loc;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000040 size_t nloc;
41};
42
43/* Valid indeces for the section data. */
44enum
45 {
46 IDX_debug_info = 0,
47 IDX_debug_abbrev,
48 IDX_debug_aranges,
49 IDX_debug_line,
50 IDX_debug_frame,
51 IDX_eh_frame,
52 IDX_debug_loc,
53 IDX_debug_pubnames,
54 IDX_debug_str,
55 IDX_debug_funcnames,
56 IDX_debug_typenames,
57 IDX_debug_varnames,
58 IDX_debug_weaknames,
59 IDX_debug_macinfo,
60 IDX_debug_ranges,
61 IDX_last
62 };
63
64
65/* Error values. */
66enum
67{
68 DWARF_E_NOERROR = 0,
69 DWARF_E_UNKNOWN_ERROR,
70 DWARF_E_INVALID_ACCESS,
71 DWARF_E_NO_REGFILE,
72 DWARF_E_IO_ERROR,
73 DWARF_E_INVALID_ELF,
74 DWARF_E_NO_DWARF,
75 DWARF_E_NOELF,
76 DWARF_E_GETEHDR_ERROR,
77 DWARF_E_NOMEM,
78 DWARF_E_UNIMPL,
79 DWARF_E_INVALID_CMD,
80 DWARF_E_INVALID_VERSION,
81 DWARF_E_INVALID_FILE,
82 DWARF_E_NO_ENTRY,
83 DWARF_E_INVALID_DWARF,
84 DWARF_E_NO_STRING,
85 DWARF_E_NO_ADDR,
86 DWARF_E_NO_CONSTANT,
87 DWARF_E_NO_REFERENCE,
88 DWARF_E_INVALID_REFERENCE,
89 DWARF_E_NO_DEBUG_LINE,
90 DWARF_E_INVALID_DEBUG_LINE,
91 DWARF_E_TOO_BIG,
92 DWARF_E_VERSION,
93 DWARF_E_INVALID_DIR_IDX,
94 DWARF_E_ADDR_OUTOFRANGE,
95 DWARF_E_NO_LOCLIST,
96 DWARF_E_NO_BLOCK,
97 DWARF_E_INVALID_LINE_IDX,
98 DWARF_E_INVALID_ARANGE_IDX,
99 DWARF_E_NO_MATCH,
100 DWARF_E_NO_FLAG,
101 DWARF_E_INVALID_OFFSET,
102 DWARF_E_NO_DEBUG_RANGES,
103};
104
105
106/* This is the structure representing the debugging state. */
107struct Dwarf
108{
109 /* The underlying ELF file. */
110 Elf *elf;
111
112 /* The section data. */
113 Elf_Data *sectiondata[IDX_last];
114
115 /* True if the file has a byte order different from the host. */
116 bool other_byte_order;
117
118 /* If true, we allocated the ELF descriptor ourselves. */
119 bool free_elf;
120
121 /* Information for traversing the .debug_pubnames section. This is
122 an array and separately allocated with malloc. */
123 struct pubnames_s
124 {
125 Dwarf_Off cu_offset;
126 Dwarf_Off set_start;
127 unsigned int cu_header_size;
128 int address_len;
129 } *pubnames_sets;
130 size_t pubnames_nsets;
131
132 /* Search tree for the CUs. */
133 void *cu_tree;
134 Dwarf_Off next_cu_offset;
135
136 /* Address ranges. */
137 Dwarf_Aranges *aranges;
138
139 /* Internal memory handling. This is basically a simplified
140 reimplementation of obstacks. Unfortunately the standard obstack
141 implementation is not usable in libraries. */
142 struct libdw_memblock
143 {
144 size_t size;
145 size_t remaining;
146 struct libdw_memblock *prev;
147 char mem[0];
148 } *mem_tail;
149
150 /* Default size of allocated memory blocks. */
151 size_t mem_default_size;
152
153 /* Registered OOM handler. */
154 Dwarf_OOM oom_handler;
155};
156
157
158/* Abbreviation representation. */
159struct Dwarf_Abbrev
160{
161 unsigned int code;
162 unsigned int tag;
163 int has_children;
164 unsigned int attrcnt;
165 unsigned char *attrp;
166 Dwarf_Off offset;
167};
168
169#include "dwarf_abbrev_hash.h"
170
171
172/* Files in line information records. */
173struct Dwarf_Files_s
174 {
175 Dwarf *dbg;
176 unsigned int nfiles;
177 struct Dwarf_Fileinfo_s
178 {
179 char *name;
180 Dwarf_Word mtime;
181 Dwarf_Word length;
182 } info[0];
183 };
184typedef struct Dwarf_Fileinfo_s Dwarf_Fileinfo;
185
186
187/* Representation of a row in the line table. */
188struct Dwarf_Lines_s
189 {
190 size_t nlines;
191
192 struct Dwarf_Line_s
193 {
194 Dwarf_Addr addr;
195 unsigned int file;
196 int line;
197 unsigned short int column;
198 unsigned int is_stmt:1;
199 unsigned int basic_block:1;
200 unsigned int end_sequence:1;
201 unsigned int prologue_end:1;
202 unsigned int epilogue_begin:1;
203
204 Dwarf_Files *files;
205 } info[0];
206 };
207
208
209/* Representation of address ranges. */
210struct Dwarf_Aranges_s
211{
212 Dwarf *dbg;
213 size_t naranges;
214
215 struct Dwarf_Arange_s
216 {
217 Dwarf_Addr addr;
218 Dwarf_Word length;
219 Dwarf_Off offset;
220 } info[0];
221};
222
223
224/* CU representation. */
225struct Dwarf_CU
226{
227 Dwarf *dbg;
228 Dwarf_Off start;
229 Dwarf_Off end;
230 uint8_t address_size;
231 uint8_t offset_size;
232
233 /* Hash table for the abbreviations. */
234 Dwarf_Abbrev_Hash abbrev_hash;
235 /* Offset of the first abbreviation. */
236 size_t orig_abbrev_offset;
237 /* Offset past last read abbreviation. */
238 size_t last_abbrev_offset;
239
240 /* The srcline information. */
241 Dwarf_Lines *lines;
242
243 /* The source file information. */
244 Dwarf_Files *files;
245
246 /* Known location lists. */
247 void *locs;
248};
249
Roland McGrath6724c902005-10-28 07:07:19 +0000250#define CUDIE(fromcu) \
251 ((Dwarf_Die) \
252 { \
253 .cu = (fromcu), \
254 .addr = ((char *) (fromcu)->dbg->sectiondata[IDX_debug_info]->d_buf \
255 + (fromcu)->start + 3 * (fromcu)->offset_size - 4 + 3), \
256 })
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000257
258
259/* Macro information. */
260struct Dwarf_Macro_s
261{
262 unsigned int opcode;
263 Dwarf_Word param1;
264 union
265 {
266 Dwarf_Word u;
267 const char *s;
268 } param2;
269};
270
271
272/* We have to include the file at this point because the inline
273 functions access internals of the Dwarf structure. */
274#include "memory-access.h"
275
276
277/* Set error value. */
278extern void __libdw_seterrno (int value) internal_function;
279
280
281/* Memory handling, the easy parts. This macro does not do any locking. */
282#define libdw_alloc(dbg, type, tsize, cnt) \
283 ({ struct libdw_memblock *_tail = (dbg)->mem_tail; \
284 size_t _required = (tsize) * (cnt); \
285 type *_result = (type *) (_tail->mem + (_tail->size - _tail->remaining));\
286 size_t _padding = ((__alignof (type) \
287 - ((uintptr_t) _result & (__alignof (type) - 1))) \
288 & (__alignof (type) - 1)); \
289 if (unlikely (_tail->remaining < _required + _padding)) \
290 { \
291 _result = (type *) __libdw_allocate (dbg, _required); \
292 _tail = (dbg)->mem_tail; \
293 } \
294 else \
295 { \
296 _required += _padding; \
297 _result = (type *) ((char *) _result + _padding); \
298 } \
299 _tail->remaining -= _required; \
300 _result; })
301
302#define libdw_typed_alloc(dbg, type) \
303 libdw_alloc (dbg, type, sizeof (type), 1)
304
305/* Callback to allocate more. */
306extern void *__libdw_allocate (Dwarf *dbg, size_t minsize)
307 __attribute__ ((__malloc__)) __nonnull_attribute__ (1);
308
309/* Default OOM handler. */
310extern void __libdw_oom (void) __attribute ((noreturn, visibility ("hidden")));
311
312/* Find CU for given offset. */
313extern struct Dwarf_CU *__libdw_findcu (Dwarf *dbg, Dwarf_Off offset)
314 __nonnull_attribute__ (1) internal_function;
315
316/* Return tag of given DIE. */
317extern Dwarf_Abbrev *__libdw_findabbrev (struct Dwarf_CU *cu,
318 unsigned int code)
319 __nonnull_attribute__ (1) internal_function;
320
321/* Get abbreviation at given offset. */
322extern Dwarf_Abbrev *__libdw_getabbrev (Dwarf *dbg, struct Dwarf_CU *cu,
323 Dwarf_Off offset, size_t *lengthp,
324 Dwarf_Abbrev *result)
325 __nonnull_attribute__ (1) internal_function;
326
327/* Helper functions for form handling. */
328extern size_t __libdw_form_val_len (Dwarf *dbg, struct Dwarf_CU *cu,
329 unsigned int form,
330 const unsigned char *valp)
331 __nonnull_attribute__ (1, 2, 4) internal_function;
332
333/* Helper function to locate attribute. */
334extern unsigned char *__libdw_find_attr (Dwarf_Die *die,
335 unsigned int search_name,
336 unsigned int *codep,
337 unsigned int *formp)
338 __nonnull_attribute__ (1) internal_function;
339
340/* Helper function to access integer attribute. */
Roland McGrath6724c902005-10-28 07:07:19 +0000341extern int __libdw_attr_intval (Dwarf_Die *die, int *valp, int attval)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000342 __nonnull_attribute__ (1, 2) internal_function;
343
Roland McGrath1ecb6062005-08-15 09:53:04 +0000344/* Helper function to walk scopes. */
Roland McGrath71e15a02005-08-27 10:33:26 +0000345struct Dwarf_Die_Chain
346{
347 Dwarf_Die die;
348 struct Dwarf_Die_Chain *parent;
349 bool prune; /* The PREVISIT function can set this. */
350};
351extern int __libdw_visit_scopes (unsigned int depth,
352 struct Dwarf_Die_Chain *root,
353 int (*previsit) (unsigned int depth,
354 struct Dwarf_Die_Chain *,
355 void *arg),
356 int (*postvisit) (unsigned int depth,
357 struct Dwarf_Die_Chain *,
358 void *arg),
Roland McGrath1ecb6062005-08-15 09:53:04 +0000359 void *arg)
360 __nonnull_attribute__ (2, 3) internal_function;
361
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000362/* Return error code of last failing function call. This value is kept
363 separately for each thread. */
364extern int __dwarf_errno_internal (void);
365
366
367/* Aliases to avoid PLTs. */
368INTDECL (dwarf_attr)
369INTDECL (dwarf_attr_integrate)
370INTDECL (dwarf_begin_elf)
371INTDECL (dwarf_child)
372INTDECL (dwarf_dieoffset)
Roland McGrath71e15a02005-08-27 10:33:26 +0000373INTDECL (dwarf_diename)
Roland McGrath4959bf82005-08-09 10:31:08 +0000374INTDECL (dwarf_end)
Roland McGrath07d4f2f2005-10-28 06:56:24 +0000375INTDECL (dwarf_entrypc)
Roland McGrath4959bf82005-08-09 10:31:08 +0000376INTDECL (dwarf_errmsg)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000377INTDECL (dwarf_formaddr)
378INTDECL (dwarf_formblock)
379INTDECL (dwarf_formref)
380INTDECL (dwarf_formref_die)
381INTDECL (dwarf_formsdata)
382INTDECL (dwarf_formstring)
383INTDECL (dwarf_formudata)
384INTDECL (dwarf_getarange_addr)
385INTDECL (dwarf_getarangeinfo)
386INTDECL (dwarf_getaranges)
387INTDECL (dwarf_getsrcfiles)
388INTDECL (dwarf_getsrclines)
389INTDECL (dwarf_hasattr)
390INTDECL (dwarf_haschildren)
391INTDECL (dwarf_haspc)
392INTDECL (dwarf_highpc)
393INTDECL (dwarf_lowpc)
394INTDECL (dwarf_nextcu)
395INTDECL (dwarf_offdie)
Roland McGrath07d4f2f2005-10-28 06:56:24 +0000396INTDECL (dwarf_ranges)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000397INTDECL (dwarf_siblingof)
398INTDECL (dwarf_tag)
399
400#endif /* libdwP.h */