1 /* Frame unwinder for frames with DWARF Call Frame Information.
3 Copyright (C) 2003-2018 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/>. */
22 #ifndef DWARF2_FRAME_H
23 #define DWARF2_FRAME_H 1
28 struct dwarf2_per_cu_data;
34 enum dwarf2_frame_reg_rule
36 /* Make certain that 0 maps onto the correct enum value; the
37 corresponding structure is being initialized using memset zero.
38 This indicates that CFI didn't provide any information at all
39 about a register, leaving how to obtain its value totally
41 DWARF2_FRAME_REG_UNSPECIFIED = 0,
43 /* The term "undefined" comes from the DWARF2 CFI spec which this
44 code is moddeling; it indicates that the register's value is
45 "undefined". GCC uses the less formal term "unsaved". Its
46 definition is a combination of REG_UNDEFINED and REG_UNSPECIFIED.
47 The failure to differentiate the two helps explain a few problems
48 with the CFI generated by GCC. */
49 DWARF2_FRAME_REG_UNDEFINED,
50 DWARF2_FRAME_REG_SAVED_OFFSET,
51 DWARF2_FRAME_REG_SAVED_REG,
52 DWARF2_FRAME_REG_SAVED_EXP,
53 DWARF2_FRAME_REG_SAME_VALUE,
55 /* These are defined in Dwarf3. */
56 DWARF2_FRAME_REG_SAVED_VAL_OFFSET,
57 DWARF2_FRAME_REG_SAVED_VAL_EXP,
59 /* These aren't defined by the DWARF2 CFI specification, but are
60 used internally by GDB. */
61 DWARF2_FRAME_REG_FN, /* Call a registered function. */
62 DWARF2_FRAME_REG_RA, /* Return Address. */
63 DWARF2_FRAME_REG_RA_OFFSET, /* Return Address with offset. */
64 DWARF2_FRAME_REG_CFA, /* Call Frame Address. */
65 DWARF2_FRAME_REG_CFA_OFFSET /* Call Frame Address with offset. */
70 struct dwarf2_frame_state_reg
72 /* Each register save state can be described in terms of a CFA slot,
73 another register, or a location expression. */
79 const gdb_byte *start;
82 struct value *(*fn) (struct frame_info *this_frame, void **this_cache,
85 enum dwarf2_frame_reg_rule how;
95 struct dwarf2_frame_state_reg_info
97 dwarf2_frame_state_reg_info () = default;
98 ~dwarf2_frame_state_reg_info ()
104 /* Copy constructor. */
105 dwarf2_frame_state_reg_info (const dwarf2_frame_state_reg_info &src)
106 : num_regs (src.num_regs), cfa_offset (src.cfa_offset),
107 cfa_reg (src.cfa_reg), cfa_how (src.cfa_how), cfa_exp (src.cfa_exp),
110 size_t size = src.num_regs * sizeof (struct dwarf2_frame_state_reg);
112 reg = (struct dwarf2_frame_state_reg *) xmalloc (size);
113 memcpy (reg, src.reg, size);
116 /* Assignment operator for both move-assignment and copy-assignment. */
117 dwarf2_frame_state_reg_info&
118 operator= (dwarf2_frame_state_reg_info rhs)
124 /* Move constructor. */
125 dwarf2_frame_state_reg_info (dwarf2_frame_state_reg_info &&rhs) noexcept
126 : reg (rhs.reg), num_regs (rhs.num_regs), cfa_offset (rhs.cfa_offset),
127 cfa_reg (rhs.cfa_reg), cfa_how (rhs.cfa_how), cfa_exp (rhs.cfa_exp),
134 /* Assert that the register set RS is large enough to store gdbarch_num_regs
135 columns. If necessary, enlarge the register set. */
136 void alloc_regs (int num_regs_requested)
138 if (num_regs_requested <= num_regs)
141 size_t size = sizeof (struct dwarf2_frame_state_reg);
143 reg = (struct dwarf2_frame_state_reg *)
144 xrealloc (reg, num_regs_requested * size);
146 /* Initialize newly allocated registers. */
147 memset (reg + num_regs, 0, (num_regs_requested - num_regs) * size);
148 num_regs = num_regs_requested;
151 struct dwarf2_frame_state_reg *reg = NULL;
154 LONGEST cfa_offset = 0;
155 ULONGEST cfa_reg = 0;
156 enum cfa_how_kind cfa_how = CFA_UNSET;
157 const gdb_byte *cfa_exp = NULL;
159 /* Used to implement DW_CFA_remember_state. */
160 struct dwarf2_frame_state_reg_info *prev = NULL;
163 friend void swap (dwarf2_frame_state_reg_info& lhs,
164 dwarf2_frame_state_reg_info& rhs)
168 swap (lhs.reg, rhs.reg);
169 swap (lhs.num_regs, rhs.num_regs);
170 swap (lhs.cfa_offset, rhs.cfa_offset);
171 swap (lhs.cfa_reg, rhs.cfa_reg);
172 swap (lhs.cfa_how, rhs.cfa_how);
173 swap (lhs.cfa_exp, rhs.cfa_exp);
174 swap (lhs.prev, rhs.prev);
180 /* Structure describing a frame state. */
182 struct dwarf2_frame_state
184 dwarf2_frame_state (CORE_ADDR pc, struct dwarf2_cie *cie);
186 /* Each register save state can be described in terms of a CFA slot,
187 another register, or a location expression. */
188 struct dwarf2_frame_state_reg_info regs {};
190 /* The PC described by the current frame state. */
193 /* Initial register set from the CIE.
194 Used to implement DW_CFA_restore. */
195 struct dwarf2_frame_state_reg_info initial {};
197 /* The information we care about from the CIE. */
198 const LONGEST data_align;
199 const ULONGEST code_align;
200 const ULONGEST retaddr_column;
202 /* Flags for known producer quirks. */
204 /* The ARM compilers, in DWARF2 mode, assume that DW_CFA_def_cfa
205 and DW_CFA_def_cfa_offset takes a factored offset. */
206 bool armcc_cfa_offsets_sf = false;
208 /* The ARM compilers, in DWARF2 or DWARF3 mode, may assume that
209 the CFA is defined as REG - OFFSET rather than REG + OFFSET. */
210 bool armcc_cfa_offsets_reversed = false;
213 /* When this is true the DWARF frame unwinders can be used if they are
214 registered with the gdbarch. Not all architectures can or do use the
215 DWARF unwinders. Setting this to true on a target that does not
216 otherwise support the DWARF unwinders has no effect. */
217 extern int dwarf2_frame_unwinders_enabled_p;
219 /* Set the architecture-specific register state initialization
220 function for GDBARCH to INIT_REG. */
222 extern void dwarf2_frame_set_init_reg (struct gdbarch *gdbarch,
223 void (*init_reg) (struct gdbarch *, int,
224 struct dwarf2_frame_state_reg *,
225 struct frame_info *));
227 /* Set the architecture-specific signal trampoline recognition
228 function for GDBARCH to SIGNAL_FRAME_P. */
231 dwarf2_frame_set_signal_frame_p (struct gdbarch *gdbarch,
232 int (*signal_frame_p) (struct gdbarch *,
233 struct frame_info *));
235 /* Set the architecture-specific adjustment of .eh_frame and .debug_frame
239 dwarf2_frame_set_adjust_regnum (struct gdbarch *gdbarch,
240 int (*adjust_regnum) (struct gdbarch *,
243 /* Append the DWARF-2 frame unwinders to GDBARCH's list. */
245 void dwarf2_append_unwinders (struct gdbarch *gdbarch);
247 /* Return the frame base methods for the function that contains PC, or
248 NULL if it can't be handled by the DWARF CFI frame unwinder. */
250 extern const struct frame_base *
251 dwarf2_frame_base_sniffer (struct frame_info *this_frame);
253 /* Compute the DWARF CFA for a frame. */
255 CORE_ADDR dwarf2_frame_cfa (struct frame_info *this_frame);
257 /* Find the CFA information for PC.
259 Return 1 if a register is used for the CFA, or 0 if another
260 expression is used. Throw an exception on error.
262 GDBARCH is the architecture to use.
263 DATA is the per-CU data.
265 REGNUM_OUT is an out parameter that is set to the register number.
266 OFFSET_OUT is the offset to use from this register.
267 These are only filled in when 1 is returned.
269 TEXT_OFFSET_OUT, CFA_START_OUT, and CFA_END_OUT describe the CFA
270 in other cases. These are only used when 0 is returned. */
272 extern int dwarf2_fetch_cfa_info (struct gdbarch *gdbarch, CORE_ADDR pc,
273 struct dwarf2_per_cu_data *data,
274 int *regnum_out, LONGEST *offset_out,
275 CORE_ADDR *text_offset_out,
276 const gdb_byte **cfa_start_out,
277 const gdb_byte **cfa_end_out);
279 #endif /* dwarf2-frame.h */