1 /* DW_EH_PE_* support for libdw unwinder.
2 Copyright (C) 2009-2010, 2014, 2015 Red Hat, Inc.
3 This file is part of elfutils.
5 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
8 * the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 3 of the License, or (at
10 your option) any later version
14 * the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at
16 your option) any later version
18 or both in parallel, as here.
20 elfutils is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 General Public License for more details.
25 You should have received copies of the GNU General Public License and
26 the GNU Lesser General Public License along with this program. If
27 not, see <http://www.gnu.org/licenses/>. */
29 #ifndef _ENCODED_VALUE_H
30 #define _ENCODED_VALUE_H 1
35 #include "../libelf/common.h"
38 /* Returns zero if the value is omitted, the encoding is unknown or
39 the (leb128) size cannot be determined. */
40 static size_t __attribute__ ((unused))
41 encoded_value_size (const Elf_Data *data, const unsigned char e_ident[],
42 uint8_t encoding, const uint8_t *p)
44 if (encoding == DW_EH_PE_omit)
47 switch (encoding & 0x07)
57 return e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
59 case DW_EH_PE_uleb128:
62 const uint8_t *end = p;
63 while (end < (uint8_t *) data->d_buf + data->d_size)
74 /* Returns zero when value was read successfully, minus one otherwise. */
75 static inline int __attribute__ ((unused))
76 __libdw_cfi_read_address_inc (const Dwarf_CFI *cache,
77 const unsigned char **addrp,
78 int width, Dwarf_Addr *ret)
80 width = width ?: cache->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
82 if (cache->dbg != NULL)
83 return __libdw_read_address_inc (cache->dbg, IDX_debug_frame,
86 /* Only .debug_frame might have relocation to consider.
87 Read plain values from .eh_frame data. */
89 const unsigned char *endp = cache->data->d.d_buf + cache->data->d.d_size;
90 Dwarf eh_dbg = { .other_byte_order = MY_ELFDATA != cache->e_ident[EI_DATA] };
94 if (unlikely (*addrp + 4 > endp))
97 __libdw_seterrno (DWARF_E_INVALID_CFI);
100 *ret = read_4ubyte_unaligned_inc (&eh_dbg, *addrp);
104 if (unlikely (*addrp + 8 > endp))
106 *ret = read_8ubyte_unaligned_inc (&eh_dbg, *addrp);
111 /* Returns true on error, false otherwise. */
112 static bool __attribute__ ((unused))
113 read_encoded_value (const Dwarf_CFI *cache, uint8_t encoding,
114 const uint8_t **p, Dwarf_Addr *result)
117 switch (encoding & 0x70)
119 case DW_EH_PE_absptr:
122 *result = (cache->frame_vaddr
123 + (*p - (const uint8_t *) cache->data->d.d_buf));
125 case DW_EH_PE_textrel:
127 *result = cache->textrel;
129 case DW_EH_PE_datarel:
132 *result = cache->datarel;
134 case DW_EH_PE_funcrel: /* XXX */
136 case DW_EH_PE_aligned:
138 const size_t size = encoded_value_size (&cache->data->d,
141 if (unlikely (size == 0))
143 size_t align = ((cache->frame_vaddr
144 + (*p - (const uint8_t *) cache->data->d.d_buf))
152 __libdw_seterrno (DWARF_E_INVALID_CFI);
156 Dwarf_Addr value = 0;
157 const unsigned char *endp = cache->data->d.d_buf + cache->data->d.d_size;
158 switch (encoding & 0x0f)
160 case DW_EH_PE_udata2:
161 if (unlikely (*p + 2 > endp))
164 __libdw_seterrno (DWARF_E_INVALID_CFI);
167 value = read_2ubyte_unaligned_inc (cache, *p);
170 case DW_EH_PE_sdata2:
171 if (unlikely (*p + 2 > endp))
173 value = read_2sbyte_unaligned_inc (cache, *p);
176 case DW_EH_PE_udata4:
177 if (unlikely (__libdw_cfi_read_address_inc (cache, p, 4, &value) != 0))
181 case DW_EH_PE_sdata4:
182 if (unlikely (__libdw_cfi_read_address_inc (cache, p, 4, &value) != 0))
184 value = (Dwarf_Sword) (Elf32_Sword) value; /* Sign-extend. */
187 case DW_EH_PE_udata8:
188 case DW_EH_PE_sdata8:
189 if (unlikely (__libdw_cfi_read_address_inc (cache, p, 8, &value) != 0))
193 case DW_EH_PE_absptr:
194 if (unlikely (__libdw_cfi_read_address_inc (cache, p, 0, &value) != 0))
198 case DW_EH_PE_uleb128:
199 get_uleb128 (value, *p, endp);
202 case DW_EH_PE_sleb128:
203 get_sleb128 (value, *p, endp);
207 __libdw_seterrno (DWARF_E_INVALID_CFI);
213 if (encoding & DW_EH_PE_indirect)
215 if (unlikely (*result < cache->frame_vaddr))
217 *result -= cache->frame_vaddr;
218 size_t ptrsize = encoded_value_size (NULL, cache->e_ident,
219 DW_EH_PE_absptr, NULL);
220 if (unlikely (cache->data->d.d_size < ptrsize
221 || *result > (cache->data->d.d_size - ptrsize)))
223 const uint8_t *ptr = cache->data->d.d_buf + *result;
224 if (unlikely (__libdw_cfi_read_address_inc (cache, &ptr, 0, result)
232 #endif /* encoded-value.h */