1 /* Frame unwinder for frames with DWARF Call Frame Information.
3 Copyright (C) 2003-2017 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 3 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, see <http://www.gnu.org/licenses/>. */
23 #include "dwarf2expr.h"
26 #include "frame-base.h"
27 #include "frame-unwind.h"
36 #include "complaints.h"
37 #include "dwarf2-frame.h"
39 #include "dwarf2loc.h"
40 #include "dwarf2-frame-tailcall.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 const gdb_byte
*initial_instructions
;
71 /* Saved augmentation, in case it's needed later. */
74 /* Encoding of addresses. */
77 /* Target address size in bytes. */
80 /* Target pointer size in bytes. */
83 /* True if a 'z' augmentation existed. */
84 unsigned char saw_z_augmentation
;
86 /* True if an 'S' augmentation existed. */
87 unsigned char signal_frame
;
89 /* The version recorded in the CIE. */
90 unsigned char version
;
92 /* The segment size. */
93 unsigned char segment_size
;
96 struct dwarf2_cie_table
99 struct dwarf2_cie
**entries
;
102 /* Frame Description Entry (FDE). */
106 /* CIE for this FDE. */
107 struct dwarf2_cie
*cie
;
109 /* First location associated with this FDE. */
110 CORE_ADDR initial_location
;
112 /* Number of bytes of program instructions described by this FDE. */
113 CORE_ADDR address_range
;
115 /* Instruction sequence. */
116 const gdb_byte
*instructions
;
119 /* True if this FDE is read from a .eh_frame instead of a .debug_frame
121 unsigned char eh_frame_p
;
124 struct dwarf2_fde_table
127 struct dwarf2_fde
**entries
;
130 /* A minimal decoding of DWARF2 compilation units. We only decode
131 what's needed to get to the call frame information. */
135 /* Keep the bfd convenient. */
138 struct objfile
*objfile
;
140 /* Pointer to the .debug_frame section loaded into memory. */
141 const gdb_byte
*dwarf_frame_buffer
;
143 /* Length of the loaded .debug_frame section. */
144 bfd_size_type dwarf_frame_size
;
146 /* Pointer to the .debug_frame section. */
147 asection
*dwarf_frame_section
;
149 /* Base for DW_EH_PE_datarel encodings. */
152 /* Base for DW_EH_PE_textrel encodings. */
156 static struct dwarf2_fde
*dwarf2_frame_find_fde (CORE_ADDR
*pc
,
157 CORE_ADDR
*out_offset
);
159 static int dwarf2_frame_adjust_regnum (struct gdbarch
*gdbarch
, int regnum
,
162 static CORE_ADDR
read_encoded_value (struct comp_unit
*unit
, gdb_byte encoding
,
163 int ptr_len
, const gdb_byte
*buf
,
164 unsigned int *bytes_read_ptr
,
165 CORE_ADDR func_base
);
168 /* Store the length the expression for the CFA in the `cfa_reg' field,
169 which is unused in that case. */
170 #define cfa_exp_len cfa_reg
172 /* Assert that the register set RS is large enough to store gdbarch_num_regs
173 columns. If necessary, enlarge the register set. */
176 dwarf2_frame_state_alloc_regs (struct dwarf2_frame_state_reg_info
*rs
,
179 size_t size
= sizeof (struct dwarf2_frame_state_reg
);
181 if (num_regs
<= rs
->num_regs
)
184 rs
->reg
= (struct dwarf2_frame_state_reg
*)
185 xrealloc (rs
->reg
, num_regs
* size
);
187 /* Initialize newly allocated registers. */
188 memset (rs
->reg
+ rs
->num_regs
, 0, (num_regs
- rs
->num_regs
) * size
);
189 rs
->num_regs
= num_regs
;
192 /* Copy the register columns in register set RS into newly allocated
193 memory and return a pointer to this newly created copy. */
195 static struct dwarf2_frame_state_reg
*
196 dwarf2_frame_state_copy_regs (struct dwarf2_frame_state_reg_info
*rs
)
198 size_t size
= rs
->num_regs
* sizeof (struct dwarf2_frame_state_reg
);
199 struct dwarf2_frame_state_reg
*reg
;
201 reg
= (struct dwarf2_frame_state_reg
*) xmalloc (size
);
202 memcpy (reg
, rs
->reg
, size
);
207 /* Release the memory allocated to register set RS. */
210 dwarf2_frame_state_free_regs (struct dwarf2_frame_state_reg_info
*rs
)
214 dwarf2_frame_state_free_regs (rs
->prev
);
221 /* Release the memory allocated to the frame state FS. */
224 dwarf2_frame_state_free (void *p
)
226 struct dwarf2_frame_state
*fs
= (struct dwarf2_frame_state
*) p
;
228 dwarf2_frame_state_free_regs (fs
->initial
.prev
);
229 dwarf2_frame_state_free_regs (fs
->regs
.prev
);
230 xfree (fs
->initial
.reg
);
231 xfree (fs
->regs
.reg
);
236 /* Helper functions for execute_stack_op. */
239 read_addr_from_reg (struct frame_info
*this_frame
, int reg
)
241 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
242 int regnum
= dwarf_reg_to_regnum_or_error (gdbarch
, reg
);
244 return address_from_register (regnum
, this_frame
);
247 /* Execute the required actions for both the DW_CFA_restore and
248 DW_CFA_restore_extended instructions. */
250 dwarf2_restore_rule (struct gdbarch
*gdbarch
, ULONGEST reg_num
,
251 struct dwarf2_frame_state
*fs
, int eh_frame_p
)
255 gdb_assert (fs
->initial
.reg
);
256 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg_num
, eh_frame_p
);
257 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
259 /* Check if this register was explicitly initialized in the
260 CIE initial instructions. If not, default the rule to
262 if (reg
< fs
->initial
.num_regs
)
263 fs
->regs
.reg
[reg
] = fs
->initial
.reg
[reg
];
265 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_UNSPECIFIED
;
267 if (fs
->regs
.reg
[reg
].how
== DWARF2_FRAME_REG_UNSPECIFIED
)
269 int regnum
= dwarf_reg_to_regnum (gdbarch
, reg
);
271 complaint (&symfile_complaints
, _("\
272 incomplete CFI data; DW_CFA_restore unspecified\n\
273 register %s (#%d) at %s"),
274 gdbarch_register_name (gdbarch
, regnum
), regnum
,
275 paddress (gdbarch
, fs
->pc
));
279 class dwarf_expr_executor
: public dwarf_expr_context
283 struct frame_info
*this_frame
;
285 CORE_ADDR
read_addr_from_reg (int reg
) OVERRIDE
287 return ::read_addr_from_reg (this_frame
, reg
);
290 struct value
*get_reg_value (struct type
*type
, int reg
) OVERRIDE
292 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
293 int regnum
= dwarf_reg_to_regnum_or_error (gdbarch
, reg
);
295 return value_from_register (type
, regnum
, this_frame
);
298 void read_mem (gdb_byte
*buf
, CORE_ADDR addr
, size_t len
) OVERRIDE
300 read_memory (addr
, buf
, len
);
303 void get_frame_base (const gdb_byte
**start
, size_t *length
) OVERRIDE
305 invalid ("DW_OP_fbreg");
308 void push_dwarf_reg_entry_value (enum call_site_parameter_kind kind
,
309 union call_site_parameter_u kind_u
,
310 int deref_size
) OVERRIDE
312 invalid ("DW_OP_entry_value");
315 CORE_ADDR
get_object_address () OVERRIDE
317 invalid ("DW_OP_push_object_address");
320 CORE_ADDR
get_frame_cfa () OVERRIDE
322 invalid ("DW_OP_call_frame_cfa");
325 CORE_ADDR
get_tls_address (CORE_ADDR offset
) OVERRIDE
327 invalid ("DW_OP_form_tls_address");
330 void dwarf_call (cu_offset die_offset
) OVERRIDE
332 invalid ("DW_OP_call*");
335 CORE_ADDR
get_addr_index (unsigned int index
)
337 invalid ("DW_OP_GNU_addr_index");
342 void invalid (const char *op
) ATTRIBUTE_NORETURN
344 error (_("%s is invalid in this context"), op
);
349 execute_stack_op (const gdb_byte
*exp
, ULONGEST len
, int addr_size
,
350 CORE_ADDR offset
, struct frame_info
*this_frame
,
351 CORE_ADDR initial
, int initial_in_stack_memory
)
355 dwarf_expr_executor ctx
;
356 scoped_value_mark free_values
;
358 ctx
.this_frame
= this_frame
;
359 ctx
.gdbarch
= get_frame_arch (this_frame
);
360 ctx
.addr_size
= addr_size
;
361 ctx
.ref_addr_size
= -1;
364 ctx
.push_address (initial
, initial_in_stack_memory
);
367 if (ctx
.location
== DWARF_VALUE_MEMORY
)
368 result
= ctx
.fetch_address (0);
369 else if (ctx
.location
== DWARF_VALUE_REGISTER
)
370 result
= ctx
.read_addr_from_reg (value_as_long (ctx
.fetch (0)));
373 /* This is actually invalid DWARF, but if we ever do run across
374 it somehow, we might as well support it. So, instead, report
375 it as unimplemented. */
377 Not implemented: computing unwound register using explicit value operator"));
384 /* Execute FDE program from INSN_PTR possibly up to INSN_END or up to inferior
385 PC. Modify FS state accordingly. Return current INSN_PTR where the
386 execution has stopped, one can resume it on the next call. */
388 static const gdb_byte
*
389 execute_cfa_program (struct dwarf2_fde
*fde
, const gdb_byte
*insn_ptr
,
390 const gdb_byte
*insn_end
, struct gdbarch
*gdbarch
,
391 CORE_ADDR pc
, struct dwarf2_frame_state
*fs
)
393 int eh_frame_p
= fde
->eh_frame_p
;
394 unsigned int bytes_read
;
395 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
397 while (insn_ptr
< insn_end
&& fs
->pc
<= pc
)
399 gdb_byte insn
= *insn_ptr
++;
403 if ((insn
& 0xc0) == DW_CFA_advance_loc
)
404 fs
->pc
+= (insn
& 0x3f) * fs
->code_align
;
405 else if ((insn
& 0xc0) == DW_CFA_offset
)
408 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
409 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
410 offset
= utmp
* fs
->data_align
;
411 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
412 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
413 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
415 else if ((insn
& 0xc0) == DW_CFA_restore
)
418 dwarf2_restore_rule (gdbarch
, reg
, fs
, eh_frame_p
);
425 fs
->pc
= read_encoded_value (fde
->cie
->unit
, fde
->cie
->encoding
,
426 fde
->cie
->ptr_size
, insn_ptr
,
427 &bytes_read
, fde
->initial_location
);
428 /* Apply the objfile offset for relocatable objects. */
429 fs
->pc
+= ANOFFSET (fde
->cie
->unit
->objfile
->section_offsets
,
430 SECT_OFF_TEXT (fde
->cie
->unit
->objfile
));
431 insn_ptr
+= bytes_read
;
434 case DW_CFA_advance_loc1
:
435 utmp
= extract_unsigned_integer (insn_ptr
, 1, byte_order
);
436 fs
->pc
+= utmp
* fs
->code_align
;
439 case DW_CFA_advance_loc2
:
440 utmp
= extract_unsigned_integer (insn_ptr
, 2, byte_order
);
441 fs
->pc
+= utmp
* fs
->code_align
;
444 case DW_CFA_advance_loc4
:
445 utmp
= extract_unsigned_integer (insn_ptr
, 4, byte_order
);
446 fs
->pc
+= utmp
* fs
->code_align
;
450 case DW_CFA_offset_extended
:
451 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
452 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
453 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
454 offset
= utmp
* fs
->data_align
;
455 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
456 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
457 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
460 case DW_CFA_restore_extended
:
461 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
462 dwarf2_restore_rule (gdbarch
, reg
, fs
, eh_frame_p
);
465 case DW_CFA_undefined
:
466 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
467 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
468 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
469 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_UNDEFINED
;
472 case DW_CFA_same_value
:
473 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
474 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
475 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
476 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAME_VALUE
;
479 case DW_CFA_register
:
480 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
481 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
482 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
483 utmp
= dwarf2_frame_adjust_regnum (gdbarch
, utmp
, eh_frame_p
);
484 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
485 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_REG
;
486 fs
->regs
.reg
[reg
].loc
.reg
= utmp
;
489 case DW_CFA_remember_state
:
491 struct dwarf2_frame_state_reg_info
*new_rs
;
493 new_rs
= XNEW (struct dwarf2_frame_state_reg_info
);
495 fs
->regs
.reg
= dwarf2_frame_state_copy_regs (&fs
->regs
);
496 fs
->regs
.prev
= new_rs
;
500 case DW_CFA_restore_state
:
502 struct dwarf2_frame_state_reg_info
*old_rs
= fs
->regs
.prev
;
506 complaint (&symfile_complaints
, _("\
507 bad CFI data; mismatched DW_CFA_restore_state at %s"),
508 paddress (gdbarch
, fs
->pc
));
512 xfree (fs
->regs
.reg
);
520 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
521 fs
->regs
.cfa_reg
= reg
;
522 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
524 if (fs
->armcc_cfa_offsets_sf
)
525 utmp
*= fs
->data_align
;
527 fs
->regs
.cfa_offset
= utmp
;
528 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
531 case DW_CFA_def_cfa_register
:
532 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
533 fs
->regs
.cfa_reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
,
535 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
538 case DW_CFA_def_cfa_offset
:
539 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
541 if (fs
->armcc_cfa_offsets_sf
)
542 utmp
*= fs
->data_align
;
544 fs
->regs
.cfa_offset
= utmp
;
545 /* cfa_how deliberately not set. */
551 case DW_CFA_def_cfa_expression
:
552 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
553 fs
->regs
.cfa_exp_len
= utmp
;
554 fs
->regs
.cfa_exp
= insn_ptr
;
555 fs
->regs
.cfa_how
= CFA_EXP
;
556 insn_ptr
+= fs
->regs
.cfa_exp_len
;
559 case DW_CFA_expression
:
560 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
561 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
562 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
563 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
564 fs
->regs
.reg
[reg
].loc
.exp
= insn_ptr
;
565 fs
->regs
.reg
[reg
].exp_len
= utmp
;
566 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_EXP
;
570 case DW_CFA_offset_extended_sf
:
571 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
572 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
573 insn_ptr
= safe_read_sleb128 (insn_ptr
, insn_end
, &offset
);
574 offset
*= fs
->data_align
;
575 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
576 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
577 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
580 case DW_CFA_val_offset
:
581 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
582 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
583 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
584 offset
= utmp
* fs
->data_align
;
585 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_VAL_OFFSET
;
586 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
589 case DW_CFA_val_offset_sf
:
590 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
591 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
592 insn_ptr
= safe_read_sleb128 (insn_ptr
, insn_end
, &offset
);
593 offset
*= fs
->data_align
;
594 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_VAL_OFFSET
;
595 fs
->regs
.reg
[reg
].loc
.offset
= offset
;
598 case DW_CFA_val_expression
:
599 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
600 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
601 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
602 fs
->regs
.reg
[reg
].loc
.exp
= insn_ptr
;
603 fs
->regs
.reg
[reg
].exp_len
= utmp
;
604 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_VAL_EXP
;
608 case DW_CFA_def_cfa_sf
:
609 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
610 fs
->regs
.cfa_reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
,
612 insn_ptr
= safe_read_sleb128 (insn_ptr
, insn_end
, &offset
);
613 fs
->regs
.cfa_offset
= offset
* fs
->data_align
;
614 fs
->regs
.cfa_how
= CFA_REG_OFFSET
;
617 case DW_CFA_def_cfa_offset_sf
:
618 insn_ptr
= safe_read_sleb128 (insn_ptr
, insn_end
, &offset
);
619 fs
->regs
.cfa_offset
= offset
* fs
->data_align
;
620 /* cfa_how deliberately not set. */
623 case DW_CFA_GNU_args_size
:
625 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
628 case DW_CFA_GNU_negative_offset_extended
:
629 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, ®
);
630 reg
= dwarf2_frame_adjust_regnum (gdbarch
, reg
, eh_frame_p
);
631 insn_ptr
= safe_read_uleb128 (insn_ptr
, insn_end
, &utmp
);
632 offset
= utmp
* fs
->data_align
;
633 dwarf2_frame_state_alloc_regs (&fs
->regs
, reg
+ 1);
634 fs
->regs
.reg
[reg
].how
= DWARF2_FRAME_REG_SAVED_OFFSET
;
635 fs
->regs
.reg
[reg
].loc
.offset
= -offset
;
639 if (insn
>= DW_CFA_lo_user
&& insn
<= DW_CFA_hi_user
)
641 /* Handle vendor-specific CFI for different architectures. */
642 if (!gdbarch_execute_dwarf_cfa_vendor_op (gdbarch
, insn
, fs
))
643 error (_("Call Frame Instruction op %d in vendor extension "
644 "space is not handled on this architecture."),
648 internal_error (__FILE__
, __LINE__
,
649 _("Unknown CFI encountered."));
654 if (fs
->initial
.reg
== NULL
)
656 /* Don't allow remember/restore between CIE and FDE programs. */
657 dwarf2_frame_state_free_regs (fs
->regs
.prev
);
658 fs
->regs
.prev
= NULL
;
665 /* Architecture-specific operations. */
667 /* Per-architecture data key. */
668 static struct gdbarch_data
*dwarf2_frame_data
;
670 struct dwarf2_frame_ops
672 /* Pre-initialize the register state REG for register REGNUM. */
673 void (*init_reg
) (struct gdbarch
*, int, struct dwarf2_frame_state_reg
*,
674 struct frame_info
*);
676 /* Check whether the THIS_FRAME is a signal trampoline. */
677 int (*signal_frame_p
) (struct gdbarch
*, struct frame_info
*);
679 /* Convert .eh_frame register number to DWARF register number, or
680 adjust .debug_frame register number. */
681 int (*adjust_regnum
) (struct gdbarch
*, int, int);
684 /* Default architecture-specific register state initialization
688 dwarf2_frame_default_init_reg (struct gdbarch
*gdbarch
, int regnum
,
689 struct dwarf2_frame_state_reg
*reg
,
690 struct frame_info
*this_frame
)
692 /* If we have a register that acts as a program counter, mark it as
693 a destination for the return address. If we have a register that
694 serves as the stack pointer, arrange for it to be filled with the
695 call frame address (CFA). The other registers are marked as
698 We copy the return address to the program counter, since many
699 parts in GDB assume that it is possible to get the return address
700 by unwinding the program counter register. However, on ISA's
701 with a dedicated return address register, the CFI usually only
702 contains information to unwind that return address register.
704 The reason we're treating the stack pointer special here is
705 because in many cases GCC doesn't emit CFI for the stack pointer
706 and implicitly assumes that it is equal to the CFA. This makes
707 some sense since the DWARF specification (version 3, draft 8,
710 "Typically, the CFA is defined to be the value of the stack
711 pointer at the call site in the previous frame (which may be
712 different from its value on entry to the current frame)."
714 However, this isn't true for all platforms supported by GCC
715 (e.g. IBM S/390 and zSeries). Those architectures should provide
716 their own architecture-specific initialization function. */
718 if (regnum
== gdbarch_pc_regnum (gdbarch
))
719 reg
->how
= DWARF2_FRAME_REG_RA
;
720 else if (regnum
== gdbarch_sp_regnum (gdbarch
))
721 reg
->how
= DWARF2_FRAME_REG_CFA
;
724 /* Return a default for the architecture-specific operations. */
727 dwarf2_frame_init (struct obstack
*obstack
)
729 struct dwarf2_frame_ops
*ops
;
731 ops
= OBSTACK_ZALLOC (obstack
, struct dwarf2_frame_ops
);
732 ops
->init_reg
= dwarf2_frame_default_init_reg
;
736 /* Set the architecture-specific register state initialization
737 function for GDBARCH to INIT_REG. */
740 dwarf2_frame_set_init_reg (struct gdbarch
*gdbarch
,
741 void (*init_reg
) (struct gdbarch
*, int,
742 struct dwarf2_frame_state_reg
*,
743 struct frame_info
*))
745 struct dwarf2_frame_ops
*ops
746 = (struct dwarf2_frame_ops
*) gdbarch_data (gdbarch
, dwarf2_frame_data
);
748 ops
->init_reg
= init_reg
;
751 /* Pre-initialize the register state REG for register REGNUM. */
754 dwarf2_frame_init_reg (struct gdbarch
*gdbarch
, int regnum
,
755 struct dwarf2_frame_state_reg
*reg
,
756 struct frame_info
*this_frame
)
758 struct dwarf2_frame_ops
*ops
759 = (struct dwarf2_frame_ops
*) gdbarch_data (gdbarch
, dwarf2_frame_data
);
761 ops
->init_reg (gdbarch
, regnum
, reg
, this_frame
);
764 /* Set the architecture-specific signal trampoline recognition
765 function for GDBARCH to SIGNAL_FRAME_P. */
768 dwarf2_frame_set_signal_frame_p (struct gdbarch
*gdbarch
,
769 int (*signal_frame_p
) (struct gdbarch
*,
770 struct frame_info
*))
772 struct dwarf2_frame_ops
*ops
773 = (struct dwarf2_frame_ops
*) gdbarch_data (gdbarch
, dwarf2_frame_data
);
775 ops
->signal_frame_p
= signal_frame_p
;
778 /* Query the architecture-specific signal frame recognizer for
782 dwarf2_frame_signal_frame_p (struct gdbarch
*gdbarch
,
783 struct frame_info
*this_frame
)
785 struct dwarf2_frame_ops
*ops
786 = (struct dwarf2_frame_ops
*) gdbarch_data (gdbarch
, dwarf2_frame_data
);
788 if (ops
->signal_frame_p
== NULL
)
790 return ops
->signal_frame_p (gdbarch
, this_frame
);
793 /* Set the architecture-specific adjustment of .eh_frame and .debug_frame
797 dwarf2_frame_set_adjust_regnum (struct gdbarch
*gdbarch
,
798 int (*adjust_regnum
) (struct gdbarch
*,
801 struct dwarf2_frame_ops
*ops
802 = (struct dwarf2_frame_ops
*) gdbarch_data (gdbarch
, dwarf2_frame_data
);
804 ops
->adjust_regnum
= adjust_regnum
;
807 /* Translate a .eh_frame register to DWARF register, or adjust a .debug_frame
811 dwarf2_frame_adjust_regnum (struct gdbarch
*gdbarch
,
812 int regnum
, int eh_frame_p
)
814 struct dwarf2_frame_ops
*ops
815 = (struct dwarf2_frame_ops
*) gdbarch_data (gdbarch
, dwarf2_frame_data
);
817 if (ops
->adjust_regnum
== NULL
)
819 return ops
->adjust_regnum (gdbarch
, regnum
, eh_frame_p
);
823 dwarf2_frame_find_quirks (struct dwarf2_frame_state
*fs
,
824 struct dwarf2_fde
*fde
)
826 struct compunit_symtab
*cust
;
828 cust
= find_pc_compunit_symtab (fs
->pc
);
832 if (producer_is_realview (COMPUNIT_PRODUCER (cust
)))
834 if (fde
->cie
->version
== 1)
835 fs
->armcc_cfa_offsets_sf
= 1;
837 if (fde
->cie
->version
== 1)
838 fs
->armcc_cfa_offsets_reversed
= 1;
840 /* The reversed offset problem is present in some compilers
841 using DWARF3, but it was eventually fixed. Check the ARM
842 defined augmentations, which are in the format "armcc" followed
843 by a list of one-character options. The "+" option means
844 this problem is fixed (no quirk needed). If the armcc
845 augmentation is missing, the quirk is needed. */
846 if (fde
->cie
->version
== 3
847 && (!startswith (fde
->cie
->augmentation
, "armcc")
848 || strchr (fde
->cie
->augmentation
+ 5, '+') == NULL
))
849 fs
->armcc_cfa_offsets_reversed
= 1;
856 /* See dwarf2-frame.h. */
859 dwarf2_fetch_cfa_info (struct gdbarch
*gdbarch
, CORE_ADDR pc
,
860 struct dwarf2_per_cu_data
*data
,
861 int *regnum_out
, LONGEST
*offset_out
,
862 CORE_ADDR
*text_offset_out
,
863 const gdb_byte
**cfa_start_out
,
864 const gdb_byte
**cfa_end_out
)
866 struct dwarf2_fde
*fde
;
867 CORE_ADDR text_offset
;
868 struct dwarf2_frame_state fs
;
870 memset (&fs
, 0, sizeof (struct dwarf2_frame_state
));
874 /* Find the correct FDE. */
875 fde
= dwarf2_frame_find_fde (&fs
.pc
, &text_offset
);
877 error (_("Could not compute CFA; needed to translate this expression"));
879 /* Extract any interesting information from the CIE. */
880 fs
.data_align
= fde
->cie
->data_alignment_factor
;
881 fs
.code_align
= fde
->cie
->code_alignment_factor
;
882 fs
.retaddr_column
= fde
->cie
->return_address_register
;
884 /* Check for "quirks" - known bugs in producers. */
885 dwarf2_frame_find_quirks (&fs
, fde
);
887 /* First decode all the insns in the CIE. */
888 execute_cfa_program (fde
, fde
->cie
->initial_instructions
,
889 fde
->cie
->end
, gdbarch
, pc
, &fs
);
891 /* Save the initialized register set. */
892 fs
.initial
= fs
.regs
;
893 fs
.initial
.reg
= dwarf2_frame_state_copy_regs (&fs
.regs
);
895 /* Then decode the insns in the FDE up to our target PC. */
896 execute_cfa_program (fde
, fde
->instructions
, fde
->end
, gdbarch
, pc
, &fs
);
898 /* Calculate the CFA. */
899 switch (fs
.regs
.cfa_how
)
903 int regnum
= dwarf_reg_to_regnum_or_error (gdbarch
, fs
.regs
.cfa_reg
);
905 *regnum_out
= regnum
;
906 if (fs
.armcc_cfa_offsets_reversed
)
907 *offset_out
= -fs
.regs
.cfa_offset
;
909 *offset_out
= fs
.regs
.cfa_offset
;
914 *text_offset_out
= text_offset
;
915 *cfa_start_out
= fs
.regs
.cfa_exp
;
916 *cfa_end_out
= fs
.regs
.cfa_exp
+ fs
.regs
.cfa_exp_len
;
920 internal_error (__FILE__
, __LINE__
, _("Unknown CFA rule."));
925 struct dwarf2_frame_cache
927 /* DWARF Call Frame Address. */
930 /* Set if the return address column was marked as unavailable
931 (required non-collected memory or registers to compute). */
932 int unavailable_retaddr
;
934 /* Set if the return address column was marked as undefined. */
935 int undefined_retaddr
;
937 /* Saved registers, indexed by GDB register number, not by DWARF
939 struct dwarf2_frame_state_reg
*reg
;
941 /* Return address register. */
942 struct dwarf2_frame_state_reg retaddr_reg
;
944 /* Target address size in bytes. */
947 /* The .text offset. */
948 CORE_ADDR text_offset
;
950 /* True if we already checked whether this frame is the bottom frame
951 of a virtual tail call frame chain. */
952 int checked_tailcall_bottom
;
954 /* If not NULL then this frame is the bottom frame of a TAILCALL_FRAME
955 sequence. If NULL then it is a normal case with no TAILCALL_FRAME
956 involved. Non-bottom frames of a virtual tail call frames chain use
957 dwarf2_tailcall_frame_unwind unwinder so this field does not apply for
959 void *tailcall_cache
;
961 /* The number of bytes to subtract from TAILCALL_FRAME frames frame
962 base to get the SP, to simulate the return address pushed on the
964 LONGEST entry_cfa_sp_offset
;
965 int entry_cfa_sp_offset_p
;
968 /* A cleanup that sets a pointer to NULL. */
971 clear_pointer_cleanup (void *arg
)
973 void **ptr
= (void **) arg
;
978 static struct dwarf2_frame_cache
*
979 dwarf2_frame_cache (struct frame_info
*this_frame
, void **this_cache
)
981 struct cleanup
*reset_cache_cleanup
, *old_chain
;
982 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
983 const int num_regs
= gdbarch_num_regs (gdbarch
)
984 + gdbarch_num_pseudo_regs (gdbarch
);
985 struct dwarf2_frame_cache
*cache
;
986 struct dwarf2_frame_state
*fs
;
987 struct dwarf2_fde
*fde
;
989 const gdb_byte
*instr
;
992 return (struct dwarf2_frame_cache
*) *this_cache
;
994 /* Allocate a new cache. */
995 cache
= FRAME_OBSTACK_ZALLOC (struct dwarf2_frame_cache
);
996 cache
->reg
= FRAME_OBSTACK_CALLOC (num_regs
, struct dwarf2_frame_state_reg
);
998 reset_cache_cleanup
= make_cleanup (clear_pointer_cleanup
, this_cache
);
1000 /* Allocate and initialize the frame state. */
1001 fs
= XCNEW (struct dwarf2_frame_state
);
1002 old_chain
= make_cleanup (dwarf2_frame_state_free
, fs
);
1006 Note that if the next frame is never supposed to return (i.e. a call
1007 to abort), the compiler might optimize away the instruction at
1008 its return address. As a result the return address will
1009 point at some random instruction, and the CFI for that
1010 instruction is probably worthless to us. GCC's unwinder solves
1011 this problem by substracting 1 from the return address to get an
1012 address in the middle of a presumed call instruction (or the
1013 instruction in the associated delay slot). This should only be
1014 done for "normal" frames and not for resume-type frames (signal
1015 handlers, sentinel frames, dummy frames). The function
1016 get_frame_address_in_block does just this. It's not clear how
1017 reliable the method is though; there is the potential for the
1018 register state pre-call being different to that on return. */
1019 fs
->pc
= get_frame_address_in_block (this_frame
);
1021 /* Find the correct FDE. */
1022 fde
= dwarf2_frame_find_fde (&fs
->pc
, &cache
->text_offset
);
1023 gdb_assert (fde
!= NULL
);
1025 /* Extract any interesting information from the CIE. */
1026 fs
->data_align
= fde
->cie
->data_alignment_factor
;
1027 fs
->code_align
= fde
->cie
->code_alignment_factor
;
1028 fs
->retaddr_column
= fde
->cie
->return_address_register
;
1029 cache
->addr_size
= fde
->cie
->addr_size
;
1031 /* Check for "quirks" - known bugs in producers. */
1032 dwarf2_frame_find_quirks (fs
, fde
);
1034 /* First decode all the insns in the CIE. */
1035 execute_cfa_program (fde
, fde
->cie
->initial_instructions
,
1036 fde
->cie
->end
, gdbarch
,
1037 get_frame_address_in_block (this_frame
), fs
);
1039 /* Save the initialized register set. */
1040 fs
->initial
= fs
->regs
;
1041 fs
->initial
.reg
= dwarf2_frame_state_copy_regs (&fs
->regs
);
1043 if (get_frame_func_if_available (this_frame
, &entry_pc
))
1045 /* Decode the insns in the FDE up to the entry PC. */
1046 instr
= execute_cfa_program (fde
, fde
->instructions
, fde
->end
, gdbarch
,
1049 if (fs
->regs
.cfa_how
== CFA_REG_OFFSET
1050 && (dwarf_reg_to_regnum (gdbarch
, fs
->regs
.cfa_reg
)
1051 == gdbarch_sp_regnum (gdbarch
)))
1053 cache
->entry_cfa_sp_offset
= fs
->regs
.cfa_offset
;
1054 cache
->entry_cfa_sp_offset_p
= 1;
1058 instr
= fde
->instructions
;
1060 /* Then decode the insns in the FDE up to our target PC. */
1061 execute_cfa_program (fde
, instr
, fde
->end
, gdbarch
,
1062 get_frame_address_in_block (this_frame
), fs
);
1066 /* Calculate the CFA. */
1067 switch (fs
->regs
.cfa_how
)
1069 case CFA_REG_OFFSET
:
1070 cache
->cfa
= read_addr_from_reg (this_frame
, fs
->regs
.cfa_reg
);
1071 if (fs
->armcc_cfa_offsets_reversed
)
1072 cache
->cfa
-= fs
->regs
.cfa_offset
;
1074 cache
->cfa
+= fs
->regs
.cfa_offset
;
1079 execute_stack_op (fs
->regs
.cfa_exp
, fs
->regs
.cfa_exp_len
,
1080 cache
->addr_size
, cache
->text_offset
,
1085 internal_error (__FILE__
, __LINE__
, _("Unknown CFA rule."));
1088 CATCH (ex
, RETURN_MASK_ERROR
)
1090 if (ex
.error
== NOT_AVAILABLE_ERROR
)
1092 cache
->unavailable_retaddr
= 1;
1093 do_cleanups (old_chain
);
1094 discard_cleanups (reset_cache_cleanup
);
1098 throw_exception (ex
);
1102 /* Initialize the register state. */
1106 for (regnum
= 0; regnum
< num_regs
; regnum
++)
1107 dwarf2_frame_init_reg (gdbarch
, regnum
, &cache
->reg
[regnum
], this_frame
);
1110 /* Go through the DWARF2 CFI generated table and save its register
1111 location information in the cache. Note that we don't skip the
1112 return address column; it's perfectly all right for it to
1113 correspond to a real register. */
1115 int column
; /* CFI speak for "register number". */
1117 for (column
= 0; column
< fs
->regs
.num_regs
; column
++)
1119 /* Use the GDB register number as the destination index. */
1120 int regnum
= dwarf_reg_to_regnum (gdbarch
, column
);
1122 /* Protect against a target returning a bad register. */
1123 if (regnum
< 0 || regnum
>= num_regs
)
1126 /* NOTE: cagney/2003-09-05: CFI should specify the disposition
1127 of all debug info registers. If it doesn't, complain (but
1128 not too loudly). It turns out that GCC assumes that an
1129 unspecified register implies "same value" when CFI (draft
1130 7) specifies nothing at all. Such a register could equally
1131 be interpreted as "undefined". Also note that this check
1132 isn't sufficient; it only checks that all registers in the
1133 range [0 .. max column] are specified, and won't detect
1134 problems when a debug info register falls outside of the
1135 table. We need a way of iterating through all the valid
1136 DWARF2 register numbers. */
1137 if (fs
->regs
.reg
[column
].how
== DWARF2_FRAME_REG_UNSPECIFIED
)
1139 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_UNSPECIFIED
)
1140 complaint (&symfile_complaints
, _("\
1141 incomplete CFI data; unspecified registers (e.g., %s) at %s"),
1142 gdbarch_register_name (gdbarch
, regnum
),
1143 paddress (gdbarch
, fs
->pc
));
1146 cache
->reg
[regnum
] = fs
->regs
.reg
[column
];
1150 /* Eliminate any DWARF2_FRAME_REG_RA rules, and save the information
1151 we need for evaluating DWARF2_FRAME_REG_RA_OFFSET rules. */
1155 for (regnum
= 0; regnum
< num_regs
; regnum
++)
1157 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA
1158 || cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA_OFFSET
)
1160 struct dwarf2_frame_state_reg
*retaddr_reg
=
1161 &fs
->regs
.reg
[fs
->retaddr_column
];
1163 /* It seems rather bizarre to specify an "empty" column as
1164 the return adress column. However, this is exactly
1165 what GCC does on some targets. It turns out that GCC
1166 assumes that the return address can be found in the
1167 register corresponding to the return address column.
1168 Incidentally, that's how we should treat a return
1169 address column specifying "same value" too. */
1170 if (fs
->retaddr_column
< fs
->regs
.num_regs
1171 && retaddr_reg
->how
!= DWARF2_FRAME_REG_UNSPECIFIED
1172 && retaddr_reg
->how
!= DWARF2_FRAME_REG_SAME_VALUE
)
1174 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA
)
1175 cache
->reg
[regnum
] = *retaddr_reg
;
1177 cache
->retaddr_reg
= *retaddr_reg
;
1181 if (cache
->reg
[regnum
].how
== DWARF2_FRAME_REG_RA
)
1183 cache
->reg
[regnum
].loc
.reg
= fs
->retaddr_column
;
1184 cache
->reg
[regnum
].how
= DWARF2_FRAME_REG_SAVED_REG
;
1188 cache
->retaddr_reg
.loc
.reg
= fs
->retaddr_column
;
1189 cache
->retaddr_reg
.how
= DWARF2_FRAME_REG_SAVED_REG
;
1196 if (fs
->retaddr_column
< fs
->regs
.num_regs
1197 && fs
->regs
.reg
[fs
->retaddr_column
].how
== DWARF2_FRAME_REG_UNDEFINED
)
1198 cache
->undefined_retaddr
= 1;
1200 do_cleanups (old_chain
);
1201 discard_cleanups (reset_cache_cleanup
);
1205 static enum unwind_stop_reason
1206 dwarf2_frame_unwind_stop_reason (struct frame_info
*this_frame
,
1209 struct dwarf2_frame_cache
*cache
1210 = dwarf2_frame_cache (this_frame
, this_cache
);
1212 if (cache
->unavailable_retaddr
)
1213 return UNWIND_UNAVAILABLE
;
1215 if (cache
->undefined_retaddr
)
1216 return UNWIND_OUTERMOST
;
1218 return UNWIND_NO_REASON
;
1222 dwarf2_frame_this_id (struct frame_info
*this_frame
, void **this_cache
,
1223 struct frame_id
*this_id
)
1225 struct dwarf2_frame_cache
*cache
=
1226 dwarf2_frame_cache (this_frame
, this_cache
);
1228 if (cache
->unavailable_retaddr
)
1229 (*this_id
) = frame_id_build_unavailable_stack (get_frame_func (this_frame
));
1230 else if (cache
->undefined_retaddr
)
1233 (*this_id
) = frame_id_build (cache
->cfa
, get_frame_func (this_frame
));
1236 static struct value
*
1237 dwarf2_frame_prev_register (struct frame_info
*this_frame
, void **this_cache
,
1240 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
1241 struct dwarf2_frame_cache
*cache
=
1242 dwarf2_frame_cache (this_frame
, this_cache
);
1246 /* Check whether THIS_FRAME is the bottom frame of a virtual tail
1247 call frame chain. */
1248 if (!cache
->checked_tailcall_bottom
)
1250 cache
->checked_tailcall_bottom
= 1;
1251 dwarf2_tailcall_sniffer_first (this_frame
, &cache
->tailcall_cache
,
1252 (cache
->entry_cfa_sp_offset_p
1253 ? &cache
->entry_cfa_sp_offset
: NULL
));
1256 /* Non-bottom frames of a virtual tail call frames chain use
1257 dwarf2_tailcall_frame_unwind unwinder so this code does not apply for
1258 them. If dwarf2_tailcall_prev_register_first does not have specific value
1259 unwind the register, tail call frames are assumed to have the register set
1260 of the top caller. */
1261 if (cache
->tailcall_cache
)
1265 val
= dwarf2_tailcall_prev_register_first (this_frame
,
1266 &cache
->tailcall_cache
,
1272 switch (cache
->reg
[regnum
].how
)
1274 case DWARF2_FRAME_REG_UNDEFINED
:
1275 /* If CFI explicitly specified that the value isn't defined,
1276 mark it as optimized away; the value isn't available. */
1277 return frame_unwind_got_optimized (this_frame
, regnum
);
1279 case DWARF2_FRAME_REG_SAVED_OFFSET
:
1280 addr
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
1281 return frame_unwind_got_memory (this_frame
, regnum
, addr
);
1283 case DWARF2_FRAME_REG_SAVED_REG
:
1284 realnum
= dwarf_reg_to_regnum_or_error
1285 (gdbarch
, cache
->reg
[regnum
].loc
.reg
);
1286 return frame_unwind_got_register (this_frame
, regnum
, realnum
);
1288 case DWARF2_FRAME_REG_SAVED_EXP
:
1289 addr
= execute_stack_op (cache
->reg
[regnum
].loc
.exp
,
1290 cache
->reg
[regnum
].exp_len
,
1291 cache
->addr_size
, cache
->text_offset
,
1292 this_frame
, cache
->cfa
, 1);
1293 return frame_unwind_got_memory (this_frame
, regnum
, addr
);
1295 case DWARF2_FRAME_REG_SAVED_VAL_OFFSET
:
1296 addr
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
1297 return frame_unwind_got_constant (this_frame
, regnum
, addr
);
1299 case DWARF2_FRAME_REG_SAVED_VAL_EXP
:
1300 addr
= execute_stack_op (cache
->reg
[regnum
].loc
.exp
,
1301 cache
->reg
[regnum
].exp_len
,
1302 cache
->addr_size
, cache
->text_offset
,
1303 this_frame
, cache
->cfa
, 1);
1304 return frame_unwind_got_constant (this_frame
, regnum
, addr
);
1306 case DWARF2_FRAME_REG_UNSPECIFIED
:
1307 /* GCC, in its infinite wisdom decided to not provide unwind
1308 information for registers that are "same value". Since
1309 DWARF2 (3 draft 7) doesn't define such behavior, said
1310 registers are actually undefined (which is different to CFI
1311 "undefined"). Code above issues a complaint about this.
1312 Here just fudge the books, assume GCC, and that the value is
1313 more inner on the stack. */
1314 return frame_unwind_got_register (this_frame
, regnum
, regnum
);
1316 case DWARF2_FRAME_REG_SAME_VALUE
:
1317 return frame_unwind_got_register (this_frame
, regnum
, regnum
);
1319 case DWARF2_FRAME_REG_CFA
:
1320 return frame_unwind_got_address (this_frame
, regnum
, cache
->cfa
);
1322 case DWARF2_FRAME_REG_CFA_OFFSET
:
1323 addr
= cache
->cfa
+ cache
->reg
[regnum
].loc
.offset
;
1324 return frame_unwind_got_address (this_frame
, regnum
, addr
);
1326 case DWARF2_FRAME_REG_RA_OFFSET
:
1327 addr
= cache
->reg
[regnum
].loc
.offset
;
1328 regnum
= dwarf_reg_to_regnum_or_error
1329 (gdbarch
, cache
->retaddr_reg
.loc
.reg
);
1330 addr
+= get_frame_register_unsigned (this_frame
, regnum
);
1331 return frame_unwind_got_address (this_frame
, regnum
, addr
);
1333 case DWARF2_FRAME_REG_FN
:
1334 return cache
->reg
[regnum
].loc
.fn (this_frame
, this_cache
, regnum
);
1337 internal_error (__FILE__
, __LINE__
, _("Unknown register rule."));
1341 /* Proxy for tailcall_frame_dealloc_cache for bottom frame of a virtual tail
1342 call frames chain. */
1345 dwarf2_frame_dealloc_cache (struct frame_info
*self
, void *this_cache
)
1347 struct dwarf2_frame_cache
*cache
= dwarf2_frame_cache (self
, &this_cache
);
1349 if (cache
->tailcall_cache
)
1350 dwarf2_tailcall_frame_unwind
.dealloc_cache (self
, cache
->tailcall_cache
);
1354 dwarf2_frame_sniffer (const struct frame_unwind
*self
,
1355 struct frame_info
*this_frame
, void **this_cache
)
1357 /* Grab an address that is guarenteed to reside somewhere within the
1358 function. get_frame_pc(), with a no-return next function, can
1359 end up returning something past the end of this function's body.
1360 If the frame we're sniffing for is a signal frame whose start
1361 address is placed on the stack by the OS, its FDE must
1362 extend one byte before its start address or we could potentially
1363 select the FDE of the previous function. */
1364 CORE_ADDR block_addr
= get_frame_address_in_block (this_frame
);
1365 struct dwarf2_fde
*fde
= dwarf2_frame_find_fde (&block_addr
, NULL
);
1370 /* On some targets, signal trampolines may have unwind information.
1371 We need to recognize them so that we set the frame type
1374 if (fde
->cie
->signal_frame
1375 || dwarf2_frame_signal_frame_p (get_frame_arch (this_frame
),
1377 return self
->type
== SIGTRAMP_FRAME
;
1379 if (self
->type
!= NORMAL_FRAME
)
1385 static const struct frame_unwind dwarf2_frame_unwind
=
1388 dwarf2_frame_unwind_stop_reason
,
1389 dwarf2_frame_this_id
,
1390 dwarf2_frame_prev_register
,
1392 dwarf2_frame_sniffer
,
1393 dwarf2_frame_dealloc_cache
1396 static const struct frame_unwind dwarf2_signal_frame_unwind
=
1399 dwarf2_frame_unwind_stop_reason
,
1400 dwarf2_frame_this_id
,
1401 dwarf2_frame_prev_register
,
1403 dwarf2_frame_sniffer
,
1405 /* TAILCALL_CACHE can never be in such frame to need dealloc_cache. */
1409 /* Append the DWARF-2 frame unwinders to GDBARCH's list. */
1412 dwarf2_append_unwinders (struct gdbarch
*gdbarch
)
1414 /* TAILCALL_FRAME must be first to find the record by
1415 dwarf2_tailcall_sniffer_first. */
1416 frame_unwind_append_unwinder (gdbarch
, &dwarf2_tailcall_frame_unwind
);
1418 frame_unwind_append_unwinder (gdbarch
, &dwarf2_frame_unwind
);
1419 frame_unwind_append_unwinder (gdbarch
, &dwarf2_signal_frame_unwind
);
1423 /* There is no explicitly defined relationship between the CFA and the
1424 location of frame's local variables and arguments/parameters.
1425 Therefore, frame base methods on this page should probably only be
1426 used as a last resort, just to avoid printing total garbage as a
1427 response to the "info frame" command. */
1430 dwarf2_frame_base_address (struct frame_info
*this_frame
, void **this_cache
)
1432 struct dwarf2_frame_cache
*cache
=
1433 dwarf2_frame_cache (this_frame
, this_cache
);
1438 static const struct frame_base dwarf2_frame_base
=
1440 &dwarf2_frame_unwind
,
1441 dwarf2_frame_base_address
,
1442 dwarf2_frame_base_address
,
1443 dwarf2_frame_base_address
1446 const struct frame_base
*
1447 dwarf2_frame_base_sniffer (struct frame_info
*this_frame
)
1449 CORE_ADDR block_addr
= get_frame_address_in_block (this_frame
);
1451 if (dwarf2_frame_find_fde (&block_addr
, NULL
))
1452 return &dwarf2_frame_base
;
1457 /* Compute the CFA for THIS_FRAME, but only if THIS_FRAME came from
1458 the DWARF unwinder. This is used to implement
1459 DW_OP_call_frame_cfa. */
1462 dwarf2_frame_cfa (struct frame_info
*this_frame
)
1464 if (frame_unwinder_is (this_frame
, &record_btrace_tailcall_frame_unwind
)
1465 || frame_unwinder_is (this_frame
, &record_btrace_frame_unwind
))
1466 throw_error (NOT_AVAILABLE_ERROR
,
1467 _("cfa not available for record btrace target"));
1469 while (get_frame_type (this_frame
) == INLINE_FRAME
)
1470 this_frame
= get_prev_frame (this_frame
);
1471 if (get_frame_unwind_stop_reason (this_frame
) == UNWIND_UNAVAILABLE
)
1472 throw_error (NOT_AVAILABLE_ERROR
,
1473 _("can't compute CFA for this frame: "
1474 "required registers or memory are unavailable"));
1476 if (get_frame_id (this_frame
).stack_status
!= FID_STACK_VALID
)
1477 throw_error (NOT_AVAILABLE_ERROR
,
1478 _("can't compute CFA for this frame: "
1479 "frame base not available"));
1481 return get_frame_base (this_frame
);
1484 const struct objfile_data
*dwarf2_frame_objfile_data
;
1487 read_1_byte (bfd
*abfd
, const gdb_byte
*buf
)
1489 return bfd_get_8 (abfd
, buf
);
1493 read_4_bytes (bfd
*abfd
, const gdb_byte
*buf
)
1495 return bfd_get_32 (abfd
, buf
);
1499 read_8_bytes (bfd
*abfd
, const gdb_byte
*buf
)
1501 return bfd_get_64 (abfd
, buf
);
1505 read_initial_length (bfd
*abfd
, const gdb_byte
*buf
,
1506 unsigned int *bytes_read_ptr
)
1510 result
= bfd_get_32 (abfd
, buf
);
1511 if (result
== 0xffffffff)
1513 result
= bfd_get_64 (abfd
, buf
+ 4);
1514 *bytes_read_ptr
= 12;
1517 *bytes_read_ptr
= 4;
1523 /* Pointer encoding helper functions. */
1525 /* GCC supports exception handling based on DWARF2 CFI. However, for
1526 technical reasons, it encodes addresses in its FDE's in a different
1527 way. Several "pointer encodings" are supported. The encoding
1528 that's used for a particular FDE is determined by the 'R'
1529 augmentation in the associated CIE. The argument of this
1530 augmentation is a single byte.
1532 The address can be encoded as 2 bytes, 4 bytes, 8 bytes, or as a
1533 LEB128. This is encoded in bits 0, 1 and 2. Bit 3 encodes whether
1534 the address is signed or unsigned. Bits 4, 5 and 6 encode how the
1535 address should be interpreted (absolute, relative to the current
1536 position in the FDE, ...). Bit 7, indicates that the address
1537 should be dereferenced. */
1540 encoding_for_size (unsigned int size
)
1545 return DW_EH_PE_udata2
;
1547 return DW_EH_PE_udata4
;
1549 return DW_EH_PE_udata8
;
1551 internal_error (__FILE__
, __LINE__
, _("Unsupported address size"));
1556 read_encoded_value (struct comp_unit
*unit
, gdb_byte encoding
,
1557 int ptr_len
, const gdb_byte
*buf
,
1558 unsigned int *bytes_read_ptr
,
1559 CORE_ADDR func_base
)
1564 /* GCC currently doesn't generate DW_EH_PE_indirect encodings for
1566 if (encoding
& DW_EH_PE_indirect
)
1567 internal_error (__FILE__
, __LINE__
,
1568 _("Unsupported encoding: DW_EH_PE_indirect"));
1570 *bytes_read_ptr
= 0;
1572 switch (encoding
& 0x70)
1574 case DW_EH_PE_absptr
:
1577 case DW_EH_PE_pcrel
:
1578 base
= bfd_get_section_vma (unit
->abfd
, unit
->dwarf_frame_section
);
1579 base
+= (buf
- unit
->dwarf_frame_buffer
);
1581 case DW_EH_PE_datarel
:
1584 case DW_EH_PE_textrel
:
1587 case DW_EH_PE_funcrel
:
1590 case DW_EH_PE_aligned
:
1592 offset
= buf
- unit
->dwarf_frame_buffer
;
1593 if ((offset
% ptr_len
) != 0)
1595 *bytes_read_ptr
= ptr_len
- (offset
% ptr_len
);
1596 buf
+= *bytes_read_ptr
;
1600 internal_error (__FILE__
, __LINE__
,
1601 _("Invalid or unsupported encoding"));
1604 if ((encoding
& 0x07) == 0x00)
1606 encoding
|= encoding_for_size (ptr_len
);
1607 if (bfd_get_sign_extend_vma (unit
->abfd
))
1608 encoding
|= DW_EH_PE_signed
;
1611 switch (encoding
& 0x0f)
1613 case DW_EH_PE_uleb128
:
1616 const gdb_byte
*end_buf
= buf
+ (sizeof (value
) + 1) * 8 / 7;
1618 *bytes_read_ptr
+= safe_read_uleb128 (buf
, end_buf
, &value
) - buf
;
1619 return base
+ value
;
1621 case DW_EH_PE_udata2
:
1622 *bytes_read_ptr
+= 2;
1623 return (base
+ bfd_get_16 (unit
->abfd
, (bfd_byte
*) buf
));
1624 case DW_EH_PE_udata4
:
1625 *bytes_read_ptr
+= 4;
1626 return (base
+ bfd_get_32 (unit
->abfd
, (bfd_byte
*) buf
));
1627 case DW_EH_PE_udata8
:
1628 *bytes_read_ptr
+= 8;
1629 return (base
+ bfd_get_64 (unit
->abfd
, (bfd_byte
*) buf
));
1630 case DW_EH_PE_sleb128
:
1633 const gdb_byte
*end_buf
= buf
+ (sizeof (value
) + 1) * 8 / 7;
1635 *bytes_read_ptr
+= safe_read_sleb128 (buf
, end_buf
, &value
) - buf
;
1636 return base
+ value
;
1638 case DW_EH_PE_sdata2
:
1639 *bytes_read_ptr
+= 2;
1640 return (base
+ bfd_get_signed_16 (unit
->abfd
, (bfd_byte
*) buf
));
1641 case DW_EH_PE_sdata4
:
1642 *bytes_read_ptr
+= 4;
1643 return (base
+ bfd_get_signed_32 (unit
->abfd
, (bfd_byte
*) buf
));
1644 case DW_EH_PE_sdata8
:
1645 *bytes_read_ptr
+= 8;
1646 return (base
+ bfd_get_signed_64 (unit
->abfd
, (bfd_byte
*) buf
));
1648 internal_error (__FILE__
, __LINE__
,
1649 _("Invalid or unsupported encoding"));
1655 bsearch_cie_cmp (const void *key
, const void *element
)
1657 ULONGEST cie_pointer
= *(ULONGEST
*) key
;
1658 struct dwarf2_cie
*cie
= *(struct dwarf2_cie
**) element
;
1660 if (cie_pointer
== cie
->cie_pointer
)
1663 return (cie_pointer
< cie
->cie_pointer
) ? -1 : 1;
1666 /* Find CIE with the given CIE_POINTER in CIE_TABLE. */
1667 static struct dwarf2_cie
*
1668 find_cie (struct dwarf2_cie_table
*cie_table
, ULONGEST cie_pointer
)
1670 struct dwarf2_cie
**p_cie
;
1672 /* The C standard (ISO/IEC 9899:TC2) requires the BASE argument to
1673 bsearch be non-NULL. */
1674 if (cie_table
->entries
== NULL
)
1676 gdb_assert (cie_table
->num_entries
== 0);
1680 p_cie
= ((struct dwarf2_cie
**)
1681 bsearch (&cie_pointer
, cie_table
->entries
, cie_table
->num_entries
,
1682 sizeof (cie_table
->entries
[0]), bsearch_cie_cmp
));
1688 /* Add a pointer to new CIE to the CIE_TABLE, allocating space for it. */
1690 add_cie (struct dwarf2_cie_table
*cie_table
, struct dwarf2_cie
*cie
)
1692 const int n
= cie_table
->num_entries
;
1695 || cie_table
->entries
[n
- 1]->cie_pointer
< cie
->cie_pointer
);
1698 = XRESIZEVEC (struct dwarf2_cie
*, cie_table
->entries
, n
+ 1);
1699 cie_table
->entries
[n
] = cie
;
1700 cie_table
->num_entries
= n
+ 1;
1704 bsearch_fde_cmp (const void *key
, const void *element
)
1706 CORE_ADDR seek_pc
= *(CORE_ADDR
*) key
;
1707 struct dwarf2_fde
*fde
= *(struct dwarf2_fde
**) element
;
1709 if (seek_pc
< fde
->initial_location
)
1711 if (seek_pc
< fde
->initial_location
+ fde
->address_range
)
1716 /* Find the FDE for *PC. Return a pointer to the FDE, and store the
1717 inital location associated with it into *PC. */
1719 static struct dwarf2_fde
*
1720 dwarf2_frame_find_fde (CORE_ADDR
*pc
, CORE_ADDR
*out_offset
)
1722 struct objfile
*objfile
;
1724 ALL_OBJFILES (objfile
)
1726 struct dwarf2_fde_table
*fde_table
;
1727 struct dwarf2_fde
**p_fde
;
1731 fde_table
= ((struct dwarf2_fde_table
*)
1732 objfile_data (objfile
, dwarf2_frame_objfile_data
));
1733 if (fde_table
== NULL
)
1735 dwarf2_build_frame_info (objfile
);
1736 fde_table
= ((struct dwarf2_fde_table
*)
1737 objfile_data (objfile
, dwarf2_frame_objfile_data
));
1739 gdb_assert (fde_table
!= NULL
);
1741 if (fde_table
->num_entries
== 0)
1744 gdb_assert (objfile
->section_offsets
);
1745 offset
= ANOFFSET (objfile
->section_offsets
, SECT_OFF_TEXT (objfile
));
1747 gdb_assert (fde_table
->num_entries
> 0);
1748 if (*pc
< offset
+ fde_table
->entries
[0]->initial_location
)
1751 seek_pc
= *pc
- offset
;
1752 p_fde
= ((struct dwarf2_fde
**)
1753 bsearch (&seek_pc
, fde_table
->entries
, fde_table
->num_entries
,
1754 sizeof (fde_table
->entries
[0]), bsearch_fde_cmp
));
1757 *pc
= (*p_fde
)->initial_location
+ offset
;
1759 *out_offset
= offset
;
1766 /* Add a pointer to new FDE to the FDE_TABLE, allocating space for it. */
1768 add_fde (struct dwarf2_fde_table
*fde_table
, struct dwarf2_fde
*fde
)
1770 if (fde
->address_range
== 0)
1771 /* Discard useless FDEs. */
1774 fde_table
->num_entries
+= 1;
1775 fde_table
->entries
= XRESIZEVEC (struct dwarf2_fde
*, fde_table
->entries
,
1776 fde_table
->num_entries
);
1777 fde_table
->entries
[fde_table
->num_entries
- 1] = fde
;
1780 #define DW64_CIE_ID 0xffffffffffffffffULL
1782 /* Defines the type of eh_frames that are expected to be decoded: CIE, FDE
1787 EH_CIE_TYPE_ID
= 1 << 0,
1788 EH_FDE_TYPE_ID
= 1 << 1,
1789 EH_CIE_OR_FDE_TYPE_ID
= EH_CIE_TYPE_ID
| EH_FDE_TYPE_ID
1792 static const gdb_byte
*decode_frame_entry (struct comp_unit
*unit
,
1793 const gdb_byte
*start
,
1795 struct dwarf2_cie_table
*cie_table
,
1796 struct dwarf2_fde_table
*fde_table
,
1797 enum eh_frame_type entry_type
);
1799 /* Decode the next CIE or FDE, entry_type specifies the expected type.
1800 Return NULL if invalid input, otherwise the next byte to be processed. */
1802 static const gdb_byte
*
1803 decode_frame_entry_1 (struct comp_unit
*unit
, const gdb_byte
*start
,
1805 struct dwarf2_cie_table
*cie_table
,
1806 struct dwarf2_fde_table
*fde_table
,
1807 enum eh_frame_type entry_type
)
1809 struct gdbarch
*gdbarch
= get_objfile_arch (unit
->objfile
);
1810 const gdb_byte
*buf
, *end
;
1812 unsigned int bytes_read
;
1815 ULONGEST cie_pointer
;
1820 length
= read_initial_length (unit
->abfd
, buf
, &bytes_read
);
1824 /* Are we still within the section? */
1825 if (end
> unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
)
1831 /* Distinguish between 32 and 64-bit encoded frame info. */
1832 dwarf64_p
= (bytes_read
== 12);
1834 /* In a .eh_frame section, zero is used to distinguish CIEs from FDEs. */
1838 cie_id
= DW64_CIE_ID
;
1844 cie_pointer
= read_8_bytes (unit
->abfd
, buf
);
1849 cie_pointer
= read_4_bytes (unit
->abfd
, buf
);
1853 if (cie_pointer
== cie_id
)
1855 /* This is a CIE. */
1856 struct dwarf2_cie
*cie
;
1858 unsigned int cie_version
;
1860 /* Check that a CIE was expected. */
1861 if ((entry_type
& EH_CIE_TYPE_ID
) == 0)
1862 error (_("Found a CIE when not expecting it."));
1864 /* Record the offset into the .debug_frame section of this CIE. */
1865 cie_pointer
= start
- unit
->dwarf_frame_buffer
;
1867 /* Check whether we've already read it. */
1868 if (find_cie (cie_table
, cie_pointer
))
1871 cie
= XOBNEW (&unit
->objfile
->objfile_obstack
, struct dwarf2_cie
);
1872 cie
->initial_instructions
= NULL
;
1873 cie
->cie_pointer
= cie_pointer
;
1875 /* The encoding for FDE's in a normal .debug_frame section
1876 depends on the target address size. */
1877 cie
->encoding
= DW_EH_PE_absptr
;
1879 /* We'll determine the final value later, but we need to
1880 initialize it conservatively. */
1881 cie
->signal_frame
= 0;
1883 /* Check version number. */
1884 cie_version
= read_1_byte (unit
->abfd
, buf
);
1885 if (cie_version
!= 1 && cie_version
!= 3 && cie_version
!= 4)
1887 cie
->version
= cie_version
;
1890 /* Interpret the interesting bits of the augmentation. */
1891 cie
->augmentation
= augmentation
= (char *) buf
;
1892 buf
+= (strlen (augmentation
) + 1);
1894 /* Ignore armcc augmentations. We only use them for quirks,
1895 and that doesn't happen until later. */
1896 if (startswith (augmentation
, "armcc"))
1897 augmentation
+= strlen (augmentation
);
1899 /* The GCC 2.x "eh" augmentation has a pointer immediately
1900 following the augmentation string, so it must be handled
1902 if (augmentation
[0] == 'e' && augmentation
[1] == 'h')
1905 buf
+= gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
;
1909 if (cie
->version
>= 4)
1911 /* FIXME: check that this is the same as from the CU header. */
1912 cie
->addr_size
= read_1_byte (unit
->abfd
, buf
);
1914 cie
->segment_size
= read_1_byte (unit
->abfd
, buf
);
1919 cie
->addr_size
= gdbarch_dwarf2_addr_size (gdbarch
);
1920 cie
->segment_size
= 0;
1922 /* Address values in .eh_frame sections are defined to have the
1923 target's pointer size. Watchout: This breaks frame info for
1924 targets with pointer size < address size, unless a .debug_frame
1925 section exists as well. */
1927 cie
->ptr_size
= gdbarch_ptr_bit (gdbarch
) / TARGET_CHAR_BIT
;
1929 cie
->ptr_size
= cie
->addr_size
;
1931 buf
= gdb_read_uleb128 (buf
, end
, &uleb128
);
1934 cie
->code_alignment_factor
= uleb128
;
1936 buf
= gdb_read_sleb128 (buf
, end
, &sleb128
);
1939 cie
->data_alignment_factor
= sleb128
;
1941 if (cie_version
== 1)
1943 cie
->return_address_register
= read_1_byte (unit
->abfd
, buf
);
1948 buf
= gdb_read_uleb128 (buf
, end
, &uleb128
);
1951 cie
->return_address_register
= uleb128
;
1954 cie
->return_address_register
1955 = dwarf2_frame_adjust_regnum (gdbarch
,
1956 cie
->return_address_register
,
1959 cie
->saw_z_augmentation
= (*augmentation
== 'z');
1960 if (cie
->saw_z_augmentation
)
1964 buf
= gdb_read_uleb128 (buf
, end
, &length
);
1967 cie
->initial_instructions
= buf
+ length
;
1971 while (*augmentation
)
1973 /* "L" indicates a byte showing how the LSDA pointer is encoded. */
1974 if (*augmentation
== 'L')
1981 /* "R" indicates a byte indicating how FDE addresses are encoded. */
1982 else if (*augmentation
== 'R')
1984 cie
->encoding
= *buf
++;
1988 /* "P" indicates a personality routine in the CIE augmentation. */
1989 else if (*augmentation
== 'P')
1991 /* Skip. Avoid indirection since we throw away the result. */
1992 gdb_byte encoding
= (*buf
++) & ~DW_EH_PE_indirect
;
1993 read_encoded_value (unit
, encoding
, cie
->ptr_size
,
1994 buf
, &bytes_read
, 0);
1999 /* "S" indicates a signal frame, such that the return
2000 address must not be decremented to locate the call frame
2001 info for the previous frame; it might even be the first
2002 instruction of a function, so decrementing it would take
2003 us to a different function. */
2004 else if (*augmentation
== 'S')
2006 cie
->signal_frame
= 1;
2010 /* Otherwise we have an unknown augmentation. Assume that either
2011 there is no augmentation data, or we saw a 'z' prefix. */
2014 if (cie
->initial_instructions
)
2015 buf
= cie
->initial_instructions
;
2020 cie
->initial_instructions
= buf
;
2024 add_cie (cie_table
, cie
);
2028 /* This is a FDE. */
2029 struct dwarf2_fde
*fde
;
2032 /* Check that an FDE was expected. */
2033 if ((entry_type
& EH_FDE_TYPE_ID
) == 0)
2034 error (_("Found an FDE when not expecting it."));
2036 /* In an .eh_frame section, the CIE pointer is the delta between the
2037 address within the FDE where the CIE pointer is stored and the
2038 address of the CIE. Convert it to an offset into the .eh_frame
2042 cie_pointer
= buf
- unit
->dwarf_frame_buffer
- cie_pointer
;
2043 cie_pointer
-= (dwarf64_p
? 8 : 4);
2046 /* In either case, validate the result is still within the section. */
2047 if (cie_pointer
>= unit
->dwarf_frame_size
)
2050 fde
= XOBNEW (&unit
->objfile
->objfile_obstack
, struct dwarf2_fde
);
2051 fde
->cie
= find_cie (cie_table
, cie_pointer
);
2052 if (fde
->cie
== NULL
)
2054 decode_frame_entry (unit
, unit
->dwarf_frame_buffer
+ cie_pointer
,
2055 eh_frame_p
, cie_table
, fde_table
,
2057 fde
->cie
= find_cie (cie_table
, cie_pointer
);
2060 gdb_assert (fde
->cie
!= NULL
);
2062 addr
= read_encoded_value (unit
, fde
->cie
->encoding
, fde
->cie
->ptr_size
,
2063 buf
, &bytes_read
, 0);
2064 fde
->initial_location
= gdbarch_adjust_dwarf2_addr (gdbarch
, addr
);
2067 fde
->address_range
=
2068 read_encoded_value (unit
, fde
->cie
->encoding
& 0x0f,
2069 fde
->cie
->ptr_size
, buf
, &bytes_read
, 0);
2070 addr
= gdbarch_adjust_dwarf2_addr (gdbarch
, addr
+ fde
->address_range
);
2071 fde
->address_range
= addr
- fde
->initial_location
;
2074 /* A 'z' augmentation in the CIE implies the presence of an
2075 augmentation field in the FDE as well. The only thing known
2076 to be in here at present is the LSDA entry for EH. So we
2077 can skip the whole thing. */
2078 if (fde
->cie
->saw_z_augmentation
)
2082 buf
= gdb_read_uleb128 (buf
, end
, &length
);
2090 fde
->instructions
= buf
;
2093 fde
->eh_frame_p
= eh_frame_p
;
2095 add_fde (fde_table
, fde
);
2101 /* Read a CIE or FDE in BUF and decode it. Entry_type specifies whether we
2102 expect an FDE or a CIE. */
2104 static const gdb_byte
*
2105 decode_frame_entry (struct comp_unit
*unit
, const gdb_byte
*start
,
2107 struct dwarf2_cie_table
*cie_table
,
2108 struct dwarf2_fde_table
*fde_table
,
2109 enum eh_frame_type entry_type
)
2111 enum { NONE
, ALIGN4
, ALIGN8
, FAIL
} workaround
= NONE
;
2112 const gdb_byte
*ret
;
2113 ptrdiff_t start_offset
;
2117 ret
= decode_frame_entry_1 (unit
, start
, eh_frame_p
,
2118 cie_table
, fde_table
, entry_type
);
2122 /* We have corrupt input data of some form. */
2124 /* ??? Try, weakly, to work around compiler/assembler/linker bugs
2125 and mismatches wrt padding and alignment of debug sections. */
2126 /* Note that there is no requirement in the standard for any
2127 alignment at all in the frame unwind sections. Testing for
2128 alignment before trying to interpret data would be incorrect.
2130 However, GCC traditionally arranged for frame sections to be
2131 sized such that the FDE length and CIE fields happen to be
2132 aligned (in theory, for performance). This, unfortunately,
2133 was done with .align directives, which had the side effect of
2134 forcing the section to be aligned by the linker.
2136 This becomes a problem when you have some other producer that
2137 creates frame sections that are not as strictly aligned. That
2138 produces a hole in the frame info that gets filled by the
2141 The GCC behaviour is arguably a bug, but it's effectively now
2142 part of the ABI, so we're now stuck with it, at least at the
2143 object file level. A smart linker may decide, in the process
2144 of compressing duplicate CIE information, that it can rewrite
2145 the entire output section without this extra padding. */
2147 start_offset
= start
- unit
->dwarf_frame_buffer
;
2148 if (workaround
< ALIGN4
&& (start_offset
& 3) != 0)
2150 start
+= 4 - (start_offset
& 3);
2151 workaround
= ALIGN4
;
2154 if (workaround
< ALIGN8
&& (start_offset
& 7) != 0)
2156 start
+= 8 - (start_offset
& 7);
2157 workaround
= ALIGN8
;
2161 /* Nothing left to try. Arrange to return as if we've consumed
2162 the entire input section. Hopefully we'll get valid info from
2163 the other of .debug_frame/.eh_frame. */
2165 ret
= unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
;
2175 complaint (&symfile_complaints
, _("\
2176 Corrupt data in %s:%s; align 4 workaround apparently succeeded"),
2177 unit
->dwarf_frame_section
->owner
->filename
,
2178 unit
->dwarf_frame_section
->name
);
2182 complaint (&symfile_complaints
, _("\
2183 Corrupt data in %s:%s; align 8 workaround apparently succeeded"),
2184 unit
->dwarf_frame_section
->owner
->filename
,
2185 unit
->dwarf_frame_section
->name
);
2189 complaint (&symfile_complaints
,
2190 _("Corrupt data in %s:%s"),
2191 unit
->dwarf_frame_section
->owner
->filename
,
2192 unit
->dwarf_frame_section
->name
);
2200 qsort_fde_cmp (const void *a
, const void *b
)
2202 struct dwarf2_fde
*aa
= *(struct dwarf2_fde
**)a
;
2203 struct dwarf2_fde
*bb
= *(struct dwarf2_fde
**)b
;
2205 if (aa
->initial_location
== bb
->initial_location
)
2207 if (aa
->address_range
!= bb
->address_range
2208 && aa
->eh_frame_p
== 0 && bb
->eh_frame_p
== 0)
2209 /* Linker bug, e.g. gold/10400.
2210 Work around it by keeping stable sort order. */
2211 return (a
< b
) ? -1 : 1;
2213 /* Put eh_frame entries after debug_frame ones. */
2214 return aa
->eh_frame_p
- bb
->eh_frame_p
;
2217 return (aa
->initial_location
< bb
->initial_location
) ? -1 : 1;
2221 dwarf2_build_frame_info (struct objfile
*objfile
)
2223 struct comp_unit
*unit
;
2224 const gdb_byte
*frame_ptr
;
2225 struct dwarf2_cie_table cie_table
;
2226 struct dwarf2_fde_table fde_table
;
2227 struct dwarf2_fde_table
*fde_table2
;
2229 cie_table
.num_entries
= 0;
2230 cie_table
.entries
= NULL
;
2232 fde_table
.num_entries
= 0;
2233 fde_table
.entries
= NULL
;
2235 /* Build a minimal decoding of the DWARF2 compilation unit. */
2236 unit
= (struct comp_unit
*) obstack_alloc (&objfile
->objfile_obstack
,
2237 sizeof (struct comp_unit
));
2238 unit
->abfd
= objfile
->obfd
;
2239 unit
->objfile
= objfile
;
2243 if (objfile
->separate_debug_objfile_backlink
== NULL
)
2245 /* Do not read .eh_frame from separate file as they must be also
2246 present in the main file. */
2247 dwarf2_get_section_info (objfile
, DWARF2_EH_FRAME
,
2248 &unit
->dwarf_frame_section
,
2249 &unit
->dwarf_frame_buffer
,
2250 &unit
->dwarf_frame_size
);
2251 if (unit
->dwarf_frame_size
)
2253 asection
*got
, *txt
;
2255 /* FIXME: kettenis/20030602: This is the DW_EH_PE_datarel base
2256 that is used for the i386/amd64 target, which currently is
2257 the only target in GCC that supports/uses the
2258 DW_EH_PE_datarel encoding. */
2259 got
= bfd_get_section_by_name (unit
->abfd
, ".got");
2261 unit
->dbase
= got
->vma
;
2263 /* GCC emits the DW_EH_PE_textrel encoding type on sh and ia64
2265 txt
= bfd_get_section_by_name (unit
->abfd
, ".text");
2267 unit
->tbase
= txt
->vma
;
2271 frame_ptr
= unit
->dwarf_frame_buffer
;
2272 while (frame_ptr
< unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
)
2273 frame_ptr
= decode_frame_entry (unit
, frame_ptr
, 1,
2274 &cie_table
, &fde_table
,
2275 EH_CIE_OR_FDE_TYPE_ID
);
2278 CATCH (e
, RETURN_MASK_ERROR
)
2280 warning (_("skipping .eh_frame info of %s: %s"),
2281 objfile_name (objfile
), e
.message
);
2283 if (fde_table
.num_entries
!= 0)
2285 xfree (fde_table
.entries
);
2286 fde_table
.entries
= NULL
;
2287 fde_table
.num_entries
= 0;
2289 /* The cie_table is discarded by the next if. */
2293 if (cie_table
.num_entries
!= 0)
2295 /* Reinit cie_table: debug_frame has different CIEs. */
2296 xfree (cie_table
.entries
);
2297 cie_table
.num_entries
= 0;
2298 cie_table
.entries
= NULL
;
2303 dwarf2_get_section_info (objfile
, DWARF2_DEBUG_FRAME
,
2304 &unit
->dwarf_frame_section
,
2305 &unit
->dwarf_frame_buffer
,
2306 &unit
->dwarf_frame_size
);
2307 if (unit
->dwarf_frame_size
)
2309 int num_old_fde_entries
= fde_table
.num_entries
;
2313 frame_ptr
= unit
->dwarf_frame_buffer
;
2314 while (frame_ptr
< unit
->dwarf_frame_buffer
+ unit
->dwarf_frame_size
)
2315 frame_ptr
= decode_frame_entry (unit
, frame_ptr
, 0,
2316 &cie_table
, &fde_table
,
2317 EH_CIE_OR_FDE_TYPE_ID
);
2319 CATCH (e
, RETURN_MASK_ERROR
)
2321 warning (_("skipping .debug_frame info of %s: %s"),
2322 objfile_name (objfile
), e
.message
);
2324 if (fde_table
.num_entries
!= 0)
2326 fde_table
.num_entries
= num_old_fde_entries
;
2327 if (num_old_fde_entries
== 0)
2329 xfree (fde_table
.entries
);
2330 fde_table
.entries
= NULL
;
2335 = XRESIZEVEC (struct dwarf2_fde
*, fde_table
.entries
,
2336 fde_table
.num_entries
);
2339 fde_table
.num_entries
= num_old_fde_entries
;
2340 /* The cie_table is discarded by the next if. */
2345 /* Discard the cie_table, it is no longer needed. */
2346 if (cie_table
.num_entries
!= 0)
2348 xfree (cie_table
.entries
);
2349 cie_table
.entries
= NULL
; /* Paranoia. */
2350 cie_table
.num_entries
= 0; /* Paranoia. */
2353 /* Copy fde_table to obstack: it is needed at runtime. */
2354 fde_table2
= XOBNEW (&objfile
->objfile_obstack
, struct dwarf2_fde_table
);
2356 if (fde_table
.num_entries
== 0)
2358 fde_table2
->entries
= NULL
;
2359 fde_table2
->num_entries
= 0;
2363 struct dwarf2_fde
*fde_prev
= NULL
;
2364 struct dwarf2_fde
*first_non_zero_fde
= NULL
;
2367 /* Prepare FDE table for lookups. */
2368 qsort (fde_table
.entries
, fde_table
.num_entries
,
2369 sizeof (fde_table
.entries
[0]), qsort_fde_cmp
);
2371 /* Check for leftovers from --gc-sections. The GNU linker sets
2372 the relevant symbols to zero, but doesn't zero the FDE *end*
2373 ranges because there's no relocation there. It's (offset,
2374 length), not (start, end). On targets where address zero is
2375 just another valid address this can be a problem, since the
2376 FDEs appear to be non-empty in the output --- we could pick
2377 out the wrong FDE. To work around this, when overlaps are
2378 detected, we prefer FDEs that do not start at zero.
2380 Start by finding the first FDE with non-zero start. Below
2381 we'll discard all FDEs that start at zero and overlap this
2383 for (i
= 0; i
< fde_table
.num_entries
; i
++)
2385 struct dwarf2_fde
*fde
= fde_table
.entries
[i
];
2387 if (fde
->initial_location
!= 0)
2389 first_non_zero_fde
= fde
;
2394 /* Since we'll be doing bsearch, squeeze out identical (except
2395 for eh_frame_p) fde entries so bsearch result is predictable.
2396 Also discard leftovers from --gc-sections. */
2397 fde_table2
->num_entries
= 0;
2398 for (i
= 0; i
< fde_table
.num_entries
; i
++)
2400 struct dwarf2_fde
*fde
= fde_table
.entries
[i
];
2402 if (fde
->initial_location
== 0
2403 && first_non_zero_fde
!= NULL
2404 && (first_non_zero_fde
->initial_location
2405 < fde
->initial_location
+ fde
->address_range
))
2408 if (fde_prev
!= NULL
2409 && fde_prev
->initial_location
== fde
->initial_location
)
2412 obstack_grow (&objfile
->objfile_obstack
, &fde_table
.entries
[i
],
2413 sizeof (fde_table
.entries
[0]));
2414 ++fde_table2
->num_entries
;
2418 = (struct dwarf2_fde
**) obstack_finish (&objfile
->objfile_obstack
);
2420 /* Discard the original fde_table. */
2421 xfree (fde_table
.entries
);
2424 set_objfile_data (objfile
, dwarf2_frame_objfile_data
, fde_table2
);
2427 /* Provide a prototype to silence -Wmissing-prototypes. */
2428 void _initialize_dwarf2_frame (void);
2431 _initialize_dwarf2_frame (void)
2433 dwarf2_frame_data
= gdbarch_data_register_pre_init (dwarf2_frame_init
);
2434 dwarf2_frame_objfile_data
= register_objfile_data ();