• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libunwind - a platform-independent unwind library
2    Copyright (c) 2003-2005 Hewlett-Packard Development Company, L.P.
3         Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4 
5 This file is part of libunwind.
6 
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14 
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
25 
26 #ifndef dwarf_h
27 #define dwarf_h
28 
29 #include <libunwind.h>
30 #include <stdatomic.h>
31 
32 struct dwarf_cursor;    /* forward-declaration */
33 struct elf_dyn_info;
34 
35 #include "dwarf-config.h"
36 
37 #ifdef HAVE_CONFIG_H
38 # include "config.h"
39 #endif
40 
41 #ifndef UNW_REMOTE_ONLY
42   #if defined(HAVE_LINK_H)
43     #include <link.h>
44   #elif defined(HAVE_SYS_LINK_H)
45     #include <sys/link.h>
46   #else
47     #error Could not find <link.h>
48   #endif
49   #if defined(__ANDROID__) && defined(__arm__) && __ANDROID_API__ < 21
50     int dl_iterate_phdr(int (*)(struct dl_phdr_info *, size_t, void *), void *);
51   #endif
52 #endif
53 
54 #include <pthread.h>
55 
56 /* DWARF expression opcodes.  */
57 
58 typedef enum
59   {
60     DW_OP_addr                  = 0x03,
61     DW_OP_deref                 = 0x06,
62     DW_OP_const1u               = 0x08,
63     DW_OP_const1s               = 0x09,
64     DW_OP_const2u               = 0x0a,
65     DW_OP_const2s               = 0x0b,
66     DW_OP_const4u               = 0x0c,
67     DW_OP_const4s               = 0x0d,
68     DW_OP_const8u               = 0x0e,
69     DW_OP_const8s               = 0x0f,
70     DW_OP_constu                = 0x10,
71     DW_OP_consts                = 0x11,
72     DW_OP_dup                   = 0x12,
73     DW_OP_drop                  = 0x13,
74     DW_OP_over                  = 0x14,
75     DW_OP_pick                  = 0x15,
76     DW_OP_swap                  = 0x16,
77     DW_OP_rot                   = 0x17,
78     DW_OP_xderef                = 0x18,
79     DW_OP_abs                   = 0x19,
80     DW_OP_and                   = 0x1a,
81     DW_OP_div                   = 0x1b,
82     DW_OP_minus                 = 0x1c,
83     DW_OP_mod                   = 0x1d,
84     DW_OP_mul                   = 0x1e,
85     DW_OP_neg                   = 0x1f,
86     DW_OP_not                   = 0x20,
87     DW_OP_or                    = 0x21,
88     DW_OP_plus                  = 0x22,
89     DW_OP_plus_uconst           = 0x23,
90     DW_OP_shl                   = 0x24,
91     DW_OP_shr                   = 0x25,
92     DW_OP_shra                  = 0x26,
93     DW_OP_xor                   = 0x27,
94     DW_OP_skip                  = 0x2f,
95     DW_OP_bra                   = 0x28,
96     DW_OP_eq                    = 0x29,
97     DW_OP_ge                    = 0x2a,
98     DW_OP_gt                    = 0x2b,
99     DW_OP_le                    = 0x2c,
100     DW_OP_lt                    = 0x2d,
101     DW_OP_ne                    = 0x2e,
102     DW_OP_lit0                  = 0x30,
103     DW_OP_lit1,  DW_OP_lit2,  DW_OP_lit3,  DW_OP_lit4,  DW_OP_lit5,
104     DW_OP_lit6,  DW_OP_lit7,  DW_OP_lit8,  DW_OP_lit9,  DW_OP_lit10,
105     DW_OP_lit11, DW_OP_lit12, DW_OP_lit13, DW_OP_lit14, DW_OP_lit15,
106     DW_OP_lit16, DW_OP_lit17, DW_OP_lit18, DW_OP_lit19, DW_OP_lit20,
107     DW_OP_lit21, DW_OP_lit22, DW_OP_lit23, DW_OP_lit24, DW_OP_lit25,
108     DW_OP_lit26, DW_OP_lit27, DW_OP_lit28, DW_OP_lit29, DW_OP_lit30,
109     DW_OP_lit31,
110     DW_OP_reg0                  = 0x50,
111     DW_OP_reg1,  DW_OP_reg2,  DW_OP_reg3,  DW_OP_reg4,  DW_OP_reg5,
112     DW_OP_reg6,  DW_OP_reg7,  DW_OP_reg8,  DW_OP_reg9,  DW_OP_reg10,
113     DW_OP_reg11, DW_OP_reg12, DW_OP_reg13, DW_OP_reg14, DW_OP_reg15,
114     DW_OP_reg16, DW_OP_reg17, DW_OP_reg18, DW_OP_reg19, DW_OP_reg20,
115     DW_OP_reg21, DW_OP_reg22, DW_OP_reg23, DW_OP_reg24, DW_OP_reg25,
116     DW_OP_reg26, DW_OP_reg27, DW_OP_reg28, DW_OP_reg29, DW_OP_reg30,
117     DW_OP_reg31,
118     DW_OP_breg0                 = 0x70,
119     DW_OP_breg1,  DW_OP_breg2,  DW_OP_breg3,  DW_OP_breg4,  DW_OP_breg5,
120     DW_OP_breg6,  DW_OP_breg7,  DW_OP_breg8,  DW_OP_breg9,  DW_OP_breg10,
121     DW_OP_breg11, DW_OP_breg12, DW_OP_breg13, DW_OP_breg14, DW_OP_breg15,
122     DW_OP_breg16, DW_OP_breg17, DW_OP_breg18, DW_OP_breg19, DW_OP_breg20,
123     DW_OP_breg21, DW_OP_breg22, DW_OP_breg23, DW_OP_breg24, DW_OP_breg25,
124     DW_OP_breg26, DW_OP_breg27, DW_OP_breg28, DW_OP_breg29, DW_OP_breg30,
125     DW_OP_breg31,
126     DW_OP_regx                  = 0x90,
127     DW_OP_fbreg                 = 0x91,
128     DW_OP_bregx                 = 0x92,
129     DW_OP_piece                 = 0x93,
130     DW_OP_deref_size            = 0x94,
131     DW_OP_xderef_size           = 0x95,
132     DW_OP_nop                   = 0x96,
133     DW_OP_push_object_address   = 0x97,
134     DW_OP_call2                 = 0x98,
135     DW_OP_call4                 = 0x99,
136     DW_OP_call_ref              = 0x9a,
137     DW_OP_lo_user               = 0xe0,
138     DW_OP_hi_user               = 0xff
139   }
140 dwarf_expr_op_t;
141 
142 #define DWARF_CIE_VERSION       3
143 #define DWARF_CIE_VERSION_MAX   4
144 
145 #define DWARF_CFA_OPCODE_MASK   0xc0
146 #define DWARF_CFA_OPERAND_MASK  0x3f
147 
148 typedef enum
149   {
150     DW_CFA_advance_loc          = 0x40,
151     DW_CFA_offset               = 0x80,
152     DW_CFA_restore              = 0xc0,
153     DW_CFA_nop                  = 0x00,
154     DW_CFA_set_loc              = 0x01,
155     DW_CFA_advance_loc1         = 0x02,
156     DW_CFA_advance_loc2         = 0x03,
157     DW_CFA_advance_loc4         = 0x04,
158     DW_CFA_offset_extended      = 0x05,
159     DW_CFA_restore_extended     = 0x06,
160     DW_CFA_undefined            = 0x07,
161     DW_CFA_same_value           = 0x08,
162     DW_CFA_register             = 0x09,
163     DW_CFA_remember_state       = 0x0a,
164     DW_CFA_restore_state        = 0x0b,
165     DW_CFA_def_cfa              = 0x0c,
166     DW_CFA_def_cfa_register     = 0x0d,
167     DW_CFA_def_cfa_offset       = 0x0e,
168     DW_CFA_def_cfa_expression   = 0x0f,
169     DW_CFA_expression           = 0x10,
170     DW_CFA_offset_extended_sf   = 0x11,
171     DW_CFA_def_cfa_sf           = 0x12,
172     DW_CFA_def_cfa_offset_sf    = 0x13,
173     DW_CFA_val_expression       = 0x16,
174     DW_CFA_lo_user              = 0x1c,
175     DW_CFA_MIPS_advance_loc8    = 0x1d,
176     DW_CFA_GNU_window_save      = 0x2d,
177     DW_CFA_GNU_args_size        = 0x2e,
178     DW_CFA_GNU_negative_offset_extended = 0x2f,
179     DW_CFA_hi_user              = 0x3c
180   }
181 dwarf_cfa_t;
182 
183 /* DWARF Pointer-Encoding (PEs).
184 
185    Pointer-Encodings were invented for the GCC exception-handling
186    support for C++, but they represent a rather generic way of
187    describing the format in which an address/pointer is stored and
188    hence we include the definitions here, in the main dwarf.h file.
189    The Pointer-Encoding format is partially documented in Linux Base
190    Spec v1.3 (http://www.linuxbase.org/spec/).  The rest is reverse
191    engineered from GCC.
192 
193 */
194 #define DW_EH_PE_FORMAT_MASK    0x0f    /* format of the encoded value */
195 #define DW_EH_PE_APPL_MASK      0x70    /* how the value is to be applied */
196 /* Flag bit.  If set, the resulting pointer is the address of the word
197    that contains the final address.  */
198 #define DW_EH_PE_indirect       0x80
199 
200 /* Pointer-encoding formats: */
201 #define DW_EH_PE_omit           0xff
202 #define DW_EH_PE_ptr            0x00    /* pointer-sized unsigned value */
203 #define DW_EH_PE_uleb128        0x01    /* unsigned LE base-128 value */
204 #define DW_EH_PE_udata2         0x02    /* unsigned 16-bit value */
205 #define DW_EH_PE_udata4         0x03    /* unsigned 32-bit value */
206 #define DW_EH_PE_udata8         0x04    /* unsigned 64-bit value */
207 #define DW_EH_PE_sleb128        0x09    /* signed LE base-128 value */
208 #define DW_EH_PE_sdata2         0x0a    /* signed 16-bit value */
209 #define DW_EH_PE_sdata4         0x0b    /* signed 32-bit value */
210 #define DW_EH_PE_sdata8         0x0c    /* signed 64-bit value */
211 
212 /* Pointer-encoding application: */
213 #define DW_EH_PE_absptr         0x00    /* absolute value */
214 #define DW_EH_PE_pcrel          0x10    /* rel. to addr. of encoded value */
215 #define DW_EH_PE_textrel        0x20    /* text-relative (GCC-specific???) */
216 #define DW_EH_PE_datarel        0x30    /* data-relative */
217 /* The following are not documented by LSB v1.3, yet they are used by
218    GCC, presumably they aren't documented by LSB since they aren't
219    used on Linux:  */
220 #define DW_EH_PE_funcrel        0x40    /* start-of-procedure-relative */
221 #define DW_EH_PE_aligned        0x50    /* aligned pointer */
222 
223 extern struct mempool dwarf_reg_state_pool;
224 extern struct mempool dwarf_cie_info_pool;
225 
226 typedef enum
227   {
228     DWARF_WHERE_UNDEF,          /* register isn't saved at all */
229     DWARF_WHERE_SAME,           /* register has same value as in prev. frame */
230     DWARF_WHERE_CFAREL,         /* register saved at CFA-relative address */
231     DWARF_WHERE_REG,            /* register saved in another register */
232     DWARF_WHERE_EXPR,           /* register saved */
233     DWARF_WHERE_VAL_EXPR,       /* register has computed value */
234   }
235 dwarf_where_t;
236 
237 /* For uniformity, we'd like to treat the CFA save-location like any
238    other register save-location, but this doesn't quite work, because
239    the CFA can be expressed as a (REGISTER,OFFSET) pair.  To handle
240    this, we use two dwarf_save_loc structures to describe the CFA.
241    The first one (CFA_REG_COLUMN), tells us where the CFA is saved.
242    In the case of DWARF_WHERE_EXPR, the CFA is defined by a DWARF
243    location expression whose address is given by member "val".  In the
244    case of DWARF_WHERE_REG, member "val" gives the number of the
245    base-register and the "val" member of DWARF_CFA_OFF_COLUMN gives
246    the offset value.  */
247 #define DWARF_CFA_REG_COLUMN    DWARF_NUM_PRESERVED_REGS
248 #define DWARF_CFA_OFF_COLUMN    (DWARF_NUM_PRESERVED_REGS + 1)
249 
250 typedef struct dwarf_reg_only_state
251   {
252     char where[DWARF_NUM_PRESERVED_REGS + 2];        /* how is the register saved? */
253     unw_word_t val[DWARF_NUM_PRESERVED_REGS + 2];             /* where it's saved */
254   }
255 dwarf_reg_only_state_t;
256 
257 typedef struct dwarf_reg_state
258   {
259     unw_word_t ret_addr_column;	/* which column in rule table represents return address */
260     dwarf_reg_only_state_t reg;
261   }
262 dwarf_reg_state_t;
263 
264 typedef struct dwarf_stackable_reg_state
265   {
266     struct dwarf_stackable_reg_state *next;       /* for rs_stack */
267     dwarf_reg_state_t state;
268   }
269 dwarf_stackable_reg_state_t;
270 
271 typedef struct dwarf_reg_cache_entry
272   {
273     unw_word_t ip;                        /* ip this rs is for */
274     unsigned short coll_chain;  /* used for hash collisions */
275     unsigned short hint;              /* hint for next rs to try (or -1) */
276     unsigned short valid : 1;         /* optional machine-dependent signal info */
277     unsigned short signal_frame : 1;  /* optional machine-dependent signal info */
278   }
279 dwarf_reg_cache_entry_t;
280 
281 typedef struct dwarf_cie_info
282   {
283     unw_word_t cie_instr_start; /* start addr. of CIE "initial_instructions" */
284     unw_word_t cie_instr_end;   /* end addr. of CIE "initial_instructions" */
285     unw_word_t fde_instr_start; /* start addr. of FDE "instructions" */
286     unw_word_t fde_instr_end;   /* end addr. of FDE "instructions" */
287     unw_word_t code_align;      /* code-alignment factor */
288     unw_word_t data_align;      /* data-alignment factor */
289     unw_word_t ret_addr_column; /* column of return-address register */
290     unw_word_t handler;         /* address of personality-routine */
291     uint16_t abi;
292     uint16_t tag;
293     uint8_t fde_encoding;
294     uint8_t lsda_encoding;
295     unsigned int sized_augmentation : 1;
296     unsigned int have_abi_marker : 1;
297     unsigned int signal_frame : 1;
298   }
299 dwarf_cie_info_t;
300 
301 typedef struct dwarf_state_record
302   {
303     unsigned char fde_encoding;
304     unw_word_t args_size;
305 
306     dwarf_reg_state_t rs_initial;       /* reg-state after CIE instructions */
307     dwarf_reg_state_t rs_current;       /* current reg-state */
308   }
309 dwarf_state_record_t;
310 
311 typedef struct dwarf_cursor
312   {
313     void *as_arg;               /* argument to address-space callbacks */
314     unw_addr_space_t as;        /* reference to per-address-space info */
315 
316     unw_word_t cfa;     /* canonical frame address; aka frame-/stack-pointer */
317     unw_word_t ip;              /* instruction pointer */
318     unw_word_t args_size;       /* size of arguments */
319     unw_word_t eh_args[UNW_TDEP_NUM_EH_REGS];
320     unsigned int eh_valid_mask;
321 
322     dwarf_loc_t loc[DWARF_NUM_PRESERVED_REGS];
323 
324     unsigned int stash_frames :1; /* stash frames for fast lookup */
325     unsigned int use_prev_instr :1; /* use previous (= call) or current (= signal) instruction? */
326     unsigned int pi_valid :1;   /* is proc_info valid? */
327     unsigned int pi_is_dynamic :1; /* proc_info found via dynamic proc info? */
328     unw_proc_info_t pi;         /* info about current procedure */
329 
330     short hint; /* faster lookup of the rs cache */
331     short prev_rs;
332   }
333 dwarf_cursor_t;
334 
335 #define DWARF_DEFAULT_LOG_UNW_CACHE_SIZE        7
336 #define DWARF_DEFAULT_UNW_CACHE_SIZE    (1 << DWARF_DEFAULT_LOG_UNW_CACHE_SIZE)
337 
338 #define DWARF_DEFAULT_LOG_UNW_HASH_SIZE (DWARF_DEFAULT_LOG_UNW_CACHE_SIZE + 1)
339 #define DWARF_DEFAULT_UNW_HASH_SIZE     (1 << DWARF_DEFAULT_LOG_UNW_HASH_SIZE)
340 
341 typedef unsigned char unw_hash_index_t;
342 
343 struct dwarf_rs_cache
344   {
345     pthread_mutex_t lock;
346     unsigned short rr_head;    /* index of least-recently allocated rs */
347 
348     unsigned short log_size;
349     unsigned short prev_log_size;
350 
351     /* hash table that maps instruction pointer to rs index: */
352     unsigned short *hash;
353 
354     _Atomic uint32_t generation;        /* generation number */
355 
356     /* rs cache: */
357     dwarf_reg_state_t *buckets;
358     dwarf_reg_cache_entry_t *links;
359 
360     /* default memory, loaded in BSS segment */
361     unsigned short default_hash[DWARF_DEFAULT_UNW_HASH_SIZE];
362     dwarf_reg_state_t default_buckets[DWARF_DEFAULT_UNW_CACHE_SIZE];
363     dwarf_reg_cache_entry_t default_links[DWARF_DEFAULT_UNW_CACHE_SIZE];
364   };
365 
366 /* A list of descriptors for loaded .debug_frame sections.  */
367 
368 struct unw_debug_frame_list
369   {
370     /* The start (inclusive) and end (exclusive) of the described region.  */
371     unw_word_t start;
372     unw_word_t end;
373     /* ELF load offset */
374     unw_word_t load_offset;
375     /* The debug frame itself.  */
376     char *debug_frame;
377     size_t debug_frame_size;
378     /* Index (for binary search).  */
379     struct table_entry *index;
380     size_t index_size;
381     /* Pointer to next descriptor.  */
382     struct unw_debug_frame_list *next;
383   };
384 
385 /* Convenience macros: */
386 #define dwarf_init                      UNW_ARCH_OBJ (dwarf_init)
387 #define dwarf_callback                  UNW_OBJ (dwarf_callback)
388 #define dwarf_find_proc_info            UNW_OBJ (dwarf_find_proc_info)
389 #define dwarf_find_debug_frame          UNW_OBJ (dwarf_find_debug_frame)
390 #define dwarf_search_unwind_table       UNW_OBJ (dwarf_search_unwind_table)
391 #define dwarf_find_unwind_table         UNW_OBJ (dwarf_find_unwind_table)
392 #define dwarf_put_unwind_info           UNW_OBJ (dwarf_put_unwind_info)
393 #define dwarf_put_unwind_info           UNW_OBJ (dwarf_put_unwind_info)
394 #define dwarf_eval_expr                 UNW_OBJ (dwarf_eval_expr)
395 #define dwarf_stack_aligned             UNW_OBJ (dwarf_stack_aligned)
396 #define dwarf_extract_proc_info_from_fde \
397                 UNW_OBJ (dwarf_extract_proc_info_from_fde)
398 #define dwarf_find_save_locs            UNW_OBJ (dwarf_find_save_locs)
399 #define dwarf_make_proc_info            UNW_OBJ (dwarf_make_proc_info)
400 #define dwarf_apply_reg_state           UNW_OBJ (dwarf_apply_reg_state)
401 #define dwarf_reg_states_iterate        UNW_OBJ (dwarf_reg_states_iterate)
402 #define dwarf_read_encoded_pointer      UNW_OBJ (dwarf_read_encoded_pointer)
403 #define dwarf_step                      UNW_OBJ (dwarf_step)
404 #define dwarf_flush_rs_cache            UNW_OBJ (dwarf_flush_rs_cache)
405 
406 extern int dwarf_init (void);
407 #ifndef UNW_REMOTE_ONLY
408 extern int dwarf_callback (struct dl_phdr_info *info, size_t size, void *ptr);
409 extern int dwarf_find_proc_info (unw_addr_space_t as, unw_word_t ip,
410                                  unw_proc_info_t *pi,
411                                  int need_unwind_info, void *arg);
412 #endif /* !UNW_REMOTE_ONLY */
413 extern int dwarf_find_debug_frame (int found, unw_dyn_info_t *di_debug,
414                                    unw_word_t ip, unw_word_t segbase,
415                                    const char* obj_name, unw_word_t start,
416                                    unw_word_t end);
417 extern int dwarf_search_unwind_table (unw_addr_space_t as,
418                                       unw_word_t ip,
419                                       unw_dyn_info_t *di,
420                                       unw_proc_info_t *pi,
421                                       int need_unwind_info, void *arg);
422 
423 extern int dwarf_find_unwind_table (struct elf_dyn_info *edi, unw_addr_space_t as,
424                                     char *path, unw_word_t segbase, unw_word_t mapoff,
425                                     unw_word_t ip);
426 extern void dwarf_put_unwind_info (unw_addr_space_t as,
427                                    unw_proc_info_t *pi, void *arg);
428 extern int dwarf_eval_expr (struct dwarf_cursor *c, unw_word_t stack_val, unw_word_t *addr,
429                             unw_word_t len, unw_word_t *valp,
430                             int *is_register);
431 extern int
432 dwarf_stack_aligned(struct dwarf_cursor *c, unw_word_t cfa_addr,
433                     unw_word_t rbp_addr, unw_word_t *offset);
434 
435 extern int dwarf_extract_proc_info_from_fde (unw_addr_space_t as,
436                                              unw_accessors_t *a,
437                                              unw_word_t *fde_addr,
438                                              unw_proc_info_t *pi,
439                                              unw_word_t base,
440                                              int need_unwind_info,
441                                              int is_debug_frame,
442                                              void *arg);
443 extern int dwarf_find_save_locs (struct dwarf_cursor *c);
444 extern int dwarf_make_proc_info (struct dwarf_cursor *c);
445 extern int dwarf_apply_reg_state (struct dwarf_cursor *c, struct dwarf_reg_state *rs);
446 extern int dwarf_reg_states_iterate (struct dwarf_cursor *c, unw_reg_states_callback cb, void *token);
447 extern int dwarf_read_encoded_pointer (unw_addr_space_t as,
448                                        unw_accessors_t *a,
449                                        unw_word_t *addr,
450                                        unsigned char encoding,
451                                        const unw_proc_info_t *pi,
452                                        unw_word_t *valp, void *arg);
453 extern int dwarf_step (struct dwarf_cursor *c);
454 extern int dwarf_flush_rs_cache (struct dwarf_rs_cache *cache);
455 
456 #endif /* dwarf_h */
457