Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* 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 | |
| 35 | /* Known location lists. */ |
| 36 | struct loc_s |
| 37 | { |
| 38 | void *addr; |
| 39 | Dwarf_Loc *loc; |
| 40 | size_t nloc; |
| 41 | }; |
| 42 | |
| 43 | /* Valid indeces for the section data. */ |
| 44 | enum |
| 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. */ |
| 66 | enum |
| 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. */ |
| 107 | struct 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. */ |
| 159 | struct 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. */ |
| 173 | struct 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 | }; |
| 184 | typedef struct Dwarf_Fileinfo_s Dwarf_Fileinfo; |
| 185 | |
| 186 | |
| 187 | /* Representation of a row in the line table. */ |
| 188 | struct 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. */ |
| 210 | struct 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. */ |
| 225 | struct 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 | |
| 250 | |
| 251 | /* Function information. */ |
| 252 | struct Dwarf_Func_s |
| 253 | { |
| 254 | // XXX If we want to cache functions, we need to change this struct. |
| 255 | Dwarf_Die *die; |
| 256 | Dwarf_Die *cudie; |
| 257 | }; |
| 258 | |
| 259 | |
| 260 | /* Macro information. */ |
| 261 | struct Dwarf_Macro_s |
| 262 | { |
| 263 | unsigned int opcode; |
| 264 | Dwarf_Word param1; |
| 265 | union |
| 266 | { |
| 267 | Dwarf_Word u; |
| 268 | const char *s; |
| 269 | } param2; |
| 270 | }; |
| 271 | |
| 272 | |
| 273 | /* We have to include the file at this point because the inline |
| 274 | functions access internals of the Dwarf structure. */ |
| 275 | #include "memory-access.h" |
| 276 | |
| 277 | |
| 278 | /* Set error value. */ |
| 279 | extern void __libdw_seterrno (int value) internal_function; |
| 280 | |
| 281 | |
| 282 | /* Memory handling, the easy parts. This macro does not do any locking. */ |
| 283 | #define libdw_alloc(dbg, type, tsize, cnt) \ |
| 284 | ({ struct libdw_memblock *_tail = (dbg)->mem_tail; \ |
| 285 | size_t _required = (tsize) * (cnt); \ |
| 286 | type *_result = (type *) (_tail->mem + (_tail->size - _tail->remaining));\ |
| 287 | size_t _padding = ((__alignof (type) \ |
| 288 | - ((uintptr_t) _result & (__alignof (type) - 1))) \ |
| 289 | & (__alignof (type) - 1)); \ |
| 290 | if (unlikely (_tail->remaining < _required + _padding)) \ |
| 291 | { \ |
| 292 | _result = (type *) __libdw_allocate (dbg, _required); \ |
| 293 | _tail = (dbg)->mem_tail; \ |
| 294 | } \ |
| 295 | else \ |
| 296 | { \ |
| 297 | _required += _padding; \ |
| 298 | _result = (type *) ((char *) _result + _padding); \ |
| 299 | } \ |
| 300 | _tail->remaining -= _required; \ |
| 301 | _result; }) |
| 302 | |
| 303 | #define libdw_typed_alloc(dbg, type) \ |
| 304 | libdw_alloc (dbg, type, sizeof (type), 1) |
| 305 | |
| 306 | /* Callback to allocate more. */ |
| 307 | extern void *__libdw_allocate (Dwarf *dbg, size_t minsize) |
| 308 | __attribute__ ((__malloc__)) __nonnull_attribute__ (1); |
| 309 | |
| 310 | /* Default OOM handler. */ |
| 311 | extern void __libdw_oom (void) __attribute ((noreturn, visibility ("hidden"))); |
| 312 | |
| 313 | /* Find CU for given offset. */ |
| 314 | extern struct Dwarf_CU *__libdw_findcu (Dwarf *dbg, Dwarf_Off offset) |
| 315 | __nonnull_attribute__ (1) internal_function; |
| 316 | |
| 317 | /* Return tag of given DIE. */ |
| 318 | extern Dwarf_Abbrev *__libdw_findabbrev (struct Dwarf_CU *cu, |
| 319 | unsigned int code) |
| 320 | __nonnull_attribute__ (1) internal_function; |
| 321 | |
| 322 | /* Get abbreviation at given offset. */ |
| 323 | extern Dwarf_Abbrev *__libdw_getabbrev (Dwarf *dbg, struct Dwarf_CU *cu, |
| 324 | Dwarf_Off offset, size_t *lengthp, |
| 325 | Dwarf_Abbrev *result) |
| 326 | __nonnull_attribute__ (1) internal_function; |
| 327 | |
| 328 | /* Helper functions for form handling. */ |
| 329 | extern size_t __libdw_form_val_len (Dwarf *dbg, struct Dwarf_CU *cu, |
| 330 | unsigned int form, |
| 331 | const unsigned char *valp) |
| 332 | __nonnull_attribute__ (1, 2, 4) internal_function; |
| 333 | |
| 334 | /* Helper function to locate attribute. */ |
| 335 | extern unsigned char *__libdw_find_attr (Dwarf_Die *die, |
| 336 | unsigned int search_name, |
| 337 | unsigned int *codep, |
| 338 | unsigned int *formp) |
| 339 | __nonnull_attribute__ (1) internal_function; |
| 340 | |
| 341 | /* Helper function to access integer attribute. */ |
| 342 | extern int __libdw_func_intval (Dwarf_Func *func, int *linep, int attval) |
| 343 | __nonnull_attribute__ (1, 2) internal_function; |
| 344 | |
Roland McGrath | 1ecb606 | 2005-08-15 09:53:04 +0000 | [diff] [blame] | 345 | /* Helper function to walk scopes. */ |
Roland McGrath | 71e15a0 | 2005-08-27 10:33:26 +0000 | [diff] [blame] | 346 | struct Dwarf_Die_Chain |
| 347 | { |
| 348 | Dwarf_Die die; |
| 349 | struct Dwarf_Die_Chain *parent; |
| 350 | bool prune; /* The PREVISIT function can set this. */ |
| 351 | }; |
| 352 | extern int __libdw_visit_scopes (unsigned int depth, |
| 353 | struct Dwarf_Die_Chain *root, |
| 354 | int (*previsit) (unsigned int depth, |
| 355 | struct Dwarf_Die_Chain *, |
| 356 | void *arg), |
| 357 | int (*postvisit) (unsigned int depth, |
| 358 | struct Dwarf_Die_Chain *, |
| 359 | void *arg), |
Roland McGrath | 1ecb606 | 2005-08-15 09:53:04 +0000 | [diff] [blame] | 360 | void *arg) |
| 361 | __nonnull_attribute__ (2, 3) internal_function; |
| 362 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 363 | /* Return error code of last failing function call. This value is kept |
| 364 | separately for each thread. */ |
| 365 | extern int __dwarf_errno_internal (void); |
| 366 | |
| 367 | |
| 368 | /* Aliases to avoid PLTs. */ |
| 369 | INTDECL (dwarf_attr) |
| 370 | INTDECL (dwarf_attr_integrate) |
| 371 | INTDECL (dwarf_begin_elf) |
| 372 | INTDECL (dwarf_child) |
| 373 | INTDECL (dwarf_dieoffset) |
Roland McGrath | 71e15a0 | 2005-08-27 10:33:26 +0000 | [diff] [blame] | 374 | INTDECL (dwarf_diename) |
Roland McGrath | 4959bf8 | 2005-08-09 10:31:08 +0000 | [diff] [blame] | 375 | INTDECL (dwarf_end) |
| 376 | INTDECL (dwarf_errmsg) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 377 | INTDECL (dwarf_formaddr) |
| 378 | INTDECL (dwarf_formblock) |
| 379 | INTDECL (dwarf_formref) |
| 380 | INTDECL (dwarf_formref_die) |
| 381 | INTDECL (dwarf_formsdata) |
| 382 | INTDECL (dwarf_formstring) |
| 383 | INTDECL (dwarf_formudata) |
| 384 | INTDECL (dwarf_getarange_addr) |
| 385 | INTDECL (dwarf_getarangeinfo) |
| 386 | INTDECL (dwarf_getaranges) |
| 387 | INTDECL (dwarf_getsrcfiles) |
| 388 | INTDECL (dwarf_getsrclines) |
| 389 | INTDECL (dwarf_hasattr) |
| 390 | INTDECL (dwarf_haschildren) |
| 391 | INTDECL (dwarf_haspc) |
| 392 | INTDECL (dwarf_highpc) |
| 393 | INTDECL (dwarf_lowpc) |
| 394 | INTDECL (dwarf_nextcu) |
| 395 | INTDECL (dwarf_offdie) |
| 396 | INTDECL (dwarf_siblingof) |
| 397 | INTDECL (dwarf_tag) |
| 398 | |
| 399 | #endif /* libdwP.h */ |