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