import.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158
  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( int count, const char *variant )
  1277. {
  1278. const unsigned int invalid_param = 0xc000000d; /* STATUS_INVALID_PARAMETER */
  1279. const char *symbol = strmake( "__wine_syscall_dispatcher%s", variant );
  1280. unsigned int i;
  1281. output( "\t.align %d\n", get_alignment(4) );
  1282. output( "\t%s\n", func_declaration(symbol) );
  1283. output( "%s\n", asm_globl(symbol) );
  1284. output_cfi( ".cfi_startproc" );
  1285. switch (target_cpu)
  1286. {
  1287. case CPU_x86:
  1288. output( "\tpushl %%ebp\n" );
  1289. output_cfi( ".cfi_adjust_cfa_offset 4\n" );
  1290. output_cfi( ".cfi_rel_offset %%ebp,0\n" );
  1291. output( "\tmovl %%esp,%%ebp\n" );
  1292. output_cfi( ".cfi_def_cfa_register %%ebp\n" );
  1293. output( "\tleal -0x2c(%%esp),%%esp\n" );
  1294. output( "\tmovl %%ebx,-0x14(%%ebp)\n" );
  1295. output_cfi( ".cfi_rel_offset %%ebx,-0x14\n" );
  1296. output( "\tmovl %%edi,-0x08(%%ebp)\n" );
  1297. output_cfi( ".cfi_rel_offset %%edi,-0x08\n" );
  1298. output( "\tmovl %%esi,-0x04(%%ebp)\n" );
  1299. output_cfi( ".cfi_rel_offset %%esi,-0x04\n" );
  1300. output( "\tpushfl\n" );
  1301. output( "\tmovw %%gs,-0x1a(%%ebp)\n" );
  1302. output( "\tmovw %%fs,-0x1c(%%ebp)\n" );
  1303. output( "\tmovw %%es,-0x1e(%%ebp)\n" );
  1304. output( "\tmovw %%ds,-0x20(%%ebp)\n" );
  1305. output( "\tmovw %%ss,-0x22(%%ebp)\n" );
  1306. output( "\tmovw %%cs,-0x24(%%ebp)\n" );
  1307. output( "\tleal 8(%%ebp),%%ecx\n" );
  1308. output( "\tmovl %%ecx,-0x28(%%ebp)\n" ); /* frame->esp */
  1309. output( "\tmovl 4(%%ebp),%%ecx\n" );
  1310. output( "\tmovl %%ecx,-0x2c(%%ebp)\n" ); /* frame->eip */
  1311. output( "\tsubl $0x2c0,%%esp\n") ;
  1312. output( "\tandl $~63,%%esp\n" );
  1313. if (!*variant)
  1314. {
  1315. output( "\tfnsave (%%esp)\n" );
  1316. output( "\tfwait\n" );
  1317. }
  1318. else if(!strcmp( variant, "_fxsave" ))
  1319. {
  1320. output( "\tfxsave (%%esp)\n" );
  1321. }
  1322. else if(!strcmp( variant, "_xsave" ))
  1323. {
  1324. output( "\tmovl %%eax,%%ecx\n ");
  1325. output( "\tmovl $7,%%eax\n" );
  1326. output( "\txorl %%edx,%%edx\n" );
  1327. for (i = 0; i < 6; i++)
  1328. output( "\tmovl %%edx,0x%x(%%esp)\n", 0x200 + i * 4 );
  1329. output( "\txsave (%%esp)\n" );
  1330. output( "\tmovl %%ecx,%%eax\n ");
  1331. }
  1332. else /* _xsavec */
  1333. {
  1334. output( "\tmovl %%eax,%%ecx\n ");
  1335. output( "\tmovl $7,%%eax\n" );
  1336. output( "\txorl %%edx,%%edx\n" );
  1337. for (i = 0; i < 16; i++)
  1338. output( "\tmovl %%edx,0x%x(%%esp)\n", 0x200 + i * 4 );
  1339. output( "\txsavec (%%esp)\n" );
  1340. output( "\tmovl %%ecx,%%eax\n ");
  1341. }
  1342. output( "\tleal -0x30(%%ebp),%%ecx\n" );
  1343. output( "\tmovl %%ecx,%%fs:0x1f8\n" ); /* x86_thread_data()->syscall_frame */
  1344. output( "\tcmpl $%u,%%eax\n", count );
  1345. output( "\tjae 4f\n" );
  1346. if (UsePIC)
  1347. {
  1348. output( "\tmovl %%eax,%%edx\n" );
  1349. output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
  1350. output( "1:\tmovzbl .Lsyscall_args-1b(%%eax,%%edx,1),%%ecx\n" );
  1351. needs_get_pc_thunk = 1;
  1352. }
  1353. else output( "\tmovzbl .Lsyscall_args(%%eax),%%ecx\n" );
  1354. output( "\tsubl %%ecx,%%esp\n" );
  1355. output( "\tshrl $2,%%ecx\n" );
  1356. output( "\tleal 12(%%ebp),%%esi\n" );
  1357. output( "\tandl $~15,%%esp\n" );
  1358. output( "\tmovl %%esp,%%edi\n" );
  1359. output( "\tcld\n" );
  1360. output( "\trep; movsl\n" );
  1361. if (UsePIC)
  1362. output( "\tcall *.Lsyscall_table-1b(%%eax,%%edx,4)\n" );
  1363. else
  1364. output( "\tcall *.Lsyscall_table(,%%eax,4)\n" );
  1365. output( "2:\tmovl $0,%%fs:0x1f8\n" );
  1366. output( "\tleal -0x2f0(%%ebp),%%ebx\n") ;
  1367. output( "\tandl $~63,%%ebx\n" );
  1368. if (!*variant)
  1369. {
  1370. output( "\tfrstor (%%ebx)\n" );
  1371. output( "\tfwait\n" );
  1372. }
  1373. else if(!strcmp( variant, "_fxsave" ))
  1374. {
  1375. output( "\tfxrstor (%%ebx)\n" );
  1376. }
  1377. else
  1378. {
  1379. output( "\tmovl %%eax,%%ecx\n" );
  1380. output( "\tmovl $7,%%eax\n" );
  1381. output( "\txorl %%edx,%%edx\n" );
  1382. output( "\txrstor (%%ebx)\n" );
  1383. output( "\tmovl %%ecx,%%eax\n" );
  1384. }
  1385. output( "\tleal -0x30(%%ebp),%%ebx\n" );
  1386. output_cfi( ".cfi_def_cfa_register %%ebx" );
  1387. output_cfi( ".cfi_adjust_cfa_offset 0x30\n" );
  1388. output( "\tmovl %%eax,0x18(%%ebx)\n" );
  1389. output( "\tmovw 0x16(%%ebx),%%gs\n" );
  1390. output( "\tmovw 0x14(%%ebx),%%fs\n" );
  1391. output( "\tmovw 0x12(%%ebx),%%es\n" );
  1392. output( "\tmovl 0x28(%%ebx),%%edi\n" );
  1393. output_cfi( ".cfi_same_value %%edi" );
  1394. output( "\tmovl 0x2c(%%ebx),%%esi\n" );
  1395. output_cfi( ".cfi_same_value %%esi" );
  1396. output( "\tmovl (%%ebp),%%ebp\n" );
  1397. output_cfi( ".cfi_same_value %%ebp" );
  1398. output( "\tmovw %%ss,%%cx\n" );
  1399. output( "\tcmpw 0x0e(%%ebx),%%cx\n" );
  1400. output( "\tjne 3f\n" );
  1401. /* As soon as we have switched stacks the context structure could
  1402. * be invalid (when signal handlers are executed for example). Copy
  1403. * values on the target stack before changing ESP. */
  1404. output( "\tmovl 0x08(%%ebx),%%ecx\n" );
  1405. output( "\tleal -3*4(%%ecx),%%ecx\n" );
  1406. output( "\tmovl (%%ebx),%%edx\n" );
  1407. output( "\tmovl %%edx,2*4(%%ecx)\n" );
  1408. output( "\tmovl 0x0c(%%ebx),%%edx\n" );
  1409. output( "\tmovl %%edx,1*4(%%ecx)\n" );
  1410. output( "\tmovl 0x04(%%ebx),%%edx\n" );
  1411. output( "\tmovl %%edx,0*4(%%ecx)\n" );
  1412. output( "\tpushl 0x10(%%ebx)\n" );
  1413. output( "\tmovl 0x1c(%%ebx),%%ebx\n" );
  1414. output_cfi( ".cfi_same_value %%ebx" );
  1415. output( "\tpopl %%ds\n" );
  1416. output( "\tmovl %%ecx,%%esp\n" );
  1417. output( "\tiret\n" );
  1418. /* Restore the context when the stack segment changes. We can't use
  1419. * the same code as above because we do not know if the stack segment
  1420. * is 16 or 32 bit, and 'movl' will throw an exception when we try to
  1421. * access memory above the limit. */
  1422. output( "\t3:\tmovl 0x18(%%ebx),%%ecx\n" );
  1423. output( "\tmovw 0x0e(%%ebx),%%ss\n" );
  1424. output( "\tmovl 0x08(%%ebx),%%esp\n" );
  1425. output( "\tpushl 0x00(%%ebx)\n" );
  1426. output( "\tpushl 0x0c(%%ebx)\n" );
  1427. output( "\tpushl 0x04(%%ebx)\n" );
  1428. output( "\tpushl 0x10(%%ebx)\n" );
  1429. output( "\tmovl 0x1c(%%ebx),%%ebx\n" );
  1430. output( "\tpopl %%ds\n" );
  1431. output( "\tiret\n" );
  1432. output( "4:\tmovl $0x%x,%%eax\n", invalid_param );
  1433. output( "\tjmp 2b\n" );
  1434. break;
  1435. case CPU_x86_64:
  1436. output( "\tpushq %%rbp\n" );
  1437. output_cfi( ".cfi_adjust_cfa_offset 8" );
  1438. output_cfi( ".cfi_rel_offset %%rbp,0" );
  1439. output( "\tmovq %%rsp,%%rbp\n" );
  1440. output_cfi( ".cfi_def_cfa_register %%rbp" );
  1441. output( "\tleaq -0x10(%%rbp),%%rsp\n" );
  1442. output( "\tpushfq\n" );
  1443. output( "\tsubq $0x3c0,%%rsp\n" );
  1444. output( "\tandq $~63,%%rsp\n" );
  1445. output( "\tmovq %%rbx,-0x90(%%rbp)\n" );
  1446. output_cfi( ".cfi_rel_offset %%rbx,-144" );
  1447. output( "\tmovq %%rsi,-0x78(%%rbp)\n" );
  1448. output_cfi( ".cfi_rel_offset %%rsi,-120" );
  1449. output( "\tmovq %%rdi,-0x70(%%rbp)\n" );
  1450. output_cfi( ".cfi_rel_offset %%rdi,-112" );
  1451. output( "\tmovq %%r12,-0x48(%%rbp)\n" );
  1452. output_cfi( ".cfi_rel_offset %%r12,-72" );
  1453. output( "\tmovq %%r13,-0x40(%%rbp)\n" );
  1454. output( "\tmovq %%r14,-0x38(%%rbp)\n" );
  1455. output( "\tmovq %%r15,-0x30(%%rbp)\n" );
  1456. /* Legends of Runeterra hooks the first system call return instruction, and
  1457. * depends on us returning to it. Adjust the return address accordingly. */
  1458. output( "\tsubq $0xb,0x8(%%rbp)\n" );
  1459. output( "\tmovq 0x8(%%rbp),%%rbx\n" );
  1460. output( "\tmovq %%rbx,-0x28(%%rbp)\n" );
  1461. output( "\tleaq 0x10(%%rbp),%%rbx\n" );
  1462. output( "\tmovq %%rbx,-0x10(%%rbp)\n" );
  1463. output( "\tmovw %%cs,-0x20(%%rbp)\n" );
  1464. output( "\tmovw %%ds,-0x1e(%%rbp)\n" );
  1465. output( "\tmovw %%es,-0x1c(%%rbp)\n" );
  1466. output( "\tmovw %%fs,-0x1a(%%rbp)\n" );
  1467. output( "\tmovw %%ss,-0x8(%%rbp)\n" );
  1468. output( "\tmovw %%gs,-0x6(%%rbp)\n" );
  1469. output( "\tmovq %%rsp,%%r12\n" );
  1470. output( "\tmovq %%rax,%%r11\n" );
  1471. if (!*variant)
  1472. {
  1473. output( "\tfxsave64 (%%r12)\n" );
  1474. }
  1475. else
  1476. {
  1477. output( "\tmovl $7,%%eax\n" );
  1478. output( "\tmovq %%rdx,%%rsi\n" );
  1479. output( "\txorq %%rdx,%%rdx\n" );
  1480. output( "\tmovq %%rdx,0x200(%%r12)\n" );
  1481. output( "\tmovq %%rdx,0x208(%%r12)\n" );
  1482. output( "\tmovq %%rdx,0x210(%%r12)\n" );
  1483. if (!strcmp( variant, "_xsavec" ))
  1484. {
  1485. output( "\tmovq %%rdx,0x218(%%r12)\n" );
  1486. output( "\tmovq %%rdx,0x220(%%r12)\n" );
  1487. output( "\tmovq %%rdx,0x228(%%r12)\n" );
  1488. output( "\tmovq %%rdx,0x230(%%r12)\n" );
  1489. output( "\tmovq %%rdx,0x238(%%r12)\n" );
  1490. output( "\txsavec64 (%%r12)\n" );
  1491. }
  1492. else
  1493. output( "\txsave64 (%%r12)\n" );
  1494. output( "\tmovq %%rsi,%%rdx\n" );
  1495. }
  1496. output( "\tmovq %%gs:0x30,%%rcx\n" );
  1497. output( "\tleaq -0x98(%%rbp),%%rbx\n" );
  1498. output( "\tmovq %%rbx,0x328(%%rcx)\n" ); /* amd64_thread_data()->syscall_frame */
  1499. output( "\tcmpq $%u,%%r11\n", count );
  1500. output( "\tjae 3f\n" );
  1501. output( "\tleaq .Lsyscall_args(%%rip),%%rcx\n" );
  1502. output( "\tmovzbl (%%rcx,%%r11),%%ecx\n" );
  1503. output( "\tsubq $0x20,%%rcx\n" );
  1504. output( "\tjbe 1f\n" );
  1505. output( "\tsubq %%rcx,%%rsp\n" );
  1506. output( "\tshrq $3,%%rcx\n" );
  1507. output( "\tleaq 0x38(%%rbp),%%rsi\n" );
  1508. output( "\tandq $~15,%%rsp\n\t" );
  1509. output( "\tmovq %%rsp,%%rdi\n" );
  1510. output( "\tcld\n" );
  1511. output( "\trep; movsq\n" );
  1512. output( "1:\tmovq %%r10,%%rcx\n" );
  1513. output( "\tsubq $0x20,%%rsp\n" );
  1514. output( "\tleaq .Lsyscall_table(%%rip),%%r10\n" );
  1515. output( "\tcallq *(%%r10,%%r11,8)\n" );
  1516. output( "2:\tmovq %%gs:0x30,%%rcx\n" );
  1517. output( "\tmovq $0,0x328(%%rcx)\n" );
  1518. if (!*variant)
  1519. {
  1520. output( "\tfxrstor64 (%%r12)\n" );
  1521. }
  1522. else
  1523. {
  1524. output( "\tmovq %%rax,%%r11\n" );
  1525. output( "\tmovl $7,%%eax\n" );
  1526. output( "\txorq %%rdx,%%rdx\n" );
  1527. output( "\txrstor64 (%%r12)\n" );
  1528. output( "\tmovq %%r11,%%rax\n" );
  1529. }
  1530. output( "\tmovq -0x30(%%rbp),%%r15\n" );
  1531. output( "\tmovq -0x38(%%rbp),%%r14\n" );
  1532. output( "\tmovq -0x40(%%rbp),%%r13\n" );
  1533. output( "\tmovq -0x48(%%rbp),%%r12\n" );
  1534. output_cfi( ".cfi_same_value %%r12" );
  1535. output( "\tmovq -0x70(%%rbp),%%rdi\n" );
  1536. output_cfi( ".cfi_same_value %%rdi" );
  1537. output( "\tmovq -0x78(%%rbp),%%rsi\n" );
  1538. output_cfi( ".cfi_same_value %%rsi" );
  1539. output( "\tmovq -0x90(%%rbp),%%rbx\n" );
  1540. output_cfi( ".cfi_same_value %%rbx" );
  1541. output( "\tleaq -0x28(%%rbp),%%rsp\n" );
  1542. output_cfi( ".cfi_def_cfa_register %%rsp" );
  1543. output_cfi( ".cfi_adjust_cfa_offset 40" );
  1544. output( "\tmovq (%%rbp),%%rbp\n" );
  1545. output_cfi( ".cfi_same_value %%rbp" );
  1546. output( "\tiretq\n" );
  1547. output( "3:\tmovl $0x%x,%%eax\n", invalid_param );
  1548. output( "\tjmp 2b\n" );
  1549. break;
  1550. case CPU_ARM:
  1551. output( "\tpush {r5-r11,lr}\n" );
  1552. output( "\tadd r6, sp, #40\n" ); /* stack parameters */
  1553. output( "\tldr r5, 6f+8\n" );
  1554. output( "\tcmp r4, r5\n" );
  1555. output( "\tbcs 5f\n" );
  1556. output( "\tsub sp, sp, #8\n" );
  1557. output( "\tmrc p15, 0, r7, c13, c0, 2\n" ); /* NtCurrentTeb() */
  1558. output( "\tadd r7, #0x1d8\n" ); /* arm_thread_data()->syscall_frame */
  1559. output( "\tmrs ip, CPSR\n" );
  1560. output( "\tbfi ip, lr, #5, #1\n" ); /* set thumb bit */
  1561. output( "\tstr ip, [sp, #4]\n" );
  1562. output( "\tstr sp, [r7]\n" ); /* syscall frame */
  1563. output( "\tldr r5, 6f+4\n");
  1564. if (UsePIC) output( "1:\tadd r5, pc\n");
  1565. output( "\tldrb r5, [r5, r4]\n" ); /* syscall args */
  1566. output( "\tsubs r5, #16\n" ); /* first 4 args are in registers */
  1567. output( "\tble 3f\n" );
  1568. output( "\tsub ip, sp, r5\n" );
  1569. output( "\tand ip, #~7\n" );
  1570. output( "\tmov sp, ip\n" );
  1571. output( "2:\tsubs r5, r5, #4\n" );
  1572. output( "\tldr ip, [r6, r5]\n" );
  1573. output( "\tstr ip, [sp, r5]\n" );
  1574. output( "\tbgt 2b\n" );
  1575. output( "3:\tldr r5, 6f\n");
  1576. if (UsePIC) output( "4:\tadd r5, pc\n");
  1577. output( "\tldr ip, [r5, r4, lsl #2]\n"); /* syscall table */
  1578. output( "\tblx ip\n");
  1579. output( "\tmov ip, #0\n" );
  1580. output( "\tstr ip, [r7]\n" );
  1581. output( "\tsub ip, r6, #40\n" );
  1582. output( "\tmov sp, ip\n" );
  1583. output( "\tpop {r5-r11,pc}\n" );
  1584. output( "5:\tldr r0, 6f+12\n" );
  1585. output( "\tpop {r5-r11,pc}\n" );
  1586. if (UsePIC)
  1587. {
  1588. output( "6:\t.long .Lsyscall_table-4b-%u\n", thumb_mode ? 4 : 8 );
  1589. output( "\t.long .Lsyscall_args-1b-%u\n", thumb_mode ? 4 : 8 );
  1590. }
  1591. else
  1592. {
  1593. output( "6:\t.long .Lsyscall_table\n" );
  1594. output( "\t.long .Lsyscall_args\n" );
  1595. }
  1596. output( "\t.long %u\n", count );
  1597. output( "\t.long 0x%x\n", invalid_param );
  1598. break;
  1599. case CPU_ARM64:
  1600. output( "\tcmp x8, %u\n", count );
  1601. output( "\tbcs 3f\n" );
  1602. output( "\tstp x29, x30, [sp,#-160]!\n" );
  1603. output_cfi( "\t.cfi_def_cfa_offset 160\n" );
  1604. output_cfi( "\t.cfi_offset 29, -160\n" );
  1605. output_cfi( "\t.cfi_offset 30, -152\n" );
  1606. output( "\tmov x29, sp\n" );
  1607. output_cfi( "\t.cfi_def_cfa_register 29\n" );
  1608. output( "\tstp x27, x28, [sp, #144]\n" );
  1609. output_cfi( "\t.cfi_offset 27, -16\n" );
  1610. output_cfi( "\t.cfi_offset 28, -8\n" );
  1611. output( "\tstp x25, x26, [sp, #128]\n" );
  1612. output_cfi( "\t.cfi_offset 25, -32\n" );
  1613. output_cfi( "\t.cfi_offset 26, -24\n" );
  1614. output( "\tstp x23, x24, [sp, #112]\n" );
  1615. output_cfi( "\t.cfi_offset 23, -48\n" );
  1616. output_cfi( "\t.cfi_offset 24, -40\n" );
  1617. output( "\tstp x21, x22, [sp, #96]\n" );
  1618. output_cfi( "\t.cfi_offset 21, -64\n" );
  1619. output_cfi( "\t.cfi_offset 22, -56\n" );
  1620. output( "\tstp x19, x20, [sp, #80]\n" );
  1621. output_cfi( "\t.cfi_offset 19, -80\n" );
  1622. output_cfi( "\t.cfi_offset 20, -72\n" );
  1623. output( "\tstp x6, x7, [sp, #64]\n" );
  1624. output( "\tstp x4, x5, [sp, #48]\n" );
  1625. output( "\tstp x2, x3, [sp, #32]\n" );
  1626. output( "\tstp x0, x1, [sp, #16]\n" );
  1627. output( "\tmov x20, x8\n" );
  1628. output( "\tbl %s\n", asm_name("NtCurrentTeb") );
  1629. output( "\tadd x19, x0, #0x2f8\n" ); /* arm64_thread_data()->syscall_frame */
  1630. output( "\tstr x29, [x19]\n" );
  1631. output( "\tldp x0, x1, [sp, #16]\n" );
  1632. output( "\tldp x2, x3, [sp, #32]\n" );
  1633. output( "\tldp x4, x5, [sp, #48]\n" );
  1634. output( "\tldp x6, x7, [sp, #64]\n" );
  1635. output( "\tadrp x16, %s\n", arm64_page(".Lsyscall_args") );
  1636. output( "\tadd x16, x16, #%s\n", arm64_pageoff(".Lsyscall_args") );
  1637. output( "\tldrb w9, [x16, x20]\n" );
  1638. output( "\tsubs x9, x9, #64\n" );
  1639. output( "\tbls 2f\n" );
  1640. output( "\tadd x11, x29, #176\n" );
  1641. output( "\tsub sp, sp, x9\n" );
  1642. output( "\ttbz x9, #3, 1f\n" );
  1643. output( "\tsub sp, sp, #8\n" );
  1644. output( "1:\tsub x9, x9, #8\n" );
  1645. output( "\tldr x10, [x11, x9]\n" );
  1646. output( "\tstr x10, [sp, x9]\n" );
  1647. output( "\tcbnz x9, 1b\n" );
  1648. output( "2:\tadrp x16, %s\n", arm64_page(".Lsyscall_table") );
  1649. output( "\tadd x16, x16, #%s\n", arm64_pageoff(".Lsyscall_table") );
  1650. output( "\tldr x16, [x16, x20, lsl 3]\n" );
  1651. output( "\tblr x16\n" );
  1652. output( "\tmov sp, x29\n" );
  1653. output( "\tstr xzr, [x19]\n" );
  1654. output( "\tldp x19, x20, [sp, #80]\n" );
  1655. output( "\tldp x21, x22, [sp, #96]\n" );
  1656. output( "\tldp x23, x24, [sp, #112]\n" );
  1657. output( "\tldp x25, x26, [sp, #128]\n" );
  1658. output( "\tldp x27, x28, [sp, #144]\n" );
  1659. output( "\tldp x29, x30, [sp], #160\n" );
  1660. output( "\tret\n" );
  1661. output( "3:\tmov x0, #0x%x\n", invalid_param & 0xffff0000 );
  1662. output( "\tmovk x0, #0x%x\n", invalid_param & 0x0000ffff );
  1663. output( "\tret\n" );
  1664. break;
  1665. default:
  1666. assert(0);
  1667. }
  1668. output_cfi( ".cfi_endproc" );
  1669. output_function_size( symbol );
  1670. }
  1671. /* output the functions for system calls */
  1672. void output_syscalls( DLLSPEC *spec )
  1673. {
  1674. int i, count;
  1675. ORDDEF **syscalls = NULL;
  1676. for (i = count = 0; i < spec->nb_entry_points; i++)
  1677. {
  1678. ORDDEF *odp = &spec->entry_points[i];
  1679. if (!(odp->flags & FLAG_SYSCALL)) continue;
  1680. if (!syscalls) syscalls = xmalloc( (spec->nb_entry_points - i) * sizeof(*syscalls) );
  1681. syscalls[count++] = odp;
  1682. }
  1683. if (!count) return;
  1684. count = sort_func_list( syscalls, count, cmp_link_name );
  1685. output( "\n/* system calls */\n\n" );
  1686. output( "\t.text\n" );
  1687. if (unix_lib)
  1688. {
  1689. output_syscall_dispatcher( count, "" );
  1690. switch( target_cpu )
  1691. {
  1692. case CPU_x86:
  1693. output_syscall_dispatcher( count, "_fxsave" );
  1694. output_syscall_dispatcher( count, "_xsave" );
  1695. output_syscall_dispatcher( count, "_xsavec" );
  1696. break;
  1697. case CPU_x86_64:
  1698. output_syscall_dispatcher( count, "_xsave" );
  1699. output_syscall_dispatcher( count, "_xsavec" );
  1700. break;
  1701. default:
  1702. break;
  1703. }
  1704. output( "\t.data\n" );
  1705. output( "\t.align %d\n", get_alignment( get_ptr_size() ) );
  1706. output( ".Lsyscall_table:\n" );
  1707. for (i = 0; i < count; i++)
  1708. output( "\t%s %s\n", get_asm_ptr_keyword(), asm_name( get_link_name( syscalls[i] )));
  1709. output( ".Lsyscall_args:\n" );
  1710. for (i = 0; i < count; i++)
  1711. output( "\t.byte %u\n", get_args_size( syscalls[i] ));
  1712. return;
  1713. }
  1714. for (i = 0; i < count; i++)
  1715. {
  1716. ORDDEF *odp = syscalls[i];
  1717. const char *name = get_link_name(odp);
  1718. output( "\t.align %d\n", get_alignment(16) );
  1719. output( "\t%s\n", func_declaration(name) );
  1720. output( "%s\n", asm_globl(name) );
  1721. output_cfi( ".cfi_startproc" );
  1722. switch (target_cpu)
  1723. {
  1724. case CPU_x86:
  1725. if (UsePIC)
  1726. {
  1727. output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
  1728. output( "1:\tmovl %s-1b(%%eax),%%edx\n", asm_name("__wine_syscall_dispatcher") );
  1729. output( "\tmovl $%u,%%eax\n", i );
  1730. needs_get_pc_thunk = 1;
  1731. }
  1732. else
  1733. {
  1734. output( "\tmovl $%u,%%eax\n", i );
  1735. output( "\tmovl $%s,%%edx\n", asm_name("__wine_syscall") );
  1736. }
  1737. output( "\tcall *%%edx\n" );
  1738. output( "\tret $%u\n", odp->type == TYPE_STDCALL ? get_args_size( odp ) : 0 );
  1739. break;
  1740. case CPU_x86_64:
  1741. /* Chromium depends on syscall thunks having the same form as on
  1742. * Windows. For 64-bit systems the only viable form we can emulate is
  1743. * having an int $0x2e fallback. Since actually using an interrupt is
  1744. * expensive, and since for some reason Chromium doesn't actually
  1745. * validate that instruction, we can just put a jmp there instead. */
  1746. output( "\t.byte 0x4c,0x8b,0xd1\n" ); /* movq %rcx,%r10 */
  1747. output( "\t.byte 0xb8\n" ); /* movl $i,%eax */
  1748. output( "\t.long %u\n", i );
  1749. output( "\t.byte 0xf6,0x04,0x25,0x08,0x03,0xfe,0x7f,0x01\n" ); /* testb $1,0x7ffe0308 */
  1750. output( "\t.byte 0x75,0x03\n" ); /* jne 1f */
  1751. output( "\t.byte 0x0f,0x05\n" ); /* syscall */
  1752. output( "\t.byte 0xc3\n" ); /* ret */
  1753. output( "\tjmp 1f\n" );
  1754. output( "\t.byte 0xc3\n" ); /* ret */
  1755. if (is_pe())
  1756. {
  1757. output( "1:\t.byte 0xff,0x14,0x25\n" ); /* 1: callq *(0x7ffe1000) */
  1758. output( "\t.long 0x7ffe1000\n" );
  1759. }
  1760. else
  1761. {
  1762. output( "\tnop\n" );
  1763. output( "1:\tcallq *%s(%%rip)\n", asm_name("__wine_syscall_dispatcher") );
  1764. }
  1765. output( "\tret\n" );
  1766. break;
  1767. case CPU_ARM:
  1768. output( "\tpush {r4,lr}\n" );
  1769. output( "\tmov r4, #%u\n", i );
  1770. if (UsePIC)
  1771. {
  1772. output( "\tldr ip, 2f\n");
  1773. output( "1:\tadd ip, pc\n" );
  1774. }
  1775. else
  1776. {
  1777. output( "\tmovw ip, :lower16:%s\n", asm_name("__wine_syscall_dispatcher") );
  1778. output( "\tmovt ip, :upper16:%s\n", asm_name("__wine_syscall_dispatcher") );
  1779. }
  1780. output( "\tldr ip, [ip]\n");
  1781. output( "\tblx ip\n");
  1782. output( "\tpop {r4,pc}\n" );
  1783. if (UsePIC) output( "2:\t.long %s-1b-%u\n", asm_name("__wine_syscall_dispatcher"), thumb_mode ? 4 : 8 );
  1784. break;
  1785. case CPU_ARM64:
  1786. output( "\tstp x29, x30, [sp,#-16]!\n" );
  1787. output_cfi( "\t.cfi_def_cfa_offset 16\n" );
  1788. output_cfi( "\t.cfi_offset 29, -16\n" );
  1789. output_cfi( "\t.cfi_offset 30, -8\n" );
  1790. output( "\tmov x8, #%u\n", i );
  1791. output( "\tadrp x16, %s\n", arm64_page( asm_name("__wine_syscall_dispatcher") ) );
  1792. output( "\tldr x16, [x16, #%s]\n", arm64_pageoff( asm_name("__wine_syscall_dispatcher") ) );
  1793. output( "\tblr x16\n");
  1794. output( "\tldp x29, x30, [sp], #16\n" );
  1795. output( "\tret\n" );
  1796. break;
  1797. default:
  1798. assert(0);
  1799. }
  1800. output_cfi( ".cfi_endproc" );
  1801. output_function_size( name );
  1802. }
  1803. if (target_cpu == CPU_x86 && !UsePIC)
  1804. {
  1805. output( "\t.align %d\n", get_alignment(16) );
  1806. output( "\t%s\n", func_declaration("__wine_syscall") );
  1807. output( "%s:\n", asm_name("__wine_syscall") );
  1808. output( "\tjmp *(%s)\n", asm_name("__wine_syscall_dispatcher") );
  1809. output_function_size( "__wine_syscall" );
  1810. }
  1811. output( "\t.data\n" );
  1812. output( "\t.align %d\n", get_alignment( get_ptr_size() ) );
  1813. output( "%s\n", asm_globl("__wine_syscall_dispatcher") );
  1814. output( "\t%s 0\n", get_asm_ptr_keyword() );
  1815. }
  1816. /* output the import and delayed import tables of a Win32 module */
  1817. void output_imports( DLLSPEC *spec )
  1818. {
  1819. if (is_pe()) return;
  1820. output_immediate_imports();
  1821. output_delayed_imports( spec );
  1822. output_immediate_import_thunks();
  1823. output_delayed_import_thunks( spec );
  1824. output_external_link_imports( spec );
  1825. }
  1826. /* create a new asm temp file */
  1827. static void new_output_as_file(void)
  1828. {
  1829. char *name;
  1830. if (output_file) fclose( output_file );
  1831. name = open_temp_output_file( ".s" );
  1832. strarray_add( &as_files, name, NULL );
  1833. }
  1834. /* assemble all the asm files */
  1835. static void assemble_files( const char *prefix )
  1836. {
  1837. unsigned int i;
  1838. if (output_file) fclose( output_file );
  1839. output_file = NULL;
  1840. for (i = 0; i < as_files.count; i++)
  1841. {
  1842. char *obj = get_temp_file_name( prefix, ".o" );
  1843. assemble_file( as_files.str[i], obj );
  1844. as_files.str[i] = obj;
  1845. }
  1846. }
  1847. /* build a library from the current asm files and any additional object files in argv */
  1848. static void build_library( const char *output_name, char **argv, int create )
  1849. {
  1850. struct strarray args;
  1851. if (!create || target_platform != PLATFORM_WINDOWS)
  1852. {
  1853. args = find_tool( "ar", NULL );
  1854. strarray_add( &args, create ? "rc" : "r", output_name, NULL );
  1855. }
  1856. else
  1857. {
  1858. args = find_link_tool();
  1859. strarray_add( &args, "/lib", strmake( "-out:%s", output_name ), NULL );
  1860. }
  1861. strarray_addall( &args, as_files );
  1862. strarray_addv( &args, argv );
  1863. if (create) unlink( output_name );
  1864. spawn( args );
  1865. if (target_platform != PLATFORM_WINDOWS)
  1866. {
  1867. struct strarray ranlib = find_tool( "ranlib", NULL );
  1868. strarray_add( &ranlib, output_name, NULL );
  1869. spawn( ranlib );
  1870. }
  1871. }
  1872. /* create a Windows-style import library */
  1873. static void build_windows_import_lib( const char *lib_name, DLLSPEC *spec )
  1874. {
  1875. struct strarray args;
  1876. char *def_file;
  1877. const char *as_flags, *m_flag;
  1878. def_file = open_temp_output_file( ".def" );
  1879. output_def_file( spec, 1 );
  1880. fclose( output_file );
  1881. args = find_tool( "dlltool", NULL );
  1882. switch (target_cpu)
  1883. {
  1884. case CPU_x86:
  1885. m_flag = "i386";
  1886. as_flags = "--as-flags=--32";
  1887. break;
  1888. case CPU_x86_64:
  1889. m_flag = "i386:x86-64";
  1890. as_flags = "--as-flags=--64";
  1891. break;
  1892. case CPU_ARM:
  1893. m_flag = "arm";
  1894. as_flags = NULL;
  1895. break;
  1896. case CPU_ARM64:
  1897. m_flag = "arm64";
  1898. as_flags = NULL;
  1899. break;
  1900. default:
  1901. m_flag = NULL;
  1902. break;
  1903. }
  1904. strarray_add( &args, "-k", strendswith( lib_name, ".delay.a" ) ? "-y" : "-l",
  1905. lib_name, "-d", def_file, NULL );
  1906. if (m_flag)
  1907. strarray_add( &args, "-m", m_flag, as_flags, NULL );
  1908. spawn( args );
  1909. }
  1910. /* create a Unix-style import library */
  1911. static void build_unix_import_lib( DLLSPEC *spec )
  1912. {
  1913. static const char valid_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789._@";
  1914. int i, total;
  1915. const char *name, *prefix;
  1916. char *dll_name = xstrdup( spec->file_name );
  1917. if (strendswith( dll_name, ".dll" )) dll_name[strlen(dll_name) - 4] = 0;
  1918. if (strspn( dll_name, valid_chars ) < strlen( dll_name ))
  1919. fatal_error( "%s contains invalid characters\n", spec->file_name );
  1920. /* entry points */
  1921. for (i = total = 0; i < spec->nb_entry_points; i++)
  1922. {
  1923. const ORDDEF *odp = &spec->entry_points[i];
  1924. if (odp->name) name = odp->name;
  1925. else if (odp->export_name) name = odp->export_name;
  1926. else continue;
  1927. if (odp->flags & FLAG_PRIVATE) continue;
  1928. total++;
  1929. /* C++ mangled names cannot be imported */
  1930. if (strpbrk( name, "?@" )) continue;
  1931. switch(odp->type)
  1932. {
  1933. case TYPE_VARARGS:
  1934. case TYPE_CDECL:
  1935. case TYPE_STDCALL:
  1936. prefix = (!odp->name || (odp->flags & FLAG_ORDINAL)) ? import_ord_prefix : import_func_prefix;
  1937. new_output_as_file();
  1938. output( "\t.text\n" );
  1939. output( "\n\t.align %d\n", get_alignment( get_ptr_size() ));
  1940. output( "\t%s\n", func_declaration( name ) );
  1941. output( "%s\n", asm_globl( name ) );
  1942. output( "\t%s %s%s$%u$%s\n", get_asm_ptr_keyword(),
  1943. asm_name( prefix ), dll_name, odp->ordinal, name );
  1944. output_function_size( name );
  1945. output_gnu_stack_note();
  1946. break;
  1947. default:
  1948. break;
  1949. }
  1950. }
  1951. if (!total) warning( "%s: Import library doesn't export anything\n", spec->file_name );
  1952. if (!as_files.count) /* create a dummy file to avoid empty import libraries */
  1953. {
  1954. new_output_as_file();
  1955. output( "\t.text\n" );
  1956. }
  1957. assemble_files( spec->file_name );
  1958. free( dll_name );
  1959. }
  1960. /* output an import library for a Win32 module and additional object files */
  1961. void output_static_lib( DLLSPEC *spec, char **argv )
  1962. {
  1963. if (is_pe())
  1964. {
  1965. if (spec) build_windows_import_lib( output_file_name, spec );
  1966. if (argv[0] || !spec) build_library( output_file_name, argv, !spec );
  1967. }
  1968. else
  1969. {
  1970. if (spec) build_unix_import_lib( spec );
  1971. build_library( output_file_name, argv, 1 );
  1972. }
  1973. }