blob: 1ceb1a92c77dd0b3018b5a0037f4f16d8d4f47ac [file] [log] [blame]
hp.com!davidmd7e9f752002-11-23 02:12:30 +00001/* libunwind - a platform-independent unwind library
mostang.com!davidm9430d352003-02-27 09:58:57 +00002 Copyright (C) 2002-2003 Hewlett-Packard Co
hp.com!davidmd7e9f752002-11-23 02:12:30 +00003 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5This file is part of libunwind.
6
7Permission is hereby granted, free of charge, to any person obtaining
8a copy of this software and associated documentation files (the
9"Software"), to deal in the Software without restriction, including
10without limitation the rights to use, copy, modify, merge, publish,
11distribute, sublicense, and/or sell copies of the Software, and to
12permit persons to whom the Software is furnished to do so, subject to
13the following conditions:
14
15The above copyright notice and this permission notice shall be
16included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
25
hp.com!davidmd7e9f752002-11-23 02:12:30 +000026/* This file defines the runtime-support routines for dynamically
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +000027generated code. Even though it is implemented as part of libunwind,
28it is logically separate from the interface to perform the actual
29unwinding. In particular, this interface is always used in the
30context of the unwind target, whereas the rest of the unwind API is
31used in context of the process that is doing the unwind (which may be
32a debugger running on another machine, for example).
33
34Note that the data-structures declared here server a dual purpose:
35when a program registers a dynamically generated procedure, it uses
36these structures directly. On the other hand, with remote-unwinding,
37the data-structures are read from the remote process's memory and
mostang.com!davidm744b9102002-12-12 09:17:41 +000038translated into internalized versions. To facilitate remote-access,
39the following rules should be followed in declaring these structures:
40
41 (1) Declare a member as a pointer only if the the information the
42 member points to needs to be internalized as well (e.g., a
43 string representing a procedure name should be declared as
44 "const char *", but the instruction pointer should be declared
45 as unw_word_t).
46
47 (2) Provide sufficient padding to ensure that no implicit padding
48 will be needed on any of the supported target architectures. For
49 the time being, padding data structures with the assumption that
50 sizeof (unw_word_t) == 8 should be sufficient. (Note: it's not
51 impossible to internalize structures with internal padding, but
52 it does make the process a bit harder).
53
54 (3) Don't declare members that contain bitfields or floating-point
55 values.
56
57 (4) Don't declare members with enumeration types. Declare them as
58 int32_t instead. */
hp.com!davidmd7e9f752002-11-23 02:12:30 +000059
60typedef enum
61 {
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +000062 UNW_DYN_STOP = 0, /* end-of-unwind-info marker */
63 UNW_DYN_SAVE_REG, /* save register to another register */
64 UNW_DYN_SPILL_FP_REL, /* frame-pointer-relative register spill */
65 UNW_DYN_SPILL_SP_REL, /* stack-pointer-relative register spill */
66 UNW_DYN_ADD, /* add constant value to a register */
67 UNW_DYN_POP_FRAMES, /* drop one or more stack frames */
68 UNW_DYN_LABEL_STATE, /* name the current state */
69 UNW_DYN_COPY_STATE, /* set the region's entry-state */
70 UNW_DYN_ALIAS /* get unwind info from an alias */
hp.com!davidmd7e9f752002-11-23 02:12:30 +000071 }
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +000072unw_dyn_operation_t;
hp.com!davidmd7e9f752002-11-23 02:12:30 +000073
mostang.com!davidm744b9102002-12-12 09:17:41 +000074typedef enum
75 {
hp.com!davidmdd8806c2003-11-25 22:33:49 +000076 UNW_INFO_FORMAT_DYNAMIC, /* unw_dyn_proc_info_t */
77 UNW_INFO_FORMAT_TABLE, /* unw_dyn_table_t */
78 UNW_INFO_FORMAT_REMOTE_TABLE, /* unw_dyn_remote_table_t */
mostang.com!davidm744b9102002-12-12 09:17:41 +000079 }
80unw_dyn_info_format_t;
81
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +000082typedef struct unw_dyn_op
hp.com!davidmd7e9f752002-11-23 02:12:30 +000083 {
mostang.com!davidm744b9102002-12-12 09:17:41 +000084 int8_t tag; /* what operation? */
85 int8_t qp; /* qualifying predicate register */
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +000086 int16_t reg; /* what register */
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +000087 int32_t when; /* when does it take effect? */
88 unw_word_t val; /* auxiliary value */
hp.com!davidmd7e9f752002-11-23 02:12:30 +000089 }
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +000090unw_dyn_op_t;
hp.com!davidmd7e9f752002-11-23 02:12:30 +000091
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +000092typedef struct unw_dyn_region_info
hp.com!davidmd7e9f752002-11-23 02:12:30 +000093 {
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +000094 struct unw_dyn_region_info *next; /* linked list of regions */
mostang.com!davidme6144052003-02-22 08:19:43 +000095 int32_t insn_count; /* region length (# of instructions) */
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +000096 uint32_t op_count; /* length of op-array */
97 unw_dyn_op_t op[1]; /* variable-length op-array */
98 }
99unw_dyn_region_info_t;
100
101typedef struct unw_dyn_proc_info
102 {
mostang.com!davidm744b9102002-12-12 09:17:41 +0000103 unw_word_t name_ptr; /* address of human-readable procedure name */
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000104 unw_word_t handler; /* address of personality routine */
105 uint32_t flags;
mostang.com!davidm744b9102002-12-12 09:17:41 +0000106 int32_t pad0;
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000107 unw_dyn_region_info_t *regions;
108 }
109unw_dyn_proc_info_t;
110
111typedef struct unw_dyn_table_info
112 {
mostang.com!davidm744b9102002-12-12 09:17:41 +0000113 unw_word_t name_ptr; /* addr. of table name (e.g., library name) */
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000114 unw_word_t segbase; /* segment base */
mostang.com!davidm744b9102002-12-12 09:17:41 +0000115 unw_word_t table_len; /* must be a multiple of sizeof(unw_word_t)! */
116 unw_word_t *table_data;
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000117 }
118unw_dyn_table_info_t;
119
hp.com!davidmdd8806c2003-11-25 22:33:49 +0000120typedef struct unw_dyn_remote_table_info
121 {
122 unw_word_t name_ptr; /* addr. of table name (e.g., library name) */
123 unw_word_t segbase; /* segment base */
124 unw_word_t table_len; /* must be a multiple of sizeof(unw_word_t)! */
125 unw_word_t table_data;
126 }
127unw_dyn_remote_table_info_t;
128
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000129typedef struct unw_dyn_info
130 {
mostang.com!davidm744b9102002-12-12 09:17:41 +0000131 /* doubly-linked list of dyn-info structures: */
132 struct unw_dyn_info *next;
133 struct unw_dyn_info *prev;
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000134 unw_word_t start_ip; /* first IP covered by this entry */
135 unw_word_t end_ip; /* first IP NOT covered by this entry */
136 unw_word_t gp; /* global-pointer in effect for this entry */
mostang.com!davidm744b9102002-12-12 09:17:41 +0000137 int32_t format; /* real type: unw_dyn_info_format_t */
138 int32_t pad;
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000139 union
140 {
141 unw_dyn_proc_info_t pi;
142 unw_dyn_table_info_t ti;
hp.com!davidmdd8806c2003-11-25 22:33:49 +0000143 unw_dyn_remote_table_info_t rti;
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000144 }
145 u;
hp.com!davidmd7e9f752002-11-23 02:12:30 +0000146 }
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000147unw_dyn_info_t;
hp.com!davidmd7e9f752002-11-23 02:12:30 +0000148
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000149typedef struct unw_dyn_info_list
150 {
mostang.com!davidm744b9102002-12-12 09:17:41 +0000151 uint32_t version;
152 uint32_t generation;
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000153 unw_dyn_info_t *first;
154 }
155unw_dyn_info_list_t;
156
157/* Return the size (in bytes) of an unw_dyn_region_info_t structure that can
hp.com!davidmd7e9f752002-11-23 02:12:30 +0000158 hold OP_COUNT ops. */
mostang.com!davidm9430d352003-02-27 09:58:57 +0000159#define _U_dyn_region_info_size(op_count) \
160 ((char *) (((unw_dyn_region_info_t *) NULL)->op + (op_count)) \
161 - (char *) NULL)
hp.com!davidmd7e9f752002-11-23 02:12:30 +0000162
163/* Register the unwind info for a single procedure.
164 This routine is NOT signal-safe. */
mostang.com!davidm744b9102002-12-12 09:17:41 +0000165extern void _U_dyn_register (unw_dyn_info_t *di);
hp.com!davidmd7e9f752002-11-23 02:12:30 +0000166
167/* Cancel the unwind info for a single procedure.
168 This routine is NOT signal-safe. */
mostang.com!davidm744b9102002-12-12 09:17:41 +0000169extern void _U_dyn_cancel (unw_dyn_info_t *di);
hp.com!davidmd7e9f752002-11-23 02:12:30 +0000170
171
172/* Convenience routines. */
173
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000174#define _U_dyn_op(_t, _q, _w, _r, _v) \
175 ((unw_dyn_op_t) { \
176 .tag = (_t), \
177 .qp = (_q), \
178 .when = (_w), \
179 .reg = (_r), \
180 .val = (_v) \
181 })
hp.com!davidmd7e9f752002-11-23 02:12:30 +0000182
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000183#define _U_dyn_op_save_reg(op, qp, when, reg, dst) \
184 (*(op) = _U_dyn_op (UNW_DYN_SAVE_REG, (qp), (when), (reg), (dst)))
hp.com!davidmd7e9f752002-11-23 02:12:30 +0000185
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000186#define _U_dyn_op_spill_fp_rel(op, qp, when, reg, offset) \
187 (*(op) = _U_dyn_op (UNW_DYN_SPILL_FP_REL, (qp), (when), (reg), \
188 (offset)))
hp.com!davidmd7e9f752002-11-23 02:12:30 +0000189
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000190#define _U_dyn_op_spill_sp_rel(op, qp, when, reg, offset) \
191 (*(op) = _U_dyn_op (UNW_DYN_SPILL_SP_REL, (qp), (when), (reg), \
192 (offset)))
hp.com!davidmd7e9f752002-11-23 02:12:30 +0000193
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000194#define _U_dyn_op_add(op, qp, when, reg, value) \
195 (*(op) = _U_dyn_op (UNW_DYN_ADD, (qp), (when), (reg), (value)))
hp.com!davidmd7e9f752002-11-23 02:12:30 +0000196
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000197#define _U_dyn_op_pop_frames(op, qp, when, num_frames) \
hp.com!davidmcf3b7562003-02-26 08:33:57 +0000198 (*(op) = _U_dyn_op (UNW_DYN_POP_FRAMES, (qp), (when), 0, (num_frames)))
hp.com!davidmd7e9f752002-11-23 02:12:30 +0000199
mostang.com!davidm9430d352003-02-27 09:58:57 +0000200#define _U_dyn_op_label_state(op, label) \
201 (*(op) = _U_dyn_op (UNW_DYN_LABEL_STATE, _U_QP_TRUE, -1, 0, (label)))
hp.com!davidmd7e9f752002-11-23 02:12:30 +0000202
mostang.com!davidm9430d352003-02-27 09:58:57 +0000203#define _U_dyn_op_copy_state(op, label) \
204 (*(op) = _U_dyn_op (UNW_DYN_COPY_STATE, _U_QP_TRUE, -1, 0, (label)))
hp.com!davidmd7e9f752002-11-23 02:12:30 +0000205
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000206#define _U_dyn_op_alias(op, qp, when, addr) \
207 (*(op) = _U_dyn_op (UNW_DYN_ALIAS, (qp), (when), 0, (addr)))
hp.com!davidmd7e9f752002-11-23 02:12:30 +0000208
mostang.com!davidmdfc08ea2002-12-03 08:19:58 +0000209#define _U_dyn_op_stop(op) \
mostang.com!davidm9430d352003-02-27 09:58:57 +0000210 (*(op) = _U_dyn_op (UNW_DYN_STOP, _U_QP_TRUE, -1, 0, 0))
211
212/* The target-dependent qualifying predicate which is always TRUE. On
213 IA-64, that's p0 (0), on non-predicated architectures, the value is
214 ignored. */
215#define _U_QP_TRUE _U_TDEP_QP_TRUE