-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gfde.c
357 lines (300 loc) · 10.6 KB
/
Gfde.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* libunwind - a platform-independent unwind library
Copyright (c) 2003-2005 Hewlett-Packard Development Company, L.P.
Contributed by David Mosberger-Tang <[email protected]>
This file is part of libunwind.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include "include/dwarf_i.h"
static inline int
is_cie_id (unw_word_t val, int is_debug_frame)
{
/* The CIE ID is normally 0xffffffff (for 32-bit ELF) or
0xffffffffffffffff (for 64-bit ELF). However, .eh_frame
uses 0. */
if (is_debug_frame)
return (val == - (uint32_t) 1 || val == - (uint64_t) 1);
else
return (val == 0);
}
/* Note: we don't need to keep track of more than the first four
characters of the augmentation string, because we (a) ignore any
augmentation string contents once we find an unrecognized character
and (b) those characters that we do recognize, can't be
repeated. */
static inline int
parse_cie (unw_addr_space_t as, unw_accessors_t *a, unw_word_t addr,
const unw_proc_info_t *pi, struct dwarf_cie_info *dci,
unw_word_t base, void *arg)
{
uint8_t version, ch, augstr[5], fde_encoding, handler_encoding;
unw_word_t len, cie_end_addr, aug_size;
uint32_t u32val;
uint64_t u64val;
size_t i;
int ret;
# define STR2(x) #x
# define STR(x) STR2(x)
/* Pick appropriate default for FDE-encoding. DWARF spec says
start-IP (initial_location) and the code-size (address_range) are
"address-unit sized constants". The `R' augmentation can be used
to override this, but by default, we pick an address-sized unit
for fde_encoding. */
switch (dwarf_addr_size (as))
{
case 4: fde_encoding = DW_EH_PE_udata4; break;
case 8: fde_encoding = DW_EH_PE_udata8; break;
default: fde_encoding = DW_EH_PE_omit; break;
}
dci->lsda_encoding = DW_EH_PE_omit;
dci->handler = 0;
if ((ret = dwarf_readu32 (as, a, &addr, &u32val, arg)) < 0)
return ret;
if (u32val != 0xffffffff)
{
/* the CIE is in the 32-bit DWARF format */
uint32_t cie_id;
/* DWARF says CIE id should be 0xffffffff, but in .eh_frame, it's 0 */
const uint32_t expected_id = (base) ? 0xffffffff : 0;
len = u32val;
cie_end_addr = addr + len;
if ((ret = dwarf_readu32 (as, a, &addr, &cie_id, arg)) < 0)
return ret;
if (cie_id != expected_id)
{
Debug (1, "Unexpected CIE id %x\n", cie_id);
return -UNW_EINVAL;
}
}
else
{
/* the CIE is in the 64-bit DWARF format */
uint64_t cie_id;
/* DWARF says CIE id should be 0xffffffffffffffff, but in
.eh_frame, it's 0 */
const uint64_t expected_id = (base) ? 0xffffffffffffffffull : 0;
if ((ret = dwarf_readu64 (as, a, &addr, &u64val, arg)) < 0)
return ret;
len = u64val;
cie_end_addr = addr + len;
if ((ret = dwarf_readu64 (as, a, &addr, &cie_id, arg)) < 0)
return ret;
if (cie_id != expected_id)
{
Debug (1, "Unexpected CIE id %llx\n", (long long) cie_id);
return -UNW_EINVAL;
}
}
dci->cie_instr_end = cie_end_addr;
if ((ret = dwarf_readu8 (as, a, &addr, &version, arg)) < 0)
return ret;
if (version != 1 && version != DWARF_CIE_VERSION)
{
Debug (1, "Got CIE version %u, expected version 1 or "
STR (DWARF_CIE_VERSION) "\n", version);
return -UNW_EBADVERSION;
}
/* read and parse the augmentation string: */
memset (augstr, 0, sizeof (augstr));
for (i = 0;;)
{
if ((ret = dwarf_readu8 (as, a, &addr, &ch, arg)) < 0)
return ret;
if (!ch)
break; /* end of augmentation string */
if (i < sizeof (augstr) - 1)
augstr[i++] = ch;
}
if ((ret = dwarf_read_uleb128 (as, a, &addr, &dci->code_align, arg)) < 0
|| (ret = dwarf_read_sleb128 (as, a, &addr, &dci->data_align, arg)) < 0)
return ret;
/* Read the return-address column either as a u8 or as a uleb128. */
if (version == 1)
{
if ((ret = dwarf_readu8 (as, a, &addr, &ch, arg)) < 0)
return ret;
dci->ret_addr_column = ch;
}
else if ((ret = dwarf_read_uleb128 (as, a, &addr, &dci->ret_addr_column,
arg)) < 0)
return ret;
i = 0;
if (augstr[0] == 'z')
{
dci->sized_augmentation = 1;
if ((ret = dwarf_read_uleb128 (as, a, &addr, &aug_size, arg)) < 0)
return ret;
i++;
}
for (; i < sizeof (augstr) && augstr[i]; ++i)
switch (augstr[i])
{
case 'L':
/* read the LSDA pointer-encoding format. */
if ((ret = dwarf_readu8 (as, a, &addr, &ch, arg)) < 0)
return ret;
dci->lsda_encoding = ch;
break;
case 'R':
/* read the FDE pointer-encoding format. */
if ((ret = dwarf_readu8 (as, a, &addr, &fde_encoding, arg)) < 0)
return ret;
break;
case 'P':
/* read the personality-routine pointer-encoding format. */
if ((ret = dwarf_readu8 (as, a, &addr, &handler_encoding, arg)) < 0)
return ret;
if ((ret = dwarf_read_encoded_pointer (as, a, &addr, handler_encoding,
pi, &dci->handler, arg)) < 0)
return ret;
break;
case 'S':
/* This is a signal frame. */
dci->signal_frame = 1;
/* Temporarily set it to one so dwarf_parse_fde() knows that
it should fetch the actual ABI/TAG pair from the FDE. */
dci->have_abi_marker = 1;
break;
default:
Debug (1, "Unexpected augmentation string `%s'\n", augstr);
if (dci->sized_augmentation)
/* If we have the size of the augmentation body, we can skip
over the parts that we don't understand, so we're OK. */
goto done;
else
return -UNW_EINVAL;
}
done:
dci->fde_encoding = fde_encoding;
dci->cie_instr_start = addr;
Debug (15, "CIE parsed OK, augmentation = \"%s\", handler=0x%lx\n",
augstr, (long) dci->handler);
return 0;
}
/* Extract proc-info from the FDE starting at adress ADDR.
Pass BASE as zero for eh_frame behaviour, or a pointer to
debug_frame base for debug_frame behaviour. */
HIDDEN int
dwarf_extract_proc_info_from_fde (unw_addr_space_t as, unw_accessors_t *a,
unw_word_t *addrp, unw_proc_info_t *pi,
int need_unwind_info, unw_word_t base,
void *arg)
{
unw_word_t fde_end_addr, cie_addr, cie_offset_addr, aug_end_addr = 0;
unw_word_t start_ip, ip_range, aug_size, addr = *addrp;
int ret, ip_range_encoding;
struct dwarf_cie_info dci;
uint64_t u64val;
uint32_t u32val;
Debug (12, "FDE @ 0x%lx\n", (long) addr);
memset (&dci, 0, sizeof (dci));
if ((ret = dwarf_readu32 (as, a, &addr, &u32val, arg)) < 0)
return ret;
if (u32val != 0xffffffff)
{
int32_t cie_offset;
/* In some configurations, an FDE with a 0 length indicates the
end of the FDE-table. */
if (u32val == 0)
return -UNW_ENOINFO;
/* the FDE is in the 32-bit DWARF format */
*addrp = fde_end_addr = addr + u32val;
cie_offset_addr = addr;
if ((ret = dwarf_reads32 (as, a, &addr, &cie_offset, arg)) < 0)
return ret;
if (is_cie_id (cie_offset, base != 0))
/* ignore CIEs (happens during linear searches) */
return 0;
if (base != 0)
cie_addr = base + cie_offset;
else
/* DWARF says that the CIE_pointer in the FDE is a
.debug_frame-relative offset, but the GCC-generated .eh_frame
sections instead store a "pcrelative" offset, which is just
as fine as it's self-contained. */
cie_addr = cie_offset_addr - cie_offset;
}
else
{
int64_t cie_offset;
/* the FDE is in the 64-bit DWARF format */
if ((ret = dwarf_readu64 (as, a, &addr, &u64val, arg)) < 0)
return ret;
*addrp = fde_end_addr = addr + u64val;
cie_offset_addr = addr;
if ((ret = dwarf_reads64 (as, a, &addr, &cie_offset, arg)) < 0)
return ret;
if (is_cie_id (cie_offset, base != 0))
/* ignore CIEs (happens during linear searches) */
return 0;
if (base != 0)
cie_addr = base + cie_offset;
else
/* DWARF says that the CIE_pointer in the FDE is a
.debug_frame-relative offset, but the GCC-generated .eh_frame
sections instead store a "pcrelative" offset, which is just
as fine as it's self-contained. */
cie_addr = (unw_word_t) ((uint64_t) cie_offset_addr - cie_offset);
}
Debug (15, "looking for CIE at address %lx\n", (long) cie_addr);
if ((ret = parse_cie (as, a, cie_addr, pi, &dci, base, arg)) < 0)
return ret;
/* IP-range has same encoding as FDE pointers, except that it's
always an absolute value: */
ip_range_encoding = dci.fde_encoding & DW_EH_PE_FORMAT_MASK;
if ((ret = dwarf_read_encoded_pointer (as, a, &addr, dci.fde_encoding,
pi, &start_ip, arg)) < 0
|| (ret = dwarf_read_encoded_pointer (as, a, &addr, ip_range_encoding,
pi, &ip_range, arg)) < 0)
return ret;
pi->start_ip = start_ip;
pi->end_ip = start_ip + ip_range;
pi->handler = dci.handler;
if (dci.sized_augmentation)
{
if ((ret = dwarf_read_uleb128 (as, a, &addr, &aug_size, arg)) < 0)
return ret;
aug_end_addr = addr + aug_size;
}
if ((ret = dwarf_read_encoded_pointer (as, a, &addr, dci.lsda_encoding,
pi, &pi->lsda, arg)) < 0)
return ret;
Debug (15, "FDE covers IP 0x%lx-0x%lx, LSDA=0x%lx\n",
(long) pi->start_ip, (long) pi->end_ip, (long) pi->lsda);
if (need_unwind_info)
{
pi->format = UNW_INFO_FORMAT_TABLE;
pi->unwind_info_size = sizeof (dci);
pi->unwind_info = mempool_alloc (&dwarf_cie_info_pool);
if (!pi->unwind_info)
return -UNW_ENOMEM;
if (dci.have_abi_marker)
{
if ((ret = dwarf_readu16 (as, a, &addr, &dci.abi, arg)) < 0
|| (ret = dwarf_readu16 (as, a, &addr, &dci.tag, arg)) < 0)
return ret;
Debug (13, "Found ABI marker = (abi=%u, tag=%u)\n",
dci.abi, dci.tag);
}
if (dci.sized_augmentation)
dci.fde_instr_start = aug_end_addr;
else
dci.fde_instr_start = addr;
dci.fde_instr_end = fde_end_addr;
memcpy (pi->unwind_info, &dci, sizeof (dci));
}
return 0;
}