ne.c 15 KB

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