Update LOCAL_PATCHES after libsanitizer merge.
[official-gcc.git] / libgcc / config / arm / unwind-arm.c
blob564e4f131577c6230ddaadcb9b62120c523aa763
1 /* ARM EABI compliant unwinding routines.
2 Copyright (C) 2004-2018 Free Software Foundation, Inc.
3 Contributed by Paul Brook
5 This file is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 3, or (at your option) any
8 later version.
10 This file is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 Under Section 7 of GPL version 3, you are granted additional
16 permissions described in the GCC Runtime Library Exception, version
17 3.1, as published by the Free Software Foundation.
19 You should have received a copy of the GNU General Public License and
20 a copy of the GCC Runtime Library Exception along with this program;
21 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
22 <http://www.gnu.org/licenses/>. */
24 #include "unwind.h"
26 /* Misc constants. */
27 #define R_IP 12
28 #define R_SP 13
29 #define R_LR 14
30 #define R_PC 15
32 #define VRS_PC(vrs) ((vrs)->core.r[R_PC])
33 #define VRS_SP(vrs) ((vrs)->core.r[R_SP])
34 #define VRS_RETURN(vrs) ((vrs)->core.r[R_LR])
36 struct core_regs
38 _uw r[16];
41 /* We use normal integer types here to avoid the compiler generating
42 coprocessor instructions. */
43 struct vfp_regs
45 _uw64 d[16];
46 _uw pad;
49 struct vfpv3_regs
51 /* Always populated via VSTM, so no need for the "pad" field from
52 vfp_regs (which is used to store the format word for FSTMX). */
53 _uw64 d[16];
56 struct wmmxd_regs
58 _uw64 wd[16];
61 struct wmmxc_regs
63 _uw wc[4];
66 /* The ABI specifies that the unwind routines may only use core registers,
67 except when actually manipulating coprocessor state. This allows
68 us to write one implementation that works on all platforms by
69 demand-saving coprocessor registers.
71 During unwinding we hold the coprocessor state in the actual hardware
72 registers and allocate demand-save areas for use during phase1
73 unwinding. */
75 typedef struct
77 /* The first fields must be the same as a phase2_vrs. */
78 _uw demand_save_flags;
79 struct core_regs core;
80 _uw prev_sp; /* Only valid during forced unwinding. */
81 struct vfp_regs vfp;
82 struct vfpv3_regs vfp_regs_16_to_31;
83 struct wmmxd_regs wmmxd;
84 struct wmmxc_regs wmmxc;
85 } phase1_vrs;
87 #define DEMAND_SAVE_VFP 1 /* VFP state has been saved if not set */
88 #define DEMAND_SAVE_VFP_D 2 /* VFP state is for FLDMD/FSTMD if set */
89 #define DEMAND_SAVE_VFP_V3 4 /* VFPv3 state for regs 16 .. 31 has
90 been saved if not set */
91 #define DEMAND_SAVE_WMMXD 8 /* iWMMXt data registers have been
92 saved if not set. */
93 #define DEMAND_SAVE_WMMXC 16 /* iWMMXt control registers have been
94 saved if not set. */
96 /* This must match the structure created by the assembly wrappers. */
97 typedef struct
99 _uw demand_save_flags;
100 struct core_regs core;
101 } phase2_vrs;
103 /* Coprocessor register state manipulation functions. */
105 /* Routines for FLDMX/FSTMX format... */
106 void __gnu_Unwind_Save_VFP (struct vfp_regs * p);
107 void __gnu_Unwind_Restore_VFP (struct vfp_regs * p);
108 void __gnu_Unwind_Save_WMMXD (struct wmmxd_regs * p);
109 void __gnu_Unwind_Restore_WMMXD (struct wmmxd_regs * p);
110 void __gnu_Unwind_Save_WMMXC (struct wmmxc_regs * p);
111 void __gnu_Unwind_Restore_WMMXC (struct wmmxc_regs * p);
113 /* ...and those for FLDMD/FSTMD format... */
114 void __gnu_Unwind_Save_VFP_D (struct vfp_regs * p);
115 void __gnu_Unwind_Restore_VFP_D (struct vfp_regs * p);
117 /* ...and those for VLDM/VSTM format, saving/restoring only registers
118 16 through 31. */
119 void __gnu_Unwind_Save_VFP_D_16_to_31 (struct vfpv3_regs * p);
120 void __gnu_Unwind_Restore_VFP_D_16_to_31 (struct vfpv3_regs * p);
122 /* Restore coprocessor state after phase1 unwinding. */
123 static void
124 restore_non_core_regs (phase1_vrs * vrs)
126 if ((vrs->demand_save_flags & DEMAND_SAVE_VFP) == 0)
128 if (vrs->demand_save_flags & DEMAND_SAVE_VFP_D)
129 __gnu_Unwind_Restore_VFP_D (&vrs->vfp);
130 else
131 __gnu_Unwind_Restore_VFP (&vrs->vfp);
134 if ((vrs->demand_save_flags & DEMAND_SAVE_VFP_V3) == 0)
135 __gnu_Unwind_Restore_VFP_D_16_to_31 (&vrs->vfp_regs_16_to_31);
137 if ((vrs->demand_save_flags & DEMAND_SAVE_WMMXD) == 0)
138 __gnu_Unwind_Restore_WMMXD (&vrs->wmmxd);
139 if ((vrs->demand_save_flags & DEMAND_SAVE_WMMXC) == 0)
140 __gnu_Unwind_Restore_WMMXC (&vrs->wmmxc);
143 #include "unwind-arm-common.inc"
145 /* ABI defined personality routines. */
146 extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr0 (_Unwind_State,
147 _Unwind_Control_Block *, _Unwind_Context *);// __attribute__((weak));
148 extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr1 (_Unwind_State,
149 _Unwind_Control_Block *, _Unwind_Context *) __attribute__((weak));
150 extern _Unwind_Reason_Code __aeabi_unwind_cpp_pr2 (_Unwind_State,
151 _Unwind_Control_Block *, _Unwind_Context *) __attribute__((weak));
153 /* ABI defined routine to store a virtual register to memory. */
155 _Unwind_VRS_Result _Unwind_VRS_Get (_Unwind_Context *context,
156 _Unwind_VRS_RegClass regclass,
157 _uw regno,
158 _Unwind_VRS_DataRepresentation representation,
159 void *valuep)
161 phase1_vrs *vrs = (phase1_vrs *) context;
163 switch (regclass)
165 case _UVRSC_CORE:
166 if (representation != _UVRSD_UINT32
167 || regno > 15)
168 return _UVRSR_FAILED;
169 *(_uw *) valuep = vrs->core.r[regno];
170 return _UVRSR_OK;
172 case _UVRSC_VFP:
173 case _UVRSC_WMMXD:
174 case _UVRSC_WMMXC:
175 return _UVRSR_NOT_IMPLEMENTED;
177 default:
178 return _UVRSR_FAILED;
183 /* ABI defined function to load a virtual register from memory. */
185 _Unwind_VRS_Result _Unwind_VRS_Set (_Unwind_Context *context,
186 _Unwind_VRS_RegClass regclass,
187 _uw regno,
188 _Unwind_VRS_DataRepresentation representation,
189 void *valuep)
191 phase1_vrs *vrs = (phase1_vrs *) context;
193 switch (regclass)
195 case _UVRSC_CORE:
196 if (representation != _UVRSD_UINT32
197 || regno > 15)
198 return _UVRSR_FAILED;
200 vrs->core.r[regno] = *(_uw *) valuep;
201 return _UVRSR_OK;
203 case _UVRSC_VFP:
204 case _UVRSC_WMMXD:
205 case _UVRSC_WMMXC:
206 return _UVRSR_NOT_IMPLEMENTED;
208 default:
209 return _UVRSR_FAILED;
214 /* ABI defined function to pop registers off the stack. */
216 _Unwind_VRS_Result _Unwind_VRS_Pop (_Unwind_Context *context,
217 _Unwind_VRS_RegClass regclass,
218 _uw discriminator,
219 _Unwind_VRS_DataRepresentation representation)
221 phase1_vrs *vrs = (phase1_vrs *) context;
223 switch (regclass)
225 case _UVRSC_CORE:
227 _uw *ptr;
228 _uw mask;
229 int i;
231 if (representation != _UVRSD_UINT32)
232 return _UVRSR_FAILED;
234 mask = discriminator & 0xffff;
235 ptr = (_uw *) vrs->core.r[R_SP];
236 /* Pop the requested registers. */
237 for (i = 0; i < 16; i++)
239 if (mask & (1 << i))
240 vrs->core.r[i] = *(ptr++);
242 /* Writeback the stack pointer value if it wasn't restored. */
243 if ((mask & (1 << R_SP)) == 0)
244 vrs->core.r[R_SP] = (_uw) ptr;
246 return _UVRSR_OK;
248 case _UVRSC_VFP:
250 _uw start = discriminator >> 16;
251 _uw count = discriminator & 0xffff;
252 struct vfp_regs tmp;
253 struct vfpv3_regs tmp_16_to_31;
254 int tmp_count;
255 _uw *sp;
256 _uw *dest;
257 int num_vfpv3_regs = 0;
259 /* We use an approximation here by bounding _UVRSD_DOUBLE
260 register numbers at 32 always, since we can't detect if
261 VFPv3 isn't present (in such a case the upper limit is 16). */
262 if ((representation != _UVRSD_VFPX && representation != _UVRSD_DOUBLE)
263 || start + count > (representation == _UVRSD_VFPX ? 16 : 32)
264 || (representation == _UVRSD_VFPX && start >= 16))
265 return _UVRSR_FAILED;
267 /* Check if we're being asked to pop VFPv3-only registers
268 (numbers 16 through 31). */
269 if (start >= 16)
270 num_vfpv3_regs = count;
271 else if (start + count > 16)
272 num_vfpv3_regs = start + count - 16;
274 if (num_vfpv3_regs && representation != _UVRSD_DOUBLE)
275 return _UVRSR_FAILED;
277 /* Demand-save coprocessor registers for stage1. */
278 if (start < 16 && (vrs->demand_save_flags & DEMAND_SAVE_VFP))
280 vrs->demand_save_flags &= ~DEMAND_SAVE_VFP;
282 if (representation == _UVRSD_DOUBLE)
284 /* Save in FLDMD/FSTMD format. */
285 vrs->demand_save_flags |= DEMAND_SAVE_VFP_D;
286 __gnu_Unwind_Save_VFP_D (&vrs->vfp);
288 else
290 /* Save in FLDMX/FSTMX format. */
291 vrs->demand_save_flags &= ~DEMAND_SAVE_VFP_D;
292 __gnu_Unwind_Save_VFP (&vrs->vfp);
296 if (num_vfpv3_regs > 0
297 && (vrs->demand_save_flags & DEMAND_SAVE_VFP_V3))
299 vrs->demand_save_flags &= ~DEMAND_SAVE_VFP_V3;
300 __gnu_Unwind_Save_VFP_D_16_to_31 (&vrs->vfp_regs_16_to_31);
303 /* Restore the registers from the stack. Do this by saving the
304 current VFP registers to a memory area, moving the in-memory
305 values into that area, and restoring from the whole area.
306 For _UVRSD_VFPX we assume FSTMX standard format 1. */
307 if (representation == _UVRSD_VFPX)
308 __gnu_Unwind_Save_VFP (&tmp);
309 else
311 /* Save registers 0 .. 15 if required. */
312 if (start < 16)
313 __gnu_Unwind_Save_VFP_D (&tmp);
315 /* Save VFPv3 registers 16 .. 31 if required. */
316 if (num_vfpv3_regs)
317 __gnu_Unwind_Save_VFP_D_16_to_31 (&tmp_16_to_31);
320 /* Work out how many registers below register 16 need popping. */
321 tmp_count = num_vfpv3_regs > 0 ? 16 - start : count;
323 /* Copy registers below 16, if needed.
324 The stack address is only guaranteed to be word aligned, so
325 we can't use doubleword copies. */
326 sp = (_uw *) vrs->core.r[R_SP];
327 if (tmp_count > 0)
329 tmp_count *= 2;
330 dest = (_uw *) &tmp.d[start];
331 while (tmp_count--)
332 *(dest++) = *(sp++);
335 /* Copy VFPv3 registers numbered >= 16, if needed. */
336 if (num_vfpv3_regs > 0)
338 /* num_vfpv3_regs is needed below, so copy it. */
339 int tmp_count_2 = num_vfpv3_regs * 2;
340 int vfpv3_start = start < 16 ? 16 : start;
342 dest = (_uw *) &tmp_16_to_31.d[vfpv3_start - 16];
343 while (tmp_count_2--)
344 *(dest++) = *(sp++);
347 /* Skip the format word space if using FLDMX/FSTMX format. */
348 if (representation == _UVRSD_VFPX)
349 sp++;
351 /* Set the new stack pointer. */
352 vrs->core.r[R_SP] = (_uw) sp;
354 /* Reload the registers. */
355 if (representation == _UVRSD_VFPX)
356 __gnu_Unwind_Restore_VFP (&tmp);
357 else
359 /* Restore registers 0 .. 15 if required. */
360 if (start < 16)
361 __gnu_Unwind_Restore_VFP_D (&tmp);
363 /* Restore VFPv3 registers 16 .. 31 if required. */
364 if (num_vfpv3_regs > 0)
365 __gnu_Unwind_Restore_VFP_D_16_to_31 (&tmp_16_to_31);
368 return _UVRSR_OK;
370 case _UVRSC_WMMXD:
372 _uw start = discriminator >> 16;
373 _uw count = discriminator & 0xffff;
374 struct wmmxd_regs tmp;
375 _uw *sp;
376 _uw *dest;
378 if ((representation != _UVRSD_UINT64) || start + count > 16)
379 return _UVRSR_FAILED;
381 if (vrs->demand_save_flags & DEMAND_SAVE_WMMXD)
383 /* Demand-save resisters for stage1. */
384 vrs->demand_save_flags &= ~DEMAND_SAVE_WMMXD;
385 __gnu_Unwind_Save_WMMXD (&vrs->wmmxd);
388 /* Restore the registers from the stack. Do this by saving the
389 current WMMXD registers to a memory area, moving the in-memory
390 values into that area, and restoring from the whole area. */
391 __gnu_Unwind_Save_WMMXD (&tmp);
393 /* The stack address is only guaranteed to be word aligned, so
394 we can't use doubleword copies. */
395 sp = (_uw *) vrs->core.r[R_SP];
396 dest = (_uw *) &tmp.wd[start];
397 count *= 2;
398 while (count--)
399 *(dest++) = *(sp++);
401 /* Set the new stack pointer. */
402 vrs->core.r[R_SP] = (_uw) sp;
404 /* Reload the registers. */
405 __gnu_Unwind_Restore_WMMXD (&tmp);
407 return _UVRSR_OK;
409 case _UVRSC_WMMXC:
411 int i;
412 struct wmmxc_regs tmp;
413 _uw *sp;
415 if ((representation != _UVRSD_UINT32) || discriminator > 16)
416 return _UVRSR_FAILED;
418 if (vrs->demand_save_flags & DEMAND_SAVE_WMMXC)
420 /* Demand-save resisters for stage1. */
421 vrs->demand_save_flags &= ~DEMAND_SAVE_WMMXC;
422 __gnu_Unwind_Save_WMMXC (&vrs->wmmxc);
425 /* Restore the registers from the stack. Do this by saving the
426 current WMMXC registers to a memory area, moving the in-memory
427 values into that area, and restoring from the whole area. */
428 __gnu_Unwind_Save_WMMXC (&tmp);
430 sp = (_uw *) vrs->core.r[R_SP];
431 for (i = 0; i < 4; i++)
432 if (discriminator & (1 << i))
433 tmp.wc[i] = *(sp++);
435 /* Set the new stack pointer. */
436 vrs->core.r[R_SP] = (_uw) sp;
438 /* Reload the registers. */
439 __gnu_Unwind_Restore_WMMXC (&tmp);
441 return _UVRSR_OK;
443 default:
444 return _UVRSR_FAILED;
449 /* Core unwinding functions. */
451 /* Calculate the address encoded by a 31-bit self-relative offset at address
452 P. */
453 static inline _uw
454 selfrel_offset31 (const _uw *p)
456 _uw offset;
458 offset = *p;
459 /* Sign extend to 32 bits. */
460 if (offset & (1 << 30))
461 offset |= 1u << 31;
462 else
463 offset &= ~(1u << 31);
465 return offset + (_uw) p;
468 static _uw
469 __gnu_unwind_get_pr_addr (int idx)
471 switch (idx)
473 case 0:
474 return (_uw) &__aeabi_unwind_cpp_pr0;
476 case 1:
477 return (_uw) &__aeabi_unwind_cpp_pr1;
479 case 2:
480 return (_uw) &__aeabi_unwind_cpp_pr2;
482 default:
483 return 0;
487 /* ABI defined personality routine entry points. */
489 _Unwind_Reason_Code
490 __aeabi_unwind_cpp_pr0 (_Unwind_State state,
491 _Unwind_Control_Block *ucbp,
492 _Unwind_Context *context)
494 return __gnu_unwind_pr_common (state, ucbp, context, 0);
497 _Unwind_Reason_Code
498 __aeabi_unwind_cpp_pr1 (_Unwind_State state,
499 _Unwind_Control_Block *ucbp,
500 _Unwind_Context *context)
502 return __gnu_unwind_pr_common (state, ucbp, context, 1);
505 _Unwind_Reason_Code
506 __aeabi_unwind_cpp_pr2 (_Unwind_State state,
507 _Unwind_Control_Block *ucbp,
508 _Unwind_Context *context)
510 return __gnu_unwind_pr_common (state, ucbp, context, 2);
513 #ifdef __FreeBSD__
514 /* FreeBSD expects these to be functions */
515 inline _Unwind_Ptr
516 _Unwind_GetIP (struct _Unwind_Context *context)
518 return _Unwind_GetGR (context, 15) & ~(_Unwind_Word)1;
521 inline _Unwind_Ptr
522 _Unwind_GetIPInfo (struct _Unwind_Context *context, int *ip_before_insn)
524 *ip_before_insn = 0;
525 return _Unwind_GetIP (context);
528 inline void
529 _Unwind_SetIP (struct _Unwind_Context *context, _Unwind_Ptr val)
531 _Unwind_SetGR (context, 15, val | (_Unwind_GetGR (context, 15) & 1));
533 #endif