bc-align.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Align bytecode
  3. *
  4. * Copyright (C) 2005-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 "bytecode.h"
  34. typedef struct bytecode_align {
  35. /*@only@*/ yasm_expr *boundary; /* alignment boundary */
  36. /* What to fill intervening locations with, NULL if using code_fill */
  37. /*@only@*/ /*@null@*/ yasm_expr *fill;
  38. /* Maximum number of bytes to skip, NULL if no maximum. */
  39. /*@only@*/ /*@null@*/ yasm_expr *maxskip;
  40. /* Code fill, NULL if using 0 fill */
  41. /*@null@*/ const unsigned char **code_fill;
  42. } bytecode_align;
  43. static void bc_align_destroy(void *contents);
  44. static void bc_align_print(const void *contents, FILE *f, int indent_level);
  45. static void bc_align_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc);
  46. static int bc_align_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span,
  47. void *add_span_data);
  48. static int bc_align_expand(yasm_bytecode *bc, int span, long old_val,
  49. long new_val, /*@out@*/ long *neg_thres,
  50. /*@out@*/ long *pos_thres);
  51. static int bc_align_tobytes(yasm_bytecode *bc, unsigned char **bufp,
  52. unsigned char *bufstart, void *d,
  53. yasm_output_value_func output_value,
  54. /*@null@*/ yasm_output_reloc_func output_reloc);
  55. static const yasm_bytecode_callback bc_align_callback = {
  56. bc_align_destroy,
  57. bc_align_print,
  58. bc_align_finalize,
  59. NULL,
  60. bc_align_calc_len,
  61. bc_align_expand,
  62. bc_align_tobytes,
  63. YASM_BC_SPECIAL_OFFSET
  64. };
  65. static void
  66. bc_align_destroy(void *contents)
  67. {
  68. bytecode_align *align = (bytecode_align *)contents;
  69. if (align->boundary)
  70. yasm_expr_destroy(align->boundary);
  71. if (align->fill)
  72. yasm_expr_destroy(align->fill);
  73. if (align->maxskip)
  74. yasm_expr_destroy(align->maxskip);
  75. yasm_xfree(contents);
  76. }
  77. static void
  78. bc_align_print(const void *contents, FILE *f, int indent_level)
  79. {
  80. const bytecode_align *align = (const bytecode_align *)contents;
  81. fprintf(f, "%*s_Align_\n", indent_level, "");
  82. fprintf(f, "%*sBoundary=", indent_level, "");
  83. yasm_expr_print(align->boundary, f);
  84. fprintf(f, "\n%*sFill=", indent_level, "");
  85. yasm_expr_print(align->fill, f);
  86. fprintf(f, "\n%*sMax Skip=", indent_level, "");
  87. yasm_expr_print(align->maxskip, f);
  88. fprintf(f, "\n");
  89. }
  90. static void
  91. bc_align_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc)
  92. {
  93. bytecode_align *align = (bytecode_align *)bc->contents;
  94. if (!yasm_expr_get_intnum(&align->boundary, 0))
  95. yasm_error_set(YASM_ERROR_NOT_CONSTANT,
  96. N_("align boundary must be a constant"));
  97. if (align->fill && !yasm_expr_get_intnum(&align->fill, 0))
  98. yasm_error_set(YASM_ERROR_NOT_CONSTANT,
  99. N_("align fill must be a constant"));
  100. if (align->maxskip && !yasm_expr_get_intnum(&align->maxskip, 0))
  101. yasm_error_set(YASM_ERROR_NOT_CONSTANT,
  102. N_("align maximum skip must be a constant"));
  103. }
  104. static int
  105. bc_align_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span,
  106. void *add_span_data)
  107. {
  108. long neg_thres = 0;
  109. long pos_thres = 0;
  110. if (bc_align_expand(bc, 0, 0, (long)bc->offset, &neg_thres,
  111. &pos_thres) < 0)
  112. return -1;
  113. return 0;
  114. }
  115. static int
  116. bc_align_expand(yasm_bytecode *bc, int span, long old_val, long new_val,
  117. /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres)
  118. {
  119. bytecode_align *align = (bytecode_align *)bc->contents;
  120. unsigned long end;
  121. unsigned long boundary =
  122. yasm_intnum_get_uint(yasm_expr_get_intnum(&align->boundary, 0));
  123. if (boundary == 0) {
  124. bc->len = 0;
  125. *pos_thres = new_val;
  126. return 0;
  127. }
  128. end = (unsigned long)new_val;
  129. if ((unsigned long)new_val & (boundary-1))
  130. end = ((unsigned long)new_val & ~(boundary-1)) + boundary;
  131. *pos_thres = (long)end;
  132. bc->len = end - (unsigned long)new_val;
  133. if (align->maxskip) {
  134. unsigned long maxskip =
  135. yasm_intnum_get_uint(yasm_expr_get_intnum(&align->maxskip, 0));
  136. if (bc->len > maxskip) {
  137. *pos_thres = (long)end-maxskip-1;
  138. bc->len = 0;
  139. }
  140. }
  141. return 1;
  142. }
  143. static int
  144. bc_align_tobytes(yasm_bytecode *bc, unsigned char **bufp,
  145. unsigned char *bufstart, void *d,
  146. yasm_output_value_func output_value,
  147. /*@unused@*/ yasm_output_reloc_func output_reloc)
  148. {
  149. bytecode_align *align = (bytecode_align *)bc->contents;
  150. unsigned long len;
  151. unsigned long boundary =
  152. yasm_intnum_get_uint(yasm_expr_get_intnum(&align->boundary, 0));
  153. if (boundary == 0)
  154. return 0;
  155. else {
  156. unsigned long end = bc->offset;
  157. if (bc->offset & (boundary-1))
  158. end = (bc->offset & ~(boundary-1)) + boundary;
  159. len = end - bc->offset;
  160. if (len == 0)
  161. return 0;
  162. if (align->maxskip) {
  163. unsigned long maxskip =
  164. yasm_intnum_get_uint(yasm_expr_get_intnum(&align->maxskip, 0));
  165. if (len > maxskip)
  166. return 0;
  167. }
  168. }
  169. if (align->fill) {
  170. unsigned long v;
  171. v = yasm_intnum_get_uint(yasm_expr_get_intnum(&align->fill, 0));
  172. memset(*bufp, (int)v, len);
  173. *bufp += len;
  174. } else if (align->code_fill) {
  175. unsigned long maxlen = 15;
  176. while (!align->code_fill[maxlen] && maxlen>0)
  177. maxlen--;
  178. if (maxlen == 0) {
  179. yasm_error_set(YASM_ERROR_GENERAL,
  180. N_("could not find any code alignment size"));
  181. return 1;
  182. }
  183. /* Fill with maximum code fill as much as possible */
  184. while (len > maxlen) {
  185. memcpy(*bufp, align->code_fill[maxlen], maxlen);
  186. *bufp += maxlen;
  187. len -= maxlen;
  188. }
  189. if (!align->code_fill[len]) {
  190. yasm_error_set(YASM_ERROR_VALUE,
  191. N_("invalid alignment size %d"), len);
  192. return 1;
  193. }
  194. /* Handle rest of code fill */
  195. memcpy(*bufp, align->code_fill[len], len);
  196. *bufp += len;
  197. } else {
  198. /* Just fill with 0 */
  199. memset(*bufp, 0, len);
  200. *bufp += len;
  201. }
  202. return 0;
  203. }
  204. yasm_bytecode *
  205. yasm_bc_create_align(yasm_expr *boundary, yasm_expr *fill,
  206. yasm_expr *maxskip, const unsigned char **code_fill,
  207. unsigned long line)
  208. {
  209. bytecode_align *align = yasm_xmalloc(sizeof(bytecode_align));
  210. align->boundary = boundary;
  211. align->fill = fill;
  212. align->maxskip = maxskip;
  213. align->code_fill = code_fill;
  214. return yasm_bc_create_common(&bc_align_callback, align, line);
  215. }