nasm-listfmt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * NASM-style list format
  3. *
  4. * Copyright (C) 2004-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.h>
  29. /* NOTE: For this code to generate relocation information, the relocations
  30. * have to be added by the object format to each section in program source
  31. * order.
  32. *
  33. * This should not be an issue, as program source order == section bytecode
  34. * order, so unless the object formats are very obtuse with their bytecode
  35. * iteration, this should just happen.
  36. */
  37. #define REGULAR_BUF_SIZE 1024
  38. yasm_listfmt_module yasm_nasm_LTX_listfmt;
  39. typedef struct sectreloc {
  40. /*@reldef@*/ SLIST_ENTRY(sectreloc) link;
  41. yasm_section *sect;
  42. /*@null@*/ yasm_reloc *next_reloc; /* next relocation in section */
  43. unsigned long next_reloc_addr;
  44. } sectreloc;
  45. typedef struct bcreloc {
  46. /*@reldef@*/ STAILQ_ENTRY(bcreloc) link;
  47. unsigned long offset; /* start of reloc from start of bytecode */
  48. size_t size; /* size of reloc in bytes */
  49. int rel; /* PC/IP-relative or "absolute" */
  50. } bcreloc;
  51. typedef struct nasm_listfmt_output_info {
  52. yasm_arch *arch;
  53. /*@reldef@*/ STAILQ_HEAD(bcrelochead, bcreloc) bcrelocs;
  54. /*@null@*/ yasm_reloc *next_reloc; /* next relocation in section */
  55. unsigned long next_reloc_addr;
  56. } nasm_listfmt_output_info;
  57. static /*@null@*/ /*@only@*/ yasm_listfmt *
  58. nasm_listfmt_create(const char *in_filename, const char *obj_filename)
  59. {
  60. yasm_listfmt_base *listfmt = yasm_xmalloc(sizeof(yasm_listfmt_base));
  61. listfmt->module = &yasm_nasm_LTX_listfmt;
  62. return (yasm_listfmt *)listfmt;
  63. }
  64. static void
  65. nasm_listfmt_destroy(/*@only@*/ yasm_listfmt *listfmt)
  66. {
  67. yasm_xfree(listfmt);
  68. }
  69. static int
  70. nasm_listfmt_output_value(yasm_value *value, unsigned char *buf,
  71. unsigned int destsize, unsigned long offset,
  72. yasm_bytecode *bc, int warn, /*@null@*/ void *d)
  73. {
  74. /*@null@*/ nasm_listfmt_output_info *info = (nasm_listfmt_output_info *)d;
  75. /*@dependent@*/ /*@null@*/ yasm_intnum *intn;
  76. unsigned int valsize = value->size;
  77. assert(info != NULL);
  78. /* Output */
  79. switch (yasm_value_output_basic(value, buf, destsize, bc, warn,
  80. info->arch)) {
  81. case -1:
  82. return 1;
  83. case 0:
  84. break;
  85. default:
  86. return 0;
  87. }
  88. /* Generate reloc if needed */
  89. if (info->next_reloc && info->next_reloc_addr == bc->offset+offset) {
  90. bcreloc *reloc = yasm_xmalloc(sizeof(bcreloc));
  91. reloc->offset = offset;
  92. reloc->size = destsize;
  93. reloc->rel = value->curpos_rel;
  94. STAILQ_INSERT_TAIL(&info->bcrelocs, reloc, link);
  95. /* Get next reloc's info */
  96. info->next_reloc = yasm_section_reloc_next(info->next_reloc);
  97. if (info->next_reloc) {
  98. yasm_intnum *addr;
  99. yasm_symrec *sym;
  100. yasm_reloc_get(info->next_reloc, &addr, &sym);
  101. info->next_reloc_addr = yasm_intnum_get_uint(addr);
  102. }
  103. }
  104. if (value->abs) {
  105. intn = yasm_expr_get_intnum(&value->abs, 0);
  106. if (intn)
  107. return yasm_arch_intnum_tobytes(info->arch, intn, buf, destsize,
  108. valsize, 0, bc, 0);
  109. else {
  110. yasm_error_set(YASM_ERROR_TOO_COMPLEX,
  111. N_("relocation too complex"));
  112. return 1;
  113. }
  114. } else {
  115. int retval;
  116. intn = yasm_intnum_create_uint(0);
  117. retval = yasm_arch_intnum_tobytes(info->arch, intn, buf, destsize,
  118. valsize, 0, bc, 0);
  119. yasm_intnum_destroy(intn);
  120. return retval;
  121. }
  122. return 0;
  123. }
  124. static void
  125. nasm_listfmt_output(yasm_listfmt *listfmt, FILE *f, yasm_linemap *linemap,
  126. yasm_arch *arch)
  127. {
  128. yasm_bytecode *bc;
  129. const char *source;
  130. unsigned long line = 1;
  131. unsigned long listline = 1;
  132. /*@only@*/ unsigned char *buf;
  133. nasm_listfmt_output_info info;
  134. /*@reldef@*/ SLIST_HEAD(sectrelochead, sectreloc) reloc_hist;
  135. /*@null@*/ sectreloc *last_hist = NULL;
  136. /*@null@*/ bcreloc *reloc = NULL;
  137. yasm_section *sect;
  138. SLIST_INIT(&reloc_hist);
  139. info.arch = arch;
  140. buf = yasm_xmalloc(REGULAR_BUF_SIZE);
  141. while (!yasm_linemap_get_source(linemap, line, &bc, &source)) {
  142. if (!bc) {
  143. fprintf(f, "%6lu %*s%s\n", listline++, 32, "", source);
  144. } else {
  145. /* get the next relocation for the bytecode's section */
  146. sect = yasm_bc_get_section(bc);
  147. if (!last_hist || last_hist->sect != sect) {
  148. int found = 0;
  149. /* look through reloc_hist for matching section */
  150. SLIST_FOREACH(last_hist, &reloc_hist, link) {
  151. if (last_hist->sect == sect) {
  152. found = 1;
  153. break;
  154. }
  155. }
  156. if (!found) {
  157. /* not found, add to list*/
  158. last_hist = yasm_xmalloc(sizeof(sectreloc));
  159. last_hist->sect = sect;
  160. last_hist->next_reloc = yasm_section_relocs_first(sect);
  161. if (last_hist->next_reloc) {
  162. yasm_intnum *addr;
  163. yasm_symrec *sym;
  164. yasm_reloc_get(last_hist->next_reloc, &addr, &sym);
  165. last_hist->next_reloc_addr =
  166. yasm_intnum_get_uint(addr);
  167. }
  168. SLIST_INSERT_HEAD(&reloc_hist, last_hist, link);
  169. }
  170. }
  171. info.next_reloc = last_hist->next_reloc;
  172. info.next_reloc_addr = last_hist->next_reloc_addr;
  173. STAILQ_INIT(&info.bcrelocs);
  174. /* loop over bytecodes on this line (usually only one) */
  175. while (bc && bc->line == line) {
  176. /*@null@*/ /*@only@*/ unsigned char *bigbuf;
  177. unsigned long size = REGULAR_BUF_SIZE;
  178. long multiple;
  179. unsigned long offset = bc->offset;
  180. unsigned char *origp, *p;
  181. int gap;
  182. /* convert bytecode into bytes, recording relocs along the
  183. * way
  184. */
  185. bigbuf = yasm_bc_tobytes(bc, buf, &size, &gap, &info,
  186. nasm_listfmt_output_value, NULL);
  187. yasm_bc_get_multiple(bc, &multiple, 1);
  188. if (multiple <= 0)
  189. size = 0;
  190. else
  191. size /= multiple;
  192. /* output bytes with reloc information */
  193. origp = bigbuf ? bigbuf : buf;
  194. p = origp;
  195. reloc = STAILQ_FIRST(&info.bcrelocs);
  196. if (gap) {
  197. fprintf(f, "%6lu %08lX <gap>%*s%s\n", listline++, offset,
  198. 18, "", source ? source : "");
  199. } else while (size > 0) {
  200. int i;
  201. fprintf(f, "%6lu %08lX ", listline++, offset);
  202. for (i=0; i<18 && size > 0; size--) {
  203. if (reloc && (unsigned long)(p-origp) ==
  204. reloc->offset) {
  205. fprintf(f, "%c", reloc->rel ? '(' : '[');
  206. i++;
  207. }
  208. fprintf(f, "%02X", *(p++));
  209. i+=2;
  210. if (reloc && (unsigned long)(p-origp) ==
  211. reloc->offset+reloc->size) {
  212. fprintf(f, "%c", reloc->rel ? ')' : ']');
  213. i++;
  214. reloc = STAILQ_NEXT(reloc, link);
  215. }
  216. }
  217. if (size > 0)
  218. fprintf(f, "-");
  219. else {
  220. if (multiple > 1) {
  221. fprintf(f, "<rept>");
  222. i += 6;
  223. }
  224. fprintf(f, "%*s", 18-i+1, "");
  225. }
  226. if (source) {
  227. fprintf(f, " %s", source);
  228. source = NULL;
  229. }
  230. fprintf(f, "\n");
  231. }
  232. if (bigbuf)
  233. yasm_xfree(bigbuf);
  234. bc = STAILQ_NEXT(bc, link);
  235. }
  236. /* delete bcrelocs (newly generated next bytecode if any) */
  237. reloc = STAILQ_FIRST(&info.bcrelocs);
  238. while (reloc) {
  239. bcreloc *reloc2 = STAILQ_NEXT(reloc, link);
  240. yasm_xfree(reloc);
  241. reloc = reloc2;
  242. }
  243. /* save reloc context */
  244. last_hist->next_reloc = info.next_reloc;
  245. last_hist->next_reloc_addr = info.next_reloc_addr;
  246. }
  247. line++;
  248. }
  249. /* delete reloc history */
  250. while (!SLIST_EMPTY(&reloc_hist)) {
  251. last_hist = SLIST_FIRST(&reloc_hist);
  252. SLIST_REMOVE_HEAD(&reloc_hist, link);
  253. yasm_xfree(last_hist);
  254. }
  255. yasm_xfree(buf);
  256. }
  257. /* Define listfmt structure -- see listfmt.h for details */
  258. yasm_listfmt_module yasm_nasm_LTX_listfmt = {
  259. "NASM-style list format",
  260. "nasm",
  261. nasm_listfmt_create,
  262. nasm_listfmt_destroy,
  263. nasm_listfmt_output
  264. };