00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "htatom.h"
00022 #include "htcoff.h"
00023 #include "htcoffhd.h"
00024 #include "httag.h"
00025 #include "htstring.h"
00026 #include "formats.h"
00027 #include "snprintf.h"
00028
00029 #include <string.h>
00030
00031 int_hash coff_machines[] =
00032 {
00033
00034 {COFF_MACHINE_I386, "Intel 386"},
00035 {COFF_MACHINE_I486, "Intel 486"},
00036 {COFF_MACHINE_I586, "Intel 586"},
00037 {COFF_MACHINE_R3000BE, "R3000(be)"},
00038 {COFF_MACHINE_R3000, "R3000"},
00039 {COFF_MACHINE_R4000, "R4000"},
00040 {COFF_MACHINE_R10000, "R10000"},
00041 {COFF_MACHINE_ALPHA, "Alpha AXP"},
00042 {COFF_MACHINE_SH3, "Hitachi SH3"},
00043 {COFF_MACHINE_SH4, "Hitachi SH4"},
00044 {COFF_MACHINE_ARM, "ARM"},
00045 {COFF_MACHINE_THUMB, "THUMB"},
00046 {COFF_MACHINE_POWERPC_LE, "Power PC (little-endian)"},
00047 {COFF_MACHINE_POWERPC_BE, "Power PC (big-endian)"},
00048 {COFF_MACHINE_IA64, "Intel IA64"},
00049 {COFF_MACHINE_MIPS16, "MIPS16"},
00050 {COFF_MACHINE_68k, "Motorola 68k"},
00051 {COFF_MACHINE_ALPHA_AXP_64, "Alpha AXP 64"},
00052 {COFF_MACHINE_MIPSf, "MIPSf"},
00053 {COFF_MACHINE_MIPS16f, "MIPS16f"},
00054 {COFF_MACHINE_AMD_HAMMER, "x86-64 (AMD Opteron)"},
00055 {0, 0}
00056 };
00057
00058 static int_hash coff_optional_magics[] =
00059 {
00060 {COFF_OPTMAGIC_ROMIMAGE, "ROM image"},
00061 {COFF_OPTMAGIC_COFF32, "COFF"},
00062 {0, 0}
00063 };
00064
00065 static int_hash coff_optional_sizes[] =
00066 {
00067 {COFF_OPTSIZE_COFF32, "COFF32"},
00068 {COFF_OPTSIZE_XCOFF32, "XCOFF32"},
00069 {0, 0}
00070 };
00071
00072 ht_tag_flags_s coff_characteristics[] =
00073 {
00074 {-1, "COFF - file characteristics"},
00075 {0, "[00] relocations stripped"},
00076 {1, "[01] file is executable"},
00077 {2, "[02] line numbers stripped"},
00078 {3, "[03] local symbols stripped"},
00079 {4, "[04] aggressively trim working set"},
00080 {5, "[05] * reserved"},
00081 {6, "[06] * reserved"},
00082 {7, "[07] low bytes of machine word are reversed"},
00083 {8, "[08] 32 bit machine"},
00084 {9, "[09] debugging information stripped"},
00085 {10, "[10] run from swap if image on removable media"},
00086 {11, "[11] run from swap if image is on net"},
00087 {12, "[12] system file"},
00088 {13, "[13] file is dynamic link library (dll)"},
00089 {14, "[14] single processor (UP) only"},
00090 {15, "[15] high bytes of machine word are reversed"},
00091 {0, 0}
00092 };
00093
00094 ht_tag_flags_s coff_section_characteristics[] =
00095 {
00096 {-1, "COFF - section characteristics"},
00097 {0, "[00] * reserved"},
00098 {1, "[01] * reserved"},
00099 {2, "[02] * reserved"},
00100 {3, "[03] * reserved"},
00101 {4, "[04] * reserved"},
00102 {5, "[05] code"},
00103 {6, "[06] initialized"},
00104 {7, "[07] uninitialized"},
00105 {8, "[08] * reserved"},
00106 {9, "[09] * reserved"},
00107 {10, "[10] * reserved"},
00108 {11, "[11] link remove"},
00109 {12, "[12] comdat"},
00110 {13, "[13] * reserved"},
00111 {14, "[14] * reserved (obsolete - protected)"},
00112 {15, "[15] fardata"},
00113 {16, "[16] * reserved (obsolete - sysheap)"},
00114 {17, "[17] purgeable/16bit ?"},
00115 {18, "[18] locked"},
00116 {19, "[19] preload"},
00117 {20, "[20] * reserved"},
00118 {21, "[21] * reserved"},
00119 {22, "[22] * reserved"},
00120 {23, "[23] * reserved"},
00121 {24, "[24] extended relocations"},
00122 {25, "[25] discardable"},
00123 {26, "[26] not cachable"},
00124 {27, "[27] not pageable"},
00125 {28, "[28] shareable"},
00126 {29, "[29] executable"},
00127 {30, "[30] readable"},
00128 {31, "[31] writable"},
00129 {0, 0}
00130 };
00131
00132 ht_mask_ptable coffheader[]=
00133 {
00134 {"machine", STATICTAG_EDIT_WORD_VE("00000000")" "STATICTAG_DESC_WORD_VE("00000000", ATOM_COFF_MACHINES_STR)},
00135 {"number of sections", STATICTAG_EDIT_WORD_VE("00000002")},
00136 {"time-date stamp", STATICTAG_EDIT_TIME("00000004")},
00137 {"pointer to symbol table", STATICTAG_EDIT_DWORD_VE("00000008")},
00138 {"number of symbols", STATICTAG_EDIT_DWORD_VE("0000000c")},
00139 {"size of optional header", STATICTAG_EDIT_WORD_VE("00000010")" "STATICTAG_DESC_WORD_VE("00000010", ATOM_COFF_OPTIONAL_SIZES_STR)},
00140 {"characteristics", STATICTAG_EDIT_WORD_VE("00000012")" "STATICTAG_FLAGS("00000012", ATOM_COFF_CHARACTERISTICS_STR)},
00141 {0, 0}
00142 };
00143
00144 ht_mask_ptable coff32header[] = {
00145 {"optional magic", STATICTAG_EDIT_WORD_VE("00000014")" "STATICTAG_DESC_WORD_VE("00000014", ATOM_COFF_OPTIONAL_MAGICS_STR)},
00146 {"major linker version", STATICTAG_EDIT_BYTE("00000016")},
00147 {"minor linker version", STATICTAG_EDIT_BYTE("00000017")},
00148 {"size of code", STATICTAG_EDIT_DWORD_VE("00000018")},
00149 {"size of data", STATICTAG_EDIT_DWORD_VE("0000001c")},
00150 {"size of bss", STATICTAG_EDIT_DWORD_VE("00000020")},
00151 {"entry point", STATICTAG_EDIT_DWORD_VE("00000024")},
00152 {"code base", STATICTAG_EDIT_DWORD_VE("00000028")},
00153 {"data base", STATICTAG_EDIT_DWORD_VE("0000002c")},
00154 {0, 0}
00155 };
00156
00157 ht_mask_ptable xcoff32header[] = {
00158 {"optional magic", STATICTAG_EDIT_WORD_VE("00000014")" "STATICTAG_DESC_WORD_VE("00000014", ATOM_COFF_OPTIONAL_MAGICS_STR)},
00159 {"major linker version", STATICTAG_EDIT_BYTE("00000016")},
00160 {"minor linker version", STATICTAG_EDIT_BYTE("00000017")},
00161 {"size of code", STATICTAG_EDIT_DWORD_VE("00000018")},
00162 {"size of data", STATICTAG_EDIT_DWORD_VE("0000001c")},
00163 {"size of bss", STATICTAG_EDIT_DWORD_VE("00000020")},
00164 {"address of entrypoint", STATICTAG_EDIT_DWORD_VE("00000024")},
00165 {"code base", STATICTAG_EDIT_DWORD_VE("00000028")},
00166 {"data base", STATICTAG_EDIT_DWORD_VE("0000002c")},
00167 {"address of TOC anchor", STATICTAG_EDIT_DWORD_VE("00000030")},
00168 {"section of entrypoint", STATICTAG_EDIT_WORD_VE("00000034")},
00169 {"section of .text", STATICTAG_EDIT_WORD_VE("00000036")},
00170 {"section of .data", STATICTAG_EDIT_WORD_VE("00000038")},
00171 {"section of TOC", STATICTAG_EDIT_WORD_VE("0000003a")},
00172 {"section of loader data", STATICTAG_EDIT_WORD_VE("0000003c")},
00173 {"section of .bss", STATICTAG_EDIT_WORD_VE("0000003e")},
00174 {"max. alignment for .text", STATICTAG_EDIT_WORD_VE("00000040")},
00175 {"max. alignment for .data", STATICTAG_EDIT_WORD_VE("00000042")},
00176 {"module type field", STATICTAG_EDIT_WORD_VE("00000044")},
00177 {"cpu type", STATICTAG_EDIT_BYTE("00000046")},
00178 {"cpu type (reserved)", STATICTAG_EDIT_BYTE("00000047")},
00179 {"max. stack size", STATICTAG_EDIT_DWORD_VE("00000048")},
00180 {"max. data size", STATICTAG_EDIT_DWORD_VE("0000004c")},
00181 {"reserved for debuggers", STATICTAG_EDIT_DWORD_VE("00000050")},
00182 {"reserved field", STATICTAG_EDIT_DWORD_VE("00000054")},
00183 {"reserved field", STATICTAG_EDIT_DWORD_VE("00000058")},
00184 {0, 0}
00185 };
00186
00187 ht_mask_ptable coff_section[] = {
00188 {"name", STATICTAG_EDIT_CHAR("00000000") STATICTAG_EDIT_CHAR("00000001") STATICTAG_EDIT_CHAR("00000002") STATICTAG_EDIT_CHAR("00000003") STATICTAG_EDIT_CHAR("00000004") STATICTAG_EDIT_CHAR("00000005") STATICTAG_EDIT_CHAR("00000006") STATICTAG_EDIT_CHAR("00000007")},
00189 {"virt. size OR phys. address", STATICTAG_EDIT_DWORD_VE("00000008")},
00190 {"virtual address", STATICTAG_EDIT_DWORD_VE("0000000c")},
00191 {"size", STATICTAG_EDIT_DWORD_VE("00000010")},
00192 {"offset", STATICTAG_EDIT_DWORD_VE("00000014")},
00193 {"relocation table offset", STATICTAG_EDIT_DWORD_VE("00000018")},
00194 {"line number table offset", STATICTAG_EDIT_DWORD_VE("0000001c")},
00195 {"relocation count", STATICTAG_EDIT_WORD_VE("00000020")},
00196 {"line number count", STATICTAG_EDIT_WORD_VE("00000022")},
00197 {"characteristics", STATICTAG_EDIT_DWORD_VE("00000024")" "STATICTAG_FLAGS("00000024", ATOM_COFF_SECTION_CHARACTERISTICS_STR)},
00198 {0, 0}
00199 };
00200
00201 static ht_view *htcoffheader_init(bounds *b, ht_streamfile *file, ht_format_group *group)
00202 {
00203 ht_coff_shared_data *coff_shared = (ht_coff_shared_data *)group->get_shared_data();
00204 bool coff_bigendian = coff_shared->endian == big_endian;
00205
00206 dword h = coff_shared->hdr_ofs;
00207 ht_uformat_viewer *v = new ht_uformat_viewer();
00208 v->init(b, DESC_COFF_HEADER, VC_EDIT, file, group);
00209
00210 ht_mask_sub *m = new ht_mask_sub();
00211 m->init(file, 0);
00212 register_atom(ATOM_COFF_MACHINES, coff_machines);
00213 register_atom(ATOM_COFF_OPTIONAL_MAGICS, coff_optional_magics);
00214 register_atom(ATOM_COFF_OPTIONAL_SIZES, coff_optional_sizes);
00215 register_atom(ATOM_COFF_CHARACTERISTICS, coff_characteristics);
00216 register_atom(ATOM_COFF_SECTION_CHARACTERISTICS, coff_section_characteristics);
00217 char info[128];
00218 ht_snprintf(info, sizeof info, "* COFF header at offset %08x", h);
00219 m->add_mask(info);
00220
00221
00222 m->add_mask("--- COFF header ---");
00223 m->add_staticmask_ptable(coffheader, h, coff_bigendian);
00224
00225 if (coff_shared->coffheader.optional_header_size >= 2) {
00226 m->add_mask("--- optional header ---");
00227 word opt = coff_shared->opt_magic;
00228
00229
00230 switch (opt) {
00231 case COFF_OPTMAGIC_COFF32:
00232 switch (coff_shared->coffheader.optional_header_size) {
00233 case COFF_OPTSIZE_COFF32:
00234 m->add_staticmask_ptable(coff32header, h, coff_bigendian);
00235 break;
00236 case COFF_OPTSIZE_XCOFF32:
00237 m->add_staticmask_ptable(xcoff32header, h, coff_bigendian);
00238 break;
00239 }
00240 break;
00241 default: {
00242 m->add_staticmask("optional magic "STATICTAG_EDIT_WORD_VE("00000018")" "STATICTAG_DESC_WORD_VE("00000018", ATOM_COFF_OPTIONAL_MAGICS_STR), h+20, coff_bigendian);
00243 m->add_mask("-------------------------------------------------------------------------");
00244 m->add_mask("Unsupported optional magic ! If you get this message in an original");
00245 m->add_mask("(unmodified) file, please contact us (see help).");
00246 }
00247 }
00248 }
00249
00250 int sc = coff_shared->sections.section_count;
00251 int os = coff_shared->coffheader.optional_header_size;
00252
00253
00254
00255 v->insertsub(m);
00256
00257 for (int i=0; i < sc; i++) {
00258 ht_mask_sub *n=new ht_mask_sub();
00259 n->init(file, i);
00260
00261 n->add_staticmask_ptable(coff_section, h+20+os+i*40, coff_bigendian);
00262
00263 char nm[9];
00264 memmove(nm, coff_shared->sections.sections[i].name, 8);
00265 nm[8] = 0;
00266
00267 char t[32];
00268 ht_snprintf(t, sizeof t, "section %d: %s", i, nm);
00269
00270 ht_collapsable_sub *cn=new ht_collapsable_sub();
00271 cn->init(file, n, 1, t, 1);
00272
00273 v->insertsub(cn);
00274 }
00275 return v;
00276 }
00277
00278 format_viewer_if htcoffheader_if = {
00279 htcoffheader_init,
00280 0
00281 };