1 /* Internal definitions for libdwarf.
2 Copyright (C) 2002-2011 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
6 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.
10 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.
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
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.
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>. */
60 /* gettext helper macros. */
61 #define _(Str) dgettext ("elfutils", Str)
64 /* Known location expressions already decoded. */
72 /* Known DW_OP_implicit_value blocks already decoded.
73 This overlaps struct loc_s exactly, but only the
74 first member really has to match. */
82 /* Valid indeces for the section data. */
104 DWARF_E_UNKNOWN_ERROR,
105 DWARF_E_INVALID_ACCESS,
111 DWARF_E_GETEHDR_ERROR,
115 DWARF_E_INVALID_VERSION,
116 DWARF_E_INVALID_FILE,
118 DWARF_E_INVALID_DWARF,
122 DWARF_E_NO_REFERENCE,
123 DWARF_E_INVALID_REFERENCE,
124 DWARF_E_NO_DEBUG_LINE,
125 DWARF_E_INVALID_DEBUG_LINE,
128 DWARF_E_INVALID_DIR_IDX,
129 DWARF_E_ADDR_OUTOFRANGE,
132 DWARF_E_INVALID_LINE_IDX,
133 DWARF_E_INVALID_ARANGE_IDX,
136 DWARF_E_INVALID_OFFSET,
137 DWARF_E_NO_DEBUG_RANGES,
142 #include "dwarf_sig8_hash.h"
144 /* This is the structure representing the debugging state. */
147 /* The underlying ELF file. */
150 /* The section data. */
151 Elf_Data *sectiondata[IDX_last];
154 /* The 1 << N bit is set if sectiondata[N] is malloc'd decompressed data. */
155 unsigned int sectiondata_gzip_mask:IDX_last;
158 /* True if the file has a byte order different from the host. */
159 bool other_byte_order;
161 /* If true, we allocated the ELF descriptor ourselves. */
164 /* Information for traversing the .debug_pubnames section. This is
165 an array and separately allocated with malloc. */
170 unsigned int cu_header_size;
173 size_t pubnames_nsets;
175 /* Search tree for the CUs. */
177 Dwarf_Off next_cu_offset;
179 /* Search tree and sig8 hash table for .debug_types type units. */
181 Dwarf_Off next_tu_offset;
182 Dwarf_Sig8_Hash sig8_hash;
184 /* Address ranges. */
185 Dwarf_Aranges *aranges;
187 /* Cached info from the CFI section. */
188 struct Dwarf_CFI_s *cfi;
190 /* Internal memory handling. This is basically a simplified
191 reimplementation of obstacks. Unfortunately the standard obstack
192 implementation is not usable in libraries. */
193 struct libdw_memblock
197 struct libdw_memblock *prev;
201 /* Default size of allocated memory blocks. */
202 size_t mem_default_size;
204 /* Registered OOM handler. */
205 Dwarf_OOM oom_handler;
209 /* Abbreviation representation. */
213 unsigned char *attrp;
214 unsigned int attrcnt;
220 #include "dwarf_abbrev_hash.h"
223 /* Files in line information records. */
229 struct Dwarf_Fileinfo_s
235 /* nfiles of those, followed by char *[ndirs]. */
237 typedef struct Dwarf_Fileinfo_s Dwarf_Fileinfo;
240 /* Representation of a row in the line table. */
249 unsigned short int column;
250 unsigned int is_stmt:1;
251 unsigned int basic_block:1;
252 unsigned int end_sequence:1;
253 unsigned int prologue_end:1;
254 unsigned int epilogue_begin:1;
255 /* The remaining bit fields are not flags, but hold values presumed to be
256 small. All the flags and other bit fields should add up to 48 bits
257 to give the whole struct a nice round size. */
258 unsigned int op_index:8;
260 unsigned int discriminator:24;
266 struct Dwarf_Line_s info[0];
269 /* Representation of address ranges. */
270 struct Dwarf_Aranges_s
275 struct Dwarf_Arange_s
284 /* CU representation. */
290 uint8_t address_size;
294 /* Zero if this is a normal CU. Nonzero if it is a type unit. */
298 /* Hash table for the abbreviations. */
299 Dwarf_Abbrev_Hash abbrev_hash;
300 /* Offset of the first abbreviation. */
301 size_t orig_abbrev_offset;
302 /* Offset past last read abbreviation. */
303 size_t last_abbrev_offset;
305 /* The srcline information. */
308 /* The source file information. */
311 /* Known location lists. */
315 /* Compute the offset of a CU's first DIE from its offset. This
318 4-bytes + 2-bytes + 4-bytes + 1-byte for 32-bit dwarf
319 12-bytes + 2-bytes + 8-bytes + 1-byte for 64-bit dwarf
320 or in .debug_types, SIGNATURE TYPE-OFFSET
321 4-bytes + 2-bytes + 4-bytes + 1-byte + 8-bytes + 4-bytes for 32-bit
322 12-bytes + 2-bytes + 8-bytes + 1-byte + 8-bytes + 8-bytes for 64-bit
324 Note the trick in the computation. If the offset_size is 4
325 the '- 4' term changes the '3 *' into a '2 *'. If the
326 offset_size is 8 it accounts for the 4-byte escape value
327 used at the start of the length. */
328 #define DIE_OFFSET_FROM_CU_OFFSET(cu_offset, offset_size, type_unit) \
329 ((type_unit) ? ((cu_offset) + 4 * (offset_size) - 4 + 3 + 8) \
330 : ((cu_offset) + 3 * (offset_size) - 4 + 3))
332 #define CUDIE(fromcu) \
336 .addr = ((char *) cu_data (fromcu)->d_buf \
337 + DIE_OFFSET_FROM_CU_OFFSET ((fromcu)->start, \
338 (fromcu)->offset_size, \
339 (fromcu)->type_offset != 0)) \
343 /* Macro information. */
356 /* We have to include the file at this point because the inline
357 functions access internals of the Dwarf structure. */
358 #include "memory-access.h"
361 /* Set error value. */
362 extern void __libdw_seterrno (int value) internal_function;
365 /* Memory handling, the easy parts. This macro does not do any locking. */
366 #define libdw_alloc(dbg, type, tsize, cnt) \
367 ({ struct libdw_memblock *_tail = (dbg)->mem_tail; \
368 size_t _required = (tsize) * (cnt); \
369 type *_result = (type *) (_tail->mem + (_tail->size - _tail->remaining));\
370 size_t _padding = ((__alignof (type) \
371 - ((uintptr_t) _result & (__alignof (type) - 1))) \
372 & (__alignof (type) - 1)); \
373 if (unlikely (_tail->remaining < _required + _padding)) \
374 _result = (type *) __libdw_allocate (dbg, _required, __alignof (type));\
377 _required += _padding; \
378 _result = (type *) ((char *) _result + _padding); \
379 _tail->remaining -= _required; \
383 #define libdw_typed_alloc(dbg, type) \
384 libdw_alloc (dbg, type, sizeof (type), 1)
386 /* Callback to allocate more. */
387 extern void *__libdw_allocate (Dwarf *dbg, size_t minsize, size_t align)
388 __attribute__ ((__malloc__)) __nonnull_attribute__ (1);
390 /* Default OOM handler. */
391 extern void __libdw_oom (void) __attribute ((noreturn, visibility ("hidden")));
394 extern void __libdw_free_zdata (Dwarf *dwarf) internal_function;
396 # define __libdw_free_zdata(dwarf) ((void) (dwarf))
399 /* Allocate the internal data for a unit not seen before. */
400 extern struct Dwarf_CU *__libdw_intern_next_unit (Dwarf *dbg, bool debug_types)
401 __nonnull_attribute__ (1) internal_function;
403 /* Find CU for given offset. */
404 extern struct Dwarf_CU *__libdw_findcu (Dwarf *dbg, Dwarf_Off offset, bool tu)
405 __nonnull_attribute__ (1) internal_function;
407 /* Return tag of given DIE. */
408 extern Dwarf_Abbrev *__libdw_findabbrev (struct Dwarf_CU *cu,
410 __nonnull_attribute__ (1) internal_function;
412 /* Get abbreviation at given offset. */
413 extern Dwarf_Abbrev *__libdw_getabbrev (Dwarf *dbg, struct Dwarf_CU *cu,
414 Dwarf_Off offset, size_t *lengthp,
415 Dwarf_Abbrev *result)
416 __nonnull_attribute__ (1) internal_function;
418 /* Helper functions for form handling. */
419 extern size_t __libdw_form_val_len (Dwarf *dbg, struct Dwarf_CU *cu,
421 const unsigned char *valp)
422 __nonnull_attribute__ (1, 2, 4) internal_function;
424 /* Helper function for DW_FORM_ref* handling. */
425 extern int __libdw_formref (Dwarf_Attribute *attr, Dwarf_Off *return_offset)
426 __nonnull_attribute__ (1, 2) internal_function;
429 /* Helper function to locate attribute. */
430 extern unsigned char *__libdw_find_attr (Dwarf_Die *die,
431 unsigned int search_name,
434 __nonnull_attribute__ (1) internal_function;
436 /* Helper function to access integer attribute. */
437 extern int __libdw_attr_intval (Dwarf_Die *die, int *valp, int attval)
438 __nonnull_attribute__ (1, 2) internal_function;
440 /* Helper function to walk scopes. */
441 struct Dwarf_Die_Chain
444 struct Dwarf_Die_Chain *parent;
445 bool prune; /* The PREVISIT function can set this. */
447 extern int __libdw_visit_scopes (unsigned int depth,
448 struct Dwarf_Die_Chain *root,
449 int (*previsit) (unsigned int depth,
450 struct Dwarf_Die_Chain *,
452 int (*postvisit) (unsigned int depth,
453 struct Dwarf_Die_Chain *,
456 __nonnull_attribute__ (2, 3) internal_function;
458 /* Parse a DWARF Dwarf_Block into an array of Dwarf_Op's,
459 and cache the result (via tsearch). */
460 extern int __libdw_intern_expression (Dwarf *dbg,
461 bool other_byte_order,
462 unsigned int address_size,
463 unsigned int ref_size,
464 void **cache, const Dwarf_Block *block,
465 bool cfap, bool valuep,
466 Dwarf_Op **llbuf, size_t *listlen,
468 __nonnull_attribute__ (5, 6, 9, 10) internal_function;
470 extern Dwarf_Die *__libdw_offdie (Dwarf *dbg, Dwarf_Off offset,
471 Dwarf_Die *result, bool debug_types)
475 /* Return error code of last failing function call. This value is kept
476 separately for each thread. */
477 extern int __dwarf_errno_internal (void);
482 /* Relocation hooks return -1 on error (in that case the error code
483 must already have been set), 0 if there is no relocation and 1 if a
484 relocation was present.*/
487 __libdw_relocate_address (Dwarf *dbg __attribute__ ((unused)),
488 int sec_index __attribute__ ((unused)),
489 const void *addr __attribute__ ((unused)),
490 int width __attribute__ ((unused)),
491 Dwarf_Addr *val __attribute__ ((unused)))
497 __libdw_relocate_offset (Dwarf *dbg __attribute__ ((unused)),
498 int sec_index __attribute__ ((unused)),
499 const void *addr __attribute__ ((unused)),
500 int width __attribute__ ((unused)),
501 Dwarf_Off *val __attribute__ ((unused)))
506 static inline Elf_Data *
507 __libdw_checked_get_data (Dwarf *dbg, int sec_index)
509 Elf_Data *data = dbg->sectiondata[sec_index];
510 if (unlikely (data == NULL)
511 || unlikely (data->d_buf == NULL))
513 __libdw_seterrno (DWARF_E_INVALID_DWARF);
520 __libdw_offset_in_section (Dwarf *dbg, int sec_index,
521 Dwarf_Off offset, size_t size)
523 Elf_Data *data = __libdw_checked_get_data (dbg, sec_index);
526 if (unlikely (offset > data->d_size)
527 || unlikely (data->d_size - offset < size))
529 __libdw_seterrno (DWARF_E_INVALID_OFFSET);
537 __libdw_in_section (Dwarf *dbg, int sec_index,
538 const void *addr, size_t size)
540 Elf_Data *data = __libdw_checked_get_data (dbg, sec_index);
543 if (unlikely (addr < data->d_buf)
544 || unlikely (data->d_size - (addr - data->d_buf) < size))
546 __libdw_seterrno (DWARF_E_INVALID_OFFSET);
553 #define READ_AND_RELOCATE(RELOC_HOOK, VAL) \
555 if (!__libdw_in_section (dbg, sec_index, addr, width)) \
558 const unsigned char *orig_addr = addr; \
560 VAL = read_4ubyte_unaligned_inc (dbg, addr); \
562 VAL = read_8ubyte_unaligned_inc (dbg, addr); \
564 int status = RELOC_HOOK (dbg, sec_index, orig_addr, width, &VAL); \
571 __libdw_read_address_inc (Dwarf *dbg,
572 int sec_index, const unsigned char **addrp,
573 int width, Dwarf_Addr *ret)
575 const unsigned char *addr = *addrp;
576 READ_AND_RELOCATE (__libdw_relocate_address, (*ret));
582 __libdw_read_address (Dwarf *dbg,
583 int sec_index, const unsigned char *addr,
584 int width, Dwarf_Addr *ret)
586 READ_AND_RELOCATE (__libdw_relocate_address, (*ret));
591 __libdw_read_offset_inc (Dwarf *dbg,
592 int sec_index, const unsigned char **addrp,
593 int width, Dwarf_Off *ret, int sec_ret,
596 const unsigned char *addr = *addrp;
597 READ_AND_RELOCATE (__libdw_relocate_offset, (*ret));
599 return __libdw_offset_in_section (dbg, sec_ret, *ret, size);
603 __libdw_read_offset (Dwarf *dbg,
604 int sec_index, const unsigned char *addr,
605 int width, Dwarf_Off *ret, int sec_ret,
608 READ_AND_RELOCATE (__libdw_relocate_offset, (*ret));
609 return __libdw_offset_in_section (dbg, sec_ret, *ret, size);
613 cu_sec_idx (struct Dwarf_CU *cu)
615 return cu->type_offset == 0 ? IDX_debug_info : IDX_debug_types;
618 static inline Elf_Data *
619 cu_data (struct Dwarf_CU *cu)
621 return cu->dbg->sectiondata[cu_sec_idx (cu)];
624 /* Read up begin/end pair and increment read pointer.
625 - If it's normal range record, set up *BEGINP and *ENDP and return 0.
626 - If it's base address selection record, set up *BASEP and return 1.
627 - If it's end of rangelist, don't set anything and return 2
628 - If an error occurs, don't set anything and return <0. */
629 int __libdw_read_begin_end_pair_inc (Dwarf *dbg, int sec_index,
630 unsigned char **addr, int width,
631 Dwarf_Addr *beginp, Dwarf_Addr *endp,
635 unsigned char * __libdw_formptr (Dwarf_Attribute *attr, int sec_index,
636 int err_nodata, unsigned char **endpp,
642 /* Aliases to avoid PLTs. */
643 INTDECL (dwarf_aggregate_size)
645 INTDECL (dwarf_attr_integrate)
646 INTDECL (dwarf_begin_elf)
647 INTDECL (dwarf_child)
648 INTDECL (dwarf_dieoffset)
649 INTDECL (dwarf_diename)
651 INTDECL (dwarf_entrypc)
652 INTDECL (dwarf_errmsg)
653 INTDECL (dwarf_formaddr)
654 INTDECL (dwarf_formblock)
655 INTDECL (dwarf_formref_die)
656 INTDECL (dwarf_formsdata)
657 INTDECL (dwarf_formstring)
658 INTDECL (dwarf_formudata)
659 INTDECL (dwarf_getarange_addr)
660 INTDECL (dwarf_getarangeinfo)
661 INTDECL (dwarf_getaranges)
662 INTDECL (dwarf_getsrcfiles)
663 INTDECL (dwarf_getsrclines)
664 INTDECL (dwarf_hasattr)
665 INTDECL (dwarf_haschildren)
666 INTDECL (dwarf_haspc)
667 INTDECL (dwarf_highpc)
668 INTDECL (dwarf_lowpc)
669 INTDECL (dwarf_nextcu)
670 INTDECL (dwarf_next_unit)
671 INTDECL (dwarf_offdie)
672 INTDECL (dwarf_ranges)
673 INTDECL (dwarf_siblingof)
674 INTDECL (dwarf_srclang)
677 #endif /* libdwP.h */