ne.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * Dumping of NE binaries
  3. *
  4. * Copyright 2002 Alexandre Julliard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include <stdarg.h>
  22. #include <stdio.h>
  23. #include <fcntl.h>
  24. #include "windef.h"
  25. #include "winbase.h"
  26. #include "winedump.h"
  27. struct ne_segtable_entry
  28. {
  29. WORD seg_data_offset; /* Sector offset of segment data */
  30. WORD seg_data_length; /* Length of segment data */
  31. WORD seg_flags; /* Flags associated with this segment */
  32. WORD min_alloc; /* Minimum allocation size for this */
  33. };
  34. struct relocation_entry
  35. {
  36. BYTE address_type; /* Relocation address type */
  37. BYTE relocation_type; /* Relocation type */
  38. WORD offset; /* Offset in segment to fixup */
  39. WORD target1; /* Target specification */
  40. WORD target2; /* Target specification */
  41. };
  42. typedef struct
  43. {
  44. WORD offset;
  45. WORD length;
  46. WORD flags;
  47. WORD id;
  48. WORD handle;
  49. WORD usage;
  50. } NE_NAMEINFO;
  51. typedef struct
  52. {
  53. WORD type_id;
  54. WORD count;
  55. DWORD resloader;
  56. } NE_TYPEINFO;
  57. #define NE_RADDR_LOWBYTE 0
  58. #define NE_RADDR_SELECTOR 2
  59. #define NE_RADDR_POINTER32 3
  60. #define NE_RADDR_OFFSET16 5
  61. #define NE_RADDR_POINTER48 11
  62. #define NE_RADDR_OFFSET32 13
  63. #define NE_RELTYPE_INTERNAL 0
  64. #define NE_RELTYPE_ORDINAL 1
  65. #define NE_RELTYPE_NAME 2
  66. #define NE_RELTYPE_OSFIXUP 3
  67. #define NE_RELFLAG_ADDITIVE 4
  68. #define NE_SEGFLAGS_DATA 0x0001
  69. #define NE_SEGFLAGS_ALLOCATED 0x0002
  70. #define NE_SEGFLAGS_LOADED 0x0004
  71. #define NE_SEGFLAGS_ITERATED 0x0008
  72. #define NE_SEGFLAGS_MOVEABLE 0x0010
  73. #define NE_SEGFLAGS_SHAREABLE 0x0020
  74. #define NE_SEGFLAGS_PRELOAD 0x0040
  75. #define NE_SEGFLAGS_EXECUTEONLY 0x0080
  76. #define NE_SEGFLAGS_READONLY 0x0080
  77. #define NE_SEGFLAGS_RELOC_DATA 0x0100
  78. #define NE_SEGFLAGS_SELFLOAD 0x0800
  79. #define NE_SEGFLAGS_DISCARDABLE 0x1000
  80. #define NE_SEGFLAGS_32BIT 0x2000
  81. #define NE_RSCTYPE_CURSOR 0x8001
  82. #define NE_RSCTYPE_BITMAP 0x8002
  83. #define NE_RSCTYPE_ICON 0x8003
  84. #define NE_RSCTYPE_MENU 0x8004
  85. #define NE_RSCTYPE_DIALOG 0x8005
  86. #define NE_RSCTYPE_STRING 0x8006
  87. #define NE_RSCTYPE_FONTDIR 0x8007
  88. #define NE_RSCTYPE_FONT 0x8008
  89. #define NE_RSCTYPE_ACCELERATOR 0x8009
  90. #define NE_RSCTYPE_RCDATA 0x800a
  91. #define NE_RSCTYPE_GROUP_CURSOR 0x800c
  92. #define NE_RSCTYPE_GROUP_ICON 0x800e
  93. #define NE_RSCTYPE_SCALABLE_FONTPATH 0x80cc
  94. static inline WORD get_word( const BYTE *ptr )
  95. {
  96. return ptr[0] | (ptr[1] << 8);
  97. }
  98. static void dump_ne_header( const IMAGE_OS2_HEADER *ne )
  99. {
  100. printf( "File header:\n" );
  101. printf( "Linker version: %d.%d\n", ne->ne_ver, ne->ne_rev );
  102. printf( "Entry table: %x len %d\n", ne->ne_enttab, ne->ne_cbenttab );
  103. printf( "Checksum: %08x\n", ne->ne_crc );
  104. printf( "Flags: %04x\n", ne->ne_flags );
  105. printf( "Auto data segment: %x\n", ne->ne_autodata );
  106. printf( "Heap size: %d bytes\n", ne->ne_heap );
  107. printf( "Stack size: %d bytes\n", ne->ne_stack );
  108. printf( "Stack pointer: %x:%04x\n", HIWORD(ne->ne_sssp), LOWORD(ne->ne_sssp) );
  109. printf( "Entry point: %x:%04x\n", HIWORD(ne->ne_csip), LOWORD(ne->ne_csip) );
  110. printf( "Number of segments: %d\n", ne->ne_cseg );
  111. printf( "Number of modrefs: %d\n", ne->ne_cmod );
  112. printf( "Segment table: %x\n", ne->ne_segtab );
  113. printf( "Resource table: %x\n", ne->ne_rsrctab );
  114. printf( "Resident name table: %x\n", ne->ne_restab );
  115. printf( "Module table: %x\n", ne->ne_modtab );
  116. printf( "Import table: %x\n", ne->ne_imptab );
  117. printf( "Non-resident table: %x\n", ne->ne_nrestab );
  118. printf( "Exe type: %x\n", ne->ne_exetyp );
  119. printf( "Other flags: %x\n", ne->ne_flagsothers );
  120. printf( "Fast load area: %x-%x\n", ne->ne_pretthunks << ne->ne_align,
  121. (ne->ne_pretthunks+ne->ne_psegrefbytes) << ne->ne_align );
  122. printf( "Expected version: %d.%d\n", HIBYTE(ne->ne_expver), LOBYTE(ne->ne_expver) );
  123. }
  124. static void dump_ne_names( const IMAGE_OS2_HEADER *ne )
  125. {
  126. const unsigned char *pstr = (const unsigned char *)ne + ne->ne_restab;
  127. printf( "\nResident name table:\n" );
  128. while (*pstr)
  129. {
  130. printf( " %4d: %*.*s\n", get_word(pstr + *pstr + 1), *pstr, *pstr, pstr + 1 );
  131. pstr += *pstr + 1 + sizeof(WORD);
  132. }
  133. if (ne->ne_cbnrestab)
  134. {
  135. unsigned int pos = ne->ne_nrestab;
  136. printf( "\nNon-resident name table:\n" );
  137. while ((pstr = PRD(pos, 0)) && *pstr)
  138. {
  139. printf( " %4d: %*.*s\n", get_word(pstr + *pstr + 1), *pstr, *pstr, pstr + 1 );
  140. pos += *pstr + 1 + sizeof(WORD);
  141. }
  142. }
  143. }
  144. static const char *get_resource_type( WORD id )
  145. {
  146. static char buffer[5];
  147. switch(id)
  148. {
  149. case NE_RSCTYPE_CURSOR: return "CURSOR";
  150. case NE_RSCTYPE_BITMAP: return "BITMAP";
  151. case NE_RSCTYPE_ICON: return "ICON";
  152. case NE_RSCTYPE_MENU: return "MENU";
  153. case NE_RSCTYPE_DIALOG: return "DIALOG";
  154. case NE_RSCTYPE_STRING: return "STRING";
  155. case NE_RSCTYPE_FONTDIR: return "FONTDIR";
  156. case NE_RSCTYPE_FONT: return "FONT";
  157. case NE_RSCTYPE_ACCELERATOR: return "ACCELERATOR";
  158. case NE_RSCTYPE_RCDATA: return "RCDATA";
  159. case NE_RSCTYPE_GROUP_CURSOR: return "CURSOR_GROUP";
  160. case NE_RSCTYPE_GROUP_ICON: return "ICON_GROUP";
  161. default:
  162. sprintf( buffer, "%04x", id );
  163. return buffer;
  164. }
  165. }
  166. static void dump_ne_resources( const IMAGE_OS2_HEADER *ne )
  167. {
  168. const NE_NAMEINFO *name;
  169. const void *res_ptr = (const char *)ne + ne->ne_rsrctab;
  170. WORD size_shift = get_word(res_ptr);
  171. const NE_TYPEINFO *info = (const NE_TYPEINFO *)((const WORD *)res_ptr + 1);
  172. int count;
  173. printf( "\nResources:\n" );
  174. while (info->type_id != 0 && (const char *)info < (const char *)ne + ne->ne_restab)
  175. {
  176. name = (const NE_NAMEINFO *)(info + 1);
  177. for (count = info->count; count > 0; count--, name++)
  178. {
  179. if (name->id & 0x8000) printf( " %d", (name->id & ~0x8000) );
  180. else printf( " %.*s", *((const unsigned char *)res_ptr + name->id),
  181. (const char *)res_ptr + name->id + 1 );
  182. if (info->type_id & 0x8000) printf( " %s", get_resource_type(info->type_id) );
  183. else printf( " %.*s", *((const unsigned char *)res_ptr + info->type_id),
  184. (const char *)res_ptr + info->type_id + 1 );
  185. printf(" flags %04x length %04x\n", name->flags, name->length << size_shift);
  186. dump_data( PRD(name->offset << size_shift, name->length << size_shift),
  187. name->length << size_shift, " " );
  188. }
  189. info = (const NE_TYPEINFO *)name;
  190. }
  191. }
  192. static const char *get_export_name( const IMAGE_OS2_HEADER *ne, int ordinal )
  193. {
  194. static char name[256];
  195. const BYTE *pstr;
  196. int pass = 0;
  197. /* search the resident names */
  198. while (pass < 2)
  199. {
  200. if (pass == 0) /* resident names */
  201. {
  202. pstr = (const BYTE *)ne + ne->ne_restab;
  203. if (*pstr) pstr += *pstr + 1 + sizeof(WORD); /* skip first entry (module name) */
  204. }
  205. else /* non-resident names */
  206. {
  207. if (!ne->ne_cbnrestab) break;
  208. pstr = PRD(ne->ne_nrestab, 0);
  209. }
  210. while (*pstr)
  211. {
  212. WORD ord = get_word(pstr + *pstr + 1);
  213. if (ord == ordinal)
  214. {
  215. memcpy( name, pstr + 1, *pstr );
  216. name[*pstr] = 0;
  217. return name;
  218. }
  219. pstr += *pstr + 1 + sizeof(WORD);
  220. }
  221. pass++;
  222. }
  223. name[0] = 0;
  224. return name;
  225. }
  226. static void dump_ne_exports( const IMAGE_OS2_HEADER *ne )
  227. {
  228. const BYTE *ptr = (const BYTE *)ne + ne->ne_enttab;
  229. const BYTE *end = ptr + ne->ne_cbenttab;
  230. int i, ordinal = 1;
  231. if (!ne->ne_cbenttab || !*ptr) return;
  232. printf( "\nExported entry points:\n" );
  233. while (ptr < end && *ptr)
  234. {
  235. BYTE count = *ptr++;
  236. BYTE type = *ptr++;
  237. switch(type)
  238. {
  239. case 0: /* next bundle */
  240. ordinal += count;
  241. break;
  242. case 0xff: /* movable */
  243. for (i = 0; i < count; i++)
  244. {
  245. printf( " %4d MOVABLE %d:%04x %s\n",
  246. ordinal + i, ptr[3], get_word(ptr + 4),
  247. get_export_name( ne, ordinal + i ) );
  248. ptr += 6;
  249. }
  250. ordinal += count;
  251. break;
  252. case 0xfe: /* constant */
  253. for (i = 0; i < count; i++)
  254. {
  255. printf( " %4d CONST %04x %s\n",
  256. ordinal + i, get_word(ptr + 1),
  257. get_export_name( ne, ordinal + i ) );
  258. ptr += 3;
  259. }
  260. ordinal += count;
  261. break;
  262. default: /* fixed */
  263. for (i = 0; i < count; i++)
  264. {
  265. printf( " %4d FIXED %d:%04x %s\n",
  266. ordinal + i, type, get_word(ptr + 1),
  267. get_export_name( ne, ordinal + i ) );
  268. ptr += 3;
  269. }
  270. ordinal += count;
  271. break;
  272. }
  273. }
  274. }
  275. static const char *get_reloc_name( BYTE addr_type, int additive )
  276. {
  277. switch(addr_type & 0x7f)
  278. {
  279. case NE_RADDR_LOWBYTE: return additive ? "byte add" : "byte";
  280. case NE_RADDR_OFFSET16: return additive ? "off16 add" : "off16";
  281. case NE_RADDR_POINTER32: return additive ? "ptr32 add" : "ptr32";
  282. case NE_RADDR_SELECTOR: return additive ? "sel add" : "sel";
  283. case NE_RADDR_POINTER48: return additive ? "ptr48 add" : "ptr48";
  284. case NE_RADDR_OFFSET32: return additive ? "off32 add" : "off32";
  285. }
  286. return "???";
  287. }
  288. static const char *get_seg_flags( WORD flags )
  289. {
  290. static char buffer[256];
  291. buffer[0] = 0;
  292. #define ADD_FLAG(x) if (flags & NE_SEGFLAGS_##x) strcat( buffer, " " #x );
  293. ADD_FLAG(DATA);
  294. ADD_FLAG(ALLOCATED);
  295. ADD_FLAG(LOADED);
  296. ADD_FLAG(ITERATED);
  297. ADD_FLAG(MOVEABLE);
  298. ADD_FLAG(SHAREABLE);
  299. ADD_FLAG(PRELOAD);
  300. ADD_FLAG(EXECUTEONLY);
  301. ADD_FLAG(READONLY);
  302. ADD_FLAG(RELOC_DATA);
  303. ADD_FLAG(SELFLOAD);
  304. ADD_FLAG(DISCARDABLE);
  305. ADD_FLAG(32BIT);
  306. #undef ADD_FLAG
  307. if (buffer[0])
  308. {
  309. buffer[0] = '(';
  310. strcat( buffer, ")" );
  311. }
  312. return buffer;
  313. }
  314. static void dump_relocations( const IMAGE_OS2_HEADER *ne, WORD count,
  315. const struct relocation_entry *rep )
  316. {
  317. const WORD *modref = (const WORD *)((const BYTE *)ne + ne->ne_modtab);
  318. const BYTE *mod_name, *func_name;
  319. WORD i;
  320. for (i = 0; i < count; i++, rep++)
  321. {
  322. int additive = rep->relocation_type & NE_RELFLAG_ADDITIVE;
  323. switch (rep->relocation_type & 3)
  324. {
  325. case NE_RELTYPE_ORDINAL:
  326. mod_name = (const BYTE *)ne + ne->ne_imptab + modref[rep->target1 - 1];
  327. printf( "%6d: %s = %*.*s.%d\n", i + 1, get_reloc_name( rep->address_type, additive ),
  328. *mod_name, *mod_name, mod_name + 1, rep->target2 );
  329. break;
  330. case NE_RELTYPE_NAME:
  331. mod_name = (const BYTE *)ne + ne->ne_imptab + modref[rep->target1 - 1];
  332. func_name = (const BYTE *)ne + ne->ne_imptab + rep->target2;
  333. printf( "%6d: %s = %*.*s.%*.*s\n", i + 1, get_reloc_name( rep->address_type, additive ),
  334. *mod_name, *mod_name, mod_name + 1,
  335. *func_name, *func_name, func_name + 1 );
  336. break;
  337. case NE_RELTYPE_INTERNAL:
  338. if ((rep->target1 & 0xff) == 0xff)
  339. {
  340. /* the module itself */
  341. mod_name = (const BYTE *)ne + ne->ne_restab;
  342. printf( "%6d: %s = %*.*s.%d\n", i + 1, get_reloc_name( rep->address_type, additive ),
  343. *mod_name, *mod_name, mod_name + 1, rep->target2 );
  344. }
  345. else
  346. printf( "%6d: %s = %d:%04x\n", i + 1, get_reloc_name( rep->address_type, additive ),
  347. rep->target1, rep->target2 );
  348. break;
  349. case NE_RELTYPE_OSFIXUP:
  350. /* Relocation type 7:
  351. *
  352. * These appear to be used as fixups for the Windows
  353. * floating point emulator. Let's just ignore them and
  354. * try to use the hardware floating point. Linux should
  355. * successfully emulate the coprocessor if it doesn't
  356. * exist.
  357. */
  358. printf( "%6d: %s = TYPE %d, OFFSET %04x, TARGET %04x %04x\n",
  359. i + 1, get_reloc_name( rep->address_type, additive ),
  360. rep->relocation_type, rep->offset,
  361. rep->target1, rep->target2 );
  362. break;
  363. }
  364. }
  365. }
  366. static void dump_ne_segment( const IMAGE_OS2_HEADER *ne, int segnum )
  367. {
  368. const struct ne_segtable_entry *table = (const struct ne_segtable_entry *)((const BYTE *)ne + ne->ne_segtab);
  369. const struct ne_segtable_entry *seg = table + segnum - 1;
  370. printf( "\nSegment %d:\n", segnum );
  371. printf( " File offset: %08x\n", seg->seg_data_offset << ne->ne_align );
  372. printf( " Length: %08x\n", seg->seg_data_length );
  373. printf( " Flags: %08x %s\n", seg->seg_flags, get_seg_flags(seg->seg_flags) );
  374. printf( " Alloc size: %08x\n", seg->min_alloc );
  375. if (seg->seg_flags & NE_SEGFLAGS_RELOC_DATA)
  376. {
  377. const BYTE *ptr = PRD((seg->seg_data_offset << ne->ne_align) + seg->seg_data_length, 0);
  378. WORD count = get_word(ptr);
  379. ptr += sizeof(WORD);
  380. printf( " Relocations:\n" );
  381. dump_relocations( ne, count, (const struct relocation_entry *)ptr );
  382. }
  383. }
  384. void ne_dump( void )
  385. {
  386. unsigned int i;
  387. const IMAGE_DOS_HEADER *dos;
  388. const IMAGE_OS2_HEADER *ne;
  389. dos = PRD(0, sizeof(*dos));
  390. if (!dos) return;
  391. ne = PRD(dos->e_lfanew, sizeof(*ne));
  392. print_fake_dll();
  393. if (globals.do_dumpheader || !globals.dumpsect)
  394. dump_ne_header( ne );
  395. if (globals.do_dumpheader)
  396. dump_ne_names( ne );
  397. if (globals.dumpsect)
  398. {
  399. BOOL all = strcmp(globals.dumpsect, "ALL") == 0;
  400. if (all || !strcmp(globals.dumpsect, "resource"))
  401. dump_ne_resources( ne );
  402. if (all || !strcmp(globals.dumpsect, "export"))
  403. dump_ne_exports( ne );
  404. }
  405. if (globals.do_dumpheader)
  406. for (i = 1; i <= ne->ne_cseg; i++) dump_ne_segment( ne, i );
  407. }