1 /* Frame unwinder for frames with DWARF Call Frame Information.
3 Copyright (C) 2003, 2004, 2005, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 Contributed by Mark Kettenis.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "dwarf2expr.h"
27 #include "frame-base.h"
28 #include "frame-unwind.h"
36 #include "gdb_assert.h"
37 #include "gdb_string.h"
39 #include "complaints.h"
40 #include "dwarf2-frame.h"
44 /* Call Frame Information (CFI). */
46 /* Common Information Entry (CIE). */
50 /* Computation Unit for this CIE. */
51 struct comp_unit
*unit
;
53 /* Offset into the .debug_frame section where this CIE was found.
54 Used to identify this CIE. */
57 /* Constant that is factored out of all advance location
59 ULONGEST code_alignment_factor
;
61 /* Constants that is factored out of all offset instructions. */
62 LONGEST data_alignment_factor
;
64 /* Return address column. */
65 ULONGEST return_address_register
;
67 /* Instruction sequence to initialize a register set. */
68 gdb_byte
*initial_instructions
;
71 /* Saved augmentation, in case it's needed later. */
74 /* Encoding of addresses. */
77 /* Target address size in bytes. */
80 /* True if a 'z' augmentation existed. */
81 unsigned char saw_z_augmentation
;
83 /* True if an 'S' augmentation existed. */
84 unsigned char signal_frame
;
86 /* The version recorded in the CIE. */
87 unsigned char version
;
90 struct dwarf2_cie_table
93 struct dwarf2_cie
**entries
;
96 /* Frame Description Entry (FDE). */
100 /* CIE for this FDE. */
101 struct dwarf2_cie
*cie
;
103 /* First location associated with this FDE. */
104 CORE_ADDR initial_location
;
106 /* Number of bytes of program instructions described by this FDE. */
107 CORE_ADDR address_range
;
109 /* Instruction sequence. */
110 gdb_byte
*instructions
;
113 /* True if this FDE is read from a .eh_frame instead of a .debug_frame
115 unsigned char eh_frame_p
;
118 struct dwarf2_fde_table
121 struct dwarf2_fde
**entries
;
124 /* A minimal decoding of DWARF2 compilation units. We only decode
125 what's needed to get to the call frame information. */
129 /* Keep the bfd convenient. */
132 struct objfile
*objfile
;
134 /* Pointer to the .debug_frame section loaded into memory. */
135 gdb_byte
*dwarf_frame_buffer
;
137 /* Length of the loaded .debug_frame section. */
138 bfd_size_type dwarf_frame_size
;
140 /* Pointer to the .debug_frame section. */
141 asection
*dwarf_frame_section
;
143 /* Base for DW_EH_PE_datarel encodings. */
146 /* Base for DW_EH_PE_textrel encodings. */
150 static struct dwarf2_fde
*dwarf2_frame_find_fde (CORE_ADDR
*pc
);
152 static int dwarf2_frame_adjust_regnum (struct gdbarch
*gdbarch
, int regnum
,
155 static CORE_ADDR
read_encoded_value (struct comp_unit
*unit
, gdb_byte encoding
,
156 int ptr_len
, gdb_byte
*buf
,
157 unsigned int *bytes_read_ptr
,
158 CORE_ADDR func_base
);
161 /* Structure describing a frame state. */
163 struct dwarf2_frame_state
165 /* Each register save state can be described in terms of a CFA slot,
166 another register, or a location expression. */
167 struct dwarf2_frame_state_reg_info
169 struct dwarf2_frame_state_reg
*reg
;
181 /* Used to implement DW_CFA_remember_state. */
182 struct dwarf2_frame_state_reg_info
*prev
;
185 /* The PC described by the current frame state. */
188 /* Initial register set from the CIE.
189 Used to implement DW_CFA_restore. */
190 struct dwarf2_frame_state_reg_info initial
;
192 /* The information we care about from the CIE. */
195 ULONGEST retaddr_column
;
197 /* Flags for known producer quirks. */
199 /* The ARM compilers, in DWARF2 mode, assume that DW_CFA_def_cfa
200 and DW_CFA_def_cfa_offset takes a factored offset. */
201 int armcc_cfa_offsets_sf
;
203 /* The ARM compilers, in DWARF2 or DWARF3 mode, may assume that
204 the CFA is defined as REG - OFFSET rather than REG + OFFSET. */
205 int armcc_cfa_offsets_reversed
;
208 /* Store the length the expression for the CFA in the `cfa_reg' field,
209 which is unused in that case. */
210 #define cfa_exp_len cfa_reg
212 /* Assert that the register set RS is large enough to store gdbarch_num_regs
213 columns. If necessary, enlarge the register set. */
216 dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info
*rs
,
219 size_t size
= sizeof (struct dwarf2_frame_state_reg
);
221 if (num_regs
<= rs
->num_regs
)
224 rs
->reg
= (struct dwarf2_frame_state_reg
*)
225 xrealloc (rs
->reg
, num_regs
* size
);
227 /* Initialize newly allocated registers. */
228 memset (rs
->reg
+ rs
->num_regs
, 0, (num_regs
- rs
->num_regs
) * size
);
229 rs
->num_regs
= num_regs
;
232 /* Copy the register columns in register set RS into newly allocated
233 memory and return a pointer to this newly created copy. */
235 static struct dwarf2_frame_state_reg
*
236 dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info
*rs
)
238 size_t size
= rs
->num_regs
* sizeof (struct dwarf2_frame_state_reg
);
239 struct dwarf2_frame_state_reg
*reg
;
241 reg
= (struct dwarf2_frame_state_reg
*) xmalloc (size
);
242 memcpy (reg
, rs
->reg
, size
);
247 /* Release the memory allocated to register set RS. */
250 dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info
*rs
)
254 dwarf2_frame_state_free_regs (rs
->prev
);
261 /* Release the memory allocated to the frame state FS. */
264 dwarf2_frame_state_free (void *p
)
266 struct dwarf2_frame_state
*fs
= p
;
268 dwarf2_frame_state_free_regs (fs
->initial
.prev
);
269 dwarf2_frame_state_free_regs (fs
->regs
.prev
);
270 xfree (fs
->initial
.reg
);
271 xfree (fs
->regs
.reg
);
276 /* Helper functions for execute_stack_op. */
279 read_reg (void *baton
, int reg
)
281 struct frame_info
*this_frame
= (struct frame_info
*) baton
;
282 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
286 regnum
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, reg
);
288 buf
= alloca (register_size (gdbarch
, regnum
));
289 get_frame_register (this_frame
, regnum
, buf
);
291 /* Convert the register to an integer. This returns a LONGEST
292 rather than a CORE_ADDR, but unpack_pointer does the same thing
293 under the covers, and this makes more sense for non-pointer
294 registers. Maybe read_reg and the associated interfaces should
295 deal with "struct value" instead of CORE_ADDR. */
296 return unpack_long (register_type (gdbarch
, regnum
), buf
);
300 read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
302 read_memory (addr
, buf
, len
);
306 no_get_frame_base (void *baton
, gdb_byte
**start
, size_t *length
)
308 internal_error (__FILE__
, __LINE__
,
309 _("Support for DW_OP_fbreg is unimplemented"));
313 no_get_tls_address (void *baton
, CORE_ADDR offset
)
315 internal_error (__FILE__
, __LINE__
,
316 _("Support for DW_OP_GNU_push_tls_address is unimplemented"));
319 /* Execute the required actions for both the DW_CFA_restore and
320 DW_CFA_restore_extended instructions. */
322 dwarf2_restore_rule (struct gdbarch
*gdbarch
, ULONGEST reg_num
,
323 struct dwarf2_frame_state
*fs
, int eh_frame_p
)
327 gdb_assert (fs
->initial
.reg
);
328 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg_num
, eh_frame_p
);
329 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
331 /* Check if this register was explicitly initialized in the
332 CIE initial instructions. If not, default the rule to
334 if (reg
< fs
->initial
.num_regs
)
335 fs
->regs
.reg
[reg
] = fs
->initial
.reg
[reg
];
337 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_UNSPECIFIED
;
339 if (fs
->regs
.reg
[reg
].how
== DWARF2_FRAME_REG_UNSPECIFIED
)
340 complaint (&symfile_complaints
, _("\
341 incomplete CFI data; DW_CFA_restore unspecified\n\
342 register %s (#%d) at %s"),
343 gdbarch_register_name
344 (gdbarch
, gdbarch_dwarf2_reg_to_regnum (gdbarch
, reg
)),
345 gdbarch_dwarf2_reg_to_regnum (gdbarch
, reg
),
346 paddress (gdbarch
, fs
->pc
));
350 execute_stack_op (gdb_byte
*exp
, ULONGEST len
, int addr_size
,
351 struct frame_info
*this_frame
, CORE_ADDR initial
)
353 struct dwarf_expr_context
*ctx
;
355 struct cleanup
*old_chain
;
357 ctx
= new_dwarf_expr_context ();
358 old_chain
= make_cleanup_free_dwarf_expr_context (ctx
);
360 ctx
->gdbarch
= get_frame_arch (this_frame
);
361 ctx
->addr_size
= addr_size
;
362 ctx
->baton
= this_frame
;
363 ctx
->read_reg
= read_reg
;
364 ctx
->read_mem
= read_mem
;
365 ctx
->get_frame_base
= no_get_frame_base
;
366 ctx
->get_tls_address
= no_get_tls_address
;
368 dwarf_expr_push (ctx
, initial
);
369 dwarf_expr_eval (ctx
, exp
, len
);
370 result
= dwarf_expr_fetch (ctx
, 0);
373 result
= read_reg (this_frame
, result
);
375 do_cleanups (old_chain
);
382 execute_cfa_program (struct dwarf2_fde
*fde
, gdb_byte
*insn_ptr
,
383 gdb_byte
*insn_end
, struct frame_info
*this_frame
,
384 struct dwarf2_frame_state
*fs
)
386 int eh_frame_p
= fde
->eh_frame_p
;
387 CORE_ADDR pc
= get_frame_pc (this_frame
);
389 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
390 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
392 while (insn_ptr
< insn_end
&& fs
->pc
<= pc
)
394 gdb_byte insn
= *insn_ptr
++;
398 if ((insn
& 0xc0) == DW_CFA_advance_loc
)
399 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
400 else if ((insn
& 0xc0) == DW_CFA_offset
)
403 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
404 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
405 offset
= utmp
* fs
->data_align
;
406 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
407 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
408 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
410 else if ((insn
& 0xc0) == DW_CFA_restore
)
413 dwarf2_restore_rule (gdbarch
, reg
, fs
, eh_frame_p
);
420 fs
->pc
= read_encoded_value (fde
->cie
->unit
, fde
->cie
->encoding
,
421 fde
->cie
->addr_size
, insn_ptr
,
422 &bytes_read
, fde
->initial_location
);
423 /* Apply the objfile offset for relocatable objects. */
424 fs
->pc
+= ANOFFSET (fde
->cie
->unit
->objfile
->section_offsets
,
425 SECT_OFF_TEXT (fde
->cie
->unit
->objfile
));
426 insn_ptr
+= bytes_read
;
429 case DW_CFA_advance_loc1
:
430 utmp
= extract_unsigned_integer (insn_ptr
, 1, byte_order
);
431 fs
->pc
+= utmp
* fs
->code_align
;
434 case DW_CFA_advance_loc2
:
435 utmp
= extract_unsigned_integer (insn_ptr
, 2, byte_order
);
436 fs
->pc
+= utmp
* fs
->code_align
;
439 case DW_CFA_advance_loc4
:
440 utmp
= extract_unsigned_integer (insn_ptr
, 4, byte_order
);
441 fs
->pc
+= utmp
* fs
->code_align
;
445 case DW_CFA_offset_extended
:
446 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
447 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
448 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
449 offset
= utmp
* fs
->data_align
;
450 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
451 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
452 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
455 case DW_CFA_restore_extended
:
456 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
457 dwarf2_restore_rule (gdbarch
, reg
, fs
, eh_frame_p
);
460 case DW_CFA_undefined
:
461 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
462 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
463 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
464 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_UNDEFINED
;
467 case DW_CFA_same_value
:
468 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
469 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
470 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
471 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAME_VALUE
;
474 case DW_CFA_register
:
475 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
476 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
477 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
478 utmp
= dwarf2_frame_adjust_regnum (gdbarch
, utmp
, eh_frame_p
);
479 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
480 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_REG
;
481 fs
->regs
.reg
[reg
].loc
.reg
= utmp
;
484 case DW_CFA_remember_state
:
486 struct dwarf2_frame_state_reg_info
*new_rs
;
488 new_rs
= XMALLOC (struct dwarf2_frame_state_reg_info
);
490 fs
->regs
.reg
= dwarf2_frame_state_copy_regs (&fs
->regs
);
491 fs
->regs
.prev
= new_rs
;
495 case DW_CFA_restore_state
:
497 struct dwarf2_frame_state_reg_info
*old_rs
= fs
->regs
.prev
;
501 complaint (&symfile_complaints
, _("\
502 bad CFI data; mismatched DW_CFA_restore_state at %s"),
503 paddress (gdbarch
, fs
->pc
));
507 xfree (fs
->regs
.reg
);
515 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->regs
.cfa_reg
);
516 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
518 if (fs
->armcc_cfa_offsets_sf
)
519 utmp
*= fs
->data_align
;
521 fs
->regs
.cfa_offset
= utmp
;
522 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
525 case DW_CFA_def_cfa_register
:
526 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->regs
.cfa_reg
);
527 fs
->regs
.cfa_reg
= dwarf2_frame_adjust_regnum (gdbarch
,
530 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
533 case DW_CFA_def_cfa_offset
:
534 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
536 if (fs
->armcc_cfa_offsets_sf
)
537 utmp
*= fs
->data_align
;
539 fs
->regs
.cfa_offset
= utmp
;
540 /* cfa_how deliberately not set. */
546 case DW_CFA_def_cfa_expression
:
547 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
,
548 &fs
->regs
.cfa_exp_len
);
549 fs
->regs
.cfa_exp
= insn_ptr
;
550 fs
->regs
.cfa_how
= CFA_EXP
;
551 insn_ptr
+= fs
->regs
.cfa_exp_len
;
554 case DW_CFA_expression
:
555 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
556 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
557 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
558 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
559 fs
->regs
.reg
[reg
].loc
.exp
= insn_ptr
;
560 fs
->regs
.reg
[reg
].exp_len
= utmp
;
561 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_EXP
;
565 case DW_CFA_offset_extended_sf
:
566 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
567 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
568 insn_ptr
= read_sleb128 (insn_ptr
, insn_end
, &offset
);
569 offset
*= fs
->data_align
;
570 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
571 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
572 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
575 case DW_CFA_val_offset
:
576 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
577 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
578 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
579 offset
= utmp
* fs
->data_align
;
580 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_VAL_OFFSET
;
581 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
584 case DW_CFA_val_offset_sf
:
585 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
586 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
587 insn_ptr
= read_sleb128 (insn_ptr
, insn_end
, &offset
);
588 offset
*= fs
->data_align
;
589 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_VAL_OFFSET
;
590 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
593 case DW_CFA_val_expression
:
594 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
595 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
596 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
597 fs
->regs
.reg
[reg
].loc
.exp
= insn_ptr
;
598 fs
->regs
.reg
[reg
].exp_len
= utmp
;
599 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_VAL_EXP
;
603 case DW_CFA_def_cfa_sf
:
604 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->regs
.cfa_reg
);
605 fs
->regs
.cfa_reg
= dwarf2_frame_adjust_regnum (gdbarch
,
608 insn_ptr
= read_sleb128 (insn_ptr
, insn_end
, &offset
);
609 fs
->regs
.cfa_offset
= offset
* fs
->data_align
;
610 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
613 case DW_CFA_def_cfa_offset_sf
:
614 insn_ptr
= read_sleb128 (insn_ptr
, insn_end
, &offset
);
615 fs
->regs
.cfa_offset
= offset
* fs
->data_align
;
616 /* cfa_how deliberately not set. */
619 case DW_CFA_GNU_window_save
:
620 /* This is SPARC-specific code, and contains hard-coded
621 constants for the register numbering scheme used by
622 GCC. Rather than having a architecture-specific
623 operation that's only ever used by a single
624 architecture, we provide the implementation here.
625 Incidentally that's what GCC does too in its
628 int size
= register_size (gdbarch
, 0);
629 dwarf2_frame_state_alloc_regs (&fs
->regs
, 32);
630 for (reg
= 8; reg
< 16; reg
++)
632 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_REG
;
633 fs
->regs
.reg
[reg
].loc
.reg
= reg
+ 16;
635 for (reg
= 16; reg
< 32; reg
++)
637 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
638 fs
->regs
.reg
[reg
].loc
.offset
= (reg
- 16) * size
;
643 case DW_CFA_GNU_args_size
:
645 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
648 case DW_CFA_GNU_negative_offset_extended
:
649 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
650 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
651 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &offset
);
652 offset
*= fs
->data_align
;
653 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
654 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
655 fs
->regs
.reg
[reg
].loc
.offset
= -offset
;
659 internal_error (__FILE__
, __LINE__
, _("Unknown CFI encountered."));
664 /* Don't allow remember/restore between CIE and FDE programs. */
665 dwarf2_frame_state_free_regs (fs
->regs
.prev
);
666 fs
->regs
.prev
= NULL
;
670 /* Architecture-specific operations. */
672 /* Per-architecture data key. */
673 static struct gdbarch_data
*dwarf2_frame_data
;
675 struct dwarf2_frame_ops
677 /* Pre-initialize the register state REG for register REGNUM. */
678 void (*init_reg
) (struct gdbarch
*, int, struct dwarf2_frame_state_reg
*,
679 struct frame_info
*);
681 /* Check whether the THIS_FRAME is a signal trampoline. */
682 int (*signal_frame_p
) (struct gdbarch
*, struct frame_info
*);
684 /* Convert .eh_frame register number to DWARF register number, or
685 adjust .debug_frame register number. */
686 int (*adjust_regnum
) (struct gdbarch
*, int, int);
689 /* Default architecture-specific register state initialization
693 dwarf2_frame_default_init_reg (struct gdbarch
*gdbarch
, int regnum
,
694 struct dwarf2_frame_state_reg
*reg
,
695 struct frame_info
*this_frame
)
697 /* If we have a register that acts as a program counter, mark it as
698 a destination for the return address. If we have a register that
699 serves as the stack pointer, arrange for it to be filled with the
700 call frame address (CFA). The other registers are marked as
703 We copy the return address to the program counter, since many
704 parts in GDB assume that it is possible to get the return address
705 by unwinding the program counter register. However, on ISA's
706 with a dedicated return address register, the CFI usually only
707 contains information to unwind that return address register.
709 The reason we're treating the stack pointer special here is
710 because in many cases GCC doesn't emit CFI for the stack pointer
711 and implicitly assumes that it is equal to the CFA. This makes
712 some sense since the DWARF specification (version 3, draft 8,
715 "Typically, the CFA is defined to be the value of the stack
716 pointer at the call site in the previous frame (which may be
717 different from its value on entry to the current frame)."
719 However, this isn't true for all platforms supported by GCC
720 (e.g. IBM S/390 and zSeries). Those architectures should provide
721 their own architecture-specific initialization function. */
723 if (regnum
== gdbarch_pc_regnum (gdbarch
))
724 reg
->how
= DWARF2_FRAME_REG_RA
;
725 else if (regnum
== gdbarch_sp_regnum (gdbarch
))
726 reg
->how
= DWARF2_FRAME_REG_CFA
;
729 /* Return a default for the architecture-specific operations. */
732 dwarf2_frame_init (struct obstack
*obstack
)
734 struct dwarf2_frame_ops
*ops
;
736 ops
= OBSTACK_ZALLOC (obstack
, struct dwarf2_frame_ops
);
737 ops
->init_reg
= dwarf2_frame_default_init_reg
;
741 /* Set the architecture-specific register state initialization
742 function for GDBARCH to INIT_REG. */
745 dwarf2_frame_set_init_reg (struct gdbarch
*gdbarch
,
746 void (*init_reg
) (struct gdbarch
*, int,
747 struct dwarf2_frame_state_reg
*,
748 struct frame_info
*))
750 struct dwarf2_frame_ops
*ops
= gdbarch_data (gdbarch
, dwarf2_frame_data
);
752 ops
->init_reg
= init_reg
;
755 /* Pre-initialize the register state REG for register REGNUM. */
758 dwarf2_frame_init_reg (struct gdbarch
*gdbarch
, int regnum
,
759 struct dwarf2_frame_state_reg
*reg
,
760 struct frame_info
*this_frame
)
762 struct dwarf2_frame_ops
*ops
= gdbarch_data (gdbarch
, dwarf2_frame_data
);
764 ops
->init_reg (gdbarch
, regnum
, reg
, this_frame
);
767 /* Set the architecture-specific signal trampoline recognition
768 function for GDBARCH to SIGNAL_FRAME_P. */
771 dwarf2_frame_set_signal_frame_p (struct gdbarch
*gdbarch
,
772 int (*signal_frame_p
) (struct gdbarch
*,
773 struct frame_info
*))
775 struct dwarf2_frame_ops
*ops
= gdbarch_data (gdbarch
, dwarf2_frame_data
);
777 ops
->signal_frame_p
= signal_frame_p
;
780 /* Query the architecture-specific signal frame recognizer for
784 dwarf2_frame_signal_frame_p (struct gdbarch
*gdbarch
,
785 struct frame_info
*this_frame
)
787 struct dwarf2_frame_ops
*ops
= gdbarch_data (gdbarch
, dwarf2_frame_data
);
789 if (ops
->signal_frame_p
== NULL
)
791 return ops
->signal_frame_p (gdbarch
, this_frame
);
794 /* Set the architecture-specific adjustment of .eh_frame and .debug_frame
798 dwarf2_frame_set_adjust_regnum (struct gdbarch
*gdbarch
,
799 int (*adjust_regnum
) (struct gdbarch
*,
802 struct dwarf2_frame_ops
*ops
= gdbarch_data (gdbarch
, dwarf2_frame_data
);
804 ops
->adjust_regnum
= adjust_regnum
;
807 /* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame
811 dwarf2_frame_adjust_regnum (struct gdbarch
*gdbarch
, int regnum
, int eh_frame_p
)
813 struct dwarf2_frame_ops
*ops
= gdbarch_data (gdbarch
, dwarf2_frame_data
);
815 if (ops
->adjust_regnum
== NULL
)
817 return ops
->adjust_regnum (gdbarch
, regnum
, eh_frame_p
);
821 dwarf2_frame_find_quirks (struct dwarf2_frame_state
*fs
,
822 struct dwarf2_fde
*fde
)
824 static const char *arm_idents
[] = {
825 "ARM C Compiler, ADS",
826 "Thumb C Compiler, ADS",
827 "ARM C++ Compiler, ADS",
828 "Thumb C++ Compiler, ADS",
829 "ARM/Thumb C/C++ Compiler, RVCT"
835 s
= find_pc_symtab (fs
->pc
);
836 if (s
== NULL
|| s
->producer
== NULL
)
839 for (i
= 0; i
< ARRAY_SIZE (arm_idents
); i
++)
840 if (strncmp (s
->producer
, arm_idents
[i
], strlen (arm_idents
[i
])) == 0)
842 if (fde
->cie
->version
== 1)
843 fs
->armcc_cfa_offsets_sf
= 1;
845 if (fde
->cie
->version
== 1)
846 fs
->armcc_cfa_offsets_reversed
= 1;
848 /* The reversed offset problem is present in some compilers
849 using DWARF3, but it was eventually fixed. Check the ARM
850 defined augmentations, which are in the format "armcc" followed
851 by a list of one-character options. The "+" option means
852 this problem is fixed (no quirk needed). If the armcc
853 augmentation is missing, the quirk is needed. */
854 if (fde
->cie
->version
== 3
855 && (strncmp (fde
->cie
->augmentation
, "armcc", 5) != 0
856 || strchr (fde
->cie
->augmentation
+ 5, '+') == NULL
))
857 fs
->armcc_cfa_offsets_reversed
= 1;
864 struct dwarf2_frame_cache
866 /* DWARF Call Frame Address. */
869 /* Set if the return address column was marked as undefined. */
870 int undefined_retaddr
;
872 /* Saved registers, indexed by GDB register number, not by DWARF
874 struct dwarf2_frame_state_reg
*reg
;
876 /* Return address register. */
877 struct dwarf2_frame_state_reg retaddr_reg
;
879 /* Target address size in bytes. */
883 static struct dwarf2_frame_cache
*
884 dwarf2_frame_cache (struct frame_info
*this_frame
, void **this_cache
)
886 struct cleanup
*old_chain
;
887 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
888 const int num_regs
= gdbarch_num_regs (gdbarch
)
889 + gdbarch_num_pseudo_regs (gdbarch
);
890 struct dwarf2_frame_cache
*cache
;
891 struct dwarf2_frame_state
*fs
;
892 struct dwarf2_fde
*fde
;
897 /* Allocate a new cache. */
898 cache
= FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache
);
899 cache
->reg
= FRAME_OBSTACK_CALLOC (num_regs
, struct dwarf2_frame_state_reg
);
901 /* Allocate and initialize the frame state. */
902 fs
= XMALLOC (struct dwarf2_frame_state
);
903 memset (fs
, 0, sizeof (struct dwarf2_frame_state
));
904 old_chain
= make_cleanup (dwarf2_frame_state_free
, fs
);
908 Note that if the next frame is never supposed to return (i.e. a call
909 to abort), the compiler might optimize away the instruction at
910 its return address. As a result the return address will
911 point at some random instruction, and the CFI for that
912 instruction is probably worthless to us. GCC's unwinder solves
913 this problem by substracting 1 from the return address to get an
914 address in the middle of a presumed call instruction (or the
915 instruction in the associated delay slot). This should only be
916 done for "normal" frames and not for resume-type frames (signal
917 handlers, sentinel frames, dummy frames). The function
918 get_frame_address_in_block does just this. It's not clear how
919 reliable the method is though; there is the potential for the
920 register state pre-call being different to that on return. */
921 fs
->pc
= get_frame_address_in_block (this_frame
);
923 /* Find the correct FDE. */
924 fde
= dwarf2_frame_find_fde (&fs
->pc
);
925 gdb_assert (fde
!= NULL
);
927 /* Extract any interesting information from the CIE. */
928 fs
->data_align
= fde
->cie
->data_alignment_factor
;
929 fs
->code_align
= fde
->cie
->code_alignment_factor
;
930 fs
->retaddr_column
= fde
->cie
->return_address_register
;
931 cache
->addr_size
= fde
->cie
->addr_size
;
933 /* Check for "quirks" - known bugs in producers. */
934 dwarf2_frame_find_quirks (fs
, fde
);
936 /* First decode all the insns in the CIE. */
937 execute_cfa_program (fde
, fde
->cie
->initial_instructions
,
938 fde
->cie
->end
, this_frame
, fs
);
940 /* Save the initialized register set. */
941 fs
->initial
= fs
->regs
;
942 fs
->initial
.reg
= dwarf2_frame_state_copy_regs (&fs
->regs
);
944 /* Then decode the insns in the FDE up to our target PC. */
945 execute_cfa_program (fde
, fde
->instructions
, fde
->end
, this_frame
, fs
);
947 /* Calculate the CFA. */
948 switch (fs
->regs
.cfa_how
)
951 cache
->cfa
= read_reg (this_frame
, fs
->regs
.cfa_reg
);
952 if (fs
->armcc_cfa_offsets_reversed
)
953 cache
->cfa
-= fs
->regs
.cfa_offset
;
955 cache
->cfa
+= fs
->regs
.cfa_offset
;
960 execute_stack_op (fs
->regs
.cfa_exp
, fs
->regs
.cfa_exp_len
,
961 cache
->addr_size
, this_frame
, 0);
965 internal_error (__FILE__
, __LINE__
, _("Unknown CFA rule."));
968 /* Initialize the register state. */
972 for (regnum
= 0; regnum
< num_regs
; regnum
++)
973 dwarf2_frame_init_reg (gdbarch
, regnum
, &cache
->reg
[regnum
], this_frame
);
976 /* Go through the DWARF2 CFI generated table and save its register
977 location information in the cache. Note that we don't skip the
978 return address column; it's perfectly all right for it to
979 correspond to a real register. If it doesn't correspond to a
980 real register, or if we shouldn't treat it as such,
981 gdbarch_dwarf2_reg_to_regnum should be defined to return a number outside
982 the range [0, gdbarch_num_regs). */
984 int column
; /* CFI speak for "register number". */
986 for (column
= 0; column
< fs
->regs
.num_regs
; column
++)
988 /* Use the GDB register number as the destination index. */
989 int regnum
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, column
);
991 /* If there's no corresponding GDB register, ignore it. */
992 if (regnum
< 0 || regnum
>= num_regs
)
995 /* NOTE: cagney/2003-09-05: CFI should specify the disposition
996 of all debug info registers. If it doesn't, complain (but
997 not too loudly). It turns out that GCC assumes that an
998 unspecified register implies "same value" when CFI (draft
999 7) specifies nothing at all. Such a register could equally
1000 be interpreted as "undefined". Also note that this check
1001 isn't sufficient; it only checks that all registers in the
1002 range [0 .. max column] are specified, and won't detect
1003 problems when a debug info register falls outside of the
1004 table. We need a way of iterating through all the valid
1005 DWARF2 register numbers. */
1006 if (fs
->regs
.reg
[column
].how
== DWARF2_FRAME_REG_UNSPECIFIED
)
1008 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_UNSPECIFIED
)
1009 complaint (&symfile_complaints
, _("\
1010 incomplete CFI data; unspecified registers (e.g., %s) at %s"),
1011 gdbarch_register_name (gdbarch
, regnum
),
1012 paddress (gdbarch
, fs
->pc
));
1015 cache
->reg
[regnum
] = fs
->regs
.reg
[column
];
1019 /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information
1020 we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules. */
1024 for (regnum
= 0; regnum
< num_regs
; regnum
++)
1026 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA
1027 || cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA_OFFSET
)
1029 struct dwarf2_frame_state_reg
*retaddr_reg
=
1030 &fs
->regs
.reg
[fs
->retaddr_column
];
1032 /* It seems rather bizarre to specify an "empty" column as
1033 the return adress column. However, this is exactly
1034 what GCC does on some targets. It turns out that GCC
1035 assumes that the return address can be found in the
1036 register corresponding to the return address column.
1037 Incidentally, that's how we should treat a return
1038 address column specifying "same value" too. */
1039 if (fs
->retaddr_column
< fs
->regs
.num_regs
1040 && retaddr_reg
->how
!= DWARF2_FRAME_REG_UNSPECIFIED
1041 && retaddr_reg
->how
!= DWARF2_FRAME_REG_SAME_VALUE
)
1043 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA
)
1044 cache
->reg
[regnum
] = *retaddr_reg
;
1046 cache
->retaddr_reg
= *retaddr_reg
;
1050 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA
)
1052 cache
->reg
[regnum
].loc
.reg
= fs
->retaddr_column
;
1053 cache
->reg
[regnum
].how
= DWARF2_FRAME_REG_SAVED_REG
;
1057 cache
->retaddr_reg
.loc
.reg
= fs
->retaddr_column
;
1058 cache
->retaddr_reg
.how
= DWARF2_FRAME_REG_SAVED_REG
;
1065 if (fs
->retaddr_column
< fs
->regs
.num_regs
1066 && fs
->regs
.reg
[fs
->retaddr_column
].how
== DWARF2_FRAME_REG_UNDEFINED
)
1067 cache
->undefined_retaddr
= 1;
1069 do_cleanups (old_chain
);
1071 *this_cache
= cache
;
1076 dwarf2_frame_this_id (struct frame_info
*this_frame
, void **this_cache
,
1077 struct frame_id
*this_id
)
1079 struct dwarf2_frame_cache
*cache
=
1080 dwarf2_frame_cache (this_frame
, this_cache
);
1082 if (cache
->undefined_retaddr
)
1085 (*this_id
) = frame_id_build (cache
->cfa
, get_frame_func (this_frame
));
1088 static struct value
*
1089 dwarf2_frame_prev_register (struct frame_info
*this_frame
, void **this_cache
,
1092 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
1093 struct dwarf2_frame_cache
*cache
=
1094 dwarf2_frame_cache (this_frame
, this_cache
);
1098 switch (cache
->reg
[regnum
].how
)
1100 case DWARF2_FRAME_REG_UNDEFINED
:
1101 /* If CFI explicitly specified that the value isn't defined,
1102 mark it as optimized away; the value isn't available. */
1103 return frame_unwind_got_optimized (this_frame
, regnum
);
1105 case DWARF2_FRAME_REG_SAVED_OFFSET
:
1106 addr
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
1107 return frame_unwind_got_memory (this_frame
, regnum
, addr
);
1109 case DWARF2_FRAME_REG_SAVED_REG
:
1111 = gdbarch_dwarf2_reg_to_regnum (gdbarch
, cache
->reg
[regnum
].loc
.reg
);
1112 return frame_unwind_got_register (this_frame
, regnum
, realnum
);
1114 case DWARF2_FRAME_REG_SAVED_EXP
:
1115 addr
= execute_stack_op (cache
->reg
[regnum
].loc
.exp
,
1116 cache
->reg
[regnum
].exp_len
,
1117 cache
->addr_size
, this_frame
, cache
->cfa
);
1118 return frame_unwind_got_memory (this_frame
, regnum
, addr
);
1120 case DWARF2_FRAME_REG_SAVED_VAL_OFFSET
:
1121 addr
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
1122 return frame_unwind_got_constant (this_frame
, regnum
, addr
);
1124 case DWARF2_FRAME_REG_SAVED_VAL_EXP
:
1125 addr
= execute_stack_op (cache
->reg
[regnum
].loc
.exp
,
1126 cache
->reg
[regnum
].exp_len
,
1127 cache
->addr_size
, this_frame
, cache
->cfa
);
1128 return frame_unwind_got_constant (this_frame
, regnum
, addr
);
1130 case DWARF2_FRAME_REG_UNSPECIFIED
:
1131 /* GCC, in its infinite wisdom decided to not provide unwind
1132 information for registers that are "same value". Since
1133 DWARF2 (3 draft 7) doesn't define such behavior, said
1134 registers are actually undefined (which is different to CFI
1135 "undefined"). Code above issues a complaint about this.
1136 Here just fudge the books, assume GCC, and that the value is
1137 more inner on the stack. */
1138 return frame_unwind_got_register (this_frame
, regnum
, regnum
);
1140 case DWARF2_FRAME_REG_SAME_VALUE
:
1141 return frame_unwind_got_register (this_frame
, regnum
, regnum
);
1143 case DWARF2_FRAME_REG_CFA
:
1144 return frame_unwind_got_address (this_frame
, regnum
, cache
->cfa
);
1146 case DWARF2_FRAME_REG_CFA_OFFSET
:
1147 addr
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
1148 return frame_unwind_got_address (this_frame
, regnum
, addr
);
1150 case DWARF2_FRAME_REG_RA_OFFSET
:
1151 addr
= cache
->reg
[regnum
].loc
.offset
;
1152 regnum
= gdbarch_dwarf2_reg_to_regnum
1153 (gdbarch
, cache
->retaddr_reg
.loc
.reg
);
1154 addr
+= get_frame_register_unsigned (this_frame
, regnum
);
1155 return frame_unwind_got_address (this_frame
, regnum
, addr
);
1157 case DWARF2_FRAME_REG_FN
:
1158 return cache
->reg
[regnum
].loc
.fn (this_frame
, this_cache
, regnum
);
1161 internal_error (__FILE__
, __LINE__
, _("Unknown register rule."));
1166 dwarf2_frame_sniffer (const struct frame_unwind
*self
,
1167 struct frame_info
*this_frame
, void **this_cache
)
1169 /* Grab an address that is guarenteed to reside somewhere within the
1170 function. get_frame_pc(), with a no-return next function, can
1171 end up returning something past the end of this function's body.
1172 If the frame we're sniffing for is a signal frame whose start
1173 address is placed on the stack by the OS, its FDE must
1174 extend one byte before its start address or we could potentially
1175 select the FDE of the previous function. */
1176 CORE_ADDR block_addr
= get_frame_address_in_block (this_frame
);
1177 struct dwarf2_fde
*fde
= dwarf2_frame_find_fde (&block_addr
);
1181 /* On some targets, signal trampolines may have unwind information.
1182 We need to recognize them so that we set the frame type
1185 if (fde
->cie
->signal_frame
1186 || dwarf2_frame_signal_frame_p (get_frame_arch (this_frame
),
1188 return self
->type
== SIGTRAMP_FRAME
;
1190 return self
->type
!= SIGTRAMP_FRAME
;
1193 static const struct frame_unwind dwarf2_frame_unwind
=
1196 dwarf2_frame_this_id
,
1197 dwarf2_frame_prev_register
,
1199 dwarf2_frame_sniffer
1202 static const struct frame_unwind dwarf2_signal_frame_unwind
=
1205 dwarf2_frame_this_id
,
1206 dwarf2_frame_prev_register
,
1208 dwarf2_frame_sniffer
1211 /* Append the DWARF-2 frame unwinders to GDBARCH's list. */
1214 dwarf2_append_unwinders (struct gdbarch
*gdbarch
)
1216 frame_unwind_append_unwinder (gdbarch
, &dwarf2_frame_unwind
);
1217 frame_unwind_append_unwinder (gdbarch
, &dwarf2_signal_frame_unwind
);
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. */
1228 dwarf2_frame_base_address (struct frame_info
*this_frame
, void **this_cache
)
1230 struct dwarf2_frame_cache
*cache
=
1231 dwarf2_frame_cache (this_frame
, this_cache
);
1236 static const struct frame_base dwarf2_frame_base
=
1238 &dwarf2_frame_unwind
,
1239 dwarf2_frame_base_address
,
1240 dwarf2_frame_base_address
,
1241 dwarf2_frame_base_address
1244 const struct frame_base
*
1245 dwarf2_frame_base_sniffer (struct frame_info
*this_frame
)
1247 CORE_ADDR block_addr
= get_frame_address_in_block (this_frame
);
1248 if (dwarf2_frame_find_fde (&block_addr
))
1249 return &dwarf2_frame_base
;
1254 const struct objfile_data
*dwarf2_frame_objfile_data
;
1257 read_1_byte (bfd
*abfd
, gdb_byte
*buf
)
1259 return bfd_get_8 (abfd
, buf
);
1263 read_4_bytes (bfd
*abfd
, gdb_byte
*buf
)
1265 return bfd_get_32 (abfd
, buf
);
1269 read_8_bytes (bfd
*abfd
, gdb_byte
*buf
)
1271 return bfd_get_64 (abfd
, buf
);
1275 read_unsigned_leb128 (bfd
*abfd
, gdb_byte
*buf
, unsigned int *bytes_read_ptr
)
1278 unsigned int num_read
;
1288 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
1291 result
|= ((byte
& 0x7f) << shift
);
1294 while (byte
& 0x80);
1296 *bytes_read_ptr
= num_read
;
1302 read_signed_leb128 (bfd
*abfd
, gdb_byte
*buf
, unsigned int *bytes_read_ptr
)
1306 unsigned int num_read
;
1315 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
1318 result
|= ((byte
& 0x7f) << shift
);
1321 while (byte
& 0x80);
1323 if (shift
< 8 * sizeof (result
) && (byte
& 0x40))
1324 result
|= -(((LONGEST
)1) << shift
);
1326 *bytes_read_ptr
= num_read
;
1332 read_initial_length (bfd
*abfd
, gdb_byte
*buf
, unsigned int *bytes_read_ptr
)
1336 result
= bfd_get_32 (abfd
, buf
);
1337 if (result
== 0xffffffff)
1339 result
= bfd_get_64 (abfd
, buf
+ 4);
1340 *bytes_read_ptr
= 12;
1343 *bytes_read_ptr
= 4;
1349 /* Pointer encoding helper functions. */
1351 /* GCC supports exception handling based on DWARF2 CFI. However, for
1352 technical reasons, it encodes addresses in its FDE's in a different
1353 way. Several "pointer encodings" are supported. The encoding
1354 that's used for a particular FDE is determined by the 'R'
1355 augmentation in the associated CIE. The argument of this
1356 augmentation is a single byte.
1358 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
1359 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
1360 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
1361 address should be interpreted (absolute, relative to the current
1362 position in the FDE, ...). Bit 7, indicates that the address
1363 should be dereferenced. */
1366 encoding_for_size (unsigned int size
)
1371 return DW_EH_PE_udata2
;
1373 return DW_EH_PE_udata4
;
1375 return DW_EH_PE_udata8
;
1377 internal_error (__FILE__
, __LINE__
, _("Unsupported address size"));
1382 read_encoded_value (struct comp_unit
*unit
, gdb_byte encoding
,
1383 int ptr_len
, gdb_byte
*buf
, unsigned int *bytes_read_ptr
,
1384 CORE_ADDR func_base
)
1389 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1391 if (encoding
& DW_EH_PE_indirect
)
1392 internal_error (__FILE__
, __LINE__
,
1393 _("Unsupported encoding: DW_EH_PE_indirect"));
1395 *bytes_read_ptr
= 0;
1397 switch (encoding
& 0x70)
1399 case DW_EH_PE_absptr
:
1402 case DW_EH_PE_pcrel
:
1403 base
= bfd_get_section_vma (unit
->abfd
, unit
->dwarf_frame_section
);
1404 base
+= (buf
- unit
->dwarf_frame_buffer
);
1406 case DW_EH_PE_datarel
:
1409 case DW_EH_PE_textrel
:
1412 case DW_EH_PE_funcrel
:
1415 case DW_EH_PE_aligned
:
1417 offset
= buf
- unit
->dwarf_frame_buffer
;
1418 if ((offset
% ptr_len
) != 0)
1420 *bytes_read_ptr
= ptr_len
- (offset
% ptr_len
);
1421 buf
+= *bytes_read_ptr
;
1425 internal_error (__FILE__
, __LINE__
, _("Invalid or unsupported encoding"));
1428 if ((encoding
& 0x07) == 0x00)
1430 encoding
|= encoding_for_size (ptr_len
);
1431 if (bfd_get_sign_extend_vma (unit
->abfd
))
1432 encoding
|= DW_EH_PE_signed
;
1435 switch (encoding
& 0x0f)
1437 case DW_EH_PE_uleb128
:
1440 gdb_byte
*end_buf
= buf
+ (sizeof (value
) + 1) * 8 / 7;
1441 *bytes_read_ptr
+= read_uleb128 (buf
, end_buf
, &value
) - buf
;
1442 return base
+ value
;
1444 case DW_EH_PE_udata2
:
1445 *bytes_read_ptr
+= 2;
1446 return (base
+ bfd_get_16 (unit
->abfd
, (bfd_byte
*) buf
));
1447 case DW_EH_PE_udata4
:
1448 *bytes_read_ptr
+= 4;
1449 return (base
+ bfd_get_32 (unit
->abfd
, (bfd_byte
*) buf
));
1450 case DW_EH_PE_udata8
:
1451 *bytes_read_ptr
+= 8;
1452 return (base
+ bfd_get_64 (unit
->abfd
, (bfd_byte
*) buf
));
1453 case DW_EH_PE_sleb128
:
1456 gdb_byte
*end_buf
= buf
+ (sizeof (value
) + 1) * 8 / 7;
1457 *bytes_read_ptr
+= read_sleb128 (buf
, end_buf
, &value
) - buf
;
1458 return base
+ value
;
1460 case DW_EH_PE_sdata2
:
1461 *bytes_read_ptr
+= 2;
1462 return (base
+ bfd_get_signed_16 (unit
->abfd
, (bfd_byte
*) buf
));
1463 case DW_EH_PE_sdata4
:
1464 *bytes_read_ptr
+= 4;
1465 return (base
+ bfd_get_signed_32 (unit
->abfd
, (bfd_byte
*) buf
));
1466 case DW_EH_PE_sdata8
:
1467 *bytes_read_ptr
+= 8;
1468 return (base
+ bfd_get_signed_64 (unit
->abfd
, (bfd_byte
*) buf
));
1470 internal_error (__FILE__
, __LINE__
, _("Invalid or unsupported encoding"));
1476 bsearch_cie_cmp (const void *key
, const void *element
)
1478 ULONGEST cie_pointer
= *(ULONGEST
*) key
;
1479 struct dwarf2_cie
*cie
= *(struct dwarf2_cie
**) element
;
1481 if (cie_pointer
== cie
->cie_pointer
)
1484 return (cie_pointer
< cie
->cie_pointer
) ? -1 : 1;
1487 /* Find CIE with the given CIE_POINTER in CIE_TABLE. */
1488 static struct dwarf2_cie
*
1489 find_cie (struct dwarf2_cie_table
*cie_table
, ULONGEST cie_pointer
)
1491 struct dwarf2_cie
**p_cie
;
1493 p_cie
= bsearch (&cie_pointer
, cie_table
->entries
, cie_table
->num_entries
,
1494 sizeof (cie_table
->entries
[0]), bsearch_cie_cmp
);
1500 /* Add a pointer to new CIE to the CIE_TABLE, allocating space for it. */
1502 add_cie (struct dwarf2_cie_table
*cie_table
, struct dwarf2_cie
*cie
)
1504 const int n
= cie_table
->num_entries
;
1507 || cie_table
->entries
[n
- 1]->cie_pointer
< cie
->cie_pointer
);
1509 cie_table
->entries
=
1510 xrealloc (cie_table
->entries
, (n
+ 1) * sizeof (cie_table
->entries
[0]));
1511 cie_table
->entries
[n
] = cie
;
1512 cie_table
->num_entries
= n
+ 1;
1516 bsearch_fde_cmp (const void *key
, const void *element
)
1518 CORE_ADDR seek_pc
= *(CORE_ADDR
*) key
;
1519 struct dwarf2_fde
*fde
= *(struct dwarf2_fde
**) element
;
1520 if (seek_pc
< fde
->initial_location
)
1522 if (seek_pc
< fde
->initial_location
+ fde
->address_range
)
1527 /* Find the FDE for *PC. Return a pointer to the FDE, and store the
1528 inital location associated with it into *PC. */
1530 static struct dwarf2_fde
*
1531 dwarf2_frame_find_fde (CORE_ADDR
*pc
)
1533 struct objfile
*objfile
;
1535 ALL_OBJFILES (objfile
)
1537 struct dwarf2_fde_table
*fde_table
;
1538 struct dwarf2_fde
**p_fde
;
1542 fde_table
= objfile_data (objfile
, dwarf2_frame_objfile_data
);
1543 if (fde_table
== NULL
)
1546 gdb_assert (objfile
->section_offsets
);
1547 offset
= ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
1549 gdb_assert (fde_table
->num_entries
> 0);
1550 if (*pc
< offset
+ fde_table
->entries
[0]->initial_location
)
1553 seek_pc
= *pc
- offset
;
1554 p_fde
= bsearch (&seek_pc
, fde_table
->entries
, fde_table
->num_entries
,
1555 sizeof (fde_table
->entries
[0]), bsearch_fde_cmp
);
1558 *pc
= (*p_fde
)->initial_location
+ offset
;
1565 /* Add a pointer to new FDE to the FDE_TABLE, allocating space for it. */
1567 add_fde (struct dwarf2_fde_table
*fde_table
, struct dwarf2_fde
*fde
)
1569 if (fde
->address_range
== 0)
1570 /* Discard useless FDEs. */
1573 fde_table
->num_entries
+= 1;
1574 fde_table
->entries
=
1575 xrealloc (fde_table
->entries
,
1576 fde_table
->num_entries
* sizeof (fde_table
->entries
[0]));
1577 fde_table
->entries
[fde_table
->num_entries
- 1] = fde
;
1580 #ifdef CC_HAS_LONG_LONG
1581 #define DW64_CIE_ID 0xffffffffffffffffULL
1583 #define DW64_CIE_ID ~0
1586 static gdb_byte
*decode_frame_entry (struct comp_unit
*unit
, gdb_byte
*start
,
1588 struct dwarf2_cie_table
*cie_table
,
1589 struct dwarf2_fde_table
*fde_table
);
1591 /* Decode the next CIE or FDE. Return NULL if invalid input, otherwise
1592 the next byte to be processed. */
1594 decode_frame_entry_1 (struct comp_unit
*unit
, gdb_byte
*start
, int eh_frame_p
,
1595 struct dwarf2_cie_table
*cie_table
,
1596 struct dwarf2_fde_table
*fde_table
)
1598 struct gdbarch
*gdbarch
= get_objfile_arch (unit
->objfile
);
1599 gdb_byte
*buf
, *end
;
1601 unsigned int bytes_read
;
1604 ULONGEST cie_pointer
;
1607 length
= read_initial_length (unit
->abfd
, buf
, &bytes_read
);
1611 /* Are we still within the section? */
1612 if (end
> unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
)
1618 /* Distinguish between 32 and 64-bit encoded frame info. */
1619 dwarf64_p
= (bytes_read
== 12);
1621 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
1625 cie_id
= DW64_CIE_ID
;
1631 cie_pointer
= read_8_bytes (unit
->abfd
, buf
);
1636 cie_pointer
= read_4_bytes (unit
->abfd
, buf
);
1640 if (cie_pointer
== cie_id
)
1642 /* This is a CIE. */
1643 struct dwarf2_cie
*cie
;
1645 unsigned int cie_version
;
1647 /* Record the offset into the .debug_frame section of this CIE. */
1648 cie_pointer
= start
- unit
->dwarf_frame_buffer
;
1650 /* Check whether we've already read it. */
1651 if (find_cie (cie_table
, cie_pointer
))
1654 cie
= (struct dwarf2_cie
*)
1655 obstack_alloc (&unit
->objfile
->objfile_obstack
,
1656 sizeof (struct dwarf2_cie
));
1657 cie
->initial_instructions
= NULL
;
1658 cie
->cie_pointer
= cie_pointer
;
1660 /* The encoding for FDE's in a normal .debug_frame section
1661 depends on the target address size. */
1662 cie
->encoding
= DW_EH_PE_absptr
;
1664 /* The target address size. For .eh_frame FDEs this is considered
1665 equal to the size of a target pointer. For .dwarf_frame FDEs,
1666 this is supposed to be the target address size from the associated
1667 CU header. FIXME: We do not have a good way to determine the
1668 latter. Always use the target pointer size for now. */
1669 cie
->addr_size
= gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
;
1671 /* We'll determine the final value later, but we need to
1672 initialize it conservatively. */
1673 cie
->signal_frame
= 0;
1675 /* Check version number. */
1676 cie_version
= read_1_byte (unit
->abfd
, buf
);
1677 if (cie_version
!= 1 && cie_version
!= 3)
1679 cie
->version
= cie_version
;
1682 /* Interpret the interesting bits of the augmentation. */
1683 cie
->augmentation
= augmentation
= (char *) buf
;
1684 buf
+= (strlen (augmentation
) + 1);
1686 /* Ignore armcc augmentations. We only use them for quirks,
1687 and that doesn't happen until later. */
1688 if (strncmp (augmentation
, "armcc", 5) == 0)
1689 augmentation
+= strlen (augmentation
);
1691 /* The GCC 2.x "eh" augmentation has a pointer immediately
1692 following the augmentation string, so it must be handled
1694 if (augmentation
[0] == 'e' && augmentation
[1] == 'h')
1697 buf
+= gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
;
1701 cie
->code_alignment_factor
=
1702 read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1705 cie
->data_alignment_factor
=
1706 read_signed_leb128 (unit
->abfd
, buf
, &bytes_read
);
1709 if (cie_version
== 1)
1711 cie
->return_address_register
= read_1_byte (unit
->abfd
, buf
);
1715 cie
->return_address_register
= read_unsigned_leb128 (unit
->abfd
, buf
,
1717 cie
->return_address_register
1718 = dwarf2_frame_adjust_regnum (gdbarch
,
1719 cie
->return_address_register
,
1724 cie
->saw_z_augmentation
= (*augmentation
== 'z');
1725 if (cie
->saw_z_augmentation
)
1729 length
= read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1733 cie
->initial_instructions
= buf
+ length
;
1737 while (*augmentation
)
1739 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1740 if (*augmentation
== 'L')
1747 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1748 else if (*augmentation
== 'R')
1750 cie
->encoding
= *buf
++;
1754 /* "P" indicates a personality routine in the CIE augmentation. */
1755 else if (*augmentation
== 'P')
1757 /* Skip. Avoid indirection since we throw away the result. */
1758 gdb_byte encoding
= (*buf
++) & ~DW_EH_PE_indirect
;
1759 read_encoded_value (unit
, encoding
, cie
->addr_size
,
1760 buf
, &bytes_read
, 0);
1765 /* "S" indicates a signal frame, such that the return
1766 address must not be decremented to locate the call frame
1767 info for the previous frame; it might even be the first
1768 instruction of a function, so decrementing it would take
1769 us to a different function. */
1770 else if (*augmentation
== 'S')
1772 cie
->signal_frame
= 1;
1776 /* Otherwise we have an unknown augmentation. Assume that either
1777 there is no augmentation data, or we saw a 'z' prefix. */
1780 if (cie
->initial_instructions
)
1781 buf
= cie
->initial_instructions
;
1786 cie
->initial_instructions
= buf
;
1790 add_cie (cie_table
, cie
);
1794 /* This is a FDE. */
1795 struct dwarf2_fde
*fde
;
1797 /* In an .eh_frame section, the CIE pointer is the delta between the
1798 address within the FDE where the CIE pointer is stored and the
1799 address of the CIE. Convert it to an offset into the .eh_frame
1803 cie_pointer
= buf
- unit
->dwarf_frame_buffer
- cie_pointer
;
1804 cie_pointer
-= (dwarf64_p
? 8 : 4);
1807 /* In either case, validate the result is still within the section. */
1808 if (cie_pointer
>= unit
->dwarf_frame_size
)
1811 fde
= (struct dwarf2_fde
*)
1812 obstack_alloc (&unit
->objfile
->objfile_obstack
,
1813 sizeof (struct dwarf2_fde
));
1814 fde
->cie
= find_cie (cie_table
, cie_pointer
);
1815 if (fde
->cie
== NULL
)
1817 decode_frame_entry (unit
, unit
->dwarf_frame_buffer
+ cie_pointer
,
1818 eh_frame_p
, cie_table
, fde_table
);
1819 fde
->cie
= find_cie (cie_table
, cie_pointer
);
1822 gdb_assert (fde
->cie
!= NULL
);
1824 fde
->initial_location
=
1825 read_encoded_value (unit
, fde
->cie
->encoding
, fde
->cie
->addr_size
,
1826 buf
, &bytes_read
, 0);
1829 fde
->address_range
=
1830 read_encoded_value (unit
, fde
->cie
->encoding
& 0x0f,
1831 fde
->cie
->addr_size
, buf
, &bytes_read
, 0);
1834 /* A 'z' augmentation in the CIE implies the presence of an
1835 augmentation field in the FDE as well. The only thing known
1836 to be in here at present is the LSDA entry for EH. So we
1837 can skip the whole thing. */
1838 if (fde
->cie
->saw_z_augmentation
)
1842 length
= read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1843 buf
+= bytes_read
+ length
;
1848 fde
->instructions
= buf
;
1851 fde
->eh_frame_p
= eh_frame_p
;
1853 add_fde (fde_table
, fde
);
1859 /* Read a CIE or FDE in BUF and decode it. */
1861 decode_frame_entry (struct comp_unit
*unit
, gdb_byte
*start
, int eh_frame_p
,
1862 struct dwarf2_cie_table
*cie_table
,
1863 struct dwarf2_fde_table
*fde_table
)
1865 enum { NONE
, ALIGN4
, ALIGN8
, FAIL
} workaround
= NONE
;
1868 ptrdiff_t start_offset
;
1872 ret
= decode_frame_entry_1 (unit
, start
, eh_frame_p
,
1873 cie_table
, fde_table
);
1877 /* We have corrupt input data of some form. */
1879 /* ??? Try, weakly, to work around compiler/assembler/linker bugs
1880 and mismatches wrt padding and alignment of debug sections. */
1881 /* Note that there is no requirement in the standard for any
1882 alignment at all in the frame unwind sections. Testing for
1883 alignment before trying to interpret data would be incorrect.
1885 However, GCC traditionally arranged for frame sections to be
1886 sized such that the FDE length and CIE fields happen to be
1887 aligned (in theory, for performance). This, unfortunately,
1888 was done with .align directives, which had the side effect of
1889 forcing the section to be aligned by the linker.
1891 This becomes a problem when you have some other producer that
1892 creates frame sections that are not as strictly aligned. That
1893 produces a hole in the frame info that gets filled by the
1896 The GCC behaviour is arguably a bug, but it's effectively now
1897 part of the ABI, so we're now stuck with it, at least at the
1898 object file level. A smart linker may decide, in the process
1899 of compressing duplicate CIE information, that it can rewrite
1900 the entire output section without this extra padding. */
1902 start_offset
= start
- unit
->dwarf_frame_buffer
;
1903 if (workaround
< ALIGN4
&& (start_offset
& 3) != 0)
1905 start
+= 4 - (start_offset
& 3);
1906 workaround
= ALIGN4
;
1909 if (workaround
< ALIGN8
&& (start_offset
& 7) != 0)
1911 start
+= 8 - (start_offset
& 7);
1912 workaround
= ALIGN8
;
1916 /* Nothing left to try. Arrange to return as if we've consumed
1917 the entire input section. Hopefully we'll get valid info from
1918 the other of .debug_frame/.eh_frame. */
1920 ret
= unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
;
1930 complaint (&symfile_complaints
,
1931 _("Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
1932 unit
->dwarf_frame_section
->owner
->filename
,
1933 unit
->dwarf_frame_section
->name
);
1937 complaint (&symfile_complaints
,
1938 _("Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
1939 unit
->dwarf_frame_section
->owner
->filename
,
1940 unit
->dwarf_frame_section
->name
);
1944 complaint (&symfile_complaints
,
1945 _("Corrupt data in %s:%s"),
1946 unit
->dwarf_frame_section
->owner
->filename
,
1947 unit
->dwarf_frame_section
->name
);
1955 /* Imported from dwarf2read.c. */
1956 extern void dwarf2_get_section_info (struct objfile
*, const char *, asection
**,
1957 gdb_byte
**, bfd_size_type
*);
1960 qsort_fde_cmp (const void *a
, const void *b
)
1962 struct dwarf2_fde
*aa
= *(struct dwarf2_fde
**)a
;
1963 struct dwarf2_fde
*bb
= *(struct dwarf2_fde
**)b
;
1965 if (aa
->initial_location
== bb
->initial_location
)
1967 if (aa
->address_range
!= bb
->address_range
1968 && aa
->eh_frame_p
== 0 && bb
->eh_frame_p
== 0)
1969 /* Linker bug, e.g. gold/10400.
1970 Work around it by keeping stable sort order. */
1971 return (a
< b
) ? -1 : 1;
1973 /* Put eh_frame entries after debug_frame ones. */
1974 return aa
->eh_frame_p
- bb
->eh_frame_p
;
1977 return (aa
->initial_location
< bb
->initial_location
) ? -1 : 1;
1981 dwarf2_build_frame_info (struct objfile
*objfile
)
1983 struct comp_unit
*unit
;
1984 gdb_byte
*frame_ptr
;
1985 struct dwarf2_cie_table cie_table
;
1986 struct dwarf2_fde_table fde_table
;
1988 cie_table
.num_entries
= 0;
1989 cie_table
.entries
= NULL
;
1991 fde_table
.num_entries
= 0;
1992 fde_table
.entries
= NULL
;
1994 /* Build a minimal decoding of the DWARF2 compilation unit. */
1995 unit
= (struct comp_unit
*) obstack_alloc (&objfile
->objfile_obstack
,
1996 sizeof (struct comp_unit
));
1997 unit
->abfd
= objfile
->obfd
;
1998 unit
->objfile
= objfile
;
2002 dwarf2_get_section_info (objfile
, ".eh_frame",
2003 &unit
->dwarf_frame_section
,
2004 &unit
->dwarf_frame_buffer
,
2005 &unit
->dwarf_frame_size
);
2006 if (unit
->dwarf_frame_size
)
2008 asection
*got
, *txt
;
2010 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
2011 that is used for the i386/amd64 target, which currently is
2012 the only target in GCC that supports/uses the
2013 DW_EH_PE_datarel encoding. */
2014 got
= bfd_get_section_by_name (unit
->abfd
, ".got");
2016 unit
->dbase
= got
->vma
;
2018 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
2020 txt
= bfd_get_section_by_name (unit
->abfd
, ".text");
2022 unit
->tbase
= txt
->vma
;
2024 frame_ptr
= unit
->dwarf_frame_buffer
;
2025 while (frame_ptr
< unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
)
2026 frame_ptr
= decode_frame_entry (unit
, frame_ptr
, 1,
2027 &cie_table
, &fde_table
);
2029 if (cie_table
.num_entries
!= 0)
2031 /* Reinit cie_table: debug_frame has different CIEs. */
2032 xfree (cie_table
.entries
);
2033 cie_table
.num_entries
= 0;
2034 cie_table
.entries
= NULL
;
2038 dwarf2_get_section_info (objfile
, ".debug_frame",
2039 &unit
->dwarf_frame_section
,
2040 &unit
->dwarf_frame_buffer
,
2041 &unit
->dwarf_frame_size
);
2042 if (unit
->dwarf_frame_size
)
2044 frame_ptr
= unit
->dwarf_frame_buffer
;
2045 while (frame_ptr
< unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
)
2046 frame_ptr
= decode_frame_entry (unit
, frame_ptr
, 0,
2047 &cie_table
, &fde_table
);
2050 /* Discard the cie_table, it is no longer needed. */
2051 if (cie_table
.num_entries
!= 0)
2053 xfree (cie_table
.entries
);
2054 cie_table
.entries
= NULL
; /* Paranoia. */
2055 cie_table
.num_entries
= 0; /* Paranoia. */
2058 if (fde_table
.num_entries
!= 0)
2060 struct dwarf2_fde_table
*fde_table2
;
2063 /* Prepare FDE table for lookups. */
2064 qsort (fde_table
.entries
, fde_table
.num_entries
,
2065 sizeof (fde_table
.entries
[0]), qsort_fde_cmp
);
2067 /* Copy fde_table to obstack: it is needed at runtime. */
2068 fde_table2
= (struct dwarf2_fde_table
*)
2069 obstack_alloc (&objfile
->objfile_obstack
, sizeof (*fde_table2
));
2071 /* Since we'll be doing bsearch, squeeze out identical (except for
2072 eh_frame_p) fde entries so bsearch result is predictable. */
2073 for (i
= 0, j
= 0; j
< fde_table
.num_entries
; ++i
)
2077 obstack_grow (&objfile
->objfile_obstack
, &fde_table
.entries
[j
],
2078 sizeof (fde_table
.entries
[0]));
2079 while (++j
< fde_table
.num_entries
2080 && (fde_table
.entries
[k
]->initial_location
==
2081 fde_table
.entries
[j
]->initial_location
))
2084 fde_table2
->entries
= obstack_finish (&objfile
->objfile_obstack
);
2085 fde_table2
->num_entries
= i
;
2086 set_objfile_data (objfile
, dwarf2_frame_objfile_data
, fde_table2
);
2088 /* Discard the original fde_table. */
2089 xfree (fde_table
.entries
);
2093 /* Provide a prototype to silence -Wmissing-prototypes. */
2094 void _initialize_dwarf2_frame (void);
2097 _initialize_dwarf2_frame (void)
2099 dwarf2_frame_data
= gdbarch_data_register_pre_init (dwarf2_frame_init
);
2100 dwarf2_frame_objfile_data
= register_objfile_data ();