Imported Upstream version 0.155
[platform/upstream/elfutils.git] / libdw / libdwP.h
1 /* Internal definitions for libdwarf.
2    Copyright (C) 2002-2011 Red Hat, Inc.
3    This file is part of elfutils.
4    Written by Ulrich Drepper <drepper@redhat.com>, 2002.
5
6    This file is free software; you can redistribute it and/or modify
7    it under the terms of either
8
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
12
13    or
14
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
18
19    or both in parallel, as here.
20
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.
25
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/>.  */
29
30 #ifndef _LIBDWP_H
31 #define _LIBDWP_H 1
32
33 #include <libintl.h>
34 #include <stdbool.h>
35
36 #include <libdw.h>
37
38
39 /* gettext helper macros.  */
40 #define _(Str) dgettext ("elfutils", Str)
41
42
43 /* Known location expressions already decoded.  */
44 struct loc_s
45 {
46   void *addr;
47   Dwarf_Op *loc;
48   size_t nloc;
49 };
50
51 /* Known DW_OP_implicit_value blocks already decoded.
52    This overlaps struct loc_s exactly, but only the
53    first member really has to match.  */
54 struct loc_block_s
55 {
56   void *addr;
57   unsigned char *data;
58   size_t length;
59 };
60
61 /* Valid indeces for the section data.  */
62 enum
63   {
64     IDX_debug_info = 0,
65     IDX_debug_types,
66     IDX_debug_abbrev,
67     IDX_debug_aranges,
68     IDX_debug_line,
69     IDX_debug_frame,
70     IDX_debug_loc,
71     IDX_debug_pubnames,
72     IDX_debug_str,
73     IDX_debug_macinfo,
74     IDX_debug_macro,
75     IDX_debug_ranges,
76     IDX_last
77   };
78
79
80 /* Error values.  */
81 enum
82 {
83   DWARF_E_NOERROR = 0,
84   DWARF_E_UNKNOWN_ERROR,
85   DWARF_E_INVALID_ACCESS,
86   DWARF_E_NO_REGFILE,
87   DWARF_E_IO_ERROR,
88   DWARF_E_INVALID_ELF,
89   DWARF_E_NO_DWARF,
90   DWARF_E_NOELF,
91   DWARF_E_GETEHDR_ERROR,
92   DWARF_E_NOMEM,
93   DWARF_E_UNIMPL,
94   DWARF_E_INVALID_CMD,
95   DWARF_E_INVALID_VERSION,
96   DWARF_E_INVALID_FILE,
97   DWARF_E_NO_ENTRY,
98   DWARF_E_INVALID_DWARF,
99   DWARF_E_NO_STRING,
100   DWARF_E_NO_ADDR,
101   DWARF_E_NO_CONSTANT,
102   DWARF_E_NO_REFERENCE,
103   DWARF_E_INVALID_REFERENCE,
104   DWARF_E_NO_DEBUG_LINE,
105   DWARF_E_INVALID_DEBUG_LINE,
106   DWARF_E_TOO_BIG,
107   DWARF_E_VERSION,
108   DWARF_E_INVALID_DIR_IDX,
109   DWARF_E_ADDR_OUTOFRANGE,
110   DWARF_E_NO_LOCLIST,
111   DWARF_E_NO_BLOCK,
112   DWARF_E_INVALID_LINE_IDX,
113   DWARF_E_INVALID_ARANGE_IDX,
114   DWARF_E_NO_MATCH,
115   DWARF_E_NO_FLAG,
116   DWARF_E_INVALID_OFFSET,
117   DWARF_E_NO_DEBUG_RANGES,
118   DWARF_E_INVALID_CFI,
119   DWARF_E_NO_ALT_DEBUGLINK
120 };
121
122
123 #include "dwarf_sig8_hash.h"
124
125 /* This is the structure representing the debugging state.  */
126 struct Dwarf
127 {
128   /* The underlying ELF file.  */
129   Elf *elf;
130
131   /* dwz alternate DWARF file.  */
132   Dwarf *alt_dwarf;
133
134   /* The section data.  */
135   Elf_Data *sectiondata[IDX_last];
136
137 #if USE_ZLIB
138   /* The 1 << N bit is set if sectiondata[N] is malloc'd decompressed data.  */
139   unsigned int sectiondata_gzip_mask:IDX_last;
140 #endif
141
142   /* True if the file has a byte order different from the host.  */
143   bool other_byte_order;
144
145   /* If true, we allocated the ELF descriptor ourselves.  */
146   bool free_elf;
147
148   /* If true, we allocated the Dwarf descriptor for alt_dwarf ourselves.  */
149   bool free_alt;
150
151   /* Information for traversing the .debug_pubnames section.  This is
152      an array and separately allocated with malloc.  */
153   struct pubnames_s
154   {
155     Dwarf_Off cu_offset;
156     Dwarf_Off set_start;
157     unsigned int cu_header_size;
158     int address_len;
159   } *pubnames_sets;
160   size_t pubnames_nsets;
161
162   /* Search tree for the CUs.  */
163   void *cu_tree;
164   Dwarf_Off next_cu_offset;
165
166   /* Search tree and sig8 hash table for .debug_types type units.  */
167   void *tu_tree;
168   Dwarf_Off next_tu_offset;
169   Dwarf_Sig8_Hash sig8_hash;
170
171   /* Address ranges.  */
172   Dwarf_Aranges *aranges;
173
174   /* Cached info from the CFI section.  */
175   struct Dwarf_CFI_s *cfi;
176
177   /* Internal memory handling.  This is basically a simplified
178      reimplementation of obstacks.  Unfortunately the standard obstack
179      implementation is not usable in libraries.  */
180   struct libdw_memblock
181   {
182     size_t size;
183     size_t remaining;
184     struct libdw_memblock *prev;
185     char mem[0];
186   } *mem_tail;
187
188   /* Default size of allocated memory blocks.  */
189   size_t mem_default_size;
190
191   /* Registered OOM handler.  */
192   Dwarf_OOM oom_handler;
193 };
194
195
196 /* Abbreviation representation.  */
197 struct Dwarf_Abbrev
198 {
199   Dwarf_Off offset;
200   unsigned char *attrp;
201   unsigned int attrcnt;
202   unsigned int code;
203   unsigned int tag;
204   bool has_children;
205 };
206
207 #include "dwarf_abbrev_hash.h"
208
209
210 /* Files in line information records.  */
211 struct Dwarf_Files_s
212   {
213     struct Dwarf_CU *cu;
214     unsigned int ndirs;
215     unsigned int nfiles;
216     struct Dwarf_Fileinfo_s
217     {
218       char *name;
219       Dwarf_Word mtime;
220       Dwarf_Word length;
221     } info[0];
222     /* nfiles of those, followed by char *[ndirs].  */
223   };
224 typedef struct Dwarf_Fileinfo_s Dwarf_Fileinfo;
225
226
227 /* Representation of a row in the line table.  */
228
229 struct Dwarf_Line_s
230 {
231   Dwarf_Files *files;
232
233   Dwarf_Addr addr;
234   unsigned int file;
235   int line;
236   unsigned short int column;
237   unsigned int is_stmt:1;
238   unsigned int basic_block:1;
239   unsigned int end_sequence:1;
240   unsigned int prologue_end:1;
241   unsigned int epilogue_begin:1;
242   /* The remaining bit fields are not flags, but hold values presumed to be
243      small.  All the flags and other bit fields should add up to 48 bits
244      to give the whole struct a nice round size.  */
245   unsigned int op_index:8;
246   unsigned int isa:8;
247   unsigned int discriminator:24;
248 };
249
250 struct Dwarf_Lines_s
251 {
252   size_t nlines;
253   struct Dwarf_Line_s info[0];
254 };
255
256 /* Representation of address ranges.  */
257 struct Dwarf_Aranges_s
258 {
259   Dwarf *dbg;
260   size_t naranges;
261
262   struct Dwarf_Arange_s
263   {
264     Dwarf_Addr addr;
265     Dwarf_Word length;
266     Dwarf_Off offset;
267   } info[0];
268 };
269
270
271 /* CU representation.  */
272 struct Dwarf_CU
273 {
274   Dwarf *dbg;
275   Dwarf_Off start;
276   Dwarf_Off end;
277   uint8_t address_size;
278   uint8_t offset_size;
279   uint16_t version;
280
281   /* Zero if this is a normal CU.  Nonzero if it is a type unit.  */
282   size_t type_offset;
283   uint64_t type_sig8;
284
285   /* Hash table for the abbreviations.  */
286   Dwarf_Abbrev_Hash abbrev_hash;
287   /* Offset of the first abbreviation.  */
288   size_t orig_abbrev_offset;
289   /* Offset past last read abbreviation.  */
290   size_t last_abbrev_offset;
291
292   /* The srcline information.  */
293   Dwarf_Lines *lines;
294
295   /* The source file information.  */
296   Dwarf_Files *files;
297
298   /* Known location lists.  */
299   void *locs;
300 };
301
302 /* Compute the offset of a CU's first DIE from its offset.  This
303    is either:
304         LEN       VER     OFFSET    ADDR
305       4-bytes + 2-bytes + 4-bytes + 1-byte  for 32-bit dwarf
306      12-bytes + 2-bytes + 8-bytes + 1-byte  for 64-bit dwarf
307    or in .debug_types,                       SIGNATURE TYPE-OFFSET
308       4-bytes + 2-bytes + 4-bytes + 1-byte + 8-bytes + 4-bytes  for 32-bit
309      12-bytes + 2-bytes + 8-bytes + 1-byte + 8-bytes + 8-bytes  for 64-bit
310
311    Note the trick in the computation.  If the offset_size is 4
312    the '- 4' term changes the '3 *' into a '2 *'.  If the
313    offset_size is 8 it accounts for the 4-byte escape value
314    used at the start of the length.  */
315 #define DIE_OFFSET_FROM_CU_OFFSET(cu_offset, offset_size, type_unit)    \
316   ((type_unit) ? ((cu_offset) + 4 * (offset_size) - 4 + 3 + 8)          \
317    : ((cu_offset) + 3 * (offset_size) - 4 + 3))
318
319 #define CUDIE(fromcu)                                                         \
320   ((Dwarf_Die)                                                                \
321    {                                                                          \
322      .cu = (fromcu),                                                          \
323      .addr = ((char *) cu_data (fromcu)->d_buf                                \
324               + DIE_OFFSET_FROM_CU_OFFSET ((fromcu)->start,                   \
325                                            (fromcu)->offset_size,             \
326                                            (fromcu)->type_offset != 0))       \
327    })                                                                         \
328
329
330 /* Macro information.  */
331 struct Dwarf_Macro_s
332 {
333   unsigned int opcode;
334   Dwarf_Word param1;
335   union
336   {
337     Dwarf_Word u;
338     const char *s;
339   } param2;
340 };
341
342
343 /* We have to include the file at this point because the inline
344    functions access internals of the Dwarf structure.  */
345 #include "memory-access.h"
346
347
348 /* Set error value.  */
349 extern void __libdw_seterrno (int value) internal_function;
350
351
352 /* Memory handling, the easy parts.  This macro does not do any locking.  */
353 #define libdw_alloc(dbg, type, tsize, cnt) \
354   ({ struct libdw_memblock *_tail = (dbg)->mem_tail;                          \
355      size_t _required = (tsize) * (cnt);                                      \
356      type *_result = (type *) (_tail->mem + (_tail->size - _tail->remaining));\
357      size_t _padding = ((__alignof (type)                                     \
358                          - ((uintptr_t) _result & (__alignof (type) - 1)))    \
359                         & (__alignof (type) - 1));                            \
360      if (unlikely (_tail->remaining < _required + _padding))                  \
361        _result = (type *) __libdw_allocate (dbg, _required, __alignof (type));\
362      else                                                                     \
363        {                                                                      \
364          _required += _padding;                                               \
365          _result = (type *) ((char *) _result + _padding);                    \
366          _tail->remaining -= _required;                                       \
367        }                                                                      \
368      _result; })
369
370 #define libdw_typed_alloc(dbg, type) \
371   libdw_alloc (dbg, type, sizeof (type), 1)
372
373 /* Callback to allocate more.  */
374 extern void *__libdw_allocate (Dwarf *dbg, size_t minsize, size_t align)
375      __attribute__ ((__malloc__)) __nonnull_attribute__ (1);
376
377 /* Default OOM handler.  */
378 extern void __libdw_oom (void) __attribute ((noreturn, visibility ("hidden")));
379
380 #if USE_ZLIB
381 extern void __libdw_free_zdata (Dwarf *dwarf) internal_function;
382 #else
383 # define __libdw_free_zdata(dwarf)      ((void) (dwarf))
384 #endif
385
386 /* Allocate the internal data for a unit not seen before.  */
387 extern struct Dwarf_CU *__libdw_intern_next_unit (Dwarf *dbg, bool debug_types)
388      __nonnull_attribute__ (1) internal_function;
389
390 /* Find CU for given offset.  */
391 extern struct Dwarf_CU *__libdw_findcu (Dwarf *dbg, Dwarf_Off offset, bool tu)
392      __nonnull_attribute__ (1) internal_function;
393
394 /* Return tag of given DIE.  */
395 extern Dwarf_Abbrev *__libdw_findabbrev (struct Dwarf_CU *cu,
396                                          unsigned int code)
397      __nonnull_attribute__ (1) internal_function;
398
399 /* Get abbreviation at given offset.  */
400 extern Dwarf_Abbrev *__libdw_getabbrev (Dwarf *dbg, struct Dwarf_CU *cu,
401                                         Dwarf_Off offset, size_t *lengthp,
402                                         Dwarf_Abbrev *result)
403      __nonnull_attribute__ (1) internal_function;
404
405 /* Helper functions for form handling.  */
406 extern size_t __libdw_form_val_len (Dwarf *dbg, struct Dwarf_CU *cu,
407                                     unsigned int form,
408                                     const unsigned char *valp)
409      __nonnull_attribute__ (1, 2, 4) internal_function;
410
411 /* Helper function for DW_FORM_ref* handling.  */
412 extern int __libdw_formref (Dwarf_Attribute *attr, Dwarf_Off *return_offset)
413      __nonnull_attribute__ (1, 2) internal_function;
414
415
416 /* Helper function to locate attribute.  */
417 extern unsigned char *__libdw_find_attr (Dwarf_Die *die,
418                                          unsigned int search_name,
419                                          unsigned int *codep,
420                                          unsigned int *formp)
421      __nonnull_attribute__ (1) internal_function;
422
423 /* Helper function to access integer attribute.  */
424 extern int __libdw_attr_intval (Dwarf_Die *die, int *valp, int attval)
425      __nonnull_attribute__ (1, 2) internal_function;
426
427 /* Helper function to walk scopes.  */
428 struct Dwarf_Die_Chain
429 {
430   Dwarf_Die die;
431   struct Dwarf_Die_Chain *parent;
432   bool prune;                   /* The PREVISIT function can set this.  */
433 };
434 extern int __libdw_visit_scopes (unsigned int depth,
435                                  struct Dwarf_Die_Chain *root,
436                                  int (*previsit) (unsigned int depth,
437                                                   struct Dwarf_Die_Chain *,
438                                                   void *arg),
439                                  int (*postvisit) (unsigned int depth,
440                                                    struct Dwarf_Die_Chain *,
441                                                    void *arg),
442                                  void *arg)
443   __nonnull_attribute__ (2, 3) internal_function;
444
445 /* Parse a DWARF Dwarf_Block into an array of Dwarf_Op's,
446    and cache the result (via tsearch).  */
447 extern int __libdw_intern_expression (Dwarf *dbg,
448                                       bool other_byte_order,
449                                       unsigned int address_size,
450                                       unsigned int ref_size,
451                                       void **cache, const Dwarf_Block *block,
452                                       bool cfap, bool valuep,
453                                       Dwarf_Op **llbuf, size_t *listlen,
454                                       int sec_index)
455   __nonnull_attribute__ (5, 6, 9, 10) internal_function;
456
457 extern Dwarf_Die *__libdw_offdie (Dwarf *dbg, Dwarf_Off offset,
458                                   Dwarf_Die *result, bool debug_types)
459   internal_function;
460
461
462 /* Return error code of last failing function call.  This value is kept
463    separately for each thread.  */
464 extern int __dwarf_errno_internal (void);
465
466
467 /* Reader hooks.  */
468
469 /* Relocation hooks return -1 on error (in that case the error code
470    must already have been set), 0 if there is no relocation and 1 if a
471    relocation was present.*/
472
473 static inline int
474 __libdw_relocate_address (Dwarf *dbg __attribute__ ((unused)),
475                           int sec_index __attribute__ ((unused)),
476                           const void *addr __attribute__ ((unused)),
477                           int width __attribute__ ((unused)),
478                           Dwarf_Addr *val __attribute__ ((unused)))
479 {
480   return 0;
481 }
482
483 static inline int
484 __libdw_relocate_offset (Dwarf *dbg __attribute__ ((unused)),
485                          int sec_index __attribute__ ((unused)),
486                          const void *addr __attribute__ ((unused)),
487                          int width __attribute__ ((unused)),
488                          Dwarf_Off *val __attribute__ ((unused)))
489 {
490   return 0;
491 }
492
493 static inline Elf_Data *
494 __libdw_checked_get_data (Dwarf *dbg, int sec_index)
495 {
496   Elf_Data *data = dbg->sectiondata[sec_index];
497   if (unlikely (data == NULL)
498       || unlikely (data->d_buf == NULL))
499     {
500       __libdw_seterrno (DWARF_E_INVALID_DWARF);
501       return NULL;
502     }
503   return data;
504 }
505
506 static inline int
507 __libdw_offset_in_section (Dwarf *dbg, int sec_index,
508                            Dwarf_Off offset, size_t size)
509 {
510   Elf_Data *data = __libdw_checked_get_data (dbg, sec_index);
511   if (data == NULL)
512     return -1;
513   if (unlikely (offset > data->d_size)
514       || unlikely (data->d_size - offset < size))
515     {
516       __libdw_seterrno (DWARF_E_INVALID_OFFSET);
517       return -1;
518     }
519
520   return 0;
521 }
522
523 static inline bool
524 __libdw_in_section (Dwarf *dbg, int sec_index,
525                     const void *addr, size_t size)
526 {
527   Elf_Data *data = __libdw_checked_get_data (dbg, sec_index);
528   if (data == NULL)
529     return false;
530   if (unlikely (addr < data->d_buf)
531       || unlikely (data->d_size - (addr - data->d_buf) < size))
532     {
533       __libdw_seterrno (DWARF_E_INVALID_OFFSET);
534       return false;
535     }
536
537   return true;
538 }
539
540 #define READ_AND_RELOCATE(RELOC_HOOK, VAL)                              \
541   ({                                                                    \
542     if (!__libdw_in_section (dbg, sec_index, addr, width))              \
543       return -1;                                                        \
544                                                                         \
545     const unsigned char *orig_addr = addr;                              \
546     if (width == 4)                                                     \
547       VAL = read_4ubyte_unaligned_inc (dbg, addr);                      \
548     else                                                                \
549       VAL = read_8ubyte_unaligned_inc (dbg, addr);                      \
550                                                                         \
551     int status = RELOC_HOOK (dbg, sec_index, orig_addr, width, &VAL);   \
552     if (status < 0)                                                     \
553       return status;                                                    \
554     status > 0;                                                         \
555    })
556
557 static inline int
558 __libdw_read_address_inc (Dwarf *dbg,
559                           int sec_index, const unsigned char **addrp,
560                           int width, Dwarf_Addr *ret)
561 {
562   const unsigned char *addr = *addrp;
563   READ_AND_RELOCATE (__libdw_relocate_address, (*ret));
564   *addrp = addr;
565   return 0;
566 }
567
568 static inline int
569 __libdw_read_address (Dwarf *dbg,
570                       int sec_index, const unsigned char *addr,
571                       int width, Dwarf_Addr *ret)
572 {
573   READ_AND_RELOCATE (__libdw_relocate_address, (*ret));
574   return 0;
575 }
576
577 static inline int
578 __libdw_read_offset_inc (Dwarf *dbg,
579                          int sec_index, const unsigned char **addrp,
580                          int width, Dwarf_Off *ret, int sec_ret,
581                          size_t size)
582 {
583   const unsigned char *addr = *addrp;
584   READ_AND_RELOCATE (__libdw_relocate_offset, (*ret));
585   *addrp = addr;
586   return __libdw_offset_in_section (dbg, sec_ret, *ret, size);
587 }
588
589 static inline int
590 __libdw_read_offset (Dwarf *dbg, Dwarf *dbg_ret,
591                      int sec_index, const unsigned char *addr,
592                      int width, Dwarf_Off *ret, int sec_ret,
593                      size_t size)
594 {
595   READ_AND_RELOCATE (__libdw_relocate_offset, (*ret));
596   return __libdw_offset_in_section (dbg_ret, sec_ret, *ret, size);
597 }
598
599 static inline size_t
600 cu_sec_idx (struct Dwarf_CU *cu)
601 {
602   return cu->type_offset == 0 ? IDX_debug_info : IDX_debug_types;
603 }
604
605 static inline Elf_Data *
606 cu_data (struct Dwarf_CU *cu)
607 {
608   return cu->dbg->sectiondata[cu_sec_idx (cu)];
609 }
610
611 /* Read up begin/end pair and increment read pointer.
612     - If it's normal range record, set up *BEGINP and *ENDP and return 0.
613     - If it's base address selection record, set up *BASEP and return 1.
614     - If it's end of rangelist, don't set anything and return 2
615     - If an error occurs, don't set anything and return <0.  */
616 int __libdw_read_begin_end_pair_inc (Dwarf *dbg, int sec_index,
617                                      unsigned char **addr, int width,
618                                      Dwarf_Addr *beginp, Dwarf_Addr *endp,
619                                      Dwarf_Addr *basep)
620   internal_function;
621
622 unsigned char * __libdw_formptr (Dwarf_Attribute *attr, int sec_index,
623                                  int err_nodata, unsigned char **endpp,
624                                  Dwarf_Off *offsetp)
625   internal_function;
626
627 #ifdef ENABLE_DWZ
628 /* Checks that the build_id of the underlying Elf matches the expected.
629    Returns zero on match, -1 on error or no build_id found or 1 when
630    build_id doesn't match.  */
631 int __check_build_id (Dwarf *dw, const uint8_t *build_id, const size_t id_len)
632   internal_function;
633 #endif /* ENABLE_DWZ */
634
635
636 /* Aliases to avoid PLTs.  */
637 INTDECL (dwarf_aggregate_size)
638 INTDECL (dwarf_attr)
639 INTDECL (dwarf_attr_integrate)
640 INTDECL (dwarf_begin)
641 INTDECL (dwarf_begin_elf)
642 INTDECL (dwarf_child)
643 INTDECL (dwarf_dieoffset)
644 INTDECL (dwarf_diename)
645 INTDECL (dwarf_end)
646 INTDECL (dwarf_entrypc)
647 INTDECL (dwarf_errmsg)
648 INTDECL (dwarf_formaddr)
649 INTDECL (dwarf_formblock)
650 INTDECL (dwarf_formref_die)
651 INTDECL (dwarf_formsdata)
652 INTDECL (dwarf_formstring)
653 INTDECL (dwarf_formudata)
654 INTDECL (dwarf_getarange_addr)
655 INTDECL (dwarf_getarangeinfo)
656 INTDECL (dwarf_getaranges)
657 INTDECL (dwarf_getsrcfiles)
658 INTDECL (dwarf_getsrclines)
659 INTDECL (dwarf_hasattr)
660 INTDECL (dwarf_haschildren)
661 INTDECL (dwarf_haspc)
662 INTDECL (dwarf_highpc)
663 INTDECL (dwarf_lowpc)
664 INTDECL (dwarf_nextcu)
665 INTDECL (dwarf_next_unit)
666 INTDECL (dwarf_offdie)
667 INTDECL (dwarf_ranges)
668 INTDECL (dwarf_siblingof)
669 INTDECL (dwarf_srclang)
670 INTDECL (dwarf_tag)
671
672 #endif  /* libdwP.h */