1 /*
2 ** DynASM x86 encoding engine.
3 ** Copyright (C) 2005-2016 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		"x86"
13 
14 #ifndef DASM_EXTERN
15 #define DASM_EXTERN(a,b,c,d)	0
16 #endif
17 
18 /* Action definitions. DASM_STOP must be 255. */
19 enum {
20   DASM_DISP = 231,
21   DASM_IMM_S, DASM_IMM_B, DASM_IMM_W, DASM_IMM_D, DASM_IMM_WB, DASM_IMM_DB,
22   DASM_VREG, DASM_SPACE, DASM_SETLABEL, DASM_REL_A, DASM_REL_LG, DASM_REL_PC,
23   DASM_IMM_LG, DASM_IMM_LG64, DASM_IMM_PC, DASM_IMM_PC64, DASM_LABEL_LG, DASM_LABEL_PC,
24   DASM_ALIGN, DASM_EXTERN, DASM_ESC, DASM_MARK, DASM_SECTION, DASM_STOP
25 };
26 
27 /* Maximum number of section buffer positions for a single dasm_put() call. */
28 #define DASM_MAXSECPOS		25
29 
30 /* DynASM encoder status codes. Action list offset or number are or'ed in. */
31 #define DASM_S_OK		0x00000000
32 #define DASM_S_NOMEM		0x01000000
33 #define DASM_S_PHASE		0x02000000
34 #define DASM_S_MATCH_SEC	0x03000000
35 #define DASM_S_RANGE_I		0x11000000
36 #define DASM_S_RANGE_SEC	0x12000000
37 #define DASM_S_RANGE_LG		0x13000000
38 #define DASM_S_RANGE_PC		0x14000000
39 #define DASM_S_RANGE_VREG	0x15000000
40 #define DASM_S_UNDEF_L		0x21000000
41 #define DASM_S_UNDEF_PC		0x22000000
42 
43 /* Macros to convert positions (8 bit section + 24 bit index). */
44 #define DASM_POS2IDX(pos)	((pos)&0x00ffffff)
45 #define DASM_POS2BIAS(pos)	((pos)&0xff000000)
46 #define DASM_SEC2POS(sec)	((sec)<<24)
47 #define DASM_POS2SEC(pos)	((pos)>>24)
48 #define DASM_POS2PTR(D, pos)	(D->sections[DASM_POS2SEC(pos)].rbuf + (pos))
49 
50 /* Action list type. */
51 typedef const unsigned char *dasm_ActList;
52 
53 /* Per-section structure. */
54 typedef struct dasm_Section {
55   int *rbuf;		/* Biased buffer pointer (negative section bias). */
56   int *buf;		/* True buffer pointer. */
57   size_t bsize;		/* Buffer size in bytes. */
58   int pos;		/* Biased buffer position. */
59   int epos;		/* End of biased buffer position - max single put. */
60   int ofs;		/* Byte offset into section. */
61 } dasm_Section;
62 
63 /* Core structure holding the DynASM encoding state. */
64 struct dasm_State {
65   size_t psize;			/* Allocated size of this structure. */
66   dasm_ActList actionlist;	/* Current actionlist pointer. */
67   int *lglabels;		/* Local/global chain/pos ptrs. */
68   size_t lgsize;
69   int *pclabels;		/* PC label chains/pos ptrs. */
70   size_t pcsize;
71   void **globals;		/* Array of globals (bias -10). */
72   dasm_Section *section;	/* Pointer to active section. */
73   size_t codesize;		/* Total size of all code sections. */
74   int maxsection;		/* 0 <= sectionidx < maxsection. */
75   int status;			/* Status code. */
76   dasm_Section sections[1];	/* All sections. Alloc-extended. */
77 };
78 
79 /* The size of the core structure depends on the max. number of sections. */
80 #define DASM_PSZ(ms)	(sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section))
81 
82 /* Perform potentially overflowing pointer operations in a way that avoids UB. */
83 #define DASM_PTR_SUB(p1, off) ((void *) ((uintptr_t) (p1) - sizeof(*p1) * (uintptr_t) (off)))
84 #define DASM_PTR_ADD(p1, off) ((void *) ((uintptr_t) (p1) + sizeof(*p1) * (uintptr_t) (off)))
85 
86 /* Initialize DynASM state. */
dasm_init(Dst_DECL,int maxsection)87 void dasm_init(Dst_DECL, int maxsection)
88 {
89   dasm_State *D;
90   size_t psz = 0;
91   int i;
92   Dst_REF = NULL;
93   DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection));
94   D = Dst_REF;
95   D->psize = psz;
96   D->lglabels = NULL;
97   D->lgsize = 0;
98   D->pclabels = NULL;
99   D->pcsize = 0;
100   D->globals = NULL;
101   D->maxsection = maxsection;
102   for (i = 0; i < maxsection; i++) {
103     D->sections[i].buf = NULL;  /* Need this for pass3. */
104     D->sections[i].rbuf = DASM_PTR_SUB(D->sections[i].buf, DASM_SEC2POS(i));
105     D->sections[i].bsize = 0;
106     D->sections[i].epos = 0;  /* Wrong, but is recalculated after resize. */
107   }
108 }
109 
110 /* Free DynASM state. */
dasm_free(Dst_DECL)111 void dasm_free(Dst_DECL)
112 {
113   dasm_State *D = Dst_REF;
114   int i;
115   for (i = 0; i < D->maxsection; i++)
116     if (D->sections[i].buf)
117       DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize);
118   if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize);
119   if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize);
120   DASM_M_FREE(Dst, D, D->psize);
121 }
122 
123 /* Setup global label array. Must be called before dasm_setup(). */
dasm_setupglobal(Dst_DECL,void ** gl,unsigned int maxgl)124 void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl)
125 {
126   dasm_State *D = Dst_REF;
127   D->globals = gl - 10;  /* Negative bias to compensate for locals. */
128   DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int));
129 }
130 
131 /* Grow PC label array. Can be called after dasm_setup(), too. */
dasm_growpc(Dst_DECL,unsigned int maxpc)132 void dasm_growpc(Dst_DECL, unsigned int maxpc)
133 {
134   dasm_State *D = Dst_REF;
135   size_t osz = D->pcsize;
136   DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int));
137   memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz);
138 }
139 
140 /* Setup encoder. */
dasm_setup(Dst_DECL,const void * actionlist)141 void dasm_setup(Dst_DECL, const void *actionlist)
142 {
143   dasm_State *D = Dst_REF;
144   int i;
145   D->actionlist = (dasm_ActList)actionlist;
146   D->status = DASM_S_OK;
147   D->section = &D->sections[0];
148   memset((void *)D->lglabels, 0, D->lgsize);
149   if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize);
150   for (i = 0; i < D->maxsection; i++) {
151     D->sections[i].pos = DASM_SEC2POS(i);
152     D->sections[i].ofs = 0;
153   }
154 }
155 
156 
157 #ifdef DASM_CHECKS
158 #define CK(x, st) \
159   do { if (!(x)) { \
160     D->status = DASM_S_##st|(int)(p-D->actionlist-1); return; } } while (0)
161 #define CKPL(kind, st) \
162   do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \
163     D->status=DASM_S_RANGE_##st|(int)(p-D->actionlist-1); return; } } while (0)
164 #else
165 #define CK(x, st)	((void)0)
166 #define CKPL(kind, st)	((void)0)
167 #endif
168 
169 /* Pass 1: Store actions and args, link branches/labels, estimate offsets. */
dasm_put(Dst_DECL,int start,...)170 void dasm_put(Dst_DECL, int start, ...)
171 {
172   va_list ap;
173   dasm_State *D = Dst_REF;
174   dasm_ActList p = D->actionlist + start;
175   dasm_Section *sec = D->section;
176   int pos = sec->pos, ofs = sec->ofs, mrm = -1;
177   int *b;
178 
179   if (pos >= sec->epos) {
180     DASM_M_GROW(Dst, int, sec->buf, sec->bsize,
181       sec->bsize + 2*DASM_MAXSECPOS*sizeof(int));
182     sec->rbuf = sec->buf - DASM_POS2BIAS(pos);
183     sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos);
184   }
185 
186   b = sec->rbuf;
187   b[pos++] = start;
188 
189   va_start(ap, start);
190   while (1) {
191     int action = *p++;
192     if (action < DASM_DISP) {
193       ofs++;
194     } else if (action <= DASM_REL_A) {
195       int n = va_arg(ap, int);
196       b[pos++] = n;
197       switch (action) {
198       case DASM_DISP:
199 	if (n == 0) { if (mrm < 0) mrm = p[-2]; if ((mrm&7) != 5) break; }
200       case DASM_IMM_DB: if ((((unsigned)n+128)&-256) == 0) goto ob;
201       case DASM_REL_A: /* Assumes ptrdiff_t is int. !x64 */
202       case DASM_IMM_D: ofs += 4; break;
203       case DASM_IMM_S: CK(((n+128)&-256) == 0, RANGE_I); goto ob;
204       case DASM_IMM_B: CK((n&-256) == 0, RANGE_I); ob: ofs++; break;
205       case DASM_IMM_WB: if (((n+128)&-256) == 0) goto ob;
206       case DASM_IMM_W: CK((n&-65536) == 0, RANGE_I); ofs += 2; break;
207       case DASM_SPACE: p++; ofs += n; break;
208       case DASM_SETLABEL: b[pos-2] = -0x40000000; break;  /* Neg. label ofs. */
209       case DASM_VREG: CK((n&-16) == 0 && (n != 4 || (*p>>5) != 2), RANGE_VREG);
210 	if (*p < 0x40 && p[1] == DASM_DISP) mrm = n;
211 	if (*p < 0x20 && (n&7) == 4) ofs++;
212 	switch ((*p++ >> 3) & 3) {
213 	case 3: n |= b[pos-3];
214 	case 2: n |= b[pos-2];
215 	case 1: if (n <= 7) { b[pos-1] |= 0x10; ofs--; }
216 	}
217 	continue;
218       }
219       mrm = -1;
220     } else {
221       int *pl, n;
222       switch (action) {
223       case DASM_IMM_LG64: ofs += 4;
224       case DASM_REL_LG:
225       case DASM_IMM_LG:
226 	n = *p++; pl = D->lglabels + n;
227 	/* Bkwd rel or global. */
228 	if (n <= 246) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; }
229 	pl -= 246; n = *pl;
230 	if (n < 0) n = 0;  /* Start new chain for fwd rel if label exists. */
231 	goto linkrel;
232 	  case DASM_IMM_PC64: ofs += 4;
233       case DASM_REL_PC:
234       case DASM_IMM_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC);
235       putrel:
236 	n = *pl;
237 	if (n < 0) {  /* Label exists. Get label pos and store it. */
238 	  b[pos] = -n;
239 	} else {
240       linkrel:
241 	  b[pos] = n;  /* Else link to rel chain, anchored at label. */
242 	  *pl = pos;
243 	}
244 	pos++;
245 	ofs += 4;  /* Maximum offset needed. */
246 	if (action == DASM_REL_LG || action == DASM_REL_PC)
247 	  b[pos++] = ofs;  /* Store pass1 offset estimate. */
248 	break;
249       case DASM_LABEL_LG: pl = D->lglabels + *p++; CKPL(lg, LG); goto putlabel;
250       case DASM_LABEL_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC);
251       putlabel:
252 	n = *pl;  /* n > 0: Collapse rel chain and replace with label pos. */
253 	while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; }
254 	*pl = -pos;  /* Label exists now. */
255 	b[pos++] = ofs;  /* Store pass1 offset estimate. */
256 	break;
257       case DASM_ALIGN:
258 	ofs += *p++;  /* Maximum alignment needed (arg is 2**n-1). */
259 	b[pos++] = ofs;  /* Store pass1 offset estimate. */
260 	break;
261       case DASM_EXTERN: p += 2; ofs += 4; break;
262       case DASM_ESC: p++; ofs++; break;
263       case DASM_MARK: mrm = p[-2]; break;
264       case DASM_SECTION:
265 	n = *p; CK(n < D->maxsection, RANGE_SEC); D->section = &D->sections[n];
266       case DASM_STOP: goto stop;
267       }
268     }
269   }
270 stop:
271   va_end(ap);
272   sec->pos = pos;
273   sec->ofs = ofs;
274 }
275 #undef CK
276 
277 /* Pass 2: Link sections, shrink branches/aligns, fix label offsets. */
dasm_link(Dst_DECL,size_t * szp)278 int dasm_link(Dst_DECL, size_t *szp)
279 {
280   dasm_State *D = Dst_REF;
281   int secnum;
282   int ofs = 0;
283 
284 #ifdef DASM_CHECKS
285   *szp = 0;
286   if (D->status != DASM_S_OK) return D->status;
287   {
288     int pc;
289     for (pc = 0; pc*sizeof(int) < D->pcsize; pc++)
290       if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc;
291   }
292 #endif
293 
294   { /* Handle globals not defined in this translation unit. */
295     int idx;
296     for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) {
297       int n = D->lglabels[idx];
298       /* Undefined label: Collapse rel chain and replace with marker (< 0). */
299       while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; }
300     }
301   }
302 
303   /* Combine all code sections. No support for data sections (yet). */
304   for (secnum = 0; secnum < D->maxsection; secnum++) {
305     dasm_Section *sec = D->sections + secnum;
306     int *b = sec->rbuf;
307     int pos = DASM_SEC2POS(secnum);
308     int lastpos = sec->pos;
309 
310     while (pos != lastpos) {
311       dasm_ActList p = D->actionlist + b[pos++];
312       while (1) {
313 	int op, action = *p++;
314 	switch (action) {
315 	case DASM_REL_LG: p++; op = p[-3]; goto rel_pc;
316 	case DASM_REL_PC: op = p[-2]; rel_pc: {
317 	  int shrink = op == 0xe9 ? 3 : ((op&0xf0) == 0x80 ? 4 : 0);
318 	  if (shrink) {  /* Shrinkable branch opcode? */
319 	    int lofs, lpos = b[pos];
320 	    if (lpos < 0) goto noshrink;  /* Ext global? */
321 	    lofs = *DASM_POS2PTR(D, lpos);
322 	    if (lpos > pos) {  /* Fwd label: add cumulative section offsets. */
323 	      int i;
324 	      for (i = secnum; i < DASM_POS2SEC(lpos); i++)
325 		lofs += D->sections[i].ofs;
326 	    } else {
327 	      lofs -= ofs;  /* Bkwd label: unfix offset. */
328 	    }
329 	    lofs -= b[pos+1];  /* Short branch ok? */
330 	    if (lofs >= -128-shrink && lofs <= 127) ofs -= shrink;  /* Yes. */
331 	    else { noshrink: shrink = 0; }  /* No, cannot shrink op. */
332 	  }
333 	  b[pos+1] = shrink;
334 	  pos += 2;
335 	  break;
336 	}
337 	case DASM_SPACE: case DASM_IMM_LG: case DASM_IMM_LG64: case DASM_VREG: p++;
338 	case DASM_DISP: case DASM_IMM_S: case DASM_IMM_B: case DASM_IMM_W:
339 	case DASM_IMM_D: case DASM_IMM_WB: case DASM_IMM_DB:
340 	case DASM_SETLABEL: case DASM_REL_A: case DASM_IMM_PC: case DASM_IMM_PC64:
341 	  pos++; break;
342 	case DASM_LABEL_LG: p++;
343 	case DASM_LABEL_PC: b[pos++] += ofs; break; /* Fix label offset. */
344 	case DASM_ALIGN: ofs -= (b[pos++]+ofs)&*p++; break; /* Adjust ofs. */
345 	case DASM_EXTERN: p += 2; break;
346 	case DASM_ESC: p++; break;
347 	case DASM_MARK: break;
348 	case DASM_SECTION: case DASM_STOP: goto stop;
349 	}
350       }
351       stop: (void)0;
352     }
353     ofs += sec->ofs;  /* Next section starts right after current section. */
354   }
355 
356   D->codesize = ofs;  /* Total size of all code sections */
357   *szp = ofs;
358   return DASM_S_OK;
359 }
360 
361 #define dasmb(x)	*cp++ = (unsigned char)(x)
362 #ifndef DASM_ALIGNED_WRITES
363 typedef ZEND_SET_ALIGNED(1, unsigned short unaligned_short);
364 typedef ZEND_SET_ALIGNED(1, unsigned int unaligned_int);
365 typedef ZEND_SET_ALIGNED(1, uint64_t unaligned_uint64_t);
366 #define dasmw(x) \
367   do { *((unaligned_short *)cp) = (unsigned short)(x); cp+=2; } while (0)
368 #define dasmd(x) \
369   do { *((unaligned_int *)cp) = (unsigned int)(x); cp+=4; } while (0)
370 #define dasmq(x) \
371   do { *((unaligned_uint64_t *)cp) = (uint64_t)(x); cp+=8; } while (0)
372 #else
373 #define dasmw(x)	do { dasmb(x); dasmb((x)>>8); } while (0)
374 #define dasmd(x)	do { dasmw(x); dasmw((x)>>16); } while (0)
375 #endif
376 
377 /* Pass 3: Encode sections. */
dasm_encode(Dst_DECL,void * buffer)378 int dasm_encode(Dst_DECL, void *buffer)
379 {
380   dasm_State *D = Dst_REF;
381   unsigned char *base = (unsigned char *)buffer;
382   unsigned char *cp = base;
383   int secnum;
384 
385   /* Encode all code sections. No support for data sections (yet). */
386   for (secnum = 0; secnum < D->maxsection; secnum++) {
387     dasm_Section *sec = D->sections + secnum;
388     int *b = sec->buf;
389     int *endb = DASM_PTR_ADD(sec->rbuf, sec->pos);
390 
391     while (b != endb) {
392       dasm_ActList p = D->actionlist + *b++;
393       unsigned char *mark = NULL;
394       while (1) {
395 	int action = *p++;
396 	int n = (action >= DASM_DISP && action <= DASM_ALIGN) ? *b++ : 0;
397 	switch (action) {
398 	case DASM_DISP: if (!mark) mark = cp; {
399 	  unsigned char *mm = mark;
400 	  if (*p != DASM_IMM_DB && *p != DASM_IMM_WB) mark = NULL;
401 	  if (n == 0) { int mrm = mm[-1]&7; if (mrm == 4) mrm = mm[0]&7;
402 	    if (mrm != 5) { mm[-1] -= 0x80; break; } }
403 	  if ((((unsigned)n+128) & -256) != 0) goto wd; else mm[-1] -= 0x40;
404 	}
405 	case DASM_IMM_S: case DASM_IMM_B: wb: dasmb(n); break;
406 	case DASM_IMM_DB: if ((((unsigned)n+128)&-256) == 0) {
407 	    db: if (!mark) mark = cp; mark[-2] += 2; mark = NULL; goto wb;
408 	  } else mark = NULL;
409 	case DASM_IMM_D: wd: dasmd(n); break;
410 	case DASM_IMM_WB: if ((((unsigned)n+128)&-256) == 0) goto db; else mark = NULL;
411 	case DASM_IMM_W: dasmw(n); break;
412 	case DASM_VREG: {
413 	  int t = *p++;
414 	  unsigned char *ex = cp - (t&7);
415 	  if ((n & 8) && t < 0xa0) {
416 	    if (*ex & 0x80) ex[1] ^= 0x20 << (t>>6); else *ex ^= 1 << (t>>6);
417 	    n &= 7;
418 	  } else if (n & 0x10) {
419 	    if (*ex & 0x80) {
420 	      *ex = 0xc5; ex[1] = (ex[1] & 0x80) | ex[2]; ex += 2;
421 	    }
422 	    while (++ex < cp) ex[-1] = *ex;
423 	    if (mark) mark--;
424 	    cp--;
425 	    n &= 7;
426 	  }
427 	  if (t >= 0xc0) n <<= 4;
428 	  else if (t >= 0x40) n <<= 3;
429 	  else if (n == 4 && t < 0x20) { cp[-1] ^= n; *cp++ = 0x20; }
430 	  cp[-1] ^= n;
431 	  break;
432 	}
433 	case DASM_REL_LG: p++; if (n >= 0) goto rel_pc;
434 	  b++; n = (int)(ptrdiff_t)D->globals[-n];
435 	case DASM_REL_A: rel_a: n -= (int)(ptrdiff_t)(cp+4); goto wd; /* !x64 */
436 	case DASM_REL_PC: rel_pc: {
437 	  int shrink = *b++;
438 	  int *pb = DASM_POS2PTR(D, n); if (*pb < 0) { n = pb[1]; goto rel_a; }
439 	  n = *pb - ((int)(cp-base) + 4-shrink);
440 	  if (shrink == 0) goto wd;
441 	  if (shrink == 4) { cp--; cp[-1] = *cp-0x10; } else cp[-1] = 0xeb;
442 	  goto wb;
443 	}
444 	case DASM_IMM_LG:
445 	  p++; if (n < 0) { n = (int)(ptrdiff_t)D->globals[-n]; goto wd; }
446 	case DASM_IMM_PC: {
447 	  int *pb = DASM_POS2PTR(D, n);
448 	  n = *pb < 0 ? pb[1] : (*pb + (int)(ptrdiff_t)base);
449 	  goto wd;
450 	}
451 	case DASM_IMM_LG64: {
452 	  p++;
453 	  if (n < 0)
454 	    dasmq((ptrdiff_t)D->globals[-n]);
455 	}
456 	case DASM_IMM_PC64: {
457 	  int *pb = DASM_POS2PTR(D, n);
458 	  dasmq(*pb < 0 ? pb[1] : (*pb + (ptrdiff_t)base));
459 	  break;
460 	}
461 	case DASM_LABEL_LG: {
462 	  int idx = *p++;
463 	  if (idx >= 10)
464 	    D->globals[idx] = (void *)(base + (*p == DASM_SETLABEL ? *b : n));
465 	  break;
466 	}
467 	case DASM_LABEL_PC: case DASM_SETLABEL: break;
468 	case DASM_SPACE: { int fill = *p++; while (n--) *cp++ = fill; break; }
469 	case DASM_ALIGN:
470 	  n = *p++;
471 	  while (((cp-base) & n)) *cp++ = 0x90; /* nop */
472 	  break;
473 	case DASM_EXTERN: n = DASM_EXTERN(Dst, cp, p[1], *p); p += 2; goto wd;
474 	case DASM_MARK: mark = cp; break;
475 	case DASM_ESC: action = *p++;
476 	default: *cp++ = action; break;
477 	case DASM_SECTION: case DASM_STOP: goto stop;
478 	}
479       }
480       stop: (void)0;
481     }
482   }
483 
484   if (base + D->codesize != cp)  /* Check for phase errors. */
485     return DASM_S_PHASE;
486   return DASM_S_OK;
487 }
488 
489 /* Get PC label offset. */
dasm_getpclabel(Dst_DECL,unsigned int pc)490 int dasm_getpclabel(Dst_DECL, unsigned int pc)
491 {
492   dasm_State *D = Dst_REF;
493   if (pc*sizeof(int) < D->pcsize) {
494     int pos = D->pclabels[pc];
495     if (pos < 0) return *DASM_POS2PTR(D, -pos);
496     if (pos > 0) return -1;  /* Undefined. */
497   }
498   return -2;  /* Unused or out of range. */
499 }
500 
501 #ifdef DASM_CHECKS
502 /* Optional sanity checker to call between isolated encoding steps. */
dasm_checkstep(Dst_DECL,int secmatch)503 int dasm_checkstep(Dst_DECL, int secmatch)
504 {
505   dasm_State *D = Dst_REF;
506   if (D->status == DASM_S_OK) {
507     int i;
508     for (i = 1; i <= 9; i++) {
509       if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_L|i; break; }
510       D->lglabels[i] = 0;
511     }
512   }
513   if (D->status == DASM_S_OK && secmatch >= 0 &&
514       D->section != &D->sections[secmatch])
515     D->status = DASM_S_MATCH_SEC|(int)(D->section-D->sections);
516   return D->status;
517 }
518 #endif
519 
520