blob: 78fd5ce7d31d4e9daa20dacdfe658c22c08ba81b [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Internal definitions for libdwarf.
Roland McGrath43da9892007-04-16 23:13:37 +00002 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 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,
Ulrich Drepper1e9ef502006-04-04 22:29:06 +000017 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
Ulrich Drepper361df7d2006-04-04 21:38:57 +000018
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;
Roland McGrath43da9892007-04-16 23:13:37 +0000212 unsigned int ndirs;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000213 unsigned int nfiles;
214 struct Dwarf_Fileinfo_s
215 {
216 char *name;
217 Dwarf_Word mtime;
218 Dwarf_Word length;
219 } info[0];
Roland McGrath43da9892007-04-16 23:13:37 +0000220 /* nfiles of those, followed by char *[ndirs]. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000221 };
222typedef struct Dwarf_Fileinfo_s Dwarf_Fileinfo;
223
224
225/* Representation of a row in the line table. */
226struct Dwarf_Lines_s
227 {
228 size_t nlines;
229
230 struct Dwarf_Line_s
231 {
232 Dwarf_Addr addr;
233 unsigned int file;
234 int line;
235 unsigned short int column;
236 unsigned int is_stmt:1;
237 unsigned int basic_block:1;
238 unsigned int end_sequence:1;
239 unsigned int prologue_end:1;
240 unsigned int epilogue_begin:1;
241
242 Dwarf_Files *files;
243 } info[0];
244 };
245
246
247/* Representation of address ranges. */
248struct Dwarf_Aranges_s
249{
250 Dwarf *dbg;
251 size_t naranges;
252
253 struct Dwarf_Arange_s
254 {
255 Dwarf_Addr addr;
256 Dwarf_Word length;
257 Dwarf_Off offset;
258 } info[0];
259};
260
261
262/* CU representation. */
263struct Dwarf_CU
264{
265 Dwarf *dbg;
266 Dwarf_Off start;
267 Dwarf_Off end;
268 uint8_t address_size;
269 uint8_t offset_size;
Roland McGrathe4c22ea2007-10-23 13:07:39 +0000270 uint16_t version;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000271
272 /* Hash table for the abbreviations. */
273 Dwarf_Abbrev_Hash abbrev_hash;
274 /* Offset of the first abbreviation. */
275 size_t orig_abbrev_offset;
276 /* Offset past last read abbreviation. */
277 size_t last_abbrev_offset;
278
279 /* The srcline information. */
280 Dwarf_Lines *lines;
281
282 /* The source file information. */
283 Dwarf_Files *files;
284
285 /* Known location lists. */
286 void *locs;
287};
288
Ulrich Drepperdb1d5432006-10-10 15:34:34 +0000289#define CUDIE(fromcu) \
Roland McGrath6724c902005-10-28 07:07:19 +0000290 ((Dwarf_Die) \
291 { \
292 .cu = (fromcu), \
293 .addr = ((char *) (fromcu)->dbg->sectiondata[IDX_debug_info]->d_buf \
294 + (fromcu)->start + 3 * (fromcu)->offset_size - 4 + 3), \
295 })
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000296
297
298/* Macro information. */
299struct Dwarf_Macro_s
300{
301 unsigned int opcode;
302 Dwarf_Word param1;
303 union
304 {
305 Dwarf_Word u;
306 const char *s;
307 } param2;
308};
309
310
311/* We have to include the file at this point because the inline
312 functions access internals of the Dwarf structure. */
313#include "memory-access.h"
314
315
316/* Set error value. */
317extern void __libdw_seterrno (int value) internal_function;
318
319
320/* Memory handling, the easy parts. This macro does not do any locking. */
Ulrich Drepperdb1d5432006-10-10 15:34:34 +0000321#define libdw_alloc(dbg, type, tsize, cnt) \
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000322 ({ struct libdw_memblock *_tail = (dbg)->mem_tail; \
323 size_t _required = (tsize) * (cnt); \
324 type *_result = (type *) (_tail->mem + (_tail->size - _tail->remaining));\
325 size_t _padding = ((__alignof (type) \
326 - ((uintptr_t) _result & (__alignof (type) - 1))) \
327 & (__alignof (type) - 1)); \
328 if (unlikely (_tail->remaining < _required + _padding)) \
Roland McGrathc373d852006-10-10 00:25:21 +0000329 _result = (type *) __libdw_allocate (dbg, _required, __alignof (type));\
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000330 else \
331 { \
332 _required += _padding; \
333 _result = (type *) ((char *) _result + _padding); \
Roland McGrathc373d852006-10-10 00:25:21 +0000334 _tail->remaining -= _required; \
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000335 } \
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000336 _result; })
337
338#define libdw_typed_alloc(dbg, type) \
339 libdw_alloc (dbg, type, sizeof (type), 1)
340
341/* Callback to allocate more. */
Roland McGrathc373d852006-10-10 00:25:21 +0000342extern void *__libdw_allocate (Dwarf *dbg, size_t minsize, size_t align)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000343 __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
Roland McGrathe4c22ea2007-10-23 13:07:39 +0000369/* Helper function for DW_FORM_ref* handling. */
370extern int __libdw_formref (Dwarf_Attribute *attr, Dwarf_Off *return_offset)
371 __nonnull_attribute__ (1, 2) internal_function;
372
373
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000374/* Helper function to locate attribute. */
375extern unsigned char *__libdw_find_attr (Dwarf_Die *die,
376 unsigned int search_name,
377 unsigned int *codep,
378 unsigned int *formp)
379 __nonnull_attribute__ (1) internal_function;
380
381/* Helper function to access integer attribute. */
Roland McGrath6724c902005-10-28 07:07:19 +0000382extern int __libdw_attr_intval (Dwarf_Die *die, int *valp, int attval)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000383 __nonnull_attribute__ (1, 2) internal_function;
384
Roland McGrath1ecb6062005-08-15 09:53:04 +0000385/* Helper function to walk scopes. */
Roland McGrath71e15a02005-08-27 10:33:26 +0000386struct Dwarf_Die_Chain
387{
388 Dwarf_Die die;
389 struct Dwarf_Die_Chain *parent;
390 bool prune; /* The PREVISIT function can set this. */
391};
392extern int __libdw_visit_scopes (unsigned int depth,
393 struct Dwarf_Die_Chain *root,
394 int (*previsit) (unsigned int depth,
395 struct Dwarf_Die_Chain *,
396 void *arg),
397 int (*postvisit) (unsigned int depth,
398 struct Dwarf_Die_Chain *,
399 void *arg),
Roland McGrath1ecb6062005-08-15 09:53:04 +0000400 void *arg)
401 __nonnull_attribute__ (2, 3) internal_function;
402
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000403/* Return error code of last failing function call. This value is kept
404 separately for each thread. */
405extern int __dwarf_errno_internal (void);
406
407
408/* Aliases to avoid PLTs. */
409INTDECL (dwarf_attr)
410INTDECL (dwarf_attr_integrate)
411INTDECL (dwarf_begin_elf)
412INTDECL (dwarf_child)
413INTDECL (dwarf_dieoffset)
Roland McGrath71e15a02005-08-27 10:33:26 +0000414INTDECL (dwarf_diename)
Roland McGrath4959bf82005-08-09 10:31:08 +0000415INTDECL (dwarf_end)
Roland McGrath07d4f2f2005-10-28 06:56:24 +0000416INTDECL (dwarf_entrypc)
Roland McGrath4959bf82005-08-09 10:31:08 +0000417INTDECL (dwarf_errmsg)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000418INTDECL (dwarf_formaddr)
419INTDECL (dwarf_formblock)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000420INTDECL (dwarf_formref_die)
421INTDECL (dwarf_formsdata)
422INTDECL (dwarf_formstring)
423INTDECL (dwarf_formudata)
424INTDECL (dwarf_getarange_addr)
425INTDECL (dwarf_getarangeinfo)
426INTDECL (dwarf_getaranges)
427INTDECL (dwarf_getsrcfiles)
428INTDECL (dwarf_getsrclines)
429INTDECL (dwarf_hasattr)
430INTDECL (dwarf_haschildren)
431INTDECL (dwarf_haspc)
432INTDECL (dwarf_highpc)
433INTDECL (dwarf_lowpc)
434INTDECL (dwarf_nextcu)
435INTDECL (dwarf_offdie)
Roland McGrath07d4f2f2005-10-28 06:56:24 +0000436INTDECL (dwarf_ranges)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000437INTDECL (dwarf_siblingof)
438INTDECL (dwarf_tag)
439
440#endif /* libdwP.h */