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 /* True if a 'z' augmentation existed. */
69 unsigned char saw_z_augmentation
;
71 struct dwarf2_cie
*next
;
74 /* Frame Description Entry (FDE). */
78 /* CIE for this FDE. */
79 struct dwarf2_cie
*cie
;
81 /* First location associated with this FDE. */
82 CORE_ADDR initial_location
;
84 /* Number of bytes of program instructions described by this FDE. */
85 CORE_ADDR address_range
;
87 /* Instruction sequence. */
88 unsigned char *instructions
;
91 struct dwarf2_fde
*next
;
94 static struct dwarf2_fde
*dwarf2_frame_find_fde (CORE_ADDR
*pc
);
97 /* Structure describing a frame state. */
99 struct dwarf2_frame_state
101 /* Each register save state can be described in terms of a CFA slot,
102 another register, or a location expression. */
103 struct dwarf2_frame_state_reg_info
105 struct dwarf2_frame_state_reg
123 /* Used to implement DW_CFA_remember_state. */
124 struct dwarf2_frame_state_reg_info
*prev
;
129 unsigned char *cfa_exp
;
136 /* The PC described by the current frame state. */
139 /* Initial register set from the CIE.
140 Used to implement DW_CFA_restore. */
141 struct dwarf2_frame_state_reg_info initial
;
143 /* The information we care about from the CIE. */
146 ULONGEST retaddr_column
;
149 /* Store the length the expression for the CFA in the `cfa_reg' field,
150 which is unused in that case. */
151 #define cfa_exp_len cfa_reg
153 /* Assert that the register set RS is large enough to store NUM_REGS
154 columns. If necessary, enlarge the register set. */
157 dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info
*rs
,
160 size_t size
= sizeof (struct dwarf2_frame_state_reg
);
162 if (num_regs
<= rs
->num_regs
)
165 rs
->reg
= (struct dwarf2_frame_state_reg
*)
166 xrealloc (rs
->reg
, num_regs
* size
);
168 /* Initialize newly allocated registers. */
169 memset (rs
->reg
+ rs
->num_regs
, 0, (num_regs
- rs
->num_regs
) * size
);
170 rs
->num_regs
= num_regs
;
173 /* Copy the register columns in register set RS into newly allocated
174 memory and return a pointer to this newly created copy. */
176 static struct dwarf2_frame_state_reg
*
177 dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info
*rs
)
179 size_t size
= rs
->num_regs
* sizeof (struct dwarf2_frame_state_reg_info
);
180 struct dwarf2_frame_state_reg
*reg
;
182 reg
= (struct dwarf2_frame_state_reg
*) xmalloc (size
);
183 memcpy (reg
, rs
->reg
, size
);
188 /* Release the memory allocated to register set RS. */
191 dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info
*rs
)
195 dwarf2_frame_state_free_regs (rs
->prev
);
202 /* Release the memory allocated to the frame state FS. */
205 dwarf2_frame_state_free (void *p
)
207 struct dwarf2_frame_state
*fs
= p
;
209 dwarf2_frame_state_free_regs (fs
->initial
.prev
);
210 dwarf2_frame_state_free_regs (fs
->regs
.prev
);
211 xfree (fs
->initial
.reg
);
212 xfree (fs
->regs
.reg
);
217 /* Helper functions for execute_stack_op. */
220 read_reg (void *baton
, int reg
)
222 struct frame_info
*next_frame
= (struct frame_info
*) baton
;
226 regnum
= DWARF2_REG_TO_REGNUM (reg
);
228 buf
= (char *) alloca (register_size (current_gdbarch
, regnum
));
229 frame_unwind_register (next_frame
, regnum
, buf
);
230 return extract_typed_address (buf
, builtin_type_void_data_ptr
);
234 read_mem (void *baton
, char *buf
, CORE_ADDR addr
, size_t len
)
236 read_memory (addr
, buf
, len
);
240 no_get_frame_base (void *baton
, unsigned char **start
, size_t *length
)
242 internal_error (__FILE__
, __LINE__
,
243 "Support for DW_OP_fbreg is unimplemented");
247 no_get_tls_address (void *baton
, CORE_ADDR offset
)
249 internal_error (__FILE__
, __LINE__
,
250 "Support for DW_OP_GNU_push_tls_address is unimplemented");
254 execute_stack_op (unsigned char *exp
, ULONGEST len
,
255 struct frame_info
*next_frame
, CORE_ADDR initial
)
257 struct dwarf_expr_context
*ctx
;
260 ctx
= new_dwarf_expr_context ();
261 ctx
->baton
= next_frame
;
262 ctx
->read_reg
= read_reg
;
263 ctx
->read_mem
= read_mem
;
264 ctx
->get_frame_base
= no_get_frame_base
;
265 ctx
->get_tls_address
= no_get_tls_address
;
267 dwarf_expr_push (ctx
, initial
);
268 dwarf_expr_eval (ctx
, exp
, len
);
269 result
= dwarf_expr_fetch (ctx
, 0);
272 result
= read_reg (next_frame
, result
);
274 free_dwarf_expr_context (ctx
);
281 execute_cfa_program (unsigned char *insn_ptr
, unsigned char *insn_end
,
282 struct frame_info
*next_frame
,
283 struct dwarf2_frame_state
*fs
)
285 CORE_ADDR pc
= frame_pc_unwind (next_frame
);
288 while (insn_ptr
< insn_end
&& fs
->pc
<= pc
)
290 unsigned char insn
= *insn_ptr
++;
294 if ((insn
& 0xc0) == DW_CFA_advance_loc
)
295 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
296 else if ((insn
& 0xc0) == DW_CFA_offset
)
299 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
300 offset
= utmp
* fs
->data_align
;
301 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
302 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
303 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
305 else if ((insn
& 0xc0) == DW_CFA_restore
)
307 gdb_assert (fs
->initial
.reg
);
309 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
310 fs
->regs
.reg
[reg
] = fs
->initial
.reg
[reg
];
317 fs
->pc
= dwarf2_read_address (insn_ptr
, insn_end
, &bytes_read
);
318 insn_ptr
+= bytes_read
;
321 case DW_CFA_advance_loc1
:
322 utmp
= extract_unsigned_integer (insn_ptr
, 1);
323 fs
->pc
+= utmp
* fs
->code_align
;
326 case DW_CFA_advance_loc2
:
327 utmp
= extract_unsigned_integer (insn_ptr
, 2);
328 fs
->pc
+= utmp
* fs
->code_align
;
331 case DW_CFA_advance_loc4
:
332 utmp
= extract_unsigned_integer (insn_ptr
, 4);
333 fs
->pc
+= utmp
* fs
->code_align
;
337 case DW_CFA_offset_extended
:
338 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
339 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
340 offset
= utmp
* fs
->data_align
;
341 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
342 fs
->regs
.reg
[reg
].how
= REG_SAVED_OFFSET
;
343 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
346 case DW_CFA_restore_extended
:
347 gdb_assert (fs
->initial
.reg
);
348 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
349 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
350 fs
->regs
.reg
[reg
] = fs
->initial
.reg
[reg
];
353 case DW_CFA_undefined
:
354 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
355 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
356 fs
->regs
.reg
[reg
].how
= REG_UNSAVED
;
359 case DW_CFA_same_value
:
360 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
361 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
362 fs
->regs
.reg
[reg
].how
= REG_UNMODIFIED
;
365 case DW_CFA_register
:
366 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
367 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
368 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
369 fs
->regs
.reg
[reg
].loc
.reg
= utmp
;
372 case DW_CFA_remember_state
:
374 struct dwarf2_frame_state_reg_info
*new_rs
;
376 new_rs
= XMALLOC (struct dwarf2_frame_state_reg_info
);
378 fs
->regs
.reg
= dwarf2_frame_state_copy_regs (&fs
->regs
);
379 fs
->regs
.prev
= new_rs
;
383 case DW_CFA_restore_state
:
385 struct dwarf2_frame_state_reg_info
*old_rs
= fs
->regs
.prev
;
389 xfree (fs
->regs
.reg
);
396 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_reg
);
397 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
398 fs
->cfa_offset
= utmp
;
399 fs
->cfa_how
= CFA_REG_OFFSET
;
402 case DW_CFA_def_cfa_register
:
403 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_reg
);
404 fs
->cfa_how
= CFA_REG_OFFSET
;
407 case DW_CFA_def_cfa_offset
:
408 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_offset
);
409 /* cfa_how deliberately not set. */
412 case DW_CFA_def_cfa_expression
:
413 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &fs
->cfa_exp_len
);
414 fs
->cfa_exp
= insn_ptr
;
415 fs
->cfa_how
= CFA_EXP
;
416 insn_ptr
+= fs
->cfa_exp_len
;
419 case DW_CFA_expression
:
420 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, ®
);
421 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
422 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
423 fs
->regs
.reg
[reg
].loc
.exp
= insn_ptr
;
424 fs
->regs
.reg
[reg
].exp_len
= utmp
;
425 fs
->regs
.reg
[reg
].how
= REG_SAVED_EXP
;
432 case DW_CFA_GNU_args_size
:
434 insn_ptr
= read_uleb128 (insn_ptr
, insn_end
, &utmp
);
438 internal_error (__FILE__
, __LINE__
, "Unknown CFI encountered.");
443 /* Don't allow remember/restore between CIE and FDE programs. */
444 dwarf2_frame_state_free_regs (fs
->regs
.prev
);
445 fs
->regs
.prev
= NULL
;
448 struct dwarf2_frame_cache
450 /* DWARF Call Frame Address. */
453 /* Saved registers, indexed by GDB register number, not by DWARF
455 struct dwarf2_frame_state_reg
*reg
;
458 struct dwarf2_frame_cache
*
459 dwarf2_frame_cache (struct frame_info
*next_frame
, void **this_cache
)
461 struct cleanup
*old_chain
;
462 int num_regs
= NUM_REGS
+ NUM_PSEUDO_REGS
;
463 struct dwarf2_frame_cache
*cache
;
464 struct dwarf2_frame_state
*fs
;
465 struct dwarf2_fde
*fde
;
471 /* Allocate a new cache. */
472 cache
= FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache
);
473 cache
->reg
= FRAME_OBSTACK_CALLOC (num_regs
, struct dwarf2_frame_state_reg
);
475 /* Allocate and initialize the frame state. */
476 fs
= XMALLOC (struct dwarf2_frame_state
);
477 memset (fs
, 0, sizeof (struct dwarf2_frame_state
));
478 old_chain
= make_cleanup (dwarf2_frame_state_free
, fs
);
482 Note that if NEXT_FRAME is never supposed to return (i.e. a call
483 to abort), the compiler might optimize away the instruction at
484 NEXT_FRAME's return address. As a result the return address will
485 point at some random instruction, and the CFI for that
486 instruction is probably wortless to us. GCC's unwinder solves
487 this problem by substracting 1 from the return address to get an
488 address in the middle of a presumed call instruction (or the
489 instruction in the associated delay slot). This should only be
490 done for "normal" frames and not for resume-type frames (signal
491 handlers, sentinel frames, dummy frames).
493 We don't do what GCC's does here (yet). It's not clear how
494 reliable the method is. There's also a problem with finding the
495 right FDE; see the comment in dwarf_frame_p. If dwarf_frame_p
496 selected this frame unwinder because it found the FDE for the
497 next function, using the adjusted return address might not yield
498 a FDE at all. The problem isn't specific to DWARF CFI; other
499 unwinders loose in similar ways. Therefore it's probably
500 acceptable to leave things slightly broken for now. */
501 fs
->pc
= frame_pc_unwind (next_frame
);
503 /* Find the correct FDE. */
504 fde
= dwarf2_frame_find_fde (&fs
->pc
);
505 gdb_assert (fde
!= NULL
);
507 /* Extract any interesting information from the CIE. */
508 fs
->data_align
= fde
->cie
->data_alignment_factor
;
509 fs
->code_align
= fde
->cie
->code_alignment_factor
;
510 fs
->retaddr_column
= fde
->cie
->return_address_register
;
512 /* First decode all the insns in the CIE. */
513 execute_cfa_program (fde
->cie
->initial_instructions
,
514 fde
->cie
->end
, next_frame
, fs
);
516 /* Save the initialized register set. */
517 fs
->initial
= fs
->regs
;
518 fs
->initial
.reg
= dwarf2_frame_state_copy_regs (&fs
->regs
);
520 /* Then decode the insns in the FDE up to our target PC. */
521 execute_cfa_program (fde
->instructions
, fde
->end
, next_frame
, fs
);
523 /* Caclulate the CFA. */
527 cache
->cfa
= read_reg (next_frame
, fs
->cfa_reg
);
528 cache
->cfa
+= fs
->cfa_offset
;
533 execute_stack_op (fs
->cfa_exp
, fs
->cfa_exp_len
, next_frame
, 0);
537 internal_error (__FILE__
, __LINE__
, "Unknown CFA rule.");
540 /* Save the register info in the cache. */
541 for (reg
= 0; reg
< fs
->regs
.num_regs
; reg
++)
545 /* Skip the return address column. */
546 if (reg
== fs
->retaddr_column
)
549 /* Use the GDB register number as index. */
550 regnum
= DWARF2_REG_TO_REGNUM (reg
);
552 if (regnum
>= 0 && regnum
< num_regs
)
553 cache
->reg
[regnum
] = fs
->regs
.reg
[reg
];
556 /* Store the location of the return addess. If the return address
557 column (adjusted) is not the same as gdb's PC_REGNUM, then this
558 implies a copy from the ra column register. */
559 if (fs
->retaddr_column
< fs
->regs
.num_regs
560 && fs
->regs
.reg
[fs
->retaddr_column
].how
!= REG_UNSAVED
)
561 cache
->reg
[PC_REGNUM
] = fs
->regs
.reg
[fs
->retaddr_column
];
564 reg
= DWARF2_REG_TO_REGNUM (fs
->retaddr_column
);
565 if (reg
!= PC_REGNUM
)
567 cache
->reg
[PC_REGNUM
].loc
.reg
= reg
;
568 cache
->reg
[PC_REGNUM
].how
= REG_SAVED_REG
;
572 do_cleanups (old_chain
);
579 dwarf2_frame_this_id (struct frame_info
*next_frame
, void **this_cache
,
580 struct frame_id
*this_id
)
582 struct dwarf2_frame_cache
*cache
=
583 dwarf2_frame_cache (next_frame
, this_cache
);
585 (*this_id
) = frame_id_build (cache
->cfa
, frame_func_unwind (next_frame
));
589 dwarf2_frame_prev_register (struct frame_info
*next_frame
, void **this_cache
,
590 int regnum
, int *optimizedp
,
591 enum lval_type
*lvalp
, CORE_ADDR
*addrp
,
592 int *realnump
, void *valuep
)
594 struct dwarf2_frame_cache
*cache
=
595 dwarf2_frame_cache (next_frame
, this_cache
);
597 switch (cache
->reg
[regnum
].how
)
604 if (regnum
== SP_REGNUM
)
606 /* GCC defines the CFA as the value of the stack pointer
607 just before the call instruction is executed. Do other
608 compilers use the same definition? */
612 /* Store the value. */
613 store_typed_address (valuep
, builtin_type_void_data_ptr
,
619 /* In some cases, for example %eflags on the i386, we have
620 to provide a sane value, even though this register wasn't
621 saved. Assume we can get it from NEXT_FRAME. */
622 frame_unwind_register (next_frame
, regnum
, valuep
);
626 case REG_SAVED_OFFSET
:
628 *lvalp
= lval_memory
;
629 *addrp
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
633 /* Read the value in from memory. */
634 read_memory (*addrp
, valuep
,
635 register_size (current_gdbarch
, regnum
));
640 regnum
= DWARF2_REG_TO_REGNUM (cache
->reg
[regnum
].loc
.reg
);
641 frame_register_unwind (next_frame
, regnum
,
642 optimizedp
, lvalp
, addrp
, realnump
, valuep
);
647 *lvalp
= lval_memory
;
648 *addrp
= execute_stack_op (cache
->reg
[regnum
].loc
.exp
,
649 cache
->reg
[regnum
].exp_len
,
650 next_frame
, cache
->cfa
);
654 /* Read the value in from memory. */
655 read_memory (*addrp
, valuep
,
656 register_size (current_gdbarch
, regnum
));
661 frame_register_unwind (next_frame
, regnum
,
662 optimizedp
, lvalp
, addrp
, realnump
, valuep
);
666 internal_error (__FILE__
, __LINE__
, "Unknown register rule.");
670 static const struct frame_unwind dwarf2_frame_unwind
=
673 dwarf2_frame_this_id
,
674 dwarf2_frame_prev_register
677 const struct frame_unwind
*
678 dwarf2_frame_p (CORE_ADDR pc
)
680 /* The way GDB works, this function can be called with PC just after
681 the last instruction of the function we're supposed to return the
682 unwind methods for. In that case we won't find the correct FDE;
683 instead we find the FDE for the next function, or we won't find
684 an FDE at all. There is a possible solution (see the comment in
685 dwarf2_frame_cache), GDB doesn't pass us enough information to
687 if (dwarf2_frame_find_fde (&pc
))
688 return &dwarf2_frame_unwind
;
694 /* There is no explicitly defined relationship between the CFA and the
695 location of frame's local variables and arguments/parameters.
696 Therefore, frame base methods on this page should probably only be
697 used as a last resort, just to avoid printing total garbage as a
698 response to the "info frame" command. */
701 dwarf2_frame_base_address (struct frame_info
*next_frame
, void **this_cache
)
703 struct dwarf2_frame_cache
*cache
=
704 dwarf2_frame_cache (next_frame
, this_cache
);
709 static const struct frame_base dwarf2_frame_base
=
711 &dwarf2_frame_unwind
,
712 dwarf2_frame_base_address
,
713 dwarf2_frame_base_address
,
714 dwarf2_frame_base_address
717 const struct frame_base
*
718 dwarf2_frame_base_p (CORE_ADDR pc
)
720 if (dwarf2_frame_find_fde (&pc
))
721 return &dwarf2_frame_base
;
726 /* A minimal decoding of DWARF2 compilation units. We only decode
727 what's needed to get to the call frame information. */
731 /* Keep the bfd convenient. */
734 struct objfile
*objfile
;
736 /* Linked list of CIEs for this object. */
737 struct dwarf2_cie
*cie
;
739 /* Address size for this unit - from unit header. */
740 unsigned char addr_size
;
742 /* Pointer to the .debug_frame section loaded into memory. */
743 char *dwarf_frame_buffer
;
745 /* Length of the loaded .debug_frame section. */
746 unsigned long dwarf_frame_size
;
748 /* Pointer to the .debug_frame section. */
749 asection
*dwarf_frame_section
;
753 read_1_byte (bfd
*bfd
, char *buf
)
755 return bfd_get_8 (abfd
, (bfd_byte
*) buf
);
759 read_4_bytes (bfd
*abfd
, char *buf
)
761 return bfd_get_32 (abfd
, (bfd_byte
*) buf
);
765 read_8_bytes (bfd
*abfd
, char *buf
)
767 return bfd_get_64 (abfd
, (bfd_byte
*) buf
);
771 read_unsigned_leb128 (bfd
*abfd
, char *buf
, unsigned int *bytes_read_ptr
)
774 unsigned int num_read
;
784 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
787 result
|= ((byte
& 0x7f) << shift
);
792 *bytes_read_ptr
= num_read
;
798 read_signed_leb128 (bfd
*abfd
, char *buf
, unsigned int *bytes_read_ptr
)
802 unsigned int num_read
;
811 byte
= bfd_get_8 (abfd
, (bfd_byte
*) buf
);
814 result
|= ((byte
& 0x7f) << shift
);
819 if ((shift
< 32) && (byte
& 0x40))
820 result
|= -(1 << shift
);
822 *bytes_read_ptr
= num_read
;
828 read_initial_length (bfd
*abfd
, char *buf
, unsigned int *bytes_read_ptr
)
832 result
= bfd_get_32 (abfd
, (bfd_byte
*) buf
);
833 if (result
== 0xffffffff)
835 result
= bfd_get_64 (abfd
, (bfd_byte
*) buf
+ 4);
836 *bytes_read_ptr
= 12;
845 /* Pointer encoding helper functions. */
847 /* GCC supports exception handling based on DWARF2 CFI. However, for
848 technical reasons, it encodes addresses in its FDE's in a different
849 way. Several "pointer encodings" are supported. The encoding
850 that's used for a particular FDE is determined by the 'R'
851 augmentation in the associated CIE. The argument of this
852 augmentation is a single byte.
854 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
855 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
856 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
857 address should be interpreted (absolute, relative to the current
858 position in the FDE, ...). Bit 7, indicates that the address
859 should be dereferenced. */
862 encoding_for_size (unsigned int size
)
867 return DW_EH_PE_udata2
;
869 return DW_EH_PE_udata4
;
871 return DW_EH_PE_udata8
;
873 internal_error (__FILE__
, __LINE__
, "Unsupported address size");
878 size_of_encoded_value (unsigned char encoding
)
880 if (encoding
== DW_EH_PE_omit
)
883 switch (encoding
& 0x07)
885 case DW_EH_PE_absptr
:
886 return TYPE_LENGTH (builtin_type_void_data_ptr
);
887 case DW_EH_PE_udata2
:
889 case DW_EH_PE_udata4
:
891 case DW_EH_PE_udata8
:
894 internal_error (__FILE__
, __LINE__
, "Invalid or unsupported encoding");
899 read_encoded_value (struct comp_unit
*unit
, unsigned char encoding
,
900 char *buf
, unsigned int *bytes_read_ptr
)
904 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
906 if (encoding
& DW_EH_PE_indirect
)
907 internal_error (__FILE__
, __LINE__
,
908 "Unsupported encoding: DW_EH_PE_indirect");
910 switch (encoding
& 0x70)
912 case DW_EH_PE_absptr
:
916 base
= bfd_get_section_vma (unit
->bfd
, unit
->dwarf_frame_section
);
917 base
+= (buf
- unit
->dwarf_frame_buffer
);
920 internal_error (__FILE__
, __LINE__
, "Invalid or unsupported encoding");
923 if ((encoding
& 0x0f) == 0x00)
924 encoding
|= encoding_for_size (TYPE_LENGTH(builtin_type_void_data_ptr
));
926 switch (encoding
& 0x0f)
928 case DW_EH_PE_udata2
:
930 return (base
+ bfd_get_16 (unit
->abfd
, (bfd_byte
*) buf
));
931 case DW_EH_PE_udata4
:
933 return (base
+ bfd_get_32 (unit
->abfd
, (bfd_byte
*) buf
));
934 case DW_EH_PE_udata8
:
936 return (base
+ bfd_get_64 (unit
->abfd
, (bfd_byte
*) buf
));
937 case DW_EH_PE_sdata2
:
939 return (base
+ bfd_get_signed_16 (unit
->abfd
, (bfd_byte
*) buf
));
940 case DW_EH_PE_sdata4
:
942 return (base
+ bfd_get_signed_32 (unit
->abfd
, (bfd_byte
*) buf
));
943 case DW_EH_PE_sdata8
:
945 return (base
+ bfd_get_signed_64 (unit
->abfd
, (bfd_byte
*) buf
));
947 internal_error (__FILE__
, __LINE__
, "Invalid or unsupported encoding");
952 /* GCC uses a single CIE for all FDEs in a .debug_frame section.
953 That's why we use a simple linked list here. */
955 static struct dwarf2_cie
*
956 find_cie (struct comp_unit
*unit
, ULONGEST cie_pointer
)
958 struct dwarf2_cie
*cie
= unit
->cie
;
962 if (cie
->cie_pointer
== cie_pointer
)
972 add_cie (struct comp_unit
*unit
, struct dwarf2_cie
*cie
)
974 cie
->next
= unit
->cie
;
978 /* Find the FDE for *PC. Return a pointer to the FDE, and store the
979 inital location associated with it into *PC. */
981 static struct dwarf2_fde
*
982 dwarf2_frame_find_fde (CORE_ADDR
*pc
)
984 struct objfile
*objfile
;
986 ALL_OBJFILES (objfile
)
988 struct dwarf2_fde
*fde
;
991 offset
= ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
993 fde
= objfile
->sym_private
;
996 if (*pc
>= fde
->initial_location
+ offset
997 && *pc
< fde
->initial_location
+ offset
+ fde
->address_range
)
999 *pc
= fde
->initial_location
+ offset
;
1011 add_fde (struct comp_unit
*unit
, struct dwarf2_fde
*fde
)
1013 fde
->next
= unit
->objfile
->sym_private
;
1014 unit
->objfile
->sym_private
= fde
;
1017 #ifdef CC_HAS_LONG_LONG
1018 #define DW64_CIE_ID 0xffffffffffffffffULL
1020 #define DW64_CIE_ID ~0
1023 /* Read a CIE or FDE in BUF and decode it. */
1026 decode_frame_entry (struct comp_unit
*unit
, char *buf
, int eh_frame_p
)
1029 unsigned int bytes_read
;
1031 ULONGEST cie_id
= DW_CIE_ID
;
1032 ULONGEST cie_pointer
;
1036 length
= read_initial_length (unit
->abfd
, buf
, &bytes_read
);
1043 if (bytes_read
== 12)
1046 /* In a .eh_frame section, zero is used to distinguish CIEs from
1051 cie_id
= DW64_CIE_ID
;
1055 cie_pointer
= read_8_bytes (unit
->abfd
, buf
);
1060 cie_pointer
= read_4_bytes (unit
->abfd
, buf
);
1064 if (cie_pointer
== cie_id
)
1066 /* This is a CIE. */
1067 struct dwarf2_cie
*cie
;
1070 /* Record the offset into the .debug_frame section of this CIE. */
1071 cie_pointer
= start
- unit
->dwarf_frame_buffer
;
1073 /* Check whether we've already read it. */
1074 if (find_cie (unit
, cie_pointer
))
1077 cie
= (struct dwarf2_cie
*)
1078 obstack_alloc (&unit
->objfile
->psymbol_obstack
,
1079 sizeof (struct dwarf2_cie
));
1080 cie
->initial_instructions
= NULL
;
1081 cie
->cie_pointer
= cie_pointer
;
1083 /* The encoding for FDE's in a normal .debug_frame section
1084 depends on the target address size as specified in the
1085 Compilation Unit Header. */
1086 cie
->encoding
= encoding_for_size (unit
->addr_size
);
1088 /* Check version number. */
1089 gdb_assert (read_1_byte (unit
->abfd
, buf
) == DW_CIE_VERSION
);
1092 /* Interpret the interesting bits of the augmentation. */
1094 buf
= augmentation
+ strlen (augmentation
) + 1;
1096 /* The GCC 2.x "eh" augmentation has a pointer immediately
1097 following the augmentation string, so it must be handled
1099 if (augmentation
[0] == 'e' && augmentation
[1] == 'h')
1102 buf
+= TYPE_LENGTH (builtin_type_void_data_ptr
);
1106 cie
->code_alignment_factor
=
1107 read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1110 cie
->data_alignment_factor
=
1111 read_signed_leb128 (unit
->abfd
, buf
, &bytes_read
);
1114 cie
->return_address_register
= read_1_byte (unit
->abfd
, buf
);
1117 cie
->saw_z_augmentation
= (*augmentation
== 'z');
1118 if (cie
->saw_z_augmentation
)
1122 length
= read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1124 cie
->initial_instructions
= buf
+ length
;
1128 while (*augmentation
)
1130 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1131 if (*augmentation
== 'L')
1138 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1139 else if (*augmentation
== 'R')
1141 cie
->encoding
= *buf
++;
1145 /* "P" indicates a personality routine in the CIE augmentation. */
1146 else if (*augmentation
== 'P')
1149 buf
+= size_of_encoded_value (*buf
++);
1153 /* Otherwise we have an unknown augmentation.
1154 Bail out unless we saw a 'z' prefix. */
1157 if (cie
->initial_instructions
== NULL
)
1160 /* Skip unknown augmentations. */
1161 buf
= cie
->initial_instructions
;
1166 cie
->initial_instructions
= buf
;
1169 add_cie (unit
, cie
);
1173 /* This is a FDE. */
1174 struct dwarf2_fde
*fde
;
1178 /* In an .eh_frame section, the CIE pointer is the delta
1179 between the address within the FDE where the CIE pointer
1180 is stored and the address of the CIE. Convert it to an
1181 offset into the .eh_frame section. */
1182 cie_pointer
= buf
- unit
->dwarf_frame_buffer
- cie_pointer
;
1183 cie_pointer
-= (dwarf64_p
? 8 : 4);
1186 fde
= (struct dwarf2_fde
*)
1187 obstack_alloc (&unit
->objfile
->psymbol_obstack
,
1188 sizeof (struct dwarf2_fde
));
1189 fde
->cie
= find_cie (unit
, cie_pointer
);
1190 if (fde
->cie
== NULL
)
1192 decode_frame_entry (unit
, unit
->dwarf_frame_buffer
+ cie_pointer
,
1194 fde
->cie
= find_cie (unit
, cie_pointer
);
1197 gdb_assert (fde
->cie
!= NULL
);
1199 fde
->initial_location
=
1200 read_encoded_value (unit
, fde
->cie
->encoding
, buf
, &bytes_read
);
1203 fde
->address_range
=
1204 read_encoded_value (unit
, fde
->cie
->encoding
& 0x0f, buf
, &bytes_read
);
1207 /* A 'z' augmentation in the CIE implies the presence of an
1208 augmentation field in the FDE as well. The only thing known
1209 to be in here at present is the LSDA entry for EH. So we
1210 can skip the whole thing. */
1211 if (fde
->cie
->saw_z_augmentation
)
1215 length
= read_unsigned_leb128 (unit
->abfd
, buf
, &bytes_read
);
1216 buf
+= bytes_read
+ length
;
1219 fde
->instructions
= buf
;
1222 add_fde (unit
, fde
);
1229 /* FIXME: kettenis/20030504: This still needs to be integrated with
1230 dwarf2read.c in a better way. */
1232 /* Imported from dwarf2read.c. */
1233 extern file_ptr dwarf_frame_offset
;
1234 extern unsigned int dwarf_frame_size
;
1235 extern asection
*dwarf_frame_section
;
1236 extern file_ptr dwarf_eh_frame_offset
;
1237 extern unsigned int dwarf_eh_frame_size
;
1238 extern asection
*dwarf_eh_frame_section
;
1240 /* Imported from dwarf2read.c. */
1241 extern char *dwarf2_read_section (struct objfile
*objfile
, file_ptr offset
,
1242 unsigned int size
, asection
*sectp
);
1245 dwarf2_build_frame_info (struct objfile
*objfile
)
1247 struct comp_unit unit
;
1250 /* Build a minimal decoding of the DWARF2 compilation unit. */
1251 unit
.abfd
= objfile
->obfd
;
1252 unit
.objfile
= objfile
;
1253 unit
.addr_size
= objfile
->obfd
->arch_info
->bits_per_address
/ 8;
1255 /* First add the information from the .eh_frame section. That way,
1256 the FDEs from that section are searched last. */
1257 if (dwarf_eh_frame_offset
)
1260 unit
.dwarf_frame_buffer
= dwarf2_read_section (objfile
,
1261 dwarf_eh_frame_offset
,
1262 dwarf_eh_frame_size
,
1263 dwarf_eh_frame_section
);
1265 unit
.dwarf_frame_size
= dwarf_eh_frame_size
;
1266 unit
.dwarf_frame_section
= dwarf_eh_frame_section
;
1268 frame_ptr
= unit
.dwarf_frame_buffer
;
1269 while (frame_ptr
< unit
.dwarf_frame_buffer
+ unit
.dwarf_frame_size
)
1270 frame_ptr
= decode_frame_entry (&unit
, frame_ptr
, 1);
1273 if (dwarf_frame_offset
)
1276 unit
.dwarf_frame_buffer
= dwarf2_read_section (objfile
,
1279 dwarf_frame_section
);
1280 unit
.dwarf_frame_size
= dwarf_frame_size
;
1281 unit
.dwarf_frame_section
= dwarf_frame_section
;
1283 frame_ptr
= unit
.dwarf_frame_buffer
;
1284 while (frame_ptr
< unit
.dwarf_frame_buffer
+ unit
.dwarf_frame_size
)
1285 frame_ptr
= decode_frame_entry (&unit
, frame_ptr
, 0);