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