blob: 9328ccfec631357ac6630adca3b799b1d041994c [file] [log] [blame]
Alexandre Julliard16f3c782002-10-02 18:50:09 +00001/*
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard16f3c782002-10-02 18:50:09 +000019 */
20
Jon Griffiths140eb972003-08-18 20:00:44 +000021#include "config.h"
22#include "wine/port.h"
23
Alexandre Julliard16f3c782002-10-02 18:50:09 +000024#include <fcntl.h>
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000025#include <stdarg.h>
Alexandre Julliard16f3c782002-10-02 18:50:09 +000026#include <stdio.h>
Jon Griffiths140eb972003-08-18 20:00:44 +000027#ifdef HAVE_UNISTD_H
Alexandre Julliard16f3c782002-10-02 18:50:09 +000028#include <unistd.h>
Jon Griffiths140eb972003-08-18 20:00:44 +000029#endif
Alexandre Julliard16f3c782002-10-02 18:50:09 +000030
Alexandre Julliard435e2e62002-12-10 22:56:43 +000031#include "windef.h"
Alexandre Julliard16f3c782002-10-02 18:50:09 +000032#include "winbase.h"
33#include "wine/winbase16.h"
34#include "winedump.h"
35
Alexandre Julliarddd41c122005-07-01 19:23:39 +000036struct ne_segtable_entry
37{
38 WORD seg_data_offset; /* Sector offset of segment data */
39 WORD seg_data_length; /* Length of segment data */
40 WORD seg_flags; /* Flags associated with this segment */
41 WORD min_alloc; /* Minimum allocation size for this */
42};
43
44struct relocation_entry
45{
46 BYTE address_type; /* Relocation address type */
47 BYTE relocation_type; /* Relocation type */
48 WORD offset; /* Offset in segment to fixup */
49 WORD target1; /* Target specification */
50 WORD target2; /* Target specification */
51};
52
53#define NE_RADDR_LOWBYTE 0
54#define NE_RADDR_SELECTOR 2
55#define NE_RADDR_POINTER32 3
56#define NE_RADDR_OFFSET16 5
57#define NE_RADDR_POINTER48 11
58#define NE_RADDR_OFFSET32 13
59
60#define NE_RELTYPE_INTERNAL 0
61#define NE_RELTYPE_ORDINAL 1
62#define NE_RELTYPE_NAME 2
63#define NE_RELTYPE_OSFIXUP 3
64#define NE_RELFLAG_ADDITIVE 4
65
Alexandre Julliard92462c22004-07-13 03:43:25 +000066static inline WORD get_word( const BYTE *ptr )
67{
68 return ptr[0] | (ptr[1] << 8);
69}
70
Alexandre Julliard16f3c782002-10-02 18:50:09 +000071static void dump_ne_header( const IMAGE_OS2_HEADER *ne )
72{
73 printf( "File header:\n" );
74 printf( "Linker version: %d.%d\n", ne->ne_ver, ne->ne_rev );
75 printf( "Entry table: %x len %d\n", ne->ne_enttab, ne->ne_cbenttab );
Michael Stefaniucba123ab2006-09-29 21:32:57 +020076 printf( "Checksum: %08x\n", ne->ne_crc );
Alexandre Julliard16f3c782002-10-02 18:50:09 +000077 printf( "Flags: %04x\n", ne->ne_flags );
78 printf( "Auto data segment: %x\n", ne->ne_autodata );
79 printf( "Heap size: %d bytes\n", ne->ne_heap );
80 printf( "Stack size: %d bytes\n", ne->ne_stack );
81 printf( "Stack pointer: %x:%04x\n", SELECTOROF(ne->ne_sssp), OFFSETOF(ne->ne_sssp) );
82 printf( "Entry point: %x:%04x\n", SELECTOROF(ne->ne_csip), OFFSETOF(ne->ne_csip) );
83 printf( "Number of segments: %d\n", ne->ne_cseg );
84 printf( "Number of modrefs: %d\n", ne->ne_cmod );
85 printf( "Segment table: %x\n", ne->ne_segtab );
86 printf( "Resource table: %x\n", ne->ne_rsrctab );
87 printf( "Resident name table: %x\n", ne->ne_restab );
88 printf( "Module table: %x\n", ne->ne_modtab );
89 printf( "Import table: %x\n", ne->ne_imptab );
Michael Stefaniucba123ab2006-09-29 21:32:57 +020090 printf( "Non-resident table: %x\n", ne->ne_nrestab );
Alexandre Julliard16f3c782002-10-02 18:50:09 +000091 printf( "Exe type: %x\n", ne->ne_exetyp );
92 printf( "Other flags: %x\n", ne->ne_flagsothers );
Patrik Stridvall1298eb42002-10-23 18:50:10 +000093 printf( "Fast load area: %x-%x\n", ne->ne_pretthunks << ne->ne_align,
94 (ne->ne_pretthunks+ne->ne_psegrefbytes) << ne->ne_align );
Alexandre Julliard16f3c782002-10-02 18:50:09 +000095 printf( "Expected version: %d.%d\n", HIBYTE(ne->ne_expver), LOBYTE(ne->ne_expver) );
96}
97
Eric Pouechafe309b2006-11-29 21:40:00 +010098static void dump_ne_names( const IMAGE_OS2_HEADER *ne )
Alexandre Julliard757caa02003-09-27 02:34:54 +000099{
Mike McCormack723ee0a2005-07-05 14:26:54 +0000100 const unsigned char *pstr = (const unsigned char *)ne + ne->ne_restab;
Alexandre Julliard757caa02003-09-27 02:34:54 +0000101
Alexandre Julliard92462c22004-07-13 03:43:25 +0000102 printf( "\nResident name table:\n" );
Alexandre Julliard757caa02003-09-27 02:34:54 +0000103 while (*pstr)
104 {
Alexandre Julliard92462c22004-07-13 03:43:25 +0000105 printf( " %4d: %*.*s\n", get_word(pstr + *pstr + 1), *pstr, *pstr, pstr + 1 );
Alexandre Julliard757caa02003-09-27 02:34:54 +0000106 pstr += *pstr + 1 + sizeof(WORD);
107 }
Alexandre Julliard92462c22004-07-13 03:43:25 +0000108 if (ne->ne_cbnrestab)
109 {
110 printf( "\nNon-resident name table:\n" );
Eric Pouechafe309b2006-11-29 21:40:00 +0100111 pstr = PRD(ne->ne_nrestab, 0);
Alexandre Julliard92462c22004-07-13 03:43:25 +0000112 while (*pstr)
113 {
114 printf( " %4d: %*.*s\n", get_word(pstr + *pstr + 1), *pstr, *pstr, pstr + 1 );
115 pstr += *pstr + 1 + sizeof(WORD);
116 }
117 }
Alexandre Julliard757caa02003-09-27 02:34:54 +0000118}
119
Alexandre Julliard16f3c782002-10-02 18:50:09 +0000120static const char *get_resource_type( WORD id )
121{
122 static char buffer[5];
123 switch(id)
124 {
125 case NE_RSCTYPE_CURSOR: return "CURSOR";
126 case NE_RSCTYPE_BITMAP: return "BITMAP";
127 case NE_RSCTYPE_ICON: return "ICON";
128 case NE_RSCTYPE_MENU: return "MENU";
129 case NE_RSCTYPE_DIALOG: return "DIALOG";
130 case NE_RSCTYPE_STRING: return "STRING";
131 case NE_RSCTYPE_FONTDIR: return "FONTDIR";
132 case NE_RSCTYPE_FONT: return "FONT";
133 case NE_RSCTYPE_ACCELERATOR: return "ACCELERATOR";
134 case NE_RSCTYPE_RCDATA: return "RCDATA";
135 case NE_RSCTYPE_GROUP_CURSOR: return "CURSOR_GROUP";
136 case NE_RSCTYPE_GROUP_ICON: return "ICON_GROUP";
137 default:
138 sprintf( buffer, "%04x", id );
139 return buffer;
140 }
141}
142
Eric Pouechafe309b2006-11-29 21:40:00 +0100143static void dump_ne_resources( const IMAGE_OS2_HEADER *ne )
Alexandre Julliard16f3c782002-10-02 18:50:09 +0000144{
Eric Pouech294835a2004-12-06 20:43:55 +0000145 const NE_NAMEINFO *name;
146 const void *res_ptr = (const char *)ne + ne->ne_rsrctab;
Alexandre Julliard92462c22004-07-13 03:43:25 +0000147 WORD size_shift = get_word(res_ptr);
Eric Pouech294835a2004-12-06 20:43:55 +0000148 const NE_TYPEINFO *info = (const NE_TYPEINFO *)((const WORD *)res_ptr + 1);
Alexandre Julliard16f3c782002-10-02 18:50:09 +0000149 int count;
150
151 printf( "\nResources:\n" );
Eric Pouech294835a2004-12-06 20:43:55 +0000152 while (info->type_id != 0 && (const char *)info < (const char *)ne + ne->ne_restab)
Alexandre Julliard16f3c782002-10-02 18:50:09 +0000153 {
Eric Pouech294835a2004-12-06 20:43:55 +0000154 name = (const NE_NAMEINFO *)(info + 1);
Alexandre Julliard16f3c782002-10-02 18:50:09 +0000155 for (count = info->count; count > 0; count--, name++)
156 {
157 if (name->id & 0x8000) printf( " %d", (name->id & ~0x8000) );
Eric Pouech294835a2004-12-06 20:43:55 +0000158 else printf( " %.*s", *((const unsigned char *)res_ptr + name->id),
159 (const char *)res_ptr + name->id + 1 );
Dmitry Timoshkov23001da2005-05-20 09:41:16 +0000160 if (info->type_id & 0x8000) printf( " %s", get_resource_type(info->type_id) );
161 else printf( " %.*s", *((const unsigned char *)res_ptr + info->type_id),
Eric Pouech294835a2004-12-06 20:43:55 +0000162 (const char *)res_ptr + info->type_id + 1 );
Dmitry Timoshkov23001da2005-05-20 09:41:16 +0000163 printf(" flags %04x length %04x\n", name->flags, name->length << size_shift);
Eric Pouechafe309b2006-11-29 21:40:00 +0100164 dump_data( PRD(name->offset << size_shift, name->length << size_shift),
Alexandre Julliard16f3c782002-10-02 18:50:09 +0000165 name->length << size_shift, " " );
166 }
Eric Pouech294835a2004-12-06 20:43:55 +0000167 info = (const NE_TYPEINFO *)name;
Alexandre Julliard16f3c782002-10-02 18:50:09 +0000168 }
169}
170
Eric Pouechafe309b2006-11-29 21:40:00 +0100171static const char *get_export_name( const IMAGE_OS2_HEADER *ne, int ordinal )
Alexandre Julliard92462c22004-07-13 03:43:25 +0000172{
173 static char name[256];
Eric Pouecha6e27ea2005-12-13 11:11:38 +0100174 const BYTE *pstr;
Alexandre Julliard92462c22004-07-13 03:43:25 +0000175 int pass = 0;
176
177 /* search the resident names */
178
179 while (pass < 2)
180 {
181 if (pass == 0) /* resident names */
182 {
Eric Pouecha6e27ea2005-12-13 11:11:38 +0100183 pstr = (const BYTE *)ne + ne->ne_restab;
Alexandre Julliard92462c22004-07-13 03:43:25 +0000184 if (*pstr) pstr += *pstr + 1 + sizeof(WORD); /* skip first entry (module name) */
185 }
186 else /* non-resident names */
187 {
188 if (!ne->ne_cbnrestab) break;
Eric Pouechafe309b2006-11-29 21:40:00 +0100189 pstr = PRD(ne->ne_nrestab, 0);
Alexandre Julliard92462c22004-07-13 03:43:25 +0000190 }
191 while (*pstr)
192 {
193 WORD ord = get_word(pstr + *pstr + 1);
194 if (ord == ordinal)
195 {
196 memcpy( name, pstr + 1, *pstr );
197 name[*pstr] = 0;
198 return name;
199 }
200 pstr += *pstr + 1 + sizeof(WORD);
201 }
202 pass++;
203 }
204 name[0] = 0;
205 return name;
206}
207
Eric Pouechafe309b2006-11-29 21:40:00 +0100208static void dump_ne_exports( const IMAGE_OS2_HEADER *ne )
Alexandre Julliard92462c22004-07-13 03:43:25 +0000209{
Eric Pouecha6e27ea2005-12-13 11:11:38 +0100210 const BYTE *ptr = (const BYTE *)ne + ne->ne_enttab;
211 const BYTE *end = ptr + ne->ne_cbenttab;
Alexandre Julliard92462c22004-07-13 03:43:25 +0000212 int i, ordinal = 1;
213
214 if (!ne->ne_cbenttab || !*ptr) return;
215
216 printf( "\nExported entry points:\n" );
217
218 while (ptr < end && *ptr)
219 {
220 BYTE count = *ptr++;
221 BYTE type = *ptr++;
222 switch(type)
223 {
224 case 0: /* next bundle */
225 ordinal += count;
226 break;
227 case 0xff: /* movable */
228 for (i = 0; i < count; i++)
229 {
230 printf( " %4d MOVABLE %d:%04x %s\n",
231 ordinal + i, ptr[3], get_word(ptr + 4),
Eric Pouechafe309b2006-11-29 21:40:00 +0100232 get_export_name( ne, ordinal + i ) );
Alexandre Julliard92462c22004-07-13 03:43:25 +0000233 ptr += 6;
234 }
235 ordinal += count;
236 break;
237 case 0xfe: /* constant */
238 for (i = 0; i < count; i++)
239 {
240 printf( " %4d CONST %04x %s\n",
241 ordinal + i, get_word(ptr + 1),
Eric Pouechafe309b2006-11-29 21:40:00 +0100242 get_export_name( ne, ordinal + i ) );
Alexandre Julliard92462c22004-07-13 03:43:25 +0000243 ptr += 3;
244 }
245 ordinal += count;
246 break;
247 default: /* fixed */
248 for (i = 0; i < count; i++)
249 {
250 printf( " %4d FIXED %d:%04x %s\n",
251 ordinal + i, type, get_word(ptr + 1),
Eric Pouechafe309b2006-11-29 21:40:00 +0100252 get_export_name( ne, ordinal + i ) );
Alexandre Julliard92462c22004-07-13 03:43:25 +0000253 ptr += 3;
254 }
255 ordinal += count;
256 break;
257 }
258 }
259}
260
Alexandre Julliarddd41c122005-07-01 19:23:39 +0000261static const char *get_reloc_name( BYTE addr_type, int additive )
262{
263 switch(addr_type & 0x7f)
264 {
265 case NE_RADDR_LOWBYTE: return additive ? "byte add" : "byte";
266 case NE_RADDR_OFFSET16: return additive ? "off16 add" : "off16";
267 case NE_RADDR_POINTER32: return additive ? "ptr32 add" : "ptr32";
268 case NE_RADDR_SELECTOR: return additive ? "sel add" : "sel";
269 case NE_RADDR_POINTER48: return additive ? "ptr48 add" : "ptr48";
270 case NE_RADDR_OFFSET32: return additive ? "off32 add" : "off32";
271 }
272 return "???";
273}
274
275static const char *get_seg_flags( WORD flags )
276{
277 static char buffer[256];
278
279 buffer[0] = 0;
280#define ADD_FLAG(x) if (flags & NE_SEGFLAGS_##x) strcat( buffer, " " #x );
281 ADD_FLAG(DATA);
282 ADD_FLAG(ALLOCATED);
283 ADD_FLAG(LOADED);
284 ADD_FLAG(ITERATED);
285 ADD_FLAG(MOVEABLE);
286 ADD_FLAG(SHAREABLE);
287 ADD_FLAG(PRELOAD);
288 ADD_FLAG(EXECUTEONLY);
289 ADD_FLAG(READONLY);
290 ADD_FLAG(RELOC_DATA);
291 ADD_FLAG(SELFLOAD);
292 ADD_FLAG(DISCARDABLE);
293 ADD_FLAG(32BIT);
294#undef ADD_FLAG
295 if (buffer[0])
296 {
297 buffer[0] = '(';
298 strcat( buffer, ")" );
299 }
300 return buffer;
301}
302
Eric Pouechafe309b2006-11-29 21:40:00 +0100303static void dump_relocations( const IMAGE_OS2_HEADER *ne, WORD count,
Alexandre Julliarddd41c122005-07-01 19:23:39 +0000304 const struct relocation_entry *rep )
305{
306 const WORD *modref = (const WORD *)((const BYTE *)ne + ne->ne_modtab);
307 const BYTE *mod_name, *func_name;
308 WORD i;
309
310 for (i = 0; i < count; i++, rep++)
311 {
312 int additive = rep->relocation_type & NE_RELFLAG_ADDITIVE;
313 switch (rep->relocation_type & 3)
314 {
315 case NE_RELTYPE_ORDINAL:
316 mod_name = (const BYTE *)ne + ne->ne_imptab + modref[rep->target1 - 1];
317 printf( "%6d: %s = %*.*s.%d\n", i + 1, get_reloc_name( rep->address_type, additive ),
318 *mod_name, *mod_name, mod_name + 1, rep->target2 );
319 break;
320 case NE_RELTYPE_NAME:
321 mod_name = (const BYTE *)ne + ne->ne_imptab + modref[rep->target1 - 1];
322 func_name = (const BYTE *)ne + ne->ne_imptab + rep->target2;
323 printf( "%6d: %s = %*.*s.%*.*s\n", i + 1, get_reloc_name( rep->address_type, additive ),
324 *mod_name, *mod_name, mod_name + 1,
325 *func_name, *func_name, func_name + 1 );
326 break;
327 case NE_RELTYPE_INTERNAL:
328 if ((rep->target1 & 0xff) == 0xff)
329 {
330 /* the module itself */
331 mod_name = (const BYTE *)ne + ne->ne_restab;
332 printf( "%6d: %s = %*.*s.%d\n", i + 1, get_reloc_name( rep->address_type, additive ),
333 *mod_name, *mod_name, mod_name + 1, rep->target2 );
334 }
335 else
336 printf( "%6d: %s = %d:%04x\n", i + 1, get_reloc_name( rep->address_type, additive ),
337 rep->target1, rep->target2 );
338 break;
339 case NE_RELTYPE_OSFIXUP:
340 /* Relocation type 7:
341 *
342 * These appear to be used as fixups for the Windows
343 * floating point emulator. Let's just ignore them and
344 * try to use the hardware floating point. Linux should
345 * successfully emulate the coprocessor if it doesn't
346 * exist.
347 */
348 printf( "%6d: %s = TYPE %d, OFFSET %04x, TARGET %04x %04x\n",
349 i + 1, get_reloc_name( rep->address_type, additive ),
350 rep->relocation_type, rep->offset,
351 rep->target1, rep->target2 );
352 break;
353 }
354 }
355}
356
Eric Pouechafe309b2006-11-29 21:40:00 +0100357static void dump_ne_segment( const IMAGE_OS2_HEADER *ne, int segnum )
Alexandre Julliarddd41c122005-07-01 19:23:39 +0000358{
359 const struct ne_segtable_entry *table = (const struct ne_segtable_entry *)((const BYTE *)ne + ne->ne_segtab);
360 const struct ne_segtable_entry *seg = table + segnum - 1;
361
362 printf( "\nSegment %d:\n", segnum );
363 printf( " File offset: %08x\n", seg->seg_data_offset << ne->ne_align );
364 printf( " Length: %08x\n", seg->seg_data_length );
365 printf( " Flags: %08x %s\n", seg->seg_flags, get_seg_flags(seg->seg_flags) );
366 printf( " Alloc size: %08x\n", seg->min_alloc );
367 if (seg->seg_flags & NE_SEGFLAGS_RELOC_DATA)
368 {
Eric Pouechafe309b2006-11-29 21:40:00 +0100369 const BYTE *ptr = PRD((seg->seg_data_offset << ne->ne_align) + seg->seg_data_length, 0);
Alexandre Julliarddd41c122005-07-01 19:23:39 +0000370 WORD count = get_word(ptr);
371 ptr += sizeof(WORD);
372 printf( " Relocations:\n" );
Eric Pouechafe309b2006-11-29 21:40:00 +0100373 dump_relocations( ne, count, (const struct relocation_entry *)ptr );
Alexandre Julliarddd41c122005-07-01 19:23:39 +0000374 }
375}
376
Eric Pouechafe309b2006-11-29 21:40:00 +0100377void ne_dump( void )
Alexandre Julliard16f3c782002-10-02 18:50:09 +0000378{
Alexandre Julliarddd41c122005-07-01 19:23:39 +0000379 unsigned int i;
Eric Pouechafe309b2006-11-29 21:40:00 +0100380 const IMAGE_DOS_HEADER *dos;
381 const IMAGE_OS2_HEADER *ne;
382
383 dos = PRD(0, sizeof(*dos));
384 if (!dos) return;
385 ne = PRD(dos->e_lfanew, sizeof(*ne));
Alexandre Julliard16f3c782002-10-02 18:50:09 +0000386
Eric Pouech1fcb0c12007-01-05 21:42:26 +0100387 if (globals.do_dumpheader || !globals.dumpsect)
388 dump_ne_header( ne );
389 if (globals.do_dumpheader)
390 dump_ne_names( ne );
391 if (globals.dumpsect)
392 {
393 BOOL all = strcmp(globals.dumpsect, "ALL") == 0;
394
395 if (all || !strcmp(globals.dumpsect, "resource"))
396 dump_ne_resources( ne );
397 if (all || !strcmp(globals.dumpsect, "export"))
398 dump_ne_exports( ne );
399 }
400 if (globals.do_dumpheader)
401 for (i = 1; i <= ne->ne_cseg; i++) dump_ne_segment( ne, i );
Alexandre Julliard16f3c782002-10-02 18:50:09 +0000402}