1 /* CFI program execution.
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/>. */
34 #include "../libebl/libebl.h"
36 #include "memory-access.h"
37 #include "encoded-value.h"
42 #define CFI_PRIMARY_MAX 0x3f
45 duplicate_frame_state (const Dwarf_Frame *original,
48 size_t size = offsetof (Dwarf_Frame, regs[original->nregs]);
49 Dwarf_Frame *copy = malloc (size);
50 if (likely (copy != NULL))
52 memcpy (copy, original, size);
58 /* Returns a DWARF_E_* error code, usually NOERROR or INVALID_CFI.
59 Frees *STATE on failure. */
61 execute_cfi (Dwarf_CFI *cache,
62 const struct dwarf_cie *cie,
64 const uint8_t *program, const uint8_t *const end, bool abi_cfi,
65 Dwarf_Addr loc, Dwarf_Addr find_pc)
67 /* The caller should not give us anything out of range. */
68 assert (loc <= find_pc);
70 int result = DWARF_E_NOERROR;
72 #define cfi_assert(ok) do { \
73 if (likely (ok)) break; \
74 result = DWARF_E_INVALID_CFI; \
78 Dwarf_Frame *fs = *state;
79 inline bool enough_registers (Dwarf_Word reg)
83 size_t size = offsetof (Dwarf_Frame, regs[reg + 1]);
84 Dwarf_Frame *bigger = realloc (fs, size);
85 if (unlikely (bigger == NULL))
87 result = DWARF_E_NOMEM;
92 bigger->nregs = reg + 1;
99 inline void require_cfa_offset (void)
101 if (unlikely (fs->cfa_rule != cfa_offset))
102 fs->cfa_rule = cfa_invalid;
105 #define register_rule(regno, r_rule, r_value) do { \
106 if (unlikely (! enough_registers (regno))) \
108 fs->regs[regno].rule = reg_##r_rule; \
109 fs->regs[regno].value = (r_value); \
112 while (program < end)
114 uint8_t opcode = *program++;
117 Dwarf_Word sf_offset;
118 Dwarf_Word operand = opcode & CFI_PRIMARY_MAX;
121 /* These cases move LOC, i.e. "create a new table row". */
123 case DW_CFA_advance_loc1:
124 operand = *program++;
125 case DW_CFA_advance_loc + 0 ... DW_CFA_advance_loc + CFI_PRIMARY_MAX:
127 loc += operand * cie->code_alignment_factor;
130 case DW_CFA_advance_loc2:
131 operand = read_2ubyte_unaligned_inc (cache, program);
133 case DW_CFA_advance_loc4:
134 operand = read_4ubyte_unaligned_inc (cache, program);
136 case DW_CFA_MIPS_advance_loc8:
137 operand = read_8ubyte_unaligned_inc (cache, program);
141 if (likely (!read_encoded_value (cache, cie->fde_encoding,
144 result = INTUSE(dwarf_errno) ();
147 /* Now all following cases affect this row, but do not touch LOC.
148 These cases end with 'continue'. We only get out of the
149 switch block for the row-copying (LOC-moving) cases above. */
152 get_uleb128 (operand, program);
153 get_uleb128 (offset, program);
155 fs->cfa_rule = cfa_offset;
156 fs->cfa_val_reg = operand;
157 fs->cfa_val_offset = offset;
158 /* Prime the rest of the Dwarf_Op so dwarf_frame_cfa can use it. */
159 fs->cfa_data.offset.atom = DW_OP_bregx;
160 fs->cfa_data.offset.offset = 0;
163 case DW_CFA_def_cfa_register:
164 get_uleb128 (regno, program);
165 require_cfa_offset ();
166 fs->cfa_val_reg = regno;
169 case DW_CFA_def_cfa_sf:
170 get_uleb128 (operand, program);
171 get_sleb128 (sf_offset, program);
172 offset = sf_offset * cie->data_alignment_factor;
175 case DW_CFA_def_cfa_offset:
176 get_uleb128 (offset, program);
178 require_cfa_offset ();
179 fs->cfa_val_offset = offset;
182 case DW_CFA_def_cfa_offset_sf:
183 get_sleb128 (sf_offset, program);
184 offset = sf_offset * cie->data_alignment_factor;
187 case DW_CFA_def_cfa_expression:
188 /* DW_FORM_block is a ULEB128 length followed by that many bytes. */
189 get_uleb128 (operand, program);
190 cfi_assert (operand <= (Dwarf_Word) (end - program));
191 fs->cfa_rule = cfa_expr;
192 fs->cfa_data.expr.data = (unsigned char *) program;
193 fs->cfa_data.expr.length = operand;
197 case DW_CFA_undefined:
198 get_uleb128 (regno, program);
199 register_rule (regno, undefined, 0);
202 case DW_CFA_same_value:
203 get_uleb128 (regno, program);
204 register_rule (regno, same_value, 0);
207 case DW_CFA_offset_extended:
208 get_uleb128 (operand, program);
209 case DW_CFA_offset + 0 ... DW_CFA_offset + CFI_PRIMARY_MAX:
210 get_uleb128 (offset, program);
211 offset *= cie->data_alignment_factor;
213 register_rule (operand, offset, offset);
216 case DW_CFA_offset_extended_sf:
217 get_uleb128 (operand, program);
218 get_sleb128 (sf_offset, program);
220 offset = sf_offset * cie->data_alignment_factor;
221 goto offset_extended;
223 case DW_CFA_GNU_negative_offset_extended:
224 /* GNU extension obsoleted by DW_CFA_offset_extended_sf. */
225 get_uleb128 (operand, program);
226 get_uleb128 (offset, program);
228 goto offset_extended_sf;
230 case DW_CFA_val_offset:
231 get_uleb128 (operand, program);
232 get_uleb128 (offset, program);
233 offset *= cie->data_alignment_factor;
235 register_rule (operand, val_offset, offset);
238 case DW_CFA_val_offset_sf:
239 get_uleb128 (operand, program);
240 get_sleb128 (sf_offset, program);
241 offset = sf_offset * cie->data_alignment_factor;
244 case DW_CFA_register:
245 get_uleb128 (regno, program);
246 get_uleb128 (operand, program);
247 register_rule (regno, register, operand);
250 case DW_CFA_expression:
251 get_uleb128 (regno, program);
252 offset = program - (const uint8_t *) cache->data->d.d_buf;
253 /* DW_FORM_block is a ULEB128 length followed by that many bytes. */
254 get_uleb128 (operand, program);
255 cfi_assert (operand <= (Dwarf_Word) (end - program));
257 register_rule (regno, expression, offset);
260 case DW_CFA_val_expression:
261 get_uleb128 (regno, program);
262 /* DW_FORM_block is a ULEB128 length followed by that many bytes. */
263 offset = program - (const uint8_t *) cache->data->d.d_buf;
264 get_uleb128 (operand, program);
265 cfi_assert (operand <= (Dwarf_Word) (end - program));
267 register_rule (regno, val_expression, offset);
270 case DW_CFA_restore_extended:
271 get_uleb128 (operand, program);
272 case DW_CFA_restore + 0 ... DW_CFA_restore + CFI_PRIMARY_MAX:
274 if (unlikely (abi_cfi) && likely (opcode == DW_CFA_restore))
276 /* Special case hack to give backend abi_cfi a shorthand. */
277 cache->default_same_value = true;
281 /* This can't be used in the CIE's own initial instructions. */
282 cfi_assert (cie->initial_state != NULL);
284 /* Restore the CIE's initial rule for this register. */
285 if (unlikely (! enough_registers (operand)))
287 if (cie->initial_state->nregs > operand)
288 fs->regs[operand] = cie->initial_state->regs[operand];
290 fs->regs[operand].rule = reg_unspecified;
293 case DW_CFA_remember_state:
295 /* Duplicate the state and chain the copy on. */
296 Dwarf_Frame *copy = duplicate_frame_state (fs, fs);
297 if (unlikely (copy == NULL))
299 result = DWARF_E_NOMEM;
306 case DW_CFA_restore_state:
308 /* Pop the current state off and use the old one instead. */
309 Dwarf_Frame *prev = fs->prev;
310 cfi_assert (prev != NULL);
319 case DW_CFA_GNU_window_save:
320 /* This is magic shorthand used only by SPARC. It's equivalent
321 to a bunch of DW_CFA_register and DW_CFA_offset operations. */
322 if (unlikely (! enough_registers (31)))
324 for (regno = 8; regno < 16; ++regno)
326 /* Find each %oN in %iN. */
327 fs->regs[regno].rule = reg_register;
328 fs->regs[regno].value = regno + 16;
330 unsigned int address_size = (cache->e_ident[EI_CLASS] == ELFCLASS32
332 for (; regno < 32; ++regno)
334 /* Find %l0..%l7 and %i0..%i7 in a block at the CFA. */
335 fs->regs[regno].rule = reg_offset;
336 fs->regs[regno].value = (regno - 16) * address_size;
340 case DW_CFA_GNU_args_size:
341 /* XXX is this useful for anything? */
342 get_uleb128 (operand, program);
350 /* We get here only for the cases that have just moved LOC. */
351 cfi_assert (cie->initial_state != NULL);
353 /* This advance has not yet reached FIND_PC. */
357 /* We have just advanced past the address we're looking for.
358 The state currently described is what we want to see. */
364 /* "The end of the instruction stream can be thought of as a
365 DW_CFA_set_loc (initial_location + address_range) instruction."
366 (DWARF 3.0 Section 6.4.3)
368 When we fall off the end of the program without an advance_loc/set_loc
369 that put us past FIND_PC, the final state left by the FDE program
370 applies to this address (the caller ensured it was inside the FDE).
371 This address (FDE->end) is already in FS->end as set by the caller. */
378 /* Pop any remembered states left on the stack. */
379 while (fs->prev != NULL)
381 Dwarf_Frame *prev = fs->prev;
382 fs->prev = prev->prev;
386 if (likely (result == DWARF_E_NOERROR))
395 cie_cache_initial_state (Dwarf_CFI *cache, struct dwarf_cie *cie)
397 int result = DWARF_E_NOERROR;
399 if (likely (cie->initial_state != NULL))
402 /* This CIE has not been used before. Play out its initial
403 instructions and cache the initial state that results.
404 First we'll let the backend fill in the default initial
405 state for this machine's ABI. */
407 Dwarf_CIE abi_info = { DW_CIE_ID_64, NULL, NULL, 1, 1, -1, "", NULL, 0, 0 };
409 /* Make sure we have a backend handle cached. */
410 if (unlikely (cache->ebl == NULL))
412 cache->ebl = ebl_openbackend (cache->data->s->elf);
413 if (unlikely (cache->ebl == NULL))
414 cache->ebl = (void *) -1l;
417 /* Fetch the ABI's default CFI program. */
418 if (likely (cache->ebl != (void *) -1l)
419 && unlikely (ebl_abi_cfi (cache->ebl, &abi_info) < 0))
420 return DWARF_E_UNKNOWN_ERROR;
422 Dwarf_Frame *cie_fs = calloc (1, sizeof (Dwarf_Frame));
423 if (unlikely (cie_fs == NULL))
424 return DWARF_E_NOMEM;
426 /* If the default state of any register is not "undefined"
427 (i.e. call-clobbered), then the backend supplies instructions
428 for the standard initial state. */
429 if (abi_info.initial_instructions_end > abi_info.initial_instructions)
431 /* Dummy CIE for backend's instructions. */
432 struct dwarf_cie abi_cie =
434 .code_alignment_factor = abi_info.code_alignment_factor,
435 .data_alignment_factor = abi_info.data_alignment_factor,
437 result = execute_cfi (cache, &abi_cie, &cie_fs,
438 abi_info.initial_instructions,
439 abi_info.initial_instructions_end, true,
440 0, (Dwarf_Addr) -1l);
443 /* Now run the CIE's initial instructions. */
444 if (cie->initial_instructions_end > cie->initial_instructions
445 && likely (result == DWARF_E_NOERROR))
446 result = execute_cfi (cache, cie, &cie_fs,
447 cie->initial_instructions,
448 cie->initial_instructions_end, false,
449 0, (Dwarf_Addr) -1l);
451 if (likely (result == DWARF_E_NOERROR))
453 /* Now we have the initial state of things that all
454 FDEs using this CIE will start from. */
455 cie_fs->cache = cache;
456 cie->initial_state = cie_fs;
464 __libdw_frame_at_address (Dwarf_CFI *cache, struct dwarf_fde *fde,
465 Dwarf_Addr address, Dwarf_Frame **frame)
467 int result = cie_cache_initial_state (cache, fde->cie);
468 if (likely (result == DWARF_E_NOERROR))
470 Dwarf_Frame *fs = duplicate_frame_state (fde->cie->initial_state, NULL);
471 if (unlikely (fs == NULL))
472 return DWARF_E_NOMEM;
475 fs->start = fde->start;
478 result = execute_cfi (cache, fde->cie, &fs,
479 fde->instructions, fde->instructions_end, false,
480 fde->start, address);
481 if (likely (result == DWARF_E_NOERROR))