dbg-objfmt.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Debugging object format (used to debug object format module interface)
  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.h>
  29. typedef struct yasm_objfmt_dbg {
  30. yasm_objfmt_base objfmt; /* base structure */
  31. FILE *dbgfile;
  32. } yasm_objfmt_dbg;
  33. yasm_objfmt_module yasm_dbg_LTX_objfmt;
  34. static yasm_objfmt *
  35. dbg_objfmt_create(yasm_object *object)
  36. {
  37. yasm_objfmt_dbg *objfmt_dbg = yasm_xmalloc(sizeof(yasm_objfmt_dbg));
  38. objfmt_dbg->objfmt.module = &yasm_dbg_LTX_objfmt;
  39. objfmt_dbg->dbgfile = tmpfile();
  40. if (!objfmt_dbg->dbgfile) {
  41. fprintf(stderr, N_("could not open temporary file"));
  42. return 0;
  43. }
  44. fprintf(objfmt_dbg->dbgfile, "create()\n");
  45. return (yasm_objfmt *)objfmt_dbg;
  46. }
  47. static void
  48. dbg_objfmt_output(yasm_object *object, FILE *f, int all_syms,
  49. yasm_errwarns *errwarns)
  50. {
  51. yasm_objfmt_dbg *objfmt_dbg = (yasm_objfmt_dbg *)object->objfmt;
  52. char buf[1024];
  53. size_t i;
  54. /* Copy temp file to real output file */
  55. rewind(objfmt_dbg->dbgfile);
  56. while ((i = fread(buf, 1, 1024, objfmt_dbg->dbgfile))) {
  57. if (fwrite(buf, 1, i, f) != i)
  58. break;
  59. }
  60. /* Reassign objfmt debug file to output file */
  61. fclose(objfmt_dbg->dbgfile);
  62. objfmt_dbg->dbgfile = f;
  63. fprintf(objfmt_dbg->dbgfile, "output(f, object->\n");
  64. yasm_object_print(object, objfmt_dbg->dbgfile, 1);
  65. fprintf(objfmt_dbg->dbgfile, "%d)\n", all_syms);
  66. fprintf(objfmt_dbg->dbgfile, " Symbol Table:\n");
  67. yasm_symtab_print(object->symtab, objfmt_dbg->dbgfile, 1);
  68. }
  69. static void
  70. dbg_objfmt_destroy(yasm_objfmt *objfmt)
  71. {
  72. yasm_objfmt_dbg *objfmt_dbg = (yasm_objfmt_dbg *)objfmt;
  73. fprintf(objfmt_dbg->dbgfile, "destroy()\n");
  74. yasm_xfree(objfmt);
  75. }
  76. static void
  77. dbg_objfmt_init_new_section(yasm_section *sect, unsigned long line)
  78. {
  79. yasm_object *object = yasm_section_get_object(sect);
  80. yasm_objfmt_dbg *objfmt_dbg = (yasm_objfmt_dbg *)object->objfmt;
  81. fprintf(objfmt_dbg->dbgfile, "init_new_section(\"%s\", %lu)\n",
  82. yasm_section_get_name(sect), line);
  83. yasm_symtab_define_label(object->symtab, ".text",
  84. yasm_section_bcs_first(sect), 1, 0);
  85. }
  86. static yasm_section *
  87. dbg_objfmt_add_default_section(yasm_object *object)
  88. {
  89. yasm_objfmt_dbg *objfmt_dbg = (yasm_objfmt_dbg *)object->objfmt;
  90. yasm_section *retval;
  91. int isnew;
  92. fprintf(objfmt_dbg->dbgfile, "add_default_section()\n");
  93. retval = yasm_object_get_general(object, ".text", 0, 0, 0, &isnew, 0);
  94. if (isnew) {
  95. yasm_section_set_default(retval, 1);
  96. }
  97. return retval;
  98. }
  99. static /*@observer@*/ /*@null@*/ yasm_section *
  100. dbg_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams,
  101. /*@unused@*/ /*@null@*/
  102. yasm_valparamhead *objext_valparams,
  103. unsigned long line)
  104. {
  105. yasm_objfmt_dbg *objfmt_dbg = (yasm_objfmt_dbg *)object->objfmt;
  106. yasm_valparam *vp;
  107. yasm_section *retval;
  108. int isnew;
  109. fprintf(objfmt_dbg->dbgfile, "section_switch(headp, ");
  110. yasm_vps_print(valparams, objfmt_dbg->dbgfile);
  111. fprintf(objfmt_dbg->dbgfile, ", ");
  112. yasm_vps_print(objext_valparams, objfmt_dbg->dbgfile);
  113. fprintf(objfmt_dbg->dbgfile, ", %lu), returning ", line);
  114. vp = yasm_vps_first(valparams);
  115. if (!yasm_vp_string(vp)) {
  116. fprintf(objfmt_dbg->dbgfile, "NULL\n");
  117. return NULL;
  118. }
  119. retval = yasm_object_get_general(object, yasm_vp_string(vp), 0, 0, 0,
  120. &isnew, line);
  121. if (isnew) {
  122. fprintf(objfmt_dbg->dbgfile, "(new) ");
  123. }
  124. yasm_section_set_default(retval, 0);
  125. fprintf(objfmt_dbg->dbgfile, "\"%s\" section\n", vp->val);
  126. return retval;
  127. }
  128. static /*@observer@*/ /*@null@*/ yasm_symrec *
  129. dbg_objfmt_get_special_sym(yasm_object *object, const char *name,
  130. const char *parser)
  131. {
  132. yasm_objfmt_dbg *objfmt_dbg = (yasm_objfmt_dbg *)object->objfmt;
  133. fprintf(objfmt_dbg->dbgfile, "get_special_sym(object, \"%s\", \"%s\")\n",
  134. name, parser);
  135. return NULL;
  136. }
  137. /* Define valid debug formats to use with this object format */
  138. static const char *dbg_objfmt_dbgfmt_keywords[] = {
  139. "null",
  140. NULL
  141. };
  142. /* Define objfmt structure -- see objfmt.h for details */
  143. yasm_objfmt_module yasm_dbg_LTX_objfmt = {
  144. "Trace of all info passed to object format module",
  145. "dbg",
  146. "dbg",
  147. 32,
  148. 0,
  149. dbg_objfmt_dbgfmt_keywords,
  150. "null",
  151. NULL, /* no directives */
  152. NULL, /* no standard macros */
  153. dbg_objfmt_create,
  154. dbg_objfmt_output,
  155. dbg_objfmt_destroy,
  156. dbg_objfmt_add_default_section,
  157. dbg_objfmt_init_new_section,
  158. dbg_objfmt_section_switch,
  159. dbg_objfmt_get_special_sym
  160. };