dasm_x86.h 15 KB

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