1 /* Frame unwinder for frames with DWARF Call Frame Information.
3 Copyright 2003 Free Software Foundation, Inc.
5 Contributed by Mark Kettenis.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
25 #include "dwarf2expr.h"
26 #include "elf/dwarf2.h"
28 #include "frame-base.h"
29 #include "frame-unwind.h"
36 #include "gdb_assert.h"
37 #include "gdb_string.h"
39 #include "dwarf2-frame.h"
41 /* Call Frame Information (CFI). */
43 /* Common Information Entry (CIE). */
47 /* Offset into the .debug_frame section where this CIE was found.
48 Used to identify this CIE. */
51 /* Constant that is factored out of all advance location
53 ULONGEST code_alignment_factor
;
55 /* Constants that is factored out of all offset instructions. */
56 LONGEST data_alignment_factor
;
58 /* Return address column. */
59 ULONGEST return_address_register
;
61 /* Instruction sequence to initialize a register set. */
62 unsigned char *initial_instructions
;
65 /* Encoding of addresses. */
66 unsigned char encoding
;
68 struct dwarf2_cie
*next
;
71 /* Frame Description Entry (FDE). */
75 /* CIE for this FDE. */
76 struct dwarf2_cie
*cie
;
78 /* First location associated with this FDE. */
79 CORE_ADDR initial_location
;
81 /* Number of bytes of program instructions described by this FDE. */
82 CORE_ADDR address_range
;
84 /* Instruction sequence. */
85 unsigned char *instructions
;
88 struct dwarf2_fde
*next
;
91 static struct dwarf2_fde
*dwarf2_frame_find_fde (CORE_ADDR
*pc
);
94 /* Structure describing a frame state. */
96 struct dwarf2_frame_state
98 /* Each register save state can be described in terms of a CFA slot,
99 another register, or a location expression. */
100 struct dwarf2_frame_state_reg_info
102 struct dwarf2_frame_state_reg
120 /* Used to implement DW_CFA_remember_state. */
121 struct dwarf2_frame_state_reg_info
*prev
;
126 unsigned char *cfa_exp
;
133 /* The PC described by the current frame state. */
136 /* Initial register set from the CIE.
137 Used to implement DW_CFA_restore. */
138 struct dwarf2_frame_state_reg_info initial
;
140 /* The information we care about from the CIE. */
143 ULONGEST retaddr_column
;
146 /* Store the length the expression for the CFA in the `cfa_reg' field,
147 which is unused in that case. */
148 #define cfa_exp_len cfa_reg
150 /* Assert that the register set RS is large enough to store NUM_REGS
151 columns. If necessary, enlarge the register set. */
154 dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info
*rs
,
157 size_t size
= sizeof (struct dwarf2_frame_state_reg
);
159 if (num_regs
<= rs
->num_regs
)
162 rs
->reg
= (struct dwarf2_frame_state_reg
*)
163 xrealloc (rs
->reg
, num_regs
* size
);
165 /* Initialize newly allocated registers. */
166 memset (rs
->reg
+ rs
->num_regs
* size
, 0, (num_regs
- rs
->num_regs
) * size
);
167 rs
->num_regs
= num_regs
;
170 /* Copy the register columns in register set RS into newly allocated
171 memory and return a pointer to this newly created copy. */
173 static struct dwarf2_frame_state_reg
*
174 dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info
*rs
)
176 size_t size
= rs
->num_regs
* sizeof (struct dwarf2_frame_state_reg_info
);
177 struct dwarf2_frame_state_reg
*reg
;
179 reg
= (struct dwarf2_frame_state_reg
*) xmalloc (size
);
180 memcpy (reg
, rs
->reg
, size
);
185 /* Release the memory allocated to register set RS. */
188 dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info
*rs
)
192 dwarf2_frame_state_free_regs (rs
->prev
);
199 /* Release the memory allocated to the frame state FS. */
202 dwarf2_frame_state_free (void *p
)
204 struct dwarf2_frame_state
*fs
= p
;
206 dwarf2_frame_state_free_regs (fs
->initial
.prev
);
207 dwarf2_frame_state_free_regs (fs
->regs
.prev
);
208 xfree (fs
->initial
.reg
);
209 xfree (fs
->regs
.reg
);
214 /* Helper functions for execute_stack_op. */
217 read_reg (void *baton
, int reg
)
219 struct frame_info
*next_frame
= (struct frame_info
*) baton
;
223 regnum
= DWARF2_REG_TO_REGNUM (reg
);
225 buf
= (char *) alloca (register_size (current_gdbarch
, regnum
));
226 frame_unwind_register (next_frame
, regnum
, buf
);
227 return extract_typed_address (buf
, builtin_type_void_data_ptr
);
231 read_mem (void *baton
, char *buf
, CORE_ADDR addr
, size_t len
)
233 read_memory (addr
, buf
, len
);
237 no_get_frame_base (void *baton
, unsigned char **start
, size_t *length
)
239 internal_error (__FILE__
, __LINE__
,
240 "Support for DW_OP_fbreg is unimplemented");
244 no_get_tls_address (void *baton
, CORE_ADDR offset
)
246 internal_error (__FILE__
, __LINE__
,
247 "Support for DW_OP_GNU_push_tls_address is unimplemented");
251 execute_stack_op (unsigned char *exp
, ULONGEST len
,
252 struct frame_info
*next_frame
, CORE_ADDR initial
)
254 struct dwarf_expr_context
*ctx
;
257 ctx
= new_dwarf_expr_context ();
258 ctx
->baton
= next_frame
;
259 ctx
->read_reg
= read_reg
;
260 ctx
->read_mem
= read_mem
;
261 ctx
->get_frame_base
= no_get_frame_base
;
262 ctx
->get_tls_address
= no_get_tls_address
;
264 dwarf_expr_push (ctx
, initial
);
265 dwarf_expr_eval (ctx
, exp
, len
);
266 result
= dwarf_expr_fetch (ctx
, 0);
269 result
= read_reg (next_frame
, result
);
271 free_dwarf_expr_context (ctx
);
278 execute_cfa_program (unsigned char *insn_ptr
, unsigned char *insn_end
,
279 struct frame_info
*next_frame
,
280 struct dwarf2_frame_state
*fs
)
282 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
285 while (insn_ptr
< insn_end
&& fs
->pc
<= pc
)
287 unsigned char insn
= *insn_ptr
++;
291 if ((insn
& 0xc0) == DW_CFA_advance_loc
)
292 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
293 else if ((insn
& 0xc0) == DW_CFA_offset
)
296 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
297 offset
= utmp
* fs
->data_align
;
298 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
299 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
300 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
302 else if ((insn
& 0xc0) == DW_CFA_restore
)
304 gdb_assert (fs
->initial
.reg
);
306 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
307 fs
->regs
.reg
[reg
] = fs
->initial
.reg
[reg
];
314 fs
->pc
= dwarf2_read_address (insn_ptr
, insn_end
, &bytes_read
);
315 insn_ptr
+= bytes_read
;
318 case DW_CFA_advance_loc1
:
319 utmp
= extract_unsigned_integer (insn_ptr
, 1);
320 fs
->pc
+= utmp
* fs
->code_align
;
323 case DW_CFA_advance_loc2
:
324 utmp
= extract_unsigned_integer (insn_ptr
, 2);
325 fs
->pc
+= utmp
* fs
->code_align
;
328 case DW_CFA_advance_loc4
:
329 utmp
= extract_unsigned_integer (insn_ptr
, 4);
330 fs
->pc
+= utmp
* fs
->code_align
;
334 case DW_CFA_offset_extended
:
335 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
336 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
337 offset
= utmp
* fs
->data_align
;
338 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
339 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
340 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
343 case DW_CFA_restore_extended
:
344 gdb_assert (fs
->initial
.reg
);
345 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
346 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
347 fs
->regs
.reg
[reg
] = fs
->initial
.reg
[reg
];
350 case DW_CFA_undefined
:
351 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
352 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
353 fs
->regs
.reg
[reg
].how
= REG_UNSAVED
;
356 case DW_CFA_same_value
:
357 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
358 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
359 fs
->regs
.reg
[reg
].how
= REG_UNMODIFIED
;
362 case DW_CFA_register
:
363 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
364 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
365 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
366 fs
->regs
.reg
[reg
].loc
.reg
= utmp
;
369 case DW_CFA_remember_state
:
371 struct dwarf2_frame_state_reg_info
*new_rs
;
373 new_rs
= XMALLOC (struct dwarf2_frame_state_reg_info
);
375 fs
->regs
.reg
= dwarf2_frame_state_copy_regs (&fs
->regs
);
376 fs
->regs
.prev
= new_rs
;
380 case DW_CFA_restore_state
:
382 struct dwarf2_frame_state_reg_info
*old_rs
= fs
->regs
.prev
;
386 xfree (fs
->regs
.reg
);
393 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_reg
);
394 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
395 fs
->cfa_offset
= utmp
;
396 fs
->cfa_how
= CFA_REG_OFFSET
;
399 case DW_CFA_def_cfa_register
:
400 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_reg
);
401 fs
->cfa_how
= CFA_REG_OFFSET
;
404 case DW_CFA_def_cfa_offset
:
405 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_offset
);
406 /* cfa_how deliberately not set. */
409 case DW_CFA_def_cfa_expression
:
410 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_exp_len
);
411 fs
->cfa_exp
= insn_ptr
;
412 fs
->cfa_how
= CFA_EXP
;
413 insn_ptr
+= fs
->cfa_exp_len
;
416 case DW_CFA_expression
:
417 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
418 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
419 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
420 fs
->regs
.reg
[reg
].loc
.exp
= insn_ptr
;
421 fs
->regs
.reg
[reg
].exp_len
= utmp
;
422 fs
->regs
.reg
[reg
].how
= REG_SAVED_EXP
;
429 case DW_CFA_GNU_args_size
:
431 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
435 internal_error (__FILE__
, __LINE__
, "Unknown CFI encountered.");
440 /* Don't allow remember/restore between CIE and FDE programs. */
441 dwarf2_frame_state_free_regs (fs
->regs
.prev
);
442 fs
->regs
.prev
= NULL
;
445 struct dwarf2_frame_cache
447 /* DWARF Call Frame Address. */
450 /* Saved registers, indexed by GDB register number, not by DWARF
452 struct dwarf2_frame_state_reg
*reg
;
455 struct dwarf2_frame_cache
*
456 dwarf2_frame_cache (struct frame_info
*next_frame
, void **this_cache
)
458 struct cleanup
*old_chain
;
459 int num_regs
= NUM_REGS
+ NUM_PSEUDO_REGS
;
460 struct dwarf2_frame_cache
*cache
;
461 struct dwarf2_frame_state
*fs
;
462 struct dwarf2_fde
*fde
;
468 /* Allocate a new cache. */
469 cache
= FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache
);
470 cache
->reg
= FRAME_OBSTACK_CALLOC (num_regs
, struct dwarf2_frame_state_reg
);
472 /* Allocate and initialize the frame state. */
473 fs
= XMALLOC (struct dwarf2_frame_state
);
474 memset (fs
, 0, sizeof (struct dwarf2_frame_state
));
475 old_chain
= make_cleanup (dwarf2_frame_state_free
, fs
);
479 Note that if NEXT_FRAME is never supposed to return (i.e. a call
480 to abort), the compiler might optimize away the instruction at
481 NEXT_FRAME's return address. As a result the return address will
482 point at some random instruction, and the CFI for that
483 instruction is probably wortless to us. GCC's unwinder solves
484 this problem by substracting 1 from the return address to get an
485 address in the middle of a presumed call instruction (or the
486 instruction in the associated delay slot). This should only be
487 done for "normal" frames and not for resume-type frames (signal
488 handlers, sentinel frames, dummy frames).
490 We don't do what GCC's does here (yet). It's not clear how
491 reliable the method is. There's also a problem with finding the
492 right FDE; see the comment in dwarf_frame_p. If dwarf_frame_p
493 selected this frame unwinder because it found the FDE for the
494 next function, using the adjusted return address might not yield
495 a FDE at all. The problem isn't specific to DWARF CFI; other
496 unwinders loose in similar ways. Therefore it's probably
497 acceptable to leave things slightly broken for now. */
498 fs
->pc
= frame_pc_unwind (next_frame
);
500 /* Find the correct FDE. */
501 fde
= dwarf2_frame_find_fde (&fs
->pc
);
502 gdb_assert (fde
!= NULL
);
504 /* Extract any interesting information from the CIE. */
505 fs
->data_align
= fde
->cie
->data_alignment_factor
;
506 fs
->code_align
= fde
->cie
->code_alignment_factor
;
507 fs
->retaddr_column
= fde
->cie
->return_address_register
;
509 /* First decode all the insns in the CIE. */
510 execute_cfa_program (fde
->cie
->initial_instructions
,
511 fde
->cie
->end
, next_frame
, fs
);
513 /* Save the initialized register set. */
514 fs
->initial
= fs
->regs
;
515 fs
->initial
.reg
= dwarf2_frame_state_copy_regs (&fs
->regs
);
517 /* Then decode the insns in the FDE up to our target PC. */
518 execute_cfa_program (fde
->instructions
, fde
->end
, next_frame
, fs
);
520 /* Caclulate the CFA. */
524 cache
->cfa
= read_reg (next_frame
, fs
->cfa_reg
);
525 cache
->cfa
+= fs
->cfa_offset
;
530 execute_stack_op (fs
->cfa_exp
, fs
->cfa_exp_len
, next_frame
, 0);
534 internal_error (__FILE__
, __LINE__
, "Unknown CFA rule.");
537 /* Save the register info in the cache. */
538 for (reg
= 0; reg
< fs
->regs
.num_regs
; reg
++)
542 /* Skip the return address column. */
543 if (reg
== fs
->retaddr_column
)
546 /* Use the GDB register number as index. */
547 regnum
= DWARF2_REG_TO_REGNUM (reg
);
549 if (regnum
>= 0 && regnum
< num_regs
)
550 cache
->reg
[regnum
] = fs
->regs
.reg
[reg
];
553 /* Stored the location of the return addess. */
554 if (fs
->retaddr_column
< fs
->regs
.num_regs
)
555 cache
->reg
[PC_REGNUM
] = fs
->regs
.reg
[fs
->retaddr_column
];
557 do_cleanups (old_chain
);
564 dwarf2_frame_this_id (struct frame_info
*next_frame
, void **this_cache
,
565 struct frame_id
*this_id
)
567 struct dwarf2_frame_cache
*cache
=
568 dwarf2_frame_cache (next_frame
, this_cache
);
570 (*this_id
) = frame_id_build (cache
->cfa
, frame_func_unwind (next_frame
));
574 dwarf2_frame_prev_register (struct frame_info
*next_frame
, void **this_cache
,
575 int regnum
, int *optimizedp
,
576 enum lval_type
*lvalp
, CORE_ADDR
*addrp
,
577 int *realnump
, void *valuep
)
579 struct dwarf2_frame_cache
*cache
=
580 dwarf2_frame_cache (next_frame
, this_cache
);
582 switch (cache
->reg
[regnum
].how
)
589 if (regnum
== SP_REGNUM
)
591 /* GCC defines the CFA as the value of the stack pointer
592 just before the call instruction is executed. Do other
593 compilers use the same definition? */
597 /* Store the value. */
598 store_typed_address (valuep
, builtin_type_void_data_ptr
,
604 /* In some cases, for example %eflags on the i386, we have
605 to provide a sane value, even though this register wasn't
606 saved. Assume we can get it from NEXT_FRAME. */
607 frame_unwind_register (next_frame
, regnum
, valuep
);
611 case REG_SAVED_OFFSET
:
613 *lvalp
= lval_memory
;
614 *addrp
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
618 /* Read the value in from memory. */
619 read_memory (*addrp
, valuep
,
620 register_size (current_gdbarch
, regnum
));
625 regnum
= DWARF2_REG_TO_REGNUM (cache
->reg
[regnum
].loc
.reg
);
626 frame_register_unwind (next_frame
, regnum
,
627 optimizedp
, lvalp
, addrp
, realnump
, valuep
);
632 *lvalp
= lval_memory
;
633 *addrp
= execute_stack_op (cache
->reg
[regnum
].loc
.exp
,
634 cache
->reg
[regnum
].exp_len
,
635 next_frame
, cache
->cfa
);
639 /* Read the value in from memory. */
640 read_memory (*addrp
, valuep
,
641 register_size (current_gdbarch
, regnum
));
646 frame_register_unwind (next_frame
, regnum
,
647 optimizedp
, lvalp
, addrp
, realnump
, valuep
);
651 internal_error (__FILE__
, __LINE__
, "Unknown register rule.");
655 static const struct frame_unwind dwarf2_frame_unwind
=
658 dwarf2_frame_this_id
,
659 dwarf2_frame_prev_register
662 const struct frame_unwind
*
663 dwarf2_frame_p (CORE_ADDR pc
)
665 /* The way GDB works, this function can be called with PC just after
666 the last instruction of the function we're supposed to return the
667 unwind methods for. In that case we won't find the correct FDE;
668 instead we find the FDE for the next function, or we won't find
669 an FDE at all. There is a possible solution (see the comment in
670 dwarf2_frame_cache), GDB doesn't pass us enough information to
672 if (dwarf2_frame_find_fde (&pc
))
673 return &dwarf2_frame_unwind
;
679 /* There is no explicitly defined relationship between the CFA and the
680 location of frame's local variables and arguments/parameters.
681 Therefore, frame base methods on this page should probably only be
682 used as a last resort, just to avoid printing total garbage as a
683 response to the "info frame" command. */
686 dwarf2_frame_base_address (struct frame_info
*next_frame
, void **this_cache
)
688 struct dwarf2_frame_cache
*cache
=
689 dwarf2_frame_cache (next_frame
, this_cache
);
694 static const struct frame_base dwarf2_frame_base
=
696 &dwarf2_frame_unwind
,
697 dwarf2_frame_base_address
,
698 dwarf2_frame_base_address
,
699 dwarf2_frame_base_address
702 const struct frame_base
*
703 dwarf2_frame_base_p (CORE_ADDR pc
)
705 if (dwarf2_frame_find_fde (&pc
))
706 return &dwarf2_frame_base
;
711 /* A minimal decoding of DWARF2 compilation units. We only decode
712 what's needed to get to the call frame information. */
716 /* Keep the bfd convenient. */
719 struct objfile
*objfile
;
721 /* Linked list of CIEs for this object. */
722 struct dwarf2_cie
*cie
;
724 /* Address size for this unit - from unit header. */
725 unsigned char addr_size
;
727 /* Pointer to the .debug_frame section loaded into memory. */
728 char *dwarf_frame_buffer
;
730 /* Length of the loaded .debug_frame section. */
731 unsigned long dwarf_frame_size
;
733 /* Pointer to the .debug_frame section. */
734 asection
*dwarf_frame_section
;
738 read_1_byte (bfd
*bfd
, char *buf
)
740 return bfd_get_8 (abfd
, (bfd_byte
*) buf
);
744 read_4_bytes (bfd
*abfd
, char *buf
)
746 return bfd_get_32 (abfd
, (bfd_byte
*) buf
);
750 read_8_bytes (bfd
*abfd
, char *buf
)
752 return bfd_get_64 (abfd
, (bfd_byte
*) buf
);
756 read_unsigned_leb128 (bfd
*abfd
, char *buf
, unsigned int *bytes_read_ptr
)
759 unsigned int num_read
;
769 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
772 result
|= ((byte
& 0x7f) << shift
);
777 *bytes_read_ptr
= num_read
;
783 read_signed_leb128 (bfd
*abfd
, char *buf
, unsigned int *bytes_read_ptr
)
787 unsigned int num_read
;
796 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
799 result
|= ((byte
& 0x7f) << shift
);
804 if ((shift
< 32) && (byte
& 0x40))
805 result
|= -(1 << shift
);
807 *bytes_read_ptr
= num_read
;
813 read_initial_length (bfd
*abfd
, char *buf
, unsigned int *bytes_read_ptr
)
817 result
= bfd_get_32 (abfd
, (bfd_byte
*) buf
);
818 if (result
== 0xffffffff)
820 result
= bfd_get_64 (abfd
, (bfd_byte
*) buf
+ 4);
821 *bytes_read_ptr
= 12;
830 /* Pointer encoding helper functions. */
832 /* GCC supports exception handling based on DWARF2 CFI. However, for
833 technical reasons, it encodes addresses in its FDE's in a different
834 way. Several "pointer encodings" are supported. The encoding
835 that's used for a particular FDE is determined by the 'R'
836 augmentation in the associated CIE. The argument of this
837 augmentation is a single byte.
839 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
840 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
841 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
842 address should be interpreted (absolute, relative to the current
843 position in the FDE, ...). Bit 7, indicates that the address
844 should be dereferenced. */
847 encoding_for_size (unsigned int size
)
852 return DW_EH_PE_udata2
;
854 return DW_EH_PE_udata4
;
856 return DW_EH_PE_udata8
;
858 internal_error (__FILE__
, __LINE__
, "Unsupported address size");
863 size_of_encoded_value (unsigned char encoding
)
865 if (encoding
== DW_EH_PE_omit
)
868 switch (encoding
& 0x07)
870 case DW_EH_PE_absptr
:
871 return TYPE_LENGTH (builtin_type_void_data_ptr
);
872 case DW_EH_PE_udata2
:
874 case DW_EH_PE_udata4
:
876 case DW_EH_PE_udata8
:
879 internal_error (__FILE__
, __LINE__
, "Invalid or unsupported encoding");
884 read_encoded_value (struct comp_unit
*unit
, unsigned char encoding
,
885 char *buf
, unsigned int *bytes_read_ptr
)
889 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
891 if (encoding
& DW_EH_PE_indirect
)
892 internal_error (__FILE__
, __LINE__
,
893 "Unsupported encoding: DW_EH_PE_indirect");
895 switch (encoding
& 0x70)
897 case DW_EH_PE_absptr
:
901 base
= bfd_get_section_vma (unit
->bfd
, unit
->dwarf_frame_section
);
902 base
+= (buf
- unit
->dwarf_frame_buffer
);
905 internal_error (__FILE__
, __LINE__
, "Invalid or unsupported encoding");
908 if ((encoding
& 0x0f) == 0x00)
909 encoding
|= encoding_for_size (TYPE_LENGTH(builtin_type_void_data_ptr
));
911 switch (encoding
& 0x0f)
913 case DW_EH_PE_udata2
:
915 return (base
+ bfd_get_16 (unit
->abfd
, (bfd_byte
*) buf
));
916 case DW_EH_PE_udata4
:
918 return (base
+ bfd_get_32 (unit
->abfd
, (bfd_byte
*) buf
));
919 case DW_EH_PE_udata8
:
921 return (base
+ bfd_get_64 (unit
->abfd
, (bfd_byte
*) buf
));
922 case DW_EH_PE_sdata2
:
924 return (base
+ bfd_get_signed_16 (unit
->abfd
, (bfd_byte
*) buf
));
925 case DW_EH_PE_sdata4
:
927 return (base
+ bfd_get_signed_32 (unit
->abfd
, (bfd_byte
*) buf
));
928 case DW_EH_PE_sdata8
:
930 return (base
+ bfd_get_signed_64 (unit
->abfd
, (bfd_byte
*) buf
));
932 internal_error (__FILE__
, __LINE__
, "Invalid or unsupported encoding");
937 /* GCC uses a single CIE for all FDEs in a .debug_frame section.
938 That's why we use a simple linked list here. */
940 static struct dwarf2_cie
*
941 find_cie (struct comp_unit
*unit
, ULONGEST cie_pointer
)
943 struct dwarf2_cie
*cie
= unit
->cie
;
947 if (cie
->cie_pointer
== cie_pointer
)
957 add_cie (struct comp_unit
*unit
, struct dwarf2_cie
*cie
)
959 cie
->next
= unit
->cie
;
963 /* Find the FDE for *PC. Return a pointer to the FDE, and store the
964 inital location associated with it into *PC. */
966 static struct dwarf2_fde
*
967 dwarf2_frame_find_fde (CORE_ADDR
*pc
)
969 struct objfile
*objfile
;
971 ALL_OBJFILES (objfile
)
973 struct dwarf2_fde
*fde
;
976 offset
= ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
978 fde
= objfile
->sym_private
;
981 if (*pc
>= fde
->initial_location
+ offset
982 && *pc
< fde
->initial_location
+ offset
+ fde
->address_range
)
984 *pc
= fde
->initial_location
+ offset
;
996 add_fde (struct comp_unit
*unit
, struct dwarf2_fde
*fde
)
998 fde
->next
= unit
->objfile
->sym_private
;
999 unit
->objfile
->sym_private
= fde
;
1002 #ifdef CC_HAS_LONG_LONG
1003 #define DW64_CIE_ID 0xffffffffffffffffULL
1005 #define DW64_CIE_ID ~0
1008 /* Read a CIE or FDE in BUF and decode it. */
1011 decode_frame_entry (struct comp_unit
*unit
, char *buf
, int eh_frame_p
)
1014 unsigned int bytes_read
;
1016 ULONGEST cie_id
= DW_CIE_ID
;
1017 ULONGEST cie_pointer
;
1021 length
= read_initial_length (unit
->abfd
, buf
, &bytes_read
);
1028 if (bytes_read
== 12)
1031 /* In a .eh_frame section, zero is used to distinguish CIEs from
1036 cie_id
= DW64_CIE_ID
;
1040 cie_pointer
= read_8_bytes (unit
->abfd
, buf
);
1045 cie_pointer
= read_4_bytes (unit
->abfd
, buf
);
1049 if (cie_pointer
== cie_id
)
1051 /* This is a CIE. */
1052 struct dwarf2_cie
*cie
;
1055 /* Record the offset into the .debug_frame section of this CIE. */
1056 cie_pointer
= start
- unit
->dwarf_frame_buffer
;
1058 /* Check whether we've already read it. */
1059 if (find_cie (unit
, cie_pointer
))
1062 cie
= (struct dwarf2_cie
*)
1063 obstack_alloc (&unit
->objfile
->psymbol_obstack
,
1064 sizeof (struct dwarf2_cie
));
1065 cie
->initial_instructions
= NULL
;
1066 cie
->cie_pointer
= cie_pointer
;
1068 /* The encoding for FDE's in a normal .debug_frame section
1069 depends on the target address size as specified in the
1070 Compilation Unit Header. */
1071 cie
->encoding
= encoding_for_size (unit
->addr_size
);
1073 /* Check version number. */
1074 gdb_assert (read_1_byte (unit
->abfd
, buf
) == DW_CIE_VERSION
);
1077 /* Interpret the interesting bits of the augmentation. */
1079 buf
= augmentation
+ strlen (augmentation
) + 1;
1081 /* The GCC 2.x "eh" augmentation has a pointer immediately
1082 following the augmentation string, so it must be handled
1084 if (augmentation
[0] == 'e' && augmentation
[1] == 'h')
1087 buf
+= TYPE_LENGTH (builtin_type_void_data_ptr
);
1091 cie
->code_alignment_factor
=
1092 read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1095 cie
->data_alignment_factor
=
1096 read_signed_leb128 (unit
->abfd
, buf
, &bytes_read
);
1099 cie
->return_address_register
= read_1_byte (unit
->abfd
, buf
);
1102 if (*augmentation
== 'z')
1106 length
= read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1108 cie
->initial_instructions
= buf
+ length
;
1112 while (*augmentation
)
1114 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1115 if (*augmentation
== 'L')
1122 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1123 else if (*augmentation
== 'R')
1125 cie
->encoding
= *buf
++;
1129 /* "P" indicates a personality routine in the CIE augmentation. */
1130 else if (*augmentation
== 'P')
1133 buf
+= size_of_encoded_value (*buf
++);
1137 /* Otherwise we have an unknown augmentation.
1138 Bail out unless we saw a 'z' prefix. */
1141 if (cie
->initial_instructions
== NULL
)
1144 /* Skip unknown augmentations. */
1145 buf
= cie
->initial_instructions
;
1150 cie
->initial_instructions
= buf
;
1153 add_cie (unit
, cie
);
1157 /* This is a FDE. */
1158 struct dwarf2_fde
*fde
;
1162 /* In an .eh_frame section, the CIE pointer is the delta
1163 between the address within the FDE where the CIE pointer
1164 is stored and the address of the CIE. Convert it to an
1165 offset into the .eh_frame section. */
1166 cie_pointer
= buf
- unit
->dwarf_frame_buffer
- cie_pointer
;
1167 cie_pointer
-= (dwarf64_p
? 8 : 4);
1170 fde
= (struct dwarf2_fde
*)
1171 obstack_alloc (&unit
->objfile
->psymbol_obstack
,
1172 sizeof (struct dwarf2_fde
));
1173 fde
->cie
= find_cie (unit
, cie_pointer
);
1174 if (fde
->cie
== NULL
)
1176 decode_frame_entry (unit
, unit
->dwarf_frame_buffer
+ cie_pointer
,
1178 fde
->cie
= find_cie (unit
, cie_pointer
);
1181 gdb_assert (fde
->cie
!= NULL
);
1183 fde
->initial_location
=
1184 read_encoded_value (unit
, fde
->cie
->encoding
, buf
, &bytes_read
);
1187 fde
->address_range
=
1188 read_encoded_value (unit
, fde
->cie
->encoding
& 0x0f, buf
, &bytes_read
);
1191 fde
->instructions
= buf
;
1194 add_fde (unit
, fde
);
1201 /* FIXME: kettenis/20030504: This still needs to be integrated with
1202 dwarf2read.c in a better way. */
1204 /* Imported from dwarf2read.c. */
1205 extern file_ptr dwarf_frame_offset
;
1206 extern unsigned int dwarf_frame_size
;
1207 extern asection
*dwarf_frame_section
;
1208 extern file_ptr dwarf_eh_frame_offset
;
1209 extern unsigned int dwarf_eh_frame_size
;
1210 extern asection
*dwarf_eh_frame_section
;
1212 /* Imported from dwarf2read.c. */
1213 extern char *dwarf2_read_section (struct objfile
*objfile
, file_ptr offset
,
1214 unsigned int size
, asection
*sectp
);
1217 dwarf2_build_frame_info (struct objfile
*objfile
)
1219 struct comp_unit unit
;
1222 /* Build a minimal decoding of the DWARF2 compilation unit. */
1223 unit
.abfd
= objfile
->obfd
;
1224 unit
.objfile
= objfile
;
1225 unit
.addr_size
= objfile
->obfd
->arch_info
->bits_per_address
/ 8;
1227 /* First add the information from the .eh_frame section. That way,
1228 the FDEs from that section are searched last. */
1229 if (dwarf_eh_frame_offset
)
1232 unit
.dwarf_frame_buffer
= dwarf2_read_section (objfile
,
1233 dwarf_eh_frame_offset
,
1234 dwarf_eh_frame_size
,
1235 dwarf_eh_frame_section
);
1237 unit
.dwarf_frame_size
= dwarf_eh_frame_size
;
1238 unit
.dwarf_frame_section
= dwarf_eh_frame_section
;
1240 frame_ptr
= unit
.dwarf_frame_buffer
;
1241 while (frame_ptr
< unit
.dwarf_frame_buffer
+ unit
.dwarf_frame_size
)
1242 frame_ptr
= decode_frame_entry (&unit
, frame_ptr
, 1);
1245 if (dwarf_frame_offset
)
1248 unit
.dwarf_frame_buffer
= dwarf2_read_section (objfile
,
1251 dwarf_frame_section
);
1252 unit
.dwarf_frame_size
= dwarf_frame_size
;
1253 unit
.dwarf_frame_section
= dwarf_frame_section
;
1255 frame_ptr
= unit
.dwarf_frame_buffer
;
1256 while (frame_ptr
< unit
.dwarf_frame_buffer
+ unit
.dwarf_frame_size
)
1257 frame_ptr
= decode_frame_entry (&unit
, frame_ptr
, 0);