1 /* DW_EH_PE_* support for libdw unwinder.
2 Copyright (C) 2009-2010 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
37 static size_t __attribute__ ((unused))
38 encoded_value_size (const Elf_Data *data, const unsigned char e_ident[],
39 uint8_t encoding, const uint8_t *p)
41 if (encoding == DW_EH_PE_omit)
44 switch (encoding & 0x07)
54 return e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
56 case DW_EH_PE_uleb128:
59 const uint8_t *end = p;
60 while (end < (uint8_t *) data->d_buf + data->d_size)
71 static inline int __attribute__ ((unused))
72 __libdw_cfi_read_address_inc (const Dwarf_CFI *cache,
73 const unsigned char **addrp,
74 int width, Dwarf_Addr *ret)
76 width = width ?: cache->e_ident[EI_CLASS] == ELFCLASS32 ? 4 : 8;
78 if (cache->dbg != NULL)
79 return __libdw_read_address_inc (cache->dbg, IDX_debug_frame,
82 /* Only .debug_frame might have relocation to consider.
83 Read plain values from .eh_frame data. */
86 *ret = read_4ubyte_unaligned_inc (cache, *addrp);
88 *ret = read_8ubyte_unaligned_inc (cache, *addrp);
92 static bool __attribute__ ((unused))
93 read_encoded_value (const Dwarf_CFI *cache, uint8_t encoding, const uint8_t **p,
97 switch (encoding & 0x70)
102 *result = (cache->frame_vaddr
103 + (*p - (const uint8_t *) cache->data->d.d_buf));
105 case DW_EH_PE_textrel:
107 *result = cache->textrel;
109 case DW_EH_PE_datarel:
112 *result = cache->datarel;
114 case DW_EH_PE_funcrel: /* XXX */
116 case DW_EH_PE_aligned:
118 const size_t size = encoded_value_size (&cache->data->d, cache->e_ident,
120 size_t align = ((cache->frame_vaddr
121 + (*p - (const uint8_t *) cache->data->d.d_buf))
133 switch (encoding & 0x0f)
135 case DW_EH_PE_udata2:
136 value = read_2ubyte_unaligned_inc (cache, *p);
139 case DW_EH_PE_sdata2:
140 value = read_2sbyte_unaligned_inc (cache, *p);
143 case DW_EH_PE_udata4:
144 if (__libdw_cfi_read_address_inc (cache, p, 4, &value))
148 case DW_EH_PE_sdata4:
149 if (__libdw_cfi_read_address_inc (cache, p, 4, &value))
151 value = (Dwarf_Sword) (Elf32_Sword) value; /* Sign-extend. */
154 case DW_EH_PE_udata8:
155 case DW_EH_PE_sdata8:
156 if (__libdw_cfi_read_address_inc (cache, p, 8, &value))
160 case DW_EH_PE_absptr:
161 if (__libdw_cfi_read_address_inc (cache, p, 0, &value))
165 case DW_EH_PE_uleb128:
166 get_uleb128 (value, *p);
169 case DW_EH_PE_sleb128:
170 get_sleb128 (value, *p);
179 if (encoding & DW_EH_PE_indirect)
181 if (unlikely (*result < cache->frame_vaddr))
183 *result -= cache->frame_vaddr;
184 if (unlikely (*result > (cache->data->d.d_size
185 - encoded_value_size (NULL, cache->e_ident,
186 DW_EH_PE_absptr, NULL))))
188 const uint8_t *ptr = cache->data->d.d_buf + *result;
189 return __libdw_cfi_read_address_inc (cache, &ptr, 0, result);
195 #endif /* encoded-value.h */