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"
25 #include "elf/dwarf2.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
;
89 struct dwarf2_cie
*next
;
92 /* Frame Description Entry (FDE). */
96 /* CIE for this FDE. */
97 struct dwarf2_cie
*cie
;
99 /* First location associated with this FDE. */
100 CORE_ADDR initial_location
;
102 /* Number of bytes of program instructions described by this FDE. */
103 CORE_ADDR address_range
;
105 /* Instruction sequence. */
106 gdb_byte
*instructions
;
109 /* True if this FDE is read from a .eh_frame instead of a .debug_frame
111 unsigned char eh_frame_p
;
113 struct dwarf2_fde
*next
;
116 /* A minimal decoding of DWARF2 compilation units. We only decode
117 what's needed to get to the call frame information. */
121 /* Keep the bfd convenient. */
124 struct objfile
*objfile
;
126 /* Linked list of CIEs for this object. */
127 struct dwarf2_cie
*cie
;
129 /* Pointer to the .debug_frame section loaded into memory. */
130 gdb_byte
*dwarf_frame_buffer
;
132 /* Length of the loaded .debug_frame section. */
133 unsigned long dwarf_frame_size
;
135 /* Pointer to the .debug_frame section. */
136 asection
*dwarf_frame_section
;
138 /* Base for DW_EH_PE_datarel encodings. */
141 /* Base for DW_EH_PE_textrel encodings. */
145 static struct dwarf2_fde
*dwarf2_frame_find_fde (CORE_ADDR
*pc
);
147 static int dwarf2_frame_adjust_regnum (struct gdbarch
*gdbarch
, int regnum
,
150 static CORE_ADDR
read_encoded_value (struct comp_unit
*unit
, gdb_byte encoding
,
151 int ptr_len
, gdb_byte
*buf
,
152 unsigned int *bytes_read_ptr
,
153 CORE_ADDR func_base
);
156 /* Structure describing a frame state. */
158 struct dwarf2_frame_state
160 /* Each register save state can be described in terms of a CFA slot,
161 another register, or a location expression. */
162 struct dwarf2_frame_state_reg_info
164 struct dwarf2_frame_state_reg
*reg
;
167 /* Used to implement DW_CFA_remember_state. */
168 struct dwarf2_frame_state_reg_info
*prev
;
180 /* The PC described by the current frame state. */
183 /* Initial register set from the CIE.
184 Used to implement DW_CFA_restore. */
185 struct dwarf2_frame_state_reg_info initial
;
187 /* The information we care about from the CIE. */
190 ULONGEST retaddr_column
;
192 /* Flags for known producer quirks. */
194 /* The ARM compilers, in DWARF2 mode, assume that DW_CFA_def_cfa
195 and DW_CFA_def_cfa_offset takes a factored offset. */
196 int armcc_cfa_offsets_sf
;
198 /* The ARM compilers, in DWARF2 or DWARF3 mode, may assume that
199 the CFA is defined as REG - OFFSET rather than REG + OFFSET. */
200 int armcc_cfa_offsets_reversed
;
203 /* Store the length the expression for the CFA in the `cfa_reg' field,
204 which is unused in that case. */
205 #define cfa_exp_len cfa_reg
207 /* Assert that the register set RS is large enough to store gdbarch_num_regs
208 columns. If necessary, enlarge the register set. */
211 dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info
*rs
,
214 size_t size
= sizeof (struct dwarf2_frame_state_reg
);
216 if (num_regs
<= rs
->num_regs
)
219 rs
->reg
= (struct dwarf2_frame_state_reg
*)
220 xrealloc (rs
->reg
, num_regs
* size
);
222 /* Initialize newly allocated registers. */
223 memset (rs
->reg
+ rs
->num_regs
, 0, (num_regs
- rs
->num_regs
) * size
);
224 rs
->num_regs
= num_regs
;
227 /* Copy the register columns in register set RS into newly allocated
228 memory and return a pointer to this newly created copy. */
230 static struct dwarf2_frame_state_reg
*
231 dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info
*rs
)
233 size_t size
= rs
->num_regs
* sizeof (struct dwarf2_frame_state_reg
);
234 struct dwarf2_frame_state_reg
*reg
;
236 reg
= (struct dwarf2_frame_state_reg
*) xmalloc (size
);
237 memcpy (reg
, rs
->reg
, size
);
242 /* Release the memory allocated to register set RS. */
245 dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info
*rs
)
249 dwarf2_frame_state_free_regs (rs
->prev
);
256 /* Release the memory allocated to the frame state FS. */
259 dwarf2_frame_state_free (void *p
)
261 struct dwarf2_frame_state
*fs
= p
;
263 dwarf2_frame_state_free_regs (fs
->initial
.prev
);
264 dwarf2_frame_state_free_regs (fs
->regs
.prev
);
265 xfree (fs
->initial
.reg
);
266 xfree (fs
->regs
.reg
);
271 /* Helper functions for execute_stack_op. */
274 read_reg (void *baton
, int reg
)
276 struct frame_info
*this_frame
= (struct frame_info
*) baton
;
277 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
281 regnum
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, reg
);
283 buf
= alloca (register_size (gdbarch
, regnum
));
284 get_frame_register (this_frame
, regnum
, buf
);
286 /* Convert the register to an integer. This returns a LONGEST
287 rather than a CORE_ADDR, but unpack_pointer does the same thing
288 under the covers, and this makes more sense for non-pointer
289 registers. Maybe read_reg and the associated interfaces should
290 deal with "struct value" instead of CORE_ADDR. */
291 return unpack_long (register_type (gdbarch
, regnum
), buf
);
295 read_mem (void *baton
, gdb_byte
*buf
, CORE_ADDR addr
, size_t len
)
297 read_memory (addr
, buf
, len
);
301 no_get_frame_base (void *baton
, gdb_byte
**start
, size_t *length
)
303 internal_error (__FILE__
, __LINE__
,
304 _("Support for DW_OP_fbreg is unimplemented"));
308 no_get_tls_address (void *baton
, CORE_ADDR offset
)
310 internal_error (__FILE__
, __LINE__
,
311 _("Support for DW_OP_GNU_push_tls_address is unimplemented"));
314 /* Execute the required actions for both the DW_CFA_restore and
315 DW_CFA_restore_extended instructions. */
317 dwarf2_restore_rule (struct gdbarch
*gdbarch
, ULONGEST reg_num
,
318 struct dwarf2_frame_state
*fs
, int eh_frame_p
)
322 gdb_assert (fs
->initial
.reg
);
323 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg_num
, eh_frame_p
);
324 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
326 /* Check if this register was explicitly initialized in the
327 CIE initial instructions. If not, default the rule to
329 if (reg
< fs
->initial
.num_regs
)
330 fs
->regs
.reg
[reg
] = fs
->initial
.reg
[reg
];
332 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_UNSPECIFIED
;
334 if (fs
->regs
.reg
[reg
].how
== DWARF2_FRAME_REG_UNSPECIFIED
)
335 complaint (&symfile_complaints
, _("\
336 incomplete CFI data; DW_CFA_restore unspecified\n\
337 register %s (#%d) at 0x%s"),
338 gdbarch_register_name
339 (gdbarch
, gdbarch_dwarf2_reg_to_regnum (gdbarch
, reg
)),
340 gdbarch_dwarf2_reg_to_regnum (gdbarch
, reg
),
345 execute_stack_op (gdb_byte
*exp
, ULONGEST len
, int addr_size
,
346 struct frame_info
*this_frame
, CORE_ADDR initial
)
348 struct dwarf_expr_context
*ctx
;
351 ctx
= new_dwarf_expr_context ();
352 ctx
->gdbarch
= get_frame_arch (this_frame
);
353 ctx
->addr_size
= addr_size
;
354 ctx
->baton
= this_frame
;
355 ctx
->read_reg
= read_reg
;
356 ctx
->read_mem
= read_mem
;
357 ctx
->get_frame_base
= no_get_frame_base
;
358 ctx
->get_tls_address
= no_get_tls_address
;
360 dwarf_expr_push (ctx
, initial
);
361 dwarf_expr_eval (ctx
, exp
, len
);
362 result
= dwarf_expr_fetch (ctx
, 0);
365 result
= read_reg (this_frame
, result
);
367 free_dwarf_expr_context (ctx
);
374 execute_cfa_program (struct dwarf2_fde
*fde
, gdb_byte
*insn_ptr
,
375 gdb_byte
*insn_end
, struct frame_info
*this_frame
,
376 struct dwarf2_frame_state
*fs
)
378 int eh_frame_p
= fde
->eh_frame_p
;
379 CORE_ADDR pc
= get_frame_pc (this_frame
);
381 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
383 while (insn_ptr
< insn_end
&& fs
->pc
<= pc
)
385 gdb_byte insn
= *insn_ptr
++;
389 if ((insn
& 0xc0) == DW_CFA_advance_loc
)
390 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
391 else if ((insn
& 0xc0) == DW_CFA_offset
)
394 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
395 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
396 offset
= utmp
* fs
->data_align
;
397 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
398 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
399 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
401 else if ((insn
& 0xc0) == DW_CFA_restore
)
404 dwarf2_restore_rule (gdbarch
, reg
, fs
, eh_frame_p
);
411 fs
->pc
= read_encoded_value (fde
->cie
->unit
, fde
->cie
->encoding
,
412 fde
->cie
->addr_size
, insn_ptr
,
413 &bytes_read
, fde
->initial_location
);
414 /* Apply the objfile offset for relocatable objects. */
415 fs
->pc
+= ANOFFSET (fde
->cie
->unit
->objfile
->section_offsets
,
416 SECT_OFF_TEXT (fde
->cie
->unit
->objfile
));
417 insn_ptr
+= bytes_read
;
420 case DW_CFA_advance_loc1
:
421 utmp
= extract_unsigned_integer (insn_ptr
, 1);
422 fs
->pc
+= utmp
* fs
->code_align
;
425 case DW_CFA_advance_loc2
:
426 utmp
= extract_unsigned_integer (insn_ptr
, 2);
427 fs
->pc
+= utmp
* fs
->code_align
;
430 case DW_CFA_advance_loc4
:
431 utmp
= extract_unsigned_integer (insn_ptr
, 4);
432 fs
->pc
+= utmp
* fs
->code_align
;
436 case DW_CFA_offset_extended
:
437 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
438 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
439 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
440 offset
= utmp
* fs
->data_align
;
441 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
442 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
443 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
446 case DW_CFA_restore_extended
:
447 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
448 dwarf2_restore_rule (gdbarch
, reg
, fs
, eh_frame_p
);
451 case DW_CFA_undefined
:
452 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
453 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
454 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
455 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_UNDEFINED
;
458 case DW_CFA_same_value
:
459 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
460 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
461 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
462 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAME_VALUE
;
465 case DW_CFA_register
:
466 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
467 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
468 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
469 utmp
= dwarf2_frame_adjust_regnum (gdbarch
, utmp
, eh_frame_p
);
470 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
471 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_REG
;
472 fs
->regs
.reg
[reg
].loc
.reg
= utmp
;
475 case DW_CFA_remember_state
:
477 struct dwarf2_frame_state_reg_info
*new_rs
;
479 new_rs
= XMALLOC (struct dwarf2_frame_state_reg_info
);
481 fs
->regs
.reg
= dwarf2_frame_state_copy_regs (&fs
->regs
);
482 fs
->regs
.prev
= new_rs
;
486 case DW_CFA_restore_state
:
488 struct dwarf2_frame_state_reg_info
*old_rs
= fs
->regs
.prev
;
492 complaint (&symfile_complaints
, _("\
493 bad CFI data; mismatched DW_CFA_restore_state at 0x%s"), paddr (fs
->pc
));
497 xfree (fs
->regs
.reg
);
505 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_reg
);
506 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
508 if (fs
->armcc_cfa_offsets_sf
)
509 utmp
*= fs
->data_align
;
511 fs
->cfa_offset
= utmp
;
512 fs
->cfa_how
= CFA_REG_OFFSET
;
515 case DW_CFA_def_cfa_register
:
516 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_reg
);
517 fs
->cfa_reg
= dwarf2_frame_adjust_regnum (gdbarch
, fs
->cfa_reg
,
519 fs
->cfa_how
= CFA_REG_OFFSET
;
522 case DW_CFA_def_cfa_offset
:
523 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
525 if (fs
->armcc_cfa_offsets_sf
)
526 utmp
*= fs
->data_align
;
528 fs
->cfa_offset
= utmp
;
529 /* cfa_how deliberately not set. */
535 case DW_CFA_def_cfa_expression
:
536 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_exp_len
);
537 fs
->cfa_exp
= insn_ptr
;
538 fs
->cfa_how
= CFA_EXP
;
539 insn_ptr
+= fs
->cfa_exp_len
;
542 case DW_CFA_expression
:
543 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
544 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
545 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
546 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
547 fs
->regs
.reg
[reg
].loc
.exp
= insn_ptr
;
548 fs
->regs
.reg
[reg
].exp_len
= utmp
;
549 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_EXP
;
553 case DW_CFA_offset_extended_sf
:
554 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
555 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
556 insn_ptr
= read_sleb128 (insn_ptr
, insn_end
, &offset
);
557 offset
*= fs
->data_align
;
558 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
559 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
560 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
563 case DW_CFA_val_offset
:
564 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
565 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
566 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
567 offset
= utmp
* fs
->data_align
;
568 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_VAL_OFFSET
;
569 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
572 case DW_CFA_val_offset_sf
:
573 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
574 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
575 insn_ptr
= read_sleb128 (insn_ptr
, insn_end
, &offset
);
576 offset
*= fs
->data_align
;
577 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_VAL_OFFSET
;
578 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
581 case DW_CFA_val_expression
:
582 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
583 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
584 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
585 fs
->regs
.reg
[reg
].loc
.exp
= insn_ptr
;
586 fs
->regs
.reg
[reg
].exp_len
= utmp
;
587 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_VAL_EXP
;
591 case DW_CFA_def_cfa_sf
:
592 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_reg
);
593 fs
->cfa_reg
= dwarf2_frame_adjust_regnum (gdbarch
, fs
->cfa_reg
,
595 insn_ptr
= read_sleb128 (insn_ptr
, insn_end
, &offset
);
596 fs
->cfa_offset
= offset
* fs
->data_align
;
597 fs
->cfa_how
= CFA_REG_OFFSET
;
600 case DW_CFA_def_cfa_offset_sf
:
601 insn_ptr
= read_sleb128 (insn_ptr
, insn_end
, &offset
);
602 fs
->cfa_offset
= offset
* fs
->data_align
;
603 /* cfa_how deliberately not set. */
606 case DW_CFA_GNU_window_save
:
607 /* This is SPARC-specific code, and contains hard-coded
608 constants for the register numbering scheme used by
609 GCC. Rather than having a architecture-specific
610 operation that's only ever used by a single
611 architecture, we provide the implementation here.
612 Incidentally that's what GCC does too in its
615 int size
= register_size (gdbarch
, 0);
616 dwarf2_frame_state_alloc_regs (&fs
->regs
, 32);
617 for (reg
= 8; reg
< 16; reg
++)
619 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_REG
;
620 fs
->regs
.reg
[reg
].loc
.reg
= reg
+ 16;
622 for (reg
= 16; reg
< 32; reg
++)
624 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
625 fs
->regs
.reg
[reg
].loc
.offset
= (reg
- 16) * size
;
630 case DW_CFA_GNU_args_size
:
632 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
635 case DW_CFA_GNU_negative_offset_extended
:
636 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
637 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
638 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &offset
);
639 offset
*= fs
->data_align
;
640 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
641 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
642 fs
->regs
.reg
[reg
].loc
.offset
= -offset
;
646 internal_error (__FILE__
, __LINE__
, _("Unknown CFI encountered."));
651 /* Don't allow remember/restore between CIE and FDE programs. */
652 dwarf2_frame_state_free_regs (fs
->regs
.prev
);
653 fs
->regs
.prev
= NULL
;
657 /* Architecture-specific operations. */
659 /* Per-architecture data key. */
660 static struct gdbarch_data
*dwarf2_frame_data
;
662 struct dwarf2_frame_ops
664 /* Pre-initialize the register state REG for register REGNUM. */
665 void (*init_reg
) (struct gdbarch
*, int, struct dwarf2_frame_state_reg
*,
666 struct frame_info
*);
668 /* Check whether the THIS_FRAME is a signal trampoline. */
669 int (*signal_frame_p
) (struct gdbarch
*, struct frame_info
*);
671 /* Convert .eh_frame register number to DWARF register number, or
672 adjust .debug_frame register number. */
673 int (*adjust_regnum
) (struct gdbarch
*, int, int);
676 /* Default architecture-specific register state initialization
680 dwarf2_frame_default_init_reg (struct gdbarch
*gdbarch
, int regnum
,
681 struct dwarf2_frame_state_reg
*reg
,
682 struct frame_info
*this_frame
)
684 /* If we have a register that acts as a program counter, mark it as
685 a destination for the return address. If we have a register that
686 serves as the stack pointer, arrange for it to be filled with the
687 call frame address (CFA). The other registers are marked as
690 We copy the return address to the program counter, since many
691 parts in GDB assume that it is possible to get the return address
692 by unwinding the program counter register. However, on ISA's
693 with a dedicated return address register, the CFI usually only
694 contains information to unwind that return address register.
696 The reason we're treating the stack pointer special here is
697 because in many cases GCC doesn't emit CFI for the stack pointer
698 and implicitly assumes that it is equal to the CFA. This makes
699 some sense since the DWARF specification (version 3, draft 8,
702 "Typically, the CFA is defined to be the value of the stack
703 pointer at the call site in the previous frame (which may be
704 different from its value on entry to the current frame)."
706 However, this isn't true for all platforms supported by GCC
707 (e.g. IBM S/390 and zSeries). Those architectures should provide
708 their own architecture-specific initialization function. */
710 if (regnum
== gdbarch_pc_regnum (gdbarch
))
711 reg
->how
= DWARF2_FRAME_REG_RA
;
712 else if (regnum
== gdbarch_sp_regnum (gdbarch
))
713 reg
->how
= DWARF2_FRAME_REG_CFA
;
716 /* Return a default for the architecture-specific operations. */
719 dwarf2_frame_init (struct obstack
*obstack
)
721 struct dwarf2_frame_ops
*ops
;
723 ops
= OBSTACK_ZALLOC (obstack
, struct dwarf2_frame_ops
);
724 ops
->init_reg
= dwarf2_frame_default_init_reg
;
728 /* Set the architecture-specific register state initialization
729 function for GDBARCH to INIT_REG. */
732 dwarf2_frame_set_init_reg (struct gdbarch
*gdbarch
,
733 void (*init_reg
) (struct gdbarch
*, int,
734 struct dwarf2_frame_state_reg
*,
735 struct frame_info
*))
737 struct dwarf2_frame_ops
*ops
= gdbarch_data (gdbarch
, dwarf2_frame_data
);
739 ops
->init_reg
= init_reg
;
742 /* Pre-initialize the register state REG for register REGNUM. */
745 dwarf2_frame_init_reg (struct gdbarch
*gdbarch
, int regnum
,
746 struct dwarf2_frame_state_reg
*reg
,
747 struct frame_info
*this_frame
)
749 struct dwarf2_frame_ops
*ops
= gdbarch_data (gdbarch
, dwarf2_frame_data
);
751 ops
->init_reg (gdbarch
, regnum
, reg
, this_frame
);
754 /* Set the architecture-specific signal trampoline recognition
755 function for GDBARCH to SIGNAL_FRAME_P. */
758 dwarf2_frame_set_signal_frame_p (struct gdbarch
*gdbarch
,
759 int (*signal_frame_p
) (struct gdbarch
*,
760 struct frame_info
*))
762 struct dwarf2_frame_ops
*ops
= gdbarch_data (gdbarch
, dwarf2_frame_data
);
764 ops
->signal_frame_p
= signal_frame_p
;
767 /* Query the architecture-specific signal frame recognizer for
771 dwarf2_frame_signal_frame_p (struct gdbarch
*gdbarch
,
772 struct frame_info
*this_frame
)
774 struct dwarf2_frame_ops
*ops
= gdbarch_data (gdbarch
, dwarf2_frame_data
);
776 if (ops
->signal_frame_p
== NULL
)
778 return ops
->signal_frame_p (gdbarch
, this_frame
);
781 /* Set the architecture-specific adjustment of .eh_frame and .debug_frame
785 dwarf2_frame_set_adjust_regnum (struct gdbarch
*gdbarch
,
786 int (*adjust_regnum
) (struct gdbarch
*,
789 struct dwarf2_frame_ops
*ops
= gdbarch_data (gdbarch
, dwarf2_frame_data
);
791 ops
->adjust_regnum
= adjust_regnum
;
794 /* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame
798 dwarf2_frame_adjust_regnum (struct gdbarch
*gdbarch
, int regnum
, int eh_frame_p
)
800 struct dwarf2_frame_ops
*ops
= gdbarch_data (gdbarch
, dwarf2_frame_data
);
802 if (ops
->adjust_regnum
== NULL
)
804 return ops
->adjust_regnum (gdbarch
, regnum
, eh_frame_p
);
808 dwarf2_frame_find_quirks (struct dwarf2_frame_state
*fs
,
809 struct dwarf2_fde
*fde
)
811 static const char *arm_idents
[] = {
812 "ARM C Compiler, ADS",
813 "Thumb C Compiler, ADS",
814 "ARM C++ Compiler, ADS",
815 "Thumb C++ Compiler, ADS",
816 "ARM/Thumb C/C++ Compiler, RVCT"
822 s
= find_pc_symtab (fs
->pc
);
823 if (s
== NULL
|| s
->producer
== NULL
)
826 for (i
= 0; i
< ARRAY_SIZE (arm_idents
); i
++)
827 if (strncmp (s
->producer
, arm_idents
[i
], strlen (arm_idents
[i
])) == 0)
829 if (fde
->cie
->version
== 1)
830 fs
->armcc_cfa_offsets_sf
= 1;
832 if (fde
->cie
->version
== 1)
833 fs
->armcc_cfa_offsets_reversed
= 1;
835 /* The reversed offset problem is present in some compilers
836 using DWARF3, but it was eventually fixed. Check the ARM
837 defined augmentations, which are in the format "armcc" followed
838 by a list of one-character options. The "+" option means
839 this problem is fixed (no quirk needed). If the armcc
840 augmentation is missing, the quirk is needed. */
841 if (fde
->cie
->version
== 3
842 && (strncmp (fde
->cie
->augmentation
, "armcc", 5) != 0
843 || strchr (fde
->cie
->augmentation
+ 5, '+') == NULL
))
844 fs
->armcc_cfa_offsets_reversed
= 1;
851 struct dwarf2_frame_cache
853 /* DWARF Call Frame Address. */
856 /* Set if the return address column was marked as undefined. */
857 int undefined_retaddr
;
859 /* Saved registers, indexed by GDB register number, not by DWARF
861 struct dwarf2_frame_state_reg
*reg
;
863 /* Return address register. */
864 struct dwarf2_frame_state_reg retaddr_reg
;
866 /* Target address size in bytes. */
870 static struct dwarf2_frame_cache
*
871 dwarf2_frame_cache (struct frame_info
*this_frame
, void **this_cache
)
873 struct cleanup
*old_chain
;
874 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
875 const int num_regs
= gdbarch_num_regs (gdbarch
)
876 + gdbarch_num_pseudo_regs (gdbarch
);
877 struct dwarf2_frame_cache
*cache
;
878 struct dwarf2_frame_state
*fs
;
879 struct dwarf2_fde
*fde
;
884 /* Allocate a new cache. */
885 cache
= FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache
);
886 cache
->reg
= FRAME_OBSTACK_CALLOC (num_regs
, struct dwarf2_frame_state_reg
);
888 /* Allocate and initialize the frame state. */
889 fs
= XMALLOC (struct dwarf2_frame_state
);
890 memset (fs
, 0, sizeof (struct dwarf2_frame_state
));
891 old_chain
= make_cleanup (dwarf2_frame_state_free
, fs
);
895 Note that if the next frame is never supposed to return (i.e. a call
896 to abort), the compiler might optimize away the instruction at
897 its return address. As a result the return address will
898 point at some random instruction, and the CFI for that
899 instruction is probably worthless to us. GCC's unwinder solves
900 this problem by substracting 1 from the return address to get an
901 address in the middle of a presumed call instruction (or the
902 instruction in the associated delay slot). This should only be
903 done for "normal" frames and not for resume-type frames (signal
904 handlers, sentinel frames, dummy frames). The function
905 get_frame_address_in_block does just this. It's not clear how
906 reliable the method is though; there is the potential for the
907 register state pre-call being different to that on return. */
908 fs
->pc
= get_frame_address_in_block (this_frame
);
910 /* Find the correct FDE. */
911 fde
= dwarf2_frame_find_fde (&fs
->pc
);
912 gdb_assert (fde
!= NULL
);
914 /* Extract any interesting information from the CIE. */
915 fs
->data_align
= fde
->cie
->data_alignment_factor
;
916 fs
->code_align
= fde
->cie
->code_alignment_factor
;
917 fs
->retaddr_column
= fde
->cie
->return_address_register
;
918 cache
->addr_size
= fde
->cie
->addr_size
;
920 /* Check for "quirks" - known bugs in producers. */
921 dwarf2_frame_find_quirks (fs
, fde
);
923 /* First decode all the insns in the CIE. */
924 execute_cfa_program (fde
, fde
->cie
->initial_instructions
,
925 fde
->cie
->end
, this_frame
, fs
);
927 /* Save the initialized register set. */
928 fs
->initial
= fs
->regs
;
929 fs
->initial
.reg
= dwarf2_frame_state_copy_regs (&fs
->regs
);
931 /* Then decode the insns in the FDE up to our target PC. */
932 execute_cfa_program (fde
, fde
->instructions
, fde
->end
, this_frame
, fs
);
934 /* Calculate the CFA. */
938 cache
->cfa
= read_reg (this_frame
, fs
->cfa_reg
);
939 if (fs
->armcc_cfa_offsets_reversed
)
940 cache
->cfa
-= fs
->cfa_offset
;
942 cache
->cfa
+= fs
->cfa_offset
;
947 execute_stack_op (fs
->cfa_exp
, fs
->cfa_exp_len
,
948 cache
->addr_size
, this_frame
, 0);
952 internal_error (__FILE__
, __LINE__
, _("Unknown CFA rule."));
955 /* Initialize the register state. */
959 for (regnum
= 0; regnum
< num_regs
; regnum
++)
960 dwarf2_frame_init_reg (gdbarch
, regnum
, &cache
->reg
[regnum
], this_frame
);
963 /* Go through the DWARF2 CFI generated table and save its register
964 location information in the cache. Note that we don't skip the
965 return address column; it's perfectly all right for it to
966 correspond to a real register. If it doesn't correspond to a
967 real register, or if we shouldn't treat it as such,
968 gdbarch_dwarf2_reg_to_regnum should be defined to return a number outside
969 the range [0, gdbarch_num_regs). */
971 int column
; /* CFI speak for "register number". */
973 for (column
= 0; column
< fs
->regs
.num_regs
; column
++)
975 /* Use the GDB register number as the destination index. */
976 int regnum
= gdbarch_dwarf2_reg_to_regnum (gdbarch
, column
);
978 /* If there's no corresponding GDB register, ignore it. */
979 if (regnum
< 0 || regnum
>= num_regs
)
982 /* NOTE: cagney/2003-09-05: CFI should specify the disposition
983 of all debug info registers. If it doesn't, complain (but
984 not too loudly). It turns out that GCC assumes that an
985 unspecified register implies "same value" when CFI (draft
986 7) specifies nothing at all. Such a register could equally
987 be interpreted as "undefined". Also note that this check
988 isn't sufficient; it only checks that all registers in the
989 range [0 .. max column] are specified, and won't detect
990 problems when a debug info register falls outside of the
991 table. We need a way of iterating through all the valid
992 DWARF2 register numbers. */
993 if (fs
->regs
.reg
[column
].how
== DWARF2_FRAME_REG_UNSPECIFIED
)
995 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_UNSPECIFIED
)
996 complaint (&symfile_complaints
, _("\
997 incomplete CFI data; unspecified registers (e.g., %s) at 0x%s"),
998 gdbarch_register_name (gdbarch
, regnum
),
1002 cache
->reg
[regnum
] = fs
->regs
.reg
[column
];
1006 /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information
1007 we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules. */
1011 for (regnum
= 0; regnum
< num_regs
; regnum
++)
1013 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA
1014 || cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA_OFFSET
)
1016 struct dwarf2_frame_state_reg
*retaddr_reg
=
1017 &fs
->regs
.reg
[fs
->retaddr_column
];
1019 /* It seems rather bizarre to specify an "empty" column as
1020 the return adress column. However, this is exactly
1021 what GCC does on some targets. It turns out that GCC
1022 assumes that the return address can be found in the
1023 register corresponding to the return address column.
1024 Incidentally, that's how we should treat a return
1025 address column specifying "same value" too. */
1026 if (fs
->retaddr_column
< fs
->regs
.num_regs
1027 && retaddr_reg
->how
!= DWARF2_FRAME_REG_UNSPECIFIED
1028 && retaddr_reg
->how
!= DWARF2_FRAME_REG_SAME_VALUE
)
1030 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA
)
1031 cache
->reg
[regnum
] = *retaddr_reg
;
1033 cache
->retaddr_reg
= *retaddr_reg
;
1037 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA
)
1039 cache
->reg
[regnum
].loc
.reg
= fs
->retaddr_column
;
1040 cache
->reg
[regnum
].how
= DWARF2_FRAME_REG_SAVED_REG
;
1044 cache
->retaddr_reg
.loc
.reg
= fs
->retaddr_column
;
1045 cache
->retaddr_reg
.how
= DWARF2_FRAME_REG_SAVED_REG
;
1052 if (fs
->retaddr_column
< fs
->regs
.num_regs
1053 && fs
->regs
.reg
[fs
->retaddr_column
].how
== DWARF2_FRAME_REG_UNDEFINED
)
1054 cache
->undefined_retaddr
= 1;
1056 do_cleanups (old_chain
);
1058 *this_cache
= cache
;
1063 dwarf2_frame_this_id (struct frame_info
*this_frame
, void **this_cache
,
1064 struct frame_id
*this_id
)
1066 struct dwarf2_frame_cache
*cache
=
1067 dwarf2_frame_cache (this_frame
, this_cache
);
1069 if (cache
->undefined_retaddr
)
1072 (*this_id
) = frame_id_build (cache
->cfa
, get_frame_func (this_frame
));
1075 static struct value
*
1076 dwarf2_frame_prev_register (struct frame_info
*this_frame
, void **this_cache
,
1079 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
1080 struct dwarf2_frame_cache
*cache
=
1081 dwarf2_frame_cache (this_frame
, this_cache
);
1085 switch (cache
->reg
[regnum
].how
)
1087 case DWARF2_FRAME_REG_UNDEFINED
:
1088 /* If CFI explicitly specified that the value isn't defined,
1089 mark it as optimized away; the value isn't available. */
1090 return frame_unwind_got_optimized (this_frame
, regnum
);
1092 case DWARF2_FRAME_REG_SAVED_OFFSET
:
1093 addr
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
1094 return frame_unwind_got_memory (this_frame
, regnum
, addr
);
1096 case DWARF2_FRAME_REG_SAVED_REG
:
1098 = gdbarch_dwarf2_reg_to_regnum (gdbarch
, cache
->reg
[regnum
].loc
.reg
);
1099 return frame_unwind_got_register (this_frame
, regnum
, realnum
);
1101 case DWARF2_FRAME_REG_SAVED_EXP
:
1102 addr
= execute_stack_op (cache
->reg
[regnum
].loc
.exp
,
1103 cache
->reg
[regnum
].exp_len
,
1104 cache
->addr_size
, this_frame
, cache
->cfa
);
1105 return frame_unwind_got_memory (this_frame
, regnum
, addr
);
1107 case DWARF2_FRAME_REG_SAVED_VAL_OFFSET
:
1108 addr
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
1109 return frame_unwind_got_constant (this_frame
, regnum
, addr
);
1111 case DWARF2_FRAME_REG_SAVED_VAL_EXP
:
1112 addr
= execute_stack_op (cache
->reg
[regnum
].loc
.exp
,
1113 cache
->reg
[regnum
].exp_len
,
1114 cache
->addr_size
, this_frame
, cache
->cfa
);
1115 return frame_unwind_got_constant (this_frame
, regnum
, addr
);
1117 case DWARF2_FRAME_REG_UNSPECIFIED
:
1118 /* GCC, in its infinite wisdom decided to not provide unwind
1119 information for registers that are "same value". Since
1120 DWARF2 (3 draft 7) doesn't define such behavior, said
1121 registers are actually undefined (which is different to CFI
1122 "undefined"). Code above issues a complaint about this.
1123 Here just fudge the books, assume GCC, and that the value is
1124 more inner on the stack. */
1125 return frame_unwind_got_register (this_frame
, regnum
, regnum
);
1127 case DWARF2_FRAME_REG_SAME_VALUE
:
1128 return frame_unwind_got_register (this_frame
, regnum
, regnum
);
1130 case DWARF2_FRAME_REG_CFA
:
1131 return frame_unwind_got_address (this_frame
, regnum
, cache
->cfa
);
1133 case DWARF2_FRAME_REG_CFA_OFFSET
:
1134 addr
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
1135 return frame_unwind_got_address (this_frame
, regnum
, addr
);
1137 case DWARF2_FRAME_REG_RA_OFFSET
:
1138 addr
= cache
->reg
[regnum
].loc
.offset
;
1139 regnum
= gdbarch_dwarf2_reg_to_regnum
1140 (gdbarch
, cache
->retaddr_reg
.loc
.reg
);
1141 addr
+= get_frame_register_unsigned (this_frame
, regnum
);
1142 return frame_unwind_got_address (this_frame
, regnum
, addr
);
1144 case DWARF2_FRAME_REG_FN
:
1145 return cache
->reg
[regnum
].loc
.fn (this_frame
, this_cache
, regnum
);
1148 internal_error (__FILE__
, __LINE__
, _("Unknown register rule."));
1153 dwarf2_frame_sniffer (const struct frame_unwind
*self
,
1154 struct frame_info
*this_frame
, void **this_cache
)
1156 /* Grab an address that is guarenteed to reside somewhere within the
1157 function. get_frame_pc(), with a no-return next function, can
1158 end up returning something past the end of this function's body.
1159 If the frame we're sniffing for is a signal frame whose start
1160 address is placed on the stack by the OS, its FDE must
1161 extend one byte before its start address or we could potentially
1162 select the FDE of the previous function. */
1163 CORE_ADDR block_addr
= get_frame_address_in_block (this_frame
);
1164 struct dwarf2_fde
*fde
= dwarf2_frame_find_fde (&block_addr
);
1168 /* On some targets, signal trampolines may have unwind information.
1169 We need to recognize them so that we set the frame type
1172 if (fde
->cie
->signal_frame
1173 || dwarf2_frame_signal_frame_p (get_frame_arch (this_frame
),
1175 return self
->type
== SIGTRAMP_FRAME
;
1177 return self
->type
!= SIGTRAMP_FRAME
;
1180 static const struct frame_unwind dwarf2_frame_unwind
=
1183 dwarf2_frame_this_id
,
1184 dwarf2_frame_prev_register
,
1186 dwarf2_frame_sniffer
1189 static const struct frame_unwind dwarf2_signal_frame_unwind
=
1192 dwarf2_frame_this_id
,
1193 dwarf2_frame_prev_register
,
1195 dwarf2_frame_sniffer
1198 /* Append the DWARF-2 frame unwinders to GDBARCH's list. */
1201 dwarf2_append_unwinders (struct gdbarch
*gdbarch
)
1203 frame_unwind_append_unwinder (gdbarch
, &dwarf2_frame_unwind
);
1204 frame_unwind_append_unwinder (gdbarch
, &dwarf2_signal_frame_unwind
);
1208 /* There is no explicitly defined relationship between the CFA and the
1209 location of frame's local variables and arguments/parameters.
1210 Therefore, frame base methods on this page should probably only be
1211 used as a last resort, just to avoid printing total garbage as a
1212 response to the "info frame" command. */
1215 dwarf2_frame_base_address (struct frame_info
*this_frame
, void **this_cache
)
1217 struct dwarf2_frame_cache
*cache
=
1218 dwarf2_frame_cache (this_frame
, this_cache
);
1223 static const struct frame_base dwarf2_frame_base
=
1225 &dwarf2_frame_unwind
,
1226 dwarf2_frame_base_address
,
1227 dwarf2_frame_base_address
,
1228 dwarf2_frame_base_address
1231 const struct frame_base
*
1232 dwarf2_frame_base_sniffer (struct frame_info
*this_frame
)
1234 CORE_ADDR block_addr
= get_frame_address_in_block (this_frame
);
1235 if (dwarf2_frame_find_fde (&block_addr
))
1236 return &dwarf2_frame_base
;
1241 const struct objfile_data
*dwarf2_frame_objfile_data
;
1244 read_1_byte (bfd
*abfd
, gdb_byte
*buf
)
1246 return bfd_get_8 (abfd
, buf
);
1250 read_4_bytes (bfd
*abfd
, gdb_byte
*buf
)
1252 return bfd_get_32 (abfd
, buf
);
1256 read_8_bytes (bfd
*abfd
, gdb_byte
*buf
)
1258 return bfd_get_64 (abfd
, buf
);
1262 read_unsigned_leb128 (bfd
*abfd
, gdb_byte
*buf
, unsigned int *bytes_read_ptr
)
1265 unsigned int num_read
;
1275 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
1278 result
|= ((byte
& 0x7f) << shift
);
1281 while (byte
& 0x80);
1283 *bytes_read_ptr
= num_read
;
1289 read_signed_leb128 (bfd
*abfd
, gdb_byte
*buf
, unsigned int *bytes_read_ptr
)
1293 unsigned int num_read
;
1302 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
1305 result
|= ((byte
& 0x7f) << shift
);
1308 while (byte
& 0x80);
1310 if (shift
< 8 * sizeof (result
) && (byte
& 0x40))
1311 result
|= -(((LONGEST
)1) << shift
);
1313 *bytes_read_ptr
= num_read
;
1319 read_initial_length (bfd
*abfd
, gdb_byte
*buf
, unsigned int *bytes_read_ptr
)
1323 result
= bfd_get_32 (abfd
, buf
);
1324 if (result
== 0xffffffff)
1326 result
= bfd_get_64 (abfd
, buf
+ 4);
1327 *bytes_read_ptr
= 12;
1330 *bytes_read_ptr
= 4;
1336 /* Pointer encoding helper functions. */
1338 /* GCC supports exception handling based on DWARF2 CFI. However, for
1339 technical reasons, it encodes addresses in its FDE's in a different
1340 way. Several "pointer encodings" are supported. The encoding
1341 that's used for a particular FDE is determined by the 'R'
1342 augmentation in the associated CIE. The argument of this
1343 augmentation is a single byte.
1345 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
1346 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
1347 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
1348 address should be interpreted (absolute, relative to the current
1349 position in the FDE, ...). Bit 7, indicates that the address
1350 should be dereferenced. */
1353 encoding_for_size (unsigned int size
)
1358 return DW_EH_PE_udata2
;
1360 return DW_EH_PE_udata4
;
1362 return DW_EH_PE_udata8
;
1364 internal_error (__FILE__
, __LINE__
, _("Unsupported address size"));
1369 read_encoded_value (struct comp_unit
*unit
, gdb_byte encoding
,
1370 int ptr_len
, gdb_byte
*buf
, unsigned int *bytes_read_ptr
,
1371 CORE_ADDR func_base
)
1376 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1378 if (encoding
& DW_EH_PE_indirect
)
1379 internal_error (__FILE__
, __LINE__
,
1380 _("Unsupported encoding: DW_EH_PE_indirect"));
1382 *bytes_read_ptr
= 0;
1384 switch (encoding
& 0x70)
1386 case DW_EH_PE_absptr
:
1389 case DW_EH_PE_pcrel
:
1390 base
= bfd_get_section_vma (unit
->abfd
, unit
->dwarf_frame_section
);
1391 base
+= (buf
- unit
->dwarf_frame_buffer
);
1393 case DW_EH_PE_datarel
:
1396 case DW_EH_PE_textrel
:
1399 case DW_EH_PE_funcrel
:
1402 case DW_EH_PE_aligned
:
1404 offset
= buf
- unit
->dwarf_frame_buffer
;
1405 if ((offset
% ptr_len
) != 0)
1407 *bytes_read_ptr
= ptr_len
- (offset
% ptr_len
);
1408 buf
+= *bytes_read_ptr
;
1412 internal_error (__FILE__
, __LINE__
, _("Invalid or unsupported encoding"));
1415 if ((encoding
& 0x07) == 0x00)
1417 encoding
|= encoding_for_size (ptr_len
);
1418 if (bfd_get_sign_extend_vma (unit
->abfd
))
1419 encoding
|= DW_EH_PE_signed
;
1422 switch (encoding
& 0x0f)
1424 case DW_EH_PE_uleb128
:
1427 gdb_byte
*end_buf
= buf
+ (sizeof (value
) + 1) * 8 / 7;
1428 *bytes_read_ptr
+= read_uleb128 (buf
, end_buf
, &value
) - buf
;
1429 return base
+ value
;
1431 case DW_EH_PE_udata2
:
1432 *bytes_read_ptr
+= 2;
1433 return (base
+ bfd_get_16 (unit
->abfd
, (bfd_byte
*) buf
));
1434 case DW_EH_PE_udata4
:
1435 *bytes_read_ptr
+= 4;
1436 return (base
+ bfd_get_32 (unit
->abfd
, (bfd_byte
*) buf
));
1437 case DW_EH_PE_udata8
:
1438 *bytes_read_ptr
+= 8;
1439 return (base
+ bfd_get_64 (unit
->abfd
, (bfd_byte
*) buf
));
1440 case DW_EH_PE_sleb128
:
1443 gdb_byte
*end_buf
= buf
+ (sizeof (value
) + 1) * 8 / 7;
1444 *bytes_read_ptr
+= read_sleb128 (buf
, end_buf
, &value
) - buf
;
1445 return base
+ value
;
1447 case DW_EH_PE_sdata2
:
1448 *bytes_read_ptr
+= 2;
1449 return (base
+ bfd_get_signed_16 (unit
->abfd
, (bfd_byte
*) buf
));
1450 case DW_EH_PE_sdata4
:
1451 *bytes_read_ptr
+= 4;
1452 return (base
+ bfd_get_signed_32 (unit
->abfd
, (bfd_byte
*) buf
));
1453 case DW_EH_PE_sdata8
:
1454 *bytes_read_ptr
+= 8;
1455 return (base
+ bfd_get_signed_64 (unit
->abfd
, (bfd_byte
*) buf
));
1457 internal_error (__FILE__
, __LINE__
, _("Invalid or unsupported encoding"));
1462 /* GCC uses a single CIE for all FDEs in a .debug_frame section.
1463 That's why we use a simple linked list here. */
1465 static struct dwarf2_cie
*
1466 find_cie (struct comp_unit
*unit
, ULONGEST cie_pointer
)
1468 struct dwarf2_cie
*cie
= unit
->cie
;
1472 if (cie
->cie_pointer
== cie_pointer
)
1482 add_cie (struct comp_unit
*unit
, struct dwarf2_cie
*cie
)
1484 cie
->next
= unit
->cie
;
1489 /* Find the FDE for *PC. Return a pointer to the FDE, and store the
1490 inital location associated with it into *PC. */
1492 static struct dwarf2_fde
*
1493 dwarf2_frame_find_fde (CORE_ADDR
*pc
)
1495 struct objfile
*objfile
;
1497 ALL_OBJFILES (objfile
)
1499 struct dwarf2_fde
*fde
;
1502 fde
= objfile_data (objfile
, dwarf2_frame_objfile_data
);
1506 gdb_assert (objfile
->section_offsets
);
1507 offset
= ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
1511 if (*pc
>= fde
->initial_location
+ offset
1512 && *pc
< fde
->initial_location
+ offset
+ fde
->address_range
)
1514 *pc
= fde
->initial_location
+ offset
;
1526 add_fde (struct comp_unit
*unit
, struct dwarf2_fde
*fde
)
1528 fde
->next
= objfile_data (unit
->objfile
, dwarf2_frame_objfile_data
);
1529 set_objfile_data (unit
->objfile
, dwarf2_frame_objfile_data
, fde
);
1532 #ifdef CC_HAS_LONG_LONG
1533 #define DW64_CIE_ID 0xffffffffffffffffULL
1535 #define DW64_CIE_ID ~0
1538 static gdb_byte
*decode_frame_entry (struct comp_unit
*unit
, gdb_byte
*start
,
1541 /* Decode the next CIE or FDE. Return NULL if invalid input, otherwise
1542 the next byte to be processed. */
1544 decode_frame_entry_1 (struct comp_unit
*unit
, gdb_byte
*start
, int eh_frame_p
)
1546 struct gdbarch
*gdbarch
= get_objfile_arch (unit
->objfile
);
1547 gdb_byte
*buf
, *end
;
1549 unsigned int bytes_read
;
1552 ULONGEST cie_pointer
;
1555 length
= read_initial_length (unit
->abfd
, buf
, &bytes_read
);
1559 /* Are we still within the section? */
1560 if (end
> unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
)
1566 /* Distinguish between 32 and 64-bit encoded frame info. */
1567 dwarf64_p
= (bytes_read
== 12);
1569 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
1573 cie_id
= DW64_CIE_ID
;
1579 cie_pointer
= read_8_bytes (unit
->abfd
, buf
);
1584 cie_pointer
= read_4_bytes (unit
->abfd
, buf
);
1588 if (cie_pointer
== cie_id
)
1590 /* This is a CIE. */
1591 struct dwarf2_cie
*cie
;
1593 unsigned int cie_version
;
1595 /* Record the offset into the .debug_frame section of this CIE. */
1596 cie_pointer
= start
- unit
->dwarf_frame_buffer
;
1598 /* Check whether we've already read it. */
1599 if (find_cie (unit
, cie_pointer
))
1602 cie
= (struct dwarf2_cie
*)
1603 obstack_alloc (&unit
->objfile
->objfile_obstack
,
1604 sizeof (struct dwarf2_cie
));
1605 cie
->initial_instructions
= NULL
;
1606 cie
->cie_pointer
= cie_pointer
;
1608 /* The encoding for FDE's in a normal .debug_frame section
1609 depends on the target address size. */
1610 cie
->encoding
= DW_EH_PE_absptr
;
1612 /* The target address size. For .eh_frame FDEs this is considered
1613 equal to the size of a target pointer. For .dwarf_frame FDEs,
1614 this is supposed to be the target address size from the associated
1615 CU header. FIXME: We do not have a good way to determine the
1616 latter. Always use the target pointer size for now. */
1617 cie
->addr_size
= gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
;
1619 /* We'll determine the final value later, but we need to
1620 initialize it conservatively. */
1621 cie
->signal_frame
= 0;
1623 /* Check version number. */
1624 cie_version
= read_1_byte (unit
->abfd
, buf
);
1625 if (cie_version
!= 1 && cie_version
!= 3)
1627 cie
->version
= cie_version
;
1630 /* Interpret the interesting bits of the augmentation. */
1631 cie
->augmentation
= augmentation
= (char *) buf
;
1632 buf
+= (strlen (augmentation
) + 1);
1634 /* Ignore armcc augmentations. We only use them for quirks,
1635 and that doesn't happen until later. */
1636 if (strncmp (augmentation
, "armcc", 5) == 0)
1637 augmentation
+= strlen (augmentation
);
1639 /* The GCC 2.x "eh" augmentation has a pointer immediately
1640 following the augmentation string, so it must be handled
1642 if (augmentation
[0] == 'e' && augmentation
[1] == 'h')
1645 buf
+= gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
;
1649 cie
->code_alignment_factor
=
1650 read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1653 cie
->data_alignment_factor
=
1654 read_signed_leb128 (unit
->abfd
, buf
, &bytes_read
);
1657 if (cie_version
== 1)
1659 cie
->return_address_register
= read_1_byte (unit
->abfd
, buf
);
1663 cie
->return_address_register
= read_unsigned_leb128 (unit
->abfd
, buf
,
1665 cie
->return_address_register
1666 = dwarf2_frame_adjust_regnum (gdbarch
,
1667 cie
->return_address_register
,
1672 cie
->saw_z_augmentation
= (*augmentation
== 'z');
1673 if (cie
->saw_z_augmentation
)
1677 length
= read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1681 cie
->initial_instructions
= buf
+ length
;
1685 while (*augmentation
)
1687 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1688 if (*augmentation
== 'L')
1695 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1696 else if (*augmentation
== 'R')
1698 cie
->encoding
= *buf
++;
1702 /* "P" indicates a personality routine in the CIE augmentation. */
1703 else if (*augmentation
== 'P')
1705 /* Skip. Avoid indirection since we throw away the result. */
1706 gdb_byte encoding
= (*buf
++) & ~DW_EH_PE_indirect
;
1707 read_encoded_value (unit
, encoding
, cie
->addr_size
,
1708 buf
, &bytes_read
, 0);
1713 /* "S" indicates a signal frame, such that the return
1714 address must not be decremented to locate the call frame
1715 info for the previous frame; it might even be the first
1716 instruction of a function, so decrementing it would take
1717 us to a different function. */
1718 else if (*augmentation
== 'S')
1720 cie
->signal_frame
= 1;
1724 /* Otherwise we have an unknown augmentation. Assume that either
1725 there is no augmentation data, or we saw a 'z' prefix. */
1728 if (cie
->initial_instructions
)
1729 buf
= cie
->initial_instructions
;
1734 cie
->initial_instructions
= buf
;
1737 add_cie (unit
, cie
);
1741 /* This is a FDE. */
1742 struct dwarf2_fde
*fde
;
1744 /* In an .eh_frame section, the CIE pointer is the delta between the
1745 address within the FDE where the CIE pointer is stored and the
1746 address of the CIE. Convert it to an offset into the .eh_frame
1750 cie_pointer
= buf
- unit
->dwarf_frame_buffer
- cie_pointer
;
1751 cie_pointer
-= (dwarf64_p
? 8 : 4);
1754 /* In either case, validate the result is still within the section. */
1755 if (cie_pointer
>= unit
->dwarf_frame_size
)
1758 fde
= (struct dwarf2_fde
*)
1759 obstack_alloc (&unit
->objfile
->objfile_obstack
,
1760 sizeof (struct dwarf2_fde
));
1761 fde
->cie
= find_cie (unit
, cie_pointer
);
1762 if (fde
->cie
== NULL
)
1764 decode_frame_entry (unit
, unit
->dwarf_frame_buffer
+ cie_pointer
,
1766 fde
->cie
= find_cie (unit
, cie_pointer
);
1769 gdb_assert (fde
->cie
!= NULL
);
1771 fde
->initial_location
=
1772 read_encoded_value (unit
, fde
->cie
->encoding
, fde
->cie
->addr_size
,
1773 buf
, &bytes_read
, 0);
1776 fde
->address_range
=
1777 read_encoded_value (unit
, fde
->cie
->encoding
& 0x0f,
1778 fde
->cie
->addr_size
, buf
, &bytes_read
, 0);
1781 /* A 'z' augmentation in the CIE implies the presence of an
1782 augmentation field in the FDE as well. The only thing known
1783 to be in here at present is the LSDA entry for EH. So we
1784 can skip the whole thing. */
1785 if (fde
->cie
->saw_z_augmentation
)
1789 length
= read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1790 buf
+= bytes_read
+ length
;
1795 fde
->instructions
= buf
;
1798 fde
->eh_frame_p
= eh_frame_p
;
1800 add_fde (unit
, fde
);
1806 /* Read a CIE or FDE in BUF and decode it. */
1808 decode_frame_entry (struct comp_unit
*unit
, gdb_byte
*start
, int eh_frame_p
)
1810 enum { NONE
, ALIGN4
, ALIGN8
, FAIL
} workaround
= NONE
;
1813 ptrdiff_t start_offset
;
1817 ret
= decode_frame_entry_1 (unit
, start
, eh_frame_p
);
1821 /* We have corrupt input data of some form. */
1823 /* ??? Try, weakly, to work around compiler/assembler/linker bugs
1824 and mismatches wrt padding and alignment of debug sections. */
1825 /* Note that there is no requirement in the standard for any
1826 alignment at all in the frame unwind sections. Testing for
1827 alignment before trying to interpret data would be incorrect.
1829 However, GCC traditionally arranged for frame sections to be
1830 sized such that the FDE length and CIE fields happen to be
1831 aligned (in theory, for performance). This, unfortunately,
1832 was done with .align directives, which had the side effect of
1833 forcing the section to be aligned by the linker.
1835 This becomes a problem when you have some other producer that
1836 creates frame sections that are not as strictly aligned. That
1837 produces a hole in the frame info that gets filled by the
1840 The GCC behaviour is arguably a bug, but it's effectively now
1841 part of the ABI, so we're now stuck with it, at least at the
1842 object file level. A smart linker may decide, in the process
1843 of compressing duplicate CIE information, that it can rewrite
1844 the entire output section without this extra padding. */
1846 start_offset
= start
- unit
->dwarf_frame_buffer
;
1847 if (workaround
< ALIGN4
&& (start_offset
& 3) != 0)
1849 start
+= 4 - (start_offset
& 3);
1850 workaround
= ALIGN4
;
1853 if (workaround
< ALIGN8
&& (start_offset
& 7) != 0)
1855 start
+= 8 - (start_offset
& 7);
1856 workaround
= ALIGN8
;
1860 /* Nothing left to try. Arrange to return as if we've consumed
1861 the entire input section. Hopefully we'll get valid info from
1862 the other of .debug_frame/.eh_frame. */
1864 ret
= unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
;
1874 complaint (&symfile_complaints
,
1875 _("Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
1876 unit
->dwarf_frame_section
->owner
->filename
,
1877 unit
->dwarf_frame_section
->name
);
1881 complaint (&symfile_complaints
,
1882 _("Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
1883 unit
->dwarf_frame_section
->owner
->filename
,
1884 unit
->dwarf_frame_section
->name
);
1888 complaint (&symfile_complaints
,
1889 _("Corrupt data in %s:%s"),
1890 unit
->dwarf_frame_section
->owner
->filename
,
1891 unit
->dwarf_frame_section
->name
);
1899 /* FIXME: kettenis/20030504: This still needs to be integrated with
1900 dwarf2read.c in a better way. */
1902 /* Imported from dwarf2read.c. */
1903 extern asection
*dwarf_frame_section
;
1904 extern asection
*dwarf_eh_frame_section
;
1906 /* Imported from dwarf2read.c. */
1907 extern gdb_byte
*dwarf2_read_section (struct objfile
*objfile
, asection
*sectp
);
1910 dwarf2_build_frame_info (struct objfile
*objfile
)
1912 struct comp_unit
*unit
;
1913 gdb_byte
*frame_ptr
;
1915 /* Build a minimal decoding of the DWARF2 compilation unit. */
1916 unit
= (struct comp_unit
*) obstack_alloc (&objfile
->objfile_obstack
,
1917 sizeof (struct comp_unit
));
1918 unit
->abfd
= objfile
->obfd
;
1919 unit
->objfile
= objfile
;
1923 /* First add the information from the .eh_frame section. That way,
1924 the FDEs from that section are searched last. */
1925 if (dwarf_eh_frame_section
)
1927 asection
*got
, *txt
;
1930 unit
->dwarf_frame_buffer
= dwarf2_read_section (objfile
,
1931 dwarf_eh_frame_section
);
1933 unit
->dwarf_frame_size
= bfd_get_section_size (dwarf_eh_frame_section
);
1934 unit
->dwarf_frame_section
= dwarf_eh_frame_section
;
1936 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
1937 that is used for the i386/amd64 target, which currently is
1938 the only target in GCC that supports/uses the
1939 DW_EH_PE_datarel encoding. */
1940 got
= bfd_get_section_by_name (unit
->abfd
, ".got");
1942 unit
->dbase
= got
->vma
;
1944 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
1946 txt
= bfd_get_section_by_name (unit
->abfd
, ".text");
1948 unit
->tbase
= txt
->vma
;
1950 frame_ptr
= unit
->dwarf_frame_buffer
;
1951 while (frame_ptr
< unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
)
1952 frame_ptr
= decode_frame_entry (unit
, frame_ptr
, 1);
1955 if (dwarf_frame_section
)
1958 unit
->dwarf_frame_buffer
= dwarf2_read_section (objfile
,
1959 dwarf_frame_section
);
1960 unit
->dwarf_frame_size
= bfd_get_section_size (dwarf_frame_section
);
1961 unit
->dwarf_frame_section
= dwarf_frame_section
;
1963 frame_ptr
= unit
->dwarf_frame_buffer
;
1964 while (frame_ptr
< unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
)
1965 frame_ptr
= decode_frame_entry (unit
, frame_ptr
, 0);
1969 /* Provide a prototype to silence -Wmissing-prototypes. */
1970 void _initialize_dwarf2_frame (void);
1973 _initialize_dwarf2_frame (void)
1975 dwarf2_frame_data
= gdbarch_data_register_pre_init (dwarf2_frame_init
);
1976 dwarf2_frame_objfile_data
= register_objfile_data ();