1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2000 Hewlett-Packard Co
4 * Copyright (C) 2000 David Mosberger-Tang <davidm@hpl.hp.com>
6 * Generic IA-64 unwind info decoder.
8 * This file is used both by the Linux kernel and objdump. Please keep
9 * the two copies of this file in sync.
11 * You need to customize the decoder by defining the following
12 * macros/constants before including this file:
15 * unw_word Unsigned integer type with at least 64 bits
29 * Decoder action macros:
30 * UNW_DEC_BAD_CODE(code)
31 * UNW_DEC_ABI(fmt,abi,context,arg)
32 * UNW_DEC_BR_GR(fmt,brmask,gr,arg)
33 * UNW_DEC_BR_MEM(fmt,brmask,arg)
34 * UNW_DEC_COPY_STATE(fmt,label,arg)
35 * UNW_DEC_EPILOGUE(fmt,t,ecount,arg)
36 * UNW_DEC_FRGR_MEM(fmt,grmask,frmask,arg)
37 * UNW_DEC_FR_MEM(fmt,frmask,arg)
38 * UNW_DEC_GR_GR(fmt,grmask,gr,arg)
39 * UNW_DEC_GR_MEM(fmt,grmask,arg)
40 * UNW_DEC_LABEL_STATE(fmt,label,arg)
41 * UNW_DEC_MEM_STACK_F(fmt,t,size,arg)
42 * UNW_DEC_MEM_STACK_V(fmt,t,arg)
43 * UNW_DEC_PRIUNAT_GR(fmt,r,arg)
44 * UNW_DEC_PRIUNAT_WHEN_GR(fmt,t,arg)
45 * UNW_DEC_PRIUNAT_WHEN_MEM(fmt,t,arg)
46 * UNW_DEC_PRIUNAT_WHEN_PSPREL(fmt,pspoff,arg)
47 * UNW_DEC_PRIUNAT_WHEN_SPREL(fmt,spoff,arg)
48 * UNW_DEC_PROLOGUE(fmt,body,rlen,arg)
49 * UNW_DEC_PROLOGUE_GR(fmt,rlen,mask,grsave,arg)
50 * UNW_DEC_REG_PSPREL(fmt,reg,pspoff,arg)
51 * UNW_DEC_REG_REG(fmt,src,dst,arg)
52 * UNW_DEC_REG_SPREL(fmt,reg,spoff,arg)
53 * UNW_DEC_REG_WHEN(fmt,reg,t,arg)
54 * UNW_DEC_RESTORE(fmt,t,abreg,arg)
55 * UNW_DEC_RESTORE_P(fmt,qp,t,abreg,arg)
56 * UNW_DEC_SPILL_BASE(fmt,pspoff,arg)
57 * UNW_DEC_SPILL_MASK(fmt,imaskp,arg)
58 * UNW_DEC_SPILL_PSPREL(fmt,t,abreg,pspoff,arg)
59 * UNW_DEC_SPILL_PSPREL_P(fmt,qp,t,abreg,pspoff,arg)
60 * UNW_DEC_SPILL_REG(fmt,t,abreg,x,ytreg,arg)
61 * UNW_DEC_SPILL_REG_P(fmt,qp,t,abreg,x,ytreg,arg)
62 * UNW_DEC_SPILL_SPREL(fmt,t,abreg,spoff,arg)
63 * UNW_DEC_SPILL_SPREL_P(fmt,qp,t,abreg,pspoff,arg)
67 unw_decode_uleb128 (unsigned char **dpp
)
70 unw_word byte
, result
= 0;
71 unsigned char *bp
= *dpp
;
76 result
|= (byte
& 0x7f) << shift
;
77 if ((byte
& 0x80) == 0)
85 static unsigned char *
86 unw_decode_x1 (unsigned char *dp
, unsigned char code
, void *arg
)
88 unsigned char byte1
, abreg
;
92 t
= unw_decode_uleb128 (&dp
);
93 off
= unw_decode_uleb128 (&dp
);
94 abreg
= (byte1
& 0x7f);
96 UNW_DEC_SPILL_SPREL(X1
, t
, abreg
, off
, arg
);
98 UNW_DEC_SPILL_PSPREL(X1
, t
, abreg
, off
, arg
);
102 static unsigned char *
103 unw_decode_x2 (unsigned char *dp
, unsigned char code
, void *arg
)
105 unsigned char byte1
, byte2
, abreg
, x
, ytreg
;
108 byte1
= *dp
++; byte2
= *dp
++;
109 t
= unw_decode_uleb128 (&dp
);
110 abreg
= (byte1
& 0x7f);
112 x
= (byte1
>> 7) & 1;
113 if ((byte1
& 0x80) == 0 && ytreg
== 0)
114 UNW_DEC_RESTORE(X2
, t
, abreg
, arg
);
116 UNW_DEC_SPILL_REG(X2
, t
, abreg
, x
, ytreg
, arg
);
120 static unsigned char *
121 unw_decode_x3 (unsigned char *dp
, unsigned char code
, void *arg
)
123 unsigned char byte1
, byte2
, abreg
, qp
;
126 byte1
= *dp
++; byte2
= *dp
++;
127 t
= unw_decode_uleb128 (&dp
);
128 off
= unw_decode_uleb128 (&dp
);
131 abreg
= (byte2
& 0x7f);
134 UNW_DEC_SPILL_SPREL_P(X3
, qp
, t
, abreg
, off
, arg
);
136 UNW_DEC_SPILL_PSPREL_P(X3
, qp
, t
, abreg
, off
, arg
);
140 static unsigned char *
141 unw_decode_x4 (unsigned char *dp
, unsigned char code
, void *arg
)
143 unsigned char byte1
, byte2
, byte3
, qp
, abreg
, x
, ytreg
;
146 byte1
= *dp
++; byte2
= *dp
++; byte3
= *dp
++;
147 t
= unw_decode_uleb128 (&dp
);
150 abreg
= (byte2
& 0x7f);
151 x
= (byte2
>> 7) & 1;
154 if ((byte2
& 0x80) == 0 && byte3
== 0)
155 UNW_DEC_RESTORE_P(X4
, qp
, t
, abreg
, arg
);
157 UNW_DEC_SPILL_REG_P(X4
, qp
, t
, abreg
, x
, ytreg
, arg
);
161 static unsigned char *
162 unw_decode_r1 (unsigned char *dp
, unsigned char code
, void *arg
)
164 int body
= (code
& 0x20) != 0;
167 rlen
= (code
& 0x1f);
168 UNW_DEC_PROLOGUE(R1
, body
, rlen
, arg
);
172 static unsigned char *
173 unw_decode_r2 (unsigned char *dp
, unsigned char code
, void *arg
)
175 unsigned char byte1
, mask
, grsave
;
180 mask
= ((code
& 0x7) << 1) | ((byte1
>> 7) & 1);
181 grsave
= (byte1
& 0x7f);
182 rlen
= unw_decode_uleb128 (&dp
);
183 UNW_DEC_PROLOGUE_GR(R2
, rlen
, mask
, grsave
, arg
);
187 static unsigned char *
188 unw_decode_r3 (unsigned char *dp
, unsigned char code
, void *arg
)
192 rlen
= unw_decode_uleb128 (&dp
);
193 UNW_DEC_PROLOGUE(R3
, ((code
& 0x3) == 1), rlen
, arg
);
197 static unsigned char *
198 unw_decode_p1 (unsigned char *dp
, unsigned char code
, void *arg
)
200 unsigned char brmask
= (code
& 0x1f);
202 UNW_DEC_BR_MEM(P1
, brmask
, arg
);
206 static unsigned char *
207 unw_decode_p2_p5 (unsigned char *dp
, unsigned char code
, void *arg
)
209 if ((code
& 0x10) == 0)
211 unsigned char byte1
= *dp
++;
213 UNW_DEC_BR_GR(P2
, ((code
& 0xf) << 1) | ((byte1
>> 7) & 1),
214 (byte1
& 0x7f), arg
);
216 else if ((code
& 0x08) == 0)
218 unsigned char byte1
= *dp
++, r
, dst
;
220 r
= ((code
& 0x7) << 1) | ((byte1
>> 7) & 1);
221 dst
= (byte1
& 0x7f);
224 case 0: UNW_DEC_REG_GR(P3
, UNW_REG_PSP
, dst
, arg
); break;
225 case 1: UNW_DEC_REG_GR(P3
, UNW_REG_RP
, dst
, arg
); break;
226 case 2: UNW_DEC_REG_GR(P3
, UNW_REG_PFS
, dst
, arg
); break;
227 case 3: UNW_DEC_REG_GR(P3
, UNW_REG_PR
, dst
, arg
); break;
228 case 4: UNW_DEC_REG_GR(P3
, UNW_REG_UNAT
, dst
, arg
); break;
229 case 5: UNW_DEC_REG_GR(P3
, UNW_REG_LC
, dst
, arg
); break;
230 case 6: UNW_DEC_RP_BR(P3
, dst
, arg
); break;
231 case 7: UNW_DEC_REG_GR(P3
, UNW_REG_RNAT
, dst
, arg
); break;
232 case 8: UNW_DEC_REG_GR(P3
, UNW_REG_BSP
, dst
, arg
); break;
233 case 9: UNW_DEC_REG_GR(P3
, UNW_REG_BSPSTORE
, dst
, arg
); break;
234 case 10: UNW_DEC_REG_GR(P3
, UNW_REG_FPSR
, dst
, arg
); break;
235 case 11: UNW_DEC_PRIUNAT_GR(P3
, dst
, arg
); break;
236 default: UNW_DEC_BAD_CODE(r
); break;
239 else if ((code
& 0x7) == 0)
240 UNW_DEC_SPILL_MASK(P4
, dp
, arg
);
241 else if ((code
& 0x7) == 1)
243 unw_word grmask
, frmask
, byte1
, byte2
, byte3
;
245 byte1
= *dp
++; byte2
= *dp
++; byte3
= *dp
++;
246 grmask
= ((byte1
>> 4) & 0xf);
247 frmask
= ((byte1
& 0xf) << 16) | (byte2
<< 8) | byte3
;
248 UNW_DEC_FRGR_MEM(P5
, grmask
, frmask
, arg
);
251 UNW_DEC_BAD_CODE(code
);
255 static unsigned char *
256 unw_decode_p6 (unsigned char *dp
, unsigned char code
, void *arg
)
258 int gregs
= (code
& 0x10) != 0;
259 unsigned char mask
= (code
& 0x0f);
262 UNW_DEC_GR_MEM(P6
, mask
, arg
);
264 UNW_DEC_FR_MEM(P6
, mask
, arg
);
268 static unsigned char *
269 unw_decode_p7_p10 (unsigned char *dp
, unsigned char code
, void *arg
)
271 unsigned char r
, byte1
, byte2
;
274 if ((code
& 0x10) == 0)
277 t
= unw_decode_uleb128 (&dp
);
281 size
= unw_decode_uleb128 (&dp
);
282 UNW_DEC_MEM_STACK_F(P7
, t
, size
, arg
);
285 case 1: UNW_DEC_MEM_STACK_V(P7
, t
, arg
); break;
286 case 2: UNW_DEC_SPILL_BASE(P7
, t
, arg
); break;
287 case 3: UNW_DEC_REG_SPREL(P7
, UNW_REG_PSP
, t
, arg
); break;
288 case 4: UNW_DEC_REG_WHEN(P7
, UNW_REG_RP
, t
, arg
); break;
289 case 5: UNW_DEC_REG_PSPREL(P7
, UNW_REG_RP
, t
, arg
); break;
290 case 6: UNW_DEC_REG_WHEN(P7
, UNW_REG_PFS
, t
, arg
); break;
291 case 7: UNW_DEC_REG_PSPREL(P7
, UNW_REG_PFS
, t
, arg
); break;
292 case 8: UNW_DEC_REG_WHEN(P7
, UNW_REG_PR
, t
, arg
); break;
293 case 9: UNW_DEC_REG_PSPREL(P7
, UNW_REG_PR
, t
, arg
); break;
294 case 10: UNW_DEC_REG_WHEN(P7
, UNW_REG_LC
, t
, arg
); break;
295 case 11: UNW_DEC_REG_PSPREL(P7
, UNW_REG_LC
, t
, arg
); break;
296 case 12: UNW_DEC_REG_WHEN(P7
, UNW_REG_UNAT
, t
, arg
); break;
297 case 13: UNW_DEC_REG_PSPREL(P7
, UNW_REG_UNAT
, t
, arg
); break;
298 case 14: UNW_DEC_REG_WHEN(P7
, UNW_REG_FPSR
, t
, arg
); break;
299 case 15: UNW_DEC_REG_PSPREL(P7
, UNW_REG_FPSR
, t
, arg
); break;
300 default: UNW_DEC_BAD_CODE(r
); break;
310 t
= unw_decode_uleb128 (&dp
);
313 case 1: UNW_DEC_REG_SPREL(P8
, UNW_REG_RP
, t
, arg
); break;
314 case 2: UNW_DEC_REG_SPREL(P8
, UNW_REG_PFS
, t
, arg
); break;
315 case 3: UNW_DEC_REG_SPREL(P8
, UNW_REG_PR
, t
, arg
); break;
316 case 4: UNW_DEC_REG_SPREL(P8
, UNW_REG_LC
, t
, arg
); break;
317 case 5: UNW_DEC_REG_SPREL(P8
, UNW_REG_UNAT
, t
, arg
); break;
318 case 6: UNW_DEC_REG_SPREL(P8
, UNW_REG_FPSR
, t
, arg
); break;
319 case 7: UNW_DEC_REG_WHEN(P8
, UNW_REG_BSP
, t
, arg
); break;
320 case 8: UNW_DEC_REG_PSPREL(P8
, UNW_REG_BSP
, t
, arg
); break;
321 case 9: UNW_DEC_REG_SPREL(P8
, UNW_REG_BSP
, t
, arg
); break;
322 case 10: UNW_DEC_REG_WHEN(P8
, UNW_REG_BSPSTORE
, t
, arg
); break;
323 case 11: UNW_DEC_REG_PSPREL(P8
, UNW_REG_BSPSTORE
, t
, arg
); break;
324 case 12: UNW_DEC_REG_SPREL(P8
, UNW_REG_BSPSTORE
, t
, arg
); break;
325 case 13: UNW_DEC_REG_WHEN(P8
, UNW_REG_RNAT
, t
, arg
); break;
326 case 14: UNW_DEC_REG_PSPREL(P8
, UNW_REG_RNAT
, t
, arg
); break;
327 case 15: UNW_DEC_REG_SPREL(P8
, UNW_REG_RNAT
, t
, arg
); break;
328 case 16: UNW_DEC_PRIUNAT_WHEN_GR(P8
, t
, arg
); break;
329 case 17: UNW_DEC_PRIUNAT_PSPREL(P8
, t
, arg
); break;
330 case 18: UNW_DEC_PRIUNAT_SPREL(P8
, t
, arg
); break;
331 case 19: UNW_DEC_PRIUNAT_WHEN_MEM(P8
, t
, arg
); break;
332 default: UNW_DEC_BAD_CODE(r
); break;
338 byte1
= *dp
++; byte2
= *dp
++;
339 UNW_DEC_GR_GR(P9
, (byte1
& 0xf), (byte2
& 0x7f), arg
);
343 byte1
= *dp
++; byte2
= *dp
++;
344 UNW_DEC_ABI(P10
, byte1
, byte2
, arg
);
348 return unw_decode_x1 (dp
, code
, arg
);
351 return unw_decode_x2 (dp
, code
, arg
);
354 return unw_decode_x3 (dp
, code
, arg
);
357 return unw_decode_x4 (dp
, code
, arg
);
360 UNW_DEC_BAD_CODE(code
);
367 static unsigned char *
368 unw_decode_b1 (unsigned char *dp
, unsigned char code
, void *arg
)
370 unw_word label
= (code
& 0x1f);
372 if ((code
& 0x20) != 0)
373 UNW_DEC_COPY_STATE(B1
, label
, arg
);
375 UNW_DEC_LABEL_STATE(B1
, label
, arg
);
379 static unsigned char *
380 unw_decode_b2 (unsigned char *dp
, unsigned char code
, void *arg
)
384 t
= unw_decode_uleb128 (&dp
);
385 UNW_DEC_EPILOGUE(B2
, t
, (code
& 0x1f), arg
);
389 static unsigned char *
390 unw_decode_b3_x4 (unsigned char *dp
, unsigned char code
, void *arg
)
392 unw_word t
, ecount
, label
;
394 if ((code
& 0x10) == 0)
396 t
= unw_decode_uleb128 (&dp
);
397 ecount
= unw_decode_uleb128 (&dp
);
398 UNW_DEC_EPILOGUE(B3
, t
, ecount
, arg
);
400 else if ((code
& 0x07) == 0)
402 label
= unw_decode_uleb128 (&dp
);
403 if ((code
& 0x08) != 0)
404 UNW_DEC_COPY_STATE(B4
, label
, arg
);
406 UNW_DEC_LABEL_STATE(B4
, label
, arg
);
411 case 1: return unw_decode_x1 (dp
, code
, arg
);
412 case 2: return unw_decode_x2 (dp
, code
, arg
);
413 case 3: return unw_decode_x3 (dp
, code
, arg
);
414 case 4: return unw_decode_x4 (dp
, code
, arg
);
415 default: UNW_DEC_BAD_CODE(code
); break;
420 typedef unsigned char *(*unw_decoder
) (unsigned char *, unsigned char, void *);
422 static unw_decoder unw_decode_table
[2][8] =
424 /* prologue table: */
426 unw_decode_r1
, /* 0 */
430 unw_decode_p1
, /* 4 */
436 unw_decode_r1
, /* 0 */
440 unw_decode_b1
, /* 4 */
448 * Decode one descriptor and return address of next descriptor.
450 static inline unsigned char *
451 unw_decode (unsigned char *dp
, int inside_body
, void *arg
)
457 decoder
= unw_decode_table
[inside_body
][code
>> 5];
458 dp
= (*decoder
) (dp
, code
, arg
);