spec16.c 34 KB

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