import.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223
  1. /*
  2. * DLL imports support
  3. *
  4. * Copyright 2000, 2004 Alexandre Julliard
  5. * Copyright 2000 Eric Pouech
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #include "config.h"
  22. #include "wine/port.h"
  23. #include <assert.h>
  24. #include <ctype.h>
  25. #include <fcntl.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #ifdef HAVE_SYS_STAT_H
  30. # include <sys/stat.h>
  31. #endif
  32. #ifdef HAVE_UNISTD_H
  33. # include <unistd.h>
  34. #endif
  35. #include "wine/list.h"
  36. #include "build.h"
  37. /* standard C functions that are also exported from ntdll */
  38. static const char *stdc_names[] =
  39. {
  40. "abs",
  41. "atan",
  42. "atoi",
  43. "atol",
  44. "bsearch",
  45. "ceil",
  46. "cos",
  47. "fabs",
  48. "floor",
  49. "isalnum",
  50. "isalpha",
  51. "iscntrl",
  52. "isdigit",
  53. "isgraph",
  54. "islower",
  55. "isprint",
  56. "ispunct",
  57. "isspace",
  58. "isupper",
  59. "iswalpha",
  60. "iswctype",
  61. "iswdigit",
  62. "iswlower",
  63. "iswspace",
  64. "iswxdigit",
  65. "isxdigit",
  66. "labs",
  67. "log",
  68. "mbstowcs",
  69. "memchr",
  70. "memcmp",
  71. "memcpy",
  72. "memmove",
  73. "memset",
  74. "pow",
  75. "qsort",
  76. "sin",
  77. "sprintf",
  78. "sqrt",
  79. "sscanf",
  80. "strcat",
  81. "strchr",
  82. "strcmp",
  83. "strcpy",
  84. "strcspn",
  85. "strlen",
  86. "strncat",
  87. "strncmp",
  88. "strncpy",
  89. "strnlen",
  90. "strpbrk",
  91. "strrchr",
  92. "strspn",
  93. "strstr",
  94. "strtol",
  95. "strtoul",
  96. "swprintf",
  97. "tan",
  98. "tolower",
  99. "toupper",
  100. "towlower",
  101. "towupper",
  102. "vsprintf",
  103. "wcscat",
  104. "wcschr",
  105. "wcscmp",
  106. "wcscpy",
  107. "wcscspn",
  108. "wcslen",
  109. "wcsncat",
  110. "wcsncmp",
  111. "wcsncpy",
  112. "wcspbrk",
  113. "wcsrchr",
  114. "wcsspn",
  115. "wcsstr",
  116. "wcstok",
  117. "wcstol",
  118. "wcstombs",
  119. "wcstoul"
  120. };
  121. static struct strarray stdc_functions = { stdc_names, ARRAY_SIZE(stdc_names), ARRAY_SIZE(stdc_names) };
  122. struct import_func
  123. {
  124. const char *name;
  125. const char *export_name;
  126. int ordinal;
  127. int hint;
  128. };
  129. struct import
  130. {
  131. struct list entry; /* entry in global dll list */
  132. char *dll_name; /* exported file name of the dll */
  133. char *c_name; /* dll name as a C-compatible identifier */
  134. char *full_name; /* full name of the input file */
  135. dev_t dev; /* device/inode of the input file */
  136. ino_t ino;
  137. ORDDEF **exports; /* functions exported from this dll */
  138. int nb_exports; /* number of exported functions */
  139. struct import_func *imports; /* functions we want to import from this dll */
  140. int nb_imports; /* number of imported functions */
  141. int max_imports; /* size of imports array */
  142. };
  143. static struct strarray undef_symbols; /* list of undefined symbols */
  144. static struct strarray extra_ld_symbols; /* list of extra symbols that ld should resolve */
  145. static struct strarray delayed_imports; /* list of delayed import dlls */
  146. static struct strarray ext_link_imports; /* list of external symbols to link to */
  147. static struct list dll_imports = LIST_INIT( dll_imports );
  148. static struct list dll_delayed = LIST_INIT( dll_delayed );
  149. static struct strarray as_files;
  150. static const char import_func_prefix[] = "__wine$func$";
  151. static const char import_ord_prefix[] = "__wine$ord$";
  152. static inline const char *ppc_reg( int reg )
  153. {
  154. static const char * const ppc_regs[32] = { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  155. "r8", "r9", "r10","r11","r12","r13","r14","r15",
  156. "r16","r17","r18","r19","r20","r21","r22","r23",
  157. "r24","r25","r26","r27","r28","r29","r30","r31" };
  158. if (target_platform == PLATFORM_APPLE) return ppc_regs[reg];
  159. return ppc_regs[reg] + 1; /* skip the 'r' */
  160. }
  161. /* compare function names; helper for resolve_imports */
  162. static int name_cmp( const void *name, const void *entry )
  163. {
  164. return strcmp( *(const char* const *)name, *(const char* const *)entry );
  165. }
  166. /* compare function names; helper for resolve_imports */
  167. static int func_cmp( const void *func1, const void *func2 )
  168. {
  169. const ORDDEF *odp1 = *(const ORDDEF * const *)func1;
  170. const ORDDEF *odp2 = *(const ORDDEF * const *)func2;
  171. return strcmp( odp1->name ? odp1->name : odp1->export_name,
  172. odp2->name ? odp2->name : odp2->export_name );
  173. }
  174. /* remove a name from a name table */
  175. static inline void remove_name( struct strarray *table, unsigned int idx )
  176. {
  177. assert( idx < table->count );
  178. memmove( table->str + idx, table->str + idx + 1,
  179. (table->count - idx - 1) * sizeof(*table->str) );
  180. table->count--;
  181. }
  182. /* locate a name in a (sorted) list */
  183. static inline const char *find_name( const char *name, const struct strarray *table )
  184. {
  185. char **res = NULL;
  186. if (table->count) res = bsearch( &name, table->str, table->count, sizeof(*table->str), name_cmp );
  187. return res ? *res : NULL;
  188. }
  189. /* sort a name table */
  190. static inline void sort_names( struct strarray *table )
  191. {
  192. if (table->count) qsort( table->str, table->count, sizeof(*table->str), name_cmp );
  193. }
  194. /* locate an export in a (sorted) export list */
  195. static inline ORDDEF *find_export( const char *name, ORDDEF **table, int size )
  196. {
  197. ORDDEF func, *odp, **res = NULL;
  198. func.name = func.export_name = xstrdup(name);
  199. odp = &func;
  200. if (table) res = bsearch( &odp, table, size, sizeof(*table), func_cmp );
  201. free( func.name );
  202. return res ? *res : NULL;
  203. }
  204. /* free an import structure */
  205. static void free_imports( struct import *imp )
  206. {
  207. free( imp->exports );
  208. free( imp->imports );
  209. free( imp->dll_name );
  210. free( imp->c_name );
  211. free( imp->full_name );
  212. free( imp );
  213. }
  214. /* check whether a given dll is imported in delayed mode */
  215. static int is_delayed_import( const char *name )
  216. {
  217. unsigned int i;
  218. for (i = 0; i < delayed_imports.count; i++)
  219. {
  220. if (!strcmp( delayed_imports.str[i], name )) return 1;
  221. }
  222. return 0;
  223. }
  224. /* find an imported dll from its name */
  225. static struct import *find_import_dll( const char *name )
  226. {
  227. struct import *import;
  228. LIST_FOR_EACH_ENTRY( import, &dll_imports, struct import, entry )
  229. if (!strcasecmp( import->dll_name, name )) return import;
  230. LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
  231. if (!strcasecmp( import->dll_name, name )) return import;
  232. return NULL;
  233. }
  234. /* open the .so library for a given dll in a specified path */
  235. static char *try_library_path( const char *path, const char *name )
  236. {
  237. char *buffer;
  238. int fd;
  239. buffer = strmake( "%s/lib%s.def", path, name );
  240. /* check if the file exists */
  241. if ((fd = open( buffer, O_RDONLY )) != -1)
  242. {
  243. close( fd );
  244. return buffer;
  245. }
  246. free( buffer );
  247. return NULL;
  248. }
  249. /* find the .def import library for a given dll */
  250. static char *find_library( const char *name )
  251. {
  252. char *fullname;
  253. unsigned int i;
  254. for (i = 0; i < lib_path.count; i++)
  255. {
  256. if ((fullname = try_library_path( lib_path.str[i], name ))) return fullname;
  257. }
  258. fatal_error( "could not open .def file for %s\n", name );
  259. return NULL;
  260. }
  261. /* read in the list of exported symbols of an import library */
  262. static DLLSPEC *read_import_lib( struct import *imp )
  263. {
  264. FILE *f;
  265. int i;
  266. struct stat stat;
  267. struct import *prev_imp;
  268. DLLSPEC *spec = alloc_dll_spec();
  269. f = open_input_file( NULL, imp->full_name );
  270. fstat( fileno(f), &stat );
  271. imp->dev = stat.st_dev;
  272. imp->ino = stat.st_ino;
  273. if (!parse_def_file( f, spec )) exit( 1 );
  274. close_input_file( f );
  275. /* check if we already imported that library from a different file */
  276. if ((prev_imp = find_import_dll( spec->file_name )))
  277. {
  278. if (prev_imp->dev != imp->dev || prev_imp->ino != imp->ino)
  279. fatal_error( "%s and %s have the same export name '%s'\n",
  280. prev_imp->full_name, imp->full_name, spec->file_name );
  281. free_dll_spec( spec );
  282. return NULL; /* the same file was already loaded, ignore this one */
  283. }
  284. if (spec->nb_entry_points)
  285. {
  286. imp->exports = xmalloc( spec->nb_entry_points * sizeof(*imp->exports) );
  287. for (i = 0; i < spec->nb_entry_points; i++)
  288. imp->exports[imp->nb_exports++] = &spec->entry_points[i];
  289. qsort( imp->exports, imp->nb_exports, sizeof(*imp->exports), func_cmp );
  290. }
  291. return spec;
  292. }
  293. /* build the dll exported name from the import lib name or path */
  294. static char *get_dll_name( const char *name, const char *filename )
  295. {
  296. char *ret;
  297. if (filename)
  298. {
  299. const char *basename = strrchr( filename, '/' );
  300. if (!basename) basename = filename;
  301. else basename++;
  302. if (!strncmp( basename, "lib", 3 )) basename += 3;
  303. ret = xmalloc( strlen(basename) + 5 );
  304. strcpy( ret, basename );
  305. if (strendswith( ret, ".def" )) ret[strlen(ret)-4] = 0;
  306. }
  307. else
  308. {
  309. ret = xmalloc( strlen(name) + 5 );
  310. strcpy( ret, name );
  311. }
  312. if (!strchr( ret, '.' )) strcat( ret, ".dll" );
  313. return ret;
  314. }
  315. /* add a dll to the list of imports */
  316. void add_import_dll( const char *name, const char *filename )
  317. {
  318. DLLSPEC *spec;
  319. char *dll_name = get_dll_name( name, filename );
  320. struct import *imp = xmalloc( sizeof(*imp) );
  321. memset( imp, 0, sizeof(*imp) );
  322. if (filename) imp->full_name = xstrdup( filename );
  323. else imp->full_name = find_library( name );
  324. if (!(spec = read_import_lib( imp )))
  325. {
  326. free_imports( imp );
  327. return;
  328. }
  329. imp->dll_name = spec->file_name ? spec->file_name : dll_name;
  330. imp->c_name = make_c_identifier( imp->dll_name );
  331. if (is_delayed_import( imp->dll_name ))
  332. list_add_tail( &dll_delayed, &imp->entry );
  333. else
  334. list_add_tail( &dll_imports, &imp->entry );
  335. }
  336. /* add a library to the list of delayed imports */
  337. void add_delayed_import( const char *name )
  338. {
  339. struct import *imp;
  340. char *fullname = get_dll_name( name, NULL );
  341. strarray_add( &delayed_imports, fullname, NULL );
  342. if ((imp = find_import_dll( fullname )))
  343. {
  344. list_remove( &imp->entry );
  345. list_add_tail( &dll_delayed, &imp->entry );
  346. }
  347. }
  348. /* add a symbol to the list of extra symbols that ld must resolve */
  349. void add_extra_ld_symbol( const char *name )
  350. {
  351. strarray_add( &extra_ld_symbols, name, NULL );
  352. }
  353. /* retrieve an imported dll, adding one if necessary */
  354. struct import *add_static_import_dll( const char *name )
  355. {
  356. struct import *import;
  357. char *dll_name = get_dll_name( name, NULL );
  358. if ((import = find_import_dll( dll_name ))) return import;
  359. import = xmalloc( sizeof(*import) );
  360. memset( import, 0, sizeof(*import) );
  361. import->dll_name = dll_name;
  362. import->full_name = xstrdup( dll_name );
  363. import->c_name = make_c_identifier( dll_name );
  364. if (is_delayed_import( dll_name ))
  365. list_add_tail( &dll_delayed, &import->entry );
  366. else
  367. list_add_tail( &dll_imports, &import->entry );
  368. return import;
  369. }
  370. /* add a function to the list of imports from a given dll */
  371. static void add_import_func( struct import *imp, const char *name, const char *export_name,
  372. int ordinal, int hint )
  373. {
  374. if (imp->nb_imports == imp->max_imports)
  375. {
  376. imp->max_imports *= 2;
  377. if (imp->max_imports < 32) imp->max_imports = 32;
  378. imp->imports = xrealloc( imp->imports, imp->max_imports * sizeof(*imp->imports) );
  379. }
  380. imp->imports[imp->nb_imports].name = name;
  381. imp->imports[imp->nb_imports].export_name = export_name;
  382. imp->imports[imp->nb_imports].ordinal = ordinal;
  383. imp->imports[imp->nb_imports].hint = hint;
  384. imp->nb_imports++;
  385. }
  386. /* add an import for an undefined function of the form __wine$func$ */
  387. static void add_undef_import( const char *name, int is_ordinal )
  388. {
  389. char *p, *dll_name = xstrdup( name );
  390. int ordinal = 0;
  391. struct import *import;
  392. if (!(p = strchr( dll_name, '$' ))) return;
  393. *p++ = 0;
  394. while (*p >= '0' && *p <= '9') ordinal = 10 * ordinal + *p++ - '0';
  395. if (*p != '$') return;
  396. p++;
  397. import = add_static_import_dll( dll_name );
  398. if (is_ordinal)
  399. add_import_func( import, NULL, xstrdup( p ), ordinal, 0 );
  400. else
  401. add_import_func( import, xstrdup( p ), NULL, ordinal, 0 );
  402. }
  403. /* check if the spec file exports any stubs */
  404. static int has_stubs( const DLLSPEC *spec )
  405. {
  406. int i;
  407. if (unix_lib) return 0;
  408. for (i = 0; i < spec->nb_entry_points; i++)
  409. {
  410. ORDDEF *odp = &spec->entry_points[i];
  411. if (odp->type == TYPE_STUB) return 1;
  412. }
  413. return 0;
  414. }
  415. /* add the extra undefined symbols that will be contained in the generated spec file itself */
  416. static void add_extra_undef_symbols( DLLSPEC *spec )
  417. {
  418. add_extra_ld_symbol( spec->init_func );
  419. if (spec->type == SPEC_WIN16) add_extra_ld_symbol( "DllMain" );
  420. if (has_stubs( spec )) add_extra_ld_symbol( "__wine_spec_unimplemented_stub" );
  421. if (delayed_imports.count) add_extra_ld_symbol( "__wine_spec_delay_load" );
  422. }
  423. /* check if a given imported dll is not needed, taking forwards into account */
  424. static int check_unused( const struct import* imp, const DLLSPEC *spec )
  425. {
  426. int i;
  427. const char *file_name = imp->dll_name;
  428. size_t len = strlen( file_name );
  429. const char *p = strchr( file_name, '.' );
  430. if (p && !strcasecmp( p, ".dll" )) len = p - file_name;
  431. for (i = spec->base; i <= spec->limit; i++)
  432. {
  433. ORDDEF *odp = spec->ordinals[i];
  434. if (!odp || !(odp->flags & FLAG_FORWARD)) continue;
  435. if (!strncasecmp( odp->link_name, file_name, len ) &&
  436. odp->link_name[len] == '.')
  437. return 0; /* found a forward, it is used */
  438. }
  439. return 1;
  440. }
  441. /* check if a given forward does exist in one of the imported dlls */
  442. static void check_undefined_forwards( DLLSPEC *spec )
  443. {
  444. struct import *imp;
  445. char *link_name, *api_name, *dll_name, *p;
  446. int i;
  447. if (unix_lib) return;
  448. for (i = 0; i < spec->nb_entry_points; i++)
  449. {
  450. ORDDEF *odp = &spec->entry_points[i];
  451. if (!(odp->flags & FLAG_FORWARD)) continue;
  452. link_name = xstrdup( odp->link_name );
  453. p = strrchr( link_name, '.' );
  454. *p = 0;
  455. api_name = p + 1;
  456. dll_name = get_dll_name( link_name, NULL );
  457. if ((imp = find_import_dll( dll_name )))
  458. {
  459. if (!find_export( api_name, imp->exports, imp->nb_exports ))
  460. warning( "%s:%d: forward '%s' not found in %s\n",
  461. spec->src_name, odp->lineno, odp->link_name, imp->dll_name );
  462. }
  463. else warning( "%s:%d: forward '%s' not found in the imported dll list\n",
  464. spec->src_name, odp->lineno, odp->link_name );
  465. free( link_name );
  466. free( dll_name );
  467. }
  468. }
  469. /* flag the dll exports that link to an undefined symbol */
  470. static void check_undefined_exports( DLLSPEC *spec )
  471. {
  472. int i;
  473. if (unix_lib) return;
  474. for (i = 0; i < spec->nb_entry_points; i++)
  475. {
  476. ORDDEF *odp = &spec->entry_points[i];
  477. if (odp->type == TYPE_STUB || odp->type == TYPE_ABS || odp->type == TYPE_VARIABLE) continue;
  478. if (odp->flags & FLAG_FORWARD) continue;
  479. if (odp->flags & FLAG_SYSCALL) continue;
  480. if (find_name( odp->link_name, &undef_symbols ))
  481. {
  482. switch(odp->type)
  483. {
  484. case TYPE_PASCAL:
  485. case TYPE_STDCALL:
  486. case TYPE_CDECL:
  487. case TYPE_VARARGS:
  488. if (link_ext_symbols)
  489. {
  490. odp->flags |= FLAG_EXT_LINK;
  491. strarray_add( &ext_link_imports, odp->link_name, NULL );
  492. }
  493. else error( "%s:%d: function '%s' not defined\n",
  494. spec->src_name, odp->lineno, odp->link_name );
  495. break;
  496. default:
  497. if (!strcmp( odp->link_name, "__wine_syscall_dispatcher" )) break;
  498. error( "%s:%d: external symbol '%s' is not a function\n",
  499. spec->src_name, odp->lineno, odp->link_name );
  500. break;
  501. }
  502. }
  503. }
  504. }
  505. /* create a .o file that references all the undefined symbols we want to resolve */
  506. static char *create_undef_symbols_file( DLLSPEC *spec )
  507. {
  508. char *as_file, *obj_file;
  509. int i;
  510. unsigned int j;
  511. if (unix_lib) return NULL;
  512. as_file = open_temp_output_file( ".s" );
  513. output( "\t.data\n" );
  514. for (i = 0; i < spec->nb_entry_points; i++)
  515. {
  516. ORDDEF *odp = &spec->entry_points[i];
  517. if (odp->type == TYPE_STUB || odp->type == TYPE_ABS || odp->type == TYPE_VARIABLE) continue;
  518. if (odp->flags & FLAG_FORWARD) continue;
  519. if (odp->flags & FLAG_SYSCALL) continue;
  520. output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name( get_link_name( odp )));
  521. }
  522. for (j = 0; j < extra_ld_symbols.count; j++)
  523. output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(extra_ld_symbols.str[j]) );
  524. fclose( output_file );
  525. obj_file = get_temp_file_name( output_file_name, ".o" );
  526. assemble_file( as_file, obj_file );
  527. return obj_file;
  528. }
  529. /* combine a list of object files with ld into a single object file */
  530. /* returns the name of the combined file */
  531. static const char *ldcombine_files( DLLSPEC *spec, char **argv )
  532. {
  533. char *ld_tmp_file, *undef_file;
  534. struct strarray args = get_ld_command();
  535. undef_file = create_undef_symbols_file( spec );
  536. ld_tmp_file = get_temp_file_name( output_file_name, ".o" );
  537. strarray_add( &args, "-r", "-o", ld_tmp_file, undef_file, NULL );
  538. strarray_addv( &args, argv );
  539. spawn( args );
  540. return ld_tmp_file;
  541. }
  542. /* read in the list of undefined symbols */
  543. void read_undef_symbols( DLLSPEC *spec, char **argv )
  544. {
  545. size_t prefix_len;
  546. FILE *f;
  547. const char *prog = get_nm_command();
  548. char *cmd, buffer[1024], name_prefix[16];
  549. int err;
  550. const char *name;
  551. if (!argv[0]) return;
  552. add_extra_undef_symbols( spec );
  553. strcpy( name_prefix, asm_name("") );
  554. prefix_len = strlen( name_prefix );
  555. name = ldcombine_files( spec, argv );
  556. cmd = strmake( "%s -u %s", prog, name );
  557. if (verbose)
  558. fprintf( stderr, "%s\n", cmd );
  559. if (!(f = popen( cmd, "r" )))
  560. fatal_error( "Cannot execute '%s'\n", cmd );
  561. while (fgets( buffer, sizeof(buffer), f ))
  562. {
  563. char *p = buffer + strlen(buffer) - 1;
  564. if (p < buffer) continue;
  565. if (*p == '\n') *p-- = 0;
  566. p = buffer;
  567. while (*p == ' ') p++;
  568. if (p[0] == 'U' && p[1] == ' ' && p[2]) p += 2;
  569. if (prefix_len && !strncmp( p, name_prefix, prefix_len )) p += prefix_len;
  570. if (!strncmp( p, import_func_prefix, strlen(import_func_prefix) ))
  571. add_undef_import( p + strlen( import_func_prefix ), 0 );
  572. else if (!strncmp( p, import_ord_prefix, strlen(import_ord_prefix) ))
  573. add_undef_import( p + strlen( import_ord_prefix ), 1 );
  574. else if (use_msvcrt || !find_name( p, &stdc_functions ))
  575. strarray_add( &undef_symbols, xstrdup( p ), NULL );
  576. }
  577. if ((err = pclose( f ))) warning( "%s failed with status %d\n", cmd, err );
  578. free( cmd );
  579. }
  580. void resolve_dll_imports( DLLSPEC *spec, struct list *list )
  581. {
  582. unsigned int j;
  583. struct import *imp, *next;
  584. ORDDEF *odp;
  585. LIST_FOR_EACH_ENTRY_SAFE( imp, next, list, struct import, entry )
  586. {
  587. for (j = 0; j < undef_symbols.count; j++)
  588. {
  589. odp = find_export( undef_symbols.str[j], imp->exports, imp->nb_exports );
  590. if (odp)
  591. {
  592. if (odp->flags & FLAG_PRIVATE) continue;
  593. if (odp->type != TYPE_STDCALL && odp->type != TYPE_CDECL)
  594. warning( "winebuild: Data export '%s' cannot be imported from %s\n",
  595. odp->link_name, imp->dll_name );
  596. else
  597. {
  598. add_import_func( imp, (odp->flags & FLAG_NONAME) ? NULL : odp->name,
  599. odp->export_name, odp->ordinal, odp->hint );
  600. remove_name( &undef_symbols, j-- );
  601. }
  602. }
  603. }
  604. if (!imp->nb_imports)
  605. {
  606. /* the dll is not used, get rid of it */
  607. if (check_unused( imp, spec ))
  608. warning( "winebuild: %s imported but no symbols used\n", imp->dll_name );
  609. list_remove( &imp->entry );
  610. free_imports( imp );
  611. }
  612. }
  613. }
  614. /* resolve the imports for a Win32 module */
  615. void resolve_imports( DLLSPEC *spec )
  616. {
  617. check_undefined_forwards( spec );
  618. resolve_dll_imports( spec, &dll_imports );
  619. resolve_dll_imports( spec, &dll_delayed );
  620. sort_names( &undef_symbols );
  621. check_undefined_exports( spec );
  622. }
  623. /* check if symbol is still undefined */
  624. int is_undefined( const char *name )
  625. {
  626. return find_name( name, &undef_symbols ) != NULL;
  627. }
  628. /* output the get_pc thunk if needed */
  629. void output_get_pc_thunk(void)
  630. {
  631. assert( target_cpu == CPU_x86 );
  632. output( "\n\t.text\n" );
  633. output( "\t.align %d\n", get_alignment(4) );
  634. output( "\t%s\n", func_declaration("__wine_spec_get_pc_thunk_eax") );
  635. output( "%s:\n", asm_name("__wine_spec_get_pc_thunk_eax") );
  636. output_cfi( ".cfi_startproc" );
  637. output( "\tmovl (%%esp),%%eax\n" );
  638. output( "\tret\n" );
  639. output_cfi( ".cfi_endproc" );
  640. output_function_size( "__wine_spec_get_pc_thunk_eax" );
  641. }
  642. /* output a single import thunk */
  643. static void output_import_thunk( const char *name, const char *table, int pos )
  644. {
  645. output( "\n\t.align %d\n", get_alignment(4) );
  646. output( "\t%s\n", func_declaration(name) );
  647. output( "%s\n", asm_globl(name) );
  648. output_cfi( ".cfi_startproc" );
  649. switch(target_cpu)
  650. {
  651. case CPU_x86:
  652. if (!UsePIC)
  653. {
  654. output( "\tjmp *(%s+%d)\n", table, pos );
  655. }
  656. else
  657. {
  658. output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
  659. output( "1:\tjmp *%s+%d-1b(%%eax)\n", table, pos );
  660. needs_get_pc_thunk = 1;
  661. }
  662. break;
  663. case CPU_x86_64:
  664. output( "\tjmpq *%s+%d(%%rip)\n", table, pos );
  665. break;
  666. case CPU_ARM:
  667. if (UsePIC)
  668. {
  669. output( "\tldr ip, 2f\n");
  670. output( "1:\tadd ip, pc\n" );
  671. output( "\tldr pc, [ip]\n");
  672. output( "2:\t.long %s+%u-1b-%u\n", table, pos, thumb_mode ? 4 : 8 );
  673. }
  674. else
  675. {
  676. output( "\tldr ip, 1f\n");
  677. output( "\tldr pc, [ip]\n");
  678. output( "1:\t.long %s+%u\n", table, pos );
  679. }
  680. break;
  681. case CPU_ARM64:
  682. output( "\tadrp x16, %s\n", arm64_page( table ) );
  683. output( "\tadd x16, x16, #%s\n", arm64_pageoff( table ) );
  684. if (pos & ~0x7fff) output( "\tadd x16, x16, #%u\n", pos & ~0x7fff );
  685. output( "\tldr x16, [x16, #%u]\n", pos & 0x7fff );
  686. output( "\tbr x16\n" );
  687. break;
  688. case CPU_POWERPC:
  689. output( "\tmr %s, %s\n", ppc_reg(0), ppc_reg(31) );
  690. if (target_platform == PLATFORM_APPLE)
  691. {
  692. output( "\tlis %s, ha16(%s+%d+32768)\n", ppc_reg(31), table, pos );
  693. output( "\tla %s, lo16(%s+%d)(%s)\n", ppc_reg(31), table, pos, ppc_reg(31) );
  694. }
  695. else
  696. {
  697. output( "\tlis %s, (%s+%d+32768)@h\n", ppc_reg(31), table, pos );
  698. output( "\tla %s, (%s+%d)@l(%s)\n", ppc_reg(31), table, pos, ppc_reg(31) );
  699. }
  700. output( "\tlwz %s, 0(%s)\n", ppc_reg(31), ppc_reg(31) );
  701. output( "\tmtctr %s\n", ppc_reg(31) );
  702. output( "\tmr %s, %s\n", ppc_reg(31), ppc_reg(0) );
  703. output( "\tbctr\n" );
  704. break;
  705. }
  706. output_cfi( ".cfi_endproc" );
  707. output_function_size( name );
  708. }
  709. /* check if we need an import directory */
  710. int has_imports(void)
  711. {
  712. return !list_empty( &dll_imports );
  713. }
  714. /* output the import table of a Win32 module */
  715. static void output_immediate_imports(void)
  716. {
  717. int i, j;
  718. struct import *import;
  719. if (list_empty( &dll_imports )) return; /* no immediate imports */
  720. /* main import header */
  721. output( "\n/* import table */\n" );
  722. output( "\n\t.data\n" );
  723. output( "\t.align %d\n", get_alignment(4) );
  724. output( ".L__wine_spec_imports:\n" );
  725. /* list of dlls */
  726. j = 0;
  727. LIST_FOR_EACH_ENTRY( import, &dll_imports, struct import, entry )
  728. {
  729. output_rva( ".L__wine_spec_import_data_names + %d", j * get_ptr_size() ); /* OriginalFirstThunk */
  730. output( "\t.long 0\n" ); /* TimeDateStamp */
  731. output( "\t.long 0\n" ); /* ForwarderChain */
  732. output_rva( ".L__wine_spec_import_name_%s", import->c_name ); /* Name */
  733. output_rva( ".L__wine_spec_import_data_ptrs + %d", j * get_ptr_size() ); /* FirstThunk */
  734. j += import->nb_imports + 1;
  735. }
  736. output( "\t.long 0\n" ); /* OriginalFirstThunk */
  737. output( "\t.long 0\n" ); /* TimeDateStamp */
  738. output( "\t.long 0\n" ); /* ForwarderChain */
  739. output( "\t.long 0\n" ); /* Name */
  740. output( "\t.long 0\n" ); /* FirstThunk */
  741. output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
  742. /* output the names twice, once for OriginalFirstThunk and once for FirstThunk */
  743. for (i = 0; i < 2; i++)
  744. {
  745. output( ".L__wine_spec_import_data_%s:\n", i ? "ptrs" : "names" );
  746. LIST_FOR_EACH_ENTRY( import, &dll_imports, struct import, entry )
  747. {
  748. for (j = 0; j < import->nb_imports; j++)
  749. {
  750. struct import_func *func = &import->imports[j];
  751. if (i)
  752. {
  753. if (func->name) output( "__imp_%s:\n", asm_name( func->name ));
  754. else if (func->export_name) output( "__imp_%s:\n", asm_name( func->export_name ));
  755. }
  756. if (func->name)
  757. output( "\t%s .L__wine_spec_import_data_%s_%s-.L__wine_spec_rva_base\n",
  758. get_asm_ptr_keyword(), import->c_name, func->name );
  759. else
  760. {
  761. if (get_ptr_size() == 8)
  762. output( "\t.quad 0x800000000000%04x\n", func->ordinal );
  763. else
  764. output( "\t.long 0x8000%04x\n", func->ordinal );
  765. }
  766. }
  767. output( "\t%s 0\n", get_asm_ptr_keyword() );
  768. }
  769. }
  770. output( ".L__wine_spec_imports_end:\n" );
  771. LIST_FOR_EACH_ENTRY( import, &dll_imports, struct import, entry )
  772. {
  773. for (j = 0; j < import->nb_imports; j++)
  774. {
  775. struct import_func *func = &import->imports[j];
  776. if (!func->name) continue;
  777. output( "\t.align %d\n", get_alignment(2) );
  778. output( ".L__wine_spec_import_data_%s_%s:\n", import->c_name, func->name );
  779. output( "\t.short %d\n", func->hint );
  780. output( "\t%s \"%s\"\n", get_asm_string_keyword(), func->name );
  781. }
  782. }
  783. LIST_FOR_EACH_ENTRY( import, &dll_imports, struct import, entry )
  784. {
  785. output( ".L__wine_spec_import_name_%s:\n\t%s \"%s\"\n",
  786. import->c_name, get_asm_string_keyword(), import->dll_name );
  787. }
  788. }
  789. /* output the import thunks of a Win32 module */
  790. static void output_immediate_import_thunks(void)
  791. {
  792. int j, pos;
  793. struct import *import;
  794. static const char import_thunks[] = "__wine_spec_import_thunks";
  795. if (list_empty( &dll_imports )) return;
  796. output( "\n/* immediate import thunks */\n\n" );
  797. output( "\t.text\n" );
  798. output( "\t.align %d\n", get_alignment(8) );
  799. output( "%s:\n", asm_name(import_thunks));
  800. pos = 0;
  801. LIST_FOR_EACH_ENTRY( import, &dll_imports, struct import, entry )
  802. {
  803. for (j = 0; j < import->nb_imports; j++, pos += get_ptr_size())
  804. {
  805. struct import_func *func = &import->imports[j];
  806. output_import_thunk( func->name ? func->name : func->export_name,
  807. ".L__wine_spec_import_data_ptrs", pos );
  808. }
  809. pos += get_ptr_size();
  810. }
  811. output_function_size( import_thunks );
  812. }
  813. /* output the delayed import table of a Win32 module */
  814. static void output_delayed_imports( const DLLSPEC *spec )
  815. {
  816. int j, mod;
  817. struct import *import;
  818. if (list_empty( &dll_delayed )) return;
  819. output( "\n/* delayed imports */\n\n" );
  820. output( "\t.data\n" );
  821. output( "\t.align %d\n", get_alignment(get_ptr_size()) );
  822. output( "%s\n", asm_globl("__wine_spec_delay_imports") );
  823. /* list of dlls */
  824. j = mod = 0;
  825. LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
  826. {
  827. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* grAttrs */
  828. output( "\t%s .L__wine_delay_name_%s\n", /* szName */
  829. get_asm_ptr_keyword(), import->c_name );
  830. output( "\t%s .L__wine_delay_modules+%d\n", /* phmod */
  831. get_asm_ptr_keyword(), mod * get_ptr_size() );
  832. output( "\t%s .L__wine_delay_IAT+%d\n", /* pIAT */
  833. get_asm_ptr_keyword(), j * get_ptr_size() );
  834. output( "\t%s .L__wine_delay_INT+%d\n", /* pINT */
  835. get_asm_ptr_keyword(), j * get_ptr_size() );
  836. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pBoundIAT */
  837. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pUnloadIAT */
  838. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* dwTimeStamp */
  839. j += import->nb_imports;
  840. mod++;
  841. }
  842. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* grAttrs */
  843. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* szName */
  844. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* phmod */
  845. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pIAT */
  846. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pINT */
  847. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pBoundIAT */
  848. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* pUnloadIAT */
  849. output( "\t%s 0\n", get_asm_ptr_keyword() ); /* dwTimeStamp */
  850. output( "\n.L__wine_delay_IAT:\n" );
  851. LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
  852. {
  853. for (j = 0; j < import->nb_imports; j++)
  854. {
  855. struct import_func *func = &import->imports[j];
  856. const char *name = func->name ? func->name : func->export_name;
  857. output( "__imp_%s:\n", asm_name( name ));
  858. output( "\t%s __wine_delay_imp_%s_%s\n",
  859. get_asm_ptr_keyword(), import->c_name, name );
  860. }
  861. }
  862. output( "\n.L__wine_delay_INT:\n" );
  863. LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
  864. {
  865. for (j = 0; j < import->nb_imports; j++)
  866. {
  867. struct import_func *func = &import->imports[j];
  868. if (!func->name)
  869. output( "\t%s %d\n", get_asm_ptr_keyword(), func->ordinal );
  870. else
  871. output( "\t%s .L__wine_delay_data_%s_%s\n",
  872. get_asm_ptr_keyword(), import->c_name, func->name );
  873. }
  874. }
  875. output( "\n.L__wine_delay_modules:\n" );
  876. LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
  877. {
  878. output( "\t%s 0\n", get_asm_ptr_keyword() );
  879. }
  880. LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
  881. {
  882. output( ".L__wine_delay_name_%s:\n", import->c_name );
  883. output( "\t%s \"%s\"\n", get_asm_string_keyword(), import->dll_name );
  884. }
  885. LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
  886. {
  887. for (j = 0; j < import->nb_imports; j++)
  888. {
  889. struct import_func *func = &import->imports[j];
  890. if (!func->name) continue;
  891. output( ".L__wine_delay_data_%s_%s:\n", import->c_name, func->name );
  892. output( "\t%s \"%s\"\n", get_asm_string_keyword(), func->name );
  893. }
  894. }
  895. output_function_size( "__wine_spec_delay_imports" );
  896. }
  897. /* output the delayed import thunks of a Win32 module */
  898. static void output_delayed_import_thunks( const DLLSPEC *spec )
  899. {
  900. int idx, j, pos, extra_stack_storage = 0;
  901. struct import *import;
  902. static const char delayed_import_loaders[] = "__wine_spec_delayed_import_loaders";
  903. static const char delayed_import_thunks[] = "__wine_spec_delayed_import_thunks";
  904. if (list_empty( &dll_delayed )) return;
  905. output( "\n/* delayed import thunks */\n\n" );
  906. output( "\t.text\n" );
  907. output( "\t.align %d\n", get_alignment(8) );
  908. output( "%s:\n", asm_name(delayed_import_loaders));
  909. output( "\t%s\n", func_declaration("__wine_delay_load_asm") );
  910. output( "%s:\n", asm_name("__wine_delay_load_asm") );
  911. output_cfi( ".cfi_startproc" );
  912. switch(target_cpu)
  913. {
  914. case CPU_x86:
  915. output( "\tpushl %%ecx\n" );
  916. output_cfi( ".cfi_adjust_cfa_offset 4" );
  917. output( "\tpushl %%edx\n" );
  918. output_cfi( ".cfi_adjust_cfa_offset 4" );
  919. output( "\tpushl %%eax\n" );
  920. output_cfi( ".cfi_adjust_cfa_offset 4" );
  921. output( "\tcall %s\n", asm_name("__wine_spec_delay_load") );
  922. output_cfi( ".cfi_adjust_cfa_offset -4" );
  923. output( "\tpopl %%edx\n" );
  924. output_cfi( ".cfi_adjust_cfa_offset -4" );
  925. output( "\tpopl %%ecx\n" );
  926. output_cfi( ".cfi_adjust_cfa_offset -4" );
  927. output( "\tjmp *%%eax\n" );
  928. break;
  929. case CPU_x86_64:
  930. output( "\tsubq $0x98,%%rsp\n" );
  931. output_cfi( ".cfi_adjust_cfa_offset 0x98" );
  932. output( "\tmovq %%rdx,0x88(%%rsp)\n" );
  933. output( "\tmovq %%rcx,0x80(%%rsp)\n" );
  934. output( "\tmovq %%r8,0x78(%%rsp)\n" );
  935. output( "\tmovq %%r9,0x70(%%rsp)\n" );
  936. output( "\tmovq %%r10,0x68(%%rsp)\n" );
  937. output( "\tmovq %%r11,0x60(%%rsp)\n" );
  938. output( "\tmovups %%xmm0,0x50(%%rsp)\n" );
  939. output( "\tmovups %%xmm1,0x40(%%rsp)\n" );
  940. output( "\tmovups %%xmm2,0x30(%%rsp)\n" );
  941. output( "\tmovups %%xmm3,0x20(%%rsp)\n" );
  942. output( "\tmovq %%rax,%%rcx\n" );
  943. output( "\tcall %s\n", asm_name("__wine_spec_delay_load") );
  944. output( "\tmovups 0x20(%%rsp),%%xmm3\n" );
  945. output( "\tmovups 0x30(%%rsp),%%xmm2\n" );
  946. output( "\tmovups 0x40(%%rsp),%%xmm1\n" );
  947. output( "\tmovups 0x50(%%rsp),%%xmm0\n" );
  948. output( "\tmovq 0x60(%%rsp),%%r11\n" );
  949. output( "\tmovq 0x68(%%rsp),%%r10\n" );
  950. output( "\tmovq 0x70(%%rsp),%%r9\n" );
  951. output( "\tmovq 0x78(%%rsp),%%r8\n" );
  952. output( "\tmovq 0x80(%%rsp),%%rcx\n" );
  953. output( "\tmovq 0x88(%%rsp),%%rdx\n" );
  954. output( "\taddq $0x98,%%rsp\n" );
  955. output_cfi( ".cfi_adjust_cfa_offset -0x98" );
  956. output( "\tjmp *%%rax\n" );
  957. break;
  958. case CPU_ARM:
  959. output( "\tpush {r0-r3,FP,LR}\n" );
  960. output( "\tmov r0,IP\n" );
  961. output( "\tbl %s\n", asm_name("__wine_spec_delay_load") );
  962. output( "\tmov IP,r0\n");
  963. output( "\tpop {r0-r3,FP,LR}\n" );
  964. output( "\tbx IP\n");
  965. break;
  966. case CPU_ARM64:
  967. output( "\tstp x29, x30, [sp,#-80]!\n" );
  968. output( "\tmov x29, sp\n" );
  969. output( "\tstp x0, x1, [sp,#16]\n" );
  970. output( "\tstp x2, x3, [sp,#32]\n" );
  971. output( "\tstp x4, x5, [sp,#48]\n" );
  972. output( "\tstp x6, x7, [sp,#64]\n" );
  973. output( "\tmov x0, x16\n" );
  974. output( "\tbl %s\n", asm_name("__wine_spec_delay_load") );
  975. output( "\tmov x16, x0\n" );
  976. output( "\tldp x0, x1, [sp,#16]\n" );
  977. output( "\tldp x2, x3, [sp,#32]\n" );
  978. output( "\tldp x4, x5, [sp,#48]\n" );
  979. output( "\tldp x6, x7, [sp,#64]\n" );
  980. output( "\tldp x29, x30, [sp],#80\n" );
  981. output( "\tbr x16\n" );
  982. break;
  983. case CPU_POWERPC:
  984. if (target_platform == PLATFORM_APPLE) extra_stack_storage = 56;
  985. /* Save all callee saved registers into a stackframe. */
  986. output( "\tstwu %s, -%d(%s)\n",ppc_reg(1), 48+extra_stack_storage, ppc_reg(1));
  987. output( "\tstw %s, %d(%s)\n", ppc_reg(3), 4+extra_stack_storage, ppc_reg(1));
  988. output( "\tstw %s, %d(%s)\n", ppc_reg(4), 8+extra_stack_storage, ppc_reg(1));
  989. output( "\tstw %s, %d(%s)\n", ppc_reg(5), 12+extra_stack_storage, ppc_reg(1));
  990. output( "\tstw %s, %d(%s)\n", ppc_reg(6), 16+extra_stack_storage, ppc_reg(1));
  991. output( "\tstw %s, %d(%s)\n", ppc_reg(7), 20+extra_stack_storage, ppc_reg(1));
  992. output( "\tstw %s, %d(%s)\n", ppc_reg(8), 24+extra_stack_storage, ppc_reg(1));
  993. output( "\tstw %s, %d(%s)\n", ppc_reg(9), 28+extra_stack_storage, ppc_reg(1));
  994. output( "\tstw %s, %d(%s)\n", ppc_reg(10),32+extra_stack_storage, ppc_reg(1));
  995. output( "\tstw %s, %d(%s)\n", ppc_reg(11),36+extra_stack_storage, ppc_reg(1));
  996. output( "\tstw %s, %d(%s)\n", ppc_reg(12),40+extra_stack_storage, ppc_reg(1));
  997. /* r0 -> r3 (arg1) */
  998. output( "\tmr %s, %s\n", ppc_reg(3), ppc_reg(0));
  999. /* save return address */
  1000. output( "\tmflr %s\n", ppc_reg(0));
  1001. output( "\tstw %s, %d(%s)\n", ppc_reg(0), 44+extra_stack_storage, ppc_reg(1));
  1002. /* Call the __wine_delay_load function, arg1 is arg1. */
  1003. output( "\tbl %s\n", asm_name("__wine_spec_delay_load") );
  1004. /* Load return value from call into ctr register */
  1005. output( "\tmtctr %s\n", ppc_reg(3));
  1006. /* restore all saved registers and drop stackframe. */
  1007. output( "\tlwz %s, %d(%s)\n", ppc_reg(3), 4+extra_stack_storage, ppc_reg(1));
  1008. output( "\tlwz %s, %d(%s)\n", ppc_reg(4), 8+extra_stack_storage, ppc_reg(1));
  1009. output( "\tlwz %s, %d(%s)\n", ppc_reg(5), 12+extra_stack_storage, ppc_reg(1));
  1010. output( "\tlwz %s, %d(%s)\n", ppc_reg(6), 16+extra_stack_storage, ppc_reg(1));
  1011. output( "\tlwz %s, %d(%s)\n", ppc_reg(7), 20+extra_stack_storage, ppc_reg(1));
  1012. output( "\tlwz %s, %d(%s)\n", ppc_reg(8), 24+extra_stack_storage, ppc_reg(1));
  1013. output( "\tlwz %s, %d(%s)\n", ppc_reg(9), 28+extra_stack_storage, ppc_reg(1));
  1014. output( "\tlwz %s, %d(%s)\n", ppc_reg(10),32+extra_stack_storage, ppc_reg(1));
  1015. output( "\tlwz %s, %d(%s)\n", ppc_reg(11),36+extra_stack_storage, ppc_reg(1));
  1016. output( "\tlwz %s, %d(%s)\n", ppc_reg(12),40+extra_stack_storage, ppc_reg(1));
  1017. /* Load return value from call into return register */
  1018. output( "\tlwz %s, %d(%s)\n", ppc_reg(0), 44+extra_stack_storage, ppc_reg(1));
  1019. output( "\tmtlr %s\n", ppc_reg(0));
  1020. output( "\taddi %s, %s, %d\n", ppc_reg(1), ppc_reg(1), 48+extra_stack_storage);
  1021. /* branch to ctr register. */
  1022. output( "\tbctr\n");
  1023. break;
  1024. }
  1025. output_cfi( ".cfi_endproc" );
  1026. output_function_size( "__wine_delay_load_asm" );
  1027. output( "\n" );
  1028. idx = 0;
  1029. LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
  1030. {
  1031. for (j = 0; j < import->nb_imports; j++)
  1032. {
  1033. struct import_func *func = &import->imports[j];
  1034. const char *name = func->name ? func->name : func->export_name;
  1035. if (thumb_mode) output( "\t.thumb_func\n" );
  1036. output( "__wine_delay_imp_%s_%s:\n", import->c_name, name );
  1037. output_cfi( ".cfi_startproc" );
  1038. switch(target_cpu)
  1039. {
  1040. case CPU_x86:
  1041. case CPU_x86_64:
  1042. output( "\tmovl $%d,%%eax\n", (idx << 16) | j );
  1043. output( "\tjmp %s\n", asm_name("__wine_delay_load_asm") );
  1044. break;
  1045. case CPU_ARM:
  1046. output( "\tmov ip, #%u\n", j );
  1047. if (idx) output( "\tmovt ip, #%u\n", idx );
  1048. output( "\tb %s\n", asm_name("__wine_delay_load_asm") );
  1049. break;
  1050. case CPU_ARM64:
  1051. if (idx)
  1052. {
  1053. output( "\tmov x16, #0x%x\n", idx << 16 );
  1054. if (j) output( "\tmovk x16, #0x%x\n", j );
  1055. }
  1056. else output( "\tmov x16, #0x%x\n", j );
  1057. output( "\tb %s\n", asm_name("__wine_delay_load_asm") );
  1058. break;
  1059. case CPU_POWERPC:
  1060. switch(target_platform)
  1061. {
  1062. case PLATFORM_APPLE:
  1063. /* On Darwin we can use r0 and r2 */
  1064. /* Upper part in r2 */
  1065. output( "\tlis %s, %d\n", ppc_reg(2), idx);
  1066. /* Lower part + r2 -> r0, Note we can't use r0 directly */
  1067. output( "\taddi %s, %s, %d\n", ppc_reg(0), ppc_reg(2), j);
  1068. output( "\tb %s\n", asm_name("__wine_delay_load_asm") );
  1069. break;
  1070. default:
  1071. /* On linux we can't use r2 since r2 is not a scratch register (hold the TOC) */
  1072. /* Save r13 on the stack */
  1073. output( "\taddi %s, %s, -0x4\n", ppc_reg(1), ppc_reg(1));
  1074. output( "\tstw %s, 0(%s)\n", ppc_reg(13), ppc_reg(1));
  1075. /* Upper part in r13 */
  1076. output( "\tlis %s, %d\n", ppc_reg(13), idx);
  1077. /* Lower part + r13 -> r0, Note we can't use r0 directly */
  1078. output( "\taddi %s, %s, %d\n", ppc_reg(0), ppc_reg(13), j);
  1079. /* Restore r13 */
  1080. output( "\tstw %s, 0(%s)\n", ppc_reg(13), ppc_reg(1));
  1081. output( "\taddic %s, %s, 0x4\n", ppc_reg(1), ppc_reg(1));
  1082. output( "\tb %s\n", asm_name("__wine_delay_load_asm") );
  1083. break;
  1084. }
  1085. break;
  1086. }
  1087. output_cfi( ".cfi_endproc" );
  1088. }
  1089. idx++;
  1090. }
  1091. output_function_size( delayed_import_loaders );
  1092. output( "\n\t.align %d\n", get_alignment(get_ptr_size()) );
  1093. output( "%s:\n", asm_name(delayed_import_thunks));
  1094. pos = 0;
  1095. LIST_FOR_EACH_ENTRY( import, &dll_delayed, struct import, entry )
  1096. {
  1097. for (j = 0; j < import->nb_imports; j++, pos += get_ptr_size())
  1098. {
  1099. struct import_func *func = &import->imports[j];
  1100. output_import_thunk( func->name ? func->name : func->export_name,
  1101. ".L__wine_delay_IAT", pos );
  1102. }
  1103. }
  1104. output_function_size( delayed_import_thunks );
  1105. }
  1106. /* output import stubs for exported entry points that link to external symbols */
  1107. static void output_external_link_imports( DLLSPEC *spec )
  1108. {
  1109. unsigned int i, pos;
  1110. if (!ext_link_imports.count) return; /* nothing to do */
  1111. sort_names( &ext_link_imports );
  1112. /* get rid of duplicate names */
  1113. for (i = 1; i < ext_link_imports.count; i++)
  1114. {
  1115. if (!strcmp( ext_link_imports.str[i-1], ext_link_imports.str[i] ))
  1116. remove_name( &ext_link_imports, i-- );
  1117. }
  1118. output( "\n/* external link thunks */\n\n" );
  1119. output( "\t.data\n" );
  1120. output( "\t.align %d\n", get_alignment(get_ptr_size()) );
  1121. output( ".L__wine_spec_external_links:\n" );
  1122. for (i = 0; i < ext_link_imports.count; i++)
  1123. output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name(ext_link_imports.str[i]) );
  1124. output( "\n\t.text\n" );
  1125. output( "\t.align %d\n", get_alignment(get_ptr_size()) );
  1126. output( "%s:\n", asm_name("__wine_spec_external_link_thunks") );
  1127. for (i = pos = 0; i < ext_link_imports.count; i++)
  1128. {
  1129. char *buffer = strmake( "__wine_spec_ext_link_%s", ext_link_imports.str[i] );
  1130. output_import_thunk( buffer, ".L__wine_spec_external_links", pos );
  1131. free( buffer );
  1132. pos += get_ptr_size();
  1133. }
  1134. output_function_size( "__wine_spec_external_link_thunks" );
  1135. }
  1136. /*******************************************************************
  1137. * output_stubs
  1138. *
  1139. * Output the functions for stub entry points
  1140. */
  1141. void output_stubs( DLLSPEC *spec )
  1142. {
  1143. const char *name, *exp_name;
  1144. int i;
  1145. if (!has_stubs( spec )) return;
  1146. output( "\n/* stub functions */\n\n" );
  1147. output( "\t.text\n" );
  1148. for (i = 0; i < spec->nb_entry_points; i++)
  1149. {
  1150. ORDDEF *odp = &spec->entry_points[i];
  1151. if (odp->type != TYPE_STUB) continue;
  1152. name = get_stub_name( odp, spec );
  1153. exp_name = odp->name ? odp->name : odp->export_name;
  1154. output( "\t.align %d\n", get_alignment(4) );
  1155. output( "\t%s\n", func_declaration(name) );
  1156. output( "%s:\n", asm_name(name) );
  1157. output_cfi( ".cfi_startproc" );
  1158. switch (target_cpu)
  1159. {
  1160. case CPU_x86:
  1161. /* flesh out the stub a bit to make safedisc happy */
  1162. output(" \tnop\n" );
  1163. output(" \tnop\n" );
  1164. output(" \tnop\n" );
  1165. output(" \tnop\n" );
  1166. output(" \tnop\n" );
  1167. output(" \tnop\n" );
  1168. output(" \tnop\n" );
  1169. output(" \tnop\n" );
  1170. output(" \tnop\n" );
  1171. output( "\tsubl $12,%%esp\n" );
  1172. output_cfi( ".cfi_adjust_cfa_offset 12" );
  1173. if (UsePIC)
  1174. {
  1175. output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
  1176. output( "1:" );
  1177. needs_get_pc_thunk = 1;
  1178. if (exp_name)
  1179. {
  1180. output( "\tleal .L%s_string-1b(%%eax),%%ecx\n", name );
  1181. output( "\tmovl %%ecx,4(%%esp)\n" );
  1182. }
  1183. else
  1184. output( "\tmovl $%d,4(%%esp)\n", odp->ordinal );
  1185. output( "\tleal .L__wine_spec_file_name-1b(%%eax),%%ecx\n" );
  1186. output( "\tmovl %%ecx,(%%esp)\n" );
  1187. }
  1188. else
  1189. {
  1190. if (exp_name)
  1191. output( "\tmovl $.L%s_string,4(%%esp)\n", name );
  1192. else
  1193. output( "\tmovl $%d,4(%%esp)\n", odp->ordinal );
  1194. output( "\tmovl $.L__wine_spec_file_name,(%%esp)\n" );
  1195. }
  1196. output( "\tcall %s\n", asm_name("__wine_spec_unimplemented_stub") );
  1197. break;
  1198. case CPU_x86_64:
  1199. output( "\tsubq $0x28,%%rsp\n" );
  1200. output_cfi( ".cfi_adjust_cfa_offset 8" );
  1201. output( "\tleaq .L__wine_spec_file_name(%%rip),%%rcx\n" );
  1202. if (exp_name)
  1203. output( "leaq .L%s_string(%%rip),%%rdx\n", name );
  1204. else
  1205. output( "\tmovq $%d,%%rdx\n", odp->ordinal );
  1206. output( "\tcall %s\n", asm_name("__wine_spec_unimplemented_stub") );
  1207. break;
  1208. case CPU_ARM:
  1209. if (UsePIC)
  1210. {
  1211. output( "\tldr r0,3f\n");
  1212. output( "1:\tadd r0,PC\n");
  1213. output( "\tldr r1,3f+4\n");
  1214. if (exp_name) output( "2:\tadd r1,PC\n");
  1215. output( "\tbl %s\n", asm_name("__wine_spec_unimplemented_stub") );
  1216. output( "3:\t.long .L__wine_spec_file_name-1b-%u\n", thumb_mode ? 4 : 8 );
  1217. if (exp_name) output( "\t.long .L%s_string-2b-%u\n", name, thumb_mode ? 4 : 8 );
  1218. else output( "\t.long %u\n", odp->ordinal );
  1219. }
  1220. else
  1221. {
  1222. output( "\tmovw r0,:lower16:.L__wine_spec_file_name\n");
  1223. output( "\tmovt r0,:upper16:.L__wine_spec_file_name\n");
  1224. if (exp_name)
  1225. {
  1226. output( "\tmovw r1,:lower16:.L%s_string\n", name );
  1227. output( "\tmovt r1,:upper16:.L%s_string\n", name );
  1228. }
  1229. else output( "\tmov r1,#%u\n", odp->ordinal );
  1230. output( "\tbl %s\n", asm_name("__wine_spec_unimplemented_stub") );
  1231. }
  1232. break;
  1233. case CPU_ARM64:
  1234. output( "\tadrp x0, %s\n", arm64_page(".L__wine_spec_file_name") );
  1235. output( "\tadd x0, x0, #%s\n", arm64_pageoff(".L__wine_spec_file_name") );
  1236. if (exp_name)
  1237. {
  1238. char *sym = strmake( ".L%s_string", name );
  1239. output( "\tadrp x1, %s\n", arm64_page( sym ) );
  1240. output( "\tadd x1, x1, #%s\n", arm64_pageoff( sym ) );
  1241. free( sym );
  1242. }
  1243. else
  1244. output( "\tmov x1, %u\n", odp->ordinal );
  1245. output( "\tbl %s\n", asm_name("__wine_spec_unimplemented_stub") );
  1246. break;
  1247. default:
  1248. assert(0);
  1249. }
  1250. output_cfi( ".cfi_endproc" );
  1251. output_function_size( name );
  1252. }
  1253. output( "\t%s\n", get_asm_string_section() );
  1254. output( ".L__wine_spec_file_name:\n" );
  1255. output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
  1256. for (i = 0; i < spec->nb_entry_points; i++)
  1257. {
  1258. ORDDEF *odp = &spec->entry_points[i];
  1259. if (odp->type != TYPE_STUB) continue;
  1260. exp_name = odp->name ? odp->name : odp->export_name;
  1261. if (exp_name)
  1262. {
  1263. name = get_stub_name( odp, spec );
  1264. output( ".L%s_string:\n", name );
  1265. output( "\t%s \"%s\"\n", get_asm_string_keyword(), exp_name );
  1266. }
  1267. }
  1268. }
  1269. static int cmp_link_name( const void *e1, const void *e2 )
  1270. {
  1271. const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
  1272. const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
  1273. return strcmp( odp1->link_name, odp2->link_name );
  1274. }
  1275. /* output dispatcher for system calls */
  1276. static void output_syscall_dispatcher(void)
  1277. {
  1278. const unsigned int invalid_param = 0xc000000d; /* STATUS_INVALID_PARAMETER */
  1279. unsigned int i;
  1280. output( "\t.align %d\n", get_alignment(4) );
  1281. output( "\t%s\n", func_declaration("__wine_syscall_dispatcher") );
  1282. output( "%s\n", asm_globl("__wine_syscall_dispatcher") );
  1283. output_cfi( ".cfi_startproc" );
  1284. switch (target_cpu)
  1285. {
  1286. case CPU_x86:
  1287. output( "\tmovl %%fs:0x1f8,%%ecx\n" ); /* x86_thread_data()->syscall_frame */
  1288. output( "\tmovl $0,0x00(%%ecx)\n" ); /* frame->restore_flags */
  1289. output( "\tpopl 0x08(%%ecx)\n" ); /* frame->eip */
  1290. output( "\tpushfl\n" );
  1291. output( "\tpopl 0x04(%%ecx)\n" ); /* frame->eflags */
  1292. output( "%s\n", asm_globl("__wine_syscall_dispatcher_prolog_end") );
  1293. output( "\tmovl %%esp,0x0c(%%ecx)\n" ); /* frame->esp */
  1294. output( "\tmovw %%cs,0x10(%%ecx)\n" );
  1295. output( "\tmovw %%ss,0x12(%%ecx)\n" );
  1296. output( "\tmovw %%ds,0x14(%%ecx)\n" );
  1297. output( "\tmovw %%es,0x16(%%ecx)\n" );
  1298. output( "\tmovw %%fs,0x18(%%ecx)\n" );
  1299. output( "\tmovw %%gs,0x1a(%%ecx)\n" );
  1300. output( "\tmovl %%eax,0x1c(%%ecx)\n" );
  1301. output( "\tmovl %%ebx,0x20(%%ecx)\n" );
  1302. output( "\tmovl %%edi,0x2c(%%ecx)\n" );
  1303. output( "\tmovl %%esi,0x30(%%ecx)\n" );
  1304. output( "\tmovl %%ebp,0x34(%%ecx)\n" );
  1305. output( "\tleal 0x34(%%ecx),%%ebp\n" );
  1306. output( "\tleal 4(%%esp),%%esi\n" ); /* first argument */
  1307. output( "\tmovl %%eax,%%edx\n" );
  1308. output( "\tshrl $8,%%edx\n" );
  1309. output( "\tandl $0x30,%%edx\n" ); /* syscall table number */
  1310. if (UsePIC)
  1311. {
  1312. output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
  1313. output( "1:\tleal %s-1b(%%eax,%%edx),%%ebx\n", asm_name("KeServiceDescriptorTable") );
  1314. needs_get_pc_thunk = 1;
  1315. }
  1316. else output( "\tleal %s(%%edx),%%ebx\n", asm_name("KeServiceDescriptorTable") );
  1317. output( "\ttestl $3,0x38(%%ecx)\n" ); /* SYSCALL_HAVE_XSAVE | SYSCALL_HAVE_XSAVEC */
  1318. output( "\tjz 2f\n" );
  1319. output( "\tmovl $7,%%eax\n" );
  1320. output( "\txorl %%edx,%%edx\n" );
  1321. for (i = 0; i < 6; i++) output( "\tmovl %%edx,0x%x(%%ecx)\n", 0x240 + i * 4 );
  1322. output( "\ttestl $2,0x38(%%ecx)\n" ); /* SYSCALL_HAVE_XSAVEC */
  1323. output( "\tjz 1f\n" );
  1324. for (i = 6; i < 16; i++) output( "\tmovl %%edx,0x%x(%%ecx)\n", 0x240 + i * 4 );
  1325. output( "\txsavec 0x40(%%ecx)\n" );
  1326. output( "\tjmp 4f\n" );
  1327. output( "1:\txsave 0x40(%%ecx)\n" );
  1328. output( "\tjmp 4f\n" );
  1329. output( "2:\ttestl $4,0x38(%%ecx)\n" ); /* SYSCALL_HAVE_FXSAVE */
  1330. output( "\tjz 3f\n" );
  1331. output( "\tfxsave 0x40(%%ecx)\n" );
  1332. output( "\tjmp 4f\n" );
  1333. output( "3:\tfnsave 0x40(%%ecx)\n" );
  1334. output( "\tfwait\n" );
  1335. output( "4:\tmovl %%ecx,%%esp\n" );
  1336. output( "\tmovl 0x1c(%%esp),%%edx\n" ); /* frame->eax */
  1337. output( "\tandl $0xfff,%%edx\n" ); /* syscall number */
  1338. output( "\tcmpl 8(%%ebx),%%edx\n" ); /* table->ServiceLimit */
  1339. output( "\tjae 6f\n" );
  1340. output( "\tmovl 12(%%ebx),%%eax\n" ); /* table->ArgumentTable */
  1341. output( "\tmovzbl (%%eax,%%edx,1),%%ecx\n" );
  1342. output( "\tmovl (%%ebx),%%eax\n" ); /* table->ServiceTable */
  1343. output( "\tsubl %%ecx,%%esp\n" );
  1344. output( "\tshrl $2,%%ecx\n" );
  1345. output( "\tandl $~15,%%esp\n" );
  1346. output( "\tmovl %%esp,%%edi\n" );
  1347. output( "\tcld\n" );
  1348. output( "\trep; movsl\n" );
  1349. output( "\tcall *(%%eax,%%edx,4)\n" );
  1350. output( "\tleal -0x34(%%ebp),%%esp\n" );
  1351. output( "5:\tmovl (%%esp),%%ecx\n" ); /* frame->restore_flags */
  1352. output( "\ttestl $0x68,%%ecx\n" ); /* CONTEXT_FLOATING_POINT | CONTEXT_EXTENDED_REGISTERS | CONTEXT_XSAVE */
  1353. output( "\tjz 3f\n" );
  1354. output( "\ttestl $3,0x38(%%esp)\n" ); /* SYSCALL_HAVE_XSAVE | SYSCALL_HAVE_XSAVEC */
  1355. output( "\tjz 1f\n" );
  1356. output( "\tmovl %%eax,%%esi\n" );
  1357. output( "\tmovl $7,%%eax\n" );
  1358. output( "\txorl %%edx,%%edx\n" );
  1359. output( "\txrstor 0x40(%%esp)\n" );
  1360. output( "\tmovl %%esi,%%eax\n" );
  1361. output( "\tjmp 3f\n" );
  1362. output( "1:\ttestl $4,0x38(%%esp)\n" ); /* SYSCALL_HAVE_FXSAVE */
  1363. output( "\tjz 2f\n" );
  1364. output( "\tfxrstor 0x40(%%esp)\n" );
  1365. output( "\tjmp 3f\n" );
  1366. output( "2:\tfrstor 0x40(%%esp)\n" );
  1367. output( "\tfwait\n" );
  1368. output( "3:\tmovl 0x2c(%%esp),%%edi\n" );
  1369. output( "\tmovl 0x30(%%esp),%%esi\n" );
  1370. output( "\tmovl 0x34(%%esp),%%ebp\n" );
  1371. output( "\ttestl $0x7,%%ecx\n" ); /* CONTEXT_CONTROL | CONTEXT_SEGMENTS | CONTEXT_INTEGER */
  1372. output( "\tjnz 1f\n" );
  1373. output( "\tmovl 0x20(%%esp),%%ebx\n" );
  1374. output( "\tmovl 0x08(%%esp),%%ecx\n" ); /* frame->eip */
  1375. output( "\tmovl 0x0c(%%esp),%%esp\n" ); /* frame->esp */
  1376. output( "\tjmpl *%%ecx\n" );
  1377. output( "1:\ttestl $0x2,%%ecx\n" ); /* CONTEXT_INTEGER */
  1378. output( "\tjz 1f\n" );
  1379. output( "\tmovl 0x1c(%%esp),%%eax\n" );
  1380. output( "\tmovl 0x24(%%esp),%%ecx\n" );
  1381. output( "\tmovl 0x28(%%esp),%%edx\n" );
  1382. output( "1:\tmovl 0x0c(%%esp),%%ebx\n" ); /* frame->esp */
  1383. output( "\tmovw 0x12(%%esp),%%ss\n" );
  1384. output( "\txchgl %%ebx,%%esp\n" );
  1385. output( "\tpushl 0x04(%%ebx)\n" ); /* frame->eflags */
  1386. output( "\tpushl 0x10(%%ebx)\n" ); /* frame->cs */
  1387. output( "\tpushl 0x08(%%ebx)\n" ); /* frame->eip */
  1388. output( "\tpushl 0x14(%%ebx)\n" ); /* frame->ds */
  1389. output( "\tmovw 0x16(%%ebx),%%es\n" );
  1390. output( "\tmovw 0x18(%%ebx),%%fs\n" );
  1391. output( "\tmovw 0x1a(%%ebx),%%gs\n" );
  1392. output( "\tmovl 0x20(%%ebx),%%ebx\n" );
  1393. output( "\tpopl %%ds\n" );
  1394. output( "\tiret\n" );
  1395. output( "6:\tmovl $0x%x,%%eax\n", invalid_param );
  1396. output( "\tjmp 5b\n" );
  1397. output( "%s\n", asm_globl("__wine_syscall_dispatcher_return") );
  1398. output( "\tmovl 8(%%esp),%%eax\n" );
  1399. output( "\tmovl 4(%%esp),%%esp\n" );
  1400. output( "\tjmp 5b\n" );
  1401. break;
  1402. case CPU_x86_64:
  1403. output( "\tmovq %%gs:0x30,%%rcx\n" );
  1404. output( "\tmovq 0x328(%%rcx),%%rcx\n" ); /* amd64_thread_data()->syscall_frame */
  1405. output( "\tpopq 0x70(%%rcx)\n" ); /* frame->rip */
  1406. output( "\tpushfq\n" );
  1407. output( "\tpopq 0x80(%%rcx)\n" );
  1408. output( "\tmovl $0,0x94(%%rcx)\n" ); /* frame->restore_flags */
  1409. output( "%s\n", asm_globl("__wine_syscall_dispatcher_prolog_end") );
  1410. output( "\tmovq %%rax,0x00(%%rcx)\n" );
  1411. output( "\tmovq %%rbx,0x08(%%rcx)\n" );
  1412. output( "\tmovq %%rdx,0x18(%%rcx)\n" );
  1413. output( "\tmovq %%rsi,0x20(%%rcx)\n" );
  1414. output( "\tmovq %%rdi,0x28(%%rcx)\n" );
  1415. output( "\tmovq %%r12,0x50(%%rcx)\n" );
  1416. output( "\tmovq %%r13,0x58(%%rcx)\n" );
  1417. output( "\tmovq %%r14,0x60(%%rcx)\n" );
  1418. output( "\tmovq %%r15,0x68(%%rcx)\n" );
  1419. output( "\tmovw %%cs,0x78(%%rcx)\n" );
  1420. output( "\tmovw %%ds,0x7a(%%rcx)\n" );
  1421. output( "\tmovw %%es,0x7c(%%rcx)\n" );
  1422. output( "\tmovw %%fs,0x7e(%%rcx)\n" );
  1423. output( "\tmovq %%rsp,0x88(%%rcx)\n" );
  1424. output( "\tmovw %%ss,0x90(%%rcx)\n" );
  1425. output( "\tmovw %%gs,0x92(%%rcx)\n" );
  1426. output( "\tmovq %%rbp,0x98(%%rcx)\n" );
  1427. /* Legends of Runeterra hooks the first system call return instruction, and
  1428. * depends on us returning to it. Adjust the return address accordingly. */
  1429. output( "\tsubq $0xb,0x70(%%rcx)\n" );
  1430. output( "\tmovl %s(%%rip),%%r14d\n", asm_name("__wine_syscall_flags") );
  1431. output( "\ttestl $3,%%r14d\n" ); /* SYSCALL_HAVE_XSAVE | SYSCALL_HAVE_XSAVEC */
  1432. output( "\tjz 2f\n" );
  1433. output( "\tmovl $7,%%eax\n" );
  1434. output( "\txorl %%edx,%%edx\n" );
  1435. output( "\tmovq %%rdx,0x2c0(%%rcx)\n" );
  1436. output( "\tmovq %%rdx,0x2c8(%%rcx)\n" );
  1437. output( "\tmovq %%rdx,0x2d0(%%rcx)\n" );
  1438. output( "\ttestl $2,%%r14d\n" ); /* SYSCALL_HAVE_XSAVEC */
  1439. output( "\tjz 1f\n" );
  1440. output( "\tmovq %%rdx,0x2d8(%%rcx)\n" );
  1441. output( "\tmovq %%rdx,0x2e0(%%rcx)\n" );
  1442. output( "\tmovq %%rdx,0x2e8(%%rcx)\n" );
  1443. output( "\tmovq %%rdx,0x2f0(%%rcx)\n" );
  1444. output( "\tmovq %%rdx,0x2f8(%%rcx)\n" );
  1445. output( "\txsavec64 0xc0(%%rcx)\n" );
  1446. output( "\tjmp 3f\n" );
  1447. output( "1:\txsave64 0xc0(%%rcx)\n" );
  1448. output( "\tjmp 3f\n" );
  1449. output( "2:\tfxsave64 0xc0(%%rcx)\n" );
  1450. output( "3:\tleaq 0x98(%%rcx),%%rbp\n" );
  1451. output( "\tleaq 0x28(%%rsp),%%rsi\n" ); /* first argument */
  1452. output( "\tmovq %%rcx,%%rsp\n" );
  1453. output( "\tmovq 0x00(%%rcx),%%rax\n" );
  1454. output( "\tmovq 0x18(%%rcx),%%rdx\n" );
  1455. output( "\tmovl %%eax,%%ebx\n" );
  1456. output( "\tshrl $8,%%ebx\n" );
  1457. output( "\tandl $0x30,%%ebx\n" ); /* syscall table number */
  1458. output( "\tleaq %s(%%rip),%%rcx\n", asm_name("KeServiceDescriptorTable") );
  1459. output( "\tleaq (%%rcx,%%rbx,2),%%rbx\n" );
  1460. output( "\tandl $0xfff,%%eax\n" ); /* syscall number */
  1461. output( "\tcmpq 16(%%rbx),%%rax\n" ); /* table->ServiceLimit */
  1462. output( "\tjae 5f\n" );
  1463. output( "\tmovq 24(%%rbx),%%rcx\n" ); /* table->ArgumentTable */
  1464. output( "\tmovzbl (%%rcx,%%rax),%%ecx\n" );
  1465. output( "\tsubq $0x20,%%rcx\n" );
  1466. output( "\tjbe 1f\n" );
  1467. output( "\tsubq %%rcx,%%rsp\n" );
  1468. output( "\tshrq $3,%%rcx\n" );
  1469. output( "\tandq $~15,%%rsp\n\t" );
  1470. output( "\tmovq %%rsp,%%rdi\n" );
  1471. output( "\tcld\n" );
  1472. output( "\trep; movsq\n" );
  1473. output( "1:\tmovq %%r10,%%rcx\n" );
  1474. output( "\tsubq $0x20,%%rsp\n" );
  1475. output( "\tmovq (%%rbx),%%r10\n" ); /* table->ServiceTable */
  1476. output( "\tcallq *(%%r10,%%rax,8)\n" );
  1477. output( "\tleaq -0x98(%%rbp),%%rcx\n" );
  1478. output( "2:\tmovl 0x94(%%rcx),%%edx\n" ); /* frame->restore_flags */
  1479. output( "\ttestl $0x48,%%edx\n" ); /* CONTEXT_FLOATING_POINT | CONTEXT_XSTATE */
  1480. output( "\tjz 4f\n" );
  1481. output( "\ttestl $3,%%r14d\n" ); /* SYSCALL_HAVE_XSAVE | SYSCALL_HAVE_XSAVEC */
  1482. output( "\tjz 3f\n" );
  1483. output( "\tmovq %%rax,%%r11\n" );
  1484. output( "\tmovl $7,%%eax\n" );
  1485. output( "\txorl %%edx,%%edx\n" );
  1486. output( "\txrstor64 0xc0(%%rcx)\n" );
  1487. output( "\tmovq %%r11,%%rax\n" );
  1488. output( "\tmovl 0x94(%%rcx),%%edx\n" );
  1489. output( "\tjmp 4f\n" );
  1490. output( "3:\tfxrstor64 0xc0(%%rcx)\n" );
  1491. output( "4:\tmovq 0x98(%%rcx),%%rbp\n" );
  1492. output( "\tmovq 0x68(%%rcx),%%r15\n" );
  1493. output( "\tmovq 0x60(%%rcx),%%r14\n" );
  1494. output( "\tmovq 0x58(%%rcx),%%r13\n" );
  1495. output( "\tmovq 0x50(%%rcx),%%r12\n" );
  1496. output( "\tmovq 0x28(%%rcx),%%rdi\n" );
  1497. output( "\tmovq 0x20(%%rcx),%%rsi\n" );
  1498. output( "\tmovq 0x08(%%rcx),%%rbx\n" );
  1499. output( "\ttestl $0x3,%%edx\n" ); /* CONTEXT_CONTROL | CONTEXT_INTEGER */
  1500. output( "\tjnz 1f\n" );
  1501. output( "\tmovq 0x88(%%rcx),%%rsp\n" );
  1502. output( "\tjmpq *0x70(%%rcx)\n" ); /* frame->rip */
  1503. output( "1:\tleaq 0x70(%%rcx),%%rsp\n" );
  1504. output( "\ttestl $0x2,%%edx\n" ); /* CONTEXT_INTEGER */
  1505. output( "\tjz 1f\n" );
  1506. output( "\tmovq 0x00(%%rcx),%%rax\n" );
  1507. output( "\tmovq 0x18(%%rcx),%%rdx\n" );
  1508. output( "\tmovq 0x30(%%rcx),%%r8\n" );
  1509. output( "\tmovq 0x38(%%rcx),%%r9\n" );
  1510. output( "\tmovq 0x40(%%rcx),%%r10\n" );
  1511. output( "\tmovq 0x48(%%rcx),%%r11\n" );
  1512. output( "\tmovq 0x10(%%rcx),%%rcx\n" );
  1513. output( "1:\tiretq\n" );
  1514. output( "5:\tmovl $0x%x,%%edx\n", invalid_param );
  1515. output( "\tmovq %%rsp,%%rcx\n" );
  1516. output( "%s\n", asm_globl("__wine_syscall_dispatcher_return") );
  1517. output( "\tmovl %s(%%rip),%%r14d\n", asm_name("__wine_syscall_flags") );
  1518. output( "\tmovq %%rdx,%%rax\n" );
  1519. output( "\tjmp 2b\n" );
  1520. break;
  1521. case CPU_ARM:
  1522. output( "\tmrc p15, 0, r1, c13, c0, 2\n" ); /* NtCurrentTeb() */
  1523. output( "\tldr r1, [r1, #0x1d8]\n" ); /* arm_thread_data()->syscall_frame */
  1524. output( "\tadd r0, r1, #0x10\n" );
  1525. output( "\tstm r0, {r4-r12,lr}\n" );
  1526. output( "\tstr sp, [r1, #0x38]\n" );
  1527. output( "\tstr r3, [r1, #0x3c]\n" );
  1528. output( "\tmrs r0, CPSR\n" );
  1529. output( "\tbfi r0, lr, #5, #1\n" ); /* set thumb bit */
  1530. output( "\tstr r0, [r1, #0x40]\n" );
  1531. output( "\tmov r0, #0\n" );
  1532. output( "\tstr r0, [r1, #0x44]\n" ); /* frame->restore_flags */
  1533. if (strcmp( float_abi_option, "soft" ))
  1534. {
  1535. output( "\tvmrs r0, fpscr\n" );
  1536. output( "\tstr r0, [r1, #0x48]\n" );
  1537. output( "\tadd r0, r1, #0x50\n" );
  1538. output( "\tvstm r0, {d0-d15}\n" );
  1539. }
  1540. output( "\tmov r6, sp\n" );
  1541. output( "\tmov sp, r1\n" );
  1542. output( "\tmov r8, r1\n" );
  1543. output( "\tldr r5, 6f\n");
  1544. if (UsePIC) output( "1:\tadd r5, pc\n");
  1545. output( "\tubfx r4, ip, #12, #2\n" ); /* syscall table number */
  1546. output( "\tbfc ip, #12, #20\n" ); /* syscall number */
  1547. output( "\tadd r4, r5, r4, lsl #4\n" );
  1548. output( "\tldr r5, [r4, #8]\n" ); /* table->ServiceLimit */
  1549. output( "\tcmp ip, r5\n" );
  1550. output( "\tbcs 5f\n" );
  1551. output( "\tldr r5, [r4, #12]\n" ); /* table->ArgumentTable */
  1552. output( "\tldrb r5, [r5, ip]\n" );
  1553. output( "\tcmp r5, #16\n" );
  1554. output( "\tit le\n" );
  1555. output( "\tmovle r5, #16\n" );
  1556. output( "\tsub r0, sp, r5\n" );
  1557. output( "\tand r0, #~7\n" );
  1558. output( "\tmov sp, r0\n" );
  1559. output( "2:\tsubs r5, r5, #4\n" );
  1560. output( "\tldr r0, [r6, r5]\n" );
  1561. output( "\tstr r0, [sp, r5]\n" );
  1562. output( "\tbgt 2b\n" );
  1563. output( "\tpop {r0-r3}\n" ); /* first 4 args are in registers */
  1564. output( "\tldr r5, [r4]\n"); /* table->ServiceTable */
  1565. output( "\tldr ip, [r5, ip, lsl #2]\n");
  1566. output( "\tblx ip\n");
  1567. output( "4:\tldr ip, [r8, #0x44]\n" ); /* frame->restore_flags */
  1568. if (strcmp( float_abi_option, "soft" ))
  1569. {
  1570. output( "\ttst ip, #4\n" ); /* CONTEXT_FLOATING_POINT */
  1571. output( "\tbeq 3f\n" );
  1572. output( "\tldr r4, [r8, #0x48]\n" );
  1573. output( "\tvmsr fpscr, r4\n" );
  1574. output( "\tadd r4, r8, #0x50\n" );
  1575. output( "\tvldm r4, {d0-d15}\n" );
  1576. output( "3:\n" );
  1577. }
  1578. output( "\ttst ip, #2\n" ); /* CONTEXT_INTEGER */
  1579. output( "\tit ne\n" );
  1580. output( "\tldmne r8, {r0-r3}\n" );
  1581. output( "\tldr lr, [r8, #0x3c]\n" );
  1582. output( "\tldr sp, [r8, #0x38]\n" );
  1583. output( "\tadd r8, r8, #0x10\n" );
  1584. output( "\tldm r8, {r4-r12,pc}\n" );
  1585. output( "5:\tmovw r0, #0x%x\n", invalid_param & 0xffff );
  1586. output( "\tmovt r0, #0x%x\n", invalid_param >> 16 );
  1587. output( "\tb 4b\n" );
  1588. output( "%s\n", asm_globl("__wine_syscall_dispatcher_return") );
  1589. output( "\tmov r8, r0\n" );
  1590. output( "\tmov r0, r1\n" );
  1591. output( "\tb 4b\n" );
  1592. if (UsePIC)
  1593. output( "6:\t.long %s-1b-%u\n", asm_name("KeServiceDescriptorTable"), thumb_mode ? 4 : 8 );
  1594. else
  1595. output( "6:\t.long %s\n", asm_name("KeServiceDescriptorTable") );
  1596. break;
  1597. case CPU_ARM64:
  1598. /* FIXME: use x18 directly instead */
  1599. output( "\tstp x0, x1, [sp, #-96]!\n" );
  1600. output( "\tstp x2, x3, [sp, #16]\n" );
  1601. output( "\tstp x4, x5, [sp, #32]\n" );
  1602. output( "\tstp x6, x7, [sp, #48]\n" );
  1603. output( "\tstp x8, x9, [sp, #64]\n" );
  1604. output( "\tstr lr, [sp, #80]\n" );
  1605. output( "\tbl %s\n", asm_name("NtCurrentTeb") );
  1606. output( "\tmov x18, x0\n" );
  1607. output( "\tldp x2, x3, [sp, #16]\n" );
  1608. output( "\tldp x4, x5, [sp, #32]\n" );
  1609. output( "\tldp x6, x7, [sp, #48]\n" );
  1610. output( "\tldp x8, x9, [sp, #64]\n" );
  1611. output( "\tldr lr, [sp, #80]\n" );
  1612. output( "\tldp x0, x1, [sp], #96\n" );
  1613. output( "\tldr x10, [x18, #0x2f8]\n" ); /* arm64_thread_data()->syscall_frame */
  1614. output( "\tstp x18, x19, [x10, #0x90]\n" );
  1615. output( "\tstp x20, x21, [x10, #0xa0]\n" );
  1616. output( "\tstp x22, x23, [x10, #0xb0]\n" );
  1617. output( "\tstp x24, x25, [x10, #0xc0]\n" );
  1618. output( "\tstp x26, x27, [x10, #0xd0]\n" );
  1619. output( "\tstp x28, x29, [x10, #0xe0]\n" );
  1620. output( "\tmov x19, sp\n" );
  1621. output( "\tstp x9, x19, [x10, #0xf0]\n" );
  1622. output( "\tmrs x9, NZCV\n" );
  1623. output( "\tstp lr, x9, [x10, #0x100]\n" );
  1624. output( "\tstr xzr, [x10, #0x110]\n" ); /* frame->restore_flags */
  1625. output( "\tmrs x9, FPCR\n" );
  1626. output( "\tstr w9, [x10, #0x118]\n" );
  1627. output( "\tmrs x9, FPSR\n" );
  1628. output( "\tstr w9, [x10, #0x11c]\n" );
  1629. output( "\tstp q0, q1, [x10, #0x120]\n" );
  1630. output( "\tstp q2, q3, [x10, #0x140]\n" );
  1631. output( "\tstp q4, q5, [x10, #0x160]\n" );
  1632. output( "\tstp q6, q7, [x10, #0x180]\n" );
  1633. output( "\tstp q8, q9, [x10, #0x1a0]\n" );
  1634. output( "\tstp q10, q11, [x10, #0x1c0]\n" );
  1635. output( "\tstp q12, q13, [x10, #0x1e0]\n" );
  1636. output( "\tstp q14, q15, [x10, #0x200]\n" );
  1637. output( "\tstp q16, q17, [x10, #0x220]\n" );
  1638. output( "\tstp q18, q19, [x10, #0x240]\n" );
  1639. output( "\tstp q20, q21, [x10, #0x260]\n" );
  1640. output( "\tstp q22, q23, [x10, #0x280]\n" );
  1641. output( "\tstp q24, q25, [x10, #0x2a0]\n" );
  1642. output( "\tstp q26, q27, [x10, #0x2c0]\n" );
  1643. output( "\tstp q28, q29, [x10, #0x2e0]\n" );
  1644. output( "\tstp q30, q31, [x10, #0x300]\n" );
  1645. output( "\tmov sp, x10\n" );
  1646. output( "\tand x20, x8, #0xfff\n" ); /* syscall number */
  1647. output( "\tubfx x21, x8, #12, #2\n" ); /* syscall table number */
  1648. output( "\tadrp x16, %s\n", arm64_page(asm_name("KeServiceDescriptorTable")) );
  1649. output( "\tadd x16, x16, #%s\n", arm64_pageoff(asm_name("KeServiceDescriptorTable")) );
  1650. output( "\tadd x21, x16, x21, lsl #5\n" );
  1651. output( "\tldr x16, [x21, #16]\n" ); /* table->ServiceLimit */
  1652. output( "\tcmp x20, x16\n" );
  1653. output( "\tbcs 4f\n" );
  1654. output( "\tmov x22, sp\n" );
  1655. output( "\tldr x16, [x21, #24]\n" ); /* table->ArgumentTable */
  1656. output( "\tldrb w9, [x16, x20]\n" );
  1657. output( "\tsubs x9, x9, #64\n" );
  1658. output( "\tbls 2f\n" );
  1659. output( "\tsub sp, sp, x9\n" );
  1660. output( "\ttbz x9, #3, 1f\n" );
  1661. output( "\tsub sp, sp, #8\n" );
  1662. output( "1:\tsub x9, x9, #8\n" );
  1663. output( "\tldr x10, [x19, x9]\n" );
  1664. output( "\tstr x10, [sp, x9]\n" );
  1665. output( "\tcbnz x9, 1b\n" );
  1666. output( "2:\tldr x16, [x21]\n" ); /* table->ServiceTable */
  1667. output( "\tldr x16, [x16, x20, lsl 3]\n" );
  1668. output( "\tblr x16\n" );
  1669. output( "\tmov sp, x22\n" );
  1670. output( "3:\tldp x18, x19, [sp, #0x90]\n" );
  1671. output( "\tldp x20, x21, [sp, #0xa0]\n" );
  1672. output( "\tldp x22, x23, [sp, #0xb0]\n" );
  1673. output( "\tldp x24, x25, [sp, #0xc0]\n" );
  1674. output( "\tldp x26, x27, [sp, #0xd0]\n" );
  1675. output( "\tldp x28, x29, [sp, #0xe0]\n" );
  1676. output( "\tldr x16, [sp, #0x110]\n" ); /* frame->restore_flags */
  1677. output( "\ttbz x16, #2, 1f\n" ); /* CONTEXT_FLOATING_POINT */
  1678. output( "\tldp q0, q1, [sp, #0x120]\n" );
  1679. output( "\tldp q2, q3, [sp, #0x140]\n" );
  1680. output( "\tldp q4, q5, [sp, #0x160]\n" );
  1681. output( "\tldp q6, q7, [sp, #0x180]\n" );
  1682. output( "\tldp q8, q9, [sp, #0x1a0]\n" );
  1683. output( "\tldp q10, q11, [sp, #0x1c0]\n" );
  1684. output( "\tldp q12, q13, [sp, #0x1e0]\n" );
  1685. output( "\tldp q14, q15, [sp, #0x200]\n" );
  1686. output( "\tldp q16, q17, [sp, #0x220]\n" );
  1687. output( "\tldp q18, q19, [sp, #0x240]\n" );
  1688. output( "\tldp q20, q21, [sp, #0x260]\n" );
  1689. output( "\tldp q22, q23, [sp, #0x280]\n" );
  1690. output( "\tldp q24, q25, [sp, #0x2a0]\n" );
  1691. output( "\tldp q26, q27, [sp, #0x2c0]\n" );
  1692. output( "\tldp q28, q29, [sp, #0x2e0]\n" );
  1693. output( "\tldp q30, q31, [sp, #0x300]\n" );
  1694. output( "\tldr w9, [sp, #0x118]\n" );
  1695. output( "\tmsr FPCR, x9\n" );
  1696. output( "\tldr w9, [sp, #0x11c]\n" );
  1697. output( "\tmsr FPSR, x9\n" );
  1698. output( "1:\ttbz x16, #1, 1f\n" ); /* CONTEXT_INTEGER */
  1699. output( "\tldp x0, x1, [sp, #0x00]\n" );
  1700. output( "\tldp x2, x3, [sp, #0x10]\n" );
  1701. output( "\tldp x4, x5, [sp, #0x20]\n" );
  1702. output( "\tldp x6, x7, [sp, #0x30]\n" );
  1703. output( "\tldp x8, x9, [sp, #0x40]\n" );
  1704. output( "\tldp x10, x11, [sp, #0x50]\n" );
  1705. output( "\tldp x12, x13, [sp, #0x60]\n" );
  1706. output( "\tldp x14, x15, [sp, #0x70]\n" );
  1707. output( "1:\tldp x16, x17, [sp, #0x100]\n" );
  1708. output( "\tmsr NZCV, x17\n" );
  1709. output( "\tldp x30, x17, [sp, #0xf0]\n" );
  1710. output( "\tmov sp, x17\n" );
  1711. output( "\tret x16\n" );
  1712. output( "4:\tmov x0, #0x%x\n", invalid_param & 0xffff0000 );
  1713. output( "\tmovk x0, #0x%x\n", invalid_param & 0x0000ffff );
  1714. output( "\tb 3b\n" );
  1715. output( "%s\n", asm_globl("__wine_syscall_dispatcher_return") );
  1716. output( "\tmov sp, x0\n" );
  1717. output( "\tmov x0, x1\n" );
  1718. output( "\tb 3b\n" );
  1719. break;
  1720. default:
  1721. assert(0);
  1722. }
  1723. output_cfi( ".cfi_endproc" );
  1724. output_function_size( "__wine_syscall_dispatcher" );
  1725. }
  1726. /* output the functions for system calls */
  1727. void output_syscalls( DLLSPEC *spec )
  1728. {
  1729. int i, count;
  1730. ORDDEF **syscalls = NULL;
  1731. for (i = count = 0; i < spec->nb_entry_points; i++)
  1732. {
  1733. ORDDEF *odp = &spec->entry_points[i];
  1734. if (!(odp->flags & FLAG_SYSCALL)) continue;
  1735. if (!syscalls) syscalls = xmalloc( (spec->nb_entry_points - i) * sizeof(*syscalls) );
  1736. syscalls[count++] = odp;
  1737. }
  1738. if (!count) return;
  1739. count = sort_func_list( syscalls, count, cmp_link_name );
  1740. output( "\n/* system calls */\n\n" );
  1741. output( "\t.text\n" );
  1742. if (unix_lib)
  1743. {
  1744. output_syscall_dispatcher();
  1745. output( "\t.data\n" );
  1746. output( "\t.align %d\n", get_alignment( get_ptr_size() ) );
  1747. output( "%s\n", asm_globl("KeServiceDescriptorTable") );
  1748. output( "\t%s .Lsyscall_table, 0, %u, .Lsyscall_args\n", get_asm_ptr_keyword(), count );
  1749. output( "\t%s 0, 0, 0, 0\n", get_asm_ptr_keyword() );
  1750. output( "\t%s 0, 0, 0, 0\n", get_asm_ptr_keyword() );
  1751. output( "\t%s 0, 0, 0, 0\n", get_asm_ptr_keyword() );
  1752. output( ".Lsyscall_table:\n" );
  1753. for (i = 0; i < count; i++)
  1754. output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name( get_link_name( syscalls[i] )));
  1755. output( ".Lsyscall_args:\n" );
  1756. for (i = 0; i < count; i++)
  1757. output( "\t.byte %u\n", get_args_size( syscalls[i] ));
  1758. output( "\t.align %d\n", get_alignment(4) );
  1759. output( "%s\n", asm_globl("__wine_syscall_flags") );
  1760. output( "\t.long 0\n" );
  1761. return;
  1762. }
  1763. for (i = 0; i < count; i++)
  1764. {
  1765. ORDDEF *odp = syscalls[i];
  1766. const char *name = get_link_name(odp);
  1767. output( "\t.align %d\n", get_alignment(16) );
  1768. output( "\t%s\n", func_declaration(name) );
  1769. output( "%s\n", asm_globl(name) );
  1770. output_cfi( ".cfi_startproc" );
  1771. switch (target_cpu)
  1772. {
  1773. case CPU_x86:
  1774. if (UsePIC)
  1775. {
  1776. output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
  1777. output( "1:\tmovl %s-1b(%%eax),%%edx\n", asm_name("__wine_syscall_dispatcher") );
  1778. output( "\tmovl $%u,%%eax\n", i );
  1779. needs_get_pc_thunk = 1;
  1780. }
  1781. else
  1782. {
  1783. output( "\tmovl $%u,%%eax\n", i );
  1784. output( "\tmovl $%s,%%edx\n", asm_name("__wine_syscall") );
  1785. }
  1786. output( "\tcall *%%edx\n" );
  1787. output( "\tret $%u\n", odp->type == TYPE_STDCALL ? get_args_size( odp ) : 0 );
  1788. break;
  1789. case CPU_x86_64:
  1790. /* Chromium depends on syscall thunks having the same form as on
  1791. * Windows. For 64-bit systems the only viable form we can emulate is
  1792. * having an int $0x2e fallback. Since actually using an interrupt is
  1793. * expensive, and since for some reason Chromium doesn't actually
  1794. * validate that instruction, we can just put a jmp there instead. */
  1795. output( "\t.byte 0x4c,0x8b,0xd1\n" ); /* movq %rcx,%r10 */
  1796. output( "\t.byte 0xb8\n" ); /* movl $i,%eax */
  1797. output( "\t.long %u\n", i );
  1798. output( "\t.byte 0xf6,0x04,0x25,0x08,0x03,0xfe,0x7f,0x01\n" ); /* testb $1,0x7ffe0308 */
  1799. output( "\t.byte 0x75,0x03\n" ); /* jne 1f */
  1800. output( "\t.byte 0x0f,0x05\n" ); /* syscall */
  1801. output( "\t.byte 0xc3\n" ); /* ret */
  1802. output( "\tjmp 1f\n" );
  1803. output( "\t.byte 0xc3\n" ); /* ret */
  1804. if (is_pe())
  1805. {
  1806. output( "1:\t.byte 0xff,0x14,0x25\n" ); /* 1: callq *(0x7ffe1000) */
  1807. output( "\t.long 0x7ffe1000\n" );
  1808. }
  1809. else
  1810. {
  1811. output( "\tnop\n" );
  1812. output( "1:\tcallq *%s(%%rip)\n", asm_name("__wine_syscall_dispatcher") );
  1813. }
  1814. output( "\tret\n" );
  1815. break;
  1816. case CPU_ARM:
  1817. output( "\tpush {r0-r3}\n" );
  1818. output( "\tmovw ip, #%u\n", i );
  1819. output( "\tmov r3, lr\n" );
  1820. output( "\tbl %s\n", asm_name("__wine_syscall") );
  1821. output( "\tadd sp, #16\n" );
  1822. output( "\tbx lr\n" );
  1823. break;
  1824. case CPU_ARM64:
  1825. output( "\tmov x8, #%u\n", i );
  1826. output( "\tmov x9, x30\n" );
  1827. output( "\tbl %s\n", asm_name("__wine_syscall" ));
  1828. output( "\tret\n" );
  1829. break;
  1830. default:
  1831. assert(0);
  1832. }
  1833. output_cfi( ".cfi_endproc" );
  1834. output_function_size( name );
  1835. }
  1836. switch (target_cpu)
  1837. {
  1838. case CPU_x86:
  1839. if (UsePIC) break;
  1840. output( "\t.align %d\n", get_alignment(16) );
  1841. output( "\t%s\n", func_declaration("__wine_syscall") );
  1842. output( "%s:\n", asm_name("__wine_syscall") );
  1843. output( "\tjmp *(%s)\n", asm_name("__wine_syscall_dispatcher") );
  1844. output_function_size( "__wine_syscall" );
  1845. break;
  1846. case CPU_ARM:
  1847. output( "\t.align %d\n", get_alignment(16) );
  1848. output( "\t%s\n", func_declaration("__wine_syscall") );
  1849. output( "%s:\n", asm_name("__wine_syscall") );
  1850. if (UsePIC)
  1851. {
  1852. output( "\tldr r0, 2f\n");
  1853. output( "1:\tadd r0, pc\n" );
  1854. }
  1855. else
  1856. {
  1857. output( "\tmovw r0, :lower16:%s\n", asm_name("__wine_syscall_dispatcher") );
  1858. output( "\tmovt r0, :upper16:%s\n", asm_name("__wine_syscall_dispatcher") );
  1859. }
  1860. output( "\tldr r0, [r0]\n");
  1861. output( "\tbx r0\n");
  1862. if (UsePIC) output( "2:\t.long %s-1b-%u\n", asm_name("__wine_syscall_dispatcher"), thumb_mode ? 4 : 8 );
  1863. output_function_size( "__wine_syscall" );
  1864. break;
  1865. case CPU_ARM64:
  1866. output( "\t.align %d\n", get_alignment(16) );
  1867. output( "\t%s\n", func_declaration("__wine_syscall") );
  1868. output( "%s:\n", asm_name("__wine_syscall") );
  1869. output( "\tadrp x16, %s\n", arm64_page( asm_name("__wine_syscall_dispatcher") ) );
  1870. output( "\tldr x16, [x16, #%s]\n", arm64_pageoff( asm_name("__wine_syscall_dispatcher") ) );
  1871. output( "\tbr x16\n");
  1872. output_function_size( "__wine_syscall" );
  1873. default:
  1874. break;
  1875. }
  1876. output( "\t.data\n" );
  1877. output( "\t.align %d\n", get_alignment( get_ptr_size() ) );
  1878. output( "%s\n", asm_globl("__wine_syscall_dispatcher") );
  1879. output( "\t%s 0\n", get_asm_ptr_keyword() );
  1880. }
  1881. /* output the import and delayed import tables of a Win32 module */
  1882. void output_imports( DLLSPEC *spec )
  1883. {
  1884. if (is_pe()) return;
  1885. output_immediate_imports();
  1886. output_delayed_imports( spec );
  1887. output_immediate_import_thunks();
  1888. output_delayed_import_thunks( spec );
  1889. output_external_link_imports( spec );
  1890. }
  1891. /* create a new asm temp file */
  1892. static void new_output_as_file(void)
  1893. {
  1894. char *name;
  1895. if (output_file) fclose( output_file );
  1896. name = open_temp_output_file( ".s" );
  1897. strarray_add( &as_files, name, NULL );
  1898. }
  1899. /* assemble all the asm files */
  1900. static void assemble_files( const char *prefix )
  1901. {
  1902. unsigned int i;
  1903. if (output_file) fclose( output_file );
  1904. output_file = NULL;
  1905. for (i = 0; i < as_files.count; i++)
  1906. {
  1907. char *obj = get_temp_file_name( prefix, ".o" );
  1908. assemble_file( as_files.str[i], obj );
  1909. as_files.str[i] = obj;
  1910. }
  1911. }
  1912. /* build a library from the current asm files and any additional object files in argv */
  1913. static void build_library( const char *output_name, char **argv, int create )
  1914. {
  1915. struct strarray args;
  1916. if (!create || target_platform != PLATFORM_WINDOWS)
  1917. {
  1918. args = find_tool( "ar", NULL );
  1919. strarray_add( &args, create ? "rc" : "r", output_name, NULL );
  1920. }
  1921. else
  1922. {
  1923. args = find_link_tool();
  1924. strarray_add( &args, "/lib", strmake( "-out:%s", output_name ), NULL );
  1925. }
  1926. strarray_addall( &args, as_files );
  1927. strarray_addv( &args, argv );
  1928. if (create) unlink( output_name );
  1929. spawn( args );
  1930. if (target_platform != PLATFORM_WINDOWS)
  1931. {
  1932. struct strarray ranlib = find_tool( "ranlib", NULL );
  1933. strarray_add( &ranlib, output_name, NULL );
  1934. spawn( ranlib );
  1935. }
  1936. }
  1937. /* create a Windows-style import library */
  1938. static void build_windows_import_lib( const char *lib_name, DLLSPEC *spec )
  1939. {
  1940. struct strarray args;
  1941. char *def_file;
  1942. const char *as_flags, *m_flag;
  1943. def_file = open_temp_output_file( ".def" );
  1944. output_def_file( spec, 1 );
  1945. fclose( output_file );
  1946. args = find_tool( "dlltool", NULL );
  1947. switch (target_cpu)
  1948. {
  1949. case CPU_x86:
  1950. m_flag = "i386";
  1951. as_flags = "--as-flags=--32";
  1952. break;
  1953. case CPU_x86_64:
  1954. m_flag = "i386:x86-64";
  1955. as_flags = "--as-flags=--64";
  1956. break;
  1957. case CPU_ARM:
  1958. m_flag = "arm";
  1959. as_flags = NULL;
  1960. break;
  1961. case CPU_ARM64:
  1962. m_flag = "arm64";
  1963. as_flags = NULL;
  1964. break;
  1965. default:
  1966. m_flag = NULL;
  1967. break;
  1968. }
  1969. strarray_add( &args, "-k", strendswith( lib_name, ".delay.a" ) ? "-y" : "-l",
  1970. lib_name, "-d", def_file, NULL );
  1971. if (m_flag)
  1972. strarray_add( &args, "-m", m_flag, as_flags, NULL );
  1973. spawn( args );
  1974. }
  1975. /* create a Unix-style import library */
  1976. static void build_unix_import_lib( DLLSPEC *spec )
  1977. {
  1978. static const char valid_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._@";
  1979. int i, total;
  1980. const char *name, *prefix;
  1981. char *dll_name = xstrdup( spec->file_name );
  1982. if (strendswith( dll_name, ".dll" )) dll_name[strlen(dll_name) - 4] = 0;
  1983. if (strspn( dll_name, valid_chars ) < strlen( dll_name ))
  1984. fatal_error( "%s contains invalid characters\n", spec->file_name );
  1985. /* entry points */
  1986. for (i = total = 0; i < spec->nb_entry_points; i++)
  1987. {
  1988. const ORDDEF *odp = &spec->entry_points[i];
  1989. if (odp->name) name = odp->name;
  1990. else if (odp->export_name) name = odp->export_name;
  1991. else continue;
  1992. if (odp->flags & FLAG_PRIVATE) continue;
  1993. total++;
  1994. /* C++ mangled names cannot be imported */
  1995. if (strpbrk( name, "?@" )) continue;
  1996. switch(odp->type)
  1997. {
  1998. case TYPE_VARARGS:
  1999. case TYPE_CDECL:
  2000. case TYPE_STDCALL:
  2001. prefix = (!odp->name || (odp->flags & FLAG_ORDINAL)) ? import_ord_prefix : import_func_prefix;
  2002. new_output_as_file();
  2003. output( "\t.text\n" );
  2004. output( "\n\t.align %d\n", get_alignment( get_ptr_size() ));
  2005. output( "\t%s\n", func_declaration( name ) );
  2006. output( "%s\n", asm_globl( name ) );
  2007. output( "\t%s %s%s$%u$%s\n", get_asm_ptr_keyword(),
  2008. asm_name( prefix ), dll_name, odp->ordinal, name );
  2009. output_function_size( name );
  2010. output_gnu_stack_note();
  2011. break;
  2012. default:
  2013. break;
  2014. }
  2015. }
  2016. if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
  2017. if (!as_files.count) /* create a dummy file to avoid empty import libraries */
  2018. {
  2019. new_output_as_file();
  2020. output( "\t.text\n" );
  2021. }
  2022. assemble_files( spec->file_name );
  2023. free( dll_name );
  2024. }
  2025. /* output an import library for a Win32 module and additional object files */
  2026. void output_static_lib( DLLSPEC *spec, char **argv )
  2027. {
  2028. if (is_pe())
  2029. {
  2030. if (spec) build_windows_import_lib( output_file_name, spec );
  2031. if (argv[0] || !spec) build_library( output_file_name, argv, !spec );
  2032. }
  2033. else
  2034. {
  2035. if (spec) build_unix_import_lib( spec );
  2036. build_library( output_file_name, argv, 1 );
  2037. }
  2038. }