spec16.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. /*
  2. * 16-bit spec files
  3. *
  4. * Copyright 1993 Robert J. Amstadt
  5. * Copyright 1995 Martin von Loewis
  6. * Copyright 1995, 1996, 1997 Alexandre Julliard
  7. * Copyright 1997 Eric Youngdale
  8. * Copyright 1999 Ulrich Weigand
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include "config.h"
  25. #include "wine/port.h"
  26. #include <assert.h>
  27. #include <ctype.h>
  28. #include "build.h"
  29. #define NE_FFLAGS_SINGLEDATA 0x0001
  30. #define NE_FFLAGS_LIBMODULE 0x8000
  31. /* argument type flags for relay debugging */
  32. enum arg_types
  33. {
  34. ARG16_NONE = 0, /* indicates end of arg list */
  35. ARG16_WORD, /* unsigned word */
  36. ARG16_SWORD, /* signed word */
  37. ARG16_LONG, /* long or segmented pointer */
  38. ARG16_PTR, /* linear pointer */
  39. ARG16_STR, /* linear pointer to null-terminated string */
  40. ARG16_SEGSTR, /* segmented pointer to null-terminated string */
  41. ARG16_VARARG /* start of varargs */
  42. };
  43. /* sequences of nops to fill a certain number of words */
  44. static const char * const nop_sequence[4] =
  45. {
  46. ".byte 0x89,0xf6", /* mov %esi,%esi */
  47. ".byte 0x8d,0x74,0x26,0x00", /* lea 0x00(%esi),%esi */
  48. ".byte 0x8d,0xb6,0x00,0x00,0x00,0x00", /* lea 0x00000000(%esi),%esi */
  49. ".byte 0x8d,0x74,0x26,0x00,0x8d,0x74,0x26,0x00" /* lea 0x00(%esi),%esi; lea 0x00(%esi),%esi */
  50. };
  51. static const char fakedll_signature[] = "Wine placeholder DLL";
  52. static inline int is_function( const ORDDEF *odp )
  53. {
  54. if (odp->flags & FLAG_EXPORT32) return 0;
  55. return (odp->type == TYPE_CDECL ||
  56. odp->type == TYPE_PASCAL ||
  57. odp->type == TYPE_VARARGS ||
  58. odp->type == TYPE_STUB);
  59. }
  60. static const char *get_args_str( const ORDDEF *odp )
  61. {
  62. static char buffer[MAX_ARGUMENTS*2+1];
  63. int i;
  64. buffer[0] = 0;
  65. for (i = 0; i < odp->u.func.nb_args; i++)
  66. {
  67. switch (odp->u.func.args[i])
  68. {
  69. case ARG_WORD: strcat( buffer, "w" ); break;
  70. case ARG_SWORD: strcat( buffer, "s" ); break;
  71. case ARG_SEGSTR: strcat( buffer, "T" ); break;
  72. case ARG_STR: strcat( buffer, "t" ); break;
  73. case ARG_LONG:
  74. case ARG_FLOAT:
  75. case ARG_SEGPTR: strcat( buffer, "l" ); break;
  76. case ARG_PTR:
  77. case ARG_WSTR:
  78. case ARG_INT128: strcat( buffer, "p" ); break;
  79. case ARG_INT64:
  80. case ARG_DOUBLE: strcat( buffer, "ll" ); break;
  81. }
  82. }
  83. return buffer;
  84. }
  85. /*******************************************************************
  86. * output_entries
  87. *
  88. * Output entries for individual symbols in the entry table.
  89. */
  90. static void output_entries( DLLSPEC *spec, int first, int count )
  91. {
  92. int i;
  93. for (i = 0; i < count; i++)
  94. {
  95. ORDDEF *odp = spec->ordinals[first + i];
  96. output( "\t.byte 0x03\n" ); /* flags: exported & public data */
  97. switch (odp->type)
  98. {
  99. case TYPE_CDECL:
  100. case TYPE_PASCAL:
  101. case TYPE_VARARGS:
  102. case TYPE_STUB:
  103. output( "\t.short .L__wine_%s_%u-.L__wine_spec_code_segment\n", spec->c_name, first + i );
  104. break;
  105. case TYPE_VARIABLE:
  106. output( "\t.short .L__wine_%s_%u-.L__wine_spec_data_segment\n", spec->c_name, first + i );
  107. break;
  108. case TYPE_ABS:
  109. output( "\t.short 0x%04x /* %s */\n",
  110. odp->u.abs.value, odp->name );
  111. break;
  112. default:
  113. assert(0);
  114. }
  115. }
  116. }
  117. /*******************************************************************
  118. * output_entry_table
  119. */
  120. static void output_entry_table( DLLSPEC *spec )
  121. {
  122. int i, prev = 0, prev_sel = -1, bundle_count = 0;
  123. for (i = 1; i <= spec->limit; i++)
  124. {
  125. int selector = 0;
  126. ORDDEF *odp = spec->ordinals[i];
  127. if (!odp) continue;
  128. if (odp->flags & FLAG_EXPORT32) continue;
  129. switch (odp->type)
  130. {
  131. case TYPE_CDECL:
  132. case TYPE_PASCAL:
  133. case TYPE_VARARGS:
  134. case TYPE_STUB:
  135. selector = 1; /* Code selector */
  136. break;
  137. case TYPE_VARIABLE:
  138. selector = 2; /* Data selector */
  139. break;
  140. case TYPE_ABS:
  141. selector = 0xfe; /* Constant selector */
  142. break;
  143. default:
  144. continue;
  145. }
  146. if (prev + 1 != i || prev_sel != selector || bundle_count == 255)
  147. {
  148. /* need to start a new bundle */
  149. /* flush previous bundle */
  150. if (bundle_count)
  151. {
  152. output( "\t/* %s.%d - %s.%d */\n",
  153. spec->dll_name, prev - bundle_count + 1, spec->dll_name, prev );
  154. output( "\t.byte 0x%02x,0x%02x\n", bundle_count, prev_sel );
  155. output_entries( spec, prev - bundle_count + 1, bundle_count );
  156. }
  157. if (prev + 1 != i)
  158. {
  159. int skip = i - (prev + 1);
  160. while (skip > 255)
  161. {
  162. output( "\t.byte 0xff,0x00\n" );
  163. skip -= 255;
  164. }
  165. output( "\t.byte 0x%02x,0x00\n", skip );
  166. }
  167. bundle_count = 0;
  168. prev_sel = selector;
  169. }
  170. bundle_count++;
  171. prev = i;
  172. }
  173. /* flush last bundle */
  174. if (bundle_count)
  175. {
  176. output( "\t.byte 0x%02x,0x%02x\n", bundle_count, prev_sel );
  177. output_entries( spec, prev - bundle_count + 1, bundle_count );
  178. }
  179. output( "\t.byte 0x00\n" );
  180. }
  181. /*******************************************************************
  182. * output_resident_name
  183. */
  184. static void output_resident_name( const char *string, int ordinal )
  185. {
  186. unsigned int i, len = strlen(string);
  187. output( "\t.byte 0x%02x", len );
  188. for (i = 0; i < len; i++) output( ",0x%02x", (unsigned char)toupper(string[i]) );
  189. output( " /* %s */\n", string );
  190. output( "\t.short %u\n", ordinal );
  191. }
  192. /*******************************************************************
  193. * get_callfrom16_name
  194. */
  195. static const char *get_callfrom16_name( const ORDDEF *odp )
  196. {
  197. static char *buffer;
  198. free( buffer );
  199. buffer = strmake( "%s_%s_%s",
  200. (odp->type == TYPE_PASCAL) ? "p" :
  201. (odp->type == TYPE_VARARGS) ? "v" : "c",
  202. (odp->flags & FLAG_REGISTER) ? "regs" :
  203. (odp->flags & FLAG_RET16) ? "word" : "long",
  204. get_args_str(odp) );
  205. return buffer;
  206. }
  207. /*******************************************************************
  208. * get_relay_name
  209. */
  210. static const char *get_relay_name( const ORDDEF *odp )
  211. {
  212. static char buffer[80];
  213. char *p;
  214. switch(odp->type)
  215. {
  216. case TYPE_PASCAL:
  217. strcpy( buffer, "p_" );
  218. break;
  219. case TYPE_VARARGS:
  220. strcpy( buffer, "v_" );
  221. break;
  222. case TYPE_CDECL:
  223. case TYPE_STUB:
  224. strcpy( buffer, "c_" );
  225. break;
  226. default:
  227. assert(0);
  228. }
  229. strcat( buffer, get_args_str(odp) );
  230. for (p = buffer + 2; *p; p++)
  231. {
  232. /* map string types to the corresponding plain pointer type */
  233. if (*p == 't') *p = 'p';
  234. else if (*p == 'T') *p = 'l';
  235. }
  236. if (odp->flags & FLAG_REGISTER) strcat( buffer, "_regs" );
  237. return buffer;
  238. }
  239. /*******************************************************************
  240. * get_function_argsize
  241. */
  242. static int get_function_argsize( const ORDDEF *odp )
  243. {
  244. int i, argsize = 0;
  245. for (i = 0; i < odp->u.func.nb_args; i++)
  246. {
  247. switch (odp->u.func.args[i])
  248. {
  249. case ARG_WORD:
  250. case ARG_SWORD:
  251. argsize += 2;
  252. break;
  253. case ARG_SEGPTR:
  254. case ARG_SEGSTR:
  255. case ARG_LONG:
  256. case ARG_PTR:
  257. case ARG_STR:
  258. case ARG_WSTR:
  259. case ARG_FLOAT:
  260. case ARG_INT128:
  261. argsize += 4;
  262. break;
  263. case ARG_INT64:
  264. case ARG_DOUBLE:
  265. argsize += 8;
  266. break;
  267. }
  268. }
  269. return argsize;
  270. }
  271. /*******************************************************************
  272. * output_call16_function
  273. *
  274. * Build a 16-bit-to-Wine callback glue function.
  275. *
  276. * The generated routines are intended to be used as argument conversion
  277. * routines to be called by the CallFrom16... core. Thus, the prototypes of
  278. * the generated routines are (see also CallFrom16):
  279. *
  280. * extern WORD WINAPI __wine_spec_call16_C_xxx( FARPROC func, LPBYTE args );
  281. * extern LONG WINAPI __wine_spec_call16_C_xxx( FARPROC func, LPBYTE args );
  282. * extern void WINAPI __wine_spec_call16_C_xxx_regs( FARPROC func, LPBYTE args, CONTEXT86 *context );
  283. *
  284. * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
  285. * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
  286. * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
  287. * 'T'=segmented pointer to null-terminated string).
  288. *
  289. * The generated routines fetch the arguments from the 16-bit stack (pointed
  290. * to by 'args'); the offsets of the single argument values are computed
  291. * according to the calling convention and the argument types. Then, the
  292. * 32-bit entry point is called with these arguments.
  293. *
  294. * For register functions, the arguments (if present) are converted just
  295. * the same as for normal functions, but in addition the CONTEXT86 pointer
  296. * filled with the current register values is passed to the 32-bit routine.
  297. */
  298. static void output_call16_function( ORDDEF *odp )
  299. {
  300. char *name;
  301. int i, pos, stack_words;
  302. int argsize = get_function_argsize( odp );
  303. int needs_ldt = (strpbrk( get_args_str( odp ), "pt" ) != NULL);
  304. name = strmake( ".L__wine_spec_call16_%s", get_relay_name(odp) );
  305. output( "\t.align %d\n", get_alignment(4) );
  306. output( "\t%s\n", func_declaration(name) );
  307. output( "%s:\n", name );
  308. output_cfi( ".cfi_startproc" );
  309. output( "\tpushl %%ebp\n" );
  310. output_cfi( ".cfi_adjust_cfa_offset 4" );
  311. output_cfi( ".cfi_rel_offset %%ebp,0" );
  312. output( "\tmovl %%esp,%%ebp\n" );
  313. output_cfi( ".cfi_def_cfa_register %%ebp" );
  314. stack_words = 2;
  315. if (needs_ldt)
  316. {
  317. output( "\tpushl %%esi\n" );
  318. output_cfi( ".cfi_rel_offset %%esi,-4" );
  319. stack_words++;
  320. if (UsePIC)
  321. {
  322. output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
  323. output( "1:\tmovl .Lwine_ldt_copy_ptr-1b(%%eax),%%esi\n" );
  324. needs_get_pc_thunk = 1;
  325. }
  326. else
  327. output( "\tmovl .Lwine_ldt_copy_ptr,%%esi\n" );
  328. }
  329. /* preserve 16-byte stack alignment */
  330. stack_words += odp->u.func.nb_args;
  331. for (i = 0; i < odp->u.func.nb_args; i++)
  332. if (odp->u.func.args[i] == ARG_DOUBLE || odp->u.func.args[i] == ARG_INT64) stack_words++;
  333. if ((odp->flags & FLAG_REGISTER) || (odp->type == TYPE_VARARGS)) stack_words++;
  334. if (stack_words % 4) output( "\tsubl $%d,%%esp\n", 16 - 4 * (stack_words % 4) );
  335. if (odp->u.func.nb_args || odp->type == TYPE_VARARGS)
  336. output( "\tmovl 12(%%ebp),%%ecx\n" ); /* args */
  337. if (odp->flags & FLAG_REGISTER)
  338. {
  339. output( "\tpushl 16(%%ebp)\n" ); /* context */
  340. }
  341. else if (odp->type == TYPE_VARARGS)
  342. {
  343. output( "\tleal %d(%%ecx),%%eax\n", argsize );
  344. output( "\tpushl %%eax\n" ); /* va_list16 */
  345. }
  346. pos = (odp->type == TYPE_PASCAL) ? 0 : argsize;
  347. for (i = odp->u.func.nb_args - 1; i >= 0; i--)
  348. {
  349. switch (odp->u.func.args[i])
  350. {
  351. case ARG_WORD:
  352. if (odp->type != TYPE_PASCAL) pos -= 2;
  353. output( "\tmovzwl %d(%%ecx),%%eax\n", pos );
  354. output( "\tpushl %%eax\n" );
  355. if (odp->type == TYPE_PASCAL) pos += 2;
  356. break;
  357. case ARG_SWORD:
  358. if (odp->type != TYPE_PASCAL) pos -= 2;
  359. output( "\tmovswl %d(%%ecx),%%eax\n", pos );
  360. output( "\tpushl %%eax\n" );
  361. if (odp->type == TYPE_PASCAL) pos += 2;
  362. break;
  363. case ARG_INT64:
  364. case ARG_DOUBLE:
  365. if (odp->type != TYPE_PASCAL) pos -= 4;
  366. output( "\tpushl %d(%%ecx)\n", pos );
  367. if (odp->type == TYPE_PASCAL) pos += 4;
  368. /* fall through */
  369. case ARG_LONG:
  370. case ARG_FLOAT:
  371. case ARG_SEGPTR:
  372. case ARG_SEGSTR:
  373. if (odp->type != TYPE_PASCAL) pos -= 4;
  374. output( "\tpushl %d(%%ecx)\n", pos );
  375. if (odp->type == TYPE_PASCAL) pos += 4;
  376. break;
  377. case ARG_PTR:
  378. case ARG_STR:
  379. case ARG_WSTR:
  380. case ARG_INT128:
  381. if (odp->type != TYPE_PASCAL) pos -= 4;
  382. output( "\tmovzwl %d(%%ecx),%%edx\n", pos + 2 ); /* sel */
  383. output( "\tshr $3,%%edx\n" );
  384. output( "\tmovzwl %d(%%ecx),%%eax\n", pos ); /* offset */
  385. output( "\taddl (%%esi,%%edx,4),%%eax\n" );
  386. output( "\tpushl %%eax\n" );
  387. if (odp->type == TYPE_PASCAL) pos += 4;
  388. break;
  389. }
  390. }
  391. output( "\tcall *8(%%ebp)\n" );
  392. if (needs_ldt)
  393. {
  394. output( "\tmovl -4(%%ebp),%%esi\n" );
  395. output_cfi( ".cfi_same_value %%esi" );
  396. }
  397. output( "\tleave\n" );
  398. output_cfi( ".cfi_def_cfa %%esp,4" );
  399. output_cfi( ".cfi_same_value %%ebp" );
  400. output( "\tret\n" );
  401. output_cfi( ".cfi_endproc" );
  402. output_function_size( name );
  403. free( name );
  404. }
  405. /*******************************************************************
  406. * callfrom16_type_compare
  407. *
  408. * Compare two callfrom16 sequences.
  409. */
  410. static int callfrom16_type_compare( const void *e1, const void *e2 )
  411. {
  412. const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
  413. const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
  414. int retval;
  415. int type1 = odp1->type;
  416. int type2 = odp2->type;
  417. char args1[80];
  418. if (type1 == TYPE_STUB) type1 = TYPE_CDECL;
  419. if (type2 == TYPE_STUB) type2 = TYPE_CDECL;
  420. if ((retval = type1 - type2) != 0) return retval;
  421. type1 = odp1->flags & (FLAG_RET16|FLAG_REGISTER);
  422. type2 = odp2->flags & (FLAG_RET16|FLAG_REGISTER);
  423. if ((retval = type1 - type2) != 0) return retval;
  424. strcpy( args1, get_args_str( odp1 ));
  425. return strcmp( args1, get_args_str( odp2 ));
  426. }
  427. /*******************************************************************
  428. * relay_type_compare
  429. *
  430. * Same as callfrom16_type_compare but ignores differences that don't affect the resulting relay function.
  431. */
  432. static int relay_type_compare( const void *e1, const void *e2 )
  433. {
  434. const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
  435. const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
  436. char name1[80];
  437. strcpy( name1, get_relay_name(odp1) );
  438. return strcmp( name1, get_relay_name(odp2) );
  439. }
  440. /*******************************************************************
  441. * output_module16
  442. *
  443. * Output code for a 16-bit module.
  444. */
  445. static void output_module16( DLLSPEC *spec )
  446. {
  447. ORDDEF **typelist;
  448. ORDDEF *entry_point = NULL;
  449. int i, j, nb_funcs;
  450. /* store the main entry point as ordinal 0 */
  451. if (!spec->ordinals)
  452. {
  453. assert(spec->limit == 0);
  454. spec->ordinals = xmalloc( sizeof(spec->ordinals[0]) );
  455. spec->ordinals[0] = NULL;
  456. }
  457. if (spec->init_func && !(spec->characteristics & IMAGE_FILE_DLL))
  458. {
  459. entry_point = xmalloc( sizeof(*entry_point) );
  460. entry_point->type = TYPE_PASCAL;
  461. entry_point->ordinal = 0;
  462. entry_point->lineno = 0;
  463. entry_point->flags = FLAG_REGISTER;
  464. entry_point->name = NULL;
  465. entry_point->link_name = xstrdup( spec->init_func );
  466. entry_point->export_name = NULL;
  467. entry_point->u.func.nb_args = 0;
  468. assert( !spec->ordinals[0] );
  469. spec->ordinals[0] = entry_point;
  470. }
  471. /* Build sorted list of all argument types, without duplicates */
  472. typelist = xmalloc( (spec->limit + 1) * sizeof(*typelist) );
  473. for (i = nb_funcs = 0; i <= spec->limit; i++)
  474. {
  475. ORDDEF *odp = spec->ordinals[i];
  476. if (!odp) continue;
  477. if (is_function( odp )) typelist[nb_funcs++] = odp;
  478. }
  479. nb_funcs = sort_func_list( typelist, nb_funcs, callfrom16_type_compare );
  480. /* Output the module structure */
  481. output( "\n/* module data */\n\n" );
  482. output( "\t.data\n" );
  483. output( "\t.align %d\n", get_alignment(16) );
  484. output( ".L__wine_spec_dos_header:\n" );
  485. output( "\t.short 0x5a4d\n" ); /* e_magic */
  486. output( "\t.short 0\n" ); /* e_cblp */
  487. output( "\t.short 0\n" ); /* e_cp */
  488. output( "\t.short 0\n" ); /* e_crlc */
  489. output( "\t.short 0\n" ); /* e_cparhdr */
  490. output( "\t.short 0\n" ); /* e_minalloc */
  491. output( "\t.short 0\n" ); /* e_maxalloc */
  492. output( "\t.short 0\n" ); /* e_ss */
  493. output( "\t.short 0\n" ); /* e_sp */
  494. output( "\t.short 0\n" ); /* e_csum */
  495. output( "\t.short 0\n" ); /* e_ip */
  496. output( "\t.short 0\n" ); /* e_cs */
  497. output( "\t.short 0\n" ); /* e_lfarlc */
  498. output( "\t.short 0\n" ); /* e_ovno */
  499. output( "\t.short 0,0,0,0\n" ); /* e_res */
  500. output( "\t.short 0\n" ); /* e_oemid */
  501. output( "\t.short 0\n" ); /* e_oeminfo */
  502. output( ".Lwine_ldt_copy_ptr:\n" ); /* e_res2, used for private data */
  503. output( "\t.long .L__wine_spec_ne_header_end-.L__wine_spec_dos_header,0,0,0,0\n" );
  504. output( "\t.long .L__wine_spec_ne_header-.L__wine_spec_dos_header\n" );/* e_lfanew */
  505. output( "\t%s \"%s\"\n", get_asm_string_keyword(), fakedll_signature );
  506. output( "\t.align %d\n", get_alignment(16) );
  507. output( ".L__wine_spec_ne_header:\n" );
  508. output( "\t.short 0x454e\n" ); /* ne_magic */
  509. output( "\t.byte 0\n" ); /* ne_ver */
  510. output( "\t.byte 0\n" ); /* ne_rev */
  511. output( "\t.short .L__wine_spec_ne_enttab-.L__wine_spec_ne_header\n" );/* ne_enttab */
  512. output( "\t.short .L__wine_spec_ne_enttab_end-.L__wine_spec_ne_enttab\n" );/* ne_cbenttab */
  513. output( "\t.long 0\n" ); /* ne_crc */
  514. output( "\t.short 0x%04x\n", NE_FFLAGS_SINGLEDATA | /* ne_flags */
  515. ((spec->characteristics & IMAGE_FILE_DLL) ? NE_FFLAGS_LIBMODULE : 0) );
  516. output( "\t.short 2\n" ); /* ne_autodata */
  517. output( "\t.short %u\n", spec->heap_size ); /* ne_heap */
  518. output( "\t.short 0\n" ); /* ne_stack */
  519. if (!entry_point) output( "\t.long 0\n" ); /* ne_csip */
  520. else output( "\t.short .L__wine_%s_0-.L__wine_spec_code_segment,1\n", spec->c_name );
  521. output( "\t.short 0,2\n" ); /* ne_sssp */
  522. output( "\t.short 2\n" ); /* ne_cseg */
  523. output( "\t.short 0\n" ); /* ne_cmod */
  524. output( "\t.short 0\n" ); /* ne_cbnrestab */
  525. output( "\t.short .L__wine_spec_ne_segtab-.L__wine_spec_ne_header\n" );/* ne_segtab */
  526. output( "\t.short .L__wine_spec_ne_rsrctab-.L__wine_spec_ne_header\n" ); /* ne_rsrctab */
  527. output( "\t.short .L__wine_spec_ne_restab-.L__wine_spec_ne_header\n" ); /* ne_restab */
  528. output( "\t.short .L__wine_spec_ne_modtab-.L__wine_spec_ne_header\n" ); /* ne_modtab */
  529. output( "\t.short .L__wine_spec_ne_imptab-.L__wine_spec_ne_header\n" ); /* ne_imptab */
  530. output( "\t.long 0\n" ); /* ne_nrestab */
  531. output( "\t.short 0\n" ); /* ne_cmovent */
  532. output( "\t.short 0\n" ); /* ne_align */
  533. output( "\t.short 0\n" ); /* ne_cres */
  534. output( "\t.byte 0x02\n" ); /* ne_exetyp = NE_OSFLAGS_WINDOWS */
  535. output( "\t.byte 0x08\n" ); /* ne_flagsothers = NE_AFLAGS_FASTLOAD */
  536. output( "\t.short 0\n" ); /* ne_pretthunks */
  537. output( "\t.short 0\n" ); /* ne_psegrefbytes */
  538. output( "\t.short 0\n" ); /* ne_swaparea */
  539. output( "\t.short 0\n" ); /* ne_expver */
  540. /* segment table */
  541. output( "\n.L__wine_spec_ne_segtab:\n" );
  542. /* code segment entry */
  543. output( "\t.short .L__wine_spec_code_segment-.L__wine_spec_dos_header\n" ); /* filepos */
  544. output( "\t.short .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n" ); /* size */
  545. output( "\t.short 0x2000\n" ); /* flags = NE_SEGFLAGS_32BIT */
  546. output( "\t.short .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n" ); /* minsize */
  547. /* data segment entry */
  548. output( "\t.short .L__wine_spec_data_segment-.L__wine_spec_dos_header\n" ); /* filepos */
  549. output( "\t.short .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n" ); /* size */
  550. output( "\t.short 0x0001\n" ); /* flags = NE_SEGFLAGS_DATA */
  551. output( "\t.short .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n" ); /* minsize */
  552. /* resource directory */
  553. output_res16_directory( spec );
  554. /* resident names table */
  555. output( "\n\t.align %d\n", get_alignment(2) );
  556. output( ".L__wine_spec_ne_restab:\n" );
  557. output_resident_name( spec->dll_name, 0 );
  558. for (i = 1; i <= spec->limit; i++)
  559. {
  560. ORDDEF *odp = spec->ordinals[i];
  561. if (!odp || !odp->name[0]) continue;
  562. if (odp->flags & FLAG_EXPORT32) continue;
  563. output_resident_name( odp->name, i );
  564. }
  565. output( "\t.byte 0\n" );
  566. /* imported names table */
  567. output( "\n\t.align %d\n", get_alignment(2) );
  568. output( ".L__wine_spec_ne_modtab:\n" );
  569. output( ".L__wine_spec_ne_imptab:\n" );
  570. output( "\t.byte 0,0\n" );
  571. /* entry table */
  572. output( "\n.L__wine_spec_ne_enttab:\n" );
  573. output_entry_table( spec );
  574. output( ".L__wine_spec_ne_enttab_end:\n" );
  575. /* code segment */
  576. output( "\n\t.align %d\n", get_alignment(2) );
  577. output( ".L__wine_spec_code_segment:\n" );
  578. for ( i = 0; i < nb_funcs; i++ )
  579. {
  580. unsigned int arg_types[2];
  581. int nop_words, pos, argsize = 0;
  582. if ( typelist[i]->type == TYPE_PASCAL )
  583. argsize = get_function_argsize( typelist[i] );
  584. /* build the arg types bit fields */
  585. arg_types[0] = arg_types[1] = 0;
  586. for (j = pos = 0; j < typelist[i]->u.func.nb_args && pos < 20; j++, pos++)
  587. {
  588. int type = 0;
  589. switch (typelist[i]->u.func.args[j])
  590. {
  591. case ARG_WORD: type = ARG16_WORD; break;
  592. case ARG_SWORD: type = ARG16_SWORD; break;
  593. case ARG_SEGPTR: type = ARG16_LONG; break;
  594. case ARG_SEGSTR: type = ARG16_SEGSTR; break;
  595. case ARG_LONG: type = ARG16_LONG; break;
  596. case ARG_PTR: type = ARG16_PTR; break;
  597. case ARG_STR: type = ARG16_STR; break;
  598. case ARG_WSTR: type = ARG16_PTR; break;
  599. case ARG_FLOAT: type = ARG16_LONG; break;
  600. case ARG_INT128: type = ARG16_PTR; break;
  601. case ARG_INT64:
  602. case ARG_DOUBLE:
  603. type = ARG16_LONG;
  604. arg_types[pos / 10] |= type << (3 * (pos % 10));
  605. pos++;
  606. break;
  607. }
  608. if (pos < 20) arg_types[pos / 10] |= type << (3 * (pos % 10));
  609. }
  610. if (typelist[i]->type == TYPE_VARARGS && pos < 20)
  611. arg_types[pos / 10] |= ARG16_VARARG << (3 * (pos % 10));
  612. output( ".L__wine_spec_callfrom16_%s:\n", get_callfrom16_name(typelist[i]) );
  613. output( "\tpushl $.L__wine_spec_call16_%s\n", get_relay_name(typelist[i]) );
  614. output( "\tlcall $0,$0\n" );
  615. if (typelist[i]->flags & FLAG_REGISTER)
  616. {
  617. nop_words = 4;
  618. }
  619. else if (typelist[i]->flags & FLAG_RET16)
  620. {
  621. output( "\torw %%ax,%%ax\n" );
  622. output( "\tnop\n" ); /* so that the lretw is aligned */
  623. nop_words = 2;
  624. }
  625. else
  626. {
  627. output( "\tshld $16,%%eax,%%edx\n" );
  628. output( "\torl %%eax,%%eax\n" );
  629. nop_words = 1;
  630. }
  631. if (argsize)
  632. {
  633. output( "\tlretw $%u\n", argsize );
  634. nop_words--;
  635. }
  636. else output( "\tlretw\n" );
  637. if (nop_words) output( "\t%s\n", nop_sequence[nop_words-1] );
  638. /* the movl is here so that the code contains only valid instructions, */
  639. /* it's never actually executed, we only care about the arg_types[] values */
  640. output( "\t.short 0x86c7\n" );
  641. output( "\t.long 0x%08x,0x%08x\n", arg_types[0], arg_types[1] );
  642. }
  643. for (i = 0; i <= spec->limit; i++)
  644. {
  645. ORDDEF *odp = spec->ordinals[i];
  646. if (!odp || !is_function( odp )) continue;
  647. output( ".L__wine_%s_%u:\n", spec->c_name, i );
  648. output( "\tpushw %%bp\n" );
  649. output( "\tpushl $%s\n",
  650. asm_name( odp->type == TYPE_STUB ? get_stub_name( odp, spec ) : get_link_name( odp )));
  651. output( "\tcallw .L__wine_spec_callfrom16_%s\n", get_callfrom16_name( odp ) );
  652. }
  653. output( ".L__wine_spec_code_segment_end:\n" );
  654. /* data segment */
  655. output( "\n.L__wine_spec_data_segment:\n" );
  656. output( "\t.byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n" ); /* instance data */
  657. for (i = 0; i <= spec->limit; i++)
  658. {
  659. ORDDEF *odp = spec->ordinals[i];
  660. if (!odp || odp->type != TYPE_VARIABLE) continue;
  661. output( ".L__wine_%s_%u:\n", spec->c_name, i );
  662. output( "\t.long " );
  663. for (j = 0; j < odp->u.var.n_values-1; j++)
  664. output( "0x%08x,", odp->u.var.values[j] );
  665. output( "0x%08x\n", odp->u.var.values[j] );
  666. }
  667. output( ".L__wine_spec_data_segment_end:\n" );
  668. /* resource data */
  669. if (spec->nb_resources)
  670. {
  671. output( "\n.L__wine_spec_resource_data:\n" );
  672. output_res16_data( spec );
  673. }
  674. output( ".L__wine_spec_ne_header_end:\n" );
  675. output( "\t.byte 0\n" ); /* make sure the last symbol points to something */
  676. /* relay functions */
  677. nb_funcs = sort_func_list( typelist, nb_funcs, relay_type_compare );
  678. if (nb_funcs)
  679. {
  680. output( "\n/* relay functions */\n\n" );
  681. output( "\t.text\n" );
  682. for ( i = 0; i < nb_funcs; i++ ) output_call16_function( typelist[i] );
  683. }
  684. free( typelist );
  685. }
  686. /*******************************************************************
  687. * output_spec16_file
  688. *
  689. * Output the complete data for a spec 16-bit file.
  690. */
  691. void output_spec16_file( DLLSPEC *spec16 )
  692. {
  693. DLLSPEC *spec32 = alloc_dll_spec();
  694. add_16bit_exports( spec32, spec16 );
  695. needs_get_pc_thunk = 0;
  696. open_output_file();
  697. output_standard_file_header();
  698. output_module( spec32 );
  699. output_module16( spec16 );
  700. output_stubs( spec16 );
  701. output_exports( spec32 );
  702. output_imports( spec16 );
  703. if (!strcmp( spec16->dll_name, "kernel" )) output_asm_relays16();
  704. if (needs_get_pc_thunk) output_get_pc_thunk();
  705. if (spec16->main_module)
  706. {
  707. output( "\n\t%s\n", get_asm_string_section() );
  708. output( ".L__wine_spec_main_module:\n" );
  709. output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec16->main_module );
  710. }
  711. output_gnu_stack_note();
  712. close_output_file();
  713. free_dll_spec( spec32 );
  714. }
  715. /*******************************************************************
  716. * output_fake_module16
  717. *
  718. * Create a fake 16-bit binary module.
  719. */
  720. void output_fake_module16( DLLSPEC *spec )
  721. {
  722. static const unsigned char code_segment[] = { 0x90, 0xc3 };
  723. static const unsigned char data_segment[16] = { 0 };
  724. const unsigned int cseg = 2;
  725. const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
  726. const unsigned int segtab = lfanew + 0x40;
  727. unsigned int i, rsrctab, restab, namelen, modtab, imptab, enttab, cbenttab, codeseg, dataseg, rsrcdata;
  728. init_output_buffer();
  729. rsrctab = lfanew;
  730. restab = segtab + 8 * cseg;
  731. if (spec->nb_resources)
  732. {
  733. output_bin_res16_directory( spec, 0 );
  734. align_output( 2 );
  735. rsrctab = restab;
  736. restab += output_buffer_pos;
  737. free( output_buffer );
  738. init_output_buffer();
  739. }
  740. namelen = strlen( spec->dll_name );
  741. modtab = restab + ((namelen + 3) & ~1);
  742. imptab = modtab;
  743. enttab = modtab + 2;
  744. cbenttab = 1;
  745. codeseg = (enttab + cbenttab + 1) & ~1;
  746. dataseg = codeseg + sizeof(code_segment);
  747. rsrcdata = dataseg + sizeof(data_segment);
  748. init_output_buffer();
  749. put_word( 0x5a4d ); /* e_magic */
  750. put_word( 0x40 ); /* e_cblp */
  751. put_word( 0x01 ); /* e_cp */
  752. put_word( 0 ); /* e_crlc */
  753. put_word( lfanew / 16 ); /* e_cparhdr */
  754. put_word( 0x0000 ); /* e_minalloc */
  755. put_word( 0xffff ); /* e_maxalloc */
  756. put_word( 0x0000 ); /* e_ss */
  757. put_word( 0x00b8 ); /* e_sp */
  758. put_word( 0 ); /* e_csum */
  759. put_word( 0 ); /* e_ip */
  760. put_word( 0 ); /* e_cs */
  761. put_word( lfanew ); /* e_lfarlc */
  762. put_word( 0 ); /* e_ovno */
  763. put_dword( 0 ); /* e_res */
  764. put_dword( 0 );
  765. put_word( 0 ); /* e_oemid */
  766. put_word( 0 ); /* e_oeminfo */
  767. put_dword( 0 ); /* e_res2 */
  768. put_dword( 0 );
  769. put_dword( 0 );
  770. put_dword( 0 );
  771. put_dword( 0 );
  772. put_dword( lfanew );
  773. put_data( fakedll_signature, sizeof(fakedll_signature) );
  774. align_output( 16 );
  775. put_word( 0x454e ); /* ne_magic */
  776. put_byte( 0 ); /* ne_ver */
  777. put_byte( 0 ); /* ne_rev */
  778. put_word( enttab - lfanew ); /* ne_enttab */
  779. put_word( cbenttab ); /* ne_cbenttab */
  780. put_dword( 0 ); /* ne_crc */
  781. put_word( NE_FFLAGS_SINGLEDATA | /* ne_flags */
  782. ((spec->characteristics & IMAGE_FILE_DLL) ? NE_FFLAGS_LIBMODULE : 0) );
  783. put_word( 2 ); /* ne_autodata */
  784. put_word( spec->heap_size ); /* ne_heap */
  785. put_word( 0 ); /* ne_stack */
  786. put_word( 0 ); put_word( 0 ); /* ne_csip */
  787. put_word( 0 ); put_word( 2 ); /* ne_sssp */
  788. put_word( cseg ); /* ne_cseg */
  789. put_word( 0 ); /* ne_cmod */
  790. put_word( 0 ); /* ne_cbnrestab */
  791. put_word( segtab - lfanew ); /* ne_segtab */
  792. put_word( rsrctab - lfanew ); /* ne_rsrctab */
  793. put_word( restab - lfanew ); /* ne_restab */
  794. put_word( modtab - lfanew ); /* ne_modtab */
  795. put_word( imptab - lfanew ); /* ne_imptab */
  796. put_dword( 0 ); /* ne_nrestab */
  797. put_word( 0 ); /* ne_cmovent */
  798. put_word( 0 ); /* ne_align */
  799. put_word( 0 ); /* ne_cres */
  800. put_byte( 2 /*NE_OSFLAGS_WINDOWS*/ ); /* ne_exetyp */
  801. put_byte( 8 /*NE_AFLAGS_FASTLOAD*/ ); /* ne_flagsothers */
  802. put_word( 0 ); /* ne_pretthunks */
  803. put_word( 0 ); /* ne_psegrefbytes */
  804. put_word( 0 ); /* ne_swaparea */
  805. put_word( 0 ); /* ne_expver */
  806. /* segment table */
  807. put_word( codeseg );
  808. put_word( sizeof(code_segment) );
  809. put_word( 0x2000 /* NE_SEGFLAGS_32BIT */ );
  810. put_word( sizeof(code_segment) );
  811. put_word( dataseg );
  812. put_word( sizeof(data_segment) );
  813. put_word( 0x0001 /* NE_SEGFLAGS_DATA */ );
  814. put_word( sizeof(data_segment) );
  815. /* resource directory */
  816. if (spec->nb_resources)
  817. {
  818. output_bin_res16_directory( spec, rsrcdata );
  819. align_output( 2 );
  820. }
  821. /* resident names table */
  822. put_byte( namelen );
  823. for (i = 0; i < namelen; i++) put_byte( toupper(spec->dll_name[i]) );
  824. put_byte( 0 );
  825. align_output( 2 );
  826. /* imported names table */
  827. put_word( 0 );
  828. /* entry table */
  829. put_byte( 0 );
  830. align_output( 2 );
  831. /* code segment */
  832. put_data( code_segment, sizeof(code_segment) );
  833. /* data segment */
  834. put_data( data_segment, sizeof(data_segment) );
  835. /* resource data */
  836. output_bin_res16_data( spec );
  837. flush_output_buffer();
  838. }