xref: /netbsd/external/gpl3/gdb/dist/gdb/dwarf2/frame.h (revision 1424dfb3)
1*1424dfb3Schristos /* Frame unwinder for frames with DWARF Call Frame Information.
2*1424dfb3Schristos 
3*1424dfb3Schristos    Copyright (C) 2003-2020 Free Software Foundation, Inc.
4*1424dfb3Schristos 
5*1424dfb3Schristos    Contributed by Mark Kettenis.
6*1424dfb3Schristos 
7*1424dfb3Schristos    This file is part of GDB.
8*1424dfb3Schristos 
9*1424dfb3Schristos    This program is free software; you can redistribute it and/or modify
10*1424dfb3Schristos    it under the terms of the GNU General Public License as published by
11*1424dfb3Schristos    the Free Software Foundation; either version 3 of the License, or
12*1424dfb3Schristos    (at your option) any later version.
13*1424dfb3Schristos 
14*1424dfb3Schristos    This program is distributed in the hope that it will be useful,
15*1424dfb3Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
16*1424dfb3Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*1424dfb3Schristos    GNU General Public License for more details.
18*1424dfb3Schristos 
19*1424dfb3Schristos    You should have received a copy of the GNU General Public License
20*1424dfb3Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21*1424dfb3Schristos 
22*1424dfb3Schristos #ifndef DWARF2_FRAME_H
23*1424dfb3Schristos #define DWARF2_FRAME_H 1
24*1424dfb3Schristos 
25*1424dfb3Schristos struct gdbarch;
26*1424dfb3Schristos struct frame_info;
27*1424dfb3Schristos struct dwarf2_per_cu_data;
28*1424dfb3Schristos struct agent_expr;
29*1424dfb3Schristos struct axs_value;
30*1424dfb3Schristos 
31*1424dfb3Schristos /* Register rule.  */
32*1424dfb3Schristos 
33*1424dfb3Schristos enum dwarf2_frame_reg_rule
34*1424dfb3Schristos {
35*1424dfb3Schristos   /* Make certain that 0 maps onto the correct enum value; the
36*1424dfb3Schristos      corresponding structure is being initialized using memset zero.
37*1424dfb3Schristos      This indicates that CFI didn't provide any information at all
38*1424dfb3Schristos      about a register, leaving how to obtain its value totally
39*1424dfb3Schristos      unspecified.  */
40*1424dfb3Schristos   DWARF2_FRAME_REG_UNSPECIFIED = 0,
41*1424dfb3Schristos 
42*1424dfb3Schristos   /* The term "undefined" comes from the DWARF2 CFI spec which this
43*1424dfb3Schristos      code is modeling; it indicates that the register's value is
44*1424dfb3Schristos      "undefined".  GCC uses the less formal term "unsaved".  Its
45*1424dfb3Schristos      definition is a combination of REG_UNDEFINED and REG_UNSPECIFIED.
46*1424dfb3Schristos      The failure to differentiate the two helps explain a few problems
47*1424dfb3Schristos      with the CFI generated by GCC.  */
48*1424dfb3Schristos   DWARF2_FRAME_REG_UNDEFINED,
49*1424dfb3Schristos   DWARF2_FRAME_REG_SAVED_OFFSET,
50*1424dfb3Schristos   DWARF2_FRAME_REG_SAVED_REG,
51*1424dfb3Schristos   DWARF2_FRAME_REG_SAVED_EXP,
52*1424dfb3Schristos   DWARF2_FRAME_REG_SAME_VALUE,
53*1424dfb3Schristos 
54*1424dfb3Schristos   /* These are defined in Dwarf3.  */
55*1424dfb3Schristos   DWARF2_FRAME_REG_SAVED_VAL_OFFSET,
56*1424dfb3Schristos   DWARF2_FRAME_REG_SAVED_VAL_EXP,
57*1424dfb3Schristos 
58*1424dfb3Schristos   /* These aren't defined by the DWARF2 CFI specification, but are
59*1424dfb3Schristos      used internally by GDB.  */
60*1424dfb3Schristos   DWARF2_FRAME_REG_FN,		/* Call a registered function.  */
61*1424dfb3Schristos   DWARF2_FRAME_REG_RA,		/* Return Address.  */
62*1424dfb3Schristos   DWARF2_FRAME_REG_RA_OFFSET,	/* Return Address with offset.  */
63*1424dfb3Schristos   DWARF2_FRAME_REG_CFA,		/* Call Frame Address.  */
64*1424dfb3Schristos   DWARF2_FRAME_REG_CFA_OFFSET	/* Call Frame Address with offset.  */
65*1424dfb3Schristos };
66*1424dfb3Schristos 
67*1424dfb3Schristos /* Register state.  */
68*1424dfb3Schristos 
69*1424dfb3Schristos struct dwarf2_frame_state_reg
70*1424dfb3Schristos {
71*1424dfb3Schristos   /* Each register save state can be described in terms of a CFA slot,
72*1424dfb3Schristos      another register, or a location expression.  */
73*1424dfb3Schristos   union {
74*1424dfb3Schristos     LONGEST offset;
75*1424dfb3Schristos     ULONGEST reg;
76*1424dfb3Schristos     struct
77*1424dfb3Schristos     {
78*1424dfb3Schristos       const gdb_byte *start;
79*1424dfb3Schristos       ULONGEST len;
80*1424dfb3Schristos     } exp;
81*1424dfb3Schristos     struct value *(*fn) (struct frame_info *this_frame, void **this_cache,
82*1424dfb3Schristos 			 int regnum);
83*1424dfb3Schristos   } loc;
84*1424dfb3Schristos   enum dwarf2_frame_reg_rule how;
85*1424dfb3Schristos };
86*1424dfb3Schristos 
87*1424dfb3Schristos enum cfa_how_kind
88*1424dfb3Schristos {
89*1424dfb3Schristos   CFA_UNSET,
90*1424dfb3Schristos   CFA_REG_OFFSET,
91*1424dfb3Schristos   CFA_EXP
92*1424dfb3Schristos };
93*1424dfb3Schristos 
94*1424dfb3Schristos struct dwarf2_frame_state_reg_info
95*1424dfb3Schristos {
96*1424dfb3Schristos   dwarf2_frame_state_reg_info () = default;
~dwarf2_frame_state_reg_infodwarf2_frame_state_reg_info97*1424dfb3Schristos   ~dwarf2_frame_state_reg_info ()
98*1424dfb3Schristos   {
99*1424dfb3Schristos     delete prev;
100*1424dfb3Schristos   }
101*1424dfb3Schristos 
102*1424dfb3Schristos   /* Copy constructor.  */
dwarf2_frame_state_reg_infodwarf2_frame_state_reg_info103*1424dfb3Schristos   dwarf2_frame_state_reg_info (const dwarf2_frame_state_reg_info &src)
104*1424dfb3Schristos     : reg (src.reg), cfa_offset (src.cfa_offset),
105*1424dfb3Schristos       cfa_reg (src.cfa_reg), cfa_how (src.cfa_how), cfa_exp (src.cfa_exp),
106*1424dfb3Schristos       prev (src.prev)
107*1424dfb3Schristos   {
108*1424dfb3Schristos   }
109*1424dfb3Schristos 
110*1424dfb3Schristos   /* Assignment operator for both move-assignment and copy-assignment.  */
111*1424dfb3Schristos   dwarf2_frame_state_reg_info&
112*1424dfb3Schristos   operator= (dwarf2_frame_state_reg_info rhs)
113*1424dfb3Schristos   {
114*1424dfb3Schristos     swap (*this, rhs);
115*1424dfb3Schristos     return *this;
116*1424dfb3Schristos   }
117*1424dfb3Schristos 
118*1424dfb3Schristos   /* Move constructor.  */
dwarf2_frame_state_reg_infodwarf2_frame_state_reg_info119*1424dfb3Schristos   dwarf2_frame_state_reg_info (dwarf2_frame_state_reg_info &&rhs) noexcept
120*1424dfb3Schristos     : reg (std::move (rhs.reg)), cfa_offset (rhs.cfa_offset),
121*1424dfb3Schristos       cfa_reg (rhs.cfa_reg), cfa_how (rhs.cfa_how), cfa_exp (rhs.cfa_exp),
122*1424dfb3Schristos       prev (rhs.prev)
123*1424dfb3Schristos   {
124*1424dfb3Schristos     rhs.prev = nullptr;
125*1424dfb3Schristos   }
126*1424dfb3Schristos 
127*1424dfb3Schristos   /* If necessary, enlarge the register set to hold NUM_REGS_REQUESTED
128*1424dfb3Schristos      registers.  */
alloc_regsdwarf2_frame_state_reg_info129*1424dfb3Schristos   void alloc_regs (int num_regs_requested)
130*1424dfb3Schristos   {
131*1424dfb3Schristos     gdb_assert (num_regs_requested > 0);
132*1424dfb3Schristos 
133*1424dfb3Schristos     if (num_regs_requested <= reg.size ())
134*1424dfb3Schristos       return;
135*1424dfb3Schristos 
136*1424dfb3Schristos     reg.resize (num_regs_requested);
137*1424dfb3Schristos   }
138*1424dfb3Schristos 
139*1424dfb3Schristos   std::vector<struct dwarf2_frame_state_reg> reg;
140*1424dfb3Schristos 
141*1424dfb3Schristos   LONGEST cfa_offset = 0;
142*1424dfb3Schristos   ULONGEST cfa_reg = 0;
143*1424dfb3Schristos   enum cfa_how_kind cfa_how = CFA_UNSET;
144*1424dfb3Schristos   const gdb_byte *cfa_exp = NULL;
145*1424dfb3Schristos 
146*1424dfb3Schristos   /* Used to implement DW_CFA_remember_state.  */
147*1424dfb3Schristos   struct dwarf2_frame_state_reg_info *prev = NULL;
148*1424dfb3Schristos 
149*1424dfb3Schristos private:
swapdwarf2_frame_state_reg_info150*1424dfb3Schristos   friend void swap (dwarf2_frame_state_reg_info& lhs,
151*1424dfb3Schristos 		    dwarf2_frame_state_reg_info& rhs)
152*1424dfb3Schristos   {
153*1424dfb3Schristos     using std::swap;
154*1424dfb3Schristos 
155*1424dfb3Schristos     swap (lhs.reg, rhs.reg);
156*1424dfb3Schristos     swap (lhs.cfa_offset, rhs.cfa_offset);
157*1424dfb3Schristos     swap (lhs.cfa_reg, rhs.cfa_reg);
158*1424dfb3Schristos     swap (lhs.cfa_how, rhs.cfa_how);
159*1424dfb3Schristos     swap (lhs.cfa_exp, rhs.cfa_exp);
160*1424dfb3Schristos     swap (lhs.prev, rhs.prev);
161*1424dfb3Schristos   }
162*1424dfb3Schristos };
163*1424dfb3Schristos 
164*1424dfb3Schristos struct dwarf2_cie;
165*1424dfb3Schristos 
166*1424dfb3Schristos /* Structure describing a frame state.  */
167*1424dfb3Schristos 
168*1424dfb3Schristos struct dwarf2_frame_state
169*1424dfb3Schristos {
170*1424dfb3Schristos   dwarf2_frame_state (CORE_ADDR pc, struct dwarf2_cie *cie);
171*1424dfb3Schristos 
172*1424dfb3Schristos   /* Each register save state can be described in terms of a CFA slot,
173*1424dfb3Schristos      another register, or a location expression.  */
174*1424dfb3Schristos   struct dwarf2_frame_state_reg_info regs {};
175*1424dfb3Schristos 
176*1424dfb3Schristos   /* The PC described by the current frame state.  */
177*1424dfb3Schristos   CORE_ADDR pc;
178*1424dfb3Schristos 
179*1424dfb3Schristos   /* Initial register set from the CIE.
180*1424dfb3Schristos      Used to implement DW_CFA_restore.  */
181*1424dfb3Schristos   struct dwarf2_frame_state_reg_info initial {};
182*1424dfb3Schristos 
183*1424dfb3Schristos   /* The information we care about from the CIE.  */
184*1424dfb3Schristos   const LONGEST data_align;
185*1424dfb3Schristos   const ULONGEST code_align;
186*1424dfb3Schristos   const ULONGEST retaddr_column;
187*1424dfb3Schristos 
188*1424dfb3Schristos   /* Flags for known producer quirks.  */
189*1424dfb3Schristos 
190*1424dfb3Schristos   /* The ARM compilers, in DWARF2 mode, assume that DW_CFA_def_cfa
191*1424dfb3Schristos      and DW_CFA_def_cfa_offset takes a factored offset.  */
192*1424dfb3Schristos   bool armcc_cfa_offsets_sf = false;
193*1424dfb3Schristos 
194*1424dfb3Schristos   /* The ARM compilers, in DWARF2 or DWARF3 mode, may assume that
195*1424dfb3Schristos      the CFA is defined as REG - OFFSET rather than REG + OFFSET.  */
196*1424dfb3Schristos   bool armcc_cfa_offsets_reversed = false;
197*1424dfb3Schristos };
198*1424dfb3Schristos 
199*1424dfb3Schristos /* When this is true the DWARF frame unwinders can be used if they are
200*1424dfb3Schristos    registered with the gdbarch.  Not all architectures can or do use the
201*1424dfb3Schristos    DWARF unwinders.  Setting this to true on a target that does not
202*1424dfb3Schristos    otherwise support the DWARF unwinders has no effect.  */
203*1424dfb3Schristos extern bool dwarf2_frame_unwinders_enabled_p;
204*1424dfb3Schristos 
205*1424dfb3Schristos /* Set the architecture-specific register state initialization
206*1424dfb3Schristos    function for GDBARCH to INIT_REG.  */
207*1424dfb3Schristos 
208*1424dfb3Schristos extern void dwarf2_frame_set_init_reg (struct gdbarch *gdbarch,
209*1424dfb3Schristos 				       void (*init_reg) (struct gdbarch *, int,
210*1424dfb3Schristos 					     struct dwarf2_frame_state_reg *,
211*1424dfb3Schristos 					     struct frame_info *));
212*1424dfb3Schristos 
213*1424dfb3Schristos /* Set the architecture-specific signal trampoline recognition
214*1424dfb3Schristos    function for GDBARCH to SIGNAL_FRAME_P.  */
215*1424dfb3Schristos 
216*1424dfb3Schristos extern void
217*1424dfb3Schristos   dwarf2_frame_set_signal_frame_p (struct gdbarch *gdbarch,
218*1424dfb3Schristos 				   int (*signal_frame_p) (struct gdbarch *,
219*1424dfb3Schristos 							  struct frame_info *));
220*1424dfb3Schristos 
221*1424dfb3Schristos /* Set the architecture-specific adjustment of .eh_frame and .debug_frame
222*1424dfb3Schristos    register numbers.  */
223*1424dfb3Schristos 
224*1424dfb3Schristos extern void
225*1424dfb3Schristos   dwarf2_frame_set_adjust_regnum (struct gdbarch *gdbarch,
226*1424dfb3Schristos 				  int (*adjust_regnum) (struct gdbarch *,
227*1424dfb3Schristos 							int, int));
228*1424dfb3Schristos 
229*1424dfb3Schristos /* Append the DWARF-2 frame unwinders to GDBARCH's list.  */
230*1424dfb3Schristos 
231*1424dfb3Schristos void dwarf2_append_unwinders (struct gdbarch *gdbarch);
232*1424dfb3Schristos 
233*1424dfb3Schristos /* Return the frame base methods for the function that contains PC, or
234*1424dfb3Schristos    NULL if it can't be handled by the DWARF CFI frame unwinder.  */
235*1424dfb3Schristos 
236*1424dfb3Schristos extern const struct frame_base *
237*1424dfb3Schristos   dwarf2_frame_base_sniffer (struct frame_info *this_frame);
238*1424dfb3Schristos 
239*1424dfb3Schristos /* Compute the DWARF CFA for a frame.  */
240*1424dfb3Schristos 
241*1424dfb3Schristos CORE_ADDR dwarf2_frame_cfa (struct frame_info *this_frame);
242*1424dfb3Schristos 
243*1424dfb3Schristos /* Find the CFA information for PC.
244*1424dfb3Schristos 
245*1424dfb3Schristos    Return 1 if a register is used for the CFA, or 0 if another
246*1424dfb3Schristos    expression is used.  Throw an exception on error.
247*1424dfb3Schristos 
248*1424dfb3Schristos    GDBARCH is the architecture to use.
249*1424dfb3Schristos    DATA is the per-CU data.
250*1424dfb3Schristos 
251*1424dfb3Schristos    REGNUM_OUT is an out parameter that is set to the register number.
252*1424dfb3Schristos    OFFSET_OUT is the offset to use from this register.
253*1424dfb3Schristos    These are only filled in when 1 is returned.
254*1424dfb3Schristos 
255*1424dfb3Schristos    TEXT_OFFSET_OUT, CFA_START_OUT, and CFA_END_OUT describe the CFA
256*1424dfb3Schristos    in other cases.  These are only used when 0 is returned.  */
257*1424dfb3Schristos 
258*1424dfb3Schristos extern int dwarf2_fetch_cfa_info (struct gdbarch *gdbarch, CORE_ADDR pc,
259*1424dfb3Schristos 				  struct dwarf2_per_cu_data *data,
260*1424dfb3Schristos 				  int *regnum_out, LONGEST *offset_out,
261*1424dfb3Schristos 				  CORE_ADDR *text_offset_out,
262*1424dfb3Schristos 				  const gdb_byte **cfa_start_out,
263*1424dfb3Schristos 				  const gdb_byte **cfa_end_out);
264*1424dfb3Schristos 
265*1424dfb3Schristos #endif /* dwarf2-frame.h */
266