bc-data.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. /*
  2. * Data (and LEB128) bytecode
  3. *
  4. * Copyright (C) 2001-2007 Peter Johnson
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "util.h"
  28. #include "libyasm-stdint.h"
  29. #include "coretype.h"
  30. #include "errwarn.h"
  31. #include "intnum.h"
  32. #include "expr.h"
  33. #include "value.h"
  34. #include "bytecode.h"
  35. #include "arch.h"
  36. struct yasm_dataval {
  37. /*@reldef@*/ STAILQ_ENTRY(yasm_dataval) link;
  38. enum { DV_EMPTY, DV_VALUE, DV_RAW, DV_ULEB128, DV_SLEB128, DV_RESERVE }
  39. type;
  40. union {
  41. yasm_value val;
  42. struct {
  43. /*@only@*/ unsigned char *contents;
  44. unsigned long len;
  45. } raw;
  46. } data;
  47. /* number of times data is repeated, NULL=1. */
  48. /*@only@*/ /*@null@*/ yasm_expr *multiple;
  49. };
  50. typedef struct bytecode_data {
  51. /* converted data (linked list) */
  52. yasm_datavalhead datahead;
  53. int item_size;
  54. } bytecode_data;
  55. static void bc_data_destroy(void *contents);
  56. static void bc_data_print(const void *contents, FILE *f, int indent_level);
  57. static void bc_data_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc);
  58. static int bc_data_item_size(yasm_bytecode *bc);
  59. static int bc_data_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span,
  60. void *add_span_data);
  61. static int bc_data_tobytes(yasm_bytecode *bc, unsigned char **bufp,
  62. unsigned char *bufstart, void *d,
  63. yasm_output_value_func output_value,
  64. /*@null@*/ yasm_output_reloc_func output_reloc);
  65. static const yasm_bytecode_callback bc_data_callback = {
  66. bc_data_destroy,
  67. bc_data_print,
  68. bc_data_finalize,
  69. bc_data_item_size,
  70. bc_data_calc_len,
  71. yasm_bc_expand_common,
  72. bc_data_tobytes,
  73. 0
  74. };
  75. static void
  76. bc_data_destroy(void *contents)
  77. {
  78. bytecode_data *bc_data = (bytecode_data *)contents;
  79. yasm_dvs_delete(&bc_data->datahead);
  80. yasm_xfree(contents);
  81. }
  82. static void
  83. bc_data_print(const void *contents, FILE *f, int indent_level)
  84. {
  85. const bytecode_data *bc_data = (const bytecode_data *)contents;
  86. fprintf(f, "%*s_Data_\n", indent_level, "");
  87. fprintf(f, "%*sElements:\n", indent_level+1, "");
  88. yasm_dvs_print(&bc_data->datahead, f, indent_level+2);
  89. }
  90. static void
  91. bc_data_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc)
  92. {
  93. bytecode_data *bc_data = (bytecode_data *)bc->contents;
  94. yasm_dataval *dv;
  95. yasm_intnum *intn;
  96. /* Convert values from simple expr to value. */
  97. STAILQ_FOREACH(dv, &bc_data->datahead, link) {
  98. switch (dv->type) {
  99. case DV_VALUE:
  100. if (yasm_value_finalize(&dv->data.val, prev_bc)) {
  101. yasm_error_set(YASM_ERROR_TOO_COMPLEX,
  102. N_("data expression too complex"));
  103. return;
  104. }
  105. break;
  106. case DV_ULEB128:
  107. case DV_SLEB128:
  108. intn = yasm_expr_get_intnum(&dv->data.val.abs, 0);
  109. if (!intn) {
  110. yasm_error_set(YASM_ERROR_NOT_CONSTANT,
  111. N_("LEB128 requires constant values"));
  112. return;
  113. }
  114. /* Warn for negative values in unsigned environment.
  115. * This could be an error instead: the likelihood this is
  116. * desired is very low!
  117. */
  118. if (yasm_intnum_sign(intn) == -1 && dv->type == DV_ULEB128)
  119. yasm_warn_set(YASM_WARN_GENERAL,
  120. N_("negative value in unsigned LEB128"));
  121. break;
  122. default:
  123. break;
  124. }
  125. if (dv->multiple) {
  126. yasm_value val;
  127. if (yasm_value_finalize_expr(&val, dv->multiple, prev_bc, 0))
  128. yasm_error_set(YASM_ERROR_TOO_COMPLEX,
  129. N_("multiple expression too complex"));
  130. else if (val.rel)
  131. yasm_error_set(YASM_ERROR_NOT_ABSOLUTE,
  132. N_("multiple expression not absolute"));
  133. dv->multiple = val.abs;
  134. }
  135. }
  136. }
  137. static int
  138. bc_data_item_size(yasm_bytecode *bc)
  139. {
  140. bytecode_data *bc_data = (bytecode_data *)bc->contents;
  141. return bc_data->item_size;
  142. }
  143. static int
  144. bc_data_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span,
  145. void *add_span_data)
  146. {
  147. bytecode_data *bc_data = (bytecode_data *)bc->contents;
  148. yasm_dataval *dv;
  149. yasm_intnum *intn;
  150. unsigned long len = 0;
  151. unsigned long multiple;
  152. /* Count up element sizes, rounding up string length. */
  153. STAILQ_FOREACH(dv, &bc_data->datahead, link) {
  154. switch (dv->type) {
  155. case DV_EMPTY:
  156. len = 0;
  157. break;
  158. case DV_VALUE:
  159. len = dv->data.val.size/8;
  160. break;
  161. case DV_RAW:
  162. len = dv->data.raw.len;
  163. break;
  164. case DV_ULEB128:
  165. case DV_SLEB128:
  166. intn = yasm_expr_get_intnum(&dv->data.val.abs, 0);
  167. if (!intn)
  168. yasm_internal_error(N_("non-constant in data_tobytes"));
  169. len = yasm_intnum_size_leb128(intn, dv->type == DV_SLEB128);
  170. break;
  171. case DV_RESERVE:
  172. len = dv->data.val.size/8;
  173. break;
  174. }
  175. if (!yasm_dv_get_multiple(dv, &multiple))
  176. len *= multiple;
  177. bc->len += len;
  178. }
  179. return 0;
  180. }
  181. static int
  182. bc_data_tobytes(yasm_bytecode *bc, unsigned char **bufp,
  183. unsigned char *bufstart, void *d,
  184. yasm_output_value_func output_value,
  185. /*@unused@*/ yasm_output_reloc_func output_reloc)
  186. {
  187. bytecode_data *bc_data = (bytecode_data *)bc->contents;
  188. yasm_dataval *dv;
  189. yasm_intnum *intn;
  190. unsigned int val_len;
  191. unsigned long multiple, i;
  192. STAILQ_FOREACH(dv, &bc_data->datahead, link) {
  193. if (yasm_dv_get_multiple(dv, &multiple) || multiple == 0)
  194. continue;
  195. switch (dv->type) {
  196. case DV_EMPTY:
  197. break;
  198. case DV_VALUE:
  199. val_len = dv->data.val.size/8;
  200. for (i=0; i<multiple; i++) {
  201. if (output_value(&dv->data.val, *bufp, val_len,
  202. (unsigned long)(*bufp-bufstart), bc, 1,
  203. d))
  204. return 1;
  205. *bufp += val_len;
  206. }
  207. break;
  208. case DV_RAW:
  209. for (i=0; i<multiple; i++) {
  210. memcpy(*bufp, dv->data.raw.contents, dv->data.raw.len);
  211. *bufp += dv->data.raw.len;
  212. }
  213. break;
  214. case DV_ULEB128:
  215. case DV_SLEB128:
  216. intn = yasm_expr_get_intnum(&dv->data.val.abs, 234);
  217. if (!intn)
  218. yasm_internal_error(N_("non-constant in data_tobytes"));
  219. for (i=0; i<multiple; i++) {
  220. *bufp +=
  221. yasm_intnum_get_leb128(intn, *bufp,
  222. dv->type == DV_SLEB128);
  223. }
  224. case DV_RESERVE:
  225. val_len = dv->data.val.size/8;
  226. for (i=0; i<multiple; i++) {
  227. memset(*bufp, 0, val_len);
  228. *bufp += val_len;
  229. }
  230. break;
  231. }
  232. }
  233. return 0;
  234. }
  235. yasm_bytecode *
  236. yasm_bc_create_data(yasm_datavalhead *datahead, unsigned int size,
  237. int append_zero, yasm_arch *arch, unsigned long line)
  238. {
  239. bytecode_data *data = yasm_xmalloc(sizeof(bytecode_data));
  240. yasm_bytecode *bc = yasm_bc_create_common(&bc_data_callback, data, line);
  241. yasm_dataval *dv, *dv2, *dvo;
  242. yasm_intnum *intn;
  243. unsigned long len = 0, rlen, i;
  244. yasm_dvs_initialize(&data->datahead);
  245. data->item_size = size;
  246. /* Prescan input data for length, etc. Careful: this needs to be
  247. * precisely paired with the second loop.
  248. */
  249. STAILQ_FOREACH(dv, datahead, link) {
  250. if (dv->multiple && dv->type != DV_EMPTY && len > 0) {
  251. /* Flush previous data */
  252. dvo = yasm_dv_create_raw(yasm_xmalloc(len), len);
  253. STAILQ_INSERT_TAIL(&data->datahead, dvo, link);
  254. len = 0;
  255. }
  256. switch (dv->type) {
  257. case DV_EMPTY:
  258. break;
  259. case DV_VALUE:
  260. case DV_ULEB128:
  261. case DV_SLEB128:
  262. intn = yasm_expr_get_intnum(&dv->data.val.abs, 0);
  263. if (intn && dv->type == DV_VALUE && (arch || size == 1))
  264. len += size;
  265. else if (intn && dv->type == DV_ULEB128)
  266. len += yasm_intnum_size_leb128(intn, 0);
  267. else if (intn && dv->type == DV_SLEB128)
  268. len += yasm_intnum_size_leb128(intn, 1);
  269. else {
  270. if (len > 0) {
  271. /* Create bytecode for all previous len */
  272. dvo = yasm_dv_create_raw(yasm_xmalloc(len), len);
  273. STAILQ_INSERT_TAIL(&data->datahead, dvo, link);
  274. len = 0;
  275. }
  276. /* Create bytecode for this value */
  277. dvo = yasm_xmalloc(sizeof(yasm_dataval));
  278. STAILQ_INSERT_TAIL(&data->datahead, dvo, link);
  279. dvo->multiple = dv->multiple;
  280. }
  281. break;
  282. case DV_RAW:
  283. rlen = dv->data.raw.len;
  284. /* find count, rounding up to nearest multiple of size */
  285. rlen = (rlen + size - 1) / size;
  286. len += rlen*size;
  287. break;
  288. case DV_RESERVE:
  289. len += size;
  290. break;
  291. }
  292. if (dv->multiple && dv->type != DV_EMPTY && len > 0) {
  293. /* Flush this data */
  294. dvo = yasm_dv_create_raw(yasm_xmalloc(len), len);
  295. STAILQ_INSERT_TAIL(&data->datahead, dvo, link);
  296. dvo->multiple = dv->multiple;
  297. len = 0;
  298. }
  299. if (append_zero)
  300. len++;
  301. }
  302. /* Create final dataval for any trailing length */
  303. if (len > 0) {
  304. dvo = yasm_dv_create_raw(yasm_xmalloc(len), len);
  305. STAILQ_INSERT_TAIL(&data->datahead, dvo, link);
  306. }
  307. /* Second iteration: copy data and delete input datavals. */
  308. dv = STAILQ_FIRST(datahead);
  309. dvo = STAILQ_FIRST(&data->datahead);
  310. len = 0;
  311. while (dv && dvo) {
  312. if (dv->multiple && dv->type != DV_EMPTY && len > 0) {
  313. dvo = STAILQ_NEXT(dvo, link);
  314. len = 0;
  315. }
  316. switch (dv->type) {
  317. case DV_EMPTY:
  318. break;
  319. case DV_VALUE:
  320. case DV_ULEB128:
  321. case DV_SLEB128:
  322. intn = yasm_expr_get_intnum(&dv->data.val.abs, 0);
  323. if (intn && dv->type == DV_VALUE && (arch || size == 1)) {
  324. if (size == 1)
  325. yasm_intnum_get_sized(intn,
  326. &dvo->data.raw.contents[len],
  327. 1, 8, 0, 0, 1);
  328. else
  329. yasm_arch_intnum_tobytes(arch, intn,
  330. &dvo->data.raw.contents[len],
  331. size, size*8, 0, bc, 1);
  332. yasm_value_delete(&dv->data.val);
  333. len += size;
  334. } else if (intn && dv->type == DV_ULEB128) {
  335. len += yasm_intnum_get_leb128(intn,
  336. &dvo->data.raw.contents[len],
  337. 0);
  338. yasm_value_delete(&dv->data.val);
  339. } else if (intn && dv->type == DV_SLEB128) {
  340. len += yasm_intnum_get_leb128(intn,
  341. &dvo->data.raw.contents[len],
  342. 1);
  343. yasm_value_delete(&dv->data.val);
  344. } else {
  345. if (len > 0)
  346. dvo = STAILQ_NEXT(dvo, link);
  347. dvo->type = dv->type;
  348. dvo->data.val = dv->data.val; /* structure copy */
  349. dvo->data.val.size = size*8; /* remember size */
  350. dvo = STAILQ_NEXT(dvo, link);
  351. len = 0;
  352. }
  353. break;
  354. case DV_RAW:
  355. rlen = dv->data.raw.len;
  356. memcpy(&dvo->data.raw.contents[len], dv->data.raw.contents,
  357. rlen);
  358. yasm_xfree(dv->data.raw.contents);
  359. len += rlen;
  360. /* pad with 0's to nearest multiple of size */
  361. rlen %= size;
  362. if (rlen > 0) {
  363. rlen = size-rlen;
  364. for (i=0; i<rlen; i++)
  365. dvo->data.raw.contents[len++] = 0;
  366. }
  367. break;
  368. case DV_RESERVE:
  369. memset(&dvo->data.raw.contents[len], 0, size);
  370. len += size;
  371. break;
  372. }
  373. if (dv->multiple && dv->type != DV_EMPTY && len > 0) {
  374. dvo = STAILQ_NEXT(dvo, link);
  375. len = 0;
  376. }
  377. if (append_zero)
  378. dvo->data.raw.contents[len++] = 0;
  379. dv2 = STAILQ_NEXT(dv, link);
  380. yasm_xfree(dv);
  381. dv = dv2;
  382. }
  383. return bc;
  384. }
  385. yasm_bytecode *
  386. yasm_bc_create_leb128(yasm_datavalhead *datahead, int sign, unsigned long line)
  387. {
  388. yasm_dataval *dv;
  389. /* Convert all values into LEB type, error on strings/raws */
  390. STAILQ_FOREACH(dv, datahead, link) {
  391. switch (dv->type) {
  392. case DV_VALUE:
  393. dv->type = sign ? DV_SLEB128 : DV_ULEB128;
  394. break;
  395. case DV_RAW:
  396. yasm_error_set(YASM_ERROR_VALUE,
  397. N_("LEB128 does not allow string constants"));
  398. break;
  399. default:
  400. break;
  401. }
  402. }
  403. return yasm_bc_create_data(datahead, 0, 0, 0, line);
  404. }
  405. yasm_dataval *
  406. yasm_dv_create_expr(yasm_expr *e)
  407. {
  408. yasm_dataval *retval = yasm_xmalloc(sizeof(yasm_dataval));
  409. retval->type = DV_VALUE;
  410. yasm_value_initialize(&retval->data.val, e, 0);
  411. retval->multiple = NULL;
  412. return retval;
  413. }
  414. yasm_dataval *
  415. yasm_dv_create_raw(unsigned char *contents, unsigned long len)
  416. {
  417. yasm_dataval *retval = yasm_xmalloc(sizeof(yasm_dataval));
  418. retval->type = DV_RAW;
  419. retval->data.raw.contents = contents;
  420. retval->data.raw.len = len;
  421. retval->multiple = NULL;
  422. return retval;
  423. }
  424. yasm_dataval *
  425. yasm_dv_create_reserve(void)
  426. {
  427. yasm_dataval *retval = yasm_xmalloc(sizeof(yasm_dataval));
  428. retval->type = DV_RESERVE;
  429. retval->multiple = NULL;
  430. return retval;
  431. }
  432. yasm_value *
  433. yasm_dv_get_value(yasm_dataval *dv)
  434. {
  435. if (dv->type != DV_VALUE)
  436. return NULL;
  437. return &dv->data.val;
  438. }
  439. void
  440. yasm_dv_set_multiple(yasm_dataval *dv, yasm_expr *e)
  441. {
  442. if (dv->multiple)
  443. dv->multiple = yasm_expr_create_tree( dv->multiple, YASM_EXPR_MUL, e,
  444. e->line);
  445. else
  446. dv->multiple = e;
  447. }
  448. int
  449. yasm_dv_get_multiple(yasm_dataval *dv, unsigned long *multiple)
  450. {
  451. /*@dependent@*/ /*@null@*/ const yasm_intnum *num;
  452. *multiple = 1;
  453. if (dv->multiple) {
  454. num = yasm_expr_get_intnum(&dv->multiple, 0);
  455. if (!num) {
  456. yasm_error_set(YASM_ERROR_VALUE,
  457. N_("could not determine multiple"));
  458. return 1;
  459. }
  460. if (yasm_intnum_sign(num) < 0) {
  461. yasm_error_set(YASM_ERROR_VALUE, N_("multiple is negative"));
  462. return 1;
  463. }
  464. *multiple = yasm_intnum_get_uint(num);
  465. }
  466. return 0;
  467. }
  468. void
  469. yasm_dvs_delete(yasm_datavalhead *headp)
  470. {
  471. yasm_dataval *cur, *next;
  472. cur = STAILQ_FIRST(headp);
  473. while (cur) {
  474. next = STAILQ_NEXT(cur, link);
  475. switch (cur->type) {
  476. case DV_VALUE:
  477. yasm_value_delete(&cur->data.val);
  478. break;
  479. case DV_RAW:
  480. yasm_xfree(cur->data.raw.contents);
  481. break;
  482. default:
  483. break;
  484. }
  485. if (cur->multiple)
  486. yasm_expr_destroy(cur->multiple);
  487. yasm_xfree(cur);
  488. cur = next;
  489. }
  490. STAILQ_INIT(headp);
  491. }
  492. yasm_dataval *
  493. yasm_dvs_append(yasm_datavalhead *headp, yasm_dataval *dv)
  494. {
  495. if (dv) {
  496. STAILQ_INSERT_TAIL(headp, dv, link);
  497. return dv;
  498. }
  499. return (yasm_dataval *)NULL;
  500. }
  501. void
  502. yasm_dvs_print(const yasm_datavalhead *head, FILE *f, int indent_level)
  503. {
  504. yasm_dataval *cur;
  505. unsigned long i;
  506. STAILQ_FOREACH(cur, head, link) {
  507. fprintf(f, "%*sMultiple=", indent_level, "");
  508. if (!cur->multiple)
  509. fprintf(f, "nil (1)");
  510. else
  511. yasm_expr_print(cur->multiple, f);
  512. switch (cur->type) {
  513. case DV_EMPTY:
  514. fprintf(f, "%*sEmpty\n", indent_level, "");
  515. break;
  516. case DV_VALUE:
  517. fprintf(f, "%*sValue:\n", indent_level, "");
  518. yasm_value_print(&cur->data.val, f, indent_level+1);
  519. break;
  520. case DV_RAW:
  521. fprintf(f, "%*sLength=%lu\n", indent_level, "",
  522. cur->data.raw.len);
  523. fprintf(f, "%*sBytes=[", indent_level, "");
  524. for (i=0; i<cur->data.raw.len; i++)
  525. fprintf(f, "0x%02x, ", cur->data.raw.contents[i]);
  526. fprintf(f, "]\n");
  527. break;
  528. case DV_ULEB128:
  529. fprintf(f, "%*sULEB128 value:\n", indent_level, "");
  530. yasm_value_print(&cur->data.val, f, indent_level+1);
  531. break;
  532. case DV_SLEB128:
  533. fprintf(f, "%*sSLEB128 value:\n", indent_level, "");
  534. yasm_value_print(&cur->data.val, f, indent_level+1);
  535. break;
  536. case DV_RESERVE:
  537. fprintf(f, "%*sReserved\n", indent_level, "");
  538. break;
  539. }
  540. }
  541. }