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