1 /* CFI program execution.
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/>. */
34 #include "../libebl/libebl.h"
36 #include "memory-access.h"
37 #include "encoded-value.h"
43 #define CFI_PRIMARY_MAX 0x3f
46 duplicate_frame_state (const Dwarf_Frame *original,
49 size_t size = offsetof (Dwarf_Frame, regs[original->nregs]);
50 Dwarf_Frame *copy = malloc (size);
51 if (likely (copy != NULL))
53 memcpy (copy, original, size);
60 enough_registers (Dwarf_Word reg, Dwarf_Frame **pfs, int *result)
62 /* Don't allow insanely large register numbers. 268435456 registers
63 should be enough for anybody. And very large values might overflow
64 the array size and offsetof calculations below. */
65 if (unlikely (reg >= INT32_MAX / sizeof ((*pfs)->regs[0])))
67 *result = DWARF_E_INVALID_CFI;
71 if ((*pfs)->nregs <= reg)
73 size_t size = offsetof (Dwarf_Frame, regs[reg + 1]);
74 Dwarf_Frame *bigger = realloc (*pfs, size);
75 if (unlikely (bigger == NULL))
77 *result = DWARF_E_NOMEM;
82 eu_static_assert (reg_unspecified == 0);
83 memset (bigger->regs + bigger->nregs, 0,
84 (reg + 1 - bigger->nregs) * sizeof bigger->regs[0]);
85 bigger->nregs = reg + 1;
93 require_cfa_offset (Dwarf_Frame *fs)
95 if (unlikely (fs->cfa_rule != cfa_offset))
96 fs->cfa_rule = cfa_invalid;
99 /* Returns a DWARF_E_* error code, usually NOERROR or INVALID_CFI.
100 Frees *STATE on failure. */
102 execute_cfi (Dwarf_CFI *cache,
103 const struct dwarf_cie *cie,
105 const uint8_t *program, const uint8_t *const end, bool abi_cfi,
106 Dwarf_Addr loc, Dwarf_Addr find_pc)
108 /* The caller should not give us anything out of range. */
109 assert (loc <= find_pc);
111 int result = DWARF_E_NOERROR;
113 #define cfi_assert(ok) do { \
114 if (likely (ok)) break; \
115 result = DWARF_E_INVALID_CFI; \
119 Dwarf_Frame *fs = *state;
121 #define register_rule(regno, r_rule, r_value) do { \
122 if (unlikely (! enough_registers (regno, &fs, &result))) \
124 fs->regs[regno].rule = reg_##r_rule; \
125 fs->regs[regno].value = (r_value); \
128 while (program < end)
130 uint8_t opcode = *program++;
133 Dwarf_Word sf_offset;
134 Dwarf_Word operand = opcode & CFI_PRIMARY_MAX;
137 /* These cases move LOC, i.e. "create a new table row". */
139 case DW_CFA_advance_loc1:
140 operand = *program++;
141 case DW_CFA_advance_loc + 0 ... DW_CFA_advance_loc + CFI_PRIMARY_MAX:
143 loc += operand * cie->code_alignment_factor;
146 case DW_CFA_advance_loc2:
147 cfi_assert (program + 2 <= end);
148 operand = read_2ubyte_unaligned_inc (cache, program);
150 case DW_CFA_advance_loc4:
151 cfi_assert (program + 4 <= end);
152 operand = read_4ubyte_unaligned_inc (cache, program);
154 case DW_CFA_MIPS_advance_loc8:
155 cfi_assert (program + 8 <= end);
156 operand = read_8ubyte_unaligned_inc (cache, program);
160 if (likely (!read_encoded_value (cache, cie->fde_encoding,
163 result = INTUSE(dwarf_errno) ();
166 /* Now all following cases affect this row, but do not touch LOC.
167 These cases end with 'continue'. We only get out of the
168 switch block for the row-copying (LOC-moving) cases above. */
171 get_uleb128 (operand, program, end);
172 cfi_assert (program < end);
173 get_uleb128 (offset, program, end);
175 fs->cfa_rule = cfa_offset;
176 fs->cfa_val_reg = operand;
177 fs->cfa_val_offset = offset;
178 /* Prime the rest of the Dwarf_Op so dwarf_frame_cfa can use it. */
179 fs->cfa_data.offset.atom = DW_OP_bregx;
180 fs->cfa_data.offset.offset = 0;
183 case DW_CFA_def_cfa_register:
184 get_uleb128 (regno, program, end);
185 require_cfa_offset (fs);
186 fs->cfa_val_reg = regno;
189 case DW_CFA_def_cfa_sf:
190 get_uleb128 (operand, program, end);
191 cfi_assert (program < end);
192 get_sleb128 (sf_offset, program, end);
193 offset = sf_offset * cie->data_alignment_factor;
196 case DW_CFA_def_cfa_offset:
197 get_uleb128 (offset, program, end);
199 require_cfa_offset (fs);
200 fs->cfa_val_offset = offset;
203 case DW_CFA_def_cfa_offset_sf:
204 get_sleb128 (sf_offset, program, end);
205 offset = sf_offset * cie->data_alignment_factor;
208 case DW_CFA_def_cfa_expression:
209 /* DW_FORM_block is a ULEB128 length followed by that many bytes. */
210 get_uleb128 (operand, program, end);
211 cfi_assert (operand <= (Dwarf_Word) (end - program));
212 fs->cfa_rule = cfa_expr;
213 fs->cfa_data.expr.data = (unsigned char *) program;
214 fs->cfa_data.expr.length = operand;
218 case DW_CFA_undefined:
219 get_uleb128 (regno, program, end);
220 register_rule (regno, undefined, 0);
223 case DW_CFA_same_value:
224 get_uleb128 (regno, program, end);
225 register_rule (regno, same_value, 0);
228 case DW_CFA_offset_extended:
229 get_uleb128 (operand, program, end);
230 cfi_assert (program < end);
231 case DW_CFA_offset + 0 ... DW_CFA_offset + CFI_PRIMARY_MAX:
232 get_uleb128 (offset, program, end);
233 offset *= cie->data_alignment_factor;
235 register_rule (operand, offset, offset);
238 case DW_CFA_offset_extended_sf:
239 get_uleb128 (operand, program, end);
240 get_sleb128 (sf_offset, program, end);
242 offset = sf_offset * cie->data_alignment_factor;
243 goto offset_extended;
245 case DW_CFA_GNU_negative_offset_extended:
246 /* GNU extension obsoleted by DW_CFA_offset_extended_sf. */
247 get_uleb128 (operand, program, end);
248 cfi_assert (program < end);
249 get_uleb128 (offset, program, end);
251 goto offset_extended_sf;
253 case DW_CFA_val_offset:
254 get_uleb128 (operand, program, end);
255 cfi_assert (program < end);
256 get_uleb128 (offset, program, end);
257 offset *= cie->data_alignment_factor;
259 register_rule (operand, val_offset, offset);
262 case DW_CFA_val_offset_sf:
263 get_uleb128 (operand, program, end);
264 cfi_assert (program < end);
265 get_sleb128 (sf_offset, program, end);
266 offset = sf_offset * cie->data_alignment_factor;
269 case DW_CFA_register:
270 get_uleb128 (regno, program, end);
271 cfi_assert (program < end);
272 get_uleb128 (operand, program, end);
273 register_rule (regno, register, operand);
276 case DW_CFA_expression:
277 /* Expression rule relies on section data, abi_cfi cannot use it. */
279 get_uleb128 (regno, program, end);
280 offset = program - (const uint8_t *) cache->data->d.d_buf;
281 /* DW_FORM_block is a ULEB128 length followed by that many bytes. */
282 cfi_assert (program < end);
283 get_uleb128 (operand, program, end);
284 cfi_assert (operand <= (Dwarf_Word) (end - program));
286 register_rule (regno, expression, offset);
289 case DW_CFA_val_expression:
290 /* Expression rule relies on section data, abi_cfi cannot use it. */
292 get_uleb128 (regno, program, end);
293 /* DW_FORM_block is a ULEB128 length followed by that many bytes. */
294 offset = program - (const uint8_t *) cache->data->d.d_buf;
295 get_uleb128 (operand, program, end);
296 cfi_assert (operand <= (Dwarf_Word) (end - program));
298 register_rule (regno, val_expression, offset);
301 case DW_CFA_restore_extended:
302 get_uleb128 (operand, program, end);
303 case DW_CFA_restore + 0 ... DW_CFA_restore + CFI_PRIMARY_MAX:
305 if (unlikely (abi_cfi) && likely (opcode == DW_CFA_restore))
307 /* Special case hack to give backend abi_cfi a shorthand. */
308 cache->default_same_value = true;
312 /* This can't be used in the CIE's own initial instructions. */
313 cfi_assert (cie->initial_state != NULL);
315 /* Restore the CIE's initial rule for this register. */
316 if (unlikely (! enough_registers (operand, &fs, &result)))
318 if (cie->initial_state->nregs > operand)
319 fs->regs[operand] = cie->initial_state->regs[operand];
321 fs->regs[operand].rule = reg_unspecified;
324 case DW_CFA_remember_state:
326 /* Duplicate the state and chain the copy on. */
327 Dwarf_Frame *copy = duplicate_frame_state (fs, fs);
328 if (unlikely (copy == NULL))
330 result = DWARF_E_NOMEM;
337 case DW_CFA_restore_state:
339 /* Pop the current state off and use the old one instead. */
340 Dwarf_Frame *prev = fs->prev;
341 cfi_assert (prev != NULL);
350 case DW_CFA_GNU_window_save:
351 /* This is magic shorthand used only by SPARC. It's equivalent
352 to a bunch of DW_CFA_register and DW_CFA_offset operations. */
353 if (unlikely (! enough_registers (31, &fs, &result)))
355 for (regno = 8; regno < 16; ++regno)
357 /* Find each %oN in %iN. */
358 fs->regs[regno].rule = reg_register;
359 fs->regs[regno].value = regno + 16;
361 unsigned int address_size = (cache->e_ident[EI_CLASS] == ELFCLASS32
363 for (; regno < 32; ++regno)
365 /* Find %l0..%l7 and %i0..%i7 in a block at the CFA. */
366 fs->regs[regno].rule = reg_offset;
367 fs->regs[regno].value = (regno - 16) * address_size;
371 case DW_CFA_GNU_args_size:
372 /* XXX is this useful for anything? */
373 get_uleb128 (operand, program, end);
381 /* We get here only for the cases that have just moved LOC. */
382 cfi_assert (cie->initial_state != NULL);
384 /* This advance has not yet reached FIND_PC. */
388 /* We have just advanced past the address we're looking for.
389 The state currently described is what we want to see. */
395 /* "The end of the instruction stream can be thought of as a
396 DW_CFA_set_loc (initial_location + address_range) instruction."
397 (DWARF 3.0 Section 6.4.3)
399 When we fall off the end of the program without an advance_loc/set_loc
400 that put us past FIND_PC, the final state left by the FDE program
401 applies to this address (the caller ensured it was inside the FDE).
402 This address (FDE->end) is already in FS->end as set by the caller. */
409 /* Pop any remembered states left on the stack. */
410 while (fs->prev != NULL)
412 Dwarf_Frame *prev = fs->prev;
413 fs->prev = prev->prev;
417 if (likely (result == DWARF_E_NOERROR))
426 cie_cache_initial_state (Dwarf_CFI *cache, struct dwarf_cie *cie)
428 int result = DWARF_E_NOERROR;
430 if (likely (cie->initial_state != NULL))
433 /* This CIE has not been used before. Play out its initial
434 instructions and cache the initial state that results.
435 First we'll let the backend fill in the default initial
436 state for this machine's ABI. */
438 Dwarf_CIE abi_info = { DW_CIE_ID_64, NULL, NULL, 1, 1, -1, "", NULL, 0, 0 };
440 /* Make sure we have a backend handle cached. */
441 if (unlikely (cache->ebl == NULL))
443 cache->ebl = ebl_openbackend (cache->data->s->elf);
444 if (unlikely (cache->ebl == NULL))
445 cache->ebl = (void *) -1l;
448 /* Fetch the ABI's default CFI program. */
449 if (likely (cache->ebl != (void *) -1l)
450 && unlikely (ebl_abi_cfi (cache->ebl, &abi_info) < 0))
451 return DWARF_E_UNKNOWN_ERROR;
453 Dwarf_Frame *cie_fs = calloc (1, sizeof (Dwarf_Frame));
454 if (unlikely (cie_fs == NULL))
455 return DWARF_E_NOMEM;
457 /* If the default state of any register is not "undefined"
458 (i.e. call-clobbered), then the backend supplies instructions
459 for the standard initial state. */
460 if (abi_info.initial_instructions_end > abi_info.initial_instructions)
462 /* Dummy CIE for backend's instructions. */
463 struct dwarf_cie abi_cie =
465 .code_alignment_factor = abi_info.code_alignment_factor,
466 .data_alignment_factor = abi_info.data_alignment_factor,
468 result = execute_cfi (cache, &abi_cie, &cie_fs,
469 abi_info.initial_instructions,
470 abi_info.initial_instructions_end, true,
471 0, (Dwarf_Addr) -1l);
474 /* Now run the CIE's initial instructions. */
475 if (cie->initial_instructions_end > cie->initial_instructions
476 && likely (result == DWARF_E_NOERROR))
477 result = execute_cfi (cache, cie, &cie_fs,
478 cie->initial_instructions,
479 cie->initial_instructions_end, false,
480 0, (Dwarf_Addr) -1l);
482 if (likely (result == DWARF_E_NOERROR))
484 /* Now we have the initial state of things that all
485 FDEs using this CIE will start from. */
486 cie_fs->cache = cache;
487 cie->initial_state = cie_fs;
495 __libdw_frame_at_address (Dwarf_CFI *cache, struct dwarf_fde *fde,
496 Dwarf_Addr address, Dwarf_Frame **frame)
498 int result = cie_cache_initial_state (cache, fde->cie);
499 if (likely (result == DWARF_E_NOERROR))
501 Dwarf_Frame *fs = duplicate_frame_state (fde->cie->initial_state, NULL);
502 if (unlikely (fs == NULL))
503 return DWARF_E_NOMEM;
506 fs->start = fde->start;
509 result = execute_cfi (cache, fde->cie, &fs,
510 fde->instructions, fde->instructions_end, false,
511 fde->start, address);
512 if (likely (result == DWARF_E_NOERROR))