1/* DWARF2 EH unwinding support for PowerPC64 FreeBSD.
2   Copyright (C) 2012-2015 Free Software Foundation, Inc.
3
4   This file is part of GCC.
5
6   GCC is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published
8   by the Free Software Foundation; either version 3, or (at your
9   option) any later version.
10
11   GCC is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
14   License for more details.
15
16   Under Section 7 of GPL version 3, you are granted additional
17   permissions described in the GCC Runtime Library Exception, version
18   3.1, as published by the Free Software Foundation.
19
20   You should have received a copy of the GNU General Public License and
21   a copy of the GCC Runtime Library Exception along with this program;
22   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23   <http://www.gnu.org/licenses/>.  */
24
25#define R_LR		65
26
27#define MD_FROB_UPDATE_CONTEXT frob_update_context
28
29static void
30frob_update_context (struct _Unwind_Context *context,
31		     _Unwind_FrameState *fs ATTRIBUTE_UNUSED)
32{
33  const unsigned int *pc = (const unsigned int *) context->ra;
34
35#ifdef __powerpc64__
36  if (fs->regs.reg[2].how == REG_UNSAVED)
37    {
38      /* If the current unwind info (FS) does not contain explicit info
39	 saving R2, then we have to do a minor amount of code reading to
40	 figure out if it was saved.  The big problem here is that the
41	 code that does the save/restore is generated by the linker, so
42	 we have no good way to determine at compile time what to do.  */
43      if (pc[0] == 0xF8410028
44	  || ((pc[0] & 0xFFFF0000) == 0x3D820000
45	      && pc[1] == 0xF8410028))
46	{
47	  /* We are in a plt call stub or r2 adjusting long branch stub,
48	     before r2 has been saved.  Keep REG_UNSAVED.  */
49	}
50      else
51	{
52	  unsigned int *insn
53	    = (unsigned int *) _Unwind_GetGR (context, R_LR);
54	  if (insn && *insn == 0xE8410028)
55	    _Unwind_SetGRPtr (context, 2, context->cfa + 40);
56	  else if (pc[0] == 0x4E800421
57		   && pc[1] == 0xE8410028)
58	    {
59	      /* We are at the bctrl instruction in a call via function
60		 pointer.  gcc always emits the load of the new R2 just
61		 before the bctrl so this is the first and only place
62		 we need to use the stored R2.  */
63	      _Unwind_Word sp = _Unwind_GetGR (context, 1);
64	      _Unwind_SetGRPtr (context, 2, (void *)(sp + 40));
65	    }
66	}
67    }
68#endif
69}
70