* gas/macros/test1.s: Rename symbols to avoid conflicts with
[deliverable/binutils-gdb.git] / gdb / dwarf2-frame.c
CommitLineData
cfc14b3a
MK
1/* Frame unwinder for frames with DWARF Call Frame Information.
2
9b254dd1 3 Copyright (C) 2003, 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
cfc14b3a
MK
4
5 Contributed by Mark Kettenis.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
a9762ec7 11 the Free Software Foundation; either version 3 of the License, or
cfc14b3a
MK
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
a9762ec7 20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
cfc14b3a
MK
21
22#include "defs.h"
23#include "dwarf2expr.h"
24#include "elf/dwarf2.h"
25#include "frame.h"
26#include "frame-base.h"
27#include "frame-unwind.h"
28#include "gdbcore.h"
29#include "gdbtypes.h"
30#include "symtab.h"
31#include "objfiles.h"
32#include "regcache.h"
f2da6b3a 33#include "value.h"
cfc14b3a
MK
34
35#include "gdb_assert.h"
36#include "gdb_string.h"
37
6896c0c7 38#include "complaints.h"
cfc14b3a
MK
39#include "dwarf2-frame.h"
40
41/* Call Frame Information (CFI). */
42
43/* Common Information Entry (CIE). */
44
45struct dwarf2_cie
46{
47 /* Offset into the .debug_frame section where this CIE was found.
48 Used to identify this CIE. */
49 ULONGEST cie_pointer;
50
51 /* Constant that is factored out of all advance location
52 instructions. */
53 ULONGEST code_alignment_factor;
54
55 /* Constants that is factored out of all offset instructions. */
56 LONGEST data_alignment_factor;
57
58 /* Return address column. */
59 ULONGEST return_address_register;
60
61 /* Instruction sequence to initialize a register set. */
852483bc
MK
62 gdb_byte *initial_instructions;
63 gdb_byte *end;
cfc14b3a 64
303b6f5d
DJ
65 /* Saved augmentation, in case it's needed later. */
66 char *augmentation;
67
cfc14b3a 68 /* Encoding of addresses. */
852483bc 69 gdb_byte encoding;
cfc14b3a 70
7131cb6e
RH
71 /* True if a 'z' augmentation existed. */
72 unsigned char saw_z_augmentation;
73
56c987f6
AO
74 /* True if an 'S' augmentation existed. */
75 unsigned char signal_frame;
76
303b6f5d
DJ
77 /* The version recorded in the CIE. */
78 unsigned char version;
79
cfc14b3a
MK
80 struct dwarf2_cie *next;
81};
82
83/* Frame Description Entry (FDE). */
84
85struct dwarf2_fde
86{
87 /* CIE for this FDE. */
88 struct dwarf2_cie *cie;
89
90 /* First location associated with this FDE. */
91 CORE_ADDR initial_location;
92
93 /* Number of bytes of program instructions described by this FDE. */
94 CORE_ADDR address_range;
95
96 /* Instruction sequence. */
852483bc
MK
97 gdb_byte *instructions;
98 gdb_byte *end;
cfc14b3a 99
4bf8967c
AS
100 /* True if this FDE is read from a .eh_frame instead of a .debug_frame
101 section. */
102 unsigned char eh_frame_p;
103
cfc14b3a
MK
104 struct dwarf2_fde *next;
105};
106
107static struct dwarf2_fde *dwarf2_frame_find_fde (CORE_ADDR *pc);
4fc771b8
DJ
108
109static int dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum,
110 int eh_frame_p);
cfc14b3a
MK
111\f
112
113/* Structure describing a frame state. */
114
115struct dwarf2_frame_state
116{
117 /* Each register save state can be described in terms of a CFA slot,
118 another register, or a location expression. */
119 struct dwarf2_frame_state_reg_info
120 {
05cbe71a 121 struct dwarf2_frame_state_reg *reg;
cfc14b3a
MK
122 int num_regs;
123
124 /* Used to implement DW_CFA_remember_state. */
125 struct dwarf2_frame_state_reg_info *prev;
126 } regs;
127
128 LONGEST cfa_offset;
129 ULONGEST cfa_reg;
852483bc 130 gdb_byte *cfa_exp;
cfc14b3a
MK
131 enum {
132 CFA_UNSET,
133 CFA_REG_OFFSET,
134 CFA_EXP
135 } cfa_how;
136
137 /* The PC described by the current frame state. */
138 CORE_ADDR pc;
139
140 /* Initial register set from the CIE.
141 Used to implement DW_CFA_restore. */
142 struct dwarf2_frame_state_reg_info initial;
143
144 /* The information we care about from the CIE. */
145 LONGEST data_align;
146 ULONGEST code_align;
147 ULONGEST retaddr_column;
303b6f5d
DJ
148
149 /* Flags for known producer quirks. */
150
151 /* The ARM compilers, in DWARF2 mode, assume that DW_CFA_def_cfa
152 and DW_CFA_def_cfa_offset takes a factored offset. */
153 int armcc_cfa_offsets_sf;
154
155 /* The ARM compilers, in DWARF2 or DWARF3 mode, may assume that
156 the CFA is defined as REG - OFFSET rather than REG + OFFSET. */
157 int armcc_cfa_offsets_reversed;
cfc14b3a
MK
158};
159
160/* Store the length the expression for the CFA in the `cfa_reg' field,
161 which is unused in that case. */
162#define cfa_exp_len cfa_reg
163
f57d151a 164/* Assert that the register set RS is large enough to store gdbarch_num_regs
cfc14b3a
MK
165 columns. If necessary, enlarge the register set. */
166
167static void
168dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info *rs,
169 int num_regs)
170{
171 size_t size = sizeof (struct dwarf2_frame_state_reg);
172
173 if (num_regs <= rs->num_regs)
174 return;
175
176 rs->reg = (struct dwarf2_frame_state_reg *)
177 xrealloc (rs->reg, num_regs * size);
178
179 /* Initialize newly allocated registers. */
2473a4a9 180 memset (rs->reg + rs->num_regs, 0, (num_regs - rs->num_regs) * size);
cfc14b3a
MK
181 rs->num_regs = num_regs;
182}
183
184/* Copy the register columns in register set RS into newly allocated
185 memory and return a pointer to this newly created copy. */
186
187static struct dwarf2_frame_state_reg *
188dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info *rs)
189{
d10891d4 190 size_t size = rs->num_regs * sizeof (struct dwarf2_frame_state_reg);
cfc14b3a
MK
191 struct dwarf2_frame_state_reg *reg;
192
193 reg = (struct dwarf2_frame_state_reg *) xmalloc (size);
194 memcpy (reg, rs->reg, size);
195
196 return reg;
197}
198
199/* Release the memory allocated to register set RS. */
200
201static void
202dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info *rs)
203{
204 if (rs)
205 {
206 dwarf2_frame_state_free_regs (rs->prev);
207
208 xfree (rs->reg);
209 xfree (rs);
210 }
211}
212
213/* Release the memory allocated to the frame state FS. */
214
215static void
216dwarf2_frame_state_free (void *p)
217{
218 struct dwarf2_frame_state *fs = p;
219
220 dwarf2_frame_state_free_regs (fs->initial.prev);
221 dwarf2_frame_state_free_regs (fs->regs.prev);
222 xfree (fs->initial.reg);
223 xfree (fs->regs.reg);
224 xfree (fs);
225}
226\f
227
228/* Helper functions for execute_stack_op. */
229
230static CORE_ADDR
231read_reg (void *baton, int reg)
232{
233 struct frame_info *next_frame = (struct frame_info *) baton;
05cbe71a 234 struct gdbarch *gdbarch = get_frame_arch (next_frame);
cfc14b3a 235 int regnum;
852483bc 236 gdb_byte *buf;
cfc14b3a 237
ad010def 238 regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, reg);
cfc14b3a 239
852483bc 240 buf = alloca (register_size (gdbarch, regnum));
cfc14b3a 241 frame_unwind_register (next_frame, regnum, buf);
f2da6b3a
DJ
242
243 /* Convert the register to an integer. This returns a LONGEST
244 rather than a CORE_ADDR, but unpack_pointer does the same thing
245 under the covers, and this makes more sense for non-pointer
246 registers. Maybe read_reg and the associated interfaces should
247 deal with "struct value" instead of CORE_ADDR. */
248 return unpack_long (register_type (gdbarch, regnum), buf);
cfc14b3a
MK
249}
250
251static void
852483bc 252read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr, size_t len)
cfc14b3a
MK
253{
254 read_memory (addr, buf, len);
255}
256
257static void
852483bc 258no_get_frame_base (void *baton, gdb_byte **start, size_t *length)
cfc14b3a
MK
259{
260 internal_error (__FILE__, __LINE__,
e2e0b3e5 261 _("Support for DW_OP_fbreg is unimplemented"));
cfc14b3a
MK
262}
263
264static CORE_ADDR
265no_get_tls_address (void *baton, CORE_ADDR offset)
266{
267 internal_error (__FILE__, __LINE__,
e2e0b3e5 268 _("Support for DW_OP_GNU_push_tls_address is unimplemented"));
cfc14b3a
MK
269}
270
a6a5a945
LM
271/* Execute the required actions for both the DW_CFA_restore and
272DW_CFA_restore_extended instructions. */
273static void
274dwarf2_restore_rule (struct gdbarch *gdbarch, ULONGEST reg_num,
275 struct dwarf2_frame_state *fs, int eh_frame_p)
276{
277 ULONGEST reg;
278
279 gdb_assert (fs->initial.reg);
280 reg = dwarf2_frame_adjust_regnum (gdbarch, reg_num, eh_frame_p);
281 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
282
283 /* Check if this register was explicitly initialized in the
284 CIE initial instructions. If not, default the rule to
285 UNSPECIFIED. */
286 if (reg < fs->initial.num_regs)
287 fs->regs.reg[reg] = fs->initial.reg[reg];
288 else
289 fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNSPECIFIED;
290
291 if (fs->regs.reg[reg].how == DWARF2_FRAME_REG_UNSPECIFIED)
292 complaint (&symfile_complaints, _("\
293incomplete CFI data; DW_CFA_restore unspecified\n\
294register %s (#%d) at 0x%s"),
295 gdbarch_register_name
296 (gdbarch, gdbarch_dwarf2_reg_to_regnum (gdbarch, reg)),
297 gdbarch_dwarf2_reg_to_regnum (gdbarch, reg),
298 paddr (fs->pc));
299}
300
cfc14b3a 301static CORE_ADDR
852483bc 302execute_stack_op (gdb_byte *exp, ULONGEST len,
cfc14b3a
MK
303 struct frame_info *next_frame, CORE_ADDR initial)
304{
305 struct dwarf_expr_context *ctx;
306 CORE_ADDR result;
307
308 ctx = new_dwarf_expr_context ();
309 ctx->baton = next_frame;
310 ctx->read_reg = read_reg;
311 ctx->read_mem = read_mem;
312 ctx->get_frame_base = no_get_frame_base;
313 ctx->get_tls_address = no_get_tls_address;
314
315 dwarf_expr_push (ctx, initial);
316 dwarf_expr_eval (ctx, exp, len);
317 result = dwarf_expr_fetch (ctx, 0);
318
319 if (ctx->in_reg)
320 result = read_reg (next_frame, result);
321
322 free_dwarf_expr_context (ctx);
323
324 return result;
325}
326\f
327
328static void
852483bc 329execute_cfa_program (gdb_byte *insn_ptr, gdb_byte *insn_end,
cfc14b3a 330 struct frame_info *next_frame,
4bf8967c 331 struct dwarf2_frame_state *fs, int eh_frame_p)
cfc14b3a
MK
332{
333 CORE_ADDR pc = frame_pc_unwind (next_frame);
334 int bytes_read;
4bf8967c 335 struct gdbarch *gdbarch = get_frame_arch (next_frame);
cfc14b3a
MK
336
337 while (insn_ptr < insn_end && fs->pc <= pc)
338 {
852483bc 339 gdb_byte insn = *insn_ptr++;
cfc14b3a
MK
340 ULONGEST utmp, reg;
341 LONGEST offset;
342
343 if ((insn & 0xc0) == DW_CFA_advance_loc)
344 fs->pc += (insn & 0x3f) * fs->code_align;
345 else if ((insn & 0xc0) == DW_CFA_offset)
346 {
347 reg = insn & 0x3f;
4fc771b8 348 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
cfc14b3a
MK
349 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
350 offset = utmp * fs->data_align;
351 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
05cbe71a 352 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
cfc14b3a
MK
353 fs->regs.reg[reg].loc.offset = offset;
354 }
355 else if ((insn & 0xc0) == DW_CFA_restore)
356 {
cfc14b3a 357 reg = insn & 0x3f;
a6a5a945 358 dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
cfc14b3a
MK
359 }
360 else
361 {
362 switch (insn)
363 {
364 case DW_CFA_set_loc:
365 fs->pc = dwarf2_read_address (insn_ptr, insn_end, &bytes_read);
366 insn_ptr += bytes_read;
367 break;
368
369 case DW_CFA_advance_loc1:
370 utmp = extract_unsigned_integer (insn_ptr, 1);
371 fs->pc += utmp * fs->code_align;
372 insn_ptr++;
373 break;
374 case DW_CFA_advance_loc2:
375 utmp = extract_unsigned_integer (insn_ptr, 2);
376 fs->pc += utmp * fs->code_align;
377 insn_ptr += 2;
378 break;
379 case DW_CFA_advance_loc4:
380 utmp = extract_unsigned_integer (insn_ptr, 4);
381 fs->pc += utmp * fs->code_align;
382 insn_ptr += 4;
383 break;
384
385 case DW_CFA_offset_extended:
386 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
4fc771b8 387 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
cfc14b3a
MK
388 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
389 offset = utmp * fs->data_align;
390 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
05cbe71a 391 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
cfc14b3a
MK
392 fs->regs.reg[reg].loc.offset = offset;
393 break;
394
395 case DW_CFA_restore_extended:
cfc14b3a 396 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
a6a5a945 397 dwarf2_restore_rule (gdbarch, reg, fs, eh_frame_p);
cfc14b3a
MK
398 break;
399
400 case DW_CFA_undefined:
401 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
4fc771b8 402 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
cfc14b3a 403 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
05cbe71a 404 fs->regs.reg[reg].how = DWARF2_FRAME_REG_UNDEFINED;
cfc14b3a
MK
405 break;
406
407 case DW_CFA_same_value:
408 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
4fc771b8 409 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
cfc14b3a 410 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
05cbe71a 411 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAME_VALUE;
cfc14b3a
MK
412 break;
413
414 case DW_CFA_register:
415 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
4fc771b8 416 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
cfc14b3a 417 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
4fc771b8 418 utmp = dwarf2_frame_adjust_regnum (gdbarch, utmp, eh_frame_p);
cfc14b3a 419 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
05cbe71a 420 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
cfc14b3a
MK
421 fs->regs.reg[reg].loc.reg = utmp;
422 break;
423
424 case DW_CFA_remember_state:
425 {
426 struct dwarf2_frame_state_reg_info *new_rs;
427
428 new_rs = XMALLOC (struct dwarf2_frame_state_reg_info);
429 *new_rs = fs->regs;
430 fs->regs.reg = dwarf2_frame_state_copy_regs (&fs->regs);
431 fs->regs.prev = new_rs;
432 }
433 break;
434
435 case DW_CFA_restore_state:
436 {
437 struct dwarf2_frame_state_reg_info *old_rs = fs->regs.prev;
438
50ea7769
MK
439 if (old_rs == NULL)
440 {
e2e0b3e5
AC
441 complaint (&symfile_complaints, _("\
442bad CFI data; mismatched DW_CFA_restore_state at 0x%s"), paddr (fs->pc));
50ea7769
MK
443 }
444 else
445 {
446 xfree (fs->regs.reg);
447 fs->regs = *old_rs;
448 xfree (old_rs);
449 }
cfc14b3a
MK
450 }
451 break;
452
453 case DW_CFA_def_cfa:
454 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_reg);
455 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
303b6f5d
DJ
456
457 if (fs->armcc_cfa_offsets_sf)
458 utmp *= fs->data_align;
459
cfc14b3a
MK
460 fs->cfa_offset = utmp;
461 fs->cfa_how = CFA_REG_OFFSET;
462 break;
463
464 case DW_CFA_def_cfa_register:
465 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_reg);
4fc771b8
DJ
466 fs->cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, fs->cfa_reg,
467 eh_frame_p);
cfc14b3a
MK
468 fs->cfa_how = CFA_REG_OFFSET;
469 break;
470
471 case DW_CFA_def_cfa_offset:
852483bc 472 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
303b6f5d
DJ
473
474 if (fs->armcc_cfa_offsets_sf)
475 utmp *= fs->data_align;
476
852483bc 477 fs->cfa_offset = utmp;
cfc14b3a
MK
478 /* cfa_how deliberately not set. */
479 break;
480
a8504492
MK
481 case DW_CFA_nop:
482 break;
483
cfc14b3a
MK
484 case DW_CFA_def_cfa_expression:
485 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_exp_len);
486 fs->cfa_exp = insn_ptr;
487 fs->cfa_how = CFA_EXP;
488 insn_ptr += fs->cfa_exp_len;
489 break;
490
491 case DW_CFA_expression:
492 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
4fc771b8 493 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
cfc14b3a
MK
494 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
495 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
496 fs->regs.reg[reg].loc.exp = insn_ptr;
497 fs->regs.reg[reg].exp_len = utmp;
05cbe71a 498 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_EXP;
cfc14b3a
MK
499 insn_ptr += utmp;
500 break;
501
a8504492
MK
502 case DW_CFA_offset_extended_sf:
503 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
4fc771b8 504 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
a8504492 505 insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset);
f6da8dd8 506 offset *= fs->data_align;
a8504492 507 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
05cbe71a 508 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
a8504492
MK
509 fs->regs.reg[reg].loc.offset = offset;
510 break;
511
46ea248b
AO
512 case DW_CFA_val_offset:
513 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
514 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
515 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
516 offset = utmp * fs->data_align;
517 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
518 fs->regs.reg[reg].loc.offset = offset;
519 break;
520
521 case DW_CFA_val_offset_sf:
522 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
523 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
524 insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset);
525 offset *= fs->data_align;
526 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_OFFSET;
527 fs->regs.reg[reg].loc.offset = offset;
528 break;
529
530 case DW_CFA_val_expression:
531 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
532 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
533 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
534 fs->regs.reg[reg].loc.exp = insn_ptr;
535 fs->regs.reg[reg].exp_len = utmp;
536 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_VAL_EXP;
537 insn_ptr += utmp;
538 break;
539
a8504492
MK
540 case DW_CFA_def_cfa_sf:
541 insn_ptr = read_uleb128 (insn_ptr, insn_end, &fs->cfa_reg);
4fc771b8
DJ
542 fs->cfa_reg = dwarf2_frame_adjust_regnum (gdbarch, fs->cfa_reg,
543 eh_frame_p);
a8504492
MK
544 insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset);
545 fs->cfa_offset = offset * fs->data_align;
546 fs->cfa_how = CFA_REG_OFFSET;
547 break;
548
549 case DW_CFA_def_cfa_offset_sf:
550 insn_ptr = read_sleb128 (insn_ptr, insn_end, &offset);
551 fs->cfa_offset = offset * fs->data_align;
552 /* cfa_how deliberately not set. */
cfc14b3a
MK
553 break;
554
a77f4086
MK
555 case DW_CFA_GNU_window_save:
556 /* This is SPARC-specific code, and contains hard-coded
557 constants for the register numbering scheme used by
558 GCC. Rather than having a architecture-specific
559 operation that's only ever used by a single
560 architecture, we provide the implementation here.
561 Incidentally that's what GCC does too in its
562 unwinder. */
563 {
564 struct gdbarch *gdbarch = get_frame_arch (next_frame);
565 int size = register_size(gdbarch, 0);
566 dwarf2_frame_state_alloc_regs (&fs->regs, 32);
567 for (reg = 8; reg < 16; reg++)
568 {
569 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_REG;
570 fs->regs.reg[reg].loc.reg = reg + 16;
571 }
572 for (reg = 16; reg < 32; reg++)
573 {
574 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
575 fs->regs.reg[reg].loc.offset = (reg - 16) * size;
576 }
577 }
578 break;
579
cfc14b3a
MK
580 case DW_CFA_GNU_args_size:
581 /* Ignored. */
582 insn_ptr = read_uleb128 (insn_ptr, insn_end, &utmp);
583 break;
584
58894217
JK
585 case DW_CFA_GNU_negative_offset_extended:
586 insn_ptr = read_uleb128 (insn_ptr, insn_end, &reg);
4fc771b8 587 reg = dwarf2_frame_adjust_regnum (gdbarch, reg, eh_frame_p);
58894217
JK
588 insn_ptr = read_uleb128 (insn_ptr, insn_end, &offset);
589 offset *= fs->data_align;
590 dwarf2_frame_state_alloc_regs (&fs->regs, reg + 1);
591 fs->regs.reg[reg].how = DWARF2_FRAME_REG_SAVED_OFFSET;
592 fs->regs.reg[reg].loc.offset = -offset;
593 break;
594
cfc14b3a 595 default:
e2e0b3e5 596 internal_error (__FILE__, __LINE__, _("Unknown CFI encountered."));
cfc14b3a
MK
597 }
598 }
599 }
600
601 /* Don't allow remember/restore between CIE and FDE programs. */
602 dwarf2_frame_state_free_regs (fs->regs.prev);
603 fs->regs.prev = NULL;
604}
8f22cb90 605\f
cfc14b3a 606
8f22cb90 607/* Architecture-specific operations. */
cfc14b3a 608
8f22cb90
MK
609/* Per-architecture data key. */
610static struct gdbarch_data *dwarf2_frame_data;
611
612struct dwarf2_frame_ops
613{
614 /* Pre-initialize the register state REG for register REGNUM. */
aff37fc1
DM
615 void (*init_reg) (struct gdbarch *, int, struct dwarf2_frame_state_reg *,
616 struct frame_info *);
3ed09a32
DJ
617
618 /* Check whether the frame preceding NEXT_FRAME will be a signal
619 trampoline. */
620 int (*signal_frame_p) (struct gdbarch *, struct frame_info *);
4bf8967c 621
4fc771b8
DJ
622 /* Convert .eh_frame register number to DWARF register number, or
623 adjust .debug_frame register number. */
624 int (*adjust_regnum) (struct gdbarch *, int, int);
cfc14b3a
MK
625};
626
8f22cb90
MK
627/* Default architecture-specific register state initialization
628 function. */
629
630static void
631dwarf2_frame_default_init_reg (struct gdbarch *gdbarch, int regnum,
aff37fc1
DM
632 struct dwarf2_frame_state_reg *reg,
633 struct frame_info *next_frame)
8f22cb90
MK
634{
635 /* If we have a register that acts as a program counter, mark it as
636 a destination for the return address. If we have a register that
637 serves as the stack pointer, arrange for it to be filled with the
638 call frame address (CFA). The other registers are marked as
639 unspecified.
640
641 We copy the return address to the program counter, since many
642 parts in GDB assume that it is possible to get the return address
643 by unwinding the program counter register. However, on ISA's
644 with a dedicated return address register, the CFI usually only
645 contains information to unwind that return address register.
646
647 The reason we're treating the stack pointer special here is
648 because in many cases GCC doesn't emit CFI for the stack pointer
649 and implicitly assumes that it is equal to the CFA. This makes
650 some sense since the DWARF specification (version 3, draft 8,
651 p. 102) says that:
652
653 "Typically, the CFA is defined to be the value of the stack
654 pointer at the call site in the previous frame (which may be
655 different from its value on entry to the current frame)."
656
657 However, this isn't true for all platforms supported by GCC
658 (e.g. IBM S/390 and zSeries). Those architectures should provide
659 their own architecture-specific initialization function. */
05cbe71a 660
ad010def 661 if (regnum == gdbarch_pc_regnum (gdbarch))
8f22cb90 662 reg->how = DWARF2_FRAME_REG_RA;
ad010def 663 else if (regnum == gdbarch_sp_regnum (gdbarch))
8f22cb90
MK
664 reg->how = DWARF2_FRAME_REG_CFA;
665}
05cbe71a 666
8f22cb90 667/* Return a default for the architecture-specific operations. */
05cbe71a 668
8f22cb90 669static void *
030f20e1 670dwarf2_frame_init (struct obstack *obstack)
8f22cb90
MK
671{
672 struct dwarf2_frame_ops *ops;
673
030f20e1 674 ops = OBSTACK_ZALLOC (obstack, struct dwarf2_frame_ops);
8f22cb90
MK
675 ops->init_reg = dwarf2_frame_default_init_reg;
676 return ops;
677}
05cbe71a 678
8f22cb90
MK
679/* Set the architecture-specific register state initialization
680 function for GDBARCH to INIT_REG. */
681
682void
683dwarf2_frame_set_init_reg (struct gdbarch *gdbarch,
684 void (*init_reg) (struct gdbarch *, int,
aff37fc1
DM
685 struct dwarf2_frame_state_reg *,
686 struct frame_info *))
8f22cb90 687{
030f20e1 688 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
8f22cb90 689
8f22cb90
MK
690 ops->init_reg = init_reg;
691}
692
693/* Pre-initialize the register state REG for register REGNUM. */
05cbe71a
MK
694
695static void
696dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
aff37fc1
DM
697 struct dwarf2_frame_state_reg *reg,
698 struct frame_info *next_frame)
05cbe71a 699{
030f20e1 700 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
8f22cb90 701
aff37fc1 702 ops->init_reg (gdbarch, regnum, reg, next_frame);
05cbe71a 703}
3ed09a32
DJ
704
705/* Set the architecture-specific signal trampoline recognition
706 function for GDBARCH to SIGNAL_FRAME_P. */
707
708void
709dwarf2_frame_set_signal_frame_p (struct gdbarch *gdbarch,
710 int (*signal_frame_p) (struct gdbarch *,
711 struct frame_info *))
712{
713 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
714
715 ops->signal_frame_p = signal_frame_p;
716}
717
718/* Query the architecture-specific signal frame recognizer for
719 NEXT_FRAME. */
720
721static int
722dwarf2_frame_signal_frame_p (struct gdbarch *gdbarch,
723 struct frame_info *next_frame)
724{
725 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
726
727 if (ops->signal_frame_p == NULL)
728 return 0;
729 return ops->signal_frame_p (gdbarch, next_frame);
730}
4bf8967c 731
4fc771b8
DJ
732/* Set the architecture-specific adjustment of .eh_frame and .debug_frame
733 register numbers. */
4bf8967c
AS
734
735void
4fc771b8
DJ
736dwarf2_frame_set_adjust_regnum (struct gdbarch *gdbarch,
737 int (*adjust_regnum) (struct gdbarch *,
738 int, int))
4bf8967c
AS
739{
740 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
741
4fc771b8 742 ops->adjust_regnum = adjust_regnum;
4bf8967c
AS
743}
744
4fc771b8
DJ
745/* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame
746 register. */
4bf8967c 747
4fc771b8
DJ
748static int
749dwarf2_frame_adjust_regnum (struct gdbarch *gdbarch, int regnum, int eh_frame_p)
4bf8967c
AS
750{
751 struct dwarf2_frame_ops *ops = gdbarch_data (gdbarch, dwarf2_frame_data);
752
4fc771b8 753 if (ops->adjust_regnum == NULL)
4bf8967c 754 return regnum;
4fc771b8 755 return ops->adjust_regnum (gdbarch, regnum, eh_frame_p);
4bf8967c 756}
303b6f5d
DJ
757
758static void
759dwarf2_frame_find_quirks (struct dwarf2_frame_state *fs,
760 struct dwarf2_fde *fde)
761{
762 static const char *arm_idents[] = {
763 "ARM C Compiler, ADS",
764 "Thumb C Compiler, ADS",
765 "ARM C++ Compiler, ADS",
766 "Thumb C++ Compiler, ADS",
767 "ARM/Thumb C/C++ Compiler, RVCT"
768 };
769 int i;
770
771 struct symtab *s;
772
773 s = find_pc_symtab (fs->pc);
774 if (s == NULL || s->producer == NULL)
775 return;
776
777 for (i = 0; i < ARRAY_SIZE (arm_idents); i++)
778 if (strncmp (s->producer, arm_idents[i], strlen (arm_idents[i])) == 0)
779 {
780 if (fde->cie->version == 1)
781 fs->armcc_cfa_offsets_sf = 1;
782
783 if (fde->cie->version == 1)
784 fs->armcc_cfa_offsets_reversed = 1;
785
786 /* The reversed offset problem is present in some compilers
787 using DWARF3, but it was eventually fixed. Check the ARM
788 defined augmentations, which are in the format "armcc" followed
789 by a list of one-character options. The "+" option means
790 this problem is fixed (no quirk needed). If the armcc
791 augmentation is missing, the quirk is needed. */
792 if (fde->cie->version == 3
793 && (strncmp (fde->cie->augmentation, "armcc", 5) != 0
794 || strchr (fde->cie->augmentation + 5, '+') == NULL))
795 fs->armcc_cfa_offsets_reversed = 1;
796
797 return;
798 }
799}
8f22cb90
MK
800\f
801
802struct dwarf2_frame_cache
803{
804 /* DWARF Call Frame Address. */
805 CORE_ADDR cfa;
806
0228dfb9
DJ
807 /* Set if the return address column was marked as undefined. */
808 int undefined_retaddr;
809
8f22cb90
MK
810 /* Saved registers, indexed by GDB register number, not by DWARF
811 register number. */
812 struct dwarf2_frame_state_reg *reg;
8d5a9abc
MK
813
814 /* Return address register. */
815 struct dwarf2_frame_state_reg retaddr_reg;
8f22cb90 816};
05cbe71a 817
b9362cc7 818static struct dwarf2_frame_cache *
cfc14b3a
MK
819dwarf2_frame_cache (struct frame_info *next_frame, void **this_cache)
820{
821 struct cleanup *old_chain;
05cbe71a 822 struct gdbarch *gdbarch = get_frame_arch (next_frame);
ad010def
UW
823 const int num_regs = gdbarch_num_regs (gdbarch)
824 + gdbarch_num_pseudo_regs (gdbarch);
cfc14b3a
MK
825 struct dwarf2_frame_cache *cache;
826 struct dwarf2_frame_state *fs;
827 struct dwarf2_fde *fde;
cfc14b3a
MK
828
829 if (*this_cache)
830 return *this_cache;
831
832 /* Allocate a new cache. */
833 cache = FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache);
834 cache->reg = FRAME_OBSTACK_CALLOC (num_regs, struct dwarf2_frame_state_reg);
835
836 /* Allocate and initialize the frame state. */
837 fs = XMALLOC (struct dwarf2_frame_state);
838 memset (fs, 0, sizeof (struct dwarf2_frame_state));
839 old_chain = make_cleanup (dwarf2_frame_state_free, fs);
840
841 /* Unwind the PC.
842
843 Note that if NEXT_FRAME is never supposed to return (i.e. a call
844 to abort), the compiler might optimize away the instruction at
845 NEXT_FRAME's return address. As a result the return address will
846 point at some random instruction, and the CFI for that
e4e9607c 847 instruction is probably worthless to us. GCC's unwinder solves
cfc14b3a
MK
848 this problem by substracting 1 from the return address to get an
849 address in the middle of a presumed call instruction (or the
850 instruction in the associated delay slot). This should only be
851 done for "normal" frames and not for resume-type frames (signal
e4e9607c
MK
852 handlers, sentinel frames, dummy frames). The function
853 frame_unwind_address_in_block does just this. It's not clear how
854 reliable the method is though; there is the potential for the
855 register state pre-call being different to that on return. */
93d42b30 856 fs->pc = frame_unwind_address_in_block (next_frame, NORMAL_FRAME);
cfc14b3a
MK
857
858 /* Find the correct FDE. */
859 fde = dwarf2_frame_find_fde (&fs->pc);
860 gdb_assert (fde != NULL);
861
862 /* Extract any interesting information from the CIE. */
863 fs->data_align = fde->cie->data_alignment_factor;
864 fs->code_align = fde->cie->code_alignment_factor;
865 fs->retaddr_column = fde->cie->return_address_register;
866
303b6f5d
DJ
867 /* Check for "quirks" - known bugs in producers. */
868 dwarf2_frame_find_quirks (fs, fde);
869
cfc14b3a
MK
870 /* First decode all the insns in the CIE. */
871 execute_cfa_program (fde->cie->initial_instructions,
4bf8967c 872 fde->cie->end, next_frame, fs, fde->eh_frame_p);
cfc14b3a
MK
873
874 /* Save the initialized register set. */
875 fs->initial = fs->regs;
876 fs->initial.reg = dwarf2_frame_state_copy_regs (&fs->regs);
877
878 /* Then decode the insns in the FDE up to our target PC. */
4bf8967c
AS
879 execute_cfa_program (fde->instructions, fde->end, next_frame, fs,
880 fde->eh_frame_p);
cfc14b3a
MK
881
882 /* Caclulate the CFA. */
883 switch (fs->cfa_how)
884 {
885 case CFA_REG_OFFSET:
886 cache->cfa = read_reg (next_frame, fs->cfa_reg);
303b6f5d
DJ
887 if (fs->armcc_cfa_offsets_reversed)
888 cache->cfa -= fs->cfa_offset;
889 else
890 cache->cfa += fs->cfa_offset;
cfc14b3a
MK
891 break;
892
893 case CFA_EXP:
894 cache->cfa =
895 execute_stack_op (fs->cfa_exp, fs->cfa_exp_len, next_frame, 0);
896 break;
897
898 default:
e2e0b3e5 899 internal_error (__FILE__, __LINE__, _("Unknown CFA rule."));
cfc14b3a
MK
900 }
901
05cbe71a 902 /* Initialize the register state. */
3e2c4033
AC
903 {
904 int regnum;
e4e9607c 905
3e2c4033 906 for (regnum = 0; regnum < num_regs; regnum++)
aff37fc1 907 dwarf2_frame_init_reg (gdbarch, regnum, &cache->reg[regnum], next_frame);
3e2c4033
AC
908 }
909
910 /* Go through the DWARF2 CFI generated table and save its register
79c4cb80
MK
911 location information in the cache. Note that we don't skip the
912 return address column; it's perfectly all right for it to
913 correspond to a real register. If it doesn't correspond to a
914 real register, or if we shouldn't treat it as such,
055d23b8 915 gdbarch_dwarf2_reg_to_regnum should be defined to return a number outside
f57d151a 916 the range [0, gdbarch_num_regs). */
3e2c4033
AC
917 {
918 int column; /* CFI speak for "register number". */
e4e9607c 919
3e2c4033
AC
920 for (column = 0; column < fs->regs.num_regs; column++)
921 {
3e2c4033 922 /* Use the GDB register number as the destination index. */
ad010def 923 int regnum = gdbarch_dwarf2_reg_to_regnum (gdbarch, column);
3e2c4033
AC
924
925 /* If there's no corresponding GDB register, ignore it. */
926 if (regnum < 0 || regnum >= num_regs)
927 continue;
928
929 /* NOTE: cagney/2003-09-05: CFI should specify the disposition
e4e9607c
MK
930 of all debug info registers. If it doesn't, complain (but
931 not too loudly). It turns out that GCC assumes that an
3e2c4033
AC
932 unspecified register implies "same value" when CFI (draft
933 7) specifies nothing at all. Such a register could equally
934 be interpreted as "undefined". Also note that this check
e4e9607c
MK
935 isn't sufficient; it only checks that all registers in the
936 range [0 .. max column] are specified, and won't detect
3e2c4033 937 problems when a debug info register falls outside of the
e4e9607c 938 table. We need a way of iterating through all the valid
3e2c4033 939 DWARF2 register numbers. */
05cbe71a 940 if (fs->regs.reg[column].how == DWARF2_FRAME_REG_UNSPECIFIED)
f059bf6f
AC
941 {
942 if (cache->reg[regnum].how == DWARF2_FRAME_REG_UNSPECIFIED)
e2e0b3e5
AC
943 complaint (&symfile_complaints, _("\
944incomplete CFI data; unspecified registers (e.g., %s) at 0x%s"),
f059bf6f
AC
945 gdbarch_register_name (gdbarch, regnum),
946 paddr_nz (fs->pc));
947 }
35889917
MK
948 else
949 cache->reg[regnum] = fs->regs.reg[column];
3e2c4033
AC
950 }
951 }
cfc14b3a 952
8d5a9abc
MK
953 /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information
954 we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules. */
35889917
MK
955 {
956 int regnum;
957
958 for (regnum = 0; regnum < num_regs; regnum++)
959 {
8d5a9abc
MK
960 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA
961 || cache->reg[regnum].how == DWARF2_FRAME_REG_RA_OFFSET)
35889917 962 {
05cbe71a
MK
963 struct dwarf2_frame_state_reg *retaddr_reg =
964 &fs->regs.reg[fs->retaddr_column];
965
d4f10bf2
MK
966 /* It seems rather bizarre to specify an "empty" column as
967 the return adress column. However, this is exactly
968 what GCC does on some targets. It turns out that GCC
969 assumes that the return address can be found in the
970 register corresponding to the return address column.
8d5a9abc
MK
971 Incidentally, that's how we should treat a return
972 address column specifying "same value" too. */
d4f10bf2 973 if (fs->retaddr_column < fs->regs.num_regs
05cbe71a
MK
974 && retaddr_reg->how != DWARF2_FRAME_REG_UNSPECIFIED
975 && retaddr_reg->how != DWARF2_FRAME_REG_SAME_VALUE)
8d5a9abc
MK
976 {
977 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
978 cache->reg[regnum] = *retaddr_reg;
979 else
980 cache->retaddr_reg = *retaddr_reg;
981 }
35889917
MK
982 else
983 {
8d5a9abc
MK
984 if (cache->reg[regnum].how == DWARF2_FRAME_REG_RA)
985 {
986 cache->reg[regnum].loc.reg = fs->retaddr_column;
987 cache->reg[regnum].how = DWARF2_FRAME_REG_SAVED_REG;
988 }
989 else
990 {
991 cache->retaddr_reg.loc.reg = fs->retaddr_column;
992 cache->retaddr_reg.how = DWARF2_FRAME_REG_SAVED_REG;
993 }
35889917
MK
994 }
995 }
996 }
997 }
cfc14b3a 998
0228dfb9
DJ
999 if (fs->retaddr_column < fs->regs.num_regs
1000 && fs->regs.reg[fs->retaddr_column].how == DWARF2_FRAME_REG_UNDEFINED)
1001 cache->undefined_retaddr = 1;
1002
cfc14b3a
MK
1003 do_cleanups (old_chain);
1004
1005 *this_cache = cache;
1006 return cache;
1007}
1008
1009static void
1010dwarf2_frame_this_id (struct frame_info *next_frame, void **this_cache,
1011 struct frame_id *this_id)
1012{
1013 struct dwarf2_frame_cache *cache =
1014 dwarf2_frame_cache (next_frame, this_cache);
1015
0228dfb9
DJ
1016 if (cache->undefined_retaddr)
1017 return;
1018
93d42b30
DJ
1019 (*this_id) = frame_id_build (cache->cfa,
1020 frame_func_unwind (next_frame, NORMAL_FRAME));
1021}
1022
1023static void
1024dwarf2_signal_frame_this_id (struct frame_info *next_frame, void **this_cache,
1025 struct frame_id *this_id)
1026{
1027 struct dwarf2_frame_cache *cache =
1028 dwarf2_frame_cache (next_frame, this_cache);
1029
1030 if (cache->undefined_retaddr)
1031 return;
1032
1033 (*this_id) = frame_id_build (cache->cfa,
1034 frame_func_unwind (next_frame, SIGTRAMP_FRAME));
cfc14b3a
MK
1035}
1036
1037static void
1038dwarf2_frame_prev_register (struct frame_info *next_frame, void **this_cache,
1039 int regnum, int *optimizedp,
1040 enum lval_type *lvalp, CORE_ADDR *addrp,
c6826062 1041 int *realnump, gdb_byte *valuep)
cfc14b3a 1042{
05cbe71a 1043 struct gdbarch *gdbarch = get_frame_arch (next_frame);
cfc14b3a
MK
1044 struct dwarf2_frame_cache *cache =
1045 dwarf2_frame_cache (next_frame, this_cache);
1046
1047 switch (cache->reg[regnum].how)
1048 {
05cbe71a 1049 case DWARF2_FRAME_REG_UNDEFINED:
3e2c4033 1050 /* If CFI explicitly specified that the value isn't defined,
e4e9607c 1051 mark it as optimized away; the value isn't available. */
cfc14b3a
MK
1052 *optimizedp = 1;
1053 *lvalp = not_lval;
1054 *addrp = 0;
1055 *realnump = -1;
35889917 1056 if (valuep)
cfc14b3a
MK
1057 {
1058 /* In some cases, for example %eflags on the i386, we have
1059 to provide a sane value, even though this register wasn't
1060 saved. Assume we can get it from NEXT_FRAME. */
1061 frame_unwind_register (next_frame, regnum, valuep);
1062 }
1063 break;
1064
05cbe71a 1065 case DWARF2_FRAME_REG_SAVED_OFFSET:
cfc14b3a
MK
1066 *optimizedp = 0;
1067 *lvalp = lval_memory;
1068 *addrp = cache->cfa + cache->reg[regnum].loc.offset;
1069 *realnump = -1;
1070 if (valuep)
1071 {
1072 /* Read the value in from memory. */
05cbe71a 1073 read_memory (*addrp, valuep, register_size (gdbarch, regnum));
cfc14b3a
MK
1074 }
1075 break;
1076
05cbe71a 1077 case DWARF2_FRAME_REG_SAVED_REG:
00b25ff3
AC
1078 *optimizedp = 0;
1079 *lvalp = lval_register;
1080 *addrp = 0;
055d23b8 1081 *realnump = gdbarch_dwarf2_reg_to_regnum
ad010def 1082 (gdbarch, cache->reg[regnum].loc.reg);
00b25ff3
AC
1083 if (valuep)
1084 frame_unwind_register (next_frame, (*realnump), valuep);
cfc14b3a
MK
1085 break;
1086
05cbe71a 1087 case DWARF2_FRAME_REG_SAVED_EXP:
cfc14b3a
MK
1088 *optimizedp = 0;
1089 *lvalp = lval_memory;
1090 *addrp = execute_stack_op (cache->reg[regnum].loc.exp,
1091 cache->reg[regnum].exp_len,
1092 next_frame, cache->cfa);
1093 *realnump = -1;
1094 if (valuep)
1095 {
1096 /* Read the value in from memory. */
05cbe71a 1097 read_memory (*addrp, valuep, register_size (gdbarch, regnum));
cfc14b3a
MK
1098 }
1099 break;
1100
46ea248b
AO
1101 case DWARF2_FRAME_REG_SAVED_VAL_OFFSET:
1102 *optimizedp = 0;
1103 *lvalp = not_lval;
1104 *addrp = 0;
1105 *realnump = -1;
1106 if (valuep)
1107 store_unsigned_integer (valuep, register_size (gdbarch, regnum),
1108 cache->cfa + cache->reg[regnum].loc.offset);
1109 break;
1110
1111 case DWARF2_FRAME_REG_SAVED_VAL_EXP:
1112 *optimizedp = 0;
1113 *lvalp = not_lval;
1114 *addrp = 0;
1115 *realnump = -1;
1116 if (valuep)
1117 store_unsigned_integer (valuep, register_size (gdbarch, regnum),
1118 execute_stack_op (cache->reg[regnum].loc.exp,
1119 cache->reg[regnum].exp_len,
1120 next_frame, cache->cfa));
1121 break;
1122
05cbe71a 1123 case DWARF2_FRAME_REG_UNSPECIFIED:
3e2c4033
AC
1124 /* GCC, in its infinite wisdom decided to not provide unwind
1125 information for registers that are "same value". Since
1126 DWARF2 (3 draft 7) doesn't define such behavior, said
1127 registers are actually undefined (which is different to CFI
1128 "undefined"). Code above issues a complaint about this.
1129 Here just fudge the books, assume GCC, and that the value is
1130 more inner on the stack. */
00b25ff3
AC
1131 *optimizedp = 0;
1132 *lvalp = lval_register;
1133 *addrp = 0;
1134 *realnump = regnum;
1135 if (valuep)
1136 frame_unwind_register (next_frame, (*realnump), valuep);
3e2c4033
AC
1137 break;
1138
05cbe71a 1139 case DWARF2_FRAME_REG_SAME_VALUE:
00b25ff3
AC
1140 *optimizedp = 0;
1141 *lvalp = lval_register;
1142 *addrp = 0;
1143 *realnump = regnum;
1144 if (valuep)
1145 frame_unwind_register (next_frame, (*realnump), valuep);
cfc14b3a
MK
1146 break;
1147
05cbe71a 1148 case DWARF2_FRAME_REG_CFA:
35889917
MK
1149 *optimizedp = 0;
1150 *lvalp = not_lval;
1151 *addrp = 0;
1152 *realnump = -1;
1153 if (valuep)
14d06750 1154 pack_long (valuep, register_type (gdbarch, regnum), cache->cfa);
35889917
MK
1155 break;
1156
ea7963f0
FR
1157 case DWARF2_FRAME_REG_CFA_OFFSET:
1158 *optimizedp = 0;
1159 *lvalp = not_lval;
1160 *addrp = 0;
1161 *realnump = -1;
1162 if (valuep)
14d06750
DJ
1163 pack_long (valuep, register_type (gdbarch, regnum),
1164 cache->cfa + cache->reg[regnum].loc.offset);
ea7963f0
FR
1165 break;
1166
8d5a9abc
MK
1167 case DWARF2_FRAME_REG_RA_OFFSET:
1168 *optimizedp = 0;
1169 *lvalp = not_lval;
1170 *addrp = 0;
1171 *realnump = -1;
1172 if (valuep)
1173 {
1174 CORE_ADDR pc = cache->reg[regnum].loc.offset;
1175
055d23b8 1176 regnum = gdbarch_dwarf2_reg_to_regnum
ad010def 1177 (gdbarch, cache->retaddr_reg.loc.reg);
8d5a9abc 1178 pc += frame_unwind_register_unsigned (next_frame, regnum);
14d06750 1179 pack_long (valuep, register_type (gdbarch, regnum), pc);
8d5a9abc
MK
1180 }
1181 break;
1182
cfc14b3a 1183 default:
e2e0b3e5 1184 internal_error (__FILE__, __LINE__, _("Unknown register rule."));
cfc14b3a
MK
1185 }
1186}
1187
1188static const struct frame_unwind dwarf2_frame_unwind =
1189{
1190 NORMAL_FRAME,
1191 dwarf2_frame_this_id,
1192 dwarf2_frame_prev_register
1193};
1194
3ed09a32
DJ
1195static const struct frame_unwind dwarf2_signal_frame_unwind =
1196{
1197 SIGTRAMP_FRAME,
93d42b30 1198 dwarf2_signal_frame_this_id,
3ed09a32
DJ
1199 dwarf2_frame_prev_register
1200};
1201
cfc14b3a 1202const struct frame_unwind *
336d1bba 1203dwarf2_frame_sniffer (struct frame_info *next_frame)
cfc14b3a 1204{
1ce5d6dd
AC
1205 /* Grab an address that is guarenteed to reside somewhere within the
1206 function. frame_pc_unwind(), for a no-return next function, can
93d42b30
DJ
1207 end up returning something past the end of this function's body.
1208 If the frame we're sniffing for is a signal frame whose start
1209 address is placed on the stack by the OS, its FDE must
1210 extend one byte before its start address or we will miss it. */
1211 CORE_ADDR block_addr = frame_unwind_address_in_block (next_frame,
1212 NORMAL_FRAME);
56c987f6
AO
1213 struct dwarf2_fde *fde = dwarf2_frame_find_fde (&block_addr);
1214 if (!fde)
3ed09a32
DJ
1215 return NULL;
1216
1217 /* On some targets, signal trampolines may have unwind information.
1218 We need to recognize them so that we set the frame type
1219 correctly. */
1220
56c987f6
AO
1221 if (fde->cie->signal_frame
1222 || dwarf2_frame_signal_frame_p (get_frame_arch (next_frame),
1223 next_frame))
3ed09a32 1224 return &dwarf2_signal_frame_unwind;
cfc14b3a 1225
3ed09a32 1226 return &dwarf2_frame_unwind;
cfc14b3a
MK
1227}
1228\f
1229
1230/* There is no explicitly defined relationship between the CFA and the
1231 location of frame's local variables and arguments/parameters.
1232 Therefore, frame base methods on this page should probably only be
1233 used as a last resort, just to avoid printing total garbage as a
1234 response to the "info frame" command. */
1235
1236static CORE_ADDR
1237dwarf2_frame_base_address (struct frame_info *next_frame, void **this_cache)
1238{
1239 struct dwarf2_frame_cache *cache =
1240 dwarf2_frame_cache (next_frame, this_cache);
1241
1242 return cache->cfa;
1243}
1244
1245static const struct frame_base dwarf2_frame_base =
1246{
1247 &dwarf2_frame_unwind,
1248 dwarf2_frame_base_address,
1249 dwarf2_frame_base_address,
1250 dwarf2_frame_base_address
1251};
1252
1253const struct frame_base *
336d1bba 1254dwarf2_frame_base_sniffer (struct frame_info *next_frame)
cfc14b3a 1255{
93d42b30
DJ
1256 CORE_ADDR block_addr = frame_unwind_address_in_block (next_frame,
1257 NORMAL_FRAME);
1258 if (dwarf2_frame_find_fde (&block_addr))
cfc14b3a
MK
1259 return &dwarf2_frame_base;
1260
1261 return NULL;
1262}
1263\f
1264/* A minimal decoding of DWARF2 compilation units. We only decode
1265 what's needed to get to the call frame information. */
1266
1267struct comp_unit
1268{
1269 /* Keep the bfd convenient. */
1270 bfd *abfd;
1271
1272 struct objfile *objfile;
1273
1274 /* Linked list of CIEs for this object. */
1275 struct dwarf2_cie *cie;
1276
cfc14b3a 1277 /* Pointer to the .debug_frame section loaded into memory. */
852483bc 1278 gdb_byte *dwarf_frame_buffer;
cfc14b3a
MK
1279
1280 /* Length of the loaded .debug_frame section. */
1281 unsigned long dwarf_frame_size;
1282
1283 /* Pointer to the .debug_frame section. */
1284 asection *dwarf_frame_section;
0912c7f2
MK
1285
1286 /* Base for DW_EH_PE_datarel encodings. */
1287 bfd_vma dbase;
0fd85043
CV
1288
1289 /* Base for DW_EH_PE_textrel encodings. */
1290 bfd_vma tbase;
cfc14b3a
MK
1291};
1292
8f22cb90 1293const struct objfile_data *dwarf2_frame_objfile_data;
0d0e1a63 1294
cfc14b3a 1295static unsigned int
852483bc 1296read_1_byte (bfd *abfd, gdb_byte *buf)
cfc14b3a 1297{
852483bc 1298 return bfd_get_8 (abfd, buf);
cfc14b3a
MK
1299}
1300
1301static unsigned int
852483bc 1302read_4_bytes (bfd *abfd, gdb_byte *buf)
cfc14b3a 1303{
852483bc 1304 return bfd_get_32 (abfd, buf);
cfc14b3a
MK
1305}
1306
1307static ULONGEST
852483bc 1308read_8_bytes (bfd *abfd, gdb_byte *buf)
cfc14b3a 1309{
852483bc 1310 return bfd_get_64 (abfd, buf);
cfc14b3a
MK
1311}
1312
1313static ULONGEST
852483bc 1314read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
cfc14b3a
MK
1315{
1316 ULONGEST result;
1317 unsigned int num_read;
1318 int shift;
852483bc 1319 gdb_byte byte;
cfc14b3a
MK
1320
1321 result = 0;
1322 shift = 0;
1323 num_read = 0;
1324
1325 do
1326 {
1327 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
1328 buf++;
1329 num_read++;
1330 result |= ((byte & 0x7f) << shift);
1331 shift += 7;
1332 }
1333 while (byte & 0x80);
1334
1335 *bytes_read_ptr = num_read;
1336
1337 return result;
1338}
1339
1340static LONGEST
852483bc 1341read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
cfc14b3a
MK
1342{
1343 LONGEST result;
1344 int shift;
1345 unsigned int num_read;
852483bc 1346 gdb_byte byte;
cfc14b3a
MK
1347
1348 result = 0;
1349 shift = 0;
1350 num_read = 0;
1351
1352 do
1353 {
1354 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
1355 buf++;
1356 num_read++;
1357 result |= ((byte & 0x7f) << shift);
1358 shift += 7;
1359 }
1360 while (byte & 0x80);
1361
77e0b926
DJ
1362 if (shift < 8 * sizeof (result) && (byte & 0x40))
1363 result |= -(((LONGEST)1) << shift);
cfc14b3a
MK
1364
1365 *bytes_read_ptr = num_read;
1366
1367 return result;
1368}
1369
1370static ULONGEST
852483bc 1371read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
cfc14b3a
MK
1372{
1373 LONGEST result;
1374
852483bc 1375 result = bfd_get_32 (abfd, buf);
cfc14b3a
MK
1376 if (result == 0xffffffff)
1377 {
852483bc 1378 result = bfd_get_64 (abfd, buf + 4);
cfc14b3a
MK
1379 *bytes_read_ptr = 12;
1380 }
1381 else
1382 *bytes_read_ptr = 4;
1383
1384 return result;
1385}
1386\f
1387
1388/* Pointer encoding helper functions. */
1389
1390/* GCC supports exception handling based on DWARF2 CFI. However, for
1391 technical reasons, it encodes addresses in its FDE's in a different
1392 way. Several "pointer encodings" are supported. The encoding
1393 that's used for a particular FDE is determined by the 'R'
1394 augmentation in the associated CIE. The argument of this
1395 augmentation is a single byte.
1396
1397 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
1398 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
1399 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
1400 address should be interpreted (absolute, relative to the current
1401 position in the FDE, ...). Bit 7, indicates that the address
1402 should be dereferenced. */
1403
852483bc 1404static gdb_byte
cfc14b3a
MK
1405encoding_for_size (unsigned int size)
1406{
1407 switch (size)
1408 {
1409 case 2:
1410 return DW_EH_PE_udata2;
1411 case 4:
1412 return DW_EH_PE_udata4;
1413 case 8:
1414 return DW_EH_PE_udata8;
1415 default:
e2e0b3e5 1416 internal_error (__FILE__, __LINE__, _("Unsupported address size"));
cfc14b3a
MK
1417 }
1418}
1419
1420static unsigned int
852483bc 1421size_of_encoded_value (gdb_byte encoding)
cfc14b3a
MK
1422{
1423 if (encoding == DW_EH_PE_omit)
1424 return 0;
1425
1426 switch (encoding & 0x07)
1427 {
1428 case DW_EH_PE_absptr:
1429 return TYPE_LENGTH (builtin_type_void_data_ptr);
1430 case DW_EH_PE_udata2:
1431 return 2;
1432 case DW_EH_PE_udata4:
1433 return 4;
1434 case DW_EH_PE_udata8:
1435 return 8;
1436 default:
e2e0b3e5 1437 internal_error (__FILE__, __LINE__, _("Invalid or unsupported encoding"));
cfc14b3a
MK
1438 }
1439}
1440
1441static CORE_ADDR
852483bc
MK
1442read_encoded_value (struct comp_unit *unit, gdb_byte encoding,
1443 gdb_byte *buf, unsigned int *bytes_read_ptr)
cfc14b3a 1444{
68f6cf99
MK
1445 int ptr_len = size_of_encoded_value (DW_EH_PE_absptr);
1446 ptrdiff_t offset;
cfc14b3a
MK
1447 CORE_ADDR base;
1448
1449 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1450 FDE's. */
1451 if (encoding & DW_EH_PE_indirect)
1452 internal_error (__FILE__, __LINE__,
e2e0b3e5 1453 _("Unsupported encoding: DW_EH_PE_indirect"));
cfc14b3a 1454
68f6cf99
MK
1455 *bytes_read_ptr = 0;
1456
cfc14b3a
MK
1457 switch (encoding & 0x70)
1458 {
1459 case DW_EH_PE_absptr:
1460 base = 0;
1461 break;
1462 case DW_EH_PE_pcrel:
f2fec864 1463 base = bfd_get_section_vma (unit->abfd, unit->dwarf_frame_section);
852483bc 1464 base += (buf - unit->dwarf_frame_buffer);
cfc14b3a 1465 break;
0912c7f2
MK
1466 case DW_EH_PE_datarel:
1467 base = unit->dbase;
1468 break;
0fd85043
CV
1469 case DW_EH_PE_textrel:
1470 base = unit->tbase;
1471 break;
03ac2a74
MK
1472 case DW_EH_PE_funcrel:
1473 /* FIXME: kettenis/20040501: For now just pretend
1474 DW_EH_PE_funcrel is equivalent to DW_EH_PE_absptr. For
1475 reading the initial location of an FDE it should be treated
1476 as such, and currently that's the only place where this code
1477 is used. */
1478 base = 0;
1479 break;
68f6cf99
MK
1480 case DW_EH_PE_aligned:
1481 base = 0;
852483bc 1482 offset = buf - unit->dwarf_frame_buffer;
68f6cf99
MK
1483 if ((offset % ptr_len) != 0)
1484 {
1485 *bytes_read_ptr = ptr_len - (offset % ptr_len);
1486 buf += *bytes_read_ptr;
1487 }
1488 break;
cfc14b3a 1489 default:
e2e0b3e5 1490 internal_error (__FILE__, __LINE__, _("Invalid or unsupported encoding"));
cfc14b3a
MK
1491 }
1492
b04de778 1493 if ((encoding & 0x07) == 0x00)
f2fec864
DJ
1494 {
1495 encoding |= encoding_for_size (ptr_len);
1496 if (bfd_get_sign_extend_vma (unit->abfd))
1497 encoding |= DW_EH_PE_signed;
1498 }
cfc14b3a
MK
1499
1500 switch (encoding & 0x0f)
1501 {
a81b10ae
MK
1502 case DW_EH_PE_uleb128:
1503 {
1504 ULONGEST value;
852483bc 1505 gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
a7289609 1506 *bytes_read_ptr += read_uleb128 (buf, end_buf, &value) - buf;
a81b10ae
MK
1507 return base + value;
1508 }
cfc14b3a 1509 case DW_EH_PE_udata2:
68f6cf99 1510 *bytes_read_ptr += 2;
cfc14b3a
MK
1511 return (base + bfd_get_16 (unit->abfd, (bfd_byte *) buf));
1512 case DW_EH_PE_udata4:
68f6cf99 1513 *bytes_read_ptr += 4;
cfc14b3a
MK
1514 return (base + bfd_get_32 (unit->abfd, (bfd_byte *) buf));
1515 case DW_EH_PE_udata8:
68f6cf99 1516 *bytes_read_ptr += 8;
cfc14b3a 1517 return (base + bfd_get_64 (unit->abfd, (bfd_byte *) buf));
a81b10ae
MK
1518 case DW_EH_PE_sleb128:
1519 {
1520 LONGEST value;
852483bc 1521 gdb_byte *end_buf = buf + (sizeof (value) + 1) * 8 / 7;
a7289609 1522 *bytes_read_ptr += read_sleb128 (buf, end_buf, &value) - buf;
a81b10ae
MK
1523 return base + value;
1524 }
cfc14b3a 1525 case DW_EH_PE_sdata2:
68f6cf99 1526 *bytes_read_ptr += 2;
cfc14b3a
MK
1527 return (base + bfd_get_signed_16 (unit->abfd, (bfd_byte *) buf));
1528 case DW_EH_PE_sdata4:
68f6cf99 1529 *bytes_read_ptr += 4;
cfc14b3a
MK
1530 return (base + bfd_get_signed_32 (unit->abfd, (bfd_byte *) buf));
1531 case DW_EH_PE_sdata8:
68f6cf99 1532 *bytes_read_ptr += 8;
cfc14b3a
MK
1533 return (base + bfd_get_signed_64 (unit->abfd, (bfd_byte *) buf));
1534 default:
e2e0b3e5 1535 internal_error (__FILE__, __LINE__, _("Invalid or unsupported encoding"));
cfc14b3a
MK
1536 }
1537}
1538\f
1539
1540/* GCC uses a single CIE for all FDEs in a .debug_frame section.
1541 That's why we use a simple linked list here. */
1542
1543static struct dwarf2_cie *
1544find_cie (struct comp_unit *unit, ULONGEST cie_pointer)
1545{
1546 struct dwarf2_cie *cie = unit->cie;
1547
1548 while (cie)
1549 {
1550 if (cie->cie_pointer == cie_pointer)
1551 return cie;
1552
1553 cie = cie->next;
1554 }
1555
1556 return NULL;
1557}
1558
1559static void
1560add_cie (struct comp_unit *unit, struct dwarf2_cie *cie)
1561{
1562 cie->next = unit->cie;
1563 unit->cie = cie;
1564}
1565
1566/* Find the FDE for *PC. Return a pointer to the FDE, and store the
1567 inital location associated with it into *PC. */
1568
1569static struct dwarf2_fde *
1570dwarf2_frame_find_fde (CORE_ADDR *pc)
1571{
1572 struct objfile *objfile;
1573
1574 ALL_OBJFILES (objfile)
1575 {
1576 struct dwarf2_fde *fde;
1577 CORE_ADDR offset;
1578
8f22cb90 1579 fde = objfile_data (objfile, dwarf2_frame_objfile_data);
4ae9ee8e
DJ
1580 if (fde == NULL)
1581 continue;
1582
1583 gdb_assert (objfile->section_offsets);
1584 offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1585
cfc14b3a
MK
1586 while (fde)
1587 {
1588 if (*pc >= fde->initial_location + offset
1589 && *pc < fde->initial_location + offset + fde->address_range)
1590 {
1591 *pc = fde->initial_location + offset;
1592 return fde;
1593 }
1594
1595 fde = fde->next;
1596 }
1597 }
1598
1599 return NULL;
1600}
1601
1602static void
1603add_fde (struct comp_unit *unit, struct dwarf2_fde *fde)
1604{
8f22cb90
MK
1605 fde->next = objfile_data (unit->objfile, dwarf2_frame_objfile_data);
1606 set_objfile_data (unit->objfile, dwarf2_frame_objfile_data, fde);
cfc14b3a
MK
1607}
1608
1609#ifdef CC_HAS_LONG_LONG
1610#define DW64_CIE_ID 0xffffffffffffffffULL
1611#else
1612#define DW64_CIE_ID ~0
1613#endif
1614
852483bc
MK
1615static gdb_byte *decode_frame_entry (struct comp_unit *unit, gdb_byte *start,
1616 int eh_frame_p);
cfc14b3a 1617
6896c0c7
RH
1618/* Decode the next CIE or FDE. Return NULL if invalid input, otherwise
1619 the next byte to be processed. */
852483bc
MK
1620static gdb_byte *
1621decode_frame_entry_1 (struct comp_unit *unit, gdb_byte *start, int eh_frame_p)
cfc14b3a 1622{
852483bc 1623 gdb_byte *buf, *end;
cfc14b3a
MK
1624 LONGEST length;
1625 unsigned int bytes_read;
6896c0c7
RH
1626 int dwarf64_p;
1627 ULONGEST cie_id;
cfc14b3a 1628 ULONGEST cie_pointer;
cfc14b3a 1629
6896c0c7 1630 buf = start;
cfc14b3a
MK
1631 length = read_initial_length (unit->abfd, buf, &bytes_read);
1632 buf += bytes_read;
1633 end = buf + length;
1634
6896c0c7
RH
1635 /* Are we still within the section? */
1636 if (end > unit->dwarf_frame_buffer + unit->dwarf_frame_size)
1637 return NULL;
1638
cfc14b3a
MK
1639 if (length == 0)
1640 return end;
1641
6896c0c7
RH
1642 /* Distinguish between 32 and 64-bit encoded frame info. */
1643 dwarf64_p = (bytes_read == 12);
cfc14b3a 1644
6896c0c7 1645 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
cfc14b3a
MK
1646 if (eh_frame_p)
1647 cie_id = 0;
1648 else if (dwarf64_p)
1649 cie_id = DW64_CIE_ID;
6896c0c7
RH
1650 else
1651 cie_id = DW_CIE_ID;
cfc14b3a
MK
1652
1653 if (dwarf64_p)
1654 {
1655 cie_pointer = read_8_bytes (unit->abfd, buf);
1656 buf += 8;
1657 }
1658 else
1659 {
1660 cie_pointer = read_4_bytes (unit->abfd, buf);
1661 buf += 4;
1662 }
1663
1664 if (cie_pointer == cie_id)
1665 {
1666 /* This is a CIE. */
1667 struct dwarf2_cie *cie;
1668 char *augmentation;
28ba0b33 1669 unsigned int cie_version;
cfc14b3a
MK
1670
1671 /* Record the offset into the .debug_frame section of this CIE. */
1672 cie_pointer = start - unit->dwarf_frame_buffer;
1673
1674 /* Check whether we've already read it. */
1675 if (find_cie (unit, cie_pointer))
1676 return end;
1677
1678 cie = (struct dwarf2_cie *)
8b92e4d5 1679 obstack_alloc (&unit->objfile->objfile_obstack,
cfc14b3a
MK
1680 sizeof (struct dwarf2_cie));
1681 cie->initial_instructions = NULL;
1682 cie->cie_pointer = cie_pointer;
1683
1684 /* The encoding for FDE's in a normal .debug_frame section
32b05c07
MK
1685 depends on the target address size. */
1686 cie->encoding = DW_EH_PE_absptr;
cfc14b3a 1687
56c987f6
AO
1688 /* We'll determine the final value later, but we need to
1689 initialize it conservatively. */
1690 cie->signal_frame = 0;
1691
cfc14b3a 1692 /* Check version number. */
28ba0b33
PB
1693 cie_version = read_1_byte (unit->abfd, buf);
1694 if (cie_version != 1 && cie_version != 3)
6896c0c7 1695 return NULL;
303b6f5d 1696 cie->version = cie_version;
cfc14b3a
MK
1697 buf += 1;
1698
1699 /* Interpret the interesting bits of the augmentation. */
303b6f5d 1700 cie->augmentation = augmentation = (char *) buf;
852483bc 1701 buf += (strlen (augmentation) + 1);
cfc14b3a 1702
303b6f5d
DJ
1703 /* Ignore armcc augmentations. We only use them for quirks,
1704 and that doesn't happen until later. */
1705 if (strncmp (augmentation, "armcc", 5) == 0)
1706 augmentation += strlen (augmentation);
1707
cfc14b3a
MK
1708 /* The GCC 2.x "eh" augmentation has a pointer immediately
1709 following the augmentation string, so it must be handled
1710 first. */
1711 if (augmentation[0] == 'e' && augmentation[1] == 'h')
1712 {
1713 /* Skip. */
1714 buf += TYPE_LENGTH (builtin_type_void_data_ptr);
1715 augmentation += 2;
1716 }
1717
1718 cie->code_alignment_factor =
1719 read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1720 buf += bytes_read;
1721
1722 cie->data_alignment_factor =
1723 read_signed_leb128 (unit->abfd, buf, &bytes_read);
1724 buf += bytes_read;
1725
28ba0b33
PB
1726 if (cie_version == 1)
1727 {
1728 cie->return_address_register = read_1_byte (unit->abfd, buf);
1729 bytes_read = 1;
1730 }
1731 else
1732 cie->return_address_register = read_unsigned_leb128 (unit->abfd, buf,
1733 &bytes_read);
4fc771b8
DJ
1734 cie->return_address_register
1735 = dwarf2_frame_adjust_regnum (current_gdbarch,
1736 cie->return_address_register,
1737 eh_frame_p);
4bf8967c 1738
28ba0b33 1739 buf += bytes_read;
cfc14b3a 1740
7131cb6e
RH
1741 cie->saw_z_augmentation = (*augmentation == 'z');
1742 if (cie->saw_z_augmentation)
cfc14b3a
MK
1743 {
1744 ULONGEST length;
1745
1746 length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1747 buf += bytes_read;
6896c0c7
RH
1748 if (buf > end)
1749 return NULL;
cfc14b3a
MK
1750 cie->initial_instructions = buf + length;
1751 augmentation++;
1752 }
1753
1754 while (*augmentation)
1755 {
1756 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1757 if (*augmentation == 'L')
1758 {
1759 /* Skip. */
1760 buf++;
1761 augmentation++;
1762 }
1763
1764 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1765 else if (*augmentation == 'R')
1766 {
1767 cie->encoding = *buf++;
1768 augmentation++;
1769 }
1770
1771 /* "P" indicates a personality routine in the CIE augmentation. */
1772 else if (*augmentation == 'P')
1773 {
1234d960 1774 /* Skip. Avoid indirection since we throw away the result. */
852483bc 1775 gdb_byte encoding = (*buf++) & ~DW_EH_PE_indirect;
f724bf08
MK
1776 read_encoded_value (unit, encoding, buf, &bytes_read);
1777 buf += bytes_read;
cfc14b3a
MK
1778 augmentation++;
1779 }
1780
56c987f6
AO
1781 /* "S" indicates a signal frame, such that the return
1782 address must not be decremented to locate the call frame
1783 info for the previous frame; it might even be the first
1784 instruction of a function, so decrementing it would take
1785 us to a different function. */
1786 else if (*augmentation == 'S')
1787 {
1788 cie->signal_frame = 1;
1789 augmentation++;
1790 }
1791
3e9a2e52
DJ
1792 /* Otherwise we have an unknown augmentation. Assume that either
1793 there is no augmentation data, or we saw a 'z' prefix. */
cfc14b3a
MK
1794 else
1795 {
3e9a2e52
DJ
1796 if (cie->initial_instructions)
1797 buf = cie->initial_instructions;
cfc14b3a
MK
1798 break;
1799 }
1800 }
1801
1802 cie->initial_instructions = buf;
1803 cie->end = end;
1804
1805 add_cie (unit, cie);
1806 }
1807 else
1808 {
1809 /* This is a FDE. */
1810 struct dwarf2_fde *fde;
1811
6896c0c7
RH
1812 /* In an .eh_frame section, the CIE pointer is the delta between the
1813 address within the FDE where the CIE pointer is stored and the
1814 address of the CIE. Convert it to an offset into the .eh_frame
1815 section. */
cfc14b3a
MK
1816 if (eh_frame_p)
1817 {
cfc14b3a
MK
1818 cie_pointer = buf - unit->dwarf_frame_buffer - cie_pointer;
1819 cie_pointer -= (dwarf64_p ? 8 : 4);
1820 }
1821
6896c0c7
RH
1822 /* In either case, validate the result is still within the section. */
1823 if (cie_pointer >= unit->dwarf_frame_size)
1824 return NULL;
1825
cfc14b3a 1826 fde = (struct dwarf2_fde *)
8b92e4d5 1827 obstack_alloc (&unit->objfile->objfile_obstack,
cfc14b3a
MK
1828 sizeof (struct dwarf2_fde));
1829 fde->cie = find_cie (unit, cie_pointer);
1830 if (fde->cie == NULL)
1831 {
1832 decode_frame_entry (unit, unit->dwarf_frame_buffer + cie_pointer,
1833 eh_frame_p);
1834 fde->cie = find_cie (unit, cie_pointer);
1835 }
1836
1837 gdb_assert (fde->cie != NULL);
1838
1839 fde->initial_location =
1840 read_encoded_value (unit, fde->cie->encoding, buf, &bytes_read);
1841 buf += bytes_read;
1842
1843 fde->address_range =
1844 read_encoded_value (unit, fde->cie->encoding & 0x0f, buf, &bytes_read);
1845 buf += bytes_read;
1846
7131cb6e
RH
1847 /* A 'z' augmentation in the CIE implies the presence of an
1848 augmentation field in the FDE as well. The only thing known
1849 to be in here at present is the LSDA entry for EH. So we
1850 can skip the whole thing. */
1851 if (fde->cie->saw_z_augmentation)
1852 {
1853 ULONGEST length;
1854
1855 length = read_unsigned_leb128 (unit->abfd, buf, &bytes_read);
1856 buf += bytes_read + length;
6896c0c7
RH
1857 if (buf > end)
1858 return NULL;
7131cb6e
RH
1859 }
1860
cfc14b3a
MK
1861 fde->instructions = buf;
1862 fde->end = end;
1863
4bf8967c
AS
1864 fde->eh_frame_p = eh_frame_p;
1865
cfc14b3a
MK
1866 add_fde (unit, fde);
1867 }
1868
1869 return end;
1870}
6896c0c7
RH
1871
1872/* Read a CIE or FDE in BUF and decode it. */
852483bc
MK
1873static gdb_byte *
1874decode_frame_entry (struct comp_unit *unit, gdb_byte *start, int eh_frame_p)
6896c0c7
RH
1875{
1876 enum { NONE, ALIGN4, ALIGN8, FAIL } workaround = NONE;
852483bc 1877 gdb_byte *ret;
6896c0c7
RH
1878 const char *msg;
1879 ptrdiff_t start_offset;
1880
1881 while (1)
1882 {
1883 ret = decode_frame_entry_1 (unit, start, eh_frame_p);
1884 if (ret != NULL)
1885 break;
1886
1887 /* We have corrupt input data of some form. */
1888
1889 /* ??? Try, weakly, to work around compiler/assembler/linker bugs
1890 and mismatches wrt padding and alignment of debug sections. */
1891 /* Note that there is no requirement in the standard for any
1892 alignment at all in the frame unwind sections. Testing for
1893 alignment before trying to interpret data would be incorrect.
1894
1895 However, GCC traditionally arranged for frame sections to be
1896 sized such that the FDE length and CIE fields happen to be
1897 aligned (in theory, for performance). This, unfortunately,
1898 was done with .align directives, which had the side effect of
1899 forcing the section to be aligned by the linker.
1900
1901 This becomes a problem when you have some other producer that
1902 creates frame sections that are not as strictly aligned. That
1903 produces a hole in the frame info that gets filled by the
1904 linker with zeros.
1905
1906 The GCC behaviour is arguably a bug, but it's effectively now
1907 part of the ABI, so we're now stuck with it, at least at the
1908 object file level. A smart linker may decide, in the process
1909 of compressing duplicate CIE information, that it can rewrite
1910 the entire output section without this extra padding. */
1911
1912 start_offset = start - unit->dwarf_frame_buffer;
1913 if (workaround < ALIGN4 && (start_offset & 3) != 0)
1914 {
1915 start += 4 - (start_offset & 3);
1916 workaround = ALIGN4;
1917 continue;
1918 }
1919 if (workaround < ALIGN8 && (start_offset & 7) != 0)
1920 {
1921 start += 8 - (start_offset & 7);
1922 workaround = ALIGN8;
1923 continue;
1924 }
1925
1926 /* Nothing left to try. Arrange to return as if we've consumed
1927 the entire input section. Hopefully we'll get valid info from
1928 the other of .debug_frame/.eh_frame. */
1929 workaround = FAIL;
1930 ret = unit->dwarf_frame_buffer + unit->dwarf_frame_size;
1931 break;
1932 }
1933
1934 switch (workaround)
1935 {
1936 case NONE:
1937 break;
1938
1939 case ALIGN4:
1940 complaint (&symfile_complaints,
e2e0b3e5 1941 _("Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
6896c0c7
RH
1942 unit->dwarf_frame_section->owner->filename,
1943 unit->dwarf_frame_section->name);
1944 break;
1945
1946 case ALIGN8:
1947 complaint (&symfile_complaints,
e2e0b3e5 1948 _("Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
6896c0c7
RH
1949 unit->dwarf_frame_section->owner->filename,
1950 unit->dwarf_frame_section->name);
1951 break;
1952
1953 default:
1954 complaint (&symfile_complaints,
e2e0b3e5 1955 _("Corrupt data in %s:%s"),
6896c0c7
RH
1956 unit->dwarf_frame_section->owner->filename,
1957 unit->dwarf_frame_section->name);
1958 break;
1959 }
1960
1961 return ret;
1962}
cfc14b3a
MK
1963\f
1964
1965/* FIXME: kettenis/20030504: This still needs to be integrated with
1966 dwarf2read.c in a better way. */
1967
1968/* Imported from dwarf2read.c. */
cfc14b3a 1969extern asection *dwarf_frame_section;
cfc14b3a
MK
1970extern asection *dwarf_eh_frame_section;
1971
1972/* Imported from dwarf2read.c. */
1193688d 1973extern gdb_byte *dwarf2_read_section (struct objfile *objfile, asection *sectp);
cfc14b3a
MK
1974
1975void
1976dwarf2_build_frame_info (struct objfile *objfile)
1977{
1978 struct comp_unit unit;
852483bc 1979 gdb_byte *frame_ptr;
cfc14b3a
MK
1980
1981 /* Build a minimal decoding of the DWARF2 compilation unit. */
1982 unit.abfd = objfile->obfd;
1983 unit.objfile = objfile;
0912c7f2 1984 unit.dbase = 0;
0fd85043 1985 unit.tbase = 0;
cfc14b3a
MK
1986
1987 /* First add the information from the .eh_frame section. That way,
1988 the FDEs from that section are searched last. */
188dd5d6 1989 if (dwarf_eh_frame_section)
cfc14b3a 1990 {
0fd85043 1991 asection *got, *txt;
0912c7f2 1992
cfc14b3a
MK
1993 unit.cie = NULL;
1994 unit.dwarf_frame_buffer = dwarf2_read_section (objfile,
cfc14b3a
MK
1995 dwarf_eh_frame_section);
1996
2c500098 1997 unit.dwarf_frame_size = bfd_get_section_size (dwarf_eh_frame_section);
cfc14b3a
MK
1998 unit.dwarf_frame_section = dwarf_eh_frame_section;
1999
0912c7f2 2000 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
37b517aa
MK
2001 that is used for the i386/amd64 target, which currently is
2002 the only target in GCC that supports/uses the
2003 DW_EH_PE_datarel encoding. */
0912c7f2
MK
2004 got = bfd_get_section_by_name (unit.abfd, ".got");
2005 if (got)
2006 unit.dbase = got->vma;
2007
22c7ba1a
MK
2008 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
2009 so far. */
0fd85043
CV
2010 txt = bfd_get_section_by_name (unit.abfd, ".text");
2011 if (txt)
2012 unit.tbase = txt->vma;
2013
cfc14b3a
MK
2014 frame_ptr = unit.dwarf_frame_buffer;
2015 while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size)
2016 frame_ptr = decode_frame_entry (&unit, frame_ptr, 1);
2017 }
2018
188dd5d6 2019 if (dwarf_frame_section)
cfc14b3a
MK
2020 {
2021 unit.cie = NULL;
2022 unit.dwarf_frame_buffer = dwarf2_read_section (objfile,
cfc14b3a 2023 dwarf_frame_section);
2c500098 2024 unit.dwarf_frame_size = bfd_get_section_size (dwarf_frame_section);
cfc14b3a
MK
2025 unit.dwarf_frame_section = dwarf_frame_section;
2026
2027 frame_ptr = unit.dwarf_frame_buffer;
2028 while (frame_ptr < unit.dwarf_frame_buffer + unit.dwarf_frame_size)
2029 frame_ptr = decode_frame_entry (&unit, frame_ptr, 0);
2030 }
2031}
0d0e1a63
MK
2032
2033/* Provide a prototype to silence -Wmissing-prototypes. */
2034void _initialize_dwarf2_frame (void);
2035
2036void
2037_initialize_dwarf2_frame (void)
2038{
030f20e1 2039 dwarf2_frame_data = gdbarch_data_register_pre_init (dwarf2_frame_init);
8f22cb90 2040 dwarf2_frame_objfile_data = register_objfile_data ();
0d0e1a63 2041}
This page took 0.481824 seconds and 4 git commands to generate.