blob: 16ad29443495d9fba910df34346b9221b0cfb19d [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.
Ulrich Drepper361df7d2006-04-04 21:38:57 +00003 This file is part of Red Hat elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
5
Ulrich Drepper361df7d2006-04-04 21:38:57 +00006 Red Hat elfutils is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by the
8 Free Software Foundation; version 2 of the License.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00009
Ulrich Drepper361df7d2006-04-04 21:38:57 +000010 Red Hat elfutils is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with Red Hat elfutils; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18
19 In addition, as a special exception, Red Hat, Inc. gives You the
20 additional right to link the code of Red Hat elfutils with code licensed
21 under any Open Source Initiative certified open source license
22 (http://www.opensource.org/licenses/index.php) which requires the
23 distribution of source code with any binary distribution and to
24 distribute linked combinations of the two. Non-GPL Code permitted under
25 this exception must only link to the code of Red Hat elfutils through
26 those well defined interfaces identified in the file named EXCEPTION
27 found in the source code files (the "Approved Interfaces"). The files
28 of Non-GPL Code may instantiate templates or use macros or inline
29 functions from the Approved Interfaces without causing the resulting
30 work to be covered by the GNU General Public License. Only Red Hat,
31 Inc. may make changes or additions to the list of Approved Interfaces.
32 Red Hat's grant of this exception is conditioned upon your not adding
33 any new exceptions. If you wish to add a new Approved Interface or
34 exception, please contact Red Hat. You must obey the GNU General Public
35 License in all respects for all of the Red Hat elfutils code and other
36 code used in conjunction with Red Hat elfutils except the Non-GPL Code
37 covered by this exception. If you modify this file, you may extend this
38 exception to your version of the file, but you are not obligated to do
39 so. If you do not wish to provide this exception without modification,
40 you must delete this exception statement from your version and license
41 this file solely under the GPL without exception.
42
43 Red Hat elfutils is an included package of the Open Invention Network.
44 An included package of the Open Invention Network is a package for which
45 Open Invention Network licensees cross-license their patents. No patent
46 license is granted, either expressly or impliedly, by designation as an
47 included package. Should you wish to participate in the Open Invention
48 Network licensing program, please visit www.openinventionnetwork.com
49 <http://www.openinventionnetwork.com>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000050
51#ifndef _LIBDWP_H
52#define _LIBDWP_H 1
53
54#include <libintl.h>
55#include <stdbool.h>
56
57#include <libdw.h>
58
59
60/* gettext helper macros. */
61#define _(Str) dgettext ("elfutils", Str)
62
63
64/* Version of the DWARF specification we support. */
65#define DWARF_VERSION 2
66
67/* Version of the CIE format. */
68#define CIE_VERSION 1
69
70
Roland McGrath6724c902005-10-28 07:07:19 +000071/* Known location expressions already decoded. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000072struct loc_s
73{
74 void *addr;
Roland McGrath6724c902005-10-28 07:07:19 +000075 Dwarf_Op *loc;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000076 size_t nloc;
77};
78
79/* Valid indeces for the section data. */
80enum
81 {
82 IDX_debug_info = 0,
83 IDX_debug_abbrev,
84 IDX_debug_aranges,
85 IDX_debug_line,
86 IDX_debug_frame,
87 IDX_eh_frame,
88 IDX_debug_loc,
89 IDX_debug_pubnames,
90 IDX_debug_str,
91 IDX_debug_funcnames,
92 IDX_debug_typenames,
93 IDX_debug_varnames,
94 IDX_debug_weaknames,
95 IDX_debug_macinfo,
96 IDX_debug_ranges,
97 IDX_last
98 };
99
100
101/* Error values. */
102enum
103{
104 DWARF_E_NOERROR = 0,
105 DWARF_E_UNKNOWN_ERROR,
106 DWARF_E_INVALID_ACCESS,
107 DWARF_E_NO_REGFILE,
108 DWARF_E_IO_ERROR,
109 DWARF_E_INVALID_ELF,
110 DWARF_E_NO_DWARF,
111 DWARF_E_NOELF,
112 DWARF_E_GETEHDR_ERROR,
113 DWARF_E_NOMEM,
114 DWARF_E_UNIMPL,
115 DWARF_E_INVALID_CMD,
116 DWARF_E_INVALID_VERSION,
117 DWARF_E_INVALID_FILE,
118 DWARF_E_NO_ENTRY,
119 DWARF_E_INVALID_DWARF,
120 DWARF_E_NO_STRING,
121 DWARF_E_NO_ADDR,
122 DWARF_E_NO_CONSTANT,
123 DWARF_E_NO_REFERENCE,
124 DWARF_E_INVALID_REFERENCE,
125 DWARF_E_NO_DEBUG_LINE,
126 DWARF_E_INVALID_DEBUG_LINE,
127 DWARF_E_TOO_BIG,
128 DWARF_E_VERSION,
129 DWARF_E_INVALID_DIR_IDX,
130 DWARF_E_ADDR_OUTOFRANGE,
131 DWARF_E_NO_LOCLIST,
132 DWARF_E_NO_BLOCK,
133 DWARF_E_INVALID_LINE_IDX,
134 DWARF_E_INVALID_ARANGE_IDX,
135 DWARF_E_NO_MATCH,
136 DWARF_E_NO_FLAG,
137 DWARF_E_INVALID_OFFSET,
138 DWARF_E_NO_DEBUG_RANGES,
139};
140
141
142/* This is the structure representing the debugging state. */
143struct Dwarf
144{
145 /* The underlying ELF file. */
146 Elf *elf;
147
148 /* The section data. */
149 Elf_Data *sectiondata[IDX_last];
150
151 /* True if the file has a byte order different from the host. */
152 bool other_byte_order;
153
154 /* If true, we allocated the ELF descriptor ourselves. */
155 bool free_elf;
156
157 /* Information for traversing the .debug_pubnames section. This is
158 an array and separately allocated with malloc. */
159 struct pubnames_s
160 {
161 Dwarf_Off cu_offset;
162 Dwarf_Off set_start;
163 unsigned int cu_header_size;
164 int address_len;
165 } *pubnames_sets;
166 size_t pubnames_nsets;
167
168 /* Search tree for the CUs. */
169 void *cu_tree;
170 Dwarf_Off next_cu_offset;
171
172 /* Address ranges. */
173 Dwarf_Aranges *aranges;
174
175 /* Internal memory handling. This is basically a simplified
176 reimplementation of obstacks. Unfortunately the standard obstack
177 implementation is not usable in libraries. */
178 struct libdw_memblock
179 {
180 size_t size;
181 size_t remaining;
182 struct libdw_memblock *prev;
183 char mem[0];
184 } *mem_tail;
185
186 /* Default size of allocated memory blocks. */
187 size_t mem_default_size;
188
189 /* Registered OOM handler. */
190 Dwarf_OOM oom_handler;
191};
192
193
194/* Abbreviation representation. */
195struct Dwarf_Abbrev
196{
197 unsigned int code;
198 unsigned int tag;
199 int has_children;
200 unsigned int attrcnt;
201 unsigned char *attrp;
202 Dwarf_Off offset;
203};
204
205#include "dwarf_abbrev_hash.h"
206
207
208/* Files in line information records. */
209struct Dwarf_Files_s
210 {
211 Dwarf *dbg;
212 unsigned int nfiles;
213 struct Dwarf_Fileinfo_s
214 {
215 char *name;
216 Dwarf_Word mtime;
217 Dwarf_Word length;
218 } info[0];
219 };
220typedef struct Dwarf_Fileinfo_s Dwarf_Fileinfo;
221
222
223/* Representation of a row in the line table. */
224struct Dwarf_Lines_s
225 {
226 size_t nlines;
227
228 struct Dwarf_Line_s
229 {
230 Dwarf_Addr addr;
231 unsigned int file;
232 int line;
233 unsigned short int column;
234 unsigned int is_stmt:1;
235 unsigned int basic_block:1;
236 unsigned int end_sequence:1;
237 unsigned int prologue_end:1;
238 unsigned int epilogue_begin:1;
239
240 Dwarf_Files *files;
241 } info[0];
242 };
243
244
245/* Representation of address ranges. */
246struct Dwarf_Aranges_s
247{
248 Dwarf *dbg;
249 size_t naranges;
250
251 struct Dwarf_Arange_s
252 {
253 Dwarf_Addr addr;
254 Dwarf_Word length;
255 Dwarf_Off offset;
256 } info[0];
257};
258
259
260/* CU representation. */
261struct Dwarf_CU
262{
263 Dwarf *dbg;
264 Dwarf_Off start;
265 Dwarf_Off end;
266 uint8_t address_size;
267 uint8_t offset_size;
268
269 /* Hash table for the abbreviations. */
270 Dwarf_Abbrev_Hash abbrev_hash;
271 /* Offset of the first abbreviation. */
272 size_t orig_abbrev_offset;
273 /* Offset past last read abbreviation. */
274 size_t last_abbrev_offset;
275
276 /* The srcline information. */
277 Dwarf_Lines *lines;
278
279 /* The source file information. */
280 Dwarf_Files *files;
281
282 /* Known location lists. */
283 void *locs;
284};
285
Roland McGrath6724c902005-10-28 07:07:19 +0000286#define CUDIE(fromcu) \
287 ((Dwarf_Die) \
288 { \
289 .cu = (fromcu), \
290 .addr = ((char *) (fromcu)->dbg->sectiondata[IDX_debug_info]->d_buf \
291 + (fromcu)->start + 3 * (fromcu)->offset_size - 4 + 3), \
292 })
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000293
294
295/* Macro information. */
296struct Dwarf_Macro_s
297{
298 unsigned int opcode;
299 Dwarf_Word param1;
300 union
301 {
302 Dwarf_Word u;
303 const char *s;
304 } param2;
305};
306
307
308/* We have to include the file at this point because the inline
309 functions access internals of the Dwarf structure. */
310#include "memory-access.h"
311
312
313/* Set error value. */
314extern void __libdw_seterrno (int value) internal_function;
315
316
317/* Memory handling, the easy parts. This macro does not do any locking. */
318#define libdw_alloc(dbg, type, tsize, cnt) \
319 ({ struct libdw_memblock *_tail = (dbg)->mem_tail; \
320 size_t _required = (tsize) * (cnt); \
321 type *_result = (type *) (_tail->mem + (_tail->size - _tail->remaining));\
322 size_t _padding = ((__alignof (type) \
323 - ((uintptr_t) _result & (__alignof (type) - 1))) \
324 & (__alignof (type) - 1)); \
325 if (unlikely (_tail->remaining < _required + _padding)) \
326 { \
327 _result = (type *) __libdw_allocate (dbg, _required); \
328 _tail = (dbg)->mem_tail; \
329 } \
330 else \
331 { \
332 _required += _padding; \
333 _result = (type *) ((char *) _result + _padding); \
334 } \
335 _tail->remaining -= _required; \
336 _result; })
337
338#define libdw_typed_alloc(dbg, type) \
339 libdw_alloc (dbg, type, sizeof (type), 1)
340
341/* Callback to allocate more. */
342extern void *__libdw_allocate (Dwarf *dbg, size_t minsize)
343 __attribute__ ((__malloc__)) __nonnull_attribute__ (1);
344
345/* Default OOM handler. */
346extern void __libdw_oom (void) __attribute ((noreturn, visibility ("hidden")));
347
348/* Find CU for given offset. */
349extern struct Dwarf_CU *__libdw_findcu (Dwarf *dbg, Dwarf_Off offset)
350 __nonnull_attribute__ (1) internal_function;
351
352/* Return tag of given DIE. */
353extern Dwarf_Abbrev *__libdw_findabbrev (struct Dwarf_CU *cu,
354 unsigned int code)
355 __nonnull_attribute__ (1) internal_function;
356
357/* Get abbreviation at given offset. */
358extern Dwarf_Abbrev *__libdw_getabbrev (Dwarf *dbg, struct Dwarf_CU *cu,
359 Dwarf_Off offset, size_t *lengthp,
360 Dwarf_Abbrev *result)
361 __nonnull_attribute__ (1) internal_function;
362
363/* Helper functions for form handling. */
364extern size_t __libdw_form_val_len (Dwarf *dbg, struct Dwarf_CU *cu,
365 unsigned int form,
366 const unsigned char *valp)
367 __nonnull_attribute__ (1, 2, 4) internal_function;
368
369/* Helper function to locate attribute. */
370extern unsigned char *__libdw_find_attr (Dwarf_Die *die,
371 unsigned int search_name,
372 unsigned int *codep,
373 unsigned int *formp)
374 __nonnull_attribute__ (1) internal_function;
375
376/* Helper function to access integer attribute. */
Roland McGrath6724c902005-10-28 07:07:19 +0000377extern int __libdw_attr_intval (Dwarf_Die *die, int *valp, int attval)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000378 __nonnull_attribute__ (1, 2) internal_function;
379
Roland McGrath1ecb6062005-08-15 09:53:04 +0000380/* Helper function to walk scopes. */
Roland McGrath71e15a02005-08-27 10:33:26 +0000381struct Dwarf_Die_Chain
382{
383 Dwarf_Die die;
384 struct Dwarf_Die_Chain *parent;
385 bool prune; /* The PREVISIT function can set this. */
386};
387extern int __libdw_visit_scopes (unsigned int depth,
388 struct Dwarf_Die_Chain *root,
389 int (*previsit) (unsigned int depth,
390 struct Dwarf_Die_Chain *,
391 void *arg),
392 int (*postvisit) (unsigned int depth,
393 struct Dwarf_Die_Chain *,
394 void *arg),
Roland McGrath1ecb6062005-08-15 09:53:04 +0000395 void *arg)
396 __nonnull_attribute__ (2, 3) internal_function;
397
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000398/* Return error code of last failing function call. This value is kept
399 separately for each thread. */
400extern int __dwarf_errno_internal (void);
401
402
403/* Aliases to avoid PLTs. */
404INTDECL (dwarf_attr)
405INTDECL (dwarf_attr_integrate)
406INTDECL (dwarf_begin_elf)
407INTDECL (dwarf_child)
408INTDECL (dwarf_dieoffset)
Roland McGrath71e15a02005-08-27 10:33:26 +0000409INTDECL (dwarf_diename)
Roland McGrath4959bf82005-08-09 10:31:08 +0000410INTDECL (dwarf_end)
Roland McGrath07d4f2f2005-10-28 06:56:24 +0000411INTDECL (dwarf_entrypc)
Roland McGrath4959bf82005-08-09 10:31:08 +0000412INTDECL (dwarf_errmsg)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000413INTDECL (dwarf_formaddr)
414INTDECL (dwarf_formblock)
415INTDECL (dwarf_formref)
416INTDECL (dwarf_formref_die)
417INTDECL (dwarf_formsdata)
418INTDECL (dwarf_formstring)
419INTDECL (dwarf_formudata)
420INTDECL (dwarf_getarange_addr)
421INTDECL (dwarf_getarangeinfo)
422INTDECL (dwarf_getaranges)
423INTDECL (dwarf_getsrcfiles)
424INTDECL (dwarf_getsrclines)
425INTDECL (dwarf_hasattr)
426INTDECL (dwarf_haschildren)
427INTDECL (dwarf_haspc)
428INTDECL (dwarf_highpc)
429INTDECL (dwarf_lowpc)
430INTDECL (dwarf_nextcu)
431INTDECL (dwarf_offdie)
Roland McGrath07d4f2f2005-10-28 06:56:24 +0000432INTDECL (dwarf_ranges)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000433INTDECL (dwarf_siblingof)
434INTDECL (dwarf_tag)
435
436#endif /* libdwP.h */