1 /* Internal definitions for libdwarf.
2 Copyright (C) 2002-2011, 2013, 2014 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
19 or both in parallel, as here.
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
40 /* gettext helper macros. */
41 #define _(Str) dgettext ("elfutils", Str)
44 /* Known location expressions already decoded. */
52 /* Known DW_OP_implicit_value blocks already decoded.
53 This overlaps struct loc_s exactly, but only the
54 first member really has to match. */
62 /* Valid indeces for the section data. */
86 DWARF_E_UNKNOWN_ERROR,
87 DWARF_E_INVALID_ACCESS,
93 DWARF_E_GETEHDR_ERROR,
97 DWARF_E_INVALID_VERSION,
100 DWARF_E_INVALID_DWARF,
104 DWARF_E_NO_REFERENCE,
105 DWARF_E_INVALID_REFERENCE,
106 DWARF_E_NO_DEBUG_LINE,
107 DWARF_E_INVALID_DEBUG_LINE,
110 DWARF_E_INVALID_DIR_IDX,
111 DWARF_E_ADDR_OUTOFRANGE,
114 DWARF_E_INVALID_LINE_IDX,
115 DWARF_E_INVALID_ARANGE_IDX,
118 DWARF_E_INVALID_OFFSET,
119 DWARF_E_NO_DEBUG_RANGES,
121 DWARF_E_NO_ALT_DEBUGLINK
125 #include "dwarf_sig8_hash.h"
127 /* This is the structure representing the debugging state. */
130 /* The underlying ELF file. */
133 /* dwz alternate DWARF file. */
136 /* The section data. */
137 Elf_Data *sectiondata[IDX_last];
140 /* The 1 << N bit is set if sectiondata[N] is malloc'd decompressed data. */
141 unsigned int sectiondata_gzip_mask:IDX_last;
144 /* True if the file has a byte order different from the host. */
145 bool other_byte_order;
147 /* If true, we allocated the ELF descriptor ourselves. */
150 /* Information for traversing the .debug_pubnames section. This is
151 an array and separately allocated with malloc. */
156 unsigned int cu_header_size;
159 size_t pubnames_nsets;
161 /* Search tree for the CUs. */
163 Dwarf_Off next_cu_offset;
165 /* Search tree and sig8 hash table for .debug_types type units. */
167 Dwarf_Off next_tu_offset;
168 Dwarf_Sig8_Hash sig8_hash;
170 /* Address ranges. */
171 Dwarf_Aranges *aranges;
173 /* Cached info from the CFI section. */
174 struct Dwarf_CFI_s *cfi;
176 /* Internal memory handling. This is basically a simplified
177 reimplementation of obstacks. Unfortunately the standard obstack
178 implementation is not usable in libraries. */
179 struct libdw_memblock
183 struct libdw_memblock *prev;
187 /* Default size of allocated memory blocks. */
188 size_t mem_default_size;
190 /* Registered OOM handler. */
191 Dwarf_OOM oom_handler;
195 /* Abbreviation representation. */
199 unsigned char *attrp;
200 unsigned int attrcnt;
206 #include "dwarf_abbrev_hash.h"
209 /* Files in line information records. */
214 struct Dwarf_Fileinfo_s
220 /* nfiles of those, followed by char *[ndirs]. */
222 typedef struct Dwarf_Fileinfo_s Dwarf_Fileinfo;
225 /* Representation of a row in the line table. */
234 unsigned short int column;
235 unsigned int is_stmt:1;
236 unsigned int basic_block:1;
237 unsigned int end_sequence:1;
238 unsigned int prologue_end:1;
239 unsigned int epilogue_begin:1;
240 /* The remaining bit fields are not flags, but hold values presumed to be
241 small. All the flags and other bit fields should add up to 48 bits
242 to give the whole struct a nice round size. */
243 unsigned int op_index:8;
245 unsigned int discriminator:24;
251 struct Dwarf_Line_s info[0];
254 /* Representation of address ranges. */
255 struct Dwarf_Aranges_s
260 struct Dwarf_Arange_s
269 /* CU representation. */
275 uint8_t address_size;
279 /* Zero if this is a normal CU. Nonzero if it is a type unit. */
283 /* Hash table for the abbreviations. */
284 Dwarf_Abbrev_Hash abbrev_hash;
285 /* Offset of the first abbreviation. */
286 size_t orig_abbrev_offset;
287 /* Offset past last read abbreviation. */
288 size_t last_abbrev_offset;
290 /* The srcline information. */
293 /* The source file information. */
296 /* Known location lists. */
300 /* Compute the offset of a CU's first DIE from its offset. This
303 4-bytes + 2-bytes + 4-bytes + 1-byte for 32-bit dwarf
304 12-bytes + 2-bytes + 8-bytes + 1-byte for 64-bit dwarf
305 or in .debug_types, SIGNATURE TYPE-OFFSET
306 4-bytes + 2-bytes + 4-bytes + 1-byte + 8-bytes + 4-bytes for 32-bit
307 12-bytes + 2-bytes + 8-bytes + 1-byte + 8-bytes + 8-bytes for 64-bit
309 Note the trick in the computation. If the offset_size is 4
310 the '- 4' term changes the '3 *' into a '2 *'. If the
311 offset_size is 8 it accounts for the 4-byte escape value
312 used at the start of the length. */
313 #define DIE_OFFSET_FROM_CU_OFFSET(cu_offset, offset_size, type_unit) \
314 ((type_unit) ? ((cu_offset) + 4 * (offset_size) - 4 + 3 + 8) \
315 : ((cu_offset) + 3 * (offset_size) - 4 + 3))
317 #define CUDIE(fromcu) \
321 .addr = ((char *) cu_data (fromcu)->d_buf \
322 + DIE_OFFSET_FROM_CU_OFFSET ((fromcu)->start, \
323 (fromcu)->offset_size, \
324 (fromcu)->type_offset != 0)) \
328 /* Macro information. */
341 /* We have to include the file at this point because the inline
342 functions access internals of the Dwarf structure. */
343 #include "memory-access.h"
346 /* Set error value. */
347 extern void __libdw_seterrno (int value) internal_function;
350 /* Memory handling, the easy parts. This macro does not do any locking. */
351 #define libdw_alloc(dbg, type, tsize, cnt) \
352 ({ struct libdw_memblock *_tail = (dbg)->mem_tail; \
353 size_t _required = (tsize) * (cnt); \
354 type *_result = (type *) (_tail->mem + (_tail->size - _tail->remaining));\
355 size_t _padding = ((__alignof (type) \
356 - ((uintptr_t) _result & (__alignof (type) - 1))) \
357 & (__alignof (type) - 1)); \
358 if (unlikely (_tail->remaining < _required + _padding)) \
359 _result = (type *) __libdw_allocate (dbg, _required, __alignof (type));\
362 _required += _padding; \
363 _result = (type *) ((char *) _result + _padding); \
364 _tail->remaining -= _required; \
368 #define libdw_typed_alloc(dbg, type) \
369 libdw_alloc (dbg, type, sizeof (type), 1)
371 /* Callback to allocate more. */
372 extern void *__libdw_allocate (Dwarf *dbg, size_t minsize, size_t align)
373 __attribute__ ((__malloc__)) __nonnull_attribute__ (1);
375 /* Default OOM handler. */
376 extern void __libdw_oom (void) __attribute ((noreturn, visibility ("hidden")));
379 extern void __libdw_free_zdata (Dwarf *dwarf) internal_function;
381 # define __libdw_free_zdata(dwarf) ((void) (dwarf))
384 /* Allocate the internal data for a unit not seen before. */
385 extern struct Dwarf_CU *__libdw_intern_next_unit (Dwarf *dbg, bool debug_types)
386 __nonnull_attribute__ (1) internal_function;
388 /* Find CU for given offset. */
389 extern struct Dwarf_CU *__libdw_findcu (Dwarf *dbg, Dwarf_Off offset, bool tu)
390 __nonnull_attribute__ (1) internal_function;
392 /* Return tag of given DIE. */
393 extern Dwarf_Abbrev *__libdw_findabbrev (struct Dwarf_CU *cu,
395 __nonnull_attribute__ (1) internal_function;
397 /* Get abbreviation at given offset. */
398 extern Dwarf_Abbrev *__libdw_getabbrev (Dwarf *dbg, struct Dwarf_CU *cu,
399 Dwarf_Off offset, size_t *lengthp,
400 Dwarf_Abbrev *result)
401 __nonnull_attribute__ (1) internal_function;
403 /* Helper functions for form handling. */
404 extern size_t __libdw_form_val_compute_len (Dwarf *dbg, struct Dwarf_CU *cu,
406 const unsigned char *valp)
407 __nonnull_attribute__ (1, 2, 4) internal_function;
409 /* Find the length of a form attribute. */
411 __nonnull_attribute__ (1, 2, 4)
412 __libdw_form_val_len (Dwarf *dbg, struct Dwarf_CU *cu,
413 unsigned int form, const unsigned char *valp)
415 /* Small lookup table of forms with fixed lengths. Absent indexes are
416 initialized 0, so any truly desired 0 is set to 0x80 and masked. */
417 static const uint8_t form_lengths[] =
419 [DW_FORM_flag_present] = 0x80,
420 [DW_FORM_data1] = 1, [DW_FORM_ref1] = 1, [DW_FORM_flag] = 1,
421 [DW_FORM_data2] = 2, [DW_FORM_ref2] = 2,
422 [DW_FORM_data4] = 4, [DW_FORM_ref4] = 4,
423 [DW_FORM_data8] = 8, [DW_FORM_ref8] = 8, [DW_FORM_ref_sig8] = 8,
426 /* Return immediately for forms with fixed lengths. */
427 if (form < sizeof form_lengths / sizeof form_lengths[0])
429 uint8_t len = form_lengths[form];
431 return len & 0x7f; /* Mask to allow 0x80 -> 0. */
434 /* Other forms require some computation. */
435 return __libdw_form_val_compute_len (dbg, cu, form, valp);
438 /* Helper function for DW_FORM_ref* handling. */
439 extern int __libdw_formref (Dwarf_Attribute *attr, Dwarf_Off *return_offset)
440 __nonnull_attribute__ (1, 2) internal_function;
443 /* Helper function to locate attribute. */
444 extern unsigned char *__libdw_find_attr (Dwarf_Die *die,
445 unsigned int search_name,
448 __nonnull_attribute__ (1) internal_function;
450 /* Helper function to access integer attribute. */
451 extern int __libdw_attr_intval (Dwarf_Die *die, int *valp, int attval)
452 __nonnull_attribute__ (1, 2) internal_function;
454 /* Helper function to walk scopes. */
455 struct Dwarf_Die_Chain
458 struct Dwarf_Die_Chain *parent;
459 bool prune; /* The PREVISIT function can set this. */
461 extern int __libdw_visit_scopes (unsigned int depth,
462 struct Dwarf_Die_Chain *root,
463 int (*previsit) (unsigned int depth,
464 struct Dwarf_Die_Chain *,
466 int (*postvisit) (unsigned int depth,
467 struct Dwarf_Die_Chain *,
470 __nonnull_attribute__ (2, 3) internal_function;
472 /* Parse a DWARF Dwarf_Block into an array of Dwarf_Op's,
473 and cache the result (via tsearch). */
474 extern int __libdw_intern_expression (Dwarf *dbg,
475 bool other_byte_order,
476 unsigned int address_size,
477 unsigned int ref_size,
478 void **cache, const Dwarf_Block *block,
479 bool cfap, bool valuep,
480 Dwarf_Op **llbuf, size_t *listlen,
482 __nonnull_attribute__ (5, 6, 9, 10) internal_function;
484 extern Dwarf_Die *__libdw_offdie (Dwarf *dbg, Dwarf_Off offset,
485 Dwarf_Die *result, bool debug_types)
489 /* Return error code of last failing function call. This value is kept
490 separately for each thread. */
491 extern int __dwarf_errno_internal (void);
496 /* Relocation hooks return -1 on error (in that case the error code
497 must already have been set), 0 if there is no relocation and 1 if a
498 relocation was present.*/
501 __libdw_relocate_address (Dwarf *dbg __attribute__ ((unused)),
502 int sec_index __attribute__ ((unused)),
503 const void *addr __attribute__ ((unused)),
504 int width __attribute__ ((unused)),
505 Dwarf_Addr *val __attribute__ ((unused)))
511 __libdw_relocate_offset (Dwarf *dbg __attribute__ ((unused)),
512 int sec_index __attribute__ ((unused)),
513 const void *addr __attribute__ ((unused)),
514 int width __attribute__ ((unused)),
515 Dwarf_Off *val __attribute__ ((unused)))
520 static inline Elf_Data *
521 __libdw_checked_get_data (Dwarf *dbg, int sec_index)
523 Elf_Data *data = dbg->sectiondata[sec_index];
524 if (unlikely (data == NULL)
525 || unlikely (data->d_buf == NULL))
527 __libdw_seterrno (DWARF_E_INVALID_DWARF);
534 __libdw_offset_in_section (Dwarf *dbg, int sec_index,
535 Dwarf_Off offset, size_t size)
537 Elf_Data *data = __libdw_checked_get_data (dbg, sec_index);
540 if (unlikely (offset > data->d_size)
541 || unlikely (data->d_size - offset < size))
543 __libdw_seterrno (DWARF_E_INVALID_OFFSET);
551 __libdw_in_section (Dwarf *dbg, int sec_index,
552 const void *addr, size_t size)
554 Elf_Data *data = __libdw_checked_get_data (dbg, sec_index);
557 if (unlikely (addr < data->d_buf)
558 || unlikely (data->d_size - (addr - data->d_buf) < size))
560 __libdw_seterrno (DWARF_E_INVALID_OFFSET);
567 #define READ_AND_RELOCATE(RELOC_HOOK, VAL) \
569 if (!__libdw_in_section (dbg, sec_index, addr, width)) \
572 const unsigned char *orig_addr = addr; \
574 VAL = read_4ubyte_unaligned_inc (dbg, addr); \
576 VAL = read_8ubyte_unaligned_inc (dbg, addr); \
578 int status = RELOC_HOOK (dbg, sec_index, orig_addr, width, &VAL); \
585 __libdw_read_address_inc (Dwarf *dbg,
586 int sec_index, const unsigned char **addrp,
587 int width, Dwarf_Addr *ret)
589 const unsigned char *addr = *addrp;
590 READ_AND_RELOCATE (__libdw_relocate_address, (*ret));
596 __libdw_read_address (Dwarf *dbg,
597 int sec_index, const unsigned char *addr,
598 int width, Dwarf_Addr *ret)
600 READ_AND_RELOCATE (__libdw_relocate_address, (*ret));
605 __libdw_read_offset_inc (Dwarf *dbg,
606 int sec_index, const unsigned char **addrp,
607 int width, Dwarf_Off *ret, int sec_ret,
610 const unsigned char *addr = *addrp;
611 READ_AND_RELOCATE (__libdw_relocate_offset, (*ret));
613 return __libdw_offset_in_section (dbg, sec_ret, *ret, size);
617 __libdw_read_offset (Dwarf *dbg, Dwarf *dbg_ret,
618 int sec_index, const unsigned char *addr,
619 int width, Dwarf_Off *ret, int sec_ret,
622 READ_AND_RELOCATE (__libdw_relocate_offset, (*ret));
623 return __libdw_offset_in_section (dbg_ret, sec_ret, *ret, size);
627 cu_sec_idx (struct Dwarf_CU *cu)
629 return cu->type_offset == 0 ? IDX_debug_info : IDX_debug_types;
632 static inline Elf_Data *
633 cu_data (struct Dwarf_CU *cu)
635 return cu->dbg->sectiondata[cu_sec_idx (cu)];
638 /* Read up begin/end pair and increment read pointer.
639 - If it's normal range record, set up *BEGINP and *ENDP and return 0.
640 - If it's base address selection record, set up *BASEP and return 1.
641 - If it's end of rangelist, don't set anything and return 2
642 - If an error occurs, don't set anything and return <0. */
643 int __libdw_read_begin_end_pair_inc (Dwarf *dbg, int sec_index,
644 unsigned char **addr, int width,
645 Dwarf_Addr *beginp, Dwarf_Addr *endp,
649 unsigned char * __libdw_formptr (Dwarf_Attribute *attr, int sec_index,
650 int err_nodata, unsigned char **endpp,
654 /* Fills in the given attribute to point at an empty location expression. */
655 void __libdw_empty_loc_attr (Dwarf_Attribute *attr, struct Dwarf_CU *cu)
659 /* Aliases to avoid PLTs. */
660 INTDECL (dwarf_aggregate_size)
662 INTDECL (dwarf_attr_integrate)
663 INTDECL (dwarf_begin)
664 INTDECL (dwarf_begin_elf)
665 INTDECL (dwarf_child)
666 INTDECL (dwarf_dieoffset)
667 INTDECL (dwarf_diename)
669 INTDECL (dwarf_entrypc)
670 INTDECL (dwarf_errmsg)
671 INTDECL (dwarf_formaddr)
672 INTDECL (dwarf_formblock)
673 INTDECL (dwarf_formref_die)
674 INTDECL (dwarf_formsdata)
675 INTDECL (dwarf_formstring)
676 INTDECL (dwarf_formudata)
677 INTDECL (dwarf_getalt)
678 INTDECL (dwarf_getarange_addr)
679 INTDECL (dwarf_getarangeinfo)
680 INTDECL (dwarf_getaranges)
681 INTDECL (dwarf_getlocation_die)
682 INTDECL (dwarf_getsrcfiles)
683 INTDECL (dwarf_getsrclines)
684 INTDECL (dwarf_hasattr)
685 INTDECL (dwarf_haschildren)
686 INTDECL (dwarf_haspc)
687 INTDECL (dwarf_highpc)
688 INTDECL (dwarf_lowpc)
689 INTDECL (dwarf_nextcu)
690 INTDECL (dwarf_next_unit)
691 INTDECL (dwarf_offdie)
692 INTDECL (dwarf_peel_type)
693 INTDECL (dwarf_ranges)
694 INTDECL (dwarf_setalt)
695 INTDECL (dwarf_siblingof)
696 INTDECL (dwarf_srclang)
699 #endif /* libdwP.h */