bc-org.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * ORG 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 "file.h"
  31. #include "errwarn.h"
  32. #include "intnum.h"
  33. #include "expr.h"
  34. #include "value.h"
  35. #include "bytecode.h"
  36. typedef struct bytecode_org {
  37. unsigned long start; /* target starting offset within section */
  38. unsigned long fill; /* fill value */
  39. } bytecode_org;
  40. static void bc_org_destroy(void *contents);
  41. static void bc_org_print(const void *contents, FILE *f, int indent_level);
  42. static void bc_org_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc);
  43. static int bc_org_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span,
  44. void *add_span_data);
  45. static int bc_org_expand(yasm_bytecode *bc, int span, long old_val,
  46. long new_val, /*@out@*/ long *neg_thres,
  47. /*@out@*/ long *pos_thres);
  48. static int bc_org_tobytes(yasm_bytecode *bc, unsigned char **bufp,
  49. unsigned char *bufstart, void *d,
  50. yasm_output_value_func output_value,
  51. /*@null@*/ yasm_output_reloc_func output_reloc);
  52. static const yasm_bytecode_callback bc_org_callback = {
  53. bc_org_destroy,
  54. bc_org_print,
  55. bc_org_finalize,
  56. NULL,
  57. bc_org_calc_len,
  58. bc_org_expand,
  59. bc_org_tobytes,
  60. YASM_BC_SPECIAL_OFFSET
  61. };
  62. static void
  63. bc_org_destroy(void *contents)
  64. {
  65. yasm_xfree(contents);
  66. }
  67. static void
  68. bc_org_print(const void *contents, FILE *f, int indent_level)
  69. {
  70. const bytecode_org *org = (const bytecode_org *)contents;
  71. fprintf(f, "%*s_Org_\n", indent_level, "");
  72. fprintf(f, "%*sStart=%lu\n", indent_level, "", org->start);
  73. }
  74. static void
  75. bc_org_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc)
  76. {
  77. }
  78. static int
  79. bc_org_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span,
  80. void *add_span_data)
  81. {
  82. bytecode_org *org = (bytecode_org *)bc->contents;
  83. long neg_thres = 0;
  84. long pos_thres = org->start;
  85. if (bc_org_expand(bc, 0, 0, (long)bc->offset, &neg_thres, &pos_thres) < 0)
  86. return -1;
  87. return 0;
  88. }
  89. static int
  90. bc_org_expand(yasm_bytecode *bc, int span, long old_val, long new_val,
  91. /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres)
  92. {
  93. bytecode_org *org = (bytecode_org *)bc->contents;
  94. /* Check for overrun */
  95. if ((unsigned long)new_val > org->start) {
  96. yasm_error_set(YASM_ERROR_GENERAL,
  97. N_("ORG overlap with already existing data"));
  98. return -1;
  99. }
  100. /* Generate space to start offset */
  101. bc->len = org->start - new_val;
  102. return 1;
  103. }
  104. static int
  105. bc_org_tobytes(yasm_bytecode *bc, unsigned char **bufp,
  106. unsigned char *bufstart, void *d,
  107. yasm_output_value_func output_value,
  108. /*@unused@*/ yasm_output_reloc_func output_reloc)
  109. {
  110. bytecode_org *org = (bytecode_org *)bc->contents;
  111. unsigned long len, i;
  112. /* Sanity check for overrun */
  113. if (bc->offset > org->start) {
  114. yasm_error_set(YASM_ERROR_GENERAL,
  115. N_("ORG overlap with already existing data"));
  116. return 1;
  117. }
  118. len = org->start - bc->offset;
  119. for (i=0; i<len; i++)
  120. YASM_WRITE_8(*bufp, org->fill); /* XXX: handle more than 8 bit? */
  121. return 0;
  122. }
  123. yasm_bytecode *
  124. yasm_bc_create_org(unsigned long start, unsigned long fill, unsigned long line)
  125. {
  126. bytecode_org *org = yasm_xmalloc(sizeof(bytecode_org));
  127. org->start = start;
  128. org->fill = fill;
  129. return yasm_bc_create_common(&bc_org_callback, org, line);
  130. }