1 /*
2 ** DynASM ARM64 encoding engine.
3 ** Copyright (C) 2005-2021 Mike Pall. All rights reserved.
4 ** Released under the MIT license. See dynasm.lua for full copyright notice.
5 */
6
7 #include <stddef.h>
8 #include <stdarg.h>
9 #include <string.h>
10 #include <stdlib.h>
11
12 #define DASM_ARCH "arm64"
13
14 #ifndef DASM_EXTERN
15 #define DASM_EXTERN(a,b,c,d) 0
16 #endif
17
18 /* Action definitions. */
19 enum {
20 DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT,
21 /* The following actions need a buffer position. */
22 DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG,
23 /* The following actions also have an argument. */
24 DASM_REL_PC, DASM_LABEL_PC, DASM_REL_A,
25 DASM_IMM, DASM_IMM6, DASM_IMM12, DASM_IMM13W, DASM_IMM13X, DASM_IMML,
26 DASM_IMMV, DASM_VREG,
27 DASM__MAX
28 };
29
30 /* Maximum number of section buffer positions for a single dasm_put() call. */
31 #define DASM_MAXSECPOS 25
32
33 /* DynASM encoder status codes. Action list offset or number are or'ed in. */
34 #define DASM_S_OK 0x00000000
35 #define DASM_S_NOMEM 0x01000000
36 #define DASM_S_PHASE 0x02000000
37 #define DASM_S_MATCH_SEC 0x03000000
38 #define DASM_S_RANGE_I 0x11000000
39 #define DASM_S_RANGE_SEC 0x12000000
40 #define DASM_S_RANGE_LG 0x13000000
41 #define DASM_S_RANGE_PC 0x14000000
42 #define DASM_S_RANGE_REL 0x15000000
43 #define DASM_S_RANGE_VREG 0x16000000
44 #define DASM_S_UNDEF_LG 0x21000000
45 #define DASM_S_UNDEF_PC 0x22000000
46
47 /* Macros to convert positions (8 bit section + 24 bit index). */
48 #define DASM_POS2IDX(pos) ((pos)&0x00ffffff)
49 #define DASM_POS2BIAS(pos) ((pos)&0xff000000)
50 #define DASM_SEC2POS(sec) ((sec)<<24)
51 #define DASM_POS2SEC(pos) ((pos)>>24)
52 #define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos))
53
54 /* Action list type. */
55 typedef const unsigned int *dasm_ActList;
56
57 /* Per-section structure. */
58 typedef struct dasm_Section {
59 int *rbuf; /* Biased buffer pointer (negative section bias). */
60 int *buf; /* True buffer pointer. */
61 size_t bsize; /* Buffer size in bytes. */
62 int pos; /* Biased buffer position. */
63 int epos; /* End of biased buffer position - max single put. */
64 int ofs; /* Byte offset into section. */
65 } dasm_Section;
66
67 /* Core structure holding the DynASM encoding state. */
68 struct dasm_State {
69 size_t psize; /* Allocated size of this structure. */
70 dasm_ActList actionlist; /* Current actionlist pointer. */
71 int *lglabels; /* Local/global chain/pos ptrs. */
72 size_t lgsize;
73 int *pclabels; /* PC label chains/pos ptrs. */
74 size_t pcsize;
75 void **globals; /* Array of globals (bias -10). */
76 dasm_Section *section; /* Pointer to active section. */
77 size_t codesize; /* Total size of all code sections. */
78 int maxsection; /* 0 <= sectionidx < maxsection. */
79 int status; /* Status code. */
80 dasm_Section sections[1]; /* All sections. Alloc-extended. */
81 };
82
83 /* The size of the core structure depends on the max. number of sections. */
84 #define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section))
85
86
87 /* Initialize DynASM state. */
dasm_init(Dst_DECL,int maxsection)88 void dasm_init(Dst_DECL, int maxsection)
89 {
90 dasm_State *D;
91 size_t psz = 0;
92 int i;
93 Dst_REF = NULL;
94 DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection));
95 D = Dst_REF;
96 D->psize = psz;
97 D->lglabels = NULL;
98 D->lgsize = 0;
99 D->pclabels = NULL;
100 D->pcsize = 0;
101 D->globals = NULL;
102 D->maxsection = maxsection;
103 for (i = 0; i < maxsection; i++) {
104 D->sections[i].buf = NULL; /* Need this for pass3. */
105 D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i);
106 D->sections[i].bsize = 0;
107 D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */
108 }
109 }
110
111 /* Free DynASM state. */
dasm_free(Dst_DECL)112 void dasm_free(Dst_DECL)
113 {
114 dasm_State *D = Dst_REF;
115 int i;
116 for (i = 0; i < D->maxsection; i++)
117 if (D->sections[i].buf)
118 DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize);
119 if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize);
120 if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize);
121 DASM_M_FREE(Dst, D, D->psize);
122 }
123
124 /* Setup global label array. Must be called before dasm_setup(). */
dasm_setupglobal(Dst_DECL,void ** gl,unsigned int maxgl)125 void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl)
126 {
127 dasm_State *D = Dst_REF;
128 D->globals = gl - 10; /* Negative bias to compensate for locals. */
129 DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int));
130 }
131
132 /* Grow PC label array. Can be called after dasm_setup(), too. */
dasm_growpc(Dst_DECL,unsigned int maxpc)133 void dasm_growpc(Dst_DECL, unsigned int maxpc)
134 {
135 dasm_State *D = Dst_REF;
136 size_t osz = D->pcsize;
137 DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int));
138 memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz);
139 }
140
141 /* Setup encoder. */
dasm_setup(Dst_DECL,const void * actionlist)142 void dasm_setup(Dst_DECL, const void *actionlist)
143 {
144 dasm_State *D = Dst_REF;
145 int i;
146 D->actionlist = (dasm_ActList)actionlist;
147 D->status = DASM_S_OK;
148 D->section = &D->sections[0];
149 memset((void *)D->lglabels, 0, D->lgsize);
150 if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize);
151 for (i = 0; i < D->maxsection; i++) {
152 D->sections[i].pos = DASM_SEC2POS(i);
153 D->sections[i].ofs = 0;
154 }
155 }
156
157
158 #ifdef DASM_CHECKS
159 #define CK(x, st) \
160 do { if (!(x)) { \
161 D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0)
162 #define CKPL(kind, st) \
163 do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \
164 D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0)
165 #else
166 #define CK(x, st) ((void)0)
167 #define CKPL(kind, st) ((void)0)
168 #endif
169
dasm_imm12(unsigned int n)170 static int dasm_imm12(unsigned int n)
171 {
172 if ((n >> 12) == 0)
173 return n;
174 else if ((n & 0xff000fff) == 0)
175 return (n >> 12) | 0x1000;
176 else
177 return -1;
178 }
179
dasm_ffs(unsigned long long x)180 static int dasm_ffs(unsigned long long x)
181 {
182 int n = -1;
183 while (x) { x >>= 1; n++; }
184 return n;
185 }
186
dasm_imm13(int lo,int hi)187 static int dasm_imm13(int lo, int hi)
188 {
189 int inv = 0, w = 64, s = 0xfff, xa, xb;
190 unsigned long long n = (((unsigned long long)hi) << 32) | (unsigned int)lo;
191 unsigned long long m = 1ULL, a, b, c;
192 if (n & 1) { n = ~n; inv = 1; }
193 a = n & -n; b = (n+a)&-(n+a); c = (n+a-b)&-(n+a-b);
194 xa = dasm_ffs(a); xb = dasm_ffs(b);
195 if (c) {
196 w = dasm_ffs(c) - xa;
197 if (w == 32) m = 0x0000000100000001UL;
198 else if (w == 16) m = 0x0001000100010001UL;
199 else if (w == 8) m = 0x0101010101010101UL;
200 else if (w == 4) m = 0x1111111111111111UL;
201 else if (w == 2) m = 0x5555555555555555UL;
202 else return -1;
203 s = (-2*w & 0x3f) - 1;
204 } else if (!a) {
205 return -1;
206 } else if (xb == -1) {
207 xb = 64;
208 }
209 if ((b-a) * m != n) return -1;
210 if (inv) {
211 return ((w - xb) << 6) | (s+w+xa-xb);
212 } else {
213 return ((w - xa) << 6) | (s+xb-xa);
214 }
215 return -1;
216 }
217
218 /* Pass 1: Store actions and args, link branches/labels, estimate offsets. */
dasm_put(Dst_DECL,int start,...)219 void dasm_put(Dst_DECL, int start, ...)
220 {
221 va_list ap;
222 dasm_State *D = Dst_REF;
223 dasm_ActList p = D->actionlist + start;
224 dasm_Section *sec = D->section;
225 int pos = sec->pos, ofs = sec->ofs;
226 int *b;
227
228 if (pos >= sec->epos) {
229 DASM_M_GROW(Dst, int, sec->buf, sec->bsize,
230 sec->bsize + 2*DASM_MAXSECPOS*sizeof(int));
231 sec->rbuf = sec->buf - DASM_POS2BIAS(pos);
232 sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos);
233 }
234
235 b = sec->rbuf;
236 b[pos++] = start;
237
238 va_start(ap, start);
239 while (1) {
240 unsigned int ins = *p++;
241 unsigned int action = (ins >> 16);
242 if (action >= DASM__MAX) {
243 ofs += 4;
244 } else {
245 int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0;
246 switch (action) {
247 case DASM_STOP: goto stop;
248 case DASM_SECTION:
249 n = (ins & 255); CK(n < D->maxsection, RANGE_SEC);
250 D->section = &D->sections[n]; goto stop;
251 case DASM_ESC: p++; ofs += 4; break;
252 case DASM_REL_EXT: if ((ins & 0x8000)) ofs += 8; break;
253 case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break;
254 case DASM_REL_LG:
255 n = (ins & 2047) - 10; pl = D->lglabels + n;
256 /* Bkwd rel or global. */
257 if (n >= 0) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; }
258 pl += 10; n = *pl;
259 if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */
260 goto linkrel;
261 case DASM_REL_PC:
262 pl = D->pclabels + n; CKPL(pc, PC);
263 putrel:
264 n = *pl;
265 if (n < 0) { /* Label exists. Get label pos and store it. */
266 b[pos] = -n;
267 } else {
268 linkrel:
269 b[pos] = n; /* Else link to rel chain, anchored at label. */
270 *pl = pos;
271 }
272 pos++;
273 if ((ins & 0x8000)) ofs += 8;
274 break;
275 case DASM_REL_A:
276 b[pos++] = n;
277 b[pos++] = va_arg(ap, int);
278 break;
279 case DASM_LABEL_LG:
280 pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel;
281 case DASM_LABEL_PC:
282 pl = D->pclabels + n; CKPL(pc, PC);
283 putlabel:
284 n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */
285 while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos;
286 }
287 *pl = -pos; /* Label exists now. */
288 b[pos++] = ofs; /* Store pass1 offset estimate. */
289 break;
290 case DASM_IMM:
291 CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I);
292 n >>= ((ins>>10)&31);
293 #ifdef DASM_CHECKS
294 if ((ins & 0x8000))
295 CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I);
296 else
297 CK((n>>((ins>>5)&31)) == 0, RANGE_I);
298 #endif
299 b[pos++] = n;
300 break;
301 case DASM_IMM6:
302 CK((n >> 6) == 0, RANGE_I);
303 b[pos++] = n;
304 break;
305 case DASM_IMM12:
306 CK(dasm_imm12((unsigned int)n) != -1, RANGE_I);
307 b[pos++] = n;
308 break;
309 case DASM_IMM13W:
310 CK(dasm_imm13(n, n) != -1, RANGE_I);
311 b[pos++] = n;
312 break;
313 case DASM_IMM13X: {
314 int m = va_arg(ap, int);
315 CK(dasm_imm13(n, m) != -1, RANGE_I);
316 b[pos++] = n;
317 b[pos++] = m;
318 break;
319 }
320 case DASM_IMML: {
321 #ifdef DASM_CHECKS
322 int scale = (ins & 3);
323 CK((!(n & ((1<<scale)-1)) && (unsigned int)(n>>scale) < 4096) ||
324 (unsigned int)(n+256) < 512, RANGE_I);
325 #endif
326 b[pos++] = n;
327 break;
328 }
329 case DASM_IMMV:
330 ofs += 4;
331 b[pos++] = n;
332 break;
333 case DASM_VREG:
334 CK(n < 32, RANGE_VREG);
335 b[pos++] = n;
336 break;
337 }
338 }
339 }
340 stop:
341 va_end(ap);
342 sec->pos = pos;
343 sec->ofs = ofs;
344 }
345 #undef CK
346
347 /* Pass 2: Link sections, shrink aligns, fix label offsets. */
dasm_link(Dst_DECL,size_t * szp)348 int dasm_link(Dst_DECL, size_t *szp)
349 {
350 dasm_State *D = Dst_REF;
351 int secnum;
352 int ofs = 0;
353
354 #ifdef DASM_CHECKS
355 *szp = 0;
356 if (D->status != DASM_S_OK) return D->status;
357 {
358 int pc;
359 for (pc = 0; pc*sizeof(int) < D->pcsize; pc++)
360 if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc;
361 }
362 #endif
363
364 { /* Handle globals not defined in this translation unit. */
365 int idx;
366 for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) {
367 int n = D->lglabels[idx];
368 /* Undefined label: Collapse rel chain and replace with marker (< 0). */
369 while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; }
370 }
371 }
372
373 /* Combine all code sections. No support for data sections (yet). */
374 for (secnum = 0; secnum < D->maxsection; secnum++) {
375 dasm_Section *sec = D->sections + secnum;
376 int *b = sec->rbuf;
377 int pos = DASM_SEC2POS(secnum);
378 int lastpos = sec->pos;
379
380 while (pos != lastpos) {
381 dasm_ActList p = D->actionlist + b[pos++];
382 while (1) {
383 unsigned int ins = *p++;
384 unsigned int action = (ins >> 16);
385 switch (action) {
386 case DASM_STOP: case DASM_SECTION: goto stop;
387 case DASM_ESC: p++; break;
388 case DASM_REL_EXT: break;
389 case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break;
390 case DASM_REL_LG: case DASM_REL_PC: pos++; break;
391 case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break;
392 case DASM_IMM: case DASM_IMM6: case DASM_IMM12: case DASM_IMM13W:
393 case DASM_IMML: case DASM_IMMV: case DASM_VREG: pos++; break;
394 case DASM_IMM13X: case DASM_REL_A: pos += 2; break;
395 }
396 }
397 stop: (void)0;
398 }
399 ofs += sec->ofs; /* Next section starts right after current section. */
400 }
401
402 D->codesize = ofs; /* Total size of all code sections */
403 *szp = ofs;
404 return DASM_S_OK;
405 }
406
407 #ifdef DASM_ADD_VENEER
408 #define CK_REL(x, o) \
409 do { if (!(x) && !(n = DASM_ADD_VENEER(D, buffer, ins, b, cp, o))) \
410 return DASM_S_RANGE_REL|(p-D->actionlist-1); \
411 } while (0)
412 #else
413 #define CK_REL(x, o) CK(x, RANGE_REL)
414 #endif
415
416 #ifdef DASM_CHECKS
417 #define CK(x, st) \
418 do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0)
419 #else
420 #define CK(x, st) ((void)0)
421 #endif
422
423 /* Pass 3: Encode sections. */
dasm_encode(Dst_DECL,void * buffer)424 int dasm_encode(Dst_DECL, void *buffer)
425 {
426 dasm_State *D = Dst_REF;
427 char *base = (char *)buffer;
428 unsigned int *cp = (unsigned int *)buffer;
429 int secnum;
430
431 /* Encode all code sections. No support for data sections (yet). */
432 for (secnum = 0; secnum < D->maxsection; secnum++) {
433 dasm_Section *sec = D->sections + secnum;
434 int *b = sec->buf;
435 int *endb = sec->rbuf + sec->pos;
436
437 while (b != endb) {
438 dasm_ActList p = D->actionlist + *b++;
439 while (1) {
440 unsigned int ins = *p++;
441 unsigned int action = (ins >> 16);
442 int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0;
443 switch (action) {
444 case DASM_STOP: case DASM_SECTION: goto stop;
445 case DASM_ESC: *cp++ = *p++; break;
446 case DASM_REL_EXT:
447 n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins&2047), !(ins&2048));
448 goto patchrel;
449 case DASM_ALIGN:
450 ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0xe1a00000;
451 break;
452 case DASM_REL_LG:
453 if (n < 0) {
454 ptrdiff_t na = (ptrdiff_t)D->globals[-n] - (ptrdiff_t)cp + 4;
455 n = (int)na;
456 CK_REL((ptrdiff_t)n == na, na);
457 goto patchrel;
458 }
459 /* fallthrough */
460 case DASM_REL_PC:
461 CK(n >= 0, UNDEF_PC);
462 n = *DASM_POS2PTR(D, n) - (int)((char *)cp - base) + 4;
463 patchrel:
464 if (!(ins & 0xf800)) { /* B, BL */
465 CK_REL((n & 3) == 0 && ((n+0x08000000) >> 28) == 0, n);
466 cp[-1] |= ((n >> 2) & 0x03ffffff);
467 } else if ((ins & 0x800)) { /* B.cond, CBZ, CBNZ, LDR* literal */
468 CK_REL((n & 3) == 0 && ((n+0x00100000) >> 21) == 0, n);
469 cp[-1] |= ((n << 3) & 0x00ffffe0);
470 } else if ((ins & 0x3000) == 0x2000) { /* ADR */
471 CK_REL(((n+0x00100000) >> 21) == 0, n);
472 cp[-1] |= ((n << 3) & 0x00ffffe0) | ((n & 3) << 29);
473 } else if ((ins & 0x3000) == 0x3000) { /* ADRP */
474 cp[-1] |= ((n >> 9) & 0x00ffffe0) | (((n >> 12) & 3) << 29);
475 } else if ((ins & 0x1000)) { /* TBZ, TBNZ */
476 CK_REL((n & 3) == 0 && ((n+0x00008000) >> 16) == 0, n);
477 cp[-1] |= ((n << 3) & 0x0007ffe0);
478 } else if ((ins & 0x8000)) { /* absolute */
479 cp[0] = (unsigned int)((ptrdiff_t)cp - 4 + n);
480 cp[1] = (unsigned int)(((ptrdiff_t)cp - 4 + n) >> 32);
481 cp += 2;
482 }
483 break;
484 case DASM_REL_A: {
485 ptrdiff_t na = (((ptrdiff_t)(*b++) << 32) | (unsigned int)n);
486 if ((ins & 0x3000) == 0x3000) { /* ADRP */
487 ins &= ~0x1000;
488 na = (na >> 12) - (((ptrdiff_t)cp - 4) >> 12);
489 } else {
490 na = na - (ptrdiff_t)cp + 4;
491 }
492 n = (int)na;
493 CK_REL((ptrdiff_t)n == na, na);
494 goto patchrel;
495 }
496 case DASM_LABEL_LG:
497 ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n);
498 break;
499 case DASM_LABEL_PC: break;
500 case DASM_IMM:
501 cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31);
502 break;
503 case DASM_IMM6:
504 cp[-1] |= ((n&31) << 19) | ((n&32) << 26);
505 break;
506 case DASM_IMM12:
507 cp[-1] |= (dasm_imm12((unsigned int)n) << 10);
508 break;
509 case DASM_IMM13W:
510 cp[-1] |= (dasm_imm13(n, n) << 10);
511 break;
512 case DASM_IMM13X:
513 cp[-1] |= (dasm_imm13(n, *b++) << 10);
514 break;
515 case DASM_IMML: {
516 int scale = (ins & 3);
517 cp[-1] |= (!(n & ((1<<scale)-1)) && (unsigned int)(n>>scale) < 4096) ?
518 ((n << (10-scale)) | 0x01000000) : ((n & 511) << 12);
519 break;
520 }
521 case DASM_IMMV:
522 *cp++ = n;
523 break;
524 case DASM_VREG:
525 cp[-1] |= (n & 0x1f) << (ins & 0x1f);
526 break;
527 default: *cp++ = ins; break;
528 }
529 }
530 stop: (void)0;
531 }
532 }
533
534 if (base + D->codesize != (char *)cp) /* Check for phase errors. */
535 return DASM_S_PHASE;
536 return DASM_S_OK;
537 }
538 #undef CK
539
540 /* Get PC label offset. */
dasm_getpclabel(Dst_DECL,unsigned int pc)541 int dasm_getpclabel(Dst_DECL, unsigned int pc)
542 {
543 dasm_State *D = Dst_REF;
544 if (pc*sizeof(int) < D->pcsize) {
545 int pos = D->pclabels[pc];
546 if (pos < 0) return *DASM_POS2PTR(D, -pos);
547 if (pos > 0) return -1; /* Undefined. */
548 }
549 return -2; /* Unused or out of range. */
550 }
551
552 #ifdef DASM_CHECKS
553 /* Optional sanity checker to call between isolated encoding steps. */
dasm_checkstep(Dst_DECL,int secmatch)554 int dasm_checkstep(Dst_DECL, int secmatch)
555 {
556 dasm_State *D = Dst_REF;
557 if (D->status == DASM_S_OK) {
558 int i;
559 for (i = 1; i <= 9; i++) {
560 if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; }
561 D->lglabels[i] = 0;
562 }
563 }
564 if (D->status == DASM_S_OK && secmatch >= 0 &&
565 D->section != &D->sections[secmatch])
566 D->status = DASM_S_MATCH_SEC|(D->section-D->sections);
567 return D->status;
568 }
569 #endif
570
571